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.
17 #include "tree-iterator.h"
27 #include "statements.h"
31 #include "expressions.h"
36 Expression::Expression(Expression_classification classification
,
38 : classification_(classification
), location_(location
)
42 Expression::~Expression()
46 // Traverse the expressions.
49 Expression::traverse(Expression
** pexpr
, Traverse
* traverse
)
51 Expression
* expr
= *pexpr
;
52 if ((traverse
->traverse_mask() & Traverse::traverse_expressions
) != 0)
54 int t
= traverse
->expression(pexpr
);
55 if (t
== TRAVERSE_EXIT
)
57 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
58 return TRAVERSE_CONTINUE
;
60 return expr
->do_traverse(traverse
);
63 // Traverse subexpressions of this expression.
66 Expression::traverse_subexpressions(Traverse
* traverse
)
68 return this->do_traverse(traverse
);
71 // Default implementation for do_traverse for child classes.
74 Expression::do_traverse(Traverse
*)
76 return TRAVERSE_CONTINUE
;
79 // This virtual function is called by the parser if the value of this
80 // expression is being discarded. By default, we give an error.
81 // Expressions with side effects override.
84 Expression::do_discarding_value()
86 this->unused_value_error();
89 // This virtual function is called to export expressions. This will
90 // only be used by expressions which may be constant.
93 Expression::do_export(Export
*) const
98 // Give an error saying that the value of the expression is not used.
101 Expression::unused_value_error()
103 error_at(this->location(), "value computed is not used");
106 // Note that this expression is an error. This is called by children
107 // when they discover an error.
110 Expression::set_is_error()
112 this->classification_
= EXPRESSION_ERROR
;
115 // For children to call to report an error conveniently.
118 Expression::report_error(const char* msg
)
120 error_at(this->location_
, "%s", msg
);
121 this->set_is_error();
124 // Set types of variables and constants. This is implemented by the
128 Expression::determine_type(const Type_context
* context
)
130 this->do_determine_type(context
);
133 // Set types when there is no context.
136 Expression::determine_type_no_context()
138 Type_context context
;
139 this->do_determine_type(&context
);
142 // Return a tree handling any conversions which must be done during
146 Expression::convert_for_assignment(Translate_context
* context
, Type
* lhs_type
,
147 Type
* rhs_type
, tree rhs_tree
,
150 if (lhs_type
->is_error() || rhs_type
->is_error())
151 return error_mark_node
;
153 if (rhs_tree
== error_mark_node
|| TREE_TYPE(rhs_tree
) == error_mark_node
)
154 return error_mark_node
;
156 Gogo
* gogo
= context
->gogo();
158 tree lhs_type_tree
= type_to_tree(lhs_type
->get_backend(gogo
));
159 if (lhs_type_tree
== error_mark_node
)
160 return error_mark_node
;
162 if (lhs_type
->forwarded() != rhs_type
->forwarded()
163 && lhs_type
->interface_type() != NULL
)
165 if (rhs_type
->interface_type() == NULL
)
166 return Expression::convert_type_to_interface(context
, lhs_type
,
170 return Expression::convert_interface_to_interface(context
, lhs_type
,
174 else if (lhs_type
->forwarded() != rhs_type
->forwarded()
175 && rhs_type
->interface_type() != NULL
)
176 return Expression::convert_interface_to_type(context
, lhs_type
, rhs_type
,
178 else if (lhs_type
->is_slice_type() && rhs_type
->is_nil_type())
180 // Assigning nil to an open array.
181 go_assert(TREE_CODE(lhs_type_tree
) == RECORD_TYPE
);
183 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
185 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
186 tree field
= TYPE_FIELDS(lhs_type_tree
);
187 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
190 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
192 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
193 field
= DECL_CHAIN(field
);
194 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
197 elt
->value
= fold_convert(TREE_TYPE(field
), integer_zero_node
);
199 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
200 field
= DECL_CHAIN(field
);
201 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
204 elt
->value
= fold_convert(TREE_TYPE(field
), integer_zero_node
);
206 tree val
= build_constructor(lhs_type_tree
, init
);
207 TREE_CONSTANT(val
) = 1;
211 else if (rhs_type
->is_nil_type())
213 // The left hand side should be a pointer type at the tree
215 go_assert(POINTER_TYPE_P(lhs_type_tree
));
216 return fold_convert(lhs_type_tree
, null_pointer_node
);
218 else if (lhs_type_tree
== TREE_TYPE(rhs_tree
))
220 // No conversion is needed.
223 else if (POINTER_TYPE_P(lhs_type_tree
)
224 || INTEGRAL_TYPE_P(lhs_type_tree
)
225 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
226 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
227 return fold_convert_loc(location
.gcc_location(), lhs_type_tree
, rhs_tree
);
228 else if ((TREE_CODE(lhs_type_tree
) == RECORD_TYPE
229 && TREE_CODE(TREE_TYPE(rhs_tree
)) == RECORD_TYPE
)
230 || (TREE_CODE(lhs_type_tree
) == ARRAY_TYPE
231 && TREE_CODE(TREE_TYPE(rhs_tree
)) == ARRAY_TYPE
))
233 // Avoid confusion from zero sized variables which may be
234 // represented as non-zero-sized.
235 if (int_size_in_bytes(lhs_type_tree
) == 0
236 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
239 // This conversion must be permitted by Go, or we wouldn't have
241 go_assert(int_size_in_bytes(lhs_type_tree
)
242 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
243 return fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
244 lhs_type_tree
, rhs_tree
);
248 go_assert(useless_type_conversion_p(lhs_type_tree
, TREE_TYPE(rhs_tree
)));
253 // Return a tree for a conversion from a non-interface type to an
257 Expression::convert_type_to_interface(Translate_context
* context
,
258 Type
* lhs_type
, Type
* rhs_type
,
259 tree rhs_tree
, Location location
)
261 Gogo
* gogo
= context
->gogo();
262 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
263 bool lhs_is_empty
= lhs_interface_type
->is_empty();
265 // Since RHS_TYPE is a static type, we can create the interface
266 // method table at compile time.
268 // When setting an interface to nil, we just set both fields to
270 if (rhs_type
->is_nil_type())
272 Btype
* lhs_btype
= lhs_type
->get_backend(gogo
);
273 return expr_to_tree(gogo
->backend()->zero_expression(lhs_btype
));
276 // This should have been checked already.
277 go_assert(lhs_interface_type
->implements_interface(rhs_type
, NULL
));
279 tree lhs_type_tree
= type_to_tree(lhs_type
->get_backend(gogo
));
280 if (lhs_type_tree
== error_mark_node
)
281 return error_mark_node
;
283 // An interface is a tuple. If LHS_TYPE is an empty interface type,
284 // then the first field is the type descriptor for RHS_TYPE.
285 // Otherwise it is the interface method table for RHS_TYPE.
286 tree first_field_value
;
288 first_field_value
= rhs_type
->type_descriptor_pointer(gogo
, location
);
291 // Build the interface method table for this interface and this
292 // object type: a list of function pointers for each interface
294 Named_type
* rhs_named_type
= rhs_type
->named_type();
295 bool is_pointer
= false;
296 if (rhs_named_type
== NULL
)
298 rhs_named_type
= rhs_type
->deref()->named_type();
302 if (rhs_named_type
== NULL
)
303 method_table
= null_pointer_node
;
306 rhs_named_type
->interface_method_table(gogo
, lhs_interface_type
,
308 first_field_value
= fold_convert_loc(location
.gcc_location(),
309 const_ptr_type_node
, method_table
);
311 if (first_field_value
== error_mark_node
)
312 return error_mark_node
;
314 // Start building a constructor for the value we will return.
316 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
318 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
319 tree field
= TYPE_FIELDS(lhs_type_tree
);
320 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
321 (lhs_is_empty
? "__type_descriptor" : "__methods")) == 0);
323 elt
->value
= fold_convert_loc(location
.gcc_location(), TREE_TYPE(field
),
326 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
327 field
= DECL_CHAIN(field
);
328 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
331 if (rhs_type
->points_to() != NULL
)
333 // We are assigning a pointer to the interface; the interface
334 // holds the pointer itself.
335 elt
->value
= rhs_tree
;
336 return build_constructor(lhs_type_tree
, init
);
339 // We are assigning a non-pointer value to the interface; the
340 // interface gets a copy of the value in the heap.
342 tree object_size
= TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree
));
344 tree space
= gogo
->allocate_memory(rhs_type
, object_size
, location
);
345 space
= fold_convert_loc(location
.gcc_location(),
346 build_pointer_type(TREE_TYPE(rhs_tree
)), space
);
347 space
= save_expr(space
);
349 tree ref
= build_fold_indirect_ref_loc(location
.gcc_location(), space
);
350 TREE_THIS_NOTRAP(ref
) = 1;
351 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
352 void_type_node
, ref
, rhs_tree
);
354 elt
->value
= fold_convert_loc(location
.gcc_location(), TREE_TYPE(field
),
357 return build2(COMPOUND_EXPR
, lhs_type_tree
, set
,
358 build_constructor(lhs_type_tree
, init
));
361 // Return a tree for the type descriptor of RHS_TREE, which has
362 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
366 Expression::get_interface_type_descriptor(Translate_context
*,
367 Type
* rhs_type
, tree rhs_tree
,
370 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
371 go_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
372 tree rhs_field
= TYPE_FIELDS(rhs_type_tree
);
373 tree v
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
375 if (rhs_type
->interface_type()->is_empty())
377 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)),
378 "__type_descriptor") == 0);
382 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__methods")
384 go_assert(POINTER_TYPE_P(TREE_TYPE(v
)));
386 tree v1
= build_fold_indirect_ref_loc(location
.gcc_location(), v
);
387 go_assert(TREE_CODE(TREE_TYPE(v1
)) == RECORD_TYPE
);
388 tree f
= TYPE_FIELDS(TREE_TYPE(v1
));
389 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f
)), "__type_descriptor")
391 v1
= build3(COMPONENT_REF
, TREE_TYPE(f
), v1
, f
, NULL_TREE
);
393 tree eq
= fold_build2_loc(location
.gcc_location(), EQ_EXPR
, boolean_type_node
,
394 v
, fold_convert_loc(location
.gcc_location(),
397 tree n
= fold_convert_loc(location
.gcc_location(), TREE_TYPE(v1
),
399 return fold_build3_loc(location
.gcc_location(), COND_EXPR
, TREE_TYPE(v1
),
403 // Return a tree for the conversion of an interface type to an
407 Expression::convert_interface_to_interface(Translate_context
* context
,
408 Type
*lhs_type
, Type
*rhs_type
,
409 tree rhs_tree
, bool for_type_guard
,
412 Gogo
* gogo
= context
->gogo();
413 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
414 bool lhs_is_empty
= lhs_interface_type
->is_empty();
416 tree lhs_type_tree
= type_to_tree(lhs_type
->get_backend(gogo
));
417 if (lhs_type_tree
== error_mark_node
)
418 return error_mark_node
;
420 // In the general case this requires runtime examination of the type
421 // method table to match it up with the interface methods.
423 // FIXME: If all of the methods in the right hand side interface
424 // also appear in the left hand side interface, then we don't need
425 // to do a runtime check, although we still need to build a new
428 // Get the type descriptor for the right hand side. This will be
429 // NULL for a nil interface.
431 if (!DECL_P(rhs_tree
))
432 rhs_tree
= save_expr(rhs_tree
);
434 tree rhs_type_descriptor
=
435 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
438 // The result is going to be a two element constructor.
440 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
442 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
443 tree field
= TYPE_FIELDS(lhs_type_tree
);
448 // A type assertion fails when converting a nil interface.
449 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
,
451 static tree assert_interface_decl
;
452 tree call
= Gogo::call_builtin(&assert_interface_decl
,
454 "__go_assert_interface",
457 TREE_TYPE(lhs_type_descriptor
),
459 TREE_TYPE(rhs_type_descriptor
),
460 rhs_type_descriptor
);
461 if (call
== error_mark_node
)
462 return error_mark_node
;
463 // This will panic if the interface conversion fails.
464 TREE_NOTHROW(assert_interface_decl
) = 0;
465 elt
->value
= fold_convert_loc(location
.gcc_location(), TREE_TYPE(field
),
468 else if (lhs_is_empty
)
470 // A convertion to an empty interface always succeeds, and the
471 // first field is just the type descriptor of the object.
472 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
473 "__type_descriptor") == 0);
474 elt
->value
= fold_convert_loc(location
.gcc_location(),
475 TREE_TYPE(field
), rhs_type_descriptor
);
479 // A conversion to a non-empty interface may fail, but unlike a
480 // type assertion converting nil will always succeed.
481 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods")
483 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
,
485 static tree convert_interface_decl
;
486 tree call
= Gogo::call_builtin(&convert_interface_decl
,
488 "__go_convert_interface",
491 TREE_TYPE(lhs_type_descriptor
),
493 TREE_TYPE(rhs_type_descriptor
),
494 rhs_type_descriptor
);
495 if (call
== error_mark_node
)
496 return error_mark_node
;
497 // This will panic if the interface conversion fails.
498 TREE_NOTHROW(convert_interface_decl
) = 0;
499 elt
->value
= fold_convert_loc(location
.gcc_location(), TREE_TYPE(field
),
503 // The second field is simply the object pointer.
505 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
506 field
= DECL_CHAIN(field
);
507 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
510 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
511 go_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
512 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
513 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
514 elt
->value
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
517 return build_constructor(lhs_type_tree
, init
);
520 // Return a tree for the conversion of an interface type to a
521 // non-interface type.
524 Expression::convert_interface_to_type(Translate_context
* context
,
525 Type
*lhs_type
, Type
* rhs_type
,
526 tree rhs_tree
, Location location
)
528 Gogo
* gogo
= context
->gogo();
529 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
531 tree lhs_type_tree
= type_to_tree(lhs_type
->get_backend(gogo
));
532 if (lhs_type_tree
== error_mark_node
)
533 return error_mark_node
;
535 // Call a function to check that the type is valid. The function
536 // will panic with an appropriate runtime type error if the type is
539 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
, location
);
541 if (!DECL_P(rhs_tree
))
542 rhs_tree
= save_expr(rhs_tree
);
544 tree rhs_type_descriptor
=
545 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
548 tree rhs_inter_descriptor
= rhs_type
->type_descriptor_pointer(gogo
,
551 static tree check_interface_type_decl
;
552 tree call
= Gogo::call_builtin(&check_interface_type_decl
,
554 "__go_check_interface_type",
557 TREE_TYPE(lhs_type_descriptor
),
559 TREE_TYPE(rhs_type_descriptor
),
561 TREE_TYPE(rhs_inter_descriptor
),
562 rhs_inter_descriptor
);
563 if (call
== error_mark_node
)
564 return error_mark_node
;
565 // This call will panic if the conversion is invalid.
566 TREE_NOTHROW(check_interface_type_decl
) = 0;
568 // If the call succeeds, pull out the value.
569 go_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
570 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
571 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
572 tree val
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
575 // If the value is a pointer, then it is the value we want.
576 // Otherwise it points to the value.
577 if (lhs_type
->points_to() == NULL
)
579 val
= fold_convert_loc(location
.gcc_location(),
580 build_pointer_type(lhs_type_tree
), val
);
581 val
= build_fold_indirect_ref_loc(location
.gcc_location(), val
);
584 return build2(COMPOUND_EXPR
, lhs_type_tree
, call
,
585 fold_convert_loc(location
.gcc_location(), lhs_type_tree
, val
));
588 // Convert an expression to a tree. This is implemented by the child
589 // class. Not that it is not in general safe to call this multiple
590 // times for a single expression, but that we don't catch such errors.
593 Expression::get_tree(Translate_context
* context
)
595 // The child may have marked this expression as having an error.
596 if (this->classification_
== EXPRESSION_ERROR
)
597 return error_mark_node
;
599 return this->do_get_tree(context
);
602 // Return a tree for VAL in TYPE.
605 Expression::integer_constant_tree(mpz_t val
, tree type
)
607 if (type
== error_mark_node
)
608 return error_mark_node
;
609 else if (TREE_CODE(type
) == INTEGER_TYPE
)
610 return double_int_to_tree(type
,
611 mpz_get_double_int(type
, val
, true));
612 else if (TREE_CODE(type
) == REAL_TYPE
)
615 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
616 tree ret
= Expression::float_constant_tree(fval
, type
);
620 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
623 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
624 tree real
= Expression::float_constant_tree(fval
, TREE_TYPE(type
));
626 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
628 return build_complex(type
, real
, imag
);
634 // Return a tree for VAL in TYPE.
637 Expression::float_constant_tree(mpfr_t val
, tree type
)
639 if (type
== error_mark_node
)
640 return error_mark_node
;
641 else if (TREE_CODE(type
) == INTEGER_TYPE
)
645 mpfr_get_z(ival
, val
, GMP_RNDN
);
646 tree ret
= Expression::integer_constant_tree(ival
, type
);
650 else if (TREE_CODE(type
) == REAL_TYPE
)
653 real_from_mpfr(&r1
, val
, type
, GMP_RNDN
);
655 real_convert(&r2
, TYPE_MODE(type
), &r1
);
656 return build_real(type
, r2
);
658 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
661 real_from_mpfr(&r1
, val
, TREE_TYPE(type
), GMP_RNDN
);
663 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
664 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
666 return build_complex(type
, build_real(TREE_TYPE(type
), r2
), imag
);
672 // Return a tree for REAL/IMAG in TYPE.
675 Expression::complex_constant_tree(mpfr_t real
, mpfr_t imag
, tree type
)
677 if (type
== error_mark_node
)
678 return error_mark_node
;
679 else if (TREE_CODE(type
) == INTEGER_TYPE
|| TREE_CODE(type
) == REAL_TYPE
)
680 return Expression::float_constant_tree(real
, type
);
681 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
684 real_from_mpfr(&r1
, real
, TREE_TYPE(type
), GMP_RNDN
);
686 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
689 real_from_mpfr(&r3
, imag
, TREE_TYPE(type
), GMP_RNDN
);
691 real_convert(&r4
, TYPE_MODE(TREE_TYPE(type
)), &r3
);
693 return build_complex(type
, build_real(TREE_TYPE(type
), r2
),
694 build_real(TREE_TYPE(type
), r4
));
700 // Return a tree which evaluates to true if VAL, of arbitrary integer
701 // type, is negative or is more than the maximum value of BOUND_TYPE.
702 // If SOFAR is not NULL, it is or'red into the result. The return
703 // value may be NULL if SOFAR is NULL.
706 Expression::check_bounds(tree val
, tree bound_type
, tree sofar
,
709 tree val_type
= TREE_TYPE(val
);
710 tree ret
= NULL_TREE
;
712 if (!TYPE_UNSIGNED(val_type
))
714 ret
= fold_build2_loc(loc
.gcc_location(), LT_EXPR
, boolean_type_node
, val
,
715 build_int_cst(val_type
, 0));
716 if (ret
== boolean_false_node
)
720 HOST_WIDE_INT val_type_size
= int_size_in_bytes(val_type
);
721 HOST_WIDE_INT bound_type_size
= int_size_in_bytes(bound_type
);
722 go_assert(val_type_size
!= -1 && bound_type_size
!= -1);
723 if (val_type_size
> bound_type_size
724 || (val_type_size
== bound_type_size
725 && TYPE_UNSIGNED(val_type
)
726 && !TYPE_UNSIGNED(bound_type
)))
728 tree max
= TYPE_MAX_VALUE(bound_type
);
729 tree big
= fold_build2_loc(loc
.gcc_location(), GT_EXPR
, boolean_type_node
,
730 val
, fold_convert_loc(loc
.gcc_location(),
732 if (big
== boolean_false_node
)
734 else if (ret
== NULL_TREE
)
737 ret
= fold_build2_loc(loc
.gcc_location(), TRUTH_OR_EXPR
,
738 boolean_type_node
, ret
, big
);
741 if (ret
== NULL_TREE
)
743 else if (sofar
== NULL_TREE
)
746 return fold_build2_loc(loc
.gcc_location(), TRUTH_OR_EXPR
, boolean_type_node
,
751 Expression::dump_expression(Ast_dump_context
* ast_dump_context
) const
753 this->do_dump_expression(ast_dump_context
);
756 // Error expressions. This are used to avoid cascading errors.
758 class Error_expression
: public Expression
761 Error_expression(Location location
)
762 : Expression(EXPRESSION_ERROR
, location
)
767 do_is_constant() const
771 do_numeric_constant_value(Numeric_constant
* nc
) const
773 nc
->set_unsigned_long(NULL
, 0);
778 do_discarding_value()
783 { return Type::make_error_type(); }
786 do_determine_type(const Type_context
*)
794 do_is_addressable() const
798 do_get_tree(Translate_context
*)
799 { return error_mark_node
; }
802 do_dump_expression(Ast_dump_context
*) const;
805 // Dump the ast representation for an error expression to a dump context.
808 Error_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
810 ast_dump_context
->ostream() << "_Error_" ;
814 Expression::make_error(Location location
)
816 return new Error_expression(location
);
819 // An expression which is really a type. This is used during parsing.
820 // It is an error if these survive after lowering.
823 Type_expression
: public Expression
826 Type_expression(Type
* type
, Location location
)
827 : Expression(EXPRESSION_TYPE
, location
),
833 do_traverse(Traverse
* traverse
)
834 { return Type::traverse(this->type_
, traverse
); }
838 { return this->type_
; }
841 do_determine_type(const Type_context
*)
845 do_check_types(Gogo
*)
846 { this->report_error(_("invalid use of type")); }
853 do_get_tree(Translate_context
*)
854 { go_unreachable(); }
856 void do_dump_expression(Ast_dump_context
*) const;
859 // The type which we are representing as an expression.
864 Type_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
866 ast_dump_context
->dump_type(this->type_
);
870 Expression::make_type(Type
* type
, Location location
)
872 return new Type_expression(type
, location
);
875 // Class Parser_expression.
878 Parser_expression::do_type()
880 // We should never really ask for the type of a Parser_expression.
881 // However, it can happen, at least when we have an invalid const
882 // whose initializer refers to the const itself. In that case we
883 // may ask for the type when lowering the const itself.
884 go_assert(saw_errors());
885 return Type::make_error_type();
888 // Class Var_expression.
890 // Lower a variable expression. Here we just make sure that the
891 // initialization expression of the variable has been lowered. This
892 // ensures that we will be able to determine the type of the variable
896 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
897 Statement_inserter
* inserter
, int)
899 if (this->variable_
->is_variable())
901 Variable
* var
= this->variable_
->var_value();
902 // This is either a local variable or a global variable. A
903 // reference to a variable which is local to an enclosing
904 // function will be a reference to a field in a closure.
905 if (var
->is_global())
910 var
->lower_init_expression(gogo
, function
, inserter
);
915 // Return the type of a reference to a variable.
918 Var_expression::do_type()
920 if (this->variable_
->is_variable())
921 return this->variable_
->var_value()->type();
922 else if (this->variable_
->is_result_variable())
923 return this->variable_
->result_var_value()->type();
928 // Determine the type of a reference to a variable.
931 Var_expression::do_determine_type(const Type_context
*)
933 if (this->variable_
->is_variable())
934 this->variable_
->var_value()->determine_type();
937 // Something takes the address of this variable. This means that we
938 // may want to move the variable onto the heap.
941 Var_expression::do_address_taken(bool escapes
)
945 if (this->variable_
->is_variable())
946 this->variable_
->var_value()->set_non_escaping_address_taken();
947 else if (this->variable_
->is_result_variable())
948 this->variable_
->result_var_value()->set_non_escaping_address_taken();
954 if (this->variable_
->is_variable())
955 this->variable_
->var_value()->set_address_taken();
956 else if (this->variable_
->is_result_variable())
957 this->variable_
->result_var_value()->set_address_taken();
963 // Get the tree for a reference to a variable.
966 Var_expression::do_get_tree(Translate_context
* context
)
968 Bvariable
* bvar
= this->variable_
->get_backend_variable(context
->gogo(),
969 context
->function());
970 tree ret
= var_to_tree(bvar
);
971 if (ret
== error_mark_node
)
972 return error_mark_node
;
974 if (this->variable_
->is_variable())
975 is_in_heap
= this->variable_
->var_value()->is_in_heap();
976 else if (this->variable_
->is_result_variable())
977 is_in_heap
= this->variable_
->result_var_value()->is_in_heap();
982 ret
= build_fold_indirect_ref_loc(this->location().gcc_location(), ret
);
983 TREE_THIS_NOTRAP(ret
) = 1;
988 // Ast dump for variable expression.
991 Var_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
993 ast_dump_context
->ostream() << this->variable_
->name() ;
996 // Make a reference to a variable in an expression.
999 Expression::make_var_reference(Named_object
* var
, Location location
)
1002 return Expression::make_sink(location
);
1004 // FIXME: Creating a new object for each reference to a variable is
1006 return new Var_expression(var
, location
);
1009 // Class Temporary_reference_expression.
1014 Temporary_reference_expression::do_type()
1016 return this->statement_
->type();
1019 // Called if something takes the address of this temporary variable.
1020 // We never have to move temporary variables to the heap, but we do
1021 // need to know that they must live in the stack rather than in a
1025 Temporary_reference_expression::do_address_taken(bool)
1027 this->statement_
->set_is_address_taken();
1030 // Get a tree referring to the variable.
1033 Temporary_reference_expression::do_get_tree(Translate_context
* context
)
1035 Bvariable
* bvar
= this->statement_
->get_backend_variable(context
);
1037 // The gcc backend can't represent the same set of recursive types
1038 // that the Go frontend can. In some cases this means that a
1039 // temporary variable won't have the right backend type. Correct
1040 // that here by adding a type cast. We need to use base() to push
1041 // the circularity down one level.
1042 tree ret
= var_to_tree(bvar
);
1043 if (!this->is_lvalue_
1044 && POINTER_TYPE_P(TREE_TYPE(ret
))
1045 && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret
))))
1047 Btype
* type_btype
= this->type()->base()->get_backend(context
->gogo());
1048 tree type_tree
= type_to_tree(type_btype
);
1049 ret
= fold_convert_loc(this->location().gcc_location(), type_tree
, ret
);
1054 // Ast dump for temporary reference.
1057 Temporary_reference_expression::do_dump_expression(
1058 Ast_dump_context
* ast_dump_context
) const
1060 ast_dump_context
->dump_temp_variable_name(this->statement_
);
1063 // Make a reference to a temporary variable.
1065 Temporary_reference_expression
*
1066 Expression::make_temporary_reference(Temporary_statement
* statement
,
1069 return new Temporary_reference_expression(statement
, location
);
1072 // Class Set_and_use_temporary_expression.
1077 Set_and_use_temporary_expression::do_type()
1079 return this->statement_
->type();
1082 // Take the address.
1085 Set_and_use_temporary_expression::do_address_taken(bool)
1087 this->statement_
->set_is_address_taken();
1090 // Return the backend representation.
1093 Set_and_use_temporary_expression::do_get_tree(Translate_context
* context
)
1095 Bvariable
* bvar
= this->statement_
->get_backend_variable(context
);
1096 tree var_tree
= var_to_tree(bvar
);
1097 tree expr_tree
= this->expr_
->get_tree(context
);
1098 if (var_tree
== error_mark_node
|| expr_tree
== error_mark_node
)
1099 return error_mark_node
;
1100 Location loc
= this->location();
1101 return build2_loc(loc
.gcc_location(), COMPOUND_EXPR
, TREE_TYPE(var_tree
),
1102 build2_loc(loc
.gcc_location(), MODIFY_EXPR
, void_type_node
,
1103 var_tree
, expr_tree
),
1110 Set_and_use_temporary_expression::do_dump_expression(
1111 Ast_dump_context
* ast_dump_context
) const
1113 ast_dump_context
->ostream() << '(';
1114 ast_dump_context
->dump_temp_variable_name(this->statement_
);
1115 ast_dump_context
->ostream() << " = ";
1116 this->expr_
->dump_expression(ast_dump_context
);
1117 ast_dump_context
->ostream() << ')';
1120 // Make a set-and-use temporary.
1122 Set_and_use_temporary_expression
*
1123 Expression::make_set_and_use_temporary(Temporary_statement
* statement
,
1124 Expression
* expr
, Location location
)
1126 return new Set_and_use_temporary_expression(statement
, expr
, location
);
1129 // A sink expression--a use of the blank identifier _.
1131 class Sink_expression
: public Expression
1134 Sink_expression(Location location
)
1135 : Expression(EXPRESSION_SINK
, location
),
1136 type_(NULL
), var_(NULL_TREE
)
1141 do_discarding_value()
1148 do_determine_type(const Type_context
*);
1152 { return new Sink_expression(this->location()); }
1155 do_get_tree(Translate_context
*);
1158 do_dump_expression(Ast_dump_context
*) const;
1161 // The type of this sink variable.
1163 // The temporary variable we generate.
1167 // Return the type of a sink expression.
1170 Sink_expression::do_type()
1172 if (this->type_
== NULL
)
1173 return Type::make_sink_type();
1177 // Determine the type of a sink expression.
1180 Sink_expression::do_determine_type(const Type_context
* context
)
1182 if (context
->type
!= NULL
)
1183 this->type_
= context
->type
;
1186 // Return a temporary variable for a sink expression. This will
1187 // presumably be a write-only variable which the middle-end will drop.
1190 Sink_expression::do_get_tree(Translate_context
* context
)
1192 if (this->var_
== NULL_TREE
)
1194 go_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1195 Btype
* bt
= this->type_
->get_backend(context
->gogo());
1196 this->var_
= create_tmp_var(type_to_tree(bt
), "blank");
1201 // Ast dump for sink expression.
1204 Sink_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1206 ast_dump_context
->ostream() << "_" ;
1209 // Make a sink expression.
1212 Expression::make_sink(Location location
)
1214 return new Sink_expression(location
);
1217 // Class Func_expression.
1219 // FIXME: Can a function expression appear in a constant expression?
1220 // The value is unchanging. Initializing a constant to the address of
1221 // a function seems like it could work, though there might be little
1227 Func_expression::do_traverse(Traverse
* traverse
)
1229 return (this->closure_
== NULL
1231 : Expression::traverse(&this->closure_
, traverse
));
1234 // Return the type of a function expression.
1237 Func_expression::do_type()
1239 if (this->function_
->is_function())
1240 return this->function_
->func_value()->type();
1241 else if (this->function_
->is_function_declaration())
1242 return this->function_
->func_declaration_value()->type();
1247 // Get the tree for a function expression without evaluating the
1251 Func_expression::get_tree_without_closure(Gogo
* gogo
)
1253 Function_type
* fntype
;
1254 if (this->function_
->is_function())
1255 fntype
= this->function_
->func_value()->type();
1256 else if (this->function_
->is_function_declaration())
1257 fntype
= this->function_
->func_declaration_value()->type();
1261 // Builtin functions are handled specially by Call_expression. We
1262 // can't take their address.
1263 if (fntype
->is_builtin())
1265 error_at(this->location(),
1266 "invalid use of special builtin function %qs; must be called",
1267 this->function_
->name().c_str());
1268 return error_mark_node
;
1271 Named_object
* no
= this->function_
;
1273 tree id
= no
->get_id(gogo
);
1274 if (id
== error_mark_node
)
1275 return error_mark_node
;
1278 if (no
->is_function())
1279 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
, id
);
1280 else if (no
->is_function_declaration())
1281 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
, id
);
1285 if (fndecl
== error_mark_node
)
1286 return error_mark_node
;
1288 return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl
);
1291 // Get the tree for a function expression. This is used when we take
1292 // the address of a function rather than simply calling it. If the
1293 // function has a closure, we must use a trampoline.
1296 Func_expression::do_get_tree(Translate_context
* context
)
1298 Gogo
* gogo
= context
->gogo();
1300 tree fnaddr
= this->get_tree_without_closure(gogo
);
1301 if (fnaddr
== error_mark_node
)
1302 return error_mark_node
;
1304 go_assert(TREE_CODE(fnaddr
) == ADDR_EXPR
1305 && TREE_CODE(TREE_OPERAND(fnaddr
, 0)) == FUNCTION_DECL
);
1306 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr
, 0)) = 1;
1308 // If there is no closure, that is all have to do.
1309 if (this->closure_
== NULL
)
1312 go_assert(this->function_
->func_value()->enclosing() != NULL
);
1314 // Get the value of the closure. This will be a pointer to space
1315 // allocated on the heap.
1316 tree closure_tree
= this->closure_
->get_tree(context
);
1317 if (closure_tree
== error_mark_node
)
1318 return error_mark_node
;
1319 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree
)));
1321 // Now we need to build some code on the heap. This code will load
1322 // the static chain pointer with the closure and then jump to the
1323 // body of the function. The normal gcc approach is to build the
1324 // code on the stack. Unfortunately we can not do that, as Go
1325 // permits us to return the function pointer.
1327 return gogo
->make_trampoline(fnaddr
, closure_tree
, this->location());
1330 // Ast dump for function.
1333 Func_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1335 ast_dump_context
->ostream() << this->function_
->name();
1336 if (this->closure_
!= NULL
)
1338 ast_dump_context
->ostream() << " {closure = ";
1339 this->closure_
->dump_expression(ast_dump_context
);
1340 ast_dump_context
->ostream() << "}";
1344 // Make a reference to a function in an expression.
1347 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1350 return new Func_expression(function
, closure
, location
);
1353 // Class Unknown_expression.
1355 // Return the name of an unknown expression.
1358 Unknown_expression::name() const
1360 return this->named_object_
->name();
1363 // Lower a reference to an unknown name.
1366 Unknown_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
1368 Location location
= this->location();
1369 Named_object
* no
= this->named_object_
;
1371 if (!no
->is_unknown())
1375 real
= no
->unknown_value()->real_named_object();
1378 if (this->is_composite_literal_key_
)
1380 if (!this->no_error_message_
)
1381 error_at(location
, "reference to undefined name %qs",
1382 this->named_object_
->message_name().c_str());
1383 return Expression::make_error(location
);
1386 switch (real
->classification())
1388 case Named_object::NAMED_OBJECT_CONST
:
1389 return Expression::make_const_reference(real
, location
);
1390 case Named_object::NAMED_OBJECT_TYPE
:
1391 return Expression::make_type(real
->type_value(), location
);
1392 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1393 if (this->is_composite_literal_key_
)
1395 if (!this->no_error_message_
)
1396 error_at(location
, "reference to undefined type %qs",
1397 real
->message_name().c_str());
1398 return Expression::make_error(location
);
1399 case Named_object::NAMED_OBJECT_VAR
:
1400 real
->var_value()->set_is_used();
1401 return Expression::make_var_reference(real
, location
);
1402 case Named_object::NAMED_OBJECT_FUNC
:
1403 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1404 return Expression::make_func_reference(real
, NULL
, location
);
1405 case Named_object::NAMED_OBJECT_PACKAGE
:
1406 if (this->is_composite_literal_key_
)
1408 if (!this->no_error_message_
)
1409 error_at(location
, "unexpected reference to package");
1410 return Expression::make_error(location
);
1416 // Dump the ast representation for an unknown expression to a dump context.
1419 Unknown_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1421 ast_dump_context
->ostream() << "_Unknown_(" << this->named_object_
->name()
1425 // Make a reference to an unknown name.
1428 Expression::make_unknown_reference(Named_object
* no
, Location location
)
1430 return new Unknown_expression(no
, location
);
1433 // A boolean expression.
1435 class Boolean_expression
: public Expression
1438 Boolean_expression(bool val
, Location location
)
1439 : Expression(EXPRESSION_BOOLEAN
, location
),
1440 val_(val
), type_(NULL
)
1448 do_is_constant() const
1455 do_determine_type(const Type_context
*);
1462 do_get_tree(Translate_context
*)
1463 { return this->val_
? boolean_true_node
: boolean_false_node
; }
1466 do_export(Export
* exp
) const
1467 { exp
->write_c_string(this->val_
? "true" : "false"); }
1470 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1471 { ast_dump_context
->ostream() << (this->val_
? "true" : "false"); }
1476 // The type as determined by context.
1483 Boolean_expression::do_type()
1485 if (this->type_
== NULL
)
1486 this->type_
= Type::make_boolean_type();
1490 // Set the type from the context.
1493 Boolean_expression::do_determine_type(const Type_context
* context
)
1495 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1497 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1498 this->type_
= context
->type
;
1499 else if (!context
->may_be_abstract
)
1500 this->type_
= Type::lookup_bool_type();
1503 // Import a boolean constant.
1506 Boolean_expression::do_import(Import
* imp
)
1508 if (imp
->peek_char() == 't')
1510 imp
->require_c_string("true");
1511 return Expression::make_boolean(true, imp
->location());
1515 imp
->require_c_string("false");
1516 return Expression::make_boolean(false, imp
->location());
1520 // Make a boolean expression.
1523 Expression::make_boolean(bool val
, Location location
)
1525 return new Boolean_expression(val
, location
);
1528 // Class String_expression.
1533 String_expression::do_type()
1535 if (this->type_
== NULL
)
1536 this->type_
= Type::make_string_type();
1540 // Set the type from the context.
1543 String_expression::do_determine_type(const Type_context
* context
)
1545 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1547 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1548 this->type_
= context
->type
;
1549 else if (!context
->may_be_abstract
)
1550 this->type_
= Type::lookup_string_type();
1553 // Build a string constant.
1556 String_expression::do_get_tree(Translate_context
* context
)
1558 return context
->gogo()->go_string_constant_tree(this->val_
);
1561 // Write string literal to string dump.
1564 String_expression::export_string(String_dump
* exp
,
1565 const String_expression
* str
)
1568 s
.reserve(str
->val_
.length() * 4 + 2);
1570 for (std::string::const_iterator p
= str
->val_
.begin();
1571 p
!= str
->val_
.end();
1574 if (*p
== '\\' || *p
== '"')
1579 else if (*p
>= 0x20 && *p
< 0x7f)
1581 else if (*p
== '\n')
1583 else if (*p
== '\t')
1588 unsigned char c
= *p
;
1589 unsigned int dig
= c
>> 4;
1590 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1592 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1596 exp
->write_string(s
);
1599 // Export a string expression.
1602 String_expression::do_export(Export
* exp
) const
1604 String_expression::export_string(exp
, this);
1607 // Import a string expression.
1610 String_expression::do_import(Import
* imp
)
1612 imp
->require_c_string("\"");
1616 int c
= imp
->get_char();
1617 if (c
== '"' || c
== -1)
1620 val
+= static_cast<char>(c
);
1623 c
= imp
->get_char();
1624 if (c
== '\\' || c
== '"')
1625 val
+= static_cast<char>(c
);
1632 c
= imp
->get_char();
1633 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1634 c
= imp
->get_char();
1635 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1636 char v
= (vh
<< 4) | vl
;
1641 error_at(imp
->location(), "bad string constant");
1642 return Expression::make_error(imp
->location());
1646 return Expression::make_string(val
, imp
->location());
1649 // Ast dump for string expression.
1652 String_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1654 String_expression::export_string(ast_dump_context
, this);
1657 // Make a string expression.
1660 Expression::make_string(const std::string
& val
, Location location
)
1662 return new String_expression(val
, location
);
1665 // Make an integer expression.
1667 class Integer_expression
: public Expression
1670 Integer_expression(const mpz_t
* val
, Type
* type
, bool is_character_constant
,
1672 : Expression(EXPRESSION_INTEGER
, location
),
1673 type_(type
), is_character_constant_(is_character_constant
)
1674 { mpz_init_set(this->val_
, *val
); }
1679 // Write VAL to string dump.
1681 export_integer(String_dump
* exp
, const mpz_t val
);
1683 // Write VAL to dump context.
1685 dump_integer(Ast_dump_context
* ast_dump_context
, const mpz_t val
);
1689 do_is_constant() const
1693 do_numeric_constant_value(Numeric_constant
* nc
) const;
1699 do_determine_type(const Type_context
* context
);
1702 do_check_types(Gogo
*);
1705 do_get_tree(Translate_context
*);
1710 if (this->is_character_constant_
)
1711 return Expression::make_character(&this->val_
, this->type_
,
1714 return Expression::make_integer(&this->val_
, this->type_
,
1719 do_export(Export
*) const;
1722 do_dump_expression(Ast_dump_context
*) const;
1725 // The integer value.
1729 // Whether this is a character constant.
1730 bool is_character_constant_
;
1733 // Return a numeric constant for this expression. We have to mark
1734 // this as a character when appropriate.
1737 Integer_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
1739 if (this->is_character_constant_
)
1740 nc
->set_rune(this->type_
, this->val_
);
1742 nc
->set_int(this->type_
, this->val_
);
1746 // Return the current type. If we haven't set the type yet, we return
1747 // an abstract integer type.
1750 Integer_expression::do_type()
1752 if (this->type_
== NULL
)
1754 if (this->is_character_constant_
)
1755 this->type_
= Type::make_abstract_character_type();
1757 this->type_
= Type::make_abstract_integer_type();
1762 // Set the type of the integer value. Here we may switch from an
1763 // abstract type to a real type.
1766 Integer_expression::do_determine_type(const Type_context
* context
)
1768 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1770 else if (context
->type
!= NULL
&& context
->type
->is_numeric_type())
1771 this->type_
= context
->type
;
1772 else if (!context
->may_be_abstract
)
1774 if (this->is_character_constant_
)
1775 this->type_
= Type::lookup_integer_type("int32");
1777 this->type_
= Type::lookup_integer_type("int");
1781 // Check the type of an integer constant.
1784 Integer_expression::do_check_types(Gogo
*)
1786 Type
* type
= this->type_
;
1789 Numeric_constant nc
;
1790 if (this->is_character_constant_
)
1791 nc
.set_rune(NULL
, this->val_
);
1793 nc
.set_int(NULL
, this->val_
);
1794 if (!nc
.set_type(type
, true, this->location()))
1795 this->set_is_error();
1798 // Get a tree for an integer constant.
1801 Integer_expression::do_get_tree(Translate_context
* context
)
1803 Gogo
* gogo
= context
->gogo();
1805 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1806 type
= type_to_tree(this->type_
->get_backend(gogo
));
1807 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1809 // We are converting to an abstract floating point type.
1810 Type
* ftype
= Type::lookup_float_type("float64");
1811 type
= type_to_tree(ftype
->get_backend(gogo
));
1813 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1815 // We are converting to an abstract complex type.
1816 Type
* ctype
= Type::lookup_complex_type("complex128");
1817 type
= type_to_tree(ctype
->get_backend(gogo
));
1821 // If we still have an abstract type here, then this is being
1822 // used in a constant expression which didn't get reduced for
1823 // some reason. Use a type which will fit the value. We use <,
1824 // not <=, because we need an extra bit for the sign bit.
1825 int bits
= mpz_sizeinbase(this->val_
, 2);
1826 if (bits
< INT_TYPE_SIZE
)
1828 Type
* t
= Type::lookup_integer_type("int");
1829 type
= type_to_tree(t
->get_backend(gogo
));
1833 Type
* t
= Type::lookup_integer_type("int64");
1834 type
= type_to_tree(t
->get_backend(gogo
));
1837 type
= long_long_integer_type_node
;
1839 return Expression::integer_constant_tree(this->val_
, type
);
1842 // Write VAL to export data.
1845 Integer_expression::export_integer(String_dump
* exp
, const mpz_t val
)
1847 char* s
= mpz_get_str(NULL
, 10, val
);
1848 exp
->write_c_string(s
);
1852 // Export an integer in a constant expression.
1855 Integer_expression::do_export(Export
* exp
) const
1857 Integer_expression::export_integer(exp
, this->val_
);
1858 if (this->is_character_constant_
)
1859 exp
->write_c_string("'");
1860 // A trailing space lets us reliably identify the end of the number.
1861 exp
->write_c_string(" ");
1864 // Import an integer, floating point, or complex value. This handles
1865 // all these types because they all start with digits.
1868 Integer_expression::do_import(Import
* imp
)
1870 std::string num
= imp
->read_identifier();
1871 imp
->require_c_string(" ");
1872 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1875 size_t plus_pos
= num
.find('+', 1);
1876 size_t minus_pos
= num
.find('-', 1);
1878 if (plus_pos
== std::string::npos
)
1880 else if (minus_pos
== std::string::npos
)
1884 error_at(imp
->location(), "bad number in import data: %qs",
1886 return Expression::make_error(imp
->location());
1888 if (pos
== std::string::npos
)
1889 mpfr_set_ui(real
, 0, GMP_RNDN
);
1892 std::string real_str
= num
.substr(0, pos
);
1893 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1895 error_at(imp
->location(), "bad number in import data: %qs",
1897 return Expression::make_error(imp
->location());
1901 std::string imag_str
;
1902 if (pos
== std::string::npos
)
1905 imag_str
= num
.substr(pos
);
1906 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
1908 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
1910 error_at(imp
->location(), "bad number in import data: %qs",
1912 return Expression::make_error(imp
->location());
1914 Expression
* ret
= Expression::make_complex(&real
, &imag
, NULL
,
1920 else if (num
.find('.') == std::string::npos
1921 && num
.find('E') == std::string::npos
)
1923 bool is_character_constant
= (!num
.empty()
1924 && num
[num
.length() - 1] == '\'');
1925 if (is_character_constant
)
1926 num
= num
.substr(0, num
.length() - 1);
1928 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
1930 error_at(imp
->location(), "bad number in import data: %qs",
1932 return Expression::make_error(imp
->location());
1935 if (is_character_constant
)
1936 ret
= Expression::make_character(&val
, NULL
, imp
->location());
1938 ret
= Expression::make_integer(&val
, NULL
, imp
->location());
1945 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
1947 error_at(imp
->location(), "bad number in import data: %qs",
1949 return Expression::make_error(imp
->location());
1951 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
1956 // Ast dump for integer expression.
1959 Integer_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1961 if (this->is_character_constant_
)
1962 ast_dump_context
->ostream() << '\'';
1963 Integer_expression::export_integer(ast_dump_context
, this->val_
);
1964 if (this->is_character_constant_
)
1965 ast_dump_context
->ostream() << '\'';
1968 // Build a new integer value.
1971 Expression::make_integer(const mpz_t
* val
, Type
* type
, Location location
)
1973 return new Integer_expression(val
, type
, false, location
);
1976 // Build a new character constant value.
1979 Expression::make_character(const mpz_t
* val
, Type
* type
, Location location
)
1981 return new Integer_expression(val
, type
, true, location
);
1986 class Float_expression
: public Expression
1989 Float_expression(const mpfr_t
* val
, Type
* type
, Location location
)
1990 : Expression(EXPRESSION_FLOAT
, location
),
1993 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
1996 // Write VAL to export data.
1998 export_float(String_dump
* exp
, const mpfr_t val
);
2000 // Write VAL to dump file.
2002 dump_float(Ast_dump_context
* ast_dump_context
, const mpfr_t val
);
2006 do_is_constant() const
2010 do_numeric_constant_value(Numeric_constant
* nc
) const
2012 nc
->set_float(this->type_
, this->val_
);
2020 do_determine_type(const Type_context
*);
2023 do_check_types(Gogo
*);
2027 { return Expression::make_float(&this->val_
, this->type_
,
2028 this->location()); }
2031 do_get_tree(Translate_context
*);
2034 do_export(Export
*) const;
2037 do_dump_expression(Ast_dump_context
*) const;
2040 // The floating point value.
2046 // Return the current type. If we haven't set the type yet, we return
2047 // an abstract float type.
2050 Float_expression::do_type()
2052 if (this->type_
== NULL
)
2053 this->type_
= Type::make_abstract_float_type();
2057 // Set the type of the float value. Here we may switch from an
2058 // abstract type to a real type.
2061 Float_expression::do_determine_type(const Type_context
* context
)
2063 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2065 else if (context
->type
!= NULL
2066 && (context
->type
->integer_type() != NULL
2067 || context
->type
->float_type() != NULL
2068 || context
->type
->complex_type() != NULL
))
2069 this->type_
= context
->type
;
2070 else if (!context
->may_be_abstract
)
2071 this->type_
= Type::lookup_float_type("float64");
2074 // Check the type of a float value.
2077 Float_expression::do_check_types(Gogo
*)
2079 Type
* type
= this->type_
;
2082 Numeric_constant nc
;
2083 nc
.set_float(NULL
, this->val_
);
2084 if (!nc
.set_type(this->type_
, true, this->location()))
2085 this->set_is_error();
2088 // Get a tree for a float constant.
2091 Float_expression::do_get_tree(Translate_context
* context
)
2093 Gogo
* gogo
= context
->gogo();
2095 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2096 type
= type_to_tree(this->type_
->get_backend(gogo
));
2097 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2099 // We have an abstract integer type. We just hope for the best.
2100 type
= type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo
));
2104 // If we still have an abstract type here, then this is being
2105 // used in a constant expression which didn't get reduced. We
2106 // just use float64 and hope for the best.
2107 Type
* ft
= Type::lookup_float_type("float64");
2108 type
= type_to_tree(ft
->get_backend(gogo
));
2110 return Expression::float_constant_tree(this->val_
, type
);
2113 // Write a floating point number to a string dump.
2116 Float_expression::export_float(String_dump
*exp
, const mpfr_t val
)
2119 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2121 exp
->write_c_string("-");
2122 exp
->write_c_string("0.");
2123 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2126 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2127 exp
->write_c_string(buf
);
2130 // Export a floating point number in a constant expression.
2133 Float_expression::do_export(Export
* exp
) const
2135 Float_expression::export_float(exp
, this->val_
);
2136 // A trailing space lets us reliably identify the end of the number.
2137 exp
->write_c_string(" ");
2140 // Dump a floating point number to the dump file.
2143 Float_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2145 Float_expression::export_float(ast_dump_context
, this->val_
);
2148 // Make a float expression.
2151 Expression::make_float(const mpfr_t
* val
, Type
* type
, Location location
)
2153 return new Float_expression(val
, type
, location
);
2158 class Complex_expression
: public Expression
2161 Complex_expression(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2163 : Expression(EXPRESSION_COMPLEX
, location
),
2166 mpfr_init_set(this->real_
, *real
, GMP_RNDN
);
2167 mpfr_init_set(this->imag_
, *imag
, GMP_RNDN
);
2170 // Write REAL/IMAG to string dump.
2172 export_complex(String_dump
* exp
, const mpfr_t real
, const mpfr_t val
);
2174 // Write REAL/IMAG to dump context.
2176 dump_complex(Ast_dump_context
* ast_dump_context
,
2177 const mpfr_t real
, const mpfr_t val
);
2181 do_is_constant() const
2185 do_numeric_constant_value(Numeric_constant
* nc
) const
2187 nc
->set_complex(this->type_
, this->real_
, this->imag_
);
2195 do_determine_type(const Type_context
*);
2198 do_check_types(Gogo
*);
2203 return Expression::make_complex(&this->real_
, &this->imag_
, this->type_
,
2208 do_get_tree(Translate_context
*);
2211 do_export(Export
*) const;
2214 do_dump_expression(Ast_dump_context
*) const;
2219 // The imaginary part;
2221 // The type if known.
2225 // Return the current type. If we haven't set the type yet, we return
2226 // an abstract complex type.
2229 Complex_expression::do_type()
2231 if (this->type_
== NULL
)
2232 this->type_
= Type::make_abstract_complex_type();
2236 // Set the type of the complex value. Here we may switch from an
2237 // abstract type to a real type.
2240 Complex_expression::do_determine_type(const Type_context
* context
)
2242 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2244 else if (context
->type
!= NULL
2245 && context
->type
->complex_type() != NULL
)
2246 this->type_
= context
->type
;
2247 else if (!context
->may_be_abstract
)
2248 this->type_
= Type::lookup_complex_type("complex128");
2251 // Check the type of a complex value.
2254 Complex_expression::do_check_types(Gogo
*)
2256 Type
* type
= this->type_
;
2259 Numeric_constant nc
;
2260 nc
.set_complex(NULL
, this->real_
, this->imag_
);
2261 if (!nc
.set_type(this->type_
, true, this->location()))
2262 this->set_is_error();
2265 // Get a tree for a complex constant.
2268 Complex_expression::do_get_tree(Translate_context
* context
)
2270 Gogo
* gogo
= context
->gogo();
2272 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2273 type
= type_to_tree(this->type_
->get_backend(gogo
));
2276 // If we still have an abstract type here, this this is being
2277 // used in a constant expression which didn't get reduced. We
2278 // just use complex128 and hope for the best.
2279 Type
* ct
= Type::lookup_complex_type("complex128");
2280 type
= type_to_tree(ct
->get_backend(gogo
));
2282 return Expression::complex_constant_tree(this->real_
, this->imag_
, type
);
2285 // Write REAL/IMAG to export data.
2288 Complex_expression::export_complex(String_dump
* exp
, const mpfr_t real
,
2291 if (!mpfr_zero_p(real
))
2293 Float_expression::export_float(exp
, real
);
2294 if (mpfr_sgn(imag
) > 0)
2295 exp
->write_c_string("+");
2297 Float_expression::export_float(exp
, imag
);
2298 exp
->write_c_string("i");
2301 // Export a complex number in a constant expression.
2304 Complex_expression::do_export(Export
* exp
) const
2306 Complex_expression::export_complex(exp
, this->real_
, this->imag_
);
2307 // A trailing space lets us reliably identify the end of the number.
2308 exp
->write_c_string(" ");
2311 // Dump a complex expression to the dump file.
2314 Complex_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2316 Complex_expression::export_complex(ast_dump_context
,
2321 // Make a complex expression.
2324 Expression::make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2327 return new Complex_expression(real
, imag
, type
, location
);
2330 // Find a named object in an expression.
2332 class Find_named_object
: public Traverse
2335 Find_named_object(Named_object
* no
)
2336 : Traverse(traverse_expressions
),
2337 no_(no
), found_(false)
2340 // Whether we found the object.
2343 { return this->found_
; }
2347 expression(Expression
**);
2350 // The object we are looking for.
2352 // Whether we found it.
2356 // A reference to a const in an expression.
2358 class Const_expression
: public Expression
2361 Const_expression(Named_object
* constant
, Location location
)
2362 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2363 constant_(constant
), type_(NULL
), seen_(false)
2368 { return this->constant_
; }
2370 // Check that the initializer does not refer to the constant itself.
2372 check_for_init_loop();
2376 do_traverse(Traverse
*);
2379 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2382 do_is_constant() const
2386 do_numeric_constant_value(Numeric_constant
* nc
) const;
2389 do_string_constant_value(std::string
* val
) const;
2394 // The type of a const is set by the declaration, not the use.
2396 do_determine_type(const Type_context
*);
2399 do_check_types(Gogo
*);
2406 do_get_tree(Translate_context
* context
);
2408 // When exporting a reference to a const as part of a const
2409 // expression, we export the value. We ignore the fact that it has
2412 do_export(Export
* exp
) const
2413 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2416 do_dump_expression(Ast_dump_context
*) const;
2420 Named_object
* constant_
;
2421 // The type of this reference. This is used if the constant has an
2424 // Used to prevent infinite recursion when a constant incorrectly
2425 // refers to itself.
2432 Const_expression::do_traverse(Traverse
* traverse
)
2434 if (this->type_
!= NULL
)
2435 return Type::traverse(this->type_
, traverse
);
2436 return TRAVERSE_CONTINUE
;
2439 // Lower a constant expression. This is where we convert the
2440 // predeclared constant iota into an integer value.
2443 Const_expression::do_lower(Gogo
* gogo
, Named_object
*,
2444 Statement_inserter
*, int iota_value
)
2446 if (this->constant_
->const_value()->expr()->classification()
2449 if (iota_value
== -1)
2451 error_at(this->location(),
2452 "iota is only defined in const declarations");
2456 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2457 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2463 // Make sure that the constant itself has been lowered.
2464 gogo
->lower_constant(this->constant_
);
2469 // Return a numeric constant value.
2472 Const_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
2477 Expression
* e
= this->constant_
->const_value()->expr();
2481 bool r
= e
->numeric_constant_value(nc
);
2483 this->seen_
= false;
2486 if (this->type_
!= NULL
)
2487 ctype
= this->type_
;
2489 ctype
= this->constant_
->const_value()->type();
2490 if (r
&& ctype
!= NULL
)
2492 if (!nc
->set_type(ctype
, false, this->location()))
2500 Const_expression::do_string_constant_value(std::string
* val
) const
2505 Expression
* e
= this->constant_
->const_value()->expr();
2508 bool ok
= e
->string_constant_value(val
);
2509 this->seen_
= false;
2514 // Return the type of the const reference.
2517 Const_expression::do_type()
2519 if (this->type_
!= NULL
)
2522 Named_constant
* nc
= this->constant_
->const_value();
2524 if (this->seen_
|| nc
->lowering())
2526 this->report_error(_("constant refers to itself"));
2527 this->type_
= Type::make_error_type();
2533 Type
* ret
= nc
->type();
2537 this->seen_
= false;
2541 // During parsing, a named constant may have a NULL type, but we
2542 // must not return a NULL type here.
2543 ret
= nc
->expr()->type();
2545 this->seen_
= false;
2550 // Set the type of the const reference.
2553 Const_expression::do_determine_type(const Type_context
* context
)
2555 Type
* ctype
= this->constant_
->const_value()->type();
2556 Type
* cetype
= (ctype
!= NULL
2558 : this->constant_
->const_value()->expr()->type());
2559 if (ctype
!= NULL
&& !ctype
->is_abstract())
2561 else if (context
->type
!= NULL
2562 && context
->type
->is_numeric_type()
2563 && cetype
->is_numeric_type())
2564 this->type_
= context
->type
;
2565 else if (context
->type
!= NULL
2566 && context
->type
->is_string_type()
2567 && cetype
->is_string_type())
2568 this->type_
= context
->type
;
2569 else if (context
->type
!= NULL
2570 && context
->type
->is_boolean_type()
2571 && cetype
->is_boolean_type())
2572 this->type_
= context
->type
;
2573 else if (!context
->may_be_abstract
)
2575 if (cetype
->is_abstract())
2576 cetype
= cetype
->make_non_abstract_type();
2577 this->type_
= cetype
;
2581 // Check for a loop in which the initializer of a constant refers to
2582 // the constant itself.
2585 Const_expression::check_for_init_loop()
2587 if (this->type_
!= NULL
&& this->type_
->is_error())
2592 this->report_error(_("constant refers to itself"));
2593 this->type_
= Type::make_error_type();
2597 Expression
* init
= this->constant_
->const_value()->expr();
2598 Find_named_object
find_named_object(this->constant_
);
2601 Expression::traverse(&init
, &find_named_object
);
2602 this->seen_
= false;
2604 if (find_named_object
.found())
2606 if (this->type_
== NULL
|| !this->type_
->is_error())
2608 this->report_error(_("constant refers to itself"));
2609 this->type_
= Type::make_error_type();
2615 // Check types of a const reference.
2618 Const_expression::do_check_types(Gogo
*)
2620 if (this->type_
!= NULL
&& this->type_
->is_error())
2623 this->check_for_init_loop();
2625 // Check that numeric constant fits in type.
2626 if (this->type_
!= NULL
&& this->type_
->is_numeric_type())
2628 Numeric_constant nc
;
2629 if (this->constant_
->const_value()->expr()->numeric_constant_value(&nc
))
2631 if (!nc
.set_type(this->type_
, true, this->location()))
2632 this->set_is_error();
2637 // Return a tree for the const reference.
2640 Const_expression::do_get_tree(Translate_context
* context
)
2642 Gogo
* gogo
= context
->gogo();
2644 if (this->type_
== NULL
)
2645 type_tree
= NULL_TREE
;
2648 type_tree
= type_to_tree(this->type_
->get_backend(gogo
));
2649 if (type_tree
== error_mark_node
)
2650 return error_mark_node
;
2653 // If the type has been set for this expression, but the underlying
2654 // object is an abstract int or float, we try to get the abstract
2655 // value. Otherwise we may lose something in the conversion.
2656 if (this->type_
!= NULL
2657 && this->type_
->is_numeric_type()
2658 && (this->constant_
->const_value()->type() == NULL
2659 || this->constant_
->const_value()->type()->is_abstract()))
2661 Expression
* expr
= this->constant_
->const_value()->expr();
2662 Numeric_constant nc
;
2663 if (expr
->numeric_constant_value(&nc
)
2664 && nc
.set_type(this->type_
, false, this->location()))
2666 Expression
* e
= nc
.expression(this->location());
2667 return e
->get_tree(context
);
2671 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2672 if (this->type_
== NULL
2673 || const_tree
== error_mark_node
2674 || TREE_TYPE(const_tree
) == error_mark_node
)
2678 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2679 ret
= fold_convert(type_tree
, const_tree
);
2680 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2681 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2682 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2683 ret
= fold(convert_to_real(type_tree
, const_tree
));
2684 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2685 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2691 // Dump ast representation for constant expression.
2694 Const_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2696 ast_dump_context
->ostream() << this->constant_
->name();
2699 // Make a reference to a constant in an expression.
2702 Expression::make_const_reference(Named_object
* constant
,
2705 return new Const_expression(constant
, location
);
2708 // Find a named object in an expression.
2711 Find_named_object::expression(Expression
** pexpr
)
2713 switch ((*pexpr
)->classification())
2715 case Expression::EXPRESSION_CONST_REFERENCE
:
2717 Const_expression
* ce
= static_cast<Const_expression
*>(*pexpr
);
2718 if (ce
->named_object() == this->no_
)
2721 // We need to check a constant initializer explicitly, as
2722 // loops here will not be caught by the loop checking for
2723 // variable initializers.
2724 ce
->check_for_init_loop();
2726 return TRAVERSE_CONTINUE
;
2729 case Expression::EXPRESSION_VAR_REFERENCE
:
2730 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2732 return TRAVERSE_CONTINUE
;
2733 case Expression::EXPRESSION_FUNC_REFERENCE
:
2734 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2736 return TRAVERSE_CONTINUE
;
2738 return TRAVERSE_CONTINUE
;
2740 this->found_
= true;
2741 return TRAVERSE_EXIT
;
2746 class Nil_expression
: public Expression
2749 Nil_expression(Location location
)
2750 : Expression(EXPRESSION_NIL
, location
)
2758 do_is_constant() const
2763 { return Type::make_nil_type(); }
2766 do_determine_type(const Type_context
*)
2774 do_get_tree(Translate_context
*)
2775 { return null_pointer_node
; }
2778 do_export(Export
* exp
) const
2779 { exp
->write_c_string("nil"); }
2782 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2783 { ast_dump_context
->ostream() << "nil"; }
2786 // Import a nil expression.
2789 Nil_expression::do_import(Import
* imp
)
2791 imp
->require_c_string("nil");
2792 return Expression::make_nil(imp
->location());
2795 // Make a nil expression.
2798 Expression::make_nil(Location location
)
2800 return new Nil_expression(location
);
2803 // The value of the predeclared constant iota. This is little more
2804 // than a marker. This will be lowered to an integer in
2805 // Const_expression::do_lower, which is where we know the value that
2808 class Iota_expression
: public Parser_expression
2811 Iota_expression(Location location
)
2812 : Parser_expression(EXPRESSION_IOTA
, location
)
2817 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
2818 { go_unreachable(); }
2820 // There should only ever be one of these.
2823 { go_unreachable(); }
2826 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2827 { ast_dump_context
->ostream() << "iota"; }
2830 // Make an iota expression. This is only called for one case: the
2831 // value of the predeclared constant iota.
2834 Expression::make_iota()
2836 static Iota_expression
iota_expression(Linemap::unknown_location());
2837 return &iota_expression
;
2840 // A type conversion expression.
2842 class Type_conversion_expression
: public Expression
2845 Type_conversion_expression(Type
* type
, Expression
* expr
,
2847 : Expression(EXPRESSION_CONVERSION
, location
),
2848 type_(type
), expr_(expr
), may_convert_function_types_(false)
2851 // Return the type to which we are converting.
2854 { return this->type_
; }
2856 // Return the expression which we are converting.
2859 { return this->expr_
; }
2861 // Permit converting from one function type to another. This is
2862 // used internally for method expressions.
2864 set_may_convert_function_types()
2866 this->may_convert_function_types_
= true;
2869 // Import a type conversion expression.
2875 do_traverse(Traverse
* traverse
);
2878 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2881 do_is_constant() const
2882 { return this->expr_
->is_constant(); }
2885 do_numeric_constant_value(Numeric_constant
*) const;
2888 do_string_constant_value(std::string
*) const;
2892 { return this->type_
; }
2895 do_determine_type(const Type_context
*)
2897 Type_context
subcontext(this->type_
, false);
2898 this->expr_
->determine_type(&subcontext
);
2902 do_check_types(Gogo
*);
2907 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2912 do_get_tree(Translate_context
* context
);
2915 do_export(Export
*) const;
2918 do_dump_expression(Ast_dump_context
*) const;
2921 // The type to convert to.
2923 // The expression to convert.
2925 // True if this is permitted to convert function types. This is
2926 // used internally for method expressions.
2927 bool may_convert_function_types_
;
2933 Type_conversion_expression::do_traverse(Traverse
* traverse
)
2935 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
2936 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
2937 return TRAVERSE_EXIT
;
2938 return TRAVERSE_CONTINUE
;
2941 // Convert to a constant at lowering time.
2944 Type_conversion_expression::do_lower(Gogo
*, Named_object
*,
2945 Statement_inserter
*, int)
2947 Type
* type
= this->type_
;
2948 Expression
* val
= this->expr_
;
2949 Location location
= this->location();
2951 if (type
->is_numeric_type())
2953 Numeric_constant nc
;
2954 if (val
->numeric_constant_value(&nc
))
2956 if (!nc
.set_type(type
, true, location
))
2957 return Expression::make_error(location
);
2958 return nc
.expression(location
);
2962 if (type
->is_slice_type())
2964 Type
* element_type
= type
->array_type()->element_type()->forwarded();
2965 bool is_byte
= (element_type
->integer_type() != NULL
2966 && element_type
->integer_type()->is_byte());
2967 bool is_rune
= (element_type
->integer_type() != NULL
2968 && element_type
->integer_type()->is_rune());
2969 if (is_byte
|| is_rune
)
2972 if (val
->string_constant_value(&s
))
2974 Expression_list
* vals
= new Expression_list();
2977 for (std::string::const_iterator p
= s
.begin();
2982 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
2983 Expression
* v
= Expression::make_integer(&val
,
2992 const char *p
= s
.data();
2993 const char *pend
= s
.data() + s
.length();
2997 int adv
= Lex::fetch_char(p
, &c
);
3000 warning_at(this->location(), 0,
3001 "invalid UTF-8 encoding");
3006 mpz_init_set_ui(val
, c
);
3007 Expression
* v
= Expression::make_integer(&val
,
3015 return Expression::make_slice_composite_literal(type
, vals
,
3024 // Return the constant numeric value if there is one.
3027 Type_conversion_expression::do_numeric_constant_value(
3028 Numeric_constant
* nc
) const
3030 if (!this->type_
->is_numeric_type())
3032 if (!this->expr_
->numeric_constant_value(nc
))
3034 return nc
->set_type(this->type_
, false, this->location());
3037 // Return the constant string value if there is one.
3040 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3042 if (this->type_
->is_string_type()
3043 && this->expr_
->type()->integer_type() != NULL
)
3045 Numeric_constant nc
;
3046 if (this->expr_
->numeric_constant_value(&nc
))
3049 if (nc
.to_unsigned_long(&ival
) == Numeric_constant::NC_UL_VALID
)
3052 Lex::append_char(ival
, true, val
, this->location());
3058 // FIXME: Could handle conversion from const []int here.
3063 // Check that types are convertible.
3066 Type_conversion_expression::do_check_types(Gogo
*)
3068 Type
* type
= this->type_
;
3069 Type
* expr_type
= this->expr_
->type();
3072 if (type
->is_error() || expr_type
->is_error())
3074 this->set_is_error();
3078 if (this->may_convert_function_types_
3079 && type
->function_type() != NULL
3080 && expr_type
->function_type() != NULL
)
3083 if (Type::are_convertible(type
, expr_type
, &reason
))
3086 error_at(this->location(), "%s", reason
.c_str());
3087 this->set_is_error();
3090 // Get a tree for a type conversion.
3093 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3095 Gogo
* gogo
= context
->gogo();
3096 tree type_tree
= type_to_tree(this->type_
->get_backend(gogo
));
3097 tree expr_tree
= this->expr_
->get_tree(context
);
3099 if (type_tree
== error_mark_node
3100 || expr_tree
== error_mark_node
3101 || TREE_TYPE(expr_tree
) == error_mark_node
)
3102 return error_mark_node
;
3104 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3105 return fold_convert(type_tree
, expr_tree
);
3107 Type
* type
= this->type_
;
3108 Type
* expr_type
= this->expr_
->type();
3110 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3111 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3112 expr_tree
, this->location());
3113 else if (type
->integer_type() != NULL
)
3115 if (expr_type
->integer_type() != NULL
3116 || expr_type
->float_type() != NULL
3117 || expr_type
->is_unsafe_pointer_type())
3118 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3122 else if (type
->float_type() != NULL
)
3124 if (expr_type
->integer_type() != NULL
3125 || expr_type
->float_type() != NULL
)
3126 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3130 else if (type
->complex_type() != NULL
)
3132 if (expr_type
->complex_type() != NULL
)
3133 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3137 else if (type
->is_string_type()
3138 && expr_type
->integer_type() != NULL
)
3140 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3141 if (host_integerp(expr_tree
, 0))
3143 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3145 Lex::append_char(intval
, true, &s
, this->location());
3146 Expression
* se
= Expression::make_string(s
, this->location());
3147 return se
->get_tree(context
);
3150 static tree int_to_string_fndecl
;
3151 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3153 "__go_int_to_string",
3157 fold_convert(integer_type_node
, expr_tree
));
3159 else if (type
->is_string_type() && expr_type
->is_slice_type())
3161 if (!DECL_P(expr_tree
))
3162 expr_tree
= save_expr(expr_tree
);
3163 Array_type
* a
= expr_type
->array_type();
3164 Type
* e
= a
->element_type()->forwarded();
3165 go_assert(e
->integer_type() != NULL
);
3166 tree valptr
= fold_convert(const_ptr_type_node
,
3167 a
->value_pointer_tree(gogo
, expr_tree
));
3168 tree len
= a
->length_tree(gogo
, expr_tree
);
3169 len
= fold_convert_loc(this->location().gcc_location(), integer_type_node
,
3171 if (e
->integer_type()->is_byte())
3173 static tree byte_array_to_string_fndecl
;
3174 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3176 "__go_byte_array_to_string",
3179 const_ptr_type_node
,
3186 go_assert(e
->integer_type()->is_rune());
3187 static tree int_array_to_string_fndecl
;
3188 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3190 "__go_int_array_to_string",
3193 const_ptr_type_node
,
3199 else if (type
->is_slice_type() && expr_type
->is_string_type())
3201 Type
* e
= type
->array_type()->element_type()->forwarded();
3202 go_assert(e
->integer_type() != NULL
);
3203 if (e
->integer_type()->is_byte())
3205 tree string_to_byte_array_fndecl
= NULL_TREE
;
3206 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3208 "__go_string_to_byte_array",
3211 TREE_TYPE(expr_tree
),
3216 go_assert(e
->integer_type()->is_rune());
3217 tree string_to_int_array_fndecl
= NULL_TREE
;
3218 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3220 "__go_string_to_int_array",
3223 TREE_TYPE(expr_tree
),
3227 else if ((type
->is_unsafe_pointer_type()
3228 && expr_type
->points_to() != NULL
)
3229 || (expr_type
->is_unsafe_pointer_type()
3230 && type
->points_to() != NULL
))
3231 ret
= fold_convert(type_tree
, expr_tree
);
3232 else if (type
->is_unsafe_pointer_type()
3233 && expr_type
->integer_type() != NULL
)
3234 ret
= convert_to_pointer(type_tree
, expr_tree
);
3235 else if (this->may_convert_function_types_
3236 && type
->function_type() != NULL
3237 && expr_type
->function_type() != NULL
)
3238 ret
= fold_convert_loc(this->location().gcc_location(), type_tree
,
3241 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3242 expr_tree
, this->location());
3247 // Output a type conversion in a constant expression.
3250 Type_conversion_expression::do_export(Export
* exp
) const
3252 exp
->write_c_string("convert(");
3253 exp
->write_type(this->type_
);
3254 exp
->write_c_string(", ");
3255 this->expr_
->export_expression(exp
);
3256 exp
->write_c_string(")");
3259 // Import a type conversion or a struct construction.
3262 Type_conversion_expression::do_import(Import
* imp
)
3264 imp
->require_c_string("convert(");
3265 Type
* type
= imp
->read_type();
3266 imp
->require_c_string(", ");
3267 Expression
* val
= Expression::import_expression(imp
);
3268 imp
->require_c_string(")");
3269 return Expression::make_cast(type
, val
, imp
->location());
3272 // Dump ast representation for a type conversion expression.
3275 Type_conversion_expression::do_dump_expression(
3276 Ast_dump_context
* ast_dump_context
) const
3278 ast_dump_context
->dump_type(this->type_
);
3279 ast_dump_context
->ostream() << "(";
3280 ast_dump_context
->dump_expression(this->expr_
);
3281 ast_dump_context
->ostream() << ") ";
3284 // Make a type cast expression.
3287 Expression::make_cast(Type
* type
, Expression
* val
, Location location
)
3289 if (type
->is_error_type() || val
->is_error_expression())
3290 return Expression::make_error(location
);
3291 return new Type_conversion_expression(type
, val
, location
);
3294 // An unsafe type conversion, used to pass values to builtin functions.
3296 class Unsafe_type_conversion_expression
: public Expression
3299 Unsafe_type_conversion_expression(Type
* type
, Expression
* expr
,
3301 : Expression(EXPRESSION_UNSAFE_CONVERSION
, location
),
3302 type_(type
), expr_(expr
)
3307 do_traverse(Traverse
* traverse
);
3311 { return this->type_
; }
3314 do_determine_type(const Type_context
*)
3315 { this->expr_
->determine_type_no_context(); }
3320 return new Unsafe_type_conversion_expression(this->type_
,
3321 this->expr_
->copy(),
3326 do_get_tree(Translate_context
*);
3329 do_dump_expression(Ast_dump_context
*) const;
3332 // The type to convert to.
3334 // The expression to convert.
3341 Unsafe_type_conversion_expression::do_traverse(Traverse
* traverse
)
3343 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3344 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3345 return TRAVERSE_EXIT
;
3346 return TRAVERSE_CONTINUE
;
3349 // Convert to backend representation.
3352 Unsafe_type_conversion_expression::do_get_tree(Translate_context
* context
)
3354 // We are only called for a limited number of cases.
3356 Type
* t
= this->type_
;
3357 Type
* et
= this->expr_
->type();
3359 tree type_tree
= type_to_tree(this->type_
->get_backend(context
->gogo()));
3360 tree expr_tree
= this->expr_
->get_tree(context
);
3361 if (type_tree
== error_mark_node
|| expr_tree
== error_mark_node
)
3362 return error_mark_node
;
3364 Location loc
= this->location();
3366 bool use_view_convert
= false;
3367 if (t
->is_slice_type())
3369 go_assert(et
->is_slice_type());
3370 use_view_convert
= true;
3372 else if (t
->map_type() != NULL
)
3373 go_assert(et
->map_type() != NULL
);
3374 else if (t
->channel_type() != NULL
)
3375 go_assert(et
->channel_type() != NULL
);
3376 else if (t
->points_to() != NULL
)
3377 go_assert(et
->points_to() != NULL
|| et
->is_nil_type());
3378 else if (et
->is_unsafe_pointer_type())
3379 go_assert(t
->points_to() != NULL
);
3380 else if (t
->interface_type() != NULL
&& !t
->interface_type()->is_empty())
3382 go_assert(et
->interface_type() != NULL
3383 && !et
->interface_type()->is_empty());
3384 use_view_convert
= true;
3386 else if (t
->interface_type() != NULL
&& t
->interface_type()->is_empty())
3388 go_assert(et
->interface_type() != NULL
3389 && et
->interface_type()->is_empty());
3390 use_view_convert
= true;
3392 else if (t
->integer_type() != NULL
)
3394 go_assert(et
->is_boolean_type()
3395 || et
->integer_type() != NULL
3396 || et
->function_type() != NULL
3397 || et
->points_to() != NULL
3398 || et
->map_type() != NULL
3399 || et
->channel_type() != NULL
);
3400 return convert_to_integer(type_tree
, expr_tree
);
3405 if (use_view_convert
)
3406 return fold_build1_loc(loc
.gcc_location(), VIEW_CONVERT_EXPR
, type_tree
,
3409 return fold_convert_loc(loc
.gcc_location(), type_tree
, expr_tree
);
3412 // Dump ast representation for an unsafe type conversion expression.
3415 Unsafe_type_conversion_expression::do_dump_expression(
3416 Ast_dump_context
* ast_dump_context
) const
3418 ast_dump_context
->dump_type(this->type_
);
3419 ast_dump_context
->ostream() << "(";
3420 ast_dump_context
->dump_expression(this->expr_
);
3421 ast_dump_context
->ostream() << ") ";
3424 // Make an unsafe type conversion expression.
3427 Expression::make_unsafe_cast(Type
* type
, Expression
* expr
,
3430 return new Unsafe_type_conversion_expression(type
, expr
, location
);
3433 // Unary expressions.
3435 class Unary_expression
: public Expression
3438 Unary_expression(Operator op
, Expression
* expr
, Location location
)
3439 : Expression(EXPRESSION_UNARY
, location
),
3440 op_(op
), escapes_(true), create_temp_(false), expr_(expr
)
3443 // Return the operator.
3446 { return this->op_
; }
3448 // Return the operand.
3451 { return this->expr_
; }
3453 // Record that an address expression does not escape.
3455 set_does_not_escape()
3457 go_assert(this->op_
== OPERATOR_AND
);
3458 this->escapes_
= false;
3461 // Record that this is an address expression which should create a
3462 // temporary variable if necessary. This is used for method calls.
3466 go_assert(this->op_
== OPERATOR_AND
);
3467 this->create_temp_
= true;
3470 // Apply unary opcode OP to UNC, setting NC. Return true if this
3471 // could be done, false if not. Issue errors for overflow.
3473 eval_constant(Operator op
, const Numeric_constant
* unc
,
3474 Location
, Numeric_constant
* nc
);
3481 do_traverse(Traverse
* traverse
)
3482 { return Expression::traverse(&this->expr_
, traverse
); }
3485 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
3488 do_is_constant() const;
3491 do_numeric_constant_value(Numeric_constant
*) const;
3497 do_determine_type(const Type_context
*);
3500 do_check_types(Gogo
*);
3505 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3510 do_must_eval_subexpressions_in_order(int*) const
3511 { return this->op_
== OPERATOR_MULT
; }
3514 do_is_addressable() const
3515 { return this->op_
== OPERATOR_MULT
; }
3518 do_get_tree(Translate_context
*);
3521 do_export(Export
*) const;
3524 do_dump_expression(Ast_dump_context
*) const;
3527 // The unary operator to apply.
3529 // Normally true. False if this is an address expression which does
3530 // not escape the current function.
3532 // True if this is an address expression which should create a
3533 // temporary variable if necessary.
3539 // If we are taking the address of a composite literal, and the
3540 // contents are not constant, then we want to make a heap composite
3544 Unary_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
3546 Location loc
= this->location();
3547 Operator op
= this->op_
;
3548 Expression
* expr
= this->expr_
;
3550 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3551 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3553 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3554 // moving x to the heap. FIXME: Is it worth doing a real escape
3555 // analysis here? This case is found in math/unsafe.go and is
3556 // therefore worth special casing.
3557 if (op
== OPERATOR_MULT
)
3559 Expression
* e
= expr
;
3560 while (e
->classification() == EXPRESSION_CONVERSION
)
3562 Type_conversion_expression
* te
3563 = static_cast<Type_conversion_expression
*>(e
);
3567 if (e
->classification() == EXPRESSION_UNARY
)
3569 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3570 if (ue
->op_
== OPERATOR_AND
)
3577 ue
->set_does_not_escape();
3582 // Catching an invalid indirection of unsafe.Pointer here avoid
3583 // having to deal with TYPE_VOID in other places.
3584 if (op
== OPERATOR_MULT
&& expr
->type()->is_unsafe_pointer_type())
3586 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3587 return Expression::make_error(this->location());
3590 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
|| op
== OPERATOR_XOR
)
3592 Numeric_constant nc
;
3593 if (expr
->numeric_constant_value(&nc
))
3595 Numeric_constant result
;
3596 if (Unary_expression::eval_constant(op
, &nc
, loc
, &result
))
3597 return result
.expression(loc
);
3604 // Return whether a unary expression is a constant.
3607 Unary_expression::do_is_constant() const
3609 if (this->op_
== OPERATOR_MULT
)
3611 // Indirecting through a pointer is only constant if the object
3612 // to which the expression points is constant, but we currently
3613 // have no way to determine that.
3616 else if (this->op_
== OPERATOR_AND
)
3618 // Taking the address of a variable is constant if it is a
3619 // global variable, not constant otherwise. In other cases
3620 // taking the address is probably not a constant.
3621 Var_expression
* ve
= this->expr_
->var_expression();
3624 Named_object
* no
= ve
->named_object();
3625 return no
->is_variable() && no
->var_value()->is_global();
3630 return this->expr_
->is_constant();
3633 // Apply unary opcode OP to UNC, setting NC. Return true if this
3634 // could be done, false if not. Issue errors for overflow.
3637 Unary_expression::eval_constant(Operator op
, const Numeric_constant
* unc
,
3638 Location location
, Numeric_constant
* nc
)
3646 case OPERATOR_MINUS
:
3647 if (unc
->is_int() || unc
->is_rune())
3649 else if (unc
->is_float())
3652 unc
->get_float(&uval
);
3655 mpfr_neg(val
, uval
, GMP_RNDN
);
3656 nc
->set_float(unc
->type(), val
);
3661 else if (unc
->is_complex())
3663 mpfr_t ureal
, uimag
;
3664 unc
->get_complex(&ureal
, &uimag
);
3668 mpfr_neg(real
, ureal
, GMP_RNDN
);
3669 mpfr_neg(imag
, uimag
, GMP_RNDN
);
3670 nc
->set_complex(unc
->type(), real
, imag
);
3692 if (!unc
->is_int() && !unc
->is_rune())
3697 unc
->get_rune(&uval
);
3699 unc
->get_int(&uval
);
3705 case OPERATOR_MINUS
:
3710 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3715 Type
* utype
= unc
->type();
3716 if (utype
->integer_type() == NULL
3717 || utype
->integer_type()->is_abstract())
3721 // The number of HOST_WIDE_INTs that it takes to represent
3723 size_t count
= ((mpz_sizeinbase(uval
, 2)
3724 + HOST_BITS_PER_WIDE_INT
3726 / HOST_BITS_PER_WIDE_INT
);
3728 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3729 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3731 size_t obits
= utype
->integer_type()->bits();
3733 if (!utype
->integer_type()->is_unsigned() && mpz_sgn(uval
) < 0)
3736 mpz_init_set_ui(adj
, 1);
3737 mpz_mul_2exp(adj
, adj
, obits
);
3738 mpz_add(uval
, uval
, adj
);
3743 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3744 go_assert(ecount
<= count
);
3746 // Trim down to the number of words required by the type.
3747 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3748 / HOST_BITS_PER_WIDE_INT
);
3749 go_assert(ocount
<= count
);
3751 for (size_t i
= 0; i
< ocount
; ++i
)
3754 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3756 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3759 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3761 if (!utype
->integer_type()->is_unsigned()
3762 && mpz_tstbit(val
, obits
- 1))
3765 mpz_init_set_ui(adj
, 1);
3766 mpz_mul_2exp(adj
, adj
, obits
);
3767 mpz_sub(val
, val
, adj
);
3781 nc
->set_rune(NULL
, val
);
3783 nc
->set_int(NULL
, val
);
3788 return nc
->set_type(unc
->type(), true, location
);
3791 // Return the integral constant value of a unary expression, if it has one.
3794 Unary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
3796 Numeric_constant unc
;
3797 if (!this->expr_
->numeric_constant_value(&unc
))
3799 return Unary_expression::eval_constant(this->op_
, &unc
, this->location(),
3803 // Return the type of a unary expression.
3806 Unary_expression::do_type()
3811 case OPERATOR_MINUS
:
3814 return this->expr_
->type();
3817 return Type::make_pointer_type(this->expr_
->type());
3821 Type
* subtype
= this->expr_
->type();
3822 Type
* points_to
= subtype
->points_to();
3823 if (points_to
== NULL
)
3824 return Type::make_error_type();
3833 // Determine abstract types for a unary expression.
3836 Unary_expression::do_determine_type(const Type_context
* context
)
3841 case OPERATOR_MINUS
:
3844 this->expr_
->determine_type(context
);
3848 // Taking the address of something.
3850 Type
* subtype
= (context
->type
== NULL
3852 : context
->type
->points_to());
3853 Type_context
subcontext(subtype
, false);
3854 this->expr_
->determine_type(&subcontext
);
3859 // Indirecting through a pointer.
3861 Type
* subtype
= (context
->type
== NULL
3863 : Type::make_pointer_type(context
->type
));
3864 Type_context
subcontext(subtype
, false);
3865 this->expr_
->determine_type(&subcontext
);
3874 // Check types for a unary expression.
3877 Unary_expression::do_check_types(Gogo
*)
3879 Type
* type
= this->expr_
->type();
3880 if (type
->is_error())
3882 this->set_is_error();
3889 case OPERATOR_MINUS
:
3890 if (type
->integer_type() == NULL
3891 && type
->float_type() == NULL
3892 && type
->complex_type() == NULL
)
3893 this->report_error(_("expected numeric type"));
3897 if (!type
->is_boolean_type())
3898 this->report_error(_("expected boolean type"));
3902 if (type
->integer_type() == NULL
3903 && !type
->is_boolean_type())
3904 this->report_error(_("expected integer or boolean type"));
3908 if (!this->expr_
->is_addressable())
3910 if (!this->create_temp_
)
3911 this->report_error(_("invalid operand for unary %<&%>"));
3914 this->expr_
->address_taken(this->escapes_
);
3918 // Indirecting through a pointer.
3919 if (type
->points_to() == NULL
)
3920 this->report_error(_("expected pointer"));
3928 // Get a tree for a unary expression.
3931 Unary_expression::do_get_tree(Translate_context
* context
)
3933 Location loc
= this->location();
3935 // Taking the address of a set-and-use-temporary expression requires
3936 // setting the temporary and then taking the address.
3937 if (this->op_
== OPERATOR_AND
)
3939 Set_and_use_temporary_expression
* sut
=
3940 this->expr_
->set_and_use_temporary_expression();
3943 Temporary_statement
* temp
= sut
->temporary();
3944 Bvariable
* bvar
= temp
->get_backend_variable(context
);
3945 tree var_tree
= var_to_tree(bvar
);
3946 Expression
* val
= sut
->expression();
3947 tree val_tree
= val
->get_tree(context
);
3948 if (var_tree
== error_mark_node
|| val_tree
== error_mark_node
)
3949 return error_mark_node
;
3950 tree addr_tree
= build_fold_addr_expr_loc(loc
.gcc_location(),
3952 return build2_loc(loc
.gcc_location(), COMPOUND_EXPR
,
3953 TREE_TYPE(addr_tree
),
3954 build2_loc(sut
->location().gcc_location(),
3955 MODIFY_EXPR
, void_type_node
,
3956 var_tree
, val_tree
),
3961 tree expr
= this->expr_
->get_tree(context
);
3962 if (expr
== error_mark_node
)
3963 return error_mark_node
;
3970 case OPERATOR_MINUS
:
3972 tree type
= TREE_TYPE(expr
);
3973 tree compute_type
= excess_precision_type(type
);
3974 if (compute_type
!= NULL_TREE
)
3975 expr
= ::convert(compute_type
, expr
);
3976 tree ret
= fold_build1_loc(loc
.gcc_location(), NEGATE_EXPR
,
3977 (compute_type
!= NULL_TREE
3981 if (compute_type
!= NULL_TREE
)
3982 ret
= ::convert(type
, ret
);
3987 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
3988 return fold_build1_loc(loc
.gcc_location(), TRUTH_NOT_EXPR
,
3989 TREE_TYPE(expr
), expr
);
3991 return fold_build2_loc(loc
.gcc_location(), NE_EXPR
, boolean_type_node
,
3992 expr
, build_int_cst(TREE_TYPE(expr
), 0));
3995 return fold_build1_loc(loc
.gcc_location(), BIT_NOT_EXPR
, TREE_TYPE(expr
),
3999 if (!this->create_temp_
)
4001 // We should not see a non-constant constructor here; cases
4002 // where we would see one should have been moved onto the
4003 // heap at parse time. Taking the address of a nonconstant
4004 // constructor will not do what the programmer expects.
4005 go_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
4006 go_assert(TREE_CODE(expr
) != ADDR_EXPR
);
4009 // Build a decl for a constant constructor.
4010 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
4012 tree decl
= build_decl(this->location().gcc_location(), VAR_DECL
,
4013 create_tmp_var_name("C"), TREE_TYPE(expr
));
4014 DECL_EXTERNAL(decl
) = 0;
4015 TREE_PUBLIC(decl
) = 0;
4016 TREE_READONLY(decl
) = 1;
4017 TREE_CONSTANT(decl
) = 1;
4018 TREE_STATIC(decl
) = 1;
4019 TREE_ADDRESSABLE(decl
) = 1;
4020 DECL_ARTIFICIAL(decl
) = 1;
4021 DECL_INITIAL(decl
) = expr
;
4022 rest_of_decl_compilation(decl
, 1, 0);
4026 if (this->create_temp_
4027 && !TREE_ADDRESSABLE(TREE_TYPE(expr
))
4028 && (TREE_CODE(expr
) == CONST_DECL
|| !DECL_P(expr
))
4029 && TREE_CODE(expr
) != INDIRECT_REF
4030 && TREE_CODE(expr
) != COMPONENT_REF
)
4032 if (current_function_decl
!= NULL
)
4034 tree tmp
= create_tmp_var(TREE_TYPE(expr
), get_name(expr
));
4035 DECL_IGNORED_P(tmp
) = 1;
4036 DECL_INITIAL(tmp
) = expr
;
4037 TREE_ADDRESSABLE(tmp
) = 1;
4038 return build2_loc(loc
.gcc_location(), COMPOUND_EXPR
,
4039 build_pointer_type(TREE_TYPE(expr
)),
4040 build1_loc(loc
.gcc_location(), DECL_EXPR
,
4041 void_type_node
, tmp
),
4042 build_fold_addr_expr_loc(loc
.gcc_location(),
4047 tree tmp
= build_decl(loc
.gcc_location(), VAR_DECL
,
4048 create_tmp_var_name("A"), TREE_TYPE(expr
));
4049 DECL_EXTERNAL(tmp
) = 0;
4050 TREE_PUBLIC(tmp
) = 0;
4051 TREE_STATIC(tmp
) = 1;
4052 DECL_ARTIFICIAL(tmp
) = 1;
4053 TREE_ADDRESSABLE(tmp
) = 1;
4055 if (!TREE_CONSTANT(expr
))
4056 make_tmp
= fold_build2_loc(loc
.gcc_location(), INIT_EXPR
,
4057 void_type_node
, tmp
, expr
);
4060 TREE_READONLY(tmp
) = 1;
4061 TREE_CONSTANT(tmp
) = 1;
4062 DECL_INITIAL(tmp
) = expr
;
4063 make_tmp
= NULL_TREE
;
4065 rest_of_decl_compilation(tmp
, 1, 0);
4066 tree addr
= build_fold_addr_expr_loc(loc
.gcc_location(), tmp
);
4067 if (make_tmp
== NULL_TREE
)
4069 return build2_loc(loc
.gcc_location(), COMPOUND_EXPR
,
4070 TREE_TYPE(addr
), make_tmp
, addr
);
4074 return build_fold_addr_expr_loc(loc
.gcc_location(), expr
);
4078 go_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
4080 // If we are dereferencing the pointer to a large struct, we
4081 // need to check for nil. We don't bother to check for small
4082 // structs because we expect the system to crash on a nil
4083 // pointer dereference.
4084 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr
));
4085 if (!VOID_TYPE_P(target_type_tree
))
4087 HOST_WIDE_INT s
= int_size_in_bytes(target_type_tree
);
4088 if (s
== -1 || s
>= 4096)
4091 expr
= save_expr(expr
);
4092 tree compare
= fold_build2_loc(loc
.gcc_location(), EQ_EXPR
,
4095 fold_convert(TREE_TYPE(expr
),
4096 null_pointer_node
));
4097 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4099 expr
= fold_build2_loc(loc
.gcc_location(), COMPOUND_EXPR
,
4100 TREE_TYPE(expr
), build3(COND_EXPR
,
4108 // If the type of EXPR is a recursive pointer type, then we
4109 // need to insert a cast before indirecting.
4110 if (VOID_TYPE_P(target_type_tree
))
4112 Type
* pt
= this->expr_
->type()->points_to();
4113 tree ind
= type_to_tree(pt
->get_backend(context
->gogo()));
4114 expr
= fold_convert_loc(loc
.gcc_location(),
4115 build_pointer_type(ind
), expr
);
4118 return build_fold_indirect_ref_loc(loc
.gcc_location(), expr
);
4126 // Export a unary expression.
4129 Unary_expression::do_export(Export
* exp
) const
4134 exp
->write_c_string("+ ");
4136 case OPERATOR_MINUS
:
4137 exp
->write_c_string("- ");
4140 exp
->write_c_string("! ");
4143 exp
->write_c_string("^ ");
4150 this->expr_
->export_expression(exp
);
4153 // Import a unary expression.
4156 Unary_expression::do_import(Import
* imp
)
4159 switch (imp
->get_char())
4165 op
= OPERATOR_MINUS
;
4176 imp
->require_c_string(" ");
4177 Expression
* expr
= Expression::import_expression(imp
);
4178 return Expression::make_unary(op
, expr
, imp
->location());
4181 // Dump ast representation of an unary expression.
4184 Unary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
4186 ast_dump_context
->dump_operator(this->op_
);
4187 ast_dump_context
->ostream() << "(";
4188 ast_dump_context
->dump_expression(this->expr_
);
4189 ast_dump_context
->ostream() << ") ";
4192 // Make a unary expression.
4195 Expression::make_unary(Operator op
, Expression
* expr
, Location location
)
4197 return new Unary_expression(op
, expr
, location
);
4200 // If this is an indirection through a pointer, return the expression
4201 // being pointed through. Otherwise return this.
4206 if (this->classification_
== EXPRESSION_UNARY
)
4208 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4209 if (ue
->op() == OPERATOR_MULT
)
4210 return ue
->operand();
4215 // Class Binary_expression.
4220 Binary_expression::do_traverse(Traverse
* traverse
)
4222 int t
= Expression::traverse(&this->left_
, traverse
);
4223 if (t
== TRAVERSE_EXIT
)
4224 return TRAVERSE_EXIT
;
4225 return Expression::traverse(&this->right_
, traverse
);
4228 // Return the type to use for a binary operation on operands of
4229 // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
4230 // such may be NULL or abstract.
4233 Binary_expression::operation_type(Operator op
, Type
* left_type
,
4234 Type
* right_type
, Type
** result_type
)
4236 if (left_type
!= right_type
4237 && !left_type
->is_abstract()
4238 && !right_type
->is_abstract()
4239 && left_type
->base() != right_type
->base()
4240 && op
!= OPERATOR_LSHIFT
4241 && op
!= OPERATOR_RSHIFT
)
4243 // May be a type error--let it be diagnosed elsewhere.
4247 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4249 if (left_type
->integer_type() != NULL
)
4250 *result_type
= left_type
;
4252 *result_type
= Type::make_abstract_integer_type();
4254 else if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
4255 *result_type
= left_type
;
4256 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
4257 *result_type
= right_type
;
4258 else if (!left_type
->is_abstract())
4259 *result_type
= left_type
;
4260 else if (!right_type
->is_abstract())
4261 *result_type
= right_type
;
4262 else if (left_type
->complex_type() != NULL
)
4263 *result_type
= left_type
;
4264 else if (right_type
->complex_type() != NULL
)
4265 *result_type
= right_type
;
4266 else if (left_type
->float_type() != NULL
)
4267 *result_type
= left_type
;
4268 else if (right_type
->float_type() != NULL
)
4269 *result_type
= right_type
;
4270 else if (left_type
->integer_type() != NULL
4271 && left_type
->integer_type()->is_rune())
4272 *result_type
= left_type
;
4273 else if (right_type
->integer_type() != NULL
4274 && right_type
->integer_type()->is_rune())
4275 *result_type
= right_type
;
4277 *result_type
= left_type
;
4282 // Convert an integer comparison code and an operator to a boolean
4286 Binary_expression::cmp_to_bool(Operator op
, int cmp
)
4293 case OPERATOR_NOTEQ
:
4310 // Compare constants according to OP.
4313 Binary_expression::compare_constant(Operator op
, Numeric_constant
* left_nc
,
4314 Numeric_constant
* right_nc
,
4315 Location location
, bool* result
)
4317 Type
* left_type
= left_nc
->type();
4318 Type
* right_type
= right_nc
->type();
4321 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
4324 // When comparing an untyped operand to a typed operand, we are
4325 // effectively coercing the untyped operand to the other operand's
4326 // type, so make sure that is valid.
4327 if (!left_nc
->set_type(type
, true, location
)
4328 || !right_nc
->set_type(type
, true, location
))
4333 if (type
->complex_type() != NULL
)
4335 if (op
!= OPERATOR_EQEQ
&& op
!= OPERATOR_NOTEQ
)
4337 ret
= Binary_expression::compare_complex(left_nc
, right_nc
, &cmp
);
4339 else if (type
->float_type() != NULL
)
4340 ret
= Binary_expression::compare_float(left_nc
, right_nc
, &cmp
);
4342 ret
= Binary_expression::compare_integer(left_nc
, right_nc
, &cmp
);
4345 *result
= Binary_expression::cmp_to_bool(op
, cmp
);
4350 // Compare integer constants.
4353 Binary_expression::compare_integer(const Numeric_constant
* left_nc
,
4354 const Numeric_constant
* right_nc
,
4358 if (!left_nc
->to_int(&left_val
))
4361 if (!right_nc
->to_int(&right_val
))
4363 mpz_clear(left_val
);
4367 *cmp
= mpz_cmp(left_val
, right_val
);
4369 mpz_clear(left_val
);
4370 mpz_clear(right_val
);
4375 // Compare floating point constants.
4378 Binary_expression::compare_float(const Numeric_constant
* left_nc
,
4379 const Numeric_constant
* right_nc
,
4383 if (!left_nc
->to_float(&left_val
))
4386 if (!right_nc
->to_float(&right_val
))
4388 mpfr_clear(left_val
);
4392 // We already coerced both operands to the same type. If that type
4393 // is not an abstract type, we need to round the values accordingly.
4394 Type
* type
= left_nc
->type();
4395 if (!type
->is_abstract() && type
->float_type() != NULL
)
4397 int bits
= type
->float_type()->bits();
4398 mpfr_prec_round(left_val
, bits
, GMP_RNDN
);
4399 mpfr_prec_round(right_val
, bits
, GMP_RNDN
);
4402 *cmp
= mpfr_cmp(left_val
, right_val
);
4404 mpfr_clear(left_val
);
4405 mpfr_clear(right_val
);
4410 // Compare complex constants. Complex numbers may only be compared
4414 Binary_expression::compare_complex(const Numeric_constant
* left_nc
,
4415 const Numeric_constant
* right_nc
,
4418 mpfr_t left_real
, left_imag
;
4419 if (!left_nc
->to_complex(&left_real
, &left_imag
))
4421 mpfr_t right_real
, right_imag
;
4422 if (!right_nc
->to_complex(&right_real
, &right_imag
))
4424 mpfr_clear(left_real
);
4425 mpfr_clear(left_imag
);
4429 // We already coerced both operands to the same type. If that type
4430 // is not an abstract type, we need to round the values accordingly.
4431 Type
* type
= left_nc
->type();
4432 if (!type
->is_abstract() && type
->complex_type() != NULL
)
4434 int bits
= type
->complex_type()->bits();
4435 mpfr_prec_round(left_real
, bits
/ 2, GMP_RNDN
);
4436 mpfr_prec_round(left_imag
, bits
/ 2, GMP_RNDN
);
4437 mpfr_prec_round(right_real
, bits
/ 2, GMP_RNDN
);
4438 mpfr_prec_round(right_imag
, bits
/ 2, GMP_RNDN
);
4441 *cmp
= (mpfr_cmp(left_real
, right_real
) != 0
4442 || mpfr_cmp(left_imag
, right_imag
) != 0);
4444 mpfr_clear(left_real
);
4445 mpfr_clear(left_imag
);
4446 mpfr_clear(right_real
);
4447 mpfr_clear(right_imag
);
4452 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
4453 // true if this could be done, false if not. Issue errors at LOCATION
4457 Binary_expression::eval_constant(Operator op
, Numeric_constant
* left_nc
,
4458 Numeric_constant
* right_nc
,
4459 Location location
, Numeric_constant
* nc
)
4464 case OPERATOR_ANDAND
:
4466 case OPERATOR_NOTEQ
:
4471 // These return boolean values, not numeric.
4477 Type
* left_type
= left_nc
->type();
4478 Type
* right_type
= right_nc
->type();
4481 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
4484 bool is_shift
= op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
;
4486 // When combining an untyped operand with a typed operand, we are
4487 // effectively coercing the untyped operand to the other operand's
4488 // type, so make sure that is valid.
4489 if (!left_nc
->set_type(type
, true, location
))
4491 if (!is_shift
&& !right_nc
->set_type(type
, true, location
))
4495 if (type
->complex_type() != NULL
)
4496 r
= Binary_expression::eval_complex(op
, left_nc
, right_nc
, location
, nc
);
4497 else if (type
->float_type() != NULL
)
4498 r
= Binary_expression::eval_float(op
, left_nc
, right_nc
, location
, nc
);
4500 r
= Binary_expression::eval_integer(op
, left_nc
, right_nc
, location
, nc
);
4503 r
= nc
->set_type(type
, true, location
);
4508 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4509 // integer operations. Return true if this could be done, false if
4513 Binary_expression::eval_integer(Operator op
, const Numeric_constant
* left_nc
,
4514 const Numeric_constant
* right_nc
,
4515 Location location
, Numeric_constant
* nc
)
4518 if (!left_nc
->to_int(&left_val
))
4521 if (!right_nc
->to_int(&right_val
))
4523 mpz_clear(left_val
);
4533 mpz_add(val
, left_val
, right_val
);
4535 case OPERATOR_MINUS
:
4536 mpz_sub(val
, left_val
, right_val
);
4539 mpz_ior(val
, left_val
, right_val
);
4542 mpz_xor(val
, left_val
, right_val
);
4545 mpz_mul(val
, left_val
, right_val
);
4548 if (mpz_sgn(right_val
) != 0)
4549 mpz_tdiv_q(val
, left_val
, right_val
);
4552 error_at(location
, "division by zero");
4557 if (mpz_sgn(right_val
) != 0)
4558 mpz_tdiv_r(val
, left_val
, right_val
);
4561 error_at(location
, "division by zero");
4565 case OPERATOR_LSHIFT
:
4567 unsigned long shift
= mpz_get_ui(right_val
);
4568 if (mpz_cmp_ui(right_val
, shift
) == 0 && shift
<= 0x100000)
4569 mpz_mul_2exp(val
, left_val
, shift
);
4572 error_at(location
, "shift count overflow");
4578 case OPERATOR_RSHIFT
:
4580 unsigned long shift
= mpz_get_ui(right_val
);
4581 if (mpz_cmp_ui(right_val
, shift
) != 0)
4583 error_at(location
, "shift count overflow");
4588 if (mpz_cmp_ui(left_val
, 0) >= 0)
4589 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4591 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4597 mpz_and(val
, left_val
, right_val
);
4599 case OPERATOR_BITCLEAR
:
4603 mpz_com(tval
, right_val
);
4604 mpz_and(val
, left_val
, tval
);
4612 mpz_clear(left_val
);
4613 mpz_clear(right_val
);
4615 if (left_nc
->is_rune()
4616 || (op
!= OPERATOR_LSHIFT
4617 && op
!= OPERATOR_RSHIFT
4618 && right_nc
->is_rune()))
4619 nc
->set_rune(NULL
, val
);
4621 nc
->set_int(NULL
, val
);
4628 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4629 // floating point operations. Return true if this could be done,
4633 Binary_expression::eval_float(Operator op
, const Numeric_constant
* left_nc
,
4634 const Numeric_constant
* right_nc
,
4635 Location location
, Numeric_constant
* nc
)
4638 if (!left_nc
->to_float(&left_val
))
4641 if (!right_nc
->to_float(&right_val
))
4643 mpfr_clear(left_val
);
4654 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4656 case OPERATOR_MINUS
:
4657 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4662 case OPERATOR_BITCLEAR
:
4664 case OPERATOR_LSHIFT
:
4665 case OPERATOR_RSHIFT
:
4666 mpfr_set_ui(val
, 0, GMP_RNDN
);
4670 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4673 if (!mpfr_zero_p(right_val
))
4674 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4677 error_at(location
, "division by zero");
4678 mpfr_set_ui(val
, 0, GMP_RNDN
);
4685 mpfr_clear(left_val
);
4686 mpfr_clear(right_val
);
4688 nc
->set_float(NULL
, val
);
4694 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4695 // complex operations. Return true if this could be done, false if
4699 Binary_expression::eval_complex(Operator op
, const Numeric_constant
* left_nc
,
4700 const Numeric_constant
* right_nc
,
4701 Location location
, Numeric_constant
* nc
)
4703 mpfr_t left_real
, left_imag
;
4704 if (!left_nc
->to_complex(&left_real
, &left_imag
))
4706 mpfr_t right_real
, right_imag
;
4707 if (!right_nc
->to_complex(&right_real
, &right_imag
))
4709 mpfr_clear(left_real
);
4710 mpfr_clear(left_imag
);
4722 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4723 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4725 case OPERATOR_MINUS
:
4726 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4727 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4732 case OPERATOR_BITCLEAR
:
4734 case OPERATOR_LSHIFT
:
4735 case OPERATOR_RSHIFT
:
4736 mpfr_set_ui(real
, 0, GMP_RNDN
);
4737 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4742 // You might think that multiplying two complex numbers would
4743 // be simple, and you would be right, until you start to think
4744 // about getting the right answer for infinity. If one
4745 // operand here is infinity and the other is anything other
4746 // than zero or NaN, then we are going to wind up subtracting
4747 // two infinity values. That will give us a NaN, but the
4748 // correct answer is infinity.
4752 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4756 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4760 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4764 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4766 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4767 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4769 // If we get NaN on both sides, check whether it should really
4770 // be infinity. The rule is that if either side of the
4771 // complex number is infinity, then the whole value is
4772 // infinity, even if the other side is NaN. So the only case
4773 // we have to fix is the one in which both sides are NaN.
4774 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4775 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4776 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4778 bool is_infinity
= false;
4782 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4783 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4787 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4788 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4790 // If the left side is infinity, then the result is
4792 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4794 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4795 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4796 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4797 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4800 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4801 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4805 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4806 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4811 // If the right side is infinity, then the result is
4813 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4815 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4816 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4817 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4818 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4821 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4822 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4826 mpfr_set_ui(li
, 0, GMP_RNDN
);
4827 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4832 // If we got an overflow in the intermediate computations,
4833 // then the result is infinity.
4835 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4836 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4840 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4841 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4845 mpfr_set_ui(li
, 0, GMP_RNDN
);
4846 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4850 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4851 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4855 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4856 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4863 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4864 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4865 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4866 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4867 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4868 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4869 mpfr_set_inf(real
, mpfr_sgn(real
));
4870 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4887 // For complex division we want to avoid having an
4888 // intermediate overflow turn the whole result in a NaN. We
4889 // scale the values to try to avoid this.
4891 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4893 error_at(location
, "division by zero");
4894 mpfr_set_ui(real
, 0, GMP_RNDN
);
4895 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4903 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4904 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4907 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4911 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4912 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4914 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4916 ilogbw
= mpfr_get_exp(t
);
4917 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4918 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4923 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4924 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4925 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4927 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4928 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4929 mpfr_add(real
, real
, t
, GMP_RNDN
);
4930 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4931 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4933 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4934 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4935 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4936 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4937 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4939 // If we wind up with NaN on both sides, check whether we
4940 // should really have infinity. The rule is that if either
4941 // side of the complex number is infinity, then the whole
4942 // value is infinity, even if the other side is NaN. So the
4943 // only case we have to fix is the one in which both sides are
4945 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4946 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4947 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4949 if (mpfr_zero_p(denom
))
4951 mpfr_set_inf(real
, mpfr_sgn(rr
));
4952 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4953 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4954 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4956 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4957 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4959 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4960 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4963 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4964 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4968 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4972 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4974 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4975 mpfr_set_inf(real
, mpfr_sgn(t3
));
4977 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4978 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4979 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4980 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4986 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4987 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4989 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4990 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4993 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4994 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4998 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
5002 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
5004 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
5005 mpfr_set_ui(real
, 0, GMP_RNDN
);
5006 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
5008 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
5009 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
5010 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
5011 mpfr_set_ui(imag
, 0, GMP_RNDN
);
5012 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
5032 mpfr_clear(left_real
);
5033 mpfr_clear(left_imag
);
5034 mpfr_clear(right_real
);
5035 mpfr_clear(right_imag
);
5037 nc
->set_complex(NULL
, real
, imag
);
5044 // Lower a binary expression. We have to evaluate constant
5045 // expressions now, in order to implement Go's unlimited precision
5049 Binary_expression::do_lower(Gogo
* gogo
, Named_object
*,
5050 Statement_inserter
* inserter
, int)
5052 Location location
= this->location();
5053 Operator op
= this->op_
;
5054 Expression
* left
= this->left_
;
5055 Expression
* right
= this->right_
;
5057 const bool is_comparison
= (op
== OPERATOR_EQEQ
5058 || op
== OPERATOR_NOTEQ
5059 || op
== OPERATOR_LT
5060 || op
== OPERATOR_LE
5061 || op
== OPERATOR_GT
5062 || op
== OPERATOR_GE
);
5064 // Numeric constant expressions.
5066 Numeric_constant left_nc
;
5067 Numeric_constant right_nc
;
5068 if (left
->numeric_constant_value(&left_nc
)
5069 && right
->numeric_constant_value(&right_nc
))
5074 if (!Binary_expression::compare_constant(op
, &left_nc
,
5075 &right_nc
, location
,
5078 return Expression::make_cast(Type::make_boolean_type(),
5079 Expression::make_boolean(result
,
5085 Numeric_constant nc
;
5086 if (!Binary_expression::eval_constant(op
, &left_nc
, &right_nc
,
5089 return nc
.expression(location
);
5094 // String constant expressions.
5095 if (left
->type()->is_string_type() && right
->type()->is_string_type())
5097 std::string left_string
;
5098 std::string right_string
;
5099 if (left
->string_constant_value(&left_string
)
5100 && right
->string_constant_value(&right_string
))
5102 if (op
== OPERATOR_PLUS
)
5103 return Expression::make_string(left_string
+ right_string
,
5105 else if (is_comparison
)
5107 int cmp
= left_string
.compare(right_string
);
5108 bool r
= Binary_expression::cmp_to_bool(op
, cmp
);
5109 return Expression::make_boolean(r
, location
);
5114 // Lower struct and array comparisons.
5115 if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
5117 if (left
->type()->struct_type() != NULL
)
5118 return this->lower_struct_comparison(gogo
, inserter
);
5119 else if (left
->type()->array_type() != NULL
5120 && !left
->type()->is_slice_type())
5121 return this->lower_array_comparison(gogo
, inserter
);
5127 // Lower a struct comparison.
5130 Binary_expression::lower_struct_comparison(Gogo
* gogo
,
5131 Statement_inserter
* inserter
)
5133 Struct_type
* st
= this->left_
->type()->struct_type();
5134 Struct_type
* st2
= this->right_
->type()->struct_type();
5137 if (st
!= st2
&& !Type::are_identical(st
, st2
, false, NULL
))
5139 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
5140 this->right_
->type(), NULL
))
5143 // See if we can compare using memcmp. As a heuristic, we use
5144 // memcmp rather than field references and comparisons if there are
5145 // more than two fields.
5146 if (st
->compare_is_identity(gogo
) && st
->total_field_count() > 2)
5147 return this->lower_compare_to_memcmp(gogo
, inserter
);
5149 Location loc
= this->location();
5151 Expression
* left
= this->left_
;
5152 Temporary_statement
* left_temp
= NULL
;
5153 if (left
->var_expression() == NULL
5154 && left
->temporary_reference_expression() == NULL
)
5156 left_temp
= Statement::make_temporary(left
->type(), NULL
, loc
);
5157 inserter
->insert(left_temp
);
5158 left
= Expression::make_set_and_use_temporary(left_temp
, left
, loc
);
5161 Expression
* right
= this->right_
;
5162 Temporary_statement
* right_temp
= NULL
;
5163 if (right
->var_expression() == NULL
5164 && right
->temporary_reference_expression() == NULL
)
5166 right_temp
= Statement::make_temporary(right
->type(), NULL
, loc
);
5167 inserter
->insert(right_temp
);
5168 right
= Expression::make_set_and_use_temporary(right_temp
, right
, loc
);
5171 Expression
* ret
= Expression::make_boolean(true, loc
);
5172 const Struct_field_list
* fields
= st
->fields();
5173 unsigned int field_index
= 0;
5174 for (Struct_field_list::const_iterator pf
= fields
->begin();
5175 pf
!= fields
->end();
5176 ++pf
, ++field_index
)
5178 if (field_index
> 0)
5180 if (left_temp
== NULL
)
5181 left
= left
->copy();
5183 left
= Expression::make_temporary_reference(left_temp
, loc
);
5184 if (right_temp
== NULL
)
5185 right
= right
->copy();
5187 right
= Expression::make_temporary_reference(right_temp
, loc
);
5189 Expression
* f1
= Expression::make_field_reference(left
, field_index
,
5191 Expression
* f2
= Expression::make_field_reference(right
, field_index
,
5193 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, f1
, f2
, loc
);
5194 ret
= Expression::make_binary(OPERATOR_ANDAND
, ret
, cond
, loc
);
5197 if (this->op_
== OPERATOR_NOTEQ
)
5198 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
5203 // Lower an array comparison.
5206 Binary_expression::lower_array_comparison(Gogo
* gogo
,
5207 Statement_inserter
* inserter
)
5209 Array_type
* at
= this->left_
->type()->array_type();
5210 Array_type
* at2
= this->right_
->type()->array_type();
5213 if (at
!= at2
&& !Type::are_identical(at
, at2
, false, NULL
))
5215 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
5216 this->right_
->type(), NULL
))
5219 // Call memcmp directly if possible. This may let the middle-end
5220 // optimize the call.
5221 if (at
->compare_is_identity(gogo
))
5222 return this->lower_compare_to_memcmp(gogo
, inserter
);
5224 // Call the array comparison function.
5225 Named_object
* hash_fn
;
5226 Named_object
* equal_fn
;
5227 at
->type_functions(gogo
, this->left_
->type()->named_type(), NULL
, NULL
,
5228 &hash_fn
, &equal_fn
);
5230 Location loc
= this->location();
5232 Expression
* func
= Expression::make_func_reference(equal_fn
, NULL
, loc
);
5234 Expression_list
* args
= new Expression_list();
5235 args
->push_back(this->operand_address(inserter
, this->left_
));
5236 args
->push_back(this->operand_address(inserter
, this->right_
));
5237 args
->push_back(Expression::make_type_info(at
, TYPE_INFO_SIZE
));
5239 Expression
* ret
= Expression::make_call(func
, args
, false, loc
);
5241 if (this->op_
== OPERATOR_NOTEQ
)
5242 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
5247 // Lower a struct or array comparison to a call to memcmp.
5250 Binary_expression::lower_compare_to_memcmp(Gogo
*, Statement_inserter
* inserter
)
5252 Location loc
= this->location();
5254 Expression
* a1
= this->operand_address(inserter
, this->left_
);
5255 Expression
* a2
= this->operand_address(inserter
, this->right_
);
5256 Expression
* len
= Expression::make_type_info(this->left_
->type(),
5259 Expression
* call
= Runtime::make_call(Runtime::MEMCMP
, loc
, 3, a1
, a2
, len
);
5262 mpz_init_set_ui(zval
, 0);
5263 Expression
* zero
= Expression::make_integer(&zval
, NULL
, loc
);
5266 return Expression::make_binary(this->op_
, call
, zero
, loc
);
5269 // Return the address of EXPR, cast to unsafe.Pointer.
5272 Binary_expression::operand_address(Statement_inserter
* inserter
,
5275 Location loc
= this->location();
5277 if (!expr
->is_addressable())
5279 Temporary_statement
* temp
= Statement::make_temporary(expr
->type(), NULL
,
5281 inserter
->insert(temp
);
5282 expr
= Expression::make_set_and_use_temporary(temp
, expr
, loc
);
5284 expr
= Expression::make_unary(OPERATOR_AND
, expr
, loc
);
5285 static_cast<Unary_expression
*>(expr
)->set_does_not_escape();
5286 Type
* void_type
= Type::make_void_type();
5287 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
5288 return Expression::make_cast(unsafe_pointer_type
, expr
, loc
);
5291 // Return the numeric constant value, if it has one.
5294 Binary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
5296 Numeric_constant left_nc
;
5297 if (!this->left_
->numeric_constant_value(&left_nc
))
5299 Numeric_constant right_nc
;
5300 if (!this->right_
->numeric_constant_value(&right_nc
))
5302 return Binary_expression::eval_constant(this->op_
, &left_nc
, &right_nc
,
5303 this->location(), nc
);
5306 // Note that the value is being discarded.
5309 Binary_expression::do_discarding_value()
5311 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5312 this->right_
->discarding_value();
5314 this->unused_value_error();
5320 Binary_expression::do_type()
5322 if (this->classification() == EXPRESSION_ERROR
)
5323 return Type::make_error_type();
5328 case OPERATOR_NOTEQ
:
5333 if (this->type_
== NULL
)
5334 this->type_
= Type::make_boolean_type();
5338 case OPERATOR_MINUS
:
5345 case OPERATOR_BITCLEAR
:
5347 case OPERATOR_ANDAND
:
5350 if (!Binary_expression::operation_type(this->op_
,
5351 this->left_
->type(),
5352 this->right_
->type(),
5354 return Type::make_error_type();
5358 case OPERATOR_LSHIFT
:
5359 case OPERATOR_RSHIFT
:
5360 return this->left_
->type();
5367 // Set type for a binary expression.
5370 Binary_expression::do_determine_type(const Type_context
* context
)
5372 Type
* tleft
= this->left_
->type();
5373 Type
* tright
= this->right_
->type();
5375 // Both sides should have the same type, except for the shift
5376 // operations. For a comparison, we should ignore the incoming
5379 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5380 || this->op_
== OPERATOR_RSHIFT
);
5382 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5383 || this->op_
== OPERATOR_NOTEQ
5384 || this->op_
== OPERATOR_LT
5385 || this->op_
== OPERATOR_LE
5386 || this->op_
== OPERATOR_GT
5387 || this->op_
== OPERATOR_GE
);
5389 Type_context
subcontext(*context
);
5393 // In a comparison, the context does not determine the types of
5395 subcontext
.type
= NULL
;
5398 // Set the context for the left hand operand.
5401 // The right hand operand of a shift plays no role in
5402 // determining the type of the left hand operand.
5404 else if (!tleft
->is_abstract())
5405 subcontext
.type
= tleft
;
5406 else if (!tright
->is_abstract())
5407 subcontext
.type
= tright
;
5408 else if (subcontext
.type
== NULL
)
5410 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5411 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5412 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5414 // Both sides have an abstract integer, abstract float, or
5415 // abstract complex type. Just let CONTEXT determine
5416 // whether they may remain abstract or not.
5418 else if (tleft
->complex_type() != NULL
)
5419 subcontext
.type
= tleft
;
5420 else if (tright
->complex_type() != NULL
)
5421 subcontext
.type
= tright
;
5422 else if (tleft
->float_type() != NULL
)
5423 subcontext
.type
= tleft
;
5424 else if (tright
->float_type() != NULL
)
5425 subcontext
.type
= tright
;
5427 subcontext
.type
= tleft
;
5429 if (subcontext
.type
!= NULL
&& !context
->may_be_abstract
)
5430 subcontext
.type
= subcontext
.type
->make_non_abstract_type();
5433 this->left_
->determine_type(&subcontext
);
5437 // We may have inherited an unusable type for the shift operand.
5438 // Give a useful error if that happened.
5439 if (tleft
->is_abstract()
5440 && subcontext
.type
!= NULL
5441 && (this->left_
->type()->integer_type() == NULL
5442 || (subcontext
.type
->integer_type() == NULL
5443 && subcontext
.type
->float_type() == NULL
5444 && subcontext
.type
->complex_type() == NULL
)))
5445 this->report_error(("invalid context-determined non-integer type "
5446 "for shift operand"));
5448 // The context for the right hand operand is the same as for the
5449 // left hand operand, except for a shift operator.
5450 subcontext
.type
= Type::lookup_integer_type("uint");
5451 subcontext
.may_be_abstract
= false;
5454 this->right_
->determine_type(&subcontext
);
5458 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
5460 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
5461 this->type_
= context
->type
;
5462 else if (!context
->may_be_abstract
)
5463 this->type_
= Type::lookup_bool_type();
5467 // Report an error if the binary operator OP does not support TYPE.
5468 // OTYPE is the type of the other operand. Return whether the
5469 // operation is OK. This should not be used for shift.
5472 Binary_expression::check_operator_type(Operator op
, Type
* type
, Type
* otype
,
5478 case OPERATOR_ANDAND
:
5479 if (!type
->is_boolean_type())
5481 error_at(location
, "expected boolean type");
5487 case OPERATOR_NOTEQ
:
5490 if (!Type::are_compatible_for_comparison(true, type
, otype
, &reason
))
5492 error_at(location
, "%s", reason
.c_str());
5504 if (!Type::are_compatible_for_comparison(false, type
, otype
, &reason
))
5506 error_at(location
, "%s", reason
.c_str());
5513 case OPERATOR_PLUSEQ
:
5514 if (type
->integer_type() == NULL
5515 && type
->float_type() == NULL
5516 && type
->complex_type() == NULL
5517 && !type
->is_string_type())
5520 "expected integer, floating, complex, or string type");
5525 case OPERATOR_MINUS
:
5526 case OPERATOR_MINUSEQ
:
5528 case OPERATOR_MULTEQ
:
5530 case OPERATOR_DIVEQ
:
5531 if (type
->integer_type() == NULL
5532 && type
->float_type() == NULL
5533 && type
->complex_type() == NULL
)
5535 error_at(location
, "expected integer, floating, or complex type");
5541 case OPERATOR_MODEQ
:
5545 case OPERATOR_ANDEQ
:
5547 case OPERATOR_XOREQ
:
5548 case OPERATOR_BITCLEAR
:
5549 case OPERATOR_BITCLEAREQ
:
5550 if (type
->integer_type() == NULL
)
5552 error_at(location
, "expected integer type");
5567 Binary_expression::do_check_types(Gogo
*)
5569 if (this->classification() == EXPRESSION_ERROR
)
5572 Type
* left_type
= this->left_
->type();
5573 Type
* right_type
= this->right_
->type();
5574 if (left_type
->is_error() || right_type
->is_error())
5576 this->set_is_error();
5580 if (this->op_
== OPERATOR_EQEQ
5581 || this->op_
== OPERATOR_NOTEQ
5582 || this->op_
== OPERATOR_LT
5583 || this->op_
== OPERATOR_LE
5584 || this->op_
== OPERATOR_GT
5585 || this->op_
== OPERATOR_GE
)
5587 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5588 && !Type::are_assignable(right_type
, left_type
, NULL
))
5590 this->report_error(_("incompatible types in binary expression"));
5593 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5596 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5600 this->set_is_error();
5604 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5606 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5608 this->report_error(_("incompatible types in binary expression"));
5611 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5615 this->set_is_error();
5621 if (left_type
->integer_type() == NULL
)
5622 this->report_error(_("shift of non-integer operand"));
5624 if (!right_type
->is_abstract()
5625 && (right_type
->integer_type() == NULL
5626 || !right_type
->integer_type()->is_unsigned()))
5627 this->report_error(_("shift count not unsigned integer"));
5630 Numeric_constant nc
;
5631 if (this->right_
->numeric_constant_value(&nc
))
5634 if (!nc
.to_int(&val
))
5635 this->report_error(_("shift count not unsigned integer"));
5638 if (mpz_sgn(val
) < 0)
5640 this->report_error(_("negative shift count"));
5642 Location rloc
= this->right_
->location();
5643 this->right_
= Expression::make_integer(&val
, right_type
,
5653 // Get a tree for a binary expression.
5656 Binary_expression::do_get_tree(Translate_context
* context
)
5658 tree left
= this->left_
->get_tree(context
);
5659 tree right
= this->right_
->get_tree(context
);
5661 if (left
== error_mark_node
|| right
== error_mark_node
)
5662 return error_mark_node
;
5664 enum tree_code code
;
5665 bool use_left_type
= true;
5666 bool is_shift_op
= false;
5667 bool is_idiv_op
= false;
5671 case OPERATOR_NOTEQ
:
5676 return Expression::comparison_tree(context
, this->type_
, this->op_
,
5677 this->left_
->type(), left
,
5678 this->right_
->type(), right
,
5682 code
= TRUTH_ORIF_EXPR
;
5683 use_left_type
= false;
5685 case OPERATOR_ANDAND
:
5686 code
= TRUTH_ANDIF_EXPR
;
5687 use_left_type
= false;
5692 case OPERATOR_MINUS
:
5696 code
= BIT_IOR_EXPR
;
5699 code
= BIT_XOR_EXPR
;
5706 Type
*t
= this->left_
->type();
5707 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5711 code
= TRUNC_DIV_EXPR
;
5717 code
= TRUNC_MOD_EXPR
;
5720 case OPERATOR_LSHIFT
:
5724 case OPERATOR_RSHIFT
:
5729 code
= BIT_AND_EXPR
;
5731 case OPERATOR_BITCLEAR
:
5732 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5733 code
= BIT_AND_EXPR
;
5739 location_t gccloc
= this->location().gcc_location();
5740 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5742 if (this->left_
->type()->is_string_type())
5744 go_assert(this->op_
== OPERATOR_PLUS
);
5745 Type
* st
= Type::make_string_type();
5746 tree string_type
= type_to_tree(st
->get_backend(context
->gogo()));
5747 static tree string_plus_decl
;
5748 return Gogo::call_builtin(&string_plus_decl
,
5759 tree compute_type
= excess_precision_type(type
);
5760 if (compute_type
!= NULL_TREE
)
5762 left
= ::convert(compute_type
, left
);
5763 right
= ::convert(compute_type
, right
);
5766 tree eval_saved
= NULL_TREE
;
5768 || (is_idiv_op
&& (go_check_divide_zero
|| go_check_divide_overflow
)))
5770 // Make sure the values are evaluated.
5773 left
= save_expr(left
);
5778 right
= save_expr(right
);
5779 if (eval_saved
== NULL_TREE
)
5782 eval_saved
= fold_build2_loc(gccloc
, COMPOUND_EXPR
,
5783 void_type_node
, eval_saved
, right
);
5787 tree ret
= fold_build2_loc(gccloc
, code
,
5788 compute_type
!= NULL_TREE
? compute_type
: type
,
5791 if (compute_type
!= NULL_TREE
)
5792 ret
= ::convert(type
, ret
);
5794 // In Go, a shift larger than the size of the type is well-defined.
5795 // This is not true in GENERIC, so we need to insert a conditional.
5798 go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5799 go_assert(this->left_
->type()->integer_type() != NULL
);
5800 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5802 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5803 build_int_cst_type(TREE_TYPE(right
), bits
));
5805 tree overflow_result
= fold_convert_loc(gccloc
, TREE_TYPE(left
),
5807 if (this->op_
== OPERATOR_RSHIFT
5808 && !this->left_
->type()->integer_type()->is_unsigned())
5811 fold_build2_loc(gccloc
, LT_EXPR
, boolean_type_node
,
5813 fold_convert_loc(gccloc
, TREE_TYPE(left
),
5814 integer_zero_node
));
5816 fold_build2_loc(gccloc
, MINUS_EXPR
, TREE_TYPE(left
),
5817 fold_convert_loc(gccloc
, TREE_TYPE(left
),
5819 fold_convert_loc(gccloc
, TREE_TYPE(left
),
5822 fold_build3_loc(gccloc
, COND_EXPR
, TREE_TYPE(left
),
5823 neg
, neg_one
, overflow_result
);
5826 ret
= fold_build3_loc(gccloc
, COND_EXPR
, TREE_TYPE(left
),
5827 compare
, ret
, overflow_result
);
5829 if (eval_saved
!= NULL_TREE
)
5830 ret
= fold_build2_loc(gccloc
, COMPOUND_EXPR
, TREE_TYPE(ret
),
5834 // Add checks for division by zero and division overflow as needed.
5837 if (go_check_divide_zero
)
5840 tree check
= fold_build2_loc(gccloc
, EQ_EXPR
, boolean_type_node
,
5842 fold_convert_loc(gccloc
,
5844 integer_zero_node
));
5846 // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO), 0
5847 int errcode
= RUNTIME_ERROR_DIVISION_BY_ZERO
;
5848 tree panic
= fold_build2_loc(gccloc
, COMPOUND_EXPR
, TREE_TYPE(ret
),
5849 Gogo::runtime_error(errcode
,
5851 fold_convert_loc(gccloc
, TREE_TYPE(ret
),
5852 integer_zero_node
));
5854 // right == 0 ? (__go_runtime_error(...), 0) : ret
5855 ret
= fold_build3_loc(gccloc
, COND_EXPR
, TREE_TYPE(ret
),
5859 if (go_check_divide_overflow
)
5862 // FIXME: It would be nice to say that this test is expected
5864 tree m1
= integer_minus_one_node
;
5865 tree check
= fold_build2_loc(gccloc
, EQ_EXPR
, boolean_type_node
,
5867 fold_convert_loc(gccloc
,
5872 if (TYPE_UNSIGNED(TREE_TYPE(ret
)))
5874 // An unsigned -1 is the largest possible number, so
5875 // dividing is always 1 or 0.
5876 tree cmp
= fold_build2_loc(gccloc
, EQ_EXPR
, boolean_type_node
,
5878 if (this->op_
== OPERATOR_DIV
)
5879 overflow
= fold_build3_loc(gccloc
, COND_EXPR
, TREE_TYPE(ret
),
5881 fold_convert_loc(gccloc
,
5884 fold_convert_loc(gccloc
,
5886 integer_zero_node
));
5888 overflow
= fold_build3_loc(gccloc
, COND_EXPR
, TREE_TYPE(ret
),
5890 fold_convert_loc(gccloc
,
5897 // Computing left / -1 is the same as computing - left,
5898 // which does not overflow since Go sets -fwrapv.
5899 if (this->op_
== OPERATOR_DIV
)
5900 overflow
= fold_build1_loc(gccloc
, NEGATE_EXPR
, TREE_TYPE(left
),
5903 overflow
= integer_zero_node
;
5905 overflow
= fold_convert_loc(gccloc
, TREE_TYPE(ret
), overflow
);
5907 // right == -1 ? - left : ret
5908 ret
= fold_build3_loc(gccloc
, COND_EXPR
, TREE_TYPE(ret
),
5909 check
, overflow
, ret
);
5912 if (eval_saved
!= NULL_TREE
)
5913 ret
= fold_build2_loc(gccloc
, COMPOUND_EXPR
, TREE_TYPE(ret
),
5920 // Export a binary expression.
5923 Binary_expression::do_export(Export
* exp
) const
5925 exp
->write_c_string("(");
5926 this->left_
->export_expression(exp
);
5930 exp
->write_c_string(" || ");
5932 case OPERATOR_ANDAND
:
5933 exp
->write_c_string(" && ");
5936 exp
->write_c_string(" == ");
5938 case OPERATOR_NOTEQ
:
5939 exp
->write_c_string(" != ");
5942 exp
->write_c_string(" < ");
5945 exp
->write_c_string(" <= ");
5948 exp
->write_c_string(" > ");
5951 exp
->write_c_string(" >= ");
5954 exp
->write_c_string(" + ");
5956 case OPERATOR_MINUS
:
5957 exp
->write_c_string(" - ");
5960 exp
->write_c_string(" | ");
5963 exp
->write_c_string(" ^ ");
5966 exp
->write_c_string(" * ");
5969 exp
->write_c_string(" / ");
5972 exp
->write_c_string(" % ");
5974 case OPERATOR_LSHIFT
:
5975 exp
->write_c_string(" << ");
5977 case OPERATOR_RSHIFT
:
5978 exp
->write_c_string(" >> ");
5981 exp
->write_c_string(" & ");
5983 case OPERATOR_BITCLEAR
:
5984 exp
->write_c_string(" &^ ");
5989 this->right_
->export_expression(exp
);
5990 exp
->write_c_string(")");
5993 // Import a binary expression.
5996 Binary_expression::do_import(Import
* imp
)
5998 imp
->require_c_string("(");
6000 Expression
* left
= Expression::import_expression(imp
);
6003 if (imp
->match_c_string(" || "))
6008 else if (imp
->match_c_string(" && "))
6010 op
= OPERATOR_ANDAND
;
6013 else if (imp
->match_c_string(" == "))
6018 else if (imp
->match_c_string(" != "))
6020 op
= OPERATOR_NOTEQ
;
6023 else if (imp
->match_c_string(" < "))
6028 else if (imp
->match_c_string(" <= "))
6033 else if (imp
->match_c_string(" > "))
6038 else if (imp
->match_c_string(" >= "))
6043 else if (imp
->match_c_string(" + "))
6048 else if (imp
->match_c_string(" - "))
6050 op
= OPERATOR_MINUS
;
6053 else if (imp
->match_c_string(" | "))
6058 else if (imp
->match_c_string(" ^ "))
6063 else if (imp
->match_c_string(" * "))
6068 else if (imp
->match_c_string(" / "))
6073 else if (imp
->match_c_string(" % "))
6078 else if (imp
->match_c_string(" << "))
6080 op
= OPERATOR_LSHIFT
;
6083 else if (imp
->match_c_string(" >> "))
6085 op
= OPERATOR_RSHIFT
;
6088 else if (imp
->match_c_string(" & "))
6093 else if (imp
->match_c_string(" &^ "))
6095 op
= OPERATOR_BITCLEAR
;
6100 error_at(imp
->location(), "unrecognized binary operator");
6101 return Expression::make_error(imp
->location());
6104 Expression
* right
= Expression::import_expression(imp
);
6106 imp
->require_c_string(")");
6108 return Expression::make_binary(op
, left
, right
, imp
->location());
6111 // Dump ast representation of a binary expression.
6114 Binary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
6116 ast_dump_context
->ostream() << "(";
6117 ast_dump_context
->dump_expression(this->left_
);
6118 ast_dump_context
->ostream() << " ";
6119 ast_dump_context
->dump_operator(this->op_
);
6120 ast_dump_context
->ostream() << " ";
6121 ast_dump_context
->dump_expression(this->right_
);
6122 ast_dump_context
->ostream() << ") ";
6125 // Make a binary expression.
6128 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6131 return new Binary_expression(op
, left
, right
, location
);
6134 // Implement a comparison.
6137 Expression::comparison_tree(Translate_context
* context
, Type
* result_type
,
6138 Operator op
, Type
* left_type
, tree left_tree
,
6139 Type
* right_type
, tree right_tree
,
6142 enum tree_code code
;
6148 case OPERATOR_NOTEQ
:
6167 if (left_type
->is_string_type() && right_type
->is_string_type())
6169 Type
* st
= Type::make_string_type();
6170 tree string_type
= type_to_tree(st
->get_backend(context
->gogo()));
6171 static tree string_compare_decl
;
6172 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6181 right_tree
= build_int_cst_type(integer_type_node
, 0);
6183 else if ((left_type
->interface_type() != NULL
6184 && right_type
->interface_type() == NULL
6185 && !right_type
->is_nil_type())
6186 || (left_type
->interface_type() == NULL
6187 && !left_type
->is_nil_type()
6188 && right_type
->interface_type() != NULL
))
6190 // Comparing an interface value to a non-interface value.
6191 if (left_type
->interface_type() == NULL
)
6193 std::swap(left_type
, right_type
);
6194 std::swap(left_tree
, right_tree
);
6197 // The right operand is not an interface. We need to take its
6198 // address if it is not a pointer.
6201 if (right_type
->points_to() != NULL
)
6203 make_tmp
= NULL_TREE
;
6206 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
))
6207 || (TREE_CODE(right_tree
) != CONST_DECL
6208 && DECL_P(right_tree
)))
6210 make_tmp
= NULL_TREE
;
6211 arg
= build_fold_addr_expr_loc(location
.gcc_location(), right_tree
);
6212 if (DECL_P(right_tree
))
6213 TREE_ADDRESSABLE(right_tree
) = 1;
6217 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6218 get_name(right_tree
));
6219 DECL_IGNORED_P(tmp
) = 0;
6220 DECL_INITIAL(tmp
) = right_tree
;
6221 TREE_ADDRESSABLE(tmp
) = 1;
6222 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6223 SET_EXPR_LOCATION(make_tmp
, location
.gcc_location());
6224 arg
= build_fold_addr_expr_loc(location
.gcc_location(), tmp
);
6226 arg
= fold_convert_loc(location
.gcc_location(), ptr_type_node
, arg
);
6228 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo(),
6231 if (left_type
->interface_type()->is_empty())
6233 static tree empty_interface_value_compare_decl
;
6234 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6236 "__go_empty_interface_value_compare",
6239 TREE_TYPE(left_tree
),
6241 TREE_TYPE(descriptor
),
6245 if (left_tree
== error_mark_node
)
6246 return error_mark_node
;
6247 // This can panic if the type is not comparable.
6248 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6252 static tree interface_value_compare_decl
;
6253 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6255 "__go_interface_value_compare",
6258 TREE_TYPE(left_tree
),
6260 TREE_TYPE(descriptor
),
6264 if (left_tree
== error_mark_node
)
6265 return error_mark_node
;
6266 // This can panic if the type is not comparable.
6267 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6269 right_tree
= build_int_cst_type(integer_type_node
, 0);
6271 if (make_tmp
!= NULL_TREE
)
6272 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6275 else if (left_type
->interface_type() != NULL
6276 && right_type
->interface_type() != NULL
)
6278 if (left_type
->interface_type()->is_empty()
6279 && right_type
->interface_type()->is_empty())
6281 static tree empty_interface_compare_decl
;
6282 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6284 "__go_empty_interface_compare",
6287 TREE_TYPE(left_tree
),
6289 TREE_TYPE(right_tree
),
6291 if (left_tree
== error_mark_node
)
6292 return error_mark_node
;
6293 // This can panic if the type is uncomparable.
6294 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6296 else if (!left_type
->interface_type()->is_empty()
6297 && !right_type
->interface_type()->is_empty())
6299 static tree interface_compare_decl
;
6300 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6302 "__go_interface_compare",
6305 TREE_TYPE(left_tree
),
6307 TREE_TYPE(right_tree
),
6309 if (left_tree
== error_mark_node
)
6310 return error_mark_node
;
6311 // This can panic if the type is uncomparable.
6312 TREE_NOTHROW(interface_compare_decl
) = 0;
6316 if (left_type
->interface_type()->is_empty())
6318 go_assert(op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
);
6319 std::swap(left_type
, right_type
);
6320 std::swap(left_tree
, right_tree
);
6322 go_assert(!left_type
->interface_type()->is_empty());
6323 go_assert(right_type
->interface_type()->is_empty());
6324 static tree interface_empty_compare_decl
;
6325 left_tree
= Gogo::call_builtin(&interface_empty_compare_decl
,
6327 "__go_interface_empty_compare",
6330 TREE_TYPE(left_tree
),
6332 TREE_TYPE(right_tree
),
6334 if (left_tree
== error_mark_node
)
6335 return error_mark_node
;
6336 // This can panic if the type is uncomparable.
6337 TREE_NOTHROW(interface_empty_compare_decl
) = 0;
6340 right_tree
= build_int_cst_type(integer_type_node
, 0);
6343 if (left_type
->is_nil_type()
6344 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6346 std::swap(left_type
, right_type
);
6347 std::swap(left_tree
, right_tree
);
6350 if (right_type
->is_nil_type())
6352 if (left_type
->array_type() != NULL
6353 && left_type
->array_type()->length() == NULL
)
6355 Array_type
* at
= left_type
->array_type();
6356 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6357 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6359 else if (left_type
->interface_type() != NULL
)
6361 // An interface is nil if the first field is nil.
6362 tree left_type_tree
= TREE_TYPE(left_tree
);
6363 go_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6364 tree field
= TYPE_FIELDS(left_type_tree
);
6365 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6367 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6371 go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6372 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6376 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6377 return error_mark_node
;
6379 tree result_type_tree
;
6380 if (result_type
== NULL
)
6381 result_type_tree
= boolean_type_node
;
6383 result_type_tree
= type_to_tree(result_type
->get_backend(context
->gogo()));
6385 tree ret
= fold_build2(code
, result_type_tree
, left_tree
, right_tree
);
6386 if (CAN_HAVE_LOCATION_P(ret
))
6387 SET_EXPR_LOCATION(ret
, location
.gcc_location());
6391 // Class Bound_method_expression.
6396 Bound_method_expression::do_traverse(Traverse
* traverse
)
6398 return Expression::traverse(&this->expr_
, traverse
);
6401 // Return the type of a bound method expression. The type of this
6402 // object is really the type of the method with no receiver. We
6403 // should be able to get away with just returning the type of the
6407 Bound_method_expression::do_type()
6409 if (this->method_
->is_function())
6410 return this->method_
->func_value()->type();
6411 else if (this->method_
->is_function_declaration())
6412 return this->method_
->func_declaration_value()->type();
6414 return Type::make_error_type();
6417 // Determine the types of a method expression.
6420 Bound_method_expression::do_determine_type(const Type_context
*)
6422 Function_type
* fntype
= this->type()->function_type();
6423 if (fntype
== NULL
|| !fntype
->is_method())
6424 this->expr_
->determine_type_no_context();
6427 Type_context
subcontext(fntype
->receiver()->type(), false);
6428 this->expr_
->determine_type(&subcontext
);
6432 // Check the types of a method expression.
6435 Bound_method_expression::do_check_types(Gogo
*)
6437 if (!this->method_
->is_function()
6438 && !this->method_
->is_function_declaration())
6439 this->report_error(_("object is not a method"));
6442 Type
* rtype
= this->type()->function_type()->receiver()->type()->deref();
6443 Type
* etype
= (this->expr_type_
!= NULL
6445 : this->expr_
->type());
6446 etype
= etype
->deref();
6447 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6448 this->report_error(_("method type does not match object type"));
6452 // Get the tree for a method expression. There is no standard tree
6453 // representation for this. The only places it may currently be used
6454 // are in a Call_expression or a Go_statement, which will take it
6455 // apart directly. So this has nothing to do at present.
6458 Bound_method_expression::do_get_tree(Translate_context
*)
6460 error_at(this->location(), "reference to method other than calling it");
6461 return error_mark_node
;
6464 // Dump ast representation of a bound method expression.
6467 Bound_method_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
6470 if (this->expr_type_
!= NULL
)
6471 ast_dump_context
->ostream() << "(";
6472 ast_dump_context
->dump_expression(this->expr_
);
6473 if (this->expr_type_
!= NULL
)
6475 ast_dump_context
->ostream() << ":";
6476 ast_dump_context
->dump_type(this->expr_type_
);
6477 ast_dump_context
->ostream() << ")";
6480 ast_dump_context
->ostream() << "." << this->method_
->name();
6483 // Make a method expression.
6485 Bound_method_expression
*
6486 Expression::make_bound_method(Expression
* expr
, Named_object
* method
,
6489 return new Bound_method_expression(expr
, method
, location
);
6492 // Class Builtin_call_expression. This is used for a call to a
6493 // builtin function.
6495 class Builtin_call_expression
: public Call_expression
6498 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6499 bool is_varargs
, Location location
);
6502 // This overrides Call_expression::do_lower.
6504 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
6507 do_is_constant() const;
6510 do_numeric_constant_value(Numeric_constant
*) const;
6513 do_discarding_value();
6519 do_determine_type(const Type_context
*);
6522 do_check_types(Gogo
*);
6527 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6528 this->args()->copy(),
6534 do_get_tree(Translate_context
*);
6537 do_export(Export
*) const;
6540 do_is_recover_call() const;
6543 do_set_recover_arg(Expression
*);
6546 // The builtin functions.
6547 enum Builtin_function_code
6551 // Predeclared builtin functions.
6568 // Builtin functions from the unsafe package.
6581 real_imag_type(Type
*);
6584 complex_type(Type
*);
6590 check_int_value(Expression
*);
6592 // A pointer back to the general IR structure. This avoids a global
6593 // variable, or passing it around everywhere.
6595 // The builtin function being called.
6596 Builtin_function_code code_
;
6597 // Used to stop endless loops when the length of an array uses len
6598 // or cap of the array itself.
6602 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6604 Expression_list
* args
,
6607 : Call_expression(fn
, args
, is_varargs
, location
),
6608 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false)
6610 Func_expression
* fnexp
= this->fn()->func_expression();
6611 go_assert(fnexp
!= NULL
);
6612 const std::string
& name(fnexp
->named_object()->name());
6613 if (name
== "append")
6614 this->code_
= BUILTIN_APPEND
;
6615 else if (name
== "cap")
6616 this->code_
= BUILTIN_CAP
;
6617 else if (name
== "close")
6618 this->code_
= BUILTIN_CLOSE
;
6619 else if (name
== "complex")
6620 this->code_
= BUILTIN_COMPLEX
;
6621 else if (name
== "copy")
6622 this->code_
= BUILTIN_COPY
;
6623 else if (name
== "delete")
6624 this->code_
= BUILTIN_DELETE
;
6625 else if (name
== "imag")
6626 this->code_
= BUILTIN_IMAG
;
6627 else if (name
== "len")
6628 this->code_
= BUILTIN_LEN
;
6629 else if (name
== "make")
6630 this->code_
= BUILTIN_MAKE
;
6631 else if (name
== "new")
6632 this->code_
= BUILTIN_NEW
;
6633 else if (name
== "panic")
6634 this->code_
= BUILTIN_PANIC
;
6635 else if (name
== "print")
6636 this->code_
= BUILTIN_PRINT
;
6637 else if (name
== "println")
6638 this->code_
= BUILTIN_PRINTLN
;
6639 else if (name
== "real")
6640 this->code_
= BUILTIN_REAL
;
6641 else if (name
== "recover")
6642 this->code_
= BUILTIN_RECOVER
;
6643 else if (name
== "Alignof")
6644 this->code_
= BUILTIN_ALIGNOF
;
6645 else if (name
== "Offsetof")
6646 this->code_
= BUILTIN_OFFSETOF
;
6647 else if (name
== "Sizeof")
6648 this->code_
= BUILTIN_SIZEOF
;
6653 // Return whether this is a call to recover. This is a virtual
6654 // function called from the parent class.
6657 Builtin_call_expression::do_is_recover_call() const
6659 if (this->classification() == EXPRESSION_ERROR
)
6661 return this->code_
== BUILTIN_RECOVER
;
6664 // Set the argument for a call to recover.
6667 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6669 const Expression_list
* args
= this->args();
6670 go_assert(args
== NULL
|| args
->empty());
6671 Expression_list
* new_args
= new Expression_list();
6672 new_args
->push_back(arg
);
6673 this->set_args(new_args
);
6676 // A traversal class which looks for a call expression.
6678 class Find_call_expression
: public Traverse
6681 Find_call_expression()
6682 : Traverse(traverse_expressions
),
6687 expression(Expression
**);
6691 { return this->found_
; }
6698 Find_call_expression::expression(Expression
** pexpr
)
6700 if ((*pexpr
)->call_expression() != NULL
)
6702 this->found_
= true;
6703 return TRAVERSE_EXIT
;
6705 return TRAVERSE_CONTINUE
;
6708 // Lower a builtin call expression. This turns new and make into
6709 // specific expressions. We also convert to a constant if we can.
6712 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
6713 Statement_inserter
* inserter
, int)
6715 if (this->classification() == EXPRESSION_ERROR
)
6718 Location loc
= this->location();
6720 if (this->is_varargs() && this->code_
!= BUILTIN_APPEND
)
6722 this->report_error(_("invalid use of %<...%> with builtin function"));
6723 return Expression::make_error(loc
);
6726 if (this->is_constant())
6728 // We can only lower len and cap if there are no function calls
6729 // in the arguments. Otherwise we have to make the call.
6730 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6732 Expression
* arg
= this->one_arg();
6733 if (arg
!= NULL
&& !arg
->is_constant())
6735 Find_call_expression find_call
;
6736 Expression::traverse(&arg
, &find_call
);
6737 if (find_call
.found())
6742 Numeric_constant nc
;
6743 if (this->numeric_constant_value(&nc
))
6744 return nc
.expression(loc
);
6747 switch (this->code_
)
6754 const Expression_list
* args
= this->args();
6755 if (args
== NULL
|| args
->size() < 1)
6756 this->report_error(_("not enough arguments"));
6757 else if (args
->size() > 1)
6758 this->report_error(_("too many arguments"));
6761 Expression
* arg
= args
->front();
6762 if (!arg
->is_type_expression())
6764 error_at(arg
->location(), "expected type");
6765 this->set_is_error();
6768 return Expression::make_allocation(arg
->type(), loc
);
6774 return this->lower_make();
6776 case BUILTIN_RECOVER
:
6777 if (function
!= NULL
)
6778 function
->func_value()->set_calls_recover();
6781 // Calling recover outside of a function always returns the
6782 // nil empty interface.
6783 Type
* eface
= Type::make_empty_interface_type(loc
);
6784 return Expression::make_cast(eface
, Expression::make_nil(loc
), loc
);
6788 case BUILTIN_APPEND
:
6790 // Lower the varargs.
6791 const Expression_list
* args
= this->args();
6792 if (args
== NULL
|| args
->empty())
6794 Type
* slice_type
= args
->front()->type();
6795 if (!slice_type
->is_slice_type())
6797 error_at(args
->front()->location(), "argument 1 must be a slice");
6798 this->set_is_error();
6801 Type
* element_type
= slice_type
->array_type()->element_type();
6802 this->lower_varargs(gogo
, function
, inserter
,
6803 Type::make_array_type(element_type
, NULL
),
6808 case BUILTIN_DELETE
:
6810 // Lower to a runtime function call.
6811 const Expression_list
* args
= this->args();
6812 if (args
== NULL
|| args
->size() < 2)
6813 this->report_error(_("not enough arguments"));
6814 else if (args
->size() > 2)
6815 this->report_error(_("too many arguments"));
6816 else if (args
->front()->type()->map_type() == NULL
)
6817 this->report_error(_("argument 1 must be a map"));
6820 // Since this function returns no value it must appear in
6821 // a statement by itself, so we don't have to worry about
6822 // order of evaluation of values around it. Evaluate the
6823 // map first to get order of evaluation right.
6824 Map_type
* mt
= args
->front()->type()->map_type();
6825 Temporary_statement
* map_temp
=
6826 Statement::make_temporary(mt
, args
->front(), loc
);
6827 inserter
->insert(map_temp
);
6829 Temporary_statement
* key_temp
=
6830 Statement::make_temporary(mt
->key_type(), args
->back(), loc
);
6831 inserter
->insert(key_temp
);
6833 Expression
* e1
= Expression::make_temporary_reference(map_temp
,
6835 Expression
* e2
= Expression::make_temporary_reference(key_temp
,
6837 e2
= Expression::make_unary(OPERATOR_AND
, e2
, loc
);
6838 return Runtime::make_call(Runtime::MAPDELETE
, this->location(),
6848 // Lower a make expression.
6851 Builtin_call_expression::lower_make()
6853 Location loc
= this->location();
6855 const Expression_list
* args
= this->args();
6856 if (args
== NULL
|| args
->size() < 1)
6858 this->report_error(_("not enough arguments"));
6859 return Expression::make_error(this->location());
6862 Expression_list::const_iterator parg
= args
->begin();
6864 Expression
* first_arg
= *parg
;
6865 if (!first_arg
->is_type_expression())
6867 error_at(first_arg
->location(), "expected type");
6868 this->set_is_error();
6869 return Expression::make_error(this->location());
6871 Type
* type
= first_arg
->type();
6873 bool is_slice
= false;
6874 bool is_map
= false;
6875 bool is_chan
= false;
6876 if (type
->is_slice_type())
6878 else if (type
->map_type() != NULL
)
6880 else if (type
->channel_type() != NULL
)
6884 this->report_error(_("invalid type for make function"));
6885 return Expression::make_error(this->location());
6888 bool have_big_args
= false;
6889 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6890 int uintptr_bits
= uintptr_type
->integer_type()->bits();
6893 Expression
* len_arg
;
6894 if (parg
== args
->end())
6898 this->report_error(_("length required when allocating a slice"));
6899 return Expression::make_error(this->location());
6903 mpz_init_set_ui(zval
, 0);
6904 len_arg
= Expression::make_integer(&zval
, NULL
, loc
);
6910 if (!this->check_int_value(len_arg
))
6912 this->report_error(_("bad size for make"));
6913 return Expression::make_error(this->location());
6915 if (len_arg
->type()->integer_type() != NULL
6916 && len_arg
->type()->integer_type()->bits() > uintptr_bits
)
6917 have_big_args
= true;
6921 Expression
* cap_arg
= NULL
;
6922 if (is_slice
&& parg
!= args
->end())
6925 if (!this->check_int_value(cap_arg
))
6927 this->report_error(_("bad capacity when making slice"));
6928 return Expression::make_error(this->location());
6930 if (cap_arg
->type()->integer_type() != NULL
6931 && cap_arg
->type()->integer_type()->bits() > uintptr_bits
)
6932 have_big_args
= true;
6936 if (parg
!= args
->end())
6938 this->report_error(_("too many arguments to make"));
6939 return Expression::make_error(this->location());
6942 Location type_loc
= first_arg
->location();
6943 Expression
* type_arg
;
6944 if (is_slice
|| is_chan
)
6945 type_arg
= Expression::make_type_descriptor(type
, type_loc
);
6947 type_arg
= Expression::make_map_descriptor(type
->map_type(), type_loc
);
6954 if (cap_arg
== NULL
)
6955 call
= Runtime::make_call((have_big_args
6956 ? Runtime::MAKESLICE1BIG
6957 : Runtime::MAKESLICE1
),
6958 loc
, 2, type_arg
, len_arg
);
6960 call
= Runtime::make_call((have_big_args
6961 ? Runtime::MAKESLICE2BIG
6962 : Runtime::MAKESLICE2
),
6963 loc
, 3, type_arg
, len_arg
, cap_arg
);
6966 call
= Runtime::make_call((have_big_args
6967 ? Runtime::MAKEMAPBIG
6968 : Runtime::MAKEMAP
),
6969 loc
, 2, type_arg
, len_arg
);
6971 call
= Runtime::make_call((have_big_args
6972 ? Runtime::MAKECHANBIG
6973 : Runtime::MAKECHAN
),
6974 loc
, 2, type_arg
, len_arg
);
6978 return Expression::make_unsafe_cast(type
, call
, loc
);
6981 // Return whether an expression has an integer value. Report an error
6982 // if not. This is used when handling calls to the predeclared make
6986 Builtin_call_expression::check_int_value(Expression
* e
)
6988 if (e
->type()->integer_type() != NULL
)
6991 // Check for a floating point constant with integer value.
6992 Numeric_constant nc
;
6994 if (e
->numeric_constant_value(&nc
) && nc
.to_int(&ival
))
7003 // Return the type of the real or imag functions, given the type of
7004 // the argument. We need to map complex to float, complex64 to
7005 // float32, and complex128 to float64, so it has to be done by name.
7006 // This returns NULL if it can't figure out the type.
7009 Builtin_call_expression::real_imag_type(Type
* arg_type
)
7011 if (arg_type
== NULL
|| arg_type
->is_abstract())
7013 Named_type
* nt
= arg_type
->named_type();
7016 while (nt
->real_type()->named_type() != NULL
)
7017 nt
= nt
->real_type()->named_type();
7018 if (nt
->name() == "complex64")
7019 return Type::lookup_float_type("float32");
7020 else if (nt
->name() == "complex128")
7021 return Type::lookup_float_type("float64");
7026 // Return the type of the complex function, given the type of one of the
7027 // argments. Like real_imag_type, we have to map by name.
7030 Builtin_call_expression::complex_type(Type
* arg_type
)
7032 if (arg_type
== NULL
|| arg_type
->is_abstract())
7034 Named_type
* nt
= arg_type
->named_type();
7037 while (nt
->real_type()->named_type() != NULL
)
7038 nt
= nt
->real_type()->named_type();
7039 if (nt
->name() == "float32")
7040 return Type::lookup_complex_type("complex64");
7041 else if (nt
->name() == "float64")
7042 return Type::lookup_complex_type("complex128");
7047 // Return a single argument, or NULL if there isn't one.
7050 Builtin_call_expression::one_arg() const
7052 const Expression_list
* args
= this->args();
7053 if (args
== NULL
|| args
->size() != 1)
7055 return args
->front();
7058 // Return whether this is constant: len of a string, or len or cap of
7059 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7062 Builtin_call_expression::do_is_constant() const
7064 switch (this->code_
)
7072 Expression
* arg
= this->one_arg();
7075 Type
* arg_type
= arg
->type();
7077 if (arg_type
->points_to() != NULL
7078 && arg_type
->points_to()->array_type() != NULL
7079 && !arg_type
->points_to()->is_slice_type())
7080 arg_type
= arg_type
->points_to();
7082 if (arg_type
->array_type() != NULL
7083 && arg_type
->array_type()->length() != NULL
)
7086 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
7089 bool ret
= arg
->is_constant();
7090 this->seen_
= false;
7096 case BUILTIN_SIZEOF
:
7097 case BUILTIN_ALIGNOF
:
7098 return this->one_arg() != NULL
;
7100 case BUILTIN_OFFSETOF
:
7102 Expression
* arg
= this->one_arg();
7105 return arg
->field_reference_expression() != NULL
;
7108 case BUILTIN_COMPLEX
:
7110 const Expression_list
* args
= this->args();
7111 if (args
!= NULL
&& args
->size() == 2)
7112 return args
->front()->is_constant() && args
->back()->is_constant();
7119 Expression
* arg
= this->one_arg();
7120 return arg
!= NULL
&& arg
->is_constant();
7130 // Return a numeric constant if possible.
7133 Builtin_call_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
7135 if (this->code_
== BUILTIN_LEN
7136 || this->code_
== BUILTIN_CAP
)
7138 Expression
* arg
= this->one_arg();
7141 Type
* arg_type
= arg
->type();
7143 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
7146 if (arg
->string_constant_value(&sval
))
7148 nc
->set_unsigned_long(Type::lookup_integer_type("int"),
7154 if (arg_type
->points_to() != NULL
7155 && arg_type
->points_to()->array_type() != NULL
7156 && !arg_type
->points_to()->is_slice_type())
7157 arg_type
= arg_type
->points_to();
7159 if (arg_type
->array_type() != NULL
7160 && arg_type
->array_type()->length() != NULL
)
7164 Expression
* e
= arg_type
->array_type()->length();
7166 bool r
= e
->numeric_constant_value(nc
);
7167 this->seen_
= false;
7170 if (!nc
->set_type(Type::lookup_integer_type("int"), false,
7177 else if (this->code_
== BUILTIN_SIZEOF
7178 || this->code_
== BUILTIN_ALIGNOF
)
7180 Expression
* arg
= this->one_arg();
7183 Type
* arg_type
= arg
->type();
7184 if (arg_type
->is_error())
7186 if (arg_type
->is_abstract())
7188 if (arg_type
->named_type() != NULL
)
7189 arg_type
->named_type()->convert(this->gogo_
);
7192 if (this->code_
== BUILTIN_SIZEOF
)
7194 if (!arg_type
->backend_type_size(this->gogo_
, &ret
))
7197 else if (this->code_
== BUILTIN_ALIGNOF
)
7199 if (arg
->field_reference_expression() == NULL
)
7201 if (!arg_type
->backend_type_align(this->gogo_
, &ret
))
7206 // Calling unsafe.Alignof(s.f) returns the alignment of
7207 // the type of f when it is used as a field in a struct.
7208 if (!arg_type
->backend_type_field_align(this->gogo_
, &ret
))
7215 nc
->set_unsigned_long(Type::lookup_integer_type("uintptr"),
7216 static_cast<unsigned long>(ret
));
7219 else if (this->code_
== BUILTIN_OFFSETOF
)
7221 Expression
* arg
= this->one_arg();
7224 Field_reference_expression
* farg
= arg
->field_reference_expression();
7227 Expression
* struct_expr
= farg
->expr();
7228 Type
* st
= struct_expr
->type();
7229 if (st
->struct_type() == NULL
)
7231 if (st
->named_type() != NULL
)
7232 st
->named_type()->convert(this->gogo_
);
7233 unsigned int offset
;
7234 if (!st
->struct_type()->backend_field_offset(this->gogo_
,
7235 farg
->field_index(),
7238 nc
->set_unsigned_long(Type::lookup_integer_type("uintptr"),
7239 static_cast<unsigned long>(offset
));
7242 else if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
7244 Expression
* arg
= this->one_arg();
7248 Numeric_constant argnc
;
7249 if (!arg
->numeric_constant_value(&argnc
))
7254 if (!argnc
.to_complex(&real
, &imag
))
7257 Type
* type
= Builtin_call_expression::real_imag_type(argnc
.type());
7258 if (this->code_
== BUILTIN_REAL
)
7259 nc
->set_float(type
, real
);
7261 nc
->set_float(type
, imag
);
7264 else if (this->code_
== BUILTIN_COMPLEX
)
7266 const Expression_list
* args
= this->args();
7267 if (args
== NULL
|| args
->size() != 2)
7270 Numeric_constant rnc
;
7271 if (!args
->front()->numeric_constant_value(&rnc
))
7273 Numeric_constant inc
;
7274 if (!args
->back()->numeric_constant_value(&inc
))
7277 if (rnc
.type() != NULL
7278 && !rnc
.type()->is_abstract()
7279 && inc
.type() != NULL
7280 && !inc
.type()->is_abstract()
7281 && !Type::are_identical(rnc
.type(), inc
.type(), false, NULL
))
7285 if (!rnc
.to_float(&r
))
7288 if (!inc
.to_float(&i
))
7294 Type
* arg_type
= rnc
.type();
7295 if (arg_type
== NULL
|| arg_type
->is_abstract())
7296 arg_type
= inc
.type();
7298 Type
* type
= Builtin_call_expression::complex_type(arg_type
);
7299 nc
->set_complex(type
, r
, i
);
7310 // Give an error if we are discarding the value of an expression which
7311 // should not normally be discarded. We don't give an error for
7312 // discarding the value of an ordinary function call, but we do for
7313 // builtin functions, purely for consistency with the gc compiler.
7316 Builtin_call_expression::do_discarding_value()
7318 switch (this->code_
)
7320 case BUILTIN_INVALID
:
7324 case BUILTIN_APPEND
:
7326 case BUILTIN_COMPLEX
:
7332 case BUILTIN_ALIGNOF
:
7333 case BUILTIN_OFFSETOF
:
7334 case BUILTIN_SIZEOF
:
7335 this->unused_value_error();
7340 case BUILTIN_DELETE
:
7343 case BUILTIN_PRINTLN
:
7344 case BUILTIN_RECOVER
:
7352 Builtin_call_expression::do_type()
7354 switch (this->code_
)
7356 case BUILTIN_INVALID
:
7363 const Expression_list
* args
= this->args();
7364 if (args
== NULL
|| args
->empty())
7365 return Type::make_error_type();
7366 return Type::make_pointer_type(args
->front()->type());
7372 return Type::lookup_integer_type("int");
7374 case BUILTIN_ALIGNOF
:
7375 case BUILTIN_OFFSETOF
:
7376 case BUILTIN_SIZEOF
:
7377 return Type::lookup_integer_type("uintptr");
7380 case BUILTIN_DELETE
:
7383 case BUILTIN_PRINTLN
:
7384 return Type::make_void_type();
7386 case BUILTIN_RECOVER
:
7387 return Type::make_empty_interface_type(Linemap::predeclared_location());
7389 case BUILTIN_APPEND
:
7391 const Expression_list
* args
= this->args();
7392 if (args
== NULL
|| args
->empty())
7393 return Type::make_error_type();
7394 return args
->front()->type();
7400 Expression
* arg
= this->one_arg();
7402 return Type::make_error_type();
7403 Type
* t
= arg
->type();
7404 if (t
->is_abstract())
7405 t
= t
->make_non_abstract_type();
7406 t
= Builtin_call_expression::real_imag_type(t
);
7408 t
= Type::make_error_type();
7412 case BUILTIN_COMPLEX
:
7414 const Expression_list
* args
= this->args();
7415 if (args
== NULL
|| args
->size() != 2)
7416 return Type::make_error_type();
7417 Type
* t
= args
->front()->type();
7418 if (t
->is_abstract())
7420 t
= args
->back()->type();
7421 if (t
->is_abstract())
7422 t
= t
->make_non_abstract_type();
7424 t
= Builtin_call_expression::complex_type(t
);
7426 t
= Type::make_error_type();
7432 // Determine the type.
7435 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7437 if (!this->determining_types())
7440 this->fn()->determine_type_no_context();
7442 const Expression_list
* args
= this->args();
7445 Type
* arg_type
= NULL
;
7446 switch (this->code_
)
7449 case BUILTIN_PRINTLN
:
7450 // Do not force a large integer constant to "int".
7456 arg_type
= Builtin_call_expression::complex_type(context
->type
);
7460 case BUILTIN_COMPLEX
:
7462 // For the complex function the type of one operand can
7463 // determine the type of the other, as in a binary expression.
7464 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7465 if (args
!= NULL
&& args
->size() == 2)
7467 Type
* t1
= args
->front()->type();
7468 Type
* t2
= args
->front()->type();
7469 if (!t1
->is_abstract())
7471 else if (!t2
->is_abstract())
7485 for (Expression_list::const_iterator pa
= args
->begin();
7489 Type_context subcontext
;
7490 subcontext
.type
= arg_type
;
7494 // We want to print large constants, we so can't just
7495 // use the appropriate nonabstract type. Use uint64 for
7496 // an integer if we know it is nonnegative, otherwise
7497 // use int64 for a integer, otherwise use float64 for a
7498 // float or complex128 for a complex.
7499 Type
* want_type
= NULL
;
7500 Type
* atype
= (*pa
)->type();
7501 if (atype
->is_abstract())
7503 if (atype
->integer_type() != NULL
)
7505 Numeric_constant nc
;
7506 if (this->numeric_constant_value(&nc
))
7509 if (nc
.to_int(&val
))
7511 if (mpz_sgn(val
) >= 0)
7512 want_type
= Type::lookup_integer_type("uint64");
7516 if (want_type
== NULL
)
7517 want_type
= Type::lookup_integer_type("int64");
7519 else if (atype
->float_type() != NULL
)
7520 want_type
= Type::lookup_float_type("float64");
7521 else if (atype
->complex_type() != NULL
)
7522 want_type
= Type::lookup_complex_type("complex128");
7523 else if (atype
->is_abstract_string_type())
7524 want_type
= Type::lookup_string_type();
7525 else if (atype
->is_abstract_boolean_type())
7526 want_type
= Type::lookup_bool_type();
7529 subcontext
.type
= want_type
;
7533 (*pa
)->determine_type(&subcontext
);
7538 // If there is exactly one argument, return true. Otherwise give an
7539 // error message and return false.
7542 Builtin_call_expression::check_one_arg()
7544 const Expression_list
* args
= this->args();
7545 if (args
== NULL
|| args
->size() < 1)
7547 this->report_error(_("not enough arguments"));
7550 else if (args
->size() > 1)
7552 this->report_error(_("too many arguments"));
7555 if (args
->front()->is_error_expression()
7556 || args
->front()->type()->is_error())
7558 this->set_is_error();
7564 // Check argument types for a builtin function.
7567 Builtin_call_expression::do_check_types(Gogo
*)
7569 if (this->is_error_expression())
7571 switch (this->code_
)
7573 case BUILTIN_INVALID
:
7576 case BUILTIN_DELETE
:
7582 // The single argument may be either a string or an array or a
7583 // map or a channel, or a pointer to a closed array.
7584 if (this->check_one_arg())
7586 Type
* arg_type
= this->one_arg()->type();
7587 if (arg_type
->points_to() != NULL
7588 && arg_type
->points_to()->array_type() != NULL
7589 && !arg_type
->points_to()->is_slice_type())
7590 arg_type
= arg_type
->points_to();
7591 if (this->code_
== BUILTIN_CAP
)
7593 if (!arg_type
->is_error()
7594 && arg_type
->array_type() == NULL
7595 && arg_type
->channel_type() == NULL
)
7596 this->report_error(_("argument must be array or slice "
7601 if (!arg_type
->is_error()
7602 && !arg_type
->is_string_type()
7603 && arg_type
->array_type() == NULL
7604 && arg_type
->map_type() == NULL
7605 && arg_type
->channel_type() == NULL
)
7606 this->report_error(_("argument must be string or "
7607 "array or slice or map or channel"));
7614 case BUILTIN_PRINTLN
:
7616 const Expression_list
* args
= this->args();
7619 if (this->code_
== BUILTIN_PRINT
)
7620 warning_at(this->location(), 0,
7621 "no arguments for builtin function %<%s%>",
7622 (this->code_
== BUILTIN_PRINT
7628 for (Expression_list::const_iterator p
= args
->begin();
7632 Type
* type
= (*p
)->type();
7633 if (type
->is_error()
7634 || type
->is_string_type()
7635 || type
->integer_type() != NULL
7636 || type
->float_type() != NULL
7637 || type
->complex_type() != NULL
7638 || type
->is_boolean_type()
7639 || type
->points_to() != NULL
7640 || type
->interface_type() != NULL
7641 || type
->channel_type() != NULL
7642 || type
->map_type() != NULL
7643 || type
->function_type() != NULL
7644 || type
->is_slice_type())
7646 else if ((*p
)->is_type_expression())
7648 // If this is a type expression it's going to give
7649 // an error anyhow, so we don't need one here.
7652 this->report_error(_("unsupported argument type to "
7653 "builtin function"));
7660 if (this->check_one_arg())
7662 if (this->one_arg()->type()->channel_type() == NULL
)
7663 this->report_error(_("argument must be channel"));
7664 else if (!this->one_arg()->type()->channel_type()->may_send())
7665 this->report_error(_("cannot close receive-only channel"));
7670 case BUILTIN_SIZEOF
:
7671 case BUILTIN_ALIGNOF
:
7672 this->check_one_arg();
7675 case BUILTIN_RECOVER
:
7676 if (this->args() != NULL
&& !this->args()->empty())
7677 this->report_error(_("too many arguments"));
7680 case BUILTIN_OFFSETOF
:
7681 if (this->check_one_arg())
7683 Expression
* arg
= this->one_arg();
7684 if (arg
->field_reference_expression() == NULL
)
7685 this->report_error(_("argument must be a field reference"));
7691 const Expression_list
* args
= this->args();
7692 if (args
== NULL
|| args
->size() < 2)
7694 this->report_error(_("not enough arguments"));
7697 else if (args
->size() > 2)
7699 this->report_error(_("too many arguments"));
7702 Type
* arg1_type
= args
->front()->type();
7703 Type
* arg2_type
= args
->back()->type();
7704 if (arg1_type
->is_error() || arg2_type
->is_error())
7708 if (arg1_type
->is_slice_type())
7709 e1
= arg1_type
->array_type()->element_type();
7712 this->report_error(_("left argument must be a slice"));
7716 if (arg2_type
->is_slice_type())
7718 Type
* e2
= arg2_type
->array_type()->element_type();
7719 if (!Type::are_identical(e1
, e2
, true, NULL
))
7720 this->report_error(_("element types must be the same"));
7722 else if (arg2_type
->is_string_type())
7724 if (e1
->integer_type() == NULL
|| !e1
->integer_type()->is_byte())
7725 this->report_error(_("first argument must be []byte"));
7728 this->report_error(_("second argument must be slice or string"));
7732 case BUILTIN_APPEND
:
7734 const Expression_list
* args
= this->args();
7735 if (args
== NULL
|| args
->size() < 2)
7737 this->report_error(_("not enough arguments"));
7740 if (args
->size() > 2)
7742 this->report_error(_("too many arguments"));
7745 if (args
->front()->type()->is_error()
7746 || args
->back()->type()->is_error())
7749 Array_type
* at
= args
->front()->type()->array_type();
7750 Type
* e
= at
->element_type();
7752 // The language permits appending a string to a []byte, as a
7754 if (args
->back()->type()->is_string_type())
7756 if (e
->integer_type() != NULL
&& e
->integer_type()->is_byte())
7760 // The language says that the second argument must be
7761 // assignable to a slice of the element type of the first
7762 // argument. We already know the first argument is a slice
7764 Type
* arg2_type
= Type::make_array_type(e
, NULL
);
7766 if (!Type::are_assignable(arg2_type
, args
->back()->type(), &reason
))
7769 this->report_error(_("argument 2 has invalid type"));
7772 error_at(this->location(), "argument 2 has invalid type (%s)",
7774 this->set_is_error();
7782 if (this->check_one_arg())
7784 if (this->one_arg()->type()->complex_type() == NULL
)
7785 this->report_error(_("argument must have complex type"));
7789 case BUILTIN_COMPLEX
:
7791 const Expression_list
* args
= this->args();
7792 if (args
== NULL
|| args
->size() < 2)
7793 this->report_error(_("not enough arguments"));
7794 else if (args
->size() > 2)
7795 this->report_error(_("too many arguments"));
7796 else if (args
->front()->is_error_expression()
7797 || args
->front()->type()->is_error()
7798 || args
->back()->is_error_expression()
7799 || args
->back()->type()->is_error())
7800 this->set_is_error();
7801 else if (!Type::are_identical(args
->front()->type(),
7802 args
->back()->type(), true, NULL
))
7803 this->report_error(_("complex arguments must have identical types"));
7804 else if (args
->front()->type()->float_type() == NULL
)
7805 this->report_error(_("complex arguments must have "
7806 "floating-point type"));
7815 // Return the tree for a builtin function.
7818 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7820 Gogo
* gogo
= context
->gogo();
7821 Location location
= this->location();
7822 switch (this->code_
)
7824 case BUILTIN_INVALID
:
7832 const Expression_list
* args
= this->args();
7833 go_assert(args
!= NULL
&& args
->size() == 1);
7834 Expression
* arg
= *args
->begin();
7835 Type
* arg_type
= arg
->type();
7839 go_assert(saw_errors());
7840 return error_mark_node
;
7844 tree arg_tree
= arg
->get_tree(context
);
7846 this->seen_
= false;
7848 if (arg_tree
== error_mark_node
)
7849 return error_mark_node
;
7851 if (arg_type
->points_to() != NULL
)
7853 arg_type
= arg_type
->points_to();
7854 go_assert(arg_type
->array_type() != NULL
7855 && !arg_type
->is_slice_type());
7856 go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7857 arg_tree
= build_fold_indirect_ref(arg_tree
);
7861 if (this->code_
== BUILTIN_LEN
)
7863 if (arg_type
->is_string_type())
7864 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7865 else if (arg_type
->array_type() != NULL
)
7869 go_assert(saw_errors());
7870 return error_mark_node
;
7873 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7874 this->seen_
= false;
7876 else if (arg_type
->map_type() != NULL
)
7878 tree arg_type_tree
= type_to_tree(arg_type
->get_backend(gogo
));
7879 static tree map_len_fndecl
;
7880 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7888 else if (arg_type
->channel_type() != NULL
)
7890 tree arg_type_tree
= type_to_tree(arg_type
->get_backend(gogo
));
7891 static tree chan_len_fndecl
;
7892 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7905 if (arg_type
->array_type() != NULL
)
7909 go_assert(saw_errors());
7910 return error_mark_node
;
7913 val_tree
= arg_type
->array_type()->capacity_tree(gogo
,
7915 this->seen_
= false;
7917 else if (arg_type
->channel_type() != NULL
)
7919 tree arg_type_tree
= type_to_tree(arg_type
->get_backend(gogo
));
7920 static tree chan_cap_fndecl
;
7921 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7933 if (val_tree
== error_mark_node
)
7934 return error_mark_node
;
7936 Type
* int_type
= Type::lookup_integer_type("int");
7937 tree type_tree
= type_to_tree(int_type
->get_backend(gogo
));
7938 if (type_tree
== TREE_TYPE(val_tree
))
7941 return fold(convert_to_integer(type_tree
, val_tree
));
7945 case BUILTIN_PRINTLN
:
7947 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7948 tree stmt_list
= NULL_TREE
;
7950 const Expression_list
* call_args
= this->args();
7951 if (call_args
!= NULL
)
7953 for (Expression_list::const_iterator p
= call_args
->begin();
7954 p
!= call_args
->end();
7957 if (is_ln
&& p
!= call_args
->begin())
7959 static tree print_space_fndecl
;
7960 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7965 if (call
== error_mark_node
)
7966 return error_mark_node
;
7967 append_to_statement_list(call
, &stmt_list
);
7970 Type
* type
= (*p
)->type();
7972 tree arg
= (*p
)->get_tree(context
);
7973 if (arg
== error_mark_node
)
7974 return error_mark_node
;
7978 if (type
->is_string_type())
7980 static tree print_string_fndecl
;
7981 pfndecl
= &print_string_fndecl
;
7982 fnname
= "__go_print_string";
7984 else if (type
->integer_type() != NULL
7985 && type
->integer_type()->is_unsigned())
7987 static tree print_uint64_fndecl
;
7988 pfndecl
= &print_uint64_fndecl
;
7989 fnname
= "__go_print_uint64";
7990 Type
* itype
= Type::lookup_integer_type("uint64");
7991 Btype
* bitype
= itype
->get_backend(gogo
);
7992 arg
= fold_convert_loc(location
.gcc_location(),
7993 type_to_tree(bitype
), arg
);
7995 else if (type
->integer_type() != NULL
)
7997 static tree print_int64_fndecl
;
7998 pfndecl
= &print_int64_fndecl
;
7999 fnname
= "__go_print_int64";
8000 Type
* itype
= Type::lookup_integer_type("int64");
8001 Btype
* bitype
= itype
->get_backend(gogo
);
8002 arg
= fold_convert_loc(location
.gcc_location(),
8003 type_to_tree(bitype
), arg
);
8005 else if (type
->float_type() != NULL
)
8007 static tree print_double_fndecl
;
8008 pfndecl
= &print_double_fndecl
;
8009 fnname
= "__go_print_double";
8010 arg
= fold_convert_loc(location
.gcc_location(),
8011 double_type_node
, arg
);
8013 else if (type
->complex_type() != NULL
)
8015 static tree print_complex_fndecl
;
8016 pfndecl
= &print_complex_fndecl
;
8017 fnname
= "__go_print_complex";
8018 arg
= fold_convert_loc(location
.gcc_location(),
8019 complex_double_type_node
, arg
);
8021 else if (type
->is_boolean_type())
8023 static tree print_bool_fndecl
;
8024 pfndecl
= &print_bool_fndecl
;
8025 fnname
= "__go_print_bool";
8027 else if (type
->points_to() != NULL
8028 || type
->channel_type() != NULL
8029 || type
->map_type() != NULL
8030 || type
->function_type() != NULL
)
8032 static tree print_pointer_fndecl
;
8033 pfndecl
= &print_pointer_fndecl
;
8034 fnname
= "__go_print_pointer";
8035 arg
= fold_convert_loc(location
.gcc_location(),
8036 ptr_type_node
, arg
);
8038 else if (type
->interface_type() != NULL
)
8040 if (type
->interface_type()->is_empty())
8042 static tree print_empty_interface_fndecl
;
8043 pfndecl
= &print_empty_interface_fndecl
;
8044 fnname
= "__go_print_empty_interface";
8048 static tree print_interface_fndecl
;
8049 pfndecl
= &print_interface_fndecl
;
8050 fnname
= "__go_print_interface";
8053 else if (type
->is_slice_type())
8055 static tree print_slice_fndecl
;
8056 pfndecl
= &print_slice_fndecl
;
8057 fnname
= "__go_print_slice";
8061 go_assert(saw_errors());
8062 return error_mark_node
;
8065 tree call
= Gogo::call_builtin(pfndecl
,
8072 if (call
== error_mark_node
)
8073 return error_mark_node
;
8074 append_to_statement_list(call
, &stmt_list
);
8080 static tree print_nl_fndecl
;
8081 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
8086 if (call
== error_mark_node
)
8087 return error_mark_node
;
8088 append_to_statement_list(call
, &stmt_list
);
8096 const Expression_list
* args
= this->args();
8097 go_assert(args
!= NULL
&& args
->size() == 1);
8098 Expression
* arg
= args
->front();
8099 tree arg_tree
= arg
->get_tree(context
);
8100 if (arg_tree
== error_mark_node
)
8101 return error_mark_node
;
8103 Type::make_empty_interface_type(Linemap::predeclared_location());
8104 arg_tree
= Expression::convert_for_assignment(context
, empty
,
8106 arg_tree
, location
);
8107 static tree panic_fndecl
;
8108 tree call
= Gogo::call_builtin(&panic_fndecl
,
8113 TREE_TYPE(arg_tree
),
8115 if (call
== error_mark_node
)
8116 return error_mark_node
;
8117 // This function will throw an exception.
8118 TREE_NOTHROW(panic_fndecl
) = 0;
8119 // This function will not return.
8120 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
8124 case BUILTIN_RECOVER
:
8126 // The argument is set when building recover thunks. It's a
8127 // boolean value which is true if we can recover a value now.
8128 const Expression_list
* args
= this->args();
8129 go_assert(args
!= NULL
&& args
->size() == 1);
8130 Expression
* arg
= args
->front();
8131 tree arg_tree
= arg
->get_tree(context
);
8132 if (arg_tree
== error_mark_node
)
8133 return error_mark_node
;
8136 Type::make_empty_interface_type(Linemap::predeclared_location());
8137 tree empty_tree
= type_to_tree(empty
->get_backend(context
->gogo()));
8139 Type
* nil_type
= Type::make_nil_type();
8140 Expression
* nil
= Expression::make_nil(location
);
8141 tree nil_tree
= nil
->get_tree(context
);
8142 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
8148 // We need to handle a deferred call to recover specially,
8149 // because it changes whether it can recover a panic or not.
8150 // See test7 in test/recover1.go.
8152 if (this->is_deferred())
8154 static tree deferred_recover_fndecl
;
8155 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
8157 "__go_deferred_recover",
8163 static tree recover_fndecl
;
8164 call
= Gogo::call_builtin(&recover_fndecl
,
8170 if (call
== error_mark_node
)
8171 return error_mark_node
;
8172 return fold_build3_loc(location
.gcc_location(), COND_EXPR
, empty_tree
,
8173 arg_tree
, call
, empty_nil_tree
);
8178 const Expression_list
* args
= this->args();
8179 go_assert(args
!= NULL
&& args
->size() == 1);
8180 Expression
* arg
= args
->front();
8181 tree arg_tree
= arg
->get_tree(context
);
8182 if (arg_tree
== error_mark_node
)
8183 return error_mark_node
;
8184 static tree close_fndecl
;
8185 return Gogo::call_builtin(&close_fndecl
,
8187 "__go_builtin_close",
8190 TREE_TYPE(arg_tree
),
8194 case BUILTIN_SIZEOF
:
8195 case BUILTIN_OFFSETOF
:
8196 case BUILTIN_ALIGNOF
:
8198 Numeric_constant nc
;
8200 if (!this->numeric_constant_value(&nc
)
8201 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
8203 go_assert(saw_errors());
8204 return error_mark_node
;
8206 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8207 tree type
= type_to_tree(uintptr_type
->get_backend(gogo
));
8208 return build_int_cst(type
, val
);
8213 const Expression_list
* args
= this->args();
8214 go_assert(args
!= NULL
&& args
->size() == 2);
8215 Expression
* arg1
= args
->front();
8216 Expression
* arg2
= args
->back();
8218 tree arg1_tree
= arg1
->get_tree(context
);
8219 tree arg2_tree
= arg2
->get_tree(context
);
8220 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
8221 return error_mark_node
;
8223 Type
* arg1_type
= arg1
->type();
8224 Array_type
* at
= arg1_type
->array_type();
8225 arg1_tree
= save_expr(arg1_tree
);
8226 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
8227 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
8228 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
8229 return error_mark_node
;
8231 Type
* arg2_type
= arg2
->type();
8234 if (arg2_type
->is_slice_type())
8236 at
= arg2_type
->array_type();
8237 arg2_tree
= save_expr(arg2_tree
);
8238 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
8239 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
8243 arg2_tree
= save_expr(arg2_tree
);
8244 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
8245 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
8247 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
8248 return error_mark_node
;
8250 arg1_len
= save_expr(arg1_len
);
8251 arg2_len
= save_expr(arg2_len
);
8252 tree len
= fold_build3_loc(location
.gcc_location(), COND_EXPR
,
8253 TREE_TYPE(arg1_len
),
8254 fold_build2_loc(location
.gcc_location(),
8255 LT_EXPR
, boolean_type_node
,
8256 arg1_len
, arg2_len
),
8257 arg1_len
, arg2_len
);
8258 len
= save_expr(len
);
8260 Type
* element_type
= at
->element_type();
8261 Btype
* element_btype
= element_type
->get_backend(gogo
);
8262 tree element_type_tree
= type_to_tree(element_btype
);
8263 if (element_type_tree
== error_mark_node
)
8264 return error_mark_node
;
8265 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
8266 tree bytecount
= fold_convert_loc(location
.gcc_location(),
8267 TREE_TYPE(element_size
), len
);
8268 bytecount
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
,
8269 TREE_TYPE(element_size
),
8270 bytecount
, element_size
);
8271 bytecount
= fold_convert_loc(location
.gcc_location(), size_type_node
,
8274 arg1_val
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
8276 arg2_val
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
8279 static tree copy_fndecl
;
8280 tree call
= Gogo::call_builtin(©_fndecl
,
8291 if (call
== error_mark_node
)
8292 return error_mark_node
;
8294 return fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
8295 TREE_TYPE(len
), call
, len
);
8298 case BUILTIN_APPEND
:
8300 const Expression_list
* args
= this->args();
8301 go_assert(args
!= NULL
&& args
->size() == 2);
8302 Expression
* arg1
= args
->front();
8303 Expression
* arg2
= args
->back();
8305 tree arg1_tree
= arg1
->get_tree(context
);
8306 tree arg2_tree
= arg2
->get_tree(context
);
8307 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
8308 return error_mark_node
;
8310 Array_type
* at
= arg1
->type()->array_type();
8311 Type
* element_type
= at
->element_type()->forwarded();
8316 if (arg2
->type()->is_string_type()
8317 && element_type
->integer_type() != NULL
8318 && element_type
->integer_type()->is_byte())
8320 arg2_tree
= save_expr(arg2_tree
);
8321 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
8322 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
8323 element_size
= size_int(1);
8327 arg2_tree
= Expression::convert_for_assignment(context
, at
,
8331 if (arg2_tree
== error_mark_node
)
8332 return error_mark_node
;
8334 arg2_tree
= save_expr(arg2_tree
);
8336 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
8337 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
8339 Btype
* element_btype
= element_type
->get_backend(gogo
);
8340 tree element_type_tree
= type_to_tree(element_btype
);
8341 if (element_type_tree
== error_mark_node
)
8342 return error_mark_node
;
8343 element_size
= TYPE_SIZE_UNIT(element_type_tree
);
8346 arg2_val
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
8348 arg2_len
= fold_convert_loc(location
.gcc_location(), size_type_node
,
8350 element_size
= fold_convert_loc(location
.gcc_location(), size_type_node
,
8353 if (arg2_val
== error_mark_node
8354 || arg2_len
== error_mark_node
8355 || element_size
== error_mark_node
)
8356 return error_mark_node
;
8358 // We rebuild the decl each time since the slice types may
8360 tree append_fndecl
= NULL_TREE
;
8361 return Gogo::call_builtin(&append_fndecl
,
8365 TREE_TYPE(arg1_tree
),
8366 TREE_TYPE(arg1_tree
),
8379 const Expression_list
* args
= this->args();
8380 go_assert(args
!= NULL
&& args
->size() == 1);
8381 Expression
* arg
= args
->front();
8382 tree arg_tree
= arg
->get_tree(context
);
8383 if (arg_tree
== error_mark_node
)
8384 return error_mark_node
;
8385 go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
8386 if (this->code_
== BUILTIN_REAL
)
8387 return fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
8388 TREE_TYPE(TREE_TYPE(arg_tree
)),
8391 return fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
8392 TREE_TYPE(TREE_TYPE(arg_tree
)),
8396 case BUILTIN_COMPLEX
:
8398 const Expression_list
* args
= this->args();
8399 go_assert(args
!= NULL
&& args
->size() == 2);
8400 tree r
= args
->front()->get_tree(context
);
8401 tree i
= args
->back()->get_tree(context
);
8402 if (r
== error_mark_node
|| i
== error_mark_node
)
8403 return error_mark_node
;
8404 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
8405 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
8406 go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
8407 return fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
8408 build_complex_type(TREE_TYPE(r
)),
8417 // We have to support exporting a builtin call expression, because
8418 // code can set a constant to the result of a builtin expression.
8421 Builtin_call_expression::do_export(Export
* exp
) const
8423 Numeric_constant nc
;
8424 if (!this->numeric_constant_value(&nc
))
8426 error_at(this->location(), "value is not constant");
8434 Integer_expression::export_integer(exp
, val
);
8437 else if (nc
.is_float())
8440 nc
.get_float(&fval
);
8441 Float_expression::export_float(exp
, fval
);
8444 else if (nc
.is_complex())
8448 Complex_expression::export_complex(exp
, real
, imag
);
8455 // A trailing space lets us reliably identify the end of the number.
8456 exp
->write_c_string(" ");
8459 // Class Call_expression.
8464 Call_expression::do_traverse(Traverse
* traverse
)
8466 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8467 return TRAVERSE_EXIT
;
8468 if (this->args_
!= NULL
)
8470 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8471 return TRAVERSE_EXIT
;
8473 return TRAVERSE_CONTINUE
;
8476 // Lower a call statement.
8479 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
8480 Statement_inserter
* inserter
, int)
8482 Location loc
= this->location();
8484 // A type cast can look like a function call.
8485 if (this->fn_
->is_type_expression()
8486 && this->args_
!= NULL
8487 && this->args_
->size() == 1)
8488 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8491 // Recognize a call to a builtin function.
8492 Func_expression
* fne
= this->fn_
->func_expression();
8494 && fne
->named_object()->is_function_declaration()
8495 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
8496 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8497 this->is_varargs_
, loc
);
8499 // Handle an argument which is a call to a function which returns
8500 // multiple results.
8501 if (this->args_
!= NULL
8502 && this->args_
->size() == 1
8503 && this->args_
->front()->call_expression() != NULL
8504 && this->fn_
->type()->function_type() != NULL
)
8506 Function_type
* fntype
= this->fn_
->type()->function_type();
8507 size_t rc
= this->args_
->front()->call_expression()->result_count();
8509 && fntype
->parameters() != NULL
8510 && (fntype
->parameters()->size() == rc
8511 || (fntype
->is_varargs()
8512 && fntype
->parameters()->size() - 1 <= rc
)))
8514 Call_expression
* call
= this->args_
->front()->call_expression();
8515 Expression_list
* args
= new Expression_list
;
8516 for (size_t i
= 0; i
< rc
; ++i
)
8517 args
->push_back(Expression::make_call_result(call
, i
));
8518 // We can't return a new call expression here, because this
8519 // one may be referenced by Call_result expressions. We
8520 // also can't delete the old arguments, because we may still
8521 // traverse them somewhere up the call stack. FIXME.
8526 // If this call returns multiple results, create a temporary
8527 // variable for each result.
8528 size_t rc
= this->result_count();
8529 if (rc
> 1 && this->results_
== NULL
)
8531 std::vector
<Temporary_statement
*>* temps
=
8532 new std::vector
<Temporary_statement
*>;
8534 const Typed_identifier_list
* results
=
8535 this->fn_
->type()->function_type()->results();
8536 for (Typed_identifier_list::const_iterator p
= results
->begin();
8537 p
!= results
->end();
8540 Temporary_statement
* temp
= Statement::make_temporary(p
->type(),
8542 inserter
->insert(temp
);
8543 temps
->push_back(temp
);
8545 this->results_
= temps
;
8548 // Handle a call to a varargs function by packaging up the extra
8550 if (this->fn_
->type()->function_type() != NULL
8551 && this->fn_
->type()->function_type()->is_varargs())
8553 Function_type
* fntype
= this->fn_
->type()->function_type();
8554 const Typed_identifier_list
* parameters
= fntype
->parameters();
8555 go_assert(parameters
!= NULL
&& !parameters
->empty());
8556 Type
* varargs_type
= parameters
->back().type();
8557 this->lower_varargs(gogo
, function
, inserter
, varargs_type
,
8558 parameters
->size());
8561 // If this is call to a method, call the method directly passing the
8562 // object as the first parameter.
8563 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8566 Named_object
* method
= bme
->method();
8567 Expression
* first_arg
= bme
->first_argument();
8569 // We always pass a pointer when calling a method.
8570 if (first_arg
->type()->points_to() == NULL
8571 && !first_arg
->type()->is_error())
8573 first_arg
= Expression::make_unary(OPERATOR_AND
, first_arg
, loc
);
8574 // We may need to create a temporary variable so that we can
8575 // take the address. We can't do that here because it will
8576 // mess up the order of evaluation.
8577 Unary_expression
* ue
= static_cast<Unary_expression
*>(first_arg
);
8578 ue
->set_create_temp();
8581 // If we are calling a method which was inherited from an
8582 // embedded struct, and the method did not get a stub, then the
8583 // first type may be wrong.
8584 Type
* fatype
= bme
->first_argument_type();
8587 if (fatype
->points_to() == NULL
)
8588 fatype
= Type::make_pointer_type(fatype
);
8589 first_arg
= Expression::make_unsafe_cast(fatype
, first_arg
, loc
);
8592 Expression_list
* new_args
= new Expression_list();
8593 new_args
->push_back(first_arg
);
8594 if (this->args_
!= NULL
)
8596 for (Expression_list::const_iterator p
= this->args_
->begin();
8597 p
!= this->args_
->end();
8599 new_args
->push_back(*p
);
8602 // We have to change in place because this structure may be
8603 // referenced by Call_result_expressions. We can't delete the
8604 // old arguments, because we may be traversing them up in some
8606 this->args_
= new_args
;
8607 this->fn_
= Expression::make_func_reference(method
, NULL
,
8614 // Lower a call to a varargs function. FUNCTION is the function in
8615 // which the call occurs--it's not the function we are calling.
8616 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8617 // PARAM_COUNT is the number of parameters of the function we are
8618 // calling; the last of these parameters will be the varargs
8622 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8623 Statement_inserter
* inserter
,
8624 Type
* varargs_type
, size_t param_count
)
8626 if (this->varargs_are_lowered_
)
8629 Location loc
= this->location();
8631 go_assert(param_count
> 0);
8632 go_assert(varargs_type
->is_slice_type());
8634 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8635 if (arg_count
< param_count
- 1)
8637 // Not enough arguments; will be caught in check_types.
8641 Expression_list
* old_args
= this->args_
;
8642 Expression_list
* new_args
= new Expression_list();
8643 bool push_empty_arg
= false;
8644 if (old_args
== NULL
|| old_args
->empty())
8646 go_assert(param_count
== 1);
8647 push_empty_arg
= true;
8651 Expression_list::const_iterator pa
;
8653 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8655 if (static_cast<size_t>(i
) == param_count
)
8657 new_args
->push_back(*pa
);
8660 // We have reached the varargs parameter.
8662 bool issued_error
= false;
8663 if (pa
== old_args
->end())
8664 push_empty_arg
= true;
8665 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8666 new_args
->push_back(*pa
);
8667 else if (this->is_varargs_
)
8669 if ((*pa
)->type()->is_slice_type())
8670 this->report_error(_("too many arguments"));
8673 error_at(this->location(),
8674 _("invalid use of %<...%> with non-slice"));
8675 this->set_is_error();
8681 Type
* element_type
= varargs_type
->array_type()->element_type();
8682 Expression_list
* vals
= new Expression_list
;
8683 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8685 // Check types here so that we get a better message.
8686 Type
* patype
= (*pa
)->type();
8687 Location paloc
= (*pa
)->location();
8688 if (!this->check_argument_type(i
, element_type
, patype
,
8689 paloc
, issued_error
))
8691 vals
->push_back(*pa
);
8694 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8695 gogo
->lower_expression(function
, inserter
, &val
);
8696 new_args
->push_back(val
);
8701 new_args
->push_back(Expression::make_nil(loc
));
8703 // We can't return a new call expression here, because this one may
8704 // be referenced by Call_result expressions. FIXME. We can't
8705 // delete OLD_ARGS because we may have both a Call_expression and a
8706 // Builtin_call_expression which refer to them. FIXME.
8707 this->args_
= new_args
;
8708 this->varargs_are_lowered_
= true;
8711 // Get the function type. This can return NULL in error cases.
8714 Call_expression::get_function_type() const
8716 return this->fn_
->type()->function_type();
8719 // Return the number of values which this call will return.
8722 Call_expression::result_count() const
8724 const Function_type
* fntype
= this->get_function_type();
8727 if (fntype
->results() == NULL
)
8729 return fntype
->results()->size();
8732 // Return the temporary which holds a result.
8734 Temporary_statement
*
8735 Call_expression::result(size_t i
) const
8737 if (this->results_
== NULL
|| this->results_
->size() <= i
)
8739 go_assert(saw_errors());
8742 return (*this->results_
)[i
];
8745 // Return whether this is a call to the predeclared function recover.
8748 Call_expression::is_recover_call() const
8750 return this->do_is_recover_call();
8753 // Set the argument to the recover function.
8756 Call_expression::set_recover_arg(Expression
* arg
)
8758 this->do_set_recover_arg(arg
);
8761 // Virtual functions also implemented by Builtin_call_expression.
8764 Call_expression::do_is_recover_call() const
8770 Call_expression::do_set_recover_arg(Expression
*)
8775 // We have found an error with this call expression; return true if
8776 // we should report it.
8779 Call_expression::issue_error()
8781 if (this->issued_error_
)
8785 this->issued_error_
= true;
8793 Call_expression::do_type()
8795 if (this->type_
!= NULL
)
8799 Function_type
* fntype
= this->get_function_type();
8801 return Type::make_error_type();
8803 const Typed_identifier_list
* results
= fntype
->results();
8804 if (results
== NULL
)
8805 ret
= Type::make_void_type();
8806 else if (results
->size() == 1)
8807 ret
= results
->begin()->type();
8809 ret
= Type::make_call_multiple_result_type(this);
8816 // Determine types for a call expression. We can use the function
8817 // parameter types to set the types of the arguments.
8820 Call_expression::do_determine_type(const Type_context
*)
8822 if (!this->determining_types())
8825 this->fn_
->determine_type_no_context();
8826 Function_type
* fntype
= this->get_function_type();
8827 const Typed_identifier_list
* parameters
= NULL
;
8829 parameters
= fntype
->parameters();
8830 if (this->args_
!= NULL
)
8832 Typed_identifier_list::const_iterator pt
;
8833 if (parameters
!= NULL
)
8834 pt
= parameters
->begin();
8836 for (Expression_list::const_iterator pa
= this->args_
->begin();
8837 pa
!= this->args_
->end();
8843 // If this is a method, the first argument is the
8845 if (fntype
!= NULL
&& fntype
->is_method())
8847 Type
* rtype
= fntype
->receiver()->type();
8848 // The receiver is always passed as a pointer.
8849 if (rtype
->points_to() == NULL
)
8850 rtype
= Type::make_pointer_type(rtype
);
8851 Type_context
subcontext(rtype
, false);
8852 (*pa
)->determine_type(&subcontext
);
8857 if (parameters
!= NULL
&& pt
!= parameters
->end())
8859 Type_context
subcontext(pt
->type(), false);
8860 (*pa
)->determine_type(&subcontext
);
8864 (*pa
)->determine_type_no_context();
8869 // Called when determining types for a Call_expression. Return true
8870 // if we should go ahead, false if they have already been determined.
8873 Call_expression::determining_types()
8875 if (this->types_are_determined_
)
8879 this->types_are_determined_
= true;
8884 // Check types for parameter I.
8887 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8888 const Type
* argument_type
,
8889 Location argument_location
,
8894 if (this->are_hidden_fields_ok_
)
8895 ok
= Type::are_assignable_hidden_ok(parameter_type
, argument_type
,
8898 ok
= Type::are_assignable(parameter_type
, argument_type
, &reason
);
8904 error_at(argument_location
, "argument %d has incompatible type", i
);
8906 error_at(argument_location
,
8907 "argument %d has incompatible type (%s)",
8910 this->set_is_error();
8919 Call_expression::do_check_types(Gogo
*)
8921 if (this->classification() == EXPRESSION_ERROR
)
8924 Function_type
* fntype
= this->get_function_type();
8927 if (!this->fn_
->type()->is_error())
8928 this->report_error(_("expected function"));
8932 bool is_method
= fntype
->is_method();
8935 go_assert(this->args_
!= NULL
&& !this->args_
->empty());
8936 Type
* rtype
= fntype
->receiver()->type();
8937 Expression
* first_arg
= this->args_
->front();
8938 // The language permits copying hidden fields for a method
8939 // receiver. We dereference the values since receivers are
8940 // always passed as pointers.
8942 if (!Type::are_assignable_hidden_ok(rtype
->deref(),
8943 first_arg
->type()->deref(),
8947 this->report_error(_("incompatible type for receiver"));
8950 error_at(this->location(),
8951 "incompatible type for receiver (%s)",
8953 this->set_is_error();
8958 // Note that varargs was handled by the lower_varargs() method, so
8959 // we don't have to worry about it here unless something is wrong.
8960 if (this->is_varargs_
&& !this->varargs_are_lowered_
)
8962 if (!fntype
->is_varargs())
8964 error_at(this->location(),
8965 _("invalid use of %<...%> calling non-variadic function"));
8966 this->set_is_error();
8971 const Typed_identifier_list
* parameters
= fntype
->parameters();
8972 if (this->args_
== NULL
)
8974 if (parameters
!= NULL
&& !parameters
->empty())
8975 this->report_error(_("not enough arguments"));
8977 else if (parameters
== NULL
)
8979 if (!is_method
|| this->args_
->size() > 1)
8980 this->report_error(_("too many arguments"));
8985 Expression_list::const_iterator pa
= this->args_
->begin();
8988 for (Typed_identifier_list::const_iterator pt
= parameters
->begin();
8989 pt
!= parameters
->end();
8992 if (pa
== this->args_
->end())
8994 this->report_error(_("not enough arguments"));
8997 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8998 (*pa
)->location(), false);
9000 if (pa
!= this->args_
->end())
9001 this->report_error(_("too many arguments"));
9005 // Return whether we have to use a temporary variable to ensure that
9006 // we evaluate this call expression in order. If the call returns no
9007 // results then it will inevitably be executed last.
9010 Call_expression::do_must_eval_in_order() const
9012 return this->result_count() > 0;
9015 // Get the function and the first argument to use when calling an
9016 // interface method.
9019 Call_expression::interface_method_function(
9020 Translate_context
* context
,
9021 Interface_field_reference_expression
* interface_method
,
9022 tree
* first_arg_ptr
)
9024 tree expr
= interface_method
->expr()->get_tree(context
);
9025 if (expr
== error_mark_node
)
9026 return error_mark_node
;
9027 expr
= save_expr(expr
);
9028 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
9029 if (first_arg
== error_mark_node
)
9030 return error_mark_node
;
9031 *first_arg_ptr
= first_arg
;
9032 return interface_method
->get_function_tree(context
, expr
);
9035 // Build the call expression.
9038 Call_expression::do_get_tree(Translate_context
* context
)
9040 if (this->tree_
!= NULL_TREE
)
9043 Function_type
* fntype
= this->get_function_type();
9045 return error_mark_node
;
9047 if (this->fn_
->is_error_expression())
9048 return error_mark_node
;
9050 Gogo
* gogo
= context
->gogo();
9051 Location location
= this->location();
9053 Func_expression
* func
= this->fn_
->func_expression();
9054 Interface_field_reference_expression
* interface_method
=
9055 this->fn_
->interface_field_reference_expression();
9056 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
9057 const bool is_interface_method
= interface_method
!= NULL
;
9061 if (this->args_
== NULL
|| this->args_
->empty())
9063 nargs
= is_interface_method
? 1 : 0;
9064 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
9066 else if (fntype
->parameters() == NULL
|| fntype
->parameters()->empty())
9068 // Passing a receiver parameter.
9069 go_assert(!is_interface_method
9070 && fntype
->is_method()
9071 && this->args_
->size() == 1);
9073 args
= new tree
[nargs
];
9074 args
[0] = this->args_
->front()->get_tree(context
);
9078 const Typed_identifier_list
* params
= fntype
->parameters();
9080 nargs
= this->args_
->size();
9081 int i
= is_interface_method
? 1 : 0;
9083 args
= new tree
[nargs
];
9085 Typed_identifier_list::const_iterator pp
= params
->begin();
9086 Expression_list::const_iterator pe
= this->args_
->begin();
9087 if (!is_interface_method
&& fntype
->is_method())
9089 args
[i
] = (*pe
)->get_tree(context
);
9093 for (; pe
!= this->args_
->end(); ++pe
, ++pp
, ++i
)
9095 go_assert(pp
!= params
->end());
9096 tree arg_val
= (*pe
)->get_tree(context
);
9097 args
[i
] = Expression::convert_for_assignment(context
,
9102 if (args
[i
] == error_mark_node
)
9105 return error_mark_node
;
9108 go_assert(pp
== params
->end());
9109 go_assert(i
== nargs
);
9112 tree rettype
= TREE_TYPE(TREE_TYPE(type_to_tree(fntype
->get_backend(gogo
))));
9113 if (rettype
== error_mark_node
)
9116 return error_mark_node
;
9121 fn
= func
->get_tree_without_closure(gogo
);
9122 else if (!is_interface_method
)
9123 fn
= this->fn_
->get_tree(context
);
9125 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
9127 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
9130 return error_mark_node
;
9134 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
9135 fndecl
= TREE_OPERAND(fndecl
, 0);
9137 // Add a type cast in case the type of the function is a recursive
9138 // type which refers to itself.
9139 if (!DECL_P(fndecl
) || !DECL_IS_BUILTIN(fndecl
))
9141 tree fnt
= type_to_tree(fntype
->get_backend(gogo
));
9142 if (fnt
== error_mark_node
)
9143 return error_mark_node
;
9144 fn
= fold_convert_loc(location
.gcc_location(), fnt
, fn
);
9147 // This is to support builtin math functions when using 80387 math.
9148 tree excess_type
= NULL_TREE
;
9150 && TREE_CODE(fndecl
) == FUNCTION_DECL
9151 && DECL_IS_BUILTIN(fndecl
)
9152 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
9154 && ((SCALAR_FLOAT_TYPE_P(rettype
)
9155 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
9156 || (COMPLEX_FLOAT_TYPE_P(rettype
)
9157 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
9159 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
9160 if (excess_type
!= NULL_TREE
)
9162 tree excess_fndecl
= mathfn_built_in(excess_type
,
9163 DECL_FUNCTION_CODE(fndecl
));
9164 if (excess_fndecl
== NULL_TREE
)
9165 excess_type
= NULL_TREE
;
9168 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
9170 for (int i
= 0; i
< nargs
; ++i
)
9172 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
9173 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
9174 args
[i
] = ::convert(excess_type
, args
[i
]);
9180 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
9184 SET_EXPR_LOCATION(ret
, location
.gcc_location());
9188 tree closure_tree
= func
->closure()->get_tree(context
);
9189 if (closure_tree
!= error_mark_node
)
9190 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
9193 // If this is a recursive function type which returns itself, as in
9195 // we have used ptr_type_node for the return type. Add a cast here
9196 // to the correct type.
9197 if (TREE_TYPE(ret
) == ptr_type_node
)
9199 tree t
= type_to_tree(this->type()->base()->get_backend(gogo
));
9200 ret
= fold_convert_loc(location
.gcc_location(), t
, ret
);
9203 if (excess_type
!= NULL_TREE
)
9205 // Calling convert here can undo our excess precision change.
9206 // That may or may not be a bug in convert_to_real.
9207 ret
= build1(NOP_EXPR
, rettype
, ret
);
9210 if (this->results_
!= NULL
)
9211 ret
= this->set_results(context
, ret
);
9218 // Set the result variables if this call returns multiple results.
9221 Call_expression::set_results(Translate_context
* context
, tree call_tree
)
9223 tree stmt_list
= NULL_TREE
;
9225 call_tree
= save_expr(call_tree
);
9227 if (TREE_CODE(TREE_TYPE(call_tree
)) != RECORD_TYPE
)
9229 go_assert(saw_errors());
9233 Location loc
= this->location();
9234 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
9235 size_t rc
= this->result_count();
9236 for (size_t i
= 0; i
< rc
; ++i
, field
= DECL_CHAIN(field
))
9238 go_assert(field
!= NULL_TREE
);
9240 Temporary_statement
* temp
= this->result(i
);
9243 go_assert(saw_errors());
9244 return error_mark_node
;
9246 Temporary_reference_expression
* ref
=
9247 Expression::make_temporary_reference(temp
, loc
);
9248 ref
->set_is_lvalue();
9249 tree temp_tree
= ref
->get_tree(context
);
9250 if (temp_tree
== error_mark_node
)
9251 return error_mark_node
;
9253 tree val_tree
= build3_loc(loc
.gcc_location(), COMPONENT_REF
,
9254 TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
9255 tree set_tree
= build2_loc(loc
.gcc_location(), MODIFY_EXPR
,
9256 void_type_node
, temp_tree
, val_tree
);
9258 append_to_statement_list(set_tree
, &stmt_list
);
9260 go_assert(field
== NULL_TREE
);
9262 return save_expr(stmt_list
);
9265 // Dump ast representation for a call expressin.
9268 Call_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
9270 this->fn_
->dump_expression(ast_dump_context
);
9271 ast_dump_context
->ostream() << "(";
9273 ast_dump_context
->dump_expression_list(this->args_
);
9275 ast_dump_context
->ostream() << ") ";
9278 // Make a call expression.
9281 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
9284 return new Call_expression(fn
, args
, is_varargs
, location
);
9287 // A single result from a call which returns multiple results.
9289 class Call_result_expression
: public Expression
9292 Call_result_expression(Call_expression
* call
, unsigned int index
)
9293 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
9294 call_(call
), index_(index
)
9299 do_traverse(Traverse
*);
9305 do_determine_type(const Type_context
*);
9308 do_check_types(Gogo
*);
9313 return new Call_result_expression(this->call_
->call_expression(),
9318 do_must_eval_in_order() const
9322 do_get_tree(Translate_context
*);
9325 do_dump_expression(Ast_dump_context
*) const;
9328 // The underlying call expression.
9330 // Which result we want.
9331 unsigned int index_
;
9334 // Traverse a call result.
9337 Call_result_expression::do_traverse(Traverse
* traverse
)
9339 if (traverse
->remember_expression(this->call_
))
9341 // We have already traversed the call expression.
9342 return TRAVERSE_CONTINUE
;
9344 return Expression::traverse(&this->call_
, traverse
);
9350 Call_result_expression::do_type()
9352 if (this->classification() == EXPRESSION_ERROR
)
9353 return Type::make_error_type();
9355 // THIS->CALL_ can be replaced with a temporary reference due to
9356 // Call_expression::do_must_eval_in_order when there is an error.
9357 Call_expression
* ce
= this->call_
->call_expression();
9360 this->set_is_error();
9361 return Type::make_error_type();
9363 Function_type
* fntype
= ce
->get_function_type();
9366 if (ce
->issue_error())
9368 if (!ce
->fn()->type()->is_error())
9369 this->report_error(_("expected function"));
9371 this->set_is_error();
9372 return Type::make_error_type();
9374 const Typed_identifier_list
* results
= fntype
->results();
9375 if (results
== NULL
|| results
->size() < 2)
9377 if (ce
->issue_error())
9378 this->report_error(_("number of results does not match "
9379 "number of values"));
9380 return Type::make_error_type();
9382 Typed_identifier_list::const_iterator pr
= results
->begin();
9383 for (unsigned int i
= 0; i
< this->index_
; ++i
)
9385 if (pr
== results
->end())
9389 if (pr
== results
->end())
9391 if (ce
->issue_error())
9392 this->report_error(_("number of results does not match "
9393 "number of values"));
9394 return Type::make_error_type();
9399 // Check the type. Just make sure that we trigger the warning in
9403 Call_result_expression::do_check_types(Gogo
*)
9408 // Determine the type. We have nothing to do here, but the 0 result
9409 // needs to pass down to the caller.
9412 Call_result_expression::do_determine_type(const Type_context
*)
9414 this->call_
->determine_type_no_context();
9417 // Return the tree. We just refer to the temporary set by the call
9418 // expression. We don't do this at lowering time because it makes it
9419 // hard to evaluate the call at the right time.
9422 Call_result_expression::do_get_tree(Translate_context
* context
)
9424 Call_expression
* ce
= this->call_
->call_expression();
9427 go_assert(this->call_
->is_error_expression());
9428 return error_mark_node
;
9430 Temporary_statement
* ts
= ce
->result(this->index_
);
9433 go_assert(saw_errors());
9434 return error_mark_node
;
9436 Expression
* ref
= Expression::make_temporary_reference(ts
, this->location());
9437 return ref
->get_tree(context
);
9440 // Dump ast representation for a call result expression.
9443 Call_result_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
9446 // FIXME: Wouldn't it be better if the call is assigned to a temporary
9447 // (struct) and the fields are referenced instead.
9448 ast_dump_context
->ostream() << this->index_
<< "@(";
9449 ast_dump_context
->dump_expression(this->call_
);
9450 ast_dump_context
->ostream() << ")";
9453 // Make a reference to a single result of a call which returns
9454 // multiple results.
9457 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
9459 return new Call_result_expression(call
, index
);
9462 // Class Index_expression.
9467 Index_expression::do_traverse(Traverse
* traverse
)
9469 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
9470 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
9471 || (this->end_
!= NULL
9472 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
9473 return TRAVERSE_EXIT
;
9474 return TRAVERSE_CONTINUE
;
9477 // Lower an index expression. This converts the generic index
9478 // expression into an array index, a string index, or a map index.
9481 Index_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
9483 Location location
= this->location();
9484 Expression
* left
= this->left_
;
9485 Expression
* start
= this->start_
;
9486 Expression
* end
= this->end_
;
9488 Type
* type
= left
->type();
9489 if (type
->is_error())
9490 return Expression::make_error(location
);
9491 else if (left
->is_type_expression())
9493 error_at(location
, "attempt to index type expression");
9494 return Expression::make_error(location
);
9496 else if (type
->array_type() != NULL
)
9497 return Expression::make_array_index(left
, start
, end
, location
);
9498 else if (type
->points_to() != NULL
9499 && type
->points_to()->array_type() != NULL
9500 && !type
->points_to()->is_slice_type())
9502 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
9504 return Expression::make_array_index(deref
, start
, end
, location
);
9506 else if (type
->is_string_type())
9507 return Expression::make_string_index(left
, start
, end
, location
);
9508 else if (type
->map_type() != NULL
)
9512 error_at(location
, "invalid slice of map");
9513 return Expression::make_error(location
);
9515 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
9517 if (this->is_lvalue_
)
9518 ret
->set_is_lvalue();
9524 "attempt to index object which is not array, string, or map");
9525 return Expression::make_error(location
);
9529 // Write an indexed expression (expr[expr:expr] or expr[expr]) to a
9533 Index_expression::dump_index_expression(Ast_dump_context
* ast_dump_context
,
9534 const Expression
* expr
,
9535 const Expression
* start
,
9536 const Expression
* end
)
9538 expr
->dump_expression(ast_dump_context
);
9539 ast_dump_context
->ostream() << "[";
9540 start
->dump_expression(ast_dump_context
);
9543 ast_dump_context
->ostream() << ":";
9544 end
->dump_expression(ast_dump_context
);
9546 ast_dump_context
->ostream() << "]";
9549 // Dump ast representation for an index expression.
9552 Index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
9555 Index_expression::dump_index_expression(ast_dump_context
, this->left_
,
9556 this->start_
, this->end_
);
9559 // Make an index expression.
9562 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
9565 return new Index_expression(left
, start
, end
, location
);
9568 // An array index. This is used for both indexing and slicing.
9570 class Array_index_expression
: public Expression
9573 Array_index_expression(Expression
* array
, Expression
* start
,
9574 Expression
* end
, Location location
)
9575 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
9576 array_(array
), start_(start
), end_(end
), type_(NULL
)
9581 do_traverse(Traverse
*);
9587 do_determine_type(const Type_context
*);
9590 do_check_types(Gogo
*);
9595 return Expression::make_array_index(this->array_
->copy(),
9596 this->start_
->copy(),
9599 : this->end_
->copy()),
9604 do_must_eval_subexpressions_in_order(int* skip
) const
9611 do_is_addressable() const;
9614 do_address_taken(bool escapes
)
9615 { this->array_
->address_taken(escapes
); }
9618 do_get_tree(Translate_context
*);
9621 do_dump_expression(Ast_dump_context
*) const;
9624 // The array we are getting a value from.
9626 // The start or only index.
9628 // The end index of a slice. This may be NULL for a simple array
9629 // index, or it may be a nil expression for the length of the array.
9631 // The type of the expression.
9635 // Array index traversal.
9638 Array_index_expression::do_traverse(Traverse
* traverse
)
9640 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9641 return TRAVERSE_EXIT
;
9642 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9643 return TRAVERSE_EXIT
;
9644 if (this->end_
!= NULL
)
9646 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9647 return TRAVERSE_EXIT
;
9649 return TRAVERSE_CONTINUE
;
9652 // Return the type of an array index.
9655 Array_index_expression::do_type()
9657 if (this->type_
== NULL
)
9659 Array_type
* type
= this->array_
->type()->array_type();
9661 this->type_
= Type::make_error_type();
9662 else if (this->end_
== NULL
)
9663 this->type_
= type
->element_type();
9664 else if (type
->is_slice_type())
9666 // A slice of a slice has the same type as the original
9668 this->type_
= this->array_
->type()->deref();
9672 // A slice of an array is a slice.
9673 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9679 // Set the type of an array index.
9682 Array_index_expression::do_determine_type(const Type_context
*)
9684 this->array_
->determine_type_no_context();
9685 this->start_
->determine_type_no_context();
9686 if (this->end_
!= NULL
)
9687 this->end_
->determine_type_no_context();
9690 // Check types of an array index.
9693 Array_index_expression::do_check_types(Gogo
*)
9695 if (this->start_
->type()->integer_type() == NULL
)
9696 this->report_error(_("index must be integer"));
9697 if (this->end_
!= NULL
9698 && this->end_
->type()->integer_type() == NULL
9699 && !this->end_
->type()->is_error()
9700 && !this->end_
->is_nil_expression()
9701 && !this->end_
->is_error_expression())
9702 this->report_error(_("slice end must be integer"));
9704 Array_type
* array_type
= this->array_
->type()->array_type();
9705 if (array_type
== NULL
)
9707 go_assert(this->array_
->type()->is_error());
9711 unsigned int int_bits
=
9712 Type::lookup_integer_type("int")->integer_type()->bits();
9714 Numeric_constant lvalnc
;
9716 bool lval_valid
= (array_type
->length() != NULL
9717 && array_type
->length()->numeric_constant_value(&lvalnc
)
9718 && lvalnc
.to_int(&lval
));
9719 Numeric_constant inc
;
9721 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
9723 if (mpz_sgn(ival
) < 0
9724 || mpz_sizeinbase(ival
, 2) >= int_bits
9726 && (this->end_
== NULL
9727 ? mpz_cmp(ival
, lval
) >= 0
9728 : mpz_cmp(ival
, lval
) > 0)))
9730 error_at(this->start_
->location(), "array index out of bounds");
9731 this->set_is_error();
9735 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9737 Numeric_constant enc
;
9739 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
9741 if (mpz_sgn(eval
) < 0
9742 || mpz_sizeinbase(eval
, 2) >= int_bits
9743 || (lval_valid
&& mpz_cmp(eval
, lval
) > 0))
9745 error_at(this->end_
->location(), "array index out of bounds");
9746 this->set_is_error();
9754 // A slice of an array requires an addressable array. A slice of a
9755 // slice is always possible.
9756 if (this->end_
!= NULL
&& !array_type
->is_slice_type())
9758 if (!this->array_
->is_addressable())
9759 this->report_error(_("slice of unaddressable value"));
9761 this->array_
->address_taken(true);
9765 // Return whether this expression is addressable.
9768 Array_index_expression::do_is_addressable() const
9770 // A slice expression is not addressable.
9771 if (this->end_
!= NULL
)
9774 // An index into a slice is addressable.
9775 if (this->array_
->type()->is_slice_type())
9778 // An index into an array is addressable if the array is
9780 return this->array_
->is_addressable();
9783 // Get a tree for an array index.
9786 Array_index_expression::do_get_tree(Translate_context
* context
)
9788 Gogo
* gogo
= context
->gogo();
9789 Location loc
= this->location();
9791 Array_type
* array_type
= this->array_
->type()->array_type();
9792 if (array_type
== NULL
)
9794 go_assert(this->array_
->type()->is_error());
9795 return error_mark_node
;
9798 tree type_tree
= type_to_tree(array_type
->get_backend(gogo
));
9799 if (type_tree
== error_mark_node
)
9800 return error_mark_node
;
9802 tree array_tree
= this->array_
->get_tree(context
);
9803 if (array_tree
== error_mark_node
)
9804 return error_mark_node
;
9806 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9807 array_tree
= save_expr(array_tree
);
9809 tree length_tree
= NULL_TREE
;
9810 if (this->end_
== NULL
|| this->end_
->is_nil_expression())
9812 length_tree
= array_type
->length_tree(gogo
, array_tree
);
9813 if (length_tree
== error_mark_node
)
9814 return error_mark_node
;
9815 length_tree
= save_expr(length_tree
);
9818 tree capacity_tree
= NULL_TREE
;
9819 if (this->end_
!= NULL
)
9821 capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9822 if (capacity_tree
== error_mark_node
)
9823 return error_mark_node
;
9824 capacity_tree
= save_expr(capacity_tree
);
9827 tree length_type
= (length_tree
!= NULL_TREE
9828 ? TREE_TYPE(length_tree
)
9829 : TREE_TYPE(capacity_tree
));
9831 tree bad_index
= boolean_false_node
;
9833 tree start_tree
= this->start_
->get_tree(context
);
9834 if (start_tree
== error_mark_node
)
9835 return error_mark_node
;
9836 if (!DECL_P(start_tree
))
9837 start_tree
= save_expr(start_tree
);
9838 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9839 start_tree
= convert_to_integer(length_type
, start_tree
);
9841 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9844 start_tree
= fold_convert_loc(loc
.gcc_location(), length_type
, start_tree
);
9845 bad_index
= fold_build2_loc(loc
.gcc_location(), TRUTH_OR_EXPR
,
9846 boolean_type_node
, bad_index
,
9847 fold_build2_loc(loc
.gcc_location(),
9851 boolean_type_node
, start_tree
,
9856 int code
= (array_type
->length() != NULL
9857 ? (this->end_
== NULL
9858 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9859 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9860 : (this->end_
== NULL
9861 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9862 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9863 tree crash
= Gogo::runtime_error(code
, loc
);
9865 if (this->end_
== NULL
)
9867 // Simple array indexing. This has to return an l-value, so
9868 // wrap the index check into START_TREE.
9869 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9870 build3(COND_EXPR
, void_type_node
,
9871 bad_index
, crash
, NULL_TREE
),
9873 start_tree
= fold_convert_loc(loc
.gcc_location(), sizetype
, start_tree
);
9875 if (array_type
->length() != NULL
)
9878 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9879 start_tree
, NULL_TREE
, NULL_TREE
);
9884 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9885 Type
* element_type
= array_type
->element_type();
9886 Btype
* belement_type
= element_type
->get_backend(gogo
);
9887 tree element_type_tree
= type_to_tree(belement_type
);
9888 if (element_type_tree
== error_mark_node
)
9889 return error_mark_node
;
9890 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9891 tree offset
= fold_build2_loc(loc
.gcc_location(), MULT_EXPR
, sizetype
,
9892 start_tree
, element_size
);
9893 tree ptr
= fold_build2_loc(loc
.gcc_location(), POINTER_PLUS_EXPR
,
9894 TREE_TYPE(values
), values
, offset
);
9895 return build_fold_indirect_ref(ptr
);
9902 if (this->end_
->is_nil_expression())
9903 end_tree
= length_tree
;
9906 end_tree
= this->end_
->get_tree(context
);
9907 if (end_tree
== error_mark_node
)
9908 return error_mark_node
;
9909 if (!DECL_P(end_tree
))
9910 end_tree
= save_expr(end_tree
);
9911 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9912 end_tree
= convert_to_integer(length_type
, end_tree
);
9914 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9917 end_tree
= fold_convert_loc(loc
.gcc_location(), length_type
, end_tree
);
9919 tree bad_end
= fold_build2_loc(loc
.gcc_location(), TRUTH_OR_EXPR
,
9921 fold_build2_loc(loc
.gcc_location(),
9922 LT_EXPR
, boolean_type_node
,
9923 end_tree
, start_tree
),
9924 fold_build2_loc(loc
.gcc_location(),
9925 GT_EXPR
, boolean_type_node
,
9926 end_tree
, capacity_tree
));
9927 bad_index
= fold_build2_loc(loc
.gcc_location(), TRUTH_OR_EXPR
,
9928 boolean_type_node
, bad_index
, bad_end
);
9931 Type
* element_type
= array_type
->element_type();
9932 tree element_type_tree
= type_to_tree(element_type
->get_backend(gogo
));
9933 if (element_type_tree
== error_mark_node
)
9934 return error_mark_node
;
9935 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9937 tree offset
= fold_build2_loc(loc
.gcc_location(), MULT_EXPR
, sizetype
,
9938 fold_convert_loc(loc
.gcc_location(), sizetype
,
9942 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9943 if (value_pointer
== error_mark_node
)
9944 return error_mark_node
;
9946 value_pointer
= fold_build2_loc(loc
.gcc_location(), POINTER_PLUS_EXPR
,
9947 TREE_TYPE(value_pointer
),
9948 value_pointer
, offset
);
9950 tree result_length_tree
= fold_build2_loc(loc
.gcc_location(), MINUS_EXPR
,
9951 length_type
, end_tree
, start_tree
);
9953 tree result_capacity_tree
= fold_build2_loc(loc
.gcc_location(), MINUS_EXPR
,
9954 length_type
, capacity_tree
,
9957 tree struct_tree
= type_to_tree(this->type()->get_backend(gogo
));
9958 go_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9960 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9962 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9963 tree field
= TYPE_FIELDS(struct_tree
);
9964 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9966 elt
->value
= value_pointer
;
9968 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9969 field
= DECL_CHAIN(field
);
9970 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9972 elt
->value
= fold_convert_loc(loc
.gcc_location(), TREE_TYPE(field
),
9973 result_length_tree
);
9975 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9976 field
= DECL_CHAIN(field
);
9977 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9979 elt
->value
= fold_convert_loc(loc
.gcc_location(), TREE_TYPE(field
),
9980 result_capacity_tree
);
9982 tree constructor
= build_constructor(struct_tree
, init
);
9984 if (TREE_CONSTANT(value_pointer
)
9985 && TREE_CONSTANT(result_length_tree
)
9986 && TREE_CONSTANT(result_capacity_tree
))
9987 TREE_CONSTANT(constructor
) = 1;
9989 return fold_build2_loc(loc
.gcc_location(), COMPOUND_EXPR
,
9990 TREE_TYPE(constructor
),
9991 build3(COND_EXPR
, void_type_node
,
9992 bad_index
, crash
, NULL_TREE
),
9996 // Dump ast representation for an array index expression.
9999 Array_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10002 Index_expression::dump_index_expression(ast_dump_context
, this->array_
,
10003 this->start_
, this->end_
);
10006 // Make an array index expression. END may be NULL.
10009 Expression::make_array_index(Expression
* array
, Expression
* start
,
10010 Expression
* end
, Location location
)
10012 return new Array_index_expression(array
, start
, end
, location
);
10015 // A string index. This is used for both indexing and slicing.
10017 class String_index_expression
: public Expression
10020 String_index_expression(Expression
* string
, Expression
* start
,
10021 Expression
* end
, Location location
)
10022 : Expression(EXPRESSION_STRING_INDEX
, location
),
10023 string_(string
), start_(start
), end_(end
)
10028 do_traverse(Traverse
*);
10034 do_determine_type(const Type_context
*);
10037 do_check_types(Gogo
*);
10042 return Expression::make_string_index(this->string_
->copy(),
10043 this->start_
->copy(),
10044 (this->end_
== NULL
10046 : this->end_
->copy()),
10051 do_must_eval_subexpressions_in_order(int* skip
) const
10058 do_get_tree(Translate_context
*);
10061 do_dump_expression(Ast_dump_context
*) const;
10064 // The string we are getting a value from.
10065 Expression
* string_
;
10066 // The start or only index.
10067 Expression
* start_
;
10068 // The end index of a slice. This may be NULL for a single index,
10069 // or it may be a nil expression for the length of the string.
10073 // String index traversal.
10076 String_index_expression::do_traverse(Traverse
* traverse
)
10078 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
10079 return TRAVERSE_EXIT
;
10080 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
10081 return TRAVERSE_EXIT
;
10082 if (this->end_
!= NULL
)
10084 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
10085 return TRAVERSE_EXIT
;
10087 return TRAVERSE_CONTINUE
;
10090 // Return the type of a string index.
10093 String_index_expression::do_type()
10095 if (this->end_
== NULL
)
10096 return Type::lookup_integer_type("uint8");
10098 return this->string_
->type();
10101 // Determine the type of a string index.
10104 String_index_expression::do_determine_type(const Type_context
*)
10106 this->string_
->determine_type_no_context();
10107 this->start_
->determine_type_no_context();
10108 if (this->end_
!= NULL
)
10109 this->end_
->determine_type_no_context();
10112 // Check types of a string index.
10115 String_index_expression::do_check_types(Gogo
*)
10117 if (this->start_
->type()->integer_type() == NULL
)
10118 this->report_error(_("index must be integer"));
10119 if (this->end_
!= NULL
10120 && this->end_
->type()->integer_type() == NULL
10121 && !this->end_
->is_nil_expression())
10122 this->report_error(_("slice end must be integer"));
10125 bool sval_valid
= this->string_
->string_constant_value(&sval
);
10127 Numeric_constant inc
;
10129 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
10131 if (mpz_sgn(ival
) < 0
10132 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
10134 error_at(this->start_
->location(), "string index out of bounds");
10135 this->set_is_error();
10139 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
10141 Numeric_constant enc
;
10143 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
10145 if (mpz_sgn(eval
) < 0
10146 || (sval_valid
&& mpz_cmp_ui(eval
, sval
.length()) > 0))
10148 error_at(this->end_
->location(), "string index out of bounds");
10149 this->set_is_error();
10156 // Get a tree for a string index.
10159 String_index_expression::do_get_tree(Translate_context
* context
)
10161 Location loc
= this->location();
10163 tree string_tree
= this->string_
->get_tree(context
);
10164 if (string_tree
== error_mark_node
)
10165 return error_mark_node
;
10167 if (this->string_
->type()->points_to() != NULL
)
10168 string_tree
= build_fold_indirect_ref(string_tree
);
10169 if (!DECL_P(string_tree
))
10170 string_tree
= save_expr(string_tree
);
10171 tree string_type
= TREE_TYPE(string_tree
);
10173 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
10174 length_tree
= save_expr(length_tree
);
10175 tree length_type
= TREE_TYPE(length_tree
);
10177 tree bad_index
= boolean_false_node
;
10179 tree start_tree
= this->start_
->get_tree(context
);
10180 if (start_tree
== error_mark_node
)
10181 return error_mark_node
;
10182 if (!DECL_P(start_tree
))
10183 start_tree
= save_expr(start_tree
);
10184 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
10185 start_tree
= convert_to_integer(length_type
, start_tree
);
10187 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
10190 start_tree
= fold_convert_loc(loc
.gcc_location(), length_type
, start_tree
);
10192 int code
= (this->end_
== NULL
10193 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
10194 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
10195 tree crash
= Gogo::runtime_error(code
, loc
);
10197 if (this->end_
== NULL
)
10199 bad_index
= fold_build2_loc(loc
.gcc_location(), TRUTH_OR_EXPR
,
10200 boolean_type_node
, bad_index
,
10201 fold_build2_loc(loc
.gcc_location(), GE_EXPR
,
10203 start_tree
, length_tree
));
10205 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
10206 tree ptr
= fold_build2_loc(loc
.gcc_location(), POINTER_PLUS_EXPR
,
10207 TREE_TYPE(bytes_tree
),
10209 fold_convert_loc(loc
.gcc_location(), sizetype
,
10211 tree index
= build_fold_indirect_ref_loc(loc
.gcc_location(), ptr
);
10213 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
10214 build3(COND_EXPR
, void_type_node
,
10215 bad_index
, crash
, NULL_TREE
),
10221 if (this->end_
->is_nil_expression())
10222 end_tree
= build_int_cst(length_type
, -1);
10225 end_tree
= this->end_
->get_tree(context
);
10226 if (end_tree
== error_mark_node
)
10227 return error_mark_node
;
10228 if (!DECL_P(end_tree
))
10229 end_tree
= save_expr(end_tree
);
10230 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
10231 end_tree
= convert_to_integer(length_type
, end_tree
);
10233 bad_index
= Expression::check_bounds(end_tree
, length_type
,
10236 end_tree
= fold_convert_loc(loc
.gcc_location(), length_type
,
10240 static tree strslice_fndecl
;
10241 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
10243 "__go_string_slice",
10252 if (ret
== error_mark_node
)
10253 return error_mark_node
;
10254 // This will panic if the bounds are out of range for the
10256 TREE_NOTHROW(strslice_fndecl
) = 0;
10258 if (bad_index
== boolean_false_node
)
10261 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
10262 build3(COND_EXPR
, void_type_node
,
10263 bad_index
, crash
, NULL_TREE
),
10268 // Dump ast representation for a string index expression.
10271 String_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10274 Index_expression::dump_index_expression(ast_dump_context
, this->string_
,
10275 this->start_
, this->end_
);
10278 // Make a string index expression. END may be NULL.
10281 Expression::make_string_index(Expression
* string
, Expression
* start
,
10282 Expression
* end
, Location location
)
10284 return new String_index_expression(string
, start
, end
, location
);
10287 // Class Map_index.
10289 // Get the type of the map.
10292 Map_index_expression::get_map_type() const
10294 Map_type
* mt
= this->map_
->type()->deref()->map_type();
10296 go_assert(saw_errors());
10300 // Map index traversal.
10303 Map_index_expression::do_traverse(Traverse
* traverse
)
10305 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
10306 return TRAVERSE_EXIT
;
10307 return Expression::traverse(&this->index_
, traverse
);
10310 // Return the type of a map index.
10313 Map_index_expression::do_type()
10315 Map_type
* mt
= this->get_map_type();
10317 return Type::make_error_type();
10318 Type
* type
= mt
->val_type();
10319 // If this map index is in a tuple assignment, we actually return a
10320 // pointer to the value type. Tuple_map_assignment_statement is
10321 // responsible for handling this correctly. We need to get the type
10322 // right in case this gets assigned to a temporary variable.
10323 if (this->is_in_tuple_assignment_
)
10324 type
= Type::make_pointer_type(type
);
10328 // Fix the type of a map index.
10331 Map_index_expression::do_determine_type(const Type_context
*)
10333 this->map_
->determine_type_no_context();
10334 Map_type
* mt
= this->get_map_type();
10335 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
10336 Type_context
subcontext(key_type
, false);
10337 this->index_
->determine_type(&subcontext
);
10340 // Check types of a map index.
10343 Map_index_expression::do_check_types(Gogo
*)
10345 std::string reason
;
10346 Map_type
* mt
= this->get_map_type();
10349 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
10351 if (reason
.empty())
10352 this->report_error(_("incompatible type for map index"));
10355 error_at(this->location(), "incompatible type for map index (%s)",
10357 this->set_is_error();
10362 // Get a tree for a map index.
10365 Map_index_expression::do_get_tree(Translate_context
* context
)
10367 Map_type
* type
= this->get_map_type();
10369 return error_mark_node
;
10371 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
10372 if (valptr
== error_mark_node
)
10373 return error_mark_node
;
10374 valptr
= save_expr(valptr
);
10376 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
10378 if (this->is_lvalue_
)
10379 return build_fold_indirect_ref(valptr
);
10380 else if (this->is_in_tuple_assignment_
)
10382 // Tuple_map_assignment_statement is responsible for using this
10388 Gogo
* gogo
= context
->gogo();
10389 Btype
* val_btype
= type
->val_type()->get_backend(gogo
);
10390 Bexpression
* val_zero
= gogo
->backend()->zero_expression(val_btype
);
10391 return fold_build3(COND_EXPR
, val_type_tree
,
10392 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
10393 fold_convert(TREE_TYPE(valptr
),
10394 null_pointer_node
)),
10395 expr_to_tree(val_zero
),
10396 build_fold_indirect_ref(valptr
));
10400 // Get a tree for the map index. This returns a tree which evaluates
10401 // to a pointer to a value. The pointer will be NULL if the key is
10405 Map_index_expression::get_value_pointer(Translate_context
* context
,
10408 Map_type
* type
= this->get_map_type();
10410 return error_mark_node
;
10412 tree map_tree
= this->map_
->get_tree(context
);
10413 tree index_tree
= this->index_
->get_tree(context
);
10414 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
10415 this->index_
->type(),
10418 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
10419 return error_mark_node
;
10421 if (this->map_
->type()->points_to() != NULL
)
10422 map_tree
= build_fold_indirect_ref(map_tree
);
10424 // We need to pass in a pointer to the key, so stuff it into a
10428 if (current_function_decl
!= NULL
)
10430 tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
10431 DECL_IGNORED_P(tmp
) = 0;
10432 DECL_INITIAL(tmp
) = index_tree
;
10433 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
10434 TREE_ADDRESSABLE(tmp
) = 1;
10438 tmp
= build_decl(this->location().gcc_location(), VAR_DECL
,
10439 create_tmp_var_name("M"),
10440 TREE_TYPE(index_tree
));
10441 DECL_EXTERNAL(tmp
) = 0;
10442 TREE_PUBLIC(tmp
) = 0;
10443 TREE_STATIC(tmp
) = 1;
10444 DECL_ARTIFICIAL(tmp
) = 1;
10445 if (!TREE_CONSTANT(index_tree
))
10446 make_tmp
= fold_build2_loc(this->location().gcc_location(),
10447 INIT_EXPR
, void_type_node
,
10451 TREE_READONLY(tmp
) = 1;
10452 TREE_CONSTANT(tmp
) = 1;
10453 DECL_INITIAL(tmp
) = index_tree
;
10454 make_tmp
= NULL_TREE
;
10456 rest_of_decl_compilation(tmp
, 1, 0);
10459 fold_convert_loc(this->location().gcc_location(), const_ptr_type_node
,
10460 build_fold_addr_expr_loc(this->location().gcc_location(),
10463 static tree map_index_fndecl
;
10464 tree call
= Gogo::call_builtin(&map_index_fndecl
,
10468 const_ptr_type_node
,
10469 TREE_TYPE(map_tree
),
10471 const_ptr_type_node
,
10475 ? boolean_true_node
10476 : boolean_false_node
));
10477 if (call
== error_mark_node
)
10478 return error_mark_node
;
10479 // This can panic on a map of interface type if the interface holds
10480 // an uncomparable or unhashable type.
10481 TREE_NOTHROW(map_index_fndecl
) = 0;
10483 Type
* val_type
= type
->val_type();
10484 tree val_type_tree
= type_to_tree(val_type
->get_backend(context
->gogo()));
10485 if (val_type_tree
== error_mark_node
)
10486 return error_mark_node
;
10487 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
10489 tree ret
= fold_convert_loc(this->location().gcc_location(),
10490 ptr_val_type_tree
, call
);
10491 if (make_tmp
!= NULL_TREE
)
10492 ret
= build2(COMPOUND_EXPR
, ptr_val_type_tree
, make_tmp
, ret
);
10496 // Dump ast representation for a map index expression
10499 Map_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10502 Index_expression::dump_index_expression(ast_dump_context
,
10503 this->map_
, this->index_
, NULL
);
10506 // Make a map index expression.
10508 Map_index_expression
*
10509 Expression::make_map_index(Expression
* map
, Expression
* index
,
10512 return new Map_index_expression(map
, index
, location
);
10515 // Class Field_reference_expression.
10517 // Return the type of a field reference.
10520 Field_reference_expression::do_type()
10522 Type
* type
= this->expr_
->type();
10523 if (type
->is_error())
10525 Struct_type
* struct_type
= type
->struct_type();
10526 go_assert(struct_type
!= NULL
);
10527 return struct_type
->field(this->field_index_
)->type();
10530 // Check the types for a field reference.
10533 Field_reference_expression::do_check_types(Gogo
*)
10535 Type
* type
= this->expr_
->type();
10536 if (type
->is_error())
10538 Struct_type
* struct_type
= type
->struct_type();
10539 go_assert(struct_type
!= NULL
);
10540 go_assert(struct_type
->field(this->field_index_
) != NULL
);
10543 // Get a tree for a field reference.
10546 Field_reference_expression::do_get_tree(Translate_context
* context
)
10548 tree struct_tree
= this->expr_
->get_tree(context
);
10549 if (struct_tree
== error_mark_node
10550 || TREE_TYPE(struct_tree
) == error_mark_node
)
10551 return error_mark_node
;
10552 go_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
10553 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
10554 if (field
== NULL_TREE
)
10556 // This can happen for a type which refers to itself indirectly
10557 // and then turns out to be erroneous.
10558 go_assert(saw_errors());
10559 return error_mark_node
;
10561 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
10563 field
= DECL_CHAIN(field
);
10564 go_assert(field
!= NULL_TREE
);
10566 if (TREE_TYPE(field
) == error_mark_node
)
10567 return error_mark_node
;
10568 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
10572 // Dump ast representation for a field reference expression.
10575 Field_reference_expression::do_dump_expression(
10576 Ast_dump_context
* ast_dump_context
) const
10578 this->expr_
->dump_expression(ast_dump_context
);
10579 ast_dump_context
->ostream() << "." << this->field_index_
;
10582 // Make a reference to a qualified identifier in an expression.
10584 Field_reference_expression
*
10585 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
10588 return new Field_reference_expression(expr
, field_index
, location
);
10591 // Class Interface_field_reference_expression.
10593 // Return a tree for the pointer to the function to call.
10596 Interface_field_reference_expression::get_function_tree(Translate_context
*,
10599 if (this->expr_
->type()->points_to() != NULL
)
10600 expr
= build_fold_indirect_ref(expr
);
10602 tree expr_type
= TREE_TYPE(expr
);
10603 go_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10605 tree field
= TYPE_FIELDS(expr_type
);
10606 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
10608 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10609 go_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
10611 table
= build_fold_indirect_ref(table
);
10612 go_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
10614 std::string name
= Gogo::unpack_hidden_name(this->name_
);
10615 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
10616 field
!= NULL_TREE
;
10617 field
= DECL_CHAIN(field
))
10619 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
10622 go_assert(field
!= NULL_TREE
);
10624 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
10627 // Return a tree for the first argument to pass to the interface
10631 Interface_field_reference_expression::get_underlying_object_tree(
10632 Translate_context
*,
10635 if (this->expr_
->type()->points_to() != NULL
)
10636 expr
= build_fold_indirect_ref(expr
);
10638 tree expr_type
= TREE_TYPE(expr
);
10639 go_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10641 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
10642 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
10644 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10650 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
10652 return Expression::traverse(&this->expr_
, traverse
);
10655 // Return the type of an interface field reference.
10658 Interface_field_reference_expression::do_type()
10660 Type
* expr_type
= this->expr_
->type();
10662 Type
* points_to
= expr_type
->points_to();
10663 if (points_to
!= NULL
)
10664 expr_type
= points_to
;
10666 Interface_type
* interface_type
= expr_type
->interface_type();
10667 if (interface_type
== NULL
)
10668 return Type::make_error_type();
10670 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
10671 if (method
== NULL
)
10672 return Type::make_error_type();
10674 return method
->type();
10677 // Determine types.
10680 Interface_field_reference_expression::do_determine_type(const Type_context
*)
10682 this->expr_
->determine_type_no_context();
10685 // Check the types for an interface field reference.
10688 Interface_field_reference_expression::do_check_types(Gogo
*)
10690 Type
* type
= this->expr_
->type();
10692 Type
* points_to
= type
->points_to();
10693 if (points_to
!= NULL
)
10696 Interface_type
* interface_type
= type
->interface_type();
10697 if (interface_type
== NULL
)
10699 if (!type
->is_error_type())
10700 this->report_error(_("expected interface or pointer to interface"));
10704 const Typed_identifier
* method
=
10705 interface_type
->find_method(this->name_
);
10706 if (method
== NULL
)
10708 error_at(this->location(), "method %qs not in interface",
10709 Gogo::message_name(this->name_
).c_str());
10710 this->set_is_error();
10715 // Get a tree for a reference to a field in an interface. There is no
10716 // standard tree type representation for this: it's a function
10717 // attached to its first argument, like a Bound_method_expression.
10718 // The only places it may currently be used are in a Call_expression
10719 // or a Go_statement, which will take it apart directly. So this has
10720 // nothing to do at present.
10723 Interface_field_reference_expression::do_get_tree(Translate_context
*)
10725 error_at(this->location(), "reference to method other than calling it");
10726 return error_mark_node
;
10729 // Dump ast representation for an interface field reference.
10732 Interface_field_reference_expression::do_dump_expression(
10733 Ast_dump_context
* ast_dump_context
) const
10735 this->expr_
->dump_expression(ast_dump_context
);
10736 ast_dump_context
->ostream() << "." << this->name_
;
10739 // Make a reference to a field in an interface.
10742 Expression::make_interface_field_reference(Expression
* expr
,
10743 const std::string
& field
,
10746 return new Interface_field_reference_expression(expr
, field
, location
);
10749 // A general selector. This is a Parser_expression for LEFT.NAME. It
10750 // is lowered after we know the type of the left hand side.
10752 class Selector_expression
: public Parser_expression
10755 Selector_expression(Expression
* left
, const std::string
& name
,
10757 : Parser_expression(EXPRESSION_SELECTOR
, location
),
10758 left_(left
), name_(name
)
10763 do_traverse(Traverse
* traverse
)
10764 { return Expression::traverse(&this->left_
, traverse
); }
10767 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
10772 return new Selector_expression(this->left_
->copy(), this->name_
,
10777 do_dump_expression(Ast_dump_context
* ast_dump_context
) const;
10781 lower_method_expression(Gogo
*);
10783 // The expression on the left hand side.
10785 // The name on the right hand side.
10789 // Lower a selector expression once we know the real type of the left
10793 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, Statement_inserter
*,
10796 Expression
* left
= this->left_
;
10797 if (left
->is_type_expression())
10798 return this->lower_method_expression(gogo
);
10799 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
10803 // Lower a method expression T.M or (*T).M. We turn this into a
10804 // function literal.
10807 Selector_expression::lower_method_expression(Gogo
* gogo
)
10809 Location location
= this->location();
10810 Type
* type
= this->left_
->type();
10811 const std::string
& name(this->name_
);
10814 if (type
->points_to() == NULL
)
10815 is_pointer
= false;
10819 type
= type
->points_to();
10821 Named_type
* nt
= type
->named_type();
10825 ("method expression requires named type or "
10826 "pointer to named type"));
10827 return Expression::make_error(location
);
10831 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
10832 const Typed_identifier
* imethod
= NULL
;
10833 if (method
== NULL
&& !is_pointer
)
10835 Interface_type
* it
= nt
->interface_type();
10837 imethod
= it
->find_method(name
);
10840 if (method
== NULL
&& imethod
== NULL
)
10843 error_at(location
, "type %<%s%s%> has no method %<%s%>",
10844 is_pointer
? "*" : "",
10845 nt
->message_name().c_str(),
10846 Gogo::message_name(name
).c_str());
10848 error_at(location
, "method %<%s%s%> is ambiguous in type %<%s%>",
10849 Gogo::message_name(name
).c_str(),
10850 is_pointer
? "*" : "",
10851 nt
->message_name().c_str());
10852 return Expression::make_error(location
);
10855 if (method
!= NULL
&& !is_pointer
&& !method
->is_value_method())
10857 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
10858 nt
->message_name().c_str(),
10859 Gogo::message_name(name
).c_str());
10860 return Expression::make_error(location
);
10863 // Build a new function type in which the receiver becomes the first
10865 Function_type
* method_type
;
10866 if (method
!= NULL
)
10868 method_type
= method
->type();
10869 go_assert(method_type
->is_method());
10873 method_type
= imethod
->type()->function_type();
10874 go_assert(method_type
!= NULL
&& !method_type
->is_method());
10877 const char* const receiver_name
= "$this";
10878 Typed_identifier_list
* parameters
= new Typed_identifier_list();
10879 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
10882 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
10883 if (method_parameters
!= NULL
)
10886 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10887 p
!= method_parameters
->end();
10890 if (!p
->name().empty())
10891 parameters
->push_back(*p
);
10895 snprintf(buf
, sizeof buf
, "$param%d", i
);
10896 parameters
->push_back(Typed_identifier(buf
, p
->type(),
10902 const Typed_identifier_list
* method_results
= method_type
->results();
10903 Typed_identifier_list
* results
;
10904 if (method_results
== NULL
)
10908 results
= new Typed_identifier_list();
10909 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
10910 p
!= method_results
->end();
10912 results
->push_back(*p
);
10915 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10917 if (method_type
->is_varargs())
10918 fntype
->set_is_varargs();
10920 // We generate methods which always takes a pointer to the receiver
10921 // as their first argument. If this is for a pointer type, we can
10922 // simply reuse the existing function. We use an internal hack to
10923 // get the right type.
10925 if (method
!= NULL
&& is_pointer
)
10927 Named_object
* mno
= (method
->needs_stub_method()
10928 ? method
->stub_object()
10929 : method
->named_object());
10930 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10931 f
= Expression::make_cast(fntype
, f
, location
);
10932 Type_conversion_expression
* tce
=
10933 static_cast<Type_conversion_expression
*>(f
);
10934 tce
->set_may_convert_function_types();
10938 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10941 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10942 go_assert(vno
!= NULL
);
10943 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10945 if (method
!= NULL
)
10946 bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10948 bm
= Expression::make_interface_field_reference(ve
, name
, location
);
10950 // Even though we found the method above, if it has an error type we
10951 // may see an error here.
10952 if (bm
->is_error_expression())
10954 gogo
->finish_function(location
);
10958 Expression_list
* args
;
10959 if (parameters
->size() <= 1)
10963 args
= new Expression_list();
10964 Typed_identifier_list::const_iterator p
= parameters
->begin();
10966 for (; p
!= parameters
->end(); ++p
)
10968 vno
= gogo
->lookup(p
->name(), NULL
);
10969 go_assert(vno
!= NULL
);
10970 args
->push_back(Expression::make_var_reference(vno
, location
));
10974 gogo
->start_block(location
);
10976 Call_expression
* call
= Expression::make_call(bm
, args
,
10977 method_type
->is_varargs(),
10980 size_t count
= call
->result_count();
10983 s
= Statement::make_statement(call
, true);
10986 Expression_list
* retvals
= new Expression_list();
10988 retvals
->push_back(call
);
10991 for (size_t i
= 0; i
< count
; ++i
)
10992 retvals
->push_back(Expression::make_call_result(call
, i
));
10994 s
= Statement::make_return_statement(retvals
, location
);
10996 gogo
->add_statement(s
);
10998 Block
* b
= gogo
->finish_block(location
);
11000 gogo
->add_block(b
, location
);
11002 // Lower the call in case there are multiple results.
11003 gogo
->lower_block(no
, b
);
11005 gogo
->finish_function(location
);
11007 return Expression::make_func_reference(no
, NULL
, location
);
11010 // Dump the ast for a selector expression.
11013 Selector_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
11016 ast_dump_context
->dump_expression(this->left_
);
11017 ast_dump_context
->ostream() << ".";
11018 ast_dump_context
->ostream() << this->name_
;
11021 // Make a selector expression.
11024 Expression::make_selector(Expression
* left
, const std::string
& name
,
11027 return new Selector_expression(left
, name
, location
);
11030 // Implement the builtin function new.
11032 class Allocation_expression
: public Expression
11035 Allocation_expression(Type
* type
, Location location
)
11036 : Expression(EXPRESSION_ALLOCATION
, location
),
11042 do_traverse(Traverse
* traverse
)
11043 { return Type::traverse(this->type_
, traverse
); }
11047 { return Type::make_pointer_type(this->type_
); }
11050 do_determine_type(const Type_context
*)
11055 { return new Allocation_expression(this->type_
, this->location()); }
11058 do_get_tree(Translate_context
*);
11061 do_dump_expression(Ast_dump_context
*) const;
11064 // The type we are allocating.
11068 // Return a tree for an allocation expression.
11071 Allocation_expression::do_get_tree(Translate_context
* context
)
11073 tree type_tree
= type_to_tree(this->type_
->get_backend(context
->gogo()));
11074 if (type_tree
== error_mark_node
)
11075 return error_mark_node
;
11076 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
11077 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
11079 if (space
== error_mark_node
)
11080 return error_mark_node
;
11081 return fold_convert(build_pointer_type(type_tree
), space
);
11084 // Dump ast representation for an allocation expression.
11087 Allocation_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
11090 ast_dump_context
->ostream() << "new(";
11091 ast_dump_context
->dump_type(this->type_
);
11092 ast_dump_context
->ostream() << ")";
11095 // Make an allocation expression.
11098 Expression::make_allocation(Type
* type
, Location location
)
11100 return new Allocation_expression(type
, location
);
11103 // Construct a struct.
11105 class Struct_construction_expression
: public Expression
11108 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
11110 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
11111 type_(type
), vals_(vals
), traverse_order_(NULL
)
11114 // Set the traversal order, used to ensure that we implement the
11115 // order of evaluation rules. Takes ownership of the argument.
11117 set_traverse_order(std::vector
<int>* traverse_order
)
11118 { this->traverse_order_
= traverse_order
; }
11120 // Return whether this is a constant initializer.
11122 is_constant_struct() const;
11126 do_traverse(Traverse
* traverse
);
11130 { return this->type_
; }
11133 do_determine_type(const Type_context
*);
11136 do_check_types(Gogo
*);
11141 Struct_construction_expression
* ret
=
11142 new Struct_construction_expression(this->type_
, this->vals_
->copy(),
11144 if (this->traverse_order_
!= NULL
)
11145 ret
->set_traverse_order(this->traverse_order_
);
11150 do_get_tree(Translate_context
*);
11153 do_export(Export
*) const;
11156 do_dump_expression(Ast_dump_context
*) const;
11159 // The type of the struct to construct.
11161 // The list of values, in order of the fields in the struct. A NULL
11162 // entry means that the field should be zero-initialized.
11163 Expression_list
* vals_
;
11164 // If not NULL, the order in which to traverse vals_. This is used
11165 // so that we implement the order of evaluation rules correctly.
11166 std::vector
<int>* traverse_order_
;
11172 Struct_construction_expression::do_traverse(Traverse
* traverse
)
11174 if (this->vals_
!= NULL
)
11176 if (this->traverse_order_
== NULL
)
11178 if (this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11179 return TRAVERSE_EXIT
;
11183 for (std::vector
<int>::const_iterator p
=
11184 this->traverse_order_
->begin();
11185 p
!= this->traverse_order_
->end();
11188 if (Expression::traverse(&this->vals_
->at(*p
), traverse
)
11190 return TRAVERSE_EXIT
;
11194 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11195 return TRAVERSE_EXIT
;
11196 return TRAVERSE_CONTINUE
;
11199 // Return whether this is a constant initializer.
11202 Struct_construction_expression::is_constant_struct() const
11204 if (this->vals_
== NULL
)
11206 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11207 pv
!= this->vals_
->end();
11211 && !(*pv
)->is_constant()
11212 && (!(*pv
)->is_composite_literal()
11213 || (*pv
)->is_nonconstant_composite_literal()))
11217 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11218 for (Struct_field_list::const_iterator pf
= fields
->begin();
11219 pf
!= fields
->end();
11222 // There are no constant constructors for interfaces.
11223 if (pf
->type()->interface_type() != NULL
)
11230 // Final type determination.
11233 Struct_construction_expression::do_determine_type(const Type_context
*)
11235 if (this->vals_
== NULL
)
11237 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11238 Expression_list::const_iterator pv
= this->vals_
->begin();
11239 for (Struct_field_list::const_iterator pf
= fields
->begin();
11240 pf
!= fields
->end();
11243 if (pv
== this->vals_
->end())
11247 Type_context
subcontext(pf
->type(), false);
11248 (*pv
)->determine_type(&subcontext
);
11251 // Extra values are an error we will report elsewhere; we still want
11252 // to determine the type to avoid knockon errors.
11253 for (; pv
!= this->vals_
->end(); ++pv
)
11254 (*pv
)->determine_type_no_context();
11260 Struct_construction_expression::do_check_types(Gogo
*)
11262 if (this->vals_
== NULL
)
11265 Struct_type
* st
= this->type_
->struct_type();
11266 if (this->vals_
->size() > st
->field_count())
11268 this->report_error(_("too many expressions for struct"));
11272 const Struct_field_list
* fields
= st
->fields();
11273 Expression_list::const_iterator pv
= this->vals_
->begin();
11275 for (Struct_field_list::const_iterator pf
= fields
->begin();
11276 pf
!= fields
->end();
11279 if (pv
== this->vals_
->end())
11281 this->report_error(_("too few expressions for struct"));
11288 std::string reason
;
11289 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
11291 if (reason
.empty())
11292 error_at((*pv
)->location(),
11293 "incompatible type for field %d in struct construction",
11296 error_at((*pv
)->location(),
11297 ("incompatible type for field %d in "
11298 "struct construction (%s)"),
11299 i
+ 1, reason
.c_str());
11300 this->set_is_error();
11303 go_assert(pv
== this->vals_
->end());
11306 // Return a tree for constructing a struct.
11309 Struct_construction_expression::do_get_tree(Translate_context
* context
)
11311 Gogo
* gogo
= context
->gogo();
11313 if (this->vals_
== NULL
)
11315 Btype
* btype
= this->type_
->get_backend(gogo
);
11316 return expr_to_tree(gogo
->backend()->zero_expression(btype
));
11319 tree type_tree
= type_to_tree(this->type_
->get_backend(gogo
));
11320 if (type_tree
== error_mark_node
)
11321 return error_mark_node
;
11322 go_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11324 bool is_constant
= true;
11325 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11326 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
11328 Struct_field_list::const_iterator pf
= fields
->begin();
11329 Expression_list::const_iterator pv
= this->vals_
->begin();
11330 for (tree field
= TYPE_FIELDS(type_tree
);
11331 field
!= NULL_TREE
;
11332 field
= DECL_CHAIN(field
), ++pf
)
11334 go_assert(pf
!= fields
->end());
11336 Btype
* fbtype
= pf
->type()->get_backend(gogo
);
11339 if (pv
== this->vals_
->end())
11340 val
= expr_to_tree(gogo
->backend()->zero_expression(fbtype
));
11341 else if (*pv
== NULL
)
11343 val
= expr_to_tree(gogo
->backend()->zero_expression(fbtype
));
11348 val
= Expression::convert_for_assignment(context
, pf
->type(),
11350 (*pv
)->get_tree(context
),
11355 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
11356 return error_mark_node
;
11358 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
11359 elt
->index
= field
;
11361 if (!TREE_CONSTANT(val
))
11362 is_constant
= false;
11364 go_assert(pf
== fields
->end());
11366 tree ret
= build_constructor(type_tree
, elts
);
11368 TREE_CONSTANT(ret
) = 1;
11372 // Export a struct construction.
11375 Struct_construction_expression::do_export(Export
* exp
) const
11377 exp
->write_c_string("convert(");
11378 exp
->write_type(this->type_
);
11379 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11380 pv
!= this->vals_
->end();
11383 exp
->write_c_string(", ");
11385 (*pv
)->export_expression(exp
);
11387 exp
->write_c_string(")");
11390 // Dump ast representation of a struct construction expression.
11393 Struct_construction_expression::do_dump_expression(
11394 Ast_dump_context
* ast_dump_context
) const
11396 ast_dump_context
->dump_type(this->type_
);
11397 ast_dump_context
->ostream() << "{";
11398 ast_dump_context
->dump_expression_list(this->vals_
);
11399 ast_dump_context
->ostream() << "}";
11402 // Make a struct composite literal. This used by the thunk code.
11405 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
11408 go_assert(type
->struct_type() != NULL
);
11409 return new Struct_construction_expression(type
, vals
, location
);
11412 // Construct an array. This class is not used directly; instead we
11413 // use the child classes, Fixed_array_construction_expression and
11414 // Open_array_construction_expression.
11416 class Array_construction_expression
: public Expression
11419 Array_construction_expression(Expression_classification classification
,
11421 const std::vector
<unsigned long>* indexes
,
11422 Expression_list
* vals
, Location location
)
11423 : Expression(classification
, location
),
11424 type_(type
), indexes_(indexes
), vals_(vals
)
11425 { go_assert(indexes
== NULL
|| indexes
->size() == vals
->size()); }
11428 // Return whether this is a constant initializer.
11430 is_constant_array() const;
11432 // Return the number of elements.
11434 element_count() const
11435 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
11439 do_traverse(Traverse
* traverse
);
11443 { return this->type_
; }
11446 do_determine_type(const Type_context
*);
11449 do_check_types(Gogo
*);
11452 do_export(Export
*) const;
11455 const std::vector
<unsigned long>*
11457 { return this->indexes_
; }
11459 // The list of values.
11462 { return this->vals_
; }
11464 // Get a constructor tree for the array values.
11466 get_constructor_tree(Translate_context
* context
, tree type_tree
);
11469 do_dump_expression(Ast_dump_context
*) const;
11472 // The type of the array to construct.
11474 // The list of indexes into the array, one for each value. This may
11475 // be NULL, in which case the indexes start at zero and increment.
11476 const std::vector
<unsigned long>* indexes_
;
11477 // The list of values. This may be NULL if there are no values.
11478 Expression_list
* vals_
;
11484 Array_construction_expression::do_traverse(Traverse
* traverse
)
11486 if (this->vals_
!= NULL
11487 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11488 return TRAVERSE_EXIT
;
11489 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11490 return TRAVERSE_EXIT
;
11491 return TRAVERSE_CONTINUE
;
11494 // Return whether this is a constant initializer.
11497 Array_construction_expression::is_constant_array() const
11499 if (this->vals_
== NULL
)
11502 // There are no constant constructors for interfaces.
11503 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
11506 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11507 pv
!= this->vals_
->end();
11511 && !(*pv
)->is_constant()
11512 && (!(*pv
)->is_composite_literal()
11513 || (*pv
)->is_nonconstant_composite_literal()))
11519 // Final type determination.
11522 Array_construction_expression::do_determine_type(const Type_context
*)
11524 if (this->vals_
== NULL
)
11526 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
11527 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11528 pv
!= this->vals_
->end();
11532 (*pv
)->determine_type(&subcontext
);
11539 Array_construction_expression::do_check_types(Gogo
*)
11541 if (this->vals_
== NULL
)
11544 Array_type
* at
= this->type_
->array_type();
11546 Type
* element_type
= at
->element_type();
11547 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11548 pv
!= this->vals_
->end();
11552 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
11554 error_at((*pv
)->location(),
11555 "incompatible type for element %d in composite literal",
11557 this->set_is_error();
11562 // Get a constructor tree for the array values.
11565 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
11568 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11569 (this->vals_
== NULL
11571 : this->vals_
->size()));
11572 Type
* element_type
= this->type_
->array_type()->element_type();
11573 bool is_constant
= true;
11574 if (this->vals_
!= NULL
)
11577 std::vector
<unsigned long>::const_iterator pi
;
11578 if (this->indexes_
!= NULL
)
11579 pi
= this->indexes_
->begin();
11580 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11581 pv
!= this->vals_
->end();
11584 if (this->indexes_
!= NULL
)
11585 go_assert(pi
!= this->indexes_
->end());
11586 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11588 if (this->indexes_
== NULL
)
11589 elt
->index
= size_int(i
);
11591 elt
->index
= size_int(*pi
);
11595 Gogo
* gogo
= context
->gogo();
11596 Btype
* ebtype
= element_type
->get_backend(gogo
);
11597 Bexpression
*zv
= gogo
->backend()->zero_expression(ebtype
);
11598 elt
->value
= expr_to_tree(zv
);
11602 tree value_tree
= (*pv
)->get_tree(context
);
11603 elt
->value
= Expression::convert_for_assignment(context
,
11609 if (elt
->value
== error_mark_node
)
11610 return error_mark_node
;
11611 if (!TREE_CONSTANT(elt
->value
))
11612 is_constant
= false;
11613 if (this->indexes_
!= NULL
)
11616 if (this->indexes_
!= NULL
)
11617 go_assert(pi
== this->indexes_
->end());
11620 tree ret
= build_constructor(type_tree
, values
);
11622 TREE_CONSTANT(ret
) = 1;
11626 // Export an array construction.
11629 Array_construction_expression::do_export(Export
* exp
) const
11631 exp
->write_c_string("convert(");
11632 exp
->write_type(this->type_
);
11633 if (this->vals_
!= NULL
)
11635 std::vector
<unsigned long>::const_iterator pi
;
11636 if (this->indexes_
!= NULL
)
11637 pi
= this->indexes_
->begin();
11638 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11639 pv
!= this->vals_
->end();
11642 exp
->write_c_string(", ");
11644 if (this->indexes_
!= NULL
)
11647 snprintf(buf
, sizeof buf
, "%lu", *pi
);
11648 exp
->write_c_string(buf
);
11649 exp
->write_c_string(":");
11653 (*pv
)->export_expression(exp
);
11655 if (this->indexes_
!= NULL
)
11659 exp
->write_c_string(")");
11662 // Dump ast representation of an array construction expressin.
11665 Array_construction_expression::do_dump_expression(
11666 Ast_dump_context
* ast_dump_context
) const
11668 Expression
* length
= this->type_
->array_type()->length();
11670 ast_dump_context
->ostream() << "[" ;
11671 if (length
!= NULL
)
11673 ast_dump_context
->dump_expression(length
);
11675 ast_dump_context
->ostream() << "]" ;
11676 ast_dump_context
->dump_type(this->type_
);
11677 ast_dump_context
->ostream() << "{" ;
11678 if (this->indexes_
== NULL
)
11679 ast_dump_context
->dump_expression_list(this->vals_
);
11682 Expression_list::const_iterator pv
= this->vals_
->begin();
11683 for (std::vector
<unsigned long>::const_iterator pi
=
11684 this->indexes_
->begin();
11685 pi
!= this->indexes_
->end();
11688 if (pi
!= this->indexes_
->begin())
11689 ast_dump_context
->ostream() << ", ";
11690 ast_dump_context
->ostream() << *pi
<< ':';
11691 ast_dump_context
->dump_expression(*pv
);
11694 ast_dump_context
->ostream() << "}" ;
11698 // Construct a fixed array.
11700 class Fixed_array_construction_expression
:
11701 public Array_construction_expression
11704 Fixed_array_construction_expression(Type
* type
,
11705 const std::vector
<unsigned long>* indexes
,
11706 Expression_list
* vals
, Location location
)
11707 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
11708 type
, indexes
, vals
, location
)
11709 { go_assert(type
->array_type() != NULL
&& !type
->is_slice_type()); }
11715 return new Fixed_array_construction_expression(this->type(),
11717 (this->vals() == NULL
11719 : this->vals()->copy()),
11724 do_get_tree(Translate_context
*);
11727 // Return a tree for constructing a fixed array.
11730 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
11732 Type
* type
= this->type();
11733 Btype
* btype
= type
->get_backend(context
->gogo());
11734 return this->get_constructor_tree(context
, type_to_tree(btype
));
11737 // Construct an open array.
11739 class Open_array_construction_expression
: public Array_construction_expression
11742 Open_array_construction_expression(Type
* type
,
11743 const std::vector
<unsigned long>* indexes
,
11744 Expression_list
* vals
, Location location
)
11745 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
11746 type
, indexes
, vals
, location
)
11747 { go_assert(type
->is_slice_type()); }
11750 // Note that taking the address of an open array literal is invalid.
11755 return new Open_array_construction_expression(this->type(),
11757 (this->vals() == NULL
11759 : this->vals()->copy()),
11764 do_get_tree(Translate_context
*);
11767 // Return a tree for constructing an open array.
11770 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
11772 Array_type
* array_type
= this->type()->array_type();
11773 if (array_type
== NULL
)
11775 go_assert(this->type()->is_error());
11776 return error_mark_node
;
11779 Type
* element_type
= array_type
->element_type();
11780 Btype
* belement_type
= element_type
->get_backend(context
->gogo());
11781 tree element_type_tree
= type_to_tree(belement_type
);
11782 if (element_type_tree
== error_mark_node
)
11783 return error_mark_node
;
11787 if (this->vals() == NULL
|| this->vals()->empty())
11789 // We need to create a unique value.
11790 tree max
= size_int(0);
11791 tree constructor_type
= build_array_type(element_type_tree
,
11792 build_index_type(max
));
11793 if (constructor_type
== error_mark_node
)
11794 return error_mark_node
;
11795 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
11796 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
11797 elt
->index
= size_int(0);
11798 Gogo
* gogo
= context
->gogo();
11799 Btype
* btype
= element_type
->get_backend(gogo
);
11800 elt
->value
= expr_to_tree(gogo
->backend()->zero_expression(btype
));
11801 values
= build_constructor(constructor_type
, vec
);
11802 if (TREE_CONSTANT(elt
->value
))
11803 TREE_CONSTANT(values
) = 1;
11804 length_tree
= size_int(0);
11808 unsigned long max_index
;
11809 if (this->indexes() == NULL
)
11810 max_index
= this->vals()->size() - 1;
11812 max_index
= this->indexes()->back();
11813 tree max_tree
= size_int(max_index
);
11814 tree constructor_type
= build_array_type(element_type_tree
,
11815 build_index_type(max_tree
));
11816 if (constructor_type
== error_mark_node
)
11817 return error_mark_node
;
11818 values
= this->get_constructor_tree(context
, constructor_type
);
11819 length_tree
= size_int(max_index
+ 1);
11822 if (values
== error_mark_node
)
11823 return error_mark_node
;
11825 bool is_constant_initializer
= TREE_CONSTANT(values
);
11827 // We have to copy the initial values into heap memory if we are in
11828 // a function or if the values are not constants. We also have to
11829 // copy them if they may contain pointers in a non-constant context,
11830 // as otherwise the garbage collector won't see them.
11831 bool copy_to_heap
= (context
->function() != NULL
11832 || !is_constant_initializer
11833 || (element_type
->has_pointer()
11834 && !context
->is_const()));
11836 if (is_constant_initializer
)
11838 tree tmp
= build_decl(this->location().gcc_location(), VAR_DECL
,
11839 create_tmp_var_name("C"), TREE_TYPE(values
));
11840 DECL_EXTERNAL(tmp
) = 0;
11841 TREE_PUBLIC(tmp
) = 0;
11842 TREE_STATIC(tmp
) = 1;
11843 DECL_ARTIFICIAL(tmp
) = 1;
11846 // If we are not copying the value to the heap, we will only
11847 // initialize the value once, so we can use this directly
11848 // rather than copying it. In that case we can't make it
11849 // read-only, because the program is permitted to change it.
11850 TREE_READONLY(tmp
) = 1;
11851 TREE_CONSTANT(tmp
) = 1;
11853 DECL_INITIAL(tmp
) = values
;
11854 rest_of_decl_compilation(tmp
, 1, 0);
11862 // the initializer will only run once.
11863 space
= build_fold_addr_expr(values
);
11868 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
11869 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
11871 space
= save_expr(space
);
11873 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
11874 tree ref
= build_fold_indirect_ref_loc(this->location().gcc_location(),
11876 TREE_THIS_NOTRAP(ref
) = 1;
11877 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
11880 // Build a constructor for the open array.
11882 tree type_tree
= type_to_tree(this->type()->get_backend(context
->gogo()));
11883 if (type_tree
== error_mark_node
)
11884 return error_mark_node
;
11885 go_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11887 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
11889 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11890 tree field
= TYPE_FIELDS(type_tree
);
11891 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
11892 elt
->index
= field
;
11893 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
11895 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11896 field
= DECL_CHAIN(field
);
11897 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
11898 elt
->index
= field
;
11899 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11901 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11902 field
= DECL_CHAIN(field
);
11903 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
11904 elt
->index
= field
;
11905 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11907 tree constructor
= build_constructor(type_tree
, init
);
11908 if (constructor
== error_mark_node
)
11909 return error_mark_node
;
11911 TREE_CONSTANT(constructor
) = 1;
11913 if (set
== NULL_TREE
)
11914 return constructor
;
11916 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
11919 // Make a slice composite literal. This is used by the type
11920 // descriptor code.
11923 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
11926 go_assert(type
->is_slice_type());
11927 return new Open_array_construction_expression(type
, NULL
, vals
, location
);
11930 // Construct a map.
11932 class Map_construction_expression
: public Expression
11935 Map_construction_expression(Type
* type
, Expression_list
* vals
,
11937 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
11938 type_(type
), vals_(vals
)
11939 { go_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
11943 do_traverse(Traverse
* traverse
);
11947 { return this->type_
; }
11950 do_determine_type(const Type_context
*);
11953 do_check_types(Gogo
*);
11958 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
11963 do_get_tree(Translate_context
*);
11966 do_export(Export
*) const;
11969 do_dump_expression(Ast_dump_context
*) const;
11972 // The type of the map to construct.
11974 // The list of values.
11975 Expression_list
* vals_
;
11981 Map_construction_expression::do_traverse(Traverse
* traverse
)
11983 if (this->vals_
!= NULL
11984 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11985 return TRAVERSE_EXIT
;
11986 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11987 return TRAVERSE_EXIT
;
11988 return TRAVERSE_CONTINUE
;
11991 // Final type determination.
11994 Map_construction_expression::do_determine_type(const Type_context
*)
11996 if (this->vals_
== NULL
)
11999 Map_type
* mt
= this->type_
->map_type();
12000 Type_context
key_context(mt
->key_type(), false);
12001 Type_context
val_context(mt
->val_type(), false);
12002 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12003 pv
!= this->vals_
->end();
12006 (*pv
)->determine_type(&key_context
);
12008 (*pv
)->determine_type(&val_context
);
12015 Map_construction_expression::do_check_types(Gogo
*)
12017 if (this->vals_
== NULL
)
12020 Map_type
* mt
= this->type_
->map_type();
12022 Type
* key_type
= mt
->key_type();
12023 Type
* val_type
= mt
->val_type();
12024 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12025 pv
!= this->vals_
->end();
12028 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
12030 error_at((*pv
)->location(),
12031 "incompatible type for element %d key in map construction",
12033 this->set_is_error();
12036 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
12038 error_at((*pv
)->location(),
12039 ("incompatible type for element %d value "
12040 "in map construction"),
12042 this->set_is_error();
12047 // Return a tree for constructing a map.
12050 Map_construction_expression::do_get_tree(Translate_context
* context
)
12052 Gogo
* gogo
= context
->gogo();
12053 Location loc
= this->location();
12055 Map_type
* mt
= this->type_
->map_type();
12057 // Build a struct to hold the key and value.
12058 tree struct_type
= make_node(RECORD_TYPE
);
12060 Type
* key_type
= mt
->key_type();
12061 tree id
= get_identifier("__key");
12062 tree key_type_tree
= type_to_tree(key_type
->get_backend(gogo
));
12063 if (key_type_tree
== error_mark_node
)
12064 return error_mark_node
;
12065 tree key_field
= build_decl(loc
.gcc_location(), FIELD_DECL
, id
,
12067 DECL_CONTEXT(key_field
) = struct_type
;
12068 TYPE_FIELDS(struct_type
) = key_field
;
12070 Type
* val_type
= mt
->val_type();
12071 id
= get_identifier("__val");
12072 tree val_type_tree
= type_to_tree(val_type
->get_backend(gogo
));
12073 if (val_type_tree
== error_mark_node
)
12074 return error_mark_node
;
12075 tree val_field
= build_decl(loc
.gcc_location(), FIELD_DECL
, id
,
12077 DECL_CONTEXT(val_field
) = struct_type
;
12078 DECL_CHAIN(key_field
) = val_field
;
12080 layout_type(struct_type
);
12082 bool is_constant
= true;
12087 if (this->vals_
== NULL
|| this->vals_
->empty())
12089 valaddr
= null_pointer_node
;
12090 make_tmp
= NULL_TREE
;
12094 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
12095 this->vals_
->size() / 2);
12097 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12098 pv
!= this->vals_
->end();
12101 bool one_is_constant
= true;
12103 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
12105 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
12106 elt
->index
= key_field
;
12107 tree val_tree
= (*pv
)->get_tree(context
);
12108 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
12111 if (elt
->value
== error_mark_node
)
12112 return error_mark_node
;
12113 if (!TREE_CONSTANT(elt
->value
))
12114 one_is_constant
= false;
12118 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
12119 elt
->index
= val_field
;
12120 val_tree
= (*pv
)->get_tree(context
);
12121 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
12124 if (elt
->value
== error_mark_node
)
12125 return error_mark_node
;
12126 if (!TREE_CONSTANT(elt
->value
))
12127 one_is_constant
= false;
12129 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
12130 elt
->index
= size_int(i
);
12131 elt
->value
= build_constructor(struct_type
, one
);
12132 if (one_is_constant
)
12133 TREE_CONSTANT(elt
->value
) = 1;
12135 is_constant
= false;
12138 tree index_type
= build_index_type(size_int(i
- 1));
12139 tree array_type
= build_array_type(struct_type
, index_type
);
12140 tree init
= build_constructor(array_type
, values
);
12142 TREE_CONSTANT(init
) = 1;
12144 if (current_function_decl
!= NULL
)
12146 tmp
= create_tmp_var(array_type
, get_name(array_type
));
12147 DECL_INITIAL(tmp
) = init
;
12148 make_tmp
= fold_build1_loc(loc
.gcc_location(), DECL_EXPR
,
12149 void_type_node
, tmp
);
12150 TREE_ADDRESSABLE(tmp
) = 1;
12154 tmp
= build_decl(loc
.gcc_location(), VAR_DECL
,
12155 create_tmp_var_name("M"), array_type
);
12156 DECL_EXTERNAL(tmp
) = 0;
12157 TREE_PUBLIC(tmp
) = 0;
12158 TREE_STATIC(tmp
) = 1;
12159 DECL_ARTIFICIAL(tmp
) = 1;
12160 if (!TREE_CONSTANT(init
))
12161 make_tmp
= fold_build2_loc(loc
.gcc_location(), INIT_EXPR
,
12162 void_type_node
, tmp
, init
);
12165 TREE_READONLY(tmp
) = 1;
12166 TREE_CONSTANT(tmp
) = 1;
12167 DECL_INITIAL(tmp
) = init
;
12168 make_tmp
= NULL_TREE
;
12170 rest_of_decl_compilation(tmp
, 1, 0);
12173 valaddr
= build_fold_addr_expr(tmp
);
12176 tree descriptor
= mt
->map_descriptor_pointer(gogo
, loc
);
12178 tree type_tree
= type_to_tree(this->type_
->get_backend(gogo
));
12179 if (type_tree
== error_mark_node
)
12180 return error_mark_node
;
12182 static tree construct_map_fndecl
;
12183 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
12185 "__go_construct_map",
12188 TREE_TYPE(descriptor
),
12193 TYPE_SIZE_UNIT(struct_type
),
12195 byte_position(val_field
),
12197 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
12198 const_ptr_type_node
,
12199 fold_convert(const_ptr_type_node
, valaddr
));
12200 if (call
== error_mark_node
)
12201 return error_mark_node
;
12204 if (make_tmp
== NULL
)
12207 ret
= fold_build2_loc(loc
.gcc_location(), COMPOUND_EXPR
, type_tree
,
12212 // Export an array construction.
12215 Map_construction_expression::do_export(Export
* exp
) const
12217 exp
->write_c_string("convert(");
12218 exp
->write_type(this->type_
);
12219 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12220 pv
!= this->vals_
->end();
12223 exp
->write_c_string(", ");
12224 (*pv
)->export_expression(exp
);
12226 exp
->write_c_string(")");
12229 // Dump ast representation for a map construction expression.
12232 Map_construction_expression::do_dump_expression(
12233 Ast_dump_context
* ast_dump_context
) const
12235 ast_dump_context
->ostream() << "{" ;
12236 ast_dump_context
->dump_expression_list(this->vals_
, true);
12237 ast_dump_context
->ostream() << "}";
12240 // A general composite literal. This is lowered to a type specific
12243 class Composite_literal_expression
: public Parser_expression
12246 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
12247 Expression_list
* vals
, Location location
)
12248 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
12249 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
12254 do_traverse(Traverse
* traverse
);
12257 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
12262 return new Composite_literal_expression(this->type_
, this->depth_
,
12264 (this->vals_
== NULL
12266 : this->vals_
->copy()),
12271 do_dump_expression(Ast_dump_context
*) const;
12275 lower_struct(Gogo
*, Type
*);
12278 lower_array(Type
*);
12281 make_array(Type
*, const std::vector
<unsigned long>*, Expression_list
*);
12284 lower_map(Gogo
*, Named_object
*, Statement_inserter
*, Type
*);
12286 // The type of the composite literal.
12288 // The depth within a list of composite literals within a composite
12289 // literal, when the type is omitted.
12291 // The values to put in the composite literal.
12292 Expression_list
* vals_
;
12293 // If this is true, then VALS_ is a list of pairs: a key and a
12294 // value. In an array initializer, a missing key will be NULL.
12301 Composite_literal_expression::do_traverse(Traverse
* traverse
)
12303 if (this->vals_
!= NULL
12304 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
12305 return TRAVERSE_EXIT
;
12306 return Type::traverse(this->type_
, traverse
);
12309 // Lower a generic composite literal into a specific version based on
12313 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
12314 Statement_inserter
* inserter
, int)
12316 Type
* type
= this->type_
;
12318 for (int depth
= this->depth_
; depth
> 0; --depth
)
12320 if (type
->array_type() != NULL
)
12321 type
= type
->array_type()->element_type();
12322 else if (type
->map_type() != NULL
)
12323 type
= type
->map_type()->val_type();
12326 if (!type
->is_error())
12327 error_at(this->location(),
12328 ("may only omit types within composite literals "
12329 "of slice, array, or map type"));
12330 return Expression::make_error(this->location());
12334 Type
*pt
= type
->points_to();
12335 bool is_pointer
= false;
12343 if (type
->is_error())
12344 return Expression::make_error(this->location());
12345 else if (type
->struct_type() != NULL
)
12346 ret
= this->lower_struct(gogo
, type
);
12347 else if (type
->array_type() != NULL
)
12348 ret
= this->lower_array(type
);
12349 else if (type
->map_type() != NULL
)
12350 ret
= this->lower_map(gogo
, function
, inserter
, type
);
12353 error_at(this->location(),
12354 ("expected struct, slice, array, or map type "
12355 "for composite literal"));
12356 return Expression::make_error(this->location());
12360 ret
= Expression::make_heap_composite(ret
, this->location());
12365 // Lower a struct composite literal.
12368 Composite_literal_expression::lower_struct(Gogo
* gogo
, Type
* type
)
12370 Location location
= this->location();
12371 Struct_type
* st
= type
->struct_type();
12372 if (this->vals_
== NULL
|| !this->has_keys_
)
12374 if (this->vals_
!= NULL
12375 && !this->vals_
->empty()
12376 && type
->named_type() != NULL
12377 && type
->named_type()->named_object()->package() != NULL
)
12379 for (Struct_field_list::const_iterator pf
= st
->fields()->begin();
12380 pf
!= st
->fields()->end();
12383 if (Gogo::is_hidden_name(pf
->field_name()))
12384 error_at(this->location(),
12385 "assignment of unexported field %qs in %qs literal",
12386 Gogo::message_name(pf
->field_name()).c_str(),
12387 type
->named_type()->message_name().c_str());
12391 return new Struct_construction_expression(type
, this->vals_
, location
);
12394 size_t field_count
= st
->field_count();
12395 std::vector
<Expression
*> vals(field_count
);
12396 std::vector
<int>* traverse_order
= new(std::vector
<int>);
12397 Expression_list::const_iterator p
= this->vals_
->begin();
12398 while (p
!= this->vals_
->end())
12400 Expression
* name_expr
= *p
;
12403 go_assert(p
!= this->vals_
->end());
12404 Expression
* val
= *p
;
12408 if (name_expr
== NULL
)
12410 error_at(val
->location(), "mixture of field and value initializers");
12411 return Expression::make_error(location
);
12414 bool bad_key
= false;
12416 const Named_object
* no
= NULL
;
12417 switch (name_expr
->classification())
12419 case EXPRESSION_UNKNOWN_REFERENCE
:
12420 name
= name_expr
->unknown_expression()->name();
12423 case EXPRESSION_CONST_REFERENCE
:
12424 no
= static_cast<Const_expression
*>(name_expr
)->named_object();
12427 case EXPRESSION_TYPE
:
12429 Type
* t
= name_expr
->type();
12430 Named_type
* nt
= t
->named_type();
12434 no
= nt
->named_object();
12438 case EXPRESSION_VAR_REFERENCE
:
12439 no
= name_expr
->var_expression()->named_object();
12442 case EXPRESSION_FUNC_REFERENCE
:
12443 no
= name_expr
->func_expression()->named_object();
12446 case EXPRESSION_UNARY
:
12447 // If there is a local variable around with the same name as
12448 // the field, and this occurs in the closure, then the
12449 // parser may turn the field reference into an indirection
12450 // through the closure. FIXME: This is a mess.
12453 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
12454 if (ue
->op() == OPERATOR_MULT
)
12456 Field_reference_expression
* fre
=
12457 ue
->operand()->field_reference_expression();
12461 fre
->expr()->type()->deref()->struct_type();
12464 const Struct_field
* sf
= st
->field(fre
->field_index());
12465 name
= sf
->field_name();
12467 // See below. FIXME.
12468 if (!Gogo::is_hidden_name(name
)
12472 if (gogo
->lookup_global(name
.c_str()) != NULL
)
12473 name
= gogo
->pack_hidden_name(name
, false);
12477 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
12478 size_t buflen
= strlen(buf
);
12479 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
12482 name
= name
.substr(0, name
.length() - buflen
);
12497 error_at(name_expr
->location(), "expected struct field name");
12498 return Expression::make_error(location
);
12505 // A predefined name won't be packed. If it starts with a
12506 // lower case letter we need to check for that case, because
12507 // the field name will be packed. FIXME.
12508 if (!Gogo::is_hidden_name(name
)
12512 Named_object
* gno
= gogo
->lookup_global(name
.c_str());
12514 name
= gogo
->pack_hidden_name(name
, false);
12518 unsigned int index
;
12519 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
12522 error_at(name_expr
->location(), "unknown field %qs in %qs",
12523 Gogo::message_name(name
).c_str(),
12524 (type
->named_type() != NULL
12525 ? type
->named_type()->message_name().c_str()
12526 : "unnamed struct"));
12527 return Expression::make_error(location
);
12529 if (vals
[index
] != NULL
)
12531 error_at(name_expr
->location(),
12532 "duplicate value for field %qs in %qs",
12533 Gogo::message_name(name
).c_str(),
12534 (type
->named_type() != NULL
12535 ? type
->named_type()->message_name().c_str()
12536 : "unnamed struct"));
12537 return Expression::make_error(location
);
12540 if (type
->named_type() != NULL
12541 && type
->named_type()->named_object()->package() != NULL
12542 && Gogo::is_hidden_name(sf
->field_name()))
12543 error_at(name_expr
->location(),
12544 "assignment of unexported field %qs in %qs literal",
12545 Gogo::message_name(sf
->field_name()).c_str(),
12546 type
->named_type()->message_name().c_str());
12549 traverse_order
->push_back(index
);
12552 Expression_list
* list
= new Expression_list
;
12553 list
->reserve(field_count
);
12554 for (size_t i
= 0; i
< field_count
; ++i
)
12555 list
->push_back(vals
[i
]);
12557 Struct_construction_expression
* ret
=
12558 new Struct_construction_expression(type
, list
, location
);
12559 ret
->set_traverse_order(traverse_order
);
12563 // Used to sort an index/value array.
12565 class Index_value_compare
12569 operator()(const std::pair
<unsigned long, Expression
*>& a
,
12570 const std::pair
<unsigned long, Expression
*>& b
)
12571 { return a
.first
< b
.first
; }
12574 // Lower an array composite literal.
12577 Composite_literal_expression::lower_array(Type
* type
)
12579 Location location
= this->location();
12580 if (this->vals_
== NULL
|| !this->has_keys_
)
12581 return this->make_array(type
, NULL
, this->vals_
);
12583 std::vector
<unsigned long>* indexes
= new std::vector
<unsigned long>;
12584 indexes
->reserve(this->vals_
->size());
12585 bool indexes_out_of_order
= false;
12586 Expression_list
* vals
= new Expression_list();
12587 vals
->reserve(this->vals_
->size());
12588 unsigned long index
= 0;
12589 Expression_list::const_iterator p
= this->vals_
->begin();
12590 while (p
!= this->vals_
->end())
12592 Expression
* index_expr
= *p
;
12595 go_assert(p
!= this->vals_
->end());
12596 Expression
* val
= *p
;
12600 if (index_expr
== NULL
)
12602 if (!indexes
->empty())
12603 indexes
->push_back(index
);
12607 if (indexes
->empty() && !vals
->empty())
12609 for (size_t i
= 0; i
< vals
->size(); ++i
)
12610 indexes
->push_back(i
);
12613 Numeric_constant nc
;
12614 if (!index_expr
->numeric_constant_value(&nc
))
12616 error_at(index_expr
->location(),
12617 "index expression is not integer constant");
12618 return Expression::make_error(location
);
12621 switch (nc
.to_unsigned_long(&index
))
12623 case Numeric_constant::NC_UL_VALID
:
12625 case Numeric_constant::NC_UL_NOTINT
:
12626 error_at(index_expr
->location(),
12627 "index expression is not integer constant");
12628 return Expression::make_error(location
);
12629 case Numeric_constant::NC_UL_NEGATIVE
:
12630 error_at(index_expr
->location(), "index expression is negative");
12631 return Expression::make_error(location
);
12632 case Numeric_constant::NC_UL_BIG
:
12633 error_at(index_expr
->location(), "index value overflow");
12634 return Expression::make_error(location
);
12639 Named_type
* ntype
= Type::lookup_integer_type("int");
12640 Integer_type
* inttype
= ntype
->integer_type();
12641 if (sizeof(index
) <= static_cast<size_t>(inttype
->bits() * 8)
12642 && index
>> (inttype
->bits() - 1) != 0)
12644 error_at(index_expr
->location(), "index value overflow");
12645 return Expression::make_error(location
);
12648 if (std::find(indexes
->begin(), indexes
->end(), index
)
12651 error_at(index_expr
->location(), "duplicate value for index %lu",
12653 return Expression::make_error(location
);
12656 if (!indexes
->empty() && index
< indexes
->back())
12657 indexes_out_of_order
= true;
12659 indexes
->push_back(index
);
12662 vals
->push_back(val
);
12667 if (indexes
->empty())
12673 if (indexes_out_of_order
)
12675 typedef std::vector
<std::pair
<unsigned long, Expression
*> > V
;
12678 v
.reserve(indexes
->size());
12679 std::vector
<unsigned long>::const_iterator pi
= indexes
->begin();
12680 for (Expression_list::const_iterator pe
= vals
->begin();
12683 v
.push_back(std::make_pair(*pi
, *pe
));
12685 std::sort(v
.begin(), v
.end(), Index_value_compare());
12689 indexes
= new std::vector
<unsigned long>();
12690 indexes
->reserve(v
.size());
12691 vals
= new Expression_list();
12692 vals
->reserve(v
.size());
12694 for (V::const_iterator p
= v
.begin(); p
!= v
.end(); ++p
)
12696 indexes
->push_back(p
->first
);
12697 vals
->push_back(p
->second
);
12701 return this->make_array(type
, indexes
, vals
);
12704 // Actually build the array composite literal. This handles
12708 Composite_literal_expression::make_array(
12710 const std::vector
<unsigned long>* indexes
,
12711 Expression_list
* vals
)
12713 Location location
= this->location();
12714 Array_type
* at
= type
->array_type();
12716 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
12721 else if (indexes
!= NULL
)
12722 size
= indexes
->back() + 1;
12725 size
= vals
->size();
12726 Integer_type
* it
= Type::lookup_integer_type("int")->integer_type();
12727 if (sizeof(size
) <= static_cast<size_t>(it
->bits() * 8)
12728 && size
>> (it
->bits() - 1) != 0)
12730 error_at(location
, "too many elements in composite literal");
12731 return Expression::make_error(location
);
12736 mpz_init_set_ui(vlen
, size
);
12737 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
12739 at
= Type::make_array_type(at
->element_type(), elen
);
12742 else if (at
->length() != NULL
12743 && !at
->length()->is_error_expression()
12744 && this->vals_
!= NULL
)
12746 Numeric_constant nc
;
12748 if (at
->length()->numeric_constant_value(&nc
)
12749 && nc
.to_unsigned_long(&val
) == Numeric_constant::NC_UL_VALID
)
12751 if (indexes
== NULL
)
12753 if (this->vals_
->size() > val
)
12755 error_at(location
, "too many elements in composite literal");
12756 return Expression::make_error(location
);
12761 unsigned long max
= indexes
->back();
12765 ("some element keys in composite literal "
12766 "are out of range"));
12767 return Expression::make_error(location
);
12773 if (at
->length() != NULL
)
12774 return new Fixed_array_construction_expression(type
, indexes
, vals
,
12777 return new Open_array_construction_expression(type
, indexes
, vals
,
12781 // Lower a map composite literal.
12784 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
12785 Statement_inserter
* inserter
,
12788 Location location
= this->location();
12789 if (this->vals_
!= NULL
)
12791 if (!this->has_keys_
)
12793 error_at(location
, "map composite literal must have keys");
12794 return Expression::make_error(location
);
12797 for (Expression_list::iterator p
= this->vals_
->begin();
12798 p
!= this->vals_
->end();
12804 error_at((*p
)->location(),
12805 "map composite literal must have keys for every value");
12806 return Expression::make_error(location
);
12808 // Make sure we have lowered the key; it may not have been
12809 // lowered in order to handle keys for struct composite
12810 // literals. Lower it now to get the right error message.
12811 if ((*p
)->unknown_expression() != NULL
)
12813 (*p
)->unknown_expression()->clear_is_composite_literal_key();
12814 gogo
->lower_expression(function
, inserter
, &*p
);
12815 go_assert((*p
)->is_error_expression());
12816 return Expression::make_error(location
);
12821 return new Map_construction_expression(type
, this->vals_
, location
);
12824 // Dump ast representation for a composite literal expression.
12827 Composite_literal_expression::do_dump_expression(
12828 Ast_dump_context
* ast_dump_context
) const
12830 ast_dump_context
->ostream() << "composite(";
12831 ast_dump_context
->dump_type(this->type_
);
12832 ast_dump_context
->ostream() << ", {";
12833 ast_dump_context
->dump_expression_list(this->vals_
, this->has_keys_
);
12834 ast_dump_context
->ostream() << "})";
12837 // Make a composite literal expression.
12840 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
12841 Expression_list
* vals
,
12844 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
12848 // Return whether this expression is a composite literal.
12851 Expression::is_composite_literal() const
12853 switch (this->classification_
)
12855 case EXPRESSION_COMPOSITE_LITERAL
:
12856 case EXPRESSION_STRUCT_CONSTRUCTION
:
12857 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
12858 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
12859 case EXPRESSION_MAP_CONSTRUCTION
:
12866 // Return whether this expression is a composite literal which is not
12870 Expression::is_nonconstant_composite_literal() const
12872 switch (this->classification_
)
12874 case EXPRESSION_STRUCT_CONSTRUCTION
:
12876 const Struct_construction_expression
*psce
=
12877 static_cast<const Struct_construction_expression
*>(this);
12878 return !psce
->is_constant_struct();
12880 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
12882 const Fixed_array_construction_expression
*pace
=
12883 static_cast<const Fixed_array_construction_expression
*>(this);
12884 return !pace
->is_constant_array();
12886 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
12888 const Open_array_construction_expression
*pace
=
12889 static_cast<const Open_array_construction_expression
*>(this);
12890 return !pace
->is_constant_array();
12892 case EXPRESSION_MAP_CONSTRUCTION
:
12899 // Return true if this is a reference to a local variable.
12902 Expression::is_local_variable() const
12904 const Var_expression
* ve
= this->var_expression();
12907 const Named_object
* no
= ve
->named_object();
12908 return (no
->is_result_variable()
12909 || (no
->is_variable() && !no
->var_value()->is_global()));
12912 // Class Type_guard_expression.
12917 Type_guard_expression::do_traverse(Traverse
* traverse
)
12919 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
12920 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12921 return TRAVERSE_EXIT
;
12922 return TRAVERSE_CONTINUE
;
12925 // Check types of a type guard expression. The expression must have
12926 // an interface type, but the actual type conversion is checked at run
12930 Type_guard_expression::do_check_types(Gogo
*)
12932 Type
* expr_type
= this->expr_
->type();
12933 if (expr_type
->interface_type() == NULL
)
12935 if (!expr_type
->is_error() && !this->type_
->is_error())
12936 this->report_error(_("type assertion only valid for interface types"));
12937 this->set_is_error();
12939 else if (this->type_
->interface_type() == NULL
)
12941 std::string reason
;
12942 if (!expr_type
->interface_type()->implements_interface(this->type_
,
12945 if (!this->type_
->is_error())
12947 if (reason
.empty())
12948 this->report_error(_("impossible type assertion: "
12949 "type does not implement interface"));
12951 error_at(this->location(),
12952 ("impossible type assertion: "
12953 "type does not implement interface (%s)"),
12956 this->set_is_error();
12961 // Return a tree for a type guard expression.
12964 Type_guard_expression::do_get_tree(Translate_context
* context
)
12966 tree expr_tree
= this->expr_
->get_tree(context
);
12967 if (expr_tree
== error_mark_node
)
12968 return error_mark_node
;
12969 if (this->type_
->interface_type() != NULL
)
12970 return Expression::convert_interface_to_interface(context
, this->type_
,
12971 this->expr_
->type(),
12975 return Expression::convert_for_assignment(context
, this->type_
,
12976 this->expr_
->type(), expr_tree
,
12980 // Dump ast representation for a type guard expression.
12983 Type_guard_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
12986 this->expr_
->dump_expression(ast_dump_context
);
12987 ast_dump_context
->ostream() << ".";
12988 ast_dump_context
->dump_type(this->type_
);
12991 // Make a type guard expression.
12994 Expression::make_type_guard(Expression
* expr
, Type
* type
,
12997 return new Type_guard_expression(expr
, type
, location
);
13000 // Class Heap_composite_expression.
13002 // When you take the address of a composite literal, it is allocated
13003 // on the heap. This class implements that.
13005 class Heap_composite_expression
: public Expression
13008 Heap_composite_expression(Expression
* expr
, Location location
)
13009 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
13015 do_traverse(Traverse
* traverse
)
13016 { return Expression::traverse(&this->expr_
, traverse
); }
13020 { return Type::make_pointer_type(this->expr_
->type()); }
13023 do_determine_type(const Type_context
*)
13024 { this->expr_
->determine_type_no_context(); }
13029 return Expression::make_heap_composite(this->expr_
->copy(),
13034 do_get_tree(Translate_context
*);
13036 // We only export global objects, and the parser does not generate
13037 // this in global scope.
13039 do_export(Export
*) const
13040 { go_unreachable(); }
13043 do_dump_expression(Ast_dump_context
*) const;
13046 // The composite literal which is being put on the heap.
13050 // Return a tree which allocates a composite literal on the heap.
13053 Heap_composite_expression::do_get_tree(Translate_context
* context
)
13055 tree expr_tree
= this->expr_
->get_tree(context
);
13056 if (expr_tree
== error_mark_node
|| TREE_TYPE(expr_tree
) == error_mark_node
)
13057 return error_mark_node
;
13058 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
13059 go_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
13060 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
13061 expr_size
, this->location());
13062 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
13063 space
= save_expr(space
);
13064 tree ref
= build_fold_indirect_ref_loc(this->location().gcc_location(),
13066 TREE_THIS_NOTRAP(ref
) = 1;
13067 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
13068 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
13070 SET_EXPR_LOCATION(ret
, this->location().gcc_location());
13074 // Dump ast representation for a heap composite expression.
13077 Heap_composite_expression::do_dump_expression(
13078 Ast_dump_context
* ast_dump_context
) const
13080 ast_dump_context
->ostream() << "&(";
13081 ast_dump_context
->dump_expression(this->expr_
);
13082 ast_dump_context
->ostream() << ")";
13085 // Allocate a composite literal on the heap.
13088 Expression::make_heap_composite(Expression
* expr
, Location location
)
13090 return new Heap_composite_expression(expr
, location
);
13093 // Class Receive_expression.
13095 // Return the type of a receive expression.
13098 Receive_expression::do_type()
13100 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
13101 if (channel_type
== NULL
)
13102 return Type::make_error_type();
13103 return channel_type
->element_type();
13106 // Check types for a receive expression.
13109 Receive_expression::do_check_types(Gogo
*)
13111 Type
* type
= this->channel_
->type();
13112 if (type
->is_error())
13114 this->set_is_error();
13117 if (type
->channel_type() == NULL
)
13119 this->report_error(_("expected channel"));
13122 if (!type
->channel_type()->may_receive())
13124 this->report_error(_("invalid receive on send-only channel"));
13129 // Get a tree for a receive expression.
13132 Receive_expression::do_get_tree(Translate_context
* context
)
13134 Location loc
= this->location();
13136 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
13137 if (channel_type
== NULL
)
13139 go_assert(this->channel_
->type()->is_error());
13140 return error_mark_node
;
13143 Expression
* td
= Expression::make_type_descriptor(channel_type
, loc
);
13144 tree td_tree
= td
->get_tree(context
);
13146 Type
* element_type
= channel_type
->element_type();
13147 Btype
* element_type_btype
= element_type
->get_backend(context
->gogo());
13148 tree element_type_tree
= type_to_tree(element_type_btype
);
13150 tree channel
= this->channel_
->get_tree(context
);
13151 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
13152 return error_mark_node
;
13154 return Gogo::receive_from_channel(element_type_tree
, td_tree
, channel
, loc
);
13157 // Dump ast representation for a receive expression.
13160 Receive_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
13162 ast_dump_context
->ostream() << " <- " ;
13163 ast_dump_context
->dump_expression(channel_
);
13166 // Make a receive expression.
13168 Receive_expression
*
13169 Expression::make_receive(Expression
* channel
, Location location
)
13171 return new Receive_expression(channel
, location
);
13174 // An expression which evaluates to a pointer to the type descriptor
13177 class Type_descriptor_expression
: public Expression
13180 Type_descriptor_expression(Type
* type
, Location location
)
13181 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
13188 { return Type::make_type_descriptor_ptr_type(); }
13191 do_determine_type(const Type_context
*)
13199 do_get_tree(Translate_context
* context
)
13201 return this->type_
->type_descriptor_pointer(context
->gogo(),
13206 do_dump_expression(Ast_dump_context
*) const;
13209 // The type for which this is the descriptor.
13213 // Dump ast representation for a type descriptor expression.
13216 Type_descriptor_expression::do_dump_expression(
13217 Ast_dump_context
* ast_dump_context
) const
13219 ast_dump_context
->dump_type(this->type_
);
13222 // Make a type descriptor expression.
13225 Expression::make_type_descriptor(Type
* type
, Location location
)
13227 return new Type_descriptor_expression(type
, location
);
13230 // An expression which evaluates to some characteristic of a type.
13231 // This is only used to initialize fields of a type descriptor. Using
13232 // a new expression class is slightly inefficient but gives us a good
13233 // separation between the frontend and the middle-end with regard to
13234 // how types are laid out.
13236 class Type_info_expression
: public Expression
13239 Type_info_expression(Type
* type
, Type_info type_info
)
13240 : Expression(EXPRESSION_TYPE_INFO
, Linemap::predeclared_location()),
13241 type_(type
), type_info_(type_info
)
13249 do_determine_type(const Type_context
*)
13257 do_get_tree(Translate_context
* context
);
13260 do_dump_expression(Ast_dump_context
*) const;
13263 // The type for which we are getting information.
13265 // What information we want.
13266 Type_info type_info_
;
13269 // The type is chosen to match what the type descriptor struct
13273 Type_info_expression::do_type()
13275 switch (this->type_info_
)
13277 case TYPE_INFO_SIZE
:
13278 return Type::lookup_integer_type("uintptr");
13279 case TYPE_INFO_ALIGNMENT
:
13280 case TYPE_INFO_FIELD_ALIGNMENT
:
13281 return Type::lookup_integer_type("uint8");
13287 // Return type information in GENERIC.
13290 Type_info_expression::do_get_tree(Translate_context
* context
)
13292 Btype
* btype
= this->type_
->get_backend(context
->gogo());
13293 Gogo
* gogo
= context
->gogo();
13295 switch (this->type_info_
)
13297 case TYPE_INFO_SIZE
:
13298 val
= gogo
->backend()->type_size(btype
);
13300 case TYPE_INFO_ALIGNMENT
:
13301 val
= gogo
->backend()->type_alignment(btype
);
13303 case TYPE_INFO_FIELD_ALIGNMENT
:
13304 val
= gogo
->backend()->type_field_alignment(btype
);
13309 tree val_type_tree
= type_to_tree(this->type()->get_backend(gogo
));
13310 go_assert(val_type_tree
!= error_mark_node
);
13311 return build_int_cstu(val_type_tree
, val
);
13314 // Dump ast representation for a type info expression.
13317 Type_info_expression::do_dump_expression(
13318 Ast_dump_context
* ast_dump_context
) const
13320 ast_dump_context
->ostream() << "typeinfo(";
13321 ast_dump_context
->dump_type(this->type_
);
13322 ast_dump_context
->ostream() << ",";
13323 ast_dump_context
->ostream() <<
13324 (this->type_info_
== TYPE_INFO_ALIGNMENT
? "alignment"
13325 : this->type_info_
== TYPE_INFO_FIELD_ALIGNMENT
? "field alignment"
13326 : this->type_info_
== TYPE_INFO_SIZE
? "size "
13328 ast_dump_context
->ostream() << ")";
13331 // Make a type info expression.
13334 Expression::make_type_info(Type
* type
, Type_info type_info
)
13336 return new Type_info_expression(type
, type_info
);
13339 // An expression which evaluates to the offset of a field within a
13340 // struct. This, like Type_info_expression, q.v., is only used to
13341 // initialize fields of a type descriptor.
13343 class Struct_field_offset_expression
: public Expression
13346 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
13347 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
,
13348 Linemap::predeclared_location()),
13349 type_(type
), field_(field
)
13355 { return Type::lookup_integer_type("uintptr"); }
13358 do_determine_type(const Type_context
*)
13366 do_get_tree(Translate_context
* context
);
13369 do_dump_expression(Ast_dump_context
*) const;
13372 // The type of the struct.
13373 Struct_type
* type_
;
13375 const Struct_field
* field_
;
13378 // Return a struct field offset in GENERIC.
13381 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
13383 tree type_tree
= type_to_tree(this->type_
->get_backend(context
->gogo()));
13384 if (type_tree
== error_mark_node
)
13385 return error_mark_node
;
13387 tree val_type_tree
= type_to_tree(this->type()->get_backend(context
->gogo()));
13388 go_assert(val_type_tree
!= error_mark_node
);
13390 const Struct_field_list
* fields
= this->type_
->fields();
13391 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
13392 Struct_field_list::const_iterator p
;
13393 for (p
= fields
->begin();
13394 p
!= fields
->end();
13395 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
13397 go_assert(struct_field_tree
!= NULL_TREE
);
13398 if (&*p
== this->field_
)
13401 go_assert(&*p
== this->field_
);
13403 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
13404 byte_position(struct_field_tree
));
13407 // Dump ast representation for a struct field offset expression.
13410 Struct_field_offset_expression::do_dump_expression(
13411 Ast_dump_context
* ast_dump_context
) const
13413 ast_dump_context
->ostream() << "unsafe.Offsetof(";
13414 ast_dump_context
->dump_type(this->type_
);
13415 ast_dump_context
->ostream() << '.';
13416 ast_dump_context
->ostream() <<
13417 Gogo::message_name(this->field_
->field_name());
13418 ast_dump_context
->ostream() << ")";
13421 // Make an expression for a struct field offset.
13424 Expression::make_struct_field_offset(Struct_type
* type
,
13425 const Struct_field
* field
)
13427 return new Struct_field_offset_expression(type
, field
);
13430 // An expression which evaluates to a pointer to the map descriptor of
13433 class Map_descriptor_expression
: public Expression
13436 Map_descriptor_expression(Map_type
* type
, Location location
)
13437 : Expression(EXPRESSION_MAP_DESCRIPTOR
, location
),
13444 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
13447 do_determine_type(const Type_context
*)
13455 do_get_tree(Translate_context
* context
)
13457 return this->type_
->map_descriptor_pointer(context
->gogo(),
13462 do_dump_expression(Ast_dump_context
*) const;
13465 // The type for which this is the descriptor.
13469 // Dump ast representation for a map descriptor expression.
13472 Map_descriptor_expression::do_dump_expression(
13473 Ast_dump_context
* ast_dump_context
) const
13475 ast_dump_context
->ostream() << "map_descriptor(";
13476 ast_dump_context
->dump_type(this->type_
);
13477 ast_dump_context
->ostream() << ")";
13480 // Make a map descriptor expression.
13483 Expression::make_map_descriptor(Map_type
* type
, Location location
)
13485 return new Map_descriptor_expression(type
, location
);
13488 // An expression which evaluates to the address of an unnamed label.
13490 class Label_addr_expression
: public Expression
13493 Label_addr_expression(Label
* label
, Location location
)
13494 : Expression(EXPRESSION_LABEL_ADDR
, location
),
13501 { return Type::make_pointer_type(Type::make_void_type()); }
13504 do_determine_type(const Type_context
*)
13509 { return new Label_addr_expression(this->label_
, this->location()); }
13512 do_get_tree(Translate_context
* context
)
13514 return expr_to_tree(this->label_
->get_addr(context
, this->location()));
13518 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
13519 { ast_dump_context
->ostream() << this->label_
->name(); }
13522 // The label whose address we are taking.
13526 // Make an expression for the address of an unnamed label.
13529 Expression::make_label_addr(Label
* label
, Location location
)
13531 return new Label_addr_expression(label
, location
);
13534 // Import an expression. This comes at the end in order to see the
13535 // various class definitions.
13538 Expression::import_expression(Import
* imp
)
13540 int c
= imp
->peek_char();
13541 if (imp
->match_c_string("- ")
13542 || imp
->match_c_string("! ")
13543 || imp
->match_c_string("^ "))
13544 return Unary_expression::do_import(imp
);
13546 return Binary_expression::do_import(imp
);
13547 else if (imp
->match_c_string("true")
13548 || imp
->match_c_string("false"))
13549 return Boolean_expression::do_import(imp
);
13551 return String_expression::do_import(imp
);
13552 else if (c
== '-' || (c
>= '0' && c
<= '9'))
13554 // This handles integers, floats and complex constants.
13555 return Integer_expression::do_import(imp
);
13557 else if (imp
->match_c_string("nil"))
13558 return Nil_expression::do_import(imp
);
13559 else if (imp
->match_c_string("convert"))
13560 return Type_conversion_expression::do_import(imp
);
13563 error_at(imp
->location(), "import error: expected expression");
13564 return Expression::make_error(imp
->location());
13568 // Class Expression_list.
13570 // Traverse the list.
13573 Expression_list::traverse(Traverse
* traverse
)
13575 for (Expression_list::iterator p
= this->begin();
13581 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
13582 return TRAVERSE_EXIT
;
13585 return TRAVERSE_CONTINUE
;
13591 Expression_list::copy()
13593 Expression_list
* ret
= new Expression_list();
13594 for (Expression_list::iterator p
= this->begin();
13599 ret
->push_back(NULL
);
13601 ret
->push_back((*p
)->copy());
13606 // Return whether an expression list has an error expression.
13609 Expression_list::contains_error() const
13611 for (Expression_list::const_iterator p
= this->begin();
13614 if (*p
!= NULL
&& (*p
)->is_error_expression())
13619 // Class Numeric_constant.
13623 Numeric_constant::~Numeric_constant()
13628 // Copy constructor.
13630 Numeric_constant::Numeric_constant(const Numeric_constant
& a
)
13631 : classification_(a
.classification_
), type_(a
.type_
)
13633 switch (a
.classification_
)
13639 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
13642 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, GMP_RNDN
);
13645 mpfr_init_set(this->u_
.complex_val
.real
, a
.u_
.complex_val
.real
,
13647 mpfr_init_set(this->u_
.complex_val
.imag
, a
.u_
.complex_val
.imag
,
13655 // Assignment operator.
13658 Numeric_constant::operator=(const Numeric_constant
& a
)
13661 this->classification_
= a
.classification_
;
13662 this->type_
= a
.type_
;
13663 switch (a
.classification_
)
13669 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
13672 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, GMP_RNDN
);
13675 mpfr_init_set(this->u_
.complex_val
.real
, a
.u_
.complex_val
.real
,
13677 mpfr_init_set(this->u_
.complex_val
.imag
, a
.u_
.complex_val
.imag
,
13686 // Clear the contents.
13689 Numeric_constant::clear()
13691 switch (this->classification_
)
13697 mpz_clear(this->u_
.int_val
);
13700 mpfr_clear(this->u_
.float_val
);
13703 mpfr_clear(this->u_
.complex_val
.real
);
13704 mpfr_clear(this->u_
.complex_val
.imag
);
13709 this->classification_
= NC_INVALID
;
13712 // Set to an unsigned long value.
13715 Numeric_constant::set_unsigned_long(Type
* type
, unsigned long val
)
13718 this->classification_
= NC_INT
;
13719 this->type_
= type
;
13720 mpz_init_set_ui(this->u_
.int_val
, val
);
13723 // Set to an integer value.
13726 Numeric_constant::set_int(Type
* type
, const mpz_t val
)
13729 this->classification_
= NC_INT
;
13730 this->type_
= type
;
13731 mpz_init_set(this->u_
.int_val
, val
);
13734 // Set to a rune value.
13737 Numeric_constant::set_rune(Type
* type
, const mpz_t val
)
13740 this->classification_
= NC_RUNE
;
13741 this->type_
= type
;
13742 mpz_init_set(this->u_
.int_val
, val
);
13745 // Set to a floating point value.
13748 Numeric_constant::set_float(Type
* type
, const mpfr_t val
)
13751 this->classification_
= NC_FLOAT
;
13752 this->type_
= type
;
13753 // Numeric constants do not have negative zero values, so remove
13754 // them here. They also don't have infinity or NaN values, but we
13755 // should never see them here.
13756 if (mpfr_zero_p(val
))
13757 mpfr_init_set_ui(this->u_
.float_val
, 0, GMP_RNDN
);
13759 mpfr_init_set(this->u_
.float_val
, val
, GMP_RNDN
);
13762 // Set to a complex value.
13765 Numeric_constant::set_complex(Type
* type
, const mpfr_t real
, const mpfr_t imag
)
13768 this->classification_
= NC_COMPLEX
;
13769 this->type_
= type
;
13770 mpfr_init_set(this->u_
.complex_val
.real
, real
, GMP_RNDN
);
13771 mpfr_init_set(this->u_
.complex_val
.imag
, imag
, GMP_RNDN
);
13774 // Get an int value.
13777 Numeric_constant::get_int(mpz_t
* val
) const
13779 go_assert(this->is_int());
13780 mpz_init_set(*val
, this->u_
.int_val
);
13783 // Get a rune value.
13786 Numeric_constant::get_rune(mpz_t
* val
) const
13788 go_assert(this->is_rune());
13789 mpz_init_set(*val
, this->u_
.int_val
);
13792 // Get a floating point value.
13795 Numeric_constant::get_float(mpfr_t
* val
) const
13797 go_assert(this->is_float());
13798 mpfr_init_set(*val
, this->u_
.float_val
, GMP_RNDN
);
13801 // Get a complex value.
13804 Numeric_constant::get_complex(mpfr_t
* real
, mpfr_t
* imag
) const
13806 go_assert(this->is_complex());
13807 mpfr_init_set(*real
, this->u_
.complex_val
.real
, GMP_RNDN
);
13808 mpfr_init_set(*imag
, this->u_
.complex_val
.imag
, GMP_RNDN
);
13811 // Express value as unsigned long if possible.
13813 Numeric_constant::To_unsigned_long
13814 Numeric_constant::to_unsigned_long(unsigned long* val
) const
13816 switch (this->classification_
)
13820 return this->mpz_to_unsigned_long(this->u_
.int_val
, val
);
13822 return this->mpfr_to_unsigned_long(this->u_
.float_val
, val
);
13824 if (!mpfr_zero_p(this->u_
.complex_val
.imag
))
13825 return NC_UL_NOTINT
;
13826 return this->mpfr_to_unsigned_long(this->u_
.complex_val
.real
, val
);
13832 // Express integer value as unsigned long if possible.
13834 Numeric_constant::To_unsigned_long
13835 Numeric_constant::mpz_to_unsigned_long(const mpz_t ival
,
13836 unsigned long *val
) const
13838 if (mpz_sgn(ival
) < 0)
13839 return NC_UL_NEGATIVE
;
13840 unsigned long ui
= mpz_get_ui(ival
);
13841 if (mpz_cmp_ui(ival
, ui
) != 0)
13844 return NC_UL_VALID
;
13847 // Express floating point value as unsigned long if possible.
13849 Numeric_constant::To_unsigned_long
13850 Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval
,
13851 unsigned long *val
) const
13853 if (!mpfr_integer_p(fval
))
13854 return NC_UL_NOTINT
;
13857 mpfr_get_z(ival
, fval
, GMP_RNDN
);
13858 To_unsigned_long ret
= this->mpz_to_unsigned_long(ival
, val
);
13863 // Convert value to integer if possible.
13866 Numeric_constant::to_int(mpz_t
* val
) const
13868 switch (this->classification_
)
13872 mpz_init_set(*val
, this->u_
.int_val
);
13875 if (!mpfr_integer_p(this->u_
.float_val
))
13878 mpfr_get_z(*val
, this->u_
.float_val
, GMP_RNDN
);
13881 if (!mpfr_zero_p(this->u_
.complex_val
.imag
)
13882 || !mpfr_integer_p(this->u_
.complex_val
.real
))
13885 mpfr_get_z(*val
, this->u_
.complex_val
.real
, GMP_RNDN
);
13892 // Convert value to floating point if possible.
13895 Numeric_constant::to_float(mpfr_t
* val
) const
13897 switch (this->classification_
)
13901 mpfr_init_set_z(*val
, this->u_
.int_val
, GMP_RNDN
);
13904 mpfr_init_set(*val
, this->u_
.float_val
, GMP_RNDN
);
13907 if (!mpfr_zero_p(this->u_
.complex_val
.imag
))
13909 mpfr_init_set(*val
, this->u_
.complex_val
.real
, GMP_RNDN
);
13916 // Convert value to complex.
13919 Numeric_constant::to_complex(mpfr_t
* vr
, mpfr_t
* vi
) const
13921 switch (this->classification_
)
13925 mpfr_init_set_z(*vr
, this->u_
.int_val
, GMP_RNDN
);
13926 mpfr_init_set_ui(*vi
, 0, GMP_RNDN
);
13929 mpfr_init_set(*vr
, this->u_
.float_val
, GMP_RNDN
);
13930 mpfr_init_set_ui(*vi
, 0, GMP_RNDN
);
13933 mpfr_init_set(*vr
, this->u_
.complex_val
.real
, GMP_RNDN
);
13934 mpfr_init_set(*vi
, this->u_
.complex_val
.imag
, GMP_RNDN
);
13944 Numeric_constant::type() const
13946 if (this->type_
!= NULL
)
13947 return this->type_
;
13948 switch (this->classification_
)
13951 return Type::make_abstract_integer_type();
13953 return Type::make_abstract_character_type();
13955 return Type::make_abstract_float_type();
13957 return Type::make_abstract_complex_type();
13963 // If the constant can be expressed in TYPE, then set the type of the
13964 // constant to TYPE and return true. Otherwise return false, and, if
13965 // ISSUE_ERROR is true, report an appropriate error message.
13968 Numeric_constant::set_type(Type
* type
, bool issue_error
, Location loc
)
13973 else if (type
->integer_type() != NULL
)
13974 ret
= this->check_int_type(type
->integer_type(), issue_error
, loc
);
13975 else if (type
->float_type() != NULL
)
13976 ret
= this->check_float_type(type
->float_type(), issue_error
, loc
);
13977 else if (type
->complex_type() != NULL
)
13978 ret
= this->check_complex_type(type
->complex_type(), issue_error
, loc
);
13982 this->type_
= type
;
13986 // Check whether the constant can be expressed in an integer type.
13989 Numeric_constant::check_int_type(Integer_type
* type
, bool issue_error
,
13990 Location location
) const
13993 switch (this->classification_
)
13997 mpz_init_set(val
, this->u_
.int_val
);
14001 if (!mpfr_integer_p(this->u_
.float_val
))
14004 error_at(location
, "floating point constant truncated to integer");
14008 mpfr_get_z(val
, this->u_
.float_val
, GMP_RNDN
);
14012 if (!mpfr_integer_p(this->u_
.complex_val
.real
)
14013 || !mpfr_zero_p(this->u_
.complex_val
.imag
))
14016 error_at(location
, "complex constant truncated to integer");
14020 mpfr_get_z(val
, this->u_
.complex_val
.real
, GMP_RNDN
);
14028 if (type
->is_abstract())
14032 int bits
= mpz_sizeinbase(val
, 2);
14033 if (type
->is_unsigned())
14035 // For an unsigned type we can only accept a nonnegative
14036 // number, and we must be able to represents at least BITS.
14037 ret
= mpz_sgn(val
) >= 0 && bits
<= type
->bits();
14041 // For a signed type we need an extra bit to indicate the
14042 // sign. We have to handle the most negative integer
14044 ret
= (bits
+ 1 <= type
->bits()
14045 || (bits
<= type
->bits()
14046 && mpz_sgn(val
) < 0
14047 && (mpz_scan1(val
, 0)
14048 == static_cast<unsigned long>(type
->bits() - 1))
14049 && mpz_scan0(val
, type
->bits()) == ULONG_MAX
));
14053 if (!ret
&& issue_error
)
14054 error_at(location
, "integer constant overflow");
14059 // Check whether the constant can be expressed in a floating point
14063 Numeric_constant::check_float_type(Float_type
* type
, bool issue_error
,
14064 Location location
) const
14067 switch (this->classification_
)
14071 mpfr_init_set_z(val
, this->u_
.int_val
, GMP_RNDN
);
14075 mpfr_init_set(val
, this->u_
.float_val
, GMP_RNDN
);
14079 if (!mpfr_zero_p(this->u_
.complex_val
.imag
))
14082 error_at(location
, "complex constant truncated to float");
14085 mpfr_init_set(val
, this->u_
.complex_val
.real
, GMP_RNDN
);
14093 if (type
->is_abstract())
14095 else if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
14097 // A NaN or Infinity always fits in the range of the type.
14102 mp_exp_t exp
= mpfr_get_exp(val
);
14104 switch (type
->bits())
14116 ret
= exp
<= max_exp
;
14121 if (!ret
&& issue_error
)
14122 error_at(location
, "floating point constant overflow");
14127 // Check whether the constant can be expressed in a complex type.
14130 Numeric_constant::check_complex_type(Complex_type
* type
, bool issue_error
,
14131 Location location
) const
14133 if (type
->is_abstract())
14137 switch (type
->bits())
14150 switch (this->classification_
)
14154 mpfr_init_set_z(real
, this->u_
.int_val
, GMP_RNDN
);
14158 mpfr_init_set(real
, this->u_
.float_val
, GMP_RNDN
);
14162 if (!mpfr_nan_p(this->u_
.complex_val
.imag
)
14163 && !mpfr_inf_p(this->u_
.complex_val
.imag
)
14164 && !mpfr_zero_p(this->u_
.complex_val
.imag
))
14166 if (mpfr_get_exp(this->u_
.complex_val
.imag
) > max_exp
)
14169 error_at(location
, "complex imaginary part overflow");
14173 mpfr_init_set(real
, this->u_
.complex_val
.real
, GMP_RNDN
);
14181 if (mpfr_nan_p(real
) || mpfr_inf_p(real
) || mpfr_zero_p(real
))
14184 ret
= mpfr_get_exp(real
) <= max_exp
;
14188 if (!ret
&& issue_error
)
14189 error_at(location
, "complex real part overflow");
14194 // Return an Expression for this value.
14197 Numeric_constant::expression(Location loc
) const
14199 switch (this->classification_
)
14202 return Expression::make_integer(&this->u_
.int_val
, this->type_
, loc
);
14204 return Expression::make_character(&this->u_
.int_val
, this->type_
, loc
);
14206 return Expression::make_float(&this->u_
.float_val
, this->type_
, loc
);
14208 return Expression::make_complex(&this->u_
.complex_val
.real
,
14209 &this->u_
.complex_val
.imag
,