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.
13 #include "go-diagnostics.h"
14 #include "go-encode-id.h"
18 #include "statements.h"
22 #include "expressions.h"
27 Expression::Expression(Expression_classification classification
,
29 : classification_(classification
), location_(location
)
33 Expression::~Expression()
37 // Traverse the expressions.
40 Expression::traverse(Expression
** pexpr
, Traverse
* traverse
)
42 Expression
* expr
= *pexpr
;
43 if ((traverse
->traverse_mask() & Traverse::traverse_expressions
) != 0)
45 int t
= traverse
->expression(pexpr
);
46 if (t
== TRAVERSE_EXIT
)
48 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
49 return TRAVERSE_CONTINUE
;
51 return expr
->do_traverse(traverse
);
54 // Traverse subexpressions of this expression.
57 Expression::traverse_subexpressions(Traverse
* traverse
)
59 return this->do_traverse(traverse
);
62 // A traversal used to set the location of subexpressions.
64 class Set_location
: public Traverse
67 Set_location(Location loc
)
68 : Traverse(traverse_expressions
),
73 expression(Expression
** pexpr
);
79 // Set the location of an expression.
82 Set_location::expression(Expression
** pexpr
)
84 // Some expressions are shared or don't have an independent
85 // location, so we shouldn't change their location. This is the set
86 // of expressions for which do_copy is just "return this" or
87 // otherwise does not pass down the location.
88 switch ((*pexpr
)->classification())
90 case Expression::EXPRESSION_ERROR
:
91 case Expression::EXPRESSION_VAR_REFERENCE
:
92 case Expression::EXPRESSION_ENCLOSED_VAR_REFERENCE
:
93 case Expression::EXPRESSION_STRING
:
94 case Expression::EXPRESSION_FUNC_DESCRIPTOR
:
95 case Expression::EXPRESSION_TYPE
:
96 case Expression::EXPRESSION_BOOLEAN
:
97 case Expression::EXPRESSION_CONST_REFERENCE
:
98 case Expression::EXPRESSION_NIL
:
99 case Expression::EXPRESSION_TYPE_DESCRIPTOR
:
100 case Expression::EXPRESSION_GC_SYMBOL
:
101 case Expression::EXPRESSION_PTRMASK_SYMBOL
:
102 case Expression::EXPRESSION_TYPE_INFO
:
103 case Expression::EXPRESSION_STRUCT_FIELD_OFFSET
:
104 return TRAVERSE_CONTINUE
;
109 (*pexpr
)->location_
= this->loc_
;
110 return TRAVERSE_CONTINUE
;
113 // Set the location of an expression and its subexpressions.
116 Expression::set_location(Location loc
)
118 this->location_
= loc
;
119 Set_location
sl(loc
);
120 this->traverse_subexpressions(&sl
);
123 // Default implementation for do_traverse for child classes.
126 Expression::do_traverse(Traverse
*)
128 return TRAVERSE_CONTINUE
;
131 // This virtual function is called by the parser if the value of this
132 // expression is being discarded. By default, we give an error.
133 // Expressions with side effects override.
136 Expression::do_discarding_value()
138 this->unused_value_error();
142 // This virtual function is called to export expressions. This will
143 // only be used by expressions which may be constant.
146 Expression::do_export(Export_function_body
*) const
151 // Write a name to the export data.
154 Expression::export_name(Export_function_body
* efb
, const Named_object
* no
)
156 if (no
->package() != NULL
)
159 snprintf(buf
, sizeof buf
, "<p%d>", efb
->package_index(no
->package()));
160 efb
->write_c_string(buf
);
163 if (!Gogo::is_hidden_name(no
->name()))
164 efb
->write_string(no
->name());
167 efb
->write_c_string(".");
168 efb
->write_string(Gogo::unpack_hidden_name(no
->name()));
172 // Give an error saying that the value of the expression is not used.
175 Expression::unused_value_error()
177 if (this->type()->is_error())
179 go_assert(saw_errors());
180 this->set_is_error();
183 this->report_error(_("value computed is not used"));
186 // Note that this expression is an error. This is called by children
187 // when they discover an error.
190 Expression::set_is_error()
192 this->classification_
= EXPRESSION_ERROR
;
195 // For children to call to report an error conveniently.
198 Expression::report_error(const char* msg
)
200 go_error_at(this->location_
, "%s", msg
);
201 this->set_is_error();
204 // Set types of variables and constants. This is implemented by the
208 Expression::determine_type(const Type_context
* context
)
210 this->do_determine_type(context
);
213 // Set types when there is no context.
216 Expression::determine_type_no_context()
218 Type_context context
;
219 this->do_determine_type(&context
);
222 // Return true if two expressions refer to the same variable or struct
223 // field. This can only be true when there are no side effects.
226 Expression::is_same_variable(Expression
* a
, Expression
* b
)
228 if (a
->classification() != b
->classification())
231 Var_expression
* av
= a
->var_expression();
233 return av
->named_object() == b
->var_expression()->named_object();
235 Field_reference_expression
* af
= a
->field_reference_expression();
238 Field_reference_expression
* bf
= b
->field_reference_expression();
239 return (af
->field_index() == bf
->field_index()
240 && Expression::is_same_variable(af
->expr(), bf
->expr()));
243 Unary_expression
* au
= a
->unary_expression();
246 Unary_expression
* bu
= b
->unary_expression();
247 return (au
->op() == OPERATOR_MULT
248 && bu
->op() == OPERATOR_MULT
249 && Expression::is_same_variable(au
->operand(),
253 Array_index_expression
* aie
= a
->array_index_expression();
256 Array_index_expression
* bie
= b
->array_index_expression();
257 return (aie
->end() == NULL
258 && bie
->end() == NULL
259 && Expression::is_same_variable(aie
->array(), bie
->array())
260 && Expression::is_same_variable(aie
->start(), bie
->start()));
263 Numeric_constant aval
;
264 if (a
->numeric_constant_value(&aval
))
266 Numeric_constant bval
;
267 if (b
->numeric_constant_value(&bval
))
268 return aval
.equals(bval
);
274 // Return an expression handling any conversions which must be done during
278 Expression::convert_for_assignment(Gogo
* gogo
, Type
* lhs_type
,
279 Expression
* rhs
, Location location
)
281 Type
* rhs_type
= rhs
->type();
282 if (lhs_type
->is_error()
283 || rhs_type
->is_error()
284 || rhs
->is_error_expression())
285 return Expression::make_error(location
);
287 bool are_identical
= Type::are_identical(lhs_type
, rhs_type
,
288 (Type::COMPARE_ERRORS
289 | Type::COMPARE_TAGS
),
291 if (!are_identical
&& lhs_type
->interface_type() != NULL
)
293 // Type to interface conversions have been made explicit early.
294 go_assert(rhs_type
->interface_type() != NULL
);
295 return Expression::convert_interface_to_interface(lhs_type
, rhs
, false,
298 else if (!are_identical
&& rhs_type
->interface_type() != NULL
)
299 return Expression::convert_interface_to_type(gogo
, lhs_type
, rhs
, location
);
300 else if (lhs_type
->is_slice_type() && rhs_type
->is_nil_type())
302 // Assigning nil to a slice.
303 Expression
* nil
= Expression::make_nil(location
);
304 Expression
* zero
= Expression::make_integer_ul(0, NULL
, location
);
305 return Expression::make_slice_value(lhs_type
, nil
, zero
, zero
, location
);
307 else if (rhs_type
->is_nil_type())
308 return Expression::make_nil(location
);
309 else if (are_identical
)
311 if (lhs_type
->forwarded() != rhs_type
->forwarded())
313 // Different but identical types require an explicit
314 // conversion. This happens with type aliases.
315 return Expression::make_cast(lhs_type
, rhs
, location
);
318 // No conversion is needed.
321 else if (lhs_type
->points_to() != NULL
)
322 return Expression::make_unsafe_cast(lhs_type
, rhs
, location
);
323 else if (lhs_type
->is_numeric_type())
324 return Expression::make_cast(lhs_type
, rhs
, location
);
325 else if ((lhs_type
->struct_type() != NULL
326 && rhs_type
->struct_type() != NULL
)
327 || (lhs_type
->array_type() != NULL
328 && rhs_type
->array_type() != NULL
))
330 // This conversion must be permitted by Go, or we wouldn't have
332 return Expression::make_unsafe_cast(lhs_type
, rhs
, location
);
338 // Return an expression for a conversion from a non-interface type to an
339 // interface type. If ON_STACK is true, it can allocate the storage on
343 Expression::convert_type_to_interface(Type
* lhs_type
, Expression
* rhs
,
344 bool on_stack
, Location location
)
346 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
347 bool lhs_is_empty
= lhs_interface_type
->is_empty();
349 // Since RHS_TYPE is a static type, we can create the interface
350 // method table at compile time.
352 // When setting an interface to nil, we just set both fields to
354 Type
* rhs_type
= rhs
->type();
355 if (rhs_type
->is_nil_type())
357 Expression
* nil
= Expression::make_nil(location
);
358 return Expression::make_interface_value(lhs_type
, nil
, nil
, location
);
361 // This should have been checked already.
362 if (!lhs_interface_type
->implements_interface(rhs_type
, NULL
))
364 go_assert(saw_errors());
365 return Expression::make_error(location
);
368 // An interface is a tuple. If LHS_TYPE is an empty interface type,
369 // then the first field is the type descriptor for RHS_TYPE.
370 // Otherwise it is the interface method table for RHS_TYPE.
371 Expression
* first_field
;
373 first_field
= Expression::make_type_descriptor(rhs_type
, location
);
376 // Build the interface method table for this interface and this
377 // object type: a list of function pointers for each interface
379 Named_type
* rhs_named_type
= rhs_type
->named_type();
380 Struct_type
* rhs_struct_type
= rhs_type
->struct_type();
381 bool is_pointer
= false;
382 if (rhs_named_type
== NULL
&& rhs_struct_type
== NULL
)
384 rhs_named_type
= rhs_type
->deref()->named_type();
385 rhs_struct_type
= rhs_type
->deref()->struct_type();
388 if (rhs_named_type
!= NULL
)
390 rhs_named_type
->interface_method_table(lhs_interface_type
,
392 else if (rhs_struct_type
!= NULL
)
394 rhs_struct_type
->interface_method_table(lhs_interface_type
,
397 first_field
= Expression::make_nil(location
);
401 if (rhs_type
->is_direct_iface_type())
403 // We are assigning a pointer to the interface; the interface
404 // holds the pointer itself.
405 obj
= unpack_direct_iface(rhs
, location
);
409 // We are assigning a non-pointer value to the interface; the
410 // interface gets a copy of the value in the heap if it escapes.
411 if (rhs
->is_constant())
412 obj
= Expression::make_unary(OPERATOR_AND
, rhs
, location
);
415 obj
= Expression::make_heap_expression(rhs
, location
);
417 obj
->heap_expression()->set_allocate_on_stack();
421 return Expression::make_interface_value(lhs_type
, first_field
, obj
, location
);
424 // Return an expression for the pointer-typed value of a direct interface
425 // type. Specifically, for single field struct or array, get the single
426 // field, and do this recursively. The reason for this is that we don't
427 // want to assign a struct or an array to a pointer-typed field. The
428 // backend may not like that.
431 Expression::unpack_direct_iface(Expression
* rhs
, Location loc
)
433 Struct_type
* st
= rhs
->type()->struct_type();
436 go_assert(st
->field_count() == 1);
437 Expression
* field
= Expression::make_field_reference(rhs
, 0, loc
);
438 return unpack_direct_iface(field
, loc
);
440 Array_type
* at
= rhs
->type()->array_type();
444 bool ok
= at
->int_length(&len
);
445 go_assert(ok
&& len
== 1);
446 Type
* int_type
= Type::lookup_integer_type("int");
447 Expression
* index
= Expression::make_integer_ul(0, int_type
, loc
);
448 Expression
* elem
= Expression::make_array_index(rhs
, index
, NULL
, NULL
, loc
);
449 return unpack_direct_iface(elem
, loc
);
454 // The opposite of unpack_direct_iface.
457 Expression::pack_direct_iface(Type
* t
, Expression
* rhs
, Location loc
)
459 if (rhs
->type() == t
)
461 Struct_type
* st
= t
->struct_type();
464 Expression_list
* vals
= new Expression_list();
465 vals
->push_back(pack_direct_iface(st
->field(0)->type(), rhs
, loc
));
466 return Expression::make_struct_composite_literal(t
, vals
, loc
);
468 Array_type
* at
= t
->array_type();
471 Expression_list
* vals
= new Expression_list();
472 vals
->push_back(pack_direct_iface(at
->element_type(), rhs
, loc
));
473 return Expression::make_array_composite_literal(t
, vals
, loc
);
475 return Expression::make_unsafe_cast(t
, rhs
, loc
);
478 // Return an expression for the type descriptor of RHS.
481 Expression::get_interface_type_descriptor(Expression
* rhs
)
483 go_assert(rhs
->type()->interface_type() != NULL
);
484 Location location
= rhs
->location();
486 // The type descriptor is the first field of an empty interface.
487 if (rhs
->type()->interface_type()->is_empty())
488 return Expression::make_interface_info(rhs
, INTERFACE_INFO_TYPE_DESCRIPTOR
,
492 Expression::make_interface_info(rhs
, INTERFACE_INFO_METHODS
, location
);
494 Expression
* descriptor
=
495 Expression::make_dereference(mtable
, NIL_CHECK_NOT_NEEDED
, location
);
496 descriptor
= Expression::make_field_reference(descriptor
, 0, location
);
497 Expression
* nil
= Expression::make_nil(location
);
500 Expression::make_binary(OPERATOR_EQEQ
, mtable
, nil
, location
);
501 return Expression::make_conditional(eq
, nil
, descriptor
, location
);
504 // Return an expression for the conversion of an interface type to an
508 Expression::convert_interface_to_interface(Type
*lhs_type
, Expression
* rhs
,
512 if (Type::are_identical(lhs_type
, rhs
->type(),
513 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
517 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
518 bool lhs_is_empty
= lhs_interface_type
->is_empty();
520 // In the general case this requires runtime examination of the type
521 // method table to match it up with the interface methods.
523 // FIXME: If all of the methods in the right hand side interface
524 // also appear in the left hand side interface, then we don't need
525 // to do a runtime check, although we still need to build a new
528 // We are going to evaluate RHS multiple times.
529 go_assert(rhs
->is_variable());
531 // Get the type descriptor for the right hand side. This will be
532 // NULL for a nil interface.
533 Expression
* rhs_type_expr
= Expression::get_interface_type_descriptor(rhs
);
534 Expression
* lhs_type_expr
=
535 Expression::make_type_descriptor(lhs_type
, location
);
537 Expression
* first_field
;
540 // A type assertion fails when converting a nil interface.
541 first_field
= Runtime::make_call(Runtime::ASSERTITAB
, location
, 2,
542 lhs_type_expr
, rhs_type_expr
);
544 else if (lhs_is_empty
)
546 // A conversion to an empty interface always succeeds, and the
547 // first field is just the type descriptor of the object.
548 first_field
= rhs_type_expr
;
552 // A conversion to a non-empty interface may fail, but unlike a
553 // type assertion converting nil will always succeed.
554 first_field
= Runtime::make_call(Runtime::REQUIREITAB
, location
, 2,
555 lhs_type_expr
, rhs_type_expr
);
558 // The second field is simply the object pointer.
560 Expression::make_interface_info(rhs
, INTERFACE_INFO_OBJECT
, location
);
561 return Expression::make_interface_value(lhs_type
, first_field
, obj
, location
);
564 // Return an expression for the conversion of an interface type to a
565 // non-interface type.
568 Expression::convert_interface_to_type(Gogo
* gogo
, Type
*lhs_type
, Expression
* rhs
,
571 // We are going to evaluate RHS multiple times.
572 go_assert(rhs
->is_variable());
574 // Build an expression to check that the type is valid. It will
575 // panic with an appropriate runtime type error if the type is not
577 // (lhs_type == rhs_type ? nil /*dummy*/ :
578 // panicdottype(lhs_type, rhs_type, inter_type))
579 // For some Oses, we need to call runtime.eqtype instead of
580 // lhs_type == rhs_type, as we may have unmerged type descriptors
581 // from shared libraries.
582 Expression
* lhs_type_expr
= Expression::make_type_descriptor(lhs_type
,
584 Expression
* rhs_descriptor
=
585 Expression::get_interface_type_descriptor(rhs
);
587 Type
* rhs_type
= rhs
->type();
588 Expression
* rhs_inter_expr
= Expression::make_type_descriptor(rhs_type
,
592 if (gogo
->need_eqtype()) {
593 cond
= Runtime::make_call(Runtime::EQTYPE
, location
,
597 cond
= Expression::make_binary(OPERATOR_EQEQ
, lhs_type_expr
,
598 rhs_descriptor
, location
);
601 rhs_descriptor
= Expression::get_interface_type_descriptor(rhs
);
602 Expression
* panic
= Runtime::make_call(Runtime::PANICDOTTYPE
, location
,
603 3, lhs_type_expr
->copy(),
606 Expression
* nil
= Expression::make_nil(location
);
607 Expression
* check
= Expression::make_conditional(cond
, nil
, panic
,
610 // If the conversion succeeds, pull out the value.
611 Expression
* obj
= Expression::make_interface_info(rhs
, INTERFACE_INFO_OBJECT
,
614 // If the value is a direct interface, then it is the value we want.
615 // Otherwise it points to the value.
616 if (lhs_type
->is_direct_iface_type())
617 obj
= Expression::pack_direct_iface(lhs_type
, obj
, location
);
620 obj
= Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type
), obj
,
622 obj
= Expression::make_dereference(obj
, NIL_CHECK_NOT_NEEDED
,
625 return Expression::make_compound(check
, obj
, location
);
628 // Convert an expression to its backend representation. This is implemented by
629 // the child class. Not that it is not in general safe to call this multiple
630 // times for a single expression, but that we don't catch such errors.
633 Expression::get_backend(Translate_context
* context
)
635 // The child may have marked this expression as having an error.
636 if (this->classification_
== EXPRESSION_ERROR
)
638 go_assert(saw_errors());
639 return context
->backend()->error_expression();
642 return this->do_get_backend(context
);
645 // Return a backend expression for VAL.
647 Expression::backend_numeric_constant_expression(Translate_context
* context
,
648 Numeric_constant
* val
)
650 Gogo
* gogo
= context
->gogo();
651 Type
* type
= val
->type();
653 return gogo
->backend()->error_expression();
655 Btype
* btype
= type
->get_backend(gogo
);
657 if (type
->integer_type() != NULL
)
660 if (!val
->to_int(&ival
))
662 go_assert(saw_errors());
663 return gogo
->backend()->error_expression();
665 ret
= gogo
->backend()->integer_constant_expression(btype
, ival
);
668 else if (type
->float_type() != NULL
)
671 if (!val
->to_float(&fval
))
673 go_assert(saw_errors());
674 return gogo
->backend()->error_expression();
676 ret
= gogo
->backend()->float_constant_expression(btype
, fval
);
679 else if (type
->complex_type() != NULL
)
682 if (!val
->to_complex(&cval
))
684 go_assert(saw_errors());
685 return gogo
->backend()->error_expression();
687 ret
= gogo
->backend()->complex_constant_expression(btype
, cval
);
696 // Insert bounds checks for an index expression. Check that that VAL
697 // >= 0 and that it fits in an int. Then check that VAL OP BOUND is
698 // true. If any condition is false, call one of the CODE runtime
699 // functions, which will panic.
702 Expression::check_bounds(Expression
* val
, Operator op
, Expression
* bound
,
703 Runtime::Function code
,
704 Runtime::Function code_u
,
705 Runtime::Function code_extend
,
706 Runtime::Function code_extend_u
,
707 Statement_inserter
* inserter
,
710 go_assert(val
->is_variable() || val
->is_constant());
711 go_assert(bound
->is_variable() || bound
->is_constant());
713 Type
* int_type
= Type::lookup_integer_type("int");
714 int int_type_size
= int_type
->integer_type()->bits();
716 Type
* val_type
= val
->type();
717 if (val_type
->integer_type() == NULL
)
719 go_assert(saw_errors());
722 int val_type_size
= val_type
->integer_type()->bits();
723 bool val_is_unsigned
= val_type
->integer_type()->is_unsigned();
725 // Check that VAL >= 0.
726 Expression
* check
= NULL
;
727 if (!val_is_unsigned
)
729 Expression
* zero
= Expression::make_integer_ul(0, val_type
, loc
);
730 check
= Expression::make_binary(OPERATOR_GE
, val
->copy(), zero
, loc
);
733 // If VAL's type is larger than int, check that VAL fits in an int.
734 if (val_type_size
> int_type_size
735 || (val_type_size
== int_type_size
739 mpz_init_set_ui(one
, 1UL);
741 // maxval = 2^(int_type_size - 1) - 1
744 mpz_mul_2exp(maxval
, one
, int_type_size
- 1);
745 mpz_sub_ui(maxval
, maxval
, 1);
746 Expression
* max
= Expression::make_integer_z(&maxval
, val_type
, loc
);
750 Expression
* cmp
= Expression::make_binary(OPERATOR_LE
, val
->copy(),
755 check
= Expression::make_binary(OPERATOR_ANDAND
, check
, cmp
, loc
);
758 // For the final check we can assume that VAL fits in an int.
760 if (val_type
== int_type
)
763 ival
= Expression::make_cast(int_type
, val
->copy(), loc
);
765 // BOUND is assumed to fit in an int. Either it comes from len or
766 // cap, or it was checked by an earlier call.
768 if (bound
->type() == int_type
)
769 ibound
= bound
->copy();
771 ibound
= Expression::make_cast(int_type
, bound
->copy(), loc
);
773 Expression
* cmp
= Expression::make_binary(op
, ival
, ibound
, loc
);
777 check
= Expression::make_binary(OPERATOR_ANDAND
, check
, cmp
, loc
);
780 if (val_type_size
> int_type_size
)
795 Expression
* ignore
= Expression::make_boolean(true, loc
);
796 Expression
* crash
= Runtime::make_call(c
, loc
, 2,
797 val
->copy(), bound
->copy());
798 Expression
* cond
= Expression::make_conditional(check
, ignore
, crash
, loc
);
799 inserter
->insert(Statement::make_statement(cond
, true));
803 Expression::dump_expression(Ast_dump_context
* ast_dump_context
) const
805 this->do_dump_expression(ast_dump_context
);
808 // Error expressions. This are used to avoid cascading errors.
810 class Error_expression
: public Expression
813 Error_expression(Location location
)
814 : Expression(EXPRESSION_ERROR
, location
)
819 do_is_constant() const
823 do_numeric_constant_value(Numeric_constant
* nc
) const
825 nc
->set_unsigned_long(NULL
, 0);
830 do_discarding_value()
835 { return Type::make_error_type(); }
838 do_determine_type(const Type_context
*)
846 do_is_addressable() const
850 do_get_backend(Translate_context
* context
)
851 { return context
->backend()->error_expression(); }
854 do_dump_expression(Ast_dump_context
*) const;
857 // Dump the ast representation for an error expression to a dump context.
860 Error_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
862 ast_dump_context
->ostream() << "_Error_" ;
866 Expression::make_error(Location location
)
868 return new Error_expression(location
);
871 // An expression which is really a type. This is used during parsing.
872 // It is an error if these survive after lowering.
875 Type_expression
: public Expression
878 Type_expression(Type
* type
, Location location
)
879 : Expression(EXPRESSION_TYPE
, location
),
885 do_traverse(Traverse
* traverse
)
886 { return Type::traverse(this->type_
, traverse
); }
890 { return this->type_
; }
893 do_determine_type(const Type_context
*)
897 do_check_types(Gogo
*);
904 do_get_backend(Translate_context
*)
905 { go_unreachable(); }
907 void do_dump_expression(Ast_dump_context
*) const;
910 // The type which we are representing as an expression.
915 Type_expression::do_check_types(Gogo
*)
917 if (this->type_
->is_error())
919 go_assert(saw_errors());
920 this->set_is_error();
923 this->report_error(_("invalid use of type"));
927 Type_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
929 ast_dump_context
->dump_type(this->type_
);
933 Expression::make_type(Type
* type
, Location location
)
935 return new Type_expression(type
, location
);
938 // Class Parser_expression.
941 Parser_expression::do_type()
943 // We should never really ask for the type of a Parser_expression.
944 // However, it can happen, at least when we have an invalid const
945 // whose initializer refers to the const itself. In that case we
946 // may ask for the type when lowering the const itself.
947 go_assert(saw_errors());
948 return Type::make_error_type();
951 // Class Var_expression.
953 // Lower a variable expression. Here we just make sure that the
954 // initialization expression of the variable has been lowered. This
955 // ensures that we will be able to determine the type of the variable
959 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
960 Statement_inserter
* inserter
, int)
962 if (this->variable_
->is_variable())
964 Variable
* var
= this->variable_
->var_value();
965 // This is either a local variable or a global variable. A
966 // reference to a variable which is local to an enclosing
967 // function will be a reference to a field in a closure.
968 if (var
->is_global())
973 var
->lower_init_expression(gogo
, function
, inserter
);
978 // Return the type of a reference to a variable.
981 Var_expression::do_type()
983 if (this->variable_
->is_variable())
984 return this->variable_
->var_value()->type();
985 else if (this->variable_
->is_result_variable())
986 return this->variable_
->result_var_value()->type();
991 // Determine the type of a reference to a variable.
994 Var_expression::do_determine_type(const Type_context
*)
996 if (this->variable_
->is_variable())
997 this->variable_
->var_value()->determine_type();
1000 // Something takes the address of this variable. This means that we
1001 // may want to move the variable onto the heap.
1004 Var_expression::do_address_taken(bool escapes
)
1008 if (this->variable_
->is_variable())
1009 this->variable_
->var_value()->set_non_escaping_address_taken();
1010 else if (this->variable_
->is_result_variable())
1011 this->variable_
->result_var_value()->set_non_escaping_address_taken();
1017 if (this->variable_
->is_variable())
1018 this->variable_
->var_value()->set_address_taken();
1019 else if (this->variable_
->is_result_variable())
1020 this->variable_
->result_var_value()->set_address_taken();
1025 if (this->variable_
->is_variable()
1026 && this->variable_
->var_value()->is_in_heap())
1028 Node::make_node(this)->set_encoding(Node::ESCAPE_HEAP
);
1029 Node::make_node(this->variable_
)->set_encoding(Node::ESCAPE_HEAP
);
1033 // Export a reference to a variable.
1036 Var_expression::do_export(Export_function_body
* efb
) const
1038 Named_object
* no
= this->variable_
;
1039 if (no
->is_result_variable() || !no
->var_value()->is_global())
1040 efb
->write_string(Gogo::unpack_hidden_name(no
->name()));
1042 Expression::export_name(efb
, no
);
1045 // Get the backend representation for a reference to a variable.
1048 Var_expression::do_get_backend(Translate_context
* context
)
1050 Bvariable
* bvar
= this->variable_
->get_backend_variable(context
->gogo(),
1051 context
->function());
1053 Location loc
= this->location();
1055 Gogo
* gogo
= context
->gogo();
1056 if (this->variable_
->is_variable())
1058 is_in_heap
= this->variable_
->var_value()->is_in_heap();
1059 btype
= this->variable_
->var_value()->type()->get_backend(gogo
);
1061 else if (this->variable_
->is_result_variable())
1063 is_in_heap
= this->variable_
->result_var_value()->is_in_heap();
1064 btype
= this->variable_
->result_var_value()->type()->get_backend(gogo
);
1070 context
->backend()->var_expression(bvar
, loc
);
1072 ret
= context
->backend()->indirect_expression(btype
, ret
, true, loc
);
1076 // Ast dump for variable expression.
1079 Var_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1081 ast_dump_context
->ostream() << this->variable_
->message_name() ;
1084 // Make a reference to a variable in an expression.
1087 Expression::make_var_reference(Named_object
* var
, Location location
)
1090 return Expression::make_sink(location
);
1092 // FIXME: Creating a new object for each reference to a variable is
1094 return new Var_expression(var
, location
);
1097 // Class Enclosed_var_expression.
1100 Enclosed_var_expression::do_traverse(Traverse
*)
1102 return TRAVERSE_CONTINUE
;
1105 // Lower the reference to the enclosed variable.
1108 Enclosed_var_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
1109 Statement_inserter
* inserter
, int)
1111 gogo
->lower_expression(function
, inserter
, &this->reference_
);
1115 // Flatten the reference to the enclosed variable.
1118 Enclosed_var_expression::do_flatten(Gogo
* gogo
, Named_object
* function
,
1119 Statement_inserter
* inserter
)
1121 gogo
->flatten_expression(function
, inserter
, &this->reference_
);
1126 Enclosed_var_expression::do_address_taken(bool escapes
)
1130 if (this->variable_
->is_variable())
1131 this->variable_
->var_value()->set_non_escaping_address_taken();
1132 else if (this->variable_
->is_result_variable())
1133 this->variable_
->result_var_value()->set_non_escaping_address_taken();
1139 if (this->variable_
->is_variable())
1140 this->variable_
->var_value()->set_address_taken();
1141 else if (this->variable_
->is_result_variable())
1142 this->variable_
->result_var_value()->set_address_taken();
1147 if (this->variable_
->is_variable()
1148 && this->variable_
->var_value()->is_in_heap())
1149 Node::make_node(this->variable_
)->set_encoding(Node::ESCAPE_HEAP
);
1152 // Ast dump for enclosed variable expression.
1155 Enclosed_var_expression::do_dump_expression(Ast_dump_context
* adc
) const
1157 adc
->ostream() << this->variable_
->message_name();
1160 // Make a reference to a variable within an enclosing function.
1163 Expression::make_enclosing_var_reference(Expression
* reference
,
1164 Named_object
* var
, Location location
)
1166 return new Enclosed_var_expression(reference
, var
, location
);
1169 // Class Temporary_reference_expression.
1174 Temporary_reference_expression::do_type()
1176 return this->statement_
->type();
1179 // Called if something takes the address of this temporary variable.
1180 // We never have to move temporary variables to the heap, but we do
1181 // need to know that they must live in the stack rather than in a
1185 Temporary_reference_expression::do_address_taken(bool)
1187 this->statement_
->set_is_address_taken();
1190 // Export a reference to a temporary.
1193 Temporary_reference_expression::do_export(Export_function_body
* efb
) const
1195 unsigned int idx
= efb
->temporary_index(this->statement_
);
1197 snprintf(buf
, sizeof buf
, "$t%u", idx
);
1198 efb
->write_c_string(buf
);
1201 // Import a reference to a temporary.
1204 Temporary_reference_expression::do_import(Import_function_body
* ifb
,
1207 std::string id
= ifb
->read_identifier();
1208 go_assert(id
[0] == '$' && id
[1] == 't');
1209 const char *p
= id
.c_str();
1211 long idx
= strtol(p
+ 2, &end
, 10);
1212 if (*end
!= '\0' || idx
> 0x7fffffff)
1214 if (!ifb
->saw_error())
1216 ("invalid export data for %qs: "
1217 "invalid temporary reference index at %lu"),
1218 ifb
->name().c_str(),
1219 static_cast<unsigned long>(ifb
->off()));
1220 ifb
->set_saw_error();
1221 return Expression::make_error(loc
);
1224 Temporary_statement
* temp
=
1225 ifb
->temporary_statement(static_cast<unsigned int>(idx
));
1228 if (!ifb
->saw_error())
1230 ("invalid export data for %qs: "
1231 "undefined temporary reference index at %lu"),
1232 ifb
->name().c_str(),
1233 static_cast<unsigned long>(ifb
->off()));
1234 ifb
->set_saw_error();
1235 return Expression::make_error(loc
);
1238 return Expression::make_temporary_reference(temp
, loc
);
1241 // Get a backend expression referring to the variable.
1244 Temporary_reference_expression::do_get_backend(Translate_context
* context
)
1246 Gogo
* gogo
= context
->gogo();
1247 Bvariable
* bvar
= this->statement_
->get_backend_variable(context
);
1248 Bexpression
* ret
= gogo
->backend()->var_expression(bvar
, this->location());
1250 // The backend can't always represent the same set of recursive types
1251 // that the Go frontend can. In some cases this means that a
1252 // temporary variable won't have the right backend type. Correct
1253 // that here by adding a type cast. We need to use base() to push
1254 // the circularity down one level.
1255 Type
* stype
= this->statement_
->type();
1256 if (!this->is_lvalue_
1257 && stype
->points_to() != NULL
1258 && stype
->points_to()->is_void_type())
1260 Btype
* btype
= this->type()->base()->get_backend(gogo
);
1261 ret
= gogo
->backend()->convert_expression(btype
, ret
, this->location());
1266 // Ast dump for temporary reference.
1269 Temporary_reference_expression::do_dump_expression(
1270 Ast_dump_context
* ast_dump_context
) const
1272 ast_dump_context
->dump_temp_variable_name(this->statement_
);
1275 // Make a reference to a temporary variable.
1277 Temporary_reference_expression
*
1278 Expression::make_temporary_reference(Temporary_statement
* statement
,
1281 statement
->add_use();
1282 return new Temporary_reference_expression(statement
, location
);
1285 // Class Set_and_use_temporary_expression.
1290 Set_and_use_temporary_expression::do_type()
1292 return this->statement_
->type();
1295 // Determine the type of the expression.
1298 Set_and_use_temporary_expression::do_determine_type(
1299 const Type_context
* context
)
1301 this->expr_
->determine_type(context
);
1304 // Take the address.
1307 Set_and_use_temporary_expression::do_address_taken(bool)
1309 this->statement_
->set_is_address_taken();
1312 // Return the backend representation.
1315 Set_and_use_temporary_expression::do_get_backend(Translate_context
* context
)
1317 Location loc
= this->location();
1318 Gogo
* gogo
= context
->gogo();
1319 Bvariable
* bvar
= this->statement_
->get_backend_variable(context
);
1320 Bexpression
* lvar_ref
= gogo
->backend()->var_expression(bvar
, loc
);
1322 Named_object
* fn
= context
->function();
1323 go_assert(fn
!= NULL
);
1324 Bfunction
* bfn
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
1325 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
1326 Bstatement
* set
= gogo
->backend()->assignment_statement(bfn
, lvar_ref
,
1328 Bexpression
* var_ref
= gogo
->backend()->var_expression(bvar
, loc
);
1329 Bexpression
* ret
= gogo
->backend()->compound_expression(set
, var_ref
, loc
);
1336 Set_and_use_temporary_expression::do_dump_expression(
1337 Ast_dump_context
* ast_dump_context
) const
1339 ast_dump_context
->ostream() << '(';
1340 ast_dump_context
->dump_temp_variable_name(this->statement_
);
1341 ast_dump_context
->ostream() << " = ";
1342 this->expr_
->dump_expression(ast_dump_context
);
1343 ast_dump_context
->ostream() << ')';
1346 // Make a set-and-use temporary.
1348 Set_and_use_temporary_expression
*
1349 Expression::make_set_and_use_temporary(Temporary_statement
* statement
,
1350 Expression
* expr
, Location location
)
1352 return new Set_and_use_temporary_expression(statement
, expr
, location
);
1355 // A sink expression--a use of the blank identifier _.
1357 class Sink_expression
: public Expression
1360 Sink_expression(Location location
)
1361 : Expression(EXPRESSION_SINK
, location
),
1362 type_(NULL
), bvar_(NULL
)
1367 do_discarding_value()
1374 do_determine_type(const Type_context
*);
1378 { return new Sink_expression(this->location()); }
1381 do_get_backend(Translate_context
*);
1384 do_dump_expression(Ast_dump_context
*) const;
1387 // The type of this sink variable.
1389 // The temporary variable we generate.
1393 // Return the type of a sink expression.
1396 Sink_expression::do_type()
1398 if (this->type_
== NULL
)
1399 return Type::make_sink_type();
1403 // Determine the type of a sink expression.
1406 Sink_expression::do_determine_type(const Type_context
* context
)
1408 if (context
->type
!= NULL
)
1409 this->type_
= context
->type
;
1412 // Return a temporary variable for a sink expression. This will
1413 // presumably be a write-only variable which the middle-end will drop.
1416 Sink_expression::do_get_backend(Translate_context
* context
)
1418 Location loc
= this->location();
1419 Gogo
* gogo
= context
->gogo();
1420 if (this->bvar_
== NULL
)
1422 go_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1423 Named_object
* fn
= context
->function();
1424 go_assert(fn
!= NULL
);
1425 Bfunction
* fn_ctx
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
1426 Btype
* bt
= this->type_
->get_backend(context
->gogo());
1429 gogo
->backend()->temporary_variable(fn_ctx
, context
->bblock(), bt
, NULL
,
1431 Bexpression
* var_ref
=
1432 gogo
->backend()->var_expression(this->bvar_
, loc
);
1433 var_ref
= gogo
->backend()->compound_expression(decl
, var_ref
, loc
);
1436 return gogo
->backend()->var_expression(this->bvar_
, loc
);
1439 // Ast dump for sink expression.
1442 Sink_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1444 ast_dump_context
->ostream() << "_" ;
1447 // Make a sink expression.
1450 Expression::make_sink(Location location
)
1452 return new Sink_expression(location
);
1455 // Class Func_expression.
1457 // FIXME: Can a function expression appear in a constant expression?
1458 // The value is unchanging. Initializing a constant to the address of
1459 // a function seems like it could work, though there might be little
1465 Func_expression::do_traverse(Traverse
* traverse
)
1467 return (this->closure_
== NULL
1469 : Expression::traverse(&this->closure_
, traverse
));
1472 // Return the type of a function expression.
1475 Func_expression::do_type()
1477 if (this->function_
->is_function())
1478 return this->function_
->func_value()->type();
1479 else if (this->function_
->is_function_declaration())
1480 return this->function_
->func_declaration_value()->type();
1485 // Get the backend representation for the code of a function expression.
1488 Func_expression::get_code_pointer(Gogo
* gogo
, Named_object
* no
, Location loc
)
1490 Function_type
* fntype
;
1491 if (no
->is_function())
1492 fntype
= no
->func_value()->type();
1493 else if (no
->is_function_declaration())
1494 fntype
= no
->func_declaration_value()->type();
1498 // Builtin functions are handled specially by Call_expression. We
1499 // can't take their address.
1500 if (fntype
->is_builtin())
1503 ("invalid use of special built-in function %qs; "
1505 no
->message_name().c_str());
1506 return gogo
->backend()->error_expression();
1510 if (no
->is_function())
1511 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
);
1512 else if (no
->is_function_declaration())
1513 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
);
1517 return gogo
->backend()->function_code_expression(fndecl
, loc
);
1520 // Get the backend representation for a function expression. This is used when
1521 // we take the address of a function rather than simply calling it. A func
1522 // value is represented as a pointer to a block of memory. The first
1523 // word of that memory is a pointer to the function code. The
1524 // remaining parts of that memory are the addresses of variables that
1525 // the function closes over.
1528 Func_expression::do_get_backend(Translate_context
* context
)
1530 // If there is no closure, just use the function descriptor.
1531 if (this->closure_
== NULL
)
1533 Gogo
* gogo
= context
->gogo();
1534 Named_object
* no
= this->function_
;
1535 Expression
* descriptor
;
1536 if (no
->is_function())
1537 descriptor
= no
->func_value()->descriptor(gogo
, no
);
1538 else if (no
->is_function_declaration())
1540 if (no
->func_declaration_value()->type()->is_builtin())
1542 go_error_at(this->location(),
1543 ("invalid use of special built-in function %qs; "
1545 no
->message_name().c_str());
1546 return gogo
->backend()->error_expression();
1548 descriptor
= no
->func_declaration_value()->descriptor(gogo
, no
);
1553 Bexpression
* bdesc
= descriptor
->get_backend(context
);
1554 return gogo
->backend()->address_expression(bdesc
, this->location());
1557 go_assert(this->function_
->func_value()->enclosing() != NULL
);
1559 // If there is a closure, then the closure is itself the function
1560 // expression. It is a pointer to a struct whose first field points
1561 // to the function code and whose remaining fields are the addresses
1562 // of the closed-over variables.
1563 Bexpression
*bexpr
= this->closure_
->get_backend(context
);
1565 // Introduce a backend type conversion, to account for any differences
1566 // between the argument type (function descriptor, struct with a
1567 // single field) and the closure (struct with multiple fields).
1568 Gogo
* gogo
= context
->gogo();
1569 Btype
*btype
= this->type()->get_backend(gogo
);
1570 return gogo
->backend()->convert_expression(btype
, bexpr
, this->location());
1573 // The cost of inlining a function reference.
1576 Func_expression::do_inlining_cost() const
1578 // FIXME: We don't inline references to nested functions.
1579 if (this->closure_
!= NULL
)
1581 if (this->function_
->is_function()
1582 && this->function_
->func_value()->enclosing() != NULL
)
1588 // Export a reference to a function.
1591 Func_expression::do_export(Export_function_body
* efb
) const
1593 Expression::export_name(efb
, this->function_
);
1596 // Ast dump for function.
1599 Func_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1601 ast_dump_context
->ostream() << this->function_
->name();
1602 if (this->closure_
!= NULL
)
1604 ast_dump_context
->ostream() << " {closure = ";
1605 this->closure_
->dump_expression(ast_dump_context
);
1606 ast_dump_context
->ostream() << "}";
1610 // Make a reference to a function in an expression.
1613 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1616 Func_expression
* fe
= new Func_expression(function
, closure
, location
);
1618 // Detect references to builtin functions and set the runtime code if
1620 if (function
->is_function_declaration())
1621 fe
->set_runtime_code(Runtime::name_to_code(function
->name()));
1625 // Class Func_descriptor_expression.
1629 Func_descriptor_expression::Func_descriptor_expression(Named_object
* fn
)
1630 : Expression(EXPRESSION_FUNC_DESCRIPTOR
, fn
->location()),
1631 fn_(fn
), dvar_(NULL
)
1633 go_assert(!fn
->is_function() || !fn
->func_value()->needs_closure());
1639 Func_descriptor_expression::do_traverse(Traverse
*)
1641 return TRAVERSE_CONTINUE
;
1644 // All function descriptors have the same type.
1646 Type
* Func_descriptor_expression::descriptor_type
;
1649 Func_descriptor_expression::make_func_descriptor_type()
1651 if (Func_descriptor_expression::descriptor_type
!= NULL
)
1653 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1654 Type
* struct_type
= Type::make_builtin_struct_type(1, "fn", uintptr_type
);
1655 Func_descriptor_expression::descriptor_type
=
1656 Type::make_builtin_named_type("functionDescriptor", struct_type
);
1660 Func_descriptor_expression::do_type()
1662 Func_descriptor_expression::make_func_descriptor_type();
1663 return Func_descriptor_expression::descriptor_type
;
1666 // The backend representation for a function descriptor.
1669 Func_descriptor_expression::do_get_backend(Translate_context
* context
)
1671 Named_object
* no
= this->fn_
;
1672 Location loc
= no
->location();
1673 if (this->dvar_
!= NULL
)
1674 return context
->backend()->var_expression(this->dvar_
, loc
);
1676 Gogo
* gogo
= context
->gogo();
1678 gogo
->function_descriptor_backend_name(no
, &bname
);
1679 bool is_descriptor
= false;
1680 if (no
->is_function_declaration()
1681 && !no
->func_declaration_value()->asm_name().empty()
1682 && Linemap::is_predeclared_location(no
->location()))
1683 is_descriptor
= true;
1685 // The runtime package implements some functions defined in the
1686 // syscall package. Let the syscall package define the descriptor
1688 if (gogo
->compiling_runtime()
1689 && gogo
->package_name() == "runtime"
1690 && no
->is_function()
1691 && !no
->func_value()->asm_name().empty()
1692 && no
->func_value()->asm_name().compare(0, 8, "syscall.") == 0)
1693 is_descriptor
= true;
1695 Btype
* btype
= this->type()->get_backend(gogo
);
1698 if (no
->package() != NULL
|| is_descriptor
)
1700 context
->backend()->immutable_struct_reference(bname
.name(),
1701 bname
.optional_asm_name(),
1705 Location bloc
= Linemap::predeclared_location();
1707 // The runtime package has hash/equality functions that are
1708 // referenced by type descriptors outside of the runtime, so the
1709 // function descriptors must be visible even though they are not
1711 bool is_exported_runtime
= false;
1712 if (gogo
->compiling_runtime()
1713 && gogo
->package_name() == "runtime"
1714 && (no
->name().find("hash") != std::string::npos
1715 || no
->name().find("equal") != std::string::npos
))
1716 is_exported_runtime
= true;
1718 bool is_hidden
= ((no
->is_function()
1719 && no
->func_value()->enclosing() != NULL
)
1720 || (Gogo::is_hidden_name(no
->name())
1721 && !is_exported_runtime
)
1722 || Gogo::is_thunk(no
));
1724 if (no
->is_function() && no
->func_value()->is_referenced_by_inline())
1727 bvar
= context
->backend()->immutable_struct(bname
.name(),
1728 bname
.optional_asm_name(),
1731 Expression_list
* vals
= new Expression_list();
1732 vals
->push_back(Expression::make_func_code_reference(this->fn_
, bloc
));
1734 Expression::make_struct_composite_literal(this->type(), vals
, bloc
);
1735 Translate_context
bcontext(gogo
, NULL
, NULL
, NULL
);
1736 bcontext
.set_is_const();
1737 Bexpression
* binit
= init
->get_backend(&bcontext
);
1738 context
->backend()->immutable_struct_set_init(bvar
, bname
.name(),
1739 is_hidden
, false, btype
,
1744 return gogo
->backend()->var_expression(bvar
, loc
);
1747 // Print a function descriptor expression.
1750 Func_descriptor_expression::do_dump_expression(Ast_dump_context
* context
) const
1752 context
->ostream() << "[descriptor " << this->fn_
->name() << "]";
1755 // Make a function descriptor expression.
1757 Func_descriptor_expression
*
1758 Expression::make_func_descriptor(Named_object
* fn
)
1760 return new Func_descriptor_expression(fn
);
1763 // Make the function descriptor type, so that it can be converted.
1766 Expression::make_func_descriptor_type()
1768 Func_descriptor_expression::make_func_descriptor_type();
1771 // A reference to just the code of a function.
1773 class Func_code_reference_expression
: public Expression
1776 Func_code_reference_expression(Named_object
* function
, Location location
)
1777 : Expression(EXPRESSION_FUNC_CODE_REFERENCE
, location
),
1783 do_traverse(Traverse
*)
1784 { return TRAVERSE_CONTINUE
; }
1787 do_is_static_initializer() const
1792 { return Type::make_pointer_type(Type::make_void_type()); }
1795 do_determine_type(const Type_context
*)
1801 return Expression::make_func_code_reference(this->function_
,
1806 do_get_backend(Translate_context
*);
1809 do_dump_expression(Ast_dump_context
* context
) const
1810 { context
->ostream() << "[raw " << this->function_
->name() << "]" ; }
1814 Named_object
* function_
;
1817 // Get the backend representation for a reference to function code.
1820 Func_code_reference_expression::do_get_backend(Translate_context
* context
)
1822 return Func_expression::get_code_pointer(context
->gogo(), this->function_
,
1826 // Make a reference to the code of a function.
1829 Expression::make_func_code_reference(Named_object
* function
, Location location
)
1831 return new Func_code_reference_expression(function
, location
);
1834 // Class Unknown_expression.
1836 // Return the name of an unknown expression.
1839 Unknown_expression::name() const
1841 return this->named_object_
->name();
1844 // Lower a reference to an unknown name.
1847 Unknown_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
1849 Location location
= this->location();
1850 Named_object
* no
= this->named_object_
;
1852 if (!no
->is_unknown())
1856 real
= no
->unknown_value()->real_named_object();
1859 if (!this->no_error_message_
)
1860 go_error_at(location
, "reference to undefined name %qs",
1861 this->named_object_
->message_name().c_str());
1862 return Expression::make_error(location
);
1865 switch (real
->classification())
1867 case Named_object::NAMED_OBJECT_CONST
:
1868 return Expression::make_const_reference(real
, location
);
1869 case Named_object::NAMED_OBJECT_TYPE
:
1870 return Expression::make_type(real
->type_value(), location
);
1871 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1872 if (!this->no_error_message_
)
1873 go_error_at(location
, "reference to undefined type %qs",
1874 real
->message_name().c_str());
1875 return Expression::make_error(location
);
1876 case Named_object::NAMED_OBJECT_VAR
:
1877 real
->var_value()->set_is_used();
1878 return Expression::make_var_reference(real
, location
);
1879 case Named_object::NAMED_OBJECT_FUNC
:
1880 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1881 return Expression::make_func_reference(real
, NULL
, location
);
1882 case Named_object::NAMED_OBJECT_PACKAGE
:
1883 if (!this->no_error_message_
)
1884 go_error_at(location
, "unexpected reference to package");
1885 return Expression::make_error(location
);
1891 // Dump the ast representation for an unknown expression to a dump context.
1894 Unknown_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1896 ast_dump_context
->ostream() << "_Unknown_(" << this->named_object_
->name()
1900 // Make a reference to an unknown name.
1903 Expression::make_unknown_reference(Named_object
* no
, Location location
)
1905 return new Unknown_expression(no
, location
);
1908 // Start exporting a type conversion for a constant, if needed. This
1909 // returns whether we need to export a closing parenthesis.
1912 Expression::export_constant_type(Export_function_body
* efb
, Type
* type
)
1915 || type
->is_abstract()
1916 || type
== efb
->type_context())
1918 efb
->write_c_string("$convert(");
1919 efb
->write_type(type
);
1920 efb
->write_c_string(", ");
1924 // Finish a type conversion for a constant.
1927 Expression::finish_export_constant_type(Export_function_body
* efb
, bool needed
)
1930 efb
->write_c_string(")");
1933 // A boolean expression.
1935 class Boolean_expression
: public Expression
1938 Boolean_expression(bool val
, Location location
)
1939 : Expression(EXPRESSION_BOOLEAN
, location
),
1940 val_(val
), type_(NULL
)
1944 do_import(Import_expression
*, Location
);
1948 do_traverse(Traverse
*);
1951 do_is_constant() const
1955 do_is_zero_value() const
1956 { return this->val_
== false; }
1959 do_boolean_constant_value(bool* val
) const
1966 do_is_static_initializer() const
1973 do_determine_type(const Type_context
*);
1980 do_get_backend(Translate_context
* context
)
1981 { return context
->backend()->boolean_constant_expression(this->val_
); }
1984 do_inlining_cost() const
1988 do_export(Export_function_body
* efb
) const;
1991 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1992 { ast_dump_context
->ostream() << (this->val_
? "true" : "false"); }
1997 // The type as determined by context.
2001 // Traverse a boolean expression. We just need to traverse the type
2005 Boolean_expression::do_traverse(Traverse
* traverse
)
2007 if (this->type_
!= NULL
)
2008 return Type::traverse(this->type_
, traverse
);
2009 return TRAVERSE_CONTINUE
;
2015 Boolean_expression::do_type()
2017 if (this->type_
== NULL
)
2018 this->type_
= Type::make_boolean_type();
2022 // Set the type from the context.
2025 Boolean_expression::do_determine_type(const Type_context
* context
)
2027 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2029 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
2030 this->type_
= context
->type
;
2031 else if (!context
->may_be_abstract
)
2032 this->type_
= Type::lookup_bool_type();
2035 // Export a boolean constant.
2038 Boolean_expression::do_export(Export_function_body
* efb
) const
2040 bool exported_type
= Expression::export_constant_type(efb
, this->type_
);
2041 efb
->write_c_string(this->val_
? "$true" : "$false");
2042 Expression::finish_export_constant_type(efb
, exported_type
);
2045 // Import a boolean constant.
2048 Boolean_expression::do_import(Import_expression
* imp
, Location loc
)
2050 if (imp
->version() >= EXPORT_FORMAT_V3
)
2051 imp
->require_c_string("$");
2052 if (imp
->peek_char() == 't')
2054 imp
->require_c_string("true");
2055 return Expression::make_boolean(true, loc
);
2059 imp
->require_c_string("false");
2060 return Expression::make_boolean(false, loc
);
2064 // Make a boolean expression.
2067 Expression::make_boolean(bool val
, Location location
)
2069 return new Boolean_expression(val
, location
);
2072 // Class String_expression.
2074 // Traverse a string expression. We just need to traverse the type
2078 String_expression::do_traverse(Traverse
* traverse
)
2080 if (this->type_
!= NULL
)
2081 return Type::traverse(this->type_
, traverse
);
2082 return TRAVERSE_CONTINUE
;
2088 String_expression::do_type()
2090 if (this->type_
== NULL
)
2091 this->type_
= Type::make_string_type();
2095 // Set the type from the context.
2098 String_expression::do_determine_type(const Type_context
* context
)
2100 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2102 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
2103 this->type_
= context
->type
;
2104 else if (!context
->may_be_abstract
)
2105 this->type_
= Type::lookup_string_type();
2108 // Build a string constant.
2111 String_expression::do_get_backend(Translate_context
* context
)
2113 Gogo
* gogo
= context
->gogo();
2114 Btype
* btype
= Type::make_string_type()->get_backend(gogo
);
2116 Location loc
= this->location();
2117 std::vector
<Bexpression
*> init(2);
2118 Bexpression
* str_cst
=
2119 gogo
->backend()->string_constant_expression(this->val_
);
2120 init
[0] = gogo
->backend()->address_expression(str_cst
, loc
);
2122 Btype
* int_btype
= Type::lookup_integer_type("int")->get_backend(gogo
);
2124 mpz_init_set_ui(lenval
, this->val_
.length());
2125 init
[1] = gogo
->backend()->integer_constant_expression(int_btype
, lenval
);
2128 return gogo
->backend()->constructor_expression(btype
, init
, loc
);
2131 // Write string literal to string dump.
2134 String_expression::export_string(String_dump
* exp
,
2135 const String_expression
* str
)
2138 s
.reserve(str
->val_
.length() * 4 + 2);
2140 for (std::string::const_iterator p
= str
->val_
.begin();
2141 p
!= str
->val_
.end();
2144 if (*p
== '\\' || *p
== '"')
2149 else if (*p
>= 0x20 && *p
< 0x7f)
2151 else if (*p
== '\n')
2153 else if (*p
== '\t')
2158 unsigned char c
= *p
;
2159 unsigned int dig
= c
>> 4;
2160 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
2162 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
2166 exp
->write_string(s
);
2169 // Export a string expression.
2172 String_expression::do_export(Export_function_body
* efb
) const
2174 bool exported_type
= Expression::export_constant_type(efb
, this->type_
);
2175 String_expression::export_string(efb
, this);
2176 Expression::finish_export_constant_type(efb
, exported_type
);
2179 // Import a string expression.
2182 String_expression::do_import(Import_expression
* imp
, Location loc
)
2184 imp
->require_c_string("\"");
2188 int c
= imp
->get_char();
2189 if (c
== '"' || c
== -1)
2192 val
+= static_cast<char>(c
);
2195 c
= imp
->get_char();
2196 if (c
== '\\' || c
== '"')
2197 val
+= static_cast<char>(c
);
2204 c
= imp
->get_char();
2205 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
2206 c
= imp
->get_char();
2207 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
2208 char v
= (vh
<< 4) | vl
;
2213 go_error_at(imp
->location(), "bad string constant");
2214 return Expression::make_error(loc
);
2218 return Expression::make_string(val
, loc
);
2221 // Ast dump for string expression.
2224 String_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2226 String_expression::export_string(ast_dump_context
, this);
2229 // Make a string expression with abstract string type (common case).
2232 Expression::make_string(const std::string
& val
, Location location
)
2234 return new String_expression(val
, NULL
, location
);
2237 // Make a string expression with a specific string type.
2240 Expression::make_string_typed(const std::string
& val
, Type
* type
, Location location
)
2242 return new String_expression(val
, type
, location
);
2245 // An expression that evaluates to some characteristic of a string.
2246 // This is used when indexing, bound-checking, or nil checking a string.
2248 class String_info_expression
: public Expression
2251 String_info_expression(Expression
* string
, String_info string_info
,
2253 : Expression(EXPRESSION_STRING_INFO
, location
),
2254 string_(string
), string_info_(string_info
)
2262 do_determine_type(const Type_context
*)
2263 { go_unreachable(); }
2268 return new String_info_expression(this->string_
->copy(), this->string_info_
,
2273 do_get_backend(Translate_context
* context
);
2276 do_dump_expression(Ast_dump_context
*) const;
2279 do_issue_nil_check()
2280 { this->string_
->issue_nil_check(); }
2283 // The string for which we are getting information.
2284 Expression
* string_
;
2285 // What information we want.
2286 String_info string_info_
;
2289 // Return the type of the string info.
2292 String_info_expression::do_type()
2294 switch (this->string_info_
)
2296 case STRING_INFO_DATA
:
2298 Type
* byte_type
= Type::lookup_integer_type("uint8");
2299 return Type::make_pointer_type(byte_type
);
2301 case STRING_INFO_LENGTH
:
2302 return Type::lookup_integer_type("int");
2308 // Return string information in GENERIC.
2311 String_info_expression::do_get_backend(Translate_context
* context
)
2313 Gogo
* gogo
= context
->gogo();
2315 Bexpression
* bstring
= this->string_
->get_backend(context
);
2316 switch (this->string_info_
)
2318 case STRING_INFO_DATA
:
2319 case STRING_INFO_LENGTH
:
2320 return gogo
->backend()->struct_field_expression(bstring
,
2329 // Dump ast representation for a type info expression.
2332 String_info_expression::do_dump_expression(
2333 Ast_dump_context
* ast_dump_context
) const
2335 ast_dump_context
->ostream() << "stringinfo(";
2336 this->string_
->dump_expression(ast_dump_context
);
2337 ast_dump_context
->ostream() << ",";
2338 ast_dump_context
->ostream() <<
2339 (this->string_info_
== STRING_INFO_DATA
? "data"
2340 : this->string_info_
== STRING_INFO_LENGTH
? "length"
2342 ast_dump_context
->ostream() << ")";
2345 // Make a string info expression.
2348 Expression::make_string_info(Expression
* string
, String_info string_info
,
2351 return new String_info_expression(string
, string_info
, location
);
2354 // An expression that represents an string value: a struct with value pointer
2355 // and length fields.
2357 class String_value_expression
: public Expression
2360 String_value_expression(Expression
* valptr
, Expression
* len
, Location location
)
2361 : Expression(EXPRESSION_STRING_VALUE
, location
),
2362 valptr_(valptr
), len_(len
)
2367 do_traverse(Traverse
*);
2371 { return Type::make_string_type(); }
2374 do_determine_type(const Type_context
*)
2375 { go_unreachable(); }
2380 return new String_value_expression(this->valptr_
->copy(),
2386 do_get_backend(Translate_context
* context
);
2389 do_dump_expression(Ast_dump_context
*) const;
2392 // The value pointer.
2393 Expression
* valptr_
;
2399 String_value_expression::do_traverse(Traverse
* traverse
)
2401 if (Expression::traverse(&this->valptr_
, traverse
) == TRAVERSE_EXIT
2402 || Expression::traverse(&this->len_
, traverse
) == TRAVERSE_EXIT
)
2403 return TRAVERSE_EXIT
;
2404 return TRAVERSE_CONTINUE
;
2408 String_value_expression::do_get_backend(Translate_context
* context
)
2410 std::vector
<Bexpression
*> vals(2);
2411 vals
[0] = this->valptr_
->get_backend(context
);
2412 vals
[1] = this->len_
->get_backend(context
);
2414 Gogo
* gogo
= context
->gogo();
2415 Btype
* btype
= Type::make_string_type()->get_backend(gogo
);
2416 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
2420 String_value_expression::do_dump_expression(
2421 Ast_dump_context
* ast_dump_context
) const
2423 ast_dump_context
->ostream() << "stringvalue(";
2424 ast_dump_context
->ostream() << "value: ";
2425 this->valptr_
->dump_expression(ast_dump_context
);
2426 ast_dump_context
->ostream() << ", length: ";
2427 this->len_
->dump_expression(ast_dump_context
);
2428 ast_dump_context
->ostream() << ")";
2432 Expression::make_string_value(Expression
* valptr
, Expression
* len
,
2435 return new String_value_expression(valptr
, len
, location
);
2438 // Make an integer expression.
2440 class Integer_expression
: public Expression
2443 Integer_expression(const mpz_t
* val
, Type
* type
, bool is_character_constant
,
2445 : Expression(EXPRESSION_INTEGER
, location
),
2446 type_(type
), is_character_constant_(is_character_constant
)
2447 { mpz_init_set(this->val_
, *val
); }
2450 do_import(Import_expression
*, Location
);
2452 // Write VAL to string dump.
2454 export_integer(String_dump
* exp
, const mpz_t val
);
2456 // Write VAL to dump context.
2458 dump_integer(Ast_dump_context
* ast_dump_context
, const mpz_t val
);
2462 do_traverse(Traverse
*);
2465 do_is_constant() const
2469 do_is_zero_value() const
2470 { return mpz_sgn(this->val_
) == 0; }
2473 do_is_static_initializer() const
2477 do_numeric_constant_value(Numeric_constant
* nc
) const;
2483 do_determine_type(const Type_context
* context
);
2486 do_check_types(Gogo
*);
2489 do_get_backend(Translate_context
*);
2494 if (this->is_character_constant_
)
2495 return Expression::make_character(&this->val_
,
2496 (this->type_
== NULL
2498 : this->type_
->copy_expressions()),
2501 return Expression::make_integer_z(&this->val_
,
2502 (this->type_
== NULL
2504 : this->type_
->copy_expressions()),
2509 do_inlining_cost() const
2513 do_export(Export_function_body
*) const;
2516 do_dump_expression(Ast_dump_context
*) const;
2519 // The integer value.
2523 // Whether this is a character constant.
2524 bool is_character_constant_
;
2527 // Traverse an integer expression. We just need to traverse the type
2531 Integer_expression::do_traverse(Traverse
* traverse
)
2533 if (this->type_
!= NULL
)
2534 return Type::traverse(this->type_
, traverse
);
2535 return TRAVERSE_CONTINUE
;
2538 // Return a numeric constant for this expression. We have to mark
2539 // this as a character when appropriate.
2542 Integer_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
2544 if (this->is_character_constant_
)
2545 nc
->set_rune(this->type_
, this->val_
);
2547 nc
->set_int(this->type_
, this->val_
);
2551 // Return the current type. If we haven't set the type yet, we return
2552 // an abstract integer type.
2555 Integer_expression::do_type()
2557 if (this->type_
== NULL
)
2559 if (this->is_character_constant_
)
2560 this->type_
= Type::make_abstract_character_type();
2562 this->type_
= Type::make_abstract_integer_type();
2567 // Set the type of the integer value. Here we may switch from an
2568 // abstract type to a real type.
2571 Integer_expression::do_determine_type(const Type_context
* context
)
2573 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2575 else if (context
->type
!= NULL
&& context
->type
->is_numeric_type())
2576 this->type_
= context
->type
;
2577 else if (!context
->may_be_abstract
)
2579 if (this->is_character_constant_
)
2580 this->type_
= Type::lookup_integer_type("int32");
2582 this->type_
= Type::lookup_integer_type("int");
2586 // Check the type of an integer constant.
2589 Integer_expression::do_check_types(Gogo
*)
2591 Type
* type
= this->type_
;
2594 Numeric_constant nc
;
2595 if (this->is_character_constant_
)
2596 nc
.set_rune(NULL
, this->val_
);
2598 nc
.set_int(NULL
, this->val_
);
2599 if (!nc
.set_type(type
, true, this->location()))
2600 this->set_is_error();
2603 // Get the backend representation for an integer constant.
2606 Integer_expression::do_get_backend(Translate_context
* context
)
2608 if (this->is_error_expression()
2609 || (this->type_
!= NULL
&& this->type_
->is_error_type()))
2611 go_assert(saw_errors());
2612 return context
->gogo()->backend()->error_expression();
2615 Type
* resolved_type
= NULL
;
2616 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2617 resolved_type
= this->type_
;
2618 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
2620 // We are converting to an abstract floating point type.
2621 resolved_type
= Type::lookup_float_type("float64");
2623 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
2625 // We are converting to an abstract complex type.
2626 resolved_type
= Type::lookup_complex_type("complex128");
2630 // If we still have an abstract type here, then this is being
2631 // used in a constant expression which didn't get reduced for
2632 // some reason. Use a type which will fit the value. We use <,
2633 // not <=, because we need an extra bit for the sign bit.
2634 int bits
= mpz_sizeinbase(this->val_
, 2);
2635 Type
* int_type
= Type::lookup_integer_type("int");
2636 if (bits
< int_type
->integer_type()->bits())
2637 resolved_type
= int_type
;
2639 resolved_type
= Type::lookup_integer_type("int64");
2643 go_error_at(this->location(),
2644 "unknown type for large integer constant");
2645 return context
->gogo()->backend()->error_expression();
2648 Numeric_constant nc
;
2649 nc
.set_int(resolved_type
, this->val_
);
2650 return Expression::backend_numeric_constant_expression(context
, &nc
);
2653 // Write VAL to export data.
2656 Integer_expression::export_integer(String_dump
* exp
, const mpz_t val
)
2658 char* s
= mpz_get_str(NULL
, 10, val
);
2659 exp
->write_c_string(s
);
2663 // Export an integer in a constant expression.
2666 Integer_expression::do_export(Export_function_body
* efb
) const
2668 bool exported_type
= Expression::export_constant_type(efb
, this->type_
);
2670 Integer_expression::export_integer(efb
, this->val_
);
2671 if (this->is_character_constant_
)
2672 efb
->write_c_string("'");
2673 // A trailing space lets us reliably identify the end of the number.
2674 efb
->write_c_string(" ");
2676 Expression::finish_export_constant_type(efb
, exported_type
);
2679 // Import an integer, floating point, or complex value. This handles
2680 // all these types because they all start with digits.
2683 Integer_expression::do_import(Import_expression
* imp
, Location loc
)
2685 std::string num
= imp
->read_identifier();
2686 imp
->require_c_string(" ");
2687 if (!num
.empty() && num
[num
.length() - 1] == 'i')
2690 size_t plus_pos
= num
.find('+', 1);
2691 size_t minus_pos
= num
.find('-', 1);
2693 if (plus_pos
== std::string::npos
)
2695 else if (minus_pos
== std::string::npos
)
2699 go_error_at(imp
->location(), "bad number in import data: %qs",
2701 return Expression::make_error(loc
);
2703 if (pos
== std::string::npos
)
2704 mpfr_set_ui(real
, 0, MPFR_RNDN
);
2707 std::string real_str
= num
.substr(0, pos
);
2708 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, MPFR_RNDN
) != 0)
2710 go_error_at(imp
->location(), "bad number in import data: %qs",
2712 return Expression::make_error(loc
);
2716 std::string imag_str
;
2717 if (pos
== std::string::npos
)
2720 imag_str
= num
.substr(pos
);
2721 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
2723 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, MPFR_RNDN
) != 0)
2725 go_error_at(imp
->location(), "bad number in import data: %qs",
2727 return Expression::make_error(loc
);
2730 mpc_init2(cval
, mpc_precision
);
2731 mpc_set_fr_fr(cval
, real
, imag
, MPC_RNDNN
);
2734 Expression
* ret
= Expression::make_complex(&cval
, NULL
, loc
);
2738 else if (num
.find('.') == std::string::npos
2739 && num
.find('E') == std::string::npos
)
2741 bool is_character_constant
= (!num
.empty()
2742 && num
[num
.length() - 1] == '\'');
2743 if (is_character_constant
)
2744 num
= num
.substr(0, num
.length() - 1);
2746 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
2748 go_error_at(imp
->location(), "bad number in import data: %qs",
2750 return Expression::make_error(loc
);
2753 if (is_character_constant
)
2754 ret
= Expression::make_character(&val
, NULL
, loc
);
2756 ret
= Expression::make_integer_z(&val
, NULL
, loc
);
2763 if (mpfr_init_set_str(val
, num
.c_str(), 10, MPFR_RNDN
) != 0)
2765 go_error_at(imp
->location(), "bad number in import data: %qs",
2767 return Expression::make_error(loc
);
2769 Expression
* ret
= Expression::make_float(&val
, NULL
, loc
);
2774 // Ast dump for integer expression.
2777 Integer_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2779 if (this->is_character_constant_
)
2780 ast_dump_context
->ostream() << '\'';
2781 Integer_expression::export_integer(ast_dump_context
, this->val_
);
2782 if (this->is_character_constant_
)
2783 ast_dump_context
->ostream() << '\'';
2786 // Build a new integer value from a multi-precision integer.
2789 Expression::make_integer_z(const mpz_t
* val
, Type
* type
, Location location
)
2791 return new Integer_expression(val
, type
, false, location
);
2794 // Build a new integer value from an unsigned long.
2797 Expression::make_integer_ul(unsigned long val
, Type
*type
, Location location
)
2800 mpz_init_set_ui(zval
, val
);
2801 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2806 // Build a new integer value from a signed long.
2809 Expression::make_integer_sl(long val
, Type
*type
, Location location
)
2812 mpz_init_set_si(zval
, val
);
2813 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2818 // Store an int64_t in an uninitialized mpz_t.
2821 set_mpz_from_int64(mpz_t
* zval
, int64_t val
)
2825 unsigned long ul
= static_cast<unsigned long>(val
);
2826 if (static_cast<int64_t>(ul
) == val
)
2828 mpz_init_set_ui(*zval
, ul
);
2834 uv
= static_cast<uint64_t>(val
);
2836 uv
= static_cast<uint64_t>(- val
);
2837 unsigned long ul
= uv
& 0xffffffffUL
;
2838 mpz_init_set_ui(*zval
, ul
);
2840 mpz_init_set_ui(hval
, static_cast<unsigned long>(uv
>> 32));
2841 mpz_mul_2exp(hval
, hval
, 32);
2842 mpz_add(*zval
, *zval
, hval
);
2845 mpz_neg(*zval
, *zval
);
2848 // Build a new integer value from an int64_t.
2851 Expression::make_integer_int64(int64_t val
, Type
* type
, Location location
)
2854 set_mpz_from_int64(&zval
, val
);
2855 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2860 // Build a new character constant value.
2863 Expression::make_character(const mpz_t
* val
, Type
* type
, Location location
)
2865 return new Integer_expression(val
, type
, true, location
);
2870 class Float_expression
: public Expression
2873 Float_expression(const mpfr_t
* val
, Type
* type
, Location location
)
2874 : Expression(EXPRESSION_FLOAT
, location
),
2877 mpfr_init_set(this->val_
, *val
, MPFR_RNDN
);
2880 // Write VAL to export data.
2882 export_float(String_dump
* exp
, const mpfr_t val
);
2884 // Write VAL to dump file.
2886 dump_float(Ast_dump_context
* ast_dump_context
, const mpfr_t val
);
2890 do_traverse(Traverse
*);
2893 do_is_constant() const
2897 do_is_zero_value() const
2899 return mpfr_zero_p(this->val_
) != 0
2900 && mpfr_signbit(this->val_
) == 0;
2904 do_is_static_initializer() const
2908 do_numeric_constant_value(Numeric_constant
* nc
) const
2910 nc
->set_float(this->type_
, this->val_
);
2918 do_determine_type(const Type_context
*);
2921 do_check_types(Gogo
*);
2925 { return Expression::make_float(&this->val_
,
2926 (this->type_
== NULL
2928 : this->type_
->copy_expressions()),
2929 this->location()); }
2932 do_get_backend(Translate_context
*);
2935 do_inlining_cost() const
2939 do_export(Export_function_body
*) const;
2942 do_dump_expression(Ast_dump_context
*) const;
2945 // The floating point value.
2951 // Traverse a float expression. We just need to traverse the type if
2955 Float_expression::do_traverse(Traverse
* traverse
)
2957 if (this->type_
!= NULL
)
2958 return Type::traverse(this->type_
, traverse
);
2959 return TRAVERSE_CONTINUE
;
2962 // Return the current type. If we haven't set the type yet, we return
2963 // an abstract float type.
2966 Float_expression::do_type()
2968 if (this->type_
== NULL
)
2969 this->type_
= Type::make_abstract_float_type();
2973 // Set the type of the float value. Here we may switch from an
2974 // abstract type to a real type.
2977 Float_expression::do_determine_type(const Type_context
* context
)
2979 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2981 else if (context
->type
!= NULL
2982 && (context
->type
->integer_type() != NULL
2983 || context
->type
->float_type() != NULL
2984 || context
->type
->complex_type() != NULL
))
2985 this->type_
= context
->type
;
2986 else if (!context
->may_be_abstract
)
2987 this->type_
= Type::lookup_float_type("float64");
2990 // Check the type of a float value.
2993 Float_expression::do_check_types(Gogo
*)
2995 Type
* type
= this->type_
;
2998 Numeric_constant nc
;
2999 nc
.set_float(NULL
, this->val_
);
3000 if (!nc
.set_type(this->type_
, true, this->location()))
3001 this->set_is_error();
3004 // Get the backend representation for a float constant.
3007 Float_expression::do_get_backend(Translate_context
* context
)
3009 if (this->is_error_expression()
3010 || (this->type_
!= NULL
&& this->type_
->is_error_type()))
3012 go_assert(saw_errors());
3013 return context
->gogo()->backend()->error_expression();
3016 Type
* resolved_type
;
3017 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
3018 resolved_type
= this->type_
;
3019 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
3021 // We have an abstract integer type. We just hope for the best.
3022 resolved_type
= Type::lookup_integer_type("int");
3024 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
3026 // We are converting to an abstract complex type.
3027 resolved_type
= Type::lookup_complex_type("complex128");
3031 // If we still have an abstract type here, then this is being
3032 // used in a constant expression which didn't get reduced. We
3033 // just use float64 and hope for the best.
3034 resolved_type
= Type::lookup_float_type("float64");
3037 Numeric_constant nc
;
3038 nc
.set_float(resolved_type
, this->val_
);
3039 return Expression::backend_numeric_constant_expression(context
, &nc
);
3042 // Write a floating point number to a string dump.
3045 Float_expression::export_float(String_dump
*exp
, const mpfr_t val
)
3047 mpfr_exp_t exponent
;
3048 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, MPFR_RNDN
);
3050 exp
->write_c_string("-");
3051 exp
->write_c_string("0.");
3052 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
3055 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
3056 exp
->write_c_string(buf
);
3059 // Export a floating point number in a constant expression.
3062 Float_expression::do_export(Export_function_body
* efb
) const
3064 bool exported_type
= Expression::export_constant_type(efb
, this->type_
);
3066 Float_expression::export_float(efb
, this->val_
);
3067 // A trailing space lets us reliably identify the end of the number.
3068 efb
->write_c_string(" ");
3070 Expression::finish_export_constant_type(efb
, exported_type
);
3073 // Dump a floating point number to the dump file.
3076 Float_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3078 Float_expression::export_float(ast_dump_context
, this->val_
);
3081 // Make a float expression.
3084 Expression::make_float(const mpfr_t
* val
, Type
* type
, Location location
)
3086 return new Float_expression(val
, type
, location
);
3091 class Complex_expression
: public Expression
3094 Complex_expression(const mpc_t
* val
, Type
* type
, Location location
)
3095 : Expression(EXPRESSION_COMPLEX
, location
),
3098 mpc_init2(this->val_
, mpc_precision
);
3099 mpc_set(this->val_
, *val
, MPC_RNDNN
);
3102 // Write VAL to string dump.
3104 export_complex(String_dump
* exp
, const mpc_t val
);
3106 // Write REAL/IMAG to dump context.
3108 dump_complex(Ast_dump_context
* ast_dump_context
, const mpc_t val
);
3112 do_traverse(Traverse
*);
3115 do_is_constant() const
3119 do_is_zero_value() const
3121 return mpfr_zero_p(mpc_realref(this->val_
)) != 0
3122 && mpfr_signbit(mpc_realref(this->val_
)) == 0
3123 && mpfr_zero_p(mpc_imagref(this->val_
)) != 0
3124 && mpfr_signbit(mpc_imagref(this->val_
)) == 0;
3128 do_is_static_initializer() const
3132 do_numeric_constant_value(Numeric_constant
* nc
) const
3134 nc
->set_complex(this->type_
, this->val_
);
3142 do_determine_type(const Type_context
*);
3145 do_check_types(Gogo
*);
3150 return Expression::make_complex(&this->val_
,
3151 (this->type_
== NULL
3153 : this->type_
->copy_expressions()),
3158 do_get_backend(Translate_context
*);
3161 do_inlining_cost() const
3165 do_export(Export_function_body
*) const;
3168 do_dump_expression(Ast_dump_context
*) const;
3171 // The complex value.
3173 // The type if known.
3177 // Traverse a complex expression. We just need to traverse the type
3181 Complex_expression::do_traverse(Traverse
* traverse
)
3183 if (this->type_
!= NULL
)
3184 return Type::traverse(this->type_
, traverse
);
3185 return TRAVERSE_CONTINUE
;
3188 // Return the current type. If we haven't set the type yet, we return
3189 // an abstract complex type.
3192 Complex_expression::do_type()
3194 if (this->type_
== NULL
)
3195 this->type_
= Type::make_abstract_complex_type();
3199 // Set the type of the complex value. Here we may switch from an
3200 // abstract type to a real type.
3203 Complex_expression::do_determine_type(const Type_context
* context
)
3205 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
3207 else if (context
->type
!= NULL
&& context
->type
->is_numeric_type())
3208 this->type_
= context
->type
;
3209 else if (!context
->may_be_abstract
)
3210 this->type_
= Type::lookup_complex_type("complex128");
3213 // Check the type of a complex value.
3216 Complex_expression::do_check_types(Gogo
*)
3218 Type
* type
= this->type_
;
3221 Numeric_constant nc
;
3222 nc
.set_complex(NULL
, this->val_
);
3223 if (!nc
.set_type(this->type_
, true, this->location()))
3224 this->set_is_error();
3227 // Get the backend representation for a complex constant.
3230 Complex_expression::do_get_backend(Translate_context
* context
)
3232 if (this->is_error_expression()
3233 || (this->type_
!= NULL
&& this->type_
->is_error_type()))
3235 go_assert(saw_errors());
3236 return context
->gogo()->backend()->error_expression();
3239 Type
* resolved_type
;
3240 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
3241 resolved_type
= this->type_
;
3242 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
3244 // We are converting to an abstract integer type.
3245 resolved_type
= Type::lookup_integer_type("int");
3247 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
3249 // We are converting to an abstract float type.
3250 resolved_type
= Type::lookup_float_type("float64");
3254 // If we still have an abstract type here, this is being
3255 // used in a constant expression which didn't get reduced. We
3256 // just use complex128 and hope for the best.
3257 resolved_type
= Type::lookup_complex_type("complex128");
3260 Numeric_constant nc
;
3261 nc
.set_complex(resolved_type
, this->val_
);
3262 return Expression::backend_numeric_constant_expression(context
, &nc
);
3265 // Write REAL/IMAG to export data.
3268 Complex_expression::export_complex(String_dump
* exp
, const mpc_t val
)
3270 if (!mpfr_zero_p(mpc_realref(val
)))
3272 Float_expression::export_float(exp
, mpc_realref(val
));
3273 if (mpfr_sgn(mpc_imagref(val
)) >= 0)
3274 exp
->write_c_string("+");
3276 Float_expression::export_float(exp
, mpc_imagref(val
));
3277 exp
->write_c_string("i");
3280 // Export a complex number in a constant expression.
3283 Complex_expression::do_export(Export_function_body
* efb
) const
3285 bool exported_type
= Expression::export_constant_type(efb
, this->type_
);
3287 Complex_expression::export_complex(efb
, this->val_
);
3288 // A trailing space lets us reliably identify the end of the number.
3289 efb
->write_c_string(" ");
3291 Expression::finish_export_constant_type(efb
, exported_type
);
3294 // Dump a complex expression to the dump file.
3297 Complex_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3299 Complex_expression::export_complex(ast_dump_context
, this->val_
);
3302 // Make a complex expression.
3305 Expression::make_complex(const mpc_t
* val
, Type
* type
, Location location
)
3307 return new Complex_expression(val
, type
, location
);
3310 // Find a named object in an expression.
3312 class Find_named_object
: public Traverse
3315 Find_named_object(Named_object
* no
)
3316 : Traverse(traverse_expressions
),
3317 no_(no
), found_(false)
3320 // Whether we found the object.
3323 { return this->found_
; }
3327 expression(Expression
**);
3330 // The object we are looking for.
3332 // Whether we found it.
3336 // A reference to a const in an expression.
3338 class Const_expression
: public Expression
3341 Const_expression(Named_object
* constant
, Location location
)
3342 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
3343 constant_(constant
), type_(NULL
), seen_(false)
3348 { return this->constant_
; }
3351 named_object() const
3352 { return this->constant_
; }
3354 // Check that the initializer does not refer to the constant itself.
3356 check_for_init_loop();
3360 do_traverse(Traverse
*);
3363 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
3366 do_is_constant() const
3370 do_is_zero_value() const
3371 { return this->constant_
->const_value()->expr()->is_zero_value(); }
3374 do_is_static_initializer() const
3378 do_numeric_constant_value(Numeric_constant
* nc
) const;
3381 do_string_constant_value(std::string
* val
) const;
3384 do_boolean_constant_value(bool* val
) const;
3389 // The type of a const is set by the declaration, not the use.
3391 do_determine_type(const Type_context
*);
3394 do_check_types(Gogo
*);
3401 do_get_backend(Translate_context
* context
);
3404 do_inlining_cost() const
3407 // When exporting a reference to a const as part of a const
3408 // expression, we export the value. We ignore the fact that it has
3411 do_export(Export_function_body
* efb
) const
3412 { this->constant_
->const_value()->expr()->export_expression(efb
); }
3415 do_dump_expression(Ast_dump_context
*) const;
3419 Named_object
* constant_
;
3420 // The type of this reference. This is used if the constant has an
3423 // Used to prevent infinite recursion when a constant incorrectly
3424 // refers to itself.
3431 Const_expression::do_traverse(Traverse
* traverse
)
3433 if (this->type_
!= NULL
)
3434 return Type::traverse(this->type_
, traverse
);
3435 return TRAVERSE_CONTINUE
;
3438 // Lower a constant expression. This is where we convert the
3439 // predeclared constant iota into an integer value.
3442 Const_expression::do_lower(Gogo
* gogo
, Named_object
*,
3443 Statement_inserter
*, int iota_value
)
3445 if (this->constant_
->const_value()->expr()->classification()
3448 if (iota_value
== -1)
3450 go_error_at(this->location(),
3451 "iota is only defined in const declarations");
3454 return Expression::make_integer_ul(iota_value
, NULL
, this->location());
3457 // Make sure that the constant itself has been lowered.
3458 gogo
->lower_constant(this->constant_
);
3463 // Return a numeric constant value.
3466 Const_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
3471 Expression
* e
= this->constant_
->const_value()->expr();
3475 bool r
= e
->numeric_constant_value(nc
);
3477 this->seen_
= false;
3480 if (this->type_
!= NULL
)
3481 ctype
= this->type_
;
3483 ctype
= this->constant_
->const_value()->type();
3484 if (r
&& ctype
!= NULL
)
3486 if (!nc
->set_type(ctype
, false, this->location()))
3494 Const_expression::do_string_constant_value(std::string
* val
) const
3499 Expression
* e
= this->constant_
->const_value()->expr();
3502 bool ok
= e
->string_constant_value(val
);
3503 this->seen_
= false;
3509 Const_expression::do_boolean_constant_value(bool* val
) const
3514 Expression
* e
= this->constant_
->const_value()->expr();
3517 bool ok
= e
->boolean_constant_value(val
);
3518 this->seen_
= false;
3523 // Return the type of the const reference.
3526 Const_expression::do_type()
3528 if (this->type_
!= NULL
)
3531 Named_constant
* nc
= this->constant_
->const_value();
3533 if (this->seen_
|| nc
->lowering())
3535 if (nc
->type() == NULL
|| !nc
->type()->is_error_type())
3537 Location loc
= this->location();
3539 loc
= nc
->location();
3540 go_error_at(loc
, "constant refers to itself");
3542 this->set_is_error();
3543 this->type_
= Type::make_error_type();
3544 nc
->set_type(this->type_
);
3550 Type
* ret
= nc
->type();
3554 this->seen_
= false;
3558 // During parsing, a named constant may have a NULL type, but we
3559 // must not return a NULL type here.
3560 ret
= nc
->expr()->type();
3562 this->seen_
= false;
3564 if (ret
->is_error_type())
3570 // Set the type of the const reference.
3573 Const_expression::do_determine_type(const Type_context
* context
)
3575 Type
* ctype
= this->constant_
->const_value()->type();
3576 Type
* cetype
= (ctype
!= NULL
3578 : this->constant_
->const_value()->expr()->type());
3579 if (ctype
!= NULL
&& !ctype
->is_abstract())
3581 else if (context
->type
!= NULL
3582 && context
->type
->is_numeric_type()
3583 && cetype
->is_numeric_type())
3584 this->type_
= context
->type
;
3585 else if (context
->type
!= NULL
3586 && context
->type
->is_string_type()
3587 && cetype
->is_string_type())
3588 this->type_
= context
->type
;
3589 else if (context
->type
!= NULL
3590 && context
->type
->is_boolean_type()
3591 && cetype
->is_boolean_type())
3592 this->type_
= context
->type
;
3593 else if (!context
->may_be_abstract
)
3595 if (cetype
->is_abstract())
3596 cetype
= cetype
->make_non_abstract_type();
3597 this->type_
= cetype
;
3601 // Check for a loop in which the initializer of a constant refers to
3602 // the constant itself.
3605 Const_expression::check_for_init_loop()
3607 if (this->type_
!= NULL
&& this->type_
->is_error())
3612 this->report_error(_("constant refers to itself"));
3613 this->type_
= Type::make_error_type();
3617 Expression
* init
= this->constant_
->const_value()->expr();
3618 Find_named_object
find_named_object(this->constant_
);
3621 Expression::traverse(&init
, &find_named_object
);
3622 this->seen_
= false;
3624 if (find_named_object
.found())
3626 if (this->type_
== NULL
|| !this->type_
->is_error())
3628 this->report_error(_("constant refers to itself"));
3629 this->type_
= Type::make_error_type();
3635 // Check types of a const reference.
3638 Const_expression::do_check_types(Gogo
*)
3640 if (this->type_
!= NULL
&& this->type_
->is_error())
3643 this->check_for_init_loop();
3645 // Check that numeric constant fits in type.
3646 if (this->type_
!= NULL
&& this->type_
->is_numeric_type())
3648 Numeric_constant nc
;
3649 if (this->constant_
->const_value()->expr()->numeric_constant_value(&nc
))
3651 if (!nc
.set_type(this->type_
, true, this->location()))
3652 this->set_is_error();
3657 // Return the backend representation for a const reference.
3660 Const_expression::do_get_backend(Translate_context
* context
)
3662 if (this->is_error_expression()
3663 || (this->type_
!= NULL
&& this->type_
->is_error()))
3665 go_assert(saw_errors());
3666 return context
->backend()->error_expression();
3669 // If the type has been set for this expression, but the underlying
3670 // object is an abstract int or float, we try to get the abstract
3671 // value. Otherwise we may lose something in the conversion.
3672 Expression
* expr
= this->constant_
->const_value()->expr();
3673 if (this->type_
!= NULL
3674 && this->type_
->is_numeric_type()
3675 && (this->constant_
->const_value()->type() == NULL
3676 || this->constant_
->const_value()->type()->is_abstract()))
3678 Numeric_constant nc
;
3679 if (expr
->numeric_constant_value(&nc
)
3680 && nc
.set_type(this->type_
, false, this->location()))
3682 Expression
* e
= nc
.expression(this->location());
3683 return e
->get_backend(context
);
3687 if (this->type_
!= NULL
)
3688 expr
= Expression::make_cast(this->type_
, expr
, this->location());
3689 return expr
->get_backend(context
);
3692 // Dump ast representation for constant expression.
3695 Const_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3697 ast_dump_context
->ostream() << this->constant_
->name();
3700 // Make a reference to a constant in an expression.
3703 Expression::make_const_reference(Named_object
* constant
,
3706 return new Const_expression(constant
, location
);
3709 // Find a named object in an expression.
3712 Find_named_object::expression(Expression
** pexpr
)
3714 switch ((*pexpr
)->classification())
3716 case Expression::EXPRESSION_CONST_REFERENCE
:
3718 Const_expression
* ce
= static_cast<Const_expression
*>(*pexpr
);
3719 if (ce
->named_object() == this->no_
)
3722 // We need to check a constant initializer explicitly, as
3723 // loops here will not be caught by the loop checking for
3724 // variable initializers.
3725 ce
->check_for_init_loop();
3727 return TRAVERSE_CONTINUE
;
3730 case Expression::EXPRESSION_VAR_REFERENCE
:
3731 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
3733 return TRAVERSE_CONTINUE
;
3734 case Expression::EXPRESSION_FUNC_REFERENCE
:
3735 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
3737 return TRAVERSE_CONTINUE
;
3739 return TRAVERSE_CONTINUE
;
3741 this->found_
= true;
3742 return TRAVERSE_EXIT
;
3747 class Nil_expression
: public Expression
3750 Nil_expression(Location location
)
3751 : Expression(EXPRESSION_NIL
, location
)
3755 do_import(Import_expression
*, Location
);
3759 do_is_constant() const
3763 do_is_zero_value() const
3767 do_is_static_initializer() const
3772 { return Type::make_nil_type(); }
3775 do_determine_type(const Type_context
*)
3783 do_get_backend(Translate_context
* context
)
3784 { return context
->backend()->nil_pointer_expression(); }
3787 do_inlining_cost() const
3791 do_export(Export_function_body
* efb
) const
3792 { efb
->write_c_string("$nil"); }
3795 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3796 { ast_dump_context
->ostream() << "nil"; }
3799 // Import a nil expression.
3802 Nil_expression::do_import(Import_expression
* imp
, Location loc
)
3804 if (imp
->version() >= EXPORT_FORMAT_V3
)
3805 imp
->require_c_string("$");
3806 imp
->require_c_string("nil");
3807 return Expression::make_nil(loc
);
3810 // Make a nil expression.
3813 Expression::make_nil(Location location
)
3815 return new Nil_expression(location
);
3818 // The value of the predeclared constant iota. This is little more
3819 // than a marker. This will be lowered to an integer in
3820 // Const_expression::do_lower, which is where we know the value that
3823 class Iota_expression
: public Parser_expression
3826 Iota_expression(Location location
)
3827 : Parser_expression(EXPRESSION_IOTA
, location
)
3832 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
3833 { go_unreachable(); }
3835 // There should only ever be one of these.
3838 { go_unreachable(); }
3841 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3842 { ast_dump_context
->ostream() << "iota"; }
3845 // Make an iota expression. This is only called for one case: the
3846 // value of the predeclared constant iota.
3849 Expression::make_iota()
3851 static Iota_expression
iota_expression(Linemap::unknown_location());
3852 return &iota_expression
;
3855 // Class Type_conversion_expression.
3860 Type_conversion_expression::do_traverse(Traverse
* traverse
)
3862 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3863 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3864 return TRAVERSE_EXIT
;
3865 return TRAVERSE_CONTINUE
;
3868 // Convert to a constant at lowering time.
3871 Type_conversion_expression::do_lower(Gogo
*, Named_object
*,
3872 Statement_inserter
*, int)
3874 Type
* type
= this->type_
;
3875 Expression
* val
= this->expr_
;
3876 Location location
= this->location();
3878 if (type
->is_numeric_type())
3880 Numeric_constant nc
;
3881 if (val
->numeric_constant_value(&nc
))
3883 if (!nc
.set_type(type
, true, location
))
3884 return Expression::make_error(location
);
3885 return nc
.expression(location
);
3889 // According to the language specification on string conversions
3890 // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type):
3891 // When converting an integer into a string, the string will be a UTF-8
3892 // representation of the integer and integers "outside the range of valid
3893 // Unicode code points are converted to '\uFFFD'."
3894 if (type
->is_string_type())
3896 Numeric_constant nc
;
3897 if (val
->numeric_constant_value(&nc
) && nc
.is_int())
3899 // An integer value doesn't fit in the Unicode code point range if it
3900 // overflows the Go "int" type or is negative.
3902 if (!nc
.set_type(Type::lookup_integer_type("int"), false, location
)
3903 || nc
.to_unsigned_long(&ul
) == Numeric_constant::NC_UL_NEGATIVE
)
3904 return Expression::make_string("\ufffd", location
);
3908 if (type
->is_slice_type())
3910 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3911 bool is_byte
= (element_type
->integer_type() != NULL
3912 && element_type
->integer_type()->is_byte());
3913 bool is_rune
= (element_type
->integer_type() != NULL
3914 && element_type
->integer_type()->is_rune());
3915 if (is_byte
|| is_rune
)
3918 if (val
->string_constant_value(&s
))
3920 Expression_list
* vals
= new Expression_list();
3923 for (std::string::const_iterator p
= s
.begin();
3927 unsigned char c
= static_cast<unsigned char>(*p
);
3928 vals
->push_back(Expression::make_integer_ul(c
,
3935 const char *p
= s
.data();
3936 const char *pend
= s
.data() + s
.length();
3940 int adv
= Lex::fetch_char(p
, &c
);
3943 go_warning_at(this->location(), 0,
3944 "invalid UTF-8 encoding");
3948 vals
->push_back(Expression::make_integer_ul(c
,
3954 return Expression::make_slice_composite_literal(type
, vals
,
3963 // Flatten a type conversion by using a temporary variable for the slice
3964 // in slice to string conversions.
3967 Type_conversion_expression::do_flatten(Gogo
*, Named_object
*,
3968 Statement_inserter
* inserter
)
3970 if (this->type()->is_error_type() || this->expr_
->is_error_expression())
3972 go_assert(saw_errors());
3973 return Expression::make_error(this->location());
3976 if (((this->type()->is_string_type()
3977 && this->expr_
->type()->is_slice_type())
3978 || this->expr_
->type()->interface_type() != NULL
)
3979 && !this->expr_
->is_variable())
3981 Temporary_statement
* temp
=
3982 Statement::make_temporary(NULL
, this->expr_
, this->location());
3983 inserter
->insert(temp
);
3984 this->expr_
= Expression::make_temporary_reference(temp
, this->location());
3987 // For interface conversion and string to/from slice conversions,
3988 // decide if we can allocate on stack.
3989 if (this->type()->interface_type() != NULL
3990 || this->type()->is_string_type()
3991 || this->expr_
->type()->is_string_type())
3993 Node
* n
= Node::make_node(this);
3994 if ((n
->encoding() & ESCAPE_MASK
) == Node::ESCAPE_NONE
)
3995 this->no_escape_
= true;
4000 // Return whether a type conversion is a constant.
4003 Type_conversion_expression::do_is_constant() const
4005 if (!this->expr_
->is_constant())
4008 // A conversion to a type that may not be used as a constant is not
4009 // a constant. For example, []byte(nil).
4010 Type
* type
= this->type_
;
4011 if (type
->integer_type() == NULL
4012 && type
->float_type() == NULL
4013 && type
->complex_type() == NULL
4014 && !type
->is_boolean_type()
4015 && !type
->is_string_type())
4021 // Return whether a type conversion is a zero value.
4024 Type_conversion_expression::do_is_zero_value() const
4026 if (!this->expr_
->is_zero_value())
4029 // Some type conversion from zero value is still not zero value.
4030 // For example, []byte("") or interface{}(0).
4031 // Conservatively, only report true if the RHS is nil.
4032 Type
* type
= this->type_
;
4033 if (type
->integer_type() == NULL
4034 && type
->float_type() == NULL
4035 && type
->complex_type() == NULL
4036 && !type
->is_boolean_type()
4037 && !type
->is_string_type())
4038 return this->expr_
->is_nil_expression();
4043 // Return whether a type conversion can be used in a constant
4047 Type_conversion_expression::do_is_static_initializer() const
4049 Type
* type
= this->type_
;
4050 Type
* expr_type
= this->expr_
->type();
4052 if (type
->interface_type() != NULL
4053 || expr_type
->interface_type() != NULL
)
4056 if (!this->expr_
->is_static_initializer())
4059 if (Type::are_identical(type
, expr_type
,
4060 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
4064 if (type
->is_string_type() && expr_type
->is_string_type())
4067 if ((type
->is_numeric_type()
4068 || type
->is_boolean_type()
4069 || type
->points_to() != NULL
)
4070 && (expr_type
->is_numeric_type()
4071 || expr_type
->is_boolean_type()
4072 || expr_type
->points_to() != NULL
))
4078 // Return the constant numeric value if there is one.
4081 Type_conversion_expression::do_numeric_constant_value(
4082 Numeric_constant
* nc
) const
4084 if (!this->type_
->is_numeric_type())
4086 if (!this->expr_
->numeric_constant_value(nc
))
4088 return nc
->set_type(this->type_
, false, this->location());
4091 // Return the constant string value if there is one.
4094 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
4096 if (this->type_
->is_string_type()
4097 && this->expr_
->type()->integer_type() != NULL
)
4099 Numeric_constant nc
;
4100 if (this->expr_
->numeric_constant_value(&nc
))
4103 if (nc
.to_unsigned_long(&ival
) == Numeric_constant::NC_UL_VALID
)
4105 unsigned int cval
= static_cast<unsigned int>(ival
);
4106 if (static_cast<unsigned long>(cval
) != ival
)
4108 go_warning_at(this->location(), 0,
4109 "unicode code point 0x%lx out of range",
4111 cval
= 0xfffd; // Unicode "replacement character."
4114 Lex::append_char(cval
, true, val
, this->location());
4120 // FIXME: Could handle conversion from const []int here.
4125 // Return the constant boolean value if there is one.
4128 Type_conversion_expression::do_boolean_constant_value(bool* val
) const
4130 if (!this->type_
->is_boolean_type())
4132 return this->expr_
->boolean_constant_value(val
);
4135 // Determine the resulting type of the conversion.
4138 Type_conversion_expression::do_determine_type(const Type_context
*)
4140 Type_context
subcontext(this->type_
, false);
4141 this->expr_
->determine_type(&subcontext
);
4144 // Check that types are convertible.
4147 Type_conversion_expression::do_check_types(Gogo
*)
4149 Type
* type
= this->type_
;
4150 Type
* expr_type
= this->expr_
->type();
4153 if (type
->is_error() || expr_type
->is_error())
4155 this->set_is_error();
4159 if (this->may_convert_function_types_
4160 && type
->function_type() != NULL
4161 && expr_type
->function_type() != NULL
)
4164 if (Type::are_convertible(type
, expr_type
, &reason
))
4167 go_error_at(this->location(), "%s", reason
.c_str());
4168 this->set_is_error();
4174 Type_conversion_expression::do_copy()
4176 Expression
* ret
= new Type_conversion_expression(this->type_
->copy_expressions(),
4177 this->expr_
->copy(),
4179 ret
->conversion_expression()->set_no_copy(this->no_copy_
);
4183 // Get the backend representation for a type conversion.
4186 Type_conversion_expression::do_get_backend(Translate_context
* context
)
4188 Type
* type
= this->type_
;
4189 Type
* expr_type
= this->expr_
->type();
4191 Gogo
* gogo
= context
->gogo();
4192 Btype
* btype
= type
->get_backend(gogo
);
4193 Location loc
= this->location();
4195 if (Type::are_identical(type
, expr_type
,
4196 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
4199 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
4200 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
4202 else if (type
->interface_type() != NULL
4203 && expr_type
->interface_type() == NULL
)
4205 Expression
* conversion
=
4206 Expression::convert_type_to_interface(type
, this->expr_
,
4207 this->no_escape_
, loc
);
4208 return conversion
->get_backend(context
);
4210 else if (type
->interface_type() != NULL
4211 || expr_type
->interface_type() != NULL
)
4213 Expression
* conversion
=
4214 Expression::convert_for_assignment(gogo
, type
, this->expr_
,
4216 return conversion
->get_backend(context
);
4218 else if (type
->is_string_type()
4219 && expr_type
->integer_type() != NULL
)
4222 Numeric_constant nc
;
4223 if (this->expr_
->numeric_constant_value(&nc
)
4224 && nc
.to_int(&intval
))
4228 if (mpz_fits_uint_p(intval
))
4229 x
= mpz_get_ui(intval
);
4232 char* ms
= mpz_get_str(NULL
, 16, intval
);
4233 go_warning_at(loc
, 0,
4234 "unicode code point 0x%s out of range in string",
4239 Lex::append_char(x
, true, &s
, loc
);
4241 Expression
* se
= Expression::make_string(s
, loc
);
4242 return se
->get_backend(context
);
4246 if (this->no_escape_
)
4248 Type
* byte_type
= Type::lookup_integer_type("uint8");
4249 Expression
* buflen
=
4250 Expression::make_integer_ul(4, NULL
, loc
);
4251 Type
* array_type
= Type::make_array_type(byte_type
, buflen
);
4252 buf
= Expression::make_allocation(array_type
, loc
);
4253 buf
->allocation_expression()->set_allocate_on_stack();
4254 buf
->allocation_expression()->set_no_zero();
4257 buf
= Expression::make_nil(loc
);
4258 Expression
* i2s_expr
=
4259 Runtime::make_call(Runtime::INTSTRING
, loc
, 2, buf
, this->expr_
);
4260 return Expression::make_cast(type
, i2s_expr
, loc
)->get_backend(context
);
4262 else if (type
->is_string_type() && expr_type
->is_slice_type())
4264 Array_type
* a
= expr_type
->array_type();
4265 Type
* e
= a
->element_type()->forwarded();
4266 go_assert(e
->integer_type() != NULL
);
4267 go_assert(this->expr_
->is_variable());
4270 if (this->no_escape_
&& !this->no_copy_
)
4272 Type
* byte_type
= Type::lookup_integer_type("uint8");
4273 Expression
* buflen
=
4274 Expression::make_integer_ul(tmp_string_buf_size
, NULL
, loc
);
4275 Type
* array_type
= Type::make_array_type(byte_type
, buflen
);
4276 buf
= Expression::make_allocation(array_type
, loc
);
4277 buf
->allocation_expression()->set_allocate_on_stack();
4278 buf
->allocation_expression()->set_no_zero();
4281 buf
= Expression::make_nil(loc
);
4283 if (e
->integer_type()->is_byte())
4286 Expression::make_slice_info(this->expr_
, SLICE_INFO_VALUE_POINTER
,
4289 Expression::make_slice_info(this->expr_
, SLICE_INFO_LENGTH
, loc
);
4292 if (gogo
->debug_optimization())
4293 go_debug(loc
, "no copy string([]byte)");
4294 Expression
* str
= Expression::make_string_value(ptr
, len
, loc
);
4295 return str
->get_backend(context
);
4297 return Runtime::make_call(Runtime::SLICEBYTETOSTRING
, loc
, 3, buf
,
4298 ptr
, len
)->get_backend(context
);
4302 go_assert(e
->integer_type()->is_rune());
4303 return Runtime::make_call(Runtime::SLICERUNETOSTRING
, loc
, 2, buf
,
4304 this->expr_
)->get_backend(context
);
4307 else if (type
->is_slice_type() && expr_type
->is_string_type())
4309 Type
* e
= type
->array_type()->element_type()->forwarded();
4310 go_assert(e
->integer_type() != NULL
);
4312 Runtime::Function code
;
4313 if (e
->integer_type()->is_byte())
4314 code
= Runtime::STRINGTOSLICEBYTE
;
4317 go_assert(e
->integer_type()->is_rune());
4318 code
= Runtime::STRINGTOSLICERUNE
;
4322 if (this->no_escape_
)
4324 Expression
* buflen
=
4325 Expression::make_integer_ul(tmp_string_buf_size
, NULL
, loc
);
4326 Type
* array_type
= Type::make_array_type(e
, buflen
);
4327 buf
= Expression::make_allocation(array_type
, loc
);
4328 buf
->allocation_expression()->set_allocate_on_stack();
4329 buf
->allocation_expression()->set_no_zero();
4332 buf
= Expression::make_nil(loc
);
4333 Expression
* s2a
= Runtime::make_call(code
, loc
, 2, buf
, this->expr_
);
4334 return Expression::make_unsafe_cast(type
, s2a
, loc
)->get_backend(context
);
4336 else if (type
->is_numeric_type())
4338 go_assert(Type::are_convertible(type
, expr_type
, NULL
));
4339 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
4340 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
4342 else if ((type
->is_unsafe_pointer_type()
4343 && (expr_type
->points_to() != NULL
4344 || expr_type
->integer_type()))
4345 || (expr_type
->is_unsafe_pointer_type()
4346 && type
->points_to() != NULL
)
4347 || (this->may_convert_function_types_
4348 && type
->function_type() != NULL
4349 && expr_type
->function_type() != NULL
))
4351 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
4352 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
4356 Expression
* conversion
=
4357 Expression::convert_for_assignment(gogo
, type
, this->expr_
, loc
);
4358 return conversion
->get_backend(context
);
4362 // Cost of inlining a type conversion.
4365 Type_conversion_expression::do_inlining_cost() const
4367 Type
* type
= this->type_
;
4368 Type
* expr_type
= this->expr_
->type();
4369 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
4371 else if (type
->is_string_type() && expr_type
->integer_type() != NULL
)
4373 else if (type
->is_string_type() && expr_type
->is_slice_type())
4375 else if (type
->is_slice_type() && expr_type
->is_string_type())
4381 // Output a type conversion in a constant expression.
4384 Type_conversion_expression::do_export(Export_function_body
* efb
) const
4386 efb
->write_c_string("$convert(");
4387 efb
->write_type(this->type_
);
4388 efb
->write_c_string(", ");
4390 Type
* old_context
= efb
->type_context();
4391 efb
->set_type_context(this->type_
);
4393 this->expr_
->export_expression(efb
);
4395 efb
->set_type_context(old_context
);
4397 efb
->write_c_string(")");
4400 // Import a type conversion or a struct construction.
4403 Type_conversion_expression::do_import(Import_expression
* imp
, Location loc
)
4405 imp
->require_c_string("$convert(");
4406 Type
* type
= imp
->read_type();
4407 imp
->require_c_string(", ");
4408 Expression
* val
= Expression::import_expression(imp
, loc
);
4409 imp
->require_c_string(")");
4410 return Expression::make_cast(type
, val
, loc
);
4413 // Dump ast representation for a type conversion expression.
4416 Type_conversion_expression::do_dump_expression(
4417 Ast_dump_context
* ast_dump_context
) const
4419 ast_dump_context
->dump_type(this->type_
);
4420 ast_dump_context
->ostream() << "(";
4421 ast_dump_context
->dump_expression(this->expr_
);
4422 ast_dump_context
->ostream() << ") ";
4425 // Make a type cast expression.
4428 Expression::make_cast(Type
* type
, Expression
* val
, Location location
)
4430 if (type
->is_error_type() || val
->is_error_expression())
4431 return Expression::make_error(location
);
4432 return new Type_conversion_expression(type
, val
, location
);
4435 // Class Unsafe_type_conversion_expression.
4440 Unsafe_type_conversion_expression::do_traverse(Traverse
* traverse
)
4442 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
4443 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
4444 return TRAVERSE_EXIT
;
4445 return TRAVERSE_CONTINUE
;
4448 // Return whether an unsafe type conversion can be used as a constant
4452 Unsafe_type_conversion_expression::do_is_static_initializer() const
4454 Type
* type
= this->type_
;
4455 Type
* expr_type
= this->expr_
->type();
4457 if (type
->interface_type() != NULL
4458 || expr_type
->interface_type() != NULL
)
4461 if (!this->expr_
->is_static_initializer())
4464 if (Type::are_convertible(type
, expr_type
, NULL
))
4467 if (type
->is_string_type() && expr_type
->is_string_type())
4470 if ((type
->is_numeric_type()
4471 || type
->is_boolean_type()
4472 || type
->points_to() != NULL
)
4473 && (expr_type
->is_numeric_type()
4474 || expr_type
->is_boolean_type()
4475 || expr_type
->points_to() != NULL
))
4484 Unsafe_type_conversion_expression::do_copy()
4486 return new Unsafe_type_conversion_expression(this->type_
->copy_expressions(),
4487 this->expr_
->copy(),
4491 // Convert to backend representation.
4494 Unsafe_type_conversion_expression::do_get_backend(Translate_context
* context
)
4496 // We are only called for a limited number of cases.
4498 Type
* t
= this->type_
;
4499 Type
* et
= this->expr_
->type();
4501 if (t
->is_error_type()
4502 || this->expr_
->is_error_expression()
4503 || et
->is_error_type())
4505 go_assert(saw_errors());
4506 return context
->backend()->error_expression();
4509 if (t
->array_type() != NULL
)
4510 go_assert(et
->array_type() != NULL
4511 && t
->is_slice_type() == et
->is_slice_type());
4512 else if (t
->struct_type() != NULL
)
4514 if (t
->named_type() != NULL
4515 && et
->named_type() != NULL
4516 && !Type::are_convertible(t
, et
, NULL
))
4518 go_assert(saw_errors());
4519 return context
->backend()->error_expression();
4522 go_assert(et
->struct_type() != NULL
4523 && Type::are_convertible(t
, et
, NULL
));
4525 else if (t
->map_type() != NULL
)
4526 go_assert(et
->map_type() != NULL
|| et
->points_to() != NULL
);
4527 else if (t
->channel_type() != NULL
)
4528 go_assert(et
->channel_type() != NULL
|| et
->points_to() != NULL
);
4529 else if (t
->points_to() != NULL
)
4530 go_assert(et
->points_to() != NULL
4531 || et
->channel_type() != NULL
4532 || et
->map_type() != NULL
4533 || et
->function_type() != NULL
4534 || et
->integer_type() != NULL
4535 || et
->is_nil_type());
4536 else if (t
->function_type() != NULL
)
4537 go_assert(et
->points_to() != NULL
);
4538 else if (et
->is_unsafe_pointer_type())
4539 go_assert(t
->points_to() != NULL
4540 || (t
->integer_type() != NULL
4541 && t
->integer_type() == Type::lookup_integer_type("uintptr")->real_type()));
4542 else if (t
->interface_type() != NULL
)
4544 bool empty_iface
= t
->interface_type()->is_empty();
4545 go_assert(et
->interface_type() != NULL
4546 && et
->interface_type()->is_empty() == empty_iface
);
4548 else if (t
->integer_type() != NULL
)
4549 go_assert(et
->is_boolean_type()
4550 || et
->integer_type() != NULL
4551 || et
->function_type() != NULL
4552 || et
->points_to() != NULL
4553 || et
->map_type() != NULL
4554 || et
->channel_type() != NULL
4555 || et
->is_nil_type());
4559 Gogo
* gogo
= context
->gogo();
4560 Btype
* btype
= t
->get_backend(gogo
);
4561 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
4562 Location loc
= this->location();
4563 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
4566 // Dump ast representation for an unsafe type conversion expression.
4569 Unsafe_type_conversion_expression::do_dump_expression(
4570 Ast_dump_context
* ast_dump_context
) const
4572 ast_dump_context
->dump_type(this->type_
);
4573 ast_dump_context
->ostream() << "(";
4574 ast_dump_context
->dump_expression(this->expr_
);
4575 ast_dump_context
->ostream() << ") ";
4578 // Make an unsafe type conversion expression.
4581 Expression::make_unsafe_cast(Type
* type
, Expression
* expr
,
4584 return new Unsafe_type_conversion_expression(type
, expr
, location
);
4587 // Class Unary_expression.
4589 // Call the address_taken method of the operand if needed. This is
4590 // called after escape analysis but before inserting write barriers.
4593 Unary_expression::check_operand_address_taken(Gogo
*)
4595 if (this->op_
!= OPERATOR_AND
)
4598 // If this->escapes_ is false at this point, then it was set to
4599 // false by an explicit call to set_does_not_escape, and the value
4600 // does not escape. If this->escapes_ is true, we may be able to
4601 // set it to false based on the escape analysis pass.
4604 Node
* n
= Node::make_node(this);
4605 if ((n
->encoding() & ESCAPE_MASK
) == int(Node::ESCAPE_NONE
))
4606 this->escapes_
= false;
4609 this->expr_
->address_taken(this->escapes_
);
4612 // If we are taking the address of a composite literal, and the
4613 // contents are not constant, then we want to make a heap expression
4617 Unary_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
4619 Location loc
= this->location();
4620 Operator op
= this->op_
;
4621 Expression
* expr
= this->expr_
;
4623 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
4624 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
4626 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
4627 // moving x to the heap. FIXME: Is it worth doing a real escape
4628 // analysis here? This case is found in math/unsafe.go and is
4629 // therefore worth special casing.
4630 if (op
== OPERATOR_MULT
)
4632 Expression
* e
= expr
;
4633 while (e
->classification() == EXPRESSION_CONVERSION
)
4635 Type_conversion_expression
* te
4636 = static_cast<Type_conversion_expression
*>(e
);
4640 if (e
->classification() == EXPRESSION_UNARY
)
4642 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
4643 if (ue
->op_
== OPERATOR_AND
)
4648 if (!ue
->expr_
->is_addressable() && !ue
->create_temp_
)
4650 go_error_at(ue
->location(),
4651 "invalid operand for unary %<&%>");
4652 this->set_is_error();
4656 ue
->set_does_not_escape();
4661 // Catching an invalid indirection of unsafe.Pointer here avoid
4662 // having to deal with TYPE_VOID in other places.
4663 if (op
== OPERATOR_MULT
&& expr
->type()->is_unsafe_pointer_type())
4665 go_error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
4666 return Expression::make_error(this->location());
4669 // Check for an invalid pointer dereference. We need to do this
4670 // here because Unary_expression::do_type will return an error type
4671 // in this case. That can cause code to appear erroneous, and
4672 // therefore disappear at lowering time, without any error message.
4673 if (op
== OPERATOR_MULT
&& expr
->type()->points_to() == NULL
)
4675 this->report_error(_("expected pointer"));
4676 return Expression::make_error(this->location());
4679 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
|| op
== OPERATOR_XOR
)
4681 Numeric_constant nc
;
4682 if (expr
->numeric_constant_value(&nc
))
4684 Numeric_constant result
;
4686 if (Unary_expression::eval_constant(op
, &nc
, loc
, &result
,
4688 return result
.expression(loc
);
4689 else if (issued_error
)
4690 return Expression::make_error(this->location());
4697 // Flatten expression if a nil check must be performed and create temporary
4698 // variables if necessary.
4701 Unary_expression::do_flatten(Gogo
* gogo
, Named_object
*,
4702 Statement_inserter
* inserter
)
4704 if (this->is_error_expression()
4705 || this->expr_
->is_error_expression()
4706 || this->expr_
->type()->is_error_type())
4708 go_assert(saw_errors());
4709 return Expression::make_error(this->location());
4712 Location location
= this->location();
4713 if (this->op_
== OPERATOR_MULT
4714 && !this->expr_
->is_variable())
4716 go_assert(this->expr_
->type()->points_to() != NULL
);
4717 switch (this->requires_nil_check(gogo
))
4719 case NIL_CHECK_ERROR_ENCOUNTERED
:
4721 go_assert(saw_errors());
4722 return Expression::make_error(this->location());
4724 case NIL_CHECK_NOT_NEEDED
:
4726 case NIL_CHECK_NEEDED
:
4727 this->create_temp_
= true;
4729 case NIL_CHECK_DEFAULT
:
4734 if (this->create_temp_
&& !this->expr_
->is_variable())
4736 Temporary_statement
* temp
=
4737 Statement::make_temporary(NULL
, this->expr_
, location
);
4738 inserter
->insert(temp
);
4739 this->expr_
= Expression::make_temporary_reference(temp
, location
);
4745 // Return whether a unary expression is a constant.
4748 Unary_expression::do_is_constant() const
4750 if (this->op_
== OPERATOR_MULT
)
4752 // Indirecting through a pointer is only constant if the object
4753 // to which the expression points is constant, but we currently
4754 // have no way to determine that.
4757 else if (this->op_
== OPERATOR_AND
)
4759 // Taking the address of a variable is constant if it is a
4760 // global variable, not constant otherwise. In other cases taking the
4761 // address is probably not a constant.
4762 Var_expression
* ve
= this->expr_
->var_expression();
4765 Named_object
* no
= ve
->named_object();
4766 return no
->is_variable() && no
->var_value()->is_global();
4771 return this->expr_
->is_constant();
4774 // Return whether a unary expression can be used as a constant
4778 Unary_expression::do_is_static_initializer() const
4780 if (this->op_
== OPERATOR_MULT
)
4782 else if (this->op_
== OPERATOR_AND
)
4783 return Unary_expression::base_is_static_initializer(this->expr_
);
4785 return this->expr_
->is_static_initializer();
4788 // Return whether the address of EXPR can be used as a static
4792 Unary_expression::base_is_static_initializer(Expression
* expr
)
4794 // The address of a field reference can be a static initializer if
4795 // the base can be a static initializer.
4796 Field_reference_expression
* fre
= expr
->field_reference_expression();
4798 return Unary_expression::base_is_static_initializer(fre
->expr());
4800 // The address of an index expression can be a static initializer if
4801 // the base can be a static initializer and the index is constant.
4802 Array_index_expression
* aind
= expr
->array_index_expression();
4804 return (aind
->end() == NULL
4805 && aind
->start()->is_constant()
4806 && Unary_expression::base_is_static_initializer(aind
->array()));
4808 // The address of a global variable can be a static initializer.
4809 Var_expression
* ve
= expr
->var_expression();
4812 Named_object
* no
= ve
->named_object();
4813 return no
->is_variable() && no
->var_value()->is_global();
4816 // The address of a composite literal can be used as a static
4817 // initializer if the composite literal is itself usable as a
4818 // static initializer.
4819 if (expr
->is_composite_literal() && expr
->is_static_initializer())
4822 // The address of a string constant can be used as a static
4823 // initializer. This can not be written in Go itself but this is
4824 // used when building a type descriptor.
4825 if (expr
->string_expression() != NULL
)
4831 // Return whether this dereference expression requires an explicit nil
4832 // check. If we are dereferencing the pointer to a large struct
4833 // (greater than the specified size threshold), we need to check for
4834 // nil. We don't bother to check for small structs because we expect
4835 // the system to crash on a nil pointer dereference. However, if we
4836 // know the address of this expression is being taken, we must always
4838 Unary_expression::Nil_check_classification
4839 Unary_expression::requires_nil_check(Gogo
* gogo
)
4841 go_assert(this->op_
== OPERATOR_MULT
);
4842 go_assert(this->expr_
->type()->points_to() != NULL
);
4844 if (this->issue_nil_check_
== NIL_CHECK_NEEDED
)
4845 return NIL_CHECK_NEEDED
;
4846 else if (this->issue_nil_check_
== NIL_CHECK_NOT_NEEDED
)
4847 return NIL_CHECK_NOT_NEEDED
;
4849 Type
* ptype
= this->expr_
->type()->points_to();
4850 int64_t type_size
= -1;
4851 if (!ptype
->is_void_type())
4853 bool ok
= ptype
->backend_type_size(gogo
, &type_size
);
4855 return NIL_CHECK_ERROR_ENCOUNTERED
;
4858 int64_t size_cutoff
= gogo
->nil_check_size_threshold();
4859 if (size_cutoff
== -1 || (type_size
!= -1 && type_size
>= size_cutoff
))
4860 this->issue_nil_check_
= NIL_CHECK_NEEDED
;
4862 this->issue_nil_check_
= NIL_CHECK_NOT_NEEDED
;
4863 return this->issue_nil_check_
;
4866 // Apply unary opcode OP to UNC, setting NC. Return true if this
4867 // could be done, false if not. On overflow, issues an error and sets
4871 Unary_expression::eval_constant(Operator op
, const Numeric_constant
* unc
,
4872 Location location
, Numeric_constant
* nc
,
4875 *issued_error
= false;
4882 case OPERATOR_MINUS
:
4883 if (unc
->is_int() || unc
->is_rune())
4885 else if (unc
->is_float())
4888 unc
->get_float(&uval
);
4891 mpfr_neg(val
, uval
, MPFR_RNDN
);
4892 nc
->set_float(unc
->type(), val
);
4897 else if (unc
->is_complex())
4900 unc
->get_complex(&uval
);
4902 mpc_init2(val
, mpc_precision
);
4903 mpc_neg(val
, uval
, MPC_RNDNN
);
4904 nc
->set_complex(unc
->type(), val
);
4924 if (!unc
->is_int() && !unc
->is_rune())
4929 unc
->get_rune(&uval
);
4931 unc
->get_int(&uval
);
4937 case OPERATOR_MINUS
:
4942 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
4947 Type
* utype
= unc
->type();
4948 if (utype
->integer_type() == NULL
4949 || utype
->integer_type()->is_abstract())
4953 // The number of HOST_WIDE_INTs that it takes to represent
4955 size_t count
= ((mpz_sizeinbase(uval
, 2)
4956 + HOST_BITS_PER_WIDE_INT
4958 / HOST_BITS_PER_WIDE_INT
);
4960 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
4961 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
4963 size_t obits
= utype
->integer_type()->bits();
4965 if (!utype
->integer_type()->is_unsigned() && mpz_sgn(uval
) < 0)
4968 mpz_init_set_ui(adj
, 1);
4969 mpz_mul_2exp(adj
, adj
, obits
);
4970 mpz_add(uval
, uval
, adj
);
4975 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
4976 go_assert(ecount
<= count
);
4978 // Trim down to the number of words required by the type.
4979 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
4980 / HOST_BITS_PER_WIDE_INT
);
4981 go_assert(ocount
<= count
);
4983 for (size_t i
= 0; i
< ocount
; ++i
)
4986 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
4988 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
4991 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
4993 if (!utype
->integer_type()->is_unsigned()
4994 && mpz_tstbit(val
, obits
- 1))
4997 mpz_init_set_ui(adj
, 1);
4998 mpz_mul_2exp(adj
, adj
, obits
);
4999 mpz_sub(val
, val
, adj
);
5013 nc
->set_rune(NULL
, val
);
5015 nc
->set_int(NULL
, val
);
5020 if (!nc
->set_type(unc
->type(), true, location
))
5022 *issued_error
= true;
5028 // Return the integral constant value of a unary expression, if it has one.
5031 Unary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
5033 Numeric_constant unc
;
5034 if (!this->expr_
->numeric_constant_value(&unc
))
5037 return Unary_expression::eval_constant(this->op_
, &unc
, this->location(),
5041 // Return the boolean constant value of a unary expression, if it has one.
5044 Unary_expression::do_boolean_constant_value(bool* val
) const
5046 if (this->op_
== OPERATOR_NOT
5047 && this->expr_
->boolean_constant_value(val
))
5055 // Return the type of a unary expression.
5058 Unary_expression::do_type()
5063 case OPERATOR_MINUS
:
5066 return this->expr_
->type();
5069 return Type::make_pointer_type(this->expr_
->type());
5073 Type
* subtype
= this->expr_
->type();
5074 Type
* points_to
= subtype
->points_to();
5075 if (points_to
== NULL
)
5076 return Type::make_error_type();
5085 // Determine abstract types for a unary expression.
5088 Unary_expression::do_determine_type(const Type_context
* context
)
5093 case OPERATOR_MINUS
:
5096 this->expr_
->determine_type(context
);
5100 // Taking the address of something.
5102 Type
* subtype
= (context
->type
== NULL
5104 : context
->type
->points_to());
5105 Type_context
subcontext(subtype
, false);
5106 this->expr_
->determine_type(&subcontext
);
5111 // Indirecting through a pointer.
5113 Type
* subtype
= (context
->type
== NULL
5115 : Type::make_pointer_type(context
->type
));
5116 Type_context
subcontext(subtype
, false);
5117 this->expr_
->determine_type(&subcontext
);
5126 // Check types for a unary expression.
5129 Unary_expression::do_check_types(Gogo
*)
5131 Type
* type
= this->expr_
->type();
5132 if (type
->is_error())
5134 this->set_is_error();
5141 case OPERATOR_MINUS
:
5142 if (type
->integer_type() == NULL
5143 && type
->float_type() == NULL
5144 && type
->complex_type() == NULL
)
5145 this->report_error(_("expected numeric type"));
5149 if (!type
->is_boolean_type())
5150 this->report_error(_("expected boolean type"));
5154 if (type
->integer_type() == NULL
)
5155 this->report_error(_("expected integer"));
5159 if (!this->expr_
->is_addressable())
5161 if (!this->create_temp_
)
5163 go_error_at(this->location(), "invalid operand for unary %<&%>");
5164 this->set_is_error();
5168 this->expr_
->issue_nil_check();
5172 // Indirecting through a pointer.
5173 if (type
->points_to() == NULL
)
5174 this->report_error(_("expected pointer"));
5175 if (type
->points_to()->is_error())
5176 this->set_is_error();
5184 // Get the backend representation for a unary expression.
5187 Unary_expression::do_get_backend(Translate_context
* context
)
5189 Gogo
* gogo
= context
->gogo();
5190 Location loc
= this->location();
5192 // Taking the address of a set-and-use-temporary expression requires
5193 // setting the temporary and then taking the address.
5194 if (this->op_
== OPERATOR_AND
)
5196 Set_and_use_temporary_expression
* sut
=
5197 this->expr_
->set_and_use_temporary_expression();
5200 Temporary_statement
* temp
= sut
->temporary();
5201 Bvariable
* bvar
= temp
->get_backend_variable(context
);
5202 Bexpression
* bvar_expr
=
5203 gogo
->backend()->var_expression(bvar
, loc
);
5204 Bexpression
* bval
= sut
->expression()->get_backend(context
);
5206 Named_object
* fn
= context
->function();
5207 go_assert(fn
!= NULL
);
5209 fn
->func_value()->get_or_make_decl(gogo
, fn
);
5210 Bstatement
* bassign
=
5211 gogo
->backend()->assignment_statement(bfn
, bvar_expr
, bval
, loc
);
5212 Bexpression
* bvar_addr
=
5213 gogo
->backend()->address_expression(bvar_expr
, loc
);
5214 return gogo
->backend()->compound_expression(bassign
, bvar_addr
, loc
);
5219 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
5220 Btype
* btype
= this->expr_
->type()->get_backend(gogo
);
5227 case OPERATOR_MINUS
:
5228 ret
= gogo
->backend()->unary_expression(this->op_
, bexpr
, loc
);
5229 ret
= gogo
->backend()->convert_expression(btype
, ret
, loc
);
5234 ret
= gogo
->backend()->unary_expression(this->op_
, bexpr
, loc
);
5238 if (!this->create_temp_
)
5240 // We should not see a non-constant constructor here; cases
5241 // where we would see one should have been moved onto the
5242 // heap at parse time. Taking the address of a nonconstant
5243 // constructor will not do what the programmer expects.
5245 go_assert(!this->expr_
->is_composite_literal()
5246 || this->expr_
->is_static_initializer());
5247 if (this->expr_
->classification() == EXPRESSION_UNARY
)
5249 Unary_expression
* ue
=
5250 static_cast<Unary_expression
*>(this->expr_
);
5251 go_assert(ue
->op() != OPERATOR_AND
);
5255 if (this->is_gc_root_
|| this->is_slice_init_
)
5257 std::string var_name
;
5258 bool copy_to_heap
= false;
5259 if (this->is_gc_root_
)
5261 // Build a decl for a GC root variable. GC roots are mutable, so
5262 // they cannot be represented as an immutable_struct in the
5264 var_name
= gogo
->gc_root_name();
5268 // Build a decl for a slice value initializer. An immutable slice
5269 // value initializer may have to be copied to the heap if it
5270 // contains pointers in a non-constant context.
5271 var_name
= gogo
->initializer_name();
5273 Array_type
* at
= this->expr_
->type()->array_type();
5274 go_assert(at
!= NULL
);
5276 // If we are not copying the value to the heap, we will only
5277 // initialize the value once, so we can use this directly
5278 // rather than copying it. In that case we can't make it
5279 // read-only, because the program is permitted to change it.
5280 copy_to_heap
= (context
->function() != NULL
5281 || context
->is_const());
5283 Bvariable
* implicit
=
5284 gogo
->backend()->implicit_variable(var_name
, "", btype
, true,
5285 copy_to_heap
, false, 0);
5286 gogo
->backend()->implicit_variable_set_init(implicit
, var_name
, btype
,
5287 true, copy_to_heap
, false,
5289 bexpr
= gogo
->backend()->var_expression(implicit
, loc
);
5291 // If we are not copying a slice initializer to the heap,
5292 // then it can be changed by the program, so if it can
5293 // contain pointers we must register it as a GC root.
5294 if (this->is_slice_init_
5296 && this->expr_
->type()->has_pointer())
5299 gogo
->backend()->var_expression(implicit
, loc
);
5300 root
= gogo
->backend()->address_expression(root
, loc
);
5301 Type
* type
= Type::make_pointer_type(this->expr_
->type());
5302 gogo
->add_gc_root(Expression::make_backend(root
, type
, loc
));
5305 else if ((this->expr_
->is_composite_literal()
5306 || this->expr_
->string_expression() != NULL
)
5307 && this->expr_
->is_static_initializer())
5309 std::string
var_name(gogo
->initializer_name());
5311 gogo
->backend()->immutable_struct(var_name
, "", true, false,
5313 gogo
->backend()->immutable_struct_set_init(decl
, var_name
, true,
5314 false, btype
, loc
, bexpr
);
5315 bexpr
= gogo
->backend()->var_expression(decl
, loc
);
5317 else if (this->expr_
->is_constant())
5319 std::string
var_name(gogo
->initializer_name());
5321 gogo
->backend()->implicit_variable(var_name
, "", btype
,
5322 true, true, false, 0);
5323 gogo
->backend()->implicit_variable_set_init(decl
, var_name
, btype
,
5326 bexpr
= gogo
->backend()->var_expression(decl
, loc
);
5329 go_assert(!this->create_temp_
|| this->expr_
->is_variable());
5330 ret
= gogo
->backend()->address_expression(bexpr
, loc
);
5335 go_assert(this->expr_
->type()->points_to() != NULL
);
5337 Type
* ptype
= this->expr_
->type()->points_to();
5338 Btype
* pbtype
= ptype
->get_backend(gogo
);
5339 switch (this->requires_nil_check(gogo
))
5341 case NIL_CHECK_NOT_NEEDED
:
5343 case NIL_CHECK_ERROR_ENCOUNTERED
:
5345 go_assert(saw_errors());
5346 return gogo
->backend()->error_expression();
5348 case NIL_CHECK_NEEDED
:
5350 go_assert(this->expr_
->is_variable());
5352 // If we're nil-checking the result of a set-and-use-temporary
5353 // expression, then pick out the target temp and use that
5354 // for the final result of the conditional.
5355 Bexpression
* tbexpr
= bexpr
;
5356 Bexpression
* ubexpr
= bexpr
;
5357 Set_and_use_temporary_expression
* sut
=
5358 this->expr_
->set_and_use_temporary_expression();
5360 Temporary_statement
* temp
= sut
->temporary();
5361 Bvariable
* bvar
= temp
->get_backend_variable(context
);
5362 ubexpr
= gogo
->backend()->var_expression(bvar
, loc
);
5365 Expression::make_nil(loc
)->get_backend(context
);
5366 Bexpression
* compare
=
5367 gogo
->backend()->binary_expression(OPERATOR_EQEQ
, tbexpr
,
5369 Expression
* crash
= Runtime::make_call(Runtime::PANIC_MEM
,
5371 Bexpression
* bcrash
= crash
->get_backend(context
);
5372 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
5373 bexpr
= gogo
->backend()->conditional_expression(bfn
, btype
,
5379 case NIL_CHECK_DEFAULT
:
5382 ret
= gogo
->backend()->indirect_expression(pbtype
, bexpr
, false, loc
);
5393 // Export a unary expression.
5396 Unary_expression::do_export(Export_function_body
* efb
) const
5401 efb
->write_c_string("+");
5403 case OPERATOR_MINUS
:
5404 efb
->write_c_string("-");
5407 efb
->write_c_string("!");
5410 efb
->write_c_string("^");
5413 efb
->write_c_string("&");
5416 efb
->write_c_string("*");
5421 this->expr_
->export_expression(efb
);
5424 // Import a unary expression.
5427 Unary_expression::do_import(Import_expression
* imp
, Location loc
)
5430 switch (imp
->get_char())
5436 op
= OPERATOR_MINUS
;
5453 if (imp
->version() < EXPORT_FORMAT_V3
)
5454 imp
->require_c_string(" ");
5455 Expression
* expr
= Expression::import_expression(imp
, loc
);
5456 return Expression::make_unary(op
, expr
, loc
);
5459 // Dump ast representation of an unary expression.
5462 Unary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
5464 ast_dump_context
->dump_operator(this->op_
);
5465 ast_dump_context
->ostream() << "(";
5466 ast_dump_context
->dump_expression(this->expr_
);
5467 ast_dump_context
->ostream() << ") ";
5470 // Make a unary expression.
5473 Expression::make_unary(Operator op
, Expression
* expr
, Location location
)
5475 return new Unary_expression(op
, expr
, location
);
5479 Expression::make_dereference(Expression
* ptr
,
5480 Nil_check_classification docheck
,
5483 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, ptr
, location
);
5484 if (docheck
== NIL_CHECK_NEEDED
)
5485 deref
->unary_expression()->set_requires_nil_check(true);
5486 else if (docheck
== NIL_CHECK_NOT_NEEDED
)
5487 deref
->unary_expression()->set_requires_nil_check(false);
5491 // If this is an indirection through a pointer, return the expression
5492 // being pointed through. Otherwise return this.
5497 if (this->classification_
== EXPRESSION_UNARY
)
5499 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
5500 if (ue
->op() == OPERATOR_MULT
)
5501 return ue
->operand();
5506 // Class Binary_expression.
5511 Binary_expression::do_traverse(Traverse
* traverse
)
5513 int t
= Expression::traverse(&this->left_
, traverse
);
5514 if (t
== TRAVERSE_EXIT
)
5515 return TRAVERSE_EXIT
;
5516 return Expression::traverse(&this->right_
, traverse
);
5519 // Return whether this expression may be used as a static initializer.
5522 Binary_expression::do_is_static_initializer() const
5524 if (!this->left_
->is_static_initializer()
5525 || !this->right_
->is_static_initializer())
5528 // Addresses can be static initializers, but we can't implement
5529 // arbitray binary expressions of them.
5530 Unary_expression
* lu
= this->left_
->unary_expression();
5531 Unary_expression
* ru
= this->right_
->unary_expression();
5532 if (lu
!= NULL
&& lu
->op() == OPERATOR_AND
)
5534 if (ru
!= NULL
&& ru
->op() == OPERATOR_AND
)
5535 return this->op_
== OPERATOR_MINUS
;
5537 return this->op_
== OPERATOR_PLUS
|| this->op_
== OPERATOR_MINUS
;
5539 else if (ru
!= NULL
&& ru
->op() == OPERATOR_AND
)
5540 return this->op_
== OPERATOR_PLUS
|| this->op_
== OPERATOR_MINUS
;
5542 // Other cases should resolve in the backend.
5546 // Return the type to use for a binary operation on operands of
5547 // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
5548 // such may be NULL or abstract.
5551 Binary_expression::operation_type(Operator op
, Type
* left_type
,
5552 Type
* right_type
, Type
** result_type
)
5554 if (left_type
!= right_type
5555 && !left_type
->is_abstract()
5556 && !right_type
->is_abstract()
5557 && left_type
->base() != right_type
->base()
5558 && op
!= OPERATOR_LSHIFT
5559 && op
!= OPERATOR_RSHIFT
)
5561 // May be a type error--let it be diagnosed elsewhere.
5565 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
5567 if (left_type
->integer_type() != NULL
)
5568 *result_type
= left_type
;
5570 *result_type
= Type::make_abstract_integer_type();
5572 else if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5573 *result_type
= left_type
;
5574 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5575 *result_type
= right_type
;
5576 else if (!left_type
->is_abstract())
5577 *result_type
= left_type
;
5578 else if (!right_type
->is_abstract())
5579 *result_type
= right_type
;
5580 else if (left_type
->complex_type() != NULL
)
5581 *result_type
= left_type
;
5582 else if (right_type
->complex_type() != NULL
)
5583 *result_type
= right_type
;
5584 else if (left_type
->float_type() != NULL
)
5585 *result_type
= left_type
;
5586 else if (right_type
->float_type() != NULL
)
5587 *result_type
= right_type
;
5588 else if (left_type
->integer_type() != NULL
5589 && left_type
->integer_type()->is_rune())
5590 *result_type
= left_type
;
5591 else if (right_type
->integer_type() != NULL
5592 && right_type
->integer_type()->is_rune())
5593 *result_type
= right_type
;
5595 *result_type
= left_type
;
5600 // Convert an integer comparison code and an operator to a boolean
5604 Binary_expression::cmp_to_bool(Operator op
, int cmp
)
5611 case OPERATOR_NOTEQ
:
5628 // Compare constants according to OP.
5631 Binary_expression::compare_constant(Operator op
, Numeric_constant
* left_nc
,
5632 Numeric_constant
* right_nc
,
5633 Location location
, bool* result
)
5635 Type
* left_type
= left_nc
->type();
5636 Type
* right_type
= right_nc
->type();
5639 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
5642 // When comparing an untyped operand to a typed operand, we are
5643 // effectively coercing the untyped operand to the other operand's
5644 // type, so make sure that is valid.
5645 if (!left_nc
->set_type(type
, true, location
)
5646 || !right_nc
->set_type(type
, true, location
))
5651 if (type
->complex_type() != NULL
)
5653 if (op
!= OPERATOR_EQEQ
&& op
!= OPERATOR_NOTEQ
)
5655 ret
= Binary_expression::compare_complex(left_nc
, right_nc
, &cmp
);
5657 else if (type
->float_type() != NULL
)
5658 ret
= Binary_expression::compare_float(left_nc
, right_nc
, &cmp
);
5660 ret
= Binary_expression::compare_integer(left_nc
, right_nc
, &cmp
);
5663 *result
= Binary_expression::cmp_to_bool(op
, cmp
);
5668 // Compare integer constants.
5671 Binary_expression::compare_integer(const Numeric_constant
* left_nc
,
5672 const Numeric_constant
* right_nc
,
5676 if (!left_nc
->to_int(&left_val
))
5679 if (!right_nc
->to_int(&right_val
))
5681 mpz_clear(left_val
);
5685 *cmp
= mpz_cmp(left_val
, right_val
);
5687 mpz_clear(left_val
);
5688 mpz_clear(right_val
);
5693 // Compare floating point constants.
5696 Binary_expression::compare_float(const Numeric_constant
* left_nc
,
5697 const Numeric_constant
* right_nc
,
5701 if (!left_nc
->to_float(&left_val
))
5704 if (!right_nc
->to_float(&right_val
))
5706 mpfr_clear(left_val
);
5710 // We already coerced both operands to the same type. If that type
5711 // is not an abstract type, we need to round the values accordingly.
5712 Type
* type
= left_nc
->type();
5713 if (!type
->is_abstract() && type
->float_type() != NULL
)
5715 int bits
= type
->float_type()->bits();
5716 mpfr_prec_round(left_val
, bits
, MPFR_RNDN
);
5717 mpfr_prec_round(right_val
, bits
, MPFR_RNDN
);
5720 *cmp
= mpfr_cmp(left_val
, right_val
);
5722 mpfr_clear(left_val
);
5723 mpfr_clear(right_val
);
5728 // Compare complex constants. Complex numbers may only be compared
5732 Binary_expression::compare_complex(const Numeric_constant
* left_nc
,
5733 const Numeric_constant
* right_nc
,
5737 if (!left_nc
->to_complex(&left_val
))
5740 if (!right_nc
->to_complex(&right_val
))
5742 mpc_clear(left_val
);
5746 // We already coerced both operands to the same type. If that type
5747 // is not an abstract type, we need to round the values accordingly.
5748 Type
* type
= left_nc
->type();
5749 if (!type
->is_abstract() && type
->complex_type() != NULL
)
5751 int bits
= type
->complex_type()->bits();
5752 mpfr_prec_round(mpc_realref(left_val
), bits
/ 2, MPFR_RNDN
);
5753 mpfr_prec_round(mpc_imagref(left_val
), bits
/ 2, MPFR_RNDN
);
5754 mpfr_prec_round(mpc_realref(right_val
), bits
/ 2, MPFR_RNDN
);
5755 mpfr_prec_round(mpc_imagref(right_val
), bits
/ 2, MPFR_RNDN
);
5758 *cmp
= mpc_cmp(left_val
, right_val
) != 0;
5760 mpc_clear(left_val
);
5761 mpc_clear(right_val
);
5766 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
5767 // true if this could be done, false if not. Issue errors at LOCATION
5768 // as appropriate, and sets *ISSUED_ERROR if it did.
5771 Binary_expression::eval_constant(Operator op
, Numeric_constant
* left_nc
,
5772 Numeric_constant
* right_nc
,
5773 Location location
, Numeric_constant
* nc
,
5776 *issued_error
= false;
5780 case OPERATOR_ANDAND
:
5782 case OPERATOR_NOTEQ
:
5787 // These return boolean values, not numeric.
5793 Type
* left_type
= left_nc
->type();
5794 Type
* right_type
= right_nc
->type();
5797 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
5800 bool is_shift
= op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
;
5802 // When combining an untyped operand with a typed operand, we are
5803 // effectively coercing the untyped operand to the other operand's
5804 // type, so make sure that is valid.
5805 if (!left_nc
->set_type(type
, true, location
))
5807 if (!is_shift
&& !right_nc
->set_type(type
, true, location
))
5810 && ((left_type
->integer_type() == NULL
5811 && !left_type
->is_abstract())
5812 || (right_type
->integer_type() == NULL
5813 && !right_type
->is_abstract())))
5817 if (type
->complex_type() != NULL
)
5818 r
= Binary_expression::eval_complex(op
, left_nc
, right_nc
, location
, nc
);
5819 else if (type
->float_type() != NULL
)
5820 r
= Binary_expression::eval_float(op
, left_nc
, right_nc
, location
, nc
);
5822 r
= Binary_expression::eval_integer(op
, left_nc
, right_nc
, location
, nc
);
5826 r
= nc
->set_type(type
, true, location
);
5828 *issued_error
= true;
5834 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
5835 // integer operations. Return true if this could be done, false if
5839 Binary_expression::eval_integer(Operator op
, const Numeric_constant
* left_nc
,
5840 const Numeric_constant
* right_nc
,
5841 Location location
, Numeric_constant
* nc
)
5844 if (!left_nc
->to_int(&left_val
))
5847 if (!right_nc
->to_int(&right_val
))
5849 mpz_clear(left_val
);
5859 mpz_add(val
, left_val
, right_val
);
5860 if (mpz_sizeinbase(val
, 2) > 0x100000)
5862 go_error_at(location
, "constant addition overflow");
5867 case OPERATOR_MINUS
:
5868 mpz_sub(val
, left_val
, right_val
);
5869 if (mpz_sizeinbase(val
, 2) > 0x100000)
5871 go_error_at(location
, "constant subtraction overflow");
5877 mpz_ior(val
, left_val
, right_val
);
5880 mpz_xor(val
, left_val
, right_val
);
5883 mpz_mul(val
, left_val
, right_val
);
5884 if (mpz_sizeinbase(val
, 2) > 0x100000)
5886 go_error_at(location
, "constant multiplication overflow");
5892 if (mpz_sgn(right_val
) != 0)
5893 mpz_tdiv_q(val
, left_val
, right_val
);
5896 go_error_at(location
, "division by zero");
5902 if (mpz_sgn(right_val
) != 0)
5903 mpz_tdiv_r(val
, left_val
, right_val
);
5906 go_error_at(location
, "division by zero");
5911 case OPERATOR_LSHIFT
:
5913 unsigned long shift
= mpz_get_ui(right_val
);
5914 if (mpz_cmp_ui(right_val
, shift
) == 0 && shift
<= 0x100000)
5915 mpz_mul_2exp(val
, left_val
, shift
);
5918 go_error_at(location
, "shift count overflow");
5925 case OPERATOR_RSHIFT
:
5927 unsigned long shift
= mpz_get_ui(right_val
);
5928 if (mpz_cmp_ui(right_val
, shift
) != 0)
5930 go_error_at(location
, "shift count overflow");
5936 if (mpz_cmp_ui(left_val
, 0) >= 0)
5937 mpz_tdiv_q_2exp(val
, left_val
, shift
);
5939 mpz_fdiv_q_2exp(val
, left_val
, shift
);
5945 mpz_and(val
, left_val
, right_val
);
5947 case OPERATOR_BITCLEAR
:
5951 mpz_com(tval
, right_val
);
5952 mpz_and(val
, left_val
, tval
);
5960 mpz_clear(left_val
);
5961 mpz_clear(right_val
);
5963 if (left_nc
->is_rune()
5964 || (op
!= OPERATOR_LSHIFT
5965 && op
!= OPERATOR_RSHIFT
5966 && right_nc
->is_rune()))
5967 nc
->set_rune(NULL
, val
);
5969 nc
->set_int(NULL
, val
);
5976 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
5977 // floating point operations. Return true if this could be done,
5981 Binary_expression::eval_float(Operator op
, const Numeric_constant
* left_nc
,
5982 const Numeric_constant
* right_nc
,
5983 Location location
, Numeric_constant
* nc
)
5986 if (!left_nc
->to_float(&left_val
))
5989 if (!right_nc
->to_float(&right_val
))
5991 mpfr_clear(left_val
);
6002 mpfr_add(val
, left_val
, right_val
, MPFR_RNDN
);
6004 case OPERATOR_MINUS
:
6005 mpfr_sub(val
, left_val
, right_val
, MPFR_RNDN
);
6010 case OPERATOR_BITCLEAR
:
6012 case OPERATOR_LSHIFT
:
6013 case OPERATOR_RSHIFT
:
6014 mpfr_set_ui(val
, 0, MPFR_RNDN
);
6018 mpfr_mul(val
, left_val
, right_val
, MPFR_RNDN
);
6021 if (!mpfr_zero_p(right_val
))
6022 mpfr_div(val
, left_val
, right_val
, MPFR_RNDN
);
6025 go_error_at(location
, "division by zero");
6027 mpfr_set_ui(val
, 0, MPFR_RNDN
);
6034 mpfr_clear(left_val
);
6035 mpfr_clear(right_val
);
6037 nc
->set_float(NULL
, val
);
6043 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
6044 // complex operations. Return true if this could be done, false if
6048 Binary_expression::eval_complex(Operator op
, const Numeric_constant
* left_nc
,
6049 const Numeric_constant
* right_nc
,
6050 Location location
, Numeric_constant
* nc
)
6053 if (!left_nc
->to_complex(&left_val
))
6056 if (!right_nc
->to_complex(&right_val
))
6058 mpc_clear(left_val
);
6063 mpc_init2(val
, mpc_precision
);
6069 mpc_add(val
, left_val
, right_val
, MPC_RNDNN
);
6071 case OPERATOR_MINUS
:
6072 mpc_sub(val
, left_val
, right_val
, MPC_RNDNN
);
6077 case OPERATOR_BITCLEAR
:
6079 case OPERATOR_LSHIFT
:
6080 case OPERATOR_RSHIFT
:
6081 mpc_set_ui(val
, 0, MPC_RNDNN
);
6085 mpc_mul(val
, left_val
, right_val
, MPC_RNDNN
);
6088 if (mpc_cmp_si(right_val
, 0) == 0)
6090 go_error_at(location
, "division by zero");
6092 mpc_set_ui(val
, 0, MPC_RNDNN
);
6095 mpc_div(val
, left_val
, right_val
, MPC_RNDNN
);
6101 mpc_clear(left_val
);
6102 mpc_clear(right_val
);
6104 nc
->set_complex(NULL
, val
);
6110 // Lower a binary expression. We have to evaluate constant
6111 // expressions now, in order to implement Go's unlimited precision
6115 Binary_expression::do_lower(Gogo
* gogo
, Named_object
*,
6116 Statement_inserter
* inserter
, int)
6118 Location location
= this->location();
6119 Operator op
= this->op_
;
6120 Expression
* left
= this->left_
;
6121 Expression
* right
= this->right_
;
6123 const bool is_comparison
= (op
== OPERATOR_EQEQ
6124 || op
== OPERATOR_NOTEQ
6125 || op
== OPERATOR_LT
6126 || op
== OPERATOR_LE
6127 || op
== OPERATOR_GT
6128 || op
== OPERATOR_GE
);
6130 // Numeric constant expressions.
6132 Numeric_constant left_nc
;
6133 Numeric_constant right_nc
;
6134 if (left
->numeric_constant_value(&left_nc
)
6135 && right
->numeric_constant_value(&right_nc
))
6140 if (!Binary_expression::compare_constant(op
, &left_nc
,
6141 &right_nc
, location
,
6144 return Expression::make_boolean(result
, location
);
6148 Numeric_constant nc
;
6150 if (!Binary_expression::eval_constant(op
, &left_nc
, &right_nc
,
6155 return Expression::make_error(location
);
6158 return nc
.expression(location
);
6163 // String constant expressions.
6165 // Avoid constant folding here if the left and right types are incompatible
6166 // (leave the operation intact so that the type checker can complain about it
6167 // later on). If concatenating an abstract string with a named string type,
6168 // result type needs to be of the named type (see issue 31412).
6169 if (left
->type()->is_string_type()
6170 && right
->type()->is_string_type()
6171 && (left
->type()->named_type() == NULL
6172 || right
->type()->named_type() == NULL
6173 || left
->type()->named_type() == right
->type()->named_type()))
6175 std::string left_string
;
6176 std::string right_string
;
6177 if (left
->string_constant_value(&left_string
)
6178 && right
->string_constant_value(&right_string
))
6180 if (op
== OPERATOR_PLUS
)
6182 Type
* result_type
= (left
->type()->named_type() != NULL
6187 return Expression::make_string_typed(left_string
+ right_string
,
6188 result_type
, location
);
6190 else if (is_comparison
)
6192 int cmp
= left_string
.compare(right_string
);
6193 bool r
= Binary_expression::cmp_to_bool(op
, cmp
);
6196 return Expression::make_boolean(r
, location
);
6201 // Lower struct, array, and some interface comparisons.
6202 if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
6204 if (left
->type()->struct_type() != NULL
6205 && right
->type()->struct_type() != NULL
)
6206 return this->lower_struct_comparison(gogo
, inserter
);
6207 else if (left
->type()->array_type() != NULL
6208 && !left
->type()->is_slice_type()
6209 && right
->type()->array_type() != NULL
6210 && !right
->type()->is_slice_type())
6211 return this->lower_array_comparison(gogo
, inserter
);
6212 else if ((left
->type()->interface_type() != NULL
6213 && right
->type()->interface_type() == NULL
)
6214 || (left
->type()->interface_type() == NULL
6215 && right
->type()->interface_type() != NULL
))
6216 return this->lower_interface_value_comparison(gogo
, inserter
);
6219 // Lower string concatenation to String_concat_expression, so that
6220 // we can group sequences of string additions.
6221 if (this->left_
->type()->is_string_type() && this->op_
== OPERATOR_PLUS
)
6223 Expression_list
* exprs
;
6224 String_concat_expression
* left_sce
=
6225 this->left_
->string_concat_expression();
6226 if (left_sce
!= NULL
)
6227 exprs
= left_sce
->exprs();
6230 exprs
= new Expression_list();
6231 exprs
->push_back(this->left_
);
6234 String_concat_expression
* right_sce
=
6235 this->right_
->string_concat_expression();
6236 if (right_sce
!= NULL
)
6237 exprs
->append(right_sce
->exprs());
6239 exprs
->push_back(this->right_
);
6241 return Expression::make_string_concat(exprs
);
6247 // Lower a struct comparison.
6250 Binary_expression::lower_struct_comparison(Gogo
* gogo
,
6251 Statement_inserter
* inserter
)
6253 Struct_type
* st
= this->left_
->type()->struct_type();
6254 Struct_type
* st2
= this->right_
->type()->struct_type();
6258 && !Type::are_identical(st
, st2
,
6259 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
6262 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
6263 this->right_
->type(), NULL
))
6266 // See if we can compare using memcmp. As a heuristic, we use
6267 // memcmp rather than field references and comparisons if there are
6268 // more than two fields.
6269 if (st
->compare_is_identity(gogo
) && st
->total_field_count() > 2)
6270 return this->lower_compare_to_memcmp(gogo
, inserter
);
6272 Location loc
= this->location();
6274 Expression
* left
= this->left_
;
6275 Temporary_statement
* left_temp
= NULL
;
6276 if (left
->var_expression() == NULL
6277 && left
->temporary_reference_expression() == NULL
)
6279 left_temp
= Statement::make_temporary(left
->type(), NULL
, loc
);
6280 inserter
->insert(left_temp
);
6281 left
= Expression::make_set_and_use_temporary(left_temp
, left
, loc
);
6284 Expression
* right
= this->right_
;
6285 Temporary_statement
* right_temp
= NULL
;
6286 if (right
->var_expression() == NULL
6287 && right
->temporary_reference_expression() == NULL
)
6289 right_temp
= Statement::make_temporary(right
->type(), NULL
, loc
);
6290 inserter
->insert(right_temp
);
6291 right
= Expression::make_set_and_use_temporary(right_temp
, right
, loc
);
6294 Expression
* ret
= Expression::make_boolean(true, loc
);
6295 const Struct_field_list
* fields
= st
->fields();
6296 unsigned int field_index
= 0;
6297 for (Struct_field_list::const_iterator pf
= fields
->begin();
6298 pf
!= fields
->end();
6299 ++pf
, ++field_index
)
6301 if (Gogo::is_sink_name(pf
->field_name()))
6304 if (field_index
> 0)
6306 if (left_temp
== NULL
)
6307 left
= left
->copy();
6309 left
= Expression::make_temporary_reference(left_temp
, loc
);
6310 if (right_temp
== NULL
)
6311 right
= right
->copy();
6313 right
= Expression::make_temporary_reference(right_temp
, loc
);
6315 Expression
* f1
= Expression::make_field_reference(left
, field_index
,
6317 Expression
* f2
= Expression::make_field_reference(right
, field_index
,
6319 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, f1
, f2
, loc
);
6320 ret
= Expression::make_binary(OPERATOR_ANDAND
, ret
, cond
, loc
);
6323 if (this->op_
== OPERATOR_NOTEQ
)
6324 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
6329 // Lower an array comparison.
6332 Binary_expression::lower_array_comparison(Gogo
* gogo
,
6333 Statement_inserter
* inserter
)
6335 Array_type
* at
= this->left_
->type()->array_type();
6336 Array_type
* at2
= this->right_
->type()->array_type();
6340 && !Type::are_identical(at
, at2
,
6341 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
6344 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
6345 this->right_
->type(), NULL
))
6348 // Call memcmp directly if possible. This may let the middle-end
6349 // optimize the call.
6350 if (at
->compare_is_identity(gogo
))
6351 return this->lower_compare_to_memcmp(gogo
, inserter
);
6353 // Call the array comparison function.
6354 Named_object
* equal_fn
=
6355 at
->equal_function(gogo
, this->left_
->type()->named_type(), NULL
);
6357 Location loc
= this->location();
6359 Expression
* func
= Expression::make_func_reference(equal_fn
, NULL
, loc
);
6361 Expression_list
* args
= new Expression_list();
6362 args
->push_back(this->operand_address(inserter
, this->left_
));
6363 args
->push_back(this->operand_address(inserter
, this->right_
));
6365 Call_expression
* ce
= Expression::make_call(func
, args
, false, loc
);
6367 // Record that this is a call to a generated equality function. We
6368 // need to do this because a comparison returns an abstract boolean
6369 // type, but the function necessarily returns "bool". The
6370 // difference shows up in code like
6372 // var b mybool = [10]string{} == [10]string{}
6373 // The comparison function returns "bool", but since a comparison
6374 // has an abstract boolean type we need an implicit conversion to
6375 // "mybool". The implicit conversion is inserted in
6376 // Call_expression::do_flatten.
6377 ce
->set_is_equal_function();
6379 Expression
* ret
= ce
;
6380 if (this->op_
== OPERATOR_NOTEQ
)
6381 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
6386 // Lower an interface to value comparison.
6389 Binary_expression::lower_interface_value_comparison(Gogo
*,
6390 Statement_inserter
* inserter
)
6392 Type
* left_type
= this->left_
->type();
6393 Type
* right_type
= this->right_
->type();
6394 Interface_type
* ift
;
6395 if (left_type
->interface_type() != NULL
)
6397 ift
= left_type
->interface_type();
6398 if (!ift
->implements_interface(right_type
, NULL
))
6403 ift
= right_type
->interface_type();
6404 if (!ift
->implements_interface(left_type
, NULL
))
6407 if (!Type::are_compatible_for_comparison(true, left_type
, right_type
, NULL
))
6410 Location loc
= this->location();
6412 if (left_type
->interface_type() == NULL
6413 && left_type
->points_to() == NULL
6414 && !this->left_
->is_addressable())
6416 Temporary_statement
* temp
=
6417 Statement::make_temporary(left_type
, NULL
, loc
);
6418 inserter
->insert(temp
);
6420 Expression::make_set_and_use_temporary(temp
, this->left_
, loc
);
6423 if (right_type
->interface_type() == NULL
6424 && right_type
->points_to() == NULL
6425 && !this->right_
->is_addressable())
6427 Temporary_statement
* temp
=
6428 Statement::make_temporary(right_type
, NULL
, loc
);
6429 inserter
->insert(temp
);
6431 Expression::make_set_and_use_temporary(temp
, this->right_
, loc
);
6437 // Lower a struct or array comparison to a call to memcmp.
6440 Binary_expression::lower_compare_to_memcmp(Gogo
*, Statement_inserter
* inserter
)
6442 Location loc
= this->location();
6444 Expression
* a1
= this->operand_address(inserter
, this->left_
);
6445 Expression
* a2
= this->operand_address(inserter
, this->right_
);
6446 Expression
* len
= Expression::make_type_info(this->left_
->type(),
6449 Expression
* call
= Runtime::make_call(Runtime::MEMCMP
, loc
, 3, a1
, a2
, len
);
6450 Type
* int32_type
= Type::lookup_integer_type("int32");
6451 Expression
* zero
= Expression::make_integer_ul(0, int32_type
, loc
);
6452 return Expression::make_binary(this->op_
, call
, zero
, loc
);
6456 Binary_expression::do_flatten(Gogo
* gogo
, Named_object
*,
6457 Statement_inserter
* inserter
)
6459 Location loc
= this->location();
6460 if (this->left_
->type()->is_error_type()
6461 || this->right_
->type()->is_error_type()
6462 || this->left_
->is_error_expression()
6463 || this->right_
->is_error_expression())
6465 go_assert(saw_errors());
6466 return Expression::make_error(loc
);
6469 Temporary_statement
* temp
;
6471 Type
* left_type
= this->left_
->type();
6472 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
6473 || this->op_
== OPERATOR_RSHIFT
);
6474 bool is_idiv_op
= ((this->op_
== OPERATOR_DIV
&&
6475 left_type
->integer_type() != NULL
)
6476 || this->op_
== OPERATOR_MOD
);
6477 bool is_string_op
= (left_type
->is_string_type()
6478 && this->right_
->type()->is_string_type());
6482 // Mark string([]byte) operands to reuse the backing store.
6483 // String comparison does not keep the reference, so it is safe.
6484 Type_conversion_expression
* lce
=
6485 this->left_
->conversion_expression();
6486 if (lce
!= NULL
&& lce
->expr()->type()->is_slice_type())
6487 lce
->set_no_copy(true);
6488 Type_conversion_expression
* rce
=
6489 this->right_
->conversion_expression();
6490 if (rce
!= NULL
&& rce
->expr()->type()->is_slice_type())
6491 rce
->set_no_copy(true);
6496 && (gogo
->check_divide_by_zero() || gogo
->check_divide_overflow()))
6499 if (!this->left_
->is_variable() && !this->left_
->is_constant())
6501 temp
= Statement::make_temporary(NULL
, this->left_
, loc
);
6502 inserter
->insert(temp
);
6503 this->left_
= Expression::make_temporary_reference(temp
, loc
);
6505 if (!this->right_
->is_variable() && !this->right_
->is_constant())
6508 Statement::make_temporary(NULL
, this->right_
, loc
);
6509 this->right_
= Expression::make_temporary_reference(temp
, loc
);
6510 inserter
->insert(temp
);
6517 // Return the address of EXPR, cast to unsafe.Pointer.
6520 Binary_expression::operand_address(Statement_inserter
* inserter
,
6523 Location loc
= this->location();
6525 if (!expr
->is_addressable())
6527 Temporary_statement
* temp
= Statement::make_temporary(expr
->type(), NULL
,
6529 inserter
->insert(temp
);
6530 expr
= Expression::make_set_and_use_temporary(temp
, expr
, loc
);
6532 expr
= Expression::make_unary(OPERATOR_AND
, expr
, loc
);
6533 static_cast<Unary_expression
*>(expr
)->set_does_not_escape();
6534 Type
* void_type
= Type::make_void_type();
6535 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
6536 return Expression::make_cast(unsafe_pointer_type
, expr
, loc
);
6539 // Return the numeric constant value, if it has one.
6542 Binary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
6544 Numeric_constant left_nc
;
6545 if (!this->left_
->numeric_constant_value(&left_nc
))
6547 Numeric_constant right_nc
;
6548 if (!this->right_
->numeric_constant_value(&right_nc
))
6551 return Binary_expression::eval_constant(this->op_
, &left_nc
, &right_nc
,
6552 this->location(), nc
, &issued_error
);
6555 // Return the boolean constant value, if it has one.
6558 Binary_expression::do_boolean_constant_value(bool* val
) const
6560 bool is_comparison
= false;
6564 case OPERATOR_NOTEQ
:
6569 is_comparison
= true;
6571 case OPERATOR_ANDAND
:
6578 Numeric_constant left_nc
, right_nc
;
6580 && this->left_
->numeric_constant_value(&left_nc
)
6581 && this->right_
->numeric_constant_value(&right_nc
))
6582 return Binary_expression::compare_constant(this->op_
, &left_nc
,
6587 std::string left_str
, right_str
;
6589 && this->left_
->string_constant_value(&left_str
)
6590 && this->right_
->string_constant_value(&right_str
))
6592 *val
= Binary_expression::cmp_to_bool(this->op_
,
6593 left_str
.compare(right_str
));
6598 if (this->left_
->boolean_constant_value(&left_bval
))
6600 if (this->op_
== OPERATOR_ANDAND
&& !left_bval
)
6605 else if (this->op_
== OPERATOR_OROR
&& left_bval
)
6612 if (this->right_
->boolean_constant_value(&right_bval
))
6617 *val
= (left_bval
== right_bval
);
6619 case OPERATOR_NOTEQ
:
6620 *val
= (left_bval
!= right_bval
);
6622 case OPERATOR_ANDAND
:
6635 // Note that the value is being discarded.
6638 Binary_expression::do_discarding_value()
6640 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
6641 return this->right_
->discarding_value();
6644 this->unused_value_error();
6652 Binary_expression::do_type()
6654 if (this->classification() == EXPRESSION_ERROR
)
6655 return Type::make_error_type();
6660 case OPERATOR_NOTEQ
:
6665 if (this->type_
== NULL
)
6666 this->type_
= Type::make_boolean_type();
6670 case OPERATOR_MINUS
:
6677 case OPERATOR_BITCLEAR
:
6679 case OPERATOR_ANDAND
:
6682 if (!Binary_expression::operation_type(this->op_
,
6683 this->left_
->type(),
6684 this->right_
->type(),
6686 return Type::make_error_type();
6690 case OPERATOR_LSHIFT
:
6691 case OPERATOR_RSHIFT
:
6692 return this->left_
->type();
6699 // Set type for a binary expression.
6702 Binary_expression::do_determine_type(const Type_context
* context
)
6704 Type
* tleft
= this->left_
->type();
6705 Type
* tright
= this->right_
->type();
6707 // Both sides should have the same type, except for the shift
6708 // operations. For a comparison, we should ignore the incoming
6711 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
6712 || this->op_
== OPERATOR_RSHIFT
);
6714 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
6715 || this->op_
== OPERATOR_NOTEQ
6716 || this->op_
== OPERATOR_LT
6717 || this->op_
== OPERATOR_LE
6718 || this->op_
== OPERATOR_GT
6719 || this->op_
== OPERATOR_GE
);
6721 // For constant expressions, the context of the result is not useful in
6722 // determining the types of the operands. It is only legal to use abstract
6723 // boolean, numeric, and string constants as operands where it is legal to
6724 // use non-abstract boolean, numeric, and string constants, respectively.
6725 // Any issues with the operation will be resolved in the check_types pass.
6726 bool is_constant_expr
= (this->left_
->is_constant()
6727 && this->right_
->is_constant());
6729 Type_context
subcontext(*context
);
6731 if (is_constant_expr
&& !is_shift_op
)
6733 subcontext
.type
= NULL
;
6734 subcontext
.may_be_abstract
= true;
6736 else if (is_comparison
)
6738 // In a comparison, the context does not determine the types of
6740 subcontext
.type
= NULL
;
6743 // Set the context for the left hand operand.
6746 // The right hand operand of a shift plays no role in
6747 // determining the type of the left hand operand.
6749 else if (!tleft
->is_abstract())
6750 subcontext
.type
= tleft
;
6751 else if (!tright
->is_abstract())
6752 subcontext
.type
= tright
;
6753 else if (subcontext
.type
== NULL
)
6755 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
6756 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
6757 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
6759 // Both sides have an abstract integer, abstract float, or
6760 // abstract complex type. Just let CONTEXT determine
6761 // whether they may remain abstract or not.
6763 else if (tleft
->complex_type() != NULL
)
6764 subcontext
.type
= tleft
;
6765 else if (tright
->complex_type() != NULL
)
6766 subcontext
.type
= tright
;
6767 else if (tleft
->float_type() != NULL
)
6768 subcontext
.type
= tleft
;
6769 else if (tright
->float_type() != NULL
)
6770 subcontext
.type
= tright
;
6772 subcontext
.type
= tleft
;
6774 if (subcontext
.type
!= NULL
&& !context
->may_be_abstract
)
6775 subcontext
.type
= subcontext
.type
->make_non_abstract_type();
6778 this->left_
->determine_type(&subcontext
);
6782 // We may have inherited an unusable type for the shift operand.
6783 // Give a useful error if that happened.
6784 if (tleft
->is_abstract()
6785 && subcontext
.type
!= NULL
6786 && !subcontext
.may_be_abstract
6787 && subcontext
.type
->interface_type() == NULL
6788 && subcontext
.type
->integer_type() == NULL
)
6789 this->report_error(("invalid context-determined non-integer type "
6790 "for left operand of shift"));
6792 // The context for the right hand operand is the same as for the
6793 // left hand operand, except for a shift operator.
6794 subcontext
.type
= Type::lookup_integer_type("uint");
6795 subcontext
.may_be_abstract
= false;
6798 this->right_
->determine_type(&subcontext
);
6802 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
6804 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
6805 this->type_
= context
->type
;
6806 else if (!context
->may_be_abstract
)
6807 this->type_
= Type::lookup_bool_type();
6811 // Report an error if the binary operator OP does not support TYPE.
6812 // OTYPE is the type of the other operand. Return whether the
6813 // operation is OK. This should not be used for shift.
6816 Binary_expression::check_operator_type(Operator op
, Type
* type
, Type
* otype
,
6822 case OPERATOR_ANDAND
:
6823 if (!type
->is_boolean_type()
6824 || !otype
->is_boolean_type())
6826 go_error_at(location
, "expected boolean type");
6832 case OPERATOR_NOTEQ
:
6835 if (!Type::are_compatible_for_comparison(true, type
, otype
, &reason
))
6837 go_error_at(location
, "%s", reason
.c_str());
6849 if (!Type::are_compatible_for_comparison(false, type
, otype
, &reason
))
6851 go_error_at(location
, "%s", reason
.c_str());
6858 case OPERATOR_PLUSEQ
:
6859 if ((!type
->is_numeric_type() && !type
->is_string_type())
6860 || (!otype
->is_numeric_type() && !otype
->is_string_type()))
6862 go_error_at(location
,
6863 "expected integer, floating, complex, or string type");
6868 case OPERATOR_MINUS
:
6869 case OPERATOR_MINUSEQ
:
6871 case OPERATOR_MULTEQ
:
6873 case OPERATOR_DIVEQ
:
6874 if (!type
->is_numeric_type() || !otype
->is_numeric_type())
6876 go_error_at(location
, "expected integer, floating, or complex type");
6882 case OPERATOR_MODEQ
:
6886 case OPERATOR_ANDEQ
:
6888 case OPERATOR_XOREQ
:
6889 case OPERATOR_BITCLEAR
:
6890 case OPERATOR_BITCLEAREQ
:
6891 if (type
->integer_type() == NULL
|| otype
->integer_type() == NULL
)
6893 go_error_at(location
, "expected integer type");
6908 Binary_expression::do_check_types(Gogo
*)
6910 if (this->classification() == EXPRESSION_ERROR
)
6913 Type
* left_type
= this->left_
->type();
6914 Type
* right_type
= this->right_
->type();
6915 if (left_type
->is_error() || right_type
->is_error())
6917 this->set_is_error();
6921 if (this->op_
== OPERATOR_EQEQ
6922 || this->op_
== OPERATOR_NOTEQ
6923 || this->op_
== OPERATOR_LT
6924 || this->op_
== OPERATOR_LE
6925 || this->op_
== OPERATOR_GT
6926 || this->op_
== OPERATOR_GE
)
6928 if (left_type
->is_nil_type() && right_type
->is_nil_type())
6930 this->report_error(_("invalid comparison of nil with nil"));
6933 if (!Type::are_assignable(left_type
, right_type
, NULL
)
6934 && !Type::are_assignable(right_type
, left_type
, NULL
))
6936 this->report_error(_("incompatible types in binary expression"));
6939 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
6942 || !Binary_expression::check_operator_type(this->op_
, right_type
,
6946 this->set_is_error();
6950 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
6952 if (!Type::are_compatible_for_binop(left_type
, right_type
))
6954 this->report_error(_("incompatible types in binary expression"));
6957 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
6961 this->set_is_error();
6964 if (this->op_
== OPERATOR_DIV
|| this->op_
== OPERATOR_MOD
)
6966 // Division by a zero integer constant is an error.
6967 Numeric_constant rconst
;
6969 if (left_type
->integer_type() != NULL
6970 && this->right_
->numeric_constant_value(&rconst
)
6971 && rconst
.to_unsigned_long(&rval
) == Numeric_constant::NC_UL_VALID
6974 this->report_error(_("integer division by zero"));
6981 if (left_type
->integer_type() == NULL
)
6982 this->report_error(_("shift of non-integer operand"));
6984 if (right_type
->is_string_type())
6985 this->report_error(_("shift count not integer"));
6986 else if (!right_type
->is_abstract()
6987 && right_type
->integer_type() == NULL
)
6988 this->report_error(_("shift count not integer"));
6991 Numeric_constant nc
;
6992 if (this->right_
->numeric_constant_value(&nc
))
6995 if (!nc
.to_int(&val
))
6996 this->report_error(_("shift count not integer"));
6999 if (mpz_sgn(val
) < 0)
7001 this->report_error(_("negative shift count"));
7002 Location rloc
= this->right_
->location();
7003 this->right_
= Expression::make_integer_ul(0, right_type
,
7013 // Get the backend representation for a binary expression.
7016 Binary_expression::do_get_backend(Translate_context
* context
)
7018 Gogo
* gogo
= context
->gogo();
7019 Location loc
= this->location();
7020 Type
* left_type
= this->left_
->type();
7021 Type
* right_type
= this->right_
->type();
7023 bool use_left_type
= true;
7024 bool is_shift_op
= false;
7025 bool is_idiv_op
= false;
7029 case OPERATOR_NOTEQ
:
7034 return Expression::comparison(context
, this->type_
, this->op_
,
7035 this->left_
, this->right_
, loc
);
7038 case OPERATOR_ANDAND
:
7039 use_left_type
= false;
7042 case OPERATOR_MINUS
:
7048 if (left_type
->float_type() != NULL
|| left_type
->complex_type() != NULL
)
7054 case OPERATOR_LSHIFT
:
7055 case OPERATOR_RSHIFT
:
7058 case OPERATOR_BITCLEAR
:
7059 this->right_
= Expression::make_unary(OPERATOR_XOR
, this->right_
, loc
);
7066 // The only binary operation for string is +, and that should have
7067 // been converted to a String_concat_expression in do_lower.
7068 go_assert(!left_type
->is_string_type());
7070 Bexpression
* left
= this->left_
->get_backend(context
);
7071 Bexpression
* right
= this->right_
->get_backend(context
);
7073 Type
* type
= use_left_type
? left_type
: right_type
;
7074 Btype
* btype
= type
->get_backend(gogo
);
7077 gogo
->backend()->binary_expression(this->op_
, left
, right
, loc
);
7078 ret
= gogo
->backend()->convert_expression(btype
, ret
, loc
);
7080 // Initialize overflow constants.
7081 Bexpression
* overflow
;
7083 mpz_init_set_ui(zero
, 0UL);
7085 mpz_init_set_ui(one
, 1UL);
7087 mpz_init_set_si(neg_one
, -1);
7089 Btype
* left_btype
= left_type
->get_backend(gogo
);
7090 Btype
* right_btype
= right_type
->get_backend(gogo
);
7092 // In Go, a shift larger than the size of the type is well-defined.
7093 // This is not true in C, so we need to insert a conditional.
7094 // We also need to check for a negative shift count.
7097 go_assert(left_type
->integer_type() != NULL
);
7098 go_assert(right_type
->integer_type() != NULL
);
7100 int bits
= left_type
->integer_type()->bits();
7102 Numeric_constant nc
;
7104 if (!this->right_
->numeric_constant_value(&nc
)
7105 || nc
.to_unsigned_long(&ul
) != Numeric_constant::NC_UL_VALID
7106 || ul
>= static_cast<unsigned long>(bits
))
7109 mpz_init_set_ui(bitsval
, bits
);
7110 Bexpression
* bits_expr
=
7111 gogo
->backend()->integer_constant_expression(right_btype
, bitsval
);
7112 Bexpression
* compare
=
7113 gogo
->backend()->binary_expression(OPERATOR_LT
,
7114 right
, bits_expr
, loc
);
7116 Bexpression
* zero_expr
=
7117 gogo
->backend()->integer_constant_expression(left_btype
, zero
);
7118 overflow
= zero_expr
;
7119 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
7120 if (this->op_
== OPERATOR_RSHIFT
7121 && !left_type
->integer_type()->is_unsigned())
7123 Bexpression
* neg_expr
=
7124 gogo
->backend()->binary_expression(OPERATOR_LT
, left
,
7126 Bexpression
* neg_one_expr
=
7127 gogo
->backend()->integer_constant_expression(left_btype
,
7129 overflow
= gogo
->backend()->conditional_expression(bfn
,
7136 ret
= gogo
->backend()->conditional_expression(bfn
, btype
, compare
,
7137 ret
, overflow
, loc
);
7141 if (!right_type
->integer_type()->is_unsigned()
7142 && (!this->right_
->numeric_constant_value(&nc
)
7143 || nc
.to_unsigned_long(&ul
) != Numeric_constant::NC_UL_VALID
))
7145 Bexpression
* zero_expr
=
7146 gogo
->backend()->integer_constant_expression(right_btype
, zero
);
7147 Bexpression
* compare
=
7148 gogo
->backend()->binary_expression(OPERATOR_LT
, right
, zero_expr
,
7150 Expression
* crash
= Runtime::make_call(Runtime::PANIC_SHIFT
,
7152 Bexpression
* bcrash
= crash
->get_backend(context
);
7153 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
7154 ret
= gogo
->backend()->conditional_expression(bfn
, btype
, compare
,
7159 // Add checks for division by zero and division overflow as needed.
7162 if (gogo
->check_divide_by_zero())
7165 Bexpression
* zero_expr
=
7166 gogo
->backend()->integer_constant_expression(right_btype
, zero
);
7167 Bexpression
* check
=
7168 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
7169 right
, zero_expr
, loc
);
7171 Expression
* crash
= Runtime::make_call(Runtime::PANIC_DIVIDE
,
7173 Bexpression
* bcrash
= crash
->get_backend(context
);
7175 // right == 0 ? (panicdivide(), 0) : ret
7176 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
7177 ret
= gogo
->backend()->conditional_expression(bfn
, btype
,
7182 if (gogo
->check_divide_overflow())
7185 // FIXME: It would be nice to say that this test is expected
7188 Bexpression
* neg_one_expr
=
7189 gogo
->backend()->integer_constant_expression(right_btype
, neg_one
);
7190 Bexpression
* check
=
7191 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
7192 right
, neg_one_expr
, loc
);
7194 Bexpression
* zero_expr
=
7195 gogo
->backend()->integer_constant_expression(btype
, zero
);
7196 Bexpression
* one_expr
=
7197 gogo
->backend()->integer_constant_expression(btype
, one
);
7198 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
7200 if (type
->integer_type()->is_unsigned())
7202 // An unsigned -1 is the largest possible number, so
7203 // dividing is always 1 or 0.
7206 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
7208 if (this->op_
== OPERATOR_DIV
)
7210 gogo
->backend()->conditional_expression(bfn
, btype
, cmp
,
7211 one_expr
, zero_expr
,
7215 gogo
->backend()->conditional_expression(bfn
, btype
, cmp
,
7221 // Computing left / -1 is the same as computing - left,
7222 // which does not overflow since Go sets -fwrapv.
7223 if (this->op_
== OPERATOR_DIV
)
7225 Expression
* negate_expr
=
7226 Expression::make_unary(OPERATOR_MINUS
, this->left_
, loc
);
7227 overflow
= negate_expr
->get_backend(context
);
7230 overflow
= zero_expr
;
7232 overflow
= gogo
->backend()->convert_expression(btype
, overflow
, loc
);
7234 // right == -1 ? - left : ret
7235 ret
= gogo
->backend()->conditional_expression(bfn
, btype
,
7247 // Export a binary expression.
7250 Binary_expression::do_export(Export_function_body
* efb
) const
7252 efb
->write_c_string("(");
7253 this->left_
->export_expression(efb
);
7257 efb
->write_c_string(" || ");
7259 case OPERATOR_ANDAND
:
7260 efb
->write_c_string(" && ");
7263 efb
->write_c_string(" == ");
7265 case OPERATOR_NOTEQ
:
7266 efb
->write_c_string(" != ");
7269 efb
->write_c_string(" < ");
7272 efb
->write_c_string(" <= ");
7275 efb
->write_c_string(" > ");
7278 efb
->write_c_string(" >= ");
7281 efb
->write_c_string(" + ");
7283 case OPERATOR_MINUS
:
7284 efb
->write_c_string(" - ");
7287 efb
->write_c_string(" | ");
7290 efb
->write_c_string(" ^ ");
7293 efb
->write_c_string(" * ");
7296 efb
->write_c_string(" / ");
7299 efb
->write_c_string(" % ");
7301 case OPERATOR_LSHIFT
:
7302 efb
->write_c_string(" << ");
7304 case OPERATOR_RSHIFT
:
7305 efb
->write_c_string(" >> ");
7308 efb
->write_c_string(" & ");
7310 case OPERATOR_BITCLEAR
:
7311 efb
->write_c_string(" &^ ");
7316 this->right_
->export_expression(efb
);
7317 efb
->write_c_string(")");
7320 // Import a binary expression.
7323 Binary_expression::do_import(Import_expression
* imp
, Location loc
)
7325 imp
->require_c_string("(");
7327 Expression
* left
= Expression::import_expression(imp
, loc
);
7330 if (imp
->match_c_string(" || "))
7335 else if (imp
->match_c_string(" && "))
7337 op
= OPERATOR_ANDAND
;
7340 else if (imp
->match_c_string(" == "))
7345 else if (imp
->match_c_string(" != "))
7347 op
= OPERATOR_NOTEQ
;
7350 else if (imp
->match_c_string(" < "))
7355 else if (imp
->match_c_string(" <= "))
7360 else if (imp
->match_c_string(" > "))
7365 else if (imp
->match_c_string(" >= "))
7370 else if (imp
->match_c_string(" + "))
7375 else if (imp
->match_c_string(" - "))
7377 op
= OPERATOR_MINUS
;
7380 else if (imp
->match_c_string(" | "))
7385 else if (imp
->match_c_string(" ^ "))
7390 else if (imp
->match_c_string(" * "))
7395 else if (imp
->match_c_string(" / "))
7400 else if (imp
->match_c_string(" % "))
7405 else if (imp
->match_c_string(" << "))
7407 op
= OPERATOR_LSHIFT
;
7410 else if (imp
->match_c_string(" >> "))
7412 op
= OPERATOR_RSHIFT
;
7415 else if (imp
->match_c_string(" & "))
7420 else if (imp
->match_c_string(" &^ "))
7422 op
= OPERATOR_BITCLEAR
;
7425 else if (imp
->match_c_string(")"))
7427 // Not a binary operator after all.
7433 go_error_at(imp
->location(), "unrecognized binary operator");
7434 return Expression::make_error(loc
);
7437 Expression
* right
= Expression::import_expression(imp
, loc
);
7439 imp
->require_c_string(")");
7441 return Expression::make_binary(op
, left
, right
, loc
);
7444 // Dump ast representation of a binary expression.
7447 Binary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
7449 ast_dump_context
->ostream() << "(";
7450 ast_dump_context
->dump_expression(this->left_
);
7451 ast_dump_context
->ostream() << " ";
7452 ast_dump_context
->dump_operator(this->op_
);
7453 ast_dump_context
->ostream() << " ";
7454 ast_dump_context
->dump_expression(this->right_
);
7455 ast_dump_context
->ostream() << ") ";
7458 // Make a binary expression.
7461 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
7464 return new Binary_expression(op
, left
, right
, location
);
7467 // Implement a comparison.
7470 Expression::comparison(Translate_context
* context
, Type
* result_type
,
7471 Operator op
, Expression
* left
, Expression
* right
,
7474 Type
* left_type
= left
->type();
7475 Type
* right_type
= right
->type();
7477 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, location
);
7479 if (left_type
->is_string_type() && right_type
->is_string_type())
7481 go_assert(left
->is_variable() || left
->is_constant());
7482 go_assert(right
->is_variable() || right
->is_constant());
7484 if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
7487 // ? (l.ptr == r.ptr ? true : memcmp(l.ptr, r.ptr, r.len) == 0)
7489 Expression
* llen
= Expression::make_string_info(left
,
7492 Expression
* rlen
= Expression::make_string_info(right
,
7495 Expression
* leneq
= Expression::make_binary(OPERATOR_EQEQ
, llen
, rlen
,
7497 Expression
* lptr
= Expression::make_string_info(left
->copy(),
7500 Expression
* rptr
= Expression::make_string_info(right
->copy(),
7503 Expression
* ptreq
= Expression::make_binary(OPERATOR_EQEQ
, lptr
, rptr
,
7505 Expression
* btrue
= Expression::make_boolean(true, location
);
7506 Expression
* call
= Runtime::make_call(Runtime::MEMCMP
, location
, 3,
7507 lptr
->copy(), rptr
->copy(),
7509 Type
* int32_type
= Type::lookup_integer_type("int32");
7510 Expression
* zero
= Expression::make_integer_ul(0, int32_type
, location
);
7511 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
, call
, zero
,
7513 Expression
* cond
= Expression::make_conditional(ptreq
, btrue
, cmp
,
7515 Expression
* bfalse
= Expression::make_boolean(false, location
);
7516 left
= Expression::make_conditional(leneq
, cond
, bfalse
, location
);
7517 right
= Expression::make_boolean(true, location
);
7521 left
= Runtime::make_call(Runtime::CMPSTRING
, location
, 2,
7526 else if ((left_type
->interface_type() != NULL
7527 && right_type
->interface_type() == NULL
7528 && !right_type
->is_nil_type())
7529 || (left_type
->interface_type() == NULL
7530 && !left_type
->is_nil_type()
7531 && right_type
->interface_type() != NULL
))
7533 // Comparing an interface value to a non-interface value.
7534 if (left_type
->interface_type() == NULL
)
7536 std::swap(left_type
, right_type
);
7537 std::swap(left
, right
);
7540 // The right operand is not an interface. We need to take its
7541 // address if it is not a direct interface type.
7542 Expression
* pointer_arg
= NULL
;
7543 if (right_type
->is_direct_iface_type())
7544 pointer_arg
= Expression::unpack_direct_iface(right
, location
);
7547 go_assert(right
->is_addressable());
7548 pointer_arg
= Expression::make_unary(OPERATOR_AND
, right
,
7552 Expression
* descriptor
=
7553 Expression::make_type_descriptor(right_type
, location
);
7555 Runtime::make_call((left_type
->interface_type()->is_empty()
7556 ? Runtime::EFACEVALEQ
7557 : Runtime::IFACEVALEQ
),
7558 location
, 3, left
, descriptor
,
7560 go_assert(op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
);
7561 right
= Expression::make_boolean(true, location
);
7563 else if (left_type
->interface_type() != NULL
7564 && right_type
->interface_type() != NULL
)
7566 Runtime::Function compare_function
;
7567 if (left_type
->interface_type()->is_empty()
7568 && right_type
->interface_type()->is_empty())
7569 compare_function
= Runtime::EFACEEQ
;
7570 else if (!left_type
->interface_type()->is_empty()
7571 && !right_type
->interface_type()->is_empty())
7572 compare_function
= Runtime::IFACEEQ
;
7575 if (left_type
->interface_type()->is_empty())
7577 std::swap(left_type
, right_type
);
7578 std::swap(left
, right
);
7580 go_assert(!left_type
->interface_type()->is_empty());
7581 go_assert(right_type
->interface_type()->is_empty());
7582 compare_function
= Runtime::IFACEEFACEEQ
;
7585 left
= Runtime::make_call(compare_function
, location
, 2, left
, right
);
7586 go_assert(op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
);
7587 right
= Expression::make_boolean(true, location
);
7590 if (left_type
->is_nil_type()
7591 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
7593 std::swap(left_type
, right_type
);
7594 std::swap(left
, right
);
7597 if (right_type
->is_nil_type())
7599 right
= Expression::make_nil(location
);
7600 if (left_type
->array_type() != NULL
7601 && left_type
->array_type()->length() == NULL
)
7603 Array_type
* at
= left_type
->array_type();
7604 bool is_lvalue
= false;
7605 left
= at
->get_value_pointer(context
->gogo(), left
, is_lvalue
);
7607 else if (left_type
->interface_type() != NULL
)
7609 // An interface is nil if the first field is nil.
7610 left
= Expression::make_field_reference(left
, 0, location
);
7614 Bexpression
* left_bexpr
= left
->get_backend(context
);
7615 Bexpression
* right_bexpr
= right
->get_backend(context
);
7617 Gogo
* gogo
= context
->gogo();
7618 Bexpression
* ret
= gogo
->backend()->binary_expression(op
, left_bexpr
,
7619 right_bexpr
, location
);
7620 if (result_type
!= NULL
)
7621 ret
= gogo
->backend()->convert_expression(result_type
->get_backend(gogo
),
7626 // Class String_concat_expression.
7629 String_concat_expression::do_is_constant() const
7631 for (Expression_list::const_iterator pe
= this->exprs_
->begin();
7632 pe
!= this->exprs_
->end();
7635 if (!(*pe
)->is_constant())
7642 String_concat_expression::do_is_zero_value() const
7644 for (Expression_list::const_iterator pe
= this->exprs_
->begin();
7645 pe
!= this->exprs_
->end();
7648 if (!(*pe
)->is_zero_value())
7655 String_concat_expression::do_is_static_initializer() const
7657 for (Expression_list::const_iterator pe
= this->exprs_
->begin();
7658 pe
!= this->exprs_
->end();
7661 if (!(*pe
)->is_static_initializer())
7668 String_concat_expression::do_type()
7670 Type
* t
= this->exprs_
->front()->type();
7671 Expression_list::iterator pe
= this->exprs_
->begin();
7673 for (; pe
!= this->exprs_
->end(); ++pe
)
7676 if (!Binary_expression::operation_type(OPERATOR_PLUS
, t
,
7679 return Type::make_error_type();
7686 String_concat_expression::do_determine_type(const Type_context
* context
)
7688 Type_context
subcontext(*context
);
7689 for (Expression_list::iterator pe
= this->exprs_
->begin();
7690 pe
!= this->exprs_
->end();
7693 Type
* t
= (*pe
)->type();
7694 if (!t
->is_abstract())
7696 subcontext
.type
= t
;
7700 if (subcontext
.type
== NULL
)
7701 subcontext
.type
= this->exprs_
->front()->type();
7702 for (Expression_list::iterator pe
= this->exprs_
->begin();
7703 pe
!= this->exprs_
->end();
7705 (*pe
)->determine_type(&subcontext
);
7709 String_concat_expression::do_check_types(Gogo
*)
7711 if (this->is_error_expression())
7713 Type
* t
= this->exprs_
->front()->type();
7716 this->set_is_error();
7719 Expression_list::iterator pe
= this->exprs_
->begin();
7721 for (; pe
!= this->exprs_
->end(); ++pe
)
7723 Type
* t1
= (*pe
)->type();
7724 if (!Type::are_compatible_for_binop(t
, t1
))
7726 this->report_error("incompatible types in binary expression");
7729 if (!Binary_expression::check_operator_type(OPERATOR_PLUS
, t
, t1
,
7732 this->set_is_error();
7739 String_concat_expression::do_flatten(Gogo
*, Named_object
*,
7740 Statement_inserter
* inserter
)
7742 if (this->is_error_expression())
7744 Location loc
= this->location();
7745 Type
* type
= this->type();
7747 // Mark string([]byte) operands to reuse the backing store.
7748 // runtime.concatstrings does not keep the reference.
7750 // Note: in the gc runtime, if all but one inputs are empty,
7751 // concatstrings returns the only nonempty input without copy.
7752 // So it is not safe to reuse the backing store if it is a
7753 // string([]byte) conversion. So the gc compiler does the
7754 // no-copy optimization only when there is at least one
7755 // constant nonempty input. Currently the gccgo runtime
7756 // doesn't do this, so we don't do the check.
7757 for (Expression_list::iterator p
= this->exprs_
->begin();
7758 p
!= this->exprs_
->end();
7761 Type_conversion_expression
* tce
= (*p
)->conversion_expression();
7763 tce
->set_no_copy(true);
7766 Expression
* buf
= NULL
;
7767 Node
* n
= Node::make_node(this);
7768 if ((n
->encoding() & ESCAPE_MASK
) == Node::ESCAPE_NONE
)
7771 for (Expression_list::iterator p
= this->exprs_
->begin();
7772 p
!= this->exprs_
->end();
7776 if ((*p
)->string_constant_value(&s
))
7779 // Make a buffer on stack if the result does not escape.
7780 // But don't do this if we know it won't fit.
7781 if (size
< (size_t)tmp_string_buf_size
)
7783 Type
* byte_type
= Type::lookup_integer_type("uint8");
7784 Expression
* buflen
=
7785 Expression::make_integer_ul(tmp_string_buf_size
, NULL
, loc
);
7786 Expression::make_integer_ul(tmp_string_buf_size
, NULL
, loc
);
7787 Type
* array_type
= Type::make_array_type(byte_type
, buflen
);
7788 buf
= Expression::make_allocation(array_type
, loc
);
7789 buf
->allocation_expression()->set_allocate_on_stack();
7790 buf
->allocation_expression()->set_no_zero();
7794 buf
= Expression::make_nil(loc
);
7795 go_assert(this->exprs_
->size() > 1);
7797 Expression::make_integer_ul(this->exprs_
->size(), NULL
, loc
);
7798 Array_type
* array_type
= Type::make_array_type(type
, len
);
7799 array_type
->set_is_array_incomparable();
7801 Expression::make_array_composite_literal(array_type
, this->exprs_
,
7803 Temporary_statement
* ts
=
7804 Statement::make_temporary(array_type
, array
, loc
);
7805 inserter
->insert(ts
);
7806 Expression
* ref
= Expression::make_temporary_reference(ts
, loc
);
7807 ref
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
7809 Runtime::make_call(Runtime::CONCATSTRINGS
, loc
, 3, buf
,
7811 return Expression::make_cast(type
, call
, loc
);
7815 String_concat_expression::do_dump_expression(
7816 Ast_dump_context
* ast_dump_context
) const
7818 ast_dump_context
->ostream() << "concat(";
7819 ast_dump_context
->dump_expression_list(this->exprs_
, false);
7820 ast_dump_context
->ostream() << ")";
7824 Expression::make_string_concat(Expression_list
* exprs
)
7826 return new String_concat_expression(exprs
);
7829 // Class Bound_method_expression.
7834 Bound_method_expression::do_traverse(Traverse
* traverse
)
7836 return Expression::traverse(&this->expr_
, traverse
);
7839 // Return the type of a bound method expression. The type of this
7840 // object is simply the type of the method with no receiver.
7843 Bound_method_expression::do_type()
7845 Named_object
* fn
= this->method_
->named_object();
7846 Function_type
* fntype
;
7847 if (fn
->is_function())
7848 fntype
= fn
->func_value()->type();
7849 else if (fn
->is_function_declaration())
7850 fntype
= fn
->func_declaration_value()->type();
7852 return Type::make_error_type();
7853 return fntype
->copy_without_receiver();
7856 // Determine the types of a method expression.
7859 Bound_method_expression::do_determine_type(const Type_context
*)
7861 Named_object
* fn
= this->method_
->named_object();
7862 Function_type
* fntype
;
7863 if (fn
->is_function())
7864 fntype
= fn
->func_value()->type();
7865 else if (fn
->is_function_declaration())
7866 fntype
= fn
->func_declaration_value()->type();
7869 if (fntype
== NULL
|| !fntype
->is_method())
7870 this->expr_
->determine_type_no_context();
7873 Type_context
subcontext(fntype
->receiver()->type(), false);
7874 this->expr_
->determine_type(&subcontext
);
7878 // Check the types of a method expression.
7881 Bound_method_expression::do_check_types(Gogo
*)
7883 Named_object
* fn
= this->method_
->named_object();
7884 if (!fn
->is_function() && !fn
->is_function_declaration())
7886 this->report_error(_("object is not a method"));
7890 Function_type
* fntype
;
7891 if (fn
->is_function())
7892 fntype
= fn
->func_value()->type();
7893 else if (fn
->is_function_declaration())
7894 fntype
= fn
->func_declaration_value()->type();
7897 Type
* rtype
= fntype
->receiver()->type()->deref();
7898 Type
* etype
= (this->expr_type_
!= NULL
7900 : this->expr_
->type());
7901 etype
= etype
->deref();
7902 if (!Type::are_identical(rtype
, etype
, Type::COMPARE_TAGS
, NULL
))
7903 this->report_error(_("method type does not match object type"));
7906 // If a bound method expression is not simply called, then it is
7907 // represented as a closure. The closure will hold a single variable,
7908 // the receiver to pass to the method. The function will be a simple
7909 // thunk that pulls that value from the closure and calls the method
7910 // with the remaining arguments.
7912 // Because method values are not common, we don't build all thunks for
7913 // every methods, but instead only build them as we need them. In
7914 // particular, we even build them on demand for methods defined in
7917 Bound_method_expression::Method_value_thunks
7918 Bound_method_expression::method_value_thunks
;
7920 // Find or create the thunk for METHOD.
7923 Bound_method_expression::create_thunk(Gogo
* gogo
, const Method
* method
,
7926 std::pair
<Named_object
*, Named_object
*> val(fn
, NULL
);
7927 std::pair
<Method_value_thunks::iterator
, bool> ins
=
7928 Bound_method_expression::method_value_thunks
.insert(val
);
7931 // We have seen this method before.
7932 go_assert(ins
.first
->second
!= NULL
);
7933 return ins
.first
->second
;
7936 Location loc
= fn
->location();
7938 Function_type
* orig_fntype
;
7939 if (fn
->is_function())
7940 orig_fntype
= fn
->func_value()->type();
7941 else if (fn
->is_function_declaration())
7942 orig_fntype
= fn
->func_declaration_value()->type();
7946 if (orig_fntype
== NULL
|| !orig_fntype
->is_method())
7949 Named_object::make_erroneous_name(gogo
->thunk_name());
7950 return ins
.first
->second
;
7953 Struct_field_list
* sfl
= new Struct_field_list();
7954 // The type here is wrong--it should be the C function type. But it
7955 // doesn't really matter.
7956 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
7957 sfl
->push_back(Struct_field(Typed_identifier("fn", vt
, loc
)));
7958 sfl
->push_back(Struct_field(Typed_identifier("val",
7959 orig_fntype
->receiver()->type(),
7961 Struct_type
* st
= Type::make_struct_type(sfl
, loc
);
7962 st
->set_is_struct_incomparable();
7963 Type
* closure_type
= Type::make_pointer_type(st
);
7965 Function_type
* new_fntype
= orig_fntype
->copy_with_names();
7967 std::string thunk_name
= gogo
->thunk_name();
7968 Named_object
* new_no
= gogo
->start_function(thunk_name
, new_fntype
,
7971 Variable
* cvar
= new Variable(closure_type
, NULL
, false, false, false, loc
);
7972 cvar
->set_is_used();
7973 cvar
->set_is_closure();
7974 Named_object
* cp
= Named_object::make_variable("$closure" + thunk_name
,
7976 new_no
->func_value()->set_closure_var(cp
);
7978 gogo
->start_block(loc
);
7980 // Field 0 of the closure is the function code pointer, field 1 is
7981 // the value on which to invoke the method.
7982 Expression
* arg
= Expression::make_var_reference(cp
, loc
);
7983 arg
= Expression::make_dereference(arg
, NIL_CHECK_NOT_NEEDED
, loc
);
7984 arg
= Expression::make_field_reference(arg
, 1, loc
);
7986 Expression
* bme
= Expression::make_bound_method(arg
, method
, fn
, loc
);
7988 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
7989 Expression_list
* args
;
7990 if (orig_params
== NULL
|| orig_params
->empty())
7994 const Typed_identifier_list
* new_params
= new_fntype
->parameters();
7995 args
= new Expression_list();
7996 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
7997 p
!= new_params
->end();
8000 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
8001 go_assert(p_no
!= NULL
8002 && p_no
->is_variable()
8003 && p_no
->var_value()->is_parameter());
8004 args
->push_back(Expression::make_var_reference(p_no
, loc
));
8008 Call_expression
* call
= Expression::make_call(bme
, args
,
8009 orig_fntype
->is_varargs(),
8011 call
->set_varargs_are_lowered();
8013 Statement
* s
= Statement::make_return_from_call(call
, loc
);
8014 gogo
->add_statement(s
);
8015 Block
* b
= gogo
->finish_block(loc
);
8016 gogo
->add_block(b
, loc
);
8017 gogo
->lower_block(new_no
, b
);
8018 gogo
->flatten_block(new_no
, b
);
8019 gogo
->finish_function(loc
);
8021 ins
.first
->second
= new_no
;
8025 // Return an expression to check *REF for nil while dereferencing
8026 // according to FIELD_INDEXES. Update *REF to build up the field
8027 // reference. This is a static function so that we don't have to
8028 // worry about declaring Field_indexes in expressions.h.
8031 bme_check_nil(const Method::Field_indexes
* field_indexes
, Location loc
,
8034 if (field_indexes
== NULL
)
8035 return Expression::make_boolean(false, loc
);
8036 Expression
* cond
= bme_check_nil(field_indexes
->next
, loc
, ref
);
8037 Struct_type
* stype
= (*ref
)->type()->deref()->struct_type();
8038 go_assert(stype
!= NULL
8039 && field_indexes
->field_index
< stype
->field_count());
8040 if ((*ref
)->type()->struct_type() == NULL
)
8042 go_assert((*ref
)->type()->points_to() != NULL
);
8043 Expression
* n
= Expression::make_binary(OPERATOR_EQEQ
, *ref
,
8044 Expression::make_nil(loc
),
8046 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, n
, loc
);
8047 *ref
= Expression::make_dereference(*ref
, Expression::NIL_CHECK_DEFAULT
,
8049 go_assert((*ref
)->type()->struct_type() == stype
);
8051 *ref
= Expression::make_field_reference(*ref
, field_indexes
->field_index
,
8056 // Flatten a method value into a struct with nil checks. We can't do
8057 // this in the lowering phase, because if the method value is called
8058 // directly we don't need a thunk. That case will have been handled
8059 // by Call_expression::do_lower, so if we get here then we do need a
8063 Bound_method_expression::do_flatten(Gogo
* gogo
, Named_object
*,
8064 Statement_inserter
* inserter
)
8066 Location loc
= this->location();
8068 Named_object
* thunk
= Bound_method_expression::create_thunk(gogo
,
8071 if (thunk
->is_erroneous())
8073 go_assert(saw_errors());
8074 return Expression::make_error(loc
);
8077 // Force the expression into a variable. This is only necessary if
8078 // we are going to do nil checks below, but it's easy enough to
8080 Expression
* expr
= this->expr_
;
8081 if (!expr
->is_variable())
8083 Temporary_statement
* etemp
= Statement::make_temporary(NULL
, expr
, loc
);
8084 inserter
->insert(etemp
);
8085 expr
= Expression::make_temporary_reference(etemp
, loc
);
8088 // If the method expects a value, and we have a pointer, we need to
8089 // dereference the pointer.
8091 Named_object
* fn
= this->method_
->named_object();
8092 Function_type
*fntype
;
8093 if (fn
->is_function())
8094 fntype
= fn
->func_value()->type();
8095 else if (fn
->is_function_declaration())
8096 fntype
= fn
->func_declaration_value()->type();
8100 Expression
* val
= expr
;
8101 if (fntype
->receiver()->type()->points_to() == NULL
8102 && val
->type()->points_to() != NULL
)
8103 val
= Expression::make_dereference(val
, NIL_CHECK_DEFAULT
, loc
);
8105 // Note that we are ignoring this->expr_type_ here. The thunk will
8106 // expect a closure whose second field has type this->expr_type_ (if
8107 // that is not NULL). We are going to pass it a closure whose
8108 // second field has type this->expr_->type(). Since
8109 // this->expr_type_ is only not-NULL for pointer types, we can get
8112 Struct_field_list
* fields
= new Struct_field_list();
8113 fields
->push_back(Struct_field(Typed_identifier("fn",
8114 thunk
->func_value()->type(),
8116 fields
->push_back(Struct_field(Typed_identifier("val", val
->type(), loc
)));
8117 Struct_type
* st
= Type::make_struct_type(fields
, loc
);
8118 st
->set_is_struct_incomparable();
8120 Expression_list
* vals
= new Expression_list();
8121 vals
->push_back(Expression::make_func_code_reference(thunk
, loc
));
8122 vals
->push_back(val
);
8124 Expression
* ret
= Expression::make_struct_composite_literal(st
, vals
, loc
);
8125 ret
= Expression::make_heap_expression(ret
, loc
);
8127 Node
* node
= Node::make_node(this);
8128 if ((node
->encoding() & ESCAPE_MASK
) == Node::ESCAPE_NONE
)
8129 ret
->heap_expression()->set_allocate_on_stack();
8130 else if (gogo
->compiling_runtime()
8131 && gogo
->package_name() == "runtime"
8133 go_error_at(loc
, "%s escapes to heap, not allowed in runtime",
8134 node
->ast_format(gogo
).c_str());
8136 // If necessary, check whether the expression or any embedded
8137 // pointers are nil.
8139 Expression
* nil_check
= NULL
;
8140 if (this->method_
->field_indexes() != NULL
)
8142 Expression
* ref
= expr
;
8143 nil_check
= bme_check_nil(this->method_
->field_indexes(), loc
, &ref
);
8147 if (this->method_
->is_value_method() && expr
->type()->points_to() != NULL
)
8149 Expression
* n
= Expression::make_binary(OPERATOR_EQEQ
, expr
,
8150 Expression::make_nil(loc
),
8152 if (nil_check
== NULL
)
8155 nil_check
= Expression::make_binary(OPERATOR_OROR
, nil_check
, n
, loc
);
8158 if (nil_check
!= NULL
)
8160 Expression
* crash
= Runtime::make_call(Runtime::PANIC_MEM
, loc
, 0);
8161 // Fix the type of the conditional expression by pretending to
8162 // evaluate to RET either way through the conditional.
8163 crash
= Expression::make_compound(crash
, ret
, loc
);
8164 ret
= Expression::make_conditional(nil_check
, crash
, ret
, loc
);
8167 // RET is a pointer to a struct, but we want a function type.
8168 ret
= Expression::make_unsafe_cast(this->type(), ret
, loc
);
8173 // Dump ast representation of a bound method expression.
8176 Bound_method_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
8179 if (this->expr_type_
!= NULL
)
8180 ast_dump_context
->ostream() << "(";
8181 ast_dump_context
->dump_expression(this->expr_
);
8182 if (this->expr_type_
!= NULL
)
8184 ast_dump_context
->ostream() << ":";
8185 ast_dump_context
->dump_type(this->expr_type_
);
8186 ast_dump_context
->ostream() << ")";
8189 ast_dump_context
->ostream() << "." << this->function_
->name();
8192 // Make a method expression.
8194 Bound_method_expression
*
8195 Expression::make_bound_method(Expression
* expr
, const Method
* method
,
8196 Named_object
* function
, Location location
)
8198 return new Bound_method_expression(expr
, method
, function
, location
);
8201 // Class Builtin_call_expression. This is used for a call to a
8202 // builtin function.
8204 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
8206 Expression_list
* args
,
8209 : Call_expression(fn
, args
, is_varargs
, location
),
8210 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false),
8211 recover_arg_is_set_(false)
8213 Func_expression
* fnexp
= this->fn()->func_expression();
8216 this->code_
= BUILTIN_INVALID
;
8219 const std::string
& name(fnexp
->named_object()->name());
8220 if (name
== "append")
8221 this->code_
= BUILTIN_APPEND
;
8222 else if (name
== "cap")
8223 this->code_
= BUILTIN_CAP
;
8224 else if (name
== "close")
8225 this->code_
= BUILTIN_CLOSE
;
8226 else if (name
== "complex")
8227 this->code_
= BUILTIN_COMPLEX
;
8228 else if (name
== "copy")
8229 this->code_
= BUILTIN_COPY
;
8230 else if (name
== "delete")
8231 this->code_
= BUILTIN_DELETE
;
8232 else if (name
== "imag")
8233 this->code_
= BUILTIN_IMAG
;
8234 else if (name
== "len")
8235 this->code_
= BUILTIN_LEN
;
8236 else if (name
== "make")
8237 this->code_
= BUILTIN_MAKE
;
8238 else if (name
== "new")
8239 this->code_
= BUILTIN_NEW
;
8240 else if (name
== "panic")
8241 this->code_
= BUILTIN_PANIC
;
8242 else if (name
== "print")
8243 this->code_
= BUILTIN_PRINT
;
8244 else if (name
== "println")
8245 this->code_
= BUILTIN_PRINTLN
;
8246 else if (name
== "real")
8247 this->code_
= BUILTIN_REAL
;
8248 else if (name
== "recover")
8249 this->code_
= BUILTIN_RECOVER
;
8250 else if (name
== "Alignof")
8251 this->code_
= BUILTIN_ALIGNOF
;
8252 else if (name
== "Offsetof")
8253 this->code_
= BUILTIN_OFFSETOF
;
8254 else if (name
== "Sizeof")
8255 this->code_
= BUILTIN_SIZEOF
;
8260 // Return whether this is a call to recover. This is a virtual
8261 // function called from the parent class.
8264 Builtin_call_expression::do_is_recover_call() const
8266 if (this->classification() == EXPRESSION_ERROR
)
8268 return this->code_
== BUILTIN_RECOVER
;
8271 // Set the argument for a call to recover.
8274 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
8276 const Expression_list
* args
= this->args();
8277 go_assert(args
== NULL
|| args
->empty());
8278 Expression_list
* new_args
= new Expression_list();
8279 new_args
->push_back(arg
);
8280 this->set_args(new_args
);
8281 this->recover_arg_is_set_
= true;
8284 // Lower a builtin call expression. This turns new and make into
8285 // specific expressions. We also convert to a constant if we can.
8288 Builtin_call_expression::do_lower(Gogo
*, Named_object
* function
,
8289 Statement_inserter
* inserter
, int)
8291 if (this->is_error_expression())
8294 Location loc
= this->location();
8296 if (this->is_varargs() && this->code_
!= BUILTIN_APPEND
)
8298 this->report_error(_("invalid use of %<...%> with builtin function"));
8299 return Expression::make_error(loc
);
8302 if (this->code_
== BUILTIN_OFFSETOF
)
8304 Expression
* arg
= this->one_arg();
8306 if (arg
->bound_method_expression() != NULL
8307 || arg
->interface_field_reference_expression() != NULL
)
8309 this->report_error(_("invalid use of method value as argument "
8314 Field_reference_expression
* farg
= arg
->field_reference_expression();
8315 while (farg
!= NULL
)
8317 if (!farg
->implicit())
8319 // When the selector refers to an embedded field,
8320 // it must not be reached through pointer indirections.
8321 if (farg
->expr()->deref() != farg
->expr())
8323 this->report_error(_("argument of Offsetof implies "
8324 "indirection of an embedded field"));
8327 // Go up until we reach the original base.
8328 farg
= farg
->expr()->field_reference_expression();
8332 if (this->is_constant())
8334 Numeric_constant nc
;
8335 if (this->numeric_constant_value(&nc
))
8336 return nc
.expression(loc
);
8339 switch (this->code_
)
8346 const Expression_list
* args
= this->args();
8347 if (args
== NULL
|| args
->size() < 1)
8348 this->report_error(_("not enough arguments"));
8349 else if (args
->size() > 1)
8350 this->report_error(_("too many arguments"));
8353 Expression
* arg
= args
->front();
8354 if (!arg
->is_type_expression())
8356 go_error_at(arg
->location(), "expected type");
8357 this->set_is_error();
8360 return Expression::make_allocation(arg
->type(), loc
);
8366 return this->lower_make(inserter
);
8368 case BUILTIN_RECOVER
:
8369 if (function
!= NULL
)
8370 function
->func_value()->set_calls_recover();
8373 // Calling recover outside of a function always returns the
8374 // nil empty interface.
8375 Type
* eface
= Type::make_empty_interface_type(loc
);
8376 return Expression::make_cast(eface
, Expression::make_nil(loc
), loc
);
8380 case BUILTIN_DELETE
:
8382 const Expression_list
* args
= this->args();
8383 if (args
== NULL
|| args
->size() < 2)
8384 this->report_error(_("not enough arguments"));
8385 else if (args
->size() > 2)
8386 this->report_error(_("too many arguments"));
8387 else if (args
->front()->type()->map_type() == NULL
)
8388 this->report_error(_("argument 1 must be a map"));
8392 args
->front()->type()->map_type()->key_type();
8393 Expression_list::iterator pa
= this->args()->begin();
8395 Type
* arg_type
= (*pa
)->type();
8397 if (!Type::are_assignable(key_type
, arg_type
, &reason
))
8400 go_error_at(loc
, "argument 2 has incompatible type");
8402 go_error_at(loc
, "argument 2 has incompatible type (%s)",
8404 this->set_is_error();
8406 else if (!Type::are_identical(key_type
, arg_type
, 0, NULL
))
8407 *pa
= Expression::make_cast(key_type
, *pa
, loc
);
8413 case BUILTIN_PRINTLN
:
8414 // Force all the arguments into temporary variables, so that we
8415 // don't try to evaluate something while holding the print lock.
8416 if (this->args() == NULL
)
8418 for (Expression_list::iterator pa
= this->args()->begin();
8419 pa
!= this->args()->end();
8422 if (!(*pa
)->is_variable() && !(*pa
)->is_constant())
8424 Temporary_statement
* temp
=
8425 Statement::make_temporary(NULL
, *pa
, loc
);
8426 inserter
->insert(temp
);
8427 *pa
= Expression::make_temporary_reference(temp
, loc
);
8436 // Flatten a builtin call expression. This turns the arguments of some
8437 // builtin calls into temporary expressions. Also expand copy and append
8438 // to runtime calls.
8441 Builtin_call_expression::do_flatten(Gogo
* gogo
, Named_object
* function
,
8442 Statement_inserter
* inserter
)
8444 if (this->is_error_expression())
8446 go_assert(saw_errors());
8450 Location loc
= this->location();
8452 switch (this->code_
)
8457 case BUILTIN_APPEND
:
8458 return this->flatten_append(gogo
, function
, inserter
, NULL
, NULL
);
8462 Type
* at
= this->args()->front()->type();
8463 for (Expression_list::iterator pa
= this->args()->begin();
8464 pa
!= this->args()->end();
8467 if ((*pa
)->is_nil_expression())
8469 Expression
* nil
= Expression::make_nil(loc
);
8470 Expression
* zero
= Expression::make_integer_ul(0, NULL
, loc
);
8471 *pa
= Expression::make_slice_value(at
, nil
, zero
, zero
, loc
);
8473 if (!(*pa
)->is_variable())
8475 Temporary_statement
* temp
=
8476 Statement::make_temporary(NULL
, *pa
, loc
);
8477 inserter
->insert(temp
);
8478 *pa
= Expression::make_temporary_reference(temp
, loc
);
8482 // Lower to runtime call.
8483 const Expression_list
* args
= this->args();
8484 go_assert(args
!= NULL
&& args
->size() == 2);
8485 Expression
* arg1
= args
->front();
8486 Expression
* arg2
= args
->back();
8487 go_assert(arg1
->is_variable());
8488 go_assert(arg2
->is_variable());
8489 bool arg2_is_string
= arg2
->type()->is_string_type();
8492 Type
* et
= at
->array_type()->element_type();
8493 if (et
->has_pointer())
8495 Expression
* td
= Expression::make_type_descriptor(et
, loc
);
8497 Expression::make_slice_info(arg1
, SLICE_INFO_VALUE_POINTER
, loc
);
8499 Expression::make_slice_info(arg1
, SLICE_INFO_LENGTH
, loc
);
8501 Expression::make_slice_info(arg2
, SLICE_INFO_VALUE_POINTER
, loc
);
8503 Expression::make_slice_info(arg2
, SLICE_INFO_LENGTH
, loc
);
8504 ret
= Runtime::make_call(Runtime::TYPEDSLICECOPY
, loc
,
8505 5, td
, pd
, ld
, ps
, ls
);
8509 Type
* int_type
= Type::lookup_integer_type("int");
8510 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8513 Named_object
* lenfn
= gogo
->lookup_global("len");
8514 Expression
* lenref
= Expression::make_func_reference(lenfn
, NULL
, loc
);
8515 Expression_list
* len_args
= new Expression_list();
8516 len_args
->push_back(arg1
->copy());
8517 Expression
* len1
= Expression::make_call(lenref
, len_args
, false, loc
);
8518 gogo
->lower_expression(function
, inserter
, &len1
);
8519 gogo
->flatten_expression(function
, inserter
, &len1
);
8520 Temporary_statement
* l1tmp
= Statement::make_temporary(int_type
, len1
, loc
);
8521 inserter
->insert(l1tmp
);
8524 len_args
= new Expression_list();
8525 len_args
->push_back(arg2
->copy());
8526 Expression
* len2
= Expression::make_call(lenref
, len_args
, false, loc
);
8527 gogo
->lower_expression(function
, inserter
, &len2
);
8528 gogo
->flatten_expression(function
, inserter
, &len2
);
8529 Temporary_statement
* l2tmp
= Statement::make_temporary(int_type
, len2
, loc
);
8530 inserter
->insert(l2tmp
);
8532 // n = (l1 < l2 ? l1 : l2)
8533 Expression
* l1ref
= Expression::make_temporary_reference(l1tmp
, loc
);
8534 Expression
* l2ref
= Expression::make_temporary_reference(l2tmp
, loc
);
8535 Expression
* cond
= Expression::make_binary(OPERATOR_LT
, l1ref
, l2ref
, loc
);
8536 Expression
* n
= Expression::make_conditional(cond
,
8540 Temporary_statement
* ntmp
= Statement::make_temporary(NULL
, n
, loc
);
8541 inserter
->insert(ntmp
);
8543 // sz = n * sizeof(elem_type)
8544 Expression
* nref
= Expression::make_temporary_reference(ntmp
, loc
);
8545 nref
= Expression::make_cast(uintptr_type
, nref
, loc
);
8546 Expression
* sz
= Expression::make_type_info(et
, TYPE_INFO_SIZE
);
8547 sz
= Expression::make_binary(OPERATOR_MULT
, sz
, nref
, loc
);
8549 // memmove(arg1.ptr, arg2.ptr, sz)
8550 Expression
* p1
= Expression::make_slice_info(arg1
,
8551 SLICE_INFO_VALUE_POINTER
,
8553 Expression
* p2
= (arg2_is_string
8554 ? Expression::make_string_info(arg2
,
8557 : Expression::make_slice_info(arg2
,
8558 SLICE_INFO_VALUE_POINTER
,
8560 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_MEMMOVE
, loc
, 3,
8563 // n is the return value of copy
8564 nref
= Expression::make_temporary_reference(ntmp
, loc
);
8565 ret
= Expression::make_compound(call
, nref
, loc
);
8572 for (Expression_list::iterator pa
= this->args()->begin();
8573 pa
!= this->args()->end();
8576 if (!(*pa
)->is_variable() && (*pa
)->type()->interface_type() != NULL
)
8578 Temporary_statement
* temp
=
8579 Statement::make_temporary(NULL
, *pa
, loc
);
8580 inserter
->insert(temp
);
8581 *pa
= Expression::make_temporary_reference(temp
, loc
);
8589 Expression_list::iterator pa
= this->args()->begin();
8590 if (!(*pa
)->is_variable()
8591 && ((*pa
)->type()->map_type() != NULL
8592 || (*pa
)->type()->channel_type() != NULL
))
8594 Temporary_statement
* temp
=
8595 Statement::make_temporary(NULL
, *pa
, loc
);
8596 inserter
->insert(temp
);
8597 *pa
= Expression::make_temporary_reference(temp
, loc
);
8602 case BUILTIN_DELETE
:
8604 // Lower to a runtime function call.
8605 const Expression_list
* args
= this->args();
8607 // Since this function returns no value it must appear in
8608 // a statement by itself, so we don't have to worry about
8609 // order of evaluation of values around it. Evaluate the
8610 // map first to get order of evaluation right.
8611 Map_type
* mt
= args
->front()->type()->map_type();
8612 Temporary_statement
* map_temp
=
8613 Statement::make_temporary(mt
, args
->front(), loc
);
8614 inserter
->insert(map_temp
);
8616 Temporary_statement
* key_temp
=
8617 Statement::make_temporary(mt
->key_type(), args
->back(), loc
);
8618 inserter
->insert(key_temp
);
8620 Expression
* e1
= Expression::make_type_descriptor(mt
, loc
);
8621 Expression
* e2
= Expression::make_temporary_reference(map_temp
,
8623 Expression
* e3
= Expression::make_temporary_reference(key_temp
,
8626 Runtime::Function code
;
8627 switch (mt
->algorithm(gogo
))
8629 case Map_type::MAP_ALG_FAST32
:
8630 case Map_type::MAP_ALG_FAST32PTR
:
8632 code
= Runtime::MAPDELETE_FAST32
;
8633 Type
* uint32_type
= Type::lookup_integer_type("uint32");
8634 Type
* uint32_ptr_type
= Type::make_pointer_type(uint32_type
);
8635 e3
= Expression::make_unary(OPERATOR_AND
, e3
, loc
);
8636 e3
= Expression::make_unsafe_cast(uint32_ptr_type
, e3
,
8638 e3
= Expression::make_dereference(e3
,
8639 Expression::NIL_CHECK_NOT_NEEDED
,
8643 case Map_type::MAP_ALG_FAST64
:
8644 case Map_type::MAP_ALG_FAST64PTR
:
8646 code
= Runtime::MAPDELETE_FAST64
;
8647 Type
* uint64_type
= Type::lookup_integer_type("uint64");
8648 Type
* uint64_ptr_type
= Type::make_pointer_type(uint64_type
);
8649 e3
= Expression::make_unary(OPERATOR_AND
, e3
, loc
);
8650 e3
= Expression::make_unsafe_cast(uint64_ptr_type
, e3
,
8652 e3
= Expression::make_dereference(e3
,
8653 Expression::NIL_CHECK_NOT_NEEDED
,
8657 case Map_type::MAP_ALG_FASTSTR
:
8658 code
= Runtime::MAPDELETE_FASTSTR
;
8661 code
= Runtime::MAPDELETE
;
8663 // If the call to delete is deferred, and is in a loop,
8664 // then the loop will only have a single instance of the
8665 // temporary variable. Passing the address of the
8666 // temporary variable here means that the deferred call
8667 // will see the last value in the loop, not the current
8668 // value. So for this unusual case copy the value into
8670 if (!this->is_deferred())
8671 e3
= Expression::make_unary(OPERATOR_AND
, e3
, loc
);
8674 Expression
* a
= Expression::make_allocation(mt
->key_type(),
8676 Temporary_statement
* atemp
=
8677 Statement::make_temporary(NULL
, a
, loc
);
8678 inserter
->insert(atemp
);
8680 a
= Expression::make_temporary_reference(atemp
, loc
);
8681 a
= Expression::make_dereference(a
, NIL_CHECK_NOT_NEEDED
, loc
);
8682 Statement
* s
= Statement::make_assignment(a
, e3
, loc
);
8683 inserter
->insert(s
);
8685 e3
= Expression::make_temporary_reference(atemp
, loc
);
8689 return Runtime::make_call(code
, loc
, 3, e1
, e2
, e3
);
8696 // Lower a make expression.
8699 Builtin_call_expression::lower_make(Statement_inserter
* inserter
)
8701 Location loc
= this->location();
8703 const Expression_list
* args
= this->args();
8704 if (args
== NULL
|| args
->size() < 1)
8706 this->report_error(_("not enough arguments"));
8707 return Expression::make_error(this->location());
8710 Expression_list::const_iterator parg
= args
->begin();
8712 Expression
* first_arg
= *parg
;
8713 if (!first_arg
->is_type_expression())
8715 go_error_at(first_arg
->location(), "expected type");
8716 this->set_is_error();
8717 return Expression::make_error(this->location());
8719 Type
* type
= first_arg
->type();
8721 if (!type
->in_heap())
8722 go_error_at(first_arg
->location(),
8723 "cannot make slice of go:notinheap type");
8725 bool is_slice
= false;
8726 bool is_map
= false;
8727 bool is_chan
= false;
8728 if (type
->is_slice_type())
8730 else if (type
->map_type() != NULL
)
8732 else if (type
->channel_type() != NULL
)
8736 this->report_error(_("invalid type for make function"));
8737 return Expression::make_error(this->location());
8740 Type_context
int_context(Type::lookup_integer_type("int"), false);
8743 Expression
* len_arg
;
8744 bool len_small
= false;
8745 if (parg
== args
->end())
8749 this->report_error(_("length required when allocating a slice"));
8750 return Expression::make_error(this->location());
8752 len_arg
= Expression::make_integer_ul(0, NULL
, loc
);
8758 len_arg
->determine_type(&int_context
);
8759 if (len_arg
->type()->integer_type() == NULL
)
8761 go_error_at(len_arg
->location(), "non-integer len argument in make");
8762 return Expression::make_error(this->location());
8764 if (!this->check_int_value(len_arg
, true, &len_small
))
8765 return Expression::make_error(this->location());
8769 Expression
* cap_arg
= NULL
;
8770 bool cap_small
= false;
8771 Numeric_constant nclen
;
8772 Numeric_constant nccap
;
8775 if (is_slice
&& parg
!= args
->end())
8778 cap_arg
->determine_type(&int_context
);
8779 if (cap_arg
->type()->integer_type() == NULL
)
8781 go_error_at(cap_arg
->location(), "non-integer cap argument in make");
8782 return Expression::make_error(this->location());
8784 if (!this->check_int_value(cap_arg
, false, &cap_small
))
8785 return Expression::make_error(this->location());
8787 if (len_arg
->numeric_constant_value(&nclen
)
8788 && cap_arg
->numeric_constant_value(&nccap
)
8789 && nclen
.to_unsigned_long(&vlen
) == Numeric_constant::NC_UL_VALID
8790 && nccap
.to_unsigned_long(&vcap
) == Numeric_constant::NC_UL_VALID
8793 this->report_error(_("len larger than cap"));
8794 return Expression::make_error(this->location());
8800 if (parg
!= args
->end())
8802 this->report_error(_("too many arguments to make"));
8803 return Expression::make_error(this->location());
8806 Location type_loc
= first_arg
->location();
8811 Temporary_statement
* len_temp
= NULL
;
8812 if (!len_arg
->is_constant())
8814 len_temp
= Statement::make_temporary(NULL
, len_arg
, loc
);
8815 inserter
->insert(len_temp
);
8816 len_arg
= Expression::make_temporary_reference(len_temp
, loc
);
8819 if (cap_arg
== NULL
)
8821 cap_small
= len_small
;
8822 if (len_temp
== NULL
)
8823 cap_arg
= len_arg
->copy();
8825 cap_arg
= Expression::make_temporary_reference(len_temp
, loc
);
8827 else if (!cap_arg
->is_constant())
8829 Temporary_statement
* cap_temp
= Statement::make_temporary(NULL
,
8832 inserter
->insert(cap_temp
);
8833 cap_arg
= Expression::make_temporary_reference(cap_temp
, loc
);
8836 Type
* et
= type
->array_type()->element_type();
8837 Expression
* type_arg
= Expression::make_type_descriptor(et
, type_loc
);
8838 Runtime::Function code
= Runtime::MAKESLICE
;
8839 if (!len_small
|| !cap_small
)
8840 code
= Runtime::MAKESLICE64
;
8841 Expression
* mem
= Runtime::make_call(code
, loc
, 3, type_arg
, len_arg
,
8843 mem
= Expression::make_unsafe_cast(Type::make_pointer_type(et
), mem
,
8845 Type
* int_type
= Type::lookup_integer_type("int");
8846 len_arg
= Expression::make_cast(int_type
, len_arg
->copy(), loc
);
8847 cap_arg
= Expression::make_cast(int_type
, cap_arg
->copy(), loc
);
8848 call
= Expression::make_slice_value(type
, mem
, len_arg
, cap_arg
, loc
);
8852 Expression
* type_arg
= Expression::make_type_descriptor(type
, type_loc
);
8854 call
= Runtime::make_call(Runtime::MAKEMAP64
, loc
, 3, type_arg
,
8856 Expression::make_nil(loc
));
8859 if (len_arg
->numeric_constant_value(&nclen
)
8860 && nclen
.to_unsigned_long(&vlen
) == Numeric_constant::NC_UL_VALID
8861 && vlen
<= Map_type::bucket_size
)
8862 call
= Runtime::make_call(Runtime::MAKEMAP_SMALL
, loc
, 0);
8864 call
= Runtime::make_call(Runtime::MAKEMAP
, loc
, 3, type_arg
,
8866 Expression::make_nil(loc
));
8871 Expression
* type_arg
= Expression::make_type_descriptor(type
, type_loc
);
8872 Runtime::Function code
= Runtime::MAKECHAN
;
8874 code
= Runtime::MAKECHAN64
;
8875 call
= Runtime::make_call(code
, loc
, 2, type_arg
, len_arg
);
8880 return Expression::make_unsafe_cast(type
, call
, loc
);
8883 // Flatten a call to the predeclared append function. We do this in
8884 // the flatten phase, not the lowering phase, so that we run after
8885 // type checking and after order_evaluations. If ASSIGN_LHS is not
8886 // NULL, this append is the right-hand-side of an assignment and
8887 // ASSIGN_LHS is the left-hand-side; in that case, set LHS directly
8888 // rather than returning a slice. This lets us omit a write barrier
8889 // in common cases like a = append(a, ...) when the slice does not
8890 // need to grow. ENCLOSING is not NULL iff ASSIGN_LHS is not NULL.
8893 Builtin_call_expression::flatten_append(Gogo
* gogo
, Named_object
* function
,
8894 Statement_inserter
* inserter
,
8895 Expression
* assign_lhs
,
8898 if (this->is_error_expression())
8901 Location loc
= this->location();
8903 const Expression_list
* args
= this->args();
8904 go_assert(args
!= NULL
&& !args
->empty());
8906 Type
* slice_type
= args
->front()->type();
8907 go_assert(slice_type
->is_slice_type());
8908 Type
* element_type
= slice_type
->array_type()->element_type();
8910 if (args
->size() == 1)
8912 // append(s) evaluates to s.
8913 if (assign_lhs
!= NULL
)
8915 return args
->front();
8918 Type
* int_type
= Type::lookup_integer_type("int");
8919 Type
* uint_type
= Type::lookup_integer_type("uint");
8922 // append(s1, s2...)
8924 // append(s1, a1, a2, a3, ...)
8927 Temporary_statement
* s1tmp
= Statement::make_temporary(NULL
, args
->front(),
8929 inserter
->insert(s1tmp
);
8931 // l1tmp := len(s1tmp)
8932 Named_object
* lenfn
= gogo
->lookup_global("len");
8933 Expression
* lenref
= Expression::make_func_reference(lenfn
, NULL
, loc
);
8934 Expression_list
* call_args
= new Expression_list();
8935 call_args
->push_back(Expression::make_temporary_reference(s1tmp
, loc
));
8936 Expression
* len
= Expression::make_call(lenref
, call_args
, false, loc
);
8937 gogo
->lower_expression(function
, inserter
, &len
);
8938 gogo
->flatten_expression(function
, inserter
, &len
);
8939 Temporary_statement
* l1tmp
= Statement::make_temporary(int_type
, len
, loc
);
8940 inserter
->insert(l1tmp
);
8942 Temporary_statement
* s2tmp
= NULL
;
8943 Temporary_statement
* l2tmp
= NULL
;
8944 Expression_list
* add
= NULL
;
8946 Call_expression
* makecall
= NULL
;
8947 if (this->is_varargs())
8949 go_assert(args
->size() == 2);
8951 std::pair
<Call_expression
*, Temporary_statement
*> p
=
8952 Expression::find_makeslice_call(args
->back());
8954 if (makecall
!= NULL
)
8957 // append(s, make([]T, len[, cap])...))
8958 // which has already been lowered to
8959 // append(s, runtime.makeslice(T, len, cap)).
8960 // We will optimize this to directly zeroing the tail,
8961 // instead of allocating a new slice then copy.
8963 // Retrieve the length and capacity. Cannot reference s2 as
8964 // we will remove the makeslice call.
8965 Expression
* len_arg
= makecall
->args()->at(1);
8966 len_arg
= Expression::make_cast(int_type
, len_arg
, loc
);
8967 l2tmp
= Statement::make_temporary(int_type
, len_arg
, loc
);
8968 inserter
->insert(l2tmp
);
8970 Expression
* cap_arg
= makecall
->args()->at(2);
8971 cap_arg
= Expression::make_cast(int_type
, cap_arg
, loc
);
8972 Temporary_statement
* c2tmp
=
8973 Statement::make_temporary(int_type
, cap_arg
, loc
);
8974 inserter
->insert(c2tmp
);
8976 // Check bad len/cap here.
8977 // checkmakeslice(type, len, cap)
8978 // (Note that if len and cap are constants, we won't see a
8979 // makeslice call here, as it will be rewritten to a stack
8980 // allocated array by Mark_address_taken::expression.)
8981 Expression
* elem
= Expression::make_type_descriptor(element_type
,
8983 len2
= Expression::make_temporary_reference(l2tmp
, loc
);
8984 Expression
* cap2
= Expression::make_temporary_reference(c2tmp
, loc
);
8985 Expression
* check
= Runtime::make_call(Runtime::CHECK_MAKE_SLICE
,
8986 loc
, 3, elem
, len2
, cap2
);
8987 gogo
->lower_expression(function
, inserter
, &check
);
8988 gogo
->flatten_expression(function
, inserter
, &check
);
8989 Statement
* s
= Statement::make_statement(check
, false);
8990 inserter
->insert(s
);
8992 // Remove the original makeslice call.
8993 Temporary_statement
* ts
= p
.second
;
8994 if (ts
!= NULL
&& ts
->uses() == 1)
8995 ts
->set_init(Expression::make_nil(loc
));
9000 s2tmp
= Statement::make_temporary(NULL
, args
->back(), loc
);
9001 inserter
->insert(s2tmp
);
9003 // l2tmp := len(s2tmp)
9004 lenref
= Expression::make_func_reference(lenfn
, NULL
, loc
);
9005 call_args
= new Expression_list();
9006 call_args
->push_back(Expression::make_temporary_reference(s2tmp
, loc
));
9007 len
= Expression::make_call(lenref
, call_args
, false, loc
);
9008 gogo
->lower_expression(function
, inserter
, &len
);
9009 gogo
->flatten_expression(function
, inserter
, &len
);
9010 l2tmp
= Statement::make_temporary(int_type
, len
, loc
);
9011 inserter
->insert(l2tmp
);
9015 len2
= Expression::make_temporary_reference(l2tmp
, loc
);
9019 // We have to ensure that all the arguments are in variables
9020 // now, because otherwise if one of them is an index expression
9021 // into the current slice we could overwrite it before we fetch
9023 add
= new Expression_list();
9024 Expression_list::const_iterator pa
= args
->begin();
9025 for (++pa
; pa
!= args
->end(); ++pa
)
9027 if ((*pa
)->is_variable())
9028 add
->push_back(*pa
);
9031 Temporary_statement
* tmp
= Statement::make_temporary(NULL
, *pa
,
9033 inserter
->insert(tmp
);
9034 add
->push_back(Expression::make_temporary_reference(tmp
, loc
));
9039 len2
= Expression::make_integer_ul(add
->size(), int_type
, loc
);
9042 // ntmp := l1tmp + len2
9043 Expression
* ref
= Expression::make_temporary_reference(l1tmp
, loc
);
9044 Expression
* sum
= Expression::make_binary(OPERATOR_PLUS
, ref
, len2
, loc
);
9045 gogo
->lower_expression(function
, inserter
, &sum
);
9046 gogo
->flatten_expression(function
, inserter
, &sum
);
9047 Temporary_statement
* ntmp
= Statement::make_temporary(int_type
, sum
, loc
);
9048 inserter
->insert(ntmp
);
9050 // s1tmp = uint(ntmp) > uint(cap(s1tmp)) ?
9051 // growslice(type, s1tmp, ntmp) :
9053 // Using uint here means that if the computation of ntmp overflowed,
9054 // we will call growslice which will panic.
9056 Named_object
* capfn
= gogo
->lookup_global("cap");
9057 Expression
* capref
= Expression::make_func_reference(capfn
, NULL
, loc
);
9058 call_args
= new Expression_list();
9059 call_args
->push_back(Expression::make_temporary_reference(s1tmp
, loc
));
9060 Expression
* cap
= Expression::make_call(capref
, call_args
, false, loc
);
9061 gogo
->lower_expression(function
, inserter
, &cap
);
9062 gogo
->flatten_expression(function
, inserter
, &cap
);
9063 Temporary_statement
* c1tmp
= Statement::make_temporary(int_type
, cap
, loc
);
9064 inserter
->insert(c1tmp
);
9066 Expression
* left
= Expression::make_temporary_reference(ntmp
, loc
);
9067 left
= Expression::make_cast(uint_type
, left
, loc
);
9068 Expression
* right
= Expression::make_temporary_reference(c1tmp
, loc
);
9069 right
= Expression::make_cast(uint_type
, right
, loc
);
9071 Expression
* cond
= Expression::make_binary(OPERATOR_GT
, left
, right
, loc
);
9073 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
9074 Expression
* a1
= Expression::make_type_descriptor(element_type
, loc
);
9075 Expression
* a2
= Expression::make_temporary_reference(s1tmp
, loc
);
9076 a2
= slice_type
->array_type()->get_value_pointer(gogo
, a2
, false);
9077 a2
= Expression::make_cast(unsafe_ptr_type
, a2
, loc
);
9078 Expression
* a3
= Expression::make_temporary_reference(l1tmp
, loc
);
9079 Expression
* a4
= Expression::make_temporary_reference(c1tmp
, loc
);
9080 Expression
* a5
= Expression::make_temporary_reference(ntmp
, loc
);
9081 Expression
* call
= Runtime::make_call(Runtime::GROWSLICE
, loc
, 5,
9082 a1
, a2
, a3
, a4
, a5
);
9083 call
= Expression::make_unsafe_cast(slice_type
, call
, loc
);
9085 ref
= Expression::make_temporary_reference(s1tmp
, loc
);
9086 Expression
* zero
= Expression::make_integer_ul(0, int_type
, loc
);
9087 Expression
* ref2
= Expression::make_temporary_reference(ntmp
, loc
);
9088 ref
= Expression::make_array_index(ref
, zero
, ref2
, NULL
, loc
);
9089 ref
->array_index_expression()->set_needs_bounds_check(false);
9091 if (assign_lhs
== NULL
)
9093 Expression
* rhs
= Expression::make_conditional(cond
, call
, ref
, loc
);
9095 gogo
->lower_expression(function
, inserter
, &rhs
);
9096 gogo
->flatten_expression(function
, inserter
, &rhs
);
9098 ref
= Expression::make_temporary_reference(s1tmp
, loc
);
9099 Statement
* assign
= Statement::make_assignment(ref
, rhs
, loc
);
9100 inserter
->insert(assign
);
9104 gogo
->lower_expression(function
, inserter
, &cond
);
9105 gogo
->flatten_expression(function
, inserter
, &cond
);
9106 gogo
->lower_expression(function
, inserter
, &call
);
9107 gogo
->flatten_expression(function
, inserter
, &call
);
9108 gogo
->lower_expression(function
, inserter
, &ref
);
9109 gogo
->flatten_expression(function
, inserter
, &ref
);
9111 Block
* then_block
= new Block(enclosing
, loc
);
9112 Assignment_statement
* assign
=
9113 Statement::make_assignment(assign_lhs
, call
, loc
);
9114 then_block
->add_statement(assign
);
9116 Block
* else_block
= new Block(enclosing
, loc
);
9117 assign
= Statement::make_assignment(assign_lhs
->copy(), ref
, loc
);
9118 // This assignment will not change the pointer value, so it does
9119 // not need a write barrier.
9120 assign
->set_omit_write_barrier();
9121 else_block
->add_statement(assign
);
9123 Statement
* s
= Statement::make_if_statement(cond
, then_block
,
9125 inserter
->insert(s
);
9127 ref
= Expression::make_temporary_reference(s1tmp
, loc
);
9128 assign
= Statement::make_assignment(ref
, assign_lhs
->copy(), loc
);
9129 inserter
->insert(assign
);
9132 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
9134 if (this->is_varargs())
9136 if (makecall
!= NULL
)
9138 // memclr(&s1tmp[l1tmp], l2tmp*sizeof(elem))
9139 a1
= Expression::make_temporary_reference(s1tmp
, loc
);
9140 ref
= Expression::make_temporary_reference(l1tmp
, loc
);
9141 a1
= Expression::make_array_index(a1
, ref
, NULL
, NULL
, loc
);
9142 a1
->array_index_expression()->set_needs_bounds_check(false);
9143 a1
= Expression::make_unary(OPERATOR_AND
, a1
, loc
);
9145 ref
= Expression::make_temporary_reference(l2tmp
, loc
);
9146 ref
= Expression::make_cast(uintptr_type
, ref
, loc
);
9147 a2
= Expression::make_type_info(element_type
, TYPE_INFO_SIZE
);
9148 a2
= Expression::make_binary(OPERATOR_MULT
, a2
, ref
, loc
);
9150 if (element_type
->has_pointer())
9151 call
= Runtime::make_call(Runtime::MEMCLRHASPTR
, loc
, 2, a1
, a2
);
9154 Type
* int32_type
= Type::lookup_integer_type("int32");
9155 zero
= Expression::make_integer_ul(0, int32_type
, loc
);
9156 call
= Runtime::make_call(Runtime::BUILTIN_MEMSET
, loc
, 3, a1
,
9160 if (element_type
->has_pointer())
9162 // For a slice containing pointers, growslice already zeroed
9163 // the memory. We only need to zero in non-growing case.
9164 // Note: growslice does not zero the memory in non-pointer case.
9165 ref
= Expression::make_temporary_reference(ntmp
, loc
);
9166 ref
= Expression::make_cast(uint_type
, ref
, loc
);
9167 ref2
= Expression::make_temporary_reference(c1tmp
, loc
);
9168 ref2
= Expression::make_cast(uint_type
, ref2
, loc
);
9169 cond
= Expression::make_binary(OPERATOR_GT
, ref
, ref2
, loc
);
9170 zero
= Expression::make_integer_ul(0, int_type
, loc
);
9171 call
= Expression::make_conditional(cond
, call
, zero
, loc
);
9176 if (element_type
->has_pointer())
9178 // copy(s1tmp[l1tmp:], s2tmp)
9179 a1
= Expression::make_temporary_reference(s1tmp
, loc
);
9180 ref
= Expression::make_temporary_reference(l1tmp
, loc
);
9181 Expression
* nil
= Expression::make_nil(loc
);
9182 a1
= Expression::make_array_index(a1
, ref
, nil
, NULL
, loc
);
9183 a1
->array_index_expression()->set_needs_bounds_check(false);
9185 a2
= Expression::make_temporary_reference(s2tmp
, loc
);
9187 Named_object
* copyfn
= gogo
->lookup_global("copy");
9188 Expression
* copyref
= Expression::make_func_reference(copyfn
, NULL
, loc
);
9189 call_args
= new Expression_list();
9190 call_args
->push_back(a1
);
9191 call_args
->push_back(a2
);
9192 call
= Expression::make_call(copyref
, call_args
, false, loc
);
9196 // memmove(&s1tmp[l1tmp], s2tmp.ptr, l2tmp*sizeof(elem))
9197 a1
= Expression::make_temporary_reference(s1tmp
, loc
);
9198 ref
= Expression::make_temporary_reference(l1tmp
, loc
);
9199 a1
= Expression::make_array_index(a1
, ref
, NULL
, NULL
, loc
);
9200 a1
->array_index_expression()->set_needs_bounds_check(false);
9201 a1
= Expression::make_unary(OPERATOR_AND
, a1
, loc
);
9203 a2
= Expression::make_temporary_reference(s2tmp
, loc
);
9204 a2
= (a2
->type()->is_string_type()
9205 ? Expression::make_string_info(a2
,
9208 : Expression::make_slice_info(a2
,
9209 SLICE_INFO_VALUE_POINTER
,
9212 ref
= Expression::make_temporary_reference(l2tmp
, loc
);
9213 ref
= Expression::make_cast(uintptr_type
, ref
, loc
);
9214 a3
= Expression::make_type_info(element_type
, TYPE_INFO_SIZE
);
9215 a3
= Expression::make_binary(OPERATOR_MULT
, a3
, ref
, loc
);
9217 call
= Runtime::make_call(Runtime::BUILTIN_MEMMOVE
, loc
, 3,
9221 gogo
->lower_expression(function
, inserter
, &call
);
9222 gogo
->flatten_expression(function
, inserter
, &call
);
9223 inserter
->insert(Statement::make_statement(call
, false));
9227 // For each argument:
9228 // s1tmp[l1tmp+i] = a
9229 unsigned long i
= 0;
9230 for (Expression_list::const_iterator pa
= add
->begin();
9234 ref
= Expression::make_temporary_reference(s1tmp
, loc
);
9235 ref2
= Expression::make_temporary_reference(l1tmp
, loc
);
9236 Expression
* off
= Expression::make_integer_ul(i
, int_type
, loc
);
9237 ref2
= Expression::make_binary(OPERATOR_PLUS
, ref2
, off
, loc
);
9238 Expression
* lhs
= Expression::make_array_index(ref
, ref2
, NULL
,
9240 lhs
->array_index_expression()->set_needs_bounds_check(false);
9241 gogo
->lower_expression(function
, inserter
, &lhs
);
9242 gogo
->flatten_expression(function
, inserter
, &lhs
);
9243 Expression
* elem
= *pa
;
9244 if (!Type::are_identical(element_type
, elem
->type(), 0, NULL
)
9245 && element_type
->interface_type() != NULL
)
9246 elem
= Expression::make_cast(element_type
, elem
, loc
);
9247 // The flatten pass runs after the write barrier pass, so we
9248 // need to insert a write barrier here if necessary.
9249 // However, if ASSIGN_LHS is not NULL, we have been called
9250 // directly before the write barrier pass.
9252 if (assign_lhs
!= NULL
9253 || !gogo
->assign_needs_write_barrier(lhs
, NULL
))
9254 assign
= Statement::make_assignment(lhs
, elem
, loc
);
9257 Function
* f
= function
== NULL
? NULL
: function
->func_value();
9258 assign
= gogo
->assign_with_write_barrier(f
, NULL
, inserter
,
9261 inserter
->insert(assign
);
9265 if (assign_lhs
!= NULL
)
9268 return Expression::make_temporary_reference(s1tmp
, loc
);
9271 // Return whether an expression has an integer value. Report an error
9272 // if not. This is used when handling calls to the predeclared make
9273 // function. Set *SMALL if the value is known to fit in type "int".
9276 Builtin_call_expression::check_int_value(Expression
* e
, bool is_length
,
9281 Numeric_constant nc
;
9282 if (e
->numeric_constant_value(&nc
))
9285 switch (nc
.to_unsigned_long(&v
))
9287 case Numeric_constant::NC_UL_VALID
:
9289 case Numeric_constant::NC_UL_NOTINT
:
9290 go_error_at(e
->location(), "non-integer %s argument to make",
9291 is_length
? "len" : "cap");
9293 case Numeric_constant::NC_UL_NEGATIVE
:
9294 go_error_at(e
->location(), "negative %s argument to make",
9295 is_length
? "len" : "cap");
9297 case Numeric_constant::NC_UL_BIG
:
9298 // We don't want to give a compile-time error for a 64-bit
9299 // value on a 32-bit target.
9304 if (!nc
.to_int(&val
))
9306 int bits
= mpz_sizeinbase(val
, 2);
9308 Type
* int_type
= Type::lookup_integer_type("int");
9309 if (bits
>= int_type
->integer_type()->bits())
9311 go_error_at(e
->location(), "%s argument too large for make",
9312 is_length
? "len" : "cap");
9320 if (e
->type()->integer_type() != NULL
)
9322 int ebits
= e
->type()->integer_type()->bits();
9323 int intbits
= Type::lookup_integer_type("int")->integer_type()->bits();
9325 // We can treat ebits == intbits as small even for an unsigned
9326 // integer type, because we will convert the value to int and
9327 // then reject it in the runtime if it is negative.
9328 *small
= ebits
<= intbits
;
9333 go_error_at(e
->location(), "non-integer %s argument to make",
9334 is_length
? "len" : "cap");
9338 // Return the type of the real or imag functions, given the type of
9339 // the argument. We need to map complex64 to float32 and complex128
9340 // to float64, so it has to be done by name. This returns NULL if it
9341 // can't figure out the type.
9344 Builtin_call_expression::real_imag_type(Type
* arg_type
)
9346 if (arg_type
== NULL
|| arg_type
->is_abstract())
9348 Named_type
* nt
= arg_type
->named_type();
9351 while (nt
->real_type()->named_type() != NULL
)
9352 nt
= nt
->real_type()->named_type();
9353 if (nt
->name() == "complex64")
9354 return Type::lookup_float_type("float32");
9355 else if (nt
->name() == "complex128")
9356 return Type::lookup_float_type("float64");
9361 // Return the type of the complex function, given the type of one of the
9362 // argments. Like real_imag_type, we have to map by name.
9365 Builtin_call_expression::complex_type(Type
* arg_type
)
9367 if (arg_type
== NULL
|| arg_type
->is_abstract())
9369 Named_type
* nt
= arg_type
->named_type();
9372 while (nt
->real_type()->named_type() != NULL
)
9373 nt
= nt
->real_type()->named_type();
9374 if (nt
->name() == "float32")
9375 return Type::lookup_complex_type("complex64");
9376 else if (nt
->name() == "float64")
9377 return Type::lookup_complex_type("complex128");
9382 // Return a single argument, or NULL if there isn't one.
9385 Builtin_call_expression::one_arg() const
9387 const Expression_list
* args
= this->args();
9388 if (args
== NULL
|| args
->size() != 1)
9390 return args
->front();
9393 // A traversal class which looks for a call or receive expression.
9395 class Find_call_expression
: public Traverse
9398 Find_call_expression()
9399 : Traverse(traverse_expressions
),
9404 expression(Expression
**);
9408 { return this->found_
; }
9415 Find_call_expression::expression(Expression
** pexpr
)
9417 Expression
* expr
= *pexpr
;
9418 if (!expr
->is_constant()
9419 && (expr
->call_expression() != NULL
9420 || expr
->receive_expression() != NULL
))
9422 this->found_
= true;
9423 return TRAVERSE_EXIT
;
9425 return TRAVERSE_CONTINUE
;
9428 // Return whether calling len or cap on EXPR, of array type, is a
9429 // constant. The language spec says "the expressions len(s) and
9430 // cap(s) are constants if the type of s is an array or pointer to an
9431 // array and the expression s does not contain channel receives or
9432 // (non-constant) function calls."
9435 Builtin_call_expression::array_len_is_constant(Expression
* expr
)
9437 go_assert(expr
->type()->deref()->array_type() != NULL
9438 && !expr
->type()->deref()->is_slice_type());
9439 if (expr
->is_constant())
9441 Find_call_expression find_call
;
9442 Expression::traverse(&expr
, &find_call
);
9443 return !find_call
.found();
9446 // Return whether this is constant: len of a string constant, or len
9447 // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof,
9451 Builtin_call_expression::do_is_constant() const
9453 if (this->is_error_expression())
9455 switch (this->code_
)
9463 Expression
* arg
= this->one_arg();
9466 Type
* arg_type
= arg
->type();
9467 if (arg_type
->is_error())
9470 if (arg_type
->points_to() != NULL
9471 && arg_type
->points_to()->array_type() != NULL
9472 && !arg_type
->points_to()->is_slice_type())
9473 arg_type
= arg_type
->points_to();
9475 if (arg_type
->array_type() != NULL
9476 && arg_type
->array_type()->length() != NULL
)
9479 bool ret
= Builtin_call_expression::array_len_is_constant(arg
);
9480 this->seen_
= false;
9484 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
9487 bool ret
= arg
->is_constant();
9488 this->seen_
= false;
9494 case BUILTIN_SIZEOF
:
9495 case BUILTIN_ALIGNOF
:
9496 return this->one_arg() != NULL
;
9498 case BUILTIN_OFFSETOF
:
9500 Expression
* arg
= this->one_arg();
9503 return arg
->field_reference_expression() != NULL
;
9506 case BUILTIN_COMPLEX
:
9508 const Expression_list
* args
= this->args();
9509 if (args
!= NULL
&& args
->size() == 2)
9510 return args
->front()->is_constant() && args
->back()->is_constant();
9517 Expression
* arg
= this->one_arg();
9518 return arg
!= NULL
&& arg
->is_constant();
9528 // Return a numeric constant if possible.
9531 Builtin_call_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
9533 if (this->code_
== BUILTIN_LEN
9534 || this->code_
== BUILTIN_CAP
)
9536 Expression
* arg
= this->one_arg();
9539 Type
* arg_type
= arg
->type();
9540 if (arg_type
->is_error())
9543 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
9546 if (arg
->string_constant_value(&sval
))
9548 nc
->set_unsigned_long(Type::lookup_integer_type("int"),
9554 if (arg_type
->points_to() != NULL
9555 && arg_type
->points_to()->array_type() != NULL
9556 && !arg_type
->points_to()->is_slice_type())
9557 arg_type
= arg_type
->points_to();
9559 if (arg_type
->array_type() != NULL
9560 && arg_type
->array_type()->length() != NULL
)
9565 // We may be replacing this expression with a constant
9566 // during lowering, so verify the type to report any errors.
9567 // It's OK to verify an array type more than once.
9569 if (!arg_type
->is_error())
9571 Expression
* e
= arg_type
->array_type()->length();
9573 bool r
= e
->numeric_constant_value(nc
);
9574 this->seen_
= false;
9577 if (!nc
->set_type(Type::lookup_integer_type("int"), false,
9585 else if (this->code_
== BUILTIN_SIZEOF
9586 || this->code_
== BUILTIN_ALIGNOF
)
9588 Expression
* arg
= this->one_arg();
9591 Type
* arg_type
= arg
->type();
9592 if (arg_type
->is_error())
9594 if (arg_type
->is_abstract())
9595 arg_type
= arg_type
->make_non_abstract_type();
9600 if (this->code_
== BUILTIN_SIZEOF
)
9603 bool ok
= arg_type
->backend_type_size(this->gogo_
, &ret
);
9604 this->seen_
= false;
9608 else if (this->code_
== BUILTIN_ALIGNOF
)
9612 if (arg
->field_reference_expression() == NULL
)
9613 ok
= arg_type
->backend_type_align(this->gogo_
, &ret
);
9616 // Calling unsafe.Alignof(s.f) returns the alignment of
9617 // the type of f when it is used as a field in a struct.
9618 ok
= arg_type
->backend_type_field_align(this->gogo_
, &ret
);
9620 this->seen_
= false;
9628 set_mpz_from_int64(&zval
, ret
);
9629 nc
->set_int(Type::lookup_integer_type("uintptr"), zval
);
9633 else if (this->code_
== BUILTIN_OFFSETOF
)
9635 Expression
* arg
= this->one_arg();
9638 Field_reference_expression
* farg
= arg
->field_reference_expression();
9644 int64_t total_offset
= 0;
9647 Expression
* struct_expr
= farg
->expr();
9648 Type
* st
= struct_expr
->type();
9649 if (st
->struct_type() == NULL
)
9651 if (st
->named_type() != NULL
)
9652 st
->named_type()->convert(this->gogo_
);
9653 if (st
->is_error_type())
9655 go_assert(saw_errors());
9660 bool ok
= st
->struct_type()->backend_field_offset(this->gogo_
,
9661 farg
->field_index(),
9663 this->seen_
= false;
9666 total_offset
+= offset
;
9667 if (farg
->implicit() && struct_expr
->field_reference_expression() != NULL
)
9669 // Go up until we reach the original base.
9670 farg
= struct_expr
->field_reference_expression();
9676 set_mpz_from_int64(&zval
, total_offset
);
9677 nc
->set_int(Type::lookup_integer_type("uintptr"), zval
);
9681 else if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
9683 Expression
* arg
= this->one_arg();
9687 Numeric_constant argnc
;
9688 if (!arg
->numeric_constant_value(&argnc
))
9692 if (!argnc
.to_complex(&val
))
9695 Type
* type
= Builtin_call_expression::real_imag_type(argnc
.type());
9696 if (this->code_
== BUILTIN_REAL
)
9697 nc
->set_float(type
, mpc_realref(val
));
9699 nc
->set_float(type
, mpc_imagref(val
));
9703 else if (this->code_
== BUILTIN_COMPLEX
)
9705 const Expression_list
* args
= this->args();
9706 if (args
== NULL
|| args
->size() != 2)
9709 Numeric_constant rnc
;
9710 if (!args
->front()->numeric_constant_value(&rnc
))
9712 Numeric_constant inc
;
9713 if (!args
->back()->numeric_constant_value(&inc
))
9716 if (rnc
.type() != NULL
9717 && !rnc
.type()->is_abstract()
9718 && inc
.type() != NULL
9719 && !inc
.type()->is_abstract()
9720 && !Type::are_identical(rnc
.type(), inc
.type(),
9721 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
9726 if (!rnc
.to_float(&r
))
9729 if (!inc
.to_float(&i
))
9735 Type
* arg_type
= rnc
.type();
9736 if (arg_type
== NULL
|| arg_type
->is_abstract())
9737 arg_type
= inc
.type();
9740 mpc_init2(val
, mpc_precision
);
9741 mpc_set_fr_fr(val
, r
, i
, MPC_RNDNN
);
9745 Type
* type
= Builtin_call_expression::complex_type(arg_type
);
9746 nc
->set_complex(type
, val
);
9756 // Give an error if we are discarding the value of an expression which
9757 // should not normally be discarded. We don't give an error for
9758 // discarding the value of an ordinary function call, but we do for
9759 // builtin functions, purely for consistency with the gc compiler.
9762 Builtin_call_expression::do_discarding_value()
9764 switch (this->code_
)
9766 case BUILTIN_INVALID
:
9770 case BUILTIN_APPEND
:
9772 case BUILTIN_COMPLEX
:
9778 case BUILTIN_ALIGNOF
:
9779 case BUILTIN_OFFSETOF
:
9780 case BUILTIN_SIZEOF
:
9781 this->unused_value_error();
9786 case BUILTIN_DELETE
:
9789 case BUILTIN_PRINTLN
:
9790 case BUILTIN_RECOVER
:
9798 Builtin_call_expression::do_type()
9800 if (this->is_error_expression())
9801 return Type::make_error_type();
9802 switch (this->code_
)
9804 case BUILTIN_INVALID
:
9806 return Type::make_error_type();
9810 const Expression_list
* args
= this->args();
9811 if (args
== NULL
|| args
->empty())
9812 return Type::make_error_type();
9813 return Type::make_pointer_type(args
->front()->type());
9818 const Expression_list
* args
= this->args();
9819 if (args
== NULL
|| args
->empty())
9820 return Type::make_error_type();
9821 return args
->front()->type();
9827 return Type::lookup_integer_type("int");
9829 case BUILTIN_ALIGNOF
:
9830 case BUILTIN_OFFSETOF
:
9831 case BUILTIN_SIZEOF
:
9832 return Type::lookup_integer_type("uintptr");
9835 case BUILTIN_DELETE
:
9838 case BUILTIN_PRINTLN
:
9839 return Type::make_void_type();
9841 case BUILTIN_RECOVER
:
9842 return Type::make_empty_interface_type(Linemap::predeclared_location());
9844 case BUILTIN_APPEND
:
9846 const Expression_list
* args
= this->args();
9847 if (args
== NULL
|| args
->empty())
9848 return Type::make_error_type();
9849 Type
*ret
= args
->front()->type();
9850 if (!ret
->is_slice_type())
9851 return Type::make_error_type();
9858 Expression
* arg
= this->one_arg();
9860 return Type::make_error_type();
9861 Type
* t
= arg
->type();
9862 if (t
->is_abstract())
9863 t
= t
->make_non_abstract_type();
9864 t
= Builtin_call_expression::real_imag_type(t
);
9866 t
= Type::make_error_type();
9870 case BUILTIN_COMPLEX
:
9872 const Expression_list
* args
= this->args();
9873 if (args
== NULL
|| args
->size() != 2)
9874 return Type::make_error_type();
9875 Type
* t
= args
->front()->type();
9876 if (t
->is_abstract())
9878 t
= args
->back()->type();
9879 if (t
->is_abstract())
9880 t
= t
->make_non_abstract_type();
9882 t
= Builtin_call_expression::complex_type(t
);
9884 t
= Type::make_error_type();
9890 // Determine the type.
9893 Builtin_call_expression::do_determine_type(const Type_context
* context
)
9895 if (!this->determining_types())
9898 this->fn()->determine_type_no_context();
9900 const Expression_list
* args
= this->args();
9903 Type
* arg_type
= NULL
;
9904 Type
* trailing_arg_types
= NULL
;
9905 switch (this->code_
)
9908 case BUILTIN_PRINTLN
:
9909 // Do not force a large integer constant to "int".
9915 arg_type
= Builtin_call_expression::complex_type(context
->type
);
9916 if (arg_type
== NULL
)
9917 arg_type
= Type::lookup_complex_type("complex128");
9921 case BUILTIN_COMPLEX
:
9923 // For the complex function the type of one operand can
9924 // determine the type of the other, as in a binary expression.
9925 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
9926 if (arg_type
== NULL
)
9927 arg_type
= Type::lookup_float_type("float64");
9928 if (args
!= NULL
&& args
->size() == 2)
9930 Type
* t1
= args
->front()->type();
9931 Type
* t2
= args
->back()->type();
9932 if (!t1
->is_abstract())
9934 else if (!t2
->is_abstract())
9941 case BUILTIN_APPEND
:
9942 if (!this->is_varargs()
9945 && args
->front()->type()->is_slice_type())
9946 trailing_arg_types
=
9947 args
->front()->type()->array_type()->element_type();
9958 for (Expression_list::const_iterator pa
= args
->begin();
9962 Type_context subcontext
;
9963 subcontext
.type
= arg_type
;
9967 // We want to print large constants, we so can't just
9968 // use the appropriate nonabstract type. Use uint64 for
9969 // an integer if we know it is nonnegative, otherwise
9970 // use int64 for a integer, otherwise use float64 for a
9971 // float or complex128 for a complex.
9972 Type
* want_type
= NULL
;
9973 Type
* atype
= (*pa
)->type();
9974 if (atype
->is_abstract())
9976 if (atype
->integer_type() != NULL
)
9978 Numeric_constant nc
;
9979 if (this->numeric_constant_value(&nc
))
9982 if (nc
.to_int(&val
))
9984 if (mpz_sgn(val
) >= 0)
9985 want_type
= Type::lookup_integer_type("uint64");
9989 if (want_type
== NULL
)
9990 want_type
= Type::lookup_integer_type("int64");
9992 else if (atype
->float_type() != NULL
)
9993 want_type
= Type::lookup_float_type("float64");
9994 else if (atype
->complex_type() != NULL
)
9995 want_type
= Type::lookup_complex_type("complex128");
9996 else if (atype
->is_abstract_string_type())
9997 want_type
= Type::lookup_string_type();
9998 else if (atype
->is_abstract_boolean_type())
9999 want_type
= Type::lookup_bool_type();
10002 subcontext
.type
= want_type
;
10006 (*pa
)->determine_type(&subcontext
);
10008 if (trailing_arg_types
!= NULL
)
10010 arg_type
= trailing_arg_types
;
10011 trailing_arg_types
= NULL
;
10017 // If there is exactly one argument, return true. Otherwise give an
10018 // error message and return false.
10021 Builtin_call_expression::check_one_arg()
10023 const Expression_list
* args
= this->args();
10024 if (args
== NULL
|| args
->size() < 1)
10026 this->report_error(_("not enough arguments"));
10029 else if (args
->size() > 1)
10031 this->report_error(_("too many arguments"));
10034 if (args
->front()->is_error_expression()
10035 || args
->front()->type()->is_error())
10037 this->set_is_error();
10043 // Check argument types for a builtin function.
10046 Builtin_call_expression::do_check_types(Gogo
*)
10048 if (this->is_error_expression())
10050 switch (this->code_
)
10052 case BUILTIN_INVALID
:
10055 case BUILTIN_DELETE
:
10061 // The single argument may be either a string or an array or a
10062 // map or a channel, or a pointer to a closed array.
10063 if (this->check_one_arg())
10065 Type
* arg_type
= this->one_arg()->type();
10066 if (arg_type
->points_to() != NULL
10067 && arg_type
->points_to()->array_type() != NULL
10068 && !arg_type
->points_to()->is_slice_type())
10069 arg_type
= arg_type
->points_to();
10070 if (this->code_
== BUILTIN_CAP
)
10072 if (!arg_type
->is_error()
10073 && arg_type
->array_type() == NULL
10074 && arg_type
->channel_type() == NULL
)
10075 this->report_error(_("argument must be array or slice "
10080 if (!arg_type
->is_error()
10081 && !arg_type
->is_string_type()
10082 && arg_type
->array_type() == NULL
10083 && arg_type
->map_type() == NULL
10084 && arg_type
->channel_type() == NULL
)
10085 this->report_error(_("argument must be string or "
10086 "array or slice or map or channel"));
10092 case BUILTIN_PRINT
:
10093 case BUILTIN_PRINTLN
:
10095 const Expression_list
* args
= this->args();
10098 if (this->code_
== BUILTIN_PRINT
)
10099 go_warning_at(this->location(), 0,
10100 "no arguments for built-in function %<%s%>",
10101 (this->code_
== BUILTIN_PRINT
10107 for (Expression_list::const_iterator p
= args
->begin();
10111 Type
* type
= (*p
)->type();
10112 if (type
->is_error()
10113 || type
->is_string_type()
10114 || type
->integer_type() != NULL
10115 || type
->float_type() != NULL
10116 || type
->complex_type() != NULL
10117 || type
->is_boolean_type()
10118 || type
->points_to() != NULL
10119 || type
->interface_type() != NULL
10120 || type
->channel_type() != NULL
10121 || type
->map_type() != NULL
10122 || type
->function_type() != NULL
10123 || type
->is_slice_type())
10125 else if ((*p
)->is_type_expression())
10127 // If this is a type expression it's going to give
10128 // an error anyhow, so we don't need one here.
10131 this->report_error(_("unsupported argument type to "
10132 "builtin function"));
10138 case BUILTIN_CLOSE
:
10139 if (this->check_one_arg())
10141 if (this->one_arg()->type()->channel_type() == NULL
)
10142 this->report_error(_("argument must be channel"));
10143 else if (!this->one_arg()->type()->channel_type()->may_send())
10144 this->report_error(_("cannot close receive-only channel"));
10148 case BUILTIN_PANIC
:
10149 case BUILTIN_SIZEOF
:
10150 case BUILTIN_ALIGNOF
:
10151 this->check_one_arg();
10154 case BUILTIN_RECOVER
:
10155 if (this->args() != NULL
10156 && !this->args()->empty()
10157 && !this->recover_arg_is_set_
)
10158 this->report_error(_("too many arguments"));
10161 case BUILTIN_OFFSETOF
:
10162 if (this->check_one_arg())
10164 Expression
* arg
= this->one_arg();
10165 if (arg
->field_reference_expression() == NULL
)
10166 this->report_error(_("argument must be a field reference"));
10172 const Expression_list
* args
= this->args();
10173 if (args
== NULL
|| args
->size() < 2)
10175 this->report_error(_("not enough arguments"));
10178 else if (args
->size() > 2)
10180 this->report_error(_("too many arguments"));
10183 Type
* arg1_type
= args
->front()->type();
10184 Type
* arg2_type
= args
->back()->type();
10185 if (arg1_type
->is_error() || arg2_type
->is_error())
10187 this->set_is_error();
10192 if (arg1_type
->is_slice_type())
10193 e1
= arg1_type
->array_type()->element_type();
10196 this->report_error(_("left argument must be a slice"));
10200 if (arg2_type
->is_slice_type())
10202 Type
* e2
= arg2_type
->array_type()->element_type();
10203 if (!Type::are_identical(e1
, e2
, Type::COMPARE_TAGS
, NULL
))
10204 this->report_error(_("element types must be the same"));
10206 else if (arg2_type
->is_string_type())
10208 if (e1
->integer_type() == NULL
|| !e1
->integer_type()->is_byte())
10209 this->report_error(_("first argument must be []byte"));
10212 this->report_error(_("second argument must be slice or string"));
10216 case BUILTIN_APPEND
:
10218 const Expression_list
* args
= this->args();
10219 if (args
== NULL
|| args
->empty())
10221 this->report_error(_("not enough arguments"));
10225 Type
* slice_type
= args
->front()->type();
10226 if (!slice_type
->is_slice_type())
10228 if (slice_type
->is_error_type())
10230 if (slice_type
->is_nil_type())
10231 go_error_at(args
->front()->location(), "use of untyped nil");
10233 go_error_at(args
->front()->location(),
10234 "argument 1 must be a slice");
10235 this->set_is_error();
10239 Type
* element_type
= slice_type
->array_type()->element_type();
10240 if (!element_type
->in_heap())
10241 go_error_at(args
->front()->location(),
10242 "cannot append to slice of go:notinheap type");
10243 if (this->is_varargs())
10245 if (!args
->back()->type()->is_slice_type()
10246 && !args
->back()->type()->is_string_type())
10248 go_error_at(args
->back()->location(),
10249 "invalid use of %<...%> with non-slice/non-string");
10250 this->set_is_error();
10254 if (args
->size() < 2)
10256 this->report_error(_("not enough arguments"));
10259 if (args
->size() > 2)
10261 this->report_error(_("too many arguments"));
10265 if (args
->back()->type()->is_string_type()
10266 && element_type
->integer_type() != NULL
10267 && element_type
->integer_type()->is_byte())
10269 // Permit append(s1, s2...) when s1 is a slice of
10270 // bytes and s2 is a string type.
10274 // We have to test for assignment compatibility to a
10275 // slice of the element type, which is not necessarily
10276 // the same as the type of the first argument: the
10277 // first argument might have a named type.
10278 Type
* check_type
= Type::make_array_type(element_type
, NULL
);
10279 std::string reason
;
10280 if (!Type::are_assignable(check_type
, args
->back()->type(),
10283 if (reason
.empty())
10284 go_error_at(args
->back()->location(),
10285 "argument 2 has invalid type");
10287 go_error_at(args
->back()->location(),
10288 "argument 2 has invalid type (%s)",
10290 this->set_is_error();
10297 Expression_list::const_iterator pa
= args
->begin();
10299 for (++pa
; pa
!= args
->end(); ++pa
, ++i
)
10301 std::string reason
;
10302 if (!Type::are_assignable(element_type
, (*pa
)->type(),
10305 if (reason
.empty())
10306 go_error_at((*pa
)->location(),
10307 "argument %d has incompatible type", i
);
10309 go_error_at((*pa
)->location(),
10310 "argument %d has incompatible type (%s)",
10311 i
, reason
.c_str());
10312 this->set_is_error();
10321 if (this->check_one_arg())
10323 if (this->one_arg()->type()->complex_type() == NULL
)
10324 this->report_error(_("argument must have complex type"));
10328 case BUILTIN_COMPLEX
:
10330 const Expression_list
* args
= this->args();
10331 if (args
== NULL
|| args
->size() < 2)
10332 this->report_error(_("not enough arguments"));
10333 else if (args
->size() > 2)
10334 this->report_error(_("too many arguments"));
10335 else if (args
->front()->is_error_expression()
10336 || args
->front()->type()->is_error()
10337 || args
->back()->is_error_expression()
10338 || args
->back()->type()->is_error())
10339 this->set_is_error();
10340 else if (!Type::are_identical(args
->front()->type(),
10341 args
->back()->type(),
10342 Type::COMPARE_TAGS
, NULL
))
10343 this->report_error(_("complex arguments must have identical types"));
10344 else if (args
->front()->type()->float_type() == NULL
)
10345 this->report_error(_("complex arguments must have "
10346 "floating-point type"));
10356 Builtin_call_expression::do_copy()
10358 Call_expression
* bce
=
10359 new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
10360 (this->args() == NULL
10362 : this->args()->copy()),
10363 this->is_varargs(),
10366 if (this->varargs_are_lowered())
10367 bce
->set_varargs_are_lowered();
10368 if (this->is_deferred())
10369 bce
->set_is_deferred();
10370 if (this->is_concurrent())
10371 bce
->set_is_concurrent();
10375 // Return the backend representation for a builtin function.
10378 Builtin_call_expression::do_get_backend(Translate_context
* context
)
10380 Gogo
* gogo
= context
->gogo();
10381 Location location
= this->location();
10383 if (this->is_erroneous_call())
10385 go_assert(saw_errors());
10386 return gogo
->backend()->error_expression();
10389 switch (this->code_
)
10391 case BUILTIN_INVALID
:
10399 const Expression_list
* args
= this->args();
10400 go_assert(args
!= NULL
&& args
->size() == 1);
10401 Expression
* arg
= args
->front();
10402 Type
* arg_type
= arg
->type();
10406 go_assert(saw_errors());
10407 return context
->backend()->error_expression();
10409 this->seen_
= true;
10410 this->seen_
= false;
10411 if (arg_type
->points_to() != NULL
)
10413 arg_type
= arg_type
->points_to();
10414 go_assert(arg_type
->array_type() != NULL
10415 && !arg_type
->is_slice_type());
10416 arg
= Expression::make_dereference(arg
, NIL_CHECK_DEFAULT
,
10420 Type
* int_type
= Type::lookup_integer_type("int");
10422 if (this->code_
== BUILTIN_LEN
)
10424 if (arg_type
->is_string_type())
10425 val
= Expression::make_string_info(arg
, STRING_INFO_LENGTH
,
10427 else if (arg_type
->array_type() != NULL
)
10431 go_assert(saw_errors());
10432 return context
->backend()->error_expression();
10434 this->seen_
= true;
10435 val
= arg_type
->array_type()->get_length(gogo
, arg
);
10436 this->seen_
= false;
10438 else if (arg_type
->map_type() != NULL
10439 || arg_type
->channel_type() != NULL
)
10441 // The first field is the length. If the pointer is
10442 // nil, the length is zero.
10443 Type
* pint_type
= Type::make_pointer_type(int_type
);
10444 arg
= Expression::make_unsafe_cast(pint_type
, arg
, location
);
10445 Expression
* nil
= Expression::make_nil(location
);
10446 nil
= Expression::make_cast(pint_type
, nil
, location
);
10447 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
,
10448 arg
, nil
, location
);
10449 Expression
* zero
= Expression::make_integer_ul(0, int_type
,
10451 Expression
* indir
=
10452 Expression::make_dereference(arg
, NIL_CHECK_NOT_NEEDED
,
10454 val
= Expression::make_conditional(cmp
, zero
, indir
, location
);
10461 if (arg_type
->array_type() != NULL
)
10465 go_assert(saw_errors());
10466 return context
->backend()->error_expression();
10468 this->seen_
= true;
10469 val
= arg_type
->array_type()->get_capacity(gogo
, arg
);
10470 this->seen_
= false;
10472 else if (arg_type
->channel_type() != NULL
)
10474 // The second field is the capacity. If the pointer
10475 // is nil, the capacity is zero.
10476 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
10477 Type
* pint_type
= Type::make_pointer_type(int_type
);
10478 Expression
* parg
= Expression::make_unsafe_cast(uintptr_type
,
10481 int off
= int_type
->integer_type()->bits() / 8;
10482 Expression
* eoff
= Expression::make_integer_ul(off
,
10485 parg
= Expression::make_binary(OPERATOR_PLUS
, parg
, eoff
,
10487 parg
= Expression::make_unsafe_cast(pint_type
, parg
, location
);
10488 Expression
* nil
= Expression::make_nil(location
);
10489 nil
= Expression::make_cast(pint_type
, nil
, location
);
10490 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
,
10491 arg
, nil
, location
);
10492 Expression
* zero
= Expression::make_integer_ul(0, int_type
,
10494 Expression
* indir
=
10495 Expression::make_dereference(parg
, NIL_CHECK_NOT_NEEDED
,
10497 val
= Expression::make_conditional(cmp
, zero
, indir
, location
);
10503 return Expression::make_cast(int_type
, val
,
10504 location
)->get_backend(context
);
10507 case BUILTIN_PRINT
:
10508 case BUILTIN_PRINTLN
:
10510 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
10512 Expression
* print_stmts
= Runtime::make_call(Runtime::PRINTLOCK
,
10515 const Expression_list
* call_args
= this->args();
10516 if (call_args
!= NULL
)
10518 for (Expression_list::const_iterator p
= call_args
->begin();
10519 p
!= call_args
->end();
10522 if (is_ln
&& p
!= call_args
->begin())
10524 Expression
* print_space
=
10525 Runtime::make_call(Runtime::PRINTSP
, location
, 0);
10528 Expression::make_compound(print_stmts
, print_space
,
10532 Expression
* arg
= *p
;
10533 Type
* type
= arg
->type();
10534 Runtime::Function code
;
10535 if (type
->is_string_type())
10536 code
= Runtime::PRINTSTRING
;
10537 else if (type
->integer_type() != NULL
10538 && type
->integer_type()->is_unsigned())
10540 Type
* itype
= Type::lookup_integer_type("uint64");
10541 arg
= Expression::make_cast(itype
, arg
, location
);
10542 if (gogo
->compiling_runtime()
10543 && type
->named_type() != NULL
10544 && gogo
->unpack_hidden_name(type
->named_type()->name())
10546 code
= Runtime::PRINTHEX
;
10548 code
= Runtime::PRINTUINT
;
10550 else if (type
->integer_type() != NULL
)
10552 Type
* itype
= Type::lookup_integer_type("int64");
10553 arg
= Expression::make_cast(itype
, arg
, location
);
10554 code
= Runtime::PRINTINT
;
10556 else if (type
->float_type() != NULL
)
10558 Type
* dtype
= Type::lookup_float_type("float64");
10559 arg
= Expression::make_cast(dtype
, arg
, location
);
10560 code
= Runtime::PRINTFLOAT
;
10562 else if (type
->complex_type() != NULL
)
10564 Type
* ctype
= Type::lookup_complex_type("complex128");
10565 arg
= Expression::make_cast(ctype
, arg
, location
);
10566 code
= Runtime::PRINTCOMPLEX
;
10568 else if (type
->is_boolean_type())
10569 code
= Runtime::PRINTBOOL
;
10570 else if (type
->points_to() != NULL
10571 || type
->channel_type() != NULL
10572 || type
->map_type() != NULL
10573 || type
->function_type() != NULL
)
10575 arg
= Expression::make_cast(type
, arg
, location
);
10576 code
= Runtime::PRINTPOINTER
;
10578 else if (type
->interface_type() != NULL
)
10580 if (type
->interface_type()->is_empty())
10581 code
= Runtime::PRINTEFACE
;
10583 code
= Runtime::PRINTIFACE
;
10585 else if (type
->is_slice_type())
10586 code
= Runtime::PRINTSLICE
;
10589 go_assert(saw_errors());
10590 return context
->backend()->error_expression();
10593 Expression
* call
= Runtime::make_call(code
, location
, 1, arg
);
10594 print_stmts
= Expression::make_compound(print_stmts
, call
,
10601 Expression
* print_nl
=
10602 Runtime::make_call(Runtime::PRINTNL
, location
, 0);
10603 print_stmts
= Expression::make_compound(print_stmts
, print_nl
,
10607 Expression
* unlock
= Runtime::make_call(Runtime::PRINTUNLOCK
,
10609 print_stmts
= Expression::make_compound(print_stmts
, unlock
, location
);
10611 return print_stmts
->get_backend(context
);
10614 case BUILTIN_PANIC
:
10616 const Expression_list
* args
= this->args();
10617 go_assert(args
!= NULL
&& args
->size() == 1);
10618 Expression
* arg
= args
->front();
10620 Type::make_empty_interface_type(Linemap::predeclared_location());
10621 arg
= Expression::convert_for_assignment(gogo
, empty
, arg
, location
);
10623 Expression
* panic
=
10624 Runtime::make_call(Runtime::GOPANIC
, location
, 1, arg
);
10625 return panic
->get_backend(context
);
10628 case BUILTIN_RECOVER
:
10630 // The argument is set when building recover thunks. It's a
10631 // boolean value which is true if we can recover a value now.
10632 const Expression_list
* args
= this->args();
10633 go_assert(args
!= NULL
&& args
->size() == 1);
10634 Expression
* arg
= args
->front();
10636 Type::make_empty_interface_type(Linemap::predeclared_location());
10638 Expression
* nil
= Expression::make_nil(location
);
10639 nil
= Expression::make_interface_value(empty
, nil
, nil
, location
);
10641 // We need to handle a deferred call to recover specially,
10642 // because it changes whether it can recover a panic or not.
10643 // See test7 in test/recover1.go.
10644 Expression
* recover
= Runtime::make_call((this->is_deferred()
10645 ? Runtime::DEFERREDRECOVER
10646 : Runtime::GORECOVER
),
10649 Expression::make_conditional(arg
, recover
, nil
, location
);
10650 return cond
->get_backend(context
);
10653 case BUILTIN_CLOSE
:
10655 const Expression_list
* args
= this->args();
10656 go_assert(args
!= NULL
&& args
->size() == 1);
10657 Expression
* arg
= args
->front();
10658 Expression
* close
= Runtime::make_call(Runtime::CLOSE
, location
,
10660 return close
->get_backend(context
);
10663 case BUILTIN_SIZEOF
:
10664 case BUILTIN_OFFSETOF
:
10665 case BUILTIN_ALIGNOF
:
10667 Numeric_constant nc
;
10669 if (!this->numeric_constant_value(&nc
)
10670 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
10672 go_assert(saw_errors());
10673 return context
->backend()->error_expression();
10675 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
10678 Expression
* int_cst
=
10679 Expression::make_integer_z(&ival
, uintptr_type
, location
);
10681 return int_cst
->get_backend(context
);
10685 // Handled in Builtin_call_expression::do_flatten.
10688 case BUILTIN_APPEND
:
10689 // Handled in Builtin_call_expression::flatten_append.
10695 const Expression_list
* args
= this->args();
10696 go_assert(args
!= NULL
&& args
->size() == 1);
10699 Bexpression
* bcomplex
= args
->front()->get_backend(context
);
10700 if (this->code_
== BUILTIN_REAL
)
10701 ret
= gogo
->backend()->real_part_expression(bcomplex
, location
);
10703 ret
= gogo
->backend()->imag_part_expression(bcomplex
, location
);
10707 case BUILTIN_COMPLEX
:
10709 const Expression_list
* args
= this->args();
10710 go_assert(args
!= NULL
&& args
->size() == 2);
10711 Bexpression
* breal
= args
->front()->get_backend(context
);
10712 Bexpression
* bimag
= args
->back()->get_backend(context
);
10713 return gogo
->backend()->complex_expression(breal
, bimag
, location
);
10721 // We have to support exporting a builtin call expression, because
10722 // code can set a constant to the result of a builtin expression.
10725 Builtin_call_expression::do_export(Export_function_body
* efb
) const
10727 Numeric_constant nc
;
10728 if (this->numeric_constant_value(&nc
))
10734 Integer_expression::export_integer(efb
, val
);
10737 else if (nc
.is_float())
10740 nc
.get_float(&fval
);
10741 Float_expression::export_float(efb
, fval
);
10744 else if (nc
.is_complex())
10747 nc
.get_complex(&cval
);
10748 Complex_expression::export_complex(efb
, cval
);
10754 // A trailing space lets us reliably identify the end of the number.
10755 efb
->write_c_string(" ");
10759 const char *s
= NULL
;
10760 switch (this->code_
)
10764 case BUILTIN_APPEND
:
10776 case BUILTIN_DELETE
:
10779 case BUILTIN_PRINT
:
10782 case BUILTIN_PRINTLN
:
10785 case BUILTIN_PANIC
:
10788 case BUILTIN_RECOVER
:
10791 case BUILTIN_CLOSE
:
10800 case BUILTIN_COMPLEX
:
10804 efb
->write_c_string(s
);
10805 this->export_arguments(efb
);
10809 // Class Call_expression.
10811 // A Go function can be viewed in a couple of different ways. The
10812 // code of a Go function becomes a backend function with parameters
10813 // whose types are simply the backend representation of the Go types.
10814 // If there are multiple results, they are returned as a backend
10817 // However, when Go code refers to a function other than simply
10818 // calling it, the backend type of that function is actually a struct.
10819 // The first field of the struct points to the Go function code
10820 // (sometimes a wrapper as described below). The remaining fields
10821 // hold addresses of closed-over variables. This struct is called a
10824 // There are a few cases to consider.
10826 // A direct function call of a known function in package scope. In
10827 // this case there are no closed-over variables, and we know the name
10828 // of the function code. We can simply produce a backend call to the
10829 // function directly, and not worry about the closure.
10831 // A direct function call of a known function literal. In this case
10832 // we know the function code and we know the closure. We generate the
10833 // function code such that it expects an additional final argument of
10834 // the closure type. We pass the closure as the last argument, after
10835 // the other arguments.
10837 // An indirect function call. In this case we have a closure. We
10838 // load the pointer to the function code from the first field of the
10839 // closure. We pass the address of the closure as the last argument.
10841 // A call to a method of an interface. Type methods are always at
10842 // package scope, so we call the function directly, and don't worry
10843 // about the closure.
10845 // This means that for a function at package scope we have two cases.
10846 // One is the direct call, which has no closure. The other is the
10847 // indirect call, which does have a closure. We can't simply ignore
10848 // the closure, even though it is the last argument, because that will
10849 // fail on targets where the function pops its arguments. So when
10850 // generating a closure for a package-scope function we set the
10851 // function code pointer in the closure to point to a wrapper
10852 // function. This wrapper function accepts a final argument that
10853 // points to the closure, ignores it, and calls the real function as a
10854 // direct function call. This wrapper will normally be efficient, and
10855 // can often simply be a tail call to the real function.
10857 // We don't use GCC's static chain pointer because 1) we don't need
10858 // it; 2) GCC only permits using a static chain to call a known
10859 // function, so we can't use it for an indirect call anyhow. Since we
10860 // can't use it for an indirect call, we may as well not worry about
10861 // using it for a direct call either.
10863 // We pass the closure last rather than first because it means that
10864 // the function wrapper we put into a closure for a package-scope
10865 // function can normally just be a tail call to the real function.
10867 // For method expressions we generate a wrapper that loads the
10868 // receiver from the closure and then calls the method. This
10869 // unfortunately forces reshuffling the arguments, since there is a
10870 // new first argument, but we can't avoid reshuffling either for
10871 // method expressions or for indirect calls of package-scope
10872 // functions, and since the latter are more common we reshuffle for
10873 // method expressions.
10875 // Note that the Go code retains the Go types. The extra final
10876 // argument only appears when we convert to the backend
10882 Call_expression::do_traverse(Traverse
* traverse
)
10884 // If we are calling a function in a different package that returns
10885 // an unnamed type, this may be the only chance we get to traverse
10886 // that type. We don't traverse this->type_ because it may be a
10887 // Call_multiple_result_type that will just lead back here.
10888 if (this->type_
!= NULL
&& !this->type_
->is_error_type())
10890 Function_type
*fntype
= this->get_function_type();
10891 if (fntype
!= NULL
&& Type::traverse(fntype
, traverse
) == TRAVERSE_EXIT
)
10892 return TRAVERSE_EXIT
;
10894 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
10895 return TRAVERSE_EXIT
;
10896 if (this->args_
!= NULL
)
10898 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10899 return TRAVERSE_EXIT
;
10901 return TRAVERSE_CONTINUE
;
10904 // Lower a call statement.
10907 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
10908 Statement_inserter
* inserter
, int)
10910 Location loc
= this->location();
10912 // A type cast can look like a function call.
10913 if (this->fn_
->is_type_expression()
10914 && this->args_
!= NULL
10915 && this->args_
->size() == 1)
10916 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
10919 // Because do_type will return an error type and thus prevent future
10920 // errors, check for that case now to ensure that the error gets
10922 Function_type
* fntype
= this->get_function_type();
10923 if (fntype
== NULL
)
10925 if (!this->fn_
->type()->is_error())
10926 this->report_error(_("expected function"));
10927 this->set_is_error();
10931 // Handle an argument which is a call to a function which returns
10932 // multiple results.
10933 if (this->args_
!= NULL
10934 && this->args_
->size() == 1
10935 && this->args_
->front()->call_expression() != NULL
)
10937 size_t rc
= this->args_
->front()->call_expression()->result_count();
10939 && ((fntype
->parameters() != NULL
10940 && (fntype
->parameters()->size() == rc
10941 || (fntype
->is_varargs()
10942 && fntype
->parameters()->size() - 1 <= rc
)))
10943 || fntype
->is_builtin()))
10945 Call_expression
* call
= this->args_
->front()->call_expression();
10946 call
->set_is_multi_value_arg();
10947 if (this->is_varargs_
)
10949 // It is not clear which result of a multiple result call
10950 // the ellipsis operator should be applied to. If we unpack the
10951 // the call into its individual results here, the ellipsis will be
10952 // applied to the last result.
10953 go_error_at(call
->location(),
10954 _("multiple-value argument in single-value context"));
10955 return Expression::make_error(call
->location());
10958 Expression_list
* args
= new Expression_list
;
10959 for (size_t i
= 0; i
< rc
; ++i
)
10960 args
->push_back(Expression::make_call_result(call
, i
));
10961 // We can't return a new call expression here, because this
10962 // one may be referenced by Call_result expressions. We
10963 // also can't delete the old arguments, because we may still
10964 // traverse them somewhere up the call stack. FIXME.
10965 this->args_
= args
;
10969 // Recognize a call to a builtin function.
10970 if (fntype
->is_builtin())
10972 Builtin_call_expression
* bce
=
10973 new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
10974 this->is_varargs_
, loc
);
10975 if (this->is_deferred_
)
10976 bce
->set_is_deferred();
10977 if (this->is_concurrent_
)
10978 bce
->set_is_concurrent();
10982 // If this call returns multiple results, create a temporary
10983 // variable to hold them.
10984 if (this->result_count() > 1 && this->call_temp_
== NULL
)
10986 Struct_field_list
* sfl
= new Struct_field_list();
10987 const Typed_identifier_list
* results
= fntype
->results();
10991 for (Typed_identifier_list::const_iterator p
= results
->begin();
10992 p
!= results
->end();
10995 snprintf(buf
, sizeof buf
, "res%d", i
);
10996 sfl
->push_back(Struct_field(Typed_identifier(buf
, p
->type(), loc
)));
10999 Struct_type
* st
= Type::make_struct_type(sfl
, loc
);
11000 st
->set_is_struct_incomparable();
11001 this->call_temp_
= Statement::make_temporary(st
, NULL
, loc
);
11002 inserter
->insert(this->call_temp_
);
11005 // Handle a call to a varargs function by packaging up the extra
11007 if (fntype
->is_varargs())
11009 const Typed_identifier_list
* parameters
= fntype
->parameters();
11010 go_assert(parameters
!= NULL
&& !parameters
->empty());
11011 Type
* varargs_type
= parameters
->back().type();
11012 this->lower_varargs(gogo
, function
, inserter
, varargs_type
,
11013 parameters
->size(), SLICE_STORAGE_MAY_ESCAPE
);
11016 // If this is call to a method, call the method directly passing the
11017 // object as the first parameter.
11018 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
11021 Named_object
* methodfn
= bme
->function();
11022 Function_type
* mft
= (methodfn
->is_function()
11023 ? methodfn
->func_value()->type()
11024 : methodfn
->func_declaration_value()->type());
11025 Expression
* first_arg
= bme
->first_argument();
11027 // We always pass a pointer when calling a method, except for
11028 // direct interface types when calling a value method.
11029 if (!first_arg
->type()->is_error()
11030 && !first_arg
->type()->is_direct_iface_type())
11032 first_arg
= Expression::make_unary(OPERATOR_AND
, first_arg
, loc
);
11033 // We may need to create a temporary variable so that we can
11034 // take the address. We can't do that here because it will
11035 // mess up the order of evaluation.
11036 Unary_expression
* ue
= static_cast<Unary_expression
*>(first_arg
);
11037 ue
->set_create_temp();
11039 else if (mft
->receiver()->type()->points_to() == NULL
11040 && first_arg
->type()->points_to() != NULL
11041 && first_arg
->type()->points_to()->is_direct_iface_type())
11042 first_arg
= Expression::make_dereference(first_arg
,
11043 Expression::NIL_CHECK_DEFAULT
,
11046 // If we are calling a method which was inherited from an
11047 // embedded struct, and the method did not get a stub, then the
11048 // first type may be wrong.
11049 Type
* fatype
= bme
->first_argument_type();
11050 if (fatype
!= NULL
)
11052 if (fatype
->points_to() == NULL
)
11053 fatype
= Type::make_pointer_type(fatype
);
11054 first_arg
= Expression::make_unsafe_cast(fatype
, first_arg
, loc
);
11057 Expression_list
* new_args
= new Expression_list();
11058 new_args
->push_back(first_arg
);
11059 if (this->args_
!= NULL
)
11061 for (Expression_list::const_iterator p
= this->args_
->begin();
11062 p
!= this->args_
->end();
11064 new_args
->push_back(*p
);
11067 // We have to change in place because this structure may be
11068 // referenced by Call_result_expressions. We can't delete the
11069 // old arguments, because we may be traversing them up in some
11071 this->args_
= new_args
;
11072 this->fn_
= Expression::make_func_reference(methodfn
, NULL
,
11076 // If this is a call to an imported function for which we have an
11077 // inlinable function body, add it to the list of functions to give
11078 // to the backend as inlining opportunities.
11079 Func_expression
* fe
= this->fn_
->func_expression();
11081 && fe
->named_object()->is_function_declaration()
11082 && fe
->named_object()->func_declaration_value()->has_imported_body())
11083 gogo
->add_imported_inlinable_function(fe
->named_object());
11088 // Lower a call to a varargs function. FUNCTION is the function in
11089 // which the call occurs--it's not the function we are calling.
11090 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
11091 // PARAM_COUNT is the number of parameters of the function we are
11092 // calling; the last of these parameters will be the varargs
11096 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
11097 Statement_inserter
* inserter
,
11098 Type
* varargs_type
, size_t param_count
,
11099 Slice_storage_escape_disp escape_disp
)
11101 if (this->varargs_are_lowered_
)
11104 Location loc
= this->location();
11106 go_assert(param_count
> 0);
11107 go_assert(varargs_type
->is_slice_type());
11109 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
11110 if (arg_count
< param_count
- 1)
11112 // Not enough arguments; will be caught in check_types.
11116 Expression_list
* old_args
= this->args_
;
11117 Expression_list
* new_args
= new Expression_list();
11118 bool push_empty_arg
= false;
11119 if (old_args
== NULL
|| old_args
->empty())
11121 go_assert(param_count
== 1);
11122 push_empty_arg
= true;
11126 Expression_list::const_iterator pa
;
11128 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
11130 if (static_cast<size_t>(i
) == param_count
)
11132 new_args
->push_back(*pa
);
11135 // We have reached the varargs parameter.
11137 bool issued_error
= false;
11138 if (pa
== old_args
->end())
11139 push_empty_arg
= true;
11140 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
11141 new_args
->push_back(*pa
);
11142 else if (this->is_varargs_
)
11144 if ((*pa
)->type()->is_slice_type())
11145 this->report_error(_("too many arguments"));
11148 go_error_at(this->location(),
11149 _("invalid use of %<...%> with non-slice"));
11150 this->set_is_error();
11156 Type
* element_type
= varargs_type
->array_type()->element_type();
11157 Expression_list
* vals
= new Expression_list
;
11158 for (; pa
!= old_args
->end(); ++pa
, ++i
)
11160 // Check types here so that we get a better message.
11161 Type
* patype
= (*pa
)->type();
11162 Location paloc
= (*pa
)->location();
11163 if (!this->check_argument_type(i
, element_type
, patype
,
11164 paloc
, issued_error
))
11166 vals
->push_back(*pa
);
11168 Slice_construction_expression
* sce
=
11169 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
11170 if (escape_disp
== SLICE_STORAGE_DOES_NOT_ESCAPE
)
11171 sce
->set_storage_does_not_escape();
11172 Expression
* val
= sce
;
11173 gogo
->lower_expression(function
, inserter
, &val
);
11174 new_args
->push_back(val
);
11178 if (push_empty_arg
)
11179 new_args
->push_back(Expression::make_nil(loc
));
11181 // We can't return a new call expression here, because this one may
11182 // be referenced by Call_result expressions. FIXME. We can't
11183 // delete OLD_ARGS because we may have both a Call_expression and a
11184 // Builtin_call_expression which refer to them. FIXME.
11185 this->args_
= new_args
;
11186 this->varargs_are_lowered_
= true;
11189 // Flatten a call with multiple results into a temporary.
11192 Call_expression::do_flatten(Gogo
* gogo
, Named_object
*,
11193 Statement_inserter
* inserter
)
11195 if (this->is_erroneous_call())
11197 go_assert(saw_errors());
11198 return Expression::make_error(this->location());
11201 if (this->is_flattened_
)
11203 this->is_flattened_
= true;
11205 // Add temporary variables for all arguments that require type
11207 Function_type
* fntype
= this->get_function_type();
11208 if (fntype
== NULL
)
11210 go_assert(saw_errors());
11213 if (this->args_
!= NULL
&& !this->args_
->empty()
11214 && fntype
->parameters() != NULL
&& !fntype
->parameters()->empty())
11216 bool is_interface_method
=
11217 this->fn_
->interface_field_reference_expression() != NULL
;
11219 Expression_list
*args
= new Expression_list();
11220 Typed_identifier_list::const_iterator pp
= fntype
->parameters()->begin();
11221 Expression_list::const_iterator pa
= this->args_
->begin();
11222 if (!is_interface_method
&& fntype
->is_method())
11224 // The receiver argument.
11225 args
->push_back(*pa
);
11228 for (; pa
!= this->args_
->end(); ++pa
, ++pp
)
11230 go_assert(pp
!= fntype
->parameters()->end());
11231 if (Type::are_identical(pp
->type(), (*pa
)->type(),
11232 Type::COMPARE_TAGS
, NULL
))
11233 args
->push_back(*pa
);
11236 Location loc
= (*pa
)->location();
11237 Expression
* arg
= *pa
;
11238 if (!arg
->is_variable())
11240 Temporary_statement
*temp
=
11241 Statement::make_temporary(NULL
, arg
, loc
);
11242 inserter
->insert(temp
);
11243 arg
= Expression::make_temporary_reference(temp
, loc
);
11245 arg
= Expression::convert_for_assignment(gogo
, pp
->type(), arg
,
11247 args
->push_back(arg
);
11250 delete this->args_
;
11251 this->args_
= args
;
11254 // Lower to compiler intrinsic if possible.
11255 Func_expression
* fe
= this->fn_
->func_expression();
11256 if (!this->is_concurrent_
&& !this->is_deferred_
11258 && (fe
->named_object()->is_function_declaration()
11259 || fe
->named_object()->is_function()))
11261 Expression
* ret
= this->intrinsify(gogo
, inserter
);
11266 // Add an implicit conversion to a boolean type, if needed. See the
11267 // comment in Binary_expression::lower_array_comparison.
11268 if (this->is_equal_function_
11269 && this->type_
!= NULL
11270 && this->type_
!= Type::lookup_bool_type())
11271 return Expression::make_cast(this->type_
, this, this->location());
11276 // Lower a call to a compiler intrinsic if possible.
11277 // Returns NULL if it is not an intrinsic.
11280 Call_expression::intrinsify(Gogo
* gogo
,
11281 Statement_inserter
* inserter
)
11283 Func_expression
* fe
= this->fn_
->func_expression();
11284 Named_object
* no
= fe
->named_object();
11285 std::string name
= Gogo::unpack_hidden_name(no
->name());
11286 std::string package
= (no
->package() != NULL
11287 ? no
->package()->pkgpath()
11288 : gogo
->pkgpath());
11289 Location loc
= this->location();
11291 Type
* int_type
= Type::lookup_integer_type("int");
11292 Type
* int32_type
= Type::lookup_integer_type("int32");
11293 Type
* int64_type
= Type::lookup_integer_type("int64");
11294 Type
* uint_type
= Type::lookup_integer_type("uint");
11295 Type
* uint32_type
= Type::lookup_integer_type("uint32");
11296 Type
* uint64_type
= Type::lookup_integer_type("uint64");
11297 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
11298 Type
* pointer_type
= Type::make_pointer_type(Type::make_void_type());
11300 int int_size
= int_type
->named_type()->real_type()->integer_type()->bits() / 8;
11301 int ptr_size
= uintptr_type
->named_type()->real_type()->integer_type()->bits() / 8;
11303 if (package
== "sync/atomic")
11305 // sync/atomic functions and runtime/internal/atomic functions
11306 // are very similar. In order not to duplicate code, we just
11307 // redirect to the latter and let the code below to handle them.
11308 // In case there is no equivalent functions (slight variance
11309 // in types), we just make an artificial name (begin with '$').
11310 // Note: no StorePointer, SwapPointer, and CompareAndSwapPointer,
11311 // as they need write barriers.
11312 if (name
== "LoadInt32")
11313 name
= "$Loadint32";
11314 else if (name
== "LoadInt64")
11315 name
= "Loadint64";
11316 else if (name
== "LoadUint32")
11318 else if (name
== "LoadUint64")
11320 else if (name
== "LoadUintptr")
11321 name
= "Loaduintptr";
11322 else if (name
== "LoadPointer")
11324 else if (name
== "StoreInt32")
11325 name
= "$Storeint32";
11326 else if (name
== "StoreInt64")
11327 name
= "$Storeint64";
11328 else if (name
== "StoreUint32")
11330 else if (name
== "StoreUint64")
11332 else if (name
== "StoreUintptr")
11333 name
= "Storeuintptr";
11334 else if (name
== "AddInt32")
11335 name
= "$Xaddint32";
11336 else if (name
== "AddInt64")
11337 name
= "Xaddint64";
11338 else if (name
== "AddUint32")
11340 else if (name
== "AddUint64")
11342 else if (name
== "AddUintptr")
11343 name
= "Xadduintptr";
11344 else if (name
== "SwapInt32")
11345 name
= "$Xchgint32";
11346 else if (name
== "SwapInt64")
11347 name
= "$Xchgint64";
11348 else if (name
== "SwapUint32")
11350 else if (name
== "SwapUint64")
11352 else if (name
== "SwapUintptr")
11353 name
= "Xchguintptr";
11354 else if (name
== "CompareAndSwapInt32")
11355 name
= "$Casint32";
11356 else if (name
== "CompareAndSwapInt64")
11357 name
= "$Casint64";
11358 else if (name
== "CompareAndSwapUint32")
11360 else if (name
== "CompareAndSwapUint64")
11362 else if (name
== "CompareAndSwapUintptr")
11363 name
= "Casuintptr";
11367 package
= "runtime/internal/atomic";
11370 if (package
== "runtime/internal/sys")
11372 // runtime/internal/sys functions and math/bits functions
11373 // are very similar. In order not to duplicate code, we just
11374 // redirect to the latter and let the code below to handle them.
11375 if (name
== "Bswap32")
11376 name
= "ReverseBytes32";
11377 else if (name
== "Bswap64")
11378 name
= "ReverseBytes64";
11379 else if (name
== "Ctz32")
11380 name
= "TrailingZeros32";
11381 else if (name
== "Ctz64")
11382 name
= "TrailingZeros64";
11386 package
= "math/bits";
11389 if (package
== "runtime")
11391 // Handle a couple of special runtime functions. In the runtime
11392 // package, getcallerpc returns the PC of the caller, and
11393 // getcallersp returns the frame pointer of the caller. Implement
11394 // these by turning them into calls to GCC builtin functions. We
11395 // could implement them in normal code, but then we would have to
11396 // explicitly unwind the stack. These functions are intended to be
11397 // efficient. Note that this technique obviously only works for
11398 // direct calls, but that is the only way they are used.
11399 if (name
== "getcallerpc"
11400 && (this->args_
== NULL
|| this->args_
->size() == 0))
11402 Expression
* arg
= Expression::make_integer_ul(0, uint32_type
, loc
);
11404 Runtime::make_call(Runtime::BUILTIN_RETURN_ADDRESS
, loc
,
11406 // The builtin functions return void*, but the Go functions return uintptr.
11407 return Expression::make_cast(uintptr_type
, call
, loc
);
11409 else if (name
== "getcallersp"
11410 && (this->args_
== NULL
|| this->args_
->size() == 0))
11414 Runtime::make_call(Runtime::BUILTIN_DWARF_CFA
, loc
, 0);
11415 // The builtin functions return void*, but the Go functions return uintptr.
11416 return Expression::make_cast(uintptr_type
, call
, loc
);
11419 else if (package
== "math/bits")
11421 if ((name
== "ReverseBytes16" || name
== "ReverseBytes32"
11422 || name
== "ReverseBytes64" || name
== "ReverseBytes")
11423 && this->args_
!= NULL
&& this->args_
->size() == 1)
11425 Runtime::Function code
;
11426 if (name
== "ReverseBytes16")
11427 code
= Runtime::BUILTIN_BSWAP16
;
11428 else if (name
== "ReverseBytes32")
11429 code
= Runtime::BUILTIN_BSWAP32
;
11430 else if (name
== "ReverseBytes64")
11431 code
= Runtime::BUILTIN_BSWAP64
;
11432 else if (name
== "ReverseBytes")
11433 code
= (int_size
== 8 ? Runtime::BUILTIN_BSWAP64
: Runtime::BUILTIN_BSWAP32
);
11436 Expression
* arg
= this->args_
->front();
11437 Expression
* call
= Runtime::make_call(code
, loc
, 1, arg
);
11438 if (name
== "ReverseBytes")
11439 return Expression::make_cast(uint_type
, call
, loc
);
11442 else if ((name
== "TrailingZeros8" || name
== "TrailingZeros16")
11443 && this->args_
!= NULL
&& this->args_
->size() == 1)
11445 // GCC does not have a ctz8 or ctz16 intrinsic. We do
11446 // ctz32(0x100 | arg) or ctz32(0x10000 | arg).
11447 Expression
* arg
= this->args_
->front();
11448 arg
= Expression::make_cast(uint32_type
, arg
, loc
);
11449 unsigned long mask
= (name
== "TrailingZeros8" ? 0x100 : 0x10000);
11450 Expression
* c
= Expression::make_integer_ul(mask
, uint32_type
, loc
);
11451 arg
= Expression::make_binary(OPERATOR_OR
, arg
, c
, loc
);
11452 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CTZ
, loc
, 1, arg
);
11453 return Expression::make_cast(int_type
, call
, loc
);
11455 else if ((name
== "TrailingZeros32"
11456 || (name
== "TrailingZeros" && int_size
== 4))
11457 && this->args_
!= NULL
&& this->args_
->size() == 1)
11459 Expression
* arg
= this->args_
->front();
11460 if (!arg
->is_variable())
11462 Temporary_statement
* ts
= Statement::make_temporary(uint32_type
, arg
, loc
);
11463 inserter
->insert(ts
);
11464 arg
= Expression::make_temporary_reference(ts
, loc
);
11466 // arg == 0 ? 32 : __builtin_ctz(arg)
11467 Expression
* zero
= Expression::make_integer_ul(0, uint32_type
, loc
);
11468 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
, arg
, zero
, loc
);
11469 Expression
* c32
= Expression::make_integer_ul(32, int_type
, loc
);
11470 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CTZ
, loc
, 1, arg
->copy());
11471 call
= Expression::make_cast(int_type
, call
, loc
);
11472 return Expression::make_conditional(cmp
, c32
, call
, loc
);
11474 else if ((name
== "TrailingZeros64"
11475 || (name
== "TrailingZeros" && int_size
== 8))
11476 && this->args_
!= NULL
&& this->args_
->size() == 1)
11478 Expression
* arg
= this->args_
->front();
11479 if (!arg
->is_variable())
11481 Temporary_statement
* ts
= Statement::make_temporary(uint64_type
, arg
, loc
);
11482 inserter
->insert(ts
);
11483 arg
= Expression::make_temporary_reference(ts
, loc
);
11485 // arg == 0 ? 64 : __builtin_ctzll(arg)
11486 Expression
* zero
= Expression::make_integer_ul(0, uint64_type
, loc
);
11487 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
, arg
, zero
, loc
);
11488 Expression
* c64
= Expression::make_integer_ul(64, int_type
, loc
);
11489 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CTZLL
, loc
, 1, arg
->copy());
11490 call
= Expression::make_cast(int_type
, call
, loc
);
11491 return Expression::make_conditional(cmp
, c64
, call
, loc
);
11493 else if ((name
== "LeadingZeros8" || name
== "LeadingZeros16"
11494 || name
== "Len8" || name
== "Len16")
11495 && this->args_
!= NULL
&& this->args_
->size() == 1)
11497 // GCC does not have a clz8 ir clz16 intrinsic. We do
11498 // clz32(arg<<24 | 0xffffff) or clz32(arg<<16 | 0xffff).
11499 Expression
* arg
= this->args_
->front();
11500 arg
= Expression::make_cast(uint32_type
, arg
, loc
);
11501 unsigned long shift
=
11502 ((name
== "LeadingZeros8" || name
== "Len8") ? 24 : 16);
11503 Expression
* c
= Expression::make_integer_ul(shift
, uint32_type
, loc
);
11504 arg
= Expression::make_binary(OPERATOR_LSHIFT
, arg
, c
, loc
);
11505 unsigned long mask
=
11506 ((name
== "LeadingZeros8" || name
== "Len8") ? 0xffffff : 0xffff);
11507 c
= Expression::make_integer_ul(mask
, uint32_type
, loc
);
11508 arg
= Expression::make_binary(OPERATOR_OR
, arg
, c
, loc
);
11509 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CLZ
, loc
, 1, arg
);
11510 call
= Expression::make_cast(int_type
, call
, loc
);
11511 // len = width - clz
11512 if (name
== "Len8")
11514 c
= Expression::make_integer_ul(8, int_type
, loc
);
11515 return Expression::make_binary(OPERATOR_MINUS
, c
, call
, loc
);
11517 else if (name
== "Len16")
11519 c
= Expression::make_integer_ul(16, int_type
, loc
);
11520 return Expression::make_binary(OPERATOR_MINUS
, c
, call
, loc
);
11524 else if ((name
== "LeadingZeros32" || name
== "Len32"
11525 || ((name
== "LeadingZeros" || name
== "Len") && int_size
== 4))
11526 && this->args_
!= NULL
&& this->args_
->size() == 1)
11528 Expression
* arg
= this->args_
->front();
11529 if (!arg
->is_variable())
11531 Temporary_statement
* ts
= Statement::make_temporary(uint32_type
, arg
, loc
);
11532 inserter
->insert(ts
);
11533 arg
= Expression::make_temporary_reference(ts
, loc
);
11535 // arg == 0 ? 32 : __builtin_clz(arg)
11536 Expression
* zero
= Expression::make_integer_ul(0, uint32_type
, loc
);
11537 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
, arg
, zero
, loc
);
11538 Expression
* c32
= Expression::make_integer_ul(32, int_type
, loc
);
11539 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CLZ
, loc
, 1, arg
->copy());
11540 call
= Expression::make_cast(int_type
, call
, loc
);
11541 Expression
* cond
= Expression::make_conditional(cmp
, c32
, call
, loc
);
11543 if (name
== "Len32" || name
== "Len")
11544 return Expression::make_binary(OPERATOR_MINUS
, c32
->copy(), cond
, loc
);
11547 else if ((name
== "LeadingZeros64" || name
== "Len64"
11548 || ((name
== "LeadingZeros" || name
== "Len") && int_size
== 8))
11549 && this->args_
!= NULL
&& this->args_
->size() == 1)
11551 Expression
* arg
= this->args_
->front();
11552 if (!arg
->is_variable())
11554 Temporary_statement
* ts
= Statement::make_temporary(uint64_type
, arg
, loc
);
11555 inserter
->insert(ts
);
11556 arg
= Expression::make_temporary_reference(ts
, loc
);
11558 // arg == 0 ? 64 : __builtin_clzll(arg)
11559 Expression
* zero
= Expression::make_integer_ul(0, uint64_type
, loc
);
11560 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
, arg
, zero
, loc
);
11561 Expression
* c64
= Expression::make_integer_ul(64, int_type
, loc
);
11562 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CLZLL
, loc
, 1, arg
->copy());
11563 call
= Expression::make_cast(int_type
, call
, loc
);
11564 Expression
* cond
= Expression::make_conditional(cmp
, c64
, call
, loc
);
11566 if (name
== "Len64" || name
== "Len")
11567 return Expression::make_binary(OPERATOR_MINUS
, c64
->copy(), cond
, loc
);
11570 else if ((name
== "OnesCount8" || name
== "OnesCount16"
11571 || name
== "OnesCount32" || name
== "OnesCount64"
11572 || name
== "OnesCount")
11573 && this->args_
!= NULL
&& this->args_
->size() == 1)
11575 Runtime::Function code
;
11576 if (name
== "OnesCount64")
11577 code
= Runtime::BUILTIN_POPCOUNTLL
;
11578 else if (name
== "OnesCount")
11579 code
= (int_size
== 8 ? Runtime::BUILTIN_POPCOUNTLL
: Runtime::BUILTIN_POPCOUNT
);
11581 code
= Runtime::BUILTIN_POPCOUNT
;
11582 Expression
* arg
= this->args_
->front();
11583 Expression
* call
= Runtime::make_call(code
, loc
, 1, arg
);
11584 return Expression::make_cast(int_type
, call
, loc
);
11587 else if (package
== "runtime/internal/atomic")
11589 int memorder
= __ATOMIC_SEQ_CST
;
11591 if ((name
== "Load" || name
== "Load64" || name
== "Loadint64" || name
== "Loadp"
11592 || name
== "Loaduint" || name
== "Loaduintptr" || name
== "LoadAcq"
11593 || name
== "$Loadint32")
11594 && this->args_
!= NULL
&& this->args_
->size() == 1)
11596 if (int_size
< 8 && (name
== "Load64" || name
== "Loadint64"))
11597 // On 32-bit architectures we need to check alignment.
11598 // Not intrinsify for now.
11601 Runtime::Function code
;
11603 if (name
== "Load")
11605 code
= Runtime::ATOMIC_LOAD_4
;
11606 res_type
= uint32_type
;
11608 else if (name
== "Load64")
11610 code
= Runtime::ATOMIC_LOAD_8
;
11611 res_type
= uint64_type
;
11613 else if (name
== "$Loadint32")
11615 code
= Runtime::ATOMIC_LOAD_4
;
11616 res_type
= int32_type
;
11618 else if (name
== "Loadint64")
11620 code
= Runtime::ATOMIC_LOAD_8
;
11621 res_type
= int64_type
;
11623 else if (name
== "Loaduint")
11625 code
= (int_size
== 8
11626 ? Runtime::ATOMIC_LOAD_8
11627 : Runtime::ATOMIC_LOAD_4
);
11628 res_type
= uint_type
;
11630 else if (name
== "Loaduintptr")
11632 code
= (ptr_size
== 8
11633 ? Runtime::ATOMIC_LOAD_8
11634 : Runtime::ATOMIC_LOAD_4
);
11635 res_type
= uintptr_type
;
11637 else if (name
== "Loadp")
11639 code
= (ptr_size
== 8
11640 ? Runtime::ATOMIC_LOAD_8
11641 : Runtime::ATOMIC_LOAD_4
);
11642 res_type
= pointer_type
;
11644 else if (name
== "LoadAcq")
11646 code
= Runtime::ATOMIC_LOAD_4
;
11647 res_type
= uint32_type
;
11648 memorder
= __ATOMIC_ACQUIRE
;
11652 Expression
* a1
= this->args_
->front();
11653 Expression
* a2
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
11654 Expression
* call
= Runtime::make_call(code
, loc
, 2, a1
, a2
);
11655 return Expression::make_unsafe_cast(res_type
, call
, loc
);
11658 if ((name
== "Store" || name
== "Store64" || name
== "StorepNoWB"
11659 || name
== "Storeuintptr" || name
== "StoreRel"
11660 || name
== "$Storeint32" || name
== "$Storeint64")
11661 && this->args_
!= NULL
&& this->args_
->size() == 2)
11663 if (int_size
< 8 && (name
== "Store64" || name
== "$Storeint64"))
11666 Runtime::Function code
;
11667 Expression
* a1
= this->args_
->at(0);
11668 Expression
* a2
= this->args_
->at(1);
11669 if (name
== "Store")
11670 code
= Runtime::ATOMIC_STORE_4
;
11671 else if (name
== "Store64")
11672 code
= Runtime::ATOMIC_STORE_8
;
11673 else if (name
== "$Storeint32")
11674 code
= Runtime::ATOMIC_STORE_4
;
11675 else if (name
== "$Storeint64")
11676 code
= Runtime::ATOMIC_STORE_8
;
11677 else if (name
== "Storeuintptr")
11678 code
= (ptr_size
== 8 ? Runtime::ATOMIC_STORE_8
: Runtime::ATOMIC_STORE_4
);
11679 else if (name
== "StorepNoWB")
11681 code
= (ptr_size
== 8 ? Runtime::ATOMIC_STORE_8
: Runtime::ATOMIC_STORE_4
);
11682 a2
= Expression::make_unsafe_cast(uintptr_type
, a2
, loc
);
11683 a2
= Expression::make_cast(uint64_type
, a2
, loc
);
11685 else if (name
== "StoreRel")
11687 code
= Runtime::ATOMIC_STORE_4
;
11688 memorder
= __ATOMIC_RELEASE
;
11692 Expression
* a3
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
11693 return Runtime::make_call(code
, loc
, 3, a1
, a2
, a3
);
11696 if ((name
== "Xchg" || name
== "Xchg64" || name
== "Xchguintptr"
11697 || name
== "$Xchgint32" || name
== "$Xchgint64")
11698 && this->args_
!= NULL
&& this->args_
->size() == 2)
11700 if (int_size
< 8 && (name
== "Xchg64" || name
== "Xchgint64"))
11703 Runtime::Function code
;
11705 if (name
== "Xchg")
11707 code
= Runtime::ATOMIC_EXCHANGE_4
;
11708 res_type
= uint32_type
;
11710 else if (name
== "Xchg64")
11712 code
= Runtime::ATOMIC_EXCHANGE_8
;
11713 res_type
= uint64_type
;
11715 else if (name
== "$Xchgint32")
11717 code
= Runtime::ATOMIC_EXCHANGE_4
;
11718 res_type
= int32_type
;
11720 else if (name
== "$Xchgint64")
11722 code
= Runtime::ATOMIC_EXCHANGE_8
;
11723 res_type
= int64_type
;
11725 else if (name
== "Xchguintptr")
11727 code
= (ptr_size
== 8
11728 ? Runtime::ATOMIC_EXCHANGE_8
11729 : Runtime::ATOMIC_EXCHANGE_4
);
11730 res_type
= uintptr_type
;
11734 Expression
* a1
= this->args_
->at(0);
11735 Expression
* a2
= this->args_
->at(1);
11736 Expression
* a3
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
11737 Expression
* call
= Runtime::make_call(code
, loc
, 3, a1
, a2
, a3
);
11738 return Expression::make_cast(res_type
, call
, loc
);
11741 if ((name
== "Cas" || name
== "Cas64" || name
== "Casuintptr"
11742 || name
== "Casp1" || name
== "CasRel"
11743 || name
== "$Casint32" || name
== "$Casint64")
11744 && this->args_
!= NULL
&& this->args_
->size() == 3)
11746 if (int_size
< 8 && (name
== "Cas64" || name
== "$Casint64"))
11749 Runtime::Function code
;
11750 Expression
* a1
= this->args_
->at(0);
11752 // Builtin cas takes a pointer to the old value.
11753 // Store it in a temporary and take the address.
11754 Expression
* a2
= this->args_
->at(1);
11755 Temporary_statement
* ts
= Statement::make_temporary(NULL
, a2
, loc
);
11756 inserter
->insert(ts
);
11757 a2
= Expression::make_temporary_reference(ts
, loc
);
11758 a2
= Expression::make_unary(OPERATOR_AND
, a2
, loc
);
11760 Expression
* a3
= this->args_
->at(2);
11762 code
= Runtime::ATOMIC_COMPARE_EXCHANGE_4
;
11763 else if (name
== "Cas64")
11764 code
= Runtime::ATOMIC_COMPARE_EXCHANGE_8
;
11765 else if (name
== "$Casint32")
11766 code
= Runtime::ATOMIC_COMPARE_EXCHANGE_4
;
11767 else if (name
== "$Casint64")
11768 code
= Runtime::ATOMIC_COMPARE_EXCHANGE_8
;
11769 else if (name
== "Casuintptr")
11770 code
= (ptr_size
== 8
11771 ? Runtime::ATOMIC_COMPARE_EXCHANGE_8
11772 : Runtime::ATOMIC_COMPARE_EXCHANGE_4
);
11773 else if (name
== "Casp1")
11775 code
= (ptr_size
== 8
11776 ? Runtime::ATOMIC_COMPARE_EXCHANGE_8
11777 : Runtime::ATOMIC_COMPARE_EXCHANGE_4
);
11778 a3
= Expression::make_unsafe_cast(uintptr_type
, a3
, loc
);
11779 a3
= Expression::make_cast(uint64_type
, a3
, loc
);
11781 else if (name
== "CasRel")
11783 code
= Runtime::ATOMIC_COMPARE_EXCHANGE_4
;
11784 memorder
= __ATOMIC_RELEASE
;
11788 Expression
* a4
= Expression::make_boolean(false, loc
);
11789 Expression
* a5
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
11790 Expression
* a6
= Expression::make_integer_ul(__ATOMIC_RELAXED
, int32_type
, loc
);
11791 return Runtime::make_call(code
, loc
, 6, a1
, a2
, a3
, a4
, a5
, a6
);
11794 if ((name
== "Xadd" || name
== "Xadd64" || name
== "Xaddint64"
11795 || name
== "Xadduintptr" || name
== "$Xaddint32")
11796 && this->args_
!= NULL
&& this->args_
->size() == 2)
11798 if (int_size
< 8 && (name
== "Xadd64" || name
== "Xaddint64"))
11801 Runtime::Function code
;
11803 if (name
== "Xadd")
11805 code
= Runtime::ATOMIC_ADD_FETCH_4
;
11806 res_type
= uint32_type
;
11808 else if (name
== "Xadd64")
11810 code
= Runtime::ATOMIC_ADD_FETCH_8
;
11811 res_type
= uint64_type
;
11813 else if (name
== "$Xaddint32")
11815 code
= Runtime::ATOMIC_ADD_FETCH_4
;
11816 res_type
= int32_type
;
11818 else if (name
== "Xaddint64")
11820 code
= Runtime::ATOMIC_ADD_FETCH_8
;
11821 res_type
= int64_type
;
11823 else if (name
== "Xadduintptr")
11825 code
= (ptr_size
== 8
11826 ? Runtime::ATOMIC_ADD_FETCH_8
11827 : Runtime::ATOMIC_ADD_FETCH_4
);
11828 res_type
= uintptr_type
;
11832 Expression
* a1
= this->args_
->at(0);
11833 Expression
* a2
= this->args_
->at(1);
11834 Expression
* a3
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
11835 Expression
* call
= Runtime::make_call(code
, loc
, 3, a1
, a2
, a3
);
11836 return Expression::make_cast(res_type
, call
, loc
);
11839 if ((name
== "And8" || name
== "Or8")
11840 && this->args_
!= NULL
&& this->args_
->size() == 2)
11842 Runtime::Function code
;
11843 if (name
== "And8")
11844 code
= Runtime::ATOMIC_AND_FETCH_1
;
11845 else if (name
== "Or8")
11846 code
= Runtime::ATOMIC_OR_FETCH_1
;
11849 Expression
* a1
= this->args_
->at(0);
11850 Expression
* a2
= this->args_
->at(1);
11851 Expression
* a3
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
11852 return Runtime::make_call(code
, loc
, 3, a1
, a2
, a3
);
11859 // Make implicit type conversions explicit.
11862 Call_expression::do_add_conversions()
11864 // Skip call that requires a thunk. We generate conversions inside the thunk.
11865 if (this->is_concurrent_
|| this->is_deferred_
)
11868 if (this->args_
== NULL
|| this->args_
->empty())
11871 Function_type
* fntype
= this->get_function_type();
11872 if (fntype
== NULL
)
11874 go_assert(saw_errors());
11877 if (fntype
->parameters() == NULL
|| fntype
->parameters()->empty())
11880 Location loc
= this->location();
11881 Expression_list::iterator pa
= this->args_
->begin();
11882 Typed_identifier_list::const_iterator pp
= fntype
->parameters()->begin();
11883 bool is_interface_method
=
11884 this->fn_
->interface_field_reference_expression() != NULL
;
11885 size_t argcount
= this->args_
->size();
11886 if (!is_interface_method
&& fntype
->is_method())
11888 // Skip the receiver argument, which cannot be interface.
11892 if (argcount
!= fntype
->parameters()->size())
11894 go_assert(saw_errors());
11897 for (; pa
!= this->args_
->end(); ++pa
, ++pp
)
11899 Type
* pt
= pp
->type();
11900 if (!Type::are_identical(pt
, (*pa
)->type(), 0, NULL
)
11901 && pt
->interface_type() != NULL
)
11902 *pa
= Expression::make_cast(pt
, *pa
, loc
);
11906 // Get the function type. This can return NULL in error cases.
11909 Call_expression::get_function_type() const
11911 return this->fn_
->type()->function_type();
11914 // Return the number of values which this call will return.
11917 Call_expression::result_count() const
11919 const Function_type
* fntype
= this->get_function_type();
11920 if (fntype
== NULL
)
11922 if (fntype
->results() == NULL
)
11924 return fntype
->results()->size();
11927 // Return the temporary that holds the result for a call with multiple
11930 Temporary_statement
*
11931 Call_expression::results() const
11933 if (this->call_temp_
== NULL
)
11935 go_assert(saw_errors());
11938 return this->call_temp_
;
11941 // Set the number of results expected from a call expression.
11944 Call_expression::set_expected_result_count(size_t count
)
11946 go_assert(this->expected_result_count_
== 0);
11947 this->expected_result_count_
= count
;
11950 // Return whether this is a call to the predeclared function recover.
11953 Call_expression::is_recover_call() const
11955 return this->do_is_recover_call();
11958 // Set the argument to the recover function.
11961 Call_expression::set_recover_arg(Expression
* arg
)
11963 this->do_set_recover_arg(arg
);
11966 // Virtual functions also implemented by Builtin_call_expression.
11969 Call_expression::do_is_recover_call() const
11975 Call_expression::do_set_recover_arg(Expression
*)
11980 // We have found an error with this call expression; return true if
11981 // we should report it.
11984 Call_expression::issue_error()
11986 if (this->issued_error_
)
11990 this->issued_error_
= true;
11995 // Whether or not this call contains errors, either in the call or the
11996 // arguments to the call.
11999 Call_expression::is_erroneous_call()
12001 if (this->is_error_expression() || this->fn()->is_error_expression())
12004 if (this->args() == NULL
)
12006 for (Expression_list::iterator pa
= this->args()->begin();
12007 pa
!= this->args()->end();
12010 if ((*pa
)->type()->is_error_type() || (*pa
)->is_error_expression())
12019 Call_expression::do_type()
12021 if (this->is_error_expression())
12022 return Type::make_error_type();
12023 if (this->type_
!= NULL
)
12024 return this->type_
;
12027 Function_type
* fntype
= this->get_function_type();
12028 if (fntype
== NULL
)
12029 return Type::make_error_type();
12031 const Typed_identifier_list
* results
= fntype
->results();
12032 if (results
== NULL
)
12033 ret
= Type::make_void_type();
12034 else if (results
->size() == 1)
12035 ret
= results
->begin()->type();
12037 ret
= Type::make_call_multiple_result_type(this);
12041 return this->type_
;
12044 // Determine types for a call expression. We can use the function
12045 // parameter types to set the types of the arguments.
12048 Call_expression::do_determine_type(const Type_context
* context
)
12050 if (!this->determining_types())
12053 this->fn_
->determine_type_no_context();
12054 Function_type
* fntype
= this->get_function_type();
12055 const Typed_identifier_list
* parameters
= NULL
;
12056 if (fntype
!= NULL
)
12057 parameters
= fntype
->parameters();
12058 if (this->args_
!= NULL
)
12060 Typed_identifier_list::const_iterator pt
;
12061 if (parameters
!= NULL
)
12062 pt
= parameters
->begin();
12064 for (Expression_list::const_iterator pa
= this->args_
->begin();
12065 pa
!= this->args_
->end();
12071 // If this is a method, the first argument is the
12073 if (fntype
!= NULL
&& fntype
->is_method())
12075 Type
* rtype
= fntype
->receiver()->type();
12076 // The receiver is always passed as a pointer.
12077 if (rtype
->points_to() == NULL
)
12078 rtype
= Type::make_pointer_type(rtype
);
12079 Type_context
subcontext(rtype
, false);
12080 (*pa
)->determine_type(&subcontext
);
12085 if (parameters
!= NULL
&& pt
!= parameters
->end())
12087 Type_context
subcontext(pt
->type(), false);
12088 (*pa
)->determine_type(&subcontext
);
12092 (*pa
)->determine_type_no_context();
12096 // If this is a call to a generated equality function, we determine
12097 // the type based on the context. See the comment in
12098 // Binary_expression::lower_array_comparison.
12099 if (this->is_equal_function_
12100 && !context
->may_be_abstract
12101 && context
->type
!= NULL
12102 && context
->type
->is_boolean_type()
12103 && context
->type
!= Type::lookup_bool_type())
12105 go_assert(this->type_
== NULL
12106 || this->type_
== Type::lookup_bool_type()
12107 || this->type_
== context
->type
12108 || this->type_
->is_error());
12109 this->type_
= context
->type
;
12113 // Called when determining types for a Call_expression. Return true
12114 // if we should go ahead, false if they have already been determined.
12117 Call_expression::determining_types()
12119 if (this->types_are_determined_
)
12123 this->types_are_determined_
= true;
12128 // Check types for parameter I.
12131 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
12132 const Type
* argument_type
,
12133 Location argument_location
,
12136 std::string reason
;
12137 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
12141 if (reason
.empty())
12142 go_error_at(argument_location
, "argument %d has incompatible type", i
);
12144 go_error_at(argument_location
,
12145 "argument %d has incompatible type (%s)",
12146 i
, reason
.c_str());
12148 this->set_is_error();
12157 Call_expression::do_check_types(Gogo
*)
12159 if (this->classification() == EXPRESSION_ERROR
)
12162 Function_type
* fntype
= this->get_function_type();
12163 if (fntype
== NULL
)
12165 if (!this->fn_
->type()->is_error())
12166 this->report_error(_("expected function"));
12170 if (this->expected_result_count_
!= 0
12171 && this->expected_result_count_
!= this->result_count())
12173 if (this->issue_error())
12174 this->report_error(_("function result count mismatch"));
12175 this->set_is_error();
12179 bool is_method
= fntype
->is_method();
12182 go_assert(this->args_
!= NULL
&& !this->args_
->empty());
12183 Type
* rtype
= fntype
->receiver()->type();
12184 Expression
* first_arg
= this->args_
->front();
12185 // We dereference the values since receivers are always passed
12187 std::string reason
;
12188 if (!Type::are_assignable(rtype
->deref(), first_arg
->type()->deref(),
12191 if (reason
.empty())
12192 this->report_error(_("incompatible type for receiver"));
12195 go_error_at(this->location(),
12196 "incompatible type for receiver (%s)",
12198 this->set_is_error();
12203 // Note that varargs was handled by the lower_varargs() method, so
12204 // we don't have to worry about it here unless something is wrong.
12205 if (this->is_varargs_
&& !this->varargs_are_lowered_
)
12207 if (!fntype
->is_varargs())
12209 go_error_at(this->location(),
12210 _("invalid use of %<...%> calling non-variadic function"));
12211 this->set_is_error();
12216 const Typed_identifier_list
* parameters
= fntype
->parameters();
12217 if (this->args_
== NULL
|| this->args_
->size() == 0)
12219 if (parameters
!= NULL
&& !parameters
->empty())
12220 this->report_error(_("not enough arguments"));
12222 else if (parameters
== NULL
)
12224 if (!is_method
|| this->args_
->size() > 1)
12225 this->report_error(_("too many arguments"));
12227 else if (this->args_
->size() == 1
12228 && this->args_
->front()->call_expression() != NULL
12229 && this->args_
->front()->call_expression()->result_count() > 1)
12231 // This is F(G()) when G returns more than one result. If the
12232 // results can be matched to parameters, it would have been
12233 // lowered in do_lower. If we get here we know there is a
12235 if (this->args_
->front()->call_expression()->result_count()
12236 < parameters
->size())
12237 this->report_error(_("not enough arguments"));
12239 this->report_error(_("too many arguments"));
12244 Expression_list::const_iterator pa
= this->args_
->begin();
12247 for (Typed_identifier_list::const_iterator pt
= parameters
->begin();
12248 pt
!= parameters
->end();
12251 if (pa
== this->args_
->end())
12253 this->report_error(_("not enough arguments"));
12256 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
12257 (*pa
)->location(), false);
12259 if (pa
!= this->args_
->end())
12260 this->report_error(_("too many arguments"));
12265 Call_expression::do_copy()
12267 Call_expression
* call
=
12268 Expression::make_call(this->fn_
->copy(),
12269 (this->args_
== NULL
12271 : this->args_
->copy()),
12272 this->is_varargs_
, this->location());
12274 if (this->varargs_are_lowered_
)
12275 call
->set_varargs_are_lowered();
12276 if (this->is_deferred_
)
12277 call
->set_is_deferred();
12278 if (this->is_concurrent_
)
12279 call
->set_is_concurrent();
12283 // Return whether we have to use a temporary variable to ensure that
12284 // we evaluate this call expression in order. If the call returns no
12285 // results then it will inevitably be executed last.
12288 Call_expression::do_must_eval_in_order() const
12290 return this->result_count() > 0;
12293 // Get the function and the first argument to use when calling an
12294 // interface method.
12297 Call_expression::interface_method_function(
12298 Interface_field_reference_expression
* interface_method
,
12299 Expression
** first_arg_ptr
,
12302 Expression
* object
= interface_method
->get_underlying_object();
12303 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
12305 Expression::make_unsafe_cast(unsafe_ptr_type
, object
, location
);
12306 return interface_method
->get_function();
12309 // Build the call expression.
12312 Call_expression::do_get_backend(Translate_context
* context
)
12314 Location location
= this->location();
12316 if (this->call_
!= NULL
)
12318 // If the call returns multiple results, make a new reference to
12320 if (this->call_temp_
!= NULL
)
12323 Expression::make_temporary_reference(this->call_temp_
, location
);
12324 return ref
->get_backend(context
);
12327 return this->call_
;
12330 Function_type
* fntype
= this->get_function_type();
12331 if (fntype
== NULL
)
12332 return context
->backend()->error_expression();
12334 if (this->fn_
->is_error_expression())
12335 return context
->backend()->error_expression();
12337 Gogo
* gogo
= context
->gogo();
12339 Func_expression
* func
= this->fn_
->func_expression();
12340 Interface_field_reference_expression
* interface_method
=
12341 this->fn_
->interface_field_reference_expression();
12342 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
12343 const bool is_interface_method
= interface_method
!= NULL
;
12345 bool has_closure_arg
;
12347 has_closure_arg
= true;
12348 else if (func
!= NULL
)
12349 has_closure_arg
= false;
12350 else if (is_interface_method
)
12351 has_closure_arg
= false;
12353 has_closure_arg
= true;
12355 Expression
* first_arg
= NULL
;
12356 if (!is_interface_method
&& fntype
->is_method())
12358 first_arg
= this->args_
->front();
12359 if (first_arg
->type()->points_to() == NULL
12360 && first_arg
->type()->is_direct_iface_type())
12361 first_arg
= Expression::unpack_direct_iface(first_arg
,
12362 first_arg
->location());
12366 std::vector
<Bexpression
*> fn_args
;
12367 if (this->args_
== NULL
|| this->args_
->empty())
12369 nargs
= is_interface_method
? 1 : 0;
12373 else if (fntype
->parameters() == NULL
|| fntype
->parameters()->empty())
12375 // Passing a receiver parameter.
12376 go_assert(!is_interface_method
12377 && fntype
->is_method()
12378 && this->args_
->size() == 1);
12381 fn_args
[0] = first_arg
->get_backend(context
);
12385 const Typed_identifier_list
* params
= fntype
->parameters();
12387 nargs
= this->args_
->size();
12388 int i
= is_interface_method
? 1 : 0;
12390 fn_args
.resize(nargs
);
12392 Typed_identifier_list::const_iterator pp
= params
->begin();
12393 Expression_list::const_iterator pe
= this->args_
->begin();
12394 if (!is_interface_method
&& fntype
->is_method())
12396 fn_args
[i
] = first_arg
->get_backend(context
);
12400 for (; pe
!= this->args_
->end(); ++pe
, ++pp
, ++i
)
12402 go_assert(pp
!= params
->end());
12404 Expression::convert_for_assignment(gogo
, pp
->type(), *pe
,
12406 fn_args
[i
] = arg
->get_backend(context
);
12408 go_assert(pp
== params
->end());
12409 go_assert(i
== nargs
);
12413 Expression
* closure
= NULL
;
12416 Named_object
* no
= func
->named_object();
12417 fn
= Expression::make_func_code_reference(no
, location
);
12419 closure
= func
->closure();
12421 else if (!is_interface_method
)
12423 closure
= this->fn_
;
12425 // The backend representation of this function type is a pointer
12426 // to a struct whose first field is the actual function to call.
12428 Type::make_pointer_type(
12429 Type::make_pointer_type(Type::make_void_type()));
12430 fn
= Expression::make_unsafe_cast(pfntype
, this->fn_
, location
);
12431 fn
= Expression::make_dereference(fn
, NIL_CHECK_NOT_NEEDED
, location
);
12436 fn
= this->interface_method_function(interface_method
, &arg0
,
12438 fn_args
[0] = arg0
->get_backend(context
);
12441 Bexpression
* bclosure
= NULL
;
12442 if (has_closure_arg
)
12443 bclosure
= closure
->get_backend(context
);
12445 go_assert(closure
== NULL
);
12447 Bexpression
* bfn
= fn
->get_backend(context
);
12449 // When not calling a named function directly, use a type conversion
12450 // in case the type of the function is a recursive type which refers
12451 // to itself. We don't do this for an interface method because 1)
12452 // an interface method never refers to itself, so we always have a
12453 // function type here; 2) we pass an extra first argument to an
12454 // interface method, so fntype is not correct.
12455 if (func
== NULL
&& !is_interface_method
)
12457 Btype
* bft
= fntype
->get_backend_fntype(gogo
);
12458 bfn
= gogo
->backend()->convert_expression(bft
, bfn
, location
);
12461 Bfunction
* bfunction
= NULL
;
12462 if (context
->function())
12463 bfunction
= context
->function()->func_value()->get_decl();
12464 Bexpression
* call
= gogo
->backend()->call_expression(bfunction
, bfn
,
12468 if (this->call_temp_
!= NULL
)
12470 // This case occurs when the call returns multiple results.
12472 Expression
* ref
= Expression::make_temporary_reference(this->call_temp_
,
12474 Bexpression
* bref
= ref
->get_backend(context
);
12475 Bstatement
* bassn
= gogo
->backend()->assignment_statement(bfunction
,
12479 ref
= Expression::make_temporary_reference(this->call_temp_
, location
);
12480 this->call_
= ref
->get_backend(context
);
12482 return gogo
->backend()->compound_expression(bassn
, this->call_
,
12486 this->call_
= call
;
12487 return this->call_
;
12490 // The cost of inlining a call expression.
12493 Call_expression::do_inlining_cost() const
12495 Func_expression
* fn
= this->fn_
->func_expression();
12497 // FIXME: We don't yet support all kinds of calls.
12498 if (fn
!= NULL
&& fn
->closure() != NULL
)
12500 if (this->fn_
->interface_field_reference_expression())
12502 if (this->get_function_type()->is_method())
12508 // Export a call expression.
12511 Call_expression::do_export(Export_function_body
* efb
) const
12513 bool simple_call
= (this->fn_
->func_expression() != NULL
);
12515 efb
->write_c_string("(");
12516 this->fn_
->export_expression(efb
);
12518 efb
->write_c_string(")");
12519 this->export_arguments(efb
);
12522 // Export call expression arguments.
12525 Call_expression::export_arguments(Export_function_body
* efb
) const
12527 efb
->write_c_string("(");
12528 if (this->args_
!= NULL
&& !this->args_
->empty())
12530 Expression_list::const_iterator pa
= this->args_
->begin();
12531 (*pa
)->export_expression(efb
);
12532 for (pa
++; pa
!= this->args_
->end(); pa
++)
12534 efb
->write_c_string(", ");
12535 (*pa
)->export_expression(efb
);
12537 if (this->is_varargs_
)
12538 efb
->write_c_string("...");
12540 efb
->write_c_string(")");
12543 // Dump ast representation for a call expression.
12546 Call_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
12548 this->fn_
->dump_expression(ast_dump_context
);
12549 ast_dump_context
->ostream() << "(";
12551 ast_dump_context
->dump_expression_list(this->args_
);
12553 ast_dump_context
->ostream() << ") ";
12556 // Make a call expression.
12559 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
12562 return new Call_expression(fn
, args
, is_varargs
, location
);
12565 // Class Call_result_expression.
12567 // Traverse a call result.
12570 Call_result_expression::do_traverse(Traverse
* traverse
)
12572 if (traverse
->remember_expression(this->call_
))
12574 // We have already traversed the call expression.
12575 return TRAVERSE_CONTINUE
;
12577 return Expression::traverse(&this->call_
, traverse
);
12583 Call_result_expression::do_type()
12585 if (this->classification() == EXPRESSION_ERROR
)
12586 return Type::make_error_type();
12588 // THIS->CALL_ can be replaced with a temporary reference due to
12589 // Call_expression::do_must_eval_in_order when there is an error.
12590 Call_expression
* ce
= this->call_
->call_expression();
12593 this->set_is_error();
12594 return Type::make_error_type();
12596 Function_type
* fntype
= ce
->get_function_type();
12597 if (fntype
== NULL
)
12599 if (ce
->issue_error())
12601 if (!ce
->fn()->type()->is_error())
12602 this->report_error(_("expected function"));
12604 this->set_is_error();
12605 return Type::make_error_type();
12607 const Typed_identifier_list
* results
= fntype
->results();
12608 if (results
== NULL
|| results
->size() < 2)
12610 if (ce
->issue_error())
12611 this->report_error(_("number of results does not match "
12612 "number of values"));
12613 return Type::make_error_type();
12615 Typed_identifier_list::const_iterator pr
= results
->begin();
12616 for (unsigned int i
= 0; i
< this->index_
; ++i
)
12618 if (pr
== results
->end())
12622 if (pr
== results
->end())
12624 if (ce
->issue_error())
12625 this->report_error(_("number of results does not match "
12626 "number of values"));
12627 return Type::make_error_type();
12632 // Check the type. Just make sure that we trigger the warning in
12636 Call_result_expression::do_check_types(Gogo
*)
12641 // Determine the type. We have nothing to do here, but the 0 result
12642 // needs to pass down to the caller.
12645 Call_result_expression::do_determine_type(const Type_context
*)
12647 this->call_
->determine_type_no_context();
12650 // Return the backend representation. We just refer to the temporary set by the
12651 // call expression. We don't do this at lowering time because it makes it
12652 // hard to evaluate the call at the right time.
12655 Call_result_expression::do_get_backend(Translate_context
* context
)
12657 Call_expression
* ce
= this->call_
->call_expression();
12660 go_assert(this->call_
->is_error_expression());
12661 return context
->backend()->error_expression();
12663 Temporary_statement
* ts
= ce
->results();
12666 go_assert(saw_errors());
12667 return context
->backend()->error_expression();
12669 Expression
* ref
= Expression::make_temporary_reference(ts
, this->location());
12670 ref
= Expression::make_field_reference(ref
, this->index_
, this->location());
12671 return ref
->get_backend(context
);
12674 // Dump ast representation for a call result expression.
12677 Call_result_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
12680 // FIXME: Wouldn't it be better if the call is assigned to a temporary
12681 // (struct) and the fields are referenced instead.
12682 ast_dump_context
->ostream() << this->index_
<< "@(";
12683 ast_dump_context
->dump_expression(this->call_
);
12684 ast_dump_context
->ostream() << ")";
12687 // Make a reference to a single result of a call which returns
12688 // multiple results.
12691 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
12693 return new Call_result_expression(call
, index
);
12696 // Class Index_expression.
12701 Index_expression::do_traverse(Traverse
* traverse
)
12703 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
12704 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
12705 || (this->end_
!= NULL
12706 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
12707 || (this->cap_
!= NULL
12708 && Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
))
12709 return TRAVERSE_EXIT
;
12710 return TRAVERSE_CONTINUE
;
12713 // Lower an index expression. This converts the generic index
12714 // expression into an array index, a string index, or a map index.
12717 Index_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
12719 Location location
= this->location();
12720 Expression
* left
= this->left_
;
12721 Expression
* start
= this->start_
;
12722 Expression
* end
= this->end_
;
12723 Expression
* cap
= this->cap_
;
12725 Type
* type
= left
->type();
12726 if (type
->is_error())
12728 go_assert(saw_errors());
12729 return Expression::make_error(location
);
12731 else if (left
->is_type_expression())
12733 go_error_at(location
, "attempt to index type expression");
12734 return Expression::make_error(location
);
12736 else if (type
->array_type() != NULL
)
12737 return Expression::make_array_index(left
, start
, end
, cap
, location
);
12738 else if (type
->points_to() != NULL
12739 && type
->points_to()->array_type() != NULL
12740 && !type
->points_to()->is_slice_type())
12742 Expression
* deref
=
12743 Expression::make_dereference(left
, NIL_CHECK_DEFAULT
, location
);
12745 // For an ordinary index into the array, the pointer will be
12746 // dereferenced. For a slice it will not--the resulting slice
12747 // will simply reuse the pointer, which is incorrect if that
12749 if (end
!= NULL
|| cap
!= NULL
)
12750 deref
->issue_nil_check();
12752 return Expression::make_array_index(deref
, start
, end
, cap
, location
);
12754 else if (type
->is_string_type())
12758 go_error_at(location
, "invalid 3-index slice of string");
12759 return Expression::make_error(location
);
12761 return Expression::make_string_index(left
, start
, end
, location
);
12763 else if (type
->map_type() != NULL
)
12765 if (end
!= NULL
|| cap
!= NULL
)
12767 go_error_at(location
, "invalid slice of map");
12768 return Expression::make_error(location
);
12770 return Expression::make_map_index(left
, start
, location
);
12772 else if (cap
!= NULL
)
12774 go_error_at(location
,
12775 "invalid 3-index slice of object that is not a slice");
12776 return Expression::make_error(location
);
12778 else if (end
!= NULL
)
12780 go_error_at(location
,
12781 ("attempt to slice object that is not "
12782 "array, slice, or string"));
12783 return Expression::make_error(location
);
12787 go_error_at(location
,
12788 ("attempt to index object that is not "
12789 "array, slice, string, or map"));
12790 return Expression::make_error(location
);
12794 // Write an indexed expression
12795 // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context.
12798 Index_expression::dump_index_expression(Ast_dump_context
* ast_dump_context
,
12799 const Expression
* expr
,
12800 const Expression
* start
,
12801 const Expression
* end
,
12802 const Expression
* cap
)
12804 expr
->dump_expression(ast_dump_context
);
12805 ast_dump_context
->ostream() << "[";
12806 start
->dump_expression(ast_dump_context
);
12809 ast_dump_context
->ostream() << ":";
12810 end
->dump_expression(ast_dump_context
);
12814 ast_dump_context
->ostream() << ":";
12815 cap
->dump_expression(ast_dump_context
);
12817 ast_dump_context
->ostream() << "]";
12820 // Dump ast representation for an index expression.
12823 Index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
12826 Index_expression::dump_index_expression(ast_dump_context
, this->left_
,
12827 this->start_
, this->end_
, this->cap_
);
12830 // Make an index expression.
12833 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
12834 Expression
* cap
, Location location
)
12836 return new Index_expression(left
, start
, end
, cap
, location
);
12839 // Class Array_index_expression.
12841 // Array index traversal.
12844 Array_index_expression::do_traverse(Traverse
* traverse
)
12846 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
12847 return TRAVERSE_EXIT
;
12848 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
12849 return TRAVERSE_EXIT
;
12850 if (this->end_
!= NULL
)
12852 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
12853 return TRAVERSE_EXIT
;
12855 if (this->cap_
!= NULL
)
12857 if (Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
)
12858 return TRAVERSE_EXIT
;
12860 return TRAVERSE_CONTINUE
;
12863 // Return the type of an array index.
12866 Array_index_expression::do_type()
12868 if (this->type_
== NULL
)
12870 Array_type
* type
= this->array_
->type()->array_type();
12872 this->type_
= Type::make_error_type();
12873 else if (this->end_
== NULL
)
12874 this->type_
= type
->element_type();
12875 else if (type
->is_slice_type())
12877 // A slice of a slice has the same type as the original
12879 this->type_
= this->array_
->type()->deref();
12883 // A slice of an array is a slice.
12884 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
12887 return this->type_
;
12890 // Set the type of an array index.
12893 Array_index_expression::do_determine_type(const Type_context
*)
12895 this->array_
->determine_type_no_context();
12897 Type_context
index_context(Type::lookup_integer_type("int"), false);
12898 this->start_
->determine_type(&index_context
);
12899 if (this->end_
!= NULL
)
12900 this->end_
->determine_type(&index_context
);
12901 if (this->cap_
!= NULL
)
12902 this->cap_
->determine_type(&index_context
);
12905 // Check types of an array index.
12908 Array_index_expression::do_check_types(Gogo
*)
12910 Numeric_constant nc
;
12912 if (this->start_
->type()->integer_type() == NULL
12913 && !this->start_
->type()->is_error()
12914 && (!this->start_
->type()->is_abstract()
12915 || !this->start_
->numeric_constant_value(&nc
)
12916 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
12917 this->report_error(_("index must be integer"));
12918 if (this->end_
!= NULL
12919 && this->end_
->type()->integer_type() == NULL
12920 && !this->end_
->type()->is_error()
12921 && !this->end_
->is_nil_expression()
12922 && !this->end_
->is_error_expression()
12923 && (!this->end_
->type()->is_abstract()
12924 || !this->end_
->numeric_constant_value(&nc
)
12925 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
12926 this->report_error(_("slice end must be integer"));
12927 if (this->cap_
!= NULL
12928 && this->cap_
->type()->integer_type() == NULL
12929 && !this->cap_
->type()->is_error()
12930 && !this->cap_
->is_nil_expression()
12931 && !this->cap_
->is_error_expression()
12932 && (!this->cap_
->type()->is_abstract()
12933 || !this->cap_
->numeric_constant_value(&nc
)
12934 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
12935 this->report_error(_("slice capacity must be integer"));
12937 Array_type
* array_type
= this->array_
->type()->array_type();
12938 if (array_type
== NULL
)
12940 go_assert(this->array_
->type()->is_error());
12944 unsigned int int_bits
=
12945 Type::lookup_integer_type("int")->integer_type()->bits();
12947 Numeric_constant lvalnc
;
12949 bool lval_valid
= (array_type
->length() != NULL
12950 && array_type
->length()->numeric_constant_value(&lvalnc
)
12951 && lvalnc
.to_int(&lval
));
12952 Numeric_constant inc
;
12954 bool ival_valid
= false;
12955 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
12958 if (mpz_sgn(ival
) < 0
12959 || mpz_sizeinbase(ival
, 2) >= int_bits
12961 && (this->end_
== NULL
12962 ? mpz_cmp(ival
, lval
) >= 0
12963 : mpz_cmp(ival
, lval
) > 0)))
12965 go_error_at(this->start_
->location(), "array index out of bounds");
12966 this->set_is_error();
12969 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
12971 Numeric_constant enc
;
12973 bool eval_valid
= false;
12974 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
12977 if (mpz_sgn(eval
) < 0
12978 || mpz_sizeinbase(eval
, 2) >= int_bits
12979 || (lval_valid
&& mpz_cmp(eval
, lval
) > 0))
12981 go_error_at(this->end_
->location(), "array index out of bounds");
12982 this->set_is_error();
12984 else if (ival_valid
&& mpz_cmp(ival
, eval
) > 0)
12985 this->report_error(_("inverted slice range"));
12988 Numeric_constant cnc
;
12990 if (this->cap_
!= NULL
12991 && this->cap_
->numeric_constant_value(&cnc
) && cnc
.to_int(&cval
))
12993 if (mpz_sgn(cval
) < 0
12994 || mpz_sizeinbase(cval
, 2) >= int_bits
12995 || (lval_valid
&& mpz_cmp(cval
, lval
) > 0))
12997 go_error_at(this->cap_
->location(), "array index out of bounds");
12998 this->set_is_error();
13000 else if (ival_valid
&& mpz_cmp(ival
, cval
) > 0)
13002 go_error_at(this->cap_
->location(),
13003 "invalid slice index: capacity less than start");
13004 this->set_is_error();
13006 else if (eval_valid
&& mpz_cmp(eval
, cval
) > 0)
13008 go_error_at(this->cap_
->location(),
13009 "invalid slice index: capacity less than length");
13010 this->set_is_error();
13023 // A slice of an array requires an addressable array. A slice of a
13024 // slice is always possible.
13025 if (this->end_
!= NULL
&& !array_type
->is_slice_type())
13027 if (!this->array_
->is_addressable())
13028 this->report_error(_("slice of unaddressable value"));
13030 // Set the array address taken but not escape. The escape
13031 // analysis will make it escape to heap when needed.
13032 this->array_
->address_taken(false);
13036 // The subexpressions of an array index must be evaluated in order.
13037 // If this is indexing into an array, rather than a slice, then only
13038 // the index should be evaluated. Since this is called for values on
13039 // the left hand side of an assigment, evaluating the array, meaning
13040 // copying the array, will cause a different array to be modified.
13043 Array_index_expression::do_must_eval_subexpressions_in_order(
13046 *skip
= this->array_
->type()->is_slice_type() ? 0 : 1;
13050 // Flatten array indexing: add temporary variables and bounds checks.
13053 Array_index_expression::do_flatten(Gogo
* gogo
, Named_object
*,
13054 Statement_inserter
* inserter
)
13056 if (this->is_flattened_
)
13058 this->is_flattened_
= true;
13060 Location loc
= this->location();
13062 if (this->is_error_expression())
13063 return Expression::make_error(loc
);
13065 Expression
* array
= this->array_
;
13066 Expression
* start
= this->start_
;
13067 Expression
* end
= this->end_
;
13068 Expression
* cap
= this->cap_
;
13069 if (array
->is_error_expression()
13070 || array
->type()->is_error_type()
13071 || start
->is_error_expression()
13072 || start
->type()->is_error_type()
13074 && (end
->is_error_expression() || end
->type()->is_error_type()))
13076 && (cap
->is_error_expression() || cap
->type()->is_error_type())))
13078 go_assert(saw_errors());
13079 return Expression::make_error(loc
);
13082 Array_type
* array_type
= this->array_
->type()->array_type();
13083 if (array_type
== NULL
)
13085 go_assert(saw_errors());
13086 return Expression::make_error(loc
);
13089 Temporary_statement
* temp
;
13090 if (array_type
->is_slice_type() && !array
->is_variable())
13092 temp
= Statement::make_temporary(NULL
, array
, loc
);
13093 inserter
->insert(temp
);
13094 this->array_
= Expression::make_temporary_reference(temp
, loc
);
13095 array
= this->array_
;
13097 if (!start
->is_variable() && !start
->is_constant())
13099 temp
= Statement::make_temporary(NULL
, start
, loc
);
13100 inserter
->insert(temp
);
13101 this->start_
= Expression::make_temporary_reference(temp
, loc
);
13102 start
= this->start_
;
13105 && !end
->is_nil_expression()
13106 && !end
->is_variable()
13107 && !end
->is_constant())
13109 temp
= Statement::make_temporary(NULL
, end
, loc
);
13110 inserter
->insert(temp
);
13111 this->end_
= Expression::make_temporary_reference(temp
, loc
);
13114 if (cap
!= NULL
&& !cap
->is_variable() && !cap
->is_constant())
13116 temp
= Statement::make_temporary(NULL
, cap
, loc
);
13117 inserter
->insert(temp
);
13118 this->cap_
= Expression::make_temporary_reference(temp
, loc
);
13122 if (!this->needs_bounds_check_
)
13126 if (!array_type
->is_slice_type())
13128 len
= array_type
->get_length(gogo
, this->array_
);
13129 go_assert(len
->is_constant());
13133 len
= array_type
->get_length(gogo
, this->array_
->copy());
13134 temp
= Statement::make_temporary(NULL
, len
, loc
);
13135 inserter
->insert(temp
);
13136 len
= Expression::make_temporary_reference(temp
, loc
);
13139 Expression
* scap
= NULL
;
13140 if (array_type
->is_slice_type())
13142 scap
= array_type
->get_capacity(gogo
, this->array_
->copy());
13143 temp
= Statement::make_temporary(NULL
, scap
, loc
);
13144 inserter
->insert(temp
);
13145 scap
= Expression::make_temporary_reference(temp
, loc
);
13148 // The order of bounds checks here matches the order used by the gc
13149 // compiler, as tested by issue30116[u].go.
13153 if (array_type
->is_slice_type())
13154 Expression::check_bounds(cap
, OPERATOR_LE
, scap
,
13155 Runtime::PANIC_SLICE3_ACAP
,
13156 Runtime::PANIC_SLICE3_ACAP_U
,
13157 Runtime::PANIC_EXTEND_SLICE3_ACAP
,
13158 Runtime::PANIC_EXTEND_SLICE3_ACAP_U
,
13161 Expression::check_bounds(cap
, OPERATOR_LE
, len
,
13162 Runtime::PANIC_SLICE3_ALEN
,
13163 Runtime::PANIC_SLICE3_ALEN_U
,
13164 Runtime::PANIC_EXTEND_SLICE3_ALEN
,
13165 Runtime::PANIC_EXTEND_SLICE3_ALEN_U
,
13168 Expression
* start_bound
= cap
;
13169 if (end
!= NULL
&& !end
->is_nil_expression())
13171 Expression::check_bounds(end
, OPERATOR_LE
, cap
,
13172 Runtime::PANIC_SLICE3_B
,
13173 Runtime::PANIC_SLICE3_B_U
,
13174 Runtime::PANIC_EXTEND_SLICE3_B
,
13175 Runtime::PANIC_EXTEND_SLICE3_B_U
,
13180 Expression::check_bounds(start
, OPERATOR_LE
, start_bound
,
13181 Runtime::PANIC_SLICE3_C
,
13182 Runtime::PANIC_SLICE3_C_U
,
13183 Runtime::PANIC_EXTEND_SLICE3_C
,
13184 Runtime::PANIC_EXTEND_SLICE3_C_U
,
13187 else if (end
!= NULL
&& !end
->is_nil_expression())
13189 if (array_type
->is_slice_type())
13190 Expression::check_bounds(end
, OPERATOR_LE
, scap
,
13191 Runtime::PANIC_SLICE_ACAP
,
13192 Runtime::PANIC_SLICE_ACAP_U
,
13193 Runtime::PANIC_EXTEND_SLICE_ACAP
,
13194 Runtime::PANIC_EXTEND_SLICE_ACAP_U
,
13197 Expression::check_bounds(end
, OPERATOR_LE
, len
,
13198 Runtime::PANIC_SLICE_ALEN
,
13199 Runtime::PANIC_SLICE_ALEN_U
,
13200 Runtime::PANIC_EXTEND_SLICE_ALEN
,
13201 Runtime::PANIC_EXTEND_SLICE_ALEN_U
,
13204 Expression::check_bounds(start
, OPERATOR_LE
, end
,
13205 Runtime::PANIC_SLICE_B
,
13206 Runtime::PANIC_SLICE_B_U
,
13207 Runtime::PANIC_EXTEND_SLICE_B
,
13208 Runtime::PANIC_EXTEND_SLICE_B_U
,
13211 else if (end
!= NULL
)
13213 Expression
* start_bound
;
13214 if (array_type
->is_slice_type())
13215 start_bound
= scap
;
13218 Expression::check_bounds(start
, OPERATOR_LE
, start_bound
,
13219 Runtime::PANIC_SLICE_B
,
13220 Runtime::PANIC_SLICE_B_U
,
13221 Runtime::PANIC_EXTEND_SLICE_B
,
13222 Runtime::PANIC_EXTEND_SLICE_B_U
,
13226 Expression::check_bounds(start
, OPERATOR_LT
, len
,
13227 Runtime::PANIC_INDEX
,
13228 Runtime::PANIC_INDEX_U
,
13229 Runtime::PANIC_EXTEND_INDEX
,
13230 Runtime::PANIC_EXTEND_INDEX_U
,
13236 // Return whether this expression is addressable.
13239 Array_index_expression::do_is_addressable() const
13241 // A slice expression is not addressable.
13242 if (this->end_
!= NULL
)
13245 // An index into a slice is addressable.
13246 if (this->array_
->type()->is_slice_type())
13249 // An index into an array is addressable if the array is
13251 return this->array_
->is_addressable();
13255 Array_index_expression::do_address_taken(bool escapes
)
13257 // In &x[0], if x is a slice, then x's address is not taken.
13258 if (!this->array_
->type()->is_slice_type())
13259 this->array_
->address_taken(escapes
);
13262 // Get the backend representation for an array index.
13265 Array_index_expression::do_get_backend(Translate_context
* context
)
13267 Array_type
* array_type
= this->array_
->type()->array_type();
13268 if (array_type
== NULL
)
13270 go_assert(this->array_
->type()->is_error());
13271 return context
->backend()->error_expression();
13273 go_assert(!array_type
->is_slice_type() || this->array_
->is_variable());
13275 Location loc
= this->location();
13276 Gogo
* gogo
= context
->gogo();
13278 Type
* int_type
= Type::lookup_integer_type("int");
13279 Btype
* int_btype
= int_type
->get_backend(gogo
);
13281 // Convert the length and capacity to "int". FIXME: Do we need to
13283 Bexpression
* length
= NULL
;
13284 if (this->end_
== NULL
|| this->end_
->is_nil_expression())
13286 Expression
* len
= array_type
->get_length(gogo
, this->array_
);
13287 length
= len
->get_backend(context
);
13288 length
= gogo
->backend()->convert_expression(int_btype
, length
, loc
);
13291 Bexpression
* capacity
= NULL
;
13292 if (this->end_
!= NULL
)
13294 Expression
* cap
= array_type
->get_capacity(gogo
, this->array_
);
13295 capacity
= cap
->get_backend(context
);
13296 capacity
= gogo
->backend()->convert_expression(int_btype
, capacity
, loc
);
13299 Bexpression
* cap_arg
= capacity
;
13300 if (this->cap_
!= NULL
)
13302 cap_arg
= this->cap_
->get_backend(context
);
13303 cap_arg
= gogo
->backend()->convert_expression(int_btype
, cap_arg
, loc
);
13306 if (length
== NULL
)
13309 if (this->start_
->type()->integer_type() == NULL
13310 && !Type::are_convertible(int_type
, this->start_
->type(), NULL
))
13312 go_assert(saw_errors());
13313 return context
->backend()->error_expression();
13316 Bexpression
* start
= this->start_
->get_backend(context
);
13317 start
= gogo
->backend()->convert_expression(int_btype
, start
, loc
);
13319 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
13320 if (this->end_
== NULL
)
13322 // Simple array indexing.
13324 if (!array_type
->is_slice_type())
13326 Bexpression
* array
= this->array_
->get_backend(context
);
13327 ret
= gogo
->backend()->array_index_expression(array
, start
, loc
);
13331 Expression
* valptr
=
13332 array_type
->get_value_pointer(gogo
, this->array_
,
13334 Bexpression
* ptr
= valptr
->get_backend(context
);
13335 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, start
, loc
);
13337 Type
* ele_type
= this->array_
->type()->array_type()->element_type();
13338 Btype
* ele_btype
= ele_type
->get_backend(gogo
);
13339 ret
= gogo
->backend()->indirect_expression(ele_btype
, ptr
, false,
13345 // Slice expression.
13348 if (this->end_
->is_nil_expression())
13352 end
= this->end_
->get_backend(context
);
13353 end
= gogo
->backend()->convert_expression(int_btype
, end
, loc
);
13356 Bexpression
* result_length
=
13357 gogo
->backend()->binary_expression(OPERATOR_MINUS
, end
, start
, loc
);
13359 Bexpression
* result_capacity
=
13360 gogo
->backend()->binary_expression(OPERATOR_MINUS
, cap_arg
, start
, loc
);
13362 // If the new capacity is zero, don't change val. Otherwise we can
13363 // get a pointer to the next object in memory, keeping it live
13364 // unnecessarily. When the capacity is zero, the actual pointer
13365 // value doesn't matter.
13366 Bexpression
* zero
=
13367 Expression::make_integer_ul(0, int_type
, loc
)->get_backend(context
);
13368 Bexpression
* cond
=
13369 gogo
->backend()->binary_expression(OPERATOR_EQEQ
, result_capacity
, zero
,
13371 Bexpression
* offset
= gogo
->backend()->conditional_expression(bfn
, int_btype
,
13374 Expression
* valptr
= array_type
->get_value_pointer(gogo
, this->array_
,
13376 Bexpression
* val
= valptr
->get_backend(context
);
13377 val
= gogo
->backend()->pointer_offset_expression(val
, offset
, loc
);
13379 Btype
* struct_btype
= this->type()->get_backend(gogo
);
13380 std::vector
<Bexpression
*> init
;
13381 init
.push_back(val
);
13382 init
.push_back(result_length
);
13383 init
.push_back(result_capacity
);
13385 return gogo
->backend()->constructor_expression(struct_btype
, init
, loc
);
13388 // Export an array index expression.
13391 Array_index_expression::do_export(Export_function_body
* efb
) const
13393 efb
->write_c_string("(");
13394 this->array_
->export_expression(efb
);
13395 efb
->write_c_string(")[");
13397 Type
* old_context
= efb
->type_context();
13398 efb
->set_type_context(Type::lookup_integer_type("int"));
13400 this->start_
->export_expression(efb
);
13401 if (this->end_
== NULL
)
13402 go_assert(this->cap_
== NULL
);
13405 efb
->write_c_string(":");
13406 if (!this->end_
->is_nil_expression())
13407 this->end_
->export_expression(efb
);
13408 if (this->cap_
!= NULL
)
13410 efb
->write_c_string(":");
13411 this->cap_
->export_expression(efb
);
13415 efb
->set_type_context(old_context
);
13417 efb
->write_c_string("]");
13420 // Dump ast representation for an array index expression.
13423 Array_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
13426 Index_expression::dump_index_expression(ast_dump_context
, this->array_
,
13427 this->start_
, this->end_
, this->cap_
);
13430 // Make an array index expression. END and CAP may be NULL.
13433 Expression::make_array_index(Expression
* array
, Expression
* start
,
13434 Expression
* end
, Expression
* cap
,
13437 return new Array_index_expression(array
, start
, end
, cap
, location
);
13440 // Class String_index_expression.
13442 // String index traversal.
13445 String_index_expression::do_traverse(Traverse
* traverse
)
13447 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
13448 return TRAVERSE_EXIT
;
13449 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
13450 return TRAVERSE_EXIT
;
13451 if (this->end_
!= NULL
)
13453 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
13454 return TRAVERSE_EXIT
;
13456 return TRAVERSE_CONTINUE
;
13460 String_index_expression::do_flatten(Gogo
*, Named_object
*,
13461 Statement_inserter
* inserter
)
13463 if (this->is_flattened_
)
13465 this->is_flattened_
= true;
13467 Location loc
= this->location();
13469 if (this->is_error_expression())
13470 return Expression::make_error(loc
);
13472 Expression
* string
= this->string_
;
13473 Expression
* start
= this->start_
;
13474 Expression
* end
= this->end_
;
13475 if (string
->is_error_expression()
13476 || string
->type()->is_error_type()
13477 || start
->is_error_expression()
13478 || start
->type()->is_error_type()
13480 && (end
->is_error_expression() || end
->type()->is_error_type())))
13482 go_assert(saw_errors());
13483 return Expression::make_error(loc
);
13486 Temporary_statement
* temp
;
13487 if (!string
->is_variable())
13489 temp
= Statement::make_temporary(NULL
, string
, loc
);
13490 inserter
->insert(temp
);
13491 this->string_
= Expression::make_temporary_reference(temp
, loc
);
13492 string
= this->string_
;
13494 if (!start
->is_variable())
13496 temp
= Statement::make_temporary(NULL
, start
, loc
);
13497 inserter
->insert(temp
);
13498 this->start_
= Expression::make_temporary_reference(temp
, loc
);
13499 start
= this->start_
;
13502 && !end
->is_nil_expression()
13503 && !end
->is_variable())
13505 temp
= Statement::make_temporary(NULL
, end
, loc
);
13506 inserter
->insert(temp
);
13507 this->end_
= Expression::make_temporary_reference(temp
, loc
);
13511 Expression
* len
= Expression::make_string_info(string
->copy(),
13512 STRING_INFO_LENGTH
, loc
);
13513 temp
= Statement::make_temporary(NULL
, len
, loc
);
13514 inserter
->insert(temp
);
13515 len
= Expression::make_temporary_reference(temp
, loc
);
13517 // The order of bounds checks here matches the order used by the gc
13518 // compiler, as tested by issue30116[u].go.
13520 if (end
!= NULL
&& !end
->is_nil_expression())
13522 Expression::check_bounds(end
, OPERATOR_LE
, len
,
13523 Runtime::PANIC_SLICE_ALEN
,
13524 Runtime::PANIC_SLICE_ALEN_U
,
13525 Runtime::PANIC_EXTEND_SLICE_ALEN
,
13526 Runtime::PANIC_EXTEND_SLICE_ALEN_U
,
13528 Expression::check_bounds(start
, OPERATOR_LE
, end
,
13529 Runtime::PANIC_SLICE_B
,
13530 Runtime::PANIC_SLICE_B_U
,
13531 Runtime::PANIC_EXTEND_SLICE_B
,
13532 Runtime::PANIC_EXTEND_SLICE_B_U
,
13535 else if (end
!= NULL
)
13536 Expression::check_bounds(start
, OPERATOR_LE
, len
,
13537 Runtime::PANIC_SLICE_B
,
13538 Runtime::PANIC_SLICE_B_U
,
13539 Runtime::PANIC_EXTEND_SLICE_B
,
13540 Runtime::PANIC_EXTEND_SLICE_B_U
,
13543 Expression::check_bounds(start
, OPERATOR_LT
, len
,
13544 Runtime::PANIC_INDEX
,
13545 Runtime::PANIC_INDEX_U
,
13546 Runtime::PANIC_EXTEND_INDEX
,
13547 Runtime::PANIC_EXTEND_INDEX_U
,
13553 // Return the type of a string index.
13556 String_index_expression::do_type()
13558 if (this->end_
== NULL
)
13559 return Type::lookup_integer_type("byte");
13561 return this->string_
->type();
13564 // Determine the type of a string index.
13567 String_index_expression::do_determine_type(const Type_context
*)
13569 this->string_
->determine_type_no_context();
13571 Type_context
index_context(Type::lookup_integer_type("int"), false);
13572 this->start_
->determine_type(&index_context
);
13573 if (this->end_
!= NULL
)
13574 this->end_
->determine_type(&index_context
);
13577 // Check types of a string index.
13580 String_index_expression::do_check_types(Gogo
*)
13582 Numeric_constant nc
;
13584 if (this->start_
->type()->integer_type() == NULL
13585 && !this->start_
->type()->is_error()
13586 && (!this->start_
->type()->is_abstract()
13587 || !this->start_
->numeric_constant_value(&nc
)
13588 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
13589 this->report_error(_("index must be integer"));
13590 if (this->end_
!= NULL
13591 && this->end_
->type()->integer_type() == NULL
13592 && !this->end_
->type()->is_error()
13593 && !this->end_
->is_nil_expression()
13594 && !this->end_
->is_error_expression()
13595 && (!this->end_
->type()->is_abstract()
13596 || !this->end_
->numeric_constant_value(&nc
)
13597 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
13598 this->report_error(_("slice end must be integer"));
13601 bool sval_valid
= this->string_
->string_constant_value(&sval
);
13603 Numeric_constant inc
;
13605 bool ival_valid
= false;
13606 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
13609 if (mpz_sgn(ival
) < 0
13611 && (this->end_
== NULL
13612 ? mpz_cmp_ui(ival
, sval
.length()) >= 0
13613 : mpz_cmp_ui(ival
, sval
.length()) > 0)))
13615 go_error_at(this->start_
->location(), "string index out of bounds");
13616 this->set_is_error();
13619 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
13621 Numeric_constant enc
;
13623 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
13625 if (mpz_sgn(eval
) < 0
13626 || (sval_valid
&& mpz_cmp_ui(eval
, sval
.length()) > 0))
13628 go_error_at(this->end_
->location(), "string index out of bounds");
13629 this->set_is_error();
13631 else if (ival_valid
&& mpz_cmp(ival
, eval
) > 0)
13632 this->report_error(_("inverted slice range"));
13640 // Get the backend representation for a string index.
13643 String_index_expression::do_get_backend(Translate_context
* context
)
13645 Location loc
= this->location();
13646 Gogo
* gogo
= context
->gogo();
13648 Type
* int_type
= Type::lookup_integer_type("int");
13650 // It is possible that an error occurred earlier because the start index
13651 // cannot be represented as an integer type. In this case, we shouldn't
13652 // try casting the starting index into an integer since
13653 // Type_conversion_expression will fail to get the backend representation.
13655 if (this->start_
->type()->integer_type() == NULL
13656 && !Type::are_convertible(int_type
, this->start_
->type(), NULL
))
13658 go_assert(saw_errors());
13659 return context
->backend()->error_expression();
13662 go_assert(this->string_
->is_variable());
13663 go_assert(this->start_
->is_variable());
13665 Expression
* start
= Expression::make_cast(int_type
, this->start_
, loc
);
13666 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
13668 Expression
* length
=
13669 Expression::make_string_info(this->string_
, STRING_INFO_LENGTH
, loc
);
13670 Expression
* bytes
=
13671 Expression::make_string_info(this->string_
, STRING_INFO_DATA
, loc
);
13673 Bexpression
* bstart
= start
->get_backend(context
);
13674 Bexpression
* ptr
= bytes
->get_backend(context
);
13676 if (this->end_
== NULL
)
13678 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, bstart
, loc
);
13679 Btype
* ubtype
= Type::lookup_integer_type("uint8")->get_backend(gogo
);
13680 return gogo
->backend()->indirect_expression(ubtype
, ptr
, false, loc
);
13683 Expression
* end
= NULL
;
13684 if (this->end_
->is_nil_expression())
13688 go_assert(this->end_
->is_variable());
13689 end
= Expression::make_cast(int_type
, this->end_
, loc
);
13693 Bexpression
* bend
= end
->get_backend(context
);
13694 Bexpression
* new_length
=
13695 gogo
->backend()->binary_expression(OPERATOR_MINUS
, bend
, bstart
, loc
);
13697 // If the new length is zero, don't change pointer. Otherwise we can
13698 // get a pointer to the next object in memory, keeping it live
13699 // unnecessarily. When the length is zero, the actual pointer
13700 // value doesn't matter.
13701 Btype
* int_btype
= int_type
->get_backend(gogo
);
13702 Bexpression
* zero
=
13703 Expression::make_integer_ul(0, int_type
, loc
)->get_backend(context
);
13704 Bexpression
* cond
=
13705 gogo
->backend()->binary_expression(OPERATOR_EQEQ
, new_length
, zero
,
13707 Bexpression
* offset
=
13708 gogo
->backend()->conditional_expression(bfn
, int_btype
, cond
, zero
,
13711 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, offset
, loc
);
13713 Btype
* str_btype
= this->type()->get_backend(gogo
);
13714 std::vector
<Bexpression
*> init
;
13715 init
.push_back(ptr
);
13716 init
.push_back(new_length
);
13717 return gogo
->backend()->constructor_expression(str_btype
, init
, loc
);
13720 // Export a string index expression.
13723 String_index_expression::do_export(Export_function_body
* efb
) const
13725 efb
->write_c_string("(");
13726 this->string_
->export_expression(efb
);
13727 efb
->write_c_string(")[");
13729 Type
* old_context
= efb
->type_context();
13730 efb
->set_type_context(Type::lookup_integer_type("int"));
13732 this->start_
->export_expression(efb
);
13733 if (this->end_
!= NULL
)
13735 efb
->write_c_string(":");
13736 if (!this->end_
->is_nil_expression())
13737 this->end_
->export_expression(efb
);
13740 efb
->set_type_context(old_context
);
13742 efb
->write_c_string("]");
13745 // Dump ast representation for a string index expression.
13748 String_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
13751 Index_expression::dump_index_expression(ast_dump_context
, this->string_
,
13752 this->start_
, this->end_
, NULL
);
13755 // Make a string index expression. END may be NULL.
13758 Expression::make_string_index(Expression
* string
, Expression
* start
,
13759 Expression
* end
, Location location
)
13761 return new String_index_expression(string
, start
, end
, location
);
13764 // Class Map_index.
13766 // Get the type of the map.
13769 Map_index_expression::get_map_type() const
13771 Map_type
* mt
= this->map_
->type()->map_type();
13773 go_assert(saw_errors());
13777 // Map index traversal.
13780 Map_index_expression::do_traverse(Traverse
* traverse
)
13782 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
13783 return TRAVERSE_EXIT
;
13784 return Expression::traverse(&this->index_
, traverse
);
13787 // We need to pass in a pointer to the key, so flatten the index into a
13788 // temporary variable if it isn't already. The value pointer will be
13789 // dereferenced and checked for nil, so flatten into a temporary to avoid
13793 Map_index_expression::do_flatten(Gogo
* gogo
, Named_object
*,
13794 Statement_inserter
* inserter
)
13796 Location loc
= this->location();
13797 Map_type
* mt
= this->get_map_type();
13798 if (this->index()->is_error_expression()
13799 || this->index()->type()->is_error_type()
13800 || mt
->is_error_type())
13802 go_assert(saw_errors());
13803 return Expression::make_error(loc
);
13806 // Avoid copy for string([]byte) conversions used in map keys.
13807 // mapaccess doesn't keep the reference, so this is safe.
13808 Type_conversion_expression
* ce
= this->index_
->conversion_expression();
13809 if (ce
!= NULL
&& ce
->type()->is_string_type()
13810 && ce
->expr()->type()->is_slice_type())
13811 ce
->set_no_copy(true);
13813 if (!Type::are_identical(mt
->key_type(), this->index_
->type(),
13814 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
13817 if (this->index_
->type()->interface_type() != NULL
13818 && !this->index_
->is_variable())
13820 Temporary_statement
* temp
=
13821 Statement::make_temporary(NULL
, this->index_
, loc
);
13822 inserter
->insert(temp
);
13823 this->index_
= Expression::make_temporary_reference(temp
, loc
);
13825 this->index_
= Expression::convert_for_assignment(gogo
, mt
->key_type(),
13826 this->index_
, loc
);
13829 if (!this->index_
->is_variable())
13831 Temporary_statement
* temp
= Statement::make_temporary(NULL
, this->index_
,
13833 inserter
->insert(temp
);
13834 this->index_
= Expression::make_temporary_reference(temp
, loc
);
13837 if (this->value_pointer_
== NULL
)
13838 this->get_value_pointer(gogo
);
13839 if (this->value_pointer_
->is_error_expression()
13840 || this->value_pointer_
->type()->is_error_type())
13841 return Expression::make_error(loc
);
13842 if (!this->value_pointer_
->is_variable())
13844 Temporary_statement
* temp
=
13845 Statement::make_temporary(NULL
, this->value_pointer_
, loc
);
13846 inserter
->insert(temp
);
13847 this->value_pointer_
= Expression::make_temporary_reference(temp
, loc
);
13853 // Return the type of a map index.
13856 Map_index_expression::do_type()
13858 Map_type
* mt
= this->get_map_type();
13860 return Type::make_error_type();
13861 return mt
->val_type();
13864 // Fix the type of a map index.
13867 Map_index_expression::do_determine_type(const Type_context
*)
13869 this->map_
->determine_type_no_context();
13870 Map_type
* mt
= this->get_map_type();
13871 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
13872 Type_context
subcontext(key_type
, false);
13873 this->index_
->determine_type(&subcontext
);
13876 // Check types of a map index.
13879 Map_index_expression::do_check_types(Gogo
*)
13881 std::string reason
;
13882 Map_type
* mt
= this->get_map_type();
13885 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
13887 if (reason
.empty())
13888 this->report_error(_("incompatible type for map index"));
13891 go_error_at(this->location(), "incompatible type for map index (%s)",
13893 this->set_is_error();
13898 // Add explicit type conversions.
13901 Map_index_expression::do_add_conversions()
13903 Map_type
* mt
= this->get_map_type();
13906 Type
* lt
= mt
->key_type();
13907 Type
* rt
= this->index_
->type();
13908 if (!Type::are_identical(lt
, rt
, 0, NULL
)
13909 && lt
->interface_type() != NULL
)
13910 this->index_
= Expression::make_cast(lt
, this->index_
, this->location());
13913 // Get the backend representation for a map index.
13916 Map_index_expression::do_get_backend(Translate_context
* context
)
13918 Map_type
* type
= this->get_map_type();
13921 go_assert(saw_errors());
13922 return context
->backend()->error_expression();
13925 go_assert(this->value_pointer_
!= NULL
13926 && this->value_pointer_
->is_variable());
13928 Expression
* val
= Expression::make_dereference(this->value_pointer_
,
13929 NIL_CHECK_NOT_NEEDED
,
13931 return val
->get_backend(context
);
13934 // Get an expression for the map index. This returns an expression
13935 // that evaluates to a pointer to a value. If the key is not in the
13936 // map, the pointer will point to a zero value.
13939 Map_index_expression::get_value_pointer(Gogo
* gogo
)
13941 if (this->value_pointer_
== NULL
)
13943 Map_type
* type
= this->get_map_type();
13946 go_assert(saw_errors());
13947 return Expression::make_error(this->location());
13950 Location loc
= this->location();
13951 Expression
* map_ref
= this->map_
;
13953 Expression
* index_ptr
= Expression::make_unary(OPERATOR_AND
,
13957 Expression
* type_expr
= Expression::make_type_descriptor(type
, loc
);
13958 Expression
* zero
= type
->fat_zero_value(gogo
);
13959 Expression
* map_index
;
13962 Runtime::Function code
;
13964 switch (type
->algorithm(gogo
))
13966 case Map_type::MAP_ALG_FAST32
:
13967 case Map_type::MAP_ALG_FAST32PTR
:
13969 Type
* uint32_type
= Type::lookup_integer_type("uint32");
13970 Type
* uint32_ptr_type
= Type::make_pointer_type(uint32_type
);
13971 key
= Expression::make_unsafe_cast(uint32_ptr_type
, index_ptr
,
13973 key
= Expression::make_dereference(key
, NIL_CHECK_NOT_NEEDED
,
13975 code
= Runtime::MAPACCESS1_FAST32
;
13978 case Map_type::MAP_ALG_FAST64
:
13979 case Map_type::MAP_ALG_FAST64PTR
:
13981 Type
* uint64_type
= Type::lookup_integer_type("uint64");
13982 Type
* uint64_ptr_type
= Type::make_pointer_type(uint64_type
);
13983 key
= Expression::make_unsafe_cast(uint64_ptr_type
, index_ptr
,
13985 key
= Expression::make_dereference(key
, NIL_CHECK_NOT_NEEDED
,
13987 code
= Runtime::MAPACCESS1_FAST64
;
13990 case Map_type::MAP_ALG_FASTSTR
:
13991 key
= this->index_
;
13992 code
= Runtime::MAPACCESS1_FASTSTR
;
13996 code
= Runtime::MAPACCESS1
;
13999 map_index
= Runtime::make_call(code
, loc
, 3,
14000 type_expr
, map_ref
, key
);
14003 map_index
= Runtime::make_call(Runtime::MAPACCESS1_FAT
, loc
, 4,
14004 type_expr
, map_ref
, index_ptr
, zero
);
14006 Type
* val_type
= type
->val_type();
14007 this->value_pointer_
=
14008 Expression::make_unsafe_cast(Type::make_pointer_type(val_type
),
14009 map_index
, this->location());
14012 return this->value_pointer_
;
14015 // Export a map index expression.
14018 Map_index_expression::do_export(Export_function_body
* efb
) const
14020 efb
->write_c_string("(");
14021 this->map_
->export_expression(efb
);
14022 efb
->write_c_string(")[");
14024 Type
* old_context
= efb
->type_context();
14025 efb
->set_type_context(this->get_map_type()->key_type());
14027 this->index_
->export_expression(efb
);
14029 efb
->set_type_context(old_context
);
14031 efb
->write_c_string("]");
14034 // Dump ast representation for a map index expression
14037 Map_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
14040 Index_expression::dump_index_expression(ast_dump_context
, this->map_
,
14041 this->index_
, NULL
, NULL
);
14044 // Make a map index expression.
14046 Map_index_expression
*
14047 Expression::make_map_index(Expression
* map
, Expression
* index
,
14050 return new Map_index_expression(map
, index
, location
);
14053 // Class Field_reference_expression.
14055 // Lower a field reference expression. There is nothing to lower, but
14056 // this is where we generate the tracking information for fields with
14057 // the magic go:"track" tag.
14060 Field_reference_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
14061 Statement_inserter
* inserter
, int)
14063 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
14064 if (struct_type
== NULL
)
14066 // Error will be reported elsewhere.
14069 const Struct_field
* field
= struct_type
->field(this->field_index_
);
14072 if (!field
->has_tag())
14074 if (field
->tag().find("go:\"track\"") == std::string::npos
)
14077 // References from functions generated by the compiler don't count.
14078 if (function
!= NULL
&& function
->func_value()->is_type_specific_function())
14081 // We have found a reference to a tracked field. Build a call to
14082 // the runtime function __go_fieldtrack with a string that describes
14083 // the field. FIXME: We should only call this once per referenced
14084 // field per function, not once for each reference to the field.
14086 if (this->called_fieldtrack_
)
14088 this->called_fieldtrack_
= true;
14090 Location loc
= this->location();
14092 std::string s
= "fieldtrack \"";
14093 Named_type
* nt
= this->expr_
->type()->unalias()->named_type();
14094 if (nt
== NULL
|| nt
->named_object()->package() == NULL
)
14095 s
.append(gogo
->pkgpath());
14097 s
.append(nt
->named_object()->package()->pkgpath());
14100 s
.append(Gogo::unpack_hidden_name(nt
->name()));
14102 s
.append(Gogo::unpack_hidden_name(field
->field_name()));
14105 // We can't use a string here, because internally a string holds a
14106 // pointer to the actual bytes; when the linker garbage collects the
14107 // string, it won't garbage collect the bytes. So we use a
14110 Expression
* length_expr
= Expression::make_integer_ul(s
.length(), NULL
, loc
);
14112 Type
* byte_type
= Type::lookup_integer_type("byte");
14113 Array_type
* array_type
= Type::make_array_type(byte_type
, length_expr
);
14114 array_type
->set_is_array_incomparable();
14116 Expression_list
* bytes
= new Expression_list();
14117 for (std::string::const_iterator p
= s
.begin(); p
!= s
.end(); p
++)
14119 unsigned char c
= static_cast<unsigned char>(*p
);
14120 bytes
->push_back(Expression::make_integer_ul(c
, NULL
, loc
));
14123 Expression
* e
= Expression::make_composite_literal(array_type
, 0, false,
14124 bytes
, false, loc
);
14126 Variable
* var
= new Variable(array_type
, e
, true, false, false, loc
);
14130 snprintf(buf
, sizeof buf
, "fieldtrack.%d", count
);
14133 Named_object
* no
= gogo
->add_variable(buf
, var
);
14134 e
= Expression::make_var_reference(no
, loc
);
14135 e
= Expression::make_unary(OPERATOR_AND
, e
, loc
);
14137 Expression
* call
= Runtime::make_call(Runtime::FIELDTRACK
, loc
, 1, e
);
14138 gogo
->lower_expression(function
, inserter
, &call
);
14139 inserter
->insert(Statement::make_statement(call
, false));
14141 // Put this function, and the global variable we just created, into
14142 // unique sections. This will permit the linker to garbage collect
14143 // them if they are not referenced. The effect is that the only
14144 // strings, indicating field references, that will wind up in the
14145 // executable will be those for functions that are actually needed.
14146 if (function
!= NULL
)
14147 function
->func_value()->set_in_unique_section();
14148 var
->set_in_unique_section();
14153 // Return the type of a field reference.
14156 Field_reference_expression::do_type()
14158 Type
* type
= this->expr_
->type();
14159 if (type
->is_error())
14161 Struct_type
* struct_type
= type
->struct_type();
14162 go_assert(struct_type
!= NULL
);
14163 return struct_type
->field(this->field_index_
)->type();
14166 // Check the types for a field reference.
14169 Field_reference_expression::do_check_types(Gogo
*)
14171 Type
* type
= this->expr_
->type();
14172 if (type
->is_error())
14174 Struct_type
* struct_type
= type
->struct_type();
14175 go_assert(struct_type
!= NULL
);
14176 go_assert(struct_type
->field(this->field_index_
) != NULL
);
14179 // Get the backend representation for a field reference.
14182 Field_reference_expression::do_get_backend(Translate_context
* context
)
14184 Bexpression
* bstruct
= this->expr_
->get_backend(context
);
14185 return context
->gogo()->backend()->struct_field_expression(bstruct
,
14186 this->field_index_
,
14190 // Dump ast representation for a field reference expression.
14193 Field_reference_expression::do_dump_expression(
14194 Ast_dump_context
* ast_dump_context
) const
14196 this->expr_
->dump_expression(ast_dump_context
);
14197 ast_dump_context
->ostream() << "." << this->field_index_
;
14200 // Make a reference to a qualified identifier in an expression.
14202 Field_reference_expression
*
14203 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
14206 return new Field_reference_expression(expr
, field_index
, location
);
14209 // Class Interface_field_reference_expression.
14211 // Return an expression for the pointer to the function to call.
14214 Interface_field_reference_expression::get_function()
14216 Expression
* ref
= this->expr_
;
14217 Location loc
= this->location();
14218 if (ref
->type()->points_to() != NULL
)
14219 ref
= Expression::make_dereference(ref
, NIL_CHECK_DEFAULT
, loc
);
14221 Expression
* mtable
=
14222 Expression::make_interface_info(ref
, INTERFACE_INFO_METHODS
, loc
);
14223 Struct_type
* mtable_type
= mtable
->type()->points_to()->struct_type();
14225 std::string name
= Gogo::unpack_hidden_name(this->name_
);
14226 unsigned int index
;
14227 const Struct_field
* field
= mtable_type
->find_local_field(name
, &index
);
14228 go_assert(field
!= NULL
);
14230 mtable
= Expression::make_dereference(mtable
, NIL_CHECK_NOT_NEEDED
, loc
);
14231 return Expression::make_field_reference(mtable
, index
, loc
);
14234 // Return an expression for the first argument to pass to the interface
14238 Interface_field_reference_expression::get_underlying_object()
14240 Expression
* expr
= this->expr_
;
14241 if (expr
->type()->points_to() != NULL
)
14242 expr
= Expression::make_dereference(expr
, NIL_CHECK_DEFAULT
,
14244 return Expression::make_interface_info(expr
, INTERFACE_INFO_OBJECT
,
14251 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
14253 return Expression::traverse(&this->expr_
, traverse
);
14256 // Lower the expression. If this expression is not called, we need to
14257 // evaluate the expression twice when converting to the backend
14258 // interface. So introduce a temporary variable if necessary.
14261 Interface_field_reference_expression::do_flatten(Gogo
*, Named_object
*,
14262 Statement_inserter
* inserter
)
14264 if (this->expr_
->is_error_expression()
14265 || this->expr_
->type()->is_error_type())
14267 go_assert(saw_errors());
14268 return Expression::make_error(this->location());
14271 if (!this->expr_
->is_variable())
14273 Temporary_statement
* temp
=
14274 Statement::make_temporary(NULL
, this->expr_
, this->location());
14275 inserter
->insert(temp
);
14276 this->expr_
= Expression::make_temporary_reference(temp
, this->location());
14281 // Return the type of an interface field reference.
14284 Interface_field_reference_expression::do_type()
14286 Type
* expr_type
= this->expr_
->type();
14288 Type
* points_to
= expr_type
->points_to();
14289 if (points_to
!= NULL
)
14290 expr_type
= points_to
;
14292 Interface_type
* interface_type
= expr_type
->interface_type();
14293 if (interface_type
== NULL
)
14294 return Type::make_error_type();
14296 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
14297 if (method
== NULL
)
14298 return Type::make_error_type();
14300 return method
->type();
14303 // Determine types.
14306 Interface_field_reference_expression::do_determine_type(const Type_context
*)
14308 this->expr_
->determine_type_no_context();
14311 // Check the types for an interface field reference.
14314 Interface_field_reference_expression::do_check_types(Gogo
*)
14316 Type
* type
= this->expr_
->type();
14318 Type
* points_to
= type
->points_to();
14319 if (points_to
!= NULL
)
14322 Interface_type
* interface_type
= type
->interface_type();
14323 if (interface_type
== NULL
)
14325 if (!type
->is_error_type())
14326 this->report_error(_("expected interface or pointer to interface"));
14330 const Typed_identifier
* method
=
14331 interface_type
->find_method(this->name_
);
14332 if (method
== NULL
)
14334 go_error_at(this->location(), "method %qs not in interface",
14335 Gogo::message_name(this->name_
).c_str());
14336 this->set_is_error();
14341 // If an interface field reference is not simply called, then it is
14342 // represented as a closure. The closure will hold a single variable,
14343 // the value of the interface on which the method should be called.
14344 // The function will be a simple thunk that pulls the value from the
14345 // closure and calls the method with the remaining arguments.
14347 // Because method values are not common, we don't build all thunks for
14348 // all possible interface methods, but instead only build them as we
14349 // need them. In particular, we even build them on demand for
14350 // interface methods defined in other packages.
14352 Interface_field_reference_expression::Interface_method_thunks
14353 Interface_field_reference_expression::interface_method_thunks
;
14355 // Find or create the thunk to call method NAME on TYPE.
14358 Interface_field_reference_expression::create_thunk(Gogo
* gogo
,
14359 Interface_type
* type
,
14360 const std::string
& name
)
14362 std::pair
<Interface_type
*, Method_thunks
*> val(type
, NULL
);
14363 std::pair
<Interface_method_thunks::iterator
, bool> ins
=
14364 Interface_field_reference_expression::interface_method_thunks
.insert(val
);
14367 // This is the first time we have seen this interface.
14368 ins
.first
->second
= new Method_thunks();
14371 for (Method_thunks::const_iterator p
= ins
.first
->second
->begin();
14372 p
!= ins
.first
->second
->end();
14374 if (p
->first
== name
)
14377 Location loc
= type
->location();
14379 const Typed_identifier
* method_id
= type
->find_method(name
);
14380 if (method_id
== NULL
)
14381 return Named_object::make_erroneous_name(gogo
->thunk_name());
14383 Function_type
* orig_fntype
= method_id
->type()->function_type();
14384 if (orig_fntype
== NULL
)
14385 return Named_object::make_erroneous_name(gogo
->thunk_name());
14387 Struct_field_list
* sfl
= new Struct_field_list();
14388 // The type here is wrong--it should be the C function type. But it
14389 // doesn't really matter.
14390 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
14391 sfl
->push_back(Struct_field(Typed_identifier("fn", vt
, loc
)));
14392 sfl
->push_back(Struct_field(Typed_identifier("val", type
, loc
)));
14393 Struct_type
* st
= Type::make_struct_type(sfl
, loc
);
14394 st
->set_is_struct_incomparable();
14395 Type
* closure_type
= Type::make_pointer_type(st
);
14397 Function_type
* new_fntype
= orig_fntype
->copy_with_names();
14399 std::string thunk_name
= gogo
->thunk_name();
14400 Named_object
* new_no
= gogo
->start_function(thunk_name
, new_fntype
,
14403 Variable
* cvar
= new Variable(closure_type
, NULL
, false, false, false, loc
);
14404 cvar
->set_is_used();
14405 cvar
->set_is_closure();
14406 Named_object
* cp
= Named_object::make_variable("$closure" + thunk_name
,
14408 new_no
->func_value()->set_closure_var(cp
);
14410 gogo
->start_block(loc
);
14412 // Field 0 of the closure is the function code pointer, field 1 is
14413 // the value on which to invoke the method.
14414 Expression
* arg
= Expression::make_var_reference(cp
, loc
);
14415 arg
= Expression::make_dereference(arg
, NIL_CHECK_NOT_NEEDED
, loc
);
14416 arg
= Expression::make_field_reference(arg
, 1, loc
);
14418 Expression
*ifre
= Expression::make_interface_field_reference(arg
, name
,
14421 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
14422 Expression_list
* args
;
14423 if (orig_params
== NULL
|| orig_params
->empty())
14427 const Typed_identifier_list
* new_params
= new_fntype
->parameters();
14428 args
= new Expression_list();
14429 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
14430 p
!= new_params
->end();
14433 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
14434 go_assert(p_no
!= NULL
14435 && p_no
->is_variable()
14436 && p_no
->var_value()->is_parameter());
14437 args
->push_back(Expression::make_var_reference(p_no
, loc
));
14441 Call_expression
* call
= Expression::make_call(ifre
, args
,
14442 orig_fntype
->is_varargs(),
14444 call
->set_varargs_are_lowered();
14446 Statement
* s
= Statement::make_return_from_call(call
, loc
);
14447 gogo
->add_statement(s
);
14448 Block
* b
= gogo
->finish_block(loc
);
14449 gogo
->add_block(b
, loc
);
14450 gogo
->lower_block(new_no
, b
);
14451 gogo
->flatten_block(new_no
, b
);
14452 gogo
->finish_function(loc
);
14454 ins
.first
->second
->push_back(std::make_pair(name
, new_no
));
14458 // Get the backend representation for a method value.
14461 Interface_field_reference_expression::do_get_backend(Translate_context
* context
)
14463 Interface_type
* type
= this->expr_
->type()->interface_type();
14466 go_assert(saw_errors());
14467 return context
->backend()->error_expression();
14470 Named_object
* thunk
=
14471 Interface_field_reference_expression::create_thunk(context
->gogo(),
14472 type
, this->name_
);
14473 if (thunk
->is_erroneous())
14475 go_assert(saw_errors());
14476 return context
->backend()->error_expression();
14479 // FIXME: We should lower this earlier, but we can't it lower it in
14480 // the lowering pass because at that point we don't know whether we
14481 // need to create the thunk or not. If the expression is called, we
14482 // don't need the thunk.
14484 Location loc
= this->location();
14486 Struct_field_list
* fields
= new Struct_field_list();
14487 fields
->push_back(Struct_field(Typed_identifier("fn",
14488 thunk
->func_value()->type(),
14490 fields
->push_back(Struct_field(Typed_identifier("val",
14491 this->expr_
->type(),
14493 Struct_type
* st
= Type::make_struct_type(fields
, loc
);
14494 st
->set_is_struct_incomparable();
14496 Expression_list
* vals
= new Expression_list();
14497 vals
->push_back(Expression::make_func_code_reference(thunk
, loc
));
14498 vals
->push_back(this->expr_
);
14500 Expression
* expr
= Expression::make_struct_composite_literal(st
, vals
, loc
);
14501 Bexpression
* bclosure
=
14502 Expression::make_heap_expression(expr
, loc
)->get_backend(context
);
14504 Gogo
* gogo
= context
->gogo();
14505 Btype
* btype
= this->type()->get_backend(gogo
);
14506 bclosure
= gogo
->backend()->convert_expression(btype
, bclosure
, loc
);
14508 Expression
* nil_check
=
14509 Expression::make_binary(OPERATOR_EQEQ
, this->expr_
,
14510 Expression::make_nil(loc
), loc
);
14511 Bexpression
* bnil_check
= nil_check
->get_backend(context
);
14513 Expression
* crash
= Runtime::make_call(Runtime::PANIC_MEM
, loc
, 0);
14514 Bexpression
* bcrash
= crash
->get_backend(context
);
14516 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
14517 Bexpression
* bcond
=
14518 gogo
->backend()->conditional_expression(bfn
, NULL
,
14519 bnil_check
, bcrash
, NULL
, loc
);
14520 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
14521 Bstatement
* cond_statement
=
14522 gogo
->backend()->expression_statement(bfunction
, bcond
);
14523 return gogo
->backend()->compound_expression(cond_statement
, bclosure
, loc
);
14526 // Dump ast representation for an interface field reference.
14529 Interface_field_reference_expression::do_dump_expression(
14530 Ast_dump_context
* ast_dump_context
) const
14532 this->expr_
->dump_expression(ast_dump_context
);
14533 ast_dump_context
->ostream() << "." << this->name_
;
14536 // Make a reference to a field in an interface.
14539 Expression::make_interface_field_reference(Expression
* expr
,
14540 const std::string
& field
,
14543 return new Interface_field_reference_expression(expr
, field
, location
);
14546 // A general selector. This is a Parser_expression for LEFT.NAME. It
14547 // is lowered after we know the type of the left hand side.
14549 class Selector_expression
: public Parser_expression
14552 Selector_expression(Expression
* left
, const std::string
& name
,
14554 : Parser_expression(EXPRESSION_SELECTOR
, location
),
14555 left_(left
), name_(name
)
14560 do_traverse(Traverse
* traverse
)
14561 { return Expression::traverse(&this->left_
, traverse
); }
14564 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
14569 return new Selector_expression(this->left_
->copy(), this->name_
,
14574 do_dump_expression(Ast_dump_context
* ast_dump_context
) const;
14578 lower_method_expression(Gogo
*);
14580 // The expression on the left hand side.
14582 // The name on the right hand side.
14586 // Lower a selector expression once we know the real type of the left
14590 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, Statement_inserter
*,
14593 Expression
* left
= this->left_
;
14594 if (left
->is_type_expression())
14595 return this->lower_method_expression(gogo
);
14596 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
14600 // Lower a method expression T.M or (*T).M. We turn this into a
14601 // function literal.
14604 Selector_expression::lower_method_expression(Gogo
* gogo
)
14606 Location location
= this->location();
14607 Type
* left_type
= this->left_
->type();
14608 Type
* type
= left_type
;
14609 const std::string
& name(this->name_
);
14612 if (type
->points_to() == NULL
)
14613 is_pointer
= false;
14617 type
= type
->points_to();
14620 Named_type
* nt
= type
->named_type();
14621 Struct_type
* st
= type
->struct_type();
14623 Method
* method
= NULL
;
14625 method
= nt
->method_function(name
, &is_ambiguous
);
14626 else if (st
!= NULL
)
14627 method
= st
->method_function(name
, &is_ambiguous
);
14628 const Typed_identifier
* imethod
= NULL
;
14629 if (method
== NULL
&& !is_pointer
)
14631 Interface_type
* it
= type
->interface_type();
14633 imethod
= it
->find_method(name
);
14636 if ((method
== NULL
&& imethod
== NULL
)
14637 || (left_type
->named_type() != NULL
&& left_type
->points_to() != NULL
))
14642 go_error_at(location
, "type %<%s%s%> has no method %<%s%>",
14643 is_pointer
? "*" : "",
14644 nt
->message_name().c_str(),
14645 Gogo::message_name(name
).c_str());
14647 go_error_at(location
, "method %<%s%s%> is ambiguous in type %<%s%>",
14648 Gogo::message_name(name
).c_str(),
14649 is_pointer
? "*" : "",
14650 nt
->message_name().c_str());
14655 go_error_at(location
, "type has no method %<%s%>",
14656 Gogo::message_name(name
).c_str());
14658 go_error_at(location
, "method %<%s%> is ambiguous",
14659 Gogo::message_name(name
).c_str());
14661 return Expression::make_error(location
);
14664 if (method
!= NULL
&& !is_pointer
&& !method
->is_value_method())
14666 go_error_at(location
, "method requires pointer (use %<(*%s).%s%>)",
14667 nt
->message_name().c_str(),
14668 Gogo::message_name(name
).c_str());
14669 return Expression::make_error(location
);
14672 // Build a new function type in which the receiver becomes the first
14674 Function_type
* method_type
;
14675 if (method
!= NULL
)
14677 method_type
= method
->type();
14678 go_assert(method_type
->is_method());
14682 method_type
= imethod
->type()->function_type();
14683 go_assert(method_type
!= NULL
&& !method_type
->is_method());
14686 const char* const receiver_name
= "$this";
14687 Typed_identifier_list
* parameters
= new Typed_identifier_list();
14688 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
14691 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
14692 if (method_parameters
!= NULL
)
14695 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
14696 p
!= method_parameters
->end();
14699 if (!p
->name().empty())
14700 parameters
->push_back(*p
);
14704 snprintf(buf
, sizeof buf
, "$param%d", i
);
14705 parameters
->push_back(Typed_identifier(buf
, p
->type(),
14711 const Typed_identifier_list
* method_results
= method_type
->results();
14712 Typed_identifier_list
* results
;
14713 if (method_results
== NULL
)
14717 results
= new Typed_identifier_list();
14718 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
14719 p
!= method_results
->end();
14721 results
->push_back(*p
);
14724 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
14726 if (method_type
->is_varargs())
14727 fntype
->set_is_varargs();
14729 // We generate methods which always takes a pointer to the receiver
14730 // as their first argument. If this is for a pointer type, we can
14731 // simply reuse the existing function. We use an internal hack to
14732 // get the right type.
14733 // FIXME: This optimization is disabled because it doesn't yet work
14734 // with function descriptors when the method expression is not
14735 // directly called.
14736 if (method
!= NULL
&& is_pointer
&& false)
14738 Named_object
* mno
= (method
->needs_stub_method()
14739 ? method
->stub_object()
14740 : method
->named_object());
14741 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
14742 f
= Expression::make_cast(fntype
, f
, location
);
14743 Type_conversion_expression
* tce
=
14744 static_cast<Type_conversion_expression
*>(f
);
14745 tce
->set_may_convert_function_types();
14749 Named_object
* no
= gogo
->start_function(gogo
->thunk_name(), fntype
, false,
14752 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
14753 go_assert(vno
!= NULL
);
14754 Expression
* ve
= Expression::make_var_reference(vno
, location
);
14756 if (method
!= NULL
)
14757 bm
= Type::bind_field_or_method(gogo
, type
, ve
, name
, location
);
14759 bm
= Expression::make_interface_field_reference(ve
, name
, location
);
14761 // Even though we found the method above, if it has an error type we
14762 // may see an error here.
14763 if (bm
->is_error_expression())
14765 gogo
->finish_function(location
);
14769 Expression_list
* args
;
14770 if (parameters
->size() <= 1)
14774 args
= new Expression_list();
14775 Typed_identifier_list::const_iterator p
= parameters
->begin();
14777 for (; p
!= parameters
->end(); ++p
)
14779 vno
= gogo
->lookup(p
->name(), NULL
);
14780 go_assert(vno
!= NULL
);
14781 args
->push_back(Expression::make_var_reference(vno
, location
));
14785 gogo
->start_block(location
);
14787 Call_expression
* call
= Expression::make_call(bm
, args
,
14788 method_type
->is_varargs(),
14791 Statement
* s
= Statement::make_return_from_call(call
, location
);
14792 gogo
->add_statement(s
);
14794 Block
* b
= gogo
->finish_block(location
);
14796 gogo
->add_block(b
, location
);
14798 // Lower the call in case there are multiple results.
14799 gogo
->lower_block(no
, b
);
14800 gogo
->flatten_block(no
, b
);
14802 gogo
->finish_function(location
);
14804 return Expression::make_func_reference(no
, NULL
, location
);
14807 // Dump the ast for a selector expression.
14810 Selector_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
14813 ast_dump_context
->dump_expression(this->left_
);
14814 ast_dump_context
->ostream() << ".";
14815 ast_dump_context
->ostream() << this->name_
;
14818 // Make a selector expression.
14821 Expression::make_selector(Expression
* left
, const std::string
& name
,
14824 return new Selector_expression(left
, name
, location
);
14827 // Class Allocation_expression.
14830 Allocation_expression::do_traverse(Traverse
* traverse
)
14832 return Type::traverse(this->type_
, traverse
);
14836 Allocation_expression::do_type()
14838 return Type::make_pointer_type(this->type_
);
14842 Allocation_expression::do_check_types(Gogo
*)
14844 if (!this->type_
->in_heap())
14845 go_error_at(this->location(), "cannot heap allocate go:notinheap type");
14848 // Make a copy of an allocation expression.
14851 Allocation_expression::do_copy()
14853 Allocation_expression
* alloc
=
14854 new Allocation_expression(this->type_
->copy_expressions(),
14856 if (this->allocate_on_stack_
)
14857 alloc
->set_allocate_on_stack();
14858 if (this->no_zero_
)
14859 alloc
->set_no_zero();
14863 // Return the backend representation for an allocation expression.
14866 Allocation_expression::do_get_backend(Translate_context
* context
)
14868 Gogo
* gogo
= context
->gogo();
14869 Location loc
= this->location();
14870 Btype
* btype
= this->type_
->get_backend(gogo
);
14872 if (this->allocate_on_stack_
)
14875 bool ok
= this->type_
->backend_type_size(gogo
, &size
);
14878 go_assert(saw_errors());
14879 return gogo
->backend()->error_expression();
14882 Named_object
* fn
= context
->function();
14883 go_assert(fn
!= NULL
);
14884 Bfunction
* fndecl
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
14885 Bexpression
* init
= (this->no_zero_
14887 : gogo
->backend()->zero_expression(btype
));
14889 gogo
->backend()->temporary_variable(fndecl
, context
->bblock(), btype
,
14890 init
, true, loc
, &decl
);
14891 Bexpression
* ret
= gogo
->backend()->var_expression(temp
, loc
);
14892 ret
= gogo
->backend()->address_expression(ret
, loc
);
14893 ret
= gogo
->backend()->compound_expression(decl
, ret
, loc
);
14897 Bexpression
* space
=
14898 gogo
->allocate_memory(this->type_
, loc
)->get_backend(context
);
14899 Btype
* pbtype
= gogo
->backend()->pointer_type(btype
);
14900 return gogo
->backend()->convert_expression(pbtype
, space
, loc
);
14903 // Dump ast representation for an allocation expression.
14906 Allocation_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
14909 ast_dump_context
->ostream() << "new(";
14910 ast_dump_context
->dump_type(this->type_
);
14911 ast_dump_context
->ostream() << ")";
14914 // Make an allocation expression.
14917 Expression::make_allocation(Type
* type
, Location location
)
14919 return new Allocation_expression(type
, location
);
14922 // Class Ordered_value_list.
14925 Ordered_value_list::traverse_vals(Traverse
* traverse
)
14927 if (this->vals_
!= NULL
)
14929 if (this->traverse_order_
== NULL
)
14931 if (this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
14932 return TRAVERSE_EXIT
;
14936 for (std::vector
<unsigned long>::const_iterator p
=
14937 this->traverse_order_
->begin();
14938 p
!= this->traverse_order_
->end();
14941 if (Expression::traverse(&this->vals_
->at(*p
), traverse
)
14943 return TRAVERSE_EXIT
;
14947 return TRAVERSE_CONTINUE
;
14950 // Class Struct_construction_expression.
14955 Struct_construction_expression::do_traverse(Traverse
* traverse
)
14957 if (this->traverse_vals(traverse
) == TRAVERSE_EXIT
)
14958 return TRAVERSE_EXIT
;
14959 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
14960 return TRAVERSE_EXIT
;
14961 return TRAVERSE_CONTINUE
;
14964 // Return whether this is a constant initializer.
14967 Struct_construction_expression::is_constant_struct() const
14969 if (this->vals() == NULL
)
14971 for (Expression_list::const_iterator pv
= this->vals()->begin();
14972 pv
!= this->vals()->end();
14976 && !(*pv
)->is_constant()
14977 && (!(*pv
)->is_composite_literal()
14978 || (*pv
)->is_nonconstant_composite_literal()))
14982 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
14983 for (Struct_field_list::const_iterator pf
= fields
->begin();
14984 pf
!= fields
->end();
14987 // There are no constant constructors for interfaces.
14988 if (pf
->type()->interface_type() != NULL
)
14995 // Return whether this is a zero value.
14998 Struct_construction_expression::do_is_zero_value() const
15000 if (this->vals() == NULL
)
15002 for (Expression_list::const_iterator pv
= this->vals()->begin();
15003 pv
!= this->vals()->end();
15005 if (*pv
!= NULL
&& !(*pv
)->is_zero_value())
15008 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
15009 for (Struct_field_list::const_iterator pf
= fields
->begin();
15010 pf
!= fields
->end();
15013 // Interface conversion may cause a zero value being converted
15014 // to a non-zero value, like interface{}(0). Be conservative.
15015 if (pf
->type()->interface_type() != NULL
)
15022 // Return whether this struct can be used as a constant initializer.
15025 Struct_construction_expression::do_is_static_initializer() const
15027 if (this->vals() == NULL
)
15029 for (Expression_list::const_iterator pv
= this->vals()->begin();
15030 pv
!= this->vals()->end();
15033 if (*pv
!= NULL
&& !(*pv
)->is_static_initializer())
15037 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
15038 for (Struct_field_list::const_iterator pf
= fields
->begin();
15039 pf
!= fields
->end();
15042 // There are no constant constructors for interfaces.
15043 if (pf
->type()->interface_type() != NULL
)
15050 // Final type determination.
15053 Struct_construction_expression::do_determine_type(const Type_context
*)
15055 if (this->vals() == NULL
)
15057 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
15058 Expression_list::const_iterator pv
= this->vals()->begin();
15059 for (Struct_field_list::const_iterator pf
= fields
->begin();
15060 pf
!= fields
->end();
15063 if (pv
== this->vals()->end())
15067 Type_context
subcontext(pf
->type(), false);
15068 (*pv
)->determine_type(&subcontext
);
15071 // Extra values are an error we will report elsewhere; we still want
15072 // to determine the type to avoid knockon errors.
15073 for (; pv
!= this->vals()->end(); ++pv
)
15074 (*pv
)->determine_type_no_context();
15080 Struct_construction_expression::do_check_types(Gogo
*)
15082 if (this->vals() == NULL
)
15085 Struct_type
* st
= this->type_
->struct_type();
15086 if (this->vals()->size() > st
->field_count())
15088 this->report_error(_("too many expressions for struct"));
15092 const Struct_field_list
* fields
= st
->fields();
15093 Expression_list::const_iterator pv
= this->vals()->begin();
15095 for (Struct_field_list::const_iterator pf
= fields
->begin();
15096 pf
!= fields
->end();
15099 if (pv
== this->vals()->end())
15101 this->report_error(_("too few expressions for struct"));
15108 std::string reason
;
15109 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
15111 if (reason
.empty())
15112 go_error_at((*pv
)->location(),
15113 "incompatible type for field %d in struct construction",
15116 go_error_at((*pv
)->location(),
15117 ("incompatible type for field %d in "
15118 "struct construction (%s)"),
15119 i
+ 1, reason
.c_str());
15120 this->set_is_error();
15123 go_assert(pv
== this->vals()->end());
15129 Struct_construction_expression::do_copy()
15131 Struct_construction_expression
* ret
=
15132 new Struct_construction_expression(this->type_
->copy_expressions(),
15133 (this->vals() == NULL
15135 : this->vals()->copy()),
15137 if (this->traverse_order() != NULL
)
15138 ret
->set_traverse_order(this->traverse_order());
15142 // Flatten a struct construction expression. Store the values into
15143 // temporaries in case they need interface conversion.
15146 Struct_construction_expression::do_flatten(Gogo
*, Named_object
*,
15147 Statement_inserter
* inserter
)
15149 if (this->vals() == NULL
)
15152 // If this is a constant struct, we don't need temporaries.
15153 if (this->is_constant_struct() || this->is_static_initializer())
15156 Location loc
= this->location();
15157 for (Expression_list::iterator pv
= this->vals()->begin();
15158 pv
!= this->vals()->end();
15163 if ((*pv
)->is_error_expression() || (*pv
)->type()->is_error_type())
15165 go_assert(saw_errors());
15166 return Expression::make_error(loc
);
15168 if (!(*pv
)->is_variable())
15170 Temporary_statement
* temp
=
15171 Statement::make_temporary(NULL
, *pv
, loc
);
15172 inserter
->insert(temp
);
15173 *pv
= Expression::make_temporary_reference(temp
, loc
);
15180 // Make implicit type conversions explicit.
15183 Struct_construction_expression::do_add_conversions()
15185 if (this->vals() == NULL
)
15188 Location loc
= this->location();
15189 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
15190 Expression_list::iterator pv
= this->vals()->begin();
15191 for (Struct_field_list::const_iterator pf
= fields
->begin();
15192 pf
!= fields
->end();
15195 if (pv
== this->vals()->end())
15199 Type
* ft
= pf
->type();
15200 if (!Type::are_identical(ft
, (*pv
)->type(), 0, NULL
)
15201 && ft
->interface_type() != NULL
)
15202 *pv
= Expression::make_cast(ft
, *pv
, loc
);
15207 // Return the backend representation for constructing a struct.
15210 Struct_construction_expression::do_get_backend(Translate_context
* context
)
15212 Gogo
* gogo
= context
->gogo();
15214 Btype
* btype
= this->type_
->get_backend(gogo
);
15215 if (this->vals() == NULL
)
15216 return gogo
->backend()->zero_expression(btype
);
15218 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
15219 Expression_list::const_iterator pv
= this->vals()->begin();
15220 std::vector
<Bexpression
*> init
;
15221 for (Struct_field_list::const_iterator pf
= fields
->begin();
15222 pf
!= fields
->end();
15225 Btype
* fbtype
= pf
->type()->get_backend(gogo
);
15226 if (pv
== this->vals()->end())
15227 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
15228 else if (*pv
== NULL
)
15230 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
15236 Expression::convert_for_assignment(gogo
, pf
->type(),
15237 *pv
, this->location());
15238 init
.push_back(val
->get_backend(context
));
15242 if (this->type_
->struct_type()->has_padding())
15244 // Feed an extra value if there is a padding field.
15245 Btype
*fbtype
= Type::lookup_integer_type("uint8")->get_backend(gogo
);
15246 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
15248 return gogo
->backend()->constructor_expression(btype
, init
, this->location());
15251 // Export a struct construction.
15254 Struct_construction_expression::do_export(Export_function_body
* efb
) const
15256 efb
->write_c_string("$convert(");
15257 efb
->write_type(this->type_
);
15258 for (Expression_list::const_iterator pv
= this->vals()->begin();
15259 pv
!= this->vals()->end();
15262 efb
->write_c_string(", ");
15264 (*pv
)->export_expression(efb
);
15266 efb
->write_c_string(")");
15269 // Dump ast representation of a struct construction expression.
15272 Struct_construction_expression::do_dump_expression(
15273 Ast_dump_context
* ast_dump_context
) const
15275 ast_dump_context
->dump_type(this->type_
);
15276 ast_dump_context
->ostream() << "{";
15277 ast_dump_context
->dump_expression_list(this->vals());
15278 ast_dump_context
->ostream() << "}";
15281 // Make a struct composite literal. This used by the thunk code.
15284 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
15287 go_assert(type
->struct_type() != NULL
);
15288 return new Struct_construction_expression(type
, vals
, location
);
15291 // Class Array_construction_expression.
15296 Array_construction_expression::do_traverse(Traverse
* traverse
)
15298 if (this->traverse_vals(traverse
) == TRAVERSE_EXIT
)
15299 return TRAVERSE_EXIT
;
15300 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
15301 return TRAVERSE_EXIT
;
15302 return TRAVERSE_CONTINUE
;
15305 // Return whether this is a constant initializer.
15308 Array_construction_expression::is_constant_array() const
15310 if (this->vals() == NULL
)
15313 // There are no constant constructors for interfaces.
15314 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
15317 for (Expression_list::const_iterator pv
= this->vals()->begin();
15318 pv
!= this->vals()->end();
15322 && !(*pv
)->is_constant()
15323 && (!(*pv
)->is_composite_literal()
15324 || (*pv
)->is_nonconstant_composite_literal()))
15330 // Return whether this is a zero value.
15333 Array_construction_expression::do_is_zero_value() const
15335 if (this->vals() == NULL
)
15338 // Interface conversion may cause a zero value being converted
15339 // to a non-zero value, like interface{}(0). Be conservative.
15340 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
15343 for (Expression_list::const_iterator pv
= this->vals()->begin();
15344 pv
!= this->vals()->end();
15346 if (*pv
!= NULL
&& !(*pv
)->is_zero_value())
15352 // Return whether this can be used a constant initializer.
15355 Array_construction_expression::do_is_static_initializer() const
15357 if (this->vals() == NULL
)
15360 // There are no constant constructors for interfaces.
15361 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
15364 for (Expression_list::const_iterator pv
= this->vals()->begin();
15365 pv
!= this->vals()->end();
15368 if (*pv
!= NULL
&& !(*pv
)->is_static_initializer())
15374 // Final type determination.
15377 Array_construction_expression::do_determine_type(const Type_context
*)
15379 if (this->is_error_expression())
15381 go_assert(saw_errors());
15385 if (this->vals() == NULL
)
15387 Array_type
* at
= this->type_
->array_type();
15388 if (at
== NULL
|| at
->is_error() || at
->element_type()->is_error())
15390 go_assert(saw_errors());
15391 this->set_is_error();
15394 Type_context
subcontext(at
->element_type(), false);
15395 for (Expression_list::const_iterator pv
= this->vals()->begin();
15396 pv
!= this->vals()->end();
15400 (*pv
)->determine_type(&subcontext
);
15407 Array_construction_expression::do_check_types(Gogo
*)
15409 if (this->is_error_expression())
15411 go_assert(saw_errors());
15415 if (this->vals() == NULL
)
15418 Array_type
* at
= this->type_
->array_type();
15419 if (at
== NULL
|| at
->is_error() || at
->element_type()->is_error())
15421 go_assert(saw_errors());
15422 this->set_is_error();
15426 Type
* element_type
= at
->element_type();
15427 for (Expression_list::const_iterator pv
= this->vals()->begin();
15428 pv
!= this->vals()->end();
15432 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
15434 go_error_at((*pv
)->location(),
15435 "incompatible type for element %d in composite literal",
15437 this->set_is_error();
15442 // Flatten an array construction expression. Store the values into
15443 // temporaries in case they need interface conversion.
15446 Array_construction_expression::do_flatten(Gogo
*, Named_object
*,
15447 Statement_inserter
* inserter
)
15449 if (this->is_error_expression())
15451 go_assert(saw_errors());
15455 if (this->vals() == NULL
)
15458 // If this is a constant array, we don't need temporaries.
15459 if (this->is_constant_array() || this->is_static_initializer())
15462 Location loc
= this->location();
15463 for (Expression_list::iterator pv
= this->vals()->begin();
15464 pv
!= this->vals()->end();
15469 if ((*pv
)->is_error_expression() || (*pv
)->type()->is_error_type())
15471 go_assert(saw_errors());
15472 return Expression::make_error(loc
);
15474 if (!(*pv
)->is_variable())
15476 Temporary_statement
* temp
=
15477 Statement::make_temporary(NULL
, *pv
, loc
);
15478 inserter
->insert(temp
);
15479 *pv
= Expression::make_temporary_reference(temp
, loc
);
15486 // Make implicit type conversions explicit.
15489 Array_construction_expression::do_add_conversions()
15491 if (this->is_error_expression())
15493 go_assert(saw_errors());
15497 if (this->vals() == NULL
)
15500 Type
* et
= this->type_
->array_type()->element_type();
15501 if (et
->interface_type() == NULL
)
15504 Location loc
= this->location();
15505 for (Expression_list::iterator pv
= this->vals()->begin();
15506 pv
!= this->vals()->end();
15508 if (!Type::are_identical(et
, (*pv
)->type(), 0, NULL
))
15509 *pv
= Expression::make_cast(et
, *pv
, loc
);
15512 // Get a constructor expression for the array values.
15515 Array_construction_expression::get_constructor(Translate_context
* context
,
15516 Btype
* array_btype
)
15518 Type
* element_type
= this->type_
->array_type()->element_type();
15520 std::vector
<unsigned long> indexes
;
15521 std::vector
<Bexpression
*> vals
;
15522 Gogo
* gogo
= context
->gogo();
15523 if (this->vals() != NULL
)
15526 std::vector
<unsigned long>::const_iterator pi
;
15527 if (this->indexes_
!= NULL
)
15528 pi
= this->indexes_
->begin();
15529 for (Expression_list::const_iterator pv
= this->vals()->begin();
15530 pv
!= this->vals()->end();
15533 if (this->indexes_
!= NULL
)
15534 go_assert(pi
!= this->indexes_
->end());
15536 if (this->indexes_
== NULL
)
15537 indexes
.push_back(i
);
15539 indexes
.push_back(*pi
);
15542 Btype
* ebtype
= element_type
->get_backend(gogo
);
15543 Bexpression
*zv
= gogo
->backend()->zero_expression(ebtype
);
15544 vals
.push_back(zv
);
15548 Expression
* val_expr
=
15549 Expression::convert_for_assignment(gogo
, element_type
, *pv
,
15551 vals
.push_back(val_expr
->get_backend(context
));
15553 if (this->indexes_
!= NULL
)
15556 if (this->indexes_
!= NULL
)
15557 go_assert(pi
== this->indexes_
->end());
15559 return gogo
->backend()->array_constructor_expression(array_btype
, indexes
,
15560 vals
, this->location());
15563 // Export an array construction.
15566 Array_construction_expression::do_export(Export_function_body
* efb
) const
15568 efb
->write_c_string("$convert(");
15569 efb
->write_type(this->type_
);
15570 if (this->vals() != NULL
)
15572 std::vector
<unsigned long>::const_iterator pi
;
15573 if (this->indexes_
!= NULL
)
15574 pi
= this->indexes_
->begin();
15575 for (Expression_list::const_iterator pv
= this->vals()->begin();
15576 pv
!= this->vals()->end();
15579 efb
->write_c_string(", ");
15581 if (this->indexes_
!= NULL
)
15584 snprintf(buf
, sizeof buf
, "%lu", *pi
);
15585 efb
->write_c_string(buf
);
15586 efb
->write_c_string(":");
15590 (*pv
)->export_expression(efb
);
15592 if (this->indexes_
!= NULL
)
15596 efb
->write_c_string(")");
15599 // Dump ast representation of an array construction expression.
15602 Array_construction_expression::do_dump_expression(
15603 Ast_dump_context
* ast_dump_context
) const
15605 Expression
* length
= this->type_
->array_type()->length();
15607 ast_dump_context
->ostream() << "[" ;
15608 if (length
!= NULL
)
15610 ast_dump_context
->dump_expression(length
);
15612 ast_dump_context
->ostream() << "]" ;
15613 ast_dump_context
->dump_type(this->type_
);
15614 this->dump_slice_storage_expression(ast_dump_context
);
15615 ast_dump_context
->ostream() << "{" ;
15616 if (this->indexes_
== NULL
)
15617 ast_dump_context
->dump_expression_list(this->vals());
15620 Expression_list::const_iterator pv
= this->vals()->begin();
15621 for (std::vector
<unsigned long>::const_iterator pi
=
15622 this->indexes_
->begin();
15623 pi
!= this->indexes_
->end();
15626 if (pi
!= this->indexes_
->begin())
15627 ast_dump_context
->ostream() << ", ";
15628 ast_dump_context
->ostream() << *pi
<< ':';
15629 ast_dump_context
->dump_expression(*pv
);
15632 ast_dump_context
->ostream() << "}" ;
15636 // Class Fixed_array_construction_expression.
15638 Fixed_array_construction_expression::Fixed_array_construction_expression(
15639 Type
* type
, const std::vector
<unsigned long>* indexes
,
15640 Expression_list
* vals
, Location location
)
15641 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
15642 type
, indexes
, vals
, location
)
15643 { go_assert(type
->array_type() != NULL
&& !type
->is_slice_type()); }
15649 Fixed_array_construction_expression::do_copy()
15651 Type
* t
= this->type()->copy_expressions();
15652 return new Fixed_array_construction_expression(t
, this->indexes(),
15653 (this->vals() == NULL
15655 : this->vals()->copy()),
15659 // Return the backend representation for constructing a fixed array.
15662 Fixed_array_construction_expression::do_get_backend(Translate_context
* context
)
15664 Type
* type
= this->type();
15665 Btype
* btype
= type
->get_backend(context
->gogo());
15666 return this->get_constructor(context
, btype
);
15670 Expression::make_array_composite_literal(Type
* type
, Expression_list
* vals
,
15673 go_assert(type
->array_type() != NULL
&& !type
->is_slice_type());
15674 return new Fixed_array_construction_expression(type
, NULL
, vals
, location
);
15677 // Class Slice_construction_expression.
15679 Slice_construction_expression::Slice_construction_expression(
15680 Type
* type
, const std::vector
<unsigned long>* indexes
,
15681 Expression_list
* vals
, Location location
)
15682 : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION
,
15683 type
, indexes
, vals
, location
),
15684 valtype_(NULL
), array_val_(NULL
), slice_storage_(NULL
),
15685 storage_escapes_(true)
15687 go_assert(type
->is_slice_type());
15689 unsigned long lenval
;
15690 Expression
* length
;
15691 if (vals
== NULL
|| vals
->empty())
15695 if (this->indexes() == NULL
)
15696 lenval
= vals
->size();
15698 lenval
= indexes
->back() + 1;
15700 Type
* int_type
= Type::lookup_integer_type("int");
15701 length
= Expression::make_integer_ul(lenval
, int_type
, location
);
15702 Type
* element_type
= type
->array_type()->element_type();
15703 Array_type
* array_type
= Type::make_array_type(element_type
, length
);
15704 array_type
->set_is_array_incomparable();
15705 this->valtype_
= array_type
;
15711 Slice_construction_expression::do_traverse(Traverse
* traverse
)
15713 if (this->Array_construction_expression::do_traverse(traverse
)
15715 return TRAVERSE_EXIT
;
15716 if (Type::traverse(this->valtype_
, traverse
) == TRAVERSE_EXIT
)
15717 return TRAVERSE_EXIT
;
15718 if (this->array_val_
!= NULL
15719 && Expression::traverse(&this->array_val_
, traverse
) == TRAVERSE_EXIT
)
15720 return TRAVERSE_EXIT
;
15721 if (this->slice_storage_
!= NULL
15722 && Expression::traverse(&this->slice_storage_
, traverse
) == TRAVERSE_EXIT
)
15723 return TRAVERSE_EXIT
;
15724 return TRAVERSE_CONTINUE
;
15727 // Helper routine to create fixed array value underlying the slice literal.
15728 // May be called during flattening, or later during do_get_backend().
15731 Slice_construction_expression::create_array_val()
15733 Array_type
* array_type
= this->type()->array_type();
15734 if (array_type
== NULL
)
15736 go_assert(this->type()->is_error());
15740 Location loc
= this->location();
15741 go_assert(this->valtype_
!= NULL
);
15743 Expression_list
* vals
= this->vals();
15744 return new Fixed_array_construction_expression(
15745 this->valtype_
, this->indexes(), vals
, loc
);
15748 // If we're previous established that the slice storage does not
15749 // escape, then create a separate array temp val here for it. We
15750 // need to do this as part of flattening so as to be able to insert
15751 // the new temp statement.
15754 Slice_construction_expression::do_flatten(Gogo
* gogo
, Named_object
* no
,
15755 Statement_inserter
* inserter
)
15757 if (this->type()->array_type() == NULL
)
15760 // Base class flattening first
15761 this->Array_construction_expression::do_flatten(gogo
, no
, inserter
);
15763 // Create a stack-allocated storage temp if storage won't escape
15764 if (!this->storage_escapes_
15765 && this->slice_storage_
== NULL
15766 && this->element_count() > 0)
15768 Location loc
= this->location();
15769 this->array_val_
= this->create_array_val();
15770 go_assert(this->array_val_
);
15771 Temporary_statement
* temp
=
15772 Statement::make_temporary(this->valtype_
, this->array_val_
, loc
);
15773 inserter
->insert(temp
);
15774 this->slice_storage_
= Expression::make_temporary_reference(temp
, loc
);
15779 // When dumping a slice construction expression that has an explicit
15780 // storeage temp, emit the temp here (if we don't do this the storage
15781 // temp appears unused in the AST dump).
15784 Slice_construction_expression::
15785 dump_slice_storage_expression(Ast_dump_context
* ast_dump_context
) const
15787 if (this->slice_storage_
== NULL
)
15789 ast_dump_context
->ostream() << "storage=" ;
15790 ast_dump_context
->dump_expression(this->slice_storage_
);
15796 Slice_construction_expression::do_copy()
15798 return new Slice_construction_expression(this->type()->copy_expressions(),
15800 (this->vals() == NULL
15802 : this->vals()->copy()),
15806 // Return the backend representation for constructing a slice.
15809 Slice_construction_expression::do_get_backend(Translate_context
* context
)
15811 if (this->array_val_
== NULL
)
15812 this->array_val_
= this->create_array_val();
15813 if (this->array_val_
== NULL
)
15815 go_assert(this->type()->is_error());
15816 return context
->backend()->error_expression();
15819 Location loc
= this->location();
15821 bool is_static_initializer
= this->array_val_
->is_static_initializer();
15823 // We have to copy the initial values into heap memory if we are in
15824 // a function or if the values are not constants.
15825 bool copy_to_heap
= context
->function() != NULL
|| !is_static_initializer
;
15829 if (this->slice_storage_
!= NULL
)
15831 go_assert(!this->storage_escapes_
);
15832 space
= Expression::make_unary(OPERATOR_AND
, this->slice_storage_
, loc
);
15834 else if (!copy_to_heap
)
15836 // The initializer will only run once.
15837 space
= Expression::make_unary(OPERATOR_AND
, this->array_val_
, loc
);
15838 space
->unary_expression()->set_is_slice_init();
15842 go_assert(this->storage_escapes_
|| this->element_count() == 0);
15843 space
= Expression::make_heap_expression(this->array_val_
, loc
);
15845 Array_type
* at
= this->valtype_
->array_type();
15846 Type
* et
= at
->element_type();
15847 space
= Expression::make_unsafe_cast(Type::make_pointer_type(et
),
15850 // Build a constructor for the slice.
15851 Expression
* len
= at
->length();
15852 Expression
* slice_val
=
15853 Expression::make_slice_value(this->type(), space
, len
, len
, loc
);
15854 return slice_val
->get_backend(context
);
15857 // Make a slice composite literal. This is used by the type
15858 // descriptor code.
15860 Slice_construction_expression
*
15861 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
15864 go_assert(type
->is_slice_type());
15865 return new Slice_construction_expression(type
, NULL
, vals
, location
);
15868 // Class Map_construction_expression.
15873 Map_construction_expression::do_traverse(Traverse
* traverse
)
15875 if (this->vals_
!= NULL
15876 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
15877 return TRAVERSE_EXIT
;
15878 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
15879 return TRAVERSE_EXIT
;
15880 return TRAVERSE_CONTINUE
;
15883 // Flatten constructor initializer into a temporary variable since
15884 // we need to take its address for __go_construct_map.
15887 Map_construction_expression::do_flatten(Gogo
* gogo
, Named_object
*,
15888 Statement_inserter
* inserter
)
15890 if (!this->is_error_expression()
15891 && this->vals_
!= NULL
15892 && !this->vals_
->empty()
15893 && this->constructor_temp_
== NULL
)
15895 Map_type
* mt
= this->type_
->map_type();
15896 Type
* key_type
= mt
->key_type();
15897 Type
* val_type
= mt
->val_type();
15898 this->element_type_
= Type::make_builtin_struct_type(2,
15900 "__val", val_type
);
15902 Expression_list
* value_pairs
= new Expression_list();
15903 Location loc
= this->location();
15906 for (Expression_list::const_iterator pv
= this->vals_
->begin();
15907 pv
!= this->vals_
->end();
15910 Expression_list
* key_value_pair
= new Expression_list();
15911 Expression
* key
= *pv
;
15912 if (key
->is_error_expression() || key
->type()->is_error_type())
15914 go_assert(saw_errors());
15915 return Expression::make_error(loc
);
15917 if (key
->type()->interface_type() != NULL
&& !key
->is_variable())
15919 Temporary_statement
* temp
=
15920 Statement::make_temporary(NULL
, key
, loc
);
15921 inserter
->insert(temp
);
15922 key
= Expression::make_temporary_reference(temp
, loc
);
15924 key
= Expression::convert_for_assignment(gogo
, key_type
, key
, loc
);
15927 Expression
* val
= *pv
;
15928 if (val
->is_error_expression() || val
->type()->is_error_type())
15930 go_assert(saw_errors());
15931 return Expression::make_error(loc
);
15933 if (val
->type()->interface_type() != NULL
&& !val
->is_variable())
15935 Temporary_statement
* temp
=
15936 Statement::make_temporary(NULL
, val
, loc
);
15937 inserter
->insert(temp
);
15938 val
= Expression::make_temporary_reference(temp
, loc
);
15940 val
= Expression::convert_for_assignment(gogo
, val_type
, val
, loc
);
15942 key_value_pair
->push_back(key
);
15943 key_value_pair
->push_back(val
);
15944 value_pairs
->push_back(
15945 Expression::make_struct_composite_literal(this->element_type_
,
15946 key_value_pair
, loc
));
15949 Expression
* element_count
= Expression::make_integer_ul(i
, NULL
, loc
);
15950 Array_type
* ctor_type
=
15951 Type::make_array_type(this->element_type_
, element_count
);
15952 ctor_type
->set_is_array_incomparable();
15953 Expression
* constructor
=
15954 new Fixed_array_construction_expression(ctor_type
, NULL
,
15957 this->constructor_temp_
=
15958 Statement::make_temporary(NULL
, constructor
, loc
);
15959 constructor
->issue_nil_check();
15960 this->constructor_temp_
->set_is_address_taken();
15961 inserter
->insert(this->constructor_temp_
);
15967 // Final type determination.
15970 Map_construction_expression::do_determine_type(const Type_context
*)
15972 if (this->vals_
== NULL
)
15975 Map_type
* mt
= this->type_
->map_type();
15976 Type_context
key_context(mt
->key_type(), false);
15977 Type_context
val_context(mt
->val_type(), false);
15978 for (Expression_list::const_iterator pv
= this->vals_
->begin();
15979 pv
!= this->vals_
->end();
15982 (*pv
)->determine_type(&key_context
);
15984 (*pv
)->determine_type(&val_context
);
15991 Map_construction_expression::do_check_types(Gogo
*)
15993 if (this->vals_
== NULL
)
15996 Map_type
* mt
= this->type_
->map_type();
15998 Type
* key_type
= mt
->key_type();
15999 Type
* val_type
= mt
->val_type();
16000 for (Expression_list::const_iterator pv
= this->vals_
->begin();
16001 pv
!= this->vals_
->end();
16004 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
16006 go_error_at((*pv
)->location(),
16007 "incompatible type for element %d key in map construction",
16009 this->set_is_error();
16012 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
16014 go_error_at((*pv
)->location(),
16015 ("incompatible type for element %d value "
16016 "in map construction"),
16018 this->set_is_error();
16026 Map_construction_expression::do_copy()
16028 return new Map_construction_expression(this->type_
->copy_expressions(),
16029 (this->vals_
== NULL
16031 : this->vals_
->copy()),
16035 // Make implicit type conversions explicit.
16038 Map_construction_expression::do_add_conversions()
16040 if (this->vals_
== NULL
|| this->vals_
->empty())
16043 Map_type
* mt
= this->type_
->map_type();
16044 Type
* kt
= mt
->key_type();
16045 Type
* vt
= mt
->val_type();
16046 bool key_is_interface
= (kt
->interface_type() != NULL
);
16047 bool val_is_interface
= (vt
->interface_type() != NULL
);
16048 if (!key_is_interface
&& !val_is_interface
)
16051 Location loc
= this->location();
16052 for (Expression_list::iterator pv
= this->vals_
->begin();
16053 pv
!= this->vals_
->end();
16056 if (key_is_interface
&&
16057 !Type::are_identical(kt
, (*pv
)->type(), 0, NULL
))
16058 *pv
= Expression::make_cast(kt
, *pv
, loc
);
16060 if (val_is_interface
&&
16061 !Type::are_identical(vt
, (*pv
)->type(), 0, NULL
))
16062 *pv
= Expression::make_cast(vt
, *pv
, loc
);
16066 // Return the backend representation for constructing a map.
16069 Map_construction_expression::do_get_backend(Translate_context
* context
)
16071 if (this->is_error_expression())
16072 return context
->backend()->error_expression();
16073 Location loc
= this->location();
16076 Expression
* ventries
;
16077 if (this->vals_
== NULL
|| this->vals_
->empty())
16078 ventries
= Expression::make_nil(loc
);
16081 go_assert(this->constructor_temp_
!= NULL
);
16082 i
= this->vals_
->size() / 2;
16084 Expression
* ctor_ref
=
16085 Expression::make_temporary_reference(this->constructor_temp_
, loc
);
16086 ventries
= Expression::make_unary(OPERATOR_AND
, ctor_ref
, loc
);
16089 Map_type
* mt
= this->type_
->map_type();
16090 if (this->element_type_
== NULL
)
16091 this->element_type_
=
16092 Type::make_builtin_struct_type(2,
16093 "__key", mt
->key_type(),
16094 "__val", mt
->val_type());
16095 Expression
* descriptor
= Expression::make_type_descriptor(mt
, loc
);
16097 Type
* uintptr_t = Type::lookup_integer_type("uintptr");
16098 Expression
* count
= Expression::make_integer_ul(i
, uintptr_t, loc
);
16100 Expression
* entry_size
=
16101 Expression::make_type_info(this->element_type_
, TYPE_INFO_SIZE
);
16103 unsigned int field_index
;
16104 const Struct_field
* valfield
=
16105 this->element_type_
->find_local_field("__val", &field_index
);
16106 Expression
* val_offset
=
16107 Expression::make_struct_field_offset(this->element_type_
, valfield
);
16109 Expression
* map_ctor
=
16110 Runtime::make_call(Runtime::CONSTRUCT_MAP
, loc
, 5, descriptor
, count
,
16111 entry_size
, val_offset
, ventries
);
16112 return map_ctor
->get_backend(context
);
16115 // Export an array construction.
16118 Map_construction_expression::do_export(Export_function_body
* efb
) const
16120 efb
->write_c_string("$convert(");
16121 efb
->write_type(this->type_
);
16122 for (Expression_list::const_iterator pv
= this->vals_
->begin();
16123 pv
!= this->vals_
->end();
16126 efb
->write_c_string(", ");
16127 (*pv
)->export_expression(efb
);
16129 efb
->write_c_string(")");
16132 // Dump ast representation for a map construction expression.
16135 Map_construction_expression::do_dump_expression(
16136 Ast_dump_context
* ast_dump_context
) const
16138 ast_dump_context
->ostream() << "{" ;
16139 ast_dump_context
->dump_expression_list(this->vals_
, true);
16140 ast_dump_context
->ostream() << "}";
16143 // A composite literal key. This is seen during parsing, but is not
16144 // resolved to a named_object in case this is a composite literal of
16147 class Composite_literal_key_expression
: public Parser_expression
16150 Composite_literal_key_expression(const std::string
& name
, Location location
)
16151 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL_KEY
, location
),
16157 { return this->name_
; }
16161 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
16166 return new Composite_literal_key_expression(this->name_
, this->location());
16170 do_dump_expression(Ast_dump_context
*) const;
16177 // Lower a composite literal key. We will never get here for keys in
16178 // composite literals of struct types, because that is prevented by
16179 // Composite_literal_expression::do_traverse. So if we do get here,
16180 // this must be a regular name reference after all.
16183 Composite_literal_key_expression::do_lower(Gogo
* gogo
, Named_object
*,
16184 Statement_inserter
*, int)
16186 Named_object
* no
= gogo
->lookup(this->name_
, NULL
);
16189 // Gogo::lookup doesn't look in the global namespace, and names
16190 // used in composite literal keys aren't seen by
16191 // Gogo::define_global_names, so we have to look in the global
16192 // namespace ourselves.
16193 no
= gogo
->lookup_global(Gogo::unpack_hidden_name(this->name_
).c_str());
16196 go_error_at(this->location(), "reference to undefined name %qs",
16197 Gogo::message_name(this->name_
).c_str());
16198 return Expression::make_error(this->location());
16201 return Expression::make_unknown_reference(no
, this->location());
16204 // Dump a composite literal key.
16207 Composite_literal_key_expression::do_dump_expression(
16208 Ast_dump_context
* ast_dump_context
) const
16210 ast_dump_context
->ostream() << "_UnknownName_(" << this->name_
<< ")";
16213 // Make a composite literal key.
16216 Expression::make_composite_literal_key(const std::string
& name
,
16219 return new Composite_literal_key_expression(name
, location
);
16222 // Class Composite_literal_expression.
16227 Composite_literal_expression::do_traverse(Traverse
* traverse
)
16229 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
16230 return TRAVERSE_EXIT
;
16232 // If this is a struct composite literal with keys, then the keys
16233 // are field names, not expressions. We don't want to traverse them
16234 // in that case. If we do, we can give an erroneous error "variable
16235 // initializer refers to itself." See bug482.go in the testsuite.
16236 if (this->has_keys_
&& this->vals_
!= NULL
)
16238 // The type may not be resolvable at this point.
16239 Type
* type
= this->type_
;
16241 for (int depth
= 0; depth
< this->depth_
; ++depth
)
16243 type
= type
->deref();
16244 if (type
->array_type() != NULL
)
16245 type
= type
->array_type()->element_type();
16246 else if (type
->map_type() != NULL
)
16248 if (this->key_path_
[depth
])
16249 type
= type
->map_type()->key_type();
16251 type
= type
->map_type()->val_type();
16255 // This error will be reported during lowering.
16256 return TRAVERSE_CONTINUE
;
16259 type
= type
->deref();
16263 if (type
->classification() == Type::TYPE_NAMED
)
16264 type
= type
->named_type()->real_type();
16265 else if (type
->classification() == Type::TYPE_FORWARD
)
16267 Type
* t
= type
->forwarded();
16276 if (type
->classification() == Type::TYPE_STRUCT
)
16278 Expression_list::iterator p
= this->vals_
->begin();
16279 while (p
!= this->vals_
->end())
16283 go_assert(p
!= this->vals_
->end());
16284 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
16285 return TRAVERSE_EXIT
;
16288 return TRAVERSE_CONTINUE
;
16292 if (this->vals_
!= NULL
)
16293 return this->vals_
->traverse(traverse
);
16295 return TRAVERSE_CONTINUE
;
16298 // Lower a generic composite literal into a specific version based on
16302 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
16303 Statement_inserter
* inserter
, int)
16305 Type
* type
= this->type_
;
16307 for (int depth
= 0; depth
< this->depth_
; ++depth
)
16309 type
= type
->deref();
16310 if (type
->array_type() != NULL
)
16311 type
= type
->array_type()->element_type();
16312 else if (type
->map_type() != NULL
)
16314 if (this->key_path_
[depth
])
16315 type
= type
->map_type()->key_type();
16317 type
= type
->map_type()->val_type();
16321 if (!type
->is_error())
16322 go_error_at(this->location(),
16323 ("may only omit types within composite literals "
16324 "of slice, array, or map type"));
16325 return Expression::make_error(this->location());
16329 Type
*pt
= type
->points_to();
16330 bool is_pointer
= false;
16338 if (type
->is_error())
16339 return Expression::make_error(this->location());
16340 else if (type
->struct_type() != NULL
)
16341 ret
= this->lower_struct(gogo
, type
);
16342 else if (type
->array_type() != NULL
)
16343 ret
= this->lower_array(type
);
16344 else if (type
->map_type() != NULL
)
16345 ret
= this->lower_map(gogo
, function
, inserter
, type
);
16348 go_error_at(this->location(),
16349 ("expected struct, slice, array, or map type "
16350 "for composite literal"));
16351 return Expression::make_error(this->location());
16355 ret
= Expression::make_heap_expression(ret
, this->location());
16360 // Lower a struct composite literal.
16363 Composite_literal_expression::lower_struct(Gogo
* gogo
, Type
* type
)
16365 Location location
= this->location();
16366 Struct_type
* st
= type
->struct_type();
16367 if (this->vals_
== NULL
|| !this->has_keys_
)
16369 if (this->vals_
!= NULL
16370 && !this->vals_
->empty()
16371 && type
->named_type() != NULL
16372 && type
->named_type()->named_object()->package() != NULL
)
16374 for (Struct_field_list::const_iterator pf
= st
->fields()->begin();
16375 pf
!= st
->fields()->end();
16378 if (Gogo::is_hidden_name(pf
->field_name())
16379 || pf
->is_embedded_builtin(gogo
))
16380 go_error_at(this->location(),
16381 "assignment of unexported field %qs in %qs literal",
16382 Gogo::message_name(pf
->field_name()).c_str(),
16383 type
->named_type()->message_name().c_str());
16387 return new Struct_construction_expression(type
, this->vals_
, location
);
16390 size_t field_count
= st
->field_count();
16391 std::vector
<Expression
*> vals(field_count
);
16392 std::vector
<unsigned long>* traverse_order
= new(std::vector
<unsigned long>);
16393 Expression_list::const_iterator p
= this->vals_
->begin();
16394 Expression
* external_expr
= NULL
;
16395 const Named_object
* external_no
= NULL
;
16396 while (p
!= this->vals_
->end())
16398 Expression
* name_expr
= *p
;
16401 go_assert(p
!= this->vals_
->end());
16402 Expression
* val
= *p
;
16406 if (name_expr
== NULL
)
16408 go_error_at(val
->location(),
16409 "mixture of field and value initializers");
16410 return Expression::make_error(location
);
16413 bool bad_key
= false;
16415 const Named_object
* no
= NULL
;
16416 switch (name_expr
->classification())
16418 case EXPRESSION_COMPOSITE_LITERAL_KEY
:
16420 static_cast<Composite_literal_key_expression
*>(name_expr
)->name();
16423 case EXPRESSION_UNKNOWN_REFERENCE
:
16424 name
= name_expr
->unknown_expression()->name();
16425 if (type
->named_type() != NULL
)
16427 // If the named object found for this field name comes from a
16428 // different package than the struct it is a part of, do not count
16429 // this incorrect lookup as a usage of the object's package.
16430 no
= name_expr
->unknown_expression()->named_object();
16431 if (no
->package() != NULL
16432 && no
->package() != type
->named_type()->named_object()->package())
16433 no
->package()->forget_usage(name_expr
);
16437 case EXPRESSION_CONST_REFERENCE
:
16438 no
= static_cast<Const_expression
*>(name_expr
)->named_object();
16441 case EXPRESSION_TYPE
:
16443 Type
* t
= name_expr
->type();
16444 Named_type
* nt
= t
->named_type();
16448 no
= nt
->named_object();
16452 case EXPRESSION_VAR_REFERENCE
:
16453 no
= name_expr
->var_expression()->named_object();
16456 case EXPRESSION_ENCLOSED_VAR_REFERENCE
:
16457 no
= name_expr
->enclosed_var_expression()->variable();
16460 case EXPRESSION_FUNC_REFERENCE
:
16461 no
= name_expr
->func_expression()->named_object();
16470 go_error_at(name_expr
->location(), "expected struct field name");
16471 return Expression::make_error(location
);
16476 if (no
->package() != NULL
&& external_expr
== NULL
)
16478 external_expr
= name_expr
;
16484 // A predefined name won't be packed. If it starts with a
16485 // lower case letter we need to check for that case, because
16486 // the field name will be packed. FIXME.
16487 if (!Gogo::is_hidden_name(name
)
16491 Named_object
* gno
= gogo
->lookup_global(name
.c_str());
16493 name
= gogo
->pack_hidden_name(name
, false);
16497 unsigned int index
;
16498 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
16501 go_error_at(name_expr
->location(), "unknown field %qs in %qs",
16502 Gogo::message_name(name
).c_str(),
16503 (type
->named_type() != NULL
16504 ? type
->named_type()->message_name().c_str()
16505 : "unnamed struct"));
16506 return Expression::make_error(location
);
16508 if (vals
[index
] != NULL
)
16510 go_error_at(name_expr
->location(),
16511 "duplicate value for field %qs in %qs",
16512 Gogo::message_name(name
).c_str(),
16513 (type
->named_type() != NULL
16514 ? type
->named_type()->message_name().c_str()
16515 : "unnamed struct"));
16516 return Expression::make_error(location
);
16519 if (type
->named_type() != NULL
16520 && type
->named_type()->named_object()->package() != NULL
16521 && (Gogo::is_hidden_name(sf
->field_name())
16522 || sf
->is_embedded_builtin(gogo
)))
16523 go_error_at(name_expr
->location(),
16524 "assignment of unexported field %qs in %qs literal",
16525 Gogo::message_name(sf
->field_name()).c_str(),
16526 type
->named_type()->message_name().c_str());
16529 traverse_order
->push_back(static_cast<unsigned long>(index
));
16532 if (!this->all_are_names_
)
16534 // This is a weird case like bug462 in the testsuite.
16535 if (external_expr
== NULL
)
16536 go_error_at(this->location(), "unknown field in %qs literal",
16537 (type
->named_type() != NULL
16538 ? type
->named_type()->message_name().c_str()
16539 : "unnamed struct"));
16541 go_error_at(external_expr
->location(), "unknown field %qs in %qs",
16542 external_no
->message_name().c_str(),
16543 (type
->named_type() != NULL
16544 ? type
->named_type()->message_name().c_str()
16545 : "unnamed struct"));
16546 return Expression::make_error(location
);
16549 Expression_list
* list
= new Expression_list
;
16550 list
->reserve(field_count
);
16551 for (size_t i
= 0; i
< field_count
; ++i
)
16552 list
->push_back(vals
[i
]);
16554 Struct_construction_expression
* ret
=
16555 new Struct_construction_expression(type
, list
, location
);
16556 ret
->set_traverse_order(traverse_order
);
16560 // Index/value/traversal-order triple.
16562 struct IVT_triple
{
16563 unsigned long index
;
16564 unsigned long traversal_order
;
16566 IVT_triple(unsigned long i
, unsigned long to
, Expression
*e
)
16567 : index(i
), traversal_order(to
), expr(e
) { }
16568 bool operator<(const IVT_triple
& other
) const
16569 { return this->index
< other
.index
; }
16572 // Lower an array composite literal.
16575 Composite_literal_expression::lower_array(Type
* type
)
16577 Location location
= this->location();
16578 if (this->vals_
== NULL
|| !this->has_keys_
)
16579 return this->make_array(type
, NULL
, this->vals_
);
16581 std::vector
<unsigned long>* indexes
= new std::vector
<unsigned long>;
16582 indexes
->reserve(this->vals_
->size());
16583 bool indexes_out_of_order
= false;
16584 Expression_list
* vals
= new Expression_list();
16585 vals
->reserve(this->vals_
->size());
16586 unsigned long index
= 0;
16587 Expression_list::const_iterator p
= this->vals_
->begin();
16588 while (p
!= this->vals_
->end())
16590 Expression
* index_expr
= *p
;
16593 go_assert(p
!= this->vals_
->end());
16594 Expression
* val
= *p
;
16598 if (index_expr
== NULL
)
16600 if (std::find(indexes
->begin(), indexes
->end(), index
)
16603 go_error_at(val
->location(),
16604 "duplicate value for index %lu", index
);
16605 return Expression::make_error(location
);
16607 if (!indexes
->empty())
16608 indexes
->push_back(index
);
16612 if (indexes
->empty() && !vals
->empty())
16614 for (size_t i
= 0; i
< vals
->size(); ++i
)
16615 indexes
->push_back(i
);
16618 Numeric_constant nc
;
16619 if (!index_expr
->numeric_constant_value(&nc
))
16621 go_error_at(index_expr
->location(),
16622 "index expression is not integer constant");
16623 return Expression::make_error(location
);
16626 switch (nc
.to_unsigned_long(&index
))
16628 case Numeric_constant::NC_UL_VALID
:
16630 case Numeric_constant::NC_UL_NOTINT
:
16631 go_error_at(index_expr
->location(),
16632 "index expression is not integer constant");
16633 return Expression::make_error(location
);
16634 case Numeric_constant::NC_UL_NEGATIVE
:
16635 go_error_at(index_expr
->location(),
16636 "index expression is negative");
16637 return Expression::make_error(location
);
16638 case Numeric_constant::NC_UL_BIG
:
16639 go_error_at(index_expr
->location(), "index value overflow");
16640 return Expression::make_error(location
);
16645 Named_type
* ntype
= Type::lookup_integer_type("int");
16646 Integer_type
* inttype
= ntype
->integer_type();
16647 if (sizeof(index
) <= static_cast<size_t>(inttype
->bits() * 8)
16648 && index
>> (inttype
->bits() - 1) != 0)
16650 go_error_at(index_expr
->location(), "index value overflow");
16651 return Expression::make_error(location
);
16654 if (std::find(indexes
->begin(), indexes
->end(), index
)
16657 go_error_at(index_expr
->location(),
16658 "duplicate value for index %lu",
16660 return Expression::make_error(location
);
16663 if (!indexes
->empty() && index
< indexes
->back())
16664 indexes_out_of_order
= true;
16666 indexes
->push_back(index
);
16669 vals
->push_back(val
);
16674 if (indexes
->empty())
16680 std::vector
<unsigned long>* traverse_order
= NULL
;
16681 if (indexes_out_of_order
)
16683 typedef std::vector
<IVT_triple
> V
;
16686 v
.reserve(indexes
->size());
16687 std::vector
<unsigned long>::const_iterator pi
= indexes
->begin();
16688 unsigned long torder
= 0;
16689 for (Expression_list::const_iterator pe
= vals
->begin();
16691 ++pe
, ++pi
, ++torder
)
16692 v
.push_back(IVT_triple(*pi
, torder
, *pe
));
16694 std::sort(v
.begin(), v
.end());
16699 indexes
= new std::vector
<unsigned long>();
16700 indexes
->reserve(v
.size());
16701 vals
= new Expression_list();
16702 vals
->reserve(v
.size());
16703 traverse_order
= new std::vector
<unsigned long>();
16704 traverse_order
->reserve(v
.size());
16706 for (V::const_iterator pv
= v
.begin(); pv
!= v
.end(); ++pv
)
16708 indexes
->push_back(pv
->index
);
16709 vals
->push_back(pv
->expr
);
16710 traverse_order
->push_back(pv
->traversal_order
);
16714 Expression
* ret
= this->make_array(type
, indexes
, vals
);
16715 Array_construction_expression
* ace
= ret
->array_literal();
16716 if (ace
!= NULL
&& traverse_order
!= NULL
)
16717 ace
->set_traverse_order(traverse_order
);
16721 // Actually build the array composite literal. This handles
16725 Composite_literal_expression::make_array(
16727 const std::vector
<unsigned long>* indexes
,
16728 Expression_list
* vals
)
16730 Location location
= this->location();
16731 Array_type
* at
= type
->array_type();
16733 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
16738 else if (indexes
!= NULL
)
16739 size
= indexes
->back() + 1;
16742 size
= vals
->size();
16743 Integer_type
* it
= Type::lookup_integer_type("int")->integer_type();
16744 if (sizeof(size
) <= static_cast<size_t>(it
->bits() * 8)
16745 && size
>> (it
->bits() - 1) != 0)
16747 go_error_at(location
, "too many elements in composite literal");
16748 return Expression::make_error(location
);
16752 Expression
* elen
= Expression::make_integer_ul(size
, NULL
, location
);
16753 at
= Type::make_array_type(at
->element_type(), elen
);
16756 else if (at
->length() != NULL
16757 && !at
->length()->is_error_expression()
16758 && this->vals_
!= NULL
)
16760 Numeric_constant nc
;
16762 if (at
->length()->numeric_constant_value(&nc
)
16763 && nc
.to_unsigned_long(&val
) == Numeric_constant::NC_UL_VALID
)
16765 if (indexes
== NULL
)
16767 if (this->vals_
->size() > val
)
16769 go_error_at(location
,
16770 "too many elements in composite literal");
16771 return Expression::make_error(location
);
16776 unsigned long max
= indexes
->back();
16779 go_error_at(location
,
16780 ("some element keys in composite literal "
16781 "are out of range"));
16782 return Expression::make_error(location
);
16788 if (at
->length() != NULL
)
16789 return new Fixed_array_construction_expression(type
, indexes
, vals
,
16792 return new Slice_construction_expression(type
, indexes
, vals
, location
);
16795 // Lower a map composite literal.
16798 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
16799 Statement_inserter
* inserter
,
16802 Location location
= this->location();
16803 Unordered_map(unsigned int, std::vector
<Expression
*>) st
;
16804 Unordered_map(unsigned int, std::vector
<Expression
*>) nt
;
16805 if (this->vals_
!= NULL
)
16807 if (!this->has_keys_
)
16809 go_error_at(location
, "map composite literal must have keys");
16810 return Expression::make_error(location
);
16813 for (Expression_list::iterator p
= this->vals_
->begin();
16814 p
!= this->vals_
->end();
16820 go_error_at((*p
)->location(),
16821 ("map composite literal must "
16822 "have keys for every value"));
16823 return Expression::make_error(location
);
16825 // Make sure we have lowered the key; it may not have been
16826 // lowered in order to handle keys for struct composite
16827 // literals. Lower it now to get the right error message.
16828 if ((*p
)->unknown_expression() != NULL
)
16830 gogo
->lower_expression(function
, inserter
, &*p
);
16831 go_assert((*p
)->is_error_expression());
16832 return Expression::make_error(location
);
16834 // Check if there are duplicate constant keys.
16835 if (!(*p
)->is_constant())
16838 Numeric_constant nval
;
16839 if ((*p
)->string_constant_value(&sval
)) // Check string keys.
16841 unsigned int h
= Gogo::hash_string(sval
, 0);
16842 // Search the index h in the hash map.
16843 Unordered_map(unsigned int, std::vector
<Expression
*>)::iterator mit
;
16845 if (mit
== st
.end())
16847 // No duplicate since h is a new index.
16848 // Create a new vector indexed by h and add it to the hash map.
16849 std::vector
<Expression
*> l
;
16851 std::pair
<unsigned int, std::vector
<Expression
*> > val(h
, l
);
16856 // Do further check since index h already exists.
16857 for (std::vector
<Expression
*>::iterator lit
=
16858 mit
->second
.begin();
16859 lit
!= mit
->second
.end();
16863 bool ok
= (*lit
)->string_constant_value(&s
);
16867 go_error_at((*p
)->location(), ("duplicate key "
16868 "in map literal"));
16869 return Expression::make_error(location
);
16872 // Add this new string key to the vector indexed by h.
16873 mit
->second
.push_back(*p
);
16876 else if ((*p
)->numeric_constant_value(&nval
)) // Check numeric keys.
16878 unsigned int h
= nval
.hash(0);
16879 Unordered_map(unsigned int, std::vector
<Expression
*>)::iterator mit
;
16881 if (mit
== nt
.end())
16883 // No duplicate since h is a new code.
16884 // Create a new vector indexed by h and add it to the hash map.
16885 std::vector
<Expression
*> l
;
16887 std::pair
<unsigned int, std::vector
<Expression
*> > val(h
, l
);
16892 // Do further check since h already exists.
16893 for (std::vector
<Expression
*>::iterator lit
=
16894 mit
->second
.begin();
16895 lit
!= mit
->second
.end();
16898 Numeric_constant rval
;
16899 bool ok
= (*lit
)->numeric_constant_value(&rval
);
16901 if (nval
.equals(rval
))
16903 go_error_at((*p
)->location(),
16904 "duplicate key in map literal");
16905 return Expression::make_error(location
);
16908 // Add this new numeric key to the vector indexed by h.
16909 mit
->second
.push_back(*p
);
16915 return new Map_construction_expression(type
, this->vals_
, location
);
16921 Composite_literal_expression::do_copy()
16923 Composite_literal_expression
* ret
=
16924 new Composite_literal_expression(this->type_
->copy_expressions(),
16925 this->depth_
, this->has_keys_
,
16926 (this->vals_
== NULL
16928 : this->vals_
->copy()),
16929 this->all_are_names_
,
16931 ret
->key_path_
= this->key_path_
;
16935 // Dump ast representation for a composite literal expression.
16938 Composite_literal_expression::do_dump_expression(
16939 Ast_dump_context
* ast_dump_context
) const
16941 ast_dump_context
->ostream() << "composite(";
16942 ast_dump_context
->dump_type(this->type_
);
16943 ast_dump_context
->ostream() << ", {";
16944 ast_dump_context
->dump_expression_list(this->vals_
, this->has_keys_
);
16945 ast_dump_context
->ostream() << "})";
16948 // Make a composite literal expression.
16951 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
16952 Expression_list
* vals
, bool all_are_names
,
16955 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
16956 all_are_names
, location
);
16959 // Return whether this expression is a composite literal.
16962 Expression::is_composite_literal() const
16964 switch (this->classification_
)
16966 case EXPRESSION_COMPOSITE_LITERAL
:
16967 case EXPRESSION_STRUCT_CONSTRUCTION
:
16968 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
16969 case EXPRESSION_SLICE_CONSTRUCTION
:
16970 case EXPRESSION_MAP_CONSTRUCTION
:
16977 // Return whether this expression is a composite literal which is not
16981 Expression::is_nonconstant_composite_literal() const
16983 switch (this->classification_
)
16985 case EXPRESSION_STRUCT_CONSTRUCTION
:
16987 const Struct_construction_expression
*psce
=
16988 static_cast<const Struct_construction_expression
*>(this);
16989 return !psce
->is_constant_struct();
16991 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
16993 const Fixed_array_construction_expression
*pace
=
16994 static_cast<const Fixed_array_construction_expression
*>(this);
16995 return !pace
->is_constant_array();
16997 case EXPRESSION_SLICE_CONSTRUCTION
:
16999 const Slice_construction_expression
*pace
=
17000 static_cast<const Slice_construction_expression
*>(this);
17001 return !pace
->is_constant_array();
17003 case EXPRESSION_MAP_CONSTRUCTION
:
17010 // Return true if this is a variable or temporary_variable.
17013 Expression::is_variable() const
17015 switch (this->classification_
)
17017 case EXPRESSION_VAR_REFERENCE
:
17018 case EXPRESSION_TEMPORARY_REFERENCE
:
17019 case EXPRESSION_SET_AND_USE_TEMPORARY
:
17020 case EXPRESSION_ENCLOSED_VAR_REFERENCE
:
17027 // Return true if this is a reference to a local variable.
17030 Expression::is_local_variable() const
17032 const Var_expression
* ve
= this->var_expression();
17035 const Named_object
* no
= ve
->named_object();
17036 return (no
->is_result_variable()
17037 || (no
->is_variable() && !no
->var_value()->is_global()));
17040 const Named_object
*
17041 Expression::named_constant() const
17043 if (this->classification() != EXPRESSION_CONST_REFERENCE
)
17045 const Const_expression
* ce
= static_cast<const Const_expression
*>(this);
17046 return ce
->named_object();
17049 // Class Type_guard_expression.
17054 Type_guard_expression::do_traverse(Traverse
* traverse
)
17056 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
17057 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
17058 return TRAVERSE_EXIT
;
17059 return TRAVERSE_CONTINUE
;
17063 Type_guard_expression::do_flatten(Gogo
*, Named_object
*,
17064 Statement_inserter
* inserter
)
17066 if (this->expr_
->is_error_expression()
17067 || this->expr_
->type()->is_error_type())
17069 go_assert(saw_errors());
17070 return Expression::make_error(this->location());
17073 if (!this->expr_
->is_variable())
17075 Temporary_statement
* temp
= Statement::make_temporary(NULL
, this->expr_
,
17077 inserter
->insert(temp
);
17079 Expression::make_temporary_reference(temp
, this->location());
17084 // Check types of a type guard expression. The expression must have
17085 // an interface type, but the actual type conversion is checked at run
17089 Type_guard_expression::do_check_types(Gogo
*)
17091 Type
* expr_type
= this->expr_
->type();
17092 if (expr_type
->interface_type() == NULL
)
17094 if (!expr_type
->is_error() && !this->type_
->is_error())
17095 this->report_error(_("type assertion only valid for interface types"));
17096 this->set_is_error();
17098 else if (this->type_
->interface_type() == NULL
)
17100 std::string reason
;
17101 if (!expr_type
->interface_type()->implements_interface(this->type_
,
17104 if (!this->type_
->is_error())
17106 if (reason
.empty())
17107 this->report_error(_("impossible type assertion: "
17108 "type does not implement interface"));
17110 go_error_at(this->location(),
17111 ("impossible type assertion: "
17112 "type does not implement interface (%s)"),
17115 this->set_is_error();
17123 Type_guard_expression::do_copy()
17125 return new Type_guard_expression(this->expr_
->copy(),
17126 this->type_
->copy_expressions(),
17130 // Return the backend representation for a type guard expression.
17133 Type_guard_expression::do_get_backend(Translate_context
* context
)
17135 Expression
* conversion
;
17136 if (this->type_
->interface_type() != NULL
)
17138 Expression::convert_interface_to_interface(this->type_
, this->expr_
,
17139 true, this->location());
17142 Expression::convert_for_assignment(context
->gogo(), this->type_
,
17143 this->expr_
, this->location());
17145 Gogo
* gogo
= context
->gogo();
17146 Btype
* bt
= this->type_
->get_backend(gogo
);
17147 Bexpression
* bexpr
= conversion
->get_backend(context
);
17148 return gogo
->backend()->convert_expression(bt
, bexpr
, this->location());
17151 // Dump ast representation for a type guard expression.
17154 Type_guard_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
17157 this->expr_
->dump_expression(ast_dump_context
);
17158 ast_dump_context
->ostream() << ".";
17159 ast_dump_context
->dump_type(this->type_
);
17162 // Make a type guard expression.
17165 Expression::make_type_guard(Expression
* expr
, Type
* type
,
17168 return new Type_guard_expression(expr
, type
, location
);
17171 // Class Heap_expression.
17173 // Return the type of the expression stored on the heap.
17176 Heap_expression::do_type()
17177 { return Type::make_pointer_type(this->expr_
->type()); }
17179 // Return the backend representation for allocating an expression on the heap.
17182 Heap_expression::do_get_backend(Translate_context
* context
)
17184 Type
* etype
= this->expr_
->type();
17185 if (this->expr_
->is_error_expression() || etype
->is_error())
17186 return context
->backend()->error_expression();
17188 Location loc
= this->location();
17189 Gogo
* gogo
= context
->gogo();
17190 Btype
* btype
= this->type()->get_backend(gogo
);
17192 Expression
* alloc
= Expression::make_allocation(etype
, loc
);
17193 if (this->allocate_on_stack_
)
17194 alloc
->allocation_expression()->set_allocate_on_stack();
17195 Bexpression
* space
= alloc
->get_backend(context
);
17198 Named_object
* fn
= context
->function();
17199 go_assert(fn
!= NULL
);
17200 Bfunction
* fndecl
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
17201 Bvariable
* space_temp
=
17202 gogo
->backend()->temporary_variable(fndecl
, context
->bblock(), btype
,
17203 space
, true, loc
, &decl
);
17204 Btype
* expr_btype
= etype
->get_backend(gogo
);
17206 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
17208 // If this assignment needs a write barrier, call typedmemmove. We
17209 // don't do this in the write barrier pass because in some cases
17210 // backend conversion can introduce new Heap_expression values.
17212 if (!etype
->has_pointer() || this->allocate_on_stack_
)
17214 space
= gogo
->backend()->var_expression(space_temp
, loc
);
17216 gogo
->backend()->indirect_expression(expr_btype
, space
, true, loc
);
17217 assn
= gogo
->backend()->assignment_statement(fndecl
, ref
, bexpr
, loc
);
17223 gogo
->backend()->temporary_variable(fndecl
, context
->bblock(),
17224 expr_btype
, bexpr
, true, loc
,
17226 Bexpression
* btempref
= gogo
->backend()->var_expression(btemp
,
17228 space
= gogo
->backend()->var_expression(space_temp
, loc
);
17229 Type
* etype_ptr
= Type::make_pointer_type(etype
);
17230 Expression
* elhs
= Expression::make_backend(space
, etype_ptr
, loc
);
17233 if (etype
->is_direct_iface_type())
17236 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
17237 erhs
= Expression::make_backend(btempref
, etype
, loc
);
17238 erhs
= Expression::unpack_direct_iface(erhs
, loc
);
17239 erhs
= Expression::make_unsafe_cast(uintptr_type
, erhs
, loc
);
17240 call
= Runtime::make_call(Runtime::GCWRITEBARRIER
, loc
, 2,
17245 Expression
* td
= Expression::make_type_descriptor(etype
, loc
);
17246 Bexpression
* addr
=
17247 gogo
->backend()->address_expression(btempref
, loc
);
17248 erhs
= Expression::make_backend(addr
, etype_ptr
, loc
);
17249 call
= Runtime::make_call(Runtime::TYPEDMEMMOVE
, loc
, 3,
17252 Statement
* cs
= Statement::make_statement(call
, false);
17254 space
= gogo
->backend()->var_expression(space_temp
, loc
);
17256 gogo
->backend()->indirect_expression(expr_btype
, space
, true, loc
);
17257 Expression
* eref
= Expression::make_backend(ref
, etype
, loc
);
17258 btempref
= gogo
->backend()->var_expression(btemp
, loc
);
17259 erhs
= Expression::make_backend(btempref
, etype
, loc
);
17260 Statement
* as
= Statement::make_assignment(eref
, erhs
, loc
);
17262 as
= gogo
->check_write_barrier(context
->block(), as
, cs
);
17263 Bstatement
* s
= as
->get_backend(context
);
17265 assn
= gogo
->backend()->compound_statement(edecl
, s
);
17267 decl
= gogo
->backend()->compound_statement(decl
, assn
);
17268 space
= gogo
->backend()->var_expression(space_temp
, loc
);
17269 return gogo
->backend()->compound_expression(decl
, space
, loc
);
17272 // Dump ast representation for a heap expression.
17275 Heap_expression::do_dump_expression(
17276 Ast_dump_context
* ast_dump_context
) const
17278 ast_dump_context
->ostream() << "&(";
17279 ast_dump_context
->dump_expression(this->expr_
);
17280 ast_dump_context
->ostream() << ")";
17283 // Allocate an expression on the heap.
17286 Expression::make_heap_expression(Expression
* expr
, Location location
)
17288 return new Heap_expression(expr
, location
);
17291 // Class Receive_expression.
17293 // Return the type of a receive expression.
17296 Receive_expression::do_type()
17298 if (this->is_error_expression())
17299 return Type::make_error_type();
17300 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
17301 if (channel_type
== NULL
)
17303 this->report_error(_("expected channel"));
17304 return Type::make_error_type();
17306 return channel_type
->element_type();
17309 // Check types for a receive expression.
17312 Receive_expression::do_check_types(Gogo
*)
17314 Type
* type
= this->channel_
->type();
17315 if (type
->is_error())
17317 go_assert(saw_errors());
17318 this->set_is_error();
17321 if (type
->channel_type() == NULL
)
17323 this->report_error(_("expected channel"));
17326 if (!type
->channel_type()->may_receive())
17328 this->report_error(_("invalid receive on send-only channel"));
17333 // Flattening for receive expressions creates a temporary variable to store
17334 // received data in for receives.
17337 Receive_expression::do_flatten(Gogo
*, Named_object
*,
17338 Statement_inserter
* inserter
)
17340 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
17341 if (channel_type
== NULL
)
17343 go_assert(saw_errors());
17346 else if (this->channel_
->is_error_expression())
17348 go_assert(saw_errors());
17349 return Expression::make_error(this->location());
17352 Type
* element_type
= channel_type
->element_type();
17353 if (this->temp_receiver_
== NULL
)
17355 this->temp_receiver_
= Statement::make_temporary(element_type
, NULL
,
17357 this->temp_receiver_
->set_is_address_taken();
17358 inserter
->insert(this->temp_receiver_
);
17364 // Get the backend representation for a receive expression.
17367 Receive_expression::do_get_backend(Translate_context
* context
)
17369 Location loc
= this->location();
17371 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
17372 if (channel_type
== NULL
)
17374 go_assert(this->channel_
->type()->is_error());
17375 return context
->backend()->error_expression();
17378 Expression
* recv_ref
=
17379 Expression::make_temporary_reference(this->temp_receiver_
, loc
);
17380 Expression
* recv_addr
=
17381 Expression::make_temporary_reference(this->temp_receiver_
, loc
);
17382 recv_addr
= Expression::make_unary(OPERATOR_AND
, recv_addr
, loc
);
17383 Expression
* recv
= Runtime::make_call(Runtime::CHANRECV1
, loc
, 2,
17384 this->channel_
, recv_addr
);
17385 return Expression::make_compound(recv
, recv_ref
, loc
)->get_backend(context
);
17388 // Export a receive expression.
17391 Receive_expression::do_export(Export_function_body
* efb
) const
17393 efb
->write_c_string("<-");
17394 this->channel_
->export_expression(efb
);
17397 // Dump ast representation for a receive expression.
17400 Receive_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
17402 ast_dump_context
->ostream() << " <- " ;
17403 ast_dump_context
->dump_expression(channel_
);
17406 // Import a receive expression.
17409 Receive_expression::do_import(Import_expression
* imp
, Location loc
)
17411 imp
->require_c_string("<-");
17412 Expression
* expr
= Expression::import_expression(imp
, loc
);
17413 return Expression::make_receive(expr
, loc
);
17416 // Make a receive expression.
17418 Receive_expression
*
17419 Expression::make_receive(Expression
* channel
, Location location
)
17421 return new Receive_expression(channel
, location
);
17424 // An expression which evaluates to a pointer to the type descriptor
17427 class Type_descriptor_expression
: public Expression
17430 Type_descriptor_expression(Type
* type
, Location location
)
17431 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
17437 do_traverse(Traverse
*);
17441 { return Type::make_type_descriptor_ptr_type(); }
17444 do_is_static_initializer() const
17448 do_determine_type(const Type_context
*)
17456 do_get_backend(Translate_context
* context
)
17458 return this->type_
->type_descriptor_pointer(context
->gogo(),
17463 do_dump_expression(Ast_dump_context
*) const;
17466 // The type for which this is the descriptor.
17471 Type_descriptor_expression::do_traverse(Traverse
* traverse
)
17473 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
17474 return TRAVERSE_EXIT
;
17475 return TRAVERSE_CONTINUE
;
17478 // Dump ast representation for a type descriptor expression.
17481 Type_descriptor_expression::do_dump_expression(
17482 Ast_dump_context
* ast_dump_context
) const
17484 ast_dump_context
->dump_type(this->type_
);
17487 // Make a type descriptor expression.
17490 Expression::make_type_descriptor(Type
* type
, Location location
)
17492 return new Type_descriptor_expression(type
, location
);
17495 // An expression which evaluates to a pointer to the Garbage Collection symbol
17498 class GC_symbol_expression
: public Expression
17501 GC_symbol_expression(Type
* type
)
17502 : Expression(EXPRESSION_GC_SYMBOL
, Linemap::predeclared_location()),
17509 { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); }
17512 do_is_static_initializer() const
17516 do_determine_type(const Type_context
*)
17524 do_get_backend(Translate_context
* context
)
17525 { return this->type_
->gc_symbol_pointer(context
->gogo()); }
17528 do_dump_expression(Ast_dump_context
*) const;
17531 // The type which this gc symbol describes.
17535 // Dump ast representation for a gc symbol expression.
17538 GC_symbol_expression::do_dump_expression(
17539 Ast_dump_context
* ast_dump_context
) const
17541 ast_dump_context
->ostream() << "gcdata(";
17542 ast_dump_context
->dump_type(this->type_
);
17543 ast_dump_context
->ostream() << ")";
17546 // Make a gc symbol expression.
17549 Expression::make_gc_symbol(Type
* type
)
17551 return new GC_symbol_expression(type
);
17554 // An expression that evaluates to a pointer to a symbol holding the
17555 // ptrmask data of a type.
17557 class Ptrmask_symbol_expression
: public Expression
17560 Ptrmask_symbol_expression(Type
* type
)
17561 : Expression(EXPRESSION_PTRMASK_SYMBOL
, Linemap::predeclared_location()),
17568 { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); }
17571 do_is_static_initializer() const
17575 do_determine_type(const Type_context
*)
17583 do_get_backend(Translate_context
*);
17586 do_dump_expression(Ast_dump_context
*) const;
17589 // The type that this ptrmask symbol describes.
17593 // Return the ptrmask variable.
17596 Ptrmask_symbol_expression::do_get_backend(Translate_context
* context
)
17598 Gogo
* gogo
= context
->gogo();
17600 // If this type does not need a gcprog, then we can use the standard
17602 int64_t ptrsize
, ptrdata
;
17603 if (!this->type_
->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
17604 return this->type_
->gc_symbol_pointer(gogo
);
17606 // Otherwise we have to build a ptrmask variable, and return a
17609 Bvariable
* bvar
= this->type_
->gc_ptrmask_var(gogo
, ptrsize
, ptrdata
);
17610 Location bloc
= Linemap::predeclared_location();
17611 Bexpression
* bref
= gogo
->backend()->var_expression(bvar
, bloc
);
17612 Bexpression
* baddr
= gogo
->backend()->address_expression(bref
, bloc
);
17614 Type
* uint8_type
= Type::lookup_integer_type("uint8");
17615 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
17616 Btype
* ubtype
= pointer_uint8_type
->get_backend(gogo
);
17617 return gogo
->backend()->convert_expression(ubtype
, baddr
, bloc
);
17620 // Dump AST for a ptrmask symbol expression.
17623 Ptrmask_symbol_expression::do_dump_expression(
17624 Ast_dump_context
* ast_dump_context
) const
17626 ast_dump_context
->ostream() << "ptrmask(";
17627 ast_dump_context
->dump_type(this->type_
);
17628 ast_dump_context
->ostream() << ")";
17631 // Make a ptrmask symbol expression.
17634 Expression::make_ptrmask_symbol(Type
* type
)
17636 return new Ptrmask_symbol_expression(type
);
17639 // An expression which evaluates to some characteristic of a type.
17640 // This is only used to initialize fields of a type descriptor. Using
17641 // a new expression class is slightly inefficient but gives us a good
17642 // separation between the frontend and the middle-end with regard to
17643 // how types are laid out.
17645 class Type_info_expression
: public Expression
17648 Type_info_expression(Type
* type
, Type_info type_info
)
17649 : Expression(EXPRESSION_TYPE_INFO
, Linemap::predeclared_location()),
17650 type_(type
), type_info_(type_info
)
17655 do_is_static_initializer() const
17662 do_determine_type(const Type_context
*)
17670 do_get_backend(Translate_context
* context
);
17673 do_dump_expression(Ast_dump_context
*) const;
17676 // The type for which we are getting information.
17678 // What information we want.
17679 Type_info type_info_
;
17682 // The type is chosen to match what the type descriptor struct
17686 Type_info_expression::do_type()
17688 switch (this->type_info_
)
17690 case TYPE_INFO_SIZE
:
17691 case TYPE_INFO_BACKEND_PTRDATA
:
17692 case TYPE_INFO_DESCRIPTOR_PTRDATA
:
17693 return Type::lookup_integer_type("uintptr");
17694 case TYPE_INFO_ALIGNMENT
:
17695 case TYPE_INFO_FIELD_ALIGNMENT
:
17696 return Type::lookup_integer_type("uint8");
17702 // Return the backend representation for type information.
17705 Type_info_expression::do_get_backend(Translate_context
* context
)
17707 Gogo
* gogo
= context
->gogo();
17710 switch (this->type_info_
)
17712 case TYPE_INFO_SIZE
:
17713 ok
= this->type_
->backend_type_size(gogo
, &val
);
17715 case TYPE_INFO_ALIGNMENT
:
17716 ok
= this->type_
->backend_type_align(gogo
, &val
);
17718 case TYPE_INFO_FIELD_ALIGNMENT
:
17719 ok
= this->type_
->backend_type_field_align(gogo
, &val
);
17721 case TYPE_INFO_BACKEND_PTRDATA
:
17722 ok
= this->type_
->backend_type_ptrdata(gogo
, &val
);
17724 case TYPE_INFO_DESCRIPTOR_PTRDATA
:
17725 ok
= this->type_
->descriptor_ptrdata(gogo
, &val
);
17732 go_assert(saw_errors());
17733 return gogo
->backend()->error_expression();
17735 Expression
* e
= Expression::make_integer_int64(val
, this->type(),
17737 return e
->get_backend(context
);
17740 // Dump ast representation for a type info expression.
17743 Type_info_expression::do_dump_expression(
17744 Ast_dump_context
* ast_dump_context
) const
17746 ast_dump_context
->ostream() << "typeinfo(";
17747 ast_dump_context
->dump_type(this->type_
);
17748 ast_dump_context
->ostream() << ",";
17749 ast_dump_context
->ostream() <<
17750 (this->type_info_
== TYPE_INFO_ALIGNMENT
? "alignment"
17751 : this->type_info_
== TYPE_INFO_FIELD_ALIGNMENT
? "field alignment"
17752 : this->type_info_
== TYPE_INFO_SIZE
? "size"
17753 : this->type_info_
== TYPE_INFO_BACKEND_PTRDATA
? "backend_ptrdata"
17754 : this->type_info_
== TYPE_INFO_DESCRIPTOR_PTRDATA
? "descriptor_ptrdata"
17756 ast_dump_context
->ostream() << ")";
17759 // Make a type info expression.
17762 Expression::make_type_info(Type
* type
, Type_info type_info
)
17764 return new Type_info_expression(type
, type_info
);
17767 // An expression that evaluates to some characteristic of a slice.
17768 // This is used when indexing, bound-checking, or nil checking a slice.
17770 class Slice_info_expression
: public Expression
17773 Slice_info_expression(Expression
* slice
, Slice_info slice_info
,
17775 : Expression(EXPRESSION_SLICE_INFO
, location
),
17776 slice_(slice
), slice_info_(slice_info
)
17784 do_determine_type(const Type_context
*)
17790 return new Slice_info_expression(this->slice_
->copy(), this->slice_info_
,
17795 do_get_backend(Translate_context
* context
);
17798 do_dump_expression(Ast_dump_context
*) const;
17801 do_issue_nil_check()
17802 { this->slice_
->issue_nil_check(); }
17805 // The slice for which we are getting information.
17806 Expression
* slice_
;
17807 // What information we want.
17808 Slice_info slice_info_
;
17811 // Return the type of the slice info.
17814 Slice_info_expression::do_type()
17816 switch (this->slice_info_
)
17818 case SLICE_INFO_VALUE_POINTER
:
17819 return Type::make_pointer_type(
17820 this->slice_
->type()->array_type()->element_type());
17821 case SLICE_INFO_LENGTH
:
17822 case SLICE_INFO_CAPACITY
:
17823 return Type::lookup_integer_type("int");
17829 // Return the backend information for slice information.
17832 Slice_info_expression::do_get_backend(Translate_context
* context
)
17834 Gogo
* gogo
= context
->gogo();
17835 Bexpression
* bslice
= this->slice_
->get_backend(context
);
17836 switch (this->slice_info_
)
17838 case SLICE_INFO_VALUE_POINTER
:
17839 case SLICE_INFO_LENGTH
:
17840 case SLICE_INFO_CAPACITY
:
17841 return gogo
->backend()->struct_field_expression(bslice
, this->slice_info_
,
17849 // Dump ast representation for a type info expression.
17852 Slice_info_expression::do_dump_expression(
17853 Ast_dump_context
* ast_dump_context
) const
17855 ast_dump_context
->ostream() << "sliceinfo(";
17856 this->slice_
->dump_expression(ast_dump_context
);
17857 ast_dump_context
->ostream() << ",";
17858 ast_dump_context
->ostream() <<
17859 (this->slice_info_
== SLICE_INFO_VALUE_POINTER
? "values"
17860 : this->slice_info_
== SLICE_INFO_LENGTH
? "length"
17861 : this->slice_info_
== SLICE_INFO_CAPACITY
? "capacity "
17863 ast_dump_context
->ostream() << ")";
17866 // Make a slice info expression.
17869 Expression::make_slice_info(Expression
* slice
, Slice_info slice_info
,
17872 return new Slice_info_expression(slice
, slice_info
, location
);
17875 // Class Slice_value_expression.
17878 Slice_value_expression::do_traverse(Traverse
* traverse
)
17880 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
17881 || Expression::traverse(&this->valmem_
, traverse
) == TRAVERSE_EXIT
17882 || Expression::traverse(&this->len_
, traverse
) == TRAVERSE_EXIT
17883 || Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
)
17884 return TRAVERSE_EXIT
;
17885 return TRAVERSE_CONTINUE
;
17889 Slice_value_expression::do_copy()
17891 return new Slice_value_expression(this->type_
->copy_expressions(),
17892 this->valmem_
->copy(),
17893 this->len_
->copy(), this->cap_
->copy(),
17898 Slice_value_expression::do_get_backend(Translate_context
* context
)
17900 std::vector
<Bexpression
*> vals(3);
17901 vals
[0] = this->valmem_
->get_backend(context
);
17902 vals
[1] = this->len_
->get_backend(context
);
17903 vals
[2] = this->cap_
->get_backend(context
);
17905 Gogo
* gogo
= context
->gogo();
17906 Btype
* btype
= this->type_
->get_backend(gogo
);
17907 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
17911 Slice_value_expression::do_dump_expression(
17912 Ast_dump_context
* ast_dump_context
) const
17914 ast_dump_context
->ostream() << "slicevalue(";
17915 ast_dump_context
->ostream() << "values: ";
17916 this->valmem_
->dump_expression(ast_dump_context
);
17917 ast_dump_context
->ostream() << ", length: ";
17918 this->len_
->dump_expression(ast_dump_context
);
17919 ast_dump_context
->ostream() << ", capacity: ";
17920 this->cap_
->dump_expression(ast_dump_context
);
17921 ast_dump_context
->ostream() << ")";
17925 Expression::make_slice_value(Type
* at
, Expression
* valmem
, Expression
* len
,
17926 Expression
* cap
, Location location
)
17928 go_assert(at
->is_slice_type());
17929 go_assert(valmem
->is_nil_expression()
17930 || (at
->array_type()->element_type()
17931 == valmem
->type()->points_to()));
17932 return new Slice_value_expression(at
, valmem
, len
, cap
, location
);
17935 // Look through the expression of a Slice_value_expression's valmem to
17936 // find an call to makeslice. If found, return the call expression and
17937 // the containing temporary statement (if any).
17939 std::pair
<Call_expression
*, Temporary_statement
*>
17940 Expression::find_makeslice_call(Expression
* expr
)
17942 Unsafe_type_conversion_expression
* utce
=
17943 expr
->unsafe_conversion_expression();
17945 expr
= utce
->expr();
17947 Slice_value_expression
* sve
= expr
->slice_value_expression();
17949 return std::make_pair
<Call_expression
*, Temporary_statement
*>(NULL
, NULL
);
17950 expr
= sve
->valmem();
17952 utce
= expr
->unsafe_conversion_expression();
17954 expr
= utce
->expr();
17956 Temporary_reference_expression
* tre
= expr
->temporary_reference_expression();
17957 Temporary_statement
* ts
= (tre
!= NULL
? tre
->statement() : NULL
);
17958 if (ts
!= NULL
&& ts
->init() != NULL
&& !ts
->assigned()
17959 && !ts
->is_address_taken())
17962 Call_expression
* call
= expr
->call_expression();
17964 return std::make_pair
<Call_expression
*, Temporary_statement
*>(NULL
, NULL
);
17966 Func_expression
* fe
= call
->fn()->func_expression();
17968 && fe
->runtime_code() == Runtime::MAKESLICE
)
17969 return std::make_pair(call
, ts
);
17971 return std::make_pair
<Call_expression
*, Temporary_statement
*>(NULL
, NULL
);
17974 // An expression that evaluates to some characteristic of a non-empty interface.
17975 // This is used to access the method table or underlying object of an interface.
17977 class Interface_info_expression
: public Expression
17980 Interface_info_expression(Expression
* iface
, Interface_info iface_info
,
17982 : Expression(EXPRESSION_INTERFACE_INFO
, location
),
17983 iface_(iface
), iface_info_(iface_info
)
17991 do_determine_type(const Type_context
*)
17997 return new Interface_info_expression(this->iface_
->copy(),
17998 this->iface_info_
, this->location());
18002 do_get_backend(Translate_context
* context
);
18005 do_dump_expression(Ast_dump_context
*) const;
18008 do_issue_nil_check()
18009 { this->iface_
->issue_nil_check(); }
18012 // The interface for which we are getting information.
18013 Expression
* iface_
;
18014 // What information we want.
18015 Interface_info iface_info_
;
18018 // Return the type of the interface info.
18021 Interface_info_expression::do_type()
18023 switch (this->iface_info_
)
18025 case INTERFACE_INFO_METHODS
:
18027 typedef Unordered_map(Interface_type
*, Type
*) Hashtable
;
18028 static Hashtable result_types
;
18030 Interface_type
* itype
= this->iface_
->type()->interface_type();
18032 Hashtable::const_iterator pr
= result_types
.find(itype
);
18033 if (pr
!= result_types
.end())
18036 Type
* pdt
= Type::make_type_descriptor_ptr_type();
18037 if (itype
->is_empty())
18039 result_types
[itype
] = pdt
;
18043 Location loc
= this->location();
18044 Struct_field_list
* sfl
= new Struct_field_list();
18046 Struct_field(Typed_identifier("__type_descriptor", pdt
, loc
)));
18048 for (Typed_identifier_list::const_iterator p
= itype
->methods()->begin();
18049 p
!= itype
->methods()->end();
18052 Function_type
* ft
= p
->type()->function_type();
18053 go_assert(ft
->receiver() == NULL
);
18055 const Typed_identifier_list
* params
= ft
->parameters();
18056 Typed_identifier_list
* mparams
= new Typed_identifier_list();
18057 if (params
!= NULL
)
18058 mparams
->reserve(params
->size() + 1);
18059 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
18060 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
18061 if (params
!= NULL
)
18063 for (Typed_identifier_list::const_iterator pp
= params
->begin();
18064 pp
!= params
->end();
18066 mparams
->push_back(*pp
);
18069 Typed_identifier_list
* mresults
= (ft
->results() == NULL
18071 : ft
->results()->copy());
18072 Backend_function_type
* mft
=
18073 Type::make_backend_function_type(NULL
, mparams
, mresults
,
18076 std::string fname
= Gogo::unpack_hidden_name(p
->name());
18077 sfl
->push_back(Struct_field(Typed_identifier(fname
, mft
, loc
)));
18080 Struct_type
* st
= Type::make_struct_type(sfl
, loc
);
18081 st
->set_is_struct_incomparable();
18082 Pointer_type
*pt
= Type::make_pointer_type(st
);
18083 result_types
[itype
] = pt
;
18086 case INTERFACE_INFO_OBJECT
:
18087 return Type::make_pointer_type(Type::make_void_type());
18093 // Return the backend representation for interface information.
18096 Interface_info_expression::do_get_backend(Translate_context
* context
)
18098 Gogo
* gogo
= context
->gogo();
18099 Bexpression
* biface
= this->iface_
->get_backend(context
);
18100 switch (this->iface_info_
)
18102 case INTERFACE_INFO_METHODS
:
18103 case INTERFACE_INFO_OBJECT
:
18104 return gogo
->backend()->struct_field_expression(biface
, this->iface_info_
,
18112 // Dump ast representation for an interface info expression.
18115 Interface_info_expression::do_dump_expression(
18116 Ast_dump_context
* ast_dump_context
) const
18118 bool is_empty
= this->iface_
->type()->interface_type()->is_empty();
18119 ast_dump_context
->ostream() << "interfaceinfo(";
18120 this->iface_
->dump_expression(ast_dump_context
);
18121 ast_dump_context
->ostream() << ",";
18122 ast_dump_context
->ostream() <<
18123 (this->iface_info_
== INTERFACE_INFO_METHODS
&& !is_empty
? "methods"
18124 : this->iface_info_
== INTERFACE_INFO_TYPE_DESCRIPTOR
? "type_descriptor"
18125 : this->iface_info_
== INTERFACE_INFO_OBJECT
? "object"
18127 ast_dump_context
->ostream() << ")";
18130 // Make an interface info expression.
18133 Expression::make_interface_info(Expression
* iface
, Interface_info iface_info
,
18136 return new Interface_info_expression(iface
, iface_info
, location
);
18139 // An expression that represents an interface value. The first field is either
18140 // a type descriptor for an empty interface or a pointer to the interface method
18141 // table for a non-empty interface. The second field is always the object.
18143 class Interface_value_expression
: public Expression
18146 Interface_value_expression(Type
* type
, Expression
* first_field
,
18147 Expression
* obj
, Location location
)
18148 : Expression(EXPRESSION_INTERFACE_VALUE
, location
),
18149 type_(type
), first_field_(first_field
), obj_(obj
)
18154 do_traverse(Traverse
*);
18158 { return this->type_
; }
18161 do_determine_type(const Type_context
*)
18162 { go_unreachable(); }
18167 return new Interface_value_expression(this->type_
->copy_expressions(),
18168 this->first_field_
->copy(),
18169 this->obj_
->copy(), this->location());
18173 do_get_backend(Translate_context
* context
);
18176 do_dump_expression(Ast_dump_context
*) const;
18179 // The type of the interface value.
18181 // The first field of the interface (either a type descriptor or a pointer
18182 // to the method table.
18183 Expression
* first_field_
;
18184 // The underlying object of the interface.
18189 Interface_value_expression::do_traverse(Traverse
* traverse
)
18191 if (Expression::traverse(&this->first_field_
, traverse
) == TRAVERSE_EXIT
18192 || Expression::traverse(&this->obj_
, traverse
) == TRAVERSE_EXIT
)
18193 return TRAVERSE_EXIT
;
18194 return TRAVERSE_CONTINUE
;
18198 Interface_value_expression::do_get_backend(Translate_context
* context
)
18200 std::vector
<Bexpression
*> vals(2);
18201 vals
[0] = this->first_field_
->get_backend(context
);
18202 vals
[1] = this->obj_
->get_backend(context
);
18204 Gogo
* gogo
= context
->gogo();
18205 Btype
* btype
= this->type_
->get_backend(gogo
);
18206 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
18210 Interface_value_expression::do_dump_expression(
18211 Ast_dump_context
* ast_dump_context
) const
18213 ast_dump_context
->ostream() << "interfacevalue(";
18214 ast_dump_context
->ostream() <<
18215 (this->type_
->interface_type()->is_empty()
18216 ? "type_descriptor: "
18218 this->first_field_
->dump_expression(ast_dump_context
);
18219 ast_dump_context
->ostream() << ", object: ";
18220 this->obj_
->dump_expression(ast_dump_context
);
18221 ast_dump_context
->ostream() << ")";
18225 Expression::make_interface_value(Type
* type
, Expression
* first_value
,
18226 Expression
* object
, Location location
)
18228 return new Interface_value_expression(type
, first_value
, object
, location
);
18231 // An interface method table for a pair of types: an interface type and a type
18232 // that implements that interface.
18234 class Interface_mtable_expression
: public Expression
18237 Interface_mtable_expression(Interface_type
* itype
, Type
* type
,
18238 bool is_pointer
, Location location
)
18239 : Expression(EXPRESSION_INTERFACE_MTABLE
, location
),
18240 itype_(itype
), type_(type
), is_pointer_(is_pointer
),
18241 method_table_type_(NULL
), bvar_(NULL
)
18246 do_traverse(Traverse
*);
18252 do_is_static_initializer() const
18256 do_determine_type(const Type_context
*)
18257 { go_unreachable(); }
18262 Interface_type
* itype
= this->itype_
->copy_expressions()->interface_type();
18263 return new Interface_mtable_expression(itype
,
18264 this->type_
->copy_expressions(),
18265 this->is_pointer_
, this->location());
18269 do_is_addressable() const
18273 do_get_backend(Translate_context
* context
);
18276 do_dump_expression(Ast_dump_context
*) const;
18279 // The interface type for which the methods are defined.
18280 Interface_type
* itype_
;
18281 // The type to construct the interface method table for.
18283 // Whether this table contains the method set for the receiver type or the
18284 // pointer receiver type.
18286 // The type of the method table.
18287 Type
* method_table_type_
;
18288 // The backend variable that refers to the interface method table.
18293 Interface_mtable_expression::do_traverse(Traverse
* traverse
)
18295 if (Type::traverse(this->itype_
, traverse
) == TRAVERSE_EXIT
18296 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
18297 return TRAVERSE_EXIT
;
18298 return TRAVERSE_CONTINUE
;
18302 Interface_mtable_expression::do_type()
18304 if (this->method_table_type_
!= NULL
)
18305 return this->method_table_type_
;
18307 const Typed_identifier_list
* interface_methods
= this->itype_
->methods();
18308 go_assert(!interface_methods
->empty());
18310 Struct_field_list
* sfl
= new Struct_field_list
;
18311 Typed_identifier
tid("__type_descriptor", Type::make_type_descriptor_ptr_type(),
18313 sfl
->push_back(Struct_field(tid
));
18314 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
18315 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
18316 p
!= interface_methods
->end();
18319 // We want C function pointers here, not func descriptors; model
18320 // using void* pointers.
18321 Typed_identifier
method(p
->name(), unsafe_ptr_type
, p
->location());
18322 sfl
->push_back(Struct_field(method
));
18324 Struct_type
* st
= Type::make_struct_type(sfl
, this->location());
18325 st
->set_is_struct_incomparable();
18326 this->method_table_type_
= st
;
18327 return this->method_table_type_
;
18331 Interface_mtable_expression::do_get_backend(Translate_context
* context
)
18333 Gogo
* gogo
= context
->gogo();
18334 Location loc
= Linemap::predeclared_location();
18335 if (this->bvar_
!= NULL
)
18336 return gogo
->backend()->var_expression(this->bvar_
, this->location());
18338 const Typed_identifier_list
* interface_methods
= this->itype_
->methods();
18339 go_assert(!interface_methods
->empty());
18341 std::string mangled_name
=
18342 gogo
->interface_method_table_name(this->itype_
, this->type_
,
18343 this->is_pointer_
);
18345 // Set is_public if we are converting a named type to an interface
18346 // type that is defined in the same package as the named type, and
18347 // the interface has hidden methods. In that case the interface
18348 // method table will be defined by the package that defines the
18350 bool is_public
= false;
18351 if (this->type_
->named_type() != NULL
18352 && (this->type_
->named_type()->named_object()->package()
18353 == this->itype_
->package()))
18355 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
18356 p
!= interface_methods
->end();
18359 if (Gogo::is_hidden_name(p
->name()))
18368 && this->type_
->named_type()->named_object()->package() != NULL
)
18370 // The interface conversion table is defined elsewhere.
18371 Btype
* btype
= this->type()->get_backend(gogo
);
18373 gogo
->backend()->immutable_struct_reference(mangled_name
, "",
18375 return gogo
->backend()->var_expression(this->bvar_
, this->location());
18378 // The first element is the type descriptor.
18380 if (!this->is_pointer_
)
18381 td_type
= this->type_
;
18383 td_type
= Type::make_pointer_type(this->type_
);
18385 std::vector
<Backend::Btyped_identifier
> bstructfields
;
18387 // Build an interface method table for a type: a type descriptor followed by a
18388 // list of function pointers, one for each interface method. This is used for
18390 Expression_list
* svals
= new Expression_list();
18391 Expression
* tdescriptor
= Expression::make_type_descriptor(td_type
, loc
);
18392 svals
->push_back(tdescriptor
);
18394 Btype
* tdesc_btype
= tdescriptor
->type()->get_backend(gogo
);
18395 Backend::Btyped_identifier
btd("_type", tdesc_btype
, loc
);
18396 bstructfields
.push_back(btd
);
18398 Named_type
* nt
= this->type_
->named_type();
18399 Struct_type
* st
= this->type_
->struct_type();
18400 go_assert(nt
!= NULL
|| st
!= NULL
);
18402 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
18403 p
!= interface_methods
->end();
18409 m
= nt
->method_function(p
->name(), &is_ambiguous
);
18411 m
= st
->method_function(p
->name(), &is_ambiguous
);
18412 go_assert(m
!= NULL
);
18415 && this->type_
->is_direct_iface_type()
18416 && m
->is_value_method()
18417 ? m
->iface_stub_object()
18418 : m
->named_object());
18420 go_assert(no
->is_function() || no
->is_function_declaration());
18422 Function_type
* fcn_type
= (no
->is_function()
18423 ? no
->func_value()->type()
18424 : no
->func_declaration_value()->type());
18425 Btype
* fcn_btype
= fcn_type
->get_backend_fntype(gogo
);
18426 Backend::Btyped_identifier
bmtype(p
->name(), fcn_btype
, loc
);
18427 bstructfields
.push_back(bmtype
);
18429 svals
->push_back(Expression::make_func_code_reference(no
, loc
));
18432 Btype
*btype
= gogo
->backend()->struct_type(bstructfields
);
18433 std::vector
<Bexpression
*> ctor_bexprs
;
18434 for (Expression_list::const_iterator pe
= svals
->begin();
18435 pe
!= svals
->end();
18438 ctor_bexprs
.push_back((*pe
)->get_backend(context
));
18440 Bexpression
* ctor
=
18441 gogo
->backend()->constructor_expression(btype
, ctor_bexprs
, loc
);
18443 this->bvar_
= gogo
->backend()->immutable_struct(mangled_name
, "", false,
18444 !is_public
, btype
, loc
);
18445 gogo
->backend()->immutable_struct_set_init(this->bvar_
, mangled_name
, false,
18446 !is_public
, btype
, loc
, ctor
);
18447 return gogo
->backend()->var_expression(this->bvar_
, loc
);
18451 Interface_mtable_expression::do_dump_expression(
18452 Ast_dump_context
* ast_dump_context
) const
18454 ast_dump_context
->ostream() << "__go_"
18455 << (this->is_pointer_
? "pimt__" : "imt_");
18456 ast_dump_context
->dump_type(this->itype_
);
18457 ast_dump_context
->ostream() << "__";
18458 ast_dump_context
->dump_type(this->type_
);
18462 Expression::make_interface_mtable_ref(Interface_type
* itype
, Type
* type
,
18463 bool is_pointer
, Location location
)
18465 return new Interface_mtable_expression(itype
, type
, is_pointer
, location
);
18468 // An expression which evaluates to the offset of a field within a
18469 // struct. This, like Type_info_expression, q.v., is only used to
18470 // initialize fields of a type descriptor.
18472 class Struct_field_offset_expression
: public Expression
18475 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
18476 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
,
18477 Linemap::predeclared_location()),
18478 type_(type
), field_(field
)
18483 do_is_static_initializer() const
18488 { return Type::lookup_integer_type("uintptr"); }
18491 do_determine_type(const Type_context
*)
18499 do_get_backend(Translate_context
* context
);
18502 do_dump_expression(Ast_dump_context
*) const;
18505 // The type of the struct.
18506 Struct_type
* type_
;
18508 const Struct_field
* field_
;
18511 // Return the backend representation for a struct field offset.
18514 Struct_field_offset_expression::do_get_backend(Translate_context
* context
)
18516 const Struct_field_list
* fields
= this->type_
->fields();
18517 Struct_field_list::const_iterator p
;
18519 for (p
= fields
->begin();
18520 p
!= fields
->end();
18522 if (&*p
== this->field_
)
18524 go_assert(&*p
== this->field_
);
18526 Gogo
* gogo
= context
->gogo();
18527 Btype
* btype
= this->type_
->get_backend(gogo
);
18529 int64_t offset
= gogo
->backend()->type_field_offset(btype
, i
);
18530 Type
* uptr_type
= Type::lookup_integer_type("uintptr");
18532 Expression::make_integer_int64(offset
, uptr_type
,
18533 Linemap::predeclared_location());
18534 return ret
->get_backend(context
);
18537 // Dump ast representation for a struct field offset expression.
18540 Struct_field_offset_expression::do_dump_expression(
18541 Ast_dump_context
* ast_dump_context
) const
18543 ast_dump_context
->ostream() << "unsafe.Offsetof(";
18544 ast_dump_context
->dump_type(this->type_
);
18545 ast_dump_context
->ostream() << '.';
18546 ast_dump_context
->ostream() <<
18547 Gogo::message_name(this->field_
->field_name());
18548 ast_dump_context
->ostream() << ")";
18551 // Make an expression for a struct field offset.
18554 Expression::make_struct_field_offset(Struct_type
* type
,
18555 const Struct_field
* field
)
18557 return new Struct_field_offset_expression(type
, field
);
18560 // An expression which evaluates to the address of an unnamed label.
18562 class Label_addr_expression
: public Expression
18565 Label_addr_expression(Label
* label
, Location location
)
18566 : Expression(EXPRESSION_LABEL_ADDR
, location
),
18573 { return Type::make_pointer_type(Type::make_void_type()); }
18576 do_determine_type(const Type_context
*)
18581 { return new Label_addr_expression(this->label_
, this->location()); }
18584 do_get_backend(Translate_context
* context
)
18585 { return this->label_
->get_addr(context
, this->location()); }
18588 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
18589 { ast_dump_context
->ostream() << this->label_
->name(); }
18592 // The label whose address we are taking.
18596 // Make an expression for the address of an unnamed label.
18599 Expression::make_label_addr(Label
* label
, Location location
)
18601 return new Label_addr_expression(label
, location
);
18604 // Class Conditional_expression.
18609 Conditional_expression::do_traverse(Traverse
* traverse
)
18611 if (Expression::traverse(&this->cond_
, traverse
) == TRAVERSE_EXIT
18612 || Expression::traverse(&this->then_
, traverse
) == TRAVERSE_EXIT
18613 || Expression::traverse(&this->else_
, traverse
) == TRAVERSE_EXIT
)
18614 return TRAVERSE_EXIT
;
18615 return TRAVERSE_CONTINUE
;
18618 // Return the type of the conditional expression.
18621 Conditional_expression::do_type()
18623 Type
* result_type
= Type::make_void_type();
18624 if (Type::are_identical(this->then_
->type(), this->else_
->type(),
18625 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
18627 result_type
= this->then_
->type();
18628 else if (this->then_
->is_nil_expression()
18629 || this->else_
->is_nil_expression())
18630 result_type
= (!this->then_
->is_nil_expression()
18631 ? this->then_
->type()
18632 : this->else_
->type());
18633 return result_type
;
18636 // Determine type for a conditional expression.
18639 Conditional_expression::do_determine_type(const Type_context
* context
)
18641 this->cond_
->determine_type_no_context();
18642 this->then_
->determine_type(context
);
18643 this->else_
->determine_type(context
);
18646 // Get the backend representation of a conditional expression.
18649 Conditional_expression::do_get_backend(Translate_context
* context
)
18651 Gogo
* gogo
= context
->gogo();
18652 Btype
* result_btype
= this->type()->get_backend(gogo
);
18653 Bexpression
* cond
= this->cond_
->get_backend(context
);
18654 Bexpression
* then
= this->then_
->get_backend(context
);
18655 Bexpression
* belse
= this->else_
->get_backend(context
);
18656 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
18657 return gogo
->backend()->conditional_expression(bfn
, result_btype
, cond
, then
,
18658 belse
, this->location());
18661 // Dump ast representation of a conditional expression.
18664 Conditional_expression::do_dump_expression(
18665 Ast_dump_context
* ast_dump_context
) const
18667 ast_dump_context
->ostream() << "(";
18668 ast_dump_context
->dump_expression(this->cond_
);
18669 ast_dump_context
->ostream() << " ? ";
18670 ast_dump_context
->dump_expression(this->then_
);
18671 ast_dump_context
->ostream() << " : ";
18672 ast_dump_context
->dump_expression(this->else_
);
18673 ast_dump_context
->ostream() << ") ";
18676 // Make a conditional expression.
18679 Expression::make_conditional(Expression
* cond
, Expression
* then
,
18680 Expression
* else_expr
, Location location
)
18682 return new Conditional_expression(cond
, then
, else_expr
, location
);
18685 // Class Compound_expression.
18690 Compound_expression::do_traverse(Traverse
* traverse
)
18692 if (Expression::traverse(&this->init_
, traverse
) == TRAVERSE_EXIT
18693 || Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
18694 return TRAVERSE_EXIT
;
18695 return TRAVERSE_CONTINUE
;
18698 // Return the type of the compound expression.
18701 Compound_expression::do_type()
18703 return this->expr_
->type();
18706 // Determine type for a compound expression.
18709 Compound_expression::do_determine_type(const Type_context
* context
)
18711 this->init_
->determine_type_no_context();
18712 this->expr_
->determine_type(context
);
18715 // Get the backend representation of a compound expression.
18718 Compound_expression::do_get_backend(Translate_context
* context
)
18720 Gogo
* gogo
= context
->gogo();
18721 Bexpression
* binit
= this->init_
->get_backend(context
);
18722 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
18723 Bstatement
* init_stmt
= gogo
->backend()->expression_statement(bfunction
,
18725 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
18726 return gogo
->backend()->compound_expression(init_stmt
, bexpr
,
18730 // Dump ast representation of a conditional expression.
18733 Compound_expression::do_dump_expression(
18734 Ast_dump_context
* ast_dump_context
) const
18736 ast_dump_context
->ostream() << "(";
18737 ast_dump_context
->dump_expression(this->init_
);
18738 ast_dump_context
->ostream() << ",";
18739 ast_dump_context
->dump_expression(this->expr_
);
18740 ast_dump_context
->ostream() << ") ";
18743 // Make a compound expression.
18746 Expression::make_compound(Expression
* init
, Expression
* expr
, Location location
)
18748 return new Compound_expression(init
, expr
, location
);
18751 // Class Backend_expression.
18754 Backend_expression::do_traverse(Traverse
*)
18756 return TRAVERSE_CONTINUE
;
18760 Backend_expression::do_copy()
18762 return new Backend_expression(this->bexpr_
, this->type_
->copy_expressions(),
18767 Backend_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
18769 ast_dump_context
->ostream() << "backend_expression<";
18770 ast_dump_context
->dump_type(this->type_
);
18771 ast_dump_context
->ostream() << ">";
18775 Expression::make_backend(Bexpression
* bexpr
, Type
* type
, Location location
)
18777 return new Backend_expression(bexpr
, type
, location
);
18780 // Import an expression. This comes at the end in order to see the
18781 // various class definitions.
18784 Expression::import_expression(Import_expression
* imp
, Location loc
)
18786 Expression
* expr
= Expression::import_expression_without_suffix(imp
, loc
);
18789 if (imp
->match_c_string("("))
18792 Expression_list
* args
= new Expression_list();
18793 bool is_varargs
= false;
18794 while (!imp
->match_c_string(")"))
18796 Expression
* arg
= Expression::import_expression(imp
, loc
);
18797 if (arg
->is_error_expression())
18799 args
->push_back(arg
);
18800 if (imp
->match_c_string(")"))
18802 else if (imp
->match_c_string("...)"))
18808 imp
->require_c_string(", ");
18810 imp
->require_c_string(")");
18811 expr
= Expression::make_call(expr
, args
, is_varargs
, loc
);
18812 expr
->call_expression()->set_varargs_are_lowered();
18814 else if (imp
->match_c_string("["))
18817 Expression
* start
= Expression::import_expression(imp
, loc
);
18818 Expression
* end
= NULL
;
18819 Expression
* cap
= NULL
;
18820 if (imp
->match_c_string(":"))
18823 int c
= imp
->peek_char();
18824 if (c
== ':' || c
== ']')
18825 end
= Expression::make_nil(loc
);
18827 end
= Expression::import_expression(imp
, loc
);
18828 if (imp
->match_c_string(":"))
18831 cap
= Expression::import_expression(imp
, loc
);
18834 imp
->require_c_string("]");
18835 expr
= Expression::make_index(expr
, start
, end
, cap
, loc
);
18844 // Import an expression without considering a suffix (function
18845 // arguments, index operations, etc.).
18848 Expression::import_expression_without_suffix(Import_expression
* imp
,
18851 int c
= imp
->peek_char();
18852 if (c
== '+' || c
== '-' || c
== '!' || c
== '^' || c
== '&' || c
== '*')
18853 return Unary_expression::do_import(imp
, loc
);
18855 return Binary_expression::do_import(imp
, loc
);
18856 else if (imp
->match_c_string("$true")
18857 || imp
->match_c_string("$false")
18858 || (imp
->version() < EXPORT_FORMAT_V3
18859 && (imp
->match_c_string("true")
18860 || imp
->match_c_string("false"))))
18861 return Boolean_expression::do_import(imp
, loc
);
18863 return String_expression::do_import(imp
, loc
);
18864 else if (c
== '-' || (c
>= '0' && c
<= '9'))
18866 // This handles integers, floats and complex constants.
18867 return Integer_expression::do_import(imp
, loc
);
18869 else if (imp
->match_c_string("<-"))
18870 return Receive_expression::do_import(imp
, loc
);
18871 else if (imp
->match_c_string("$nil")
18872 || (imp
->version() < EXPORT_FORMAT_V3
18873 && imp
->match_c_string("nil")))
18874 return Nil_expression::do_import(imp
, loc
);
18875 else if (imp
->match_c_string("$convert")
18876 || (imp
->version() < EXPORT_FORMAT_V3
18877 && imp
->match_c_string("convert")))
18878 return Type_conversion_expression::do_import(imp
, loc
);
18880 Import_function_body
* ifb
= imp
->ifb();
18883 go_error_at(imp
->location(), "import error: expected expression");
18884 return Expression::make_error(loc
);
18886 if (ifb
->saw_error())
18887 return Expression::make_error(loc
);
18889 if (ifb
->match_c_string("$t"))
18890 return Temporary_reference_expression::do_import(ifb
, loc
);
18892 return Expression::import_identifier(ifb
, loc
);
18895 // Import an identifier in an expression. This is a reference to a
18896 // variable or function.
18899 Expression::import_identifier(Import_function_body
* ifb
, Location loc
)
18904 if (!Import::read_qualified_identifier(ifb
, &id
, &pkg
, &is_exported
))
18906 if (!ifb
->saw_error())
18907 go_error_at(ifb
->location(),
18908 "import error for %qs: bad qualified identifier at %lu",
18909 ifb
->name().c_str(),
18910 static_cast<unsigned long>(ifb
->off()));
18911 ifb
->set_saw_error();
18912 return Expression::make_error(loc
);
18915 Named_object
* no
= NULL
;
18916 if (pkg
== NULL
&& is_exported
)
18917 no
= ifb
->block()->bindings()->lookup(id
);
18920 const Package
* ipkg
= pkg
;
18922 ipkg
= ifb
->function()->package();
18924 id
= '.' + ipkg
->pkgpath() + '.' + id
;
18925 no
= ipkg
->bindings()->lookup(id
);
18928 no
= ifb
->gogo()->lookup_global(id
.c_str());
18932 if (!ifb
->saw_error())
18933 go_error_at(ifb
->location(),
18934 "import error for %qs: lookup of %qs failed",
18935 ifb
->name().c_str(), id
.c_str());
18936 ifb
->set_saw_error();
18937 return Expression::make_error(loc
);
18940 if (no
->is_variable() || no
->is_result_variable())
18941 return Expression::make_var_reference(no
, loc
);
18942 else if (no
->is_function() || no
->is_function_declaration())
18943 return Expression::make_func_reference(no
, NULL
, loc
);
18946 if (!ifb
->saw_error())
18947 go_error_at(ifb
->location(),
18948 ("import error for %qs: "
18949 "unexpected type of identifier %qs (%d)"),
18950 ifb
->name().c_str(),
18951 id
.c_str(), no
->classification());
18952 ifb
->set_saw_error();
18953 return Expression::make_error(loc
);
18957 // Class Expression_list.
18959 // Traverse the list.
18962 Expression_list::traverse(Traverse
* traverse
)
18964 for (Expression_list::iterator p
= this->begin();
18970 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
18971 return TRAVERSE_EXIT
;
18974 return TRAVERSE_CONTINUE
;
18980 Expression_list::copy()
18982 Expression_list
* ret
= new Expression_list();
18983 for (Expression_list::iterator p
= this->begin();
18988 ret
->push_back(NULL
);
18990 ret
->push_back((*p
)->copy());
18995 // Return whether an expression list has an error expression.
18998 Expression_list::contains_error() const
19000 for (Expression_list::const_iterator p
= this->begin();
19003 if (*p
!= NULL
&& (*p
)->is_error_expression())
19008 // Class Numeric_constant.
19012 Numeric_constant::~Numeric_constant()
19017 // Copy constructor.
19019 Numeric_constant::Numeric_constant(const Numeric_constant
& a
)
19020 : classification_(a
.classification_
), type_(a
.type_
)
19022 switch (a
.classification_
)
19028 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
19031 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, MPFR_RNDN
);
19034 mpc_init2(this->u_
.complex_val
, mpc_precision
);
19035 mpc_set(this->u_
.complex_val
, a
.u_
.complex_val
, MPC_RNDNN
);
19042 // Assignment operator.
19045 Numeric_constant::operator=(const Numeric_constant
& a
)
19048 this->classification_
= a
.classification_
;
19049 this->type_
= a
.type_
;
19050 switch (a
.classification_
)
19056 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
19059 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, MPFR_RNDN
);
19062 mpc_init2(this->u_
.complex_val
, mpc_precision
);
19063 mpc_set(this->u_
.complex_val
, a
.u_
.complex_val
, MPC_RNDNN
);
19071 // Check equality with another numeric constant.
19074 Numeric_constant::equals(const Numeric_constant
& a
) const
19076 if (this->classification_
!= a
.classification_
)
19079 if (this->type_
!= NULL
&& a
.type_
!= NULL
19080 && !Type::are_identical(this->type_
, a
.type_
,
19081 Type::COMPARE_ALIASES
, NULL
))
19084 switch (a
.classification_
)
19090 return mpz_cmp(this->u_
.int_val
, a
.u_
.int_val
) == 0;
19092 return mpfr_cmp(this->u_
.float_val
, a
.u_
.float_val
) == 0;
19094 return mpc_cmp(this->u_
.complex_val
, a
.u_
.complex_val
) == 0;
19101 // Clear the contents.
19104 Numeric_constant::clear()
19106 switch (this->classification_
)
19112 mpz_clear(this->u_
.int_val
);
19115 mpfr_clear(this->u_
.float_val
);
19118 mpc_clear(this->u_
.complex_val
);
19123 this->classification_
= NC_INVALID
;
19126 // Set to an unsigned long value.
19129 Numeric_constant::set_unsigned_long(Type
* type
, unsigned long val
)
19132 this->classification_
= NC_INT
;
19133 this->type_
= type
;
19134 mpz_init_set_ui(this->u_
.int_val
, val
);
19137 // Set to an integer value.
19140 Numeric_constant::set_int(Type
* type
, const mpz_t val
)
19143 this->classification_
= NC_INT
;
19144 this->type_
= type
;
19145 mpz_init_set(this->u_
.int_val
, val
);
19148 // Set to a rune value.
19151 Numeric_constant::set_rune(Type
* type
, const mpz_t val
)
19154 this->classification_
= NC_RUNE
;
19155 this->type_
= type
;
19156 mpz_init_set(this->u_
.int_val
, val
);
19159 // Set to a floating point value.
19162 Numeric_constant::set_float(Type
* type
, const mpfr_t val
)
19165 this->classification_
= NC_FLOAT
;
19166 this->type_
= type
;
19168 // Numeric constants do not have negative zero values, so remove
19169 // them here. They also don't have infinity or NaN values, but we
19170 // should never see them here.
19173 && type
->float_type() != NULL
19174 && !type
->float_type()->is_abstract())
19175 bits
= type
->float_type()->bits();
19176 if (Numeric_constant::is_float_neg_zero(val
, bits
))
19177 mpfr_init_set_ui(this->u_
.float_val
, 0, MPFR_RNDN
);
19179 mpfr_init_set(this->u_
.float_val
, val
, MPFR_RNDN
);
19182 // Set to a complex value.
19185 Numeric_constant::set_complex(Type
* type
, const mpc_t val
)
19188 this->classification_
= NC_COMPLEX
;
19189 this->type_
= type
;
19191 // Avoid negative zero as in set_float.
19194 && type
->complex_type() != NULL
19195 && !type
->complex_type()->is_abstract())
19196 bits
= type
->complex_type()->bits() / 2;
19199 mpfr_init_set(real
, mpc_realref(val
), MPFR_RNDN
);
19200 if (Numeric_constant::is_float_neg_zero(real
, bits
))
19201 mpfr_set_ui(real
, 0, MPFR_RNDN
);
19204 mpfr_init_set(imag
, mpc_imagref(val
), MPFR_RNDN
);
19205 if (Numeric_constant::is_float_neg_zero(imag
, bits
))
19206 mpfr_set_ui(imag
, 0, MPFR_RNDN
);
19208 mpc_init2(this->u_
.complex_val
, mpc_precision
);
19209 mpc_set_fr_fr(this->u_
.complex_val
, real
, imag
, MPC_RNDNN
);
19215 // Return whether VAL, at a precision of BITS, is a negative zero.
19216 // BITS may be zero in which case it is ignored.
19219 Numeric_constant::is_float_neg_zero(const mpfr_t val
, int bits
)
19221 if (!mpfr_signbit(val
))
19223 if (mpfr_zero_p(val
))
19225 mpfr_exp_t min_exp
;
19231 // In a denormalized float32 the exponent is -126, and there are
19232 // 24 bits of which at least the last must be 1, so the smallest
19233 // representable non-zero exponent is -126 - (24 - 1) == -149.
19237 // Minimum exponent is -1022, there are 53 bits.
19243 return mpfr_get_exp(val
) < min_exp
;
19246 // Get an int value.
19249 Numeric_constant::get_int(mpz_t
* val
) const
19251 go_assert(this->is_int());
19252 mpz_init_set(*val
, this->u_
.int_val
);
19255 // Get a rune value.
19258 Numeric_constant::get_rune(mpz_t
* val
) const
19260 go_assert(this->is_rune());
19261 mpz_init_set(*val
, this->u_
.int_val
);
19264 // Get a floating point value.
19267 Numeric_constant::get_float(mpfr_t
* val
) const
19269 go_assert(this->is_float());
19270 mpfr_init_set(*val
, this->u_
.float_val
, MPFR_RNDN
);
19273 // Get a complex value.
19276 Numeric_constant::get_complex(mpc_t
* val
) const
19278 go_assert(this->is_complex());
19279 mpc_init2(*val
, mpc_precision
);
19280 mpc_set(*val
, this->u_
.complex_val
, MPC_RNDNN
);
19283 // Express value as unsigned long if possible.
19285 Numeric_constant::To_unsigned_long
19286 Numeric_constant::to_unsigned_long(unsigned long* val
) const
19288 switch (this->classification_
)
19292 return this->mpz_to_unsigned_long(this->u_
.int_val
, val
);
19294 return this->mpfr_to_unsigned_long(this->u_
.float_val
, val
);
19296 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
19297 return NC_UL_NOTINT
;
19298 return this->mpfr_to_unsigned_long(mpc_realref(this->u_
.complex_val
),
19305 // Express integer value as unsigned long if possible.
19307 Numeric_constant::To_unsigned_long
19308 Numeric_constant::mpz_to_unsigned_long(const mpz_t ival
,
19309 unsigned long *val
) const
19311 if (mpz_sgn(ival
) < 0)
19312 return NC_UL_NEGATIVE
;
19313 unsigned long ui
= mpz_get_ui(ival
);
19314 if (mpz_cmp_ui(ival
, ui
) != 0)
19317 return NC_UL_VALID
;
19320 // Express floating point value as unsigned long if possible.
19322 Numeric_constant::To_unsigned_long
19323 Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval
,
19324 unsigned long *val
) const
19326 if (!mpfr_integer_p(fval
))
19327 return NC_UL_NOTINT
;
19330 mpfr_get_z(ival
, fval
, MPFR_RNDN
);
19331 To_unsigned_long ret
= this->mpz_to_unsigned_long(ival
, val
);
19336 // Express value as memory size if possible.
19339 Numeric_constant::to_memory_size(int64_t* val
) const
19341 switch (this->classification_
)
19345 return this->mpz_to_memory_size(this->u_
.int_val
, val
);
19347 return this->mpfr_to_memory_size(this->u_
.float_val
, val
);
19349 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
19351 return this->mpfr_to_memory_size(mpc_realref(this->u_
.complex_val
), val
);
19357 // Express integer as memory size if possible.
19360 Numeric_constant::mpz_to_memory_size(const mpz_t ival
, int64_t* val
) const
19362 if (mpz_sgn(ival
) < 0)
19364 if (mpz_fits_slong_p(ival
))
19366 *val
= static_cast<int64_t>(mpz_get_si(ival
));
19370 // Test >= 64, not > 64, because an int64_t can hold 63 bits of a
19372 if (mpz_sizeinbase(ival
, 2) >= 64)
19378 mpz_tdiv_q_2exp(q
, ival
, 32);
19379 mpz_tdiv_r_2exp(r
, ival
, 32);
19380 go_assert(mpz_fits_ulong_p(q
) && mpz_fits_ulong_p(r
));
19381 *val
= ((static_cast<int64_t>(mpz_get_ui(q
)) << 32)
19382 + static_cast<int64_t>(mpz_get_ui(r
)));
19388 // Express floating point value as memory size if possible.
19391 Numeric_constant::mpfr_to_memory_size(const mpfr_t fval
, int64_t* val
) const
19393 if (!mpfr_integer_p(fval
))
19397 mpfr_get_z(ival
, fval
, MPFR_RNDN
);
19398 bool ret
= this->mpz_to_memory_size(ival
, val
);
19403 // Convert value to integer if possible.
19406 Numeric_constant::to_int(mpz_t
* val
) const
19408 switch (this->classification_
)
19412 mpz_init_set(*val
, this->u_
.int_val
);
19415 if (!mpfr_integer_p(this->u_
.float_val
))
19418 mpfr_get_z(*val
, this->u_
.float_val
, MPFR_RNDN
);
19421 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
))
19422 || !mpfr_integer_p(mpc_realref(this->u_
.complex_val
)))
19425 mpfr_get_z(*val
, mpc_realref(this->u_
.complex_val
), MPFR_RNDN
);
19432 // Convert value to floating point if possible.
19435 Numeric_constant::to_float(mpfr_t
* val
) const
19437 switch (this->classification_
)
19441 mpfr_init_set_z(*val
, this->u_
.int_val
, MPFR_RNDN
);
19444 mpfr_init_set(*val
, this->u_
.float_val
, MPFR_RNDN
);
19447 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
19449 mpfr_init_set(*val
, mpc_realref(this->u_
.complex_val
), MPFR_RNDN
);
19456 // Convert value to complex.
19459 Numeric_constant::to_complex(mpc_t
* val
) const
19461 mpc_init2(*val
, mpc_precision
);
19462 switch (this->classification_
)
19466 mpc_set_z(*val
, this->u_
.int_val
, MPC_RNDNN
);
19469 mpc_set_fr(*val
, this->u_
.float_val
, MPC_RNDNN
);
19472 mpc_set(*val
, this->u_
.complex_val
, MPC_RNDNN
);
19482 Numeric_constant::type() const
19484 if (this->type_
!= NULL
)
19485 return this->type_
;
19486 switch (this->classification_
)
19489 return Type::make_abstract_integer_type();
19491 return Type::make_abstract_character_type();
19493 return Type::make_abstract_float_type();
19495 return Type::make_abstract_complex_type();
19501 // If the constant can be expressed in TYPE, then set the type of the
19502 // constant to TYPE and return true. Otherwise return false, and, if
19503 // ISSUE_ERROR is true, report an appropriate error message.
19506 Numeric_constant::set_type(Type
* type
, bool issue_error
, Location loc
)
19509 if (type
== NULL
|| type
->is_error())
19511 else if (type
->integer_type() != NULL
)
19512 ret
= this->check_int_type(type
->integer_type(), issue_error
, loc
);
19513 else if (type
->float_type() != NULL
)
19514 ret
= this->check_float_type(type
->float_type(), issue_error
, loc
);
19515 else if (type
->complex_type() != NULL
)
19516 ret
= this->check_complex_type(type
->complex_type(), issue_error
, loc
);
19521 go_assert(saw_errors());
19524 this->type_
= type
;
19528 // Check whether the constant can be expressed in an integer type.
19531 Numeric_constant::check_int_type(Integer_type
* type
, bool issue_error
,
19535 switch (this->classification_
)
19539 mpz_init_set(val
, this->u_
.int_val
);
19543 if (!mpfr_integer_p(this->u_
.float_val
))
19547 go_error_at(location
,
19548 "floating-point constant truncated to integer");
19549 this->set_invalid();
19554 mpfr_get_z(val
, this->u_
.float_val
, MPFR_RNDN
);
19558 if (!mpfr_integer_p(mpc_realref(this->u_
.complex_val
))
19559 || !mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
19563 go_error_at(location
, "complex constant truncated to integer");
19564 this->set_invalid();
19569 mpfr_get_z(val
, mpc_realref(this->u_
.complex_val
), MPFR_RNDN
);
19577 if (type
->is_abstract())
19581 int bits
= mpz_sizeinbase(val
, 2);
19582 if (type
->is_unsigned())
19584 // For an unsigned type we can only accept a nonnegative
19585 // number, and we must be able to represents at least BITS.
19586 ret
= mpz_sgn(val
) >= 0 && bits
<= type
->bits();
19590 // For a signed type we need an extra bit to indicate the
19591 // sign. We have to handle the most negative integer
19593 ret
= (bits
+ 1 <= type
->bits()
19594 || (bits
<= type
->bits()
19595 && mpz_sgn(val
) < 0
19596 && (mpz_scan1(val
, 0)
19597 == static_cast<unsigned long>(type
->bits() - 1))
19598 && mpz_scan0(val
, type
->bits()) == ULONG_MAX
));
19602 if (!ret
&& issue_error
)
19604 go_error_at(location
, "integer constant overflow");
19605 this->set_invalid();
19611 // Check whether the constant can be expressed in a floating point
19615 Numeric_constant::check_float_type(Float_type
* type
, bool issue_error
,
19619 switch (this->classification_
)
19623 mpfr_init_set_z(val
, this->u_
.int_val
, MPFR_RNDN
);
19627 mpfr_init_set(val
, this->u_
.float_val
, MPFR_RNDN
);
19631 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
19635 this->set_invalid();
19636 go_error_at(location
,
19637 "complex constant truncated to floating-point");
19641 mpfr_init_set(val
, mpc_realref(this->u_
.complex_val
), MPFR_RNDN
);
19649 if (type
->is_abstract())
19651 else if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
19653 // A NaN or Infinity always fits in the range of the type.
19658 mpfr_exp_t exp
= mpfr_get_exp(val
);
19659 mpfr_exp_t max_exp
;
19660 switch (type
->bits())
19672 ret
= exp
<= max_exp
;
19676 // Round the constant to the desired type.
19679 switch (type
->bits())
19682 mpfr_set_prec(t
, 24);
19685 mpfr_set_prec(t
, 53);
19690 mpfr_set(t
, val
, MPFR_RNDN
);
19691 mpfr_set(val
, t
, MPFR_RNDN
);
19694 this->set_float(type
, val
);
19700 if (!ret
&& issue_error
)
19702 go_error_at(location
, "floating-point constant overflow");
19703 this->set_invalid();
19709 // Check whether the constant can be expressed in a complex type.
19712 Numeric_constant::check_complex_type(Complex_type
* type
, bool issue_error
,
19715 if (type
->is_abstract())
19718 mpfr_exp_t max_exp
;
19719 switch (type
->bits())
19732 mpc_init2(val
, mpc_precision
);
19733 switch (this->classification_
)
19737 mpc_set_z(val
, this->u_
.int_val
, MPC_RNDNN
);
19741 mpc_set_fr(val
, this->u_
.float_val
, MPC_RNDNN
);
19745 mpc_set(val
, this->u_
.complex_val
, MPC_RNDNN
);
19753 if (!mpfr_nan_p(mpc_realref(val
))
19754 && !mpfr_inf_p(mpc_realref(val
))
19755 && !mpfr_zero_p(mpc_realref(val
))
19756 && mpfr_get_exp(mpc_realref(val
)) > max_exp
)
19760 go_error_at(location
, "complex real part overflow");
19761 this->set_invalid();
19766 if (!mpfr_nan_p(mpc_imagref(val
))
19767 && !mpfr_inf_p(mpc_imagref(val
))
19768 && !mpfr_zero_p(mpc_imagref(val
))
19769 && mpfr_get_exp(mpc_imagref(val
)) > max_exp
)
19773 go_error_at(location
, "complex imaginary part overflow");
19774 this->set_invalid();
19781 // Round the constant to the desired type.
19783 switch (type
->bits())
19794 mpc_set(t
, val
, MPC_RNDNN
);
19795 mpc_set(val
, t
, MPC_RNDNN
);
19798 this->set_complex(type
, val
);
19806 // Return an Expression for this value.
19809 Numeric_constant::expression(Location loc
) const
19811 switch (this->classification_
)
19814 return Expression::make_integer_z(&this->u_
.int_val
, this->type_
, loc
);
19816 return Expression::make_character(&this->u_
.int_val
, this->type_
, loc
);
19818 return Expression::make_float(&this->u_
.float_val
, this->type_
, loc
);
19820 return Expression::make_complex(&this->u_
.complex_val
, this->type_
, loc
);
19822 go_assert(saw_errors());
19823 return Expression::make_error(loc
);
19829 // Calculate a hash code with a given seed.
19832 Numeric_constant::hash(unsigned int seed
) const
19835 const unsigned int PRIME
= 97;
19840 switch (this->classification_
)
19846 val
= mpz_get_ui(this->u_
.int_val
);
19850 mpc_abs(m
, this->u_
.complex_val
, MPFR_RNDN
);
19851 val
= mpfr_get_ui(m
, MPFR_RNDN
);
19855 f
= mpfr_get_d_2exp(&e
, this->u_
.float_val
, MPFR_RNDN
) * 4294967295.0;
19856 val
= static_cast<unsigned long>(e
+ static_cast<long>(f
));
19862 return (static_cast<unsigned int>(val
) + seed
) * PRIME
;