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.
412 // An exception is &global if global is notinheap, which is a
413 // pointer value but not a direct-iface type and we can't simply
415 bool is_address
= (rhs
->unary_expression() != NULL
416 && rhs
->unary_expression()->op() == OPERATOR_AND
);
418 if (rhs
->is_constant() && !is_address
)
419 obj
= Expression::make_unary(OPERATOR_AND
, rhs
, location
);
422 obj
= Expression::make_heap_expression(rhs
, location
);
424 obj
->heap_expression()->set_allocate_on_stack();
428 return Expression::make_interface_value(lhs_type
, first_field
, obj
, location
);
431 // Return an expression for the pointer-typed value of a direct interface
432 // type. Specifically, for single field struct or array, get the single
433 // field, and do this recursively. The reason for this is that we don't
434 // want to assign a struct or an array to a pointer-typed field. The
435 // backend may not like that.
438 Expression::unpack_direct_iface(Expression
* rhs
, Location loc
)
440 Struct_type
* st
= rhs
->type()->struct_type();
443 go_assert(st
->field_count() == 1);
444 Expression
* field
= Expression::make_field_reference(rhs
, 0, loc
);
445 return unpack_direct_iface(field
, loc
);
447 Array_type
* at
= rhs
->type()->array_type();
451 bool ok
= at
->int_length(&len
);
452 go_assert(ok
&& len
== 1);
453 Type
* int_type
= Type::lookup_integer_type("int");
454 Expression
* index
= Expression::make_integer_ul(0, int_type
, loc
);
455 Expression
* elem
= Expression::make_array_index(rhs
, index
, NULL
, NULL
, loc
);
456 return unpack_direct_iface(elem
, loc
);
461 // The opposite of unpack_direct_iface.
464 Expression::pack_direct_iface(Type
* t
, Expression
* rhs
, Location loc
)
466 if (rhs
->type() == t
)
468 Struct_type
* st
= t
->struct_type();
471 Expression_list
* vals
= new Expression_list();
472 vals
->push_back(pack_direct_iface(st
->field(0)->type(), rhs
, loc
));
473 return Expression::make_struct_composite_literal(t
, vals
, loc
);
475 Array_type
* at
= t
->array_type();
478 Expression_list
* vals
= new Expression_list();
479 vals
->push_back(pack_direct_iface(at
->element_type(), rhs
, loc
));
480 return Expression::make_array_composite_literal(t
, vals
, loc
);
482 return Expression::make_unsafe_cast(t
, rhs
, loc
);
485 // Return an expression for the type descriptor of RHS.
488 Expression::get_interface_type_descriptor(Expression
* rhs
)
490 go_assert(rhs
->type()->interface_type() != NULL
);
491 Location location
= rhs
->location();
493 // The type descriptor is the first field of an empty interface.
494 if (rhs
->type()->interface_type()->is_empty())
495 return Expression::make_interface_info(rhs
, INTERFACE_INFO_TYPE_DESCRIPTOR
,
499 Expression::make_interface_info(rhs
, INTERFACE_INFO_METHODS
, location
);
501 Expression
* descriptor
=
502 Expression::make_dereference(mtable
, NIL_CHECK_NOT_NEEDED
, location
);
503 descriptor
= Expression::make_field_reference(descriptor
, 0, location
);
504 Expression
* nil
= Expression::make_nil(location
);
507 Expression::make_binary(OPERATOR_EQEQ
, mtable
, nil
, location
);
508 return Expression::make_conditional(eq
, nil
, descriptor
, location
);
511 // Return an expression for the conversion of an interface type to an
515 Expression::convert_interface_to_interface(Type
*lhs_type
, Expression
* rhs
,
519 if (Type::are_identical(lhs_type
, rhs
->type(),
520 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
524 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
525 bool lhs_is_empty
= lhs_interface_type
->is_empty();
527 // In the general case this requires runtime examination of the type
528 // method table to match it up with the interface methods.
530 // FIXME: If all of the methods in the right hand side interface
531 // also appear in the left hand side interface, then we don't need
532 // to do a runtime check, although we still need to build a new
535 // We are going to evaluate RHS multiple times.
536 go_assert(rhs
->is_multi_eval_safe());
538 // Get the type descriptor for the right hand side. This will be
539 // NULL for a nil interface.
540 Expression
* rhs_type_expr
= Expression::get_interface_type_descriptor(rhs
);
541 Expression
* lhs_type_expr
=
542 Expression::make_type_descriptor(lhs_type
, location
);
544 Expression
* first_field
;
547 // A type assertion fails when converting a nil interface.
548 first_field
= Runtime::make_call(Runtime::ASSERTITAB
, location
, 2,
549 lhs_type_expr
, rhs_type_expr
);
551 else if (lhs_is_empty
)
553 // A conversion to an empty interface always succeeds, and the
554 // first field is just the type descriptor of the object.
555 first_field
= rhs_type_expr
;
559 // A conversion to a non-empty interface may fail, but unlike a
560 // type assertion converting nil will always succeed.
561 first_field
= Runtime::make_call(Runtime::REQUIREITAB
, location
, 2,
562 lhs_type_expr
, rhs_type_expr
);
565 // The second field is simply the object pointer.
567 Expression::make_interface_info(rhs
, INTERFACE_INFO_OBJECT
, location
);
568 return Expression::make_interface_value(lhs_type
, first_field
, obj
, location
);
571 // Return an expression for the conversion of an interface type to a
572 // non-interface type.
575 Expression::convert_interface_to_type(Gogo
* gogo
, Type
*lhs_type
, Expression
* rhs
,
578 // We are going to evaluate RHS multiple times.
579 go_assert(rhs
->is_multi_eval_safe());
581 // Build an expression to check that the type is valid. It will
582 // panic with an appropriate runtime type error if the type is not
584 // (lhs_type == rhs_type ? nil /*dummy*/ :
585 // panicdottype(lhs_type, rhs_type, inter_type))
586 // For some Oses, we need to call runtime.eqtype instead of
587 // lhs_type == rhs_type, as we may have unmerged type descriptors
588 // from shared libraries.
589 Expression
* lhs_type_expr
= Expression::make_type_descriptor(lhs_type
,
591 Expression
* rhs_descriptor
=
592 Expression::get_interface_type_descriptor(rhs
);
594 Type
* rhs_type
= rhs
->type();
595 Expression
* rhs_inter_expr
= Expression::make_type_descriptor(rhs_type
,
599 if (gogo
->need_eqtype()) {
600 cond
= Runtime::make_call(Runtime::EQTYPE
, location
,
604 cond
= Expression::make_binary(OPERATOR_EQEQ
, lhs_type_expr
,
605 rhs_descriptor
, location
);
608 rhs_descriptor
= Expression::get_interface_type_descriptor(rhs
);
609 Expression
* panic
= Runtime::make_call(Runtime::PANICDOTTYPE
, location
,
610 3, lhs_type_expr
->copy(),
613 Expression
* nil
= Expression::make_nil(location
);
614 Expression
* check
= Expression::make_conditional(cond
, nil
, panic
,
617 // If the conversion succeeds, pull out the value.
618 Expression
* obj
= Expression::make_interface_info(rhs
, INTERFACE_INFO_OBJECT
,
621 // If the value is a direct interface, then it is the value we want.
622 // Otherwise it points to the value.
623 if (lhs_type
->is_direct_iface_type())
624 obj
= Expression::pack_direct_iface(lhs_type
, obj
, location
);
627 obj
= Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type
), obj
,
629 obj
= Expression::make_dereference(obj
, NIL_CHECK_NOT_NEEDED
,
632 return Expression::make_compound(check
, obj
, location
);
635 // Convert an expression to its backend representation. This is implemented by
636 // the child class. Not that it is not in general safe to call this multiple
637 // times for a single expression, but that we don't catch such errors.
640 Expression::get_backend(Translate_context
* context
)
642 // The child may have marked this expression as having an error.
643 if (this->classification_
== EXPRESSION_ERROR
)
645 go_assert(saw_errors());
646 return context
->backend()->error_expression();
649 return this->do_get_backend(context
);
652 // Return a backend expression for VAL.
654 Expression::backend_numeric_constant_expression(Translate_context
* context
,
655 Numeric_constant
* val
)
657 Gogo
* gogo
= context
->gogo();
658 Type
* type
= val
->type();
660 return gogo
->backend()->error_expression();
662 Btype
* btype
= type
->get_backend(gogo
);
664 if (type
->integer_type() != NULL
)
667 if (!val
->to_int(&ival
))
669 go_assert(saw_errors());
670 return gogo
->backend()->error_expression();
672 ret
= gogo
->backend()->integer_constant_expression(btype
, ival
);
675 else if (type
->float_type() != NULL
)
678 if (!val
->to_float(&fval
))
680 go_assert(saw_errors());
681 return gogo
->backend()->error_expression();
683 ret
= gogo
->backend()->float_constant_expression(btype
, fval
);
686 else if (type
->complex_type() != NULL
)
689 if (!val
->to_complex(&cval
))
691 go_assert(saw_errors());
692 return gogo
->backend()->error_expression();
694 ret
= gogo
->backend()->complex_constant_expression(btype
, cval
);
703 // Insert bounds checks for an index expression. Check that that VAL
704 // >= 0 and that it fits in an int. Then check that VAL OP BOUND is
705 // true. If any condition is false, call one of the CODE runtime
706 // functions, which will panic.
709 Expression::check_bounds(Expression
* val
, Operator op
, Expression
* bound
,
710 Runtime::Function code
,
711 Runtime::Function code_u
,
712 Runtime::Function code_extend
,
713 Runtime::Function code_extend_u
,
714 Statement_inserter
* inserter
,
717 go_assert(val
->is_multi_eval_safe());
718 go_assert(bound
->is_multi_eval_safe());
720 Type
* int_type
= Type::lookup_integer_type("int");
721 int int_type_size
= int_type
->integer_type()->bits();
723 Type
* val_type
= val
->type();
724 if (val_type
->integer_type() == NULL
)
726 go_assert(saw_errors());
729 int val_type_size
= val_type
->integer_type()->bits();
730 bool val_is_unsigned
= val_type
->integer_type()->is_unsigned();
732 // Check that VAL >= 0.
733 Expression
* check
= NULL
;
734 if (!val_is_unsigned
)
736 Expression
* zero
= Expression::make_integer_ul(0, val_type
, loc
);
737 check
= Expression::make_binary(OPERATOR_GE
, val
->copy(), zero
, loc
);
740 // If VAL's type is larger than int, check that VAL fits in an int.
741 if (val_type_size
> int_type_size
742 || (val_type_size
== int_type_size
746 mpz_init_set_ui(one
, 1UL);
748 // maxval = 2^(int_type_size - 1) - 1
751 mpz_mul_2exp(maxval
, one
, int_type_size
- 1);
752 mpz_sub_ui(maxval
, maxval
, 1);
753 Expression
* max
= Expression::make_integer_z(&maxval
, val_type
, loc
);
757 Expression
* cmp
= Expression::make_binary(OPERATOR_LE
, val
->copy(),
762 check
= Expression::make_binary(OPERATOR_ANDAND
, check
, cmp
, loc
);
765 // For the final check we can assume that VAL fits in an int.
767 if (val_type
== int_type
)
770 ival
= Expression::make_cast(int_type
, val
->copy(), loc
);
772 // BOUND is assumed to fit in an int. Either it comes from len or
773 // cap, or it was checked by an earlier call.
775 if (bound
->type() == int_type
)
776 ibound
= bound
->copy();
778 ibound
= Expression::make_cast(int_type
, bound
->copy(), loc
);
780 Expression
* cmp
= Expression::make_binary(op
, ival
, ibound
, loc
);
784 check
= Expression::make_binary(OPERATOR_ANDAND
, check
, cmp
, loc
);
787 if (val_type_size
> int_type_size
)
802 Expression
* ignore
= Expression::make_boolean(true, loc
);
803 Expression
* crash
= Runtime::make_call(c
, loc
, 2,
804 val
->copy(), bound
->copy());
805 Expression
* cond
= Expression::make_conditional(check
, ignore
, crash
, loc
);
806 inserter
->insert(Statement::make_statement(cond
, true));
810 Expression::dump_expression(Ast_dump_context
* ast_dump_context
) const
812 this->do_dump_expression(ast_dump_context
);
815 // Error expressions. This are used to avoid cascading errors.
817 class Error_expression
: public Expression
820 Error_expression(Location location
)
821 : Expression(EXPRESSION_ERROR
, location
)
826 do_is_constant() const
830 do_numeric_constant_value(Numeric_constant
* nc
) const
832 nc
->set_unsigned_long(NULL
, 0);
837 do_discarding_value()
842 { return Type::make_error_type(); }
845 do_determine_type(const Type_context
*)
853 do_is_addressable() const
857 do_get_backend(Translate_context
* context
)
858 { return context
->backend()->error_expression(); }
861 do_dump_expression(Ast_dump_context
*) const;
864 // Dump the ast representation for an error expression to a dump context.
867 Error_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
869 ast_dump_context
->ostream() << "_Error_" ;
873 Expression::make_error(Location location
)
875 return new Error_expression(location
);
878 // An expression which is really a type. This is used during parsing.
879 // It is an error if these survive after lowering.
882 Type_expression
: public Expression
885 Type_expression(Type
* type
, Location location
)
886 : Expression(EXPRESSION_TYPE
, location
),
892 do_traverse(Traverse
* traverse
)
893 { return Type::traverse(this->type_
, traverse
); }
897 { return this->type_
; }
900 do_determine_type(const Type_context
*)
904 do_check_types(Gogo
*);
911 do_get_backend(Translate_context
*)
912 { go_unreachable(); }
914 void do_dump_expression(Ast_dump_context
*) const;
917 // The type which we are representing as an expression.
922 Type_expression::do_check_types(Gogo
*)
924 if (this->type_
->is_error())
926 go_assert(saw_errors());
927 this->set_is_error();
930 this->report_error(_("invalid use of type"));
934 Type_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
936 ast_dump_context
->dump_type(this->type_
);
940 Expression::make_type(Type
* type
, Location location
)
942 return new Type_expression(type
, location
);
945 // Class Parser_expression.
948 Parser_expression::do_type()
950 // We should never really ask for the type of a Parser_expression.
951 // However, it can happen, at least when we have an invalid const
952 // whose initializer refers to the const itself. In that case we
953 // may ask for the type when lowering the const itself.
954 go_assert(saw_errors());
955 return Type::make_error_type();
958 // Class Var_expression.
960 // Lower a variable expression. Here we just make sure that the
961 // initialization expression of the variable has been lowered. This
962 // ensures that we will be able to determine the type of the variable
966 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
967 Statement_inserter
* inserter
, int)
969 if (this->variable_
->is_variable())
971 Variable
* var
= this->variable_
->var_value();
972 // This is either a local variable or a global variable. A
973 // reference to a variable which is local to an enclosing
974 // function will be a reference to a field in a closure.
975 if (var
->is_global())
980 var
->lower_init_expression(gogo
, function
, inserter
);
985 // Return the type of a reference to a variable.
988 Var_expression::do_type()
990 if (this->variable_
->is_variable())
991 return this->variable_
->var_value()->type();
992 else if (this->variable_
->is_result_variable())
993 return this->variable_
->result_var_value()->type();
998 // Determine the type of a reference to a variable.
1001 Var_expression::do_determine_type(const Type_context
*)
1003 if (this->variable_
->is_variable())
1004 this->variable_
->var_value()->determine_type();
1007 // Something takes the address of this variable. This means that we
1008 // may want to move the variable onto the heap.
1011 Var_expression::do_address_taken(bool escapes
)
1015 if (this->variable_
->is_variable())
1016 this->variable_
->var_value()->set_non_escaping_address_taken();
1017 else if (this->variable_
->is_result_variable())
1018 this->variable_
->result_var_value()->set_non_escaping_address_taken();
1024 if (this->variable_
->is_variable())
1025 this->variable_
->var_value()->set_address_taken();
1026 else if (this->variable_
->is_result_variable())
1027 this->variable_
->result_var_value()->set_address_taken();
1032 if (this->variable_
->is_variable()
1033 && this->variable_
->var_value()->is_in_heap())
1035 Node::make_node(this)->set_encoding(Node::ESCAPE_HEAP
);
1036 Node::make_node(this->variable_
)->set_encoding(Node::ESCAPE_HEAP
);
1040 // Export a reference to a variable.
1043 Var_expression::do_export(Export_function_body
* efb
) const
1045 Named_object
* no
= this->variable_
;
1046 if (no
->is_result_variable() || !no
->var_value()->is_global())
1047 efb
->write_string(Gogo::unpack_hidden_name(no
->name()));
1049 Expression::export_name(efb
, no
);
1052 // Get the backend representation for a reference to a variable.
1055 Var_expression::do_get_backend(Translate_context
* context
)
1057 Bvariable
* bvar
= this->variable_
->get_backend_variable(context
->gogo(),
1058 context
->function());
1060 Location loc
= this->location();
1062 Gogo
* gogo
= context
->gogo();
1063 if (this->variable_
->is_variable())
1065 is_in_heap
= this->variable_
->var_value()->is_in_heap();
1066 btype
= this->variable_
->var_value()->type()->get_backend(gogo
);
1068 else if (this->variable_
->is_result_variable())
1070 is_in_heap
= this->variable_
->result_var_value()->is_in_heap();
1071 btype
= this->variable_
->result_var_value()->type()->get_backend(gogo
);
1077 context
->backend()->var_expression(bvar
, loc
);
1079 ret
= context
->backend()->indirect_expression(btype
, ret
, true, loc
);
1083 // Ast dump for variable expression.
1086 Var_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1088 ast_dump_context
->ostream() << this->variable_
->message_name() ;
1091 // Make a reference to a variable in an expression.
1094 Expression::make_var_reference(Named_object
* var
, Location location
)
1097 return Expression::make_sink(location
);
1099 // FIXME: Creating a new object for each reference to a variable is
1101 return new Var_expression(var
, location
);
1104 // Class Enclosed_var_expression.
1107 Enclosed_var_expression::do_traverse(Traverse
*)
1109 return TRAVERSE_CONTINUE
;
1112 // Lower the reference to the enclosed variable.
1115 Enclosed_var_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
1116 Statement_inserter
* inserter
, int)
1118 gogo
->lower_expression(function
, inserter
, &this->reference_
);
1122 // Flatten the reference to the enclosed variable.
1125 Enclosed_var_expression::do_flatten(Gogo
* gogo
, Named_object
* function
,
1126 Statement_inserter
* inserter
)
1128 gogo
->flatten_expression(function
, inserter
, &this->reference_
);
1133 Enclosed_var_expression::do_address_taken(bool escapes
)
1137 if (this->variable_
->is_variable())
1138 this->variable_
->var_value()->set_non_escaping_address_taken();
1139 else if (this->variable_
->is_result_variable())
1140 this->variable_
->result_var_value()->set_non_escaping_address_taken();
1146 if (this->variable_
->is_variable())
1147 this->variable_
->var_value()->set_address_taken();
1148 else if (this->variable_
->is_result_variable())
1149 this->variable_
->result_var_value()->set_address_taken();
1154 if (this->variable_
->is_variable()
1155 && this->variable_
->var_value()->is_in_heap())
1156 Node::make_node(this->variable_
)->set_encoding(Node::ESCAPE_HEAP
);
1159 // Ast dump for enclosed variable expression.
1162 Enclosed_var_expression::do_dump_expression(Ast_dump_context
* adc
) const
1164 adc
->ostream() << this->variable_
->message_name();
1167 // Make a reference to a variable within an enclosing function.
1170 Expression::make_enclosing_var_reference(Expression
* reference
,
1171 Named_object
* var
, Location location
)
1173 return new Enclosed_var_expression(reference
, var
, location
);
1176 // Class Temporary_reference_expression.
1181 Temporary_reference_expression::do_type()
1183 return this->statement_
->type();
1186 // Called if something takes the address of this temporary variable.
1187 // We never have to move temporary variables to the heap, but we do
1188 // need to know that they must live in the stack rather than in a
1192 Temporary_reference_expression::do_address_taken(bool)
1194 this->statement_
->set_is_address_taken();
1197 // Export a reference to a temporary.
1200 Temporary_reference_expression::do_export(Export_function_body
* efb
) const
1202 unsigned int idx
= efb
->temporary_index(this->statement_
);
1204 snprintf(buf
, sizeof buf
, "$t%u", idx
);
1205 efb
->write_c_string(buf
);
1208 // Import a reference to a temporary.
1211 Temporary_reference_expression::do_import(Import_function_body
* ifb
,
1214 std::string id
= ifb
->read_identifier();
1215 go_assert(id
[0] == '$' && id
[1] == 't');
1216 const char *p
= id
.c_str();
1218 long idx
= strtol(p
+ 2, &end
, 10);
1219 if (*end
!= '\0' || idx
> 0x7fffffff)
1221 if (!ifb
->saw_error())
1223 ("invalid export data for %qs: "
1224 "invalid temporary reference index at %lu"),
1225 ifb
->name().c_str(),
1226 static_cast<unsigned long>(ifb
->off()));
1227 ifb
->set_saw_error();
1228 return Expression::make_error(loc
);
1231 Temporary_statement
* temp
=
1232 ifb
->temporary_statement(static_cast<unsigned int>(idx
));
1235 if (!ifb
->saw_error())
1237 ("invalid export data for %qs: "
1238 "undefined temporary reference index at %lu"),
1239 ifb
->name().c_str(),
1240 static_cast<unsigned long>(ifb
->off()));
1241 ifb
->set_saw_error();
1242 return Expression::make_error(loc
);
1245 return Expression::make_temporary_reference(temp
, loc
);
1248 // Get a backend expression referring to the variable.
1251 Temporary_reference_expression::do_get_backend(Translate_context
* context
)
1253 Gogo
* gogo
= context
->gogo();
1254 Bvariable
* bvar
= this->statement_
->get_backend_variable(context
);
1255 Bexpression
* ret
= gogo
->backend()->var_expression(bvar
, this->location());
1257 // The backend can't always represent the same set of recursive types
1258 // that the Go frontend can. In some cases this means that a
1259 // temporary variable won't have the right backend type. Correct
1260 // that here by adding a type cast. We need to use base() to push
1261 // the circularity down one level.
1262 Type
* stype
= this->statement_
->type();
1263 if (!this->is_lvalue_
1264 && stype
->points_to() != NULL
1265 && stype
->points_to()->is_void_type())
1267 Btype
* btype
= this->type()->base()->get_backend(gogo
);
1268 ret
= gogo
->backend()->convert_expression(btype
, ret
, this->location());
1273 // Ast dump for temporary reference.
1276 Temporary_reference_expression::do_dump_expression(
1277 Ast_dump_context
* ast_dump_context
) const
1279 ast_dump_context
->dump_temp_variable_name(this->statement_
);
1282 // Make a reference to a temporary variable.
1284 Temporary_reference_expression
*
1285 Expression::make_temporary_reference(Temporary_statement
* statement
,
1288 statement
->add_use();
1289 return new Temporary_reference_expression(statement
, location
);
1292 // Class Set_and_use_temporary_expression.
1297 Set_and_use_temporary_expression::do_type()
1299 return this->statement_
->type();
1302 // Determine the type of the expression.
1305 Set_and_use_temporary_expression::do_determine_type(
1306 const Type_context
* context
)
1308 this->expr_
->determine_type(context
);
1311 // Take the address.
1314 Set_and_use_temporary_expression::do_address_taken(bool)
1316 this->statement_
->set_is_address_taken();
1319 // Return the backend representation.
1322 Set_and_use_temporary_expression::do_get_backend(Translate_context
* context
)
1324 Location loc
= this->location();
1325 Gogo
* gogo
= context
->gogo();
1326 Bvariable
* bvar
= this->statement_
->get_backend_variable(context
);
1327 Bexpression
* lvar_ref
= gogo
->backend()->var_expression(bvar
, loc
);
1329 Named_object
* fn
= context
->function();
1330 go_assert(fn
!= NULL
);
1331 Bfunction
* bfn
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
1332 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
1333 Bstatement
* set
= gogo
->backend()->assignment_statement(bfn
, lvar_ref
,
1335 Bexpression
* var_ref
= gogo
->backend()->var_expression(bvar
, loc
);
1336 Bexpression
* ret
= gogo
->backend()->compound_expression(set
, var_ref
, loc
);
1343 Set_and_use_temporary_expression::do_dump_expression(
1344 Ast_dump_context
* ast_dump_context
) const
1346 ast_dump_context
->ostream() << '(';
1347 ast_dump_context
->dump_temp_variable_name(this->statement_
);
1348 ast_dump_context
->ostream() << " = ";
1349 this->expr_
->dump_expression(ast_dump_context
);
1350 ast_dump_context
->ostream() << ')';
1353 // Make a set-and-use temporary.
1355 Set_and_use_temporary_expression
*
1356 Expression::make_set_and_use_temporary(Temporary_statement
* statement
,
1357 Expression
* expr
, Location location
)
1359 return new Set_and_use_temporary_expression(statement
, expr
, location
);
1362 // A sink expression--a use of the blank identifier _.
1364 class Sink_expression
: public Expression
1367 Sink_expression(Location location
)
1368 : Expression(EXPRESSION_SINK
, location
),
1369 type_(NULL
), bvar_(NULL
)
1374 do_discarding_value()
1381 do_determine_type(const Type_context
*);
1385 { return new Sink_expression(this->location()); }
1388 do_get_backend(Translate_context
*);
1391 do_dump_expression(Ast_dump_context
*) const;
1394 // The type of this sink variable.
1396 // The temporary variable we generate.
1400 // Return the type of a sink expression.
1403 Sink_expression::do_type()
1405 if (this->type_
== NULL
)
1406 return Type::make_sink_type();
1410 // Determine the type of a sink expression.
1413 Sink_expression::do_determine_type(const Type_context
* context
)
1415 if (context
->type
!= NULL
)
1416 this->type_
= context
->type
;
1419 // Return a temporary variable for a sink expression. This will
1420 // presumably be a write-only variable which the middle-end will drop.
1423 Sink_expression::do_get_backend(Translate_context
* context
)
1425 Location loc
= this->location();
1426 Gogo
* gogo
= context
->gogo();
1427 if (this->bvar_
== NULL
)
1429 if (this->type_
== NULL
|| this->type_
->is_sink_type())
1431 go_assert(saw_errors());
1432 return gogo
->backend()->error_expression();
1435 Named_object
* fn
= context
->function();
1436 go_assert(fn
!= NULL
);
1437 Bfunction
* fn_ctx
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
1438 Btype
* bt
= this->type_
->get_backend(context
->gogo());
1441 gogo
->backend()->temporary_variable(fn_ctx
, context
->bblock(), bt
, NULL
,
1443 Bexpression
* var_ref
=
1444 gogo
->backend()->var_expression(this->bvar_
, loc
);
1445 var_ref
= gogo
->backend()->compound_expression(decl
, var_ref
, loc
);
1448 return gogo
->backend()->var_expression(this->bvar_
, loc
);
1451 // Ast dump for sink expression.
1454 Sink_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1456 ast_dump_context
->ostream() << "_" ;
1459 // Make a sink expression.
1462 Expression::make_sink(Location location
)
1464 return new Sink_expression(location
);
1467 // Class Func_expression.
1469 // FIXME: Can a function expression appear in a constant expression?
1470 // The value is unchanging. Initializing a constant to the address of
1471 // a function seems like it could work, though there might be little
1477 Func_expression::do_traverse(Traverse
* traverse
)
1479 return (this->closure_
== NULL
1481 : Expression::traverse(&this->closure_
, traverse
));
1484 // Return the type of a function expression.
1487 Func_expression::do_type()
1489 if (this->function_
->is_function())
1490 return this->function_
->func_value()->type();
1491 else if (this->function_
->is_function_declaration())
1492 return this->function_
->func_declaration_value()->type();
1497 // Get the backend representation for the code of a function expression.
1500 Func_expression::get_code_pointer(Gogo
* gogo
, Named_object
* no
, Location loc
)
1502 Function_type
* fntype
;
1503 if (no
->is_function())
1504 fntype
= no
->func_value()->type();
1505 else if (no
->is_function_declaration())
1506 fntype
= no
->func_declaration_value()->type();
1510 // Builtin functions are handled specially by Call_expression. We
1511 // can't take their address.
1512 if (fntype
->is_builtin())
1515 ("invalid use of special built-in function %qs; "
1517 no
->message_name().c_str());
1518 return gogo
->backend()->error_expression();
1522 if (no
->is_function())
1523 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
);
1524 else if (no
->is_function_declaration())
1525 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
);
1529 return gogo
->backend()->function_code_expression(fndecl
, loc
);
1532 // Get the backend representation for a function expression. This is used when
1533 // we take the address of a function rather than simply calling it. A func
1534 // value is represented as a pointer to a block of memory. The first
1535 // word of that memory is a pointer to the function code. The
1536 // remaining parts of that memory are the addresses of variables that
1537 // the function closes over.
1540 Func_expression::do_get_backend(Translate_context
* context
)
1542 // If there is no closure, just use the function descriptor.
1543 if (this->closure_
== NULL
)
1545 Gogo
* gogo
= context
->gogo();
1546 Named_object
* no
= this->function_
;
1547 Expression
* descriptor
;
1548 if (no
->is_function())
1549 descriptor
= no
->func_value()->descriptor(gogo
, no
);
1550 else if (no
->is_function_declaration())
1552 if (no
->func_declaration_value()->type()->is_builtin())
1554 go_error_at(this->location(),
1555 ("invalid use of special built-in function %qs; "
1557 no
->message_name().c_str());
1558 return gogo
->backend()->error_expression();
1560 descriptor
= no
->func_declaration_value()->descriptor(gogo
, no
);
1565 Bexpression
* bdesc
= descriptor
->get_backend(context
);
1566 return gogo
->backend()->address_expression(bdesc
, this->location());
1569 go_assert(this->function_
->func_value()->enclosing() != NULL
);
1571 // If there is a closure, then the closure is itself the function
1572 // expression. It is a pointer to a struct whose first field points
1573 // to the function code and whose remaining fields are the addresses
1574 // of the closed-over variables.
1575 Bexpression
*bexpr
= this->closure_
->get_backend(context
);
1577 // Introduce a backend type conversion, to account for any differences
1578 // between the argument type (function descriptor, struct with a
1579 // single field) and the closure (struct with multiple fields).
1580 Gogo
* gogo
= context
->gogo();
1581 Btype
*btype
= this->type()->get_backend(gogo
);
1582 return gogo
->backend()->convert_expression(btype
, bexpr
, this->location());
1585 // The cost of inlining a function reference.
1588 Func_expression::do_inlining_cost() const
1590 // FIXME: We don't inline references to nested functions.
1591 if (this->closure_
!= NULL
)
1593 if (this->function_
->is_function()
1594 && this->function_
->func_value()->enclosing() != NULL
)
1600 // Export a reference to a function.
1603 Func_expression::do_export(Export_function_body
* efb
) const
1605 Expression::export_name(efb
, this->function_
);
1608 // Ast dump for function.
1611 Func_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1613 ast_dump_context
->ostream() << this->function_
->name();
1614 if (this->closure_
!= NULL
)
1616 ast_dump_context
->ostream() << " {closure = ";
1617 this->closure_
->dump_expression(ast_dump_context
);
1618 ast_dump_context
->ostream() << "}";
1622 // Make a reference to a function in an expression.
1625 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1628 Func_expression
* fe
= new Func_expression(function
, closure
, location
);
1630 // Detect references to builtin functions and set the runtime code if
1632 if (function
->is_function_declaration())
1633 fe
->set_runtime_code(Runtime::name_to_code(function
->name()));
1637 // Class Func_descriptor_expression.
1641 Func_descriptor_expression::Func_descriptor_expression(Named_object
* fn
)
1642 : Expression(EXPRESSION_FUNC_DESCRIPTOR
, fn
->location()),
1643 fn_(fn
), dvar_(NULL
)
1645 go_assert(!fn
->is_function() || !fn
->func_value()->needs_closure());
1651 Func_descriptor_expression::do_traverse(Traverse
*)
1653 return TRAVERSE_CONTINUE
;
1656 // All function descriptors have the same type.
1658 Type
* Func_descriptor_expression::descriptor_type
;
1661 Func_descriptor_expression::make_func_descriptor_type()
1663 if (Func_descriptor_expression::descriptor_type
!= NULL
)
1665 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1666 Type
* struct_type
= Type::make_builtin_struct_type(1, "fn", uintptr_type
);
1667 Func_descriptor_expression::descriptor_type
=
1668 Type::make_builtin_named_type("functionDescriptor", struct_type
);
1672 Func_descriptor_expression::do_type()
1674 Func_descriptor_expression::make_func_descriptor_type();
1675 return Func_descriptor_expression::descriptor_type
;
1678 // The backend representation for a function descriptor.
1681 Func_descriptor_expression::do_get_backend(Translate_context
* context
)
1683 Named_object
* no
= this->fn_
;
1684 Location loc
= no
->location();
1685 if (this->dvar_
!= NULL
)
1686 return context
->backend()->var_expression(this->dvar_
, loc
);
1688 Gogo
* gogo
= context
->gogo();
1690 gogo
->function_descriptor_backend_name(no
, &bname
);
1691 bool is_descriptor
= false;
1692 if (no
->is_function_declaration()
1693 && !no
->func_declaration_value()->asm_name().empty()
1694 && Linemap::is_predeclared_location(no
->location()))
1695 is_descriptor
= true;
1697 // The runtime package implements some functions defined in the
1698 // syscall package. Let the syscall package define the descriptor
1700 if (gogo
->compiling_runtime()
1701 && gogo
->package_name() == "runtime"
1702 && no
->is_function()
1703 && !no
->func_value()->asm_name().empty()
1704 && no
->func_value()->asm_name().compare(0, 8, "syscall.") == 0)
1705 is_descriptor
= true;
1707 Btype
* btype
= this->type()->get_backend(gogo
);
1710 if (no
->package() != NULL
|| is_descriptor
)
1712 context
->backend()->immutable_struct_reference(bname
.name(),
1713 bname
.optional_asm_name(),
1717 Location bloc
= Linemap::predeclared_location();
1719 // The runtime package has hash/equality functions that are
1720 // referenced by type descriptors outside of the runtime, so the
1721 // function descriptors must be visible even though they are not
1723 bool is_exported_runtime
= false;
1724 if (gogo
->compiling_runtime()
1725 && gogo
->package_name() == "runtime"
1726 && (no
->name().find("hash") != std::string::npos
1727 || no
->name().find("equal") != std::string::npos
))
1728 is_exported_runtime
= true;
1730 bool is_hidden
= ((no
->is_function()
1731 && no
->func_value()->enclosing() != NULL
)
1732 || (Gogo::is_hidden_name(no
->name())
1733 && !is_exported_runtime
)
1734 || Gogo::is_thunk(no
));
1736 if (no
->is_function() && no
->func_value()->is_referenced_by_inline())
1739 unsigned int flags
= 0;
1741 flags
|= Backend::variable_is_hidden
;
1742 bvar
= context
->backend()->immutable_struct(bname
.name(),
1743 bname
.optional_asm_name(),
1744 flags
, btype
, bloc
);
1745 Expression_list
* vals
= new Expression_list();
1746 vals
->push_back(Expression::make_func_code_reference(this->fn_
, bloc
));
1748 Expression::make_struct_composite_literal(this->type(), vals
, bloc
);
1749 Translate_context
bcontext(gogo
, NULL
, NULL
, NULL
);
1750 bcontext
.set_is_const();
1751 Bexpression
* binit
= init
->get_backend(&bcontext
);
1752 context
->backend()->immutable_struct_set_init(bvar
, bname
.name(),
1753 flags
, btype
, bloc
, binit
);
1757 return gogo
->backend()->var_expression(bvar
, loc
);
1760 // Print a function descriptor expression.
1763 Func_descriptor_expression::do_dump_expression(Ast_dump_context
* context
) const
1765 context
->ostream() << "[descriptor " << this->fn_
->name() << "]";
1768 // Make a function descriptor expression.
1770 Func_descriptor_expression
*
1771 Expression::make_func_descriptor(Named_object
* fn
)
1773 return new Func_descriptor_expression(fn
);
1776 // Make the function descriptor type, so that it can be converted.
1779 Expression::make_func_descriptor_type()
1781 Func_descriptor_expression::make_func_descriptor_type();
1784 // A reference to just the code of a function.
1786 class Func_code_reference_expression
: public Expression
1789 Func_code_reference_expression(Named_object
* function
, Location location
)
1790 : Expression(EXPRESSION_FUNC_CODE_REFERENCE
, location
),
1796 do_traverse(Traverse
*)
1797 { return TRAVERSE_CONTINUE
; }
1800 do_is_static_initializer() const
1805 { return Type::make_pointer_type(Type::make_void_type()); }
1808 do_determine_type(const Type_context
*)
1814 return Expression::make_func_code_reference(this->function_
,
1819 do_get_backend(Translate_context
*);
1822 do_dump_expression(Ast_dump_context
* context
) const
1823 { context
->ostream() << "[raw " << this->function_
->name() << "]" ; }
1827 Named_object
* function_
;
1830 // Get the backend representation for a reference to function code.
1833 Func_code_reference_expression::do_get_backend(Translate_context
* context
)
1835 return Func_expression::get_code_pointer(context
->gogo(), this->function_
,
1839 // Make a reference to the code of a function.
1842 Expression::make_func_code_reference(Named_object
* function
, Location location
)
1844 return new Func_code_reference_expression(function
, location
);
1847 // Class Unknown_expression.
1849 // Return the name of an unknown expression.
1852 Unknown_expression::name() const
1854 return this->named_object_
->name();
1857 // Lower a reference to an unknown name.
1860 Unknown_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
1862 Location location
= this->location();
1863 Named_object
* no
= this->named_object_
;
1865 if (!no
->is_unknown())
1869 real
= no
->unknown_value()->real_named_object();
1872 if (!this->no_error_message_
)
1873 go_error_at(location
, "reference to undefined name %qs",
1874 this->named_object_
->message_name().c_str());
1875 return Expression::make_error(location
);
1878 switch (real
->classification())
1880 case Named_object::NAMED_OBJECT_CONST
:
1881 return Expression::make_const_reference(real
, location
);
1882 case Named_object::NAMED_OBJECT_TYPE
:
1883 return Expression::make_type(real
->type_value(), location
);
1884 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1885 if (!this->no_error_message_
)
1886 go_error_at(location
, "reference to undefined type %qs",
1887 real
->message_name().c_str());
1888 return Expression::make_error(location
);
1889 case Named_object::NAMED_OBJECT_VAR
:
1890 real
->var_value()->set_is_used();
1891 return Expression::make_var_reference(real
, location
);
1892 case Named_object::NAMED_OBJECT_FUNC
:
1893 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1894 return Expression::make_func_reference(real
, NULL
, location
);
1895 case Named_object::NAMED_OBJECT_PACKAGE
:
1896 if (!this->no_error_message_
)
1897 go_error_at(location
, "unexpected reference to package");
1898 return Expression::make_error(location
);
1904 // Dump the ast representation for an unknown expression to a dump context.
1907 Unknown_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1909 ast_dump_context
->ostream() << "_Unknown_(" << this->named_object_
->name()
1913 // Make a reference to an unknown name.
1916 Expression::make_unknown_reference(Named_object
* no
, Location location
)
1918 return new Unknown_expression(no
, location
);
1921 // Start exporting a type conversion for a constant, if needed. This
1922 // returns whether we need to export a closing parenthesis.
1925 Expression::export_constant_type(Export_function_body
* efb
, Type
* type
)
1928 || type
->is_abstract()
1929 || type
== efb
->type_context())
1931 efb
->write_c_string("$convert(");
1932 efb
->write_type(type
);
1933 efb
->write_c_string(", ");
1937 // Finish a type conversion for a constant.
1940 Expression::finish_export_constant_type(Export_function_body
* efb
, bool needed
)
1943 efb
->write_c_string(")");
1946 // A boolean expression.
1948 class Boolean_expression
: public Expression
1951 Boolean_expression(bool val
, Location location
)
1952 : Expression(EXPRESSION_BOOLEAN
, location
),
1953 val_(val
), type_(NULL
)
1957 do_import(Import_expression
*, Location
);
1961 do_traverse(Traverse
*);
1964 do_is_constant() const
1968 do_is_zero_value() const
1969 { return this->val_
== false; }
1972 do_boolean_constant_value(bool* val
) const
1979 do_is_static_initializer() const
1986 do_determine_type(const Type_context
*);
1993 do_get_backend(Translate_context
* context
)
1994 { return context
->backend()->boolean_constant_expression(this->val_
); }
1997 do_inlining_cost() const
2001 do_export(Export_function_body
* efb
) const;
2004 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2005 { ast_dump_context
->ostream() << (this->val_
? "true" : "false"); }
2010 // The type as determined by context.
2014 // Traverse a boolean expression. We just need to traverse the type
2018 Boolean_expression::do_traverse(Traverse
* traverse
)
2020 if (this->type_
!= NULL
)
2021 return Type::traverse(this->type_
, traverse
);
2022 return TRAVERSE_CONTINUE
;
2028 Boolean_expression::do_type()
2030 if (this->type_
== NULL
)
2031 this->type_
= Type::make_boolean_type();
2035 // Set the type from the context.
2038 Boolean_expression::do_determine_type(const Type_context
* context
)
2040 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2042 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
2043 this->type_
= context
->type
;
2044 else if (!context
->may_be_abstract
)
2045 this->type_
= Type::lookup_bool_type();
2048 // Export a boolean constant.
2051 Boolean_expression::do_export(Export_function_body
* efb
) const
2053 bool exported_type
= Expression::export_constant_type(efb
, this->type_
);
2054 efb
->write_c_string(this->val_
? "$true" : "$false");
2055 Expression::finish_export_constant_type(efb
, exported_type
);
2058 // Import a boolean constant.
2061 Boolean_expression::do_import(Import_expression
* imp
, Location loc
)
2063 if (imp
->version() >= EXPORT_FORMAT_V3
)
2064 imp
->require_c_string("$");
2065 if (imp
->peek_char() == 't')
2067 imp
->require_c_string("true");
2068 return Expression::make_boolean(true, loc
);
2072 imp
->require_c_string("false");
2073 return Expression::make_boolean(false, loc
);
2077 // Make a boolean expression.
2080 Expression::make_boolean(bool val
, Location location
)
2082 return new Boolean_expression(val
, location
);
2085 // Class String_expression.
2087 // Traverse a string expression. We just need to traverse the type
2091 String_expression::do_traverse(Traverse
* traverse
)
2093 if (this->type_
!= NULL
)
2094 return Type::traverse(this->type_
, traverse
);
2095 return TRAVERSE_CONTINUE
;
2101 String_expression::do_type()
2103 if (this->type_
== NULL
)
2104 this->type_
= Type::make_string_type();
2108 // Set the type from the context.
2111 String_expression::do_determine_type(const Type_context
* context
)
2113 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2115 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
2116 this->type_
= context
->type
;
2117 else if (!context
->may_be_abstract
)
2118 this->type_
= Type::lookup_string_type();
2121 // Build a string constant.
2124 String_expression::do_get_backend(Translate_context
* context
)
2126 Gogo
* gogo
= context
->gogo();
2127 Btype
* btype
= Type::make_string_type()->get_backend(gogo
);
2129 Location loc
= this->location();
2130 std::vector
<Bexpression
*> init(2);
2132 if (this->val_
.size() == 0)
2133 init
[0] = gogo
->backend()->nil_pointer_expression();
2136 Bexpression
* str_cst
=
2137 gogo
->backend()->string_constant_expression(this->val_
);
2138 init
[0] = gogo
->backend()->address_expression(str_cst
, loc
);
2141 Btype
* int_btype
= Type::lookup_integer_type("int")->get_backend(gogo
);
2143 mpz_init_set_ui(lenval
, this->val_
.length());
2144 init
[1] = gogo
->backend()->integer_constant_expression(int_btype
, lenval
);
2147 return gogo
->backend()->constructor_expression(btype
, init
, loc
);
2150 // Write string literal to string dump.
2153 String_expression::export_string(String_dump
* exp
,
2154 const String_expression
* str
)
2157 s
.reserve(str
->val_
.length() * 4 + 2);
2159 for (std::string::const_iterator p
= str
->val_
.begin();
2160 p
!= str
->val_
.end();
2163 if (*p
== '\\' || *p
== '"')
2168 else if (*p
>= 0x20 && *p
< 0x7f)
2170 else if (*p
== '\n')
2172 else if (*p
== '\t')
2177 unsigned char c
= *p
;
2178 unsigned int dig
= c
>> 4;
2179 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
2181 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
2185 exp
->write_string(s
);
2188 // Export a string expression.
2191 String_expression::do_export(Export_function_body
* efb
) const
2193 bool exported_type
= Expression::export_constant_type(efb
, this->type_
);
2194 String_expression::export_string(efb
, this);
2195 Expression::finish_export_constant_type(efb
, exported_type
);
2198 // Import a string expression.
2201 String_expression::do_import(Import_expression
* imp
, Location loc
)
2203 imp
->require_c_string("\"");
2207 int c
= imp
->get_char();
2208 if (c
== '"' || c
== -1)
2211 val
+= static_cast<char>(c
);
2214 c
= imp
->get_char();
2215 if (c
== '\\' || c
== '"')
2216 val
+= static_cast<char>(c
);
2223 c
= imp
->get_char();
2224 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
2225 c
= imp
->get_char();
2226 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
2227 char v
= (vh
<< 4) | vl
;
2232 go_error_at(imp
->location(), "bad string constant");
2233 return Expression::make_error(loc
);
2237 return Expression::make_string(val
, loc
);
2240 // Ast dump for string expression.
2243 String_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2245 String_expression::export_string(ast_dump_context
, this);
2248 // Make a string expression with abstract string type (common case).
2251 Expression::make_string(const std::string
& val
, Location location
)
2253 return new String_expression(val
, NULL
, location
);
2256 // Make a string expression with a specific string type.
2259 Expression::make_string_typed(const std::string
& val
, Type
* type
, Location location
)
2261 return new String_expression(val
, type
, location
);
2264 // An expression that evaluates to some characteristic of a string.
2265 // This is used when indexing, bound-checking, or nil checking a string.
2267 class String_info_expression
: public Expression
2270 String_info_expression(Expression
* string
, String_info string_info
,
2272 : Expression(EXPRESSION_STRING_INFO
, location
),
2273 string_(string
), string_info_(string_info
)
2281 do_determine_type(const Type_context
*)
2282 { go_unreachable(); }
2287 return new String_info_expression(this->string_
->copy(), this->string_info_
,
2292 do_get_backend(Translate_context
* context
);
2295 do_dump_expression(Ast_dump_context
*) const;
2298 do_issue_nil_check()
2299 { this->string_
->issue_nil_check(); }
2302 // The string for which we are getting information.
2303 Expression
* string_
;
2304 // What information we want.
2305 String_info string_info_
;
2308 // Return the type of the string info.
2311 String_info_expression::do_type()
2313 switch (this->string_info_
)
2315 case STRING_INFO_DATA
:
2317 Type
* byte_type
= Type::lookup_integer_type("uint8");
2318 return Type::make_pointer_type(byte_type
);
2320 case STRING_INFO_LENGTH
:
2321 return Type::lookup_integer_type("int");
2327 // Return string information in GENERIC.
2330 String_info_expression::do_get_backend(Translate_context
* context
)
2332 Gogo
* gogo
= context
->gogo();
2334 Bexpression
* bstring
= this->string_
->get_backend(context
);
2335 switch (this->string_info_
)
2337 case STRING_INFO_DATA
:
2338 case STRING_INFO_LENGTH
:
2339 return gogo
->backend()->struct_field_expression(bstring
,
2348 // Dump ast representation for a type info expression.
2351 String_info_expression::do_dump_expression(
2352 Ast_dump_context
* ast_dump_context
) const
2354 ast_dump_context
->ostream() << "stringinfo(";
2355 this->string_
->dump_expression(ast_dump_context
);
2356 ast_dump_context
->ostream() << ",";
2357 ast_dump_context
->ostream() <<
2358 (this->string_info_
== STRING_INFO_DATA
? "data"
2359 : this->string_info_
== STRING_INFO_LENGTH
? "length"
2361 ast_dump_context
->ostream() << ")";
2364 // Make a string info expression.
2367 Expression::make_string_info(Expression
* string
, String_info string_info
,
2370 return new String_info_expression(string
, string_info
, location
);
2373 // An expression that represents an string value: a struct with value pointer
2374 // and length fields.
2376 class String_value_expression
: public Expression
2379 String_value_expression(Expression
* valptr
, Expression
* len
, Location location
)
2380 : Expression(EXPRESSION_STRING_VALUE
, location
),
2381 valptr_(valptr
), len_(len
)
2386 do_traverse(Traverse
*);
2390 { return Type::make_string_type(); }
2393 do_determine_type(const Type_context
*)
2394 { go_unreachable(); }
2399 return new String_value_expression(this->valptr_
->copy(),
2405 do_get_backend(Translate_context
* context
);
2408 do_dump_expression(Ast_dump_context
*) const;
2411 // The value pointer.
2412 Expression
* valptr_
;
2418 String_value_expression::do_traverse(Traverse
* traverse
)
2420 if (Expression::traverse(&this->valptr_
, traverse
) == TRAVERSE_EXIT
2421 || Expression::traverse(&this->len_
, traverse
) == TRAVERSE_EXIT
)
2422 return TRAVERSE_EXIT
;
2423 return TRAVERSE_CONTINUE
;
2427 String_value_expression::do_get_backend(Translate_context
* context
)
2429 std::vector
<Bexpression
*> vals(2);
2430 vals
[0] = this->valptr_
->get_backend(context
);
2431 vals
[1] = this->len_
->get_backend(context
);
2433 Gogo
* gogo
= context
->gogo();
2434 Btype
* btype
= Type::make_string_type()->get_backend(gogo
);
2435 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
2439 String_value_expression::do_dump_expression(
2440 Ast_dump_context
* ast_dump_context
) const
2442 ast_dump_context
->ostream() << "stringvalue(";
2443 ast_dump_context
->ostream() << "value: ";
2444 this->valptr_
->dump_expression(ast_dump_context
);
2445 ast_dump_context
->ostream() << ", length: ";
2446 this->len_
->dump_expression(ast_dump_context
);
2447 ast_dump_context
->ostream() << ")";
2451 Expression::make_string_value(Expression
* valptr
, Expression
* len
,
2454 return new String_value_expression(valptr
, len
, location
);
2457 // Make an integer expression.
2459 class Integer_expression
: public Expression
2462 Integer_expression(const mpz_t
* val
, Type
* type
, bool is_character_constant
,
2464 : Expression(EXPRESSION_INTEGER
, location
),
2465 type_(type
), is_character_constant_(is_character_constant
)
2466 { mpz_init_set(this->val_
, *val
); }
2469 do_import(Import_expression
*, Location
);
2471 // Write VAL to string dump.
2473 export_integer(String_dump
* exp
, const mpz_t val
);
2475 // Write VAL to dump context.
2477 dump_integer(Ast_dump_context
* ast_dump_context
, const mpz_t val
);
2481 do_traverse(Traverse
*);
2484 do_is_constant() const
2488 do_is_zero_value() const
2489 { return mpz_sgn(this->val_
) == 0; }
2492 do_is_static_initializer() const
2496 do_numeric_constant_value(Numeric_constant
* nc
) const;
2502 do_determine_type(const Type_context
* context
);
2505 do_check_types(Gogo
*);
2508 do_get_backend(Translate_context
*);
2513 if (this->is_character_constant_
)
2514 return Expression::make_character(&this->val_
,
2515 (this->type_
== NULL
2517 : this->type_
->copy_expressions()),
2520 return Expression::make_integer_z(&this->val_
,
2521 (this->type_
== NULL
2523 : this->type_
->copy_expressions()),
2528 do_inlining_cost() const
2532 do_export(Export_function_body
*) const;
2535 do_dump_expression(Ast_dump_context
*) const;
2538 // The integer value.
2542 // Whether this is a character constant.
2543 bool is_character_constant_
;
2546 // Traverse an integer expression. We just need to traverse the type
2550 Integer_expression::do_traverse(Traverse
* traverse
)
2552 if (this->type_
!= NULL
)
2553 return Type::traverse(this->type_
, traverse
);
2554 return TRAVERSE_CONTINUE
;
2557 // Return a numeric constant for this expression. We have to mark
2558 // this as a character when appropriate.
2561 Integer_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
2563 if (this->is_character_constant_
)
2564 nc
->set_rune(this->type_
, this->val_
);
2566 nc
->set_int(this->type_
, this->val_
);
2570 // Return the current type. If we haven't set the type yet, we return
2571 // an abstract integer type.
2574 Integer_expression::do_type()
2576 if (this->type_
== NULL
)
2578 if (this->is_character_constant_
)
2579 this->type_
= Type::make_abstract_character_type();
2581 this->type_
= Type::make_abstract_integer_type();
2586 // Set the type of the integer value. Here we may switch from an
2587 // abstract type to a real type.
2590 Integer_expression::do_determine_type(const Type_context
* context
)
2592 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2594 else if (context
->type
!= NULL
&& context
->type
->is_numeric_type())
2595 this->type_
= context
->type
;
2596 else if (!context
->may_be_abstract
)
2598 if (this->is_character_constant_
)
2599 this->type_
= Type::lookup_integer_type("int32");
2601 this->type_
= Type::lookup_integer_type("int");
2605 // Check the type of an integer constant.
2608 Integer_expression::do_check_types(Gogo
*)
2610 Type
* type
= this->type_
;
2613 Numeric_constant nc
;
2614 if (this->is_character_constant_
)
2615 nc
.set_rune(NULL
, this->val_
);
2617 nc
.set_int(NULL
, this->val_
);
2618 if (!nc
.set_type(type
, true, this->location()))
2619 this->set_is_error();
2622 // Get the backend representation for an integer constant.
2625 Integer_expression::do_get_backend(Translate_context
* context
)
2627 if (this->is_error_expression()
2628 || (this->type_
!= NULL
&& this->type_
->is_error_type()))
2630 go_assert(saw_errors());
2631 return context
->gogo()->backend()->error_expression();
2634 Type
* resolved_type
= NULL
;
2635 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2636 resolved_type
= this->type_
;
2637 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
2639 // We are converting to an abstract floating point type.
2640 resolved_type
= Type::lookup_float_type("float64");
2642 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
2644 // We are converting to an abstract complex type.
2645 resolved_type
= Type::lookup_complex_type("complex128");
2649 // If we still have an abstract type here, then this is being
2650 // used in a constant expression which didn't get reduced for
2651 // some reason. Use a type which will fit the value. We use <,
2652 // not <=, because we need an extra bit for the sign bit.
2653 int bits
= mpz_sizeinbase(this->val_
, 2);
2654 Type
* int_type
= Type::lookup_integer_type("int");
2655 if (bits
< int_type
->integer_type()->bits())
2656 resolved_type
= int_type
;
2658 resolved_type
= Type::lookup_integer_type("int64");
2662 go_error_at(this->location(),
2663 "unknown type for large integer constant");
2664 return context
->gogo()->backend()->error_expression();
2667 Numeric_constant nc
;
2668 nc
.set_int(resolved_type
, this->val_
);
2669 return Expression::backend_numeric_constant_expression(context
, &nc
);
2672 // Write VAL to export data.
2675 Integer_expression::export_integer(String_dump
* exp
, const mpz_t val
)
2677 char* s
= mpz_get_str(NULL
, 10, val
);
2678 exp
->write_c_string(s
);
2682 // Export an integer in a constant expression.
2685 Integer_expression::do_export(Export_function_body
* efb
) const
2687 bool exported_type
= Expression::export_constant_type(efb
, this->type_
);
2689 Integer_expression::export_integer(efb
, this->val_
);
2690 if (this->is_character_constant_
)
2691 efb
->write_c_string("'");
2692 // A trailing space lets us reliably identify the end of the number.
2693 efb
->write_c_string(" ");
2695 Expression::finish_export_constant_type(efb
, exported_type
);
2698 // Import an integer, floating point, or complex value. This handles
2699 // all these types because they all start with digits.
2702 Integer_expression::do_import(Import_expression
* imp
, Location loc
)
2704 std::string num
= imp
->read_identifier();
2705 imp
->require_c_string(" ");
2706 if (!num
.empty() && num
[num
.length() - 1] == 'i')
2709 size_t plus_pos
= num
.find('+', 1);
2710 size_t minus_pos
= num
.find('-', 1);
2712 if (plus_pos
== std::string::npos
)
2714 else if (minus_pos
== std::string::npos
)
2718 go_error_at(imp
->location(), "bad number in import data: %qs",
2720 return Expression::make_error(loc
);
2722 if (pos
== std::string::npos
)
2723 mpfr_init_set_ui(real
, 0, MPFR_RNDN
);
2726 std::string real_str
= num
.substr(0, pos
);
2727 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, MPFR_RNDN
) != 0)
2729 go_error_at(imp
->location(), "bad number in import data: %qs",
2731 return Expression::make_error(loc
);
2735 std::string imag_str
;
2736 if (pos
== std::string::npos
)
2739 imag_str
= num
.substr(pos
);
2740 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
2742 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, MPFR_RNDN
) != 0)
2744 go_error_at(imp
->location(), "bad number in import data: %qs",
2746 return Expression::make_error(loc
);
2749 mpc_init2(cval
, mpc_precision
);
2750 mpc_set_fr_fr(cval
, real
, imag
, MPC_RNDNN
);
2753 Expression
* ret
= Expression::make_complex(&cval
, NULL
, loc
);
2757 else if (num
.find('.') == std::string::npos
2758 && num
.find('E') == std::string::npos
)
2760 bool is_character_constant
= (!num
.empty()
2761 && num
[num
.length() - 1] == '\'');
2762 if (is_character_constant
)
2763 num
= num
.substr(0, num
.length() - 1);
2765 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
2767 go_error_at(imp
->location(), "bad number in import data: %qs",
2769 return Expression::make_error(loc
);
2772 if (is_character_constant
)
2773 ret
= Expression::make_character(&val
, NULL
, loc
);
2775 ret
= Expression::make_integer_z(&val
, NULL
, loc
);
2782 if (mpfr_init_set_str(val
, num
.c_str(), 10, MPFR_RNDN
) != 0)
2784 go_error_at(imp
->location(), "bad number in import data: %qs",
2786 return Expression::make_error(loc
);
2788 Expression
* ret
= Expression::make_float(&val
, NULL
, loc
);
2793 // Ast dump for integer expression.
2796 Integer_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2798 if (this->is_character_constant_
)
2799 ast_dump_context
->ostream() << '\'';
2800 Integer_expression::export_integer(ast_dump_context
, this->val_
);
2801 if (this->is_character_constant_
)
2802 ast_dump_context
->ostream() << '\'';
2805 // Build a new integer value from a multi-precision integer.
2808 Expression::make_integer_z(const mpz_t
* val
, Type
* type
, Location location
)
2810 return new Integer_expression(val
, type
, false, location
);
2813 // Build a new integer value from an unsigned long.
2816 Expression::make_integer_ul(unsigned long val
, Type
*type
, Location location
)
2819 mpz_init_set_ui(zval
, val
);
2820 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2825 // Build a new integer value from a signed long.
2828 Expression::make_integer_sl(long val
, Type
*type
, Location location
)
2831 mpz_init_set_si(zval
, val
);
2832 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2837 // Store an int64_t in an uninitialized mpz_t.
2840 set_mpz_from_int64(mpz_t
* zval
, int64_t val
)
2844 unsigned long ul
= static_cast<unsigned long>(val
);
2845 if (static_cast<int64_t>(ul
) == val
)
2847 mpz_init_set_ui(*zval
, ul
);
2853 uv
= static_cast<uint64_t>(val
);
2855 uv
= static_cast<uint64_t>(- val
);
2856 unsigned long ul
= uv
& 0xffffffffUL
;
2857 mpz_init_set_ui(*zval
, ul
);
2859 mpz_init_set_ui(hval
, static_cast<unsigned long>(uv
>> 32));
2860 mpz_mul_2exp(hval
, hval
, 32);
2861 mpz_add(*zval
, *zval
, hval
);
2864 mpz_neg(*zval
, *zval
);
2867 // Build a new integer value from an int64_t.
2870 Expression::make_integer_int64(int64_t val
, Type
* type
, Location location
)
2873 set_mpz_from_int64(&zval
, val
);
2874 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2879 // Build a new character constant value.
2882 Expression::make_character(const mpz_t
* val
, Type
* type
, Location location
)
2884 return new Integer_expression(val
, type
, true, location
);
2889 class Float_expression
: public Expression
2892 Float_expression(const mpfr_t
* val
, Type
* type
, Location location
)
2893 : Expression(EXPRESSION_FLOAT
, location
),
2896 mpfr_init_set(this->val_
, *val
, MPFR_RNDN
);
2899 // Write VAL to export data.
2901 export_float(String_dump
* exp
, const mpfr_t val
);
2903 // Write VAL to dump file.
2905 dump_float(Ast_dump_context
* ast_dump_context
, const mpfr_t val
);
2909 do_traverse(Traverse
*);
2912 do_is_constant() const
2916 do_is_zero_value() const
2918 return mpfr_zero_p(this->val_
) != 0
2919 && mpfr_signbit(this->val_
) == 0;
2923 do_is_static_initializer() const
2927 do_numeric_constant_value(Numeric_constant
* nc
) const
2929 nc
->set_float(this->type_
, this->val_
);
2937 do_determine_type(const Type_context
*);
2940 do_check_types(Gogo
*);
2944 { return Expression::make_float(&this->val_
,
2945 (this->type_
== NULL
2947 : this->type_
->copy_expressions()),
2948 this->location()); }
2951 do_get_backend(Translate_context
*);
2954 do_inlining_cost() const
2958 do_export(Export_function_body
*) const;
2961 do_dump_expression(Ast_dump_context
*) const;
2964 // The floating point value.
2970 // Traverse a float expression. We just need to traverse the type if
2974 Float_expression::do_traverse(Traverse
* traverse
)
2976 if (this->type_
!= NULL
)
2977 return Type::traverse(this->type_
, traverse
);
2978 return TRAVERSE_CONTINUE
;
2981 // Return the current type. If we haven't set the type yet, we return
2982 // an abstract float type.
2985 Float_expression::do_type()
2987 if (this->type_
== NULL
)
2988 this->type_
= Type::make_abstract_float_type();
2992 // Set the type of the float value. Here we may switch from an
2993 // abstract type to a real type.
2996 Float_expression::do_determine_type(const Type_context
* context
)
2998 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
3000 else if (context
->type
!= NULL
3001 && (context
->type
->integer_type() != NULL
3002 || context
->type
->float_type() != NULL
3003 || context
->type
->complex_type() != NULL
))
3004 this->type_
= context
->type
;
3005 else if (!context
->may_be_abstract
)
3006 this->type_
= Type::lookup_float_type("float64");
3009 // Check the type of a float value.
3012 Float_expression::do_check_types(Gogo
*)
3014 Type
* type
= this->type_
;
3017 Numeric_constant nc
;
3018 nc
.set_float(NULL
, this->val_
);
3019 if (!nc
.set_type(this->type_
, true, this->location()))
3020 this->set_is_error();
3023 // Get the backend representation for a float constant.
3026 Float_expression::do_get_backend(Translate_context
* context
)
3028 if (this->is_error_expression()
3029 || (this->type_
!= NULL
&& this->type_
->is_error_type()))
3031 go_assert(saw_errors());
3032 return context
->gogo()->backend()->error_expression();
3035 Type
* resolved_type
;
3036 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
3037 resolved_type
= this->type_
;
3038 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
3040 // We have an abstract integer type. We just hope for the best.
3041 resolved_type
= Type::lookup_integer_type("int");
3043 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
3045 // We are converting to an abstract complex type.
3046 resolved_type
= Type::lookup_complex_type("complex128");
3050 // If we still have an abstract type here, then this is being
3051 // used in a constant expression which didn't get reduced. We
3052 // just use float64 and hope for the best.
3053 resolved_type
= Type::lookup_float_type("float64");
3056 Numeric_constant nc
;
3057 nc
.set_float(resolved_type
, this->val_
);
3058 return Expression::backend_numeric_constant_expression(context
, &nc
);
3061 // Write a floating point number to a string dump.
3064 Float_expression::export_float(String_dump
*exp
, const mpfr_t val
)
3066 mpfr_exp_t exponent
;
3067 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, MPFR_RNDN
);
3069 exp
->write_c_string("-");
3070 exp
->write_c_string("0.");
3071 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
3074 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
3075 exp
->write_c_string(buf
);
3078 // Export a floating point number in a constant expression.
3081 Float_expression::do_export(Export_function_body
* efb
) const
3083 bool exported_type
= Expression::export_constant_type(efb
, this->type_
);
3085 Float_expression::export_float(efb
, this->val_
);
3086 // A trailing space lets us reliably identify the end of the number.
3087 efb
->write_c_string(" ");
3089 Expression::finish_export_constant_type(efb
, exported_type
);
3092 // Dump a floating point number to the dump file.
3095 Float_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3097 Float_expression::export_float(ast_dump_context
, this->val_
);
3100 // Make a float expression.
3103 Expression::make_float(const mpfr_t
* val
, Type
* type
, Location location
)
3105 return new Float_expression(val
, type
, location
);
3110 class Complex_expression
: public Expression
3113 Complex_expression(const mpc_t
* val
, Type
* type
, Location location
)
3114 : Expression(EXPRESSION_COMPLEX
, location
),
3117 mpc_init2(this->val_
, mpc_precision
);
3118 mpc_set(this->val_
, *val
, MPC_RNDNN
);
3121 // Write VAL to string dump.
3123 export_complex(String_dump
* exp
, const mpc_t val
);
3125 // Write REAL/IMAG to dump context.
3127 dump_complex(Ast_dump_context
* ast_dump_context
, const mpc_t val
);
3131 do_traverse(Traverse
*);
3134 do_is_constant() const
3138 do_is_zero_value() const
3140 return mpfr_zero_p(mpc_realref(this->val_
)) != 0
3141 && mpfr_signbit(mpc_realref(this->val_
)) == 0
3142 && mpfr_zero_p(mpc_imagref(this->val_
)) != 0
3143 && mpfr_signbit(mpc_imagref(this->val_
)) == 0;
3147 do_is_static_initializer() const
3151 do_numeric_constant_value(Numeric_constant
* nc
) const
3153 nc
->set_complex(this->type_
, this->val_
);
3161 do_determine_type(const Type_context
*);
3164 do_check_types(Gogo
*);
3169 return Expression::make_complex(&this->val_
,
3170 (this->type_
== NULL
3172 : this->type_
->copy_expressions()),
3177 do_get_backend(Translate_context
*);
3180 do_inlining_cost() const
3184 do_export(Export_function_body
*) const;
3187 do_dump_expression(Ast_dump_context
*) const;
3190 // The complex value.
3192 // The type if known.
3196 // Traverse a complex expression. We just need to traverse the type
3200 Complex_expression::do_traverse(Traverse
* traverse
)
3202 if (this->type_
!= NULL
)
3203 return Type::traverse(this->type_
, traverse
);
3204 return TRAVERSE_CONTINUE
;
3207 // Return the current type. If we haven't set the type yet, we return
3208 // an abstract complex type.
3211 Complex_expression::do_type()
3213 if (this->type_
== NULL
)
3214 this->type_
= Type::make_abstract_complex_type();
3218 // Set the type of the complex value. Here we may switch from an
3219 // abstract type to a real type.
3222 Complex_expression::do_determine_type(const Type_context
* context
)
3224 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
3226 else if (context
->type
!= NULL
&& context
->type
->is_numeric_type())
3227 this->type_
= context
->type
;
3228 else if (!context
->may_be_abstract
)
3229 this->type_
= Type::lookup_complex_type("complex128");
3232 // Check the type of a complex value.
3235 Complex_expression::do_check_types(Gogo
*)
3237 Type
* type
= this->type_
;
3240 Numeric_constant nc
;
3241 nc
.set_complex(NULL
, this->val_
);
3242 if (!nc
.set_type(this->type_
, true, this->location()))
3243 this->set_is_error();
3246 // Get the backend representation for a complex constant.
3249 Complex_expression::do_get_backend(Translate_context
* context
)
3251 if (this->is_error_expression()
3252 || (this->type_
!= NULL
&& this->type_
->is_error_type()))
3254 go_assert(saw_errors());
3255 return context
->gogo()->backend()->error_expression();
3258 Type
* resolved_type
;
3259 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
3260 resolved_type
= this->type_
;
3261 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
3263 // We are converting to an abstract integer type.
3264 resolved_type
= Type::lookup_integer_type("int");
3266 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
3268 // We are converting to an abstract float type.
3269 resolved_type
= Type::lookup_float_type("float64");
3273 // If we still have an abstract type here, this is being
3274 // used in a constant expression which didn't get reduced. We
3275 // just use complex128 and hope for the best.
3276 resolved_type
= Type::lookup_complex_type("complex128");
3279 Numeric_constant nc
;
3280 nc
.set_complex(resolved_type
, this->val_
);
3281 return Expression::backend_numeric_constant_expression(context
, &nc
);
3284 // Write REAL/IMAG to export data.
3287 Complex_expression::export_complex(String_dump
* exp
, const mpc_t val
)
3289 if (!mpfr_zero_p(mpc_realref(val
)))
3291 Float_expression::export_float(exp
, mpc_realref(val
));
3292 if (mpfr_sgn(mpc_imagref(val
)) >= 0)
3293 exp
->write_c_string("+");
3295 Float_expression::export_float(exp
, mpc_imagref(val
));
3296 exp
->write_c_string("i");
3299 // Export a complex number in a constant expression.
3302 Complex_expression::do_export(Export_function_body
* efb
) const
3304 bool exported_type
= Expression::export_constant_type(efb
, this->type_
);
3306 Complex_expression::export_complex(efb
, this->val_
);
3307 // A trailing space lets us reliably identify the end of the number.
3308 efb
->write_c_string(" ");
3310 Expression::finish_export_constant_type(efb
, exported_type
);
3313 // Dump a complex expression to the dump file.
3316 Complex_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3318 Complex_expression::export_complex(ast_dump_context
, this->val_
);
3321 // Make a complex expression.
3324 Expression::make_complex(const mpc_t
* val
, Type
* type
, Location location
)
3326 return new Complex_expression(val
, type
, location
);
3329 // Find a named object in an expression.
3331 class Find_named_object
: public Traverse
3334 Find_named_object(Named_object
* no
)
3335 : Traverse(traverse_expressions
),
3336 no_(no
), found_(false)
3339 // Whether we found the object.
3342 { return this->found_
; }
3346 expression(Expression
**);
3349 // The object we are looking for.
3351 // Whether we found it.
3355 // A reference to a const in an expression.
3357 class Const_expression
: public Expression
3360 Const_expression(Named_object
* constant
, Location location
)
3361 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
3362 constant_(constant
), type_(NULL
), seen_(false)
3367 { return this->constant_
; }
3370 named_object() const
3371 { return this->constant_
; }
3373 // Check that the initializer does not refer to the constant itself.
3375 check_for_init_loop();
3379 do_traverse(Traverse
*);
3382 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
3385 do_is_constant() const
3389 do_is_zero_value() const
3390 { return this->constant_
->const_value()->expr()->is_zero_value(); }
3393 do_is_static_initializer() const
3397 do_numeric_constant_value(Numeric_constant
* nc
) const;
3400 do_string_constant_value(std::string
* val
) const;
3403 do_boolean_constant_value(bool* val
) const;
3408 // The type of a const is set by the declaration, not the use.
3410 do_determine_type(const Type_context
*);
3413 do_check_types(Gogo
*);
3420 do_get_backend(Translate_context
* context
);
3423 do_inlining_cost() const
3426 // When exporting a reference to a const as part of a const
3427 // expression, we export the value. We ignore the fact that it has
3430 do_export(Export_function_body
* efb
) const
3431 { this->constant_
->const_value()->expr()->export_expression(efb
); }
3434 do_dump_expression(Ast_dump_context
*) const;
3438 Named_object
* constant_
;
3439 // The type of this reference. This is used if the constant has an
3442 // Used to prevent infinite recursion when a constant incorrectly
3443 // refers to itself.
3450 Const_expression::do_traverse(Traverse
* traverse
)
3452 if (this->type_
!= NULL
)
3453 return Type::traverse(this->type_
, traverse
);
3454 return TRAVERSE_CONTINUE
;
3457 // Lower a constant expression. This is where we convert the
3458 // predeclared constant iota into an integer value.
3461 Const_expression::do_lower(Gogo
* gogo
, Named_object
*,
3462 Statement_inserter
*, int iota_value
)
3464 if (this->constant_
->const_value()->expr()->classification()
3467 if (iota_value
== -1)
3469 go_error_at(this->location(),
3470 "iota is only defined in const declarations");
3473 return Expression::make_integer_ul(iota_value
, NULL
, this->location());
3476 // Make sure that the constant itself has been lowered.
3477 gogo
->lower_constant(this->constant_
);
3482 // Return a numeric constant value.
3485 Const_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
3490 Expression
* e
= this->constant_
->const_value()->expr();
3494 bool r
= e
->numeric_constant_value(nc
);
3496 this->seen_
= false;
3499 if (this->type_
!= NULL
)
3500 ctype
= this->type_
;
3502 ctype
= this->constant_
->const_value()->type();
3503 if (r
&& ctype
!= NULL
)
3505 if (!nc
->set_type(ctype
, false, this->location()))
3513 Const_expression::do_string_constant_value(std::string
* val
) const
3518 Expression
* e
= this->constant_
->const_value()->expr();
3521 bool ok
= e
->string_constant_value(val
);
3522 this->seen_
= false;
3528 Const_expression::do_boolean_constant_value(bool* val
) const
3533 Expression
* e
= this->constant_
->const_value()->expr();
3536 bool ok
= e
->boolean_constant_value(val
);
3537 this->seen_
= false;
3542 // Return the type of the const reference.
3545 Const_expression::do_type()
3547 if (this->type_
!= NULL
)
3550 Named_constant
* nc
= this->constant_
->const_value();
3552 if (this->seen_
|| nc
->lowering())
3554 if (nc
->type() == NULL
|| !nc
->type()->is_error_type())
3556 Location loc
= this->location();
3558 loc
= nc
->location();
3559 go_error_at(loc
, "constant refers to itself");
3561 this->set_is_error();
3562 this->type_
= Type::make_error_type();
3563 nc
->set_type(this->type_
);
3569 Type
* ret
= nc
->type();
3573 this->seen_
= false;
3577 // During parsing, a named constant may have a NULL type, but we
3578 // must not return a NULL type here.
3579 ret
= nc
->expr()->type();
3581 this->seen_
= false;
3583 if (ret
->is_error_type())
3589 // Set the type of the const reference.
3592 Const_expression::do_determine_type(const Type_context
* context
)
3594 Type
* ctype
= this->constant_
->const_value()->type();
3595 Type
* cetype
= (ctype
!= NULL
3597 : this->constant_
->const_value()->expr()->type());
3598 if (ctype
!= NULL
&& !ctype
->is_abstract())
3600 else if (context
->type
!= NULL
3601 && context
->type
->is_numeric_type()
3602 && cetype
->is_numeric_type())
3603 this->type_
= context
->type
;
3604 else if (context
->type
!= NULL
3605 && context
->type
->is_string_type()
3606 && cetype
->is_string_type())
3607 this->type_
= context
->type
;
3608 else if (context
->type
!= NULL
3609 && context
->type
->is_boolean_type()
3610 && cetype
->is_boolean_type())
3611 this->type_
= context
->type
;
3612 else if (!context
->may_be_abstract
)
3614 if (cetype
->is_abstract())
3615 cetype
= cetype
->make_non_abstract_type();
3616 this->type_
= cetype
;
3620 // Check for a loop in which the initializer of a constant refers to
3621 // the constant itself.
3624 Const_expression::check_for_init_loop()
3626 if (this->type_
!= NULL
&& this->type_
->is_error())
3631 this->report_error(_("constant refers to itself"));
3632 this->type_
= Type::make_error_type();
3636 Expression
* init
= this->constant_
->const_value()->expr();
3637 Find_named_object
find_named_object(this->constant_
);
3640 Expression::traverse(&init
, &find_named_object
);
3641 this->seen_
= false;
3643 if (find_named_object
.found())
3645 if (this->type_
== NULL
|| !this->type_
->is_error())
3647 this->report_error(_("constant refers to itself"));
3648 this->type_
= Type::make_error_type();
3654 // Check types of a const reference.
3657 Const_expression::do_check_types(Gogo
*)
3659 if (this->type_
!= NULL
&& this->type_
->is_error())
3662 this->check_for_init_loop();
3664 // Check that numeric constant fits in type.
3665 if (this->type_
!= NULL
&& this->type_
->is_numeric_type())
3667 Numeric_constant nc
;
3668 if (this->constant_
->const_value()->expr()->numeric_constant_value(&nc
))
3670 if (!nc
.set_type(this->type_
, true, this->location()))
3671 this->set_is_error();
3676 // Return the backend representation for a const reference.
3679 Const_expression::do_get_backend(Translate_context
* context
)
3681 if (this->is_error_expression()
3682 || (this->type_
!= NULL
&& this->type_
->is_error()))
3684 go_assert(saw_errors());
3685 return context
->backend()->error_expression();
3688 // If the type has been set for this expression, but the underlying
3689 // object is an abstract int or float, we try to get the abstract
3690 // value. Otherwise we may lose something in the conversion.
3691 Expression
* expr
= this->constant_
->const_value()->expr();
3692 if (this->type_
!= NULL
3693 && this->type_
->is_numeric_type()
3694 && (this->constant_
->const_value()->type() == NULL
3695 || this->constant_
->const_value()->type()->is_abstract()))
3697 Numeric_constant nc
;
3698 if (expr
->numeric_constant_value(&nc
)
3699 && nc
.set_type(this->type_
, false, this->location()))
3701 Expression
* e
= nc
.expression(this->location());
3702 return e
->get_backend(context
);
3706 if (this->type_
!= NULL
)
3707 expr
= Expression::make_cast(this->type_
, expr
, this->location());
3708 return expr
->get_backend(context
);
3711 // Dump ast representation for constant expression.
3714 Const_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3716 ast_dump_context
->ostream() << this->constant_
->name();
3719 // Make a reference to a constant in an expression.
3722 Expression::make_const_reference(Named_object
* constant
,
3725 return new Const_expression(constant
, location
);
3728 // Find a named object in an expression.
3731 Find_named_object::expression(Expression
** pexpr
)
3733 switch ((*pexpr
)->classification())
3735 case Expression::EXPRESSION_CONST_REFERENCE
:
3737 Const_expression
* ce
= static_cast<Const_expression
*>(*pexpr
);
3738 if (ce
->named_object() == this->no_
)
3741 // We need to check a constant initializer explicitly, as
3742 // loops here will not be caught by the loop checking for
3743 // variable initializers.
3744 ce
->check_for_init_loop();
3746 return TRAVERSE_CONTINUE
;
3749 case Expression::EXPRESSION_VAR_REFERENCE
:
3750 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
3752 return TRAVERSE_CONTINUE
;
3753 case Expression::EXPRESSION_FUNC_REFERENCE
:
3754 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
3756 return TRAVERSE_CONTINUE
;
3758 return TRAVERSE_CONTINUE
;
3760 this->found_
= true;
3761 return TRAVERSE_EXIT
;
3766 class Nil_expression
: public Expression
3769 Nil_expression(Location location
)
3770 : Expression(EXPRESSION_NIL
, location
)
3774 do_import(Import_expression
*, Location
);
3778 do_is_constant() const
3782 do_is_zero_value() const
3786 do_is_static_initializer() const
3791 { return Type::make_nil_type(); }
3794 do_determine_type(const Type_context
*)
3802 do_get_backend(Translate_context
* context
)
3803 { return context
->backend()->nil_pointer_expression(); }
3806 do_inlining_cost() const
3810 do_export(Export_function_body
* efb
) const
3811 { efb
->write_c_string("$nil"); }
3814 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3815 { ast_dump_context
->ostream() << "nil"; }
3818 // Import a nil expression.
3821 Nil_expression::do_import(Import_expression
* imp
, Location loc
)
3823 if (imp
->version() >= EXPORT_FORMAT_V3
)
3824 imp
->require_c_string("$");
3825 imp
->require_c_string("nil");
3826 return Expression::make_nil(loc
);
3829 // Make a nil expression.
3832 Expression::make_nil(Location location
)
3834 return new Nil_expression(location
);
3837 // The value of the predeclared constant iota. This is little more
3838 // than a marker. This will be lowered to an integer in
3839 // Const_expression::do_lower, which is where we know the value that
3842 class Iota_expression
: public Parser_expression
3845 Iota_expression(Location location
)
3846 : Parser_expression(EXPRESSION_IOTA
, location
)
3851 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
3852 { go_unreachable(); }
3854 // There should only ever be one of these.
3857 { go_unreachable(); }
3860 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3861 { ast_dump_context
->ostream() << "iota"; }
3864 // Make an iota expression. This is only called for one case: the
3865 // value of the predeclared constant iota.
3868 Expression::make_iota()
3870 static Iota_expression
iota_expression(Linemap::unknown_location());
3871 return &iota_expression
;
3874 // Class Type_conversion_expression.
3879 Type_conversion_expression::do_traverse(Traverse
* traverse
)
3881 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3882 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3883 return TRAVERSE_EXIT
;
3884 return TRAVERSE_CONTINUE
;
3887 // Convert to a constant at lowering time. Also lower conversions
3888 // from slice to pointer-to-array, as they can panic.
3891 Type_conversion_expression::do_lower(Gogo
*, Named_object
*,
3892 Statement_inserter
* inserter
, int)
3894 Type
* type
= this->type_
;
3895 Expression
* val
= this->expr_
;
3896 Location location
= this->location();
3898 if (type
->is_numeric_type())
3900 Numeric_constant nc
;
3901 if (val
->numeric_constant_value(&nc
))
3903 if (!nc
.set_type(type
, true, location
))
3904 return Expression::make_error(location
);
3905 return nc
.expression(location
);
3909 // According to the language specification on string conversions
3910 // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type):
3911 // When converting an integer into a string, the string will be a UTF-8
3912 // representation of the integer and integers "outside the range of valid
3913 // Unicode code points are converted to '\uFFFD'."
3914 if (type
->is_string_type())
3916 Numeric_constant nc
;
3917 if (val
->numeric_constant_value(&nc
) && nc
.is_int())
3919 // An integer value doesn't fit in the Unicode code point range if it
3920 // overflows the Go "int" type or is negative.
3922 if (!nc
.set_type(Type::lookup_integer_type("int"), false, location
)
3923 || nc
.to_unsigned_long(&ul
) == Numeric_constant::NC_UL_NEGATIVE
)
3924 return Expression::make_string("\ufffd", location
);
3928 if (type
->is_slice_type())
3930 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3931 bool is_byte
= (element_type
->integer_type() != NULL
3932 && element_type
->integer_type()->is_byte());
3933 bool is_rune
= (element_type
->integer_type() != NULL
3934 && element_type
->integer_type()->is_rune());
3935 if (is_byte
|| is_rune
)
3938 if (val
->string_constant_value(&s
))
3940 Expression_list
* vals
= new Expression_list();
3943 for (std::string::const_iterator p
= s
.begin();
3947 unsigned char c
= static_cast<unsigned char>(*p
);
3948 vals
->push_back(Expression::make_integer_ul(c
,
3955 const char *p
= s
.data();
3956 const char *pend
= s
.data() + s
.length();
3960 int adv
= Lex::fetch_char(p
, &c
);
3963 go_warning_at(this->location(), 0,
3964 "invalid UTF-8 encoding");
3968 vals
->push_back(Expression::make_integer_ul(c
,
3974 return Expression::make_slice_composite_literal(type
, vals
,
3980 if (type
->points_to() != NULL
3981 && type
->points_to()->array_type() != NULL
3982 && !type
->points_to()->is_slice_type()
3983 && val
->type()->is_slice_type()
3984 && Type::are_identical(type
->points_to()->array_type()->element_type(),
3985 val
->type()->array_type()->element_type(),
3988 Temporary_statement
* val_temp
= NULL
;
3989 if (!val
->is_multi_eval_safe())
3991 val_temp
= Statement::make_temporary(val
->type(), NULL
, location
);
3992 inserter
->insert(val_temp
);
3993 val
= Expression::make_set_and_use_temporary(val_temp
, val
,
3997 Type
* int_type
= Type::lookup_integer_type("int");
3998 Temporary_statement
* vallen_temp
=
3999 Statement::make_temporary(int_type
, NULL
, location
);
4000 inserter
->insert(vallen_temp
);
4002 Expression
* arrlen
= type
->points_to()->array_type()->length();
4003 Expression
* vallen
=
4004 Expression::make_slice_info(val
, Expression::SLICE_INFO_LENGTH
,
4006 vallen
= Expression::make_set_and_use_temporary(vallen_temp
, vallen
,
4008 Expression
* cond
= Expression::make_binary(OPERATOR_GT
, arrlen
, vallen
,
4011 vallen
= Expression::make_temporary_reference(vallen_temp
, location
);
4012 Expression
* panic
= Runtime::make_call(Runtime::PANIC_SLICE_CONVERT
,
4013 location
, 2, arrlen
, vallen
);
4015 Expression
* nil
= Expression::make_nil(location
);
4016 Expression
* check
= Expression::make_conditional(cond
, panic
, nil
,
4019 if (val_temp
== NULL
)
4022 val
= Expression::make_temporary_reference(val_temp
, location
);
4024 Expression::make_slice_info(val
, Expression::SLICE_INFO_VALUE_POINTER
,
4026 ptr
= Expression::make_unsafe_cast(type
, ptr
, location
);
4028 return Expression::make_compound(check
, ptr
, location
);
4034 // Flatten a type conversion by using a temporary variable for the slice
4035 // in slice to string conversions.
4038 Type_conversion_expression::do_flatten(Gogo
*, Named_object
*,
4039 Statement_inserter
* inserter
)
4041 if (this->type()->is_error_type() || this->expr_
->is_error_expression())
4043 go_assert(saw_errors());
4044 return Expression::make_error(this->location());
4047 if (((this->type()->is_string_type()
4048 && this->expr_
->type()->is_slice_type())
4049 || this->expr_
->type()->interface_type() != NULL
)
4050 && !this->expr_
->is_multi_eval_safe())
4052 Temporary_statement
* temp
=
4053 Statement::make_temporary(NULL
, this->expr_
, this->location());
4054 inserter
->insert(temp
);
4055 this->expr_
= Expression::make_temporary_reference(temp
, this->location());
4058 // For interface conversion and string to/from slice conversions,
4059 // decide if we can allocate on stack.
4060 if (this->type()->interface_type() != NULL
4061 || this->type()->is_string_type()
4062 || this->expr_
->type()->is_string_type())
4064 Node
* n
= Node::make_node(this);
4065 if ((n
->encoding() & ESCAPE_MASK
) == Node::ESCAPE_NONE
)
4066 this->no_escape_
= true;
4071 // Return whether a type conversion is a constant.
4074 Type_conversion_expression::do_is_constant() const
4076 if (!this->expr_
->is_constant())
4079 // A conversion to a type that may not be used as a constant is not
4080 // a constant. For example, []byte(nil).
4081 Type
* type
= this->type_
;
4082 if (type
->integer_type() == NULL
4083 && type
->float_type() == NULL
4084 && type
->complex_type() == NULL
4085 && !type
->is_boolean_type()
4086 && !type
->is_string_type())
4092 // Return whether a type conversion is a zero value.
4095 Type_conversion_expression::do_is_zero_value() const
4097 if (!this->expr_
->is_zero_value())
4100 // Some type conversion from zero value is still not zero value.
4101 // For example, []byte("") or interface{}(0).
4102 // Conservatively, only report true if the RHS is nil.
4103 Type
* type
= this->type_
;
4104 if (type
->integer_type() == NULL
4105 && type
->float_type() == NULL
4106 && type
->complex_type() == NULL
4107 && !type
->is_boolean_type()
4108 && !type
->is_string_type())
4109 return this->expr_
->is_nil_expression();
4114 // Return whether a type conversion can be used in a constant
4118 Type_conversion_expression::do_is_static_initializer() const
4120 Type
* type
= this->type_
;
4121 Type
* expr_type
= this->expr_
->type();
4123 if (type
->interface_type() != NULL
4124 || expr_type
->interface_type() != NULL
)
4127 if (!this->expr_
->is_static_initializer())
4130 if (Type::are_identical(type
, expr_type
,
4131 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
4135 if (type
->is_string_type() && expr_type
->is_string_type())
4138 if ((type
->is_numeric_type()
4139 || type
->is_boolean_type()
4140 || type
->points_to() != NULL
)
4141 && (expr_type
->is_numeric_type()
4142 || expr_type
->is_boolean_type()
4143 || expr_type
->points_to() != NULL
))
4149 // Return the constant numeric value if there is one.
4152 Type_conversion_expression::do_numeric_constant_value(
4153 Numeric_constant
* nc
) const
4155 if (!this->type_
->is_numeric_type())
4157 if (!this->expr_
->numeric_constant_value(nc
))
4159 return nc
->set_type(this->type_
, false, this->location());
4162 // Return the constant string value if there is one.
4165 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
4167 if (this->type_
->is_string_type()
4168 && this->expr_
->type()->integer_type() != NULL
)
4170 Numeric_constant nc
;
4171 if (this->expr_
->numeric_constant_value(&nc
))
4174 if (nc
.to_unsigned_long(&ival
) == Numeric_constant::NC_UL_VALID
)
4176 unsigned int cval
= static_cast<unsigned int>(ival
);
4177 if (static_cast<unsigned long>(cval
) != ival
)
4179 go_warning_at(this->location(), 0,
4180 "unicode code point 0x%lx out of range",
4182 cval
= 0xfffd; // Unicode "replacement character."
4185 Lex::append_char(cval
, true, val
, this->location());
4191 // FIXME: Could handle conversion from const []int here.
4196 // Return the constant boolean value if there is one.
4199 Type_conversion_expression::do_boolean_constant_value(bool* val
) const
4201 if (!this->type_
->is_boolean_type())
4203 return this->expr_
->boolean_constant_value(val
);
4206 // Determine the resulting type of the conversion.
4209 Type_conversion_expression::do_determine_type(const Type_context
*)
4211 Type_context
subcontext(this->type_
, false);
4212 this->expr_
->determine_type(&subcontext
);
4215 // Check that types are convertible.
4218 Type_conversion_expression::do_check_types(Gogo
*)
4220 Type
* type
= this->type_
;
4221 Type
* expr_type
= this->expr_
->type();
4224 if (type
->is_error() || expr_type
->is_error())
4226 this->set_is_error();
4230 if (this->may_convert_function_types_
4231 && type
->function_type() != NULL
4232 && expr_type
->function_type() != NULL
)
4235 if (Type::are_convertible(type
, expr_type
, &reason
))
4238 go_error_at(this->location(), "%s", reason
.c_str());
4239 this->set_is_error();
4245 Type_conversion_expression::do_copy()
4247 Expression
* ret
= new Type_conversion_expression(this->type_
->copy_expressions(),
4248 this->expr_
->copy(),
4250 ret
->conversion_expression()->set_no_copy(this->no_copy_
);
4254 // Get the backend representation for a type conversion.
4257 Type_conversion_expression::do_get_backend(Translate_context
* context
)
4259 Type
* type
= this->type_
;
4260 Type
* expr_type
= this->expr_
->type();
4262 Gogo
* gogo
= context
->gogo();
4263 Btype
* btype
= type
->get_backend(gogo
);
4264 Location loc
= this->location();
4266 if (Type::are_identical(type
, expr_type
,
4267 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
4270 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
4271 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
4273 else if (type
->interface_type() != NULL
4274 && expr_type
->interface_type() == NULL
)
4276 Expression
* conversion
=
4277 Expression::convert_type_to_interface(type
, this->expr_
,
4278 this->no_escape_
, loc
);
4279 return conversion
->get_backend(context
);
4281 else if (type
->interface_type() != NULL
4282 || expr_type
->interface_type() != NULL
)
4284 Expression
* conversion
=
4285 Expression::convert_for_assignment(gogo
, type
, this->expr_
,
4287 return conversion
->get_backend(context
);
4289 else if (type
->is_string_type()
4290 && expr_type
->integer_type() != NULL
)
4293 Numeric_constant nc
;
4294 if (this->expr_
->numeric_constant_value(&nc
)
4295 && nc
.to_int(&intval
))
4299 if (mpz_fits_uint_p(intval
))
4300 x
= mpz_get_ui(intval
);
4303 char* ms
= mpz_get_str(NULL
, 16, intval
);
4304 go_warning_at(loc
, 0,
4305 "unicode code point 0x%s out of range in string",
4310 Lex::append_char(x
, true, &s
, loc
);
4312 Expression
* se
= Expression::make_string(s
, loc
);
4313 return se
->get_backend(context
);
4317 if (this->no_escape_
)
4319 Type
* byte_type
= Type::lookup_integer_type("uint8");
4320 Expression
* buflen
=
4321 Expression::make_integer_ul(4, NULL
, loc
);
4322 Type
* array_type
= Type::make_array_type(byte_type
, buflen
);
4323 buf
= Expression::make_allocation(array_type
, loc
);
4324 buf
->allocation_expression()->set_allocate_on_stack();
4325 buf
->allocation_expression()->set_no_zero();
4328 buf
= Expression::make_nil(loc
);
4329 Expression
* i2s_expr
=
4330 Runtime::make_call(Runtime::INTSTRING
, loc
, 2, buf
, this->expr_
);
4331 return Expression::make_cast(type
, i2s_expr
, loc
)->get_backend(context
);
4333 else if (type
->is_string_type() && expr_type
->is_slice_type())
4335 Array_type
* a
= expr_type
->array_type();
4336 Type
* e
= a
->element_type()->forwarded();
4337 go_assert(e
->integer_type() != NULL
);
4338 go_assert(this->expr_
->is_multi_eval_safe());
4341 if (this->no_escape_
&& !this->no_copy_
)
4343 Type
* byte_type
= Type::lookup_integer_type("uint8");
4344 Expression
* buflen
=
4345 Expression::make_integer_ul(tmp_string_buf_size
, NULL
, loc
);
4346 Type
* array_type
= Type::make_array_type(byte_type
, buflen
);
4347 buf
= Expression::make_allocation(array_type
, loc
);
4348 buf
->allocation_expression()->set_allocate_on_stack();
4349 buf
->allocation_expression()->set_no_zero();
4352 buf
= Expression::make_nil(loc
);
4354 if (e
->integer_type()->is_byte())
4357 Expression::make_slice_info(this->expr_
, SLICE_INFO_VALUE_POINTER
,
4360 Expression::make_slice_info(this->expr_
, SLICE_INFO_LENGTH
, loc
);
4363 if (gogo
->debug_optimization())
4364 go_debug(loc
, "no copy string([]byte)");
4365 Expression
* str
= Expression::make_string_value(ptr
, len
, loc
);
4366 return str
->get_backend(context
);
4368 return Runtime::make_call(Runtime::SLICEBYTETOSTRING
, loc
, 3, buf
,
4369 ptr
, len
)->get_backend(context
);
4373 go_assert(e
->integer_type()->is_rune());
4374 return Runtime::make_call(Runtime::SLICERUNETOSTRING
, loc
, 2, buf
,
4375 this->expr_
)->get_backend(context
);
4378 else if (type
->is_slice_type() && expr_type
->is_string_type())
4380 Type
* e
= type
->array_type()->element_type()->forwarded();
4381 go_assert(e
->integer_type() != NULL
);
4383 Runtime::Function code
;
4384 if (e
->integer_type()->is_byte())
4385 code
= Runtime::STRINGTOSLICEBYTE
;
4388 go_assert(e
->integer_type()->is_rune());
4389 code
= Runtime::STRINGTOSLICERUNE
;
4393 if (this->no_escape_
)
4395 Expression
* buflen
=
4396 Expression::make_integer_ul(tmp_string_buf_size
, NULL
, loc
);
4397 Type
* array_type
= Type::make_array_type(e
, buflen
);
4398 buf
= Expression::make_allocation(array_type
, loc
);
4399 buf
->allocation_expression()->set_allocate_on_stack();
4400 buf
->allocation_expression()->set_no_zero();
4403 buf
= Expression::make_nil(loc
);
4404 Expression
* s2a
= Runtime::make_call(code
, loc
, 2, buf
, this->expr_
);
4405 return Expression::make_unsafe_cast(type
, s2a
, loc
)->get_backend(context
);
4407 else if (type
->is_numeric_type())
4409 go_assert(Type::are_convertible(type
, expr_type
, NULL
));
4410 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
4411 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
4413 else if ((type
->is_unsafe_pointer_type()
4414 && (expr_type
->points_to() != NULL
4415 || expr_type
->integer_type()))
4416 || (expr_type
->is_unsafe_pointer_type()
4417 && type
->points_to() != NULL
)
4418 || (this->may_convert_function_types_
4419 && type
->function_type() != NULL
4420 && expr_type
->function_type() != NULL
))
4422 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
4423 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
4427 Expression
* conversion
=
4428 Expression::convert_for_assignment(gogo
, type
, this->expr_
, loc
);
4429 return conversion
->get_backend(context
);
4433 // Cost of inlining a type conversion.
4436 Type_conversion_expression::do_inlining_cost() const
4438 Type
* type
= this->type_
;
4439 Type
* expr_type
= this->expr_
->type();
4440 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
4442 else if (type
->is_string_type() && expr_type
->integer_type() != NULL
)
4444 else if (type
->is_string_type() && expr_type
->is_slice_type())
4446 else if (type
->is_slice_type() && expr_type
->is_string_type())
4452 // Output a type conversion in a constant expression.
4455 Type_conversion_expression::do_export(Export_function_body
* efb
) const
4457 efb
->write_c_string("$convert(");
4458 efb
->write_type(this->type_
);
4459 efb
->write_c_string(", ");
4461 Type
* old_context
= efb
->type_context();
4462 efb
->set_type_context(this->type_
);
4464 this->expr_
->export_expression(efb
);
4466 efb
->set_type_context(old_context
);
4468 efb
->write_c_string(")");
4471 // Import a type conversion or a struct construction.
4474 Type_conversion_expression::do_import(Import_expression
* imp
, Location loc
)
4476 imp
->require_c_string("$convert(");
4477 Type
* type
= imp
->read_type();
4478 imp
->require_c_string(", ");
4479 Expression
* val
= Expression::import_expression(imp
, loc
);
4480 imp
->require_c_string(")");
4481 return Expression::make_cast(type
, val
, loc
);
4484 // Dump ast representation for a type conversion expression.
4487 Type_conversion_expression::do_dump_expression(
4488 Ast_dump_context
* ast_dump_context
) const
4490 ast_dump_context
->dump_type(this->type_
);
4491 ast_dump_context
->ostream() << "(";
4492 ast_dump_context
->dump_expression(this->expr_
);
4493 ast_dump_context
->ostream() << ") ";
4496 // Make a type cast expression.
4499 Expression::make_cast(Type
* type
, Expression
* val
, Location location
)
4501 if (type
->is_error_type() || val
->is_error_expression())
4502 return Expression::make_error(location
);
4503 return new Type_conversion_expression(type
, val
, location
);
4506 // Class Unsafe_type_conversion_expression.
4511 Unsafe_type_conversion_expression::do_traverse(Traverse
* traverse
)
4513 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
4514 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
4515 return TRAVERSE_EXIT
;
4516 return TRAVERSE_CONTINUE
;
4519 // Return whether an unsafe type conversion can be used as a constant
4523 Unsafe_type_conversion_expression::do_is_static_initializer() const
4525 Type
* type
= this->type_
;
4526 Type
* expr_type
= this->expr_
->type();
4528 if (type
->interface_type() != NULL
4529 || expr_type
->interface_type() != NULL
)
4532 if (!this->expr_
->is_static_initializer())
4535 if (Type::are_convertible(type
, expr_type
, NULL
))
4538 if (type
->is_string_type() && expr_type
->is_string_type())
4541 if ((type
->is_numeric_type()
4542 || type
->is_boolean_type()
4543 || type
->points_to() != NULL
)
4544 && (expr_type
->is_numeric_type()
4545 || expr_type
->is_boolean_type()
4546 || expr_type
->points_to() != NULL
))
4555 Unsafe_type_conversion_expression::do_copy()
4557 return new Unsafe_type_conversion_expression(this->type_
->copy_expressions(),
4558 this->expr_
->copy(),
4562 // Convert to backend representation.
4565 Unsafe_type_conversion_expression::do_get_backend(Translate_context
* context
)
4567 // We are only called for a limited number of cases.
4569 Type
* t
= this->type_
;
4570 Type
* et
= this->expr_
->type();
4572 if (t
->is_error_type()
4573 || this->expr_
->is_error_expression()
4574 || et
->is_error_type())
4576 go_assert(saw_errors());
4577 return context
->backend()->error_expression();
4580 if (t
->array_type() != NULL
)
4581 go_assert(et
->array_type() != NULL
4582 && t
->is_slice_type() == et
->is_slice_type());
4583 else if (t
->struct_type() != NULL
)
4585 if (t
->named_type() != NULL
4586 && et
->named_type() != NULL
4587 && !Type::are_convertible(t
, et
, NULL
))
4589 go_assert(saw_errors());
4590 return context
->backend()->error_expression();
4593 go_assert(et
->struct_type() != NULL
4594 && Type::are_convertible(t
, et
, NULL
));
4596 else if (t
->map_type() != NULL
)
4597 go_assert(et
->map_type() != NULL
|| et
->points_to() != NULL
);
4598 else if (t
->channel_type() != NULL
)
4599 go_assert(et
->channel_type() != NULL
|| et
->points_to() != NULL
);
4600 else if (t
->points_to() != NULL
)
4601 go_assert(et
->points_to() != NULL
4602 || et
->channel_type() != NULL
4603 || et
->map_type() != NULL
4604 || et
->function_type() != NULL
4605 || et
->integer_type() != NULL
4606 || et
->is_nil_type());
4607 else if (t
->function_type() != NULL
)
4608 go_assert(et
->points_to() != NULL
);
4609 else if (et
->is_unsafe_pointer_type())
4610 go_assert(t
->points_to() != NULL
4611 || (t
->integer_type() != NULL
4612 && t
->integer_type() == Type::lookup_integer_type("uintptr")->real_type()));
4613 else if (t
->interface_type() != NULL
)
4615 bool empty_iface
= t
->interface_type()->is_empty();
4616 go_assert(et
->interface_type() != NULL
4617 && et
->interface_type()->is_empty() == empty_iface
);
4619 else if (t
->integer_type() != NULL
)
4620 go_assert(et
->is_boolean_type()
4621 || et
->integer_type() != NULL
4622 || et
->function_type() != NULL
4623 || et
->points_to() != NULL
4624 || et
->map_type() != NULL
4625 || et
->channel_type() != NULL
4626 || et
->is_nil_type());
4630 Gogo
* gogo
= context
->gogo();
4631 Btype
* btype
= t
->get_backend(gogo
);
4632 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
4633 Location loc
= this->location();
4634 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
4637 // Dump ast representation for an unsafe type conversion expression.
4640 Unsafe_type_conversion_expression::do_dump_expression(
4641 Ast_dump_context
* ast_dump_context
) const
4643 ast_dump_context
->dump_type(this->type_
);
4644 ast_dump_context
->ostream() << "(";
4645 ast_dump_context
->dump_expression(this->expr_
);
4646 ast_dump_context
->ostream() << ") ";
4649 // Make an unsafe type conversion expression.
4652 Expression::make_unsafe_cast(Type
* type
, Expression
* expr
,
4655 return new Unsafe_type_conversion_expression(type
, expr
, location
);
4658 // Class Unary_expression.
4660 // Call the address_taken method of the operand if needed. This is
4661 // called after escape analysis but before inserting write barriers.
4664 Unary_expression::check_operand_address_taken(Gogo
*)
4666 if (this->op_
!= OPERATOR_AND
)
4669 // If this->escapes_ is false at this point, then it was set to
4670 // false by an explicit call to set_does_not_escape, and the value
4671 // does not escape. If this->escapes_ is true, we may be able to
4672 // set it to false based on the escape analysis pass.
4675 Node
* n
= Node::make_node(this);
4676 if ((n
->encoding() & ESCAPE_MASK
) == int(Node::ESCAPE_NONE
))
4677 this->escapes_
= false;
4680 this->expr_
->address_taken(this->escapes_
);
4683 // If we are taking the address of a composite literal, and the
4684 // contents are not constant, then we want to make a heap expression
4688 Unary_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
4690 Location loc
= this->location();
4691 Operator op
= this->op_
;
4692 Expression
* expr
= this->expr_
;
4694 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
4695 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
4697 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
4698 // moving x to the heap. FIXME: Is it worth doing a real escape
4699 // analysis here? This case is found in math/unsafe.go and is
4700 // therefore worth special casing.
4701 if (op
== OPERATOR_MULT
)
4703 Expression
* e
= expr
;
4704 while (e
->classification() == EXPRESSION_CONVERSION
)
4706 Type_conversion_expression
* te
4707 = static_cast<Type_conversion_expression
*>(e
);
4711 if (e
->classification() == EXPRESSION_UNARY
)
4713 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
4714 if (ue
->op_
== OPERATOR_AND
)
4719 if (!ue
->expr_
->is_addressable() && !ue
->create_temp_
)
4721 go_error_at(ue
->location(),
4722 "invalid operand for unary %<&%>");
4723 this->set_is_error();
4727 ue
->set_does_not_escape();
4732 // Catching an invalid indirection of unsafe.Pointer here avoid
4733 // having to deal with TYPE_VOID in other places.
4734 if (op
== OPERATOR_MULT
&& expr
->type()->is_unsafe_pointer_type())
4736 go_error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
4737 return Expression::make_error(this->location());
4740 // Check for an invalid pointer dereference. We need to do this
4741 // here because Unary_expression::do_type will return an error type
4742 // in this case. That can cause code to appear erroneous, and
4743 // therefore disappear at lowering time, without any error message.
4744 if (op
== OPERATOR_MULT
&& expr
->type()->points_to() == NULL
)
4746 this->report_error(_("expected pointer"));
4747 return Expression::make_error(this->location());
4750 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
|| op
== OPERATOR_XOR
)
4752 Numeric_constant nc
;
4753 if (expr
->numeric_constant_value(&nc
))
4755 Numeric_constant result
;
4757 if (Unary_expression::eval_constant(op
, &nc
, loc
, &result
,
4759 return result
.expression(loc
);
4760 else if (issued_error
)
4761 return Expression::make_error(this->location());
4768 // Flatten expression if a nil check must be performed and create temporary
4769 // variables if necessary.
4772 Unary_expression::do_flatten(Gogo
* gogo
, Named_object
*,
4773 Statement_inserter
* inserter
)
4775 if (this->is_error_expression()
4776 || this->expr_
->is_error_expression()
4777 || this->expr_
->type()->is_error_type())
4779 go_assert(saw_errors());
4780 return Expression::make_error(this->location());
4783 Location location
= this->location();
4784 if (this->op_
== OPERATOR_MULT
4785 && !this->expr_
->is_multi_eval_safe())
4787 go_assert(this->expr_
->type()->points_to() != NULL
);
4788 switch (this->requires_nil_check(gogo
))
4790 case NIL_CHECK_ERROR_ENCOUNTERED
:
4792 go_assert(saw_errors());
4793 return Expression::make_error(this->location());
4795 case NIL_CHECK_NOT_NEEDED
:
4797 case NIL_CHECK_NEEDED
:
4798 this->create_temp_
= true;
4800 case NIL_CHECK_DEFAULT
:
4805 if (this->create_temp_
&& !this->expr_
->is_multi_eval_safe())
4807 Temporary_statement
* temp
=
4808 Statement::make_temporary(NULL
, this->expr_
, location
);
4809 inserter
->insert(temp
);
4810 this->expr_
= Expression::make_temporary_reference(temp
, location
);
4816 // Return whether a unary expression is a constant.
4819 Unary_expression::do_is_constant() const
4821 if (this->op_
== OPERATOR_MULT
)
4823 // Indirecting through a pointer is only constant if the object
4824 // to which the expression points is constant, but we currently
4825 // have no way to determine that.
4828 else if (this->op_
== OPERATOR_AND
)
4830 // Taking the address of a variable is constant if it is a
4831 // global variable, not constant otherwise. In other cases taking the
4832 // address is probably not a constant.
4833 Var_expression
* ve
= this->expr_
->var_expression();
4836 Named_object
* no
= ve
->named_object();
4837 return no
->is_variable() && no
->var_value()->is_global();
4842 return this->expr_
->is_constant();
4845 // Return whether a unary expression can be used as a constant
4849 Unary_expression::do_is_static_initializer() const
4851 if (this->op_
== OPERATOR_MULT
)
4853 else if (this->op_
== OPERATOR_AND
)
4854 return Unary_expression::base_is_static_initializer(this->expr_
);
4856 return this->expr_
->is_static_initializer();
4859 // Return whether the address of EXPR can be used as a static
4863 Unary_expression::base_is_static_initializer(Expression
* expr
)
4865 // The address of a field reference can be a static initializer if
4866 // the base can be a static initializer.
4867 Field_reference_expression
* fre
= expr
->field_reference_expression();
4869 return Unary_expression::base_is_static_initializer(fre
->expr());
4871 // The address of an index expression can be a static initializer if
4872 // the base can be a static initializer and the index is constant.
4873 Array_index_expression
* aind
= expr
->array_index_expression();
4875 return (aind
->end() == NULL
4876 && aind
->start()->is_constant()
4877 && Unary_expression::base_is_static_initializer(aind
->array()));
4879 // The address of a global variable can be a static initializer.
4880 Var_expression
* ve
= expr
->var_expression();
4883 Named_object
* no
= ve
->named_object();
4884 return no
->is_variable() && no
->var_value()->is_global();
4887 // The address of a composite literal can be used as a static
4888 // initializer if the composite literal is itself usable as a
4889 // static initializer.
4890 if (expr
->is_composite_literal() && expr
->is_static_initializer())
4893 // The address of a string constant can be used as a static
4894 // initializer. This can not be written in Go itself but this is
4895 // used when building a type descriptor.
4896 if (expr
->string_expression() != NULL
)
4902 // Return whether this dereference expression requires an explicit nil
4903 // check. If we are dereferencing the pointer to a large struct
4904 // (greater than the specified size threshold), we need to check for
4905 // nil. We don't bother to check for small structs because we expect
4906 // the system to crash on a nil pointer dereference. However, if we
4907 // know the address of this expression is being taken, we must always
4909 Unary_expression::Nil_check_classification
4910 Unary_expression::requires_nil_check(Gogo
* gogo
)
4912 go_assert(this->op_
== OPERATOR_MULT
);
4913 go_assert(this->expr_
->type()->points_to() != NULL
);
4915 if (this->issue_nil_check_
== NIL_CHECK_NEEDED
)
4916 return NIL_CHECK_NEEDED
;
4917 else if (this->issue_nil_check_
== NIL_CHECK_NOT_NEEDED
)
4918 return NIL_CHECK_NOT_NEEDED
;
4920 Type
* ptype
= this->expr_
->type()->points_to();
4921 int64_t type_size
= -1;
4922 if (!ptype
->is_void_type())
4924 bool ok
= ptype
->backend_type_size(gogo
, &type_size
);
4926 return NIL_CHECK_ERROR_ENCOUNTERED
;
4929 int64_t size_cutoff
= gogo
->nil_check_size_threshold();
4930 if (size_cutoff
== -1 || (type_size
!= -1 && type_size
>= size_cutoff
))
4931 this->issue_nil_check_
= NIL_CHECK_NEEDED
;
4933 this->issue_nil_check_
= NIL_CHECK_NOT_NEEDED
;
4934 return this->issue_nil_check_
;
4937 // Apply unary opcode OP to UNC, setting NC. Return true if this
4938 // could be done, false if not. On overflow, issues an error and sets
4942 Unary_expression::eval_constant(Operator op
, const Numeric_constant
* unc
,
4943 Location location
, Numeric_constant
* nc
,
4946 *issued_error
= false;
4953 case OPERATOR_MINUS
:
4954 if (unc
->is_int() || unc
->is_rune())
4956 else if (unc
->is_float())
4959 unc
->get_float(&uval
);
4962 mpfr_neg(val
, uval
, MPFR_RNDN
);
4963 nc
->set_float(unc
->type(), val
);
4968 else if (unc
->is_complex())
4971 unc
->get_complex(&uval
);
4973 mpc_init2(val
, mpc_precision
);
4974 mpc_neg(val
, uval
, MPC_RNDNN
);
4975 nc
->set_complex(unc
->type(), val
);
4995 if (!unc
->is_int() && !unc
->is_rune())
5000 unc
->get_rune(&uval
);
5002 unc
->get_int(&uval
);
5008 case OPERATOR_MINUS
:
5013 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
5018 Type
* utype
= unc
->type();
5019 if (utype
->integer_type() == NULL
5020 || utype
->integer_type()->is_abstract())
5024 // The number of HOST_WIDE_INTs that it takes to represent
5026 size_t count
= ((mpz_sizeinbase(uval
, 2)
5027 + HOST_BITS_PER_WIDE_INT
5029 / HOST_BITS_PER_WIDE_INT
);
5031 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
5032 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
5034 size_t obits
= utype
->integer_type()->bits();
5036 if (!utype
->integer_type()->is_unsigned() && mpz_sgn(uval
) < 0)
5039 mpz_init_set_ui(adj
, 1);
5040 mpz_mul_2exp(adj
, adj
, obits
);
5041 mpz_add(uval
, uval
, adj
);
5046 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
5047 go_assert(ecount
<= count
);
5049 // Trim down to the number of words required by the type.
5050 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
5051 / HOST_BITS_PER_WIDE_INT
);
5052 go_assert(ocount
<= count
);
5054 for (size_t i
= 0; i
< ocount
; ++i
)
5057 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
5059 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
5062 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
5064 if (!utype
->integer_type()->is_unsigned()
5065 && mpz_tstbit(val
, obits
- 1))
5068 mpz_init_set_ui(adj
, 1);
5069 mpz_mul_2exp(adj
, adj
, obits
);
5070 mpz_sub(val
, val
, adj
);
5084 nc
->set_rune(NULL
, val
);
5086 nc
->set_int(NULL
, val
);
5091 if (!nc
->set_type(unc
->type(), true, location
))
5093 *issued_error
= true;
5099 // Return the integral constant value of a unary expression, if it has one.
5102 Unary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
5104 Numeric_constant unc
;
5105 if (!this->expr_
->numeric_constant_value(&unc
))
5108 return Unary_expression::eval_constant(this->op_
, &unc
, this->location(),
5112 // Return the boolean constant value of a unary expression, if it has one.
5115 Unary_expression::do_boolean_constant_value(bool* val
) const
5117 if (this->op_
== OPERATOR_NOT
5118 && this->expr_
->boolean_constant_value(val
))
5126 // Return the type of a unary expression.
5129 Unary_expression::do_type()
5134 case OPERATOR_MINUS
:
5137 return this->expr_
->type();
5140 return Type::make_pointer_type(this->expr_
->type());
5144 Type
* subtype
= this->expr_
->type();
5145 Type
* points_to
= subtype
->points_to();
5146 if (points_to
== NULL
)
5147 return Type::make_error_type();
5156 // Determine abstract types for a unary expression.
5159 Unary_expression::do_determine_type(const Type_context
* context
)
5164 case OPERATOR_MINUS
:
5167 this->expr_
->determine_type(context
);
5171 // Taking the address of something.
5173 Type
* subtype
= (context
->type
== NULL
5175 : context
->type
->points_to());
5176 Type_context
subcontext(subtype
, false);
5177 this->expr_
->determine_type(&subcontext
);
5182 // Indirecting through a pointer.
5184 Type
* subtype
= (context
->type
== NULL
5186 : Type::make_pointer_type(context
->type
));
5187 Type_context
subcontext(subtype
, false);
5188 this->expr_
->determine_type(&subcontext
);
5197 // Check types for a unary expression.
5200 Unary_expression::do_check_types(Gogo
*)
5202 Type
* type
= this->expr_
->type();
5203 if (type
->is_error())
5205 this->set_is_error();
5212 case OPERATOR_MINUS
:
5213 if (type
->integer_type() == NULL
5214 && type
->float_type() == NULL
5215 && type
->complex_type() == NULL
)
5216 this->report_error(_("expected numeric type"));
5220 if (!type
->is_boolean_type())
5221 this->report_error(_("expected boolean type"));
5225 if (type
->integer_type() == NULL
)
5226 this->report_error(_("expected integer"));
5230 if (!this->expr_
->is_addressable())
5232 if (!this->create_temp_
)
5234 go_error_at(this->location(), "invalid operand for unary %<&%>");
5235 this->set_is_error();
5239 this->expr_
->issue_nil_check();
5243 // Indirecting through a pointer.
5244 if (type
->points_to() == NULL
)
5245 this->report_error(_("expected pointer"));
5246 if (type
->points_to()->is_error())
5247 this->set_is_error();
5255 // Get the backend representation for a unary expression.
5258 Unary_expression::do_get_backend(Translate_context
* context
)
5260 Gogo
* gogo
= context
->gogo();
5261 Location loc
= this->location();
5263 // Taking the address of a set-and-use-temporary expression requires
5264 // setting the temporary and then taking the address.
5265 if (this->op_
== OPERATOR_AND
)
5267 Set_and_use_temporary_expression
* sut
=
5268 this->expr_
->set_and_use_temporary_expression();
5271 Temporary_statement
* temp
= sut
->temporary();
5272 Bvariable
* bvar
= temp
->get_backend_variable(context
);
5273 Bexpression
* bvar_expr
=
5274 gogo
->backend()->var_expression(bvar
, loc
);
5275 Bexpression
* bval
= sut
->expression()->get_backend(context
);
5277 Named_object
* fn
= context
->function();
5278 go_assert(fn
!= NULL
);
5280 fn
->func_value()->get_or_make_decl(gogo
, fn
);
5281 Bstatement
* bassign
=
5282 gogo
->backend()->assignment_statement(bfn
, bvar_expr
, bval
, loc
);
5283 Bexpression
* bvar_addr
=
5284 gogo
->backend()->address_expression(bvar_expr
, loc
);
5285 return gogo
->backend()->compound_expression(bassign
, bvar_addr
, loc
);
5290 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
5291 Btype
* btype
= this->expr_
->type()->get_backend(gogo
);
5298 case OPERATOR_MINUS
:
5299 ret
= gogo
->backend()->unary_expression(this->op_
, bexpr
, loc
);
5300 ret
= gogo
->backend()->convert_expression(btype
, ret
, loc
);
5305 ret
= gogo
->backend()->unary_expression(this->op_
, bexpr
, loc
);
5309 if (!this->create_temp_
)
5311 // We should not see a non-constant constructor here; cases
5312 // where we would see one should have been moved onto the
5313 // heap at parse time. Taking the address of a nonconstant
5314 // constructor will not do what the programmer expects.
5316 go_assert(!this->expr_
->is_composite_literal()
5317 || this->expr_
->is_static_initializer());
5318 if (this->expr_
->classification() == EXPRESSION_UNARY
)
5320 Unary_expression
* ue
=
5321 static_cast<Unary_expression
*>(this->expr_
);
5322 go_assert(ue
->op() != OPERATOR_AND
);
5326 if (this->is_gc_root_
|| this->is_slice_init_
)
5328 std::string var_name
;
5329 bool copy_to_heap
= false;
5330 if (this->is_gc_root_
)
5332 // Build a decl for a GC root variable. GC roots are mutable, so
5333 // they cannot be represented as an immutable_struct in the
5335 var_name
= gogo
->gc_root_name();
5339 // Build a decl for a slice value initializer. An immutable slice
5340 // value initializer may have to be copied to the heap if it
5341 // contains pointers in a non-constant context.
5342 var_name
= gogo
->initializer_name();
5344 Array_type
* at
= this->expr_
->type()->array_type();
5345 go_assert(at
!= NULL
);
5347 // If we are not copying the value to the heap, we will only
5348 // initialize the value once, so we can use this directly
5349 // rather than copying it. In that case we can't make it
5350 // read-only, because the program is permitted to change it.
5351 copy_to_heap
= (context
->function() != NULL
5352 || context
->is_const());
5354 unsigned int flags
= (Backend::variable_is_hidden
5355 | Backend::variable_address_is_taken
);
5357 flags
|= Backend::variable_is_constant
;
5358 Bvariable
* implicit
=
5359 gogo
->backend()->implicit_variable(var_name
, "", btype
, flags
, 0);
5360 gogo
->backend()->implicit_variable_set_init(implicit
, var_name
, btype
,
5362 bexpr
= gogo
->backend()->var_expression(implicit
, loc
);
5364 // If we are not copying a slice initializer to the heap,
5365 // then it can be changed by the program, so if it can
5366 // contain pointers we must register it as a GC root.
5367 if (this->is_slice_init_
5369 && this->expr_
->type()->has_pointer())
5372 gogo
->backend()->var_expression(implicit
, loc
);
5373 root
= gogo
->backend()->address_expression(root
, loc
);
5374 Type
* type
= Type::make_pointer_type(this->expr_
->type());
5375 gogo
->add_gc_root(Expression::make_backend(root
, type
, loc
));
5378 else if ((this->expr_
->is_composite_literal()
5379 || this->expr_
->string_expression() != NULL
)
5380 && this->expr_
->is_static_initializer())
5382 std::string
var_name(gogo
->initializer_name());
5383 unsigned int flags
= (Backend::variable_is_hidden
5384 | Backend::variable_address_is_taken
);
5386 gogo
->backend()->immutable_struct(var_name
, "", flags
, btype
, loc
);
5387 gogo
->backend()->immutable_struct_set_init(decl
, var_name
, flags
,
5389 bexpr
= gogo
->backend()->var_expression(decl
, loc
);
5391 else if (this->expr_
->is_constant())
5393 std::string
var_name(gogo
->initializer_name());
5394 unsigned int flags
= (Backend::variable_is_hidden
5395 | Backend::variable_is_constant
5396 | Backend::variable_address_is_taken
);
5398 gogo
->backend()->implicit_variable(var_name
, "", btype
, flags
, 0);
5399 gogo
->backend()->implicit_variable_set_init(decl
, var_name
, btype
,
5401 bexpr
= gogo
->backend()->var_expression(decl
, loc
);
5404 go_assert(!this->create_temp_
|| this->expr_
->is_multi_eval_safe());
5405 ret
= gogo
->backend()->address_expression(bexpr
, loc
);
5410 go_assert(this->expr_
->type()->points_to() != NULL
);
5412 Type
* ptype
= this->expr_
->type()->points_to();
5413 Btype
* pbtype
= ptype
->get_backend(gogo
);
5414 switch (this->requires_nil_check(gogo
))
5416 case NIL_CHECK_NOT_NEEDED
:
5418 case NIL_CHECK_ERROR_ENCOUNTERED
:
5420 go_assert(saw_errors());
5421 return gogo
->backend()->error_expression();
5423 case NIL_CHECK_NEEDED
:
5425 go_assert(this->expr_
->is_multi_eval_safe());
5427 // If we're nil-checking the result of a set-and-use-temporary
5428 // expression, then pick out the target temp and use that
5429 // for the final result of the conditional.
5430 Bexpression
* tbexpr
= bexpr
;
5431 Bexpression
* ubexpr
= bexpr
;
5432 Set_and_use_temporary_expression
* sut
=
5433 this->expr_
->set_and_use_temporary_expression();
5435 Temporary_statement
* temp
= sut
->temporary();
5436 Bvariable
* bvar
= temp
->get_backend_variable(context
);
5437 ubexpr
= gogo
->backend()->var_expression(bvar
, loc
);
5440 Expression::make_nil(loc
)->get_backend(context
);
5441 Bexpression
* compare
=
5442 gogo
->backend()->binary_expression(OPERATOR_EQEQ
, tbexpr
,
5444 Expression
* crash
= Runtime::make_call(Runtime::PANIC_MEM
,
5446 Bexpression
* bcrash
= crash
->get_backend(context
);
5447 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
5448 bexpr
= gogo
->backend()->conditional_expression(bfn
, btype
,
5454 case NIL_CHECK_DEFAULT
:
5457 ret
= gogo
->backend()->indirect_expression(pbtype
, bexpr
, false, loc
);
5468 // Export a unary expression.
5471 Unary_expression::do_export(Export_function_body
* efb
) const
5476 efb
->write_c_string("+");
5478 case OPERATOR_MINUS
:
5479 efb
->write_c_string("-");
5482 efb
->write_c_string("!");
5485 efb
->write_c_string("^");
5488 efb
->write_c_string("&");
5491 efb
->write_c_string("*");
5496 this->expr_
->export_expression(efb
);
5499 // Import a unary expression.
5502 Unary_expression::do_import(Import_expression
* imp
, Location loc
)
5505 switch (imp
->get_char())
5511 op
= OPERATOR_MINUS
;
5528 if (imp
->version() < EXPORT_FORMAT_V3
)
5529 imp
->require_c_string(" ");
5530 Expression
* expr
= Expression::import_expression(imp
, loc
);
5531 return Expression::make_unary(op
, expr
, loc
);
5534 // Dump ast representation of an unary expression.
5537 Unary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
5539 ast_dump_context
->dump_operator(this->op_
);
5540 ast_dump_context
->ostream() << "(";
5541 ast_dump_context
->dump_expression(this->expr_
);
5542 ast_dump_context
->ostream() << ") ";
5545 // Make a unary expression.
5548 Expression::make_unary(Operator op
, Expression
* expr
, Location location
)
5550 return new Unary_expression(op
, expr
, location
);
5554 Expression::make_dereference(Expression
* ptr
,
5555 Nil_check_classification docheck
,
5558 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, ptr
, location
);
5559 if (docheck
== NIL_CHECK_NEEDED
)
5560 deref
->unary_expression()->set_requires_nil_check(true);
5561 else if (docheck
== NIL_CHECK_NOT_NEEDED
)
5562 deref
->unary_expression()->set_requires_nil_check(false);
5566 // If this is an indirection through a pointer, return the expression
5567 // being pointed through. Otherwise return this.
5572 if (this->classification_
== EXPRESSION_UNARY
)
5574 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
5575 if (ue
->op() == OPERATOR_MULT
)
5576 return ue
->operand();
5581 // Class Binary_expression.
5586 Binary_expression::do_traverse(Traverse
* traverse
)
5588 int t
= Expression::traverse(&this->left_
, traverse
);
5589 if (t
== TRAVERSE_EXIT
)
5590 return TRAVERSE_EXIT
;
5591 return Expression::traverse(&this->right_
, traverse
);
5594 // Return whether this expression may be used as a static initializer.
5597 Binary_expression::do_is_static_initializer() const
5599 if (!this->left_
->is_static_initializer()
5600 || !this->right_
->is_static_initializer())
5603 // Addresses can be static initializers, but we can't implement
5604 // arbitray binary expressions of them.
5605 Unary_expression
* lu
= this->left_
->unary_expression();
5606 Unary_expression
* ru
= this->right_
->unary_expression();
5607 if (lu
!= NULL
&& lu
->op() == OPERATOR_AND
)
5609 if (ru
!= NULL
&& ru
->op() == OPERATOR_AND
)
5610 return this->op_
== OPERATOR_MINUS
;
5612 return this->op_
== OPERATOR_PLUS
|| this->op_
== OPERATOR_MINUS
;
5614 else if (ru
!= NULL
&& ru
->op() == OPERATOR_AND
)
5615 return this->op_
== OPERATOR_PLUS
|| this->op_
== OPERATOR_MINUS
;
5617 // Other cases should resolve in the backend.
5621 // Return the type to use for a binary operation on operands of
5622 // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
5623 // such may be NULL or abstract.
5626 Binary_expression::operation_type(Operator op
, Type
* left_type
,
5627 Type
* right_type
, Type
** result_type
)
5629 if (left_type
!= right_type
5630 && !left_type
->is_abstract()
5631 && !right_type
->is_abstract()
5632 && left_type
->base() != right_type
->base()
5633 && op
!= OPERATOR_LSHIFT
5634 && op
!= OPERATOR_RSHIFT
)
5636 // May be a type error--let it be diagnosed elsewhere.
5640 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
5642 if (left_type
->integer_type() != NULL
)
5643 *result_type
= left_type
;
5645 *result_type
= Type::make_abstract_integer_type();
5647 else if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5648 *result_type
= left_type
;
5649 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5650 *result_type
= right_type
;
5651 else if (!left_type
->is_abstract())
5652 *result_type
= left_type
;
5653 else if (!right_type
->is_abstract())
5654 *result_type
= right_type
;
5655 else if (left_type
->complex_type() != NULL
)
5656 *result_type
= left_type
;
5657 else if (right_type
->complex_type() != NULL
)
5658 *result_type
= right_type
;
5659 else if (left_type
->float_type() != NULL
)
5660 *result_type
= left_type
;
5661 else if (right_type
->float_type() != NULL
)
5662 *result_type
= right_type
;
5663 else if (left_type
->integer_type() != NULL
5664 && left_type
->integer_type()->is_rune())
5665 *result_type
= left_type
;
5666 else if (right_type
->integer_type() != NULL
5667 && right_type
->integer_type()->is_rune())
5668 *result_type
= right_type
;
5670 *result_type
= left_type
;
5675 // Convert an integer comparison code and an operator to a boolean
5679 Binary_expression::cmp_to_bool(Operator op
, int cmp
)
5686 case OPERATOR_NOTEQ
:
5703 // Compare constants according to OP.
5706 Binary_expression::compare_constant(Operator op
, Numeric_constant
* left_nc
,
5707 Numeric_constant
* right_nc
,
5708 Location location
, bool* result
)
5710 Type
* left_type
= left_nc
->type();
5711 Type
* right_type
= right_nc
->type();
5714 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
5717 // When comparing an untyped operand to a typed operand, we are
5718 // effectively coercing the untyped operand to the other operand's
5719 // type, so make sure that is valid.
5720 if (!left_nc
->set_type(type
, true, location
)
5721 || !right_nc
->set_type(type
, true, location
))
5726 if (type
->complex_type() != NULL
)
5728 if (op
!= OPERATOR_EQEQ
&& op
!= OPERATOR_NOTEQ
)
5730 ret
= Binary_expression::compare_complex(left_nc
, right_nc
, &cmp
);
5732 else if (type
->float_type() != NULL
)
5733 ret
= Binary_expression::compare_float(left_nc
, right_nc
, &cmp
);
5735 ret
= Binary_expression::compare_integer(left_nc
, right_nc
, &cmp
);
5738 *result
= Binary_expression::cmp_to_bool(op
, cmp
);
5743 // Compare integer constants.
5746 Binary_expression::compare_integer(const Numeric_constant
* left_nc
,
5747 const Numeric_constant
* right_nc
,
5751 if (!left_nc
->to_int(&left_val
))
5754 if (!right_nc
->to_int(&right_val
))
5756 mpz_clear(left_val
);
5760 *cmp
= mpz_cmp(left_val
, right_val
);
5762 mpz_clear(left_val
);
5763 mpz_clear(right_val
);
5768 // Compare floating point constants.
5771 Binary_expression::compare_float(const Numeric_constant
* left_nc
,
5772 const Numeric_constant
* right_nc
,
5776 if (!left_nc
->to_float(&left_val
))
5779 if (!right_nc
->to_float(&right_val
))
5781 mpfr_clear(left_val
);
5785 // We already coerced both operands to the same type. If that type
5786 // is not an abstract type, we need to round the values accordingly.
5787 Type
* type
= left_nc
->type();
5788 if (!type
->is_abstract() && type
->float_type() != NULL
)
5790 int bits
= type
->float_type()->bits();
5791 mpfr_prec_round(left_val
, bits
, MPFR_RNDN
);
5792 mpfr_prec_round(right_val
, bits
, MPFR_RNDN
);
5795 *cmp
= mpfr_cmp(left_val
, right_val
);
5797 mpfr_clear(left_val
);
5798 mpfr_clear(right_val
);
5803 // Compare complex constants. Complex numbers may only be compared
5807 Binary_expression::compare_complex(const Numeric_constant
* left_nc
,
5808 const Numeric_constant
* right_nc
,
5812 if (!left_nc
->to_complex(&left_val
))
5815 if (!right_nc
->to_complex(&right_val
))
5817 mpc_clear(left_val
);
5821 // We already coerced both operands to the same type. If that type
5822 // is not an abstract type, we need to round the values accordingly.
5823 Type
* type
= left_nc
->type();
5824 if (!type
->is_abstract() && type
->complex_type() != NULL
)
5826 int bits
= type
->complex_type()->bits();
5827 mpfr_prec_round(mpc_realref(left_val
), bits
/ 2, MPFR_RNDN
);
5828 mpfr_prec_round(mpc_imagref(left_val
), bits
/ 2, MPFR_RNDN
);
5829 mpfr_prec_round(mpc_realref(right_val
), bits
/ 2, MPFR_RNDN
);
5830 mpfr_prec_round(mpc_imagref(right_val
), bits
/ 2, MPFR_RNDN
);
5833 *cmp
= mpc_cmp(left_val
, right_val
) != 0;
5835 mpc_clear(left_val
);
5836 mpc_clear(right_val
);
5841 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
5842 // true if this could be done, false if not. Issue errors at LOCATION
5843 // as appropriate, and sets *ISSUED_ERROR if it did.
5846 Binary_expression::eval_constant(Operator op
, Numeric_constant
* left_nc
,
5847 Numeric_constant
* right_nc
,
5848 Location location
, Numeric_constant
* nc
,
5851 *issued_error
= false;
5855 case OPERATOR_ANDAND
:
5857 case OPERATOR_NOTEQ
:
5862 // These return boolean values, not numeric.
5868 Type
* left_type
= left_nc
->type();
5869 Type
* right_type
= right_nc
->type();
5872 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
5875 bool is_shift
= op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
;
5877 // When combining an untyped operand with a typed operand, we are
5878 // effectively coercing the untyped operand to the other operand's
5879 // type, so make sure that is valid.
5880 if (!left_nc
->set_type(type
, true, location
))
5882 if (!is_shift
&& !right_nc
->set_type(type
, true, location
))
5885 && ((left_type
->integer_type() == NULL
5886 && !left_type
->is_abstract())
5887 || (right_type
->integer_type() == NULL
5888 && !right_type
->is_abstract())))
5892 if (type
->complex_type() != NULL
)
5893 r
= Binary_expression::eval_complex(op
, left_nc
, right_nc
, location
, nc
);
5894 else if (type
->float_type() != NULL
)
5895 r
= Binary_expression::eval_float(op
, left_nc
, right_nc
, location
, nc
);
5897 r
= Binary_expression::eval_integer(op
, left_nc
, right_nc
, location
, nc
);
5901 r
= nc
->set_type(type
, true, location
);
5903 *issued_error
= true;
5909 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
5910 // integer operations. Return true if this could be done, false if
5914 Binary_expression::eval_integer(Operator op
, const Numeric_constant
* left_nc
,
5915 const Numeric_constant
* right_nc
,
5916 Location location
, Numeric_constant
* nc
)
5919 if (!left_nc
->to_int(&left_val
))
5922 if (!right_nc
->to_int(&right_val
))
5924 mpz_clear(left_val
);
5934 mpz_add(val
, left_val
, right_val
);
5935 if (mpz_sizeinbase(val
, 2) > 0x100000)
5937 go_error_at(location
, "constant addition overflow");
5942 case OPERATOR_MINUS
:
5943 mpz_sub(val
, left_val
, right_val
);
5944 if (mpz_sizeinbase(val
, 2) > 0x100000)
5946 go_error_at(location
, "constant subtraction overflow");
5952 mpz_ior(val
, left_val
, right_val
);
5955 mpz_xor(val
, left_val
, right_val
);
5958 mpz_mul(val
, left_val
, right_val
);
5959 if (mpz_sizeinbase(val
, 2) > 0x100000)
5961 go_error_at(location
, "constant multiplication overflow");
5967 if (mpz_sgn(right_val
) != 0)
5968 mpz_tdiv_q(val
, left_val
, right_val
);
5971 go_error_at(location
, "division by zero");
5977 if (mpz_sgn(right_val
) != 0)
5978 mpz_tdiv_r(val
, left_val
, right_val
);
5981 go_error_at(location
, "division by zero");
5986 case OPERATOR_LSHIFT
:
5988 unsigned long shift
= mpz_get_ui(right_val
);
5989 if (mpz_cmp_ui(right_val
, shift
) == 0 && shift
<= 0x100000)
5990 mpz_mul_2exp(val
, left_val
, shift
);
5993 go_error_at(location
, "shift count overflow");
6000 case OPERATOR_RSHIFT
:
6002 unsigned long shift
= mpz_get_ui(right_val
);
6003 if (mpz_cmp_ui(right_val
, shift
) != 0)
6005 go_error_at(location
, "shift count overflow");
6011 if (mpz_cmp_ui(left_val
, 0) >= 0)
6012 mpz_tdiv_q_2exp(val
, left_val
, shift
);
6014 mpz_fdiv_q_2exp(val
, left_val
, shift
);
6020 mpz_and(val
, left_val
, right_val
);
6022 case OPERATOR_BITCLEAR
:
6026 mpz_com(tval
, right_val
);
6027 mpz_and(val
, left_val
, tval
);
6035 mpz_clear(left_val
);
6036 mpz_clear(right_val
);
6038 if (left_nc
->is_rune()
6039 || (op
!= OPERATOR_LSHIFT
6040 && op
!= OPERATOR_RSHIFT
6041 && right_nc
->is_rune()))
6042 nc
->set_rune(NULL
, val
);
6044 nc
->set_int(NULL
, val
);
6051 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
6052 // floating point operations. Return true if this could be done,
6056 Binary_expression::eval_float(Operator op
, const Numeric_constant
* left_nc
,
6057 const Numeric_constant
* right_nc
,
6058 Location location
, Numeric_constant
* nc
)
6061 if (!left_nc
->to_float(&left_val
))
6064 if (!right_nc
->to_float(&right_val
))
6066 mpfr_clear(left_val
);
6077 mpfr_add(val
, left_val
, right_val
, MPFR_RNDN
);
6079 case OPERATOR_MINUS
:
6080 mpfr_sub(val
, left_val
, right_val
, MPFR_RNDN
);
6085 case OPERATOR_BITCLEAR
:
6087 case OPERATOR_LSHIFT
:
6088 case OPERATOR_RSHIFT
:
6089 mpfr_set_ui(val
, 0, MPFR_RNDN
);
6093 mpfr_mul(val
, left_val
, right_val
, MPFR_RNDN
);
6096 if (!mpfr_zero_p(right_val
))
6097 mpfr_div(val
, left_val
, right_val
, MPFR_RNDN
);
6100 go_error_at(location
, "division by zero");
6102 mpfr_set_ui(val
, 0, MPFR_RNDN
);
6109 mpfr_clear(left_val
);
6110 mpfr_clear(right_val
);
6112 nc
->set_float(NULL
, val
);
6118 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
6119 // complex operations. Return true if this could be done, false if
6123 Binary_expression::eval_complex(Operator op
, const Numeric_constant
* left_nc
,
6124 const Numeric_constant
* right_nc
,
6125 Location location
, Numeric_constant
* nc
)
6128 if (!left_nc
->to_complex(&left_val
))
6131 if (!right_nc
->to_complex(&right_val
))
6133 mpc_clear(left_val
);
6138 mpc_init2(val
, mpc_precision
);
6144 mpc_add(val
, left_val
, right_val
, MPC_RNDNN
);
6146 case OPERATOR_MINUS
:
6147 mpc_sub(val
, left_val
, right_val
, MPC_RNDNN
);
6152 case OPERATOR_BITCLEAR
:
6154 case OPERATOR_LSHIFT
:
6155 case OPERATOR_RSHIFT
:
6156 mpc_set_ui(val
, 0, MPC_RNDNN
);
6160 mpc_mul(val
, left_val
, right_val
, MPC_RNDNN
);
6163 if (mpc_cmp_si(right_val
, 0) == 0)
6165 go_error_at(location
, "division by zero");
6167 mpc_set_ui(val
, 0, MPC_RNDNN
);
6170 mpc_div(val
, left_val
, right_val
, MPC_RNDNN
);
6176 mpc_clear(left_val
);
6177 mpc_clear(right_val
);
6179 nc
->set_complex(NULL
, val
);
6185 // Lower a binary expression. We have to evaluate constant
6186 // expressions now, in order to implement Go's unlimited precision
6190 Binary_expression::do_lower(Gogo
* gogo
, Named_object
*,
6191 Statement_inserter
* inserter
, int)
6193 Location location
= this->location();
6194 Operator op
= this->op_
;
6195 Expression
* left
= this->left_
;
6196 Expression
* right
= this->right_
;
6198 const bool is_comparison
= (op
== OPERATOR_EQEQ
6199 || op
== OPERATOR_NOTEQ
6200 || op
== OPERATOR_LT
6201 || op
== OPERATOR_LE
6202 || op
== OPERATOR_GT
6203 || op
== OPERATOR_GE
);
6205 // Numeric constant expressions.
6207 Numeric_constant left_nc
;
6208 Numeric_constant right_nc
;
6209 if (left
->numeric_constant_value(&left_nc
)
6210 && right
->numeric_constant_value(&right_nc
))
6215 if (!Binary_expression::compare_constant(op
, &left_nc
,
6216 &right_nc
, location
,
6219 return Expression::make_boolean(result
, location
);
6223 Numeric_constant nc
;
6225 if (!Binary_expression::eval_constant(op
, &left_nc
, &right_nc
,
6230 return Expression::make_error(location
);
6233 return nc
.expression(location
);
6238 // String constant expressions.
6240 // Avoid constant folding here if the left and right types are incompatible
6241 // (leave the operation intact so that the type checker can complain about it
6242 // later on). If concatenating an abstract string with a named string type,
6243 // result type needs to be of the named type (see issue 31412).
6244 if (left
->type()->is_string_type()
6245 && right
->type()->is_string_type()
6246 && (left
->type()->named_type() == NULL
6247 || right
->type()->named_type() == NULL
6248 || left
->type()->named_type() == right
->type()->named_type()))
6250 std::string left_string
;
6251 std::string right_string
;
6252 if (left
->string_constant_value(&left_string
)
6253 && right
->string_constant_value(&right_string
))
6255 if (op
== OPERATOR_PLUS
)
6257 Type
* result_type
= (left
->type()->named_type() != NULL
6262 return Expression::make_string_typed(left_string
+ right_string
,
6263 result_type
, location
);
6265 else if (is_comparison
)
6267 int cmp
= left_string
.compare(right_string
);
6268 bool r
= Binary_expression::cmp_to_bool(op
, cmp
);
6271 return Expression::make_boolean(r
, location
);
6276 // Lower struct, array, and some interface comparisons.
6277 if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
6279 if (left
->type()->struct_type() != NULL
6280 && right
->type()->struct_type() != NULL
)
6281 return this->lower_struct_comparison(gogo
, inserter
);
6282 else if (left
->type()->array_type() != NULL
6283 && !left
->type()->is_slice_type()
6284 && right
->type()->array_type() != NULL
6285 && !right
->type()->is_slice_type())
6286 return this->lower_array_comparison(gogo
, inserter
);
6287 else if ((left
->type()->interface_type() != NULL
6288 && right
->type()->interface_type() == NULL
)
6289 || (left
->type()->interface_type() == NULL
6290 && right
->type()->interface_type() != NULL
))
6291 return this->lower_interface_value_comparison(gogo
, inserter
);
6294 // Lower string concatenation to String_concat_expression, so that
6295 // we can group sequences of string additions.
6296 if (this->left_
->type()->is_string_type() && this->op_
== OPERATOR_PLUS
)
6298 Expression_list
* exprs
;
6299 String_concat_expression
* left_sce
=
6300 this->left_
->string_concat_expression();
6301 if (left_sce
!= NULL
)
6302 exprs
= left_sce
->exprs();
6305 exprs
= new Expression_list();
6306 exprs
->push_back(this->left_
);
6309 String_concat_expression
* right_sce
=
6310 this->right_
->string_concat_expression();
6311 if (right_sce
!= NULL
)
6312 exprs
->append(right_sce
->exprs());
6314 exprs
->push_back(this->right_
);
6316 return Expression::make_string_concat(exprs
);
6322 // Lower a struct comparison.
6325 Binary_expression::lower_struct_comparison(Gogo
* gogo
,
6326 Statement_inserter
* inserter
)
6328 Struct_type
* st
= this->left_
->type()->struct_type();
6329 Struct_type
* st2
= this->right_
->type()->struct_type();
6333 && !Type::are_identical(st
, st2
,
6334 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
6337 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
6338 this->right_
->type(), NULL
))
6341 // See if we can compare using memcmp. As a heuristic, we use
6342 // memcmp rather than field references and comparisons if there are
6343 // more than two fields.
6344 if (st
->compare_is_identity(gogo
) && st
->total_field_count() > 2)
6345 return this->lower_compare_to_memcmp(gogo
, inserter
);
6347 Location loc
= this->location();
6349 Expression
* left
= this->left_
;
6350 Temporary_statement
* left_temp
= NULL
;
6351 if (left
->var_expression() == NULL
6352 && left
->temporary_reference_expression() == NULL
)
6354 left_temp
= Statement::make_temporary(left
->type(), NULL
, loc
);
6355 inserter
->insert(left_temp
);
6356 left
= Expression::make_set_and_use_temporary(left_temp
, left
, loc
);
6359 Expression
* right
= this->right_
;
6360 Temporary_statement
* right_temp
= NULL
;
6361 if (right
->var_expression() == NULL
6362 && right
->temporary_reference_expression() == NULL
)
6364 right_temp
= Statement::make_temporary(right
->type(), NULL
, loc
);
6365 inserter
->insert(right_temp
);
6366 right
= Expression::make_set_and_use_temporary(right_temp
, right
, loc
);
6369 Expression
* ret
= Expression::make_boolean(true, loc
);
6370 const Struct_field_list
* fields
= st
->fields();
6371 unsigned int field_index
= 0;
6372 for (Struct_field_list::const_iterator pf
= fields
->begin();
6373 pf
!= fields
->end();
6374 ++pf
, ++field_index
)
6376 if (Gogo::is_sink_name(pf
->field_name()))
6379 if (field_index
> 0)
6381 if (left_temp
== NULL
)
6382 left
= left
->copy();
6384 left
= Expression::make_temporary_reference(left_temp
, loc
);
6385 if (right_temp
== NULL
)
6386 right
= right
->copy();
6388 right
= Expression::make_temporary_reference(right_temp
, loc
);
6390 Expression
* f1
= Expression::make_field_reference(left
, field_index
,
6392 Expression
* f2
= Expression::make_field_reference(right
, field_index
,
6394 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, f1
, f2
, loc
);
6395 ret
= Expression::make_binary(OPERATOR_ANDAND
, ret
, cond
, loc
);
6398 if (this->op_
== OPERATOR_NOTEQ
)
6399 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
6404 // Lower an array comparison.
6407 Binary_expression::lower_array_comparison(Gogo
* gogo
,
6408 Statement_inserter
* inserter
)
6410 Array_type
* at
= this->left_
->type()->array_type();
6411 Array_type
* at2
= this->right_
->type()->array_type();
6415 && !Type::are_identical(at
, at2
,
6416 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
6419 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
6420 this->right_
->type(), NULL
))
6423 // Call memcmp directly if possible. This may let the middle-end
6424 // optimize the call.
6425 if (at
->compare_is_identity(gogo
))
6426 return this->lower_compare_to_memcmp(gogo
, inserter
);
6428 // Call the array comparison function.
6429 Named_object
* equal_fn
=
6430 at
->equal_function(gogo
, this->left_
->type()->named_type(), NULL
);
6432 Location loc
= this->location();
6434 Expression
* func
= Expression::make_func_reference(equal_fn
, NULL
, loc
);
6436 Expression_list
* args
= new Expression_list();
6437 args
->push_back(this->operand_address(inserter
, this->left_
));
6438 args
->push_back(this->operand_address(inserter
, this->right_
));
6440 Call_expression
* ce
= Expression::make_call(func
, args
, false, loc
);
6442 // Record that this is a call to a generated equality function. We
6443 // need to do this because a comparison returns an abstract boolean
6444 // type, but the function necessarily returns "bool". The
6445 // difference shows up in code like
6447 // var b mybool = [10]string{} == [10]string{}
6448 // The comparison function returns "bool", but since a comparison
6449 // has an abstract boolean type we need an implicit conversion to
6450 // "mybool". The implicit conversion is inserted in
6451 // Call_expression::do_flatten.
6452 ce
->set_is_equal_function();
6454 Expression
* ret
= ce
;
6455 if (this->op_
== OPERATOR_NOTEQ
)
6456 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
6461 // Lower an interface to value comparison.
6464 Binary_expression::lower_interface_value_comparison(Gogo
*,
6465 Statement_inserter
* inserter
)
6467 Type
* left_type
= this->left_
->type();
6468 Type
* right_type
= this->right_
->type();
6469 Interface_type
* ift
;
6470 if (left_type
->interface_type() != NULL
)
6472 ift
= left_type
->interface_type();
6473 if (!ift
->implements_interface(right_type
, NULL
))
6478 ift
= right_type
->interface_type();
6479 if (!ift
->implements_interface(left_type
, NULL
))
6482 if (!Type::are_compatible_for_comparison(true, left_type
, right_type
, NULL
))
6485 Location loc
= this->location();
6487 if (left_type
->interface_type() == NULL
6488 && left_type
->points_to() == NULL
6489 && !this->left_
->is_addressable())
6491 Temporary_statement
* temp
=
6492 Statement::make_temporary(left_type
, NULL
, loc
);
6493 inserter
->insert(temp
);
6495 Expression::make_set_and_use_temporary(temp
, this->left_
, loc
);
6498 if (right_type
->interface_type() == NULL
6499 && right_type
->points_to() == NULL
6500 && !this->right_
->is_addressable())
6502 Temporary_statement
* temp
=
6503 Statement::make_temporary(right_type
, NULL
, loc
);
6504 inserter
->insert(temp
);
6506 Expression::make_set_and_use_temporary(temp
, this->right_
, loc
);
6512 // Lower a struct or array comparison to a call to memcmp.
6515 Binary_expression::lower_compare_to_memcmp(Gogo
*, Statement_inserter
* inserter
)
6517 Location loc
= this->location();
6519 Expression
* a1
= this->operand_address(inserter
, this->left_
);
6520 Expression
* a2
= this->operand_address(inserter
, this->right_
);
6521 Expression
* len
= Expression::make_type_info(this->left_
->type(),
6524 Expression
* call
= Runtime::make_call(Runtime::MEMCMP
, loc
, 3, a1
, a2
, len
);
6525 Type
* int32_type
= Type::lookup_integer_type("int32");
6526 Expression
* zero
= Expression::make_integer_ul(0, int32_type
, loc
);
6527 return Expression::make_binary(this->op_
, call
, zero
, loc
);
6531 Binary_expression::do_flatten(Gogo
* gogo
, Named_object
*,
6532 Statement_inserter
* inserter
)
6534 Location loc
= this->location();
6535 if (this->left_
->type()->is_error_type()
6536 || this->right_
->type()->is_error_type()
6537 || this->left_
->is_error_expression()
6538 || this->right_
->is_error_expression())
6540 go_assert(saw_errors());
6541 return Expression::make_error(loc
);
6544 Temporary_statement
* temp
;
6546 Type
* left_type
= this->left_
->type();
6547 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
6548 || this->op_
== OPERATOR_RSHIFT
);
6549 bool is_idiv_op
= ((this->op_
== OPERATOR_DIV
&&
6550 left_type
->integer_type() != NULL
)
6551 || this->op_
== OPERATOR_MOD
);
6552 bool is_string_op
= (left_type
->is_string_type()
6553 && this->right_
->type()->is_string_type());
6557 // Mark string([]byte) operands to reuse the backing store.
6558 // String comparison does not keep the reference, so it is safe.
6559 Type_conversion_expression
* lce
=
6560 this->left_
->conversion_expression();
6561 if (lce
!= NULL
&& lce
->expr()->type()->is_slice_type())
6562 lce
->set_no_copy(true);
6563 Type_conversion_expression
* rce
=
6564 this->right_
->conversion_expression();
6565 if (rce
!= NULL
&& rce
->expr()->type()->is_slice_type())
6566 rce
->set_no_copy(true);
6571 && (gogo
->check_divide_by_zero() || gogo
->check_divide_overflow()))
6574 if (!this->left_
->is_multi_eval_safe())
6576 temp
= Statement::make_temporary(NULL
, this->left_
, loc
);
6577 inserter
->insert(temp
);
6578 this->left_
= Expression::make_temporary_reference(temp
, loc
);
6580 if (!this->right_
->is_multi_eval_safe())
6583 Statement::make_temporary(NULL
, this->right_
, loc
);
6584 this->right_
= Expression::make_temporary_reference(temp
, loc
);
6585 inserter
->insert(temp
);
6592 // Return the address of EXPR, cast to unsafe.Pointer.
6595 Binary_expression::operand_address(Statement_inserter
* inserter
,
6598 Location loc
= this->location();
6600 if (!expr
->is_addressable())
6602 Temporary_statement
* temp
= Statement::make_temporary(expr
->type(), NULL
,
6604 inserter
->insert(temp
);
6605 expr
= Expression::make_set_and_use_temporary(temp
, expr
, loc
);
6607 expr
= Expression::make_unary(OPERATOR_AND
, expr
, loc
);
6608 static_cast<Unary_expression
*>(expr
)->set_does_not_escape();
6609 Type
* void_type
= Type::make_void_type();
6610 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
6611 return Expression::make_cast(unsafe_pointer_type
, expr
, loc
);
6614 // Return the numeric constant value, if it has one.
6617 Binary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
6619 Numeric_constant left_nc
;
6620 if (!this->left_
->numeric_constant_value(&left_nc
))
6622 Numeric_constant right_nc
;
6623 if (!this->right_
->numeric_constant_value(&right_nc
))
6626 return Binary_expression::eval_constant(this->op_
, &left_nc
, &right_nc
,
6627 this->location(), nc
, &issued_error
);
6630 // Return the boolean constant value, if it has one.
6633 Binary_expression::do_boolean_constant_value(bool* val
) const
6635 bool is_comparison
= false;
6639 case OPERATOR_NOTEQ
:
6644 is_comparison
= true;
6646 case OPERATOR_ANDAND
:
6653 Numeric_constant left_nc
, right_nc
;
6655 && this->left_
->numeric_constant_value(&left_nc
)
6656 && this->right_
->numeric_constant_value(&right_nc
))
6657 return Binary_expression::compare_constant(this->op_
, &left_nc
,
6662 std::string left_str
, right_str
;
6664 && this->left_
->string_constant_value(&left_str
)
6665 && this->right_
->string_constant_value(&right_str
))
6667 *val
= Binary_expression::cmp_to_bool(this->op_
,
6668 left_str
.compare(right_str
));
6673 if (this->left_
->boolean_constant_value(&left_bval
))
6675 if (this->op_
== OPERATOR_ANDAND
&& !left_bval
)
6680 else if (this->op_
== OPERATOR_OROR
&& left_bval
)
6687 if (this->right_
->boolean_constant_value(&right_bval
))
6692 *val
= (left_bval
== right_bval
);
6694 case OPERATOR_NOTEQ
:
6695 *val
= (left_bval
!= right_bval
);
6697 case OPERATOR_ANDAND
:
6710 // Note that the value is being discarded.
6713 Binary_expression::do_discarding_value()
6715 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
6716 return this->right_
->discarding_value();
6719 this->unused_value_error();
6727 Binary_expression::do_type()
6729 if (this->classification() == EXPRESSION_ERROR
)
6730 return Type::make_error_type();
6735 case OPERATOR_NOTEQ
:
6740 if (this->type_
== NULL
)
6741 this->type_
= Type::make_boolean_type();
6745 case OPERATOR_MINUS
:
6752 case OPERATOR_BITCLEAR
:
6754 case OPERATOR_ANDAND
:
6757 if (!Binary_expression::operation_type(this->op_
,
6758 this->left_
->type(),
6759 this->right_
->type(),
6761 return Type::make_error_type();
6765 case OPERATOR_LSHIFT
:
6766 case OPERATOR_RSHIFT
:
6767 return this->left_
->type();
6774 // Set type for a binary expression.
6777 Binary_expression::do_determine_type(const Type_context
* context
)
6779 Type
* tleft
= this->left_
->type();
6780 Type
* tright
= this->right_
->type();
6782 // Both sides should have the same type, except for the shift
6783 // operations. For a comparison, we should ignore the incoming
6786 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
6787 || this->op_
== OPERATOR_RSHIFT
);
6789 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
6790 || this->op_
== OPERATOR_NOTEQ
6791 || this->op_
== OPERATOR_LT
6792 || this->op_
== OPERATOR_LE
6793 || this->op_
== OPERATOR_GT
6794 || this->op_
== OPERATOR_GE
);
6796 // For constant expressions, the context of the result is not useful in
6797 // determining the types of the operands. It is only legal to use abstract
6798 // boolean, numeric, and string constants as operands where it is legal to
6799 // use non-abstract boolean, numeric, and string constants, respectively.
6800 // Any issues with the operation will be resolved in the check_types pass.
6801 bool is_constant_expr
= (this->left_
->is_constant()
6802 && this->right_
->is_constant());
6804 Type_context
subcontext(*context
);
6806 if (is_constant_expr
&& !is_shift_op
)
6808 subcontext
.type
= NULL
;
6809 subcontext
.may_be_abstract
= true;
6811 else if (is_comparison
)
6813 // In a comparison, the context does not determine the types of
6815 subcontext
.type
= NULL
;
6818 // Set the context for the left hand operand.
6821 // The right hand operand of a shift plays no role in
6822 // determining the type of the left hand operand.
6824 else if (!tleft
->is_abstract())
6825 subcontext
.type
= tleft
;
6826 else if (!tright
->is_abstract())
6827 subcontext
.type
= tright
;
6828 else if (subcontext
.type
== NULL
)
6830 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
6831 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
6832 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
)
6833 || (tleft
->is_boolean_type() && tright
->is_boolean_type()))
6835 // Both sides have an abstract integer, abstract float,
6836 // abstract complex, or abstract boolean type. Just let
6837 // CONTEXT determine whether they may remain abstract or not.
6839 else if (tleft
->complex_type() != NULL
)
6840 subcontext
.type
= tleft
;
6841 else if (tright
->complex_type() != NULL
)
6842 subcontext
.type
= tright
;
6843 else if (tleft
->float_type() != NULL
)
6844 subcontext
.type
= tleft
;
6845 else if (tright
->float_type() != NULL
)
6846 subcontext
.type
= tright
;
6848 subcontext
.type
= tleft
;
6850 if (subcontext
.type
!= NULL
&& !context
->may_be_abstract
)
6851 subcontext
.type
= subcontext
.type
->make_non_abstract_type();
6854 this->left_
->determine_type(&subcontext
);
6858 // We may have inherited an unusable type for the shift operand.
6859 // Give a useful error if that happened.
6860 if (tleft
->is_abstract()
6861 && subcontext
.type
!= NULL
6862 && !subcontext
.may_be_abstract
6863 && subcontext
.type
->interface_type() == NULL
6864 && subcontext
.type
->integer_type() == NULL
)
6865 this->report_error(("invalid context-determined non-integer type "
6866 "for left operand of shift"));
6868 // The context for the right hand operand is the same as for the
6869 // left hand operand, except for a shift operator.
6870 subcontext
.type
= Type::lookup_integer_type("uint");
6871 subcontext
.may_be_abstract
= false;
6874 this->right_
->determine_type(&subcontext
);
6878 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
6880 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
6881 this->type_
= context
->type
;
6882 else if (!context
->may_be_abstract
)
6883 this->type_
= Type::lookup_bool_type();
6887 // Report an error if the binary operator OP does not support TYPE.
6888 // OTYPE is the type of the other operand. Return whether the
6889 // operation is OK. This should not be used for shift.
6892 Binary_expression::check_operator_type(Operator op
, Type
* type
, Type
* otype
,
6898 case OPERATOR_ANDAND
:
6899 if (!type
->is_boolean_type()
6900 || !otype
->is_boolean_type())
6902 go_error_at(location
, "expected boolean type");
6908 case OPERATOR_NOTEQ
:
6911 if (!Type::are_compatible_for_comparison(true, type
, otype
, &reason
))
6913 go_error_at(location
, "%s", reason
.c_str());
6925 if (!Type::are_compatible_for_comparison(false, type
, otype
, &reason
))
6927 go_error_at(location
, "%s", reason
.c_str());
6934 case OPERATOR_PLUSEQ
:
6935 if ((!type
->is_numeric_type() && !type
->is_string_type())
6936 || (!otype
->is_numeric_type() && !otype
->is_string_type()))
6938 go_error_at(location
,
6939 "expected integer, floating, complex, or string type");
6944 case OPERATOR_MINUS
:
6945 case OPERATOR_MINUSEQ
:
6947 case OPERATOR_MULTEQ
:
6949 case OPERATOR_DIVEQ
:
6950 if (!type
->is_numeric_type() || !otype
->is_numeric_type())
6952 go_error_at(location
, "expected integer, floating, or complex type");
6958 case OPERATOR_MODEQ
:
6962 case OPERATOR_ANDEQ
:
6964 case OPERATOR_XOREQ
:
6965 case OPERATOR_BITCLEAR
:
6966 case OPERATOR_BITCLEAREQ
:
6967 if (type
->integer_type() == NULL
|| otype
->integer_type() == NULL
)
6969 go_error_at(location
, "expected integer type");
6984 Binary_expression::do_check_types(Gogo
*)
6986 if (this->classification() == EXPRESSION_ERROR
)
6989 Type
* left_type
= this->left_
->type();
6990 Type
* right_type
= this->right_
->type();
6991 if (left_type
->is_error() || right_type
->is_error())
6993 this->set_is_error();
6997 if (this->op_
== OPERATOR_EQEQ
6998 || this->op_
== OPERATOR_NOTEQ
6999 || this->op_
== OPERATOR_LT
7000 || this->op_
== OPERATOR_LE
7001 || this->op_
== OPERATOR_GT
7002 || this->op_
== OPERATOR_GE
)
7004 if (left_type
->is_nil_type() && right_type
->is_nil_type())
7006 this->report_error(_("invalid comparison of nil with nil"));
7009 if (!Type::are_assignable(left_type
, right_type
, NULL
)
7010 && !Type::are_assignable(right_type
, left_type
, NULL
))
7012 this->report_error(_("incompatible types in binary expression"));
7015 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
7018 || !Binary_expression::check_operator_type(this->op_
, right_type
,
7022 this->set_is_error();
7026 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
7028 if (!Type::are_compatible_for_binop(left_type
, right_type
))
7030 this->report_error(_("incompatible types in binary expression"));
7033 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
7037 this->set_is_error();
7040 if (this->op_
== OPERATOR_DIV
|| this->op_
== OPERATOR_MOD
)
7042 // Division by a zero integer constant is an error.
7043 Numeric_constant rconst
;
7045 if (left_type
->integer_type() != NULL
7046 && this->right_
->numeric_constant_value(&rconst
)
7047 && rconst
.to_unsigned_long(&rval
) == Numeric_constant::NC_UL_VALID
7050 this->report_error(_("integer division by zero"));
7057 if (left_type
->integer_type() == NULL
)
7058 this->report_error(_("shift of non-integer operand"));
7060 if (right_type
->is_string_type())
7061 this->report_error(_("shift count not integer"));
7062 else if (!right_type
->is_abstract()
7063 && right_type
->integer_type() == NULL
)
7064 this->report_error(_("shift count not integer"));
7067 Numeric_constant nc
;
7068 if (this->right_
->numeric_constant_value(&nc
))
7071 if (!nc
.to_int(&val
))
7072 this->report_error(_("shift count not integer"));
7075 if (mpz_sgn(val
) < 0)
7077 this->report_error(_("negative shift count"));
7078 Location rloc
= this->right_
->location();
7079 this->right_
= Expression::make_integer_ul(0, right_type
,
7089 // Get the backend representation for a binary expression.
7092 Binary_expression::do_get_backend(Translate_context
* context
)
7094 Gogo
* gogo
= context
->gogo();
7095 Location loc
= this->location();
7096 Type
* left_type
= this->left_
->type();
7097 Type
* right_type
= this->right_
->type();
7099 bool use_left_type
= true;
7100 bool is_shift_op
= false;
7101 bool is_idiv_op
= false;
7105 case OPERATOR_NOTEQ
:
7110 return Expression::comparison(context
, this->type_
, this->op_
,
7111 this->left_
, this->right_
, loc
);
7114 case OPERATOR_ANDAND
:
7115 use_left_type
= false;
7118 case OPERATOR_MINUS
:
7124 if (left_type
->float_type() != NULL
|| left_type
->complex_type() != NULL
)
7130 case OPERATOR_LSHIFT
:
7131 case OPERATOR_RSHIFT
:
7134 case OPERATOR_BITCLEAR
:
7135 this->right_
= Expression::make_unary(OPERATOR_XOR
, this->right_
, loc
);
7142 // The only binary operation for string is +, and that should have
7143 // been converted to a String_concat_expression in do_lower.
7144 go_assert(!left_type
->is_string_type());
7146 Bexpression
* left
= this->left_
->get_backend(context
);
7147 Bexpression
* right
= this->right_
->get_backend(context
);
7149 Type
* type
= use_left_type
? left_type
: right_type
;
7150 Btype
* btype
= type
->get_backend(gogo
);
7153 gogo
->backend()->binary_expression(this->op_
, left
, right
, loc
);
7154 ret
= gogo
->backend()->convert_expression(btype
, ret
, loc
);
7156 // Initialize overflow constants.
7157 Bexpression
* overflow
;
7159 mpz_init_set_ui(zero
, 0UL);
7161 mpz_init_set_ui(one
, 1UL);
7163 mpz_init_set_si(neg_one
, -1);
7165 Btype
* left_btype
= left_type
->get_backend(gogo
);
7166 Btype
* right_btype
= right_type
->get_backend(gogo
);
7168 // In Go, a shift larger than the size of the type is well-defined.
7169 // This is not true in C, so we need to insert a conditional.
7170 // We also need to check for a negative shift count.
7173 go_assert(left_type
->integer_type() != NULL
);
7174 go_assert(right_type
->integer_type() != NULL
);
7176 int bits
= left_type
->integer_type()->bits();
7178 Numeric_constant nc
;
7180 if (!this->right_
->numeric_constant_value(&nc
)
7181 || nc
.to_unsigned_long(&ul
) != Numeric_constant::NC_UL_VALID
7182 || ul
>= static_cast<unsigned long>(bits
))
7185 mpz_init_set_ui(bitsval
, bits
);
7186 Bexpression
* bits_expr
=
7187 gogo
->backend()->integer_constant_expression(right_btype
, bitsval
);
7188 Bexpression
* compare
=
7189 gogo
->backend()->binary_expression(OPERATOR_LT
,
7190 right
, bits_expr
, loc
);
7192 Bexpression
* zero_expr
=
7193 gogo
->backend()->integer_constant_expression(left_btype
, zero
);
7194 overflow
= zero_expr
;
7195 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
7196 if (this->op_
== OPERATOR_RSHIFT
7197 && !left_type
->integer_type()->is_unsigned())
7199 Bexpression
* neg_expr
=
7200 gogo
->backend()->binary_expression(OPERATOR_LT
, left
,
7202 Bexpression
* neg_one_expr
=
7203 gogo
->backend()->integer_constant_expression(left_btype
,
7205 overflow
= gogo
->backend()->conditional_expression(bfn
,
7212 ret
= gogo
->backend()->conditional_expression(bfn
, btype
, compare
,
7213 ret
, overflow
, loc
);
7217 if (!right_type
->integer_type()->is_unsigned()
7218 && (!this->right_
->numeric_constant_value(&nc
)
7219 || nc
.to_unsigned_long(&ul
) != Numeric_constant::NC_UL_VALID
))
7221 Bexpression
* zero_expr
=
7222 gogo
->backend()->integer_constant_expression(right_btype
, zero
);
7223 Bexpression
* compare
=
7224 gogo
->backend()->binary_expression(OPERATOR_LT
, right
, zero_expr
,
7226 Expression
* crash
= Runtime::make_call(Runtime::PANIC_SHIFT
,
7228 Bexpression
* bcrash
= crash
->get_backend(context
);
7229 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
7230 ret
= gogo
->backend()->conditional_expression(bfn
, btype
, compare
,
7235 // Add checks for division by zero and division overflow as needed.
7238 if (gogo
->check_divide_by_zero())
7241 Bexpression
* zero_expr
=
7242 gogo
->backend()->integer_constant_expression(right_btype
, zero
);
7243 Bexpression
* check
=
7244 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
7245 right
, zero_expr
, loc
);
7247 Expression
* crash
= Runtime::make_call(Runtime::PANIC_DIVIDE
,
7249 Bexpression
* bcrash
= crash
->get_backend(context
);
7251 // right == 0 ? (panicdivide(), 0) : ret
7252 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
7253 ret
= gogo
->backend()->conditional_expression(bfn
, btype
,
7258 if (gogo
->check_divide_overflow())
7261 // FIXME: It would be nice to say that this test is expected
7264 Bexpression
* neg_one_expr
=
7265 gogo
->backend()->integer_constant_expression(right_btype
, neg_one
);
7266 Bexpression
* check
=
7267 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
7268 right
, neg_one_expr
, loc
);
7270 Bexpression
* zero_expr
=
7271 gogo
->backend()->integer_constant_expression(btype
, zero
);
7272 Bexpression
* one_expr
=
7273 gogo
->backend()->integer_constant_expression(btype
, one
);
7274 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
7276 if (type
->integer_type()->is_unsigned())
7278 // An unsigned -1 is the largest possible number, so
7279 // dividing is always 1 or 0.
7282 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
7284 if (this->op_
== OPERATOR_DIV
)
7286 gogo
->backend()->conditional_expression(bfn
, btype
, cmp
,
7287 one_expr
, zero_expr
,
7291 gogo
->backend()->conditional_expression(bfn
, btype
, cmp
,
7297 // Computing left / -1 is the same as computing - left,
7298 // which does not overflow since Go sets -fwrapv.
7299 if (this->op_
== OPERATOR_DIV
)
7301 Expression
* negate_expr
=
7302 Expression::make_unary(OPERATOR_MINUS
, this->left_
, loc
);
7303 overflow
= negate_expr
->get_backend(context
);
7306 overflow
= zero_expr
;
7308 overflow
= gogo
->backend()->convert_expression(btype
, overflow
, loc
);
7310 // right == -1 ? - left : ret
7311 ret
= gogo
->backend()->conditional_expression(bfn
, btype
,
7323 // Export a binary expression.
7326 Binary_expression::do_export(Export_function_body
* efb
) const
7328 efb
->write_c_string("(");
7329 this->left_
->export_expression(efb
);
7333 efb
->write_c_string(" || ");
7335 case OPERATOR_ANDAND
:
7336 efb
->write_c_string(" && ");
7339 efb
->write_c_string(" == ");
7341 case OPERATOR_NOTEQ
:
7342 efb
->write_c_string(" != ");
7345 efb
->write_c_string(" < ");
7348 efb
->write_c_string(" <= ");
7351 efb
->write_c_string(" > ");
7354 efb
->write_c_string(" >= ");
7357 efb
->write_c_string(" + ");
7359 case OPERATOR_MINUS
:
7360 efb
->write_c_string(" - ");
7363 efb
->write_c_string(" | ");
7366 efb
->write_c_string(" ^ ");
7369 efb
->write_c_string(" * ");
7372 efb
->write_c_string(" / ");
7375 efb
->write_c_string(" % ");
7377 case OPERATOR_LSHIFT
:
7378 efb
->write_c_string(" << ");
7380 case OPERATOR_RSHIFT
:
7381 efb
->write_c_string(" >> ");
7384 efb
->write_c_string(" & ");
7386 case OPERATOR_BITCLEAR
:
7387 efb
->write_c_string(" &^ ");
7392 this->right_
->export_expression(efb
);
7393 efb
->write_c_string(")");
7396 // Import a binary expression.
7399 Binary_expression::do_import(Import_expression
* imp
, Location loc
)
7401 imp
->require_c_string("(");
7403 Expression
* left
= Expression::import_expression(imp
, loc
);
7406 if (imp
->match_c_string(" || "))
7411 else if (imp
->match_c_string(" && "))
7413 op
= OPERATOR_ANDAND
;
7416 else if (imp
->match_c_string(" == "))
7421 else if (imp
->match_c_string(" != "))
7423 op
= OPERATOR_NOTEQ
;
7426 else if (imp
->match_c_string(" < "))
7431 else if (imp
->match_c_string(" <= "))
7436 else if (imp
->match_c_string(" > "))
7441 else if (imp
->match_c_string(" >= "))
7446 else if (imp
->match_c_string(" + "))
7451 else if (imp
->match_c_string(" - "))
7453 op
= OPERATOR_MINUS
;
7456 else if (imp
->match_c_string(" | "))
7461 else if (imp
->match_c_string(" ^ "))
7466 else if (imp
->match_c_string(" * "))
7471 else if (imp
->match_c_string(" / "))
7476 else if (imp
->match_c_string(" % "))
7481 else if (imp
->match_c_string(" << "))
7483 op
= OPERATOR_LSHIFT
;
7486 else if (imp
->match_c_string(" >> "))
7488 op
= OPERATOR_RSHIFT
;
7491 else if (imp
->match_c_string(" & "))
7496 else if (imp
->match_c_string(" &^ "))
7498 op
= OPERATOR_BITCLEAR
;
7501 else if (imp
->match_c_string(")"))
7503 // Not a binary operator after all.
7509 go_error_at(imp
->location(), "unrecognized binary operator");
7510 return Expression::make_error(loc
);
7513 Expression
* right
= Expression::import_expression(imp
, loc
);
7515 imp
->require_c_string(")");
7517 return Expression::make_binary(op
, left
, right
, loc
);
7520 // Dump ast representation of a binary expression.
7523 Binary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
7525 ast_dump_context
->ostream() << "(";
7526 ast_dump_context
->dump_expression(this->left_
);
7527 ast_dump_context
->ostream() << " ";
7528 ast_dump_context
->dump_operator(this->op_
);
7529 ast_dump_context
->ostream() << " ";
7530 ast_dump_context
->dump_expression(this->right_
);
7531 ast_dump_context
->ostream() << ") ";
7534 // Make a binary expression.
7537 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
7540 return new Binary_expression(op
, left
, right
, location
);
7543 // Implement a comparison.
7546 Expression::comparison(Translate_context
* context
, Type
* result_type
,
7547 Operator op
, Expression
* left
, Expression
* right
,
7550 Type
* left_type
= left
->type();
7551 Type
* right_type
= right
->type();
7553 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, location
);
7555 if (left_type
->is_string_type() && right_type
->is_string_type())
7557 go_assert(left
->is_multi_eval_safe());
7558 go_assert(right
->is_multi_eval_safe());
7560 if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
7563 // ? (l.ptr == r.ptr ? true : memcmp(l.ptr, r.ptr, r.len) == 0)
7565 Expression
* llen
= Expression::make_string_info(left
,
7568 Expression
* rlen
= Expression::make_string_info(right
,
7571 Expression
* leneq
= Expression::make_binary(OPERATOR_EQEQ
, llen
, rlen
,
7573 Expression
* lptr
= Expression::make_string_info(left
->copy(),
7576 Expression
* rptr
= Expression::make_string_info(right
->copy(),
7579 Expression
* ptreq
= Expression::make_binary(OPERATOR_EQEQ
, lptr
, rptr
,
7581 Expression
* btrue
= Expression::make_boolean(true, location
);
7582 Expression
* call
= Runtime::make_call(Runtime::MEMCMP
, location
, 3,
7583 lptr
->copy(), rptr
->copy(),
7585 Type
* int32_type
= Type::lookup_integer_type("int32");
7586 Expression
* zero
= Expression::make_integer_ul(0, int32_type
, location
);
7587 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
, call
, zero
,
7589 Expression
* cond
= Expression::make_conditional(ptreq
, btrue
, cmp
,
7591 Expression
* bfalse
= Expression::make_boolean(false, location
);
7592 left
= Expression::make_conditional(leneq
, cond
, bfalse
, location
);
7593 right
= Expression::make_boolean(true, location
);
7597 left
= Runtime::make_call(Runtime::CMPSTRING
, location
, 2,
7602 else if ((left_type
->interface_type() != NULL
7603 && right_type
->interface_type() == NULL
7604 && !right_type
->is_nil_type())
7605 || (left_type
->interface_type() == NULL
7606 && !left_type
->is_nil_type()
7607 && right_type
->interface_type() != NULL
))
7609 // Comparing an interface value to a non-interface value.
7610 if (left_type
->interface_type() == NULL
)
7612 std::swap(left_type
, right_type
);
7613 std::swap(left
, right
);
7616 // The right operand is not an interface. We need to take its
7617 // address if it is not a direct interface type.
7618 Expression
* pointer_arg
= NULL
;
7619 if (right_type
->is_direct_iface_type())
7620 pointer_arg
= Expression::unpack_direct_iface(right
, location
);
7623 go_assert(right
->is_addressable());
7624 pointer_arg
= Expression::make_unary(OPERATOR_AND
, right
,
7628 Expression
* descriptor
=
7629 Expression::make_type_descriptor(right_type
, location
);
7631 Runtime::make_call((left_type
->interface_type()->is_empty()
7632 ? Runtime::EFACEVALEQ
7633 : Runtime::IFACEVALEQ
),
7634 location
, 3, left
, descriptor
,
7636 go_assert(op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
);
7637 right
= Expression::make_boolean(true, location
);
7639 else if (left_type
->interface_type() != NULL
7640 && right_type
->interface_type() != NULL
)
7642 Runtime::Function compare_function
;
7643 if (left_type
->interface_type()->is_empty()
7644 && right_type
->interface_type()->is_empty())
7645 compare_function
= Runtime::EFACEEQ
;
7646 else if (!left_type
->interface_type()->is_empty()
7647 && !right_type
->interface_type()->is_empty())
7648 compare_function
= Runtime::IFACEEQ
;
7651 if (left_type
->interface_type()->is_empty())
7653 std::swap(left_type
, right_type
);
7654 std::swap(left
, right
);
7656 go_assert(!left_type
->interface_type()->is_empty());
7657 go_assert(right_type
->interface_type()->is_empty());
7658 compare_function
= Runtime::IFACEEFACEEQ
;
7661 left
= Runtime::make_call(compare_function
, location
, 2, left
, right
);
7662 go_assert(op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
);
7663 right
= Expression::make_boolean(true, location
);
7666 if (left_type
->is_nil_type()
7667 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
7669 std::swap(left_type
, right_type
);
7670 std::swap(left
, right
);
7673 if (right_type
->is_nil_type())
7675 right
= Expression::make_nil(location
);
7676 if (left_type
->array_type() != NULL
7677 && left_type
->array_type()->length() == NULL
)
7679 Array_type
* at
= left_type
->array_type();
7680 left
= at
->get_value_pointer(context
->gogo(), left
);
7682 else if (left_type
->interface_type() != NULL
)
7684 // An interface is nil if the first field is nil.
7685 left
= Expression::make_field_reference(left
, 0, location
);
7689 Bexpression
* left_bexpr
= left
->get_backend(context
);
7690 Bexpression
* right_bexpr
= right
->get_backend(context
);
7692 Gogo
* gogo
= context
->gogo();
7693 Bexpression
* ret
= gogo
->backend()->binary_expression(op
, left_bexpr
,
7694 right_bexpr
, location
);
7695 if (result_type
!= NULL
)
7696 ret
= gogo
->backend()->convert_expression(result_type
->get_backend(gogo
),
7701 // Class String_concat_expression.
7704 String_concat_expression::do_is_constant() const
7706 for (Expression_list::const_iterator pe
= this->exprs_
->begin();
7707 pe
!= this->exprs_
->end();
7710 if (!(*pe
)->is_constant())
7717 String_concat_expression::do_is_zero_value() const
7719 for (Expression_list::const_iterator pe
= this->exprs_
->begin();
7720 pe
!= this->exprs_
->end();
7723 if (!(*pe
)->is_zero_value())
7730 String_concat_expression::do_is_static_initializer() const
7732 for (Expression_list::const_iterator pe
= this->exprs_
->begin();
7733 pe
!= this->exprs_
->end();
7736 if (!(*pe
)->is_static_initializer())
7743 String_concat_expression::do_type()
7745 Type
* t
= this->exprs_
->front()->type();
7746 Expression_list::iterator pe
= this->exprs_
->begin();
7748 for (; pe
!= this->exprs_
->end(); ++pe
)
7751 if (!Binary_expression::operation_type(OPERATOR_PLUS
, t
,
7754 return Type::make_error_type();
7761 String_concat_expression::do_determine_type(const Type_context
* context
)
7763 Type_context
subcontext(*context
);
7764 for (Expression_list::iterator pe
= this->exprs_
->begin();
7765 pe
!= this->exprs_
->end();
7768 Type
* t
= (*pe
)->type();
7769 if (!t
->is_abstract())
7771 subcontext
.type
= t
;
7775 if (subcontext
.type
== NULL
)
7776 subcontext
.type
= this->exprs_
->front()->type();
7777 for (Expression_list::iterator pe
= this->exprs_
->begin();
7778 pe
!= this->exprs_
->end();
7780 (*pe
)->determine_type(&subcontext
);
7784 String_concat_expression::do_check_types(Gogo
*)
7786 if (this->is_error_expression())
7788 Type
* t
= this->exprs_
->front()->type();
7791 this->set_is_error();
7794 Expression_list::iterator pe
= this->exprs_
->begin();
7796 for (; pe
!= this->exprs_
->end(); ++pe
)
7798 Type
* t1
= (*pe
)->type();
7799 if (!Type::are_compatible_for_binop(t
, t1
))
7801 this->report_error("incompatible types in binary expression");
7804 if (!Binary_expression::check_operator_type(OPERATOR_PLUS
, t
, t1
,
7807 this->set_is_error();
7814 String_concat_expression::do_flatten(Gogo
*, Named_object
*,
7815 Statement_inserter
* inserter
)
7817 if (this->is_error_expression())
7819 Location loc
= this->location();
7820 Type
* type
= this->type();
7822 // Mark string([]byte) operands to reuse the backing store.
7823 // runtime.concatstrings does not keep the reference.
7825 // Note: in the gc runtime, if all but one inputs are empty,
7826 // concatstrings returns the only nonempty input without copy.
7827 // So it is not safe to reuse the backing store if it is a
7828 // string([]byte) conversion. So the gc compiler does the
7829 // no-copy optimization only when there is at least one
7830 // constant nonempty input. Currently the gccgo runtime
7831 // doesn't do this, so we don't do the check.
7832 for (Expression_list::iterator p
= this->exprs_
->begin();
7833 p
!= this->exprs_
->end();
7836 Type_conversion_expression
* tce
= (*p
)->conversion_expression();
7838 tce
->set_no_copy(true);
7841 Expression
* buf
= NULL
;
7842 Node
* n
= Node::make_node(this);
7843 if ((n
->encoding() & ESCAPE_MASK
) == Node::ESCAPE_NONE
)
7846 for (Expression_list::iterator p
= this->exprs_
->begin();
7847 p
!= this->exprs_
->end();
7851 if ((*p
)->string_constant_value(&s
))
7854 // Make a buffer on stack if the result does not escape.
7855 // But don't do this if we know it won't fit.
7856 if (size
< (size_t)tmp_string_buf_size
)
7858 Type
* byte_type
= Type::lookup_integer_type("uint8");
7859 Expression
* buflen
=
7860 Expression::make_integer_ul(tmp_string_buf_size
, NULL
, loc
);
7861 Expression::make_integer_ul(tmp_string_buf_size
, NULL
, loc
);
7862 Type
* array_type
= Type::make_array_type(byte_type
, buflen
);
7863 buf
= Expression::make_allocation(array_type
, loc
);
7864 buf
->allocation_expression()->set_allocate_on_stack();
7865 buf
->allocation_expression()->set_no_zero();
7869 buf
= Expression::make_nil(loc
);
7870 go_assert(this->exprs_
->size() > 1);
7872 Expression::make_integer_ul(this->exprs_
->size(), NULL
, loc
);
7873 Array_type
* array_type
= Type::make_array_type(type
, len
);
7874 array_type
->set_is_array_incomparable();
7876 Expression::make_array_composite_literal(array_type
, this->exprs_
,
7878 Temporary_statement
* ts
=
7879 Statement::make_temporary(array_type
, array
, loc
);
7880 inserter
->insert(ts
);
7881 Expression
* ref
= Expression::make_temporary_reference(ts
, loc
);
7882 ref
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
7884 Runtime::make_call(Runtime::CONCATSTRINGS
, loc
, 3, buf
,
7886 return Expression::make_cast(type
, call
, loc
);
7890 String_concat_expression::do_dump_expression(
7891 Ast_dump_context
* ast_dump_context
) const
7893 ast_dump_context
->ostream() << "concat(";
7894 ast_dump_context
->dump_expression_list(this->exprs_
, false);
7895 ast_dump_context
->ostream() << ")";
7899 Expression::make_string_concat(Expression_list
* exprs
)
7901 return new String_concat_expression(exprs
);
7904 // Class Bound_method_expression.
7909 Bound_method_expression::do_traverse(Traverse
* traverse
)
7911 return Expression::traverse(&this->expr_
, traverse
);
7914 // Return the type of a bound method expression. The type of this
7915 // object is simply the type of the method with no receiver.
7918 Bound_method_expression::do_type()
7920 Named_object
* fn
= this->method_
->named_object();
7921 Function_type
* fntype
;
7922 if (fn
->is_function())
7923 fntype
= fn
->func_value()->type();
7924 else if (fn
->is_function_declaration())
7925 fntype
= fn
->func_declaration_value()->type();
7927 return Type::make_error_type();
7928 return fntype
->copy_without_receiver();
7931 // Determine the types of a method expression.
7934 Bound_method_expression::do_determine_type(const Type_context
*)
7936 Named_object
* fn
= this->method_
->named_object();
7937 Function_type
* fntype
;
7938 if (fn
->is_function())
7939 fntype
= fn
->func_value()->type();
7940 else if (fn
->is_function_declaration())
7941 fntype
= fn
->func_declaration_value()->type();
7944 if (fntype
== NULL
|| !fntype
->is_method())
7945 this->expr_
->determine_type_no_context();
7948 Type_context
subcontext(fntype
->receiver()->type(), false);
7949 this->expr_
->determine_type(&subcontext
);
7953 // Check the types of a method expression.
7956 Bound_method_expression::do_check_types(Gogo
*)
7958 Named_object
* fn
= this->method_
->named_object();
7959 if (!fn
->is_function() && !fn
->is_function_declaration())
7961 this->report_error(_("object is not a method"));
7965 Function_type
* fntype
;
7966 if (fn
->is_function())
7967 fntype
= fn
->func_value()->type();
7968 else if (fn
->is_function_declaration())
7969 fntype
= fn
->func_declaration_value()->type();
7972 Type
* rtype
= fntype
->receiver()->type()->deref();
7973 Type
* etype
= (this->expr_type_
!= NULL
7975 : this->expr_
->type());
7976 etype
= etype
->deref();
7977 if (!Type::are_identical(rtype
, etype
, Type::COMPARE_TAGS
, NULL
))
7978 this->report_error(_("method type does not match object type"));
7981 // If a bound method expression is not simply called, then it is
7982 // represented as a closure. The closure will hold a single variable,
7983 // the receiver to pass to the method. The function will be a simple
7984 // thunk that pulls that value from the closure and calls the method
7985 // with the remaining arguments.
7987 // Because method values are not common, we don't build all thunks for
7988 // every methods, but instead only build them as we need them. In
7989 // particular, we even build them on demand for methods defined in
7992 Bound_method_expression::Method_value_thunks
7993 Bound_method_expression::method_value_thunks
;
7995 // Find or create the thunk for FN.
7998 Bound_method_expression::create_thunk(Gogo
* gogo
, const Method
* method
,
8001 std::pair
<Named_object
*, Named_object
*> val(fn
, NULL
);
8002 std::pair
<Method_value_thunks::iterator
, bool> ins
=
8003 Bound_method_expression::method_value_thunks
.insert(val
);
8006 // We have seen this method before.
8007 go_assert(ins
.first
->second
!= NULL
);
8008 return ins
.first
->second
;
8011 Location loc
= fn
->location();
8013 Function_type
* orig_fntype
;
8014 if (fn
->is_function())
8015 orig_fntype
= fn
->func_value()->type();
8016 else if (fn
->is_function_declaration())
8017 orig_fntype
= fn
->func_declaration_value()->type();
8021 if (orig_fntype
== NULL
|| !orig_fntype
->is_method())
8024 Named_object::make_erroneous_name(gogo
->thunk_name());
8025 return ins
.first
->second
;
8028 Struct_field_list
* sfl
= new Struct_field_list();
8029 // The type here is wrong--it should be the C function type. But it
8030 // doesn't really matter.
8031 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
8032 sfl
->push_back(Struct_field(Typed_identifier("fn", vt
, loc
)));
8033 sfl
->push_back(Struct_field(Typed_identifier("val",
8034 orig_fntype
->receiver()->type(),
8036 Struct_type
* st
= Type::make_struct_type(sfl
, loc
);
8037 st
->set_is_struct_incomparable();
8038 Type
* closure_type
= Type::make_pointer_type(st
);
8040 Function_type
* new_fntype
= orig_fntype
->copy_with_names();
8042 std::string thunk_name
= gogo
->thunk_name();
8043 Named_object
* new_no
= gogo
->start_function(thunk_name
, new_fntype
,
8046 Variable
* cvar
= new Variable(closure_type
, NULL
, false, false, false, loc
);
8047 cvar
->set_is_used();
8048 cvar
->set_is_closure();
8049 Named_object
* cp
= Named_object::make_variable("$closure" + thunk_name
,
8051 new_no
->func_value()->set_closure_var(cp
);
8053 gogo
->start_block(loc
);
8055 // Field 0 of the closure is the function code pointer, field 1 is
8056 // the value on which to invoke the method.
8057 Expression
* arg
= Expression::make_var_reference(cp
, loc
);
8058 arg
= Expression::make_dereference(arg
, NIL_CHECK_NOT_NEEDED
, loc
);
8059 arg
= Expression::make_field_reference(arg
, 1, loc
);
8061 Expression
* bme
= Expression::make_bound_method(arg
, method
, fn
, loc
);
8063 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
8064 Expression_list
* args
;
8065 if (orig_params
== NULL
|| orig_params
->empty())
8069 const Typed_identifier_list
* new_params
= new_fntype
->parameters();
8070 args
= new Expression_list();
8071 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
8072 p
!= new_params
->end();
8075 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
8076 go_assert(p_no
!= NULL
8077 && p_no
->is_variable()
8078 && p_no
->var_value()->is_parameter());
8079 args
->push_back(Expression::make_var_reference(p_no
, loc
));
8083 Call_expression
* call
= Expression::make_call(bme
, args
,
8084 orig_fntype
->is_varargs(),
8086 call
->set_varargs_are_lowered();
8088 Statement
* s
= Statement::make_return_from_call(call
, loc
);
8089 gogo
->add_statement(s
);
8090 Block
* b
= gogo
->finish_block(loc
);
8091 gogo
->add_block(b
, loc
);
8093 // This is called after lowering but before determine_types.
8094 gogo
->lower_block(new_no
, b
);
8096 gogo
->finish_function(loc
);
8098 ins
.first
->second
= new_no
;
8102 // Look up a thunk for FN.
8105 Bound_method_expression::lookup_thunk(Named_object
* fn
)
8107 Method_value_thunks::const_iterator p
=
8108 Bound_method_expression::method_value_thunks
.find(fn
);
8109 if (p
== Bound_method_expression::method_value_thunks
.end())
8114 // Return an expression to check *REF for nil while dereferencing
8115 // according to FIELD_INDEXES. Update *REF to build up the field
8116 // reference. This is a static function so that we don't have to
8117 // worry about declaring Field_indexes in expressions.h.
8120 bme_check_nil(const Method::Field_indexes
* field_indexes
, Location loc
,
8123 if (field_indexes
== NULL
)
8124 return Expression::make_boolean(false, loc
);
8125 Expression
* cond
= bme_check_nil(field_indexes
->next
, loc
, ref
);
8126 Struct_type
* stype
= (*ref
)->type()->deref()->struct_type();
8127 go_assert(stype
!= NULL
8128 && field_indexes
->field_index
< stype
->field_count());
8129 if ((*ref
)->type()->struct_type() == NULL
)
8131 go_assert((*ref
)->type()->points_to() != NULL
);
8132 Expression
* n
= Expression::make_binary(OPERATOR_EQEQ
, *ref
,
8133 Expression::make_nil(loc
),
8135 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, n
, loc
);
8136 *ref
= Expression::make_dereference(*ref
, Expression::NIL_CHECK_DEFAULT
,
8138 go_assert((*ref
)->type()->struct_type() == stype
);
8140 *ref
= Expression::make_field_reference(*ref
, field_indexes
->field_index
,
8145 // Flatten a method value into a struct with nil checks. We can't do
8146 // this in the lowering phase, because if the method value is called
8147 // directly we don't need a thunk. That case will have been handled
8148 // by Call_expression::do_lower, so if we get here then we do need a
8152 Bound_method_expression::do_flatten(Gogo
* gogo
, Named_object
*,
8153 Statement_inserter
* inserter
)
8155 Location loc
= this->location();
8157 Named_object
* thunk
= Bound_method_expression::lookup_thunk(this->function_
);
8159 // The thunk should have been created during the
8160 // create_function_descriptors pass.
8161 if (thunk
== NULL
|| thunk
->is_erroneous())
8163 go_assert(saw_errors());
8164 return Expression::make_error(loc
);
8167 // Force the expression into a variable. This is only necessary if
8168 // we are going to do nil checks below, but it's easy enough to
8170 Expression
* expr
= this->expr_
;
8171 if (!expr
->is_multi_eval_safe())
8173 Temporary_statement
* etemp
= Statement::make_temporary(NULL
, expr
, loc
);
8174 inserter
->insert(etemp
);
8175 expr
= Expression::make_temporary_reference(etemp
, loc
);
8178 // If the method expects a value, and we have a pointer, we need to
8179 // dereference the pointer.
8181 Named_object
* fn
= this->method_
->named_object();
8182 Function_type
*fntype
;
8183 if (fn
->is_function())
8184 fntype
= fn
->func_value()->type();
8185 else if (fn
->is_function_declaration())
8186 fntype
= fn
->func_declaration_value()->type();
8190 Expression
* val
= expr
;
8191 if (fntype
->receiver()->type()->points_to() == NULL
8192 && val
->type()->points_to() != NULL
)
8193 val
= Expression::make_dereference(val
, NIL_CHECK_DEFAULT
, loc
);
8195 // Note that we are ignoring this->expr_type_ here. The thunk will
8196 // expect a closure whose second field has type this->expr_type_ (if
8197 // that is not NULL). We are going to pass it a closure whose
8198 // second field has type this->expr_->type(). Since
8199 // this->expr_type_ is only not-NULL for pointer types, we can get
8202 Struct_field_list
* fields
= new Struct_field_list();
8203 fields
->push_back(Struct_field(Typed_identifier("fn",
8204 thunk
->func_value()->type(),
8206 fields
->push_back(Struct_field(Typed_identifier("val", val
->type(), loc
)));
8207 Struct_type
* st
= Type::make_struct_type(fields
, loc
);
8208 st
->set_is_struct_incomparable();
8210 Expression_list
* vals
= new Expression_list();
8211 vals
->push_back(Expression::make_func_code_reference(thunk
, loc
));
8212 vals
->push_back(val
);
8214 Expression
* ret
= Expression::make_struct_composite_literal(st
, vals
, loc
);
8215 ret
= Expression::make_heap_expression(ret
, loc
);
8217 Node
* node
= Node::make_node(this);
8218 if ((node
->encoding() & ESCAPE_MASK
) == Node::ESCAPE_NONE
)
8219 ret
->heap_expression()->set_allocate_on_stack();
8220 else if (gogo
->compiling_runtime()
8221 && gogo
->package_name() == "runtime"
8223 go_error_at(loc
, "%s escapes to heap, not allowed in runtime",
8224 node
->ast_format(gogo
).c_str());
8226 // If necessary, check whether the expression or any embedded
8227 // pointers are nil.
8229 Expression
* nil_check
= NULL
;
8230 if (this->method_
->field_indexes() != NULL
)
8232 Expression
* ref
= expr
;
8233 nil_check
= bme_check_nil(this->method_
->field_indexes(), loc
, &ref
);
8237 if (this->method_
->is_value_method() && expr
->type()->points_to() != NULL
)
8239 Expression
* n
= Expression::make_binary(OPERATOR_EQEQ
, expr
,
8240 Expression::make_nil(loc
),
8242 if (nil_check
== NULL
)
8245 nil_check
= Expression::make_binary(OPERATOR_OROR
, nil_check
, n
, loc
);
8248 if (nil_check
!= NULL
)
8250 Expression
* crash
= Runtime::make_call(Runtime::PANIC_MEM
, loc
, 0);
8251 // Fix the type of the conditional expression by pretending to
8252 // evaluate to RET either way through the conditional.
8253 crash
= Expression::make_compound(crash
, ret
, loc
);
8254 ret
= Expression::make_conditional(nil_check
, crash
, ret
, loc
);
8257 // RET is a pointer to a struct, but we want a function type.
8258 ret
= Expression::make_unsafe_cast(this->type(), ret
, loc
);
8263 // Dump ast representation of a bound method expression.
8266 Bound_method_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
8269 if (this->expr_type_
!= NULL
)
8270 ast_dump_context
->ostream() << "(";
8271 ast_dump_context
->dump_expression(this->expr_
);
8272 if (this->expr_type_
!= NULL
)
8274 ast_dump_context
->ostream() << ":";
8275 ast_dump_context
->dump_type(this->expr_type_
);
8276 ast_dump_context
->ostream() << ")";
8279 ast_dump_context
->ostream() << "." << this->function_
->name();
8282 // Make a method expression.
8284 Bound_method_expression
*
8285 Expression::make_bound_method(Expression
* expr
, const Method
* method
,
8286 Named_object
* function
, Location location
)
8288 return new Bound_method_expression(expr
, method
, function
, location
);
8291 // Class Builtin_call_expression. This is used for a call to a
8292 // builtin function.
8294 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
8296 Expression_list
* args
,
8299 : Call_expression(fn
, args
, is_varargs
, location
),
8300 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false),
8301 recover_arg_is_set_(false)
8303 Func_expression
* fnexp
= this->fn()->func_expression();
8306 this->code_
= BUILTIN_INVALID
;
8309 const std::string
& name(fnexp
->named_object()->name());
8310 if (name
== "append")
8311 this->code_
= BUILTIN_APPEND
;
8312 else if (name
== "cap")
8313 this->code_
= BUILTIN_CAP
;
8314 else if (name
== "close")
8315 this->code_
= BUILTIN_CLOSE
;
8316 else if (name
== "complex")
8317 this->code_
= BUILTIN_COMPLEX
;
8318 else if (name
== "copy")
8319 this->code_
= BUILTIN_COPY
;
8320 else if (name
== "delete")
8321 this->code_
= BUILTIN_DELETE
;
8322 else if (name
== "imag")
8323 this->code_
= BUILTIN_IMAG
;
8324 else if (name
== "len")
8325 this->code_
= BUILTIN_LEN
;
8326 else if (name
== "make")
8327 this->code_
= BUILTIN_MAKE
;
8328 else if (name
== "new")
8329 this->code_
= BUILTIN_NEW
;
8330 else if (name
== "panic")
8331 this->code_
= BUILTIN_PANIC
;
8332 else if (name
== "print")
8333 this->code_
= BUILTIN_PRINT
;
8334 else if (name
== "println")
8335 this->code_
= BUILTIN_PRINTLN
;
8336 else if (name
== "real")
8337 this->code_
= BUILTIN_REAL
;
8338 else if (name
== "recover")
8339 this->code_
= BUILTIN_RECOVER
;
8340 else if (name
== "Add")
8341 this->code_
= BUILTIN_ADD
;
8342 else if (name
== "Alignof")
8343 this->code_
= BUILTIN_ALIGNOF
;
8344 else if (name
== "Offsetof")
8345 this->code_
= BUILTIN_OFFSETOF
;
8346 else if (name
== "Sizeof")
8347 this->code_
= BUILTIN_SIZEOF
;
8348 else if (name
== "Slice")
8349 this->code_
= BUILTIN_SLICE
;
8354 // Return whether this is a call to recover. This is a virtual
8355 // function called from the parent class.
8358 Builtin_call_expression::do_is_recover_call() const
8360 if (this->classification() == EXPRESSION_ERROR
)
8362 return this->code_
== BUILTIN_RECOVER
;
8365 // Set the argument for a call to recover.
8368 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
8370 const Expression_list
* args
= this->args();
8371 go_assert(args
== NULL
|| args
->empty());
8372 Expression_list
* new_args
= new Expression_list();
8373 new_args
->push_back(arg
);
8374 this->set_args(new_args
);
8375 this->recover_arg_is_set_
= true;
8378 // Lower a builtin call expression. This turns new and make into
8379 // specific expressions. We also convert to a constant if we can.
8382 Builtin_call_expression::do_lower(Gogo
*, Named_object
* function
,
8383 Statement_inserter
* inserter
, int)
8385 if (this->is_error_expression())
8388 Location loc
= this->location();
8390 if (this->is_varargs() && this->code_
!= BUILTIN_APPEND
)
8392 this->report_error(_("invalid use of %<...%> with builtin function"));
8393 return Expression::make_error(loc
);
8396 if (this->code_
== BUILTIN_OFFSETOF
)
8398 Expression
* arg
= this->one_arg();
8400 if (arg
->bound_method_expression() != NULL
8401 || arg
->interface_field_reference_expression() != NULL
)
8403 this->report_error(_("invalid use of method value as argument "
8408 Field_reference_expression
* farg
= arg
->field_reference_expression();
8409 while (farg
!= NULL
)
8411 if (!farg
->implicit())
8413 // When the selector refers to an embedded field,
8414 // it must not be reached through pointer indirections.
8415 if (farg
->expr()->deref() != farg
->expr())
8417 this->report_error(_("argument of Offsetof implies "
8418 "indirection of an embedded field"));
8421 // Go up until we reach the original base.
8422 farg
= farg
->expr()->field_reference_expression();
8426 if (this->is_constant())
8428 Numeric_constant nc
;
8429 if (this->numeric_constant_value(&nc
))
8430 return nc
.expression(loc
);
8433 switch (this->code_
)
8440 const Expression_list
* args
= this->args();
8441 if (args
== NULL
|| args
->size() < 1)
8442 this->report_error(_("not enough arguments"));
8443 else if (args
->size() > 1)
8444 this->report_error(_("too many arguments"));
8447 Expression
* arg
= args
->front();
8448 if (!arg
->is_type_expression())
8450 go_error_at(arg
->location(), "expected type");
8451 this->set_is_error();
8454 return Expression::make_allocation(arg
->type(), loc
);
8460 return this->lower_make(inserter
);
8462 case BUILTIN_RECOVER
:
8463 if (function
!= NULL
)
8464 function
->func_value()->set_calls_recover();
8467 // Calling recover outside of a function always returns the
8468 // nil empty interface.
8469 Type
* eface
= Type::make_empty_interface_type(loc
);
8470 return Expression::make_cast(eface
, Expression::make_nil(loc
), loc
);
8474 case BUILTIN_DELETE
:
8476 const Expression_list
* args
= this->args();
8477 if (args
== NULL
|| args
->size() < 2)
8478 this->report_error(_("not enough arguments"));
8479 else if (args
->size() > 2)
8480 this->report_error(_("too many arguments"));
8481 else if (args
->front()->type()->map_type() == NULL
)
8482 this->report_error(_("argument 1 must be a map"));
8486 args
->front()->type()->map_type()->key_type();
8487 Expression_list::iterator pa
= this->args()->begin();
8489 Type
* arg_type
= (*pa
)->type();
8491 if (!Type::are_assignable(key_type
, arg_type
, &reason
))
8494 go_error_at(loc
, "argument 2 has incompatible type");
8496 go_error_at(loc
, "argument 2 has incompatible type (%s)",
8498 this->set_is_error();
8500 else if (!Type::are_identical(key_type
, arg_type
, 0, NULL
))
8501 *pa
= Expression::make_cast(key_type
, *pa
, loc
);
8507 case BUILTIN_PRINTLN
:
8508 // Force all the arguments into temporary variables, so that we
8509 // don't try to evaluate something while holding the print lock.
8510 if (this->args() == NULL
)
8512 for (Expression_list::iterator pa
= this->args()->begin();
8513 pa
!= this->args()->end();
8516 if (!(*pa
)->is_multi_eval_safe())
8518 Temporary_statement
* temp
=
8519 Statement::make_temporary(NULL
, *pa
, loc
);
8520 inserter
->insert(temp
);
8521 *pa
= Expression::make_temporary_reference(temp
, loc
);
8530 // Flatten a builtin call expression. This turns the arguments of some
8531 // builtin calls into temporary expressions. Also expand copy and append
8532 // to runtime calls.
8535 Builtin_call_expression::do_flatten(Gogo
* gogo
, Named_object
* function
,
8536 Statement_inserter
* inserter
)
8538 if (this->is_error_expression())
8540 go_assert(saw_errors());
8544 Location loc
= this->location();
8546 switch (this->code_
)
8551 case BUILTIN_APPEND
:
8552 return this->flatten_append(gogo
, function
, inserter
, NULL
, NULL
);
8556 Type
* at
= this->args()->front()->type();
8557 for (Expression_list::iterator pa
= this->args()->begin();
8558 pa
!= this->args()->end();
8561 if ((*pa
)->is_nil_expression())
8563 Expression
* nil
= Expression::make_nil(loc
);
8564 Expression
* zero
= Expression::make_integer_ul(0, NULL
, loc
);
8565 *pa
= Expression::make_slice_value(at
, nil
, zero
, zero
, loc
);
8567 if (!(*pa
)->is_multi_eval_safe())
8569 Temporary_statement
* temp
=
8570 Statement::make_temporary(NULL
, *pa
, loc
);
8571 inserter
->insert(temp
);
8572 *pa
= Expression::make_temporary_reference(temp
, loc
);
8576 // Lower to runtime call.
8577 const Expression_list
* args
= this->args();
8578 go_assert(args
!= NULL
&& args
->size() == 2);
8579 Expression
* arg1
= args
->front();
8580 Expression
* arg2
= args
->back();
8581 go_assert(arg1
->is_multi_eval_safe());
8582 go_assert(arg2
->is_multi_eval_safe());
8583 bool arg2_is_string
= arg2
->type()->is_string_type();
8586 Type
* et
= at
->array_type()->element_type();
8587 if (et
->has_pointer())
8589 Expression
* td
= Expression::make_type_descriptor(et
, loc
);
8591 Expression::make_slice_info(arg1
, SLICE_INFO_VALUE_POINTER
, loc
);
8593 Expression::make_slice_info(arg1
, SLICE_INFO_LENGTH
, loc
);
8595 Expression::make_slice_info(arg2
, SLICE_INFO_VALUE_POINTER
, loc
);
8597 Expression::make_slice_info(arg2
, SLICE_INFO_LENGTH
, loc
);
8598 ret
= Runtime::make_call(Runtime::TYPEDSLICECOPY
, loc
,
8599 5, td
, pd
, ld
, ps
, ls
);
8603 Type
* int_type
= Type::lookup_integer_type("int");
8604 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8607 Named_object
* lenfn
= gogo
->lookup_global("len");
8608 Expression
* lenref
= Expression::make_func_reference(lenfn
, NULL
, loc
);
8609 Expression_list
* len_args
= new Expression_list();
8610 len_args
->push_back(arg1
->copy());
8611 Expression
* len1
= Expression::make_call(lenref
, len_args
, false, loc
);
8612 gogo
->lower_expression(function
, inserter
, &len1
);
8613 gogo
->flatten_expression(function
, inserter
, &len1
);
8614 Temporary_statement
* l1tmp
= Statement::make_temporary(int_type
, len1
, loc
);
8615 inserter
->insert(l1tmp
);
8618 len_args
= new Expression_list();
8619 len_args
->push_back(arg2
->copy());
8620 Expression
* len2
= Expression::make_call(lenref
, len_args
, false, loc
);
8621 gogo
->lower_expression(function
, inserter
, &len2
);
8622 gogo
->flatten_expression(function
, inserter
, &len2
);
8623 Temporary_statement
* l2tmp
= Statement::make_temporary(int_type
, len2
, loc
);
8624 inserter
->insert(l2tmp
);
8626 // n = (l1 < l2 ? l1 : l2)
8627 Expression
* l1ref
= Expression::make_temporary_reference(l1tmp
, loc
);
8628 Expression
* l2ref
= Expression::make_temporary_reference(l2tmp
, loc
);
8629 Expression
* cond
= Expression::make_binary(OPERATOR_LT
, l1ref
, l2ref
, loc
);
8630 Expression
* n
= Expression::make_conditional(cond
,
8634 Temporary_statement
* ntmp
= Statement::make_temporary(NULL
, n
, loc
);
8635 inserter
->insert(ntmp
);
8637 // sz = n * sizeof(elem_type)
8638 Expression
* nref
= Expression::make_temporary_reference(ntmp
, loc
);
8639 nref
= Expression::make_cast(uintptr_type
, nref
, loc
);
8640 Expression
* sz
= Expression::make_type_info(et
, TYPE_INFO_SIZE
);
8641 sz
= Expression::make_binary(OPERATOR_MULT
, sz
, nref
, loc
);
8643 // memmove(arg1.ptr, arg2.ptr, sz)
8644 Expression
* p1
= Expression::make_slice_info(arg1
,
8645 SLICE_INFO_VALUE_POINTER
,
8647 Expression
* p2
= (arg2_is_string
8648 ? Expression::make_string_info(arg2
,
8651 : Expression::make_slice_info(arg2
,
8652 SLICE_INFO_VALUE_POINTER
,
8654 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_MEMMOVE
, loc
, 3,
8657 // n is the return value of copy
8658 nref
= Expression::make_temporary_reference(ntmp
, loc
);
8659 ret
= Expression::make_compound(call
, nref
, loc
);
8666 for (Expression_list::iterator pa
= this->args()->begin();
8667 pa
!= this->args()->end();
8670 if (!(*pa
)->is_multi_eval_safe()
8671 && (*pa
)->type()->interface_type() != NULL
)
8673 Temporary_statement
* temp
=
8674 Statement::make_temporary(NULL
, *pa
, loc
);
8675 inserter
->insert(temp
);
8676 *pa
= Expression::make_temporary_reference(temp
, loc
);
8684 Expression_list::iterator pa
= this->args()->begin();
8685 if (!(*pa
)->is_multi_eval_safe()
8686 && ((*pa
)->type()->map_type() != NULL
8687 || (*pa
)->type()->channel_type() != NULL
))
8689 Temporary_statement
* temp
=
8690 Statement::make_temporary(NULL
, *pa
, loc
);
8691 inserter
->insert(temp
);
8692 *pa
= Expression::make_temporary_reference(temp
, loc
);
8697 case BUILTIN_DELETE
:
8699 // Lower to a runtime function call.
8700 const Expression_list
* args
= this->args();
8702 // Since this function returns no value it must appear in
8703 // a statement by itself, so we don't have to worry about
8704 // order of evaluation of values around it. Evaluate the
8705 // map first to get order of evaluation right.
8706 Map_type
* mt
= args
->front()->type()->map_type();
8707 Temporary_statement
* map_temp
=
8708 Statement::make_temporary(mt
, args
->front(), loc
);
8709 inserter
->insert(map_temp
);
8711 Temporary_statement
* key_temp
=
8712 Statement::make_temporary(mt
->key_type(), args
->back(), loc
);
8713 inserter
->insert(key_temp
);
8715 Expression
* e1
= Expression::make_type_descriptor(mt
, loc
);
8716 Expression
* e2
= Expression::make_temporary_reference(map_temp
,
8718 Expression
* e3
= Expression::make_temporary_reference(key_temp
,
8721 Runtime::Function code
;
8722 switch (mt
->algorithm(gogo
))
8724 case Map_type::MAP_ALG_FAST32
:
8725 case Map_type::MAP_ALG_FAST32PTR
:
8727 code
= Runtime::MAPDELETE_FAST32
;
8728 Type
* uint32_type
= Type::lookup_integer_type("uint32");
8729 Type
* uint32_ptr_type
= Type::make_pointer_type(uint32_type
);
8730 e3
= Expression::make_unary(OPERATOR_AND
, e3
, loc
);
8731 e3
= Expression::make_unsafe_cast(uint32_ptr_type
, e3
,
8733 e3
= Expression::make_dereference(e3
,
8734 Expression::NIL_CHECK_NOT_NEEDED
,
8738 case Map_type::MAP_ALG_FAST64
:
8739 case Map_type::MAP_ALG_FAST64PTR
:
8741 code
= Runtime::MAPDELETE_FAST64
;
8742 Type
* uint64_type
= Type::lookup_integer_type("uint64");
8743 Type
* uint64_ptr_type
= Type::make_pointer_type(uint64_type
);
8744 e3
= Expression::make_unary(OPERATOR_AND
, e3
, loc
);
8745 e3
= Expression::make_unsafe_cast(uint64_ptr_type
, e3
,
8747 e3
= Expression::make_dereference(e3
,
8748 Expression::NIL_CHECK_NOT_NEEDED
,
8752 case Map_type::MAP_ALG_FASTSTR
:
8753 code
= Runtime::MAPDELETE_FASTSTR
;
8756 code
= Runtime::MAPDELETE
;
8758 // If the call to delete is deferred, and is in a loop,
8759 // then the loop will only have a single instance of the
8760 // temporary variable. Passing the address of the
8761 // temporary variable here means that the deferred call
8762 // will see the last value in the loop, not the current
8763 // value. So for this unusual case copy the value into
8765 if (!this->is_deferred())
8766 e3
= Expression::make_unary(OPERATOR_AND
, e3
, loc
);
8769 Expression
* a
= Expression::make_allocation(mt
->key_type(),
8771 Temporary_statement
* atemp
=
8772 Statement::make_temporary(NULL
, a
, loc
);
8773 inserter
->insert(atemp
);
8775 a
= Expression::make_temporary_reference(atemp
, loc
);
8776 a
= Expression::make_dereference(a
, NIL_CHECK_NOT_NEEDED
, loc
);
8777 Statement
* s
= Statement::make_assignment(a
, e3
, loc
);
8778 inserter
->insert(s
);
8780 e3
= Expression::make_temporary_reference(atemp
, loc
);
8784 return Runtime::make_call(code
, loc
, 3, e1
, e2
, e3
);
8789 Expression
* ptr
= this->args()->front();
8790 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8791 ptr
= Expression::make_cast(uintptr_type
, ptr
, loc
);
8792 Expression
* len
= this->args()->back();
8793 len
= Expression::make_cast(uintptr_type
, len
, loc
);
8794 Expression
* add
= Expression::make_binary(OPERATOR_PLUS
, ptr
, len
,
8796 return Expression::make_cast(this->args()->front()->type(), add
, loc
);
8801 Expression
* ptr
= this->args()->front();
8802 Temporary_statement
* ptr_temp
= NULL
;
8803 if (!ptr
->is_multi_eval_safe())
8805 ptr_temp
= Statement::make_temporary(NULL
, ptr
, loc
);
8806 inserter
->insert(ptr_temp
);
8807 ptr
= Expression::make_temporary_reference(ptr_temp
, loc
);
8810 Expression
* len
= this->args()->back();
8811 Temporary_statement
* len_temp
= NULL
;
8812 if (!len
->is_multi_eval_safe())
8814 len_temp
= Statement::make_temporary(NULL
, len
, loc
);
8815 inserter
->insert(len_temp
);
8816 len
= Expression::make_temporary_reference(len_temp
, loc
);
8820 Numeric_constant nc
;
8821 if (this->args()->back()->numeric_constant_value(&nc
))
8823 // We gave an error for constants that don't fit in int in
8829 Integer_type
* itype
= this->args()->back()->type()->integer_type();
8830 go_assert(itype
!= NULL
);
8831 int ebits
= itype
->bits();
8833 Type::lookup_integer_type("int")->integer_type()->bits();
8835 // We can treat ebits == intbits as small even for an
8836 // unsigned integer type, because we will convert the
8837 // value to int and then reject it in the runtime if it is
8840 fits_in_int
= ebits
<= intbits
;
8843 Runtime::Function code
= (fits_in_int
8844 ? Runtime::UNSAFESLICE
8845 : Runtime::UNSAFESLICE64
);
8847 Expression::make_type_descriptor(ptr
->type()->points_to(), loc
);
8848 Expression
* check
= Runtime::make_call(code
, loc
, 3,
8851 if (ptr_temp
== NULL
)
8854 ptr
= Expression::make_temporary_reference(ptr_temp
, loc
);
8855 Expression
* nil
= Expression::make_nil(loc
);
8856 nil
= Expression::make_cast(ptr
->type(), nil
, loc
);
8857 Expression
* is_nil
= Expression::make_binary(OPERATOR_EQEQ
, ptr
, nil
,
8860 if (len_temp
== NULL
)
8863 len
= Expression::make_temporary_reference(len_temp
, loc
);
8864 Expression
* zero
= Expression::make_integer_ul(0, len
->type(), loc
);
8865 Expression
* is_zero
= Expression::make_binary(OPERATOR_EQEQ
, len
, zero
,
8868 Expression
* cond
= Expression::make_binary(OPERATOR_ANDAND
, is_nil
,
8871 Type
* slice_type
= Type::make_array_type(ptr
->type()->points_to(),
8873 nil
= Expression::make_nil(loc
);
8874 Expression
* nil_slice
= Expression::make_cast(slice_type
, nil
, loc
);
8876 if (ptr_temp
== NULL
)
8879 ptr
= Expression::make_temporary_reference(ptr_temp
, loc
);
8881 if (len_temp
== NULL
)
8884 len
= Expression::make_temporary_reference(len_temp
, loc
);
8887 if (len_temp
== NULL
)
8890 cap
= Expression::make_temporary_reference(len_temp
, loc
);
8892 Expression
* slice
= Expression::make_slice_value(slice_type
, ptr
,
8895 slice
= Expression::make_conditional(cond
, nil_slice
, slice
, loc
);
8897 return Expression::make_compound(check
, slice
, loc
);
8904 // Lower a make expression.
8907 Builtin_call_expression::lower_make(Statement_inserter
* inserter
)
8909 Location loc
= this->location();
8911 const Expression_list
* args
= this->args();
8912 if (args
== NULL
|| args
->size() < 1)
8914 this->report_error(_("not enough arguments"));
8915 return Expression::make_error(this->location());
8918 Expression_list::const_iterator parg
= args
->begin();
8920 Expression
* first_arg
= *parg
;
8921 if (!first_arg
->is_type_expression())
8923 go_error_at(first_arg
->location(), "expected type");
8924 this->set_is_error();
8925 return Expression::make_error(this->location());
8927 Type
* type
= first_arg
->type();
8929 if (!type
->in_heap())
8930 go_error_at(first_arg
->location(),
8931 "cannot make slice of go:notinheap type");
8933 bool is_slice
= false;
8934 bool is_map
= false;
8935 bool is_chan
= false;
8936 if (type
->is_slice_type())
8938 else if (type
->map_type() != NULL
)
8940 else if (type
->channel_type() != NULL
)
8944 this->report_error(_("invalid type for make function"));
8945 return Expression::make_error(this->location());
8948 Type_context
int_context(Type::lookup_integer_type("int"), false);
8951 Expression
* len_arg
;
8952 bool len_small
= false;
8953 if (parg
== args
->end())
8957 this->report_error(_("length required when allocating a slice"));
8958 return Expression::make_error(this->location());
8960 len_arg
= Expression::make_integer_ul(0, NULL
, loc
);
8966 len_arg
->determine_type(&int_context
);
8967 if (len_arg
->type()->integer_type() == NULL
)
8969 go_error_at(len_arg
->location(), "non-integer len argument in make");
8970 return Expression::make_error(this->location());
8972 if (!this->check_int_value(len_arg
, true, &len_small
))
8973 return Expression::make_error(this->location());
8977 Expression
* cap_arg
= NULL
;
8978 bool cap_small
= false;
8979 Numeric_constant nclen
;
8980 Numeric_constant nccap
;
8983 if (is_slice
&& parg
!= args
->end())
8986 cap_arg
->determine_type(&int_context
);
8987 if (cap_arg
->type()->integer_type() == NULL
)
8989 go_error_at(cap_arg
->location(), "non-integer cap argument in make");
8990 return Expression::make_error(this->location());
8992 if (!this->check_int_value(cap_arg
, false, &cap_small
))
8993 return Expression::make_error(this->location());
8995 if (len_arg
->numeric_constant_value(&nclen
)
8996 && cap_arg
->numeric_constant_value(&nccap
)
8997 && nclen
.to_unsigned_long(&vlen
) == Numeric_constant::NC_UL_VALID
8998 && nccap
.to_unsigned_long(&vcap
) == Numeric_constant::NC_UL_VALID
9001 this->report_error(_("len larger than cap"));
9002 return Expression::make_error(this->location());
9008 if (parg
!= args
->end())
9010 this->report_error(_("too many arguments to make"));
9011 return Expression::make_error(this->location());
9014 Location type_loc
= first_arg
->location();
9019 Temporary_statement
* len_temp
= NULL
;
9020 if (!len_arg
->is_constant())
9022 len_temp
= Statement::make_temporary(NULL
, len_arg
, loc
);
9023 inserter
->insert(len_temp
);
9024 len_arg
= Expression::make_temporary_reference(len_temp
, loc
);
9027 if (cap_arg
== NULL
)
9029 cap_small
= len_small
;
9030 if (len_temp
== NULL
)
9031 cap_arg
= len_arg
->copy();
9033 cap_arg
= Expression::make_temporary_reference(len_temp
, loc
);
9035 else if (!cap_arg
->is_constant())
9037 Temporary_statement
* cap_temp
= Statement::make_temporary(NULL
,
9040 inserter
->insert(cap_temp
);
9041 cap_arg
= Expression::make_temporary_reference(cap_temp
, loc
);
9044 Type
* et
= type
->array_type()->element_type();
9045 Expression
* type_arg
= Expression::make_type_descriptor(et
, type_loc
);
9046 Runtime::Function code
= Runtime::MAKESLICE
;
9047 if (!len_small
|| !cap_small
)
9048 code
= Runtime::MAKESLICE64
;
9049 Expression
* mem
= Runtime::make_call(code
, loc
, 3, type_arg
, len_arg
,
9051 mem
= Expression::make_unsafe_cast(Type::make_pointer_type(et
), mem
,
9053 Type
* int_type
= Type::lookup_integer_type("int");
9054 len_arg
= Expression::make_cast(int_type
, len_arg
->copy(), loc
);
9055 cap_arg
= Expression::make_cast(int_type
, cap_arg
->copy(), loc
);
9056 call
= Expression::make_slice_value(type
, mem
, len_arg
, cap_arg
, loc
);
9060 Expression
* type_arg
= Expression::make_type_descriptor(type
, type_loc
);
9062 call
= Runtime::make_call(Runtime::MAKEMAP64
, loc
, 3, type_arg
,
9064 Expression::make_nil(loc
));
9067 if (len_arg
->numeric_constant_value(&nclen
)
9068 && nclen
.to_unsigned_long(&vlen
) == Numeric_constant::NC_UL_VALID
9069 && vlen
<= Map_type::bucket_size
)
9070 call
= Runtime::make_call(Runtime::MAKEMAP_SMALL
, loc
, 0);
9072 call
= Runtime::make_call(Runtime::MAKEMAP
, loc
, 3, type_arg
,
9074 Expression::make_nil(loc
));
9079 Expression
* type_arg
= Expression::make_type_descriptor(type
, type_loc
);
9080 Runtime::Function code
= Runtime::MAKECHAN
;
9082 code
= Runtime::MAKECHAN64
;
9083 call
= Runtime::make_call(code
, loc
, 2, type_arg
, len_arg
);
9088 return Expression::make_unsafe_cast(type
, call
, loc
);
9091 // Flatten a call to the predeclared append function. We do this in
9092 // the flatten phase, not the lowering phase, so that we run after
9093 // type checking and after order_evaluations. If ASSIGN_LHS is not
9094 // NULL, this append is the right-hand-side of an assignment and
9095 // ASSIGN_LHS is the left-hand-side; in that case, set LHS directly
9096 // rather than returning a slice. This lets us omit a write barrier
9097 // in common cases like a = append(a, ...) when the slice does not
9098 // need to grow. ENCLOSING is not NULL iff ASSIGN_LHS is not NULL.
9101 Builtin_call_expression::flatten_append(Gogo
* gogo
, Named_object
* function
,
9102 Statement_inserter
* inserter
,
9103 Expression
* assign_lhs
,
9106 if (this->is_error_expression())
9109 Location loc
= this->location();
9111 const Expression_list
* args
= this->args();
9112 go_assert(args
!= NULL
&& !args
->empty());
9114 Type
* slice_type
= args
->front()->type();
9115 go_assert(slice_type
->is_slice_type());
9116 Type
* element_type
= slice_type
->array_type()->element_type();
9118 if (args
->size() == 1)
9120 // append(s) evaluates to s.
9121 if (assign_lhs
!= NULL
)
9123 return args
->front();
9126 Type
* int_type
= Type::lookup_integer_type("int");
9127 Type
* uint_type
= Type::lookup_integer_type("uint");
9130 // append(s1, s2...)
9132 // append(s1, a1, a2, a3, ...)
9135 Temporary_statement
* s1tmp
= Statement::make_temporary(NULL
, args
->front(),
9137 inserter
->insert(s1tmp
);
9139 // l1tmp := len(s1tmp)
9140 Named_object
* lenfn
= gogo
->lookup_global("len");
9141 Expression
* lenref
= Expression::make_func_reference(lenfn
, NULL
, loc
);
9142 Expression_list
* call_args
= new Expression_list();
9143 call_args
->push_back(Expression::make_temporary_reference(s1tmp
, loc
));
9144 Expression
* len
= Expression::make_call(lenref
, call_args
, false, loc
);
9145 gogo
->lower_expression(function
, inserter
, &len
);
9146 gogo
->flatten_expression(function
, inserter
, &len
);
9147 Temporary_statement
* l1tmp
= Statement::make_temporary(int_type
, len
, loc
);
9148 inserter
->insert(l1tmp
);
9150 Temporary_statement
* s2tmp
= NULL
;
9151 Temporary_statement
* l2tmp
= NULL
;
9152 Expression_list
* add
= NULL
;
9154 Call_expression
* makecall
= NULL
;
9155 if (this->is_varargs())
9157 go_assert(args
->size() == 2);
9159 std::pair
<Call_expression
*, Temporary_statement
*> p
=
9160 Expression::find_makeslice_call(args
->back());
9162 if (makecall
!= NULL
)
9165 // append(s, make([]T, len[, cap])...))
9166 // which has already been lowered to
9167 // append(s, runtime.makeslice(T, len, cap)).
9168 // We will optimize this to directly zeroing the tail,
9169 // instead of allocating a new slice then copy.
9171 // Retrieve the length and capacity. Cannot reference s2 as
9172 // we will remove the makeslice call.
9173 Expression
* len_arg
= makecall
->args()->at(1);
9174 len_arg
= Expression::make_cast(int_type
, len_arg
, loc
);
9175 l2tmp
= Statement::make_temporary(int_type
, len_arg
, loc
);
9176 inserter
->insert(l2tmp
);
9178 Expression
* cap_arg
= makecall
->args()->at(2);
9179 cap_arg
= Expression::make_cast(int_type
, cap_arg
, loc
);
9180 Temporary_statement
* c2tmp
=
9181 Statement::make_temporary(int_type
, cap_arg
, loc
);
9182 inserter
->insert(c2tmp
);
9184 // Check bad len/cap here.
9185 // checkmakeslice(type, len, cap)
9186 // (Note that if len and cap are constants, we won't see a
9187 // makeslice call here, as it will be rewritten to a stack
9188 // allocated array by Mark_address_taken::expression.)
9189 Expression
* elem
= Expression::make_type_descriptor(element_type
,
9191 len2
= Expression::make_temporary_reference(l2tmp
, loc
);
9192 Expression
* cap2
= Expression::make_temporary_reference(c2tmp
, loc
);
9193 Expression
* check
= Runtime::make_call(Runtime::CHECK_MAKE_SLICE
,
9194 loc
, 3, elem
, len2
, cap2
);
9195 gogo
->lower_expression(function
, inserter
, &check
);
9196 gogo
->flatten_expression(function
, inserter
, &check
);
9197 Statement
* s
= Statement::make_statement(check
, false);
9198 inserter
->insert(s
);
9200 // Remove the original makeslice call.
9201 Temporary_statement
* ts
= p
.second
;
9202 if (ts
!= NULL
&& ts
->uses() == 1)
9203 ts
->set_init(Expression::make_nil(loc
));
9208 s2tmp
= Statement::make_temporary(NULL
, args
->back(), loc
);
9209 inserter
->insert(s2tmp
);
9211 // l2tmp := len(s2tmp)
9212 lenref
= Expression::make_func_reference(lenfn
, NULL
, loc
);
9213 call_args
= new Expression_list();
9214 call_args
->push_back(Expression::make_temporary_reference(s2tmp
, loc
));
9215 len
= Expression::make_call(lenref
, call_args
, false, loc
);
9216 gogo
->lower_expression(function
, inserter
, &len
);
9217 gogo
->flatten_expression(function
, inserter
, &len
);
9218 l2tmp
= Statement::make_temporary(int_type
, len
, loc
);
9219 inserter
->insert(l2tmp
);
9223 len2
= Expression::make_temporary_reference(l2tmp
, loc
);
9227 // We have to ensure that all the arguments are in variables
9228 // now, because otherwise if one of them is an index expression
9229 // into the current slice we could overwrite it before we fetch
9231 add
= new Expression_list();
9232 Expression_list::const_iterator pa
= args
->begin();
9233 for (++pa
; pa
!= args
->end(); ++pa
)
9235 if ((*pa
)->is_multi_eval_safe())
9236 add
->push_back(*pa
);
9239 Temporary_statement
* tmp
= Statement::make_temporary(NULL
, *pa
,
9241 inserter
->insert(tmp
);
9242 add
->push_back(Expression::make_temporary_reference(tmp
, loc
));
9247 len2
= Expression::make_integer_ul(add
->size(), int_type
, loc
);
9250 // ntmp := l1tmp + len2
9251 Expression
* ref
= Expression::make_temporary_reference(l1tmp
, loc
);
9252 Expression
* sum
= Expression::make_binary(OPERATOR_PLUS
, ref
, len2
, loc
);
9253 gogo
->lower_expression(function
, inserter
, &sum
);
9254 gogo
->flatten_expression(function
, inserter
, &sum
);
9255 Temporary_statement
* ntmp
= Statement::make_temporary(int_type
, sum
, loc
);
9256 inserter
->insert(ntmp
);
9258 // s1tmp = uint(ntmp) > uint(cap(s1tmp)) ?
9259 // growslice(type, s1tmp, ntmp) :
9261 // Using uint here means that if the computation of ntmp overflowed,
9262 // we will call growslice which will panic.
9264 Named_object
* capfn
= gogo
->lookup_global("cap");
9265 Expression
* capref
= Expression::make_func_reference(capfn
, NULL
, loc
);
9266 call_args
= new Expression_list();
9267 call_args
->push_back(Expression::make_temporary_reference(s1tmp
, loc
));
9268 Expression
* cap
= Expression::make_call(capref
, call_args
, false, loc
);
9269 gogo
->lower_expression(function
, inserter
, &cap
);
9270 gogo
->flatten_expression(function
, inserter
, &cap
);
9271 Temporary_statement
* c1tmp
= Statement::make_temporary(int_type
, cap
, loc
);
9272 inserter
->insert(c1tmp
);
9274 Expression
* left
= Expression::make_temporary_reference(ntmp
, loc
);
9275 left
= Expression::make_cast(uint_type
, left
, loc
);
9276 Expression
* right
= Expression::make_temporary_reference(c1tmp
, loc
);
9277 right
= Expression::make_cast(uint_type
, right
, loc
);
9279 Expression
* cond
= Expression::make_binary(OPERATOR_GT
, left
, right
, loc
);
9281 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
9282 Expression
* a1
= Expression::make_type_descriptor(element_type
, loc
);
9283 Expression
* a2
= Expression::make_temporary_reference(s1tmp
, loc
);
9284 a2
= slice_type
->array_type()->get_value_pointer(gogo
, a2
);
9285 a2
= Expression::make_cast(unsafe_ptr_type
, a2
, loc
);
9286 Expression
* a3
= Expression::make_temporary_reference(l1tmp
, loc
);
9287 Expression
* a4
= Expression::make_temporary_reference(c1tmp
, loc
);
9288 Expression
* a5
= Expression::make_temporary_reference(ntmp
, loc
);
9289 Expression
* call
= Runtime::make_call(Runtime::GROWSLICE
, loc
, 5,
9290 a1
, a2
, a3
, a4
, a5
);
9291 call
= Expression::make_unsafe_cast(slice_type
, call
, loc
);
9293 ref
= Expression::make_temporary_reference(s1tmp
, loc
);
9294 Expression
* zero
= Expression::make_integer_ul(0, int_type
, loc
);
9295 Expression
* ref2
= Expression::make_temporary_reference(ntmp
, loc
);
9296 ref
= Expression::make_array_index(ref
, zero
, ref2
, NULL
, loc
);
9297 ref
->array_index_expression()->set_needs_bounds_check(false);
9299 if (assign_lhs
== NULL
)
9301 Expression
* rhs
= Expression::make_conditional(cond
, call
, ref
, loc
);
9303 gogo
->lower_expression(function
, inserter
, &rhs
);
9304 gogo
->flatten_expression(function
, inserter
, &rhs
);
9306 ref
= Expression::make_temporary_reference(s1tmp
, loc
);
9307 Statement
* assign
= Statement::make_assignment(ref
, rhs
, loc
);
9308 inserter
->insert(assign
);
9312 gogo
->lower_expression(function
, inserter
, &cond
);
9313 gogo
->flatten_expression(function
, inserter
, &cond
);
9314 gogo
->lower_expression(function
, inserter
, &call
);
9315 gogo
->flatten_expression(function
, inserter
, &call
);
9316 gogo
->lower_expression(function
, inserter
, &ref
);
9317 gogo
->flatten_expression(function
, inserter
, &ref
);
9319 Block
* then_block
= new Block(enclosing
, loc
);
9320 Assignment_statement
* assign
=
9321 Statement::make_assignment(assign_lhs
, call
, loc
);
9322 then_block
->add_statement(assign
);
9324 Block
* else_block
= new Block(enclosing
, loc
);
9325 assign
= Statement::make_assignment(assign_lhs
->copy(), ref
, loc
);
9326 // This assignment will not change the pointer value, so it does
9327 // not need a write barrier.
9328 assign
->set_omit_write_barrier();
9329 else_block
->add_statement(assign
);
9331 Statement
* s
= Statement::make_if_statement(cond
, then_block
,
9333 inserter
->insert(s
);
9335 ref
= Expression::make_temporary_reference(s1tmp
, loc
);
9336 assign
= Statement::make_assignment(ref
, assign_lhs
->copy(), loc
);
9337 inserter
->insert(assign
);
9340 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
9342 if (this->is_varargs())
9344 if (makecall
!= NULL
)
9346 // memclr(&s1tmp[l1tmp], l2tmp*sizeof(elem))
9347 a1
= Expression::make_temporary_reference(s1tmp
, loc
);
9348 ref
= Expression::make_temporary_reference(l1tmp
, loc
);
9349 a1
= Expression::make_array_index(a1
, ref
, NULL
, NULL
, loc
);
9350 a1
->array_index_expression()->set_needs_bounds_check(false);
9351 a1
= Expression::make_unary(OPERATOR_AND
, a1
, loc
);
9353 ref
= Expression::make_temporary_reference(l2tmp
, loc
);
9354 ref
= Expression::make_cast(uintptr_type
, ref
, loc
);
9355 a2
= Expression::make_type_info(element_type
, TYPE_INFO_SIZE
);
9356 a2
= Expression::make_binary(OPERATOR_MULT
, a2
, ref
, loc
);
9358 if (element_type
->has_pointer())
9359 call
= Runtime::make_call(Runtime::MEMCLRHASPTR
, loc
, 2, a1
, a2
);
9362 Type
* int32_type
= Type::lookup_integer_type("int32");
9363 zero
= Expression::make_integer_ul(0, int32_type
, loc
);
9364 call
= Runtime::make_call(Runtime::BUILTIN_MEMSET
, loc
, 3, a1
,
9368 if (element_type
->has_pointer())
9370 // For a slice containing pointers, growslice already zeroed
9371 // the memory. We only need to zero in non-growing case.
9372 // Note: growslice does not zero the memory in non-pointer case.
9373 ref
= Expression::make_temporary_reference(ntmp
, loc
);
9374 ref
= Expression::make_cast(uint_type
, ref
, loc
);
9375 ref2
= Expression::make_temporary_reference(c1tmp
, loc
);
9376 ref2
= Expression::make_cast(uint_type
, ref2
, loc
);
9377 cond
= Expression::make_binary(OPERATOR_GT
, ref
, ref2
, loc
);
9378 zero
= Expression::make_integer_ul(0, int_type
, loc
);
9379 call
= Expression::make_conditional(cond
, zero
, call
, loc
);
9384 if (element_type
->has_pointer())
9386 // copy(s1tmp[l1tmp:], s2tmp)
9387 a1
= Expression::make_temporary_reference(s1tmp
, loc
);
9388 ref
= Expression::make_temporary_reference(l1tmp
, loc
);
9389 Expression
* nil
= Expression::make_nil(loc
);
9390 a1
= Expression::make_array_index(a1
, ref
, nil
, NULL
, loc
);
9391 a1
->array_index_expression()->set_needs_bounds_check(false);
9393 a2
= Expression::make_temporary_reference(s2tmp
, loc
);
9395 Named_object
* copyfn
= gogo
->lookup_global("copy");
9396 Expression
* copyref
= Expression::make_func_reference(copyfn
, NULL
, loc
);
9397 call_args
= new Expression_list();
9398 call_args
->push_back(a1
);
9399 call_args
->push_back(a2
);
9400 call
= Expression::make_call(copyref
, call_args
, false, loc
);
9404 // memmove(&s1tmp[l1tmp], s2tmp.ptr, l2tmp*sizeof(elem))
9405 a1
= Expression::make_temporary_reference(s1tmp
, loc
);
9406 ref
= Expression::make_temporary_reference(l1tmp
, loc
);
9407 a1
= Expression::make_array_index(a1
, ref
, NULL
, NULL
, loc
);
9408 a1
->array_index_expression()->set_needs_bounds_check(false);
9409 a1
= Expression::make_unary(OPERATOR_AND
, a1
, loc
);
9411 a2
= Expression::make_temporary_reference(s2tmp
, loc
);
9412 a2
= (a2
->type()->is_string_type()
9413 ? Expression::make_string_info(a2
,
9416 : Expression::make_slice_info(a2
,
9417 SLICE_INFO_VALUE_POINTER
,
9420 ref
= Expression::make_temporary_reference(l2tmp
, loc
);
9421 ref
= Expression::make_cast(uintptr_type
, ref
, loc
);
9422 a3
= Expression::make_type_info(element_type
, TYPE_INFO_SIZE
);
9423 a3
= Expression::make_binary(OPERATOR_MULT
, a3
, ref
, loc
);
9425 call
= Runtime::make_call(Runtime::BUILTIN_MEMMOVE
, loc
, 3,
9429 gogo
->lower_expression(function
, inserter
, &call
);
9430 gogo
->flatten_expression(function
, inserter
, &call
);
9431 inserter
->insert(Statement::make_statement(call
, false));
9435 // For each argument:
9436 // s1tmp[l1tmp+i] = a
9437 unsigned long i
= 0;
9438 for (Expression_list::const_iterator pa
= add
->begin();
9442 ref
= Expression::make_temporary_reference(s1tmp
, loc
);
9443 ref2
= Expression::make_temporary_reference(l1tmp
, loc
);
9444 Expression
* off
= Expression::make_integer_ul(i
, int_type
, loc
);
9445 ref2
= Expression::make_binary(OPERATOR_PLUS
, ref2
, off
, loc
);
9446 Expression
* lhs
= Expression::make_array_index(ref
, ref2
, NULL
,
9448 lhs
->array_index_expression()->set_needs_bounds_check(false);
9449 gogo
->lower_expression(function
, inserter
, &lhs
);
9450 gogo
->flatten_expression(function
, inserter
, &lhs
);
9451 Expression
* elem
= *pa
;
9452 if (!Type::are_identical(element_type
, elem
->type(), 0, NULL
)
9453 && element_type
->interface_type() != NULL
)
9454 elem
= Expression::make_cast(element_type
, elem
, loc
);
9455 // The flatten pass runs after the write barrier pass, so we
9456 // need to insert a write barrier here if necessary.
9457 // However, if ASSIGN_LHS is not NULL, we have been called
9458 // directly before the write barrier pass.
9460 if (assign_lhs
!= NULL
9461 || !gogo
->assign_needs_write_barrier(lhs
, NULL
))
9462 assign
= Statement::make_assignment(lhs
, elem
, loc
);
9465 Function
* f
= function
== NULL
? NULL
: function
->func_value();
9466 assign
= gogo
->assign_with_write_barrier(f
, NULL
, inserter
,
9469 inserter
->insert(assign
);
9473 if (assign_lhs
!= NULL
)
9476 return Expression::make_temporary_reference(s1tmp
, loc
);
9479 // Return whether an expression has an integer value. Report an error
9480 // if not. This is used when handling calls to the predeclared make
9481 // function. Set *SMALL if the value is known to fit in type "int".
9484 Builtin_call_expression::check_int_value(Expression
* e
, bool is_length
,
9489 Numeric_constant nc
;
9490 if (e
->numeric_constant_value(&nc
))
9493 switch (nc
.to_unsigned_long(&v
))
9495 case Numeric_constant::NC_UL_VALID
:
9497 case Numeric_constant::NC_UL_NOTINT
:
9498 go_error_at(e
->location(), "non-integer %s argument to make",
9499 is_length
? "len" : "cap");
9501 case Numeric_constant::NC_UL_NEGATIVE
:
9502 go_error_at(e
->location(), "negative %s argument to make",
9503 is_length
? "len" : "cap");
9505 case Numeric_constant::NC_UL_BIG
:
9506 // We don't want to give a compile-time error for a 64-bit
9507 // value on a 32-bit target.
9512 if (!nc
.to_int(&val
))
9514 int bits
= mpz_sizeinbase(val
, 2);
9516 Type
* int_type
= Type::lookup_integer_type("int");
9517 if (bits
>= int_type
->integer_type()->bits())
9519 go_error_at(e
->location(), "%s argument too large for make",
9520 is_length
? "len" : "cap");
9528 if (e
->type()->integer_type() != NULL
)
9530 int ebits
= e
->type()->integer_type()->bits();
9531 int intbits
= Type::lookup_integer_type("int")->integer_type()->bits();
9533 // We can treat ebits == intbits as small even for an unsigned
9534 // integer type, because we will convert the value to int and
9535 // then reject it in the runtime if it is negative.
9536 *small
= ebits
<= intbits
;
9541 go_error_at(e
->location(), "non-integer %s argument to make",
9542 is_length
? "len" : "cap");
9546 // Return the type of the real or imag functions, given the type of
9547 // the argument. We need to map complex64 to float32 and complex128
9548 // to float64, so it has to be done by name. This returns NULL if it
9549 // can't figure out the type.
9552 Builtin_call_expression::real_imag_type(Type
* arg_type
)
9554 if (arg_type
== NULL
|| arg_type
->is_abstract())
9556 Named_type
* nt
= arg_type
->named_type();
9559 while (nt
->real_type()->named_type() != NULL
)
9560 nt
= nt
->real_type()->named_type();
9561 if (nt
->name() == "complex64")
9562 return Type::lookup_float_type("float32");
9563 else if (nt
->name() == "complex128")
9564 return Type::lookup_float_type("float64");
9569 // Return the type of the complex function, given the type of one of the
9570 // argments. Like real_imag_type, we have to map by name.
9573 Builtin_call_expression::complex_type(Type
* arg_type
)
9575 if (arg_type
== NULL
|| arg_type
->is_abstract())
9577 Named_type
* nt
= arg_type
->named_type();
9580 while (nt
->real_type()->named_type() != NULL
)
9581 nt
= nt
->real_type()->named_type();
9582 if (nt
->name() == "float32")
9583 return Type::lookup_complex_type("complex64");
9584 else if (nt
->name() == "float64")
9585 return Type::lookup_complex_type("complex128");
9590 // Return a single argument, or NULL if there isn't one.
9593 Builtin_call_expression::one_arg() const
9595 const Expression_list
* args
= this->args();
9596 if (args
== NULL
|| args
->size() != 1)
9598 return args
->front();
9601 // A traversal class which looks for a call or receive expression.
9603 class Find_call_expression
: public Traverse
9606 Find_call_expression()
9607 : Traverse(traverse_expressions
),
9612 expression(Expression
**);
9616 { return this->found_
; }
9623 Find_call_expression::expression(Expression
** pexpr
)
9625 Expression
* expr
= *pexpr
;
9626 if (!expr
->is_constant()
9627 && (expr
->call_expression() != NULL
9628 || expr
->receive_expression() != NULL
))
9630 this->found_
= true;
9631 return TRAVERSE_EXIT
;
9633 return TRAVERSE_CONTINUE
;
9636 // Return whether calling len or cap on EXPR, of array type, is a
9637 // constant. The language spec says "the expressions len(s) and
9638 // cap(s) are constants if the type of s is an array or pointer to an
9639 // array and the expression s does not contain channel receives or
9640 // (non-constant) function calls."
9643 Builtin_call_expression::array_len_is_constant(Expression
* expr
)
9645 go_assert(expr
->type()->deref()->array_type() != NULL
9646 && !expr
->type()->deref()->is_slice_type());
9647 if (expr
->is_constant())
9649 Find_call_expression find_call
;
9650 Expression::traverse(&expr
, &find_call
);
9651 return !find_call
.found();
9654 // Return whether this is constant: len of a string constant, or len
9655 // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof,
9659 Builtin_call_expression::do_is_constant() const
9661 if (this->is_error_expression())
9663 switch (this->code_
)
9671 Expression
* arg
= this->one_arg();
9674 Type
* arg_type
= arg
->type();
9675 if (arg_type
->is_error())
9678 if (arg_type
->points_to() != NULL
9679 && arg_type
->points_to()->array_type() != NULL
9680 && !arg_type
->points_to()->is_slice_type())
9681 arg_type
= arg_type
->points_to();
9683 if (arg_type
->array_type() != NULL
9684 && arg_type
->array_type()->length() != NULL
)
9687 bool ret
= Builtin_call_expression::array_len_is_constant(arg
);
9688 this->seen_
= false;
9692 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
9695 bool ret
= arg
->is_constant();
9696 this->seen_
= false;
9702 case BUILTIN_SIZEOF
:
9703 case BUILTIN_ALIGNOF
:
9704 return this->one_arg() != NULL
;
9706 case BUILTIN_OFFSETOF
:
9708 Expression
* arg
= this->one_arg();
9711 return arg
->field_reference_expression() != NULL
;
9714 case BUILTIN_COMPLEX
:
9716 const Expression_list
* args
= this->args();
9717 if (args
!= NULL
&& args
->size() == 2)
9718 return args
->front()->is_constant() && args
->back()->is_constant();
9725 Expression
* arg
= this->one_arg();
9726 return arg
!= NULL
&& arg
->is_constant();
9736 // Return a numeric constant if possible.
9739 Builtin_call_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
9741 if (this->code_
== BUILTIN_LEN
9742 || this->code_
== BUILTIN_CAP
)
9744 Expression
* arg
= this->one_arg();
9747 Type
* arg_type
= arg
->type();
9748 if (arg_type
->is_error())
9751 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
9754 if (arg
->string_constant_value(&sval
))
9756 nc
->set_unsigned_long(Type::lookup_integer_type("int"),
9762 if (arg_type
->points_to() != NULL
9763 && arg_type
->points_to()->array_type() != NULL
9764 && !arg_type
->points_to()->is_slice_type())
9765 arg_type
= arg_type
->points_to();
9767 if (arg_type
->array_type() != NULL
9768 && arg_type
->array_type()->length() != NULL
)
9773 // We may be replacing this expression with a constant
9774 // during lowering, so verify the type to report any errors.
9775 // It's OK to verify an array type more than once.
9777 if (!arg_type
->is_error())
9779 Expression
* e
= arg_type
->array_type()->length();
9781 bool r
= e
->numeric_constant_value(nc
);
9782 this->seen_
= false;
9785 if (!nc
->set_type(Type::lookup_integer_type("int"), false,
9793 else if (this->code_
== BUILTIN_SIZEOF
9794 || this->code_
== BUILTIN_ALIGNOF
)
9796 Expression
* arg
= this->one_arg();
9799 Type
* arg_type
= arg
->type();
9800 if (arg_type
->is_error())
9802 if (arg_type
->is_abstract())
9803 arg_type
= arg_type
->make_non_abstract_type();
9808 if (this->code_
== BUILTIN_SIZEOF
)
9811 bool ok
= arg_type
->backend_type_size(this->gogo_
, &ret
);
9812 this->seen_
= false;
9816 else if (this->code_
== BUILTIN_ALIGNOF
)
9820 if (arg
->field_reference_expression() == NULL
)
9821 ok
= arg_type
->backend_type_align(this->gogo_
, &ret
);
9824 // Calling unsafe.Alignof(s.f) returns the alignment of
9825 // the type of f when it is used as a field in a struct.
9826 ok
= arg_type
->backend_type_field_align(this->gogo_
, &ret
);
9828 this->seen_
= false;
9836 set_mpz_from_int64(&zval
, ret
);
9837 nc
->set_int(Type::lookup_integer_type("uintptr"), zval
);
9841 else if (this->code_
== BUILTIN_OFFSETOF
)
9843 Expression
* arg
= this->one_arg();
9846 Field_reference_expression
* farg
= arg
->field_reference_expression();
9852 int64_t total_offset
= 0;
9855 Expression
* struct_expr
= farg
->expr();
9856 Type
* st
= struct_expr
->type();
9857 if (st
->struct_type() == NULL
)
9859 if (st
->named_type() != NULL
)
9860 st
->named_type()->convert(this->gogo_
);
9861 if (st
->is_error_type())
9863 go_assert(saw_errors());
9868 bool ok
= st
->struct_type()->backend_field_offset(this->gogo_
,
9869 farg
->field_index(),
9871 this->seen_
= false;
9874 total_offset
+= offset
;
9875 if (farg
->implicit() && struct_expr
->field_reference_expression() != NULL
)
9877 // Go up until we reach the original base.
9878 farg
= struct_expr
->field_reference_expression();
9884 set_mpz_from_int64(&zval
, total_offset
);
9885 nc
->set_int(Type::lookup_integer_type("uintptr"), zval
);
9889 else if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
9891 Expression
* arg
= this->one_arg();
9895 Numeric_constant argnc
;
9896 if (!arg
->numeric_constant_value(&argnc
))
9900 if (!argnc
.to_complex(&val
))
9903 Type
* type
= Builtin_call_expression::real_imag_type(argnc
.type());
9904 if (this->code_
== BUILTIN_REAL
)
9905 nc
->set_float(type
, mpc_realref(val
));
9907 nc
->set_float(type
, mpc_imagref(val
));
9911 else if (this->code_
== BUILTIN_COMPLEX
)
9913 const Expression_list
* args
= this->args();
9914 if (args
== NULL
|| args
->size() != 2)
9917 Numeric_constant rnc
;
9918 if (!args
->front()->numeric_constant_value(&rnc
))
9920 Numeric_constant inc
;
9921 if (!args
->back()->numeric_constant_value(&inc
))
9924 if (rnc
.type() != NULL
9925 && !rnc
.type()->is_abstract()
9926 && inc
.type() != NULL
9927 && !inc
.type()->is_abstract()
9928 && !Type::are_identical(rnc
.type(), inc
.type(),
9929 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
9934 if (!rnc
.to_float(&r
))
9937 if (!inc
.to_float(&i
))
9943 Type
* arg_type
= rnc
.type();
9944 if (arg_type
== NULL
|| arg_type
->is_abstract())
9945 arg_type
= inc
.type();
9948 mpc_init2(val
, mpc_precision
);
9949 mpc_set_fr_fr(val
, r
, i
, MPC_RNDNN
);
9953 Type
* type
= Builtin_call_expression::complex_type(arg_type
);
9954 nc
->set_complex(type
, val
);
9964 // Give an error if we are discarding the value of an expression which
9965 // should not normally be discarded. We don't give an error for
9966 // discarding the value of an ordinary function call, but we do for
9967 // builtin functions, purely for consistency with the gc compiler.
9970 Builtin_call_expression::do_discarding_value()
9972 switch (this->code_
)
9974 case BUILTIN_INVALID
:
9978 case BUILTIN_APPEND
:
9980 case BUILTIN_COMPLEX
:
9987 case BUILTIN_ALIGNOF
:
9988 case BUILTIN_OFFSETOF
:
9989 case BUILTIN_SIZEOF
:
9991 this->unused_value_error();
9996 case BUILTIN_DELETE
:
9999 case BUILTIN_PRINTLN
:
10000 case BUILTIN_RECOVER
:
10005 // Return the type.
10008 Builtin_call_expression::do_type()
10010 if (this->is_error_expression())
10011 return Type::make_error_type();
10012 switch (this->code_
)
10014 case BUILTIN_INVALID
:
10016 return Type::make_error_type();
10020 const Expression_list
* args
= this->args();
10021 if (args
== NULL
|| args
->empty())
10022 return Type::make_error_type();
10023 return Type::make_pointer_type(args
->front()->type());
10028 const Expression_list
* args
= this->args();
10029 if (args
== NULL
|| args
->empty())
10030 return Type::make_error_type();
10031 return args
->front()->type();
10037 return Type::lookup_integer_type("int");
10039 case BUILTIN_ALIGNOF
:
10040 case BUILTIN_OFFSETOF
:
10041 case BUILTIN_SIZEOF
:
10042 return Type::lookup_integer_type("uintptr");
10044 case BUILTIN_CLOSE
:
10045 case BUILTIN_DELETE
:
10046 case BUILTIN_PANIC
:
10047 case BUILTIN_PRINT
:
10048 case BUILTIN_PRINTLN
:
10049 return Type::make_void_type();
10051 case BUILTIN_RECOVER
:
10052 return Type::make_empty_interface_type(Linemap::predeclared_location());
10054 case BUILTIN_APPEND
:
10056 const Expression_list
* args
= this->args();
10057 if (args
== NULL
|| args
->empty())
10058 return Type::make_error_type();
10059 Type
*ret
= args
->front()->type();
10060 if (!ret
->is_slice_type())
10061 return Type::make_error_type();
10068 Expression
* arg
= this->one_arg();
10070 return Type::make_error_type();
10071 Type
* t
= arg
->type();
10072 if (t
->is_abstract())
10073 t
= t
->make_non_abstract_type();
10074 t
= Builtin_call_expression::real_imag_type(t
);
10076 t
= Type::make_error_type();
10080 case BUILTIN_COMPLEX
:
10082 const Expression_list
* args
= this->args();
10083 if (args
== NULL
|| args
->size() != 2)
10084 return Type::make_error_type();
10085 Type
* t
= args
->front()->type();
10086 if (t
->is_abstract())
10088 t
= args
->back()->type();
10089 if (t
->is_abstract())
10090 t
= t
->make_non_abstract_type();
10092 t
= Builtin_call_expression::complex_type(t
);
10094 t
= Type::make_error_type();
10099 return Type::make_pointer_type(Type::make_void_type());
10101 case BUILTIN_SLICE
:
10102 const Expression_list
* args
= this->args();
10103 if (args
== NULL
|| args
->size() != 2)
10104 return Type::make_error_type();
10105 Type
* pt
= args
->front()->type()->points_to();
10107 return Type::make_error_type();
10108 return Type::make_array_type(pt
, NULL
);
10112 // Determine the type.
10115 Builtin_call_expression::do_determine_type(const Type_context
* context
)
10117 if (!this->determining_types())
10120 this->fn()->determine_type_no_context();
10122 const Expression_list
* args
= this->args();
10125 Type
* arg_type
= NULL
;
10126 Type
* trailing_arg_types
= NULL
;
10127 switch (this->code_
)
10129 case BUILTIN_PRINT
:
10130 case BUILTIN_PRINTLN
:
10131 // Do not force a large integer constant to "int".
10137 arg_type
= Builtin_call_expression::complex_type(context
->type
);
10138 if (arg_type
== NULL
)
10139 arg_type
= Type::lookup_complex_type("complex128");
10143 case BUILTIN_COMPLEX
:
10145 // For the complex function the type of one operand can
10146 // determine the type of the other, as in a binary expression.
10147 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
10148 if (arg_type
== NULL
)
10149 arg_type
= Type::lookup_float_type("float64");
10150 if (args
!= NULL
&& args
->size() == 2)
10152 Type
* t1
= args
->front()->type();
10153 Type
* t2
= args
->back()->type();
10154 if (!t1
->is_abstract())
10156 else if (!t2
->is_abstract())
10163 case BUILTIN_APPEND
:
10164 if (!this->is_varargs()
10167 && args
->front()->type()->is_slice_type())
10168 trailing_arg_types
=
10169 args
->front()->type()->array_type()->element_type();
10174 case BUILTIN_SLICE
:
10175 // Both unsafe.Add and unsafe.Slice take two arguments, and the
10176 // second arguments defaults to "int".
10177 if (args
!= NULL
&& args
->size() == 2)
10179 if (this->code_
== BUILTIN_SLICE
)
10180 args
->front()->determine_type_no_context();
10183 Type
* pointer
= Type::make_pointer_type(Type::make_void_type());
10184 Type_context
subcontext(pointer
, false);
10185 args
->front()->determine_type(&subcontext
);
10187 Type
* int_type
= Type::lookup_integer_type("int");
10188 Type_context
subcontext(int_type
, false);
10189 args
->back()->determine_type(&subcontext
);
10202 for (Expression_list::const_iterator pa
= args
->begin();
10206 Type_context subcontext
;
10207 subcontext
.type
= arg_type
;
10211 // We want to print large constants, we so can't just
10212 // use the appropriate nonabstract type. Use uint64 for
10213 // an integer if we know it is nonnegative, otherwise
10214 // use int64 for a integer, otherwise use float64 for a
10215 // float or complex128 for a complex.
10216 Type
* want_type
= NULL
;
10217 Type
* atype
= (*pa
)->type();
10218 if (atype
->is_abstract())
10220 if (atype
->integer_type() != NULL
)
10222 Numeric_constant nc
;
10223 if (this->numeric_constant_value(&nc
))
10226 if (nc
.to_int(&val
))
10228 if (mpz_sgn(val
) >= 0)
10229 want_type
= Type::lookup_integer_type("uint64");
10233 if (want_type
== NULL
)
10234 want_type
= Type::lookup_integer_type("int64");
10236 else if (atype
->float_type() != NULL
)
10237 want_type
= Type::lookup_float_type("float64");
10238 else if (atype
->complex_type() != NULL
)
10239 want_type
= Type::lookup_complex_type("complex128");
10240 else if (atype
->is_abstract_string_type())
10241 want_type
= Type::lookup_string_type();
10242 else if (atype
->is_abstract_boolean_type())
10243 want_type
= Type::lookup_bool_type();
10246 subcontext
.type
= want_type
;
10250 (*pa
)->determine_type(&subcontext
);
10252 if (trailing_arg_types
!= NULL
)
10254 arg_type
= trailing_arg_types
;
10255 trailing_arg_types
= NULL
;
10261 // If there is exactly one argument, return true. Otherwise give an
10262 // error message and return false.
10265 Builtin_call_expression::check_one_arg()
10267 const Expression_list
* args
= this->args();
10268 if (args
== NULL
|| args
->size() < 1)
10270 this->report_error(_("not enough arguments"));
10273 else if (args
->size() > 1)
10275 this->report_error(_("too many arguments"));
10278 if (args
->front()->is_error_expression()
10279 || args
->front()->type()->is_error())
10281 this->set_is_error();
10287 // Check argument types for a builtin function.
10290 Builtin_call_expression::do_check_types(Gogo
*)
10292 if (this->is_error_expression())
10294 switch (this->code_
)
10296 case BUILTIN_INVALID
:
10299 case BUILTIN_DELETE
:
10305 // The single argument may be either a string or an array or a
10306 // map or a channel, or a pointer to a closed array.
10307 if (this->check_one_arg())
10309 Type
* arg_type
= this->one_arg()->type();
10310 if (arg_type
->points_to() != NULL
10311 && arg_type
->points_to()->array_type() != NULL
10312 && !arg_type
->points_to()->is_slice_type())
10313 arg_type
= arg_type
->points_to();
10314 if (this->code_
== BUILTIN_CAP
)
10316 if (!arg_type
->is_error()
10317 && arg_type
->array_type() == NULL
10318 && arg_type
->channel_type() == NULL
)
10319 this->report_error(_("argument must be array or slice "
10324 if (!arg_type
->is_error()
10325 && !arg_type
->is_string_type()
10326 && arg_type
->array_type() == NULL
10327 && arg_type
->map_type() == NULL
10328 && arg_type
->channel_type() == NULL
)
10329 this->report_error(_("argument must be string or "
10330 "array or slice or map or channel"));
10336 case BUILTIN_PRINT
:
10337 case BUILTIN_PRINTLN
:
10339 const Expression_list
* args
= this->args();
10342 for (Expression_list::const_iterator p
= args
->begin();
10346 Type
* type
= (*p
)->type();
10347 if (type
->is_error()
10348 || type
->is_string_type()
10349 || type
->integer_type() != NULL
10350 || type
->float_type() != NULL
10351 || type
->complex_type() != NULL
10352 || type
->is_boolean_type()
10353 || type
->points_to() != NULL
10354 || type
->interface_type() != NULL
10355 || type
->channel_type() != NULL
10356 || type
->map_type() != NULL
10357 || type
->function_type() != NULL
10358 || type
->is_slice_type())
10360 else if ((*p
)->is_type_expression())
10362 // If this is a type expression it's going to give
10363 // an error anyhow, so we don't need one here.
10366 this->report_error(_("unsupported argument type to "
10367 "builtin function"));
10373 case BUILTIN_CLOSE
:
10374 if (this->check_one_arg())
10376 if (this->one_arg()->type()->channel_type() == NULL
)
10377 this->report_error(_("argument must be channel"));
10378 else if (!this->one_arg()->type()->channel_type()->may_send())
10379 this->report_error(_("cannot close receive-only channel"));
10383 case BUILTIN_PANIC
:
10384 case BUILTIN_SIZEOF
:
10385 case BUILTIN_ALIGNOF
:
10386 this->check_one_arg();
10389 case BUILTIN_RECOVER
:
10390 if (this->args() != NULL
10391 && !this->args()->empty()
10392 && !this->recover_arg_is_set_
)
10393 this->report_error(_("too many arguments"));
10396 case BUILTIN_OFFSETOF
:
10397 if (this->check_one_arg())
10399 Expression
* arg
= this->one_arg();
10400 if (arg
->field_reference_expression() == NULL
)
10401 this->report_error(_("argument must be a field reference"));
10407 const Expression_list
* args
= this->args();
10408 if (args
== NULL
|| args
->size() < 2)
10410 this->report_error(_("not enough arguments"));
10413 else if (args
->size() > 2)
10415 this->report_error(_("too many arguments"));
10418 Type
* arg1_type
= args
->front()->type();
10419 Type
* arg2_type
= args
->back()->type();
10420 if (arg1_type
->is_error() || arg2_type
->is_error())
10422 this->set_is_error();
10427 if (arg1_type
->is_slice_type())
10428 e1
= arg1_type
->array_type()->element_type();
10431 this->report_error(_("left argument must be a slice"));
10435 if (arg2_type
->is_slice_type())
10437 Type
* e2
= arg2_type
->array_type()->element_type();
10438 if (!Type::are_identical(e1
, e2
, Type::COMPARE_TAGS
, NULL
))
10439 this->report_error(_("element types must be the same"));
10441 else if (arg2_type
->is_string_type())
10443 if (e1
->integer_type() == NULL
|| !e1
->integer_type()->is_byte())
10444 this->report_error(_("first argument must be []byte"));
10447 this->report_error(_("second argument must be slice or string"));
10451 case BUILTIN_APPEND
:
10453 const Expression_list
* args
= this->args();
10454 if (args
== NULL
|| args
->empty())
10456 this->report_error(_("not enough arguments"));
10460 Type
* slice_type
= args
->front()->type();
10461 if (!slice_type
->is_slice_type())
10463 if (slice_type
->is_error_type())
10465 if (slice_type
->is_nil_type())
10466 go_error_at(args
->front()->location(), "use of untyped nil");
10468 go_error_at(args
->front()->location(),
10469 "argument 1 must be a slice");
10470 this->set_is_error();
10474 Type
* element_type
= slice_type
->array_type()->element_type();
10475 if (!element_type
->in_heap())
10476 go_error_at(args
->front()->location(),
10477 "cannot append to slice of go:notinheap type");
10478 if (this->is_varargs())
10480 if (!args
->back()->type()->is_slice_type()
10481 && !args
->back()->type()->is_string_type())
10483 go_error_at(args
->back()->location(),
10484 "invalid use of %<...%> with non-slice/non-string");
10485 this->set_is_error();
10489 if (args
->size() < 2)
10491 this->report_error(_("not enough arguments"));
10494 if (args
->size() > 2)
10496 this->report_error(_("too many arguments"));
10500 if (args
->back()->type()->is_string_type()
10501 && element_type
->integer_type() != NULL
10502 && element_type
->integer_type()->is_byte())
10504 // Permit append(s1, s2...) when s1 is a slice of
10505 // bytes and s2 is a string type.
10509 // We have to test for assignment compatibility to a
10510 // slice of the element type, which is not necessarily
10511 // the same as the type of the first argument: the
10512 // first argument might have a named type.
10513 Type
* check_type
= Type::make_array_type(element_type
, NULL
);
10514 std::string reason
;
10515 if (!Type::are_assignable(check_type
, args
->back()->type(),
10518 if (reason
.empty())
10519 go_error_at(args
->back()->location(),
10520 "argument 2 has invalid type");
10522 go_error_at(args
->back()->location(),
10523 "argument 2 has invalid type (%s)",
10525 this->set_is_error();
10532 Expression_list::const_iterator pa
= args
->begin();
10534 for (++pa
; pa
!= args
->end(); ++pa
, ++i
)
10536 std::string reason
;
10537 if (!Type::are_assignable(element_type
, (*pa
)->type(),
10540 if (reason
.empty())
10541 go_error_at((*pa
)->location(),
10542 "argument %d has incompatible type", i
);
10544 go_error_at((*pa
)->location(),
10545 "argument %d has incompatible type (%s)",
10546 i
, reason
.c_str());
10547 this->set_is_error();
10556 if (this->check_one_arg())
10558 if (this->one_arg()->type()->complex_type() == NULL
)
10559 this->report_error(_("argument must have complex type"));
10563 case BUILTIN_COMPLEX
:
10565 const Expression_list
* args
= this->args();
10566 if (args
== NULL
|| args
->size() < 2)
10567 this->report_error(_("not enough arguments"));
10568 else if (args
->size() > 2)
10569 this->report_error(_("too many arguments"));
10570 else if (args
->front()->is_error_expression()
10571 || args
->front()->type()->is_error()
10572 || args
->back()->is_error_expression()
10573 || args
->back()->type()->is_error())
10574 this->set_is_error();
10575 else if (!Type::are_identical(args
->front()->type(),
10576 args
->back()->type(),
10577 Type::COMPARE_TAGS
, NULL
))
10578 this->report_error(_("complex arguments must have identical types"));
10579 else if (args
->front()->type()->float_type() == NULL
)
10580 this->report_error(_("complex arguments must have "
10581 "floating-point type"));
10586 case BUILTIN_SLICE
:
10588 Numeric_constant nc
;
10590 const Expression_list
* args
= this->args();
10591 if (args
== NULL
|| args
->size() < 2)
10592 this->report_error(_("not enough arguments"));
10593 else if (args
->size() > 2)
10594 this->report_error(_("too many arguments"));
10595 else if (args
->front()->is_error_expression()
10596 || args
->front()->type()->is_error()
10597 || args
->back()->is_error_expression()
10598 || args
->back()->type()->is_error())
10599 this->set_is_error();
10600 else if (args
->back()->type()->integer_type() == NULL
10601 && (!args
->back()->type()->is_abstract()
10602 || !args
->back()->numeric_constant_value(&nc
)
10603 || (nc
.to_unsigned_long(&v
)
10604 == Numeric_constant::NC_UL_NOTINT
)))
10606 if (this->code_
== BUILTIN_ADD
)
10607 go_error_at(args
->back()->location(), "non-integer offset");
10609 go_error_at(args
->back()->location(), "non-integer size");
10611 else if (this->code_
== BUILTIN_ADD
)
10613 Type
* pointer_type
=
10614 Type::make_pointer_type(Type::make_void_type());
10615 std::string reason
;
10616 if (!Type::are_assignable(pointer_type
, args
->front()->type(),
10619 if (reason
.empty())
10620 go_error_at(args
->front()->location(),
10621 "argument 1 has incompatible type");
10623 go_error_at(args
->front()->location(),
10624 "argument 1 has incompatible type (%s)",
10626 this->set_is_error();
10631 if (args
->front()->type()->points_to() == NULL
)
10633 go_error_at(args
->front()->location(),
10634 "argument 1 must be a pointer");
10635 this->set_is_error();
10638 unsigned int int_bits
=
10639 Type::lookup_integer_type("int")->integer_type()->bits();
10642 if (args
->back()->numeric_constant_value(&nc
) && nc
.to_int(&ival
))
10644 if (mpz_sgn(ival
) < 0
10645 || mpz_sizeinbase(ival
, 2) >= int_bits
)
10647 go_error_at(args
->back()->location(),
10648 "slice length out of range");
10649 this->set_is_error();
10663 Builtin_call_expression::do_copy()
10665 Call_expression
* bce
=
10666 new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
10667 (this->args() == NULL
10669 : this->args()->copy()),
10670 this->is_varargs(),
10673 if (this->varargs_are_lowered())
10674 bce
->set_varargs_are_lowered();
10675 if (this->is_deferred())
10676 bce
->set_is_deferred();
10677 if (this->is_concurrent())
10678 bce
->set_is_concurrent();
10682 // Return the backend representation for a builtin function.
10685 Builtin_call_expression::do_get_backend(Translate_context
* context
)
10687 Gogo
* gogo
= context
->gogo();
10688 Location location
= this->location();
10690 if (this->is_erroneous_call())
10692 go_assert(saw_errors());
10693 return gogo
->backend()->error_expression();
10696 switch (this->code_
)
10698 case BUILTIN_INVALID
:
10702 case BUILTIN_SLICE
:
10708 const Expression_list
* args
= this->args();
10709 go_assert(args
!= NULL
&& args
->size() == 1);
10710 Expression
* arg
= args
->front();
10711 Type
* arg_type
= arg
->type();
10715 go_assert(saw_errors());
10716 return context
->backend()->error_expression();
10718 this->seen_
= true;
10719 this->seen_
= false;
10720 if (arg_type
->points_to() != NULL
)
10722 arg_type
= arg_type
->points_to();
10723 go_assert(arg_type
->array_type() != NULL
10724 && !arg_type
->is_slice_type());
10725 arg
= Expression::make_dereference(arg
, NIL_CHECK_DEFAULT
,
10729 Type
* int_type
= Type::lookup_integer_type("int");
10731 if (this->code_
== BUILTIN_LEN
)
10733 if (arg_type
->is_string_type())
10734 val
= Expression::make_string_info(arg
, STRING_INFO_LENGTH
,
10736 else if (arg_type
->array_type() != NULL
)
10740 go_assert(saw_errors());
10741 return context
->backend()->error_expression();
10743 this->seen_
= true;
10744 val
= arg_type
->array_type()->get_length(gogo
, arg
);
10745 this->seen_
= false;
10747 else if (arg_type
->map_type() != NULL
10748 || arg_type
->channel_type() != NULL
)
10750 // The first field is the length. If the pointer is
10751 // nil, the length is zero.
10752 Type
* pint_type
= Type::make_pointer_type(int_type
);
10753 arg
= Expression::make_unsafe_cast(pint_type
, arg
, location
);
10754 Expression
* nil
= Expression::make_nil(location
);
10755 nil
= Expression::make_cast(pint_type
, nil
, location
);
10756 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
,
10757 arg
, nil
, location
);
10758 Expression
* zero
= Expression::make_integer_ul(0, int_type
,
10760 Expression
* indir
=
10761 Expression::make_dereference(arg
, NIL_CHECK_NOT_NEEDED
,
10763 val
= Expression::make_conditional(cmp
, zero
, indir
, location
);
10770 if (arg_type
->array_type() != NULL
)
10774 go_assert(saw_errors());
10775 return context
->backend()->error_expression();
10777 this->seen_
= true;
10778 val
= arg_type
->array_type()->get_capacity(gogo
, arg
);
10779 this->seen_
= false;
10781 else if (arg_type
->channel_type() != NULL
)
10783 // The second field is the capacity. If the pointer
10784 // is nil, the capacity is zero.
10785 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
10786 Type
* pint_type
= Type::make_pointer_type(int_type
);
10787 Expression
* parg
= Expression::make_unsafe_cast(uintptr_type
,
10790 int off
= int_type
->integer_type()->bits() / 8;
10791 Expression
* eoff
= Expression::make_integer_ul(off
,
10794 parg
= Expression::make_binary(OPERATOR_PLUS
, parg
, eoff
,
10796 parg
= Expression::make_unsafe_cast(pint_type
, parg
, location
);
10797 Expression
* nil
= Expression::make_nil(location
);
10798 nil
= Expression::make_cast(pint_type
, nil
, location
);
10799 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
,
10800 arg
, nil
, location
);
10801 Expression
* zero
= Expression::make_integer_ul(0, int_type
,
10803 Expression
* indir
=
10804 Expression::make_dereference(parg
, NIL_CHECK_NOT_NEEDED
,
10806 val
= Expression::make_conditional(cmp
, zero
, indir
, location
);
10812 return Expression::make_cast(int_type
, val
,
10813 location
)->get_backend(context
);
10816 case BUILTIN_PRINT
:
10817 case BUILTIN_PRINTLN
:
10819 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
10821 Expression
* print_stmts
= Runtime::make_call(Runtime::PRINTLOCK
,
10824 const Expression_list
* call_args
= this->args();
10825 if (call_args
!= NULL
)
10827 for (Expression_list::const_iterator p
= call_args
->begin();
10828 p
!= call_args
->end();
10831 if (is_ln
&& p
!= call_args
->begin())
10833 Expression
* print_space
=
10834 Runtime::make_call(Runtime::PRINTSP
, location
, 0);
10837 Expression::make_compound(print_stmts
, print_space
,
10841 Expression
* arg
= *p
;
10842 Type
* type
= arg
->type();
10843 Runtime::Function code
;
10844 if (type
->is_string_type())
10845 code
= Runtime::PRINTSTRING
;
10846 else if (type
->integer_type() != NULL
10847 && type
->integer_type()->is_unsigned())
10849 Type
* itype
= Type::lookup_integer_type("uint64");
10850 arg
= Expression::make_cast(itype
, arg
, location
);
10851 if (gogo
->compiling_runtime()
10852 && type
->named_type() != NULL
10853 && gogo
->unpack_hidden_name(type
->named_type()->name())
10855 code
= Runtime::PRINTHEX
;
10857 code
= Runtime::PRINTUINT
;
10859 else if (type
->integer_type() != NULL
)
10861 Type
* itype
= Type::lookup_integer_type("int64");
10862 arg
= Expression::make_cast(itype
, arg
, location
);
10863 code
= Runtime::PRINTINT
;
10865 else if (type
->float_type() != NULL
)
10867 Type
* dtype
= Type::lookup_float_type("float64");
10868 arg
= Expression::make_cast(dtype
, arg
, location
);
10869 code
= Runtime::PRINTFLOAT
;
10871 else if (type
->complex_type() != NULL
)
10873 Type
* ctype
= Type::lookup_complex_type("complex128");
10874 arg
= Expression::make_cast(ctype
, arg
, location
);
10875 code
= Runtime::PRINTCOMPLEX
;
10877 else if (type
->is_boolean_type())
10878 code
= Runtime::PRINTBOOL
;
10879 else if (type
->points_to() != NULL
10880 || type
->channel_type() != NULL
10881 || type
->map_type() != NULL
10882 || type
->function_type() != NULL
)
10884 arg
= Expression::make_cast(type
, arg
, location
);
10885 code
= Runtime::PRINTPOINTER
;
10887 else if (type
->interface_type() != NULL
)
10889 if (type
->interface_type()->is_empty())
10890 code
= Runtime::PRINTEFACE
;
10892 code
= Runtime::PRINTIFACE
;
10894 else if (type
->is_slice_type())
10895 code
= Runtime::PRINTSLICE
;
10898 go_assert(saw_errors());
10899 return context
->backend()->error_expression();
10902 Expression
* call
= Runtime::make_call(code
, location
, 1, arg
);
10903 print_stmts
= Expression::make_compound(print_stmts
, call
,
10910 Expression
* print_nl
=
10911 Runtime::make_call(Runtime::PRINTNL
, location
, 0);
10912 print_stmts
= Expression::make_compound(print_stmts
, print_nl
,
10916 Expression
* unlock
= Runtime::make_call(Runtime::PRINTUNLOCK
,
10918 print_stmts
= Expression::make_compound(print_stmts
, unlock
, location
);
10920 return print_stmts
->get_backend(context
);
10923 case BUILTIN_PANIC
:
10925 const Expression_list
* args
= this->args();
10926 go_assert(args
!= NULL
&& args
->size() == 1);
10927 Expression
* arg
= args
->front();
10929 Type::make_empty_interface_type(Linemap::predeclared_location());
10930 arg
= Expression::convert_for_assignment(gogo
, empty
, arg
, location
);
10932 Expression
* panic
=
10933 Runtime::make_call(Runtime::GOPANIC
, location
, 1, arg
);
10934 return panic
->get_backend(context
);
10937 case BUILTIN_RECOVER
:
10939 // The argument is set when building recover thunks. It's a
10940 // boolean value which is true if we can recover a value now.
10941 const Expression_list
* args
= this->args();
10942 go_assert(args
!= NULL
&& args
->size() == 1);
10943 Expression
* arg
= args
->front();
10945 Type::make_empty_interface_type(Linemap::predeclared_location());
10947 Expression
* nil
= Expression::make_nil(location
);
10948 nil
= Expression::make_interface_value(empty
, nil
, nil
, location
);
10950 // We need to handle a deferred call to recover specially,
10951 // because it changes whether it can recover a panic or not.
10952 // See test7 in test/recover1.go.
10953 Expression
* recover
= Runtime::make_call((this->is_deferred()
10954 ? Runtime::DEFERREDRECOVER
10955 : Runtime::GORECOVER
),
10958 Expression::make_conditional(arg
, recover
, nil
, location
);
10959 return cond
->get_backend(context
);
10962 case BUILTIN_CLOSE
:
10964 const Expression_list
* args
= this->args();
10965 go_assert(args
!= NULL
&& args
->size() == 1);
10966 Expression
* arg
= args
->front();
10967 Expression
* close
= Runtime::make_call(Runtime::CLOSE
, location
,
10969 return close
->get_backend(context
);
10972 case BUILTIN_SIZEOF
:
10973 case BUILTIN_OFFSETOF
:
10974 case BUILTIN_ALIGNOF
:
10976 Numeric_constant nc
;
10978 if (!this->numeric_constant_value(&nc
)
10979 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
10981 go_assert(saw_errors());
10982 return context
->backend()->error_expression();
10984 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
10987 Expression
* int_cst
=
10988 Expression::make_integer_z(&ival
, uintptr_type
, location
);
10990 return int_cst
->get_backend(context
);
10994 // Handled in Builtin_call_expression::do_flatten.
10997 case BUILTIN_APPEND
:
10998 // Handled in Builtin_call_expression::flatten_append.
11004 const Expression_list
* args
= this->args();
11005 go_assert(args
!= NULL
&& args
->size() == 1);
11008 Bexpression
* bcomplex
= args
->front()->get_backend(context
);
11009 if (this->code_
== BUILTIN_REAL
)
11010 ret
= gogo
->backend()->real_part_expression(bcomplex
, location
);
11012 ret
= gogo
->backend()->imag_part_expression(bcomplex
, location
);
11016 case BUILTIN_COMPLEX
:
11018 const Expression_list
* args
= this->args();
11019 go_assert(args
!= NULL
&& args
->size() == 2);
11020 Bexpression
* breal
= args
->front()->get_backend(context
);
11021 Bexpression
* bimag
= args
->back()->get_backend(context
);
11022 return gogo
->backend()->complex_expression(breal
, bimag
, location
);
11030 // We have to support exporting a builtin call expression, because
11031 // code can set a constant to the result of a builtin expression.
11034 Builtin_call_expression::do_export(Export_function_body
* efb
) const
11036 Numeric_constant nc
;
11037 if (this->numeric_constant_value(&nc
))
11043 Integer_expression::export_integer(efb
, val
);
11046 else if (nc
.is_float())
11049 nc
.get_float(&fval
);
11050 Float_expression::export_float(efb
, fval
);
11053 else if (nc
.is_complex())
11056 nc
.get_complex(&cval
);
11057 Complex_expression::export_complex(efb
, cval
);
11063 // A trailing space lets us reliably identify the end of the number.
11064 efb
->write_c_string(" ");
11066 else if (this->code_
== BUILTIN_ADD
|| this->code_
== BUILTIN_SLICE
)
11069 snprintf(buf
, sizeof buf
, "<p%d>%s", efb
->unsafe_package_index(),
11070 (this->code_
== BUILTIN_ADD
? "Add" : "Slice"));
11071 efb
->write_c_string(buf
);
11072 this->export_arguments(efb
);
11076 const char *s
= NULL
;
11077 switch (this->code_
)
11081 case BUILTIN_APPEND
:
11093 case BUILTIN_DELETE
:
11096 case BUILTIN_PRINT
:
11099 case BUILTIN_PRINTLN
:
11102 case BUILTIN_PANIC
:
11105 case BUILTIN_RECOVER
:
11108 case BUILTIN_CLOSE
:
11117 case BUILTIN_COMPLEX
:
11121 efb
->write_c_string(s
);
11122 this->export_arguments(efb
);
11126 // Class Call_expression.
11128 // A Go function can be viewed in a couple of different ways. The
11129 // code of a Go function becomes a backend function with parameters
11130 // whose types are simply the backend representation of the Go types.
11131 // If there are multiple results, they are returned as a backend
11134 // However, when Go code refers to a function other than simply
11135 // calling it, the backend type of that function is actually a struct.
11136 // The first field of the struct points to the Go function code
11137 // (sometimes a wrapper as described below). The remaining fields
11138 // hold addresses of closed-over variables. This struct is called a
11141 // There are a few cases to consider.
11143 // A direct function call of a known function in package scope. In
11144 // this case there are no closed-over variables, and we know the name
11145 // of the function code. We can simply produce a backend call to the
11146 // function directly, and not worry about the closure.
11148 // A direct function call of a known function literal. In this case
11149 // we know the function code and we know the closure. We generate the
11150 // function code such that it expects an additional final argument of
11151 // the closure type. We pass the closure as the last argument, after
11152 // the other arguments.
11154 // An indirect function call. In this case we have a closure. We
11155 // load the pointer to the function code from the first field of the
11156 // closure. We pass the address of the closure as the last argument.
11158 // A call to a method of an interface. Type methods are always at
11159 // package scope, so we call the function directly, and don't worry
11160 // about the closure.
11162 // This means that for a function at package scope we have two cases.
11163 // One is the direct call, which has no closure. The other is the
11164 // indirect call, which does have a closure. We can't simply ignore
11165 // the closure, even though it is the last argument, because that will
11166 // fail on targets where the function pops its arguments. So when
11167 // generating a closure for a package-scope function we set the
11168 // function code pointer in the closure to point to a wrapper
11169 // function. This wrapper function accepts a final argument that
11170 // points to the closure, ignores it, and calls the real function as a
11171 // direct function call. This wrapper will normally be efficient, and
11172 // can often simply be a tail call to the real function.
11174 // We don't use GCC's static chain pointer because 1) we don't need
11175 // it; 2) GCC only permits using a static chain to call a known
11176 // function, so we can't use it for an indirect call anyhow. Since we
11177 // can't use it for an indirect call, we may as well not worry about
11178 // using it for a direct call either.
11180 // We pass the closure last rather than first because it means that
11181 // the function wrapper we put into a closure for a package-scope
11182 // function can normally just be a tail call to the real function.
11184 // For method expressions we generate a wrapper that loads the
11185 // receiver from the closure and then calls the method. This
11186 // unfortunately forces reshuffling the arguments, since there is a
11187 // new first argument, but we can't avoid reshuffling either for
11188 // method expressions or for indirect calls of package-scope
11189 // functions, and since the latter are more common we reshuffle for
11190 // method expressions.
11192 // Note that the Go code retains the Go types. The extra final
11193 // argument only appears when we convert to the backend
11199 Call_expression::do_traverse(Traverse
* traverse
)
11201 // If we are calling a function in a different package that returns
11202 // an unnamed type, this may be the only chance we get to traverse
11203 // that type. We don't traverse this->type_ because it may be a
11204 // Call_multiple_result_type that will just lead back here.
11205 if (this->type_
!= NULL
&& !this->type_
->is_error_type())
11207 Function_type
*fntype
= this->get_function_type();
11208 if (fntype
!= NULL
&& Type::traverse(fntype
, traverse
) == TRAVERSE_EXIT
)
11209 return TRAVERSE_EXIT
;
11211 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
11212 return TRAVERSE_EXIT
;
11213 if (this->args_
!= NULL
)
11215 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
11216 return TRAVERSE_EXIT
;
11218 return TRAVERSE_CONTINUE
;
11221 // Lower a call statement.
11224 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
11225 Statement_inserter
* inserter
, int)
11227 Location loc
= this->location();
11229 if (this->is_error_expression())
11230 return Expression::make_error(loc
);
11232 // A type cast can look like a function call.
11233 if (this->fn_
->is_type_expression()
11234 && this->args_
!= NULL
11235 && this->args_
->size() == 1)
11237 if (this->expected_result_count_
!= 0
11238 && this->expected_result_count_
!= 1)
11240 this->report_error(_("type conversion result count mismatch"));
11241 return Expression::make_error(loc
);
11243 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
11247 // Because do_type will return an error type and thus prevent future
11248 // errors, check for that case now to ensure that the error gets
11250 Function_type
* fntype
= this->get_function_type();
11251 if (fntype
== NULL
)
11253 if (!this->fn_
->type()->is_error())
11254 this->report_error(_("expected function"));
11255 this->set_is_error();
11259 // Handle an argument which is a call to a function which returns
11260 // multiple results.
11261 if (this->args_
!= NULL
11262 && this->args_
->size() == 1
11263 && this->args_
->front()->call_expression() != NULL
)
11265 size_t rc
= this->args_
->front()->call_expression()->result_count();
11267 && ((fntype
->parameters() != NULL
11268 && (fntype
->parameters()->size() == rc
11269 || (fntype
->is_varargs()
11270 && fntype
->parameters()->size() - 1 <= rc
)))
11271 || fntype
->is_builtin()))
11273 Call_expression
* call
= this->args_
->front()->call_expression();
11274 call
->set_is_multi_value_arg();
11275 if (this->is_varargs_
)
11277 // It is not clear which result of a multiple result call
11278 // the ellipsis operator should be applied to. If we unpack the
11279 // the call into its individual results here, the ellipsis will be
11280 // applied to the last result.
11281 go_error_at(call
->location(),
11282 _("multiple-value argument in single-value context"));
11283 return Expression::make_error(call
->location());
11286 Expression_list
* args
= new Expression_list
;
11287 for (size_t i
= 0; i
< rc
; ++i
)
11288 args
->push_back(Expression::make_call_result(call
, i
));
11289 // We can't return a new call expression here, because this
11290 // one may be referenced by Call_result expressions. We
11291 // also can't delete the old arguments, because we may still
11292 // traverse them somewhere up the call stack. FIXME.
11293 this->args_
= args
;
11297 // Recognize a call to a builtin function.
11298 if (fntype
->is_builtin())
11300 Builtin_call_expression
* bce
=
11301 new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
11302 this->is_varargs_
, loc
);
11303 if (this->is_deferred_
)
11304 bce
->set_is_deferred();
11305 if (this->is_concurrent_
)
11306 bce
->set_is_concurrent();
11310 // If this call returns multiple results, create a temporary
11311 // variable to hold them.
11312 if (this->result_count() > 1 && this->call_temp_
== NULL
)
11314 Struct_field_list
* sfl
= new Struct_field_list();
11315 const Typed_identifier_list
* results
= fntype
->results();
11319 for (Typed_identifier_list::const_iterator p
= results
->begin();
11320 p
!= results
->end();
11323 snprintf(buf
, sizeof buf
, "res%d", i
);
11324 sfl
->push_back(Struct_field(Typed_identifier(buf
, p
->type(), loc
)));
11327 Struct_type
* st
= Type::make_struct_type(sfl
, loc
);
11328 st
->set_is_struct_incomparable();
11329 this->call_temp_
= Statement::make_temporary(st
, NULL
, loc
);
11330 inserter
->insert(this->call_temp_
);
11333 // Handle a call to a varargs function by packaging up the extra
11335 if (fntype
->is_varargs())
11337 const Typed_identifier_list
* parameters
= fntype
->parameters();
11338 go_assert(parameters
!= NULL
&& !parameters
->empty());
11339 Type
* varargs_type
= parameters
->back().type();
11340 this->lower_varargs(gogo
, function
, inserter
, varargs_type
,
11341 parameters
->size(), SLICE_STORAGE_MAY_ESCAPE
);
11344 // If this is call to a method, call the method directly passing the
11345 // object as the first parameter.
11346 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
11347 if (bme
!= NULL
&& !this->is_deferred_
&& !this->is_concurrent_
)
11349 Named_object
* methodfn
= bme
->function();
11350 Function_type
* mft
= (methodfn
->is_function()
11351 ? methodfn
->func_value()->type()
11352 : methodfn
->func_declaration_value()->type());
11353 Expression
* first_arg
= bme
->first_argument();
11355 // We always pass a pointer when calling a method, except for
11356 // direct interface types when calling a value method.
11357 if (!first_arg
->type()->is_error()
11358 && first_arg
->type()->points_to() == NULL
11359 && !first_arg
->type()->is_direct_iface_type())
11361 first_arg
= Expression::make_unary(OPERATOR_AND
, first_arg
, loc
);
11362 // We may need to create a temporary variable so that we can
11363 // take the address. We can't do that here because it will
11364 // mess up the order of evaluation.
11365 Unary_expression
* ue
= static_cast<Unary_expression
*>(first_arg
);
11366 ue
->set_create_temp();
11368 else if (mft
->receiver()->type()->points_to() == NULL
11369 && first_arg
->type()->points_to() != NULL
11370 && first_arg
->type()->points_to()->is_direct_iface_type())
11371 first_arg
= Expression::make_dereference(first_arg
,
11372 Expression::NIL_CHECK_DEFAULT
,
11375 // If we are calling a method which was inherited from an
11376 // embedded struct, and the method did not get a stub, then the
11377 // first type may be wrong.
11378 Type
* fatype
= bme
->first_argument_type();
11379 if (fatype
!= NULL
)
11381 if (fatype
->points_to() == NULL
)
11382 fatype
= Type::make_pointer_type(fatype
);
11383 first_arg
= Expression::make_unsafe_cast(fatype
, first_arg
, loc
);
11386 Expression_list
* new_args
= new Expression_list();
11387 new_args
->push_back(first_arg
);
11388 if (this->args_
!= NULL
)
11390 for (Expression_list::const_iterator p
= this->args_
->begin();
11391 p
!= this->args_
->end();
11393 new_args
->push_back(*p
);
11396 // We have to change in place because this structure may be
11397 // referenced by Call_result_expressions. We can't delete the
11398 // old arguments, because we may be traversing them up in some
11400 this->args_
= new_args
;
11401 this->fn_
= Expression::make_func_reference(methodfn
, NULL
,
11405 // If this is a call to an imported function for which we have an
11406 // inlinable function body, add it to the list of functions to give
11407 // to the backend as inlining opportunities.
11408 Func_expression
* fe
= this->fn_
->func_expression();
11410 && fe
->named_object()->is_function_declaration()
11411 && fe
->named_object()->func_declaration_value()->has_imported_body())
11412 gogo
->add_imported_inlinable_function(fe
->named_object());
11417 // Lower a call to a varargs function. FUNCTION is the function in
11418 // which the call occurs--it's not the function we are calling.
11419 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
11420 // PARAM_COUNT is the number of parameters of the function we are
11421 // calling; the last of these parameters will be the varargs
11425 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
11426 Statement_inserter
* inserter
,
11427 Type
* varargs_type
, size_t param_count
,
11428 Slice_storage_escape_disp escape_disp
)
11430 if (this->varargs_are_lowered_
)
11433 Location loc
= this->location();
11435 go_assert(param_count
> 0);
11436 go_assert(varargs_type
->is_slice_type());
11438 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
11439 if (arg_count
< param_count
- 1)
11441 // Not enough arguments; will be caught in check_types.
11445 Expression_list
* old_args
= this->args_
;
11446 Expression_list
* new_args
= new Expression_list();
11447 bool push_empty_arg
= false;
11448 if (old_args
== NULL
|| old_args
->empty())
11450 go_assert(param_count
== 1);
11451 push_empty_arg
= true;
11455 Expression_list::const_iterator pa
;
11457 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
11459 if (static_cast<size_t>(i
) == param_count
)
11461 new_args
->push_back(*pa
);
11464 // We have reached the varargs parameter.
11466 bool issued_error
= false;
11467 if (pa
== old_args
->end())
11468 push_empty_arg
= true;
11469 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
11470 new_args
->push_back(*pa
);
11471 else if (this->is_varargs_
)
11473 if ((*pa
)->type()->is_slice_type())
11474 this->report_error(_("too many arguments"));
11477 go_error_at(this->location(),
11478 _("invalid use of %<...%> with non-slice"));
11479 this->set_is_error();
11485 Type
* element_type
= varargs_type
->array_type()->element_type();
11486 Expression_list
* vals
= new Expression_list
;
11487 for (; pa
!= old_args
->end(); ++pa
, ++i
)
11489 // Check types here so that we get a better message.
11490 Type
* patype
= (*pa
)->type();
11491 Location paloc
= (*pa
)->location();
11492 if (!this->check_argument_type(i
, element_type
, patype
,
11493 paloc
, issued_error
))
11495 vals
->push_back(*pa
);
11497 Slice_construction_expression
* sce
=
11498 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
11499 if (escape_disp
== SLICE_STORAGE_DOES_NOT_ESCAPE
)
11500 sce
->set_storage_does_not_escape();
11501 Expression
* val
= sce
;
11502 gogo
->lower_expression(function
, inserter
, &val
);
11503 new_args
->push_back(val
);
11507 if (push_empty_arg
)
11508 new_args
->push_back(Expression::make_nil(loc
));
11510 // We can't return a new call expression here, because this one may
11511 // be referenced by Call_result expressions. FIXME. We can't
11512 // delete OLD_ARGS because we may have both a Call_expression and a
11513 // Builtin_call_expression which refer to them. FIXME.
11514 this->args_
= new_args
;
11515 this->varargs_are_lowered_
= true;
11518 // Flatten a call with multiple results into a temporary.
11521 Call_expression::do_flatten(Gogo
* gogo
, Named_object
*,
11522 Statement_inserter
* inserter
)
11524 if (this->is_erroneous_call())
11526 go_assert(saw_errors());
11527 return Expression::make_error(this->location());
11530 if (this->is_flattened_
)
11532 this->is_flattened_
= true;
11534 // Add temporary variables for all arguments that require type
11536 Function_type
* fntype
= this->get_function_type();
11537 if (fntype
== NULL
)
11539 go_assert(saw_errors());
11542 if (this->args_
!= NULL
&& !this->args_
->empty()
11543 && fntype
->parameters() != NULL
&& !fntype
->parameters()->empty())
11545 bool is_interface_method
=
11546 this->fn_
->interface_field_reference_expression() != NULL
;
11548 Expression_list
*args
= new Expression_list();
11549 Typed_identifier_list::const_iterator pp
= fntype
->parameters()->begin();
11550 Expression_list::const_iterator pa
= this->args_
->begin();
11551 if (!is_interface_method
&& fntype
->is_method())
11553 // The receiver argument.
11554 args
->push_back(*pa
);
11557 for (; pa
!= this->args_
->end(); ++pa
, ++pp
)
11559 go_assert(pp
!= fntype
->parameters()->end());
11560 if (Type::are_identical(pp
->type(), (*pa
)->type(),
11561 Type::COMPARE_TAGS
, NULL
))
11562 args
->push_back(*pa
);
11565 Location loc
= (*pa
)->location();
11566 Expression
* arg
= *pa
;
11567 if (!arg
->is_multi_eval_safe())
11569 Temporary_statement
*temp
=
11570 Statement::make_temporary(NULL
, arg
, loc
);
11571 inserter
->insert(temp
);
11572 arg
= Expression::make_temporary_reference(temp
, loc
);
11574 arg
= Expression::convert_for_assignment(gogo
, pp
->type(), arg
,
11576 args
->push_back(arg
);
11579 delete this->args_
;
11580 this->args_
= args
;
11583 // Lower to compiler intrinsic if possible.
11584 Func_expression
* fe
= this->fn_
->func_expression();
11585 if (!this->is_concurrent_
&& !this->is_deferred_
11587 && (fe
->named_object()->is_function_declaration()
11588 || fe
->named_object()->is_function()))
11590 Expression
* ret
= this->intrinsify(gogo
, inserter
);
11595 // Add an implicit conversion to a boolean type, if needed. See the
11596 // comment in Binary_expression::lower_array_comparison.
11597 if (this->is_equal_function_
11598 && this->type_
!= NULL
11599 && this->type_
!= Type::lookup_bool_type())
11600 return Expression::make_cast(this->type_
, this, this->location());
11605 // Lower a call to a compiler intrinsic if possible.
11606 // Returns NULL if it is not an intrinsic.
11609 Call_expression::intrinsify(Gogo
* gogo
,
11610 Statement_inserter
* inserter
)
11612 Func_expression
* fe
= this->fn_
->func_expression();
11613 Named_object
* no
= fe
->named_object();
11614 std::string name
= Gogo::unpack_hidden_name(no
->name());
11615 std::string package
= (no
->package() != NULL
11616 ? no
->package()->pkgpath()
11617 : gogo
->pkgpath());
11618 bool is_method
= ((no
->is_function() && no
->func_value()->is_method())
11619 || (no
->is_function_declaration()
11620 && no
->func_declaration_value()->is_method()));
11621 Location loc
= this->location();
11623 Type
* int_type
= Type::lookup_integer_type("int");
11624 Type
* int32_type
= Type::lookup_integer_type("int32");
11625 Type
* int64_type
= Type::lookup_integer_type("int64");
11626 Type
* uint_type
= Type::lookup_integer_type("uint");
11627 Type
* uint8_type
= Type::lookup_integer_type("uint8");
11628 Type
* uint32_type
= Type::lookup_integer_type("uint32");
11629 Type
* uint64_type
= Type::lookup_integer_type("uint64");
11630 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
11631 Type
* pointer_type
= Type::make_pointer_type(Type::make_void_type());
11633 int int_size
= int_type
->named_type()->real_type()->integer_type()->bits() / 8;
11634 int ptr_size
= uintptr_type
->named_type()->real_type()->integer_type()->bits() / 8;
11636 if (package
== "sync/atomic")
11641 // sync/atomic functions and runtime/internal/atomic functions
11642 // are very similar. In order not to duplicate code, we just
11643 // redirect to the latter and let the code below to handle them.
11644 // Note: no StorePointer, SwapPointer, and CompareAndSwapPointer,
11645 // as they need write barriers.
11646 if (name
== "LoadInt32")
11647 name
= "Loadint32";
11648 else if (name
== "LoadInt64")
11649 name
= "Loadint64";
11650 else if (name
== "LoadUint32")
11652 else if (name
== "LoadUint64")
11654 else if (name
== "LoadUintptr")
11655 name
= "Loaduintptr";
11656 else if (name
== "LoadPointer")
11658 else if (name
== "StoreInt32")
11659 name
= "Storeint32";
11660 else if (name
== "StoreInt64")
11661 name
= "Storeint64";
11662 else if (name
== "StoreUint32")
11664 else if (name
== "StoreUint64")
11666 else if (name
== "StoreUintptr")
11667 name
= "Storeuintptr";
11668 else if (name
== "AddInt32")
11669 name
= "Xaddint32";
11670 else if (name
== "AddInt64")
11671 name
= "Xaddint64";
11672 else if (name
== "AddUint32")
11674 else if (name
== "AddUint64")
11676 else if (name
== "AddUintptr")
11677 name
= "Xadduintptr";
11678 else if (name
== "SwapInt32")
11679 name
= "Xchgint32";
11680 else if (name
== "SwapInt64")
11681 name
= "Xchgint64";
11682 else if (name
== "SwapUint32")
11684 else if (name
== "SwapUint64")
11686 else if (name
== "SwapUintptr")
11687 name
= "Xchguintptr";
11688 else if (name
== "CompareAndSwapInt32")
11690 else if (name
== "CompareAndSwapInt64")
11692 else if (name
== "CompareAndSwapUint32")
11694 else if (name
== "CompareAndSwapUint64")
11696 else if (name
== "CompareAndSwapUintptr")
11697 name
= "Casuintptr";
11701 package
= "runtime/internal/atomic";
11704 if (package
== "runtime/internal/sys")
11709 // runtime/internal/sys functions and math/bits functions
11710 // are very similar. In order not to duplicate code, we just
11711 // redirect to the latter and let the code below to handle them.
11712 if (name
== "Bswap32")
11713 name
= "ReverseBytes32";
11714 else if (name
== "Bswap64")
11715 name
= "ReverseBytes64";
11716 else if (name
== "Ctz32")
11717 name
= "TrailingZeros32";
11718 else if (name
== "Ctz64")
11719 name
= "TrailingZeros64";
11723 package
= "math/bits";
11726 if (package
== "runtime")
11731 // Handle a couple of special runtime functions. In the runtime
11732 // package, getcallerpc returns the PC of the caller, and
11733 // getcallersp returns the frame pointer of the caller. Implement
11734 // these by turning them into calls to GCC builtin functions. We
11735 // could implement them in normal code, but then we would have to
11736 // explicitly unwind the stack. These functions are intended to be
11737 // efficient. Note that this technique obviously only works for
11738 // direct calls, but that is the only way they are used.
11739 if (name
== "getcallerpc"
11740 && (this->args_
== NULL
|| this->args_
->size() == 0))
11742 Expression
* arg
= Expression::make_integer_ul(0, uint32_type
, loc
);
11744 Runtime::make_call(Runtime::BUILTIN_RETURN_ADDRESS
, loc
,
11746 // The builtin functions return void*, but the Go functions return uintptr.
11747 return Expression::make_cast(uintptr_type
, call
, loc
);
11749 else if (name
== "getcallersp"
11750 && (this->args_
== NULL
|| this->args_
->size() == 0))
11754 Runtime::make_call(Runtime::BUILTIN_DWARF_CFA
, loc
, 0);
11755 // The builtin functions return void*, but the Go functions return uintptr.
11756 return Expression::make_cast(uintptr_type
, call
, loc
);
11759 else if (package
== "math/bits")
11764 if ((name
== "ReverseBytes16" || name
== "ReverseBytes32"
11765 || name
== "ReverseBytes64" || name
== "ReverseBytes")
11766 && this->args_
!= NULL
&& this->args_
->size() == 1)
11768 Runtime::Function code
;
11769 if (name
== "ReverseBytes16")
11770 code
= Runtime::BUILTIN_BSWAP16
;
11771 else if (name
== "ReverseBytes32")
11772 code
= Runtime::BUILTIN_BSWAP32
;
11773 else if (name
== "ReverseBytes64")
11774 code
= Runtime::BUILTIN_BSWAP64
;
11775 else if (name
== "ReverseBytes")
11776 code
= (int_size
== 8 ? Runtime::BUILTIN_BSWAP64
: Runtime::BUILTIN_BSWAP32
);
11779 Expression
* arg
= this->args_
->front();
11780 Expression
* call
= Runtime::make_call(code
, loc
, 1, arg
);
11781 if (name
== "ReverseBytes")
11782 return Expression::make_cast(uint_type
, call
, loc
);
11785 else if ((name
== "TrailingZeros8" || name
== "TrailingZeros16")
11786 && this->args_
!= NULL
&& this->args_
->size() == 1)
11788 // GCC does not have a ctz8 or ctz16 intrinsic. We do
11789 // ctz32(0x100 | arg) or ctz32(0x10000 | arg).
11790 Expression
* arg
= this->args_
->front();
11791 arg
= Expression::make_cast(uint32_type
, arg
, loc
);
11792 unsigned long mask
= (name
== "TrailingZeros8" ? 0x100 : 0x10000);
11793 Expression
* c
= Expression::make_integer_ul(mask
, uint32_type
, loc
);
11794 arg
= Expression::make_binary(OPERATOR_OR
, arg
, c
, loc
);
11795 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CTZ
, loc
, 1, arg
);
11796 return Expression::make_cast(int_type
, call
, loc
);
11798 else if ((name
== "TrailingZeros32"
11799 || (name
== "TrailingZeros" && int_size
== 4))
11800 && this->args_
!= NULL
&& this->args_
->size() == 1)
11802 Expression
* arg
= this->args_
->front();
11803 if (!arg
->is_multi_eval_safe())
11805 Temporary_statement
* ts
= Statement::make_temporary(uint32_type
, arg
, loc
);
11806 inserter
->insert(ts
);
11807 arg
= Expression::make_temporary_reference(ts
, loc
);
11809 // arg == 0 ? 32 : __builtin_ctz(arg)
11810 Expression
* zero
= Expression::make_integer_ul(0, uint32_type
, loc
);
11811 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
, arg
, zero
, loc
);
11812 Expression
* c32
= Expression::make_integer_ul(32, int_type
, loc
);
11813 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CTZ
, loc
, 1, arg
->copy());
11814 call
= Expression::make_cast(int_type
, call
, loc
);
11815 return Expression::make_conditional(cmp
, c32
, call
, loc
);
11817 else if ((name
== "TrailingZeros64"
11818 || (name
== "TrailingZeros" && int_size
== 8))
11819 && this->args_
!= NULL
&& this->args_
->size() == 1)
11821 Expression
* arg
= this->args_
->front();
11822 if (!arg
->is_multi_eval_safe())
11824 Temporary_statement
* ts
= Statement::make_temporary(uint64_type
, arg
, loc
);
11825 inserter
->insert(ts
);
11826 arg
= Expression::make_temporary_reference(ts
, loc
);
11828 // arg == 0 ? 64 : __builtin_ctzll(arg)
11829 Expression
* zero
= Expression::make_integer_ul(0, uint64_type
, loc
);
11830 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
, arg
, zero
, loc
);
11831 Expression
* c64
= Expression::make_integer_ul(64, int_type
, loc
);
11832 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CTZLL
, loc
, 1, arg
->copy());
11833 call
= Expression::make_cast(int_type
, call
, loc
);
11834 return Expression::make_conditional(cmp
, c64
, call
, loc
);
11836 else if ((name
== "LeadingZeros8" || name
== "LeadingZeros16"
11837 || name
== "Len8" || name
== "Len16")
11838 && this->args_
!= NULL
&& this->args_
->size() == 1)
11840 // GCC does not have a clz8 ir clz16 intrinsic. We do
11841 // clz32(arg<<24 | 0xffffff) or clz32(arg<<16 | 0xffff).
11842 Expression
* arg
= this->args_
->front();
11843 arg
= Expression::make_cast(uint32_type
, arg
, loc
);
11844 unsigned long shift
=
11845 ((name
== "LeadingZeros8" || name
== "Len8") ? 24 : 16);
11846 Expression
* c
= Expression::make_integer_ul(shift
, uint32_type
, loc
);
11847 arg
= Expression::make_binary(OPERATOR_LSHIFT
, arg
, c
, loc
);
11848 unsigned long mask
=
11849 ((name
== "LeadingZeros8" || name
== "Len8") ? 0xffffff : 0xffff);
11850 c
= Expression::make_integer_ul(mask
, uint32_type
, loc
);
11851 arg
= Expression::make_binary(OPERATOR_OR
, arg
, c
, loc
);
11852 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CLZ
, loc
, 1, arg
);
11853 call
= Expression::make_cast(int_type
, call
, loc
);
11854 // len = width - clz
11855 if (name
== "Len8")
11857 c
= Expression::make_integer_ul(8, int_type
, loc
);
11858 return Expression::make_binary(OPERATOR_MINUS
, c
, call
, loc
);
11860 else if (name
== "Len16")
11862 c
= Expression::make_integer_ul(16, int_type
, loc
);
11863 return Expression::make_binary(OPERATOR_MINUS
, c
, call
, loc
);
11867 else if ((name
== "LeadingZeros32" || name
== "Len32"
11868 || ((name
== "LeadingZeros" || name
== "Len") && int_size
== 4))
11869 && this->args_
!= NULL
&& this->args_
->size() == 1)
11871 Expression
* arg
= this->args_
->front();
11872 if (!arg
->is_multi_eval_safe())
11874 Temporary_statement
* ts
= Statement::make_temporary(uint32_type
, arg
, loc
);
11875 inserter
->insert(ts
);
11876 arg
= Expression::make_temporary_reference(ts
, loc
);
11878 // arg == 0 ? 32 : __builtin_clz(arg)
11879 Expression
* zero
= Expression::make_integer_ul(0, uint32_type
, loc
);
11880 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
, arg
, zero
, loc
);
11881 Expression
* c32
= Expression::make_integer_ul(32, int_type
, loc
);
11882 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CLZ
, loc
, 1, arg
->copy());
11883 call
= Expression::make_cast(int_type
, call
, loc
);
11884 Expression
* cond
= Expression::make_conditional(cmp
, c32
, call
, loc
);
11886 if (name
== "Len32" || name
== "Len")
11887 return Expression::make_binary(OPERATOR_MINUS
, c32
->copy(), cond
, loc
);
11890 else if ((name
== "LeadingZeros64" || name
== "Len64"
11891 || ((name
== "LeadingZeros" || name
== "Len") && int_size
== 8))
11892 && this->args_
!= NULL
&& this->args_
->size() == 1)
11894 Expression
* arg
= this->args_
->front();
11895 if (!arg
->is_multi_eval_safe())
11897 Temporary_statement
* ts
= Statement::make_temporary(uint64_type
, arg
, loc
);
11898 inserter
->insert(ts
);
11899 arg
= Expression::make_temporary_reference(ts
, loc
);
11901 // arg == 0 ? 64 : __builtin_clzll(arg)
11902 Expression
* zero
= Expression::make_integer_ul(0, uint64_type
, loc
);
11903 Expression
* cmp
= Expression::make_binary(OPERATOR_EQEQ
, arg
, zero
, loc
);
11904 Expression
* c64
= Expression::make_integer_ul(64, int_type
, loc
);
11905 Expression
* call
= Runtime::make_call(Runtime::BUILTIN_CLZLL
, loc
, 1, arg
->copy());
11906 call
= Expression::make_cast(int_type
, call
, loc
);
11907 Expression
* cond
= Expression::make_conditional(cmp
, c64
, call
, loc
);
11909 if (name
== "Len64" || name
== "Len")
11910 return Expression::make_binary(OPERATOR_MINUS
, c64
->copy(), cond
, loc
);
11913 else if ((name
== "OnesCount8" || name
== "OnesCount16"
11914 || name
== "OnesCount32" || name
== "OnesCount64"
11915 || name
== "OnesCount")
11916 && this->args_
!= NULL
&& this->args_
->size() == 1)
11918 Runtime::Function code
;
11919 if (name
== "OnesCount64")
11920 code
= Runtime::BUILTIN_POPCOUNTLL
;
11921 else if (name
== "OnesCount")
11922 code
= (int_size
== 8 ? Runtime::BUILTIN_POPCOUNTLL
: Runtime::BUILTIN_POPCOUNT
);
11924 code
= Runtime::BUILTIN_POPCOUNT
;
11925 Expression
* arg
= this->args_
->front();
11926 Expression
* call
= Runtime::make_call(code
, loc
, 1, arg
);
11927 return Expression::make_cast(int_type
, call
, loc
);
11930 else if (package
== "runtime/internal/atomic")
11932 int memorder
= __ATOMIC_SEQ_CST
;
11936 Function_type
* ftype
= (no
->is_function()
11937 ? no
->func_value()->type()
11938 : no
->func_declaration_value()->type());
11939 Type
* rtype
= ftype
->receiver()->type()->deref();
11940 go_assert(rtype
->named_type() != NULL
);
11941 const std::string
& rname(rtype
->named_type()->name());
11942 if (rname
== "Int32")
11944 if (name
== "Load")
11945 name
= "LoadInt32";
11946 else if (name
== "Store")
11947 name
= "Storeint32";
11948 else if (name
== "CompareAndSwap")
11950 else if (name
== "Swap")
11951 name
= "Xchgint32";
11952 else if (name
== "Add")
11953 name
= "Xaddint32";
11957 else if (rname
== "Int64")
11959 if (name
== "Load")
11960 name
= "LoadInt64";
11961 else if (name
== "Store")
11962 name
= "Storeint64";
11963 else if (name
== "CompareAndSwap")
11965 else if (name
== "Swap")
11966 name
= "Xchgint64";
11967 else if (name
== "Add")
11968 name
= "Xaddint64";
11972 else if (rname
== "Uint8")
11974 if (name
== "Load")
11976 else if (name
== "Store")
11978 else if (name
== "And")
11980 else if (name
== "Or")
11985 else if (rname
== "Uint32")
11987 if (name
== "Load")
11989 else if (name
== "LoadAcquire")
11991 else if (name
== "Store")
11993 else if (name
== "CompareAndSwap")
11995 else if (name
== "CompareAndSwapRelease")
11997 else if (name
== "Swap")
11999 else if (name
== "And")
12001 else if (name
== "Or")
12003 else if (name
== "Add")
12008 else if (rname
== "Uint64")
12010 if (name
== "Load")
12012 else if (name
== "Store")
12014 else if (name
== "CompareAndSwap")
12016 else if (name
== "Swap")
12018 else if (name
== "Add")
12023 else if (rname
== "Uintptr")
12025 if (name
== "Load")
12026 name
= "Loaduintptr";
12027 else if (name
== "LoadAcquire")
12028 name
= "Loadacquintptr";
12029 else if (name
== "Store")
12030 name
= "Storeuintptr";
12031 else if (name
== "StoreRelease")
12032 name
= "StoreReluintptr";
12033 else if (name
== "CompareAndSwap")
12034 name
= "Casuintptr";
12035 else if (name
== "Swap")
12036 name
= "Xchguintptr";
12037 else if (name
== "Add")
12038 name
= "Xadduintptr";
12042 else if (rname
== "Float64")
12044 // Needs unsafe type conversion. Don't intrinsify for now.
12047 else if (rname
== "UnsafePointer")
12049 if (name
== "Load")
12051 else if (name
== "StoreNoWB")
12052 name
= "StorepoWB";
12053 else if (name
== "CompareAndSwapNoWB")
12062 if ((name
== "Load" || name
== "Load64" || name
== "Loadint64" || name
== "Loadp"
12063 || name
== "Loaduint" || name
== "Loaduintptr" || name
== "LoadAcq"
12064 || name
== "Loadint32" || name
== "Load8")
12065 && this->args_
!= NULL
&& this->args_
->size() == 1)
12067 if (int_size
< 8 && (name
== "Load64" || name
== "Loadint64"))
12068 // On 32-bit architectures we need to check alignment.
12069 // Not intrinsify for now.
12072 Runtime::Function code
;
12074 if (name
== "Load")
12076 code
= Runtime::ATOMIC_LOAD_4
;
12077 res_type
= uint32_type
;
12079 else if (name
== "Load64")
12081 code
= Runtime::ATOMIC_LOAD_8
;
12082 res_type
= uint64_type
;
12084 else if (name
== "Loadint32")
12086 code
= Runtime::ATOMIC_LOAD_4
;
12087 res_type
= int32_type
;
12089 else if (name
== "Loadint64")
12091 code
= Runtime::ATOMIC_LOAD_8
;
12092 res_type
= int64_type
;
12094 else if (name
== "Loaduint")
12096 code
= (int_size
== 8
12097 ? Runtime::ATOMIC_LOAD_8
12098 : Runtime::ATOMIC_LOAD_4
);
12099 res_type
= uint_type
;
12101 else if (name
== "Loaduintptr")
12103 code
= (ptr_size
== 8
12104 ? Runtime::ATOMIC_LOAD_8
12105 : Runtime::ATOMIC_LOAD_4
);
12106 res_type
= uintptr_type
;
12108 else if (name
== "Loadp")
12110 code
= (ptr_size
== 8
12111 ? Runtime::ATOMIC_LOAD_8
12112 : Runtime::ATOMIC_LOAD_4
);
12113 res_type
= pointer_type
;
12115 else if (name
== "LoadAcq")
12117 code
= Runtime::ATOMIC_LOAD_4
;
12118 res_type
= uint32_type
;
12119 memorder
= __ATOMIC_ACQUIRE
;
12121 else if (name
== "Load8")
12123 code
= Runtime::ATOMIC_LOAD_1
;
12124 res_type
= uint8_type
;
12128 Expression
* a1
= this->args_
->front();
12129 Expression
* a2
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
12130 Expression
* call
= Runtime::make_call(code
, loc
, 2, a1
, a2
);
12131 return Expression::make_unsafe_cast(res_type
, call
, loc
);
12134 if ((name
== "Store" || name
== "Store64" || name
== "StorepNoWB"
12135 || name
== "Storeuintptr" || name
== "StoreRel"
12136 || name
== "Storeint32" || name
== "Storeint64")
12137 && this->args_
!= NULL
&& this->args_
->size() == 2)
12139 if (int_size
< 8 && (name
== "Store64" || name
== "Storeint64"))
12142 Runtime::Function code
;
12143 Expression
* a1
= this->args_
->at(0);
12144 Expression
* a2
= this->args_
->at(1);
12145 if (name
== "Store")
12146 code
= Runtime::ATOMIC_STORE_4
;
12147 else if (name
== "Store64")
12148 code
= Runtime::ATOMIC_STORE_8
;
12149 else if (name
== "Storeint32")
12150 code
= Runtime::ATOMIC_STORE_4
;
12151 else if (name
== "Storeint64")
12152 code
= Runtime::ATOMIC_STORE_8
;
12153 else if (name
== "Storeuintptr")
12154 code
= (ptr_size
== 8 ? Runtime::ATOMIC_STORE_8
: Runtime::ATOMIC_STORE_4
);
12155 else if (name
== "StorepNoWB")
12157 code
= (ptr_size
== 8 ? Runtime::ATOMIC_STORE_8
: Runtime::ATOMIC_STORE_4
);
12158 a2
= Expression::make_unsafe_cast(uintptr_type
, a2
, loc
);
12159 a2
= Expression::make_cast(uint64_type
, a2
, loc
);
12161 else if (name
== "StoreRel")
12163 code
= Runtime::ATOMIC_STORE_4
;
12164 memorder
= __ATOMIC_RELEASE
;
12166 else if (name
== "Store8")
12167 code
= Runtime::ATOMIC_STORE_1
;
12170 Expression
* a3
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
12171 return Runtime::make_call(code
, loc
, 3, a1
, a2
, a3
);
12174 if ((name
== "Xchg" || name
== "Xchg64" || name
== "Xchguintptr"
12175 || name
== "Xchgint32" || name
== "Xchgint64")
12176 && this->args_
!= NULL
&& this->args_
->size() == 2)
12178 if (int_size
< 8 && (name
== "Xchg64" || name
== "Xchgint64"))
12181 Runtime::Function code
;
12183 if (name
== "Xchg")
12185 code
= Runtime::ATOMIC_EXCHANGE_4
;
12186 res_type
= uint32_type
;
12188 else if (name
== "Xchg64")
12190 code
= Runtime::ATOMIC_EXCHANGE_8
;
12191 res_type
= uint64_type
;
12193 else if (name
== "Xchgint32")
12195 code
= Runtime::ATOMIC_EXCHANGE_4
;
12196 res_type
= int32_type
;
12198 else if (name
== "Xchgint64")
12200 code
= Runtime::ATOMIC_EXCHANGE_8
;
12201 res_type
= int64_type
;
12203 else if (name
== "Xchguintptr")
12205 code
= (ptr_size
== 8
12206 ? Runtime::ATOMIC_EXCHANGE_8
12207 : Runtime::ATOMIC_EXCHANGE_4
);
12208 res_type
= uintptr_type
;
12212 Expression
* a1
= this->args_
->at(0);
12213 Expression
* a2
= this->args_
->at(1);
12214 Expression
* a3
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
12215 Expression
* call
= Runtime::make_call(code
, loc
, 3, a1
, a2
, a3
);
12216 return Expression::make_cast(res_type
, call
, loc
);
12219 if ((name
== "Cas" || name
== "Cas64" || name
== "Casuintptr"
12220 || name
== "Casp1" || name
== "CasRel"
12221 || name
== "Casint32" || name
== "Casint64")
12222 && this->args_
!= NULL
&& this->args_
->size() == 3)
12224 if (int_size
< 8 && (name
== "Cas64" || name
== "Casint64"))
12227 Runtime::Function code
;
12228 Expression
* a1
= this->args_
->at(0);
12230 // Builtin cas takes a pointer to the old value.
12231 // Store it in a temporary and take the address.
12232 Expression
* a2
= this->args_
->at(1);
12233 Temporary_statement
* ts
= Statement::make_temporary(NULL
, a2
, loc
);
12234 inserter
->insert(ts
);
12235 a2
= Expression::make_temporary_reference(ts
, loc
);
12236 a2
= Expression::make_unary(OPERATOR_AND
, a2
, loc
);
12238 Expression
* a3
= this->args_
->at(2);
12240 code
= Runtime::ATOMIC_COMPARE_EXCHANGE_4
;
12241 else if (name
== "Cas64")
12242 code
= Runtime::ATOMIC_COMPARE_EXCHANGE_8
;
12243 else if (name
== "Casint32")
12244 code
= Runtime::ATOMIC_COMPARE_EXCHANGE_4
;
12245 else if (name
== "Casint64")
12246 code
= Runtime::ATOMIC_COMPARE_EXCHANGE_8
;
12247 else if (name
== "Casuintptr")
12248 code
= (ptr_size
== 8
12249 ? Runtime::ATOMIC_COMPARE_EXCHANGE_8
12250 : Runtime::ATOMIC_COMPARE_EXCHANGE_4
);
12251 else if (name
== "Casp1")
12253 code
= (ptr_size
== 8
12254 ? Runtime::ATOMIC_COMPARE_EXCHANGE_8
12255 : Runtime::ATOMIC_COMPARE_EXCHANGE_4
);
12256 a3
= Expression::make_unsafe_cast(uintptr_type
, a3
, loc
);
12257 a3
= Expression::make_cast(uint64_type
, a3
, loc
);
12259 else if (name
== "CasRel")
12261 code
= Runtime::ATOMIC_COMPARE_EXCHANGE_4
;
12262 memorder
= __ATOMIC_RELEASE
;
12266 Expression
* a4
= Expression::make_boolean(false, loc
);
12267 Expression
* a5
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
12268 Expression
* a6
= Expression::make_integer_ul(__ATOMIC_RELAXED
, int32_type
, loc
);
12269 return Runtime::make_call(code
, loc
, 6, a1
, a2
, a3
, a4
, a5
, a6
);
12272 if ((name
== "Xadd" || name
== "Xadd64" || name
== "Xaddint64"
12273 || name
== "Xadduintptr" || name
== "Xaddint32")
12274 && this->args_
!= NULL
&& this->args_
->size() == 2)
12276 if (int_size
< 8 && (name
== "Xadd64" || name
== "Xaddint64"))
12279 Runtime::Function code
;
12281 if (name
== "Xadd")
12283 code
= Runtime::ATOMIC_ADD_FETCH_4
;
12284 res_type
= uint32_type
;
12286 else if (name
== "Xadd64")
12288 code
= Runtime::ATOMIC_ADD_FETCH_8
;
12289 res_type
= uint64_type
;
12291 else if (name
== "Xaddint32")
12293 code
= Runtime::ATOMIC_ADD_FETCH_4
;
12294 res_type
= int32_type
;
12296 else if (name
== "Xaddint64")
12298 code
= Runtime::ATOMIC_ADD_FETCH_8
;
12299 res_type
= int64_type
;
12301 else if (name
== "Xadduintptr")
12303 code
= (ptr_size
== 8
12304 ? Runtime::ATOMIC_ADD_FETCH_8
12305 : Runtime::ATOMIC_ADD_FETCH_4
);
12306 res_type
= uintptr_type
;
12310 Expression
* a1
= this->args_
->at(0);
12311 Expression
* a2
= this->args_
->at(1);
12312 Expression
* a3
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
12313 Expression
* call
= Runtime::make_call(code
, loc
, 3, a1
, a2
, a3
);
12314 return Expression::make_cast(res_type
, call
, loc
);
12317 if ((name
== "And8" || name
== "Or8")
12318 && this->args_
!= NULL
&& this->args_
->size() == 2)
12320 Runtime::Function code
;
12321 if (name
== "And8")
12322 code
= Runtime::ATOMIC_AND_FETCH_1
;
12323 else if (name
== "Or8")
12324 code
= Runtime::ATOMIC_OR_FETCH_1
;
12327 Expression
* a1
= this->args_
->at(0);
12328 Expression
* a2
= this->args_
->at(1);
12329 Expression
* a3
= Expression::make_integer_ul(memorder
, int32_type
, loc
);
12330 return Runtime::make_call(code
, loc
, 3, a1
, a2
, a3
);
12333 else if (package
== "internal/abi")
12338 if ((name
== "FuncPCABI0" || name
== "FuncPCABIInternal")
12339 && this->args_
!= NULL
12340 && this->args_
->size() == 1)
12342 // We expect to see a conversion from the expression to "any".
12343 Expression
* expr
= this->args_
->front();
12344 Type_conversion_expression
* tce
= expr
->conversion_expression();
12346 expr
= tce
->expr();
12347 Func_expression
* fe
= expr
->func_expression();
12348 Interface_field_reference_expression
* interface_method
=
12349 expr
->interface_field_reference_expression();
12352 Named_object
* no
= fe
->named_object();
12353 Expression
* ref
= Expression::make_func_code_reference(no
, loc
);
12354 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
12355 return Expression::make_cast(uintptr_type
, ref
, loc
);
12357 else if (interface_method
!= NULL
)
12358 return interface_method
->get_function();
12361 expr
= this->args_
->front();
12362 go_assert(expr
->type()->interface_type() != NULL
12363 && expr
->type()->interface_type()->is_empty());
12364 expr
= Expression::make_interface_info(expr
,
12365 INTERFACE_INFO_OBJECT
,
12367 // Trust that this is a function type, which means that
12368 // it is a direct iface type and we can use EXPR
12369 // directly. The backend representation of this
12370 // function is a pointer to a struct whose first field
12371 // is the actual function to call.
12372 Type
* pvoid
= Type::make_pointer_type(Type::make_void_type());
12373 Type
* pfntype
= Type::make_pointer_type(pvoid
);
12374 Expression
* ref
= make_unsafe_cast(pfntype
, expr
, loc
);
12375 return Expression::make_dereference(ref
, NIL_CHECK_NOT_NEEDED
,
12384 // Make implicit type conversions explicit.
12387 Call_expression::do_add_conversions()
12389 // Skip call that requires a thunk. We generate conversions inside the thunk.
12390 if (this->is_concurrent_
|| this->is_deferred_
)
12393 if (this->args_
== NULL
|| this->args_
->empty())
12396 Function_type
* fntype
= this->get_function_type();
12397 if (fntype
== NULL
)
12399 go_assert(saw_errors());
12402 if (fntype
->parameters() == NULL
|| fntype
->parameters()->empty())
12405 Location loc
= this->location();
12406 Expression_list::iterator pa
= this->args_
->begin();
12407 Typed_identifier_list::const_iterator pp
= fntype
->parameters()->begin();
12408 bool is_interface_method
=
12409 this->fn_
->interface_field_reference_expression() != NULL
;
12410 size_t argcount
= this->args_
->size();
12411 if (!is_interface_method
&& fntype
->is_method())
12413 // Skip the receiver argument, which cannot be interface.
12417 if (argcount
!= fntype
->parameters()->size())
12419 go_assert(saw_errors());
12422 for (; pa
!= this->args_
->end(); ++pa
, ++pp
)
12424 Type
* pt
= pp
->type();
12425 if (!Type::are_identical(pt
, (*pa
)->type(), 0, NULL
)
12426 && pt
->interface_type() != NULL
)
12427 *pa
= Expression::make_cast(pt
, *pa
, loc
);
12431 // Get the function type. This can return NULL in error cases.
12434 Call_expression::get_function_type() const
12436 return this->fn_
->type()->function_type();
12439 // Return the number of values which this call will return.
12442 Call_expression::result_count() const
12444 const Function_type
* fntype
= this->get_function_type();
12445 if (fntype
== NULL
)
12447 if (fntype
->results() == NULL
)
12449 return fntype
->results()->size();
12452 // Return the temporary that holds the result for a call with multiple
12455 Temporary_statement
*
12456 Call_expression::results() const
12458 if (this->call_temp_
== NULL
)
12460 go_assert(saw_errors());
12463 return this->call_temp_
;
12466 // Set the number of results expected from a call expression.
12469 Call_expression::set_expected_result_count(size_t count
)
12471 go_assert(this->expected_result_count_
== 0);
12472 this->expected_result_count_
= count
;
12475 // Return whether this is a call to the predeclared function recover.
12478 Call_expression::is_recover_call() const
12480 return this->do_is_recover_call();
12483 // Set the argument to the recover function.
12486 Call_expression::set_recover_arg(Expression
* arg
)
12488 this->do_set_recover_arg(arg
);
12491 // Virtual functions also implemented by Builtin_call_expression.
12494 Call_expression::do_is_recover_call() const
12500 Call_expression::do_set_recover_arg(Expression
*)
12505 // We have found an error with this call expression; return true if
12506 // we should report it.
12509 Call_expression::issue_error()
12511 if (this->issued_error_
)
12515 this->issued_error_
= true;
12520 // Whether or not this call contains errors, either in the call or the
12521 // arguments to the call.
12524 Call_expression::is_erroneous_call()
12526 if (this->is_error_expression() || this->fn()->is_error_expression())
12529 if (this->args() == NULL
)
12531 for (Expression_list::iterator pa
= this->args()->begin();
12532 pa
!= this->args()->end();
12535 if ((*pa
)->type()->is_error_type() || (*pa
)->is_error_expression())
12544 Call_expression::do_type()
12546 if (this->is_error_expression())
12547 return Type::make_error_type();
12548 if (this->type_
!= NULL
)
12549 return this->type_
;
12552 Function_type
* fntype
= this->get_function_type();
12553 if (fntype
== NULL
)
12554 return Type::make_error_type();
12556 const Typed_identifier_list
* results
= fntype
->results();
12557 if (results
== NULL
)
12558 ret
= Type::make_void_type();
12559 else if (results
->size() == 1)
12560 ret
= results
->begin()->type();
12562 ret
= Type::make_call_multiple_result_type(this);
12566 return this->type_
;
12569 // Determine types for a call expression. We can use the function
12570 // parameter types to set the types of the arguments.
12573 Call_expression::do_determine_type(const Type_context
* context
)
12575 if (!this->determining_types())
12578 this->fn_
->determine_type_no_context();
12579 Function_type
* fntype
= this->get_function_type();
12580 const Typed_identifier_list
* parameters
= NULL
;
12581 if (fntype
!= NULL
)
12582 parameters
= fntype
->parameters();
12583 if (this->args_
!= NULL
)
12585 Typed_identifier_list::const_iterator pt
;
12586 if (parameters
!= NULL
)
12587 pt
= parameters
->begin();
12589 for (Expression_list::const_iterator pa
= this->args_
->begin();
12590 pa
!= this->args_
->end();
12596 // If this is a method, the first argument is the
12598 if (fntype
!= NULL
&& fntype
->is_method())
12600 Type
* rtype
= fntype
->receiver()->type();
12601 // The receiver is always passed as a pointer.
12602 if (rtype
->points_to() == NULL
)
12603 rtype
= Type::make_pointer_type(rtype
);
12604 Type_context
subcontext(rtype
, false);
12605 (*pa
)->determine_type(&subcontext
);
12610 if (parameters
!= NULL
&& pt
!= parameters
->end())
12612 Type_context
subcontext(pt
->type(), false);
12613 (*pa
)->determine_type(&subcontext
);
12617 (*pa
)->determine_type_no_context();
12621 // If this is a call to a generated equality function, we determine
12622 // the type based on the context. See the comment in
12623 // Binary_expression::lower_array_comparison.
12624 if (this->is_equal_function_
12625 && !context
->may_be_abstract
12626 && context
->type
!= NULL
12627 && context
->type
->is_boolean_type()
12628 && context
->type
!= Type::lookup_bool_type())
12630 go_assert(this->type_
== NULL
12631 || this->type_
== Type::lookup_bool_type()
12632 || this->type_
== context
->type
12633 || this->type_
->is_error());
12634 this->type_
= context
->type
;
12638 // Called when determining types for a Call_expression. Return true
12639 // if we should go ahead, false if they have already been determined.
12642 Call_expression::determining_types()
12644 if (this->types_are_determined_
)
12648 this->types_are_determined_
= true;
12653 // Check types for parameter I.
12656 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
12657 const Type
* argument_type
,
12658 Location argument_location
,
12661 std::string reason
;
12662 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
12666 if (reason
.empty())
12667 go_error_at(argument_location
, "argument %d has incompatible type", i
);
12669 go_error_at(argument_location
,
12670 "argument %d has incompatible type (%s)",
12671 i
, reason
.c_str());
12673 this->set_is_error();
12682 Call_expression::do_check_types(Gogo
*)
12684 if (this->classification() == EXPRESSION_ERROR
)
12687 Function_type
* fntype
= this->get_function_type();
12688 if (fntype
== NULL
)
12690 if (!this->fn_
->type()->is_error())
12691 this->report_error(_("expected function"));
12695 if (this->expected_result_count_
!= 0
12696 && this->expected_result_count_
!= this->result_count())
12698 if (this->issue_error())
12699 this->report_error(_("function result count mismatch"));
12700 this->set_is_error();
12704 bool is_method
= fntype
->is_method();
12707 go_assert(this->args_
!= NULL
&& !this->args_
->empty());
12708 Type
* rtype
= fntype
->receiver()->type();
12709 Expression
* first_arg
= this->args_
->front();
12710 // We dereference the values since receivers are always passed
12712 std::string reason
;
12713 if (!Type::are_assignable(rtype
->deref(), first_arg
->type()->deref(),
12716 if (reason
.empty())
12717 this->report_error(_("incompatible type for receiver"));
12720 go_error_at(this->location(),
12721 "incompatible type for receiver (%s)",
12723 this->set_is_error();
12728 // Note that varargs was handled by the lower_varargs() method, so
12729 // we don't have to worry about it here unless something is wrong.
12730 if (this->is_varargs_
&& !this->varargs_are_lowered_
)
12732 if (!fntype
->is_varargs())
12734 go_error_at(this->location(),
12735 _("invalid use of %<...%> calling non-variadic function"));
12736 this->set_is_error();
12741 const Typed_identifier_list
* parameters
= fntype
->parameters();
12742 if (this->args_
== NULL
|| this->args_
->size() == 0)
12744 if (parameters
!= NULL
&& !parameters
->empty())
12745 this->report_error(_("not enough arguments"));
12747 else if (parameters
== NULL
)
12749 if (!is_method
|| this->args_
->size() > 1)
12750 this->report_error(_("too many arguments"));
12752 else if (this->args_
->size() == 1
12753 && this->args_
->front()->call_expression() != NULL
12754 && this->args_
->front()->call_expression()->result_count() > 1)
12756 // This is F(G()) when G returns more than one result. If the
12757 // results can be matched to parameters, it would have been
12758 // lowered in do_lower. If we get here we know there is a
12760 if (this->args_
->front()->call_expression()->result_count()
12761 < parameters
->size())
12762 this->report_error(_("not enough arguments"));
12764 this->report_error(_("too many arguments"));
12769 Expression_list::const_iterator pa
= this->args_
->begin();
12772 for (Typed_identifier_list::const_iterator pt
= parameters
->begin();
12773 pt
!= parameters
->end();
12776 if (pa
== this->args_
->end())
12778 this->report_error(_("not enough arguments"));
12781 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
12782 (*pa
)->location(), false);
12784 if (pa
!= this->args_
->end())
12785 this->report_error(_("too many arguments"));
12790 Call_expression::do_copy()
12792 Call_expression
* call
=
12793 Expression::make_call(this->fn_
->copy(),
12794 (this->args_
== NULL
12796 : this->args_
->copy()),
12797 this->is_varargs_
, this->location());
12799 if (this->varargs_are_lowered_
)
12800 call
->set_varargs_are_lowered();
12801 if (this->is_deferred_
)
12802 call
->set_is_deferred();
12803 if (this->is_concurrent_
)
12804 call
->set_is_concurrent();
12808 // Return whether we have to use a temporary variable to ensure that
12809 // we evaluate this call expression in order. If the call returns no
12810 // results then it will inevitably be executed last.
12813 Call_expression::do_must_eval_in_order() const
12815 return this->result_count() > 0;
12818 // Get the function and the first argument to use when calling an
12819 // interface method.
12822 Call_expression::interface_method_function(
12823 Interface_field_reference_expression
* interface_method
,
12824 Expression
** first_arg_ptr
,
12827 Expression
* object
= interface_method
->get_underlying_object();
12828 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
12830 Expression::make_unsafe_cast(unsafe_ptr_type
, object
, location
);
12831 return interface_method
->get_function();
12834 // Build the call expression.
12837 Call_expression::do_get_backend(Translate_context
* context
)
12839 Location location
= this->location();
12841 if (this->call_
!= NULL
)
12843 // If the call returns multiple results, make a new reference to
12845 if (this->call_temp_
!= NULL
)
12848 Expression::make_temporary_reference(this->call_temp_
, location
);
12849 return ref
->get_backend(context
);
12852 return this->call_
;
12855 Function_type
* fntype
= this->get_function_type();
12856 if (fntype
== NULL
)
12857 return context
->backend()->error_expression();
12859 if (this->fn_
->is_error_expression())
12860 return context
->backend()->error_expression();
12862 Gogo
* gogo
= context
->gogo();
12864 Func_expression
* func
= this->fn_
->func_expression();
12865 Interface_field_reference_expression
* interface_method
=
12866 this->fn_
->interface_field_reference_expression();
12867 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
12868 const bool is_interface_method
= interface_method
!= NULL
;
12870 bool has_closure_arg
;
12872 has_closure_arg
= true;
12873 else if (func
!= NULL
)
12874 has_closure_arg
= false;
12875 else if (is_interface_method
)
12876 has_closure_arg
= false;
12878 has_closure_arg
= true;
12880 Expression
* first_arg
= NULL
;
12881 if (!is_interface_method
&& fntype
->is_method())
12883 first_arg
= this->args_
->front();
12884 if (first_arg
->type()->points_to() == NULL
12885 && first_arg
->type()->is_direct_iface_type())
12886 first_arg
= Expression::unpack_direct_iface(first_arg
,
12887 first_arg
->location());
12891 std::vector
<Bexpression
*> fn_args
;
12892 if (this->args_
== NULL
|| this->args_
->empty())
12894 nargs
= is_interface_method
? 1 : 0;
12898 else if (fntype
->parameters() == NULL
|| fntype
->parameters()->empty())
12900 // Passing a receiver parameter.
12901 go_assert(!is_interface_method
12902 && fntype
->is_method()
12903 && this->args_
->size() == 1);
12906 fn_args
[0] = first_arg
->get_backend(context
);
12910 const Typed_identifier_list
* params
= fntype
->parameters();
12912 nargs
= this->args_
->size();
12913 int i
= is_interface_method
? 1 : 0;
12915 fn_args
.resize(nargs
);
12917 Typed_identifier_list::const_iterator pp
= params
->begin();
12918 Expression_list::const_iterator pe
= this->args_
->begin();
12919 if (!is_interface_method
&& fntype
->is_method())
12921 fn_args
[i
] = first_arg
->get_backend(context
);
12925 for (; pe
!= this->args_
->end(); ++pe
, ++pp
, ++i
)
12927 go_assert(pp
!= params
->end());
12929 Expression::convert_for_assignment(gogo
, pp
->type(), *pe
,
12931 fn_args
[i
] = arg
->get_backend(context
);
12933 go_assert(pp
== params
->end());
12934 go_assert(i
== nargs
);
12938 Expression
* closure
= NULL
;
12941 Named_object
* no
= func
->named_object();
12942 fn
= Expression::make_func_code_reference(no
, location
);
12944 closure
= func
->closure();
12946 else if (!is_interface_method
)
12948 closure
= this->fn_
;
12950 // The backend representation of this function type is a pointer
12951 // to a struct whose first field is the actual function to call.
12953 Type::make_pointer_type(
12954 Type::make_pointer_type(Type::make_void_type()));
12955 fn
= Expression::make_unsafe_cast(pfntype
, this->fn_
, location
);
12956 fn
= Expression::make_dereference(fn
, NIL_CHECK_NOT_NEEDED
, location
);
12961 fn
= this->interface_method_function(interface_method
, &arg0
,
12963 fn_args
[0] = arg0
->get_backend(context
);
12966 Bexpression
* bclosure
= NULL
;
12967 if (has_closure_arg
)
12968 bclosure
= closure
->get_backend(context
);
12970 go_assert(closure
== NULL
);
12972 Bexpression
* bfn
= fn
->get_backend(context
);
12974 // When not calling a named function directly, use a type conversion
12975 // in case the type of the function is a recursive type which refers
12976 // to itself. We don't do this for an interface method because 1)
12977 // an interface method never refers to itself, so we always have a
12978 // function type here; 2) we pass an extra first argument to an
12979 // interface method, so fntype is not correct.
12980 if (func
== NULL
&& !is_interface_method
)
12982 Btype
* bft
= fntype
->get_backend_fntype(gogo
);
12983 bfn
= gogo
->backend()->convert_expression(bft
, bfn
, location
);
12986 Bfunction
* bfunction
= NULL
;
12987 if (context
->function())
12988 bfunction
= context
->function()->func_value()->get_decl();
12989 Bexpression
* call
= gogo
->backend()->call_expression(bfunction
, bfn
,
12993 if (this->call_temp_
!= NULL
)
12995 // This case occurs when the call returns multiple results.
12997 Expression
* ref
= Expression::make_temporary_reference(this->call_temp_
,
12999 Bexpression
* bref
= ref
->get_backend(context
);
13000 Bstatement
* bassn
= gogo
->backend()->assignment_statement(bfunction
,
13004 ref
= Expression::make_temporary_reference(this->call_temp_
, location
);
13005 this->call_
= ref
->get_backend(context
);
13007 return gogo
->backend()->compound_expression(bassn
, this->call_
,
13011 this->call_
= call
;
13012 return this->call_
;
13015 // The cost of inlining a call expression.
13018 Call_expression::do_inlining_cost() const
13020 Func_expression
* fn
= this->fn_
->func_expression();
13022 // FIXME: We don't yet support all kinds of calls.
13023 if (fn
!= NULL
&& fn
->closure() != NULL
)
13025 if (this->fn_
->interface_field_reference_expression())
13027 if (this->get_function_type()->is_method())
13033 // Export a call expression.
13036 Call_expression::do_export(Export_function_body
* efb
) const
13038 bool simple_call
= (this->fn_
->func_expression() != NULL
);
13040 efb
->write_c_string("(");
13041 this->fn_
->export_expression(efb
);
13043 efb
->write_c_string(")");
13044 this->export_arguments(efb
);
13047 // Export call expression arguments.
13050 Call_expression::export_arguments(Export_function_body
* efb
) const
13052 efb
->write_c_string("(");
13053 if (this->args_
!= NULL
&& !this->args_
->empty())
13055 Expression_list::const_iterator pa
= this->args_
->begin();
13056 (*pa
)->export_expression(efb
);
13057 for (pa
++; pa
!= this->args_
->end(); pa
++)
13059 efb
->write_c_string(", ");
13060 (*pa
)->export_expression(efb
);
13062 if (this->is_varargs_
)
13063 efb
->write_c_string("...");
13065 efb
->write_c_string(")");
13068 // Dump ast representation for a call expression.
13071 Call_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
13073 this->fn_
->dump_expression(ast_dump_context
);
13074 ast_dump_context
->ostream() << "(";
13076 ast_dump_context
->dump_expression_list(this->args_
);
13078 ast_dump_context
->ostream() << ") ";
13081 // Make a call expression.
13084 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
13087 return new Call_expression(fn
, args
, is_varargs
, location
);
13090 // Class Call_result_expression.
13092 // Traverse a call result.
13095 Call_result_expression::do_traverse(Traverse
* traverse
)
13097 if (traverse
->remember_expression(this->call_
))
13099 // We have already traversed the call expression.
13100 return TRAVERSE_CONTINUE
;
13102 return Expression::traverse(&this->call_
, traverse
);
13108 Call_result_expression::do_type()
13110 if (this->classification() == EXPRESSION_ERROR
)
13111 return Type::make_error_type();
13113 // THIS->CALL_ can be replaced with a temporary reference due to
13114 // Call_expression::do_must_eval_in_order when there is an error.
13115 Call_expression
* ce
= this->call_
->call_expression();
13118 this->set_is_error();
13119 return Type::make_error_type();
13121 Function_type
* fntype
= ce
->get_function_type();
13122 if (fntype
== NULL
)
13124 if (ce
->issue_error())
13126 if (!ce
->fn()->type()->is_error())
13127 this->report_error(_("expected function"));
13129 this->set_is_error();
13130 return Type::make_error_type();
13132 const Typed_identifier_list
* results
= fntype
->results();
13133 if (results
== NULL
|| results
->size() < 2)
13135 if (ce
->issue_error())
13136 this->report_error(_("number of results does not match "
13137 "number of values"));
13138 return Type::make_error_type();
13140 Typed_identifier_list::const_iterator pr
= results
->begin();
13141 for (unsigned int i
= 0; i
< this->index_
; ++i
)
13143 if (pr
== results
->end())
13147 if (pr
== results
->end())
13149 if (ce
->issue_error())
13150 this->report_error(_("number of results does not match "
13151 "number of values"));
13152 return Type::make_error_type();
13157 // Check the type. Just make sure that we trigger the warning in
13161 Call_result_expression::do_check_types(Gogo
*)
13166 // Determine the type. We have nothing to do here, but the 0 result
13167 // needs to pass down to the caller.
13170 Call_result_expression::do_determine_type(const Type_context
*)
13172 this->call_
->determine_type_no_context();
13175 // Return the backend representation. We just refer to the temporary set by the
13176 // call expression. We don't do this at lowering time because it makes it
13177 // hard to evaluate the call at the right time.
13180 Call_result_expression::do_get_backend(Translate_context
* context
)
13182 Call_expression
* ce
= this->call_
->call_expression();
13185 go_assert(this->call_
->is_error_expression());
13186 return context
->backend()->error_expression();
13188 Temporary_statement
* ts
= ce
->results();
13191 go_assert(saw_errors());
13192 return context
->backend()->error_expression();
13194 Expression
* ref
= Expression::make_temporary_reference(ts
, this->location());
13195 ref
= Expression::make_field_reference(ref
, this->index_
, this->location());
13196 return ref
->get_backend(context
);
13199 // Dump ast representation for a call result expression.
13202 Call_result_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
13205 // FIXME: Wouldn't it be better if the call is assigned to a temporary
13206 // (struct) and the fields are referenced instead.
13207 ast_dump_context
->ostream() << this->index_
<< "@(";
13208 ast_dump_context
->dump_expression(this->call_
);
13209 ast_dump_context
->ostream() << ")";
13212 // Make a reference to a single result of a call which returns
13213 // multiple results.
13216 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
13218 return new Call_result_expression(call
, index
);
13221 // Class Index_expression.
13226 Index_expression::do_traverse(Traverse
* traverse
)
13228 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
13229 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
13230 || (this->end_
!= NULL
13231 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
13232 || (this->cap_
!= NULL
13233 && Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
))
13234 return TRAVERSE_EXIT
;
13235 return TRAVERSE_CONTINUE
;
13238 // Lower an index expression. This converts the generic index
13239 // expression into an array index, a string index, or a map index.
13242 Index_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
13244 Location location
= this->location();
13245 Expression
* left
= this->left_
;
13246 Expression
* start
= this->start_
;
13247 Expression
* end
= this->end_
;
13248 Expression
* cap
= this->cap_
;
13250 Type
* type
= left
->type();
13251 if (type
->is_error())
13253 go_assert(saw_errors());
13254 return Expression::make_error(location
);
13256 else if (left
->is_type_expression())
13258 go_error_at(location
, "attempt to index type expression");
13259 return Expression::make_error(location
);
13261 else if (type
->array_type() != NULL
)
13262 return Expression::make_array_index(left
, start
, end
, cap
, location
);
13263 else if (type
->points_to() != NULL
13264 && type
->points_to()->array_type() != NULL
13265 && !type
->points_to()->is_slice_type())
13267 Expression
* deref
=
13268 Expression::make_dereference(left
, NIL_CHECK_DEFAULT
, location
);
13270 // For an ordinary index into the array, the pointer will be
13271 // dereferenced. For a slice it will not--the resulting slice
13272 // will simply reuse the pointer, which is incorrect if that
13274 if (end
!= NULL
|| cap
!= NULL
)
13275 deref
->issue_nil_check();
13277 return Expression::make_array_index(deref
, start
, end
, cap
, location
);
13279 else if (type
->is_string_type())
13283 go_error_at(location
, "invalid 3-index slice of string");
13284 return Expression::make_error(location
);
13286 return Expression::make_string_index(left
, start
, end
, location
);
13288 else if (type
->map_type() != NULL
)
13290 if (end
!= NULL
|| cap
!= NULL
)
13292 go_error_at(location
, "invalid slice of map");
13293 return Expression::make_error(location
);
13295 return Expression::make_map_index(left
, start
, location
);
13297 else if (cap
!= NULL
)
13299 go_error_at(location
,
13300 "invalid 3-index slice of object that is not a slice");
13301 return Expression::make_error(location
);
13303 else if (end
!= NULL
)
13305 go_error_at(location
,
13306 ("attempt to slice object that is not "
13307 "array, slice, or string"));
13308 return Expression::make_error(location
);
13312 go_error_at(location
,
13313 ("attempt to index object that is not "
13314 "array, slice, string, or map"));
13315 return Expression::make_error(location
);
13319 // Write an indexed expression
13320 // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context.
13323 Index_expression::dump_index_expression(Ast_dump_context
* ast_dump_context
,
13324 const Expression
* expr
,
13325 const Expression
* start
,
13326 const Expression
* end
,
13327 const Expression
* cap
)
13329 expr
->dump_expression(ast_dump_context
);
13330 ast_dump_context
->ostream() << "[";
13331 start
->dump_expression(ast_dump_context
);
13334 ast_dump_context
->ostream() << ":";
13335 end
->dump_expression(ast_dump_context
);
13339 ast_dump_context
->ostream() << ":";
13340 cap
->dump_expression(ast_dump_context
);
13342 ast_dump_context
->ostream() << "]";
13345 // Dump ast representation for an index expression.
13348 Index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
13351 Index_expression::dump_index_expression(ast_dump_context
, this->left_
,
13352 this->start_
, this->end_
, this->cap_
);
13355 // Make an index expression.
13358 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
13359 Expression
* cap
, Location location
)
13361 return new Index_expression(left
, start
, end
, cap
, location
);
13364 // Class Array_index_expression.
13366 // Array index traversal.
13369 Array_index_expression::do_traverse(Traverse
* traverse
)
13371 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
13372 return TRAVERSE_EXIT
;
13373 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
13374 return TRAVERSE_EXIT
;
13375 if (this->end_
!= NULL
)
13377 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
13378 return TRAVERSE_EXIT
;
13380 if (this->cap_
!= NULL
)
13382 if (Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
)
13383 return TRAVERSE_EXIT
;
13385 return TRAVERSE_CONTINUE
;
13388 // Return the type of an array index.
13391 Array_index_expression::do_type()
13393 if (this->type_
== NULL
)
13395 Array_type
* type
= this->array_
->type()->array_type();
13397 this->type_
= Type::make_error_type();
13398 else if (this->end_
== NULL
)
13399 this->type_
= type
->element_type();
13400 else if (type
->is_slice_type())
13402 // A slice of a slice has the same type as the original
13404 this->type_
= this->array_
->type()->deref();
13408 // A slice of an array is a slice.
13409 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
13412 return this->type_
;
13415 // Set the type of an array index.
13418 Array_index_expression::do_determine_type(const Type_context
*)
13420 this->array_
->determine_type_no_context();
13422 Type_context
index_context(Type::lookup_integer_type("int"), false);
13423 this->start_
->determine_type(&index_context
);
13424 if (this->end_
!= NULL
)
13425 this->end_
->determine_type(&index_context
);
13426 if (this->cap_
!= NULL
)
13427 this->cap_
->determine_type(&index_context
);
13430 // Check types of an array index.
13433 Array_index_expression::do_check_types(Gogo
*)
13435 Numeric_constant nc
;
13437 if (this->start_
->type()->integer_type() == NULL
13438 && !this->start_
->type()->is_error()
13439 && (!this->start_
->type()->is_abstract()
13440 || !this->start_
->numeric_constant_value(&nc
)
13441 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
13442 this->report_error(_("index must be integer"));
13443 if (this->end_
!= NULL
13444 && this->end_
->type()->integer_type() == NULL
13445 && !this->end_
->type()->is_error()
13446 && !this->end_
->is_nil_expression()
13447 && !this->end_
->is_error_expression()
13448 && (!this->end_
->type()->is_abstract()
13449 || !this->end_
->numeric_constant_value(&nc
)
13450 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
13451 this->report_error(_("slice end must be integer"));
13452 if (this->cap_
!= NULL
13453 && this->cap_
->type()->integer_type() == NULL
13454 && !this->cap_
->type()->is_error()
13455 && !this->cap_
->is_nil_expression()
13456 && !this->cap_
->is_error_expression()
13457 && (!this->cap_
->type()->is_abstract()
13458 || !this->cap_
->numeric_constant_value(&nc
)
13459 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
13460 this->report_error(_("slice capacity must be integer"));
13462 Array_type
* array_type
= this->array_
->type()->array_type();
13463 if (array_type
== NULL
)
13465 go_assert(this->array_
->type()->is_error());
13469 unsigned int int_bits
=
13470 Type::lookup_integer_type("int")->integer_type()->bits();
13472 Numeric_constant lvalnc
;
13474 bool lval_valid
= (array_type
->length() != NULL
13475 && array_type
->length()->numeric_constant_value(&lvalnc
)
13476 && lvalnc
.to_int(&lval
));
13477 Numeric_constant inc
;
13479 bool ival_valid
= false;
13480 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
13483 if (mpz_sgn(ival
) < 0
13484 || mpz_sizeinbase(ival
, 2) >= int_bits
13486 && (this->end_
== NULL
13487 ? mpz_cmp(ival
, lval
) >= 0
13488 : mpz_cmp(ival
, lval
) > 0)))
13490 go_error_at(this->start_
->location(), "array index out of bounds");
13491 this->set_is_error();
13494 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
13496 Numeric_constant enc
;
13498 bool eval_valid
= false;
13499 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
13502 if (mpz_sgn(eval
) < 0
13503 || mpz_sizeinbase(eval
, 2) >= int_bits
13504 || (lval_valid
&& mpz_cmp(eval
, lval
) > 0))
13506 go_error_at(this->end_
->location(), "array index out of bounds");
13507 this->set_is_error();
13509 else if (ival_valid
&& mpz_cmp(ival
, eval
) > 0)
13510 this->report_error(_("inverted slice range"));
13513 Numeric_constant cnc
;
13515 if (this->cap_
!= NULL
13516 && this->cap_
->numeric_constant_value(&cnc
) && cnc
.to_int(&cval
))
13518 if (mpz_sgn(cval
) < 0
13519 || mpz_sizeinbase(cval
, 2) >= int_bits
13520 || (lval_valid
&& mpz_cmp(cval
, lval
) > 0))
13522 go_error_at(this->cap_
->location(), "array index out of bounds");
13523 this->set_is_error();
13525 else if (ival_valid
&& mpz_cmp(ival
, cval
) > 0)
13527 go_error_at(this->cap_
->location(),
13528 "invalid slice index: capacity less than start");
13529 this->set_is_error();
13531 else if (eval_valid
&& mpz_cmp(eval
, cval
) > 0)
13533 go_error_at(this->cap_
->location(),
13534 "invalid slice index: capacity less than length");
13535 this->set_is_error();
13548 // A slice of an array requires an addressable array. A slice of a
13549 // slice is always possible.
13550 if (this->end_
!= NULL
&& !array_type
->is_slice_type())
13552 if (!this->array_
->is_addressable())
13553 this->report_error(_("slice of unaddressable value"));
13555 // Set the array address taken but not escape. The escape
13556 // analysis will make it escape to heap when needed.
13557 this->array_
->address_taken(false);
13561 // The subexpressions of an array index must be evaluated in order.
13562 // If this is indexing into an array, rather than a slice, then only
13563 // the index should be evaluated. Since this is called for values on
13564 // the left hand side of an assigment, evaluating the array, meaning
13565 // copying the array, will cause a different array to be modified.
13568 Array_index_expression::do_must_eval_subexpressions_in_order(
13571 *skip
= this->array_
->type()->is_slice_type() ? 0 : 1;
13575 // Flatten array indexing: add temporary variables and bounds checks.
13578 Array_index_expression::do_flatten(Gogo
* gogo
, Named_object
*,
13579 Statement_inserter
* inserter
)
13581 if (this->is_flattened_
)
13583 this->is_flattened_
= true;
13585 Location loc
= this->location();
13587 if (this->is_error_expression())
13588 return Expression::make_error(loc
);
13590 Expression
* array
= this->array_
;
13591 Expression
* start
= this->start_
;
13592 Expression
* end
= this->end_
;
13593 Expression
* cap
= this->cap_
;
13594 if (array
->is_error_expression()
13595 || array
->type()->is_error_type()
13596 || start
->is_error_expression()
13597 || start
->type()->is_error_type()
13599 && (end
->is_error_expression() || end
->type()->is_error_type()))
13601 && (cap
->is_error_expression() || cap
->type()->is_error_type())))
13603 go_assert(saw_errors());
13604 return Expression::make_error(loc
);
13607 Array_type
* array_type
= this->array_
->type()->array_type();
13608 if (array_type
== NULL
)
13610 go_assert(saw_errors());
13611 return Expression::make_error(loc
);
13614 Temporary_statement
* temp
;
13615 if (array_type
->is_slice_type() && !array
->is_multi_eval_safe())
13617 temp
= Statement::make_temporary(NULL
, array
, loc
);
13618 inserter
->insert(temp
);
13619 this->array_
= Expression::make_temporary_reference(temp
, loc
);
13620 array
= this->array_
;
13622 if (!start
->is_multi_eval_safe())
13624 temp
= Statement::make_temporary(NULL
, start
, loc
);
13625 inserter
->insert(temp
);
13626 this->start_
= Expression::make_temporary_reference(temp
, loc
);
13627 start
= this->start_
;
13630 && !end
->is_nil_expression()
13631 && !end
->is_multi_eval_safe())
13633 temp
= Statement::make_temporary(NULL
, end
, loc
);
13634 inserter
->insert(temp
);
13635 this->end_
= Expression::make_temporary_reference(temp
, loc
);
13638 if (cap
!= NULL
&& !cap
->is_multi_eval_safe())
13640 temp
= Statement::make_temporary(NULL
, cap
, loc
);
13641 inserter
->insert(temp
);
13642 this->cap_
= Expression::make_temporary_reference(temp
, loc
);
13646 if (!this->needs_bounds_check_
)
13650 if (!array_type
->is_slice_type())
13652 len
= array_type
->get_length(gogo
, this->array_
);
13653 go_assert(len
->is_constant());
13657 len
= array_type
->get_length(gogo
, this->array_
->copy());
13658 temp
= Statement::make_temporary(NULL
, len
, loc
);
13659 inserter
->insert(temp
);
13660 len
= Expression::make_temporary_reference(temp
, loc
);
13663 Expression
* scap
= NULL
;
13664 if (array_type
->is_slice_type())
13666 scap
= array_type
->get_capacity(gogo
, this->array_
->copy());
13667 temp
= Statement::make_temporary(NULL
, scap
, loc
);
13668 inserter
->insert(temp
);
13669 scap
= Expression::make_temporary_reference(temp
, loc
);
13672 // The order of bounds checks here matches the order used by the gc
13673 // compiler, as tested by issue30116[u].go.
13677 if (array_type
->is_slice_type())
13678 Expression::check_bounds(cap
, OPERATOR_LE
, scap
,
13679 Runtime::PANIC_SLICE3_ACAP
,
13680 Runtime::PANIC_SLICE3_ACAP_U
,
13681 Runtime::PANIC_EXTEND_SLICE3_ACAP
,
13682 Runtime::PANIC_EXTEND_SLICE3_ACAP_U
,
13685 Expression::check_bounds(cap
, OPERATOR_LE
, len
,
13686 Runtime::PANIC_SLICE3_ALEN
,
13687 Runtime::PANIC_SLICE3_ALEN_U
,
13688 Runtime::PANIC_EXTEND_SLICE3_ALEN
,
13689 Runtime::PANIC_EXTEND_SLICE3_ALEN_U
,
13692 Expression
* start_bound
= cap
;
13693 if (end
!= NULL
&& !end
->is_nil_expression())
13695 Expression::check_bounds(end
, OPERATOR_LE
, cap
,
13696 Runtime::PANIC_SLICE3_B
,
13697 Runtime::PANIC_SLICE3_B_U
,
13698 Runtime::PANIC_EXTEND_SLICE3_B
,
13699 Runtime::PANIC_EXTEND_SLICE3_B_U
,
13704 Expression::check_bounds(start
, OPERATOR_LE
, start_bound
,
13705 Runtime::PANIC_SLICE3_C
,
13706 Runtime::PANIC_SLICE3_C_U
,
13707 Runtime::PANIC_EXTEND_SLICE3_C
,
13708 Runtime::PANIC_EXTEND_SLICE3_C_U
,
13711 else if (end
!= NULL
&& !end
->is_nil_expression())
13713 if (array_type
->is_slice_type())
13714 Expression::check_bounds(end
, OPERATOR_LE
, scap
,
13715 Runtime::PANIC_SLICE_ACAP
,
13716 Runtime::PANIC_SLICE_ACAP_U
,
13717 Runtime::PANIC_EXTEND_SLICE_ACAP
,
13718 Runtime::PANIC_EXTEND_SLICE_ACAP_U
,
13721 Expression::check_bounds(end
, OPERATOR_LE
, len
,
13722 Runtime::PANIC_SLICE_ALEN
,
13723 Runtime::PANIC_SLICE_ALEN_U
,
13724 Runtime::PANIC_EXTEND_SLICE_ALEN
,
13725 Runtime::PANIC_EXTEND_SLICE_ALEN_U
,
13728 Expression::check_bounds(start
, OPERATOR_LE
, end
,
13729 Runtime::PANIC_SLICE_B
,
13730 Runtime::PANIC_SLICE_B_U
,
13731 Runtime::PANIC_EXTEND_SLICE_B
,
13732 Runtime::PANIC_EXTEND_SLICE_B_U
,
13735 else if (end
!= NULL
)
13737 Expression
* start_bound
;
13738 if (array_type
->is_slice_type())
13739 start_bound
= scap
;
13742 Expression::check_bounds(start
, OPERATOR_LE
, start_bound
,
13743 Runtime::PANIC_SLICE_B
,
13744 Runtime::PANIC_SLICE_B_U
,
13745 Runtime::PANIC_EXTEND_SLICE_B
,
13746 Runtime::PANIC_EXTEND_SLICE_B_U
,
13750 Expression::check_bounds(start
, OPERATOR_LT
, len
,
13751 Runtime::PANIC_INDEX
,
13752 Runtime::PANIC_INDEX_U
,
13753 Runtime::PANIC_EXTEND_INDEX
,
13754 Runtime::PANIC_EXTEND_INDEX_U
,
13760 // Return whether this expression is addressable.
13763 Array_index_expression::do_is_addressable() const
13765 // A slice expression is not addressable.
13766 if (this->end_
!= NULL
)
13769 // An index into a slice is addressable.
13770 if (this->array_
->type()->is_slice_type())
13773 // An index into an array is addressable if the array is
13775 return this->array_
->is_addressable();
13779 Array_index_expression::do_address_taken(bool escapes
)
13781 // In &x[0], if x is a slice, then x's address is not taken.
13782 if (!this->array_
->type()->is_slice_type())
13783 this->array_
->address_taken(escapes
);
13786 // Get the backend representation for an array index.
13789 Array_index_expression::do_get_backend(Translate_context
* context
)
13791 Array_type
* array_type
= this->array_
->type()->array_type();
13792 if (array_type
== NULL
)
13794 go_assert(this->array_
->type()->is_error());
13795 return context
->backend()->error_expression();
13797 go_assert(!array_type
->is_slice_type()
13798 || this->array_
->is_multi_eval_safe());
13800 Location loc
= this->location();
13801 Gogo
* gogo
= context
->gogo();
13803 Type
* int_type
= Type::lookup_integer_type("int");
13804 Btype
* int_btype
= int_type
->get_backend(gogo
);
13806 // Convert the length and capacity to "int". FIXME: Do we need to
13808 Bexpression
* length
= NULL
;
13809 if (this->end_
== NULL
|| this->end_
->is_nil_expression())
13811 Expression
* len
= array_type
->get_length(gogo
, this->array_
);
13812 length
= len
->get_backend(context
);
13813 length
= gogo
->backend()->convert_expression(int_btype
, length
, loc
);
13816 Bexpression
* capacity
= NULL
;
13817 if (this->end_
!= NULL
)
13819 Expression
* cap
= array_type
->get_capacity(gogo
, this->array_
);
13820 capacity
= cap
->get_backend(context
);
13821 capacity
= gogo
->backend()->convert_expression(int_btype
, capacity
, loc
);
13824 Bexpression
* cap_arg
= capacity
;
13825 if (this->cap_
!= NULL
)
13827 cap_arg
= this->cap_
->get_backend(context
);
13828 cap_arg
= gogo
->backend()->convert_expression(int_btype
, cap_arg
, loc
);
13831 if (length
== NULL
)
13834 if (this->start_
->type()->integer_type() == NULL
13835 && !Type::are_convertible(int_type
, this->start_
->type(), NULL
))
13837 go_assert(saw_errors());
13838 return context
->backend()->error_expression();
13841 Bexpression
* start
= this->start_
->get_backend(context
);
13842 start
= gogo
->backend()->convert_expression(int_btype
, start
, loc
);
13844 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
13845 if (this->end_
== NULL
)
13847 // Simple array indexing.
13849 if (!array_type
->is_slice_type())
13851 Bexpression
* array
= this->array_
->get_backend(context
);
13852 ret
= gogo
->backend()->array_index_expression(array
, start
, loc
);
13856 Expression
* valptr
= array_type
->get_value_pointer(gogo
,
13858 Bexpression
* ptr
= valptr
->get_backend(context
);
13859 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, start
, loc
);
13861 Type
* ele_type
= this->array_
->type()->array_type()->element_type();
13862 Btype
* ele_btype
= ele_type
->get_backend(gogo
);
13863 ret
= gogo
->backend()->indirect_expression(ele_btype
, ptr
, false,
13869 // Slice expression.
13872 if (this->end_
->is_nil_expression())
13876 end
= this->end_
->get_backend(context
);
13877 end
= gogo
->backend()->convert_expression(int_btype
, end
, loc
);
13880 Bexpression
* result_length
=
13881 gogo
->backend()->binary_expression(OPERATOR_MINUS
, end
, start
, loc
);
13883 Bexpression
* result_capacity
=
13884 gogo
->backend()->binary_expression(OPERATOR_MINUS
, cap_arg
, start
, loc
);
13886 // If the new capacity is zero, don't change val. Otherwise we can
13887 // get a pointer to the next object in memory, keeping it live
13888 // unnecessarily. When the capacity is zero, the actual pointer
13889 // value doesn't matter.
13890 Bexpression
* zero
=
13891 Expression::make_integer_ul(0, int_type
, loc
)->get_backend(context
);
13892 Bexpression
* cond
=
13893 gogo
->backend()->binary_expression(OPERATOR_EQEQ
, result_capacity
, zero
,
13895 Bexpression
* offset
= gogo
->backend()->conditional_expression(bfn
, int_btype
,
13898 Expression
* valptr
= array_type
->get_value_pointer(gogo
, this->array_
);
13899 Bexpression
* val
= valptr
->get_backend(context
);
13900 val
= gogo
->backend()->pointer_offset_expression(val
, offset
, loc
);
13902 Btype
* struct_btype
= this->type()->get_backend(gogo
);
13903 std::vector
<Bexpression
*> init
;
13904 init
.push_back(val
);
13905 init
.push_back(result_length
);
13906 init
.push_back(result_capacity
);
13908 return gogo
->backend()->constructor_expression(struct_btype
, init
, loc
);
13911 // Export an array index expression.
13914 Array_index_expression::do_export(Export_function_body
* efb
) const
13916 efb
->write_c_string("(");
13917 this->array_
->export_expression(efb
);
13918 efb
->write_c_string(")[");
13920 Type
* old_context
= efb
->type_context();
13921 efb
->set_type_context(Type::lookup_integer_type("int"));
13923 this->start_
->export_expression(efb
);
13924 if (this->end_
== NULL
)
13925 go_assert(this->cap_
== NULL
);
13928 efb
->write_c_string(":");
13929 if (!this->end_
->is_nil_expression())
13930 this->end_
->export_expression(efb
);
13931 if (this->cap_
!= NULL
)
13933 efb
->write_c_string(":");
13934 this->cap_
->export_expression(efb
);
13938 efb
->set_type_context(old_context
);
13940 efb
->write_c_string("]");
13943 // Dump ast representation for an array index expression.
13946 Array_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
13949 Index_expression::dump_index_expression(ast_dump_context
, this->array_
,
13950 this->start_
, this->end_
, this->cap_
);
13953 // Make an array index expression. END and CAP may be NULL.
13956 Expression::make_array_index(Expression
* array
, Expression
* start
,
13957 Expression
* end
, Expression
* cap
,
13960 return new Array_index_expression(array
, start
, end
, cap
, location
);
13963 // Class String_index_expression.
13965 // String index traversal.
13968 String_index_expression::do_traverse(Traverse
* traverse
)
13970 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
13971 return TRAVERSE_EXIT
;
13972 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
13973 return TRAVERSE_EXIT
;
13974 if (this->end_
!= NULL
)
13976 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
13977 return TRAVERSE_EXIT
;
13979 return TRAVERSE_CONTINUE
;
13983 String_index_expression::do_flatten(Gogo
*, Named_object
*,
13984 Statement_inserter
* inserter
)
13986 if (this->is_flattened_
)
13988 this->is_flattened_
= true;
13990 Location loc
= this->location();
13992 if (this->is_error_expression())
13993 return Expression::make_error(loc
);
13995 Expression
* string
= this->string_
;
13996 Expression
* start
= this->start_
;
13997 Expression
* end
= this->end_
;
13998 if (string
->is_error_expression()
13999 || string
->type()->is_error_type()
14000 || start
->is_error_expression()
14001 || start
->type()->is_error_type()
14003 && (end
->is_error_expression() || end
->type()->is_error_type())))
14005 go_assert(saw_errors());
14006 return Expression::make_error(loc
);
14009 Temporary_statement
* temp
;
14010 if (!string
->is_multi_eval_safe())
14012 temp
= Statement::make_temporary(NULL
, string
, loc
);
14013 inserter
->insert(temp
);
14014 this->string_
= Expression::make_temporary_reference(temp
, loc
);
14015 string
= this->string_
;
14017 if (!start
->is_multi_eval_safe())
14019 temp
= Statement::make_temporary(NULL
, start
, loc
);
14020 inserter
->insert(temp
);
14021 this->start_
= Expression::make_temporary_reference(temp
, loc
);
14022 start
= this->start_
;
14025 && !end
->is_nil_expression()
14026 && !end
->is_multi_eval_safe())
14028 temp
= Statement::make_temporary(NULL
, end
, loc
);
14029 inserter
->insert(temp
);
14030 this->end_
= Expression::make_temporary_reference(temp
, loc
);
14034 Expression
* len
= Expression::make_string_info(string
->copy(),
14035 STRING_INFO_LENGTH
, loc
);
14036 temp
= Statement::make_temporary(NULL
, len
, loc
);
14037 inserter
->insert(temp
);
14038 len
= Expression::make_temporary_reference(temp
, loc
);
14040 // The order of bounds checks here matches the order used by the gc
14041 // compiler, as tested by issue30116[u].go.
14043 if (end
!= NULL
&& !end
->is_nil_expression())
14045 Expression::check_bounds(end
, OPERATOR_LE
, len
,
14046 Runtime::PANIC_SLICE_ALEN
,
14047 Runtime::PANIC_SLICE_ALEN_U
,
14048 Runtime::PANIC_EXTEND_SLICE_ALEN
,
14049 Runtime::PANIC_EXTEND_SLICE_ALEN_U
,
14051 Expression::check_bounds(start
, OPERATOR_LE
, end
,
14052 Runtime::PANIC_SLICE_B
,
14053 Runtime::PANIC_SLICE_B_U
,
14054 Runtime::PANIC_EXTEND_SLICE_B
,
14055 Runtime::PANIC_EXTEND_SLICE_B_U
,
14058 else if (end
!= NULL
)
14059 Expression::check_bounds(start
, OPERATOR_LE
, len
,
14060 Runtime::PANIC_SLICE_B
,
14061 Runtime::PANIC_SLICE_B_U
,
14062 Runtime::PANIC_EXTEND_SLICE_B
,
14063 Runtime::PANIC_EXTEND_SLICE_B_U
,
14066 Expression::check_bounds(start
, OPERATOR_LT
, len
,
14067 Runtime::PANIC_INDEX
,
14068 Runtime::PANIC_INDEX_U
,
14069 Runtime::PANIC_EXTEND_INDEX
,
14070 Runtime::PANIC_EXTEND_INDEX_U
,
14076 // Return the type of a string index.
14079 String_index_expression::do_type()
14081 if (this->end_
== NULL
)
14082 return Type::lookup_integer_type("byte");
14084 return this->string_
->type();
14087 // Determine the type of a string index.
14090 String_index_expression::do_determine_type(const Type_context
*)
14092 this->string_
->determine_type_no_context();
14094 Type_context
index_context(Type::lookup_integer_type("int"), false);
14095 this->start_
->determine_type(&index_context
);
14096 if (this->end_
!= NULL
)
14097 this->end_
->determine_type(&index_context
);
14100 // Check types of a string index.
14103 String_index_expression::do_check_types(Gogo
*)
14105 Numeric_constant nc
;
14107 if (this->start_
->type()->integer_type() == NULL
14108 && !this->start_
->type()->is_error()
14109 && (!this->start_
->type()->is_abstract()
14110 || !this->start_
->numeric_constant_value(&nc
)
14111 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
14112 this->report_error(_("index must be integer"));
14113 if (this->end_
!= NULL
14114 && this->end_
->type()->integer_type() == NULL
14115 && !this->end_
->type()->is_error()
14116 && !this->end_
->is_nil_expression()
14117 && !this->end_
->is_error_expression()
14118 && (!this->end_
->type()->is_abstract()
14119 || !this->end_
->numeric_constant_value(&nc
)
14120 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
14121 this->report_error(_("slice end must be integer"));
14124 bool sval_valid
= this->string_
->string_constant_value(&sval
);
14126 Numeric_constant inc
;
14128 bool ival_valid
= false;
14129 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
14132 if (mpz_sgn(ival
) < 0
14134 && (this->end_
== NULL
14135 ? mpz_cmp_ui(ival
, sval
.length()) >= 0
14136 : mpz_cmp_ui(ival
, sval
.length()) > 0)))
14138 go_error_at(this->start_
->location(), "string index out of bounds");
14139 this->set_is_error();
14142 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
14144 Numeric_constant enc
;
14146 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
14148 if (mpz_sgn(eval
) < 0
14149 || (sval_valid
&& mpz_cmp_ui(eval
, sval
.length()) > 0))
14151 go_error_at(this->end_
->location(), "string index out of bounds");
14152 this->set_is_error();
14154 else if (ival_valid
&& mpz_cmp(ival
, eval
) > 0)
14155 this->report_error(_("inverted slice range"));
14163 // Get the backend representation for a string index.
14166 String_index_expression::do_get_backend(Translate_context
* context
)
14168 Location loc
= this->location();
14169 Gogo
* gogo
= context
->gogo();
14171 Type
* int_type
= Type::lookup_integer_type("int");
14173 // It is possible that an error occurred earlier because the start index
14174 // cannot be represented as an integer type. In this case, we shouldn't
14175 // try casting the starting index into an integer since
14176 // Type_conversion_expression will fail to get the backend representation.
14178 if (this->start_
->type()->integer_type() == NULL
14179 && !Type::are_convertible(int_type
, this->start_
->type(), NULL
))
14181 go_assert(saw_errors());
14182 return context
->backend()->error_expression();
14185 go_assert(this->string_
->is_multi_eval_safe());
14186 go_assert(this->start_
->is_multi_eval_safe());
14188 Expression
* start
= Expression::make_cast(int_type
, this->start_
, loc
);
14189 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
14191 Expression
* length
=
14192 Expression::make_string_info(this->string_
, STRING_INFO_LENGTH
, loc
);
14193 Expression
* bytes
=
14194 Expression::make_string_info(this->string_
, STRING_INFO_DATA
, loc
);
14196 Bexpression
* bstart
= start
->get_backend(context
);
14197 Bexpression
* ptr
= bytes
->get_backend(context
);
14199 if (this->end_
== NULL
)
14201 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, bstart
, loc
);
14202 Btype
* ubtype
= Type::lookup_integer_type("uint8")->get_backend(gogo
);
14203 return gogo
->backend()->indirect_expression(ubtype
, ptr
, false, loc
);
14206 Expression
* end
= NULL
;
14207 if (this->end_
->is_nil_expression())
14211 go_assert(this->end_
->is_multi_eval_safe());
14212 end
= Expression::make_cast(int_type
, this->end_
, loc
);
14216 Bexpression
* bend
= end
->get_backend(context
);
14217 Bexpression
* new_length
=
14218 gogo
->backend()->binary_expression(OPERATOR_MINUS
, bend
, bstart
, loc
);
14220 // If the new length is zero, don't change pointer. Otherwise we can
14221 // get a pointer to the next object in memory, keeping it live
14222 // unnecessarily. When the length is zero, the actual pointer
14223 // value doesn't matter.
14224 Btype
* int_btype
= int_type
->get_backend(gogo
);
14225 Bexpression
* zero
=
14226 Expression::make_integer_ul(0, int_type
, loc
)->get_backend(context
);
14227 Bexpression
* cond
=
14228 gogo
->backend()->binary_expression(OPERATOR_EQEQ
, new_length
, zero
,
14230 Bexpression
* offset
=
14231 gogo
->backend()->conditional_expression(bfn
, int_btype
, cond
, zero
,
14234 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, offset
, loc
);
14236 Btype
* str_btype
= this->type()->get_backend(gogo
);
14237 std::vector
<Bexpression
*> init
;
14238 init
.push_back(ptr
);
14239 init
.push_back(new_length
);
14240 return gogo
->backend()->constructor_expression(str_btype
, init
, loc
);
14243 // Export a string index expression.
14246 String_index_expression::do_export(Export_function_body
* efb
) const
14248 efb
->write_c_string("(");
14249 this->string_
->export_expression(efb
);
14250 efb
->write_c_string(")[");
14252 Type
* old_context
= efb
->type_context();
14253 efb
->set_type_context(Type::lookup_integer_type("int"));
14255 this->start_
->export_expression(efb
);
14256 if (this->end_
!= NULL
)
14258 efb
->write_c_string(":");
14259 if (!this->end_
->is_nil_expression())
14260 this->end_
->export_expression(efb
);
14263 efb
->set_type_context(old_context
);
14265 efb
->write_c_string("]");
14268 // Dump ast representation for a string index expression.
14271 String_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
14274 Index_expression::dump_index_expression(ast_dump_context
, this->string_
,
14275 this->start_
, this->end_
, NULL
);
14278 // Make a string index expression. END may be NULL.
14281 Expression::make_string_index(Expression
* string
, Expression
* start
,
14282 Expression
* end
, Location location
)
14284 return new String_index_expression(string
, start
, end
, location
);
14287 // Class Map_index.
14289 // Get the type of the map.
14292 Map_index_expression::get_map_type() const
14294 Map_type
* mt
= this->map_
->type()->map_type();
14296 go_assert(saw_errors());
14300 // Map index traversal.
14303 Map_index_expression::do_traverse(Traverse
* traverse
)
14305 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
14306 return TRAVERSE_EXIT
;
14307 return Expression::traverse(&this->index_
, traverse
);
14310 // We need to pass in a pointer to the key, so flatten the index into a
14311 // temporary variable if it isn't already. The value pointer will be
14312 // dereferenced and checked for nil, so flatten into a temporary to avoid
14316 Map_index_expression::do_flatten(Gogo
* gogo
, Named_object
*,
14317 Statement_inserter
* inserter
)
14319 Location loc
= this->location();
14320 Map_type
* mt
= this->get_map_type();
14321 if (this->index()->is_error_expression()
14322 || this->index()->type()->is_error_type()
14323 || mt
->is_error_type())
14325 go_assert(saw_errors());
14326 return Expression::make_error(loc
);
14329 // Avoid copy for string([]byte) conversions used in map keys.
14330 // mapaccess doesn't keep the reference, so this is safe.
14331 Type_conversion_expression
* ce
= this->index_
->conversion_expression();
14332 if (ce
!= NULL
&& ce
->type()->is_string_type()
14333 && ce
->expr()->type()->is_slice_type())
14334 ce
->set_no_copy(true);
14336 if (!Type::are_identical(mt
->key_type(), this->index_
->type(),
14337 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
14340 if (this->index_
->type()->interface_type() != NULL
14341 && !this->index_
->is_multi_eval_safe())
14343 Temporary_statement
* temp
=
14344 Statement::make_temporary(NULL
, this->index_
, loc
);
14345 inserter
->insert(temp
);
14346 this->index_
= Expression::make_temporary_reference(temp
, loc
);
14348 this->index_
= Expression::convert_for_assignment(gogo
, mt
->key_type(),
14349 this->index_
, loc
);
14352 if (!this->index_
->is_multi_eval_safe())
14354 Temporary_statement
* temp
= Statement::make_temporary(NULL
, this->index_
,
14356 inserter
->insert(temp
);
14357 this->index_
= Expression::make_temporary_reference(temp
, loc
);
14360 if (this->value_pointer_
== NULL
)
14361 this->get_value_pointer(gogo
);
14362 if (this->value_pointer_
->is_error_expression()
14363 || this->value_pointer_
->type()->is_error_type())
14364 return Expression::make_error(loc
);
14365 if (!this->value_pointer_
->is_multi_eval_safe())
14367 Temporary_statement
* temp
=
14368 Statement::make_temporary(NULL
, this->value_pointer_
, loc
);
14369 inserter
->insert(temp
);
14370 this->value_pointer_
= Expression::make_temporary_reference(temp
, loc
);
14376 // Return the type of a map index.
14379 Map_index_expression::do_type()
14381 Map_type
* mt
= this->get_map_type();
14383 return Type::make_error_type();
14384 return mt
->val_type();
14387 // Fix the type of a map index.
14390 Map_index_expression::do_determine_type(const Type_context
*)
14392 this->map_
->determine_type_no_context();
14393 Map_type
* mt
= this->get_map_type();
14394 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
14395 Type_context
subcontext(key_type
, false);
14396 this->index_
->determine_type(&subcontext
);
14399 // Check types of a map index.
14402 Map_index_expression::do_check_types(Gogo
*)
14404 std::string reason
;
14405 Map_type
* mt
= this->get_map_type();
14408 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
14410 if (reason
.empty())
14411 this->report_error(_("incompatible type for map index"));
14414 go_error_at(this->location(), "incompatible type for map index (%s)",
14416 this->set_is_error();
14421 // Add explicit type conversions.
14424 Map_index_expression::do_add_conversions()
14426 Map_type
* mt
= this->get_map_type();
14429 Type
* lt
= mt
->key_type();
14430 Type
* rt
= this->index_
->type();
14431 if (!Type::are_identical(lt
, rt
, 0, NULL
)
14432 && lt
->interface_type() != NULL
)
14433 this->index_
= Expression::make_cast(lt
, this->index_
, this->location());
14436 // Get the backend representation for a map index.
14439 Map_index_expression::do_get_backend(Translate_context
* context
)
14441 Map_type
* type
= this->get_map_type();
14444 go_assert(saw_errors());
14445 return context
->backend()->error_expression();
14448 go_assert(this->value_pointer_
!= NULL
14449 && this->value_pointer_
->is_multi_eval_safe());
14451 Expression
* val
= Expression::make_dereference(this->value_pointer_
,
14452 NIL_CHECK_NOT_NEEDED
,
14454 return val
->get_backend(context
);
14457 // Get an expression for the map index. This returns an expression
14458 // that evaluates to a pointer to a value. If the key is not in the
14459 // map, the pointer will point to a zero value.
14462 Map_index_expression::get_value_pointer(Gogo
* gogo
)
14464 if (this->value_pointer_
== NULL
)
14466 Map_type
* type
= this->get_map_type();
14469 go_assert(saw_errors());
14470 return Expression::make_error(this->location());
14473 Location loc
= this->location();
14474 Expression
* map_ref
= this->map_
;
14476 Expression
* index_ptr
= Expression::make_unary(OPERATOR_AND
,
14480 Expression
* type_expr
= Expression::make_type_descriptor(type
, loc
);
14481 Expression
* zero
= type
->fat_zero_value(gogo
);
14482 Expression
* map_index
;
14485 Runtime::Function code
;
14487 switch (type
->algorithm(gogo
))
14489 case Map_type::MAP_ALG_FAST32
:
14490 case Map_type::MAP_ALG_FAST32PTR
:
14492 Type
* uint32_type
= Type::lookup_integer_type("uint32");
14493 Type
* uint32_ptr_type
= Type::make_pointer_type(uint32_type
);
14494 key
= Expression::make_unsafe_cast(uint32_ptr_type
, index_ptr
,
14496 key
= Expression::make_dereference(key
, NIL_CHECK_NOT_NEEDED
,
14498 code
= Runtime::MAPACCESS1_FAST32
;
14501 case Map_type::MAP_ALG_FAST64
:
14502 case Map_type::MAP_ALG_FAST64PTR
:
14504 Type
* uint64_type
= Type::lookup_integer_type("uint64");
14505 Type
* uint64_ptr_type
= Type::make_pointer_type(uint64_type
);
14506 key
= Expression::make_unsafe_cast(uint64_ptr_type
, index_ptr
,
14508 key
= Expression::make_dereference(key
, NIL_CHECK_NOT_NEEDED
,
14510 code
= Runtime::MAPACCESS1_FAST64
;
14513 case Map_type::MAP_ALG_FASTSTR
:
14514 key
= this->index_
;
14515 code
= Runtime::MAPACCESS1_FASTSTR
;
14519 code
= Runtime::MAPACCESS1
;
14522 map_index
= Runtime::make_call(code
, loc
, 3,
14523 type_expr
, map_ref
, key
);
14526 map_index
= Runtime::make_call(Runtime::MAPACCESS1_FAT
, loc
, 4,
14527 type_expr
, map_ref
, index_ptr
, zero
);
14529 Type
* val_type
= type
->val_type();
14530 this->value_pointer_
=
14531 Expression::make_unsafe_cast(Type::make_pointer_type(val_type
),
14532 map_index
, this->location());
14535 return this->value_pointer_
;
14538 // Export a map index expression.
14541 Map_index_expression::do_export(Export_function_body
* efb
) const
14543 efb
->write_c_string("(");
14544 this->map_
->export_expression(efb
);
14545 efb
->write_c_string(")[");
14547 Type
* old_context
= efb
->type_context();
14548 efb
->set_type_context(this->get_map_type()->key_type());
14550 this->index_
->export_expression(efb
);
14552 efb
->set_type_context(old_context
);
14554 efb
->write_c_string("]");
14557 // Dump ast representation for a map index expression
14560 Map_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
14563 Index_expression::dump_index_expression(ast_dump_context
, this->map_
,
14564 this->index_
, NULL
, NULL
);
14567 // Make a map index expression.
14569 Map_index_expression
*
14570 Expression::make_map_index(Expression
* map
, Expression
* index
,
14573 return new Map_index_expression(map
, index
, location
);
14576 // Class Field_reference_expression.
14578 // Lower a field reference expression. There is nothing to lower, but
14579 // this is where we generate the tracking information for fields with
14580 // the magic go:"track" tag.
14583 Field_reference_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
14584 Statement_inserter
* inserter
, int)
14586 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
14587 if (struct_type
== NULL
)
14589 // Error will be reported elsewhere.
14592 const Struct_field
* field
= struct_type
->field(this->field_index_
);
14595 if (!field
->has_tag())
14597 if (field
->tag().find("go:\"track\"") == std::string::npos
)
14600 // References from functions generated by the compiler don't count.
14601 if (function
!= NULL
&& function
->func_value()->is_type_specific_function())
14604 // We have found a reference to a tracked field. Build a call to
14605 // the runtime function __go_fieldtrack with a string that describes
14606 // the field. FIXME: We should only call this once per referenced
14607 // field per function, not once for each reference to the field.
14609 if (this->called_fieldtrack_
)
14611 this->called_fieldtrack_
= true;
14613 Location loc
= this->location();
14615 std::string s
= "fieldtrack \"";
14616 Named_type
* nt
= this->expr_
->type()->unalias()->named_type();
14617 if (nt
== NULL
|| nt
->named_object()->package() == NULL
)
14618 s
.append(gogo
->pkgpath());
14620 s
.append(nt
->named_object()->package()->pkgpath());
14623 s
.append(Gogo::unpack_hidden_name(nt
->name()));
14625 s
.append(Gogo::unpack_hidden_name(field
->field_name()));
14628 // We can't use a string here, because internally a string holds a
14629 // pointer to the actual bytes; when the linker garbage collects the
14630 // string, it won't garbage collect the bytes. So we use a
14633 Expression
* length_expr
= Expression::make_integer_ul(s
.length(), NULL
, loc
);
14635 Type
* byte_type
= Type::lookup_integer_type("byte");
14636 Array_type
* array_type
= Type::make_array_type(byte_type
, length_expr
);
14637 array_type
->set_is_array_incomparable();
14639 Expression_list
* bytes
= new Expression_list();
14640 for (std::string::const_iterator p
= s
.begin(); p
!= s
.end(); p
++)
14642 unsigned char c
= static_cast<unsigned char>(*p
);
14643 bytes
->push_back(Expression::make_integer_ul(c
, NULL
, loc
));
14646 Expression
* e
= Expression::make_composite_literal(array_type
, 0, false,
14647 bytes
, false, loc
);
14649 Variable
* var
= new Variable(array_type
, e
, true, false, false, loc
);
14653 snprintf(buf
, sizeof buf
, "fieldtrack.%d", count
);
14656 Named_object
* no
= gogo
->add_variable(buf
, var
);
14657 e
= Expression::make_var_reference(no
, loc
);
14658 e
= Expression::make_unary(OPERATOR_AND
, e
, loc
);
14660 Expression
* call
= Runtime::make_call(Runtime::FIELDTRACK
, loc
, 1, e
);
14661 gogo
->lower_expression(function
, inserter
, &call
);
14662 inserter
->insert(Statement::make_statement(call
, false));
14664 // Put this function, and the global variable we just created, into
14665 // unique sections. This will permit the linker to garbage collect
14666 // them if they are not referenced. The effect is that the only
14667 // strings, indicating field references, that will wind up in the
14668 // executable will be those for functions that are actually needed.
14669 if (function
!= NULL
)
14670 function
->func_value()->set_in_unique_section();
14671 var
->set_in_unique_section();
14676 // Return the type of a field reference.
14679 Field_reference_expression::do_type()
14681 Type
* type
= this->expr_
->type();
14682 if (type
->is_error())
14684 Struct_type
* struct_type
= type
->struct_type();
14685 go_assert(struct_type
!= NULL
);
14686 return struct_type
->field(this->field_index_
)->type();
14689 // Check the types for a field reference.
14692 Field_reference_expression::do_check_types(Gogo
*)
14694 Type
* type
= this->expr_
->type();
14695 if (type
->is_error())
14697 Struct_type
* struct_type
= type
->struct_type();
14698 go_assert(struct_type
!= NULL
);
14699 go_assert(struct_type
->field(this->field_index_
) != NULL
);
14702 // Get the backend representation for a field reference.
14705 Field_reference_expression::do_get_backend(Translate_context
* context
)
14707 Bexpression
* bstruct
= this->expr_
->get_backend(context
);
14708 return context
->gogo()->backend()->struct_field_expression(bstruct
,
14709 this->field_index_
,
14713 // Dump ast representation for a field reference expression.
14716 Field_reference_expression::do_dump_expression(
14717 Ast_dump_context
* ast_dump_context
) const
14719 this->expr_
->dump_expression(ast_dump_context
);
14720 ast_dump_context
->ostream() << "." << this->field_index_
;
14723 // Make a reference to a qualified identifier in an expression.
14725 Field_reference_expression
*
14726 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
14729 return new Field_reference_expression(expr
, field_index
, location
);
14732 // Class Interface_field_reference_expression.
14734 // Return an expression for the pointer to the function to call.
14737 Interface_field_reference_expression::get_function()
14739 Expression
* ref
= this->expr_
;
14740 Location loc
= this->location();
14741 if (ref
->type()->points_to() != NULL
)
14742 ref
= Expression::make_dereference(ref
, NIL_CHECK_DEFAULT
, loc
);
14744 Expression
* mtable
=
14745 Expression::make_interface_info(ref
, INTERFACE_INFO_METHODS
, loc
);
14746 Struct_type
* mtable_type
= mtable
->type()->points_to()->struct_type();
14748 std::string name
= Gogo::unpack_hidden_name(this->name_
);
14749 unsigned int index
;
14750 const Struct_field
* field
= mtable_type
->find_local_field(name
, &index
);
14751 go_assert(field
!= NULL
);
14753 mtable
= Expression::make_dereference(mtable
, NIL_CHECK_NOT_NEEDED
, loc
);
14754 return Expression::make_field_reference(mtable
, index
, loc
);
14757 // Return an expression for the first argument to pass to the interface
14761 Interface_field_reference_expression::get_underlying_object()
14763 Expression
* expr
= this->expr_
;
14764 if (expr
->type()->points_to() != NULL
)
14765 expr
= Expression::make_dereference(expr
, NIL_CHECK_DEFAULT
,
14767 return Expression::make_interface_info(expr
, INTERFACE_INFO_OBJECT
,
14774 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
14776 return Expression::traverse(&this->expr_
, traverse
);
14779 // Lower the expression. If this expression is not called, we need to
14780 // evaluate the expression twice when converting to the backend
14781 // interface. So introduce a temporary variable if necessary.
14784 Interface_field_reference_expression::do_flatten(Gogo
*, Named_object
*,
14785 Statement_inserter
* inserter
)
14787 if (this->expr_
->is_error_expression()
14788 || this->expr_
->type()->is_error_type())
14790 go_assert(saw_errors());
14791 return Expression::make_error(this->location());
14794 if (!this->expr_
->is_multi_eval_safe())
14796 Temporary_statement
* temp
=
14797 Statement::make_temporary(NULL
, this->expr_
, this->location());
14798 inserter
->insert(temp
);
14799 this->expr_
= Expression::make_temporary_reference(temp
, this->location());
14804 // Return the type of an interface field reference.
14807 Interface_field_reference_expression::do_type()
14809 Type
* expr_type
= this->expr_
->type();
14811 Type
* points_to
= expr_type
->points_to();
14812 if (points_to
!= NULL
)
14813 expr_type
= points_to
;
14815 Interface_type
* interface_type
= expr_type
->interface_type();
14816 if (interface_type
== NULL
)
14817 return Type::make_error_type();
14819 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
14820 if (method
== NULL
)
14821 return Type::make_error_type();
14823 return method
->type();
14826 // Determine types.
14829 Interface_field_reference_expression::do_determine_type(const Type_context
*)
14831 this->expr_
->determine_type_no_context();
14834 // Check the types for an interface field reference.
14837 Interface_field_reference_expression::do_check_types(Gogo
*)
14839 Type
* type
= this->expr_
->type();
14841 Type
* points_to
= type
->points_to();
14842 if (points_to
!= NULL
)
14845 Interface_type
* interface_type
= type
->interface_type();
14846 if (interface_type
== NULL
)
14848 if (!type
->is_error_type())
14849 this->report_error(_("expected interface or pointer to interface"));
14853 const Typed_identifier
* method
=
14854 interface_type
->find_method(this->name_
);
14855 if (method
== NULL
)
14857 go_error_at(this->location(), "method %qs not in interface",
14858 Gogo::message_name(this->name_
).c_str());
14859 this->set_is_error();
14864 // If an interface field reference is not simply called, then it is
14865 // represented as a closure. The closure will hold a single variable,
14866 // the value of the interface on which the method should be called.
14867 // The function will be a simple thunk that pulls the value from the
14868 // closure and calls the method with the remaining arguments.
14870 // Because method values are not common, we don't build all thunks for
14871 // all possible interface methods, but instead only build them as we
14872 // need them. In particular, we even build them on demand for
14873 // interface methods defined in other packages.
14875 Interface_field_reference_expression::Interface_method_thunks
14876 Interface_field_reference_expression::interface_method_thunks
;
14878 // Find or create the thunk to call method NAME on TYPE.
14881 Interface_field_reference_expression::create_thunk(Gogo
* gogo
,
14882 Interface_type
* type
,
14883 const std::string
& name
)
14885 std::pair
<Interface_type
*, Method_thunks
*> val(type
, NULL
);
14886 std::pair
<Interface_method_thunks::iterator
, bool> ins
=
14887 Interface_field_reference_expression::interface_method_thunks
.insert(val
);
14890 // This is the first time we have seen this interface.
14891 ins
.first
->second
= new Method_thunks();
14894 for (Method_thunks::const_iterator p
= ins
.first
->second
->begin();
14895 p
!= ins
.first
->second
->end();
14897 if (p
->first
== name
)
14900 Location loc
= type
->location();
14902 const Typed_identifier
* method_id
= type
->find_method(name
);
14903 if (method_id
== NULL
)
14904 return Named_object::make_erroneous_name(gogo
->thunk_name());
14906 Function_type
* orig_fntype
= method_id
->type()->function_type();
14907 if (orig_fntype
== NULL
)
14908 return Named_object::make_erroneous_name(gogo
->thunk_name());
14910 Struct_field_list
* sfl
= new Struct_field_list();
14911 // The type here is wrong--it should be the C function type. But it
14912 // doesn't really matter.
14913 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
14914 sfl
->push_back(Struct_field(Typed_identifier("fn", vt
, loc
)));
14915 sfl
->push_back(Struct_field(Typed_identifier("val", type
, loc
)));
14916 Struct_type
* st
= Type::make_struct_type(sfl
, loc
);
14917 st
->set_is_struct_incomparable();
14918 Type
* closure_type
= Type::make_pointer_type(st
);
14920 Function_type
* new_fntype
= orig_fntype
->copy_with_names();
14922 std::string thunk_name
= gogo
->thunk_name();
14923 Named_object
* new_no
= gogo
->start_function(thunk_name
, new_fntype
,
14926 Variable
* cvar
= new Variable(closure_type
, NULL
, false, false, false, loc
);
14927 cvar
->set_is_used();
14928 cvar
->set_is_closure();
14929 Named_object
* cp
= Named_object::make_variable("$closure" + thunk_name
,
14931 new_no
->func_value()->set_closure_var(cp
);
14933 gogo
->start_block(loc
);
14935 // Field 0 of the closure is the function code pointer, field 1 is
14936 // the value on which to invoke the method.
14937 Expression
* arg
= Expression::make_var_reference(cp
, loc
);
14938 arg
= Expression::make_dereference(arg
, NIL_CHECK_NOT_NEEDED
, loc
);
14939 arg
= Expression::make_field_reference(arg
, 1, loc
);
14941 Expression
*ifre
= Expression::make_interface_field_reference(arg
, name
,
14944 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
14945 Expression_list
* args
;
14946 if (orig_params
== NULL
|| orig_params
->empty())
14950 const Typed_identifier_list
* new_params
= new_fntype
->parameters();
14951 args
= new Expression_list();
14952 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
14953 p
!= new_params
->end();
14956 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
14957 go_assert(p_no
!= NULL
14958 && p_no
->is_variable()
14959 && p_no
->var_value()->is_parameter());
14960 args
->push_back(Expression::make_var_reference(p_no
, loc
));
14964 Call_expression
* call
= Expression::make_call(ifre
, args
,
14965 orig_fntype
->is_varargs(),
14967 call
->set_varargs_are_lowered();
14969 Statement
* s
= Statement::make_return_from_call(call
, loc
);
14970 gogo
->add_statement(s
);
14971 Block
* b
= gogo
->finish_block(loc
);
14972 gogo
->add_block(b
, loc
);
14974 // This is called after lowering but before determine_types.
14975 gogo
->lower_block(new_no
, b
);
14977 gogo
->finish_function(loc
);
14979 ins
.first
->second
->push_back(std::make_pair(name
, new_no
));
14983 // Lookup a thunk to call method NAME on TYPE.
14986 Interface_field_reference_expression::lookup_thunk(Interface_type
* type
,
14987 const std::string
& name
)
14989 Interface_method_thunks::const_iterator p
=
14990 Interface_field_reference_expression::interface_method_thunks
.find(type
);
14991 if (p
== Interface_field_reference_expression::interface_method_thunks
.end())
14993 for (Method_thunks::const_iterator pm
= p
->second
->begin();
14994 pm
!= p
->second
->end();
14996 if (pm
->first
== name
)
15001 // Get the backend representation for a method value.
15004 Interface_field_reference_expression::do_get_backend(Translate_context
* context
)
15006 Interface_type
* type
= this->expr_
->type()->interface_type();
15009 go_assert(saw_errors());
15010 return context
->backend()->error_expression();
15013 Named_object
* thunk
=
15014 Interface_field_reference_expression::lookup_thunk(type
, this->name_
);
15016 // The thunk should have been created during the
15017 // create_function_descriptors pass.
15018 if (thunk
== NULL
|| thunk
->is_erroneous())
15020 go_assert(saw_errors());
15021 return context
->backend()->error_expression();
15024 // FIXME: We should lower this earlier, but we can't it lower it in
15025 // the lowering pass because at that point we don't know whether we
15026 // need to create the thunk or not. If the expression is called, we
15027 // don't need the thunk.
15029 Location loc
= this->location();
15031 Struct_field_list
* fields
= new Struct_field_list();
15032 fields
->push_back(Struct_field(Typed_identifier("fn",
15033 thunk
->func_value()->type(),
15035 fields
->push_back(Struct_field(Typed_identifier("val",
15036 this->expr_
->type(),
15038 Struct_type
* st
= Type::make_struct_type(fields
, loc
);
15039 st
->set_is_struct_incomparable();
15041 Expression_list
* vals
= new Expression_list();
15042 vals
->push_back(Expression::make_func_code_reference(thunk
, loc
));
15043 vals
->push_back(this->expr_
);
15045 Expression
* expr
= Expression::make_struct_composite_literal(st
, vals
, loc
);
15046 Bexpression
* bclosure
=
15047 Expression::make_heap_expression(expr
, loc
)->get_backend(context
);
15049 Gogo
* gogo
= context
->gogo();
15050 Btype
* btype
= this->type()->get_backend(gogo
);
15051 bclosure
= gogo
->backend()->convert_expression(btype
, bclosure
, loc
);
15053 Expression
* nil_check
=
15054 Expression::make_binary(OPERATOR_EQEQ
, this->expr_
,
15055 Expression::make_nil(loc
), loc
);
15056 Bexpression
* bnil_check
= nil_check
->get_backend(context
);
15058 Expression
* crash
= Runtime::make_call(Runtime::PANIC_MEM
, loc
, 0);
15059 Bexpression
* bcrash
= crash
->get_backend(context
);
15061 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
15062 Bexpression
* bcond
=
15063 gogo
->backend()->conditional_expression(bfn
, NULL
,
15064 bnil_check
, bcrash
, NULL
, loc
);
15065 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
15066 Bstatement
* cond_statement
=
15067 gogo
->backend()->expression_statement(bfunction
, bcond
);
15068 return gogo
->backend()->compound_expression(cond_statement
, bclosure
, loc
);
15071 // Dump ast representation for an interface field reference.
15074 Interface_field_reference_expression::do_dump_expression(
15075 Ast_dump_context
* ast_dump_context
) const
15077 this->expr_
->dump_expression(ast_dump_context
);
15078 ast_dump_context
->ostream() << "." << this->name_
;
15081 // Make a reference to a field in an interface.
15084 Expression::make_interface_field_reference(Expression
* expr
,
15085 const std::string
& field
,
15088 return new Interface_field_reference_expression(expr
, field
, location
);
15091 // A general selector. This is a Parser_expression for LEFT.NAME. It
15092 // is lowered after we know the type of the left hand side.
15094 class Selector_expression
: public Parser_expression
15097 Selector_expression(Expression
* left
, const std::string
& name
,
15099 : Parser_expression(EXPRESSION_SELECTOR
, location
),
15100 left_(left
), name_(name
)
15105 do_traverse(Traverse
* traverse
)
15106 { return Expression::traverse(&this->left_
, traverse
); }
15109 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
15114 return new Selector_expression(this->left_
->copy(), this->name_
,
15119 do_dump_expression(Ast_dump_context
* ast_dump_context
) const;
15123 lower_method_expression(Gogo
*);
15125 // The expression on the left hand side.
15127 // The name on the right hand side.
15131 // Lower a selector expression once we know the real type of the left
15135 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, Statement_inserter
*,
15138 Expression
* left
= this->left_
;
15139 if (left
->is_type_expression())
15140 return this->lower_method_expression(gogo
);
15141 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
15145 // Lower a method expression T.M or (*T).M. We turn this into a
15146 // function literal.
15149 Selector_expression::lower_method_expression(Gogo
* gogo
)
15151 Location location
= this->location();
15152 Type
* left_type
= this->left_
->type();
15153 Type
* type
= left_type
;
15154 const std::string
& name(this->name_
);
15157 if (type
->points_to() == NULL
)
15158 is_pointer
= false;
15162 type
= type
->points_to();
15165 Named_type
* nt
= type
->named_type();
15166 Struct_type
* st
= type
->struct_type();
15168 Method
* method
= NULL
;
15170 method
= nt
->method_function(name
, &is_ambiguous
);
15171 else if (st
!= NULL
)
15172 method
= st
->method_function(name
, &is_ambiguous
);
15173 const Typed_identifier
* imethod
= NULL
;
15174 if (method
== NULL
&& !is_pointer
)
15176 Interface_type
* it
= type
->interface_type();
15178 imethod
= it
->find_method(name
);
15181 if ((method
== NULL
&& imethod
== NULL
)
15182 || (left_type
->named_type() != NULL
&& left_type
->points_to() != NULL
))
15187 go_error_at(location
, "type %<%s%s%> has no method %<%s%>",
15188 is_pointer
? "*" : "",
15189 nt
->message_name().c_str(),
15190 Gogo::message_name(name
).c_str());
15192 go_error_at(location
, "method %<%s%s%> is ambiguous in type %<%s%>",
15193 Gogo::message_name(name
).c_str(),
15194 is_pointer
? "*" : "",
15195 nt
->message_name().c_str());
15200 go_error_at(location
, "type has no method %<%s%>",
15201 Gogo::message_name(name
).c_str());
15203 go_error_at(location
, "method %<%s%> is ambiguous",
15204 Gogo::message_name(name
).c_str());
15206 return Expression::make_error(location
);
15209 if (method
!= NULL
&& !is_pointer
&& !method
->is_value_method())
15211 go_error_at(location
, "method requires pointer (use %<(*%s).%s%>)",
15212 nt
->message_name().c_str(),
15213 Gogo::message_name(name
).c_str());
15214 return Expression::make_error(location
);
15217 // Build a new function type in which the receiver becomes the first
15219 Function_type
* method_type
;
15220 if (method
!= NULL
)
15222 method_type
= method
->type();
15223 go_assert(method_type
->is_method());
15227 method_type
= imethod
->type()->function_type();
15228 go_assert(method_type
!= NULL
&& !method_type
->is_method());
15231 const char* const receiver_name
= "$this";
15232 Typed_identifier_list
* parameters
= new Typed_identifier_list();
15233 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
15236 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
15237 if (method_parameters
!= NULL
)
15240 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
15241 p
!= method_parameters
->end();
15244 if (!p
->name().empty() && !Gogo::is_sink_name(p
->name()))
15245 parameters
->push_back(*p
);
15249 snprintf(buf
, sizeof buf
, "$param%d", i
);
15250 parameters
->push_back(Typed_identifier(buf
, p
->type(),
15256 const Typed_identifier_list
* method_results
= method_type
->results();
15257 Typed_identifier_list
* results
;
15258 if (method_results
== NULL
)
15262 results
= new Typed_identifier_list();
15263 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
15264 p
!= method_results
->end();
15266 results
->push_back(*p
);
15269 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
15271 if (method_type
->is_varargs())
15272 fntype
->set_is_varargs();
15274 // We generate methods which always takes a pointer to the receiver
15275 // as their first argument. If this is for a pointer type, we can
15276 // simply reuse the existing function. We use an internal hack to
15277 // get the right type.
15278 // FIXME: This optimization is disabled because it doesn't yet work
15279 // with function descriptors when the method expression is not
15280 // directly called.
15281 if (method
!= NULL
&& is_pointer
&& false)
15283 Named_object
* mno
= (method
->needs_stub_method()
15284 ? method
->stub_object()
15285 : method
->named_object());
15286 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
15287 f
= Expression::make_cast(fntype
, f
, location
);
15288 Type_conversion_expression
* tce
=
15289 static_cast<Type_conversion_expression
*>(f
);
15290 tce
->set_may_convert_function_types();
15294 Named_object
* no
= gogo
->start_function(gogo
->thunk_name(), fntype
, false,
15297 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
15298 go_assert(vno
!= NULL
);
15299 Expression
* ve
= Expression::make_var_reference(vno
, location
);
15301 if (method
!= NULL
)
15302 bm
= Type::bind_field_or_method(gogo
, type
, ve
, name
, location
);
15304 bm
= Expression::make_interface_field_reference(ve
, name
, location
);
15306 // Even though we found the method above, if it has an error type we
15307 // may see an error here.
15308 if (bm
->is_error_expression())
15310 gogo
->finish_function(location
);
15314 Expression_list
* args
;
15315 if (parameters
->size() <= 1)
15319 args
= new Expression_list();
15320 Typed_identifier_list::const_iterator p
= parameters
->begin();
15322 for (; p
!= parameters
->end(); ++p
)
15324 vno
= gogo
->lookup(p
->name(), NULL
);
15325 go_assert(vno
!= NULL
);
15326 args
->push_back(Expression::make_var_reference(vno
, location
));
15330 gogo
->start_block(location
);
15332 Call_expression
* call
= Expression::make_call(bm
, args
,
15333 method_type
->is_varargs(),
15336 Statement
* s
= Statement::make_return_from_call(call
, location
);
15337 gogo
->add_statement(s
);
15339 Block
* b
= gogo
->finish_block(location
);
15341 gogo
->add_block(b
, location
);
15343 // Lower the call in case there are multiple results.
15344 gogo
->lower_block(no
, b
);
15345 gogo
->flatten_block(no
, b
);
15347 gogo
->finish_function(location
);
15349 return Expression::make_func_reference(no
, NULL
, location
);
15352 // Dump the ast for a selector expression.
15355 Selector_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
15358 ast_dump_context
->dump_expression(this->left_
);
15359 ast_dump_context
->ostream() << ".";
15360 ast_dump_context
->ostream() << this->name_
;
15363 // Make a selector expression.
15366 Expression::make_selector(Expression
* left
, const std::string
& name
,
15369 return new Selector_expression(left
, name
, location
);
15372 // Class Allocation_expression.
15375 Allocation_expression::do_traverse(Traverse
* traverse
)
15377 return Type::traverse(this->type_
, traverse
);
15381 Allocation_expression::do_type()
15383 return Type::make_pointer_type(this->type_
);
15387 Allocation_expression::do_check_types(Gogo
*)
15389 if (!this->type_
->in_heap())
15390 go_error_at(this->location(), "cannot heap allocate go:notinheap type");
15393 // Make a copy of an allocation expression.
15396 Allocation_expression::do_copy()
15398 Allocation_expression
* alloc
=
15399 new Allocation_expression(this->type_
->copy_expressions(),
15401 if (this->allocate_on_stack_
)
15402 alloc
->set_allocate_on_stack();
15403 if (this->no_zero_
)
15404 alloc
->set_no_zero();
15408 // Return the backend representation for an allocation expression.
15411 Allocation_expression::do_get_backend(Translate_context
* context
)
15413 Gogo
* gogo
= context
->gogo();
15414 Location loc
= this->location();
15415 Btype
* btype
= this->type_
->get_backend(gogo
);
15417 if (this->allocate_on_stack_
)
15420 bool ok
= this->type_
->backend_type_size(gogo
, &size
);
15423 go_assert(saw_errors());
15424 return gogo
->backend()->error_expression();
15427 Named_object
* fn
= context
->function();
15428 go_assert(fn
!= NULL
);
15429 Bfunction
* fndecl
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
15430 Bexpression
* init
= (this->no_zero_
15432 : gogo
->backend()->zero_expression(btype
));
15434 gogo
->backend()->temporary_variable(fndecl
, context
->bblock(), btype
,
15436 Backend::variable_address_is_taken
,
15438 Bexpression
* ret
= gogo
->backend()->var_expression(temp
, loc
);
15439 ret
= gogo
->backend()->address_expression(ret
, loc
);
15440 ret
= gogo
->backend()->compound_expression(decl
, ret
, loc
);
15444 Bexpression
* space
=
15445 gogo
->allocate_memory(this->type_
, loc
)->get_backend(context
);
15446 Btype
* pbtype
= gogo
->backend()->pointer_type(btype
);
15447 return gogo
->backend()->convert_expression(pbtype
, space
, loc
);
15450 // Dump ast representation for an allocation expression.
15453 Allocation_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
15456 ast_dump_context
->ostream() << "new(";
15457 ast_dump_context
->dump_type(this->type_
);
15458 ast_dump_context
->ostream() << ")";
15461 // Make an allocation expression.
15464 Expression::make_allocation(Type
* type
, Location location
)
15466 return new Allocation_expression(type
, location
);
15469 // Class Ordered_value_list.
15472 Ordered_value_list::traverse_vals(Traverse
* traverse
)
15474 if (this->vals_
!= NULL
)
15476 if (this->traverse_order_
== NULL
)
15478 if (this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
15479 return TRAVERSE_EXIT
;
15483 for (std::vector
<unsigned long>::const_iterator p
=
15484 this->traverse_order_
->begin();
15485 p
!= this->traverse_order_
->end();
15488 if (Expression::traverse(&this->vals_
->at(*p
), traverse
)
15490 return TRAVERSE_EXIT
;
15494 return TRAVERSE_CONTINUE
;
15497 // Class Struct_construction_expression.
15502 Struct_construction_expression::do_traverse(Traverse
* traverse
)
15504 if (this->traverse_vals(traverse
) == TRAVERSE_EXIT
)
15505 return TRAVERSE_EXIT
;
15506 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
15507 return TRAVERSE_EXIT
;
15508 return TRAVERSE_CONTINUE
;
15511 // Return whether this is a constant initializer.
15514 Struct_construction_expression::is_constant_struct() const
15516 if (this->vals() == NULL
)
15518 for (Expression_list::const_iterator pv
= this->vals()->begin();
15519 pv
!= this->vals()->end();
15523 && !(*pv
)->is_constant()
15524 && (!(*pv
)->is_composite_literal()
15525 || (*pv
)->is_nonconstant_composite_literal()))
15529 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
15530 for (Struct_field_list::const_iterator pf
= fields
->begin();
15531 pf
!= fields
->end();
15534 // There are no constant constructors for interfaces.
15535 if (pf
->type()->interface_type() != NULL
)
15542 // Return whether this is a zero value.
15545 Struct_construction_expression::do_is_zero_value() const
15547 if (this->vals() == NULL
)
15549 for (Expression_list::const_iterator pv
= this->vals()->begin();
15550 pv
!= this->vals()->end();
15552 if (*pv
!= NULL
&& !(*pv
)->is_zero_value())
15555 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
15556 for (Struct_field_list::const_iterator pf
= fields
->begin();
15557 pf
!= fields
->end();
15560 // Interface conversion may cause a zero value being converted
15561 // to a non-zero value, like interface{}(0). Be conservative.
15562 if (pf
->type()->interface_type() != NULL
)
15569 // Return whether this struct can be used as a constant initializer.
15572 Struct_construction_expression::do_is_static_initializer() const
15574 if (this->vals() == NULL
)
15576 for (Expression_list::const_iterator pv
= this->vals()->begin();
15577 pv
!= this->vals()->end();
15580 if (*pv
!= NULL
&& !(*pv
)->is_static_initializer())
15584 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
15585 for (Struct_field_list::const_iterator pf
= fields
->begin();
15586 pf
!= fields
->end();
15589 // There are no constant constructors for interfaces.
15590 if (pf
->type()->interface_type() != NULL
)
15597 // Final type determination.
15600 Struct_construction_expression::do_determine_type(const Type_context
*)
15602 if (this->vals() == NULL
)
15604 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
15605 Expression_list::const_iterator pv
= this->vals()->begin();
15606 for (Struct_field_list::const_iterator pf
= fields
->begin();
15607 pf
!= fields
->end();
15610 if (pv
== this->vals()->end())
15614 Type_context
subcontext(pf
->type(), false);
15615 (*pv
)->determine_type(&subcontext
);
15618 // Extra values are an error we will report elsewhere; we still want
15619 // to determine the type to avoid knockon errors.
15620 for (; pv
!= this->vals()->end(); ++pv
)
15621 (*pv
)->determine_type_no_context();
15627 Struct_construction_expression::do_check_types(Gogo
*)
15629 if (this->vals() == NULL
)
15632 Struct_type
* st
= this->type_
->struct_type();
15633 if (this->vals()->size() > st
->field_count())
15635 this->report_error(_("too many expressions for struct"));
15639 const Struct_field_list
* fields
= st
->fields();
15640 Expression_list::const_iterator pv
= this->vals()->begin();
15642 for (Struct_field_list::const_iterator pf
= fields
->begin();
15643 pf
!= fields
->end();
15646 if (pv
== this->vals()->end())
15648 this->report_error(_("too few expressions for struct"));
15655 std::string reason
;
15656 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
15658 if (reason
.empty())
15659 go_error_at((*pv
)->location(),
15660 "incompatible type for field %d in struct construction",
15663 go_error_at((*pv
)->location(),
15664 ("incompatible type for field %d in "
15665 "struct construction (%s)"),
15666 i
+ 1, reason
.c_str());
15667 this->set_is_error();
15670 go_assert(pv
== this->vals()->end());
15676 Struct_construction_expression::do_copy()
15678 Struct_construction_expression
* ret
=
15679 new Struct_construction_expression(this->type_
->copy_expressions(),
15680 (this->vals() == NULL
15682 : this->vals()->copy()),
15684 if (this->traverse_order() != NULL
)
15685 ret
->set_traverse_order(this->traverse_order());
15689 // Make implicit type conversions explicit.
15692 Struct_construction_expression::do_add_conversions()
15694 if (this->vals() == NULL
)
15697 Location loc
= this->location();
15698 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
15699 Expression_list::iterator pv
= this->vals()->begin();
15700 for (Struct_field_list::const_iterator pf
= fields
->begin();
15701 pf
!= fields
->end();
15704 if (pv
== this->vals()->end())
15708 Type
* ft
= pf
->type();
15709 if (!Type::are_identical(ft
, (*pv
)->type(), 0, NULL
)
15710 && ft
->interface_type() != NULL
)
15711 *pv
= Expression::make_cast(ft
, *pv
, loc
);
15716 // Return the backend representation for constructing a struct.
15719 Struct_construction_expression::do_get_backend(Translate_context
* context
)
15721 Gogo
* gogo
= context
->gogo();
15723 Btype
* btype
= this->type_
->get_backend(gogo
);
15724 if (this->vals() == NULL
)
15725 return gogo
->backend()->zero_expression(btype
);
15727 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
15728 Expression_list::const_iterator pv
= this->vals()->begin();
15729 std::vector
<Bexpression
*> init
;
15730 for (Struct_field_list::const_iterator pf
= fields
->begin();
15731 pf
!= fields
->end();
15734 Btype
* fbtype
= pf
->type()->get_backend(gogo
);
15735 if (pv
== this->vals()->end())
15736 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
15737 else if (*pv
== NULL
)
15739 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
15745 Expression::convert_for_assignment(gogo
, pf
->type(),
15746 *pv
, this->location());
15747 init
.push_back(val
->get_backend(context
));
15751 if (this->type_
->struct_type()->has_padding())
15753 // Feed an extra value if there is a padding field.
15754 Btype
*fbtype
= Type::lookup_integer_type("uint8")->get_backend(gogo
);
15755 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
15757 return gogo
->backend()->constructor_expression(btype
, init
, this->location());
15760 // Export a struct construction.
15763 Struct_construction_expression::do_export(Export_function_body
* efb
) const
15765 efb
->write_c_string("$convert(");
15766 efb
->write_type(this->type_
);
15767 for (Expression_list::const_iterator pv
= this->vals()->begin();
15768 pv
!= this->vals()->end();
15771 efb
->write_c_string(", ");
15773 (*pv
)->export_expression(efb
);
15775 efb
->write_c_string(")");
15778 // Dump ast representation of a struct construction expression.
15781 Struct_construction_expression::do_dump_expression(
15782 Ast_dump_context
* ast_dump_context
) const
15784 ast_dump_context
->dump_type(this->type_
);
15785 ast_dump_context
->ostream() << "{";
15786 ast_dump_context
->dump_expression_list(this->vals());
15787 ast_dump_context
->ostream() << "}";
15790 // Make a struct composite literal. This used by the thunk code.
15793 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
15796 go_assert(type
->struct_type() != NULL
);
15797 return new Struct_construction_expression(type
, vals
, location
);
15800 // Class Array_construction_expression.
15805 Array_construction_expression::do_traverse(Traverse
* traverse
)
15807 if (this->traverse_vals(traverse
) == TRAVERSE_EXIT
)
15808 return TRAVERSE_EXIT
;
15809 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
15810 return TRAVERSE_EXIT
;
15811 return TRAVERSE_CONTINUE
;
15814 // Return whether this is a constant initializer.
15817 Array_construction_expression::is_constant_array() const
15819 if (this->vals() == NULL
)
15822 // There are no constant constructors for interfaces.
15823 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
15826 for (Expression_list::const_iterator pv
= this->vals()->begin();
15827 pv
!= this->vals()->end();
15831 && !(*pv
)->is_constant()
15832 && (!(*pv
)->is_composite_literal()
15833 || (*pv
)->is_nonconstant_composite_literal()))
15839 // Return whether this is a zero value.
15842 Array_construction_expression::do_is_zero_value() const
15844 if (this->vals() == NULL
)
15847 // Interface conversion may cause a zero value being converted
15848 // to a non-zero value, like interface{}(0). Be conservative.
15849 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
15852 for (Expression_list::const_iterator pv
= this->vals()->begin();
15853 pv
!= this->vals()->end();
15855 if (*pv
!= NULL
&& !(*pv
)->is_zero_value())
15861 // Return whether this can be used a constant initializer.
15864 Array_construction_expression::do_is_static_initializer() const
15866 if (this->vals() == NULL
)
15869 // There are no constant constructors for interfaces.
15870 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
15873 for (Expression_list::const_iterator pv
= this->vals()->begin();
15874 pv
!= this->vals()->end();
15877 if (*pv
!= NULL
&& !(*pv
)->is_static_initializer())
15883 // Final type determination.
15886 Array_construction_expression::do_determine_type(const Type_context
*)
15888 if (this->is_error_expression())
15890 go_assert(saw_errors());
15894 if (this->vals() == NULL
)
15896 Array_type
* at
= this->type_
->array_type();
15897 if (at
== NULL
|| at
->is_error() || at
->element_type()->is_error())
15899 go_assert(saw_errors());
15900 this->set_is_error();
15903 Type_context
subcontext(at
->element_type(), false);
15904 for (Expression_list::const_iterator pv
= this->vals()->begin();
15905 pv
!= this->vals()->end();
15909 (*pv
)->determine_type(&subcontext
);
15916 Array_construction_expression::do_check_types(Gogo
*)
15918 if (this->is_error_expression())
15920 go_assert(saw_errors());
15924 if (this->vals() == NULL
)
15927 Array_type
* at
= this->type_
->array_type();
15928 if (at
== NULL
|| at
->is_error() || at
->element_type()->is_error())
15930 go_assert(saw_errors());
15931 this->set_is_error();
15935 Type
* element_type
= at
->element_type();
15936 for (Expression_list::const_iterator pv
= this->vals()->begin();
15937 pv
!= this->vals()->end();
15941 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
15943 go_error_at((*pv
)->location(),
15944 "incompatible type for element %d in composite literal",
15946 this->set_is_error();
15951 // Make implicit type conversions explicit.
15954 Array_construction_expression::do_add_conversions()
15956 if (this->is_error_expression())
15958 go_assert(saw_errors());
15962 if (this->vals() == NULL
)
15965 Type
* et
= this->type_
->array_type()->element_type();
15966 if (et
->interface_type() == NULL
)
15969 Location loc
= this->location();
15970 for (Expression_list::iterator pv
= this->vals()->begin();
15971 pv
!= this->vals()->end();
15973 if (!Type::are_identical(et
, (*pv
)->type(), 0, NULL
))
15974 *pv
= Expression::make_cast(et
, *pv
, loc
);
15977 // Get a constructor expression for the array values.
15980 Array_construction_expression::get_constructor(Translate_context
* context
,
15981 Btype
* array_btype
)
15983 Type
* element_type
= this->type_
->array_type()->element_type();
15985 std::vector
<unsigned long> indexes
;
15986 std::vector
<Bexpression
*> vals
;
15987 Gogo
* gogo
= context
->gogo();
15988 if (this->vals() != NULL
)
15991 std::vector
<unsigned long>::const_iterator pi
;
15992 if (this->indexes_
!= NULL
)
15993 pi
= this->indexes_
->begin();
15994 for (Expression_list::const_iterator pv
= this->vals()->begin();
15995 pv
!= this->vals()->end();
15998 if (this->indexes_
!= NULL
)
15999 go_assert(pi
!= this->indexes_
->end());
16001 if (this->indexes_
== NULL
)
16002 indexes
.push_back(i
);
16004 indexes
.push_back(*pi
);
16007 Btype
* ebtype
= element_type
->get_backend(gogo
);
16008 Bexpression
*zv
= gogo
->backend()->zero_expression(ebtype
);
16009 vals
.push_back(zv
);
16013 Expression
* val_expr
=
16014 Expression::convert_for_assignment(gogo
, element_type
, *pv
,
16016 vals
.push_back(val_expr
->get_backend(context
));
16018 if (this->indexes_
!= NULL
)
16021 if (this->indexes_
!= NULL
)
16022 go_assert(pi
== this->indexes_
->end());
16024 return gogo
->backend()->array_constructor_expression(array_btype
, indexes
,
16025 vals
, this->location());
16028 // Export an array construction.
16031 Array_construction_expression::do_export(Export_function_body
* efb
) const
16033 efb
->write_c_string("$convert(");
16034 efb
->write_type(this->type_
);
16035 if (this->vals() != NULL
)
16037 std::vector
<unsigned long>::const_iterator pi
;
16038 if (this->indexes_
!= NULL
)
16039 pi
= this->indexes_
->begin();
16040 for (Expression_list::const_iterator pv
= this->vals()->begin();
16041 pv
!= this->vals()->end();
16044 efb
->write_c_string(", ");
16046 if (this->indexes_
!= NULL
)
16049 snprintf(buf
, sizeof buf
, "%lu", *pi
);
16050 efb
->write_c_string(buf
);
16051 efb
->write_c_string(":");
16055 (*pv
)->export_expression(efb
);
16057 if (this->indexes_
!= NULL
)
16061 efb
->write_c_string(")");
16064 // Dump ast representation of an array construction expression.
16067 Array_construction_expression::do_dump_expression(
16068 Ast_dump_context
* ast_dump_context
) const
16070 Expression
* length
= this->type_
->array_type()->length();
16072 ast_dump_context
->ostream() << "[" ;
16073 if (length
!= NULL
)
16075 ast_dump_context
->dump_expression(length
);
16077 ast_dump_context
->ostream() << "]" ;
16078 ast_dump_context
->dump_type(this->type_
);
16079 this->dump_slice_storage_expression(ast_dump_context
);
16080 ast_dump_context
->ostream() << "{" ;
16081 if (this->indexes_
== NULL
)
16082 ast_dump_context
->dump_expression_list(this->vals());
16085 Expression_list::const_iterator pv
= this->vals()->begin();
16086 for (std::vector
<unsigned long>::const_iterator pi
=
16087 this->indexes_
->begin();
16088 pi
!= this->indexes_
->end();
16091 if (pi
!= this->indexes_
->begin())
16092 ast_dump_context
->ostream() << ", ";
16093 ast_dump_context
->ostream() << *pi
<< ':';
16094 ast_dump_context
->dump_expression(*pv
);
16097 ast_dump_context
->ostream() << "}" ;
16101 // Class Fixed_array_construction_expression.
16103 Fixed_array_construction_expression::Fixed_array_construction_expression(
16104 Type
* type
, const std::vector
<unsigned long>* indexes
,
16105 Expression_list
* vals
, Location location
)
16106 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
16107 type
, indexes
, vals
, location
)
16108 { go_assert(type
->array_type() != NULL
&& !type
->is_slice_type()); }
16114 Fixed_array_construction_expression::do_copy()
16116 Type
* t
= this->type()->copy_expressions();
16117 return new Fixed_array_construction_expression(t
, this->indexes(),
16118 (this->vals() == NULL
16120 : this->vals()->copy()),
16124 // Return the backend representation for constructing a fixed array.
16127 Fixed_array_construction_expression::do_get_backend(Translate_context
* context
)
16129 Type
* type
= this->type();
16130 Btype
* btype
= type
->get_backend(context
->gogo());
16131 return this->get_constructor(context
, btype
);
16135 Expression::make_array_composite_literal(Type
* type
, Expression_list
* vals
,
16138 go_assert(type
->array_type() != NULL
&& !type
->is_slice_type());
16139 return new Fixed_array_construction_expression(type
, NULL
, vals
, location
);
16142 // Class Slice_construction_expression.
16144 Slice_construction_expression::Slice_construction_expression(
16145 Type
* type
, const std::vector
<unsigned long>* indexes
,
16146 Expression_list
* vals
, Location location
)
16147 : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION
,
16148 type
, indexes
, vals
, location
),
16149 valtype_(NULL
), array_val_(NULL
), slice_storage_(NULL
),
16150 storage_escapes_(true)
16152 go_assert(type
->is_slice_type());
16154 unsigned long lenval
;
16155 Expression
* length
;
16156 if (vals
== NULL
|| vals
->empty())
16160 if (this->indexes() == NULL
)
16161 lenval
= vals
->size();
16163 lenval
= indexes
->back() + 1;
16165 Type
* int_type
= Type::lookup_integer_type("int");
16166 length
= Expression::make_integer_ul(lenval
, int_type
, location
);
16167 Type
* element_type
= type
->array_type()->element_type();
16168 Array_type
* array_type
= Type::make_array_type(element_type
, length
);
16169 array_type
->set_is_array_incomparable();
16170 this->valtype_
= array_type
;
16176 Slice_construction_expression::do_traverse(Traverse
* traverse
)
16178 if (this->Array_construction_expression::do_traverse(traverse
)
16180 return TRAVERSE_EXIT
;
16181 if (Type::traverse(this->valtype_
, traverse
) == TRAVERSE_EXIT
)
16182 return TRAVERSE_EXIT
;
16183 if (this->array_val_
!= NULL
16184 && Expression::traverse(&this->array_val_
, traverse
) == TRAVERSE_EXIT
)
16185 return TRAVERSE_EXIT
;
16186 if (this->slice_storage_
!= NULL
16187 && Expression::traverse(&this->slice_storage_
, traverse
) == TRAVERSE_EXIT
)
16188 return TRAVERSE_EXIT
;
16189 return TRAVERSE_CONTINUE
;
16192 // Helper routine to create fixed array value underlying the slice literal.
16193 // May be called during flattening, or later during do_get_backend().
16196 Slice_construction_expression::create_array_val()
16198 Array_type
* array_type
= this->type()->array_type();
16199 if (array_type
== NULL
)
16201 go_assert(this->type()->is_error());
16205 Location loc
= this->location();
16206 go_assert(this->valtype_
!= NULL
);
16208 Expression_list
* vals
= this->vals();
16209 return new Fixed_array_construction_expression(
16210 this->valtype_
, this->indexes(), vals
, loc
);
16213 // If we're previous established that the slice storage does not
16214 // escape, then create a separate array temp val here for it. We
16215 // need to do this as part of flattening so as to be able to insert
16216 // the new temp statement.
16219 Slice_construction_expression::do_flatten(Gogo
*, Named_object
*,
16220 Statement_inserter
* inserter
)
16222 if (this->type()->array_type() == NULL
)
16224 go_assert(saw_errors());
16225 return Expression::make_error(this->location());
16228 // Create a stack-allocated storage temp if storage won't escape
16229 if (!this->storage_escapes_
16230 && this->slice_storage_
== NULL
16231 && this->element_count() > 0)
16233 Location loc
= this->location();
16234 this->array_val_
= this->create_array_val();
16235 go_assert(this->array_val_
!= NULL
);
16236 Temporary_statement
* temp
=
16237 Statement::make_temporary(this->valtype_
, this->array_val_
, loc
);
16238 inserter
->insert(temp
);
16239 this->slice_storage_
= Expression::make_temporary_reference(temp
, loc
);
16244 // When dumping a slice construction expression that has an explicit
16245 // storeage temp, emit the temp here (if we don't do this the storage
16246 // temp appears unused in the AST dump).
16249 Slice_construction_expression::
16250 dump_slice_storage_expression(Ast_dump_context
* ast_dump_context
) const
16252 if (this->slice_storage_
== NULL
)
16254 ast_dump_context
->ostream() << "storage=" ;
16255 ast_dump_context
->dump_expression(this->slice_storage_
);
16261 Slice_construction_expression::do_copy()
16263 return new Slice_construction_expression(this->type()->copy_expressions(),
16265 (this->vals() == NULL
16267 : this->vals()->copy()),
16271 // Return the backend representation for constructing a slice.
16274 Slice_construction_expression::do_get_backend(Translate_context
* context
)
16276 if (this->array_val_
== NULL
)
16277 this->array_val_
= this->create_array_val();
16278 if (this->array_val_
== NULL
)
16280 go_assert(this->type()->is_error());
16281 return context
->backend()->error_expression();
16284 Location loc
= this->location();
16286 bool is_static_initializer
= this->array_val_
->is_static_initializer();
16288 // We have to copy the initial values into heap memory if we are in
16289 // a function or if the values are not constants.
16290 bool copy_to_heap
= context
->function() != NULL
|| !is_static_initializer
;
16294 if (this->slice_storage_
!= NULL
)
16296 go_assert(!this->storage_escapes_
);
16297 space
= Expression::make_unary(OPERATOR_AND
, this->slice_storage_
, loc
);
16299 else if (!copy_to_heap
)
16301 // The initializer will only run once.
16302 space
= Expression::make_unary(OPERATOR_AND
, this->array_val_
, loc
);
16303 space
->unary_expression()->set_is_slice_init();
16307 go_assert(this->storage_escapes_
|| this->element_count() == 0);
16308 space
= Expression::make_heap_expression(this->array_val_
, loc
);
16310 Array_type
* at
= this->valtype_
->array_type();
16311 Type
* et
= at
->element_type();
16312 space
= Expression::make_unsafe_cast(Type::make_pointer_type(et
),
16315 // Build a constructor for the slice.
16316 Expression
* len
= at
->length();
16317 Expression
* slice_val
=
16318 Expression::make_slice_value(this->type(), space
, len
, len
, loc
);
16319 return slice_val
->get_backend(context
);
16322 // Make a slice composite literal. This is used by the type
16323 // descriptor code.
16325 Slice_construction_expression
*
16326 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
16329 go_assert(type
->is_slice_type());
16330 return new Slice_construction_expression(type
, NULL
, vals
, location
);
16333 // Class Map_construction_expression.
16338 Map_construction_expression::do_traverse(Traverse
* traverse
)
16340 if (this->vals_
!= NULL
16341 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
16342 return TRAVERSE_EXIT
;
16343 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
16344 return TRAVERSE_EXIT
;
16345 return TRAVERSE_CONTINUE
;
16348 // Flatten constructor initializer into a temporary variable since
16349 // we need to take its address for __go_construct_map.
16352 Map_construction_expression::do_flatten(Gogo
* gogo
, Named_object
*,
16353 Statement_inserter
* inserter
)
16355 if (!this->is_error_expression()
16356 && this->vals_
!= NULL
16357 && !this->vals_
->empty()
16358 && this->constructor_temp_
== NULL
)
16360 Map_type
* mt
= this->type_
->map_type();
16361 Type
* key_type
= mt
->key_type();
16362 Type
* val_type
= mt
->val_type();
16363 this->element_type_
= Type::make_builtin_struct_type(2,
16365 "__val", val_type
);
16367 Expression_list
* value_pairs
= new Expression_list();
16368 Location loc
= this->location();
16371 for (Expression_list::const_iterator pv
= this->vals_
->begin();
16372 pv
!= this->vals_
->end();
16375 Expression_list
* key_value_pair
= new Expression_list();
16376 Expression
* key
= *pv
;
16377 if (key
->is_error_expression() || key
->type()->is_error_type())
16379 go_assert(saw_errors());
16380 return Expression::make_error(loc
);
16382 if (key
->type()->interface_type() != NULL
16383 && !key
->is_multi_eval_safe())
16385 Temporary_statement
* temp
=
16386 Statement::make_temporary(NULL
, key
, loc
);
16387 inserter
->insert(temp
);
16388 key
= Expression::make_temporary_reference(temp
, loc
);
16390 key
= Expression::convert_for_assignment(gogo
, key_type
, key
, loc
);
16393 Expression
* val
= *pv
;
16394 if (val
->is_error_expression() || val
->type()->is_error_type())
16396 go_assert(saw_errors());
16397 return Expression::make_error(loc
);
16399 if (val
->type()->interface_type() != NULL
16400 && !val
->is_multi_eval_safe())
16402 Temporary_statement
* temp
=
16403 Statement::make_temporary(NULL
, val
, loc
);
16404 inserter
->insert(temp
);
16405 val
= Expression::make_temporary_reference(temp
, loc
);
16407 val
= Expression::convert_for_assignment(gogo
, val_type
, val
, loc
);
16409 key_value_pair
->push_back(key
);
16410 key_value_pair
->push_back(val
);
16411 value_pairs
->push_back(
16412 Expression::make_struct_composite_literal(this->element_type_
,
16413 key_value_pair
, loc
));
16416 Expression
* element_count
= Expression::make_integer_ul(i
, NULL
, loc
);
16417 Array_type
* ctor_type
=
16418 Type::make_array_type(this->element_type_
, element_count
);
16419 ctor_type
->set_is_array_incomparable();
16420 Expression
* constructor
=
16421 new Fixed_array_construction_expression(ctor_type
, NULL
,
16424 this->constructor_temp_
=
16425 Statement::make_temporary(NULL
, constructor
, loc
);
16426 constructor
->issue_nil_check();
16427 this->constructor_temp_
->set_is_address_taken();
16428 inserter
->insert(this->constructor_temp_
);
16434 // Final type determination.
16437 Map_construction_expression::do_determine_type(const Type_context
*)
16439 if (this->vals_
== NULL
)
16442 Map_type
* mt
= this->type_
->map_type();
16443 Type_context
key_context(mt
->key_type(), false);
16444 Type_context
val_context(mt
->val_type(), false);
16445 for (Expression_list::const_iterator pv
= this->vals_
->begin();
16446 pv
!= this->vals_
->end();
16449 (*pv
)->determine_type(&key_context
);
16451 (*pv
)->determine_type(&val_context
);
16458 Map_construction_expression::do_check_types(Gogo
*)
16460 if (this->vals_
== NULL
)
16463 Map_type
* mt
= this->type_
->map_type();
16465 Type
* key_type
= mt
->key_type();
16466 Type
* val_type
= mt
->val_type();
16467 for (Expression_list::const_iterator pv
= this->vals_
->begin();
16468 pv
!= this->vals_
->end();
16471 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
16473 go_error_at((*pv
)->location(),
16474 "incompatible type for element %d key in map construction",
16476 this->set_is_error();
16479 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
16481 go_error_at((*pv
)->location(),
16482 ("incompatible type for element %d value "
16483 "in map construction"),
16485 this->set_is_error();
16493 Map_construction_expression::do_copy()
16495 return new Map_construction_expression(this->type_
->copy_expressions(),
16496 (this->vals_
== NULL
16498 : this->vals_
->copy()),
16502 // Make implicit type conversions explicit.
16505 Map_construction_expression::do_add_conversions()
16507 if (this->vals_
== NULL
|| this->vals_
->empty())
16510 Map_type
* mt
= this->type_
->map_type();
16511 Type
* kt
= mt
->key_type();
16512 Type
* vt
= mt
->val_type();
16513 bool key_is_interface
= (kt
->interface_type() != NULL
);
16514 bool val_is_interface
= (vt
->interface_type() != NULL
);
16515 if (!key_is_interface
&& !val_is_interface
)
16518 Location loc
= this->location();
16519 for (Expression_list::iterator pv
= this->vals_
->begin();
16520 pv
!= this->vals_
->end();
16523 if (key_is_interface
&&
16524 !Type::are_identical(kt
, (*pv
)->type(), 0, NULL
))
16525 *pv
= Expression::make_cast(kt
, *pv
, loc
);
16527 if (val_is_interface
&&
16528 !Type::are_identical(vt
, (*pv
)->type(), 0, NULL
))
16529 *pv
= Expression::make_cast(vt
, *pv
, loc
);
16533 // Return the backend representation for constructing a map.
16536 Map_construction_expression::do_get_backend(Translate_context
* context
)
16538 if (this->is_error_expression())
16539 return context
->backend()->error_expression();
16540 Location loc
= this->location();
16543 Expression
* ventries
;
16544 if (this->vals_
== NULL
|| this->vals_
->empty())
16545 ventries
= Expression::make_nil(loc
);
16548 go_assert(this->constructor_temp_
!= NULL
);
16549 i
= this->vals_
->size() / 2;
16551 Expression
* ctor_ref
=
16552 Expression::make_temporary_reference(this->constructor_temp_
, loc
);
16553 ventries
= Expression::make_unary(OPERATOR_AND
, ctor_ref
, loc
);
16556 Map_type
* mt
= this->type_
->map_type();
16557 if (this->element_type_
== NULL
)
16558 this->element_type_
=
16559 Type::make_builtin_struct_type(2,
16560 "__key", mt
->key_type(),
16561 "__val", mt
->val_type());
16562 Expression
* descriptor
= Expression::make_type_descriptor(mt
, loc
);
16564 Type
* uintptr_t = Type::lookup_integer_type("uintptr");
16565 Expression
* count
= Expression::make_integer_ul(i
, uintptr_t, loc
);
16567 Expression
* entry_size
=
16568 Expression::make_type_info(this->element_type_
, TYPE_INFO_SIZE
);
16570 unsigned int field_index
;
16571 const Struct_field
* valfield
=
16572 this->element_type_
->find_local_field("__val", &field_index
);
16573 Expression
* val_offset
=
16574 Expression::make_struct_field_offset(this->element_type_
, valfield
);
16576 Expression
* map_ctor
=
16577 Runtime::make_call(Runtime::CONSTRUCT_MAP
, loc
, 5, descriptor
, count
,
16578 entry_size
, val_offset
, ventries
);
16579 return map_ctor
->get_backend(context
);
16582 // Export an array construction.
16585 Map_construction_expression::do_export(Export_function_body
* efb
) const
16587 efb
->write_c_string("$convert(");
16588 efb
->write_type(this->type_
);
16589 for (Expression_list::const_iterator pv
= this->vals_
->begin();
16590 pv
!= this->vals_
->end();
16593 efb
->write_c_string(", ");
16594 (*pv
)->export_expression(efb
);
16596 efb
->write_c_string(")");
16599 // Dump ast representation for a map construction expression.
16602 Map_construction_expression::do_dump_expression(
16603 Ast_dump_context
* ast_dump_context
) const
16605 ast_dump_context
->ostream() << "{" ;
16606 ast_dump_context
->dump_expression_list(this->vals_
, true);
16607 ast_dump_context
->ostream() << "}";
16610 // A composite literal key. This is seen during parsing, but is not
16611 // resolved to a named_object in case this is a composite literal of
16614 class Composite_literal_key_expression
: public Parser_expression
16617 Composite_literal_key_expression(const std::string
& name
, Location location
)
16618 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL_KEY
, location
),
16624 { return this->name_
; }
16628 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
16633 return new Composite_literal_key_expression(this->name_
, this->location());
16637 do_dump_expression(Ast_dump_context
*) const;
16644 // Lower a composite literal key. We will never get here for keys in
16645 // composite literals of struct types, because that is prevented by
16646 // Composite_literal_expression::do_traverse. So if we do get here,
16647 // this must be a regular name reference after all.
16650 Composite_literal_key_expression::do_lower(Gogo
* gogo
, Named_object
*,
16651 Statement_inserter
*, int)
16653 Named_object
* no
= gogo
->lookup(this->name_
, NULL
);
16656 // Gogo::lookup doesn't look in the global namespace, and names
16657 // used in composite literal keys aren't seen by
16658 // Gogo::define_global_names, so we have to look in the global
16659 // namespace ourselves.
16660 no
= gogo
->lookup_global(Gogo::unpack_hidden_name(this->name_
).c_str());
16663 go_error_at(this->location(), "reference to undefined name %qs",
16664 Gogo::message_name(this->name_
).c_str());
16665 return Expression::make_error(this->location());
16668 return Expression::make_unknown_reference(no
, this->location());
16671 // Dump a composite literal key.
16674 Composite_literal_key_expression::do_dump_expression(
16675 Ast_dump_context
* ast_dump_context
) const
16677 ast_dump_context
->ostream() << "_UnknownName_(" << this->name_
<< ")";
16680 // Make a composite literal key.
16683 Expression::make_composite_literal_key(const std::string
& name
,
16686 return new Composite_literal_key_expression(name
, location
);
16689 // Class Composite_literal_expression.
16694 Composite_literal_expression::do_traverse(Traverse
* traverse
)
16696 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
16697 return TRAVERSE_EXIT
;
16699 // If this is a struct composite literal with keys, then the keys
16700 // are field names, not expressions. We don't want to traverse them
16701 // in that case. If we do, we can give an erroneous error "variable
16702 // initializer refers to itself." See bug482.go in the testsuite.
16703 if (this->has_keys_
&& this->vals_
!= NULL
)
16705 // The type may not be resolvable at this point.
16706 Type
* type
= this->type_
;
16708 for (int depth
= 0; depth
< this->depth_
; ++depth
)
16710 type
= type
->deref();
16711 if (type
->array_type() != NULL
)
16712 type
= type
->array_type()->element_type();
16713 else if (type
->map_type() != NULL
)
16715 if (this->key_path_
[depth
])
16716 type
= type
->map_type()->key_type();
16718 type
= type
->map_type()->val_type();
16722 // This error will be reported during lowering.
16723 return TRAVERSE_CONTINUE
;
16726 type
= type
->deref();
16730 if (type
->classification() == Type::TYPE_NAMED
)
16731 type
= type
->named_type()->real_type();
16732 else if (type
->classification() == Type::TYPE_FORWARD
)
16734 Type
* t
= type
->forwarded();
16743 if (type
->classification() == Type::TYPE_STRUCT
)
16745 Expression_list::iterator p
= this->vals_
->begin();
16746 while (p
!= this->vals_
->end())
16750 go_assert(p
!= this->vals_
->end());
16751 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
16752 return TRAVERSE_EXIT
;
16755 return TRAVERSE_CONTINUE
;
16759 if (this->vals_
!= NULL
)
16760 return this->vals_
->traverse(traverse
);
16762 return TRAVERSE_CONTINUE
;
16765 // Lower a generic composite literal into a specific version based on
16769 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
16770 Statement_inserter
* inserter
, int)
16772 Type
* type
= this->type_
;
16774 for (int depth
= 0; depth
< this->depth_
; ++depth
)
16776 type
= type
->deref();
16777 if (type
->array_type() != NULL
)
16778 type
= type
->array_type()->element_type();
16779 else if (type
->map_type() != NULL
)
16781 if (this->key_path_
[depth
])
16782 type
= type
->map_type()->key_type();
16784 type
= type
->map_type()->val_type();
16788 if (!type
->is_error())
16789 go_error_at(this->location(),
16790 ("may only omit types within composite literals "
16791 "of slice, array, or map type"));
16792 return Expression::make_error(this->location());
16796 Type
*pt
= type
->points_to();
16797 bool is_pointer
= false;
16805 if (type
->is_error())
16806 return Expression::make_error(this->location());
16807 else if (type
->struct_type() != NULL
)
16808 ret
= this->lower_struct(gogo
, type
);
16809 else if (type
->array_type() != NULL
)
16810 ret
= this->lower_array(type
);
16811 else if (type
->map_type() != NULL
)
16812 ret
= this->lower_map(gogo
, function
, inserter
, type
);
16815 go_error_at(this->location(),
16816 ("expected struct, slice, array, or map type "
16817 "for composite literal"));
16818 return Expression::make_error(this->location());
16822 ret
= Expression::make_heap_expression(ret
, this->location());
16827 // Lower a struct composite literal.
16830 Composite_literal_expression::lower_struct(Gogo
* gogo
, Type
* type
)
16832 Location location
= this->location();
16833 Struct_type
* st
= type
->struct_type();
16834 if (this->vals_
== NULL
|| !this->has_keys_
)
16836 if (this->vals_
!= NULL
16837 && !this->vals_
->empty()
16838 && type
->named_type() != NULL
16839 && type
->named_type()->named_object()->package() != NULL
)
16841 for (Struct_field_list::const_iterator pf
= st
->fields()->begin();
16842 pf
!= st
->fields()->end();
16845 if (Gogo::is_hidden_name(pf
->field_name())
16846 || pf
->is_embedded_builtin(gogo
))
16847 go_error_at(this->location(),
16848 "assignment of unexported field %qs in %qs literal",
16849 Gogo::message_name(pf
->field_name()).c_str(),
16850 type
->named_type()->message_name().c_str());
16854 return new Struct_construction_expression(type
, this->vals_
, location
);
16857 size_t field_count
= st
->field_count();
16858 std::vector
<Expression
*> vals(field_count
);
16859 std::vector
<unsigned long>* traverse_order
= new(std::vector
<unsigned long>);
16860 Expression_list::const_iterator p
= this->vals_
->begin();
16861 Expression
* external_expr
= NULL
;
16862 const Named_object
* external_no
= NULL
;
16863 while (p
!= this->vals_
->end())
16865 Expression
* name_expr
= *p
;
16868 go_assert(p
!= this->vals_
->end());
16869 Expression
* val
= *p
;
16873 if (name_expr
== NULL
)
16875 go_error_at(val
->location(),
16876 "mixture of field and value initializers");
16877 return Expression::make_error(location
);
16880 bool bad_key
= false;
16882 const Named_object
* no
= NULL
;
16883 switch (name_expr
->classification())
16885 case EXPRESSION_COMPOSITE_LITERAL_KEY
:
16887 static_cast<Composite_literal_key_expression
*>(name_expr
)->name();
16890 case EXPRESSION_UNKNOWN_REFERENCE
:
16891 name
= name_expr
->unknown_expression()->name();
16892 if (type
->named_type() != NULL
)
16894 // If the named object found for this field name comes from a
16895 // different package than the struct it is a part of, do not count
16896 // this incorrect lookup as a usage of the object's package.
16897 no
= name_expr
->unknown_expression()->named_object();
16898 if (no
->package() != NULL
16899 && no
->package() != type
->named_type()->named_object()->package())
16900 no
->package()->forget_usage(name_expr
);
16904 case EXPRESSION_CONST_REFERENCE
:
16905 no
= static_cast<Const_expression
*>(name_expr
)->named_object();
16908 case EXPRESSION_TYPE
:
16910 Type
* t
= name_expr
->type();
16911 Named_type
* nt
= t
->named_type();
16915 no
= nt
->named_object();
16919 case EXPRESSION_VAR_REFERENCE
:
16920 no
= name_expr
->var_expression()->named_object();
16923 case EXPRESSION_ENCLOSED_VAR_REFERENCE
:
16924 no
= name_expr
->enclosed_var_expression()->variable();
16927 case EXPRESSION_FUNC_REFERENCE
:
16928 no
= name_expr
->func_expression()->named_object();
16937 go_error_at(name_expr
->location(), "expected struct field name");
16938 return Expression::make_error(location
);
16943 if (no
->package() != NULL
&& external_expr
== NULL
)
16945 external_expr
= name_expr
;
16951 // A predefined name won't be packed. If it starts with a
16952 // lower case letter we need to check for that case, because
16953 // the field name will be packed. FIXME.
16954 if (!Gogo::is_hidden_name(name
)
16958 Named_object
* gno
= gogo
->lookup_global(name
.c_str());
16960 name
= gogo
->pack_hidden_name(name
, false);
16964 unsigned int index
;
16965 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
16968 go_error_at(name_expr
->location(), "unknown field %qs in %qs",
16969 Gogo::message_name(name
).c_str(),
16970 (type
->named_type() != NULL
16971 ? type
->named_type()->message_name().c_str()
16972 : "unnamed struct"));
16973 return Expression::make_error(location
);
16975 if (vals
[index
] != NULL
)
16977 go_error_at(name_expr
->location(),
16978 "duplicate value for field %qs in %qs",
16979 Gogo::message_name(name
).c_str(),
16980 (type
->named_type() != NULL
16981 ? type
->named_type()->message_name().c_str()
16982 : "unnamed struct"));
16983 return Expression::make_error(location
);
16986 if (type
->named_type() != NULL
16987 && type
->named_type()->named_object()->package() != NULL
16988 && (Gogo::is_hidden_name(sf
->field_name())
16989 || sf
->is_embedded_builtin(gogo
)))
16990 go_error_at(name_expr
->location(),
16991 "assignment of unexported field %qs in %qs literal",
16992 Gogo::message_name(sf
->field_name()).c_str(),
16993 type
->named_type()->message_name().c_str());
16996 traverse_order
->push_back(static_cast<unsigned long>(index
));
16999 if (!this->all_are_names_
)
17001 // This is a weird case like bug462 in the testsuite.
17002 if (external_expr
== NULL
)
17003 go_error_at(this->location(), "unknown field in %qs literal",
17004 (type
->named_type() != NULL
17005 ? type
->named_type()->message_name().c_str()
17006 : "unnamed struct"));
17008 go_error_at(external_expr
->location(), "unknown field %qs in %qs",
17009 external_no
->message_name().c_str(),
17010 (type
->named_type() != NULL
17011 ? type
->named_type()->message_name().c_str()
17012 : "unnamed struct"));
17013 return Expression::make_error(location
);
17016 Expression_list
* list
= new Expression_list
;
17017 list
->reserve(field_count
);
17018 for (size_t i
= 0; i
< field_count
; ++i
)
17019 list
->push_back(vals
[i
]);
17021 Struct_construction_expression
* ret
=
17022 new Struct_construction_expression(type
, list
, location
);
17023 ret
->set_traverse_order(traverse_order
);
17027 // Index/value/traversal-order triple.
17029 struct IVT_triple
{
17030 unsigned long index
;
17031 unsigned long traversal_order
;
17033 IVT_triple(unsigned long i
, unsigned long to
, Expression
*e
)
17034 : index(i
), traversal_order(to
), expr(e
) { }
17035 bool operator<(const IVT_triple
& other
) const
17036 { return this->index
< other
.index
; }
17039 // Lower an array composite literal.
17042 Composite_literal_expression::lower_array(Type
* type
)
17044 Location location
= this->location();
17045 if (this->vals_
== NULL
|| !this->has_keys_
)
17046 return this->make_array(type
, NULL
, this->vals_
);
17048 std::vector
<unsigned long>* indexes
= new std::vector
<unsigned long>;
17049 indexes
->reserve(this->vals_
->size());
17050 bool indexes_out_of_order
= false;
17051 Expression_list
* vals
= new Expression_list();
17052 vals
->reserve(this->vals_
->size());
17053 unsigned long index
= 0;
17054 Expression_list::const_iterator p
= this->vals_
->begin();
17055 while (p
!= this->vals_
->end())
17057 Expression
* index_expr
= *p
;
17060 go_assert(p
!= this->vals_
->end());
17061 Expression
* val
= *p
;
17065 if (index_expr
== NULL
)
17067 if (std::find(indexes
->begin(), indexes
->end(), index
)
17070 go_error_at(val
->location(),
17071 "duplicate value for index %lu", index
);
17072 return Expression::make_error(location
);
17074 if (!indexes
->empty())
17075 indexes
->push_back(index
);
17079 if (indexes
->empty() && !vals
->empty())
17081 for (size_t i
= 0; i
< vals
->size(); ++i
)
17082 indexes
->push_back(i
);
17085 Numeric_constant nc
;
17086 if (!index_expr
->numeric_constant_value(&nc
))
17088 go_error_at(index_expr
->location(),
17089 "index expression is not integer constant");
17090 return Expression::make_error(location
);
17093 switch (nc
.to_unsigned_long(&index
))
17095 case Numeric_constant::NC_UL_VALID
:
17097 case Numeric_constant::NC_UL_NOTINT
:
17098 go_error_at(index_expr
->location(),
17099 "index expression is not integer constant");
17100 return Expression::make_error(location
);
17101 case Numeric_constant::NC_UL_NEGATIVE
:
17102 go_error_at(index_expr
->location(),
17103 "index expression is negative");
17104 return Expression::make_error(location
);
17105 case Numeric_constant::NC_UL_BIG
:
17106 go_error_at(index_expr
->location(), "index value overflow");
17107 return Expression::make_error(location
);
17112 Named_type
* ntype
= Type::lookup_integer_type("int");
17113 Integer_type
* inttype
= ntype
->integer_type();
17114 if (sizeof(index
) <= static_cast<size_t>(inttype
->bits() * 8)
17115 && index
>> (inttype
->bits() - 1) != 0)
17117 go_error_at(index_expr
->location(), "index value overflow");
17118 return Expression::make_error(location
);
17121 if (std::find(indexes
->begin(), indexes
->end(), index
)
17124 go_error_at(index_expr
->location(),
17125 "duplicate value for index %lu",
17127 return Expression::make_error(location
);
17130 if (!indexes
->empty() && index
< indexes
->back())
17131 indexes_out_of_order
= true;
17133 indexes
->push_back(index
);
17136 vals
->push_back(val
);
17141 if (indexes
->empty())
17147 std::vector
<unsigned long>* traverse_order
= NULL
;
17148 if (indexes_out_of_order
)
17150 typedef std::vector
<IVT_triple
> V
;
17153 v
.reserve(indexes
->size());
17154 std::vector
<unsigned long>::const_iterator pi
= indexes
->begin();
17155 unsigned long torder
= 0;
17156 for (Expression_list::const_iterator pe
= vals
->begin();
17158 ++pe
, ++pi
, ++torder
)
17159 v
.push_back(IVT_triple(*pi
, torder
, *pe
));
17161 std::sort(v
.begin(), v
.end());
17166 indexes
= new std::vector
<unsigned long>();
17167 indexes
->reserve(v
.size());
17168 vals
= new Expression_list();
17169 vals
->reserve(v
.size());
17170 traverse_order
= new std::vector
<unsigned long>();
17171 traverse_order
->reserve(v
.size());
17173 for (V::const_iterator pv
= v
.begin(); pv
!= v
.end(); ++pv
)
17175 indexes
->push_back(pv
->index
);
17176 vals
->push_back(pv
->expr
);
17177 traverse_order
->push_back(pv
->traversal_order
);
17181 Expression
* ret
= this->make_array(type
, indexes
, vals
);
17182 Array_construction_expression
* ace
= ret
->array_literal();
17183 if (ace
!= NULL
&& traverse_order
!= NULL
)
17184 ace
->set_traverse_order(traverse_order
);
17188 // Actually build the array composite literal. This handles
17192 Composite_literal_expression::make_array(
17194 const std::vector
<unsigned long>* indexes
,
17195 Expression_list
* vals
)
17197 Location location
= this->location();
17198 Array_type
* at
= type
->array_type();
17200 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
17205 else if (indexes
!= NULL
)
17206 size
= indexes
->back() + 1;
17209 size
= vals
->size();
17210 Integer_type
* it
= Type::lookup_integer_type("int")->integer_type();
17211 if (sizeof(size
) <= static_cast<size_t>(it
->bits() * 8)
17212 && size
>> (it
->bits() - 1) != 0)
17214 go_error_at(location
, "too many elements in composite literal");
17215 return Expression::make_error(location
);
17219 Expression
* elen
= Expression::make_integer_ul(size
, NULL
, location
);
17220 at
= Type::make_array_type(at
->element_type(), elen
);
17223 else if (at
->length() != NULL
17224 && !at
->length()->is_error_expression()
17225 && this->vals_
!= NULL
)
17227 Numeric_constant nc
;
17229 if (at
->length()->numeric_constant_value(&nc
)
17230 && nc
.to_unsigned_long(&val
) == Numeric_constant::NC_UL_VALID
)
17232 if (indexes
== NULL
)
17234 if (this->vals_
->size() > val
)
17236 go_error_at(location
,
17237 "too many elements in composite literal");
17238 return Expression::make_error(location
);
17243 unsigned long max
= indexes
->back();
17246 go_error_at(location
,
17247 ("some element keys in composite literal "
17248 "are out of range"));
17249 return Expression::make_error(location
);
17255 if (at
->length() != NULL
)
17256 return new Fixed_array_construction_expression(type
, indexes
, vals
,
17259 return new Slice_construction_expression(type
, indexes
, vals
, location
);
17262 // Lower a map composite literal.
17265 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
17266 Statement_inserter
* inserter
,
17269 Location location
= this->location();
17270 Unordered_map(unsigned int, std::vector
<Expression
*>) st
;
17271 Unordered_map(unsigned int, std::vector
<Expression
*>) nt
;
17272 bool saw_false
= false;
17273 bool saw_true
= false;
17274 if (this->vals_
!= NULL
)
17276 if (!this->has_keys_
)
17278 go_error_at(location
, "map composite literal must have keys");
17279 return Expression::make_error(location
);
17282 for (Expression_list::iterator p
= this->vals_
->begin();
17283 p
!= this->vals_
->end();
17289 go_error_at((*p
)->location(),
17290 ("map composite literal must "
17291 "have keys for every value"));
17292 return Expression::make_error(location
);
17294 // Make sure we have lowered the key; it may not have been
17295 // lowered in order to handle keys for struct composite
17296 // literals. Lower it now to get the right error message.
17297 if ((*p
)->unknown_expression() != NULL
)
17299 gogo
->lower_expression(function
, inserter
, &*p
);
17300 go_assert((*p
)->is_error_expression());
17301 return Expression::make_error(location
);
17303 // Check if there are duplicate constant keys.
17304 if (!(*p
)->is_constant())
17307 Numeric_constant nval
;
17309 if ((*p
)->string_constant_value(&sval
)) // Check string keys.
17311 unsigned int h
= Gogo::hash_string(sval
, 0);
17312 // Search the index h in the hash map.
17313 Unordered_map(unsigned int, std::vector
<Expression
*>)::iterator mit
;
17315 if (mit
== st
.end())
17317 // No duplicate since h is a new index.
17318 // Create a new vector indexed by h and add it to the hash map.
17319 std::vector
<Expression
*> l
;
17321 std::pair
<unsigned int, std::vector
<Expression
*> > val(h
, l
);
17326 // Do further check since index h already exists.
17327 for (std::vector
<Expression
*>::iterator lit
=
17328 mit
->second
.begin();
17329 lit
!= mit
->second
.end();
17333 bool ok
= (*lit
)->string_constant_value(&s
);
17337 go_error_at((*p
)->location(), ("duplicate key "
17338 "in map literal"));
17339 return Expression::make_error(location
);
17342 // Add this new string key to the vector indexed by h.
17343 mit
->second
.push_back(*p
);
17346 else if ((*p
)->numeric_constant_value(&nval
)) // Check numeric keys.
17348 unsigned int h
= nval
.hash(0);
17349 Unordered_map(unsigned int, std::vector
<Expression
*>)::iterator mit
;
17351 if (mit
== nt
.end())
17353 // No duplicate since h is a new code.
17354 // Create a new vector indexed by h and add it to the hash map.
17355 std::vector
<Expression
*> l
;
17357 std::pair
<unsigned int, std::vector
<Expression
*> > val(h
, l
);
17362 // Do further check since h already exists.
17363 for (std::vector
<Expression
*>::iterator lit
=
17364 mit
->second
.begin();
17365 lit
!= mit
->second
.end();
17368 Numeric_constant rval
;
17369 bool ok
= (*lit
)->numeric_constant_value(&rval
);
17371 if (nval
.equals(rval
))
17373 go_error_at((*p
)->location(),
17374 "duplicate key in map literal");
17375 return Expression::make_error(location
);
17378 // Add this new numeric key to the vector indexed by h.
17379 mit
->second
.push_back(*p
);
17382 else if ((*p
)->boolean_constant_value(&bval
))
17384 if ((bval
&& saw_true
) || (!bval
&& saw_false
))
17386 go_error_at((*p
)->location(),
17387 "duplicate key in map literal");
17388 return Expression::make_error(location
);
17398 return new Map_construction_expression(type
, this->vals_
, location
);
17404 Composite_literal_expression::do_copy()
17406 Composite_literal_expression
* ret
=
17407 new Composite_literal_expression(this->type_
->copy_expressions(),
17408 this->depth_
, this->has_keys_
,
17409 (this->vals_
== NULL
17411 : this->vals_
->copy()),
17412 this->all_are_names_
,
17414 ret
->key_path_
= this->key_path_
;
17418 // Dump ast representation for a composite literal expression.
17421 Composite_literal_expression::do_dump_expression(
17422 Ast_dump_context
* ast_dump_context
) const
17424 ast_dump_context
->ostream() << "composite(";
17425 ast_dump_context
->dump_type(this->type_
);
17426 ast_dump_context
->ostream() << ", {";
17427 ast_dump_context
->dump_expression_list(this->vals_
, this->has_keys_
);
17428 ast_dump_context
->ostream() << "})";
17431 // Make a composite literal expression.
17434 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
17435 Expression_list
* vals
, bool all_are_names
,
17438 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
17439 all_are_names
, location
);
17442 // Return whether this expression is a composite literal.
17445 Expression::is_composite_literal() const
17447 switch (this->classification_
)
17449 case EXPRESSION_COMPOSITE_LITERAL
:
17450 case EXPRESSION_STRUCT_CONSTRUCTION
:
17451 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
17452 case EXPRESSION_SLICE_CONSTRUCTION
:
17453 case EXPRESSION_MAP_CONSTRUCTION
:
17460 // Return whether this expression is a composite literal which is not
17464 Expression::is_nonconstant_composite_literal() const
17466 switch (this->classification_
)
17468 case EXPRESSION_STRUCT_CONSTRUCTION
:
17470 const Struct_construction_expression
*psce
=
17471 static_cast<const Struct_construction_expression
*>(this);
17472 return !psce
->is_constant_struct();
17474 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
17476 const Fixed_array_construction_expression
*pace
=
17477 static_cast<const Fixed_array_construction_expression
*>(this);
17478 return !pace
->is_constant_array();
17480 case EXPRESSION_SLICE_CONSTRUCTION
:
17482 const Slice_construction_expression
*pace
=
17483 static_cast<const Slice_construction_expression
*>(this);
17484 return !pace
->is_constant_array();
17486 case EXPRESSION_MAP_CONSTRUCTION
:
17493 // Return true if this is a variable or temporary_variable.
17496 Expression::is_variable() const
17498 switch (this->classification_
)
17500 case EXPRESSION_VAR_REFERENCE
:
17501 case EXPRESSION_TEMPORARY_REFERENCE
:
17502 case EXPRESSION_SET_AND_USE_TEMPORARY
:
17503 case EXPRESSION_ENCLOSED_VAR_REFERENCE
:
17510 // Return true if this is a reference to a local variable.
17513 Expression::is_local_variable() const
17515 const Var_expression
* ve
= this->var_expression();
17518 const Named_object
* no
= ve
->named_object();
17519 return (no
->is_result_variable()
17520 || (no
->is_variable() && !no
->var_value()->is_global()));
17523 // Return true if multiple evaluations are OK.
17526 Expression::is_multi_eval_safe()
17528 switch (this->classification_
)
17530 case EXPRESSION_VAR_REFERENCE
:
17532 // A variable is a simple reference if not stored in the heap.
17533 const Named_object
* no
= this->var_expression()->named_object();
17534 if (no
->is_variable())
17535 return !no
->var_value()->is_in_heap();
17536 else if (no
->is_result_variable())
17537 return !no
->result_var_value()->is_in_heap();
17542 case EXPRESSION_TEMPORARY_REFERENCE
:
17549 if (!this->is_constant())
17552 // Only numeric and boolean constants are really multi-evaluation
17553 // safe. We don't want multiple copies of string constants.
17554 Type
* type
= this->type();
17555 return type
->is_numeric_type() || type
->is_boolean_type();
17558 const Named_object
*
17559 Expression::named_constant() const
17561 if (this->classification() != EXPRESSION_CONST_REFERENCE
)
17563 const Const_expression
* ce
= static_cast<const Const_expression
*>(this);
17564 return ce
->named_object();
17567 // Class Type_guard_expression.
17572 Type_guard_expression::do_traverse(Traverse
* traverse
)
17574 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
17575 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
17576 return TRAVERSE_EXIT
;
17577 return TRAVERSE_CONTINUE
;
17581 Type_guard_expression::do_flatten(Gogo
*, Named_object
*,
17582 Statement_inserter
* inserter
)
17584 if (this->expr_
->is_error_expression()
17585 || this->expr_
->type()->is_error_type())
17587 go_assert(saw_errors());
17588 return Expression::make_error(this->location());
17591 if (!this->expr_
->is_multi_eval_safe())
17593 Temporary_statement
* temp
= Statement::make_temporary(NULL
, this->expr_
,
17595 inserter
->insert(temp
);
17597 Expression::make_temporary_reference(temp
, this->location());
17602 // Check types of a type guard expression. The expression must have
17603 // an interface type, but the actual type conversion is checked at run
17607 Type_guard_expression::do_check_types(Gogo
*)
17609 Type
* expr_type
= this->expr_
->type();
17610 if (expr_type
->interface_type() == NULL
)
17612 if (!expr_type
->is_error() && !this->type_
->is_error())
17613 this->report_error(_("type assertion only valid for interface types"));
17614 this->set_is_error();
17616 else if (this->type_
->interface_type() == NULL
)
17618 std::string reason
;
17619 if (!expr_type
->interface_type()->implements_interface(this->type_
,
17622 if (!this->type_
->is_error())
17624 if (reason
.empty())
17625 this->report_error(_("impossible type assertion: "
17626 "type does not implement interface"));
17628 go_error_at(this->location(),
17629 ("impossible type assertion: "
17630 "type does not implement interface (%s)"),
17633 this->set_is_error();
17641 Type_guard_expression::do_copy()
17643 return new Type_guard_expression(this->expr_
->copy(),
17644 this->type_
->copy_expressions(),
17648 // Return the backend representation for a type guard expression.
17651 Type_guard_expression::do_get_backend(Translate_context
* context
)
17653 Expression
* conversion
;
17654 if (this->type_
->interface_type() != NULL
)
17656 Expression::convert_interface_to_interface(this->type_
, this->expr_
,
17657 true, this->location());
17660 Expression::convert_for_assignment(context
->gogo(), this->type_
,
17661 this->expr_
, this->location());
17663 Gogo
* gogo
= context
->gogo();
17664 Btype
* bt
= this->type_
->get_backend(gogo
);
17665 Bexpression
* bexpr
= conversion
->get_backend(context
);
17666 return gogo
->backend()->convert_expression(bt
, bexpr
, this->location());
17669 // Dump ast representation for a type guard expression.
17672 Type_guard_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
17675 this->expr_
->dump_expression(ast_dump_context
);
17676 ast_dump_context
->ostream() << ".";
17677 ast_dump_context
->dump_type(this->type_
);
17680 // Make a type guard expression.
17683 Expression::make_type_guard(Expression
* expr
, Type
* type
,
17686 return new Type_guard_expression(expr
, type
, location
);
17689 // Class Heap_expression.
17691 // Return the type of the expression stored on the heap.
17694 Heap_expression::do_type()
17695 { return Type::make_pointer_type(this->expr_
->type()); }
17697 // Return the backend representation for allocating an expression on the heap.
17700 Heap_expression::do_get_backend(Translate_context
* context
)
17702 Type
* etype
= this->expr_
->type();
17703 if (this->expr_
->is_error_expression() || etype
->is_error())
17704 return context
->backend()->error_expression();
17706 Location loc
= this->location();
17707 Gogo
* gogo
= context
->gogo();
17708 Btype
* btype
= this->type()->get_backend(gogo
);
17710 Expression
* alloc
= Expression::make_allocation(etype
, loc
);
17711 if (this->allocate_on_stack_
)
17712 alloc
->allocation_expression()->set_allocate_on_stack();
17713 Bexpression
* space
= alloc
->get_backend(context
);
17716 Named_object
* fn
= context
->function();
17717 go_assert(fn
!= NULL
);
17718 Bfunction
* fndecl
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
17719 Bvariable
* space_temp
=
17720 gogo
->backend()->temporary_variable(fndecl
, context
->bblock(), btype
,
17722 Backend::variable_address_is_taken
,
17724 Btype
* expr_btype
= etype
->get_backend(gogo
);
17726 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
17728 // If this assignment needs a write barrier, call typedmemmove. We
17729 // don't do this in the write barrier pass because in some cases
17730 // backend conversion can introduce new Heap_expression values.
17732 if (!etype
->has_pointer() || this->allocate_on_stack_
)
17734 space
= gogo
->backend()->var_expression(space_temp
, loc
);
17736 gogo
->backend()->indirect_expression(expr_btype
, space
, true, loc
);
17737 assn
= gogo
->backend()->assignment_statement(fndecl
, ref
, bexpr
, loc
);
17743 gogo
->backend()->temporary_variable(fndecl
, context
->bblock(),
17745 Backend::variable_address_is_taken
,
17747 Bexpression
* btempref
= gogo
->backend()->var_expression(btemp
,
17749 space
= gogo
->backend()->var_expression(space_temp
, loc
);
17750 Type
* etype_ptr
= Type::make_pointer_type(etype
);
17751 Expression
* elhs
= Expression::make_backend(space
, etype_ptr
, loc
);
17754 if (etype
->is_direct_iface_type())
17757 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
17758 erhs
= Expression::make_backend(btempref
, etype
, loc
);
17759 erhs
= Expression::unpack_direct_iface(erhs
, loc
);
17760 erhs
= Expression::make_unsafe_cast(uintptr_type
, erhs
, loc
);
17761 call
= Runtime::make_call(Runtime::GCWRITEBARRIER
, loc
, 2,
17766 Expression
* td
= Expression::make_type_descriptor(etype
, loc
);
17767 Bexpression
* addr
=
17768 gogo
->backend()->address_expression(btempref
, loc
);
17769 erhs
= Expression::make_backend(addr
, etype_ptr
, loc
);
17770 call
= Runtime::make_call(Runtime::TYPEDMEMMOVE
, loc
, 3,
17773 Statement
* cs
= Statement::make_statement(call
, false);
17775 space
= gogo
->backend()->var_expression(space_temp
, loc
);
17777 gogo
->backend()->indirect_expression(expr_btype
, space
, true, loc
);
17778 Expression
* eref
= Expression::make_backend(ref
, etype
, loc
);
17779 btempref
= gogo
->backend()->var_expression(btemp
, loc
);
17780 erhs
= Expression::make_backend(btempref
, etype
, loc
);
17781 Statement
* as
= Statement::make_assignment(eref
, erhs
, loc
);
17783 as
= gogo
->check_write_barrier(context
->block(), as
, cs
);
17784 Bstatement
* s
= as
->get_backend(context
);
17786 assn
= gogo
->backend()->compound_statement(edecl
, s
);
17788 decl
= gogo
->backend()->compound_statement(decl
, assn
);
17789 space
= gogo
->backend()->var_expression(space_temp
, loc
);
17790 return gogo
->backend()->compound_expression(decl
, space
, loc
);
17793 // Dump ast representation for a heap expression.
17796 Heap_expression::do_dump_expression(
17797 Ast_dump_context
* ast_dump_context
) const
17799 ast_dump_context
->ostream() << "&(";
17800 ast_dump_context
->dump_expression(this->expr_
);
17801 ast_dump_context
->ostream() << ")";
17804 // Allocate an expression on the heap.
17807 Expression::make_heap_expression(Expression
* expr
, Location location
)
17809 return new Heap_expression(expr
, location
);
17812 // Class Receive_expression.
17814 // Return the type of a receive expression.
17817 Receive_expression::do_type()
17819 if (this->is_error_expression())
17820 return Type::make_error_type();
17821 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
17822 if (channel_type
== NULL
)
17824 this->report_error(_("expected channel"));
17825 return Type::make_error_type();
17827 return channel_type
->element_type();
17830 // Check types for a receive expression.
17833 Receive_expression::do_check_types(Gogo
*)
17835 Type
* type
= this->channel_
->type();
17836 if (type
->is_error())
17838 go_assert(saw_errors());
17839 this->set_is_error();
17842 if (type
->channel_type() == NULL
)
17844 this->report_error(_("expected channel"));
17847 if (!type
->channel_type()->may_receive())
17849 this->report_error(_("invalid receive on send-only channel"));
17854 // Flattening for receive expressions creates a temporary variable to store
17855 // received data in for receives.
17858 Receive_expression::do_flatten(Gogo
*, Named_object
*,
17859 Statement_inserter
* inserter
)
17861 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
17862 if (channel_type
== NULL
)
17864 go_assert(saw_errors());
17867 else if (this->channel_
->is_error_expression())
17869 go_assert(saw_errors());
17870 return Expression::make_error(this->location());
17873 Type
* element_type
= channel_type
->element_type();
17874 if (this->temp_receiver_
== NULL
)
17876 this->temp_receiver_
= Statement::make_temporary(element_type
, NULL
,
17878 this->temp_receiver_
->set_is_address_taken();
17879 inserter
->insert(this->temp_receiver_
);
17885 // Get the backend representation for a receive expression.
17888 Receive_expression::do_get_backend(Translate_context
* context
)
17890 Location loc
= this->location();
17892 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
17893 if (channel_type
== NULL
)
17895 go_assert(this->channel_
->type()->is_error());
17896 return context
->backend()->error_expression();
17899 Expression
* recv_ref
=
17900 Expression::make_temporary_reference(this->temp_receiver_
, loc
);
17901 Expression
* recv_addr
=
17902 Expression::make_temporary_reference(this->temp_receiver_
, loc
);
17903 recv_addr
= Expression::make_unary(OPERATOR_AND
, recv_addr
, loc
);
17904 Expression
* recv
= Runtime::make_call(Runtime::CHANRECV1
, loc
, 2,
17905 this->channel_
, recv_addr
);
17906 return Expression::make_compound(recv
, recv_ref
, loc
)->get_backend(context
);
17909 // Export a receive expression.
17912 Receive_expression::do_export(Export_function_body
* efb
) const
17914 efb
->write_c_string("<-");
17915 this->channel_
->export_expression(efb
);
17918 // Dump ast representation for a receive expression.
17921 Receive_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
17923 ast_dump_context
->ostream() << " <- " ;
17924 ast_dump_context
->dump_expression(channel_
);
17927 // Import a receive expression.
17930 Receive_expression::do_import(Import_expression
* imp
, Location loc
)
17932 imp
->require_c_string("<-");
17933 Expression
* expr
= Expression::import_expression(imp
, loc
);
17934 return Expression::make_receive(expr
, loc
);
17937 // Make a receive expression.
17939 Receive_expression
*
17940 Expression::make_receive(Expression
* channel
, Location location
)
17942 return new Receive_expression(channel
, location
);
17945 // An expression which evaluates to a pointer to the type descriptor
17948 class Type_descriptor_expression
: public Expression
17951 Type_descriptor_expression(Type
* type
, Location location
)
17952 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
17958 do_traverse(Traverse
*);
17962 { return Type::make_type_descriptor_ptr_type(); }
17965 do_is_static_initializer() const
17969 do_determine_type(const Type_context
*)
17977 do_get_backend(Translate_context
* context
)
17979 return this->type_
->type_descriptor_pointer(context
->gogo(),
17984 do_dump_expression(Ast_dump_context
*) const;
17987 // The type for which this is the descriptor.
17992 Type_descriptor_expression::do_traverse(Traverse
* traverse
)
17994 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
17995 return TRAVERSE_EXIT
;
17996 return TRAVERSE_CONTINUE
;
17999 // Dump ast representation for a type descriptor expression.
18002 Type_descriptor_expression::do_dump_expression(
18003 Ast_dump_context
* ast_dump_context
) const
18005 ast_dump_context
->dump_type(this->type_
);
18008 // Make a type descriptor expression.
18011 Expression::make_type_descriptor(Type
* type
, Location location
)
18013 return new Type_descriptor_expression(type
, location
);
18016 // An expression which evaluates to a pointer to the Garbage Collection symbol
18019 class GC_symbol_expression
: public Expression
18022 GC_symbol_expression(Type
* type
)
18023 : Expression(EXPRESSION_GC_SYMBOL
, Linemap::predeclared_location()),
18030 { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); }
18033 do_is_static_initializer() const
18037 do_determine_type(const Type_context
*)
18045 do_get_backend(Translate_context
* context
)
18046 { return this->type_
->gc_symbol_pointer(context
->gogo()); }
18049 do_dump_expression(Ast_dump_context
*) const;
18052 // The type which this gc symbol describes.
18056 // Dump ast representation for a gc symbol expression.
18059 GC_symbol_expression::do_dump_expression(
18060 Ast_dump_context
* ast_dump_context
) const
18062 ast_dump_context
->ostream() << "gcdata(";
18063 ast_dump_context
->dump_type(this->type_
);
18064 ast_dump_context
->ostream() << ")";
18067 // Make a gc symbol expression.
18070 Expression::make_gc_symbol(Type
* type
)
18072 return new GC_symbol_expression(type
);
18075 // An expression that evaluates to a pointer to a symbol holding the
18076 // ptrmask data of a type.
18078 class Ptrmask_symbol_expression
: public Expression
18081 Ptrmask_symbol_expression(Type
* type
)
18082 : Expression(EXPRESSION_PTRMASK_SYMBOL
, Linemap::predeclared_location()),
18089 { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); }
18092 do_is_static_initializer() const
18096 do_determine_type(const Type_context
*)
18104 do_get_backend(Translate_context
*);
18107 do_dump_expression(Ast_dump_context
*) const;
18110 // The type that this ptrmask symbol describes.
18114 // Return the ptrmask variable.
18117 Ptrmask_symbol_expression::do_get_backend(Translate_context
* context
)
18119 Gogo
* gogo
= context
->gogo();
18121 // If this type does not need a gcprog, then we can use the standard
18123 int64_t ptrsize
, ptrdata
;
18124 if (!this->type_
->needs_gcprog(gogo
, &ptrsize
, &ptrdata
))
18125 return this->type_
->gc_symbol_pointer(gogo
);
18127 // Otherwise we have to build a ptrmask variable, and return a
18130 Bvariable
* bvar
= this->type_
->gc_ptrmask_var(gogo
, ptrsize
, ptrdata
);
18131 Location bloc
= Linemap::predeclared_location();
18132 Bexpression
* bref
= gogo
->backend()->var_expression(bvar
, bloc
);
18133 Bexpression
* baddr
= gogo
->backend()->address_expression(bref
, bloc
);
18135 Type
* uint8_type
= Type::lookup_integer_type("uint8");
18136 Type
* pointer_uint8_type
= Type::make_pointer_type(uint8_type
);
18137 Btype
* ubtype
= pointer_uint8_type
->get_backend(gogo
);
18138 return gogo
->backend()->convert_expression(ubtype
, baddr
, bloc
);
18141 // Dump AST for a ptrmask symbol expression.
18144 Ptrmask_symbol_expression::do_dump_expression(
18145 Ast_dump_context
* ast_dump_context
) const
18147 ast_dump_context
->ostream() << "ptrmask(";
18148 ast_dump_context
->dump_type(this->type_
);
18149 ast_dump_context
->ostream() << ")";
18152 // Make a ptrmask symbol expression.
18155 Expression::make_ptrmask_symbol(Type
* type
)
18157 return new Ptrmask_symbol_expression(type
);
18160 // An expression which evaluates to some characteristic of a type.
18161 // This is only used to initialize fields of a type descriptor. Using
18162 // a new expression class is slightly inefficient but gives us a good
18163 // separation between the frontend and the middle-end with regard to
18164 // how types are laid out.
18166 class Type_info_expression
: public Expression
18169 Type_info_expression(Type
* type
, Type_info type_info
)
18170 : Expression(EXPRESSION_TYPE_INFO
, Linemap::predeclared_location()),
18171 type_(type
), type_info_(type_info
)
18176 do_is_static_initializer() const
18183 do_determine_type(const Type_context
*)
18191 do_get_backend(Translate_context
* context
);
18194 do_dump_expression(Ast_dump_context
*) const;
18197 // The type for which we are getting information.
18199 // What information we want.
18200 Type_info type_info_
;
18203 // The type is chosen to match what the type descriptor struct
18207 Type_info_expression::do_type()
18209 switch (this->type_info_
)
18211 case TYPE_INFO_SIZE
:
18212 case TYPE_INFO_BACKEND_PTRDATA
:
18213 case TYPE_INFO_DESCRIPTOR_PTRDATA
:
18214 return Type::lookup_integer_type("uintptr");
18215 case TYPE_INFO_ALIGNMENT
:
18216 case TYPE_INFO_FIELD_ALIGNMENT
:
18217 return Type::lookup_integer_type("uint8");
18223 // Return the backend representation for type information.
18226 Type_info_expression::do_get_backend(Translate_context
* context
)
18228 Gogo
* gogo
= context
->gogo();
18231 switch (this->type_info_
)
18233 case TYPE_INFO_SIZE
:
18234 ok
= this->type_
->backend_type_size(gogo
, &val
);
18236 case TYPE_INFO_ALIGNMENT
:
18237 ok
= this->type_
->backend_type_align(gogo
, &val
);
18239 case TYPE_INFO_FIELD_ALIGNMENT
:
18240 ok
= this->type_
->backend_type_field_align(gogo
, &val
);
18242 case TYPE_INFO_BACKEND_PTRDATA
:
18243 ok
= this->type_
->backend_type_ptrdata(gogo
, &val
);
18245 case TYPE_INFO_DESCRIPTOR_PTRDATA
:
18246 ok
= this->type_
->descriptor_ptrdata(gogo
, &val
);
18253 go_assert(saw_errors());
18254 return gogo
->backend()->error_expression();
18256 Expression
* e
= Expression::make_integer_int64(val
, this->type(),
18258 return e
->get_backend(context
);
18261 // Dump ast representation for a type info expression.
18264 Type_info_expression::do_dump_expression(
18265 Ast_dump_context
* ast_dump_context
) const
18267 ast_dump_context
->ostream() << "typeinfo(";
18268 ast_dump_context
->dump_type(this->type_
);
18269 ast_dump_context
->ostream() << ",";
18270 ast_dump_context
->ostream() <<
18271 (this->type_info_
== TYPE_INFO_ALIGNMENT
? "alignment"
18272 : this->type_info_
== TYPE_INFO_FIELD_ALIGNMENT
? "field alignment"
18273 : this->type_info_
== TYPE_INFO_SIZE
? "size"
18274 : this->type_info_
== TYPE_INFO_BACKEND_PTRDATA
? "backend_ptrdata"
18275 : this->type_info_
== TYPE_INFO_DESCRIPTOR_PTRDATA
? "descriptor_ptrdata"
18277 ast_dump_context
->ostream() << ")";
18280 // Make a type info expression.
18283 Expression::make_type_info(Type
* type
, Type_info type_info
)
18285 return new Type_info_expression(type
, type_info
);
18288 // Slice_info_expression.
18290 // Return the type of the slice info.
18293 Slice_info_expression::do_type()
18295 switch (this->slice_info_
)
18297 case SLICE_INFO_VALUE_POINTER
:
18298 return Type::make_pointer_type(
18299 this->slice_
->type()->array_type()->element_type());
18300 case SLICE_INFO_LENGTH
:
18301 case SLICE_INFO_CAPACITY
:
18302 return Type::lookup_integer_type("int");
18308 // Return the backend information for slice information.
18311 Slice_info_expression::do_get_backend(Translate_context
* context
)
18313 Gogo
* gogo
= context
->gogo();
18314 Bexpression
* bslice
= this->slice_
->get_backend(context
);
18315 switch (this->slice_info_
)
18317 case SLICE_INFO_VALUE_POINTER
:
18318 case SLICE_INFO_LENGTH
:
18319 case SLICE_INFO_CAPACITY
:
18320 return gogo
->backend()->struct_field_expression(bslice
, this->slice_info_
,
18328 // Dump ast representation for a type info expression.
18331 Slice_info_expression::do_dump_expression(
18332 Ast_dump_context
* ast_dump_context
) const
18334 ast_dump_context
->ostream() << "sliceinfo(";
18335 this->slice_
->dump_expression(ast_dump_context
);
18336 ast_dump_context
->ostream() << ",";
18337 ast_dump_context
->ostream() <<
18338 (this->slice_info_
== SLICE_INFO_VALUE_POINTER
? "values"
18339 : this->slice_info_
== SLICE_INFO_LENGTH
? "length"
18340 : this->slice_info_
== SLICE_INFO_CAPACITY
? "capacity "
18342 ast_dump_context
->ostream() << ")";
18345 // Make a slice info expression.
18348 Expression::make_slice_info(Expression
* slice
, Slice_info slice_info
,
18351 return new Slice_info_expression(slice
, slice_info
, location
);
18354 // Class Slice_value_expression.
18357 Slice_value_expression::do_traverse(Traverse
* traverse
)
18359 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
18360 || Expression::traverse(&this->valmem_
, traverse
) == TRAVERSE_EXIT
18361 || Expression::traverse(&this->len_
, traverse
) == TRAVERSE_EXIT
18362 || Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
)
18363 return TRAVERSE_EXIT
;
18364 return TRAVERSE_CONTINUE
;
18368 Slice_value_expression::do_copy()
18370 return new Slice_value_expression(this->type_
->copy_expressions(),
18371 this->valmem_
->copy(),
18372 this->len_
->copy(), this->cap_
->copy(),
18377 Slice_value_expression::do_get_backend(Translate_context
* context
)
18379 std::vector
<Bexpression
*> vals(3);
18380 vals
[0] = this->valmem_
->get_backend(context
);
18381 vals
[1] = this->len_
->get_backend(context
);
18382 vals
[2] = this->cap_
->get_backend(context
);
18384 Gogo
* gogo
= context
->gogo();
18385 Btype
* btype
= this->type_
->get_backend(gogo
);
18386 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
18390 Slice_value_expression::do_dump_expression(
18391 Ast_dump_context
* ast_dump_context
) const
18393 ast_dump_context
->ostream() << "slicevalue(";
18394 ast_dump_context
->ostream() << "values: ";
18395 this->valmem_
->dump_expression(ast_dump_context
);
18396 ast_dump_context
->ostream() << ", length: ";
18397 this->len_
->dump_expression(ast_dump_context
);
18398 ast_dump_context
->ostream() << ", capacity: ";
18399 this->cap_
->dump_expression(ast_dump_context
);
18400 ast_dump_context
->ostream() << ")";
18404 Expression::make_slice_value(Type
* at
, Expression
* valmem
, Expression
* len
,
18405 Expression
* cap
, Location location
)
18407 go_assert(at
->is_slice_type());
18408 go_assert(valmem
->is_nil_expression()
18409 || (at
->array_type()->element_type()
18410 == valmem
->type()->points_to()));
18411 return new Slice_value_expression(at
, valmem
, len
, cap
, location
);
18414 // Look through the expression of a Slice_value_expression's valmem to
18415 // find an call to makeslice. If found, return the call expression and
18416 // the containing temporary statement (if any).
18418 std::pair
<Call_expression
*, Temporary_statement
*>
18419 Expression::find_makeslice_call(Expression
* expr
)
18421 Unsafe_type_conversion_expression
* utce
=
18422 expr
->unsafe_conversion_expression();
18424 expr
= utce
->expr();
18426 Slice_value_expression
* sve
= expr
->slice_value_expression();
18428 return std::make_pair
<Call_expression
*, Temporary_statement
*>(NULL
, NULL
);
18429 expr
= sve
->valmem();
18431 utce
= expr
->unsafe_conversion_expression();
18433 expr
= utce
->expr();
18435 Temporary_reference_expression
* tre
= expr
->temporary_reference_expression();
18436 Temporary_statement
* ts
= (tre
!= NULL
? tre
->statement() : NULL
);
18437 if (ts
!= NULL
&& ts
->init() != NULL
&& !ts
->assigned()
18438 && !ts
->is_address_taken())
18441 Call_expression
* call
= expr
->call_expression();
18443 return std::make_pair
<Call_expression
*, Temporary_statement
*>(NULL
, NULL
);
18445 Func_expression
* fe
= call
->fn()->func_expression();
18447 && fe
->runtime_code() == Runtime::MAKESLICE
)
18448 return std::make_pair(call
, ts
);
18450 return std::make_pair
<Call_expression
*, Temporary_statement
*>(NULL
, NULL
);
18453 // An expression that evaluates to some characteristic of a non-empty interface.
18454 // This is used to access the method table or underlying object of an interface.
18456 class Interface_info_expression
: public Expression
18459 Interface_info_expression(Expression
* iface
, Interface_info iface_info
,
18461 : Expression(EXPRESSION_INTERFACE_INFO
, location
),
18462 iface_(iface
), iface_info_(iface_info
)
18470 do_determine_type(const Type_context
*)
18476 return new Interface_info_expression(this->iface_
->copy(),
18477 this->iface_info_
, this->location());
18481 do_get_backend(Translate_context
* context
);
18484 do_dump_expression(Ast_dump_context
*) const;
18487 do_issue_nil_check()
18488 { this->iface_
->issue_nil_check(); }
18491 // The interface for which we are getting information.
18492 Expression
* iface_
;
18493 // What information we want.
18494 Interface_info iface_info_
;
18497 // Return the type of the interface info.
18500 Interface_info_expression::do_type()
18502 switch (this->iface_info_
)
18504 case INTERFACE_INFO_METHODS
:
18506 typedef Unordered_map(Interface_type
*, Type
*) Hashtable
;
18507 static Hashtable result_types
;
18509 Interface_type
* itype
= this->iface_
->type()->interface_type();
18511 Hashtable::const_iterator pr
= result_types
.find(itype
);
18512 if (pr
!= result_types
.end())
18515 Type
* pdt
= Type::make_type_descriptor_ptr_type();
18516 if (itype
->is_empty())
18518 result_types
[itype
] = pdt
;
18522 Location loc
= this->location();
18523 Struct_field_list
* sfl
= new Struct_field_list();
18525 Struct_field(Typed_identifier("__type_descriptor", pdt
, loc
)));
18527 for (Typed_identifier_list::const_iterator p
= itype
->methods()->begin();
18528 p
!= itype
->methods()->end();
18531 Function_type
* ft
= p
->type()->function_type();
18532 go_assert(ft
->receiver() == NULL
);
18534 const Typed_identifier_list
* params
= ft
->parameters();
18535 Typed_identifier_list
* mparams
= new Typed_identifier_list();
18536 if (params
!= NULL
)
18537 mparams
->reserve(params
->size() + 1);
18538 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
18539 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
18540 if (params
!= NULL
)
18542 for (Typed_identifier_list::const_iterator pp
= params
->begin();
18543 pp
!= params
->end();
18545 mparams
->push_back(*pp
);
18548 Typed_identifier_list
* mresults
= (ft
->results() == NULL
18550 : ft
->results()->copy());
18551 Backend_function_type
* mft
=
18552 Type::make_backend_function_type(NULL
, mparams
, mresults
,
18555 std::string fname
= Gogo::unpack_hidden_name(p
->name());
18556 sfl
->push_back(Struct_field(Typed_identifier(fname
, mft
, loc
)));
18559 Struct_type
* st
= Type::make_struct_type(sfl
, loc
);
18560 st
->set_is_struct_incomparable();
18561 Pointer_type
*pt
= Type::make_pointer_type(st
);
18562 result_types
[itype
] = pt
;
18565 case INTERFACE_INFO_OBJECT
:
18566 return Type::make_pointer_type(Type::make_void_type());
18572 // Return the backend representation for interface information.
18575 Interface_info_expression::do_get_backend(Translate_context
* context
)
18577 Gogo
* gogo
= context
->gogo();
18578 Bexpression
* biface
= this->iface_
->get_backend(context
);
18579 switch (this->iface_info_
)
18581 case INTERFACE_INFO_METHODS
:
18582 case INTERFACE_INFO_OBJECT
:
18583 return gogo
->backend()->struct_field_expression(biface
, this->iface_info_
,
18591 // Dump ast representation for an interface info expression.
18594 Interface_info_expression::do_dump_expression(
18595 Ast_dump_context
* ast_dump_context
) const
18597 bool is_empty
= this->iface_
->type()->interface_type()->is_empty();
18598 ast_dump_context
->ostream() << "interfaceinfo(";
18599 this->iface_
->dump_expression(ast_dump_context
);
18600 ast_dump_context
->ostream() << ",";
18601 ast_dump_context
->ostream() <<
18602 (this->iface_info_
== INTERFACE_INFO_METHODS
&& !is_empty
? "methods"
18603 : this->iface_info_
== INTERFACE_INFO_TYPE_DESCRIPTOR
? "type_descriptor"
18604 : this->iface_info_
== INTERFACE_INFO_OBJECT
? "object"
18606 ast_dump_context
->ostream() << ")";
18609 // Make an interface info expression.
18612 Expression::make_interface_info(Expression
* iface
, Interface_info iface_info
,
18615 return new Interface_info_expression(iface
, iface_info
, location
);
18618 // An expression that represents an interface value. The first field is either
18619 // a type descriptor for an empty interface or a pointer to the interface method
18620 // table for a non-empty interface. The second field is always the object.
18622 class Interface_value_expression
: public Expression
18625 Interface_value_expression(Type
* type
, Expression
* first_field
,
18626 Expression
* obj
, Location location
)
18627 : Expression(EXPRESSION_INTERFACE_VALUE
, location
),
18628 type_(type
), first_field_(first_field
), obj_(obj
)
18633 do_traverse(Traverse
*);
18637 { return this->type_
; }
18640 do_determine_type(const Type_context
*)
18641 { go_unreachable(); }
18646 return new Interface_value_expression(this->type_
->copy_expressions(),
18647 this->first_field_
->copy(),
18648 this->obj_
->copy(), this->location());
18652 do_get_backend(Translate_context
* context
);
18655 do_dump_expression(Ast_dump_context
*) const;
18658 // The type of the interface value.
18660 // The first field of the interface (either a type descriptor or a pointer
18661 // to the method table.
18662 Expression
* first_field_
;
18663 // The underlying object of the interface.
18668 Interface_value_expression::do_traverse(Traverse
* traverse
)
18670 if (Expression::traverse(&this->first_field_
, traverse
) == TRAVERSE_EXIT
18671 || Expression::traverse(&this->obj_
, traverse
) == TRAVERSE_EXIT
)
18672 return TRAVERSE_EXIT
;
18673 return TRAVERSE_CONTINUE
;
18677 Interface_value_expression::do_get_backend(Translate_context
* context
)
18679 std::vector
<Bexpression
*> vals(2);
18680 vals
[0] = this->first_field_
->get_backend(context
);
18681 vals
[1] = this->obj_
->get_backend(context
);
18683 Gogo
* gogo
= context
->gogo();
18684 Btype
* btype
= this->type_
->get_backend(gogo
);
18685 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
18689 Interface_value_expression::do_dump_expression(
18690 Ast_dump_context
* ast_dump_context
) const
18692 ast_dump_context
->ostream() << "interfacevalue(";
18693 ast_dump_context
->ostream() <<
18694 (this->type_
->interface_type()->is_empty()
18695 ? "type_descriptor: "
18697 this->first_field_
->dump_expression(ast_dump_context
);
18698 ast_dump_context
->ostream() << ", object: ";
18699 this->obj_
->dump_expression(ast_dump_context
);
18700 ast_dump_context
->ostream() << ")";
18704 Expression::make_interface_value(Type
* type
, Expression
* first_value
,
18705 Expression
* object
, Location location
)
18707 return new Interface_value_expression(type
, first_value
, object
, location
);
18710 // An interface method table for a pair of types: an interface type and a type
18711 // that implements that interface.
18713 class Interface_mtable_expression
: public Expression
18716 Interface_mtable_expression(Interface_type
* itype
, Type
* type
,
18717 bool is_pointer
, Location location
)
18718 : Expression(EXPRESSION_INTERFACE_MTABLE
, location
),
18719 itype_(itype
), type_(type
), is_pointer_(is_pointer
),
18720 method_table_type_(NULL
), bvar_(NULL
)
18725 do_traverse(Traverse
*);
18731 do_is_static_initializer() const
18735 do_determine_type(const Type_context
*)
18736 { go_unreachable(); }
18741 Interface_type
* itype
= this->itype_
->copy_expressions()->interface_type();
18742 return new Interface_mtable_expression(itype
,
18743 this->type_
->copy_expressions(),
18744 this->is_pointer_
, this->location());
18748 do_is_addressable() const
18752 do_get_backend(Translate_context
* context
);
18755 do_dump_expression(Ast_dump_context
*) const;
18758 // The interface type for which the methods are defined.
18759 Interface_type
* itype_
;
18760 // The type to construct the interface method table for.
18762 // Whether this table contains the method set for the receiver type or the
18763 // pointer receiver type.
18765 // The type of the method table.
18766 Type
* method_table_type_
;
18767 // The backend variable that refers to the interface method table.
18772 Interface_mtable_expression::do_traverse(Traverse
* traverse
)
18774 if (Type::traverse(this->itype_
, traverse
) == TRAVERSE_EXIT
18775 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
18776 return TRAVERSE_EXIT
;
18777 return TRAVERSE_CONTINUE
;
18781 Interface_mtable_expression::do_type()
18783 if (this->method_table_type_
!= NULL
)
18784 return this->method_table_type_
;
18786 const Typed_identifier_list
* interface_methods
= this->itype_
->methods();
18787 go_assert(!interface_methods
->empty());
18789 Struct_field_list
* sfl
= new Struct_field_list
;
18790 Typed_identifier
tid("__type_descriptor", Type::make_type_descriptor_ptr_type(),
18792 sfl
->push_back(Struct_field(tid
));
18793 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
18794 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
18795 p
!= interface_methods
->end();
18798 // We want C function pointers here, not func descriptors; model
18799 // using void* pointers.
18800 Typed_identifier
method(p
->name(), unsafe_ptr_type
, p
->location());
18801 sfl
->push_back(Struct_field(method
));
18803 Struct_type
* st
= Type::make_struct_type(sfl
, this->location());
18804 st
->set_is_struct_incomparable();
18805 this->method_table_type_
= st
;
18806 return this->method_table_type_
;
18810 Interface_mtable_expression::do_get_backend(Translate_context
* context
)
18812 Gogo
* gogo
= context
->gogo();
18813 Location loc
= Linemap::predeclared_location();
18814 if (this->bvar_
!= NULL
)
18815 return gogo
->backend()->var_expression(this->bvar_
, this->location());
18817 const Typed_identifier_list
* interface_methods
= this->itype_
->methods();
18818 go_assert(!interface_methods
->empty());
18820 std::string mangled_name
=
18821 gogo
->interface_method_table_name(this->itype_
, this->type_
,
18822 this->is_pointer_
);
18824 // Set is_public if we are converting a named type to an interface
18825 // type that is defined in the same package as the named type, and
18826 // the interface has hidden methods. In that case the interface
18827 // method table will be defined by the package that defines the
18829 bool is_public
= false;
18830 if (this->type_
->named_type() != NULL
18831 && (this->type_
->named_type()->named_object()->package()
18832 == this->itype_
->package()))
18834 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
18835 p
!= interface_methods
->end();
18838 if (Gogo::is_hidden_name(p
->name()))
18847 && this->type_
->named_type()->named_object()->package() != NULL
)
18849 // The interface conversion table is defined elsewhere.
18850 Btype
* btype
= this->type()->get_backend(gogo
);
18852 gogo
->backend()->immutable_struct_reference(mangled_name
, "",
18854 return gogo
->backend()->var_expression(this->bvar_
, this->location());
18857 // The first element is the type descriptor.
18859 if (!this->is_pointer_
)
18860 td_type
= this->type_
;
18862 td_type
= Type::make_pointer_type(this->type_
);
18864 std::vector
<Backend::Btyped_identifier
> bstructfields
;
18866 // Build an interface method table for a type: a type descriptor followed by a
18867 // list of function pointers, one for each interface method. This is used for
18869 Expression_list
* svals
= new Expression_list();
18870 Expression
* tdescriptor
= Expression::make_type_descriptor(td_type
, loc
);
18871 svals
->push_back(tdescriptor
);
18873 Btype
* tdesc_btype
= tdescriptor
->type()->get_backend(gogo
);
18874 Backend::Btyped_identifier
btd("_type", tdesc_btype
, loc
);
18875 bstructfields
.push_back(btd
);
18877 Named_type
* nt
= this->type_
->named_type();
18878 Struct_type
* st
= this->type_
->struct_type();
18879 go_assert(nt
!= NULL
|| st
!= NULL
);
18881 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
18882 p
!= interface_methods
->end();
18888 m
= nt
->method_function(p
->name(), &is_ambiguous
);
18890 m
= st
->method_function(p
->name(), &is_ambiguous
);
18891 go_assert(m
!= NULL
);
18893 // See the comment in Type::method_constructor.
18894 bool use_direct_iface_stub
= false;
18895 if (m
->is_value_method()
18896 && this->is_pointer_
18897 && this->type_
->is_direct_iface_type())
18898 use_direct_iface_stub
= true;
18899 if (!m
->is_value_method()
18900 && this->is_pointer_
18901 && !this->type_
->in_heap())
18902 use_direct_iface_stub
= true;
18903 Named_object
* no
= (use_direct_iface_stub
18904 ? m
->iface_stub_object()
18905 : m
->named_object());
18907 go_assert(no
->is_function() || no
->is_function_declaration());
18909 Function_type
* fcn_type
= (no
->is_function()
18910 ? no
->func_value()->type()
18911 : no
->func_declaration_value()->type());
18912 Btype
* fcn_btype
= fcn_type
->get_backend_fntype(gogo
);
18913 Backend::Btyped_identifier
bmtype(p
->name(), fcn_btype
, loc
);
18914 bstructfields
.push_back(bmtype
);
18916 svals
->push_back(Expression::make_func_code_reference(no
, loc
));
18919 Btype
*btype
= gogo
->backend()->struct_type(bstructfields
);
18920 std::vector
<Bexpression
*> ctor_bexprs
;
18921 for (Expression_list::const_iterator pe
= svals
->begin();
18922 pe
!= svals
->end();
18925 ctor_bexprs
.push_back((*pe
)->get_backend(context
));
18927 Bexpression
* ctor
=
18928 gogo
->backend()->constructor_expression(btype
, ctor_bexprs
, loc
);
18930 unsigned int flags
= 0;
18932 flags
|= Backend::variable_is_hidden
;
18933 this->bvar_
= gogo
->backend()->immutable_struct(mangled_name
, "", flags
,
18935 gogo
->backend()->immutable_struct_set_init(this->bvar_
, mangled_name
, flags
,
18937 return gogo
->backend()->var_expression(this->bvar_
, loc
);
18941 Interface_mtable_expression::do_dump_expression(
18942 Ast_dump_context
* ast_dump_context
) const
18944 ast_dump_context
->ostream() << "__go_"
18945 << (this->is_pointer_
? "pimt__" : "imt_");
18946 ast_dump_context
->dump_type(this->itype_
);
18947 ast_dump_context
->ostream() << "__";
18948 ast_dump_context
->dump_type(this->type_
);
18952 Expression::make_interface_mtable_ref(Interface_type
* itype
, Type
* type
,
18953 bool is_pointer
, Location location
)
18955 return new Interface_mtable_expression(itype
, type
, is_pointer
, location
);
18958 // An expression which evaluates to the offset of a field within a
18959 // struct. This, like Type_info_expression, q.v., is only used to
18960 // initialize fields of a type descriptor.
18962 class Struct_field_offset_expression
: public Expression
18965 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
18966 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
,
18967 Linemap::predeclared_location()),
18968 type_(type
), field_(field
)
18973 do_is_static_initializer() const
18978 { return Type::lookup_integer_type("uintptr"); }
18981 do_determine_type(const Type_context
*)
18989 do_get_backend(Translate_context
* context
);
18992 do_dump_expression(Ast_dump_context
*) const;
18995 // The type of the struct.
18996 Struct_type
* type_
;
18998 const Struct_field
* field_
;
19001 // Return the backend representation for a struct field offset.
19004 Struct_field_offset_expression::do_get_backend(Translate_context
* context
)
19006 const Struct_field_list
* fields
= this->type_
->fields();
19007 Struct_field_list::const_iterator p
;
19009 for (p
= fields
->begin();
19010 p
!= fields
->end();
19012 if (&*p
== this->field_
)
19014 go_assert(&*p
== this->field_
);
19016 Gogo
* gogo
= context
->gogo();
19017 Btype
* btype
= this->type_
->get_backend(gogo
);
19019 int64_t offset
= gogo
->backend()->type_field_offset(btype
, i
);
19020 Type
* uptr_type
= Type::lookup_integer_type("uintptr");
19022 Expression::make_integer_int64(offset
, uptr_type
,
19023 Linemap::predeclared_location());
19024 return ret
->get_backend(context
);
19027 // Dump ast representation for a struct field offset expression.
19030 Struct_field_offset_expression::do_dump_expression(
19031 Ast_dump_context
* ast_dump_context
) const
19033 ast_dump_context
->ostream() << "unsafe.Offsetof(";
19034 ast_dump_context
->dump_type(this->type_
);
19035 ast_dump_context
->ostream() << '.';
19036 ast_dump_context
->ostream() <<
19037 Gogo::message_name(this->field_
->field_name());
19038 ast_dump_context
->ostream() << ")";
19041 // Make an expression for a struct field offset.
19044 Expression::make_struct_field_offset(Struct_type
* type
,
19045 const Struct_field
* field
)
19047 return new Struct_field_offset_expression(type
, field
);
19050 // An expression which evaluates to the address of an unnamed label.
19052 class Label_addr_expression
: public Expression
19055 Label_addr_expression(Label
* label
, Location location
)
19056 : Expression(EXPRESSION_LABEL_ADDR
, location
),
19063 { return Type::make_pointer_type(Type::make_void_type()); }
19066 do_determine_type(const Type_context
*)
19071 { return new Label_addr_expression(this->label_
, this->location()); }
19074 do_get_backend(Translate_context
* context
)
19075 { return this->label_
->get_addr(context
, this->location()); }
19078 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
19079 { ast_dump_context
->ostream() << this->label_
->name(); }
19082 // The label whose address we are taking.
19086 // Make an expression for the address of an unnamed label.
19089 Expression::make_label_addr(Label
* label
, Location location
)
19091 return new Label_addr_expression(label
, location
);
19094 // Class Conditional_expression.
19099 Conditional_expression::do_traverse(Traverse
* traverse
)
19101 if (Expression::traverse(&this->cond_
, traverse
) == TRAVERSE_EXIT
19102 || Expression::traverse(&this->then_
, traverse
) == TRAVERSE_EXIT
19103 || Expression::traverse(&this->else_
, traverse
) == TRAVERSE_EXIT
)
19104 return TRAVERSE_EXIT
;
19105 return TRAVERSE_CONTINUE
;
19108 // Return the type of the conditional expression.
19111 Conditional_expression::do_type()
19113 Type
* result_type
= Type::make_void_type();
19114 if (Type::are_identical(this->then_
->type(), this->else_
->type(),
19115 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
19117 result_type
= this->then_
->type();
19118 else if (this->then_
->is_nil_expression()
19119 || this->else_
->is_nil_expression())
19120 result_type
= (!this->then_
->is_nil_expression()
19121 ? this->then_
->type()
19122 : this->else_
->type());
19123 return result_type
;
19126 // Determine type for a conditional expression.
19129 Conditional_expression::do_determine_type(const Type_context
* context
)
19131 this->cond_
->determine_type_no_context();
19132 this->then_
->determine_type(context
);
19133 this->else_
->determine_type(context
);
19136 // Get the backend representation of a conditional expression.
19139 Conditional_expression::do_get_backend(Translate_context
* context
)
19141 Gogo
* gogo
= context
->gogo();
19142 Btype
* result_btype
= this->type()->get_backend(gogo
);
19143 Bexpression
* cond
= this->cond_
->get_backend(context
);
19144 Bexpression
* then
= this->then_
->get_backend(context
);
19145 Bexpression
* belse
= this->else_
->get_backend(context
);
19146 Bfunction
* bfn
= context
->function()->func_value()->get_decl();
19147 return gogo
->backend()->conditional_expression(bfn
, result_btype
, cond
, then
,
19148 belse
, this->location());
19151 // Dump ast representation of a conditional expression.
19154 Conditional_expression::do_dump_expression(
19155 Ast_dump_context
* ast_dump_context
) const
19157 ast_dump_context
->ostream() << "(";
19158 ast_dump_context
->dump_expression(this->cond_
);
19159 ast_dump_context
->ostream() << " ? ";
19160 ast_dump_context
->dump_expression(this->then_
);
19161 ast_dump_context
->ostream() << " : ";
19162 ast_dump_context
->dump_expression(this->else_
);
19163 ast_dump_context
->ostream() << ") ";
19166 // Make a conditional expression.
19169 Expression::make_conditional(Expression
* cond
, Expression
* then
,
19170 Expression
* else_expr
, Location location
)
19172 return new Conditional_expression(cond
, then
, else_expr
, location
);
19175 // Class Compound_expression.
19180 Compound_expression::do_traverse(Traverse
* traverse
)
19182 if (Expression::traverse(&this->init_
, traverse
) == TRAVERSE_EXIT
19183 || Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
19184 return TRAVERSE_EXIT
;
19185 return TRAVERSE_CONTINUE
;
19188 // Return the type of the compound expression.
19191 Compound_expression::do_type()
19193 return this->expr_
->type();
19196 // Determine type for a compound expression.
19199 Compound_expression::do_determine_type(const Type_context
* context
)
19201 this->init_
->determine_type_no_context();
19202 this->expr_
->determine_type(context
);
19205 // Get the backend representation of a compound expression.
19208 Compound_expression::do_get_backend(Translate_context
* context
)
19210 Gogo
* gogo
= context
->gogo();
19211 Bexpression
* binit
= this->init_
->get_backend(context
);
19212 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
19213 Bstatement
* init_stmt
= gogo
->backend()->expression_statement(bfunction
,
19215 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
19216 return gogo
->backend()->compound_expression(init_stmt
, bexpr
,
19220 // Dump ast representation of a conditional expression.
19223 Compound_expression::do_dump_expression(
19224 Ast_dump_context
* ast_dump_context
) const
19226 ast_dump_context
->ostream() << "(";
19227 ast_dump_context
->dump_expression(this->init_
);
19228 ast_dump_context
->ostream() << ",";
19229 ast_dump_context
->dump_expression(this->expr_
);
19230 ast_dump_context
->ostream() << ") ";
19233 // Make a compound expression.
19236 Expression::make_compound(Expression
* init
, Expression
* expr
, Location location
)
19238 return new Compound_expression(init
, expr
, location
);
19241 // Class Backend_expression.
19244 Backend_expression::do_traverse(Traverse
*)
19246 return TRAVERSE_CONTINUE
;
19250 Backend_expression::do_copy()
19252 return new Backend_expression(this->bexpr_
, this->type_
->copy_expressions(),
19257 Backend_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
19259 ast_dump_context
->ostream() << "backend_expression<";
19260 ast_dump_context
->dump_type(this->type_
);
19261 ast_dump_context
->ostream() << ">";
19265 Expression::make_backend(Bexpression
* bexpr
, Type
* type
, Location location
)
19267 return new Backend_expression(bexpr
, type
, location
);
19270 // Import an expression. This comes at the end in order to see the
19271 // various class definitions.
19274 Expression::import_expression(Import_expression
* imp
, Location loc
)
19276 Expression
* expr
= Expression::import_expression_without_suffix(imp
, loc
);
19279 if (imp
->match_c_string("("))
19282 Expression_list
* args
= new Expression_list();
19283 bool is_varargs
= false;
19284 while (!imp
->match_c_string(")"))
19286 Expression
* arg
= Expression::import_expression(imp
, loc
);
19287 if (arg
->is_error_expression())
19289 args
->push_back(arg
);
19290 if (imp
->match_c_string(")"))
19292 else if (imp
->match_c_string("...)"))
19298 imp
->require_c_string(", ");
19300 imp
->require_c_string(")");
19301 expr
= Expression::make_call(expr
, args
, is_varargs
, loc
);
19302 expr
->call_expression()->set_varargs_are_lowered();
19304 else if (imp
->match_c_string("["))
19307 Expression
* start
= Expression::import_expression(imp
, loc
);
19308 Expression
* end
= NULL
;
19309 Expression
* cap
= NULL
;
19310 if (imp
->match_c_string(":"))
19313 int c
= imp
->peek_char();
19314 if (c
== ':' || c
== ']')
19315 end
= Expression::make_nil(loc
);
19317 end
= Expression::import_expression(imp
, loc
);
19318 if (imp
->match_c_string(":"))
19321 cap
= Expression::import_expression(imp
, loc
);
19324 imp
->require_c_string("]");
19325 expr
= Expression::make_index(expr
, start
, end
, cap
, loc
);
19334 // Import an expression without considering a suffix (function
19335 // arguments, index operations, etc.).
19338 Expression::import_expression_without_suffix(Import_expression
* imp
,
19341 int c
= imp
->peek_char();
19342 if (c
== '+' || c
== '-' || c
== '!' || c
== '^' || c
== '&' || c
== '*')
19343 return Unary_expression::do_import(imp
, loc
);
19345 return Binary_expression::do_import(imp
, loc
);
19346 else if (imp
->match_c_string("$true")
19347 || imp
->match_c_string("$false")
19348 || (imp
->version() < EXPORT_FORMAT_V3
19349 && (imp
->match_c_string("true")
19350 || imp
->match_c_string("false"))))
19351 return Boolean_expression::do_import(imp
, loc
);
19353 return String_expression::do_import(imp
, loc
);
19354 else if (c
== '-' || (c
>= '0' && c
<= '9'))
19356 // This handles integers, floats and complex constants.
19357 return Integer_expression::do_import(imp
, loc
);
19359 else if (imp
->match_c_string("<-"))
19360 return Receive_expression::do_import(imp
, loc
);
19361 else if (imp
->match_c_string("$nil")
19362 || (imp
->version() < EXPORT_FORMAT_V3
19363 && imp
->match_c_string("nil")))
19364 return Nil_expression::do_import(imp
, loc
);
19365 else if (imp
->match_c_string("$convert")
19366 || (imp
->version() < EXPORT_FORMAT_V3
19367 && imp
->match_c_string("convert")))
19368 return Type_conversion_expression::do_import(imp
, loc
);
19370 Import_function_body
* ifb
= imp
->ifb();
19373 go_error_at(imp
->location(), "import error: expected expression");
19374 return Expression::make_error(loc
);
19376 if (ifb
->saw_error())
19377 return Expression::make_error(loc
);
19379 if (ifb
->match_c_string("$t"))
19380 return Temporary_reference_expression::do_import(ifb
, loc
);
19382 return Expression::import_identifier(ifb
, loc
);
19385 // Import an identifier in an expression. This is a reference to a
19386 // variable or function.
19389 Expression::import_identifier(Import_function_body
* ifb
, Location loc
)
19394 if (!Import::read_qualified_identifier(ifb
, &id
, &pkg
, &is_exported
))
19396 if (!ifb
->saw_error())
19397 go_error_at(ifb
->location(),
19398 "import error for %qs: bad qualified identifier at %lu",
19399 ifb
->name().c_str(),
19400 static_cast<unsigned long>(ifb
->off()));
19401 ifb
->set_saw_error();
19402 return Expression::make_error(loc
);
19405 Named_object
* no
= NULL
;
19406 if (pkg
== NULL
&& is_exported
)
19407 no
= ifb
->block()->bindings()->lookup(id
);
19410 const Package
* ipkg
= pkg
;
19412 ipkg
= ifb
->function()->package();
19414 id
= '.' + ipkg
->pkgpath() + '.' + id
;
19415 no
= ipkg
->bindings()->lookup(id
);
19418 no
= ifb
->gogo()->lookup_global(id
.c_str());
19422 if (!ifb
->saw_error())
19423 go_error_at(ifb
->location(),
19424 "import error for %qs: lookup of %qs failed",
19425 ifb
->name().c_str(), id
.c_str());
19426 ifb
->set_saw_error();
19427 return Expression::make_error(loc
);
19430 if (no
->is_variable() || no
->is_result_variable())
19431 return Expression::make_var_reference(no
, loc
);
19432 else if (no
->is_function() || no
->is_function_declaration())
19433 return Expression::make_func_reference(no
, NULL
, loc
);
19436 if (!ifb
->saw_error())
19437 go_error_at(ifb
->location(),
19438 ("import error for %qs: "
19439 "unexpected type of identifier %qs (%d)"),
19440 ifb
->name().c_str(),
19441 id
.c_str(), no
->classification());
19442 ifb
->set_saw_error();
19443 return Expression::make_error(loc
);
19447 // Class Expression_list.
19449 // Traverse the list.
19452 Expression_list::traverse(Traverse
* traverse
)
19454 for (Expression_list::iterator p
= this->begin();
19460 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
19461 return TRAVERSE_EXIT
;
19464 return TRAVERSE_CONTINUE
;
19470 Expression_list::copy()
19472 Expression_list
* ret
= new Expression_list();
19473 for (Expression_list::iterator p
= this->begin();
19478 ret
->push_back(NULL
);
19480 ret
->push_back((*p
)->copy());
19485 // Return whether an expression list has an error expression.
19488 Expression_list::contains_error() const
19490 for (Expression_list::const_iterator p
= this->begin();
19493 if (*p
!= NULL
&& (*p
)->is_error_expression())
19498 // Class Numeric_constant.
19502 Numeric_constant::~Numeric_constant()
19507 // Copy constructor.
19509 Numeric_constant::Numeric_constant(const Numeric_constant
& a
)
19510 : classification_(a
.classification_
), type_(a
.type_
)
19512 switch (a
.classification_
)
19518 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
19521 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, MPFR_RNDN
);
19524 mpc_init2(this->u_
.complex_val
, mpc_precision
);
19525 mpc_set(this->u_
.complex_val
, a
.u_
.complex_val
, MPC_RNDNN
);
19532 // Assignment operator.
19535 Numeric_constant::operator=(const Numeric_constant
& a
)
19538 this->classification_
= a
.classification_
;
19539 this->type_
= a
.type_
;
19540 switch (a
.classification_
)
19546 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
19549 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, MPFR_RNDN
);
19552 mpc_init2(this->u_
.complex_val
, mpc_precision
);
19553 mpc_set(this->u_
.complex_val
, a
.u_
.complex_val
, MPC_RNDNN
);
19561 // Check equality with another numeric constant.
19564 Numeric_constant::equals(const Numeric_constant
& a
) const
19566 if (this->classification_
!= a
.classification_
)
19569 if (this->type_
!= NULL
&& a
.type_
!= NULL
19570 && !Type::are_identical(this->type_
, a
.type_
,
19571 Type::COMPARE_ALIASES
, NULL
))
19574 switch (a
.classification_
)
19580 return mpz_cmp(this->u_
.int_val
, a
.u_
.int_val
) == 0;
19582 return mpfr_cmp(this->u_
.float_val
, a
.u_
.float_val
) == 0;
19584 return mpc_cmp(this->u_
.complex_val
, a
.u_
.complex_val
) == 0;
19591 // Clear the contents.
19594 Numeric_constant::clear()
19596 switch (this->classification_
)
19602 mpz_clear(this->u_
.int_val
);
19605 mpfr_clear(this->u_
.float_val
);
19608 mpc_clear(this->u_
.complex_val
);
19613 this->classification_
= NC_INVALID
;
19616 // Set to an unsigned long value.
19619 Numeric_constant::set_unsigned_long(Type
* type
, unsigned long val
)
19622 this->classification_
= NC_INT
;
19623 this->type_
= type
;
19624 mpz_init_set_ui(this->u_
.int_val
, val
);
19627 // Set to an integer value.
19630 Numeric_constant::set_int(Type
* type
, const mpz_t val
)
19633 this->classification_
= NC_INT
;
19634 this->type_
= type
;
19635 mpz_init_set(this->u_
.int_val
, val
);
19638 // Set to a rune value.
19641 Numeric_constant::set_rune(Type
* type
, const mpz_t val
)
19644 this->classification_
= NC_RUNE
;
19645 this->type_
= type
;
19646 mpz_init_set(this->u_
.int_val
, val
);
19649 // Set to a floating point value.
19652 Numeric_constant::set_float(Type
* type
, const mpfr_t val
)
19655 this->classification_
= NC_FLOAT
;
19656 this->type_
= type
;
19658 // Numeric constants do not have negative zero values, so remove
19659 // them here. They also don't have infinity or NaN values, but we
19660 // should never see them here.
19663 && type
->float_type() != NULL
19664 && !type
->float_type()->is_abstract())
19665 bits
= type
->float_type()->bits();
19666 if (Numeric_constant::is_float_neg_zero(val
, bits
))
19667 mpfr_init_set_ui(this->u_
.float_val
, 0, MPFR_RNDN
);
19669 mpfr_init_set(this->u_
.float_val
, val
, MPFR_RNDN
);
19672 // Set to a complex value.
19675 Numeric_constant::set_complex(Type
* type
, const mpc_t val
)
19678 this->classification_
= NC_COMPLEX
;
19679 this->type_
= type
;
19681 // Avoid negative zero as in set_float.
19684 && type
->complex_type() != NULL
19685 && !type
->complex_type()->is_abstract())
19686 bits
= type
->complex_type()->bits() / 2;
19689 mpfr_init_set(real
, mpc_realref(val
), MPFR_RNDN
);
19690 if (Numeric_constant::is_float_neg_zero(real
, bits
))
19691 mpfr_set_ui(real
, 0, MPFR_RNDN
);
19694 mpfr_init_set(imag
, mpc_imagref(val
), MPFR_RNDN
);
19695 if (Numeric_constant::is_float_neg_zero(imag
, bits
))
19696 mpfr_set_ui(imag
, 0, MPFR_RNDN
);
19698 mpc_init2(this->u_
.complex_val
, mpc_precision
);
19699 mpc_set_fr_fr(this->u_
.complex_val
, real
, imag
, MPC_RNDNN
);
19705 // Return whether VAL, at a precision of BITS, is a negative zero.
19706 // BITS may be zero in which case it is ignored.
19709 Numeric_constant::is_float_neg_zero(const mpfr_t val
, int bits
)
19711 if (!mpfr_signbit(val
))
19713 if (mpfr_zero_p(val
))
19715 mpfr_exp_t min_exp
;
19721 // In a denormalized float32 the exponent is -126, and there are
19722 // 24 bits of which at least the last must be 1, so the smallest
19723 // representable non-zero exponent is -126 - (24 - 1) == -149.
19727 // Minimum exponent is -1022, there are 53 bits.
19733 return mpfr_get_exp(val
) < min_exp
;
19736 // Get an int value.
19739 Numeric_constant::get_int(mpz_t
* val
) const
19741 go_assert(this->is_int());
19742 mpz_init_set(*val
, this->u_
.int_val
);
19745 // Get a rune value.
19748 Numeric_constant::get_rune(mpz_t
* val
) const
19750 go_assert(this->is_rune());
19751 mpz_init_set(*val
, this->u_
.int_val
);
19754 // Get a floating point value.
19757 Numeric_constant::get_float(mpfr_t
* val
) const
19759 go_assert(this->is_float());
19760 mpfr_init_set(*val
, this->u_
.float_val
, MPFR_RNDN
);
19763 // Get a complex value.
19766 Numeric_constant::get_complex(mpc_t
* val
) const
19768 go_assert(this->is_complex());
19769 mpc_init2(*val
, mpc_precision
);
19770 mpc_set(*val
, this->u_
.complex_val
, MPC_RNDNN
);
19773 // Express value as unsigned long if possible.
19775 Numeric_constant::To_unsigned_long
19776 Numeric_constant::to_unsigned_long(unsigned long* val
) const
19778 switch (this->classification_
)
19782 return this->mpz_to_unsigned_long(this->u_
.int_val
, val
);
19784 return this->mpfr_to_unsigned_long(this->u_
.float_val
, val
);
19786 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
19787 return NC_UL_NOTINT
;
19788 return this->mpfr_to_unsigned_long(mpc_realref(this->u_
.complex_val
),
19795 // Express integer value as unsigned long if possible.
19797 Numeric_constant::To_unsigned_long
19798 Numeric_constant::mpz_to_unsigned_long(const mpz_t ival
,
19799 unsigned long *val
) const
19801 if (mpz_sgn(ival
) < 0)
19802 return NC_UL_NEGATIVE
;
19803 unsigned long ui
= mpz_get_ui(ival
);
19804 if (mpz_cmp_ui(ival
, ui
) != 0)
19807 return NC_UL_VALID
;
19810 // Express floating point value as unsigned long if possible.
19812 Numeric_constant::To_unsigned_long
19813 Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval
,
19814 unsigned long *val
) const
19816 if (!mpfr_integer_p(fval
))
19817 return NC_UL_NOTINT
;
19820 mpfr_get_z(ival
, fval
, MPFR_RNDN
);
19821 To_unsigned_long ret
= this->mpz_to_unsigned_long(ival
, val
);
19826 // Express value as memory size if possible.
19829 Numeric_constant::to_memory_size(int64_t* val
) const
19831 switch (this->classification_
)
19835 return this->mpz_to_memory_size(this->u_
.int_val
, val
);
19837 return this->mpfr_to_memory_size(this->u_
.float_val
, val
);
19839 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
19841 return this->mpfr_to_memory_size(mpc_realref(this->u_
.complex_val
), val
);
19847 // Express integer as memory size if possible.
19850 Numeric_constant::mpz_to_memory_size(const mpz_t ival
, int64_t* val
) const
19852 if (mpz_sgn(ival
) < 0)
19854 if (mpz_fits_slong_p(ival
))
19856 *val
= static_cast<int64_t>(mpz_get_si(ival
));
19860 // Test >= 64, not > 64, because an int64_t can hold 63 bits of a
19862 if (mpz_sizeinbase(ival
, 2) >= 64)
19868 mpz_tdiv_q_2exp(q
, ival
, 32);
19869 mpz_tdiv_r_2exp(r
, ival
, 32);
19870 go_assert(mpz_fits_ulong_p(q
) && mpz_fits_ulong_p(r
));
19871 *val
= ((static_cast<int64_t>(mpz_get_ui(q
)) << 32)
19872 + static_cast<int64_t>(mpz_get_ui(r
)));
19878 // Express floating point value as memory size if possible.
19881 Numeric_constant::mpfr_to_memory_size(const mpfr_t fval
, int64_t* val
) const
19883 if (!mpfr_integer_p(fval
))
19887 mpfr_get_z(ival
, fval
, MPFR_RNDN
);
19888 bool ret
= this->mpz_to_memory_size(ival
, val
);
19893 // Convert value to integer if possible.
19896 Numeric_constant::to_int(mpz_t
* val
) const
19898 switch (this->classification_
)
19902 mpz_init_set(*val
, this->u_
.int_val
);
19905 if (!mpfr_integer_p(this->u_
.float_val
))
19908 mpfr_get_z(*val
, this->u_
.float_val
, MPFR_RNDN
);
19911 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
))
19912 || !mpfr_integer_p(mpc_realref(this->u_
.complex_val
)))
19915 mpfr_get_z(*val
, mpc_realref(this->u_
.complex_val
), MPFR_RNDN
);
19922 // Convert value to floating point if possible.
19925 Numeric_constant::to_float(mpfr_t
* val
) const
19927 switch (this->classification_
)
19931 mpfr_init_set_z(*val
, this->u_
.int_val
, MPFR_RNDN
);
19934 mpfr_init_set(*val
, this->u_
.float_val
, MPFR_RNDN
);
19937 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
19939 mpfr_init_set(*val
, mpc_realref(this->u_
.complex_val
), MPFR_RNDN
);
19946 // Convert value to complex.
19949 Numeric_constant::to_complex(mpc_t
* val
) const
19951 mpc_init2(*val
, mpc_precision
);
19952 switch (this->classification_
)
19956 mpc_set_z(*val
, this->u_
.int_val
, MPC_RNDNN
);
19959 mpc_set_fr(*val
, this->u_
.float_val
, MPC_RNDNN
);
19962 mpc_set(*val
, this->u_
.complex_val
, MPC_RNDNN
);
19972 Numeric_constant::type() const
19974 if (this->type_
!= NULL
)
19975 return this->type_
;
19976 switch (this->classification_
)
19979 return Type::make_abstract_integer_type();
19981 return Type::make_abstract_character_type();
19983 return Type::make_abstract_float_type();
19985 return Type::make_abstract_complex_type();
19991 // If the constant can be expressed in TYPE, then set the type of the
19992 // constant to TYPE and return true. Otherwise return false, and, if
19993 // ISSUE_ERROR is true, report an appropriate error message.
19996 Numeric_constant::set_type(Type
* type
, bool issue_error
, Location loc
)
19999 if (type
== NULL
|| type
->is_error())
20001 else if (type
->integer_type() != NULL
)
20002 ret
= this->check_int_type(type
->integer_type(), issue_error
, loc
);
20003 else if (type
->float_type() != NULL
)
20004 ret
= this->check_float_type(type
->float_type(), issue_error
, loc
);
20005 else if (type
->complex_type() != NULL
)
20006 ret
= this->check_complex_type(type
->complex_type(), issue_error
, loc
);
20011 go_assert(saw_errors());
20014 this->type_
= type
;
20018 // Check whether the constant can be expressed in an integer type.
20021 Numeric_constant::check_int_type(Integer_type
* type
, bool issue_error
,
20025 switch (this->classification_
)
20029 mpz_init_set(val
, this->u_
.int_val
);
20033 if (!mpfr_integer_p(this->u_
.float_val
))
20037 go_error_at(location
,
20038 "floating-point constant truncated to integer");
20039 this->set_invalid();
20044 mpfr_get_z(val
, this->u_
.float_val
, MPFR_RNDN
);
20048 if (!mpfr_integer_p(mpc_realref(this->u_
.complex_val
))
20049 || !mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
20053 go_error_at(location
, "complex constant truncated to integer");
20054 this->set_invalid();
20059 mpfr_get_z(val
, mpc_realref(this->u_
.complex_val
), MPFR_RNDN
);
20067 if (type
->is_abstract())
20071 int bits
= mpz_sizeinbase(val
, 2);
20072 if (type
->is_unsigned())
20074 // For an unsigned type we can only accept a nonnegative
20075 // number, and we must be able to represents at least BITS.
20076 ret
= mpz_sgn(val
) >= 0 && bits
<= type
->bits();
20080 // For a signed type we need an extra bit to indicate the
20081 // sign. We have to handle the most negative integer
20083 ret
= (bits
+ 1 <= type
->bits()
20084 || (bits
<= type
->bits()
20085 && mpz_sgn(val
) < 0
20086 && (mpz_scan1(val
, 0)
20087 == static_cast<unsigned long>(type
->bits() - 1))
20088 && mpz_scan0(val
, type
->bits()) == ULONG_MAX
));
20092 if (!ret
&& issue_error
)
20094 go_error_at(location
, "integer constant overflow");
20095 this->set_invalid();
20101 // Check whether the constant can be expressed in a floating point
20105 Numeric_constant::check_float_type(Float_type
* type
, bool issue_error
,
20109 switch (this->classification_
)
20113 mpfr_init_set_z(val
, this->u_
.int_val
, MPFR_RNDN
);
20117 mpfr_init_set(val
, this->u_
.float_val
, MPFR_RNDN
);
20121 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
20125 this->set_invalid();
20126 go_error_at(location
,
20127 "complex constant truncated to floating-point");
20131 mpfr_init_set(val
, mpc_realref(this->u_
.complex_val
), MPFR_RNDN
);
20139 if (type
->is_abstract())
20141 else if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
20143 // A NaN or Infinity always fits in the range of the type.
20148 mpfr_exp_t exp
= mpfr_get_exp(val
);
20149 mpfr_exp_t max_exp
;
20150 switch (type
->bits())
20162 ret
= exp
<= max_exp
;
20166 // Round the constant to the desired type.
20169 switch (type
->bits())
20172 mpfr_set_prec(t
, 24);
20175 mpfr_set_prec(t
, 53);
20180 mpfr_set(t
, val
, MPFR_RNDN
);
20181 mpfr_set(val
, t
, MPFR_RNDN
);
20184 this->set_float(type
, val
);
20190 if (!ret
&& issue_error
)
20192 go_error_at(location
, "floating-point constant overflow");
20193 this->set_invalid();
20199 // Check whether the constant can be expressed in a complex type.
20202 Numeric_constant::check_complex_type(Complex_type
* type
, bool issue_error
,
20205 if (type
->is_abstract())
20208 mpfr_exp_t max_exp
;
20209 switch (type
->bits())
20222 mpc_init2(val
, mpc_precision
);
20223 switch (this->classification_
)
20227 mpc_set_z(val
, this->u_
.int_val
, MPC_RNDNN
);
20231 mpc_set_fr(val
, this->u_
.float_val
, MPC_RNDNN
);
20235 mpc_set(val
, this->u_
.complex_val
, MPC_RNDNN
);
20243 if (!mpfr_nan_p(mpc_realref(val
))
20244 && !mpfr_inf_p(mpc_realref(val
))
20245 && !mpfr_zero_p(mpc_realref(val
))
20246 && mpfr_get_exp(mpc_realref(val
)) > max_exp
)
20250 go_error_at(location
, "complex real part overflow");
20251 this->set_invalid();
20256 if (!mpfr_nan_p(mpc_imagref(val
))
20257 && !mpfr_inf_p(mpc_imagref(val
))
20258 && !mpfr_zero_p(mpc_imagref(val
))
20259 && mpfr_get_exp(mpc_imagref(val
)) > max_exp
)
20263 go_error_at(location
, "complex imaginary part overflow");
20264 this->set_invalid();
20271 // Round the constant to the desired type.
20273 switch (type
->bits())
20284 mpc_set(t
, val
, MPC_RNDNN
);
20285 mpc_set(val
, t
, MPC_RNDNN
);
20288 this->set_complex(type
, val
);
20296 // Return an Expression for this value.
20299 Numeric_constant::expression(Location loc
) const
20301 switch (this->classification_
)
20304 return Expression::make_integer_z(&this->u_
.int_val
, this->type_
, loc
);
20306 return Expression::make_character(&this->u_
.int_val
, this->type_
, loc
);
20308 return Expression::make_float(&this->u_
.float_val
, this->type_
, loc
);
20310 return Expression::make_complex(&this->u_
.complex_val
, this->type_
, loc
);
20312 go_assert(saw_errors());
20313 return Expression::make_error(loc
);
20319 // Calculate a hash code with a given seed.
20322 Numeric_constant::hash(unsigned int seed
) const
20325 const unsigned int PRIME
= 97;
20330 switch (this->classification_
)
20336 val
= mpz_get_ui(this->u_
.int_val
);
20340 mpc_abs(m
, this->u_
.complex_val
, MPFR_RNDN
);
20341 val
= mpfr_get_ui(m
, MPFR_RNDN
);
20345 f
= mpfr_get_d_2exp(&e
, this->u_
.float_val
, MPFR_RNDN
) * 4294967295.0;
20346 val
= static_cast<unsigned long>(e
+ static_cast<long>(f
));
20352 return (static_cast<unsigned int>(val
) + seed
) * PRIME
;