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.
16 #include "statements.h"
20 #include "expressions.h"
25 Expression::Expression(Expression_classification classification
,
27 : classification_(classification
), location_(location
)
31 Expression::~Expression()
35 // Traverse the expressions.
38 Expression::traverse(Expression
** pexpr
, Traverse
* traverse
)
40 Expression
* expr
= *pexpr
;
41 if ((traverse
->traverse_mask() & Traverse::traverse_expressions
) != 0)
43 int t
= traverse
->expression(pexpr
);
44 if (t
== TRAVERSE_EXIT
)
46 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
47 return TRAVERSE_CONTINUE
;
49 return expr
->do_traverse(traverse
);
52 // Traverse subexpressions of this expression.
55 Expression::traverse_subexpressions(Traverse
* traverse
)
57 return this->do_traverse(traverse
);
60 // Default implementation for do_traverse for child classes.
63 Expression::do_traverse(Traverse
*)
65 return TRAVERSE_CONTINUE
;
68 // This virtual function is called by the parser if the value of this
69 // expression is being discarded. By default, we give an error.
70 // Expressions with side effects override.
73 Expression::do_discarding_value()
75 this->unused_value_error();
79 // This virtual function is called to export expressions. This will
80 // only be used by expressions which may be constant.
83 Expression::do_export(Export
*) const
88 // Give an error saying that the value of the expression is not used.
91 Expression::unused_value_error()
93 this->report_error(_("value computed is not used"));
96 // Note that this expression is an error. This is called by children
97 // when they discover an error.
100 Expression::set_is_error()
102 this->classification_
= EXPRESSION_ERROR
;
105 // For children to call to report an error conveniently.
108 Expression::report_error(const char* msg
)
110 error_at(this->location_
, "%s", msg
);
111 this->set_is_error();
114 // Set types of variables and constants. This is implemented by the
118 Expression::determine_type(const Type_context
* context
)
120 this->do_determine_type(context
);
123 // Set types when there is no context.
126 Expression::determine_type_no_context()
128 Type_context context
;
129 this->do_determine_type(&context
);
132 // Return an expression handling any conversions which must be done during
136 Expression::convert_for_assignment(Gogo
*, Type
* lhs_type
,
137 Expression
* rhs
, Location location
)
139 Type
* rhs_type
= rhs
->type();
140 if (lhs_type
->is_error()
141 || rhs_type
->is_error()
142 || rhs
->is_error_expression())
143 return Expression::make_error(location
);
145 if (lhs_type
->forwarded() != rhs_type
->forwarded()
146 && lhs_type
->interface_type() != NULL
)
148 if (rhs_type
->interface_type() == NULL
)
149 return Expression::convert_type_to_interface(lhs_type
, rhs
, location
);
151 return Expression::convert_interface_to_interface(lhs_type
, rhs
, false,
154 else if (lhs_type
->forwarded() != rhs_type
->forwarded()
155 && rhs_type
->interface_type() != NULL
)
156 return Expression::convert_interface_to_type(lhs_type
, rhs
, location
);
157 else if (lhs_type
->is_slice_type() && rhs_type
->is_nil_type())
159 // Assigning nil to a slice.
160 Expression
* nil
= Expression::make_nil(location
);
161 Expression
* zero
= Expression::make_integer_ul(0, NULL
, location
);
162 return Expression::make_slice_value(lhs_type
, nil
, zero
, zero
, location
);
164 else if (rhs_type
->is_nil_type())
165 return Expression::make_nil(location
);
166 else if (Type::are_identical(lhs_type
, rhs_type
, false, NULL
))
168 // No conversion is needed.
171 else if (lhs_type
->points_to() != NULL
)
172 return Expression::make_unsafe_cast(lhs_type
, rhs
, location
);
173 else if (lhs_type
->is_numeric_type())
174 return Expression::make_cast(lhs_type
, rhs
, location
);
175 else if ((lhs_type
->struct_type() != NULL
176 && rhs_type
->struct_type() != NULL
)
177 || (lhs_type
->array_type() != NULL
178 && rhs_type
->array_type() != NULL
))
180 // This conversion must be permitted by Go, or we wouldn't have
182 return Expression::make_unsafe_cast(lhs_type
, rhs
, location
);
188 // Return an expression for a conversion from a non-interface type to an
192 Expression::convert_type_to_interface(Type
* lhs_type
, Expression
* rhs
,
195 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
196 bool lhs_is_empty
= lhs_interface_type
->is_empty();
198 // Since RHS_TYPE is a static type, we can create the interface
199 // method table at compile time.
201 // When setting an interface to nil, we just set both fields to
203 Type
* rhs_type
= rhs
->type();
204 if (rhs_type
->is_nil_type())
206 Expression
* nil
= Expression::make_nil(location
);
207 return Expression::make_interface_value(lhs_type
, nil
, nil
, location
);
210 // This should have been checked already.
211 go_assert(lhs_interface_type
->implements_interface(rhs_type
, NULL
));
213 // An interface is a tuple. If LHS_TYPE is an empty interface type,
214 // then the first field is the type descriptor for RHS_TYPE.
215 // Otherwise it is the interface method table for RHS_TYPE.
216 Expression
* first_field
;
218 first_field
= Expression::make_type_descriptor(rhs_type
, location
);
221 // Build the interface method table for this interface and this
222 // object type: a list of function pointers for each interface
224 Named_type
* rhs_named_type
= rhs_type
->named_type();
225 Struct_type
* rhs_struct_type
= rhs_type
->struct_type();
226 bool is_pointer
= false;
227 if (rhs_named_type
== NULL
&& rhs_struct_type
== NULL
)
229 rhs_named_type
= rhs_type
->deref()->named_type();
230 rhs_struct_type
= rhs_type
->deref()->struct_type();
233 if (rhs_named_type
!= NULL
)
235 rhs_named_type
->interface_method_table(lhs_interface_type
,
237 else if (rhs_struct_type
!= NULL
)
239 rhs_struct_type
->interface_method_table(lhs_interface_type
,
242 first_field
= Expression::make_nil(location
);
246 if (rhs_type
->points_to() != NULL
)
248 // We are assigning a pointer to the interface; the interface
249 // holds the pointer itself.
254 // We are assigning a non-pointer value to the interface; the
255 // interface gets a copy of the value in the heap if it escapes.
256 // TODO(cmang): Associate escape state state of RHS with newly
258 obj
= Expression::make_heap_expression(rhs
, location
);
261 return Expression::make_interface_value(lhs_type
, first_field
, obj
, location
);
264 // Return an expression for the type descriptor of RHS.
267 Expression::get_interface_type_descriptor(Expression
* rhs
)
269 go_assert(rhs
->type()->interface_type() != NULL
);
270 Location location
= rhs
->location();
272 // The type descriptor is the first field of an empty interface.
273 if (rhs
->type()->interface_type()->is_empty())
274 return Expression::make_interface_info(rhs
, INTERFACE_INFO_TYPE_DESCRIPTOR
,
278 Expression::make_interface_info(rhs
, INTERFACE_INFO_METHODS
, location
);
280 Expression
* descriptor
=
281 Expression::make_unary(OPERATOR_MULT
, mtable
, location
);
282 descriptor
= Expression::make_field_reference(descriptor
, 0, location
);
283 Expression
* nil
= Expression::make_nil(location
);
286 Expression::make_binary(OPERATOR_EQEQ
, mtable
, nil
, location
);
287 return Expression::make_conditional(eq
, nil
, descriptor
, location
);
290 // Return an expression for the conversion of an interface type to an
294 Expression::convert_interface_to_interface(Type
*lhs_type
, Expression
* rhs
,
298 if (Type::are_identical(lhs_type
, rhs
->type(), false, NULL
))
301 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
302 bool lhs_is_empty
= lhs_interface_type
->is_empty();
304 // In the general case this requires runtime examination of the type
305 // method table to match it up with the interface methods.
307 // FIXME: If all of the methods in the right hand side interface
308 // also appear in the left hand side interface, then we don't need
309 // to do a runtime check, although we still need to build a new
312 // We are going to evaluate RHS multiple times.
313 go_assert(rhs
->is_variable());
315 // Get the type descriptor for the right hand side. This will be
316 // NULL for a nil interface.
317 Expression
* rhs_type_expr
= Expression::get_interface_type_descriptor(rhs
);
318 Expression
* lhs_type_expr
=
319 Expression::make_type_descriptor(lhs_type
, location
);
321 Expression
* first_field
;
324 // A type assertion fails when converting a nil interface.
326 Runtime::make_call(Runtime::ASSERT_INTERFACE
, location
, 2,
327 lhs_type_expr
, rhs_type_expr
);
329 else if (lhs_is_empty
)
331 // A conversion to an empty interface always succeeds, and the
332 // first field is just the type descriptor of the object.
333 first_field
= rhs_type_expr
;
337 // A conversion to a non-empty interface may fail, but unlike a
338 // type assertion converting nil will always succeed.
340 Runtime::make_call(Runtime::CONVERT_INTERFACE
, location
, 2,
341 lhs_type_expr
, rhs_type_expr
);
344 // The second field is simply the object pointer.
346 Expression::make_interface_info(rhs
, INTERFACE_INFO_OBJECT
, location
);
347 return Expression::make_interface_value(lhs_type
, first_field
, obj
, location
);
350 // Return an expression for the conversion of an interface type to a
351 // non-interface type.
354 Expression::convert_interface_to_type(Type
*lhs_type
, Expression
* rhs
,
357 // We are going to evaluate RHS multiple times.
358 go_assert(rhs
->is_variable());
360 // Call a function to check that the type is valid. The function
361 // will panic with an appropriate runtime type error if the type is
363 Expression
* lhs_type_expr
= Expression::make_type_descriptor(lhs_type
,
365 Expression
* rhs_descriptor
=
366 Expression::get_interface_type_descriptor(rhs
);
368 Type
* rhs_type
= rhs
->type();
369 Expression
* rhs_inter_expr
= Expression::make_type_descriptor(rhs_type
,
372 Expression
* check_iface
= Runtime::make_call(Runtime::CHECK_INTERFACE_TYPE
,
373 location
, 3, lhs_type_expr
,
374 rhs_descriptor
, rhs_inter_expr
);
376 // If the call succeeds, pull out the value.
377 Expression
* obj
= Expression::make_interface_info(rhs
, INTERFACE_INFO_OBJECT
,
380 // If the value is a pointer, then it is the value we want.
381 // Otherwise it points to the value.
382 if (lhs_type
->points_to() == NULL
)
384 obj
= Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type
), obj
,
386 obj
= Expression::make_unary(OPERATOR_MULT
, obj
, location
);
388 return Expression::make_compound(check_iface
, obj
, location
);
391 // Convert an expression to its backend representation. This is implemented by
392 // the child class. Not that it is not in general safe to call this multiple
393 // times for a single expression, but that we don't catch such errors.
396 Expression::get_backend(Translate_context
* context
)
398 // The child may have marked this expression as having an error.
399 if (this->classification_
== EXPRESSION_ERROR
)
400 return context
->backend()->error_expression();
402 return this->do_get_backend(context
);
405 // Return a backend expression for VAL.
407 Expression::backend_numeric_constant_expression(Translate_context
* context
,
408 Numeric_constant
* val
)
410 Gogo
* gogo
= context
->gogo();
411 Type
* type
= val
->type();
413 return gogo
->backend()->error_expression();
415 Btype
* btype
= type
->get_backend(gogo
);
417 if (type
->integer_type() != NULL
)
420 if (!val
->to_int(&ival
))
422 go_assert(saw_errors());
423 return gogo
->backend()->error_expression();
425 ret
= gogo
->backend()->integer_constant_expression(btype
, ival
);
428 else if (type
->float_type() != NULL
)
431 if (!val
->to_float(&fval
))
433 go_assert(saw_errors());
434 return gogo
->backend()->error_expression();
436 ret
= gogo
->backend()->float_constant_expression(btype
, fval
);
439 else if (type
->complex_type() != NULL
)
442 if (!val
->to_complex(&cval
))
444 go_assert(saw_errors());
445 return gogo
->backend()->error_expression();
447 ret
= gogo
->backend()->complex_constant_expression(btype
, cval
);
456 // Return an expression which evaluates to true if VAL, of arbitrary integer
457 // type, is negative or is more than the maximum value of the Go type "int".
460 Expression::check_bounds(Expression
* val
, Location loc
)
462 Type
* val_type
= val
->type();
463 Type
* bound_type
= Type::lookup_integer_type("int");
466 bool val_is_unsigned
= false;
467 if (val_type
->integer_type() != NULL
)
469 val_type_size
= val_type
->integer_type()->bits();
470 val_is_unsigned
= val_type
->integer_type()->is_unsigned();
474 if (!val_type
->is_numeric_type()
475 || !Type::are_convertible(bound_type
, val_type
, NULL
))
477 go_assert(saw_errors());
478 return Expression::make_boolean(true, loc
);
481 if (val_type
->complex_type() != NULL
)
482 val_type_size
= val_type
->complex_type()->bits();
484 val_type_size
= val_type
->float_type()->bits();
487 Expression
* negative_index
= Expression::make_boolean(false, loc
);
488 Expression
* index_overflows
= Expression::make_boolean(false, loc
);
489 if (!val_is_unsigned
)
491 Expression
* zero
= Expression::make_integer_ul(0, val_type
, loc
);
492 negative_index
= Expression::make_binary(OPERATOR_LT
, val
, zero
, loc
);
495 int bound_type_size
= bound_type
->integer_type()->bits();
496 if (val_type_size
> bound_type_size
497 || (val_type_size
== bound_type_size
501 mpz_init_set_ui(one
, 1UL);
503 // maxval = 2^(bound_type_size - 1) - 1
506 mpz_mul_2exp(maxval
, one
, bound_type_size
- 1);
507 mpz_sub_ui(maxval
, maxval
, 1);
508 Expression
* max
= Expression::make_integer_z(&maxval
, val_type
, loc
);
512 index_overflows
= Expression::make_binary(OPERATOR_GT
, val
, max
, loc
);
515 return Expression::make_binary(OPERATOR_OROR
, negative_index
, index_overflows
,
520 Expression::dump_expression(Ast_dump_context
* ast_dump_context
) const
522 this->do_dump_expression(ast_dump_context
);
525 // Error expressions. This are used to avoid cascading errors.
527 class Error_expression
: public Expression
530 Error_expression(Location location
)
531 : Expression(EXPRESSION_ERROR
, location
)
536 do_is_constant() const
540 do_is_immutable() const
544 do_numeric_constant_value(Numeric_constant
* nc
) const
546 nc
->set_unsigned_long(NULL
, 0);
551 do_discarding_value()
556 { return Type::make_error_type(); }
559 do_determine_type(const Type_context
*)
567 do_is_addressable() const
571 do_get_backend(Translate_context
* context
)
572 { return context
->backend()->error_expression(); }
575 do_dump_expression(Ast_dump_context
*) const;
578 // Dump the ast representation for an error expression to a dump context.
581 Error_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
583 ast_dump_context
->ostream() << "_Error_" ;
587 Expression::make_error(Location location
)
589 return new Error_expression(location
);
592 // An expression which is really a type. This is used during parsing.
593 // It is an error if these survive after lowering.
596 Type_expression
: public Expression
599 Type_expression(Type
* type
, Location location
)
600 : Expression(EXPRESSION_TYPE
, location
),
606 do_traverse(Traverse
* traverse
)
607 { return Type::traverse(this->type_
, traverse
); }
611 { return this->type_
; }
614 do_determine_type(const Type_context
*)
618 do_check_types(Gogo
*)
619 { this->report_error(_("invalid use of type")); }
626 do_get_backend(Translate_context
*)
627 { go_unreachable(); }
629 void do_dump_expression(Ast_dump_context
*) const;
632 // The type which we are representing as an expression.
637 Type_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
639 ast_dump_context
->dump_type(this->type_
);
643 Expression::make_type(Type
* type
, Location location
)
645 return new Type_expression(type
, location
);
648 // Class Parser_expression.
651 Parser_expression::do_type()
653 // We should never really ask for the type of a Parser_expression.
654 // However, it can happen, at least when we have an invalid const
655 // whose initializer refers to the const itself. In that case we
656 // may ask for the type when lowering the const itself.
657 go_assert(saw_errors());
658 return Type::make_error_type();
661 // Class Var_expression.
663 // Lower a variable expression. Here we just make sure that the
664 // initialization expression of the variable has been lowered. This
665 // ensures that we will be able to determine the type of the variable
669 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
670 Statement_inserter
* inserter
, int)
672 if (this->variable_
->is_variable())
674 Variable
* var
= this->variable_
->var_value();
675 // This is either a local variable or a global variable. A
676 // reference to a variable which is local to an enclosing
677 // function will be a reference to a field in a closure.
678 if (var
->is_global())
683 var
->lower_init_expression(gogo
, function
, inserter
);
688 // Return the type of a reference to a variable.
691 Var_expression::do_type()
693 if (this->variable_
->is_variable())
694 return this->variable_
->var_value()->type();
695 else if (this->variable_
->is_result_variable())
696 return this->variable_
->result_var_value()->type();
701 // Determine the type of a reference to a variable.
704 Var_expression::do_determine_type(const Type_context
*)
706 if (this->variable_
->is_variable())
707 this->variable_
->var_value()->determine_type();
710 // Something takes the address of this variable. This means that we
711 // may want to move the variable onto the heap.
714 Var_expression::do_address_taken(bool escapes
)
718 if (this->variable_
->is_variable())
719 this->variable_
->var_value()->set_non_escaping_address_taken();
720 else if (this->variable_
->is_result_variable())
721 this->variable_
->result_var_value()->set_non_escaping_address_taken();
727 if (this->variable_
->is_variable())
728 this->variable_
->var_value()->set_address_taken();
729 else if (this->variable_
->is_result_variable())
730 this->variable_
->result_var_value()->set_address_taken();
735 if (this->variable_
->is_variable()
736 && this->variable_
->var_value()->is_in_heap())
738 Node::make_node(this)->set_encoding(Node::ESCAPE_HEAP
);
739 Node::make_node(this->variable_
)->set_encoding(Node::ESCAPE_HEAP
);
743 // Get the backend representation for a reference to a variable.
746 Var_expression::do_get_backend(Translate_context
* context
)
748 Bvariable
* bvar
= this->variable_
->get_backend_variable(context
->gogo(),
749 context
->function());
751 Location loc
= this->location();
753 Gogo
* gogo
= context
->gogo();
754 if (this->variable_
->is_variable())
756 is_in_heap
= this->variable_
->var_value()->is_in_heap();
757 btype
= this->variable_
->var_value()->type()->get_backend(gogo
);
759 else if (this->variable_
->is_result_variable())
761 is_in_heap
= this->variable_
->result_var_value()->is_in_heap();
762 btype
= this->variable_
->result_var_value()->type()->get_backend(gogo
);
767 Bexpression
* ret
= context
->backend()->var_expression(bvar
, loc
);
769 ret
= context
->backend()->indirect_expression(btype
, ret
, true, loc
);
773 // Ast dump for variable expression.
776 Var_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
778 ast_dump_context
->ostream() << this->variable_
->name() ;
781 // Make a reference to a variable in an expression.
784 Expression::make_var_reference(Named_object
* var
, Location location
)
787 return Expression::make_sink(location
);
789 // FIXME: Creating a new object for each reference to a variable is
791 return new Var_expression(var
, location
);
794 // Class Enclosed_var_expression.
797 Enclosed_var_expression::do_traverse(Traverse
*)
799 return TRAVERSE_CONTINUE
;
802 // Lower the reference to the enclosed variable.
805 Enclosed_var_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
806 Statement_inserter
* inserter
, int)
808 gogo
->lower_expression(function
, inserter
, &this->reference_
);
812 // Flatten the reference to the enclosed variable.
815 Enclosed_var_expression::do_flatten(Gogo
* gogo
, Named_object
* function
,
816 Statement_inserter
* inserter
)
818 gogo
->flatten_expression(function
, inserter
, &this->reference_
);
823 Enclosed_var_expression::do_address_taken(bool escapes
)
827 if (this->variable_
->is_variable())
828 this->variable_
->var_value()->set_non_escaping_address_taken();
829 else if (this->variable_
->is_result_variable())
830 this->variable_
->result_var_value()->set_non_escaping_address_taken();
836 if (this->variable_
->is_variable())
837 this->variable_
->var_value()->set_address_taken();
838 else if (this->variable_
->is_result_variable())
839 this->variable_
->result_var_value()->set_address_taken();
844 if (this->variable_
->is_variable()
845 && this->variable_
->var_value()->is_in_heap())
846 Node::make_node(this->variable_
)->set_encoding(Node::ESCAPE_HEAP
);
849 // Ast dump for enclosed variable expression.
852 Enclosed_var_expression::do_dump_expression(Ast_dump_context
* adc
) const
854 adc
->ostream() << this->variable_
->name();
857 // Make a reference to a variable within an enclosing function.
860 Expression::make_enclosing_var_reference(Expression
* reference
,
861 Named_object
* var
, Location location
)
863 return new Enclosed_var_expression(reference
, var
, location
);
866 // Class Temporary_reference_expression.
871 Temporary_reference_expression::do_type()
873 return this->statement_
->type();
876 // Called if something takes the address of this temporary variable.
877 // We never have to move temporary variables to the heap, but we do
878 // need to know that they must live in the stack rather than in a
882 Temporary_reference_expression::do_address_taken(bool)
884 this->statement_
->set_is_address_taken();
887 // Get a backend expression referring to the variable.
890 Temporary_reference_expression::do_get_backend(Translate_context
* context
)
892 Gogo
* gogo
= context
->gogo();
893 Bvariable
* bvar
= this->statement_
->get_backend_variable(context
);
894 Bexpression
* ret
= gogo
->backend()->var_expression(bvar
, this->location());
896 // The backend can't always represent the same set of recursive types
897 // that the Go frontend can. In some cases this means that a
898 // temporary variable won't have the right backend type. Correct
899 // that here by adding a type cast. We need to use base() to push
900 // the circularity down one level.
901 Type
* stype
= this->statement_
->type();
902 if (!this->is_lvalue_
903 && stype
->has_pointer()
904 && stype
->deref()->is_void_type())
906 Btype
* btype
= this->type()->base()->get_backend(gogo
);
907 ret
= gogo
->backend()->convert_expression(btype
, ret
, this->location());
912 // Ast dump for temporary reference.
915 Temporary_reference_expression::do_dump_expression(
916 Ast_dump_context
* ast_dump_context
) const
918 ast_dump_context
->dump_temp_variable_name(this->statement_
);
921 // Make a reference to a temporary variable.
923 Temporary_reference_expression
*
924 Expression::make_temporary_reference(Temporary_statement
* statement
,
927 return new Temporary_reference_expression(statement
, location
);
930 // Class Set_and_use_temporary_expression.
935 Set_and_use_temporary_expression::do_type()
937 return this->statement_
->type();
940 // Determine the type of the expression.
943 Set_and_use_temporary_expression::do_determine_type(
944 const Type_context
* context
)
946 this->expr_
->determine_type(context
);
952 Set_and_use_temporary_expression::do_address_taken(bool)
954 this->statement_
->set_is_address_taken();
957 // Return the backend representation.
960 Set_and_use_temporary_expression::do_get_backend(Translate_context
* context
)
962 Location loc
= this->location();
963 Gogo
* gogo
= context
->gogo();
964 Bvariable
* bvar
= this->statement_
->get_backend_variable(context
);
965 Bexpression
* var_ref
= gogo
->backend()->var_expression(bvar
, loc
);
967 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
968 Bstatement
* set
= gogo
->backend()->assignment_statement(var_ref
, bexpr
, loc
);
969 var_ref
= gogo
->backend()->var_expression(bvar
, loc
);
970 Bexpression
* ret
= gogo
->backend()->compound_expression(set
, var_ref
, loc
);
977 Set_and_use_temporary_expression::do_dump_expression(
978 Ast_dump_context
* ast_dump_context
) const
980 ast_dump_context
->ostream() << '(';
981 ast_dump_context
->dump_temp_variable_name(this->statement_
);
982 ast_dump_context
->ostream() << " = ";
983 this->expr_
->dump_expression(ast_dump_context
);
984 ast_dump_context
->ostream() << ')';
987 // Make a set-and-use temporary.
989 Set_and_use_temporary_expression
*
990 Expression::make_set_and_use_temporary(Temporary_statement
* statement
,
991 Expression
* expr
, Location location
)
993 return new Set_and_use_temporary_expression(statement
, expr
, location
);
996 // A sink expression--a use of the blank identifier _.
998 class Sink_expression
: public Expression
1001 Sink_expression(Location location
)
1002 : Expression(EXPRESSION_SINK
, location
),
1003 type_(NULL
), bvar_(NULL
)
1008 do_discarding_value()
1015 do_determine_type(const Type_context
*);
1019 { return new Sink_expression(this->location()); }
1022 do_get_backend(Translate_context
*);
1025 do_dump_expression(Ast_dump_context
*) const;
1028 // The type of this sink variable.
1030 // The temporary variable we generate.
1034 // Return the type of a sink expression.
1037 Sink_expression::do_type()
1039 if (this->type_
== NULL
)
1040 return Type::make_sink_type();
1044 // Determine the type of a sink expression.
1047 Sink_expression::do_determine_type(const Type_context
* context
)
1049 if (context
->type
!= NULL
)
1050 this->type_
= context
->type
;
1053 // Return a temporary variable for a sink expression. This will
1054 // presumably be a write-only variable which the middle-end will drop.
1057 Sink_expression::do_get_backend(Translate_context
* context
)
1059 Location loc
= this->location();
1060 Gogo
* gogo
= context
->gogo();
1061 if (this->bvar_
== NULL
)
1063 go_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1064 Named_object
* fn
= context
->function();
1065 go_assert(fn
!= NULL
);
1066 Bfunction
* fn_ctx
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
1067 Btype
* bt
= this->type_
->get_backend(context
->gogo());
1070 gogo
->backend()->temporary_variable(fn_ctx
, context
->bblock(), bt
, NULL
,
1072 Bexpression
* var_ref
= gogo
->backend()->var_expression(this->bvar_
, loc
);
1073 var_ref
= gogo
->backend()->compound_expression(decl
, var_ref
, loc
);
1076 return gogo
->backend()->var_expression(this->bvar_
, loc
);
1079 // Ast dump for sink expression.
1082 Sink_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1084 ast_dump_context
->ostream() << "_" ;
1087 // Make a sink expression.
1090 Expression::make_sink(Location location
)
1092 return new Sink_expression(location
);
1095 // Class Func_expression.
1097 // FIXME: Can a function expression appear in a constant expression?
1098 // The value is unchanging. Initializing a constant to the address of
1099 // a function seems like it could work, though there might be little
1105 Func_expression::do_traverse(Traverse
* traverse
)
1107 return (this->closure_
== NULL
1109 : Expression::traverse(&this->closure_
, traverse
));
1112 // Return the type of a function expression.
1115 Func_expression::do_type()
1117 if (this->function_
->is_function())
1118 return this->function_
->func_value()->type();
1119 else if (this->function_
->is_function_declaration())
1120 return this->function_
->func_declaration_value()->type();
1125 // Get the backend representation for the code of a function expression.
1128 Func_expression::get_code_pointer(Gogo
* gogo
, Named_object
* no
, Location loc
)
1130 Function_type
* fntype
;
1131 if (no
->is_function())
1132 fntype
= no
->func_value()->type();
1133 else if (no
->is_function_declaration())
1134 fntype
= no
->func_declaration_value()->type();
1138 // Builtin functions are handled specially by Call_expression. We
1139 // can't take their address.
1140 if (fntype
->is_builtin())
1143 "invalid use of special builtin function %qs; must be called",
1144 no
->message_name().c_str());
1145 return gogo
->backend()->error_expression();
1149 if (no
->is_function())
1150 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
);
1151 else if (no
->is_function_declaration())
1152 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
);
1156 return gogo
->backend()->function_code_expression(fndecl
, loc
);
1159 // Get the backend representation for a function expression. This is used when
1160 // we take the address of a function rather than simply calling it. A func
1161 // value is represented as a pointer to a block of memory. The first
1162 // word of that memory is a pointer to the function code. The
1163 // remaining parts of that memory are the addresses of variables that
1164 // the function closes over.
1167 Func_expression::do_get_backend(Translate_context
* context
)
1169 // If there is no closure, just use the function descriptor.
1170 if (this->closure_
== NULL
)
1172 Gogo
* gogo
= context
->gogo();
1173 Named_object
* no
= this->function_
;
1174 Expression
* descriptor
;
1175 if (no
->is_function())
1176 descriptor
= no
->func_value()->descriptor(gogo
, no
);
1177 else if (no
->is_function_declaration())
1179 if (no
->func_declaration_value()->type()->is_builtin())
1181 error_at(this->location(),
1182 ("invalid use of special builtin function %qs; "
1184 no
->message_name().c_str());
1185 return gogo
->backend()->error_expression();
1187 descriptor
= no
->func_declaration_value()->descriptor(gogo
, no
);
1192 Bexpression
* bdesc
= descriptor
->get_backend(context
);
1193 return gogo
->backend()->address_expression(bdesc
, this->location());
1196 go_assert(this->function_
->func_value()->enclosing() != NULL
);
1198 // If there is a closure, then the closure is itself the function
1199 // expression. It is a pointer to a struct whose first field points
1200 // to the function code and whose remaining fields are the addresses
1201 // of the closed-over variables.
1202 return this->closure_
->get_backend(context
);
1205 // Ast dump for function.
1208 Func_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1210 ast_dump_context
->ostream() << this->function_
->name();
1211 if (this->closure_
!= NULL
)
1213 ast_dump_context
->ostream() << " {closure = ";
1214 this->closure_
->dump_expression(ast_dump_context
);
1215 ast_dump_context
->ostream() << "}";
1219 // Make a reference to a function in an expression.
1222 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1225 Func_expression
* fe
= new Func_expression(function
, closure
, location
);
1227 // Detect references to builtin functions and set the runtime code if
1229 if (function
->is_function_declaration())
1230 fe
->set_runtime_code(Runtime::name_to_code(function
->name()));
1234 // Class Func_descriptor_expression.
1238 Func_descriptor_expression::Func_descriptor_expression(Named_object
* fn
)
1239 : Expression(EXPRESSION_FUNC_DESCRIPTOR
, fn
->location()),
1240 fn_(fn
), dvar_(NULL
)
1242 go_assert(!fn
->is_function() || !fn
->func_value()->needs_closure());
1248 Func_descriptor_expression::do_traverse(Traverse
*)
1250 return TRAVERSE_CONTINUE
;
1253 // All function descriptors have the same type.
1255 Type
* Func_descriptor_expression::descriptor_type
;
1258 Func_descriptor_expression::make_func_descriptor_type()
1260 if (Func_descriptor_expression::descriptor_type
!= NULL
)
1262 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1263 Type
* struct_type
= Type::make_builtin_struct_type(1, "code", uintptr_type
);
1264 Func_descriptor_expression::descriptor_type
=
1265 Type::make_builtin_named_type("functionDescriptor", struct_type
);
1269 Func_descriptor_expression::do_type()
1271 Func_descriptor_expression::make_func_descriptor_type();
1272 return Func_descriptor_expression::descriptor_type
;
1275 // The backend representation for a function descriptor.
1278 Func_descriptor_expression::do_get_backend(Translate_context
* context
)
1280 Named_object
* no
= this->fn_
;
1281 Location loc
= no
->location();
1282 if (this->dvar_
!= NULL
)
1283 return context
->backend()->var_expression(this->dvar_
, loc
);
1285 Gogo
* gogo
= context
->gogo();
1286 std::string var_name
;
1287 bool is_descriptor
= false;
1288 if (no
->is_function_declaration()
1289 && !no
->func_declaration_value()->asm_name().empty()
1290 && Linemap::is_predeclared_location(no
->location()))
1292 var_name
= no
->func_declaration_value()->asm_name() + "_descriptor";
1293 is_descriptor
= true;
1297 if (no
->package() == NULL
)
1298 var_name
= gogo
->pkgpath_symbol();
1300 var_name
= no
->package()->pkgpath_symbol();
1301 var_name
.push_back('.');
1302 var_name
.append(Gogo::unpack_hidden_name(no
->name()));
1303 var_name
.append("$descriptor");
1306 Btype
* btype
= this->type()->get_backend(gogo
);
1309 if (no
->package() != NULL
|| is_descriptor
)
1310 bvar
= context
->backend()->immutable_struct_reference(var_name
, btype
,
1314 Location bloc
= Linemap::predeclared_location();
1315 bool is_hidden
= ((no
->is_function()
1316 && no
->func_value()->enclosing() != NULL
)
1317 || Gogo::is_thunk(no
));
1318 bvar
= context
->backend()->immutable_struct(var_name
, is_hidden
, false,
1320 Expression_list
* vals
= new Expression_list();
1321 vals
->push_back(Expression::make_func_code_reference(this->fn_
, bloc
));
1323 Expression::make_struct_composite_literal(this->type(), vals
, bloc
);
1324 Translate_context
bcontext(gogo
, NULL
, NULL
, NULL
);
1325 bcontext
.set_is_const();
1326 Bexpression
* binit
= init
->get_backend(&bcontext
);
1327 context
->backend()->immutable_struct_set_init(bvar
, var_name
, is_hidden
,
1328 false, btype
, bloc
, binit
);
1332 return gogo
->backend()->var_expression(bvar
, loc
);
1335 // Print a function descriptor expression.
1338 Func_descriptor_expression::do_dump_expression(Ast_dump_context
* context
) const
1340 context
->ostream() << "[descriptor " << this->fn_
->name() << "]";
1343 // Make a function descriptor expression.
1345 Func_descriptor_expression
*
1346 Expression::make_func_descriptor(Named_object
* fn
)
1348 return new Func_descriptor_expression(fn
);
1351 // Make the function descriptor type, so that it can be converted.
1354 Expression::make_func_descriptor_type()
1356 Func_descriptor_expression::make_func_descriptor_type();
1359 // A reference to just the code of a function.
1361 class Func_code_reference_expression
: public Expression
1364 Func_code_reference_expression(Named_object
* function
, Location location
)
1365 : Expression(EXPRESSION_FUNC_CODE_REFERENCE
, location
),
1371 do_traverse(Traverse
*)
1372 { return TRAVERSE_CONTINUE
; }
1375 do_is_immutable() const
1380 { return Type::make_pointer_type(Type::make_void_type()); }
1383 do_determine_type(const Type_context
*)
1389 return Expression::make_func_code_reference(this->function_
,
1394 do_get_backend(Translate_context
*);
1397 do_dump_expression(Ast_dump_context
* context
) const
1398 { context
->ostream() << "[raw " << this->function_
->name() << "]" ; }
1402 Named_object
* function_
;
1405 // Get the backend representation for a reference to function code.
1408 Func_code_reference_expression::do_get_backend(Translate_context
* context
)
1410 return Func_expression::get_code_pointer(context
->gogo(), this->function_
,
1414 // Make a reference to the code of a function.
1417 Expression::make_func_code_reference(Named_object
* function
, Location location
)
1419 return new Func_code_reference_expression(function
, location
);
1422 // Class Unknown_expression.
1424 // Return the name of an unknown expression.
1427 Unknown_expression::name() const
1429 return this->named_object_
->name();
1432 // Lower a reference to an unknown name.
1435 Unknown_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
1437 Location location
= this->location();
1438 Named_object
* no
= this->named_object_
;
1440 if (!no
->is_unknown())
1444 real
= no
->unknown_value()->real_named_object();
1447 if (this->is_composite_literal_key_
)
1449 if (!this->no_error_message_
)
1450 error_at(location
, "reference to undefined name %qs",
1451 this->named_object_
->message_name().c_str());
1452 return Expression::make_error(location
);
1455 switch (real
->classification())
1457 case Named_object::NAMED_OBJECT_CONST
:
1458 return Expression::make_const_reference(real
, location
);
1459 case Named_object::NAMED_OBJECT_TYPE
:
1460 return Expression::make_type(real
->type_value(), location
);
1461 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1462 if (this->is_composite_literal_key_
)
1464 if (!this->no_error_message_
)
1465 error_at(location
, "reference to undefined type %qs",
1466 real
->message_name().c_str());
1467 return Expression::make_error(location
);
1468 case Named_object::NAMED_OBJECT_VAR
:
1469 real
->var_value()->set_is_used();
1470 return Expression::make_var_reference(real
, location
);
1471 case Named_object::NAMED_OBJECT_FUNC
:
1472 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1473 return Expression::make_func_reference(real
, NULL
, location
);
1474 case Named_object::NAMED_OBJECT_PACKAGE
:
1475 if (this->is_composite_literal_key_
)
1477 if (!this->no_error_message_
)
1478 error_at(location
, "unexpected reference to package");
1479 return Expression::make_error(location
);
1485 // Dump the ast representation for an unknown expression to a dump context.
1488 Unknown_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1490 ast_dump_context
->ostream() << "_Unknown_(" << this->named_object_
->name()
1494 // Make a reference to an unknown name.
1497 Expression::make_unknown_reference(Named_object
* no
, Location location
)
1499 return new Unknown_expression(no
, location
);
1502 // A boolean expression.
1504 class Boolean_expression
: public Expression
1507 Boolean_expression(bool val
, Location location
)
1508 : Expression(EXPRESSION_BOOLEAN
, location
),
1509 val_(val
), type_(NULL
)
1517 do_is_constant() const
1521 do_is_immutable() const
1528 do_determine_type(const Type_context
*);
1535 do_get_backend(Translate_context
* context
)
1536 { return context
->backend()->boolean_constant_expression(this->val_
); }
1539 do_export(Export
* exp
) const
1540 { exp
->write_c_string(this->val_
? "true" : "false"); }
1543 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1544 { ast_dump_context
->ostream() << (this->val_
? "true" : "false"); }
1549 // The type as determined by context.
1556 Boolean_expression::do_type()
1558 if (this->type_
== NULL
)
1559 this->type_
= Type::make_boolean_type();
1563 // Set the type from the context.
1566 Boolean_expression::do_determine_type(const Type_context
* context
)
1568 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1570 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1571 this->type_
= context
->type
;
1572 else if (!context
->may_be_abstract
)
1573 this->type_
= Type::lookup_bool_type();
1576 // Import a boolean constant.
1579 Boolean_expression::do_import(Import
* imp
)
1581 if (imp
->peek_char() == 't')
1583 imp
->require_c_string("true");
1584 return Expression::make_boolean(true, imp
->location());
1588 imp
->require_c_string("false");
1589 return Expression::make_boolean(false, imp
->location());
1593 // Make a boolean expression.
1596 Expression::make_boolean(bool val
, Location location
)
1598 return new Boolean_expression(val
, location
);
1601 // Class String_expression.
1606 String_expression::do_type()
1608 if (this->type_
== NULL
)
1609 this->type_
= Type::make_string_type();
1613 // Set the type from the context.
1616 String_expression::do_determine_type(const Type_context
* context
)
1618 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1620 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1621 this->type_
= context
->type
;
1622 else if (!context
->may_be_abstract
)
1623 this->type_
= Type::lookup_string_type();
1626 // Build a string constant.
1629 String_expression::do_get_backend(Translate_context
* context
)
1631 Gogo
* gogo
= context
->gogo();
1632 Btype
* btype
= Type::make_string_type()->get_backend(gogo
);
1634 Location loc
= this->location();
1635 std::vector
<Bexpression
*> init(2);
1636 Bexpression
* str_cst
=
1637 gogo
->backend()->string_constant_expression(this->val_
);
1638 init
[0] = gogo
->backend()->address_expression(str_cst
, loc
);
1640 Btype
* int_btype
= Type::lookup_integer_type("int")->get_backend(gogo
);
1642 mpz_init_set_ui(lenval
, this->val_
.length());
1643 init
[1] = gogo
->backend()->integer_constant_expression(int_btype
, lenval
);
1646 return gogo
->backend()->constructor_expression(btype
, init
, loc
);
1649 // Write string literal to string dump.
1652 String_expression::export_string(String_dump
* exp
,
1653 const String_expression
* str
)
1656 s
.reserve(str
->val_
.length() * 4 + 2);
1658 for (std::string::const_iterator p
= str
->val_
.begin();
1659 p
!= str
->val_
.end();
1662 if (*p
== '\\' || *p
== '"')
1667 else if (*p
>= 0x20 && *p
< 0x7f)
1669 else if (*p
== '\n')
1671 else if (*p
== '\t')
1676 unsigned char c
= *p
;
1677 unsigned int dig
= c
>> 4;
1678 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1680 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1684 exp
->write_string(s
);
1687 // Export a string expression.
1690 String_expression::do_export(Export
* exp
) const
1692 String_expression::export_string(exp
, this);
1695 // Import a string expression.
1698 String_expression::do_import(Import
* imp
)
1700 imp
->require_c_string("\"");
1704 int c
= imp
->get_char();
1705 if (c
== '"' || c
== -1)
1708 val
+= static_cast<char>(c
);
1711 c
= imp
->get_char();
1712 if (c
== '\\' || c
== '"')
1713 val
+= static_cast<char>(c
);
1720 c
= imp
->get_char();
1721 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1722 c
= imp
->get_char();
1723 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1724 char v
= (vh
<< 4) | vl
;
1729 error_at(imp
->location(), "bad string constant");
1730 return Expression::make_error(imp
->location());
1734 return Expression::make_string(val
, imp
->location());
1737 // Ast dump for string expression.
1740 String_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1742 String_expression::export_string(ast_dump_context
, this);
1745 // Make a string expression.
1748 Expression::make_string(const std::string
& val
, Location location
)
1750 return new String_expression(val
, location
);
1753 // An expression that evaluates to some characteristic of a string.
1754 // This is used when indexing, bound-checking, or nil checking a string.
1756 class String_info_expression
: public Expression
1759 String_info_expression(Expression
* string
, String_info string_info
,
1761 : Expression(EXPRESSION_STRING_INFO
, location
),
1762 string_(string
), string_info_(string_info
)
1770 do_determine_type(const Type_context
*)
1771 { go_unreachable(); }
1776 return new String_info_expression(this->string_
->copy(), this->string_info_
,
1781 do_get_backend(Translate_context
* context
);
1784 do_dump_expression(Ast_dump_context
*) const;
1787 do_issue_nil_check()
1788 { this->string_
->issue_nil_check(); }
1791 // The string for which we are getting information.
1792 Expression
* string_
;
1793 // What information we want.
1794 String_info string_info_
;
1797 // Return the type of the string info.
1800 String_info_expression::do_type()
1802 switch (this->string_info_
)
1804 case STRING_INFO_DATA
:
1806 Type
* byte_type
= Type::lookup_integer_type("uint8");
1807 return Type::make_pointer_type(byte_type
);
1809 case STRING_INFO_LENGTH
:
1810 return Type::lookup_integer_type("int");
1816 // Return string information in GENERIC.
1819 String_info_expression::do_get_backend(Translate_context
* context
)
1821 Gogo
* gogo
= context
->gogo();
1823 Bexpression
* bstring
= this->string_
->get_backend(context
);
1824 switch (this->string_info_
)
1826 case STRING_INFO_DATA
:
1827 case STRING_INFO_LENGTH
:
1828 return gogo
->backend()->struct_field_expression(bstring
,
1837 // Dump ast representation for a type info expression.
1840 String_info_expression::do_dump_expression(
1841 Ast_dump_context
* ast_dump_context
) const
1843 ast_dump_context
->ostream() << "stringinfo(";
1844 this->string_
->dump_expression(ast_dump_context
);
1845 ast_dump_context
->ostream() << ",";
1846 ast_dump_context
->ostream() <<
1847 (this->string_info_
== STRING_INFO_DATA
? "data"
1848 : this->string_info_
== STRING_INFO_LENGTH
? "length"
1850 ast_dump_context
->ostream() << ")";
1853 // Make a string info expression.
1856 Expression::make_string_info(Expression
* string
, String_info string_info
,
1859 return new String_info_expression(string
, string_info
, location
);
1862 // Make an integer expression.
1864 class Integer_expression
: public Expression
1867 Integer_expression(const mpz_t
* val
, Type
* type
, bool is_character_constant
,
1869 : Expression(EXPRESSION_INTEGER
, location
),
1870 type_(type
), is_character_constant_(is_character_constant
)
1871 { mpz_init_set(this->val_
, *val
); }
1876 // Write VAL to string dump.
1878 export_integer(String_dump
* exp
, const mpz_t val
);
1880 // Write VAL to dump context.
1882 dump_integer(Ast_dump_context
* ast_dump_context
, const mpz_t val
);
1886 do_is_constant() const
1890 do_is_immutable() const
1894 do_numeric_constant_value(Numeric_constant
* nc
) const;
1900 do_determine_type(const Type_context
* context
);
1903 do_check_types(Gogo
*);
1906 do_get_backend(Translate_context
*);
1911 if (this->is_character_constant_
)
1912 return Expression::make_character(&this->val_
, this->type_
,
1915 return Expression::make_integer_z(&this->val_
, this->type_
,
1920 do_export(Export
*) const;
1923 do_dump_expression(Ast_dump_context
*) const;
1926 // The integer value.
1930 // Whether this is a character constant.
1931 bool is_character_constant_
;
1934 // Return a numeric constant for this expression. We have to mark
1935 // this as a character when appropriate.
1938 Integer_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
1940 if (this->is_character_constant_
)
1941 nc
->set_rune(this->type_
, this->val_
);
1943 nc
->set_int(this->type_
, this->val_
);
1947 // Return the current type. If we haven't set the type yet, we return
1948 // an abstract integer type.
1951 Integer_expression::do_type()
1953 if (this->type_
== NULL
)
1955 if (this->is_character_constant_
)
1956 this->type_
= Type::make_abstract_character_type();
1958 this->type_
= Type::make_abstract_integer_type();
1963 // Set the type of the integer value. Here we may switch from an
1964 // abstract type to a real type.
1967 Integer_expression::do_determine_type(const Type_context
* context
)
1969 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1971 else if (context
->type
!= NULL
&& context
->type
->is_numeric_type())
1972 this->type_
= context
->type
;
1973 else if (!context
->may_be_abstract
)
1975 if (this->is_character_constant_
)
1976 this->type_
= Type::lookup_integer_type("int32");
1978 this->type_
= Type::lookup_integer_type("int");
1982 // Check the type of an integer constant.
1985 Integer_expression::do_check_types(Gogo
*)
1987 Type
* type
= this->type_
;
1990 Numeric_constant nc
;
1991 if (this->is_character_constant_
)
1992 nc
.set_rune(NULL
, this->val_
);
1994 nc
.set_int(NULL
, this->val_
);
1995 if (!nc
.set_type(type
, true, this->location()))
1996 this->set_is_error();
1999 // Get the backend representation for an integer constant.
2002 Integer_expression::do_get_backend(Translate_context
* context
)
2004 if (this->is_error_expression()
2005 || (this->type_
!= NULL
&& this->type_
->is_error_type()))
2007 go_assert(saw_errors());
2008 return context
->gogo()->backend()->error_expression();
2011 Type
* resolved_type
= NULL
;
2012 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2013 resolved_type
= this->type_
;
2014 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
2016 // We are converting to an abstract floating point type.
2017 resolved_type
= Type::lookup_float_type("float64");
2019 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
2021 // We are converting to an abstract complex type.
2022 resolved_type
= Type::lookup_complex_type("complex128");
2026 // If we still have an abstract type here, then this is being
2027 // used in a constant expression which didn't get reduced for
2028 // some reason. Use a type which will fit the value. We use <,
2029 // not <=, because we need an extra bit for the sign bit.
2030 int bits
= mpz_sizeinbase(this->val_
, 2);
2031 Type
* int_type
= Type::lookup_integer_type("int");
2032 if (bits
< int_type
->integer_type()->bits())
2033 resolved_type
= int_type
;
2035 resolved_type
= Type::lookup_integer_type("int64");
2039 error_at(this->location(),
2040 "unknown type for large integer constant");
2041 return context
->gogo()->backend()->error_expression();
2044 Numeric_constant nc
;
2045 nc
.set_int(resolved_type
, this->val_
);
2046 return Expression::backend_numeric_constant_expression(context
, &nc
);
2049 // Write VAL to export data.
2052 Integer_expression::export_integer(String_dump
* exp
, const mpz_t val
)
2054 char* s
= mpz_get_str(NULL
, 10, val
);
2055 exp
->write_c_string(s
);
2059 // Export an integer in a constant expression.
2062 Integer_expression::do_export(Export
* exp
) const
2064 Integer_expression::export_integer(exp
, this->val_
);
2065 if (this->is_character_constant_
)
2066 exp
->write_c_string("'");
2067 // A trailing space lets us reliably identify the end of the number.
2068 exp
->write_c_string(" ");
2071 // Import an integer, floating point, or complex value. This handles
2072 // all these types because they all start with digits.
2075 Integer_expression::do_import(Import
* imp
)
2077 std::string num
= imp
->read_identifier();
2078 imp
->require_c_string(" ");
2079 if (!num
.empty() && num
[num
.length() - 1] == 'i')
2082 size_t plus_pos
= num
.find('+', 1);
2083 size_t minus_pos
= num
.find('-', 1);
2085 if (plus_pos
== std::string::npos
)
2087 else if (minus_pos
== std::string::npos
)
2091 error_at(imp
->location(), "bad number in import data: %qs",
2093 return Expression::make_error(imp
->location());
2095 if (pos
== std::string::npos
)
2096 mpfr_set_ui(real
, 0, GMP_RNDN
);
2099 std::string real_str
= num
.substr(0, pos
);
2100 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
2102 error_at(imp
->location(), "bad number in import data: %qs",
2104 return Expression::make_error(imp
->location());
2108 std::string imag_str
;
2109 if (pos
== std::string::npos
)
2112 imag_str
= num
.substr(pos
);
2113 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
2115 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
2117 error_at(imp
->location(), "bad number in import data: %qs",
2119 return Expression::make_error(imp
->location());
2122 mpc_init2(cval
, mpc_precision
);
2123 mpc_set_fr_fr(cval
, real
, imag
, MPC_RNDNN
);
2126 Expression
* ret
= Expression::make_complex(&cval
, NULL
, imp
->location());
2130 else if (num
.find('.') == std::string::npos
2131 && num
.find('E') == std::string::npos
)
2133 bool is_character_constant
= (!num
.empty()
2134 && num
[num
.length() - 1] == '\'');
2135 if (is_character_constant
)
2136 num
= num
.substr(0, num
.length() - 1);
2138 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
2140 error_at(imp
->location(), "bad number in import data: %qs",
2142 return Expression::make_error(imp
->location());
2145 if (is_character_constant
)
2146 ret
= Expression::make_character(&val
, NULL
, imp
->location());
2148 ret
= Expression::make_integer_z(&val
, NULL
, imp
->location());
2155 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
2157 error_at(imp
->location(), "bad number in import data: %qs",
2159 return Expression::make_error(imp
->location());
2161 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
2166 // Ast dump for integer expression.
2169 Integer_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2171 if (this->is_character_constant_
)
2172 ast_dump_context
->ostream() << '\'';
2173 Integer_expression::export_integer(ast_dump_context
, this->val_
);
2174 if (this->is_character_constant_
)
2175 ast_dump_context
->ostream() << '\'';
2178 // Build a new integer value from a multi-precision integer.
2181 Expression::make_integer_z(const mpz_t
* val
, Type
* type
, Location location
)
2183 return new Integer_expression(val
, type
, false, location
);
2186 // Build a new integer value from an unsigned long.
2189 Expression::make_integer_ul(unsigned long val
, Type
*type
, Location location
)
2192 mpz_init_set_ui(zval
, val
);
2193 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2198 // Build a new integer value from a signed long.
2201 Expression::make_integer_sl(long val
, Type
*type
, Location location
)
2204 mpz_init_set_si(zval
, val
);
2205 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2210 // Store an int64_t in an uninitialized mpz_t.
2213 set_mpz_from_int64(mpz_t
* zval
, int64_t val
)
2217 unsigned long ul
= static_cast<unsigned long>(val
);
2218 if (static_cast<int64_t>(ul
) == val
)
2220 mpz_init_set_ui(*zval
, ul
);
2226 uv
= static_cast<uint64_t>(val
);
2228 uv
= static_cast<uint64_t>(- val
);
2229 unsigned long ul
= uv
& 0xffffffffUL
;
2230 mpz_init_set_ui(*zval
, ul
);
2232 mpz_init_set_ui(hval
, static_cast<unsigned long>(uv
>> 32));
2233 mpz_mul_2exp(hval
, hval
, 32);
2234 mpz_add(*zval
, *zval
, hval
);
2237 mpz_neg(*zval
, *zval
);
2240 // Build a new integer value from an int64_t.
2243 Expression::make_integer_int64(int64_t val
, Type
* type
, Location location
)
2246 set_mpz_from_int64(&zval
, val
);
2247 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2252 // Build a new character constant value.
2255 Expression::make_character(const mpz_t
* val
, Type
* type
, Location location
)
2257 return new Integer_expression(val
, type
, true, location
);
2262 class Float_expression
: public Expression
2265 Float_expression(const mpfr_t
* val
, Type
* type
, Location location
)
2266 : Expression(EXPRESSION_FLOAT
, location
),
2269 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
2272 // Write VAL to export data.
2274 export_float(String_dump
* exp
, const mpfr_t val
);
2276 // Write VAL to dump file.
2278 dump_float(Ast_dump_context
* ast_dump_context
, const mpfr_t val
);
2282 do_is_constant() const
2286 do_is_immutable() const
2290 do_numeric_constant_value(Numeric_constant
* nc
) const
2292 nc
->set_float(this->type_
, this->val_
);
2300 do_determine_type(const Type_context
*);
2303 do_check_types(Gogo
*);
2307 { return Expression::make_float(&this->val_
, this->type_
,
2308 this->location()); }
2311 do_get_backend(Translate_context
*);
2314 do_export(Export
*) const;
2317 do_dump_expression(Ast_dump_context
*) const;
2320 // The floating point value.
2326 // Return the current type. If we haven't set the type yet, we return
2327 // an abstract float type.
2330 Float_expression::do_type()
2332 if (this->type_
== NULL
)
2333 this->type_
= Type::make_abstract_float_type();
2337 // Set the type of the float value. Here we may switch from an
2338 // abstract type to a real type.
2341 Float_expression::do_determine_type(const Type_context
* context
)
2343 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2345 else if (context
->type
!= NULL
2346 && (context
->type
->integer_type() != NULL
2347 || context
->type
->float_type() != NULL
2348 || context
->type
->complex_type() != NULL
))
2349 this->type_
= context
->type
;
2350 else if (!context
->may_be_abstract
)
2351 this->type_
= Type::lookup_float_type("float64");
2354 // Check the type of a float value.
2357 Float_expression::do_check_types(Gogo
*)
2359 Type
* type
= this->type_
;
2362 Numeric_constant nc
;
2363 nc
.set_float(NULL
, this->val_
);
2364 if (!nc
.set_type(this->type_
, true, this->location()))
2365 this->set_is_error();
2368 // Get the backend representation for a float constant.
2371 Float_expression::do_get_backend(Translate_context
* context
)
2373 if (this->is_error_expression()
2374 || (this->type_
!= NULL
&& this->type_
->is_error_type()))
2376 go_assert(saw_errors());
2377 return context
->gogo()->backend()->error_expression();
2380 Type
* resolved_type
;
2381 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2382 resolved_type
= this->type_
;
2383 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2385 // We have an abstract integer type. We just hope for the best.
2386 resolved_type
= Type::lookup_integer_type("int");
2388 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
2390 // We are converting to an abstract complex type.
2391 resolved_type
= Type::lookup_complex_type("complex128");
2395 // If we still have an abstract type here, then this is being
2396 // used in a constant expression which didn't get reduced. We
2397 // just use float64 and hope for the best.
2398 resolved_type
= Type::lookup_float_type("float64");
2401 Numeric_constant nc
;
2402 nc
.set_float(resolved_type
, this->val_
);
2403 return Expression::backend_numeric_constant_expression(context
, &nc
);
2406 // Write a floating point number to a string dump.
2409 Float_expression::export_float(String_dump
*exp
, const mpfr_t val
)
2412 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2414 exp
->write_c_string("-");
2415 exp
->write_c_string("0.");
2416 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2419 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2420 exp
->write_c_string(buf
);
2423 // Export a floating point number in a constant expression.
2426 Float_expression::do_export(Export
* exp
) const
2428 Float_expression::export_float(exp
, this->val_
);
2429 // A trailing space lets us reliably identify the end of the number.
2430 exp
->write_c_string(" ");
2433 // Dump a floating point number to the dump file.
2436 Float_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2438 Float_expression::export_float(ast_dump_context
, this->val_
);
2441 // Make a float expression.
2444 Expression::make_float(const mpfr_t
* val
, Type
* type
, Location location
)
2446 return new Float_expression(val
, type
, location
);
2451 class Complex_expression
: public Expression
2454 Complex_expression(const mpc_t
* val
, Type
* type
, Location location
)
2455 : Expression(EXPRESSION_COMPLEX
, location
),
2458 mpc_init2(this->val_
, mpc_precision
);
2459 mpc_set(this->val_
, *val
, MPC_RNDNN
);
2462 // Write VAL to string dump.
2464 export_complex(String_dump
* exp
, const mpc_t val
);
2466 // Write REAL/IMAG to dump context.
2468 dump_complex(Ast_dump_context
* ast_dump_context
, const mpc_t val
);
2472 do_is_constant() const
2476 do_is_immutable() const
2480 do_numeric_constant_value(Numeric_constant
* nc
) const
2482 nc
->set_complex(this->type_
, this->val_
);
2490 do_determine_type(const Type_context
*);
2493 do_check_types(Gogo
*);
2498 return Expression::make_complex(&this->val_
, this->type_
,
2503 do_get_backend(Translate_context
*);
2506 do_export(Export
*) const;
2509 do_dump_expression(Ast_dump_context
*) const;
2512 // The complex value.
2514 // The type if known.
2518 // Return the current type. If we haven't set the type yet, we return
2519 // an abstract complex type.
2522 Complex_expression::do_type()
2524 if (this->type_
== NULL
)
2525 this->type_
= Type::make_abstract_complex_type();
2529 // Set the type of the complex value. Here we may switch from an
2530 // abstract type to a real type.
2533 Complex_expression::do_determine_type(const Type_context
* context
)
2535 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2537 else if (context
->type
!= NULL
&& context
->type
->is_numeric_type())
2538 this->type_
= context
->type
;
2539 else if (!context
->may_be_abstract
)
2540 this->type_
= Type::lookup_complex_type("complex128");
2543 // Check the type of a complex value.
2546 Complex_expression::do_check_types(Gogo
*)
2548 Type
* type
= this->type_
;
2551 Numeric_constant nc
;
2552 nc
.set_complex(NULL
, this->val_
);
2553 if (!nc
.set_type(this->type_
, true, this->location()))
2554 this->set_is_error();
2557 // Get the backend representation for a complex constant.
2560 Complex_expression::do_get_backend(Translate_context
* context
)
2562 if (this->is_error_expression()
2563 || (this->type_
!= NULL
&& this->type_
->is_error_type()))
2565 go_assert(saw_errors());
2566 return context
->gogo()->backend()->error_expression();
2569 Type
* resolved_type
;
2570 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2571 resolved_type
= this->type_
;
2572 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2574 // We are converting to an abstract integer type.
2575 resolved_type
= Type::lookup_integer_type("int");
2577 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
2579 // We are converting to an abstract float type.
2580 resolved_type
= Type::lookup_float_type("float64");
2584 // If we still have an abstract type here, this is being
2585 // used in a constant expression which didn't get reduced. We
2586 // just use complex128 and hope for the best.
2587 resolved_type
= Type::lookup_complex_type("complex128");
2590 Numeric_constant nc
;
2591 nc
.set_complex(resolved_type
, this->val_
);
2592 return Expression::backend_numeric_constant_expression(context
, &nc
);
2595 // Write REAL/IMAG to export data.
2598 Complex_expression::export_complex(String_dump
* exp
, const mpc_t val
)
2600 if (!mpfr_zero_p(mpc_realref(val
)))
2602 Float_expression::export_float(exp
, mpc_realref(val
));
2603 if (mpfr_sgn(mpc_imagref(val
)) >= 0)
2604 exp
->write_c_string("+");
2606 Float_expression::export_float(exp
, mpc_imagref(val
));
2607 exp
->write_c_string("i");
2610 // Export a complex number in a constant expression.
2613 Complex_expression::do_export(Export
* exp
) const
2615 Complex_expression::export_complex(exp
, this->val_
);
2616 // A trailing space lets us reliably identify the end of the number.
2617 exp
->write_c_string(" ");
2620 // Dump a complex expression to the dump file.
2623 Complex_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2625 Complex_expression::export_complex(ast_dump_context
, this->val_
);
2628 // Make a complex expression.
2631 Expression::make_complex(const mpc_t
* val
, Type
* type
, Location location
)
2633 return new Complex_expression(val
, type
, location
);
2636 // Find a named object in an expression.
2638 class Find_named_object
: public Traverse
2641 Find_named_object(Named_object
* no
)
2642 : Traverse(traverse_expressions
),
2643 no_(no
), found_(false)
2646 // Whether we found the object.
2649 { return this->found_
; }
2653 expression(Expression
**);
2656 // The object we are looking for.
2658 // Whether we found it.
2662 // A reference to a const in an expression.
2664 class Const_expression
: public Expression
2667 Const_expression(Named_object
* constant
, Location location
)
2668 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2669 constant_(constant
), type_(NULL
), seen_(false)
2674 { return this->constant_
; }
2676 // Check that the initializer does not refer to the constant itself.
2678 check_for_init_loop();
2682 do_traverse(Traverse
*);
2685 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2688 do_is_constant() const
2692 do_is_immutable() const
2696 do_numeric_constant_value(Numeric_constant
* nc
) const;
2699 do_string_constant_value(std::string
* val
) const;
2704 // The type of a const is set by the declaration, not the use.
2706 do_determine_type(const Type_context
*);
2709 do_check_types(Gogo
*);
2716 do_get_backend(Translate_context
* context
);
2718 // When exporting a reference to a const as part of a const
2719 // expression, we export the value. We ignore the fact that it has
2722 do_export(Export
* exp
) const
2723 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2726 do_dump_expression(Ast_dump_context
*) const;
2730 Named_object
* constant_
;
2731 // The type of this reference. This is used if the constant has an
2734 // Used to prevent infinite recursion when a constant incorrectly
2735 // refers to itself.
2742 Const_expression::do_traverse(Traverse
* traverse
)
2744 if (this->type_
!= NULL
)
2745 return Type::traverse(this->type_
, traverse
);
2746 return TRAVERSE_CONTINUE
;
2749 // Lower a constant expression. This is where we convert the
2750 // predeclared constant iota into an integer value.
2753 Const_expression::do_lower(Gogo
* gogo
, Named_object
*,
2754 Statement_inserter
*, int iota_value
)
2756 if (this->constant_
->const_value()->expr()->classification()
2759 if (iota_value
== -1)
2761 error_at(this->location(),
2762 "iota is only defined in const declarations");
2765 return Expression::make_integer_ul(iota_value
, NULL
, this->location());
2768 // Make sure that the constant itself has been lowered.
2769 gogo
->lower_constant(this->constant_
);
2774 // Return a numeric constant value.
2777 Const_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
2782 Expression
* e
= this->constant_
->const_value()->expr();
2786 bool r
= e
->numeric_constant_value(nc
);
2788 this->seen_
= false;
2791 if (this->type_
!= NULL
)
2792 ctype
= this->type_
;
2794 ctype
= this->constant_
->const_value()->type();
2795 if (r
&& ctype
!= NULL
)
2797 if (!nc
->set_type(ctype
, false, this->location()))
2805 Const_expression::do_string_constant_value(std::string
* val
) const
2810 Expression
* e
= this->constant_
->const_value()->expr();
2813 bool ok
= e
->string_constant_value(val
);
2814 this->seen_
= false;
2819 // Return the type of the const reference.
2822 Const_expression::do_type()
2824 if (this->type_
!= NULL
)
2827 Named_constant
* nc
= this->constant_
->const_value();
2829 if (this->seen_
|| nc
->lowering())
2831 this->report_error(_("constant refers to itself"));
2832 this->type_
= Type::make_error_type();
2838 Type
* ret
= nc
->type();
2842 this->seen_
= false;
2846 // During parsing, a named constant may have a NULL type, but we
2847 // must not return a NULL type here.
2848 ret
= nc
->expr()->type();
2850 this->seen_
= false;
2855 // Set the type of the const reference.
2858 Const_expression::do_determine_type(const Type_context
* context
)
2860 Type
* ctype
= this->constant_
->const_value()->type();
2861 Type
* cetype
= (ctype
!= NULL
2863 : this->constant_
->const_value()->expr()->type());
2864 if (ctype
!= NULL
&& !ctype
->is_abstract())
2866 else if (context
->type
!= NULL
2867 && context
->type
->is_numeric_type()
2868 && cetype
->is_numeric_type())
2869 this->type_
= context
->type
;
2870 else if (context
->type
!= NULL
2871 && context
->type
->is_string_type()
2872 && cetype
->is_string_type())
2873 this->type_
= context
->type
;
2874 else if (context
->type
!= NULL
2875 && context
->type
->is_boolean_type()
2876 && cetype
->is_boolean_type())
2877 this->type_
= context
->type
;
2878 else if (!context
->may_be_abstract
)
2880 if (cetype
->is_abstract())
2881 cetype
= cetype
->make_non_abstract_type();
2882 this->type_
= cetype
;
2886 // Check for a loop in which the initializer of a constant refers to
2887 // the constant itself.
2890 Const_expression::check_for_init_loop()
2892 if (this->type_
!= NULL
&& this->type_
->is_error())
2897 this->report_error(_("constant refers to itself"));
2898 this->type_
= Type::make_error_type();
2902 Expression
* init
= this->constant_
->const_value()->expr();
2903 Find_named_object
find_named_object(this->constant_
);
2906 Expression::traverse(&init
, &find_named_object
);
2907 this->seen_
= false;
2909 if (find_named_object
.found())
2911 if (this->type_
== NULL
|| !this->type_
->is_error())
2913 this->report_error(_("constant refers to itself"));
2914 this->type_
= Type::make_error_type();
2920 // Check types of a const reference.
2923 Const_expression::do_check_types(Gogo
*)
2925 if (this->type_
!= NULL
&& this->type_
->is_error())
2928 this->check_for_init_loop();
2930 // Check that numeric constant fits in type.
2931 if (this->type_
!= NULL
&& this->type_
->is_numeric_type())
2933 Numeric_constant nc
;
2934 if (this->constant_
->const_value()->expr()->numeric_constant_value(&nc
))
2936 if (!nc
.set_type(this->type_
, true, this->location()))
2937 this->set_is_error();
2942 // Return the backend representation for a const reference.
2945 Const_expression::do_get_backend(Translate_context
* context
)
2947 if (this->is_error_expression()
2948 || (this->type_
!= NULL
&& this->type_
->is_error()))
2950 go_assert(saw_errors());
2951 return context
->backend()->error_expression();
2954 // If the type has been set for this expression, but the underlying
2955 // object is an abstract int or float, we try to get the abstract
2956 // value. Otherwise we may lose something in the conversion.
2957 Expression
* expr
= this->constant_
->const_value()->expr();
2958 if (this->type_
!= NULL
2959 && this->type_
->is_numeric_type()
2960 && (this->constant_
->const_value()->type() == NULL
2961 || this->constant_
->const_value()->type()->is_abstract()))
2963 Numeric_constant nc
;
2964 if (expr
->numeric_constant_value(&nc
)
2965 && nc
.set_type(this->type_
, false, this->location()))
2967 Expression
* e
= nc
.expression(this->location());
2968 return e
->get_backend(context
);
2972 if (this->type_
!= NULL
)
2973 expr
= Expression::make_cast(this->type_
, expr
, this->location());
2974 return expr
->get_backend(context
);
2977 // Dump ast representation for constant expression.
2980 Const_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2982 ast_dump_context
->ostream() << this->constant_
->name();
2985 // Make a reference to a constant in an expression.
2988 Expression::make_const_reference(Named_object
* constant
,
2991 return new Const_expression(constant
, location
);
2994 // Find a named object in an expression.
2997 Find_named_object::expression(Expression
** pexpr
)
2999 switch ((*pexpr
)->classification())
3001 case Expression::EXPRESSION_CONST_REFERENCE
:
3003 Const_expression
* ce
= static_cast<Const_expression
*>(*pexpr
);
3004 if (ce
->named_object() == this->no_
)
3007 // We need to check a constant initializer explicitly, as
3008 // loops here will not be caught by the loop checking for
3009 // variable initializers.
3010 ce
->check_for_init_loop();
3012 return TRAVERSE_CONTINUE
;
3015 case Expression::EXPRESSION_VAR_REFERENCE
:
3016 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
3018 return TRAVERSE_CONTINUE
;
3019 case Expression::EXPRESSION_FUNC_REFERENCE
:
3020 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
3022 return TRAVERSE_CONTINUE
;
3024 return TRAVERSE_CONTINUE
;
3026 this->found_
= true;
3027 return TRAVERSE_EXIT
;
3032 class Nil_expression
: public Expression
3035 Nil_expression(Location location
)
3036 : Expression(EXPRESSION_NIL
, location
)
3044 do_is_constant() const
3048 do_is_immutable() const
3053 { return Type::make_nil_type(); }
3056 do_determine_type(const Type_context
*)
3064 do_get_backend(Translate_context
* context
)
3065 { return context
->backend()->nil_pointer_expression(); }
3068 do_export(Export
* exp
) const
3069 { exp
->write_c_string("nil"); }
3072 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3073 { ast_dump_context
->ostream() << "nil"; }
3076 // Import a nil expression.
3079 Nil_expression::do_import(Import
* imp
)
3081 imp
->require_c_string("nil");
3082 return Expression::make_nil(imp
->location());
3085 // Make a nil expression.
3088 Expression::make_nil(Location location
)
3090 return new Nil_expression(location
);
3093 // The value of the predeclared constant iota. This is little more
3094 // than a marker. This will be lowered to an integer in
3095 // Const_expression::do_lower, which is where we know the value that
3098 class Iota_expression
: public Parser_expression
3101 Iota_expression(Location location
)
3102 : Parser_expression(EXPRESSION_IOTA
, location
)
3107 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
3108 { go_unreachable(); }
3110 // There should only ever be one of these.
3113 { go_unreachable(); }
3116 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
3117 { ast_dump_context
->ostream() << "iota"; }
3120 // Make an iota expression. This is only called for one case: the
3121 // value of the predeclared constant iota.
3124 Expression::make_iota()
3126 static Iota_expression
iota_expression(Linemap::unknown_location());
3127 return &iota_expression
;
3130 // Class Type_conversion_expression.
3135 Type_conversion_expression::do_traverse(Traverse
* traverse
)
3137 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3138 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3139 return TRAVERSE_EXIT
;
3140 return TRAVERSE_CONTINUE
;
3143 // Convert to a constant at lowering time.
3146 Type_conversion_expression::do_lower(Gogo
*, Named_object
*,
3147 Statement_inserter
*, int)
3149 Type
* type
= this->type_
;
3150 Expression
* val
= this->expr_
;
3151 Location location
= this->location();
3153 if (type
->is_numeric_type())
3155 Numeric_constant nc
;
3156 if (val
->numeric_constant_value(&nc
))
3158 if (!nc
.set_type(type
, true, location
))
3159 return Expression::make_error(location
);
3160 return nc
.expression(location
);
3164 // According to the language specification on string conversions
3165 // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type):
3166 // When converting an integer into a string, the string will be a UTF-8
3167 // representation of the integer and integers "outside the range of valid
3168 // Unicode code points are converted to '\uFFFD'."
3169 if (type
->is_string_type())
3171 Numeric_constant nc
;
3172 if (val
->numeric_constant_value(&nc
) && nc
.is_int())
3174 // An integer value doesn't fit in the Unicode code point range if it
3175 // overflows the Go "int" type or is negative.
3177 if (!nc
.set_type(Type::lookup_integer_type("int"), false, location
)
3178 || nc
.to_unsigned_long(&ul
) == Numeric_constant::NC_UL_NEGATIVE
)
3179 return Expression::make_string("\ufffd", location
);
3183 if (type
->is_slice_type())
3185 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3186 bool is_byte
= (element_type
->integer_type() != NULL
3187 && element_type
->integer_type()->is_byte());
3188 bool is_rune
= (element_type
->integer_type() != NULL
3189 && element_type
->integer_type()->is_rune());
3190 if (is_byte
|| is_rune
)
3193 if (val
->string_constant_value(&s
))
3195 Expression_list
* vals
= new Expression_list();
3198 for (std::string::const_iterator p
= s
.begin();
3202 unsigned char c
= static_cast<unsigned char>(*p
);
3203 vals
->push_back(Expression::make_integer_ul(c
,
3210 const char *p
= s
.data();
3211 const char *pend
= s
.data() + s
.length();
3215 int adv
= Lex::fetch_char(p
, &c
);
3218 warning_at(this->location(), 0,
3219 "invalid UTF-8 encoding");
3223 vals
->push_back(Expression::make_integer_ul(c
,
3229 return Expression::make_slice_composite_literal(type
, vals
,
3238 // Flatten a type conversion by using a temporary variable for the slice
3239 // in slice to string conversions.
3242 Type_conversion_expression::do_flatten(Gogo
*, Named_object
*,
3243 Statement_inserter
* inserter
)
3245 if (this->type()->is_error_type() || this->expr_
->is_error_expression())
3247 go_assert(saw_errors());
3248 return Expression::make_error(this->location());
3251 if (((this->type()->is_string_type()
3252 && this->expr_
->type()->is_slice_type())
3253 || this->expr_
->type()->interface_type() != NULL
)
3254 && !this->expr_
->is_variable())
3256 Temporary_statement
* temp
=
3257 Statement::make_temporary(NULL
, this->expr_
, this->location());
3258 inserter
->insert(temp
);
3259 this->expr_
= Expression::make_temporary_reference(temp
, this->location());
3264 // Return whether a type conversion is a constant.
3267 Type_conversion_expression::do_is_constant() const
3269 if (!this->expr_
->is_constant())
3272 // A conversion to a type that may not be used as a constant is not
3273 // a constant. For example, []byte(nil).
3274 Type
* type
= this->type_
;
3275 if (type
->integer_type() == NULL
3276 && type
->float_type() == NULL
3277 && type
->complex_type() == NULL
3278 && !type
->is_boolean_type()
3279 && !type
->is_string_type())
3285 // Return whether a type conversion is immutable.
3288 Type_conversion_expression::do_is_immutable() const
3290 Type
* type
= this->type_
;
3291 Type
* expr_type
= this->expr_
->type();
3293 if (type
->interface_type() != NULL
3294 || expr_type
->interface_type() != NULL
)
3297 if (!this->expr_
->is_immutable())
3300 if (Type::are_identical(type
, expr_type
, false, NULL
))
3303 return type
->is_basic_type() && expr_type
->is_basic_type();
3306 // Return the constant numeric value if there is one.
3309 Type_conversion_expression::do_numeric_constant_value(
3310 Numeric_constant
* nc
) const
3312 if (!this->type_
->is_numeric_type())
3314 if (!this->expr_
->numeric_constant_value(nc
))
3316 return nc
->set_type(this->type_
, false, this->location());
3319 // Return the constant string value if there is one.
3322 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3324 if (this->type_
->is_string_type()
3325 && this->expr_
->type()->integer_type() != NULL
)
3327 Numeric_constant nc
;
3328 if (this->expr_
->numeric_constant_value(&nc
))
3331 if (nc
.to_unsigned_long(&ival
) == Numeric_constant::NC_UL_VALID
)
3334 Lex::append_char(ival
, true, val
, this->location());
3340 // FIXME: Could handle conversion from const []int here.
3345 // Determine the resulting type of the conversion.
3348 Type_conversion_expression::do_determine_type(const Type_context
*)
3350 Type_context
subcontext(this->type_
, false);
3351 this->expr_
->determine_type(&subcontext
);
3354 // Check that types are convertible.
3357 Type_conversion_expression::do_check_types(Gogo
*)
3359 Type
* type
= this->type_
;
3360 Type
* expr_type
= this->expr_
->type();
3363 if (type
->is_error() || expr_type
->is_error())
3365 this->set_is_error();
3369 if (this->may_convert_function_types_
3370 && type
->function_type() != NULL
3371 && expr_type
->function_type() != NULL
)
3374 if (Type::are_convertible(type
, expr_type
, &reason
))
3377 error_at(this->location(), "%s", reason
.c_str());
3378 this->set_is_error();
3381 // Get the backend representation for a type conversion.
3384 Type_conversion_expression::do_get_backend(Translate_context
* context
)
3386 Type
* type
= this->type_
;
3387 Type
* expr_type
= this->expr_
->type();
3389 Gogo
* gogo
= context
->gogo();
3390 Btype
* btype
= type
->get_backend(gogo
);
3391 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
3392 Location loc
= this->location();
3394 if (Type::are_identical(type
, expr_type
, false, NULL
))
3395 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3396 else if (type
->interface_type() != NULL
3397 || expr_type
->interface_type() != NULL
)
3399 Expression
* conversion
=
3400 Expression::convert_for_assignment(gogo
, type
, this->expr_
,
3402 return conversion
->get_backend(context
);
3404 else if (type
->is_string_type()
3405 && expr_type
->integer_type() != NULL
)
3408 Numeric_constant nc
;
3409 if (this->expr_
->numeric_constant_value(&nc
)
3410 && nc
.to_int(&intval
)
3411 && mpz_fits_ushort_p(intval
))
3414 Lex::append_char(mpz_get_ui(intval
), true, &s
, loc
);
3416 Expression
* se
= Expression::make_string(s
, loc
);
3417 return se
->get_backend(context
);
3420 Expression
* i2s_expr
=
3421 Runtime::make_call(Runtime::INT_TO_STRING
, loc
, 1, this->expr_
);
3422 return Expression::make_cast(type
, i2s_expr
, loc
)->get_backend(context
);
3424 else if (type
->is_string_type() && expr_type
->is_slice_type())
3426 Array_type
* a
= expr_type
->array_type();
3427 Type
* e
= a
->element_type()->forwarded();
3428 go_assert(e
->integer_type() != NULL
);
3429 go_assert(this->expr_
->is_variable());
3431 Runtime::Function code
;
3432 if (e
->integer_type()->is_byte())
3433 code
= Runtime::BYTE_ARRAY_TO_STRING
;
3436 go_assert(e
->integer_type()->is_rune());
3437 code
= Runtime::INT_ARRAY_TO_STRING
;
3439 Expression
* valptr
= a
->get_value_pointer(gogo
, this->expr_
);
3440 Expression
* len
= a
->get_length(gogo
, this->expr_
);
3441 return Runtime::make_call(code
, loc
, 2, valptr
,
3442 len
)->get_backend(context
);
3444 else if (type
->is_slice_type() && expr_type
->is_string_type())
3446 Type
* e
= type
->array_type()->element_type()->forwarded();
3447 go_assert(e
->integer_type() != NULL
);
3449 Runtime::Function code
;
3450 if (e
->integer_type()->is_byte())
3451 code
= Runtime::STRING_TO_BYTE_ARRAY
;
3454 go_assert(e
->integer_type()->is_rune());
3455 code
= Runtime::STRING_TO_INT_ARRAY
;
3457 Expression
* s2a
= Runtime::make_call(code
, loc
, 1, this->expr_
);
3458 return Expression::make_unsafe_cast(type
, s2a
, loc
)->get_backend(context
);
3460 else if (type
->is_numeric_type())
3462 go_assert(Type::are_convertible(type
, expr_type
, NULL
));
3463 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3465 else if ((type
->is_unsafe_pointer_type()
3466 && (expr_type
->points_to() != NULL
3467 || expr_type
->integer_type()))
3468 || (expr_type
->is_unsafe_pointer_type()
3469 && type
->points_to() != NULL
)
3470 || (this->may_convert_function_types_
3471 && type
->function_type() != NULL
3472 && expr_type
->function_type() != NULL
))
3473 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3476 Expression
* conversion
=
3477 Expression::convert_for_assignment(gogo
, type
, this->expr_
, loc
);
3478 return conversion
->get_backend(context
);
3482 // Output a type conversion in a constant expression.
3485 Type_conversion_expression::do_export(Export
* exp
) const
3487 exp
->write_c_string("convert(");
3488 exp
->write_type(this->type_
);
3489 exp
->write_c_string(", ");
3490 this->expr_
->export_expression(exp
);
3491 exp
->write_c_string(")");
3494 // Import a type conversion or a struct construction.
3497 Type_conversion_expression::do_import(Import
* imp
)
3499 imp
->require_c_string("convert(");
3500 Type
* type
= imp
->read_type();
3501 imp
->require_c_string(", ");
3502 Expression
* val
= Expression::import_expression(imp
);
3503 imp
->require_c_string(")");
3504 return Expression::make_cast(type
, val
, imp
->location());
3507 // Dump ast representation for a type conversion expression.
3510 Type_conversion_expression::do_dump_expression(
3511 Ast_dump_context
* ast_dump_context
) const
3513 ast_dump_context
->dump_type(this->type_
);
3514 ast_dump_context
->ostream() << "(";
3515 ast_dump_context
->dump_expression(this->expr_
);
3516 ast_dump_context
->ostream() << ") ";
3519 // Make a type cast expression.
3522 Expression::make_cast(Type
* type
, Expression
* val
, Location location
)
3524 if (type
->is_error_type() || val
->is_error_expression())
3525 return Expression::make_error(location
);
3526 return new Type_conversion_expression(type
, val
, location
);
3529 // Class Unsafe_type_conversion_expression.
3534 Unsafe_type_conversion_expression::do_traverse(Traverse
* traverse
)
3536 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3537 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3538 return TRAVERSE_EXIT
;
3539 return TRAVERSE_CONTINUE
;
3542 // Return whether an unsafe type conversion is immutable.
3545 Unsafe_type_conversion_expression::do_is_immutable() const
3547 Type
* type
= this->type_
;
3548 Type
* expr_type
= this->expr_
->type();
3550 if (type
->interface_type() != NULL
3551 || expr_type
->interface_type() != NULL
)
3554 if (!this->expr_
->is_immutable())
3557 if (Type::are_convertible(type
, expr_type
, NULL
))
3560 return type
->is_basic_type() && expr_type
->is_basic_type();
3563 // Convert to backend representation.
3566 Unsafe_type_conversion_expression::do_get_backend(Translate_context
* context
)
3568 // We are only called for a limited number of cases.
3570 Type
* t
= this->type_
;
3571 Type
* et
= this->expr_
->type();
3573 if (t
->is_error_type()
3574 || this->expr_
->is_error_expression()
3575 || et
->is_error_type())
3577 go_assert(saw_errors());
3578 return context
->backend()->error_expression();
3581 if (t
->array_type() != NULL
)
3582 go_assert(et
->array_type() != NULL
3583 && t
->is_slice_type() == et
->is_slice_type());
3584 else if (t
->struct_type() != NULL
)
3586 if (t
->named_type() != NULL
3587 && et
->named_type() != NULL
3588 && !Type::are_convertible(t
, et
, NULL
))
3590 go_assert(saw_errors());
3591 return context
->backend()->error_expression();
3594 go_assert(et
->struct_type() != NULL
3595 && Type::are_convertible(t
, et
, NULL
));
3597 else if (t
->map_type() != NULL
)
3598 go_assert(et
->map_type() != NULL
);
3599 else if (t
->channel_type() != NULL
)
3600 go_assert(et
->channel_type() != NULL
);
3601 else if (t
->points_to() != NULL
)
3602 go_assert(et
->points_to() != NULL
3603 || et
->channel_type() != NULL
3604 || et
->map_type() != NULL
3605 || et
->function_type() != NULL
3606 || et
->is_nil_type());
3607 else if (et
->is_unsafe_pointer_type())
3608 go_assert(t
->points_to() != NULL
);
3609 else if (t
->interface_type() != NULL
)
3611 bool empty_iface
= t
->interface_type()->is_empty();
3612 go_assert(et
->interface_type() != NULL
3613 && et
->interface_type()->is_empty() == empty_iface
);
3615 else if (t
->integer_type() != NULL
)
3616 go_assert(et
->is_boolean_type()
3617 || et
->integer_type() != NULL
3618 || et
->function_type() != NULL
3619 || et
->points_to() != NULL
3620 || et
->map_type() != NULL
3621 || et
->channel_type() != NULL
3622 || et
->is_nil_type());
3626 Gogo
* gogo
= context
->gogo();
3627 Btype
* btype
= t
->get_backend(gogo
);
3628 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
3629 Location loc
= this->location();
3630 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3633 // Dump ast representation for an unsafe type conversion expression.
3636 Unsafe_type_conversion_expression::do_dump_expression(
3637 Ast_dump_context
* ast_dump_context
) const
3639 ast_dump_context
->dump_type(this->type_
);
3640 ast_dump_context
->ostream() << "(";
3641 ast_dump_context
->dump_expression(this->expr_
);
3642 ast_dump_context
->ostream() << ") ";
3645 // Make an unsafe type conversion expression.
3648 Expression::make_unsafe_cast(Type
* type
, Expression
* expr
,
3651 return new Unsafe_type_conversion_expression(type
, expr
, location
);
3654 // Class Unary_expression.
3656 // If we are taking the address of a composite literal, and the
3657 // contents are not constant, then we want to make a heap expression
3661 Unary_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
3663 Location loc
= this->location();
3664 Operator op
= this->op_
;
3665 Expression
* expr
= this->expr_
;
3667 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3668 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3670 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3671 // moving x to the heap. FIXME: Is it worth doing a real escape
3672 // analysis here? This case is found in math/unsafe.go and is
3673 // therefore worth special casing.
3674 if (op
== OPERATOR_MULT
)
3676 Expression
* e
= expr
;
3677 while (e
->classification() == EXPRESSION_CONVERSION
)
3679 Type_conversion_expression
* te
3680 = static_cast<Type_conversion_expression
*>(e
);
3684 if (e
->classification() == EXPRESSION_UNARY
)
3686 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3687 if (ue
->op_
== OPERATOR_AND
)
3692 if (!ue
->expr_
->is_addressable() && !ue
->create_temp_
)
3694 error_at(ue
->location(),
3695 "invalid operand for unary %<&%>");
3696 this->set_is_error();
3700 ue
->set_does_not_escape();
3705 // Catching an invalid indirection of unsafe.Pointer here avoid
3706 // having to deal with TYPE_VOID in other places.
3707 if (op
== OPERATOR_MULT
&& expr
->type()->is_unsafe_pointer_type())
3709 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3710 return Expression::make_error(this->location());
3713 // Check for an invalid pointer dereference. We need to do this
3714 // here because Unary_expression::do_type will return an error type
3715 // in this case. That can cause code to appear erroneous, and
3716 // therefore disappear at lowering time, without any error message.
3717 if (op
== OPERATOR_MULT
&& expr
->type()->points_to() == NULL
)
3719 this->report_error(_("expected pointer"));
3720 return Expression::make_error(this->location());
3723 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
|| op
== OPERATOR_XOR
)
3725 Numeric_constant nc
;
3726 if (expr
->numeric_constant_value(&nc
))
3728 Numeric_constant result
;
3729 if (Unary_expression::eval_constant(op
, &nc
, loc
, &result
))
3730 return result
.expression(loc
);
3737 // Flatten expression if a nil check must be performed and create temporary
3738 // variables if necessary.
3741 Unary_expression::do_flatten(Gogo
* gogo
, Named_object
*,
3742 Statement_inserter
* inserter
)
3744 if (this->is_error_expression()
3745 || this->expr_
->is_error_expression()
3746 || this->expr_
->type()->is_error_type())
3748 go_assert(saw_errors());
3749 return Expression::make_error(this->location());
3752 Location location
= this->location();
3753 if (this->op_
== OPERATOR_MULT
3754 && !this->expr_
->is_variable())
3756 go_assert(this->expr_
->type()->points_to() != NULL
);
3757 Type
* ptype
= this->expr_
->type()->points_to();
3758 if (!ptype
->is_void_type())
3761 bool ok
= ptype
->backend_type_size(gogo
, &s
);
3764 go_assert(saw_errors());
3765 return Expression::make_error(this->location());
3767 if (s
>= 4096 || this->issue_nil_check_
)
3769 Temporary_statement
* temp
=
3770 Statement::make_temporary(NULL
, this->expr_
, location
);
3771 inserter
->insert(temp
);
3773 Expression::make_temporary_reference(temp
, location
);
3778 if (this->op_
== OPERATOR_AND
)
3780 // If this->escapes_ is false at this point, then it was set to
3781 // false by an explicit call to set_does_not_escape, and the
3782 // value does not escape. If this->escapes_ is true, we may be
3783 // able to set it to false if taking the address of a variable
3784 // that does not escape.
3785 Node
* n
= Node::make_node(this);
3786 if ((n
->encoding() & ESCAPE_MASK
) == int(Node::ESCAPE_NONE
))
3787 this->escapes_
= false;
3789 Named_object
* var
= NULL
;
3790 if (this->expr_
->var_expression() != NULL
)
3791 var
= this->expr_
->var_expression()->named_object();
3792 else if (this->expr_
->enclosed_var_expression() != NULL
)
3793 var
= this->expr_
->enclosed_var_expression()->variable();
3795 if (this->escapes_
&& var
!= NULL
)
3797 if (var
->is_variable())
3798 this->escapes_
= var
->var_value()->escapes();
3799 if (var
->is_result_variable())
3800 this->escapes_
= var
->result_var_value()->escapes();
3802 this->expr_
->address_taken(this->escapes_
);
3805 if (this->create_temp_
&& !this->expr_
->is_variable())
3807 Temporary_statement
* temp
=
3808 Statement::make_temporary(NULL
, this->expr_
, location
);
3809 inserter
->insert(temp
);
3810 this->expr_
= Expression::make_temporary_reference(temp
, location
);
3816 // Return whether a unary expression is a constant.
3819 Unary_expression::do_is_constant() const
3821 if (this->op_
== OPERATOR_MULT
)
3823 // Indirecting through a pointer is only constant if the object
3824 // to which the expression points is constant, but we currently
3825 // have no way to determine that.
3828 else if (this->op_
== OPERATOR_AND
)
3830 // Taking the address of a variable is constant if it is a
3831 // global variable, not constant otherwise. In other cases taking the
3832 // address is probably not a constant.
3833 Var_expression
* ve
= this->expr_
->var_expression();
3836 Named_object
* no
= ve
->named_object();
3837 return no
->is_variable() && no
->var_value()->is_global();
3842 return this->expr_
->is_constant();
3845 // Apply unary opcode OP to UNC, setting NC. Return true if this
3846 // could be done, false if not. Issue errors for overflow.
3849 Unary_expression::eval_constant(Operator op
, const Numeric_constant
* unc
,
3850 Location location
, Numeric_constant
* nc
)
3858 case OPERATOR_MINUS
:
3859 if (unc
->is_int() || unc
->is_rune())
3861 else if (unc
->is_float())
3864 unc
->get_float(&uval
);
3867 mpfr_neg(val
, uval
, GMP_RNDN
);
3868 nc
->set_float(unc
->type(), val
);
3873 else if (unc
->is_complex())
3876 unc
->get_complex(&uval
);
3878 mpc_init2(val
, mpc_precision
);
3879 mpc_neg(val
, uval
, MPC_RNDNN
);
3880 nc
->set_complex(unc
->type(), val
);
3900 if (!unc
->is_int() && !unc
->is_rune())
3905 unc
->get_rune(&uval
);
3907 unc
->get_int(&uval
);
3913 case OPERATOR_MINUS
:
3918 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3923 Type
* utype
= unc
->type();
3924 if (utype
->integer_type() == NULL
3925 || utype
->integer_type()->is_abstract())
3929 // The number of HOST_WIDE_INTs that it takes to represent
3931 size_t count
= ((mpz_sizeinbase(uval
, 2)
3932 + HOST_BITS_PER_WIDE_INT
3934 / HOST_BITS_PER_WIDE_INT
);
3936 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3937 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3939 size_t obits
= utype
->integer_type()->bits();
3941 if (!utype
->integer_type()->is_unsigned() && mpz_sgn(uval
) < 0)
3944 mpz_init_set_ui(adj
, 1);
3945 mpz_mul_2exp(adj
, adj
, obits
);
3946 mpz_add(uval
, uval
, adj
);
3951 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3952 go_assert(ecount
<= count
);
3954 // Trim down to the number of words required by the type.
3955 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3956 / HOST_BITS_PER_WIDE_INT
);
3957 go_assert(ocount
<= count
);
3959 for (size_t i
= 0; i
< ocount
; ++i
)
3962 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3964 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3967 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3969 if (!utype
->integer_type()->is_unsigned()
3970 && mpz_tstbit(val
, obits
- 1))
3973 mpz_init_set_ui(adj
, 1);
3974 mpz_mul_2exp(adj
, adj
, obits
);
3975 mpz_sub(val
, val
, adj
);
3989 nc
->set_rune(NULL
, val
);
3991 nc
->set_int(NULL
, val
);
3996 return nc
->set_type(unc
->type(), true, location
);
3999 // Return the integral constant value of a unary expression, if it has one.
4002 Unary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
4004 Numeric_constant unc
;
4005 if (!this->expr_
->numeric_constant_value(&unc
))
4007 return Unary_expression::eval_constant(this->op_
, &unc
, this->location(),
4011 // Return the type of a unary expression.
4014 Unary_expression::do_type()
4019 case OPERATOR_MINUS
:
4022 return this->expr_
->type();
4025 return Type::make_pointer_type(this->expr_
->type());
4029 Type
* subtype
= this->expr_
->type();
4030 Type
* points_to
= subtype
->points_to();
4031 if (points_to
== NULL
)
4032 return Type::make_error_type();
4041 // Determine abstract types for a unary expression.
4044 Unary_expression::do_determine_type(const Type_context
* context
)
4049 case OPERATOR_MINUS
:
4052 this->expr_
->determine_type(context
);
4056 // Taking the address of something.
4058 Type
* subtype
= (context
->type
== NULL
4060 : context
->type
->points_to());
4061 Type_context
subcontext(subtype
, false);
4062 this->expr_
->determine_type(&subcontext
);
4067 // Indirecting through a pointer.
4069 Type
* subtype
= (context
->type
== NULL
4071 : Type::make_pointer_type(context
->type
));
4072 Type_context
subcontext(subtype
, false);
4073 this->expr_
->determine_type(&subcontext
);
4082 // Check types for a unary expression.
4085 Unary_expression::do_check_types(Gogo
*)
4087 Type
* type
= this->expr_
->type();
4088 if (type
->is_error())
4090 this->set_is_error();
4097 case OPERATOR_MINUS
:
4098 if (type
->integer_type() == NULL
4099 && type
->float_type() == NULL
4100 && type
->complex_type() == NULL
)
4101 this->report_error(_("expected numeric type"));
4105 if (!type
->is_boolean_type())
4106 this->report_error(_("expected boolean type"));
4110 if (type
->integer_type() == NULL
)
4111 this->report_error(_("expected integer"));
4115 if (!this->expr_
->is_addressable())
4117 if (!this->create_temp_
)
4119 error_at(this->location(), "invalid operand for unary %<&%>");
4120 this->set_is_error();
4124 this->expr_
->issue_nil_check();
4128 // Indirecting through a pointer.
4129 if (type
->points_to() == NULL
)
4130 this->report_error(_("expected pointer"));
4131 if (type
->points_to()->is_error())
4132 this->set_is_error();
4140 // Get the backend representation for a unary expression.
4143 Unary_expression::do_get_backend(Translate_context
* context
)
4145 Gogo
* gogo
= context
->gogo();
4146 Location loc
= this->location();
4148 // Taking the address of a set-and-use-temporary expression requires
4149 // setting the temporary and then taking the address.
4150 if (this->op_
== OPERATOR_AND
)
4152 Set_and_use_temporary_expression
* sut
=
4153 this->expr_
->set_and_use_temporary_expression();
4156 Temporary_statement
* temp
= sut
->temporary();
4157 Bvariable
* bvar
= temp
->get_backend_variable(context
);
4158 Bexpression
* bvar_expr
= gogo
->backend()->var_expression(bvar
, loc
);
4159 Bexpression
* bval
= sut
->expression()->get_backend(context
);
4161 Bstatement
* bassign
=
4162 gogo
->backend()->assignment_statement(bvar_expr
, bval
, loc
);
4163 Bexpression
* bvar_addr
=
4164 gogo
->backend()->address_expression(bvar_expr
, loc
);
4165 return gogo
->backend()->compound_expression(bassign
, bvar_addr
, loc
);
4170 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
4171 Btype
* btype
= this->expr_
->type()->get_backend(gogo
);
4178 case OPERATOR_MINUS
:
4179 ret
= gogo
->backend()->unary_expression(this->op_
, bexpr
, loc
);
4180 ret
= gogo
->backend()->convert_expression(btype
, ret
, loc
);
4185 ret
= gogo
->backend()->unary_expression(this->op_
, bexpr
, loc
);
4189 if (!this->create_temp_
)
4191 // We should not see a non-constant constructor here; cases
4192 // where we would see one should have been moved onto the
4193 // heap at parse time. Taking the address of a nonconstant
4194 // constructor will not do what the programmer expects.
4196 go_assert(!this->expr_
->is_composite_literal()
4197 || this->expr_
->is_immutable());
4198 if (this->expr_
->classification() == EXPRESSION_UNARY
)
4200 Unary_expression
* ue
=
4201 static_cast<Unary_expression
*>(this->expr_
);
4202 go_assert(ue
->op() != OPERATOR_AND
);
4206 static unsigned int counter
;
4208 if (this->is_gc_root_
|| this->is_slice_init_
)
4210 bool copy_to_heap
= false;
4211 if (this->is_gc_root_
)
4213 // Build a decl for a GC root variable. GC roots are mutable, so
4214 // they cannot be represented as an immutable_struct in the
4216 static unsigned int root_counter
;
4217 snprintf(buf
, sizeof buf
, "gc%u", root_counter
);
4222 // Build a decl for a slice value initializer. An immutable slice
4223 // value initializer may have to be copied to the heap if it
4224 // contains pointers in a non-constant context.
4225 snprintf(buf
, sizeof buf
, "C%u", counter
);
4228 Array_type
* at
= this->expr_
->type()->array_type();
4229 go_assert(at
!= NULL
);
4231 // If we are not copying the value to the heap, we will only
4232 // initialize the value once, so we can use this directly
4233 // rather than copying it. In that case we can't make it
4234 // read-only, because the program is permitted to change it.
4235 copy_to_heap
= (at
->element_type()->has_pointer()
4236 && !context
->is_const());
4238 Bvariable
* implicit
=
4239 gogo
->backend()->implicit_variable(buf
, btype
, true, copy_to_heap
,
4241 gogo
->backend()->implicit_variable_set_init(implicit
, buf
, btype
,
4242 true, copy_to_heap
, false,
4244 bexpr
= gogo
->backend()->var_expression(implicit
, loc
);
4246 else if ((this->expr_
->is_composite_literal()
4247 || this->expr_
->string_expression() != NULL
)
4248 && this->expr_
->is_immutable())
4250 // Build a decl for a constant constructor.
4251 snprintf(buf
, sizeof buf
, "C%u", counter
);
4255 gogo
->backend()->immutable_struct(buf
, true, false, btype
, loc
);
4256 gogo
->backend()->immutable_struct_set_init(decl
, buf
, true, false,
4258 bexpr
= gogo
->backend()->var_expression(decl
, loc
);
4261 go_assert(!this->create_temp_
|| this->expr_
->is_variable());
4262 ret
= gogo
->backend()->address_expression(bexpr
, loc
);
4267 go_assert(this->expr_
->type()->points_to() != NULL
);
4269 // If we are dereferencing the pointer to a large struct, we
4270 // need to check for nil. We don't bother to check for small
4271 // structs because we expect the system to crash on a nil
4272 // pointer dereference. However, if we know the address of this
4273 // expression is being taken, we must always check for nil.
4275 Type
* ptype
= this->expr_
->type()->points_to();
4276 Btype
* pbtype
= ptype
->get_backend(gogo
);
4277 if (!ptype
->is_void_type())
4280 bool ok
= ptype
->backend_type_size(gogo
, &s
);
4283 go_assert(saw_errors());
4284 return gogo
->backend()->error_expression();
4286 if (s
>= 4096 || this->issue_nil_check_
)
4288 go_assert(this->expr_
->is_variable());
4290 Expression::make_nil(loc
)->get_backend(context
);
4291 Bexpression
* compare
=
4292 gogo
->backend()->binary_expression(OPERATOR_EQEQ
, bexpr
,
4294 Bexpression
* crash
=
4295 gogo
->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4296 loc
)->get_backend(context
);
4297 bexpr
= gogo
->backend()->conditional_expression(btype
, compare
,
4303 ret
= gogo
->backend()->indirect_expression(pbtype
, bexpr
, false, loc
);
4314 // Export a unary expression.
4317 Unary_expression::do_export(Export
* exp
) const
4322 exp
->write_c_string("+ ");
4324 case OPERATOR_MINUS
:
4325 exp
->write_c_string("- ");
4328 exp
->write_c_string("! ");
4331 exp
->write_c_string("^ ");
4338 this->expr_
->export_expression(exp
);
4341 // Import a unary expression.
4344 Unary_expression::do_import(Import
* imp
)
4347 switch (imp
->get_char())
4353 op
= OPERATOR_MINUS
;
4364 imp
->require_c_string(" ");
4365 Expression
* expr
= Expression::import_expression(imp
);
4366 return Expression::make_unary(op
, expr
, imp
->location());
4369 // Dump ast representation of an unary expression.
4372 Unary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
4374 ast_dump_context
->dump_operator(this->op_
);
4375 ast_dump_context
->ostream() << "(";
4376 ast_dump_context
->dump_expression(this->expr_
);
4377 ast_dump_context
->ostream() << ") ";
4380 // Make a unary expression.
4383 Expression::make_unary(Operator op
, Expression
* expr
, Location location
)
4385 return new Unary_expression(op
, expr
, location
);
4388 // If this is an indirection through a pointer, return the expression
4389 // being pointed through. Otherwise return this.
4394 if (this->classification_
== EXPRESSION_UNARY
)
4396 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4397 if (ue
->op() == OPERATOR_MULT
)
4398 return ue
->operand();
4403 // Class Binary_expression.
4408 Binary_expression::do_traverse(Traverse
* traverse
)
4410 int t
= Expression::traverse(&this->left_
, traverse
);
4411 if (t
== TRAVERSE_EXIT
)
4412 return TRAVERSE_EXIT
;
4413 return Expression::traverse(&this->right_
, traverse
);
4416 // Return the type to use for a binary operation on operands of
4417 // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
4418 // such may be NULL or abstract.
4421 Binary_expression::operation_type(Operator op
, Type
* left_type
,
4422 Type
* right_type
, Type
** result_type
)
4424 if (left_type
!= right_type
4425 && !left_type
->is_abstract()
4426 && !right_type
->is_abstract()
4427 && left_type
->base() != right_type
->base()
4428 && op
!= OPERATOR_LSHIFT
4429 && op
!= OPERATOR_RSHIFT
)
4431 // May be a type error--let it be diagnosed elsewhere.
4435 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4437 if (left_type
->integer_type() != NULL
)
4438 *result_type
= left_type
;
4440 *result_type
= Type::make_abstract_integer_type();
4442 else if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
4443 *result_type
= left_type
;
4444 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
4445 *result_type
= right_type
;
4446 else if (!left_type
->is_abstract())
4447 *result_type
= left_type
;
4448 else if (!right_type
->is_abstract())
4449 *result_type
= right_type
;
4450 else if (left_type
->complex_type() != NULL
)
4451 *result_type
= left_type
;
4452 else if (right_type
->complex_type() != NULL
)
4453 *result_type
= right_type
;
4454 else if (left_type
->float_type() != NULL
)
4455 *result_type
= left_type
;
4456 else if (right_type
->float_type() != NULL
)
4457 *result_type
= right_type
;
4458 else if (left_type
->integer_type() != NULL
4459 && left_type
->integer_type()->is_rune())
4460 *result_type
= left_type
;
4461 else if (right_type
->integer_type() != NULL
4462 && right_type
->integer_type()->is_rune())
4463 *result_type
= right_type
;
4465 *result_type
= left_type
;
4470 // Convert an integer comparison code and an operator to a boolean
4474 Binary_expression::cmp_to_bool(Operator op
, int cmp
)
4481 case OPERATOR_NOTEQ
:
4498 // Compare constants according to OP.
4501 Binary_expression::compare_constant(Operator op
, Numeric_constant
* left_nc
,
4502 Numeric_constant
* right_nc
,
4503 Location location
, bool* result
)
4505 Type
* left_type
= left_nc
->type();
4506 Type
* right_type
= right_nc
->type();
4509 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
4512 // When comparing an untyped operand to a typed operand, we are
4513 // effectively coercing the untyped operand to the other operand's
4514 // type, so make sure that is valid.
4515 if (!left_nc
->set_type(type
, true, location
)
4516 || !right_nc
->set_type(type
, true, location
))
4521 if (type
->complex_type() != NULL
)
4523 if (op
!= OPERATOR_EQEQ
&& op
!= OPERATOR_NOTEQ
)
4525 ret
= Binary_expression::compare_complex(left_nc
, right_nc
, &cmp
);
4527 else if (type
->float_type() != NULL
)
4528 ret
= Binary_expression::compare_float(left_nc
, right_nc
, &cmp
);
4530 ret
= Binary_expression::compare_integer(left_nc
, right_nc
, &cmp
);
4533 *result
= Binary_expression::cmp_to_bool(op
, cmp
);
4538 // Compare integer constants.
4541 Binary_expression::compare_integer(const Numeric_constant
* left_nc
,
4542 const Numeric_constant
* right_nc
,
4546 if (!left_nc
->to_int(&left_val
))
4549 if (!right_nc
->to_int(&right_val
))
4551 mpz_clear(left_val
);
4555 *cmp
= mpz_cmp(left_val
, right_val
);
4557 mpz_clear(left_val
);
4558 mpz_clear(right_val
);
4563 // Compare floating point constants.
4566 Binary_expression::compare_float(const Numeric_constant
* left_nc
,
4567 const Numeric_constant
* right_nc
,
4571 if (!left_nc
->to_float(&left_val
))
4574 if (!right_nc
->to_float(&right_val
))
4576 mpfr_clear(left_val
);
4580 // We already coerced both operands to the same type. If that type
4581 // is not an abstract type, we need to round the values accordingly.
4582 Type
* type
= left_nc
->type();
4583 if (!type
->is_abstract() && type
->float_type() != NULL
)
4585 int bits
= type
->float_type()->bits();
4586 mpfr_prec_round(left_val
, bits
, GMP_RNDN
);
4587 mpfr_prec_round(right_val
, bits
, GMP_RNDN
);
4590 *cmp
= mpfr_cmp(left_val
, right_val
);
4592 mpfr_clear(left_val
);
4593 mpfr_clear(right_val
);
4598 // Compare complex constants. Complex numbers may only be compared
4602 Binary_expression::compare_complex(const Numeric_constant
* left_nc
,
4603 const Numeric_constant
* right_nc
,
4607 if (!left_nc
->to_complex(&left_val
))
4610 if (!right_nc
->to_complex(&right_val
))
4612 mpc_clear(left_val
);
4616 // We already coerced both operands to the same type. If that type
4617 // is not an abstract type, we need to round the values accordingly.
4618 Type
* type
= left_nc
->type();
4619 if (!type
->is_abstract() && type
->complex_type() != NULL
)
4621 int bits
= type
->complex_type()->bits();
4622 mpfr_prec_round(mpc_realref(left_val
), bits
/ 2, GMP_RNDN
);
4623 mpfr_prec_round(mpc_imagref(left_val
), bits
/ 2, GMP_RNDN
);
4624 mpfr_prec_round(mpc_realref(right_val
), bits
/ 2, GMP_RNDN
);
4625 mpfr_prec_round(mpc_imagref(right_val
), bits
/ 2, GMP_RNDN
);
4628 *cmp
= mpc_cmp(left_val
, right_val
) != 0;
4630 mpc_clear(left_val
);
4631 mpc_clear(right_val
);
4636 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
4637 // true if this could be done, false if not. Issue errors at LOCATION
4641 Binary_expression::eval_constant(Operator op
, Numeric_constant
* left_nc
,
4642 Numeric_constant
* right_nc
,
4643 Location location
, Numeric_constant
* nc
)
4648 case OPERATOR_ANDAND
:
4650 case OPERATOR_NOTEQ
:
4655 // These return boolean values, not numeric.
4661 Type
* left_type
= left_nc
->type();
4662 Type
* right_type
= right_nc
->type();
4665 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
4668 bool is_shift
= op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
;
4670 // When combining an untyped operand with a typed operand, we are
4671 // effectively coercing the untyped operand to the other operand's
4672 // type, so make sure that is valid.
4673 if (!left_nc
->set_type(type
, true, location
))
4675 if (!is_shift
&& !right_nc
->set_type(type
, true, location
))
4678 && ((left_type
->integer_type() == NULL
4679 && !left_type
->is_abstract())
4680 || (right_type
->integer_type() == NULL
4681 && !right_type
->is_abstract())))
4685 if (type
->complex_type() != NULL
)
4686 r
= Binary_expression::eval_complex(op
, left_nc
, right_nc
, location
, nc
);
4687 else if (type
->float_type() != NULL
)
4688 r
= Binary_expression::eval_float(op
, left_nc
, right_nc
, location
, nc
);
4690 r
= Binary_expression::eval_integer(op
, left_nc
, right_nc
, location
, nc
);
4693 r
= nc
->set_type(type
, true, location
);
4698 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4699 // integer operations. Return true if this could be done, false if
4703 Binary_expression::eval_integer(Operator op
, const Numeric_constant
* left_nc
,
4704 const Numeric_constant
* right_nc
,
4705 Location location
, Numeric_constant
* nc
)
4708 if (!left_nc
->to_int(&left_val
))
4711 if (!right_nc
->to_int(&right_val
))
4713 mpz_clear(left_val
);
4723 mpz_add(val
, left_val
, right_val
);
4724 if (mpz_sizeinbase(val
, 2) > 0x100000)
4726 error_at(location
, "constant addition overflow");
4731 case OPERATOR_MINUS
:
4732 mpz_sub(val
, left_val
, right_val
);
4733 if (mpz_sizeinbase(val
, 2) > 0x100000)
4735 error_at(location
, "constant subtraction overflow");
4741 mpz_ior(val
, left_val
, right_val
);
4744 mpz_xor(val
, left_val
, right_val
);
4747 mpz_mul(val
, left_val
, right_val
);
4748 if (mpz_sizeinbase(val
, 2) > 0x100000)
4750 error_at(location
, "constant multiplication overflow");
4756 if (mpz_sgn(right_val
) != 0)
4757 mpz_tdiv_q(val
, left_val
, right_val
);
4760 error_at(location
, "division by zero");
4766 if (mpz_sgn(right_val
) != 0)
4767 mpz_tdiv_r(val
, left_val
, right_val
);
4770 error_at(location
, "division by zero");
4775 case OPERATOR_LSHIFT
:
4777 unsigned long shift
= mpz_get_ui(right_val
);
4778 if (mpz_cmp_ui(right_val
, shift
) == 0 && shift
<= 0x100000)
4779 mpz_mul_2exp(val
, left_val
, shift
);
4782 error_at(location
, "shift count overflow");
4789 case OPERATOR_RSHIFT
:
4791 unsigned long shift
= mpz_get_ui(right_val
);
4792 if (mpz_cmp_ui(right_val
, shift
) != 0)
4794 error_at(location
, "shift count overflow");
4800 if (mpz_cmp_ui(left_val
, 0) >= 0)
4801 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4803 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4809 mpz_and(val
, left_val
, right_val
);
4811 case OPERATOR_BITCLEAR
:
4815 mpz_com(tval
, right_val
);
4816 mpz_and(val
, left_val
, tval
);
4824 mpz_clear(left_val
);
4825 mpz_clear(right_val
);
4827 if (left_nc
->is_rune()
4828 || (op
!= OPERATOR_LSHIFT
4829 && op
!= OPERATOR_RSHIFT
4830 && right_nc
->is_rune()))
4831 nc
->set_rune(NULL
, val
);
4833 nc
->set_int(NULL
, val
);
4840 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4841 // floating point operations. Return true if this could be done,
4845 Binary_expression::eval_float(Operator op
, const Numeric_constant
* left_nc
,
4846 const Numeric_constant
* right_nc
,
4847 Location location
, Numeric_constant
* nc
)
4850 if (!left_nc
->to_float(&left_val
))
4853 if (!right_nc
->to_float(&right_val
))
4855 mpfr_clear(left_val
);
4866 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4868 case OPERATOR_MINUS
:
4869 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4874 case OPERATOR_BITCLEAR
:
4876 case OPERATOR_LSHIFT
:
4877 case OPERATOR_RSHIFT
:
4878 mpfr_set_ui(val
, 0, GMP_RNDN
);
4882 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4885 if (!mpfr_zero_p(right_val
))
4886 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4889 error_at(location
, "division by zero");
4891 mpfr_set_ui(val
, 0, GMP_RNDN
);
4898 mpfr_clear(left_val
);
4899 mpfr_clear(right_val
);
4901 nc
->set_float(NULL
, val
);
4907 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4908 // complex operations. Return true if this could be done, false if
4912 Binary_expression::eval_complex(Operator op
, const Numeric_constant
* left_nc
,
4913 const Numeric_constant
* right_nc
,
4914 Location location
, Numeric_constant
* nc
)
4917 if (!left_nc
->to_complex(&left_val
))
4920 if (!right_nc
->to_complex(&right_val
))
4922 mpc_clear(left_val
);
4927 mpc_init2(val
, mpc_precision
);
4933 mpc_add(val
, left_val
, right_val
, MPC_RNDNN
);
4935 case OPERATOR_MINUS
:
4936 mpc_sub(val
, left_val
, right_val
, MPC_RNDNN
);
4941 case OPERATOR_BITCLEAR
:
4943 case OPERATOR_LSHIFT
:
4944 case OPERATOR_RSHIFT
:
4945 mpc_set_ui(val
, 0, MPC_RNDNN
);
4949 mpc_mul(val
, left_val
, right_val
, MPC_RNDNN
);
4952 if (mpc_cmp_si(right_val
, 0) == 0)
4954 error_at(location
, "division by zero");
4956 mpc_set_ui(val
, 0, MPC_RNDNN
);
4959 mpc_div(val
, left_val
, right_val
, MPC_RNDNN
);
4965 mpc_clear(left_val
);
4966 mpc_clear(right_val
);
4968 nc
->set_complex(NULL
, val
);
4974 // Lower a binary expression. We have to evaluate constant
4975 // expressions now, in order to implement Go's unlimited precision
4979 Binary_expression::do_lower(Gogo
* gogo
, Named_object
*,
4980 Statement_inserter
* inserter
, int)
4982 Location location
= this->location();
4983 Operator op
= this->op_
;
4984 Expression
* left
= this->left_
;
4985 Expression
* right
= this->right_
;
4987 const bool is_comparison
= (op
== OPERATOR_EQEQ
4988 || op
== OPERATOR_NOTEQ
4989 || op
== OPERATOR_LT
4990 || op
== OPERATOR_LE
4991 || op
== OPERATOR_GT
4992 || op
== OPERATOR_GE
);
4994 // Numeric constant expressions.
4996 Numeric_constant left_nc
;
4997 Numeric_constant right_nc
;
4998 if (left
->numeric_constant_value(&left_nc
)
4999 && right
->numeric_constant_value(&right_nc
))
5004 if (!Binary_expression::compare_constant(op
, &left_nc
,
5005 &right_nc
, location
,
5008 return Expression::make_cast(Type::make_boolean_type(),
5009 Expression::make_boolean(result
,
5015 Numeric_constant nc
;
5016 if (!Binary_expression::eval_constant(op
, &left_nc
, &right_nc
,
5019 return nc
.expression(location
);
5024 // String constant expressions.
5025 if (left
->type()->is_string_type() && right
->type()->is_string_type())
5027 std::string left_string
;
5028 std::string right_string
;
5029 if (left
->string_constant_value(&left_string
)
5030 && right
->string_constant_value(&right_string
))
5032 if (op
== OPERATOR_PLUS
)
5033 return Expression::make_string(left_string
+ right_string
,
5035 else if (is_comparison
)
5037 int cmp
= left_string
.compare(right_string
);
5038 bool r
= Binary_expression::cmp_to_bool(op
, cmp
);
5039 return Expression::make_boolean(r
, location
);
5044 // Lower struct, array, and some interface comparisons.
5045 if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
5047 if (left
->type()->struct_type() != NULL
5048 && right
->type()->struct_type() != NULL
)
5049 return this->lower_struct_comparison(gogo
, inserter
);
5050 else if (left
->type()->array_type() != NULL
5051 && !left
->type()->is_slice_type()
5052 && right
->type()->array_type() != NULL
5053 && !right
->type()->is_slice_type())
5054 return this->lower_array_comparison(gogo
, inserter
);
5055 else if ((left
->type()->interface_type() != NULL
5056 && right
->type()->interface_type() == NULL
)
5057 || (left
->type()->interface_type() == NULL
5058 && right
->type()->interface_type() != NULL
))
5059 return this->lower_interface_value_comparison(gogo
, inserter
);
5065 // Lower a struct comparison.
5068 Binary_expression::lower_struct_comparison(Gogo
* gogo
,
5069 Statement_inserter
* inserter
)
5071 Struct_type
* st
= this->left_
->type()->struct_type();
5072 Struct_type
* st2
= this->right_
->type()->struct_type();
5075 if (st
!= st2
&& !Type::are_identical(st
, st2
, false, NULL
))
5077 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
5078 this->right_
->type(), NULL
))
5081 // See if we can compare using memcmp. As a heuristic, we use
5082 // memcmp rather than field references and comparisons if there are
5083 // more than two fields.
5084 if (st
->compare_is_identity(gogo
) && st
->total_field_count() > 2)
5085 return this->lower_compare_to_memcmp(gogo
, inserter
);
5087 Location loc
= this->location();
5089 Expression
* left
= this->left_
;
5090 Temporary_statement
* left_temp
= NULL
;
5091 if (left
->var_expression() == NULL
5092 && left
->temporary_reference_expression() == NULL
)
5094 left_temp
= Statement::make_temporary(left
->type(), NULL
, loc
);
5095 inserter
->insert(left_temp
);
5096 left
= Expression::make_set_and_use_temporary(left_temp
, left
, loc
);
5099 Expression
* right
= this->right_
;
5100 Temporary_statement
* right_temp
= NULL
;
5101 if (right
->var_expression() == NULL
5102 && right
->temporary_reference_expression() == NULL
)
5104 right_temp
= Statement::make_temporary(right
->type(), NULL
, loc
);
5105 inserter
->insert(right_temp
);
5106 right
= Expression::make_set_and_use_temporary(right_temp
, right
, loc
);
5109 Expression
* ret
= Expression::make_boolean(true, loc
);
5110 const Struct_field_list
* fields
= st
->fields();
5111 unsigned int field_index
= 0;
5112 for (Struct_field_list::const_iterator pf
= fields
->begin();
5113 pf
!= fields
->end();
5114 ++pf
, ++field_index
)
5116 if (Gogo::is_sink_name(pf
->field_name()))
5119 if (field_index
> 0)
5121 if (left_temp
== NULL
)
5122 left
= left
->copy();
5124 left
= Expression::make_temporary_reference(left_temp
, loc
);
5125 if (right_temp
== NULL
)
5126 right
= right
->copy();
5128 right
= Expression::make_temporary_reference(right_temp
, loc
);
5130 Expression
* f1
= Expression::make_field_reference(left
, field_index
,
5132 Expression
* f2
= Expression::make_field_reference(right
, field_index
,
5134 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, f1
, f2
, loc
);
5135 ret
= Expression::make_binary(OPERATOR_ANDAND
, ret
, cond
, loc
);
5138 if (this->op_
== OPERATOR_NOTEQ
)
5139 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
5144 // Lower an array comparison.
5147 Binary_expression::lower_array_comparison(Gogo
* gogo
,
5148 Statement_inserter
* inserter
)
5150 Array_type
* at
= this->left_
->type()->array_type();
5151 Array_type
* at2
= this->right_
->type()->array_type();
5154 if (at
!= at2
&& !Type::are_identical(at
, at2
, false, NULL
))
5156 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
5157 this->right_
->type(), NULL
))
5160 // Call memcmp directly if possible. This may let the middle-end
5161 // optimize the call.
5162 if (at
->compare_is_identity(gogo
))
5163 return this->lower_compare_to_memcmp(gogo
, inserter
);
5165 // Call the array comparison function.
5166 Named_object
* hash_fn
;
5167 Named_object
* equal_fn
;
5168 at
->type_functions(gogo
, this->left_
->type()->named_type(), NULL
, NULL
,
5169 &hash_fn
, &equal_fn
);
5171 Location loc
= this->location();
5173 Expression
* func
= Expression::make_func_reference(equal_fn
, NULL
, loc
);
5175 Expression_list
* args
= new Expression_list();
5176 args
->push_back(this->operand_address(inserter
, this->left_
));
5177 args
->push_back(this->operand_address(inserter
, this->right_
));
5178 args
->push_back(Expression::make_type_info(at
, TYPE_INFO_SIZE
));
5180 Expression
* ret
= Expression::make_call(func
, args
, false, loc
);
5182 if (this->op_
== OPERATOR_NOTEQ
)
5183 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
5188 // Lower an interface to value comparison.
5191 Binary_expression::lower_interface_value_comparison(Gogo
*,
5192 Statement_inserter
* inserter
)
5194 Type
* left_type
= this->left_
->type();
5195 Type
* right_type
= this->right_
->type();
5196 Interface_type
* ift
;
5197 if (left_type
->interface_type() != NULL
)
5199 ift
= left_type
->interface_type();
5200 if (!ift
->implements_interface(right_type
, NULL
))
5205 ift
= right_type
->interface_type();
5206 if (!ift
->implements_interface(left_type
, NULL
))
5209 if (!Type::are_compatible_for_comparison(true, left_type
, right_type
, NULL
))
5212 Location loc
= this->location();
5214 if (left_type
->interface_type() == NULL
5215 && left_type
->points_to() == NULL
5216 && !this->left_
->is_addressable())
5218 Temporary_statement
* temp
=
5219 Statement::make_temporary(left_type
, NULL
, loc
);
5220 inserter
->insert(temp
);
5222 Expression::make_set_and_use_temporary(temp
, this->left_
, loc
);
5225 if (right_type
->interface_type() == NULL
5226 && right_type
->points_to() == NULL
5227 && !this->right_
->is_addressable())
5229 Temporary_statement
* temp
=
5230 Statement::make_temporary(right_type
, NULL
, loc
);
5231 inserter
->insert(temp
);
5233 Expression::make_set_and_use_temporary(temp
, this->right_
, loc
);
5239 // Lower a struct or array comparison to a call to memcmp.
5242 Binary_expression::lower_compare_to_memcmp(Gogo
*, Statement_inserter
* inserter
)
5244 Location loc
= this->location();
5246 Expression
* a1
= this->operand_address(inserter
, this->left_
);
5247 Expression
* a2
= this->operand_address(inserter
, this->right_
);
5248 Expression
* len
= Expression::make_type_info(this->left_
->type(),
5251 Expression
* call
= Runtime::make_call(Runtime::MEMCMP
, loc
, 3, a1
, a2
, len
);
5252 Expression
* zero
= Expression::make_integer_ul(0, NULL
, loc
);
5253 return Expression::make_binary(this->op_
, call
, zero
, loc
);
5257 Binary_expression::do_flatten(Gogo
* gogo
, Named_object
*,
5258 Statement_inserter
* inserter
)
5260 Location loc
= this->location();
5261 if (this->left_
->type()->is_error_type()
5262 || this->right_
->type()->is_error_type()
5263 || this->left_
->is_error_expression()
5264 || this->right_
->is_error_expression())
5266 go_assert(saw_errors());
5267 return Expression::make_error(loc
);
5270 Temporary_statement
* temp
;
5271 if (this->left_
->type()->is_string_type()
5272 && this->op_
== OPERATOR_PLUS
)
5274 if (!this->left_
->is_variable()
5275 && !this->left_
->is_constant())
5277 temp
= Statement::make_temporary(NULL
, this->left_
, loc
);
5278 inserter
->insert(temp
);
5279 this->left_
= Expression::make_temporary_reference(temp
, loc
);
5281 if (!this->right_
->is_variable()
5282 && !this->right_
->is_constant())
5285 Statement::make_temporary(this->left_
->type(), this->right_
, loc
);
5286 this->right_
= Expression::make_temporary_reference(temp
, loc
);
5287 inserter
->insert(temp
);
5291 Type
* left_type
= this->left_
->type();
5292 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5293 || this->op_
== OPERATOR_RSHIFT
);
5294 bool is_idiv_op
= ((this->op_
== OPERATOR_DIV
&&
5295 left_type
->integer_type() != NULL
)
5296 || this->op_
== OPERATOR_MOD
);
5300 && (gogo
->check_divide_by_zero() || gogo
->check_divide_overflow())))
5302 if (!this->left_
->is_variable() && !this->left_
->is_constant())
5304 temp
= Statement::make_temporary(NULL
, this->left_
, loc
);
5305 inserter
->insert(temp
);
5306 this->left_
= Expression::make_temporary_reference(temp
, loc
);
5308 if (!this->right_
->is_variable() && !this->right_
->is_constant())
5311 Statement::make_temporary(NULL
, this->right_
, loc
);
5312 this->right_
= Expression::make_temporary_reference(temp
, loc
);
5313 inserter
->insert(temp
);
5320 // Return the address of EXPR, cast to unsafe.Pointer.
5323 Binary_expression::operand_address(Statement_inserter
* inserter
,
5326 Location loc
= this->location();
5328 if (!expr
->is_addressable())
5330 Temporary_statement
* temp
= Statement::make_temporary(expr
->type(), NULL
,
5332 inserter
->insert(temp
);
5333 expr
= Expression::make_set_and_use_temporary(temp
, expr
, loc
);
5335 expr
= Expression::make_unary(OPERATOR_AND
, expr
, loc
);
5336 static_cast<Unary_expression
*>(expr
)->set_does_not_escape();
5337 Type
* void_type
= Type::make_void_type();
5338 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
5339 return Expression::make_cast(unsafe_pointer_type
, expr
, loc
);
5342 // Return the numeric constant value, if it has one.
5345 Binary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
5347 Numeric_constant left_nc
;
5348 if (!this->left_
->numeric_constant_value(&left_nc
))
5350 Numeric_constant right_nc
;
5351 if (!this->right_
->numeric_constant_value(&right_nc
))
5353 return Binary_expression::eval_constant(this->op_
, &left_nc
, &right_nc
,
5354 this->location(), nc
);
5357 // Note that the value is being discarded.
5360 Binary_expression::do_discarding_value()
5362 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5363 return this->right_
->discarding_value();
5366 this->unused_value_error();
5374 Binary_expression::do_type()
5376 if (this->classification() == EXPRESSION_ERROR
)
5377 return Type::make_error_type();
5382 case OPERATOR_NOTEQ
:
5387 if (this->type_
== NULL
)
5388 this->type_
= Type::make_boolean_type();
5392 case OPERATOR_MINUS
:
5399 case OPERATOR_BITCLEAR
:
5401 case OPERATOR_ANDAND
:
5404 if (!Binary_expression::operation_type(this->op_
,
5405 this->left_
->type(),
5406 this->right_
->type(),
5408 return Type::make_error_type();
5412 case OPERATOR_LSHIFT
:
5413 case OPERATOR_RSHIFT
:
5414 return this->left_
->type();
5421 // Set type for a binary expression.
5424 Binary_expression::do_determine_type(const Type_context
* context
)
5426 Type
* tleft
= this->left_
->type();
5427 Type
* tright
= this->right_
->type();
5429 // Both sides should have the same type, except for the shift
5430 // operations. For a comparison, we should ignore the incoming
5433 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5434 || this->op_
== OPERATOR_RSHIFT
);
5436 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5437 || this->op_
== OPERATOR_NOTEQ
5438 || this->op_
== OPERATOR_LT
5439 || this->op_
== OPERATOR_LE
5440 || this->op_
== OPERATOR_GT
5441 || this->op_
== OPERATOR_GE
);
5443 // For constant expressions, the context of the result is not useful in
5444 // determining the types of the operands. It is only legal to use abstract
5445 // boolean, numeric, and string constants as operands where it is legal to
5446 // use non-abstract boolean, numeric, and string constants, respectively.
5447 // Any issues with the operation will be resolved in the check_types pass.
5448 bool is_constant_expr
= (this->left_
->is_constant()
5449 && this->right_
->is_constant());
5451 Type_context
subcontext(*context
);
5455 // In a comparison, the context does not determine the types of
5457 subcontext
.type
= NULL
;
5460 // Set the context for the left hand operand.
5463 // The right hand operand of a shift plays no role in
5464 // determining the type of the left hand operand.
5466 else if (!tleft
->is_abstract())
5467 subcontext
.type
= tleft
;
5468 else if (!tright
->is_abstract())
5469 subcontext
.type
= tright
;
5470 else if (subcontext
.type
== NULL
)
5472 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5473 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5474 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5476 // Both sides have an abstract integer, abstract float, or
5477 // abstract complex type. Just let CONTEXT determine
5478 // whether they may remain abstract or not.
5480 else if (tleft
->complex_type() != NULL
)
5481 subcontext
.type
= tleft
;
5482 else if (tright
->complex_type() != NULL
)
5483 subcontext
.type
= tright
;
5484 else if (tleft
->float_type() != NULL
)
5485 subcontext
.type
= tleft
;
5486 else if (tright
->float_type() != NULL
)
5487 subcontext
.type
= tright
;
5489 subcontext
.type
= tleft
;
5491 if (subcontext
.type
!= NULL
&& !context
->may_be_abstract
)
5492 subcontext
.type
= subcontext
.type
->make_non_abstract_type();
5495 if (!is_constant_expr
)
5496 this->left_
->determine_type(&subcontext
);
5500 // We may have inherited an unusable type for the shift operand.
5501 // Give a useful error if that happened.
5502 if (tleft
->is_abstract()
5503 && subcontext
.type
!= NULL
5504 && !subcontext
.may_be_abstract
5505 && subcontext
.type
->interface_type() == NULL
5506 && subcontext
.type
->integer_type() == NULL
)
5507 this->report_error(("invalid context-determined non-integer type "
5508 "for left operand of shift"));
5510 // The context for the right hand operand is the same as for the
5511 // left hand operand, except for a shift operator.
5512 subcontext
.type
= Type::lookup_integer_type("uint");
5513 subcontext
.may_be_abstract
= false;
5516 if (!is_constant_expr
)
5517 this->right_
->determine_type(&subcontext
);
5521 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
5523 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
5524 this->type_
= context
->type
;
5525 else if (!context
->may_be_abstract
)
5526 this->type_
= Type::lookup_bool_type();
5530 // Report an error if the binary operator OP does not support TYPE.
5531 // OTYPE is the type of the other operand. Return whether the
5532 // operation is OK. This should not be used for shift.
5535 Binary_expression::check_operator_type(Operator op
, Type
* type
, Type
* otype
,
5541 case OPERATOR_ANDAND
:
5542 if (!type
->is_boolean_type()
5543 || !otype
->is_boolean_type())
5545 error_at(location
, "expected boolean type");
5551 case OPERATOR_NOTEQ
:
5554 if (!Type::are_compatible_for_comparison(true, type
, otype
, &reason
))
5556 error_at(location
, "%s", reason
.c_str());
5568 if (!Type::are_compatible_for_comparison(false, type
, otype
, &reason
))
5570 error_at(location
, "%s", reason
.c_str());
5577 case OPERATOR_PLUSEQ
:
5578 if ((!type
->is_numeric_type() && !type
->is_string_type())
5579 || (!otype
->is_numeric_type() && !otype
->is_string_type()))
5582 "expected integer, floating, complex, or string type");
5587 case OPERATOR_MINUS
:
5588 case OPERATOR_MINUSEQ
:
5590 case OPERATOR_MULTEQ
:
5592 case OPERATOR_DIVEQ
:
5593 if (!type
->is_numeric_type() || !otype
->is_numeric_type())
5595 error_at(location
, "expected integer, floating, or complex type");
5601 case OPERATOR_MODEQ
:
5605 case OPERATOR_ANDEQ
:
5607 case OPERATOR_XOREQ
:
5608 case OPERATOR_BITCLEAR
:
5609 case OPERATOR_BITCLEAREQ
:
5610 if (type
->integer_type() == NULL
|| otype
->integer_type() == NULL
)
5612 error_at(location
, "expected integer type");
5627 Binary_expression::do_check_types(Gogo
*)
5629 if (this->classification() == EXPRESSION_ERROR
)
5632 Type
* left_type
= this->left_
->type();
5633 Type
* right_type
= this->right_
->type();
5634 if (left_type
->is_error() || right_type
->is_error())
5636 this->set_is_error();
5640 if (this->op_
== OPERATOR_EQEQ
5641 || this->op_
== OPERATOR_NOTEQ
5642 || this->op_
== OPERATOR_LT
5643 || this->op_
== OPERATOR_LE
5644 || this->op_
== OPERATOR_GT
5645 || this->op_
== OPERATOR_GE
)
5647 if (left_type
->is_nil_type() && right_type
->is_nil_type())
5649 this->report_error(_("invalid comparison of nil with nil"));
5652 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5653 && !Type::are_assignable(right_type
, left_type
, NULL
))
5655 this->report_error(_("incompatible types in binary expression"));
5658 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5661 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5665 this->set_is_error();
5669 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5671 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5673 this->report_error(_("incompatible types in binary expression"));
5676 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5680 this->set_is_error();
5683 if (this->op_
== OPERATOR_DIV
|| this->op_
== OPERATOR_MOD
)
5685 // Division by a zero integer constant is an error.
5686 Numeric_constant rconst
;
5688 if (left_type
->integer_type() != NULL
5689 && this->right_
->numeric_constant_value(&rconst
)
5690 && rconst
.to_unsigned_long(&rval
) == Numeric_constant::NC_UL_VALID
5693 this->report_error(_("integer division by zero"));
5700 if (left_type
->integer_type() == NULL
)
5701 this->report_error(_("shift of non-integer operand"));
5703 if (right_type
->is_string_type())
5704 this->report_error(_("shift count not unsigned integer"));
5705 else if (!right_type
->is_abstract()
5706 && (right_type
->integer_type() == NULL
5707 || !right_type
->integer_type()->is_unsigned()))
5708 this->report_error(_("shift count not unsigned integer"));
5711 Numeric_constant nc
;
5712 if (this->right_
->numeric_constant_value(&nc
))
5715 if (!nc
.to_int(&val
))
5716 this->report_error(_("shift count not unsigned integer"));
5719 if (mpz_sgn(val
) < 0)
5721 this->report_error(_("negative shift count"));
5722 Location rloc
= this->right_
->location();
5723 this->right_
= Expression::make_integer_ul(0, right_type
,
5733 // Get the backend representation for a binary expression.
5736 Binary_expression::do_get_backend(Translate_context
* context
)
5738 Gogo
* gogo
= context
->gogo();
5739 Location loc
= this->location();
5740 Type
* left_type
= this->left_
->type();
5741 Type
* right_type
= this->right_
->type();
5743 bool use_left_type
= true;
5744 bool is_shift_op
= false;
5745 bool is_idiv_op
= false;
5749 case OPERATOR_NOTEQ
:
5754 return Expression::comparison(context
, this->type_
, this->op_
,
5755 this->left_
, this->right_
, loc
);
5758 case OPERATOR_ANDAND
:
5759 use_left_type
= false;
5762 case OPERATOR_MINUS
:
5768 if (left_type
->float_type() != NULL
|| left_type
->complex_type() != NULL
)
5774 case OPERATOR_LSHIFT
:
5775 case OPERATOR_RSHIFT
:
5778 case OPERATOR_BITCLEAR
:
5779 this->right_
= Expression::make_unary(OPERATOR_XOR
, this->right_
, loc
);
5786 if (left_type
->is_string_type())
5788 go_assert(this->op_
== OPERATOR_PLUS
);
5789 Expression
* string_plus
=
5790 Runtime::make_call(Runtime::STRING_PLUS
, loc
, 2,
5791 this->left_
, this->right_
);
5792 return string_plus
->get_backend(context
);
5795 // For complex division Go might want slightly different results than the
5796 // backend implementation provides, so we have our own runtime routine.
5797 if (this->op_
== OPERATOR_DIV
&& this->left_
->type()->complex_type() != NULL
)
5799 Runtime::Function complex_code
;
5800 switch (this->left_
->type()->complex_type()->bits())
5803 complex_code
= Runtime::COMPLEX64_DIV
;
5806 complex_code
= Runtime::COMPLEX128_DIV
;
5811 Expression
* complex_div
=
5812 Runtime::make_call(complex_code
, loc
, 2, this->left_
, this->right_
);
5813 return complex_div
->get_backend(context
);
5816 Bexpression
* left
= this->left_
->get_backend(context
);
5817 Bexpression
* right
= this->right_
->get_backend(context
);
5819 Type
* type
= use_left_type
? left_type
: right_type
;
5820 Btype
* btype
= type
->get_backend(gogo
);
5823 gogo
->backend()->binary_expression(this->op_
, left
, right
, loc
);
5824 ret
= gogo
->backend()->convert_expression(btype
, ret
, loc
);
5826 // Initialize overflow constants.
5827 Bexpression
* overflow
;
5829 mpz_init_set_ui(zero
, 0UL);
5831 mpz_init_set_ui(one
, 1UL);
5833 mpz_init_set_si(neg_one
, -1);
5835 Btype
* left_btype
= left_type
->get_backend(gogo
);
5836 Btype
* right_btype
= right_type
->get_backend(gogo
);
5838 // In Go, a shift larger than the size of the type is well-defined.
5839 // This is not true in C, so we need to insert a conditional.
5842 go_assert(left_type
->integer_type() != NULL
);
5845 int bits
= left_type
->integer_type()->bits();
5846 mpz_init_set_ui(bitsval
, bits
);
5847 Bexpression
* bits_expr
=
5848 gogo
->backend()->integer_constant_expression(right_btype
, bitsval
);
5849 Bexpression
* compare
=
5850 gogo
->backend()->binary_expression(OPERATOR_LT
,
5851 right
, bits_expr
, loc
);
5853 Bexpression
* zero_expr
=
5854 gogo
->backend()->integer_constant_expression(left_btype
, zero
);
5855 overflow
= zero_expr
;
5856 if (this->op_
== OPERATOR_RSHIFT
5857 && !left_type
->integer_type()->is_unsigned())
5859 Bexpression
* neg_expr
=
5860 gogo
->backend()->binary_expression(OPERATOR_LT
, left
,
5862 Bexpression
* neg_one_expr
=
5863 gogo
->backend()->integer_constant_expression(left_btype
, neg_one
);
5864 overflow
= gogo
->backend()->conditional_expression(btype
, neg_expr
,
5868 ret
= gogo
->backend()->conditional_expression(btype
, compare
, ret
,
5873 // Add checks for division by zero and division overflow as needed.
5876 if (gogo
->check_divide_by_zero())
5879 Bexpression
* zero_expr
=
5880 gogo
->backend()->integer_constant_expression(right_btype
, zero
);
5881 Bexpression
* check
=
5882 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
5883 right
, zero_expr
, loc
);
5885 // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO)
5886 int errcode
= RUNTIME_ERROR_DIVISION_BY_ZERO
;
5887 Bexpression
* crash
= gogo
->runtime_error(errcode
,
5888 loc
)->get_backend(context
);
5890 // right == 0 ? (__go_runtime_error(...), 0) : ret
5891 ret
= gogo
->backend()->conditional_expression(btype
, check
, crash
,
5895 if (gogo
->check_divide_overflow())
5898 // FIXME: It would be nice to say that this test is expected
5901 Bexpression
* neg_one_expr
=
5902 gogo
->backend()->integer_constant_expression(right_btype
, neg_one
);
5903 Bexpression
* check
=
5904 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
5905 right
, neg_one_expr
, loc
);
5907 Bexpression
* zero_expr
=
5908 gogo
->backend()->integer_constant_expression(btype
, zero
);
5909 Bexpression
* one_expr
=
5910 gogo
->backend()->integer_constant_expression(btype
, one
);
5912 if (type
->integer_type()->is_unsigned())
5914 // An unsigned -1 is the largest possible number, so
5915 // dividing is always 1 or 0.
5918 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
5920 if (this->op_
== OPERATOR_DIV
)
5922 gogo
->backend()->conditional_expression(btype
, cmp
,
5923 one_expr
, zero_expr
,
5927 gogo
->backend()->conditional_expression(btype
, cmp
,
5933 // Computing left / -1 is the same as computing - left,
5934 // which does not overflow since Go sets -fwrapv.
5935 if (this->op_
== OPERATOR_DIV
)
5937 Expression
* negate_expr
=
5938 Expression::make_unary(OPERATOR_MINUS
, this->left_
, loc
);
5939 overflow
= negate_expr
->get_backend(context
);
5942 overflow
= zero_expr
;
5944 overflow
= gogo
->backend()->convert_expression(btype
, overflow
, loc
);
5946 // right == -1 ? - left : ret
5947 ret
= gogo
->backend()->conditional_expression(btype
, check
, overflow
,
5958 // Export a binary expression.
5961 Binary_expression::do_export(Export
* exp
) const
5963 exp
->write_c_string("(");
5964 this->left_
->export_expression(exp
);
5968 exp
->write_c_string(" || ");
5970 case OPERATOR_ANDAND
:
5971 exp
->write_c_string(" && ");
5974 exp
->write_c_string(" == ");
5976 case OPERATOR_NOTEQ
:
5977 exp
->write_c_string(" != ");
5980 exp
->write_c_string(" < ");
5983 exp
->write_c_string(" <= ");
5986 exp
->write_c_string(" > ");
5989 exp
->write_c_string(" >= ");
5992 exp
->write_c_string(" + ");
5994 case OPERATOR_MINUS
:
5995 exp
->write_c_string(" - ");
5998 exp
->write_c_string(" | ");
6001 exp
->write_c_string(" ^ ");
6004 exp
->write_c_string(" * ");
6007 exp
->write_c_string(" / ");
6010 exp
->write_c_string(" % ");
6012 case OPERATOR_LSHIFT
:
6013 exp
->write_c_string(" << ");
6015 case OPERATOR_RSHIFT
:
6016 exp
->write_c_string(" >> ");
6019 exp
->write_c_string(" & ");
6021 case OPERATOR_BITCLEAR
:
6022 exp
->write_c_string(" &^ ");
6027 this->right_
->export_expression(exp
);
6028 exp
->write_c_string(")");
6031 // Import a binary expression.
6034 Binary_expression::do_import(Import
* imp
)
6036 imp
->require_c_string("(");
6038 Expression
* left
= Expression::import_expression(imp
);
6041 if (imp
->match_c_string(" || "))
6046 else if (imp
->match_c_string(" && "))
6048 op
= OPERATOR_ANDAND
;
6051 else if (imp
->match_c_string(" == "))
6056 else if (imp
->match_c_string(" != "))
6058 op
= OPERATOR_NOTEQ
;
6061 else if (imp
->match_c_string(" < "))
6066 else if (imp
->match_c_string(" <= "))
6071 else if (imp
->match_c_string(" > "))
6076 else if (imp
->match_c_string(" >= "))
6081 else if (imp
->match_c_string(" + "))
6086 else if (imp
->match_c_string(" - "))
6088 op
= OPERATOR_MINUS
;
6091 else if (imp
->match_c_string(" | "))
6096 else if (imp
->match_c_string(" ^ "))
6101 else if (imp
->match_c_string(" * "))
6106 else if (imp
->match_c_string(" / "))
6111 else if (imp
->match_c_string(" % "))
6116 else if (imp
->match_c_string(" << "))
6118 op
= OPERATOR_LSHIFT
;
6121 else if (imp
->match_c_string(" >> "))
6123 op
= OPERATOR_RSHIFT
;
6126 else if (imp
->match_c_string(" & "))
6131 else if (imp
->match_c_string(" &^ "))
6133 op
= OPERATOR_BITCLEAR
;
6138 error_at(imp
->location(), "unrecognized binary operator");
6139 return Expression::make_error(imp
->location());
6142 Expression
* right
= Expression::import_expression(imp
);
6144 imp
->require_c_string(")");
6146 return Expression::make_binary(op
, left
, right
, imp
->location());
6149 // Dump ast representation of a binary expression.
6152 Binary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
6154 ast_dump_context
->ostream() << "(";
6155 ast_dump_context
->dump_expression(this->left_
);
6156 ast_dump_context
->ostream() << " ";
6157 ast_dump_context
->dump_operator(this->op_
);
6158 ast_dump_context
->ostream() << " ";
6159 ast_dump_context
->dump_expression(this->right_
);
6160 ast_dump_context
->ostream() << ") ";
6163 // Make a binary expression.
6166 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6169 return new Binary_expression(op
, left
, right
, location
);
6172 // Implement a comparison.
6175 Expression::comparison(Translate_context
* context
, Type
* result_type
,
6176 Operator op
, Expression
* left
, Expression
* right
,
6179 Type
* left_type
= left
->type();
6180 Type
* right_type
= right
->type();
6182 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, location
);
6184 if (left_type
->is_string_type() && right_type
->is_string_type())
6186 left
= Runtime::make_call(Runtime::STRCMP
, location
, 2,
6190 else if ((left_type
->interface_type() != NULL
6191 && right_type
->interface_type() == NULL
6192 && !right_type
->is_nil_type())
6193 || (left_type
->interface_type() == NULL
6194 && !left_type
->is_nil_type()
6195 && right_type
->interface_type() != NULL
))
6197 // Comparing an interface value to a non-interface value.
6198 if (left_type
->interface_type() == NULL
)
6200 std::swap(left_type
, right_type
);
6201 std::swap(left
, right
);
6204 // The right operand is not an interface. We need to take its
6205 // address if it is not a pointer.
6206 Expression
* pointer_arg
= NULL
;
6207 if (right_type
->points_to() != NULL
)
6208 pointer_arg
= right
;
6211 go_assert(right
->is_addressable());
6212 pointer_arg
= Expression::make_unary(OPERATOR_AND
, right
,
6216 Expression
* descriptor
=
6217 Expression::make_type_descriptor(right_type
, location
);
6219 Runtime::make_call((left_type
->interface_type()->is_empty()
6220 ? Runtime::EMPTY_INTERFACE_VALUE_COMPARE
6221 : Runtime::INTERFACE_VALUE_COMPARE
),
6222 location
, 3, left
, descriptor
,
6226 else if (left_type
->interface_type() != NULL
6227 && right_type
->interface_type() != NULL
)
6229 Runtime::Function compare_function
;
6230 if (left_type
->interface_type()->is_empty()
6231 && right_type
->interface_type()->is_empty())
6232 compare_function
= Runtime::EMPTY_INTERFACE_COMPARE
;
6233 else if (!left_type
->interface_type()->is_empty()
6234 && !right_type
->interface_type()->is_empty())
6235 compare_function
= Runtime::INTERFACE_COMPARE
;
6238 if (left_type
->interface_type()->is_empty())
6240 go_assert(op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
);
6241 std::swap(left_type
, right_type
);
6242 std::swap(left
, right
);
6244 go_assert(!left_type
->interface_type()->is_empty());
6245 go_assert(right_type
->interface_type()->is_empty());
6246 compare_function
= Runtime::INTERFACE_EMPTY_COMPARE
;
6249 left
= Runtime::make_call(compare_function
, location
, 2, left
, right
);
6253 if (left_type
->is_nil_type()
6254 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6256 std::swap(left_type
, right_type
);
6257 std::swap(left
, right
);
6260 if (right_type
->is_nil_type())
6262 right
= Expression::make_nil(location
);
6263 if (left_type
->array_type() != NULL
6264 && left_type
->array_type()->length() == NULL
)
6266 Array_type
* at
= left_type
->array_type();
6267 left
= at
->get_value_pointer(context
->gogo(), left
);
6269 else if (left_type
->interface_type() != NULL
)
6271 // An interface is nil if the first field is nil.
6272 left
= Expression::make_field_reference(left
, 0, location
);
6276 Bexpression
* left_bexpr
= left
->get_backend(context
);
6277 Bexpression
* right_bexpr
= right
->get_backend(context
);
6279 Gogo
* gogo
= context
->gogo();
6280 Bexpression
* ret
= gogo
->backend()->binary_expression(op
, left_bexpr
,
6281 right_bexpr
, location
);
6282 if (result_type
!= NULL
)
6283 ret
= gogo
->backend()->convert_expression(result_type
->get_backend(gogo
),
6288 // Class Bound_method_expression.
6293 Bound_method_expression::do_traverse(Traverse
* traverse
)
6295 return Expression::traverse(&this->expr_
, traverse
);
6298 // Lower the expression. If this is a method value rather than being
6299 // called, and the method is accessed via a pointer, we may need to
6300 // add nil checks. Introduce a temporary variable so that those nil
6301 // checks do not cause multiple evaluation.
6304 Bound_method_expression::do_lower(Gogo
*, Named_object
*,
6305 Statement_inserter
* inserter
, int)
6307 // For simplicity we use a temporary for every call to an embedded
6308 // method, even though some of them might be pure value methods and
6309 // not require a temporary.
6310 if (this->expr_
->var_expression() == NULL
6311 && this->expr_
->temporary_reference_expression() == NULL
6312 && this->expr_
->set_and_use_temporary_expression() == NULL
6313 && (this->method_
->field_indexes() != NULL
6314 || (this->method_
->is_value_method()
6315 && this->expr_
->type()->points_to() != NULL
)))
6317 Temporary_statement
* temp
=
6318 Statement::make_temporary(this->expr_
->type(), NULL
, this->location());
6319 inserter
->insert(temp
);
6320 this->expr_
= Expression::make_set_and_use_temporary(temp
, this->expr_
,
6326 // Return the type of a bound method expression. The type of this
6327 // object is simply the type of the method with no receiver.
6330 Bound_method_expression::do_type()
6332 Named_object
* fn
= this->method_
->named_object();
6333 Function_type
* fntype
;
6334 if (fn
->is_function())
6335 fntype
= fn
->func_value()->type();
6336 else if (fn
->is_function_declaration())
6337 fntype
= fn
->func_declaration_value()->type();
6339 return Type::make_error_type();
6340 return fntype
->copy_without_receiver();
6343 // Determine the types of a method expression.
6346 Bound_method_expression::do_determine_type(const Type_context
*)
6348 Named_object
* fn
= this->method_
->named_object();
6349 Function_type
* fntype
;
6350 if (fn
->is_function())
6351 fntype
= fn
->func_value()->type();
6352 else if (fn
->is_function_declaration())
6353 fntype
= fn
->func_declaration_value()->type();
6356 if (fntype
== NULL
|| !fntype
->is_method())
6357 this->expr_
->determine_type_no_context();
6360 Type_context
subcontext(fntype
->receiver()->type(), false);
6361 this->expr_
->determine_type(&subcontext
);
6365 // Check the types of a method expression.
6368 Bound_method_expression::do_check_types(Gogo
*)
6370 Named_object
* fn
= this->method_
->named_object();
6371 if (!fn
->is_function() && !fn
->is_function_declaration())
6373 this->report_error(_("object is not a method"));
6377 Function_type
* fntype
;
6378 if (fn
->is_function())
6379 fntype
= fn
->func_value()->type();
6380 else if (fn
->is_function_declaration())
6381 fntype
= fn
->func_declaration_value()->type();
6384 Type
* rtype
= fntype
->receiver()->type()->deref();
6385 Type
* etype
= (this->expr_type_
!= NULL
6387 : this->expr_
->type());
6388 etype
= etype
->deref();
6389 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6390 this->report_error(_("method type does not match object type"));
6393 // If a bound method expression is not simply called, then it is
6394 // represented as a closure. The closure will hold a single variable,
6395 // the receiver to pass to the method. The function will be a simple
6396 // thunk that pulls that value from the closure and calls the method
6397 // with the remaining arguments.
6399 // Because method values are not common, we don't build all thunks for
6400 // every methods, but instead only build them as we need them. In
6401 // particular, we even build them on demand for methods defined in
6404 Bound_method_expression::Method_value_thunks
6405 Bound_method_expression::method_value_thunks
;
6407 // Find or create the thunk for METHOD.
6410 Bound_method_expression::create_thunk(Gogo
* gogo
, const Method
* method
,
6413 std::pair
<Named_object
*, Named_object
*> val(fn
, NULL
);
6414 std::pair
<Method_value_thunks::iterator
, bool> ins
=
6415 Bound_method_expression::method_value_thunks
.insert(val
);
6418 // We have seen this method before.
6419 go_assert(ins
.first
->second
!= NULL
);
6420 return ins
.first
->second
;
6423 Location loc
= fn
->location();
6425 Function_type
* orig_fntype
;
6426 if (fn
->is_function())
6427 orig_fntype
= fn
->func_value()->type();
6428 else if (fn
->is_function_declaration())
6429 orig_fntype
= fn
->func_declaration_value()->type();
6433 if (orig_fntype
== NULL
|| !orig_fntype
->is_method())
6435 ins
.first
->second
= Named_object::make_erroneous_name(Gogo::thunk_name());
6436 return ins
.first
->second
;
6439 Struct_field_list
* sfl
= new Struct_field_list();
6440 // The type here is wrong--it should be the C function type. But it
6441 // doesn't really matter.
6442 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
6443 sfl
->push_back(Struct_field(Typed_identifier("fn.0", vt
, loc
)));
6444 sfl
->push_back(Struct_field(Typed_identifier("val.1",
6445 orig_fntype
->receiver()->type(),
6447 Type
* closure_type
= Type::make_struct_type(sfl
, loc
);
6448 closure_type
= Type::make_pointer_type(closure_type
);
6450 Function_type
* new_fntype
= orig_fntype
->copy_with_names();
6452 std::string thunk_name
= Gogo::thunk_name();
6453 Named_object
* new_no
= gogo
->start_function(thunk_name
, new_fntype
,
6456 Variable
* cvar
= new Variable(closure_type
, NULL
, false, false, false, loc
);
6457 cvar
->set_is_used();
6458 cvar
->set_is_closure();
6459 Named_object
* cp
= Named_object::make_variable("$closure" + thunk_name
,
6461 new_no
->func_value()->set_closure_var(cp
);
6463 gogo
->start_block(loc
);
6465 // Field 0 of the closure is the function code pointer, field 1 is
6466 // the value on which to invoke the method.
6467 Expression
* arg
= Expression::make_var_reference(cp
, loc
);
6468 arg
= Expression::make_unary(OPERATOR_MULT
, arg
, loc
);
6469 arg
= Expression::make_field_reference(arg
, 1, loc
);
6471 Expression
* bme
= Expression::make_bound_method(arg
, method
, fn
, loc
);
6473 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
6474 Expression_list
* args
;
6475 if (orig_params
== NULL
|| orig_params
->empty())
6479 const Typed_identifier_list
* new_params
= new_fntype
->parameters();
6480 args
= new Expression_list();
6481 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
6482 p
!= new_params
->end();
6485 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
6486 go_assert(p_no
!= NULL
6487 && p_no
->is_variable()
6488 && p_no
->var_value()->is_parameter());
6489 args
->push_back(Expression::make_var_reference(p_no
, loc
));
6493 Call_expression
* call
= Expression::make_call(bme
, args
,
6494 orig_fntype
->is_varargs(),
6496 call
->set_varargs_are_lowered();
6498 Statement
* s
= Statement::make_return_from_call(call
, loc
);
6499 gogo
->add_statement(s
);
6500 Block
* b
= gogo
->finish_block(loc
);
6501 gogo
->add_block(b
, loc
);
6502 gogo
->lower_block(new_no
, b
);
6503 gogo
->flatten_block(new_no
, b
);
6504 gogo
->finish_function(loc
);
6506 ins
.first
->second
= new_no
;
6510 // Return an expression to check *REF for nil while dereferencing
6511 // according to FIELD_INDEXES. Update *REF to build up the field
6512 // reference. This is a static function so that we don't have to
6513 // worry about declaring Field_indexes in expressions.h.
6516 bme_check_nil(const Method::Field_indexes
* field_indexes
, Location loc
,
6519 if (field_indexes
== NULL
)
6520 return Expression::make_boolean(false, loc
);
6521 Expression
* cond
= bme_check_nil(field_indexes
->next
, loc
, ref
);
6522 Struct_type
* stype
= (*ref
)->type()->deref()->struct_type();
6523 go_assert(stype
!= NULL
6524 && field_indexes
->field_index
< stype
->field_count());
6525 if ((*ref
)->type()->struct_type() == NULL
)
6527 go_assert((*ref
)->type()->points_to() != NULL
);
6528 Expression
* n
= Expression::make_binary(OPERATOR_EQEQ
, *ref
,
6529 Expression::make_nil(loc
),
6531 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, n
, loc
);
6532 *ref
= Expression::make_unary(OPERATOR_MULT
, *ref
, loc
);
6533 go_assert((*ref
)->type()->struct_type() == stype
);
6535 *ref
= Expression::make_field_reference(*ref
, field_indexes
->field_index
,
6540 // Get the backend representation for a method value.
6543 Bound_method_expression::do_get_backend(Translate_context
* context
)
6545 Named_object
* thunk
= Bound_method_expression::create_thunk(context
->gogo(),
6548 if (thunk
->is_erroneous())
6550 go_assert(saw_errors());
6551 return context
->backend()->error_expression();
6554 // FIXME: We should lower this earlier, but we can't lower it in the
6555 // lowering pass because at that point we don't know whether we need
6556 // to create the thunk or not. If the expression is called, we
6557 // don't need the thunk.
6559 Location loc
= this->location();
6561 // If the method expects a value, and we have a pointer, we need to
6562 // dereference the pointer.
6564 Named_object
* fn
= this->method_
->named_object();
6565 Function_type
* fntype
;
6566 if (fn
->is_function())
6567 fntype
= fn
->func_value()->type();
6568 else if (fn
->is_function_declaration())
6569 fntype
= fn
->func_declaration_value()->type();
6573 Expression
* val
= this->expr_
;
6574 if (fntype
->receiver()->type()->points_to() == NULL
6575 && val
->type()->points_to() != NULL
)
6576 val
= Expression::make_unary(OPERATOR_MULT
, val
, loc
);
6578 // Note that we are ignoring this->expr_type_ here. The thunk will
6579 // expect a closure whose second field has type this->expr_type_ (if
6580 // that is not NULL). We are going to pass it a closure whose
6581 // second field has type this->expr_->type(). Since
6582 // this->expr_type_ is only not-NULL for pointer types, we can get
6585 Struct_field_list
* fields
= new Struct_field_list();
6586 fields
->push_back(Struct_field(Typed_identifier("fn.0",
6587 thunk
->func_value()->type(),
6589 fields
->push_back(Struct_field(Typed_identifier("val.1", val
->type(), loc
)));
6590 Struct_type
* st
= Type::make_struct_type(fields
, loc
);
6592 Expression_list
* vals
= new Expression_list();
6593 vals
->push_back(Expression::make_func_code_reference(thunk
, loc
));
6594 vals
->push_back(val
);
6596 Expression
* ret
= Expression::make_struct_composite_literal(st
, vals
, loc
);
6597 ret
= Expression::make_heap_expression(ret
, loc
);
6599 // See whether the expression or any embedded pointers are nil.
6601 Expression
* nil_check
= NULL
;
6602 Expression
* expr
= this->expr_
;
6603 if (this->method_
->field_indexes() != NULL
)
6605 // Note that we are evaluating this->expr_ twice, but that is OK
6606 // because in the lowering pass we forced it into a temporary
6608 Expression
* ref
= expr
;
6609 nil_check
= bme_check_nil(this->method_
->field_indexes(), loc
, &ref
);
6613 if (this->method_
->is_value_method() && expr
->type()->points_to() != NULL
)
6615 Expression
* n
= Expression::make_binary(OPERATOR_EQEQ
, expr
,
6616 Expression::make_nil(loc
),
6618 if (nil_check
== NULL
)
6621 nil_check
= Expression::make_binary(OPERATOR_OROR
, nil_check
, n
, loc
);
6624 Bexpression
* bme
= ret
->get_backend(context
);
6625 if (nil_check
!= NULL
)
6627 Gogo
* gogo
= context
->gogo();
6628 Bexpression
* crash
=
6629 gogo
->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
6630 loc
)->get_backend(context
);
6631 Btype
* btype
= ret
->type()->get_backend(gogo
);
6632 Bexpression
* bcheck
= nil_check
->get_backend(context
);
6633 bme
= gogo
->backend()->conditional_expression(btype
, bcheck
, crash
,
6639 // Dump ast representation of a bound method expression.
6642 Bound_method_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
6645 if (this->expr_type_
!= NULL
)
6646 ast_dump_context
->ostream() << "(";
6647 ast_dump_context
->dump_expression(this->expr_
);
6648 if (this->expr_type_
!= NULL
)
6650 ast_dump_context
->ostream() << ":";
6651 ast_dump_context
->dump_type(this->expr_type_
);
6652 ast_dump_context
->ostream() << ")";
6655 ast_dump_context
->ostream() << "." << this->function_
->name();
6658 // Make a method expression.
6660 Bound_method_expression
*
6661 Expression::make_bound_method(Expression
* expr
, const Method
* method
,
6662 Named_object
* function
, Location location
)
6664 return new Bound_method_expression(expr
, method
, function
, location
);
6667 // Class Builtin_call_expression. This is used for a call to a
6668 // builtin function.
6670 class Builtin_call_expression
: public Call_expression
6673 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6674 bool is_varargs
, Location location
);
6677 // This overrides Call_expression::do_lower.
6679 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
6682 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
6685 do_is_constant() const;
6688 do_numeric_constant_value(Numeric_constant
*) const;
6691 do_discarding_value();
6697 do_determine_type(const Type_context
*);
6700 do_check_types(Gogo
*);
6706 do_get_backend(Translate_context
*);
6709 do_export(Export
*) const;
6712 do_is_recover_call() const;
6715 do_set_recover_arg(Expression
*);
6718 // The builtin functions.
6719 enum Builtin_function_code
6723 // Predeclared builtin functions.
6740 // Builtin functions from the unsafe package.
6753 real_imag_type(Type
*);
6756 complex_type(Type
*);
6762 check_int_value(Expression
*, bool is_length
);
6764 // A pointer back to the general IR structure. This avoids a global
6765 // variable, or passing it around everywhere.
6767 // The builtin function being called.
6768 Builtin_function_code code_
;
6769 // Used to stop endless loops when the length of an array uses len
6770 // or cap of the array itself.
6772 // Whether the argument is set for calls to BUILTIN_RECOVER.
6773 bool recover_arg_is_set_
;
6776 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6778 Expression_list
* args
,
6781 : Call_expression(fn
, args
, is_varargs
, location
),
6782 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false),
6783 recover_arg_is_set_(false)
6785 Func_expression
* fnexp
= this->fn()->func_expression();
6788 this->code_
= BUILTIN_INVALID
;
6791 const std::string
& name(fnexp
->named_object()->name());
6792 if (name
== "append")
6793 this->code_
= BUILTIN_APPEND
;
6794 else if (name
== "cap")
6795 this->code_
= BUILTIN_CAP
;
6796 else if (name
== "close")
6797 this->code_
= BUILTIN_CLOSE
;
6798 else if (name
== "complex")
6799 this->code_
= BUILTIN_COMPLEX
;
6800 else if (name
== "copy")
6801 this->code_
= BUILTIN_COPY
;
6802 else if (name
== "delete")
6803 this->code_
= BUILTIN_DELETE
;
6804 else if (name
== "imag")
6805 this->code_
= BUILTIN_IMAG
;
6806 else if (name
== "len")
6807 this->code_
= BUILTIN_LEN
;
6808 else if (name
== "make")
6809 this->code_
= BUILTIN_MAKE
;
6810 else if (name
== "new")
6811 this->code_
= BUILTIN_NEW
;
6812 else if (name
== "panic")
6813 this->code_
= BUILTIN_PANIC
;
6814 else if (name
== "print")
6815 this->code_
= BUILTIN_PRINT
;
6816 else if (name
== "println")
6817 this->code_
= BUILTIN_PRINTLN
;
6818 else if (name
== "real")
6819 this->code_
= BUILTIN_REAL
;
6820 else if (name
== "recover")
6821 this->code_
= BUILTIN_RECOVER
;
6822 else if (name
== "Alignof")
6823 this->code_
= BUILTIN_ALIGNOF
;
6824 else if (name
== "Offsetof")
6825 this->code_
= BUILTIN_OFFSETOF
;
6826 else if (name
== "Sizeof")
6827 this->code_
= BUILTIN_SIZEOF
;
6832 // Return whether this is a call to recover. This is a virtual
6833 // function called from the parent class.
6836 Builtin_call_expression::do_is_recover_call() const
6838 if (this->classification() == EXPRESSION_ERROR
)
6840 return this->code_
== BUILTIN_RECOVER
;
6843 // Set the argument for a call to recover.
6846 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6848 const Expression_list
* args
= this->args();
6849 go_assert(args
== NULL
|| args
->empty());
6850 Expression_list
* new_args
= new Expression_list();
6851 new_args
->push_back(arg
);
6852 this->set_args(new_args
);
6853 this->recover_arg_is_set_
= true;
6856 // Lower a builtin call expression. This turns new and make into
6857 // specific expressions. We also convert to a constant if we can.
6860 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
6861 Statement_inserter
* inserter
, int)
6863 if (this->is_error_expression())
6866 Location loc
= this->location();
6868 if (this->is_varargs() && this->code_
!= BUILTIN_APPEND
)
6870 this->report_error(_("invalid use of %<...%> with builtin function"));
6871 return Expression::make_error(loc
);
6874 if (this->code_
== BUILTIN_OFFSETOF
)
6876 Expression
* arg
= this->one_arg();
6878 if (arg
->bound_method_expression() != NULL
6879 || arg
->interface_field_reference_expression() != NULL
)
6881 this->report_error(_("invalid use of method value as argument "
6886 Field_reference_expression
* farg
= arg
->field_reference_expression();
6887 while (farg
!= NULL
)
6889 if (!farg
->implicit())
6891 // When the selector refers to an embedded field,
6892 // it must not be reached through pointer indirections.
6893 if (farg
->expr()->deref() != farg
->expr())
6895 this->report_error(_("argument of Offsetof implies "
6896 "indirection of an embedded field"));
6899 // Go up until we reach the original base.
6900 farg
= farg
->expr()->field_reference_expression();
6904 if (this->is_constant())
6906 Numeric_constant nc
;
6907 if (this->numeric_constant_value(&nc
))
6908 return nc
.expression(loc
);
6911 switch (this->code_
)
6918 const Expression_list
* args
= this->args();
6919 if (args
== NULL
|| args
->size() < 1)
6920 this->report_error(_("not enough arguments"));
6921 else if (args
->size() > 1)
6922 this->report_error(_("too many arguments"));
6925 Expression
* arg
= args
->front();
6926 if (!arg
->is_type_expression())
6928 error_at(arg
->location(), "expected type");
6929 this->set_is_error();
6932 return Expression::make_allocation(arg
->type(), loc
);
6938 return this->lower_make();
6940 case BUILTIN_RECOVER
:
6941 if (function
!= NULL
)
6942 function
->func_value()->set_calls_recover();
6945 // Calling recover outside of a function always returns the
6946 // nil empty interface.
6947 Type
* eface
= Type::make_empty_interface_type(loc
);
6948 return Expression::make_cast(eface
, Expression::make_nil(loc
), loc
);
6952 case BUILTIN_APPEND
:
6954 // Lower the varargs.
6955 const Expression_list
* args
= this->args();
6956 if (args
== NULL
|| args
->empty())
6958 Type
* slice_type
= args
->front()->type();
6959 if (!slice_type
->is_slice_type())
6961 if (slice_type
->is_nil_type())
6962 error_at(args
->front()->location(), "use of untyped nil");
6964 error_at(args
->front()->location(),
6965 "argument 1 must be a slice");
6966 this->set_is_error();
6969 Type
* element_type
= slice_type
->array_type()->element_type();
6970 this->lower_varargs(gogo
, function
, inserter
,
6971 Type::make_array_type(element_type
, NULL
),
6976 case BUILTIN_DELETE
:
6978 // Lower to a runtime function call.
6979 const Expression_list
* args
= this->args();
6980 if (args
== NULL
|| args
->size() < 2)
6981 this->report_error(_("not enough arguments"));
6982 else if (args
->size() > 2)
6983 this->report_error(_("too many arguments"));
6984 else if (args
->front()->type()->map_type() == NULL
)
6985 this->report_error(_("argument 1 must be a map"));
6988 // Since this function returns no value it must appear in
6989 // a statement by itself, so we don't have to worry about
6990 // order of evaluation of values around it. Evaluate the
6991 // map first to get order of evaluation right.
6992 Map_type
* mt
= args
->front()->type()->map_type();
6993 Temporary_statement
* map_temp
=
6994 Statement::make_temporary(mt
, args
->front(), loc
);
6995 inserter
->insert(map_temp
);
6997 Temporary_statement
* key_temp
=
6998 Statement::make_temporary(mt
->key_type(), args
->back(), loc
);
6999 inserter
->insert(key_temp
);
7001 Expression
* e1
= Expression::make_temporary_reference(map_temp
,
7003 Expression
* e2
= Expression::make_temporary_reference(key_temp
,
7005 e2
= Expression::make_unary(OPERATOR_AND
, e2
, loc
);
7006 return Runtime::make_call(Runtime::MAPDELETE
, this->location(),
7016 // Flatten a builtin call expression. This turns the arguments of copy and
7017 // append into temporary expressions.
7020 Builtin_call_expression::do_flatten(Gogo
*, Named_object
*,
7021 Statement_inserter
* inserter
)
7023 Location loc
= this->location();
7025 switch (this->code_
)
7030 case BUILTIN_APPEND
:
7033 Type
* at
= this->args()->front()->type();
7034 for (Expression_list::iterator pa
= this->args()->begin();
7035 pa
!= this->args()->end();
7038 if ((*pa
)->is_nil_expression())
7040 Expression
* nil
= Expression::make_nil(loc
);
7041 Expression
* zero
= Expression::make_integer_ul(0, NULL
, loc
);
7042 *pa
= Expression::make_slice_value(at
, nil
, zero
, zero
, loc
);
7044 if (!(*pa
)->is_variable())
7046 Temporary_statement
* temp
=
7047 Statement::make_temporary(NULL
, *pa
, loc
);
7048 inserter
->insert(temp
);
7049 *pa
= Expression::make_temporary_reference(temp
, loc
);
7056 for (Expression_list::iterator pa
= this->args()->begin();
7057 pa
!= this->args()->end();
7060 if (!(*pa
)->is_variable() && (*pa
)->type()->interface_type() != NULL
)
7062 Temporary_statement
* temp
=
7063 Statement::make_temporary(NULL
, *pa
, loc
);
7064 inserter
->insert(temp
);
7065 *pa
= Expression::make_temporary_reference(temp
, loc
);
7073 // Lower a make expression.
7076 Builtin_call_expression::lower_make()
7078 Location loc
= this->location();
7080 const Expression_list
* args
= this->args();
7081 if (args
== NULL
|| args
->size() < 1)
7083 this->report_error(_("not enough arguments"));
7084 return Expression::make_error(this->location());
7087 Expression_list::const_iterator parg
= args
->begin();
7089 Expression
* first_arg
= *parg
;
7090 if (!first_arg
->is_type_expression())
7092 error_at(first_arg
->location(), "expected type");
7093 this->set_is_error();
7094 return Expression::make_error(this->location());
7096 Type
* type
= first_arg
->type();
7098 bool is_slice
= false;
7099 bool is_map
= false;
7100 bool is_chan
= false;
7101 if (type
->is_slice_type())
7103 else if (type
->map_type() != NULL
)
7105 else if (type
->channel_type() != NULL
)
7109 this->report_error(_("invalid type for make function"));
7110 return Expression::make_error(this->location());
7113 bool have_big_args
= false;
7114 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
7115 int uintptr_bits
= uintptr_type
->integer_type()->bits();
7117 Type_context
int_context(Type::lookup_integer_type("int"), false);
7120 Expression
* len_arg
;
7121 if (parg
== args
->end())
7125 this->report_error(_("length required when allocating a slice"));
7126 return Expression::make_error(this->location());
7128 len_arg
= Expression::make_integer_ul(0, NULL
, loc
);
7133 len_arg
->determine_type(&int_context
);
7134 if (!this->check_int_value(len_arg
, true))
7135 return Expression::make_error(this->location());
7136 if (len_arg
->type()->integer_type() != NULL
7137 && len_arg
->type()->integer_type()->bits() > uintptr_bits
)
7138 have_big_args
= true;
7142 Expression
* cap_arg
= NULL
;
7143 if (is_slice
&& parg
!= args
->end())
7146 cap_arg
->determine_type(&int_context
);
7147 if (!this->check_int_value(cap_arg
, false))
7148 return Expression::make_error(this->location());
7150 Numeric_constant nclen
;
7151 Numeric_constant nccap
;
7154 if (len_arg
->numeric_constant_value(&nclen
)
7155 && cap_arg
->numeric_constant_value(&nccap
)
7156 && nclen
.to_unsigned_long(&vlen
) == Numeric_constant::NC_UL_VALID
7157 && nccap
.to_unsigned_long(&vcap
) == Numeric_constant::NC_UL_VALID
7160 this->report_error(_("len larger than cap"));
7161 return Expression::make_error(this->location());
7164 if (cap_arg
->type()->integer_type() != NULL
7165 && cap_arg
->type()->integer_type()->bits() > uintptr_bits
)
7166 have_big_args
= true;
7170 if (parg
!= args
->end())
7172 this->report_error(_("too many arguments to make"));
7173 return Expression::make_error(this->location());
7176 Location type_loc
= first_arg
->location();
7177 Expression
* type_arg
;
7178 if (is_slice
|| is_chan
)
7179 type_arg
= Expression::make_type_descriptor(type
, type_loc
);
7181 type_arg
= Expression::make_map_descriptor(type
->map_type(), type_loc
);
7188 if (cap_arg
== NULL
)
7189 call
= Runtime::make_call((have_big_args
7190 ? Runtime::MAKESLICE1BIG
7191 : Runtime::MAKESLICE1
),
7192 loc
, 2, type_arg
, len_arg
);
7194 call
= Runtime::make_call((have_big_args
7195 ? Runtime::MAKESLICE2BIG
7196 : Runtime::MAKESLICE2
),
7197 loc
, 3, type_arg
, len_arg
, cap_arg
);
7200 call
= Runtime::make_call((have_big_args
7201 ? Runtime::MAKEMAPBIG
7202 : Runtime::MAKEMAP
),
7203 loc
, 2, type_arg
, len_arg
);
7205 call
= Runtime::make_call((have_big_args
7206 ? Runtime::MAKECHANBIG
7207 : Runtime::MAKECHAN
),
7208 loc
, 2, type_arg
, len_arg
);
7212 return Expression::make_unsafe_cast(type
, call
, loc
);
7215 // Return whether an expression has an integer value. Report an error
7216 // if not. This is used when handling calls to the predeclared make
7220 Builtin_call_expression::check_int_value(Expression
* e
, bool is_length
)
7222 Numeric_constant nc
;
7223 if (e
->numeric_constant_value(&nc
))
7226 switch (nc
.to_unsigned_long(&v
))
7228 case Numeric_constant::NC_UL_VALID
:
7230 case Numeric_constant::NC_UL_NOTINT
:
7231 error_at(e
->location(), "non-integer %s argument to make",
7232 is_length
? "len" : "cap");
7234 case Numeric_constant::NC_UL_NEGATIVE
:
7235 error_at(e
->location(), "negative %s argument to make",
7236 is_length
? "len" : "cap");
7238 case Numeric_constant::NC_UL_BIG
:
7239 // We don't want to give a compile-time error for a 64-bit
7240 // value on a 32-bit target.
7245 if (!nc
.to_int(&val
))
7247 int bits
= mpz_sizeinbase(val
, 2);
7249 Type
* int_type
= Type::lookup_integer_type("int");
7250 if (bits
>= int_type
->integer_type()->bits())
7252 error_at(e
->location(), "%s argument too large for make",
7253 is_length
? "len" : "cap");
7260 if (e
->type()->integer_type() != NULL
)
7263 error_at(e
->location(), "non-integer %s argument to make",
7264 is_length
? "len" : "cap");
7268 // Return the type of the real or imag functions, given the type of
7269 // the argument. We need to map complex64 to float32 and complex128
7270 // to float64, so it has to be done by name. This returns NULL if it
7271 // can't figure out the type.
7274 Builtin_call_expression::real_imag_type(Type
* arg_type
)
7276 if (arg_type
== NULL
|| arg_type
->is_abstract())
7278 Named_type
* nt
= arg_type
->named_type();
7281 while (nt
->real_type()->named_type() != NULL
)
7282 nt
= nt
->real_type()->named_type();
7283 if (nt
->name() == "complex64")
7284 return Type::lookup_float_type("float32");
7285 else if (nt
->name() == "complex128")
7286 return Type::lookup_float_type("float64");
7291 // Return the type of the complex function, given the type of one of the
7292 // argments. Like real_imag_type, we have to map by name.
7295 Builtin_call_expression::complex_type(Type
* arg_type
)
7297 if (arg_type
== NULL
|| arg_type
->is_abstract())
7299 Named_type
* nt
= arg_type
->named_type();
7302 while (nt
->real_type()->named_type() != NULL
)
7303 nt
= nt
->real_type()->named_type();
7304 if (nt
->name() == "float32")
7305 return Type::lookup_complex_type("complex64");
7306 else if (nt
->name() == "float64")
7307 return Type::lookup_complex_type("complex128");
7312 // Return a single argument, or NULL if there isn't one.
7315 Builtin_call_expression::one_arg() const
7317 const Expression_list
* args
= this->args();
7318 if (args
== NULL
|| args
->size() != 1)
7320 return args
->front();
7323 // A traversal class which looks for a call or receive expression.
7325 class Find_call_expression
: public Traverse
7328 Find_call_expression()
7329 : Traverse(traverse_expressions
),
7334 expression(Expression
**);
7338 { return this->found_
; }
7345 Find_call_expression::expression(Expression
** pexpr
)
7347 if ((*pexpr
)->call_expression() != NULL
7348 || (*pexpr
)->receive_expression() != NULL
)
7350 this->found_
= true;
7351 return TRAVERSE_EXIT
;
7353 return TRAVERSE_CONTINUE
;
7356 // Return whether this is constant: len of a string constant, or len
7357 // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof,
7361 Builtin_call_expression::do_is_constant() const
7363 if (this->is_error_expression())
7365 switch (this->code_
)
7373 Expression
* arg
= this->one_arg();
7376 Type
* arg_type
= arg
->type();
7378 if (arg_type
->points_to() != NULL
7379 && arg_type
->points_to()->array_type() != NULL
7380 && !arg_type
->points_to()->is_slice_type())
7381 arg_type
= arg_type
->points_to();
7383 // The len and cap functions are only constant if there are no
7384 // function calls or channel operations in the arguments.
7385 // Otherwise we have to make the call.
7386 if (!arg
->is_constant())
7388 Find_call_expression find_call
;
7389 Expression::traverse(&arg
, &find_call
);
7390 if (find_call
.found())
7394 if (arg_type
->array_type() != NULL
7395 && arg_type
->array_type()->length() != NULL
)
7398 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
7401 bool ret
= arg
->is_constant();
7402 this->seen_
= false;
7408 case BUILTIN_SIZEOF
:
7409 case BUILTIN_ALIGNOF
:
7410 return this->one_arg() != NULL
;
7412 case BUILTIN_OFFSETOF
:
7414 Expression
* arg
= this->one_arg();
7417 return arg
->field_reference_expression() != NULL
;
7420 case BUILTIN_COMPLEX
:
7422 const Expression_list
* args
= this->args();
7423 if (args
!= NULL
&& args
->size() == 2)
7424 return args
->front()->is_constant() && args
->back()->is_constant();
7431 Expression
* arg
= this->one_arg();
7432 return arg
!= NULL
&& arg
->is_constant();
7442 // Return a numeric constant if possible.
7445 Builtin_call_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
7447 if (this->code_
== BUILTIN_LEN
7448 || this->code_
== BUILTIN_CAP
)
7450 Expression
* arg
= this->one_arg();
7453 Type
* arg_type
= arg
->type();
7455 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
7458 if (arg
->string_constant_value(&sval
))
7460 nc
->set_unsigned_long(Type::lookup_integer_type("int"),
7466 if (arg_type
->points_to() != NULL
7467 && arg_type
->points_to()->array_type() != NULL
7468 && !arg_type
->points_to()->is_slice_type())
7469 arg_type
= arg_type
->points_to();
7471 if (arg_type
->array_type() != NULL
7472 && arg_type
->array_type()->length() != NULL
)
7476 Expression
* e
= arg_type
->array_type()->length();
7478 bool r
= e
->numeric_constant_value(nc
);
7479 this->seen_
= false;
7482 if (!nc
->set_type(Type::lookup_integer_type("int"), false,
7489 else if (this->code_
== BUILTIN_SIZEOF
7490 || this->code_
== BUILTIN_ALIGNOF
)
7492 Expression
* arg
= this->one_arg();
7495 Type
* arg_type
= arg
->type();
7496 if (arg_type
->is_error())
7498 if (arg_type
->is_abstract())
7504 if (this->code_
== BUILTIN_SIZEOF
)
7507 bool ok
= arg_type
->backend_type_size(this->gogo_
, &ret
);
7508 this->seen_
= false;
7512 else if (this->code_
== BUILTIN_ALIGNOF
)
7516 if (arg
->field_reference_expression() == NULL
)
7517 ok
= arg_type
->backend_type_align(this->gogo_
, &ret
);
7520 // Calling unsafe.Alignof(s.f) returns the alignment of
7521 // the type of f when it is used as a field in a struct.
7522 ok
= arg_type
->backend_type_field_align(this->gogo_
, &ret
);
7524 this->seen_
= false;
7532 set_mpz_from_int64(&zval
, ret
);
7533 nc
->set_int(Type::lookup_integer_type("uintptr"), zval
);
7537 else if (this->code_
== BUILTIN_OFFSETOF
)
7539 Expression
* arg
= this->one_arg();
7542 Field_reference_expression
* farg
= arg
->field_reference_expression();
7548 int64_t total_offset
= 0;
7551 Expression
* struct_expr
= farg
->expr();
7552 Type
* st
= struct_expr
->type();
7553 if (st
->struct_type() == NULL
)
7555 if (st
->named_type() != NULL
)
7556 st
->named_type()->convert(this->gogo_
);
7559 bool ok
= st
->struct_type()->backend_field_offset(this->gogo_
,
7560 farg
->field_index(),
7562 this->seen_
= false;
7565 total_offset
+= offset
;
7566 if (farg
->implicit() && struct_expr
->field_reference_expression() != NULL
)
7568 // Go up until we reach the original base.
7569 farg
= struct_expr
->field_reference_expression();
7575 set_mpz_from_int64(&zval
, total_offset
);
7576 nc
->set_int(Type::lookup_integer_type("uintptr"), zval
);
7580 else if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
7582 Expression
* arg
= this->one_arg();
7586 Numeric_constant argnc
;
7587 if (!arg
->numeric_constant_value(&argnc
))
7591 if (!argnc
.to_complex(&val
))
7594 Type
* type
= Builtin_call_expression::real_imag_type(argnc
.type());
7595 if (this->code_
== BUILTIN_REAL
)
7596 nc
->set_float(type
, mpc_realref(val
));
7598 nc
->set_float(type
, mpc_imagref(val
));
7602 else if (this->code_
== BUILTIN_COMPLEX
)
7604 const Expression_list
* args
= this->args();
7605 if (args
== NULL
|| args
->size() != 2)
7608 Numeric_constant rnc
;
7609 if (!args
->front()->numeric_constant_value(&rnc
))
7611 Numeric_constant inc
;
7612 if (!args
->back()->numeric_constant_value(&inc
))
7615 if (rnc
.type() != NULL
7616 && !rnc
.type()->is_abstract()
7617 && inc
.type() != NULL
7618 && !inc
.type()->is_abstract()
7619 && !Type::are_identical(rnc
.type(), inc
.type(), false, NULL
))
7623 if (!rnc
.to_float(&r
))
7626 if (!inc
.to_float(&i
))
7632 Type
* arg_type
= rnc
.type();
7633 if (arg_type
== NULL
|| arg_type
->is_abstract())
7634 arg_type
= inc
.type();
7637 mpc_init2(val
, mpc_precision
);
7638 mpc_set_fr_fr(val
, r
, i
, MPC_RNDNN
);
7642 Type
* type
= Builtin_call_expression::complex_type(arg_type
);
7643 nc
->set_complex(type
, val
);
7653 // Give an error if we are discarding the value of an expression which
7654 // should not normally be discarded. We don't give an error for
7655 // discarding the value of an ordinary function call, but we do for
7656 // builtin functions, purely for consistency with the gc compiler.
7659 Builtin_call_expression::do_discarding_value()
7661 switch (this->code_
)
7663 case BUILTIN_INVALID
:
7667 case BUILTIN_APPEND
:
7669 case BUILTIN_COMPLEX
:
7675 case BUILTIN_ALIGNOF
:
7676 case BUILTIN_OFFSETOF
:
7677 case BUILTIN_SIZEOF
:
7678 this->unused_value_error();
7683 case BUILTIN_DELETE
:
7686 case BUILTIN_PRINTLN
:
7687 case BUILTIN_RECOVER
:
7695 Builtin_call_expression::do_type()
7697 if (this->is_error_expression())
7698 return Type::make_error_type();
7699 switch (this->code_
)
7701 case BUILTIN_INVALID
:
7703 return Type::make_error_type();
7708 const Expression_list
* args
= this->args();
7709 if (args
== NULL
|| args
->empty())
7710 return Type::make_error_type();
7711 return Type::make_pointer_type(args
->front()->type());
7717 return Type::lookup_integer_type("int");
7719 case BUILTIN_ALIGNOF
:
7720 case BUILTIN_OFFSETOF
:
7721 case BUILTIN_SIZEOF
:
7722 return Type::lookup_integer_type("uintptr");
7725 case BUILTIN_DELETE
:
7728 case BUILTIN_PRINTLN
:
7729 return Type::make_void_type();
7731 case BUILTIN_RECOVER
:
7732 return Type::make_empty_interface_type(Linemap::predeclared_location());
7734 case BUILTIN_APPEND
:
7736 const Expression_list
* args
= this->args();
7737 if (args
== NULL
|| args
->empty())
7738 return Type::make_error_type();
7739 Type
*ret
= args
->front()->type();
7740 if (!ret
->is_slice_type())
7741 return Type::make_error_type();
7748 Expression
* arg
= this->one_arg();
7750 return Type::make_error_type();
7751 Type
* t
= arg
->type();
7752 if (t
->is_abstract())
7753 t
= t
->make_non_abstract_type();
7754 t
= Builtin_call_expression::real_imag_type(t
);
7756 t
= Type::make_error_type();
7760 case BUILTIN_COMPLEX
:
7762 const Expression_list
* args
= this->args();
7763 if (args
== NULL
|| args
->size() != 2)
7764 return Type::make_error_type();
7765 Type
* t
= args
->front()->type();
7766 if (t
->is_abstract())
7768 t
= args
->back()->type();
7769 if (t
->is_abstract())
7770 t
= t
->make_non_abstract_type();
7772 t
= Builtin_call_expression::complex_type(t
);
7774 t
= Type::make_error_type();
7780 // Determine the type.
7783 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7785 if (!this->determining_types())
7788 this->fn()->determine_type_no_context();
7790 const Expression_list
* args
= this->args();
7793 Type
* arg_type
= NULL
;
7794 switch (this->code_
)
7797 case BUILTIN_PRINTLN
:
7798 // Do not force a large integer constant to "int".
7804 arg_type
= Builtin_call_expression::complex_type(context
->type
);
7805 if (arg_type
== NULL
)
7806 arg_type
= Type::lookup_complex_type("complex128");
7810 case BUILTIN_COMPLEX
:
7812 // For the complex function the type of one operand can
7813 // determine the type of the other, as in a binary expression.
7814 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7815 if (arg_type
== NULL
)
7816 arg_type
= Type::lookup_float_type("float64");
7817 if (args
!= NULL
&& args
->size() == 2)
7819 Type
* t1
= args
->front()->type();
7820 Type
* t2
= args
->back()->type();
7821 if (!t1
->is_abstract())
7823 else if (!t2
->is_abstract())
7837 for (Expression_list::const_iterator pa
= args
->begin();
7841 Type_context subcontext
;
7842 subcontext
.type
= arg_type
;
7846 // We want to print large constants, we so can't just
7847 // use the appropriate nonabstract type. Use uint64 for
7848 // an integer if we know it is nonnegative, otherwise
7849 // use int64 for a integer, otherwise use float64 for a
7850 // float or complex128 for a complex.
7851 Type
* want_type
= NULL
;
7852 Type
* atype
= (*pa
)->type();
7853 if (atype
->is_abstract())
7855 if (atype
->integer_type() != NULL
)
7857 Numeric_constant nc
;
7858 if (this->numeric_constant_value(&nc
))
7861 if (nc
.to_int(&val
))
7863 if (mpz_sgn(val
) >= 0)
7864 want_type
= Type::lookup_integer_type("uint64");
7868 if (want_type
== NULL
)
7869 want_type
= Type::lookup_integer_type("int64");
7871 else if (atype
->float_type() != NULL
)
7872 want_type
= Type::lookup_float_type("float64");
7873 else if (atype
->complex_type() != NULL
)
7874 want_type
= Type::lookup_complex_type("complex128");
7875 else if (atype
->is_abstract_string_type())
7876 want_type
= Type::lookup_string_type();
7877 else if (atype
->is_abstract_boolean_type())
7878 want_type
= Type::lookup_bool_type();
7881 subcontext
.type
= want_type
;
7885 (*pa
)->determine_type(&subcontext
);
7890 // If there is exactly one argument, return true. Otherwise give an
7891 // error message and return false.
7894 Builtin_call_expression::check_one_arg()
7896 const Expression_list
* args
= this->args();
7897 if (args
== NULL
|| args
->size() < 1)
7899 this->report_error(_("not enough arguments"));
7902 else if (args
->size() > 1)
7904 this->report_error(_("too many arguments"));
7907 if (args
->front()->is_error_expression()
7908 || args
->front()->type()->is_error())
7910 this->set_is_error();
7916 // Check argument types for a builtin function.
7919 Builtin_call_expression::do_check_types(Gogo
*)
7921 if (this->is_error_expression())
7923 switch (this->code_
)
7925 case BUILTIN_INVALID
:
7928 case BUILTIN_DELETE
:
7934 // The single argument may be either a string or an array or a
7935 // map or a channel, or a pointer to a closed array.
7936 if (this->check_one_arg())
7938 Type
* arg_type
= this->one_arg()->type();
7939 if (arg_type
->points_to() != NULL
7940 && arg_type
->points_to()->array_type() != NULL
7941 && !arg_type
->points_to()->is_slice_type())
7942 arg_type
= arg_type
->points_to();
7943 if (this->code_
== BUILTIN_CAP
)
7945 if (!arg_type
->is_error()
7946 && arg_type
->array_type() == NULL
7947 && arg_type
->channel_type() == NULL
)
7948 this->report_error(_("argument must be array or slice "
7953 if (!arg_type
->is_error()
7954 && !arg_type
->is_string_type()
7955 && arg_type
->array_type() == NULL
7956 && arg_type
->map_type() == NULL
7957 && arg_type
->channel_type() == NULL
)
7958 this->report_error(_("argument must be string or "
7959 "array or slice or map or channel"));
7966 case BUILTIN_PRINTLN
:
7968 const Expression_list
* args
= this->args();
7971 if (this->code_
== BUILTIN_PRINT
)
7972 warning_at(this->location(), 0,
7973 "no arguments for builtin function %<%s%>",
7974 (this->code_
== BUILTIN_PRINT
7980 for (Expression_list::const_iterator p
= args
->begin();
7984 Type
* type
= (*p
)->type();
7985 if (type
->is_error()
7986 || type
->is_string_type()
7987 || type
->integer_type() != NULL
7988 || type
->float_type() != NULL
7989 || type
->complex_type() != NULL
7990 || type
->is_boolean_type()
7991 || type
->points_to() != NULL
7992 || type
->interface_type() != NULL
7993 || type
->channel_type() != NULL
7994 || type
->map_type() != NULL
7995 || type
->function_type() != NULL
7996 || type
->is_slice_type())
7998 else if ((*p
)->is_type_expression())
8000 // If this is a type expression it's going to give
8001 // an error anyhow, so we don't need one here.
8004 this->report_error(_("unsupported argument type to "
8005 "builtin function"));
8012 if (this->check_one_arg())
8014 if (this->one_arg()->type()->channel_type() == NULL
)
8015 this->report_error(_("argument must be channel"));
8016 else if (!this->one_arg()->type()->channel_type()->may_send())
8017 this->report_error(_("cannot close receive-only channel"));
8022 case BUILTIN_SIZEOF
:
8023 case BUILTIN_ALIGNOF
:
8024 this->check_one_arg();
8027 case BUILTIN_RECOVER
:
8028 if (this->args() != NULL
8029 && !this->args()->empty()
8030 && !this->recover_arg_is_set_
)
8031 this->report_error(_("too many arguments"));
8034 case BUILTIN_OFFSETOF
:
8035 if (this->check_one_arg())
8037 Expression
* arg
= this->one_arg();
8038 if (arg
->field_reference_expression() == NULL
)
8039 this->report_error(_("argument must be a field reference"));
8045 const Expression_list
* args
= this->args();
8046 if (args
== NULL
|| args
->size() < 2)
8048 this->report_error(_("not enough arguments"));
8051 else if (args
->size() > 2)
8053 this->report_error(_("too many arguments"));
8056 Type
* arg1_type
= args
->front()->type();
8057 Type
* arg2_type
= args
->back()->type();
8058 if (arg1_type
->is_error() || arg2_type
->is_error())
8060 this->set_is_error();
8065 if (arg1_type
->is_slice_type())
8066 e1
= arg1_type
->array_type()->element_type();
8069 this->report_error(_("left argument must be a slice"));
8073 if (arg2_type
->is_slice_type())
8075 Type
* e2
= arg2_type
->array_type()->element_type();
8076 if (!Type::are_identical(e1
, e2
, true, NULL
))
8077 this->report_error(_("element types must be the same"));
8079 else if (arg2_type
->is_string_type())
8081 if (e1
->integer_type() == NULL
|| !e1
->integer_type()->is_byte())
8082 this->report_error(_("first argument must be []byte"));
8085 this->report_error(_("second argument must be slice or string"));
8089 case BUILTIN_APPEND
:
8091 const Expression_list
* args
= this->args();
8092 if (args
== NULL
|| args
->size() < 2)
8094 this->report_error(_("not enough arguments"));
8097 if (args
->size() > 2)
8099 this->report_error(_("too many arguments"));
8102 if (args
->front()->type()->is_error()
8103 || args
->back()->type()->is_error())
8105 this->set_is_error();
8109 Array_type
* at
= args
->front()->type()->array_type();
8110 Type
* e
= at
->element_type();
8112 // The language permits appending a string to a []byte, as a
8114 if (args
->back()->type()->is_string_type())
8116 if (e
->integer_type() != NULL
&& e
->integer_type()->is_byte())
8120 // The language says that the second argument must be
8121 // assignable to a slice of the element type of the first
8122 // argument. We already know the first argument is a slice
8124 Type
* arg2_type
= Type::make_array_type(e
, NULL
);
8126 if (!Type::are_assignable(arg2_type
, args
->back()->type(), &reason
))
8129 this->report_error(_("argument 2 has invalid type"));
8132 error_at(this->location(), "argument 2 has invalid type (%s)",
8134 this->set_is_error();
8142 if (this->check_one_arg())
8144 if (this->one_arg()->type()->complex_type() == NULL
)
8145 this->report_error(_("argument must have complex type"));
8149 case BUILTIN_COMPLEX
:
8151 const Expression_list
* args
= this->args();
8152 if (args
== NULL
|| args
->size() < 2)
8153 this->report_error(_("not enough arguments"));
8154 else if (args
->size() > 2)
8155 this->report_error(_("too many arguments"));
8156 else if (args
->front()->is_error_expression()
8157 || args
->front()->type()->is_error()
8158 || args
->back()->is_error_expression()
8159 || args
->back()->type()->is_error())
8160 this->set_is_error();
8161 else if (!Type::are_identical(args
->front()->type(),
8162 args
->back()->type(), true, NULL
))
8163 this->report_error(_("complex arguments must have identical types"));
8164 else if (args
->front()->type()->float_type() == NULL
)
8165 this->report_error(_("complex arguments must have "
8166 "floating-point type"));
8176 Builtin_call_expression::do_copy()
8178 Call_expression
* bce
=
8179 new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
8180 (this->args() == NULL
8182 : this->args()->copy()),
8186 if (this->varargs_are_lowered())
8187 bce
->set_varargs_are_lowered();
8191 // Return the backend representation for a builtin function.
8194 Builtin_call_expression::do_get_backend(Translate_context
* context
)
8196 Gogo
* gogo
= context
->gogo();
8197 Location location
= this->location();
8199 if (this->is_erroneous_call())
8201 go_assert(saw_errors());
8202 return gogo
->backend()->error_expression();
8205 switch (this->code_
)
8207 case BUILTIN_INVALID
:
8215 const Expression_list
* args
= this->args();
8216 go_assert(args
!= NULL
&& args
->size() == 1);
8217 Expression
* arg
= args
->front();
8218 Type
* arg_type
= arg
->type();
8222 go_assert(saw_errors());
8223 return context
->backend()->error_expression();
8226 this->seen_
= false;
8227 if (arg_type
->points_to() != NULL
)
8229 arg_type
= arg_type
->points_to();
8230 go_assert(arg_type
->array_type() != NULL
8231 && !arg_type
->is_slice_type());
8232 arg
= Expression::make_unary(OPERATOR_MULT
, arg
, location
);
8235 Type
* int_type
= Type::lookup_integer_type("int");
8237 if (this->code_
== BUILTIN_LEN
)
8239 if (arg_type
->is_string_type())
8240 val
= Expression::make_string_info(arg
, STRING_INFO_LENGTH
,
8242 else if (arg_type
->array_type() != NULL
)
8246 go_assert(saw_errors());
8247 return context
->backend()->error_expression();
8250 val
= arg_type
->array_type()->get_length(gogo
, arg
);
8251 this->seen_
= false;
8253 else if (arg_type
->map_type() != NULL
)
8254 val
= Runtime::make_call(Runtime::MAP_LEN
, location
, 1, arg
);
8255 else if (arg_type
->channel_type() != NULL
)
8256 val
= Runtime::make_call(Runtime::CHAN_LEN
, location
, 1, arg
);
8262 if (arg_type
->array_type() != NULL
)
8266 go_assert(saw_errors());
8267 return context
->backend()->error_expression();
8270 val
= arg_type
->array_type()->get_capacity(gogo
, arg
);
8271 this->seen_
= false;
8273 else if (arg_type
->channel_type() != NULL
)
8274 val
= Runtime::make_call(Runtime::CHAN_CAP
, location
, 1, arg
);
8279 return Expression::make_cast(int_type
, val
,
8280 location
)->get_backend(context
);
8284 case BUILTIN_PRINTLN
:
8286 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
8287 Expression
* print_stmts
= NULL
;
8289 const Expression_list
* call_args
= this->args();
8290 if (call_args
!= NULL
)
8292 for (Expression_list::const_iterator p
= call_args
->begin();
8293 p
!= call_args
->end();
8296 if (is_ln
&& p
!= call_args
->begin())
8298 Expression
* print_space
=
8299 Runtime::make_call(Runtime::PRINT_SPACE
,
8300 this->location(), 0);
8303 Expression::make_compound(print_stmts
, print_space
,
8307 Expression
* arg
= *p
;
8308 Type
* type
= arg
->type();
8309 Runtime::Function code
;
8310 if (type
->is_string_type())
8311 code
= Runtime::PRINT_STRING
;
8312 else if (type
->integer_type() != NULL
8313 && type
->integer_type()->is_unsigned())
8315 Type
* itype
= Type::lookup_integer_type("uint64");
8316 arg
= Expression::make_cast(itype
, arg
, location
);
8317 code
= Runtime::PRINT_UINT64
;
8319 else if (type
->integer_type() != NULL
)
8321 Type
* itype
= Type::lookup_integer_type("int64");
8322 arg
= Expression::make_cast(itype
, arg
, location
);
8323 code
= Runtime::PRINT_INT64
;
8325 else if (type
->float_type() != NULL
)
8327 Type
* dtype
= Type::lookup_float_type("float64");
8328 arg
= Expression::make_cast(dtype
, arg
, location
);
8329 code
= Runtime::PRINT_DOUBLE
;
8331 else if (type
->complex_type() != NULL
)
8333 Type
* ctype
= Type::lookup_complex_type("complex128");
8334 arg
= Expression::make_cast(ctype
, arg
, location
);
8335 code
= Runtime::PRINT_COMPLEX
;
8337 else if (type
->is_boolean_type())
8338 code
= Runtime::PRINT_BOOL
;
8339 else if (type
->points_to() != NULL
8340 || type
->channel_type() != NULL
8341 || type
->map_type() != NULL
8342 || type
->function_type() != NULL
)
8344 arg
= Expression::make_cast(type
, arg
, location
);
8345 code
= Runtime::PRINT_POINTER
;
8347 else if (type
->interface_type() != NULL
)
8349 if (type
->interface_type()->is_empty())
8350 code
= Runtime::PRINT_EMPTY_INTERFACE
;
8352 code
= Runtime::PRINT_INTERFACE
;
8354 else if (type
->is_slice_type())
8355 code
= Runtime::PRINT_SLICE
;
8358 go_assert(saw_errors());
8359 return context
->backend()->error_expression();
8362 Expression
* call
= Runtime::make_call(code
, location
, 1, arg
);
8363 if (print_stmts
== NULL
)
8366 print_stmts
= Expression::make_compound(print_stmts
, call
,
8373 Expression
* print_nl
=
8374 Runtime::make_call(Runtime::PRINT_NL
, location
, 0);
8375 if (print_stmts
== NULL
)
8376 print_stmts
= print_nl
;
8378 print_stmts
= Expression::make_compound(print_stmts
, print_nl
,
8382 // There aren't any arguments to the print builtin. The compiler
8383 // issues a warning for this so we should avoid getting the backend
8384 // representation for this call. Instead, perform a no-op.
8385 if (print_stmts
== NULL
)
8386 return context
->backend()->boolean_constant_expression(false);
8388 return print_stmts
->get_backend(context
);
8393 const Expression_list
* args
= this->args();
8394 go_assert(args
!= NULL
&& args
->size() == 1);
8395 Expression
* arg
= args
->front();
8397 Type::make_empty_interface_type(Linemap::predeclared_location());
8398 arg
= Expression::convert_for_assignment(gogo
, empty
, arg
, location
);
8401 Runtime::make_call(Runtime::PANIC
, location
, 1, arg
);
8402 return panic
->get_backend(context
);
8405 case BUILTIN_RECOVER
:
8407 // The argument is set when building recover thunks. It's a
8408 // boolean value which is true if we can recover a value now.
8409 const Expression_list
* args
= this->args();
8410 go_assert(args
!= NULL
&& args
->size() == 1);
8411 Expression
* arg
= args
->front();
8413 Type::make_empty_interface_type(Linemap::predeclared_location());
8415 Expression
* nil
= Expression::make_nil(location
);
8416 nil
= Expression::convert_for_assignment(gogo
, empty
, nil
, location
);
8418 // We need to handle a deferred call to recover specially,
8419 // because it changes whether it can recover a panic or not.
8420 // See test7 in test/recover1.go.
8421 Expression
* recover
= Runtime::make_call((this->is_deferred()
8422 ? Runtime::DEFERRED_RECOVER
8423 : Runtime::RECOVER
),
8426 Expression::make_conditional(arg
, recover
, nil
, location
);
8427 return cond
->get_backend(context
);
8432 const Expression_list
* args
= this->args();
8433 go_assert(args
!= NULL
&& args
->size() == 1);
8434 Expression
* arg
= args
->front();
8435 Expression
* close
= Runtime::make_call(Runtime::CLOSE
, location
,
8437 return close
->get_backend(context
);
8440 case BUILTIN_SIZEOF
:
8441 case BUILTIN_OFFSETOF
:
8442 case BUILTIN_ALIGNOF
:
8444 Numeric_constant nc
;
8446 if (!this->numeric_constant_value(&nc
)
8447 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
8449 go_assert(saw_errors());
8450 return context
->backend()->error_expression();
8452 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8455 Expression
* int_cst
=
8456 Expression::make_integer_z(&ival
, uintptr_type
, location
);
8458 return int_cst
->get_backend(context
);
8463 const Expression_list
* args
= this->args();
8464 go_assert(args
!= NULL
&& args
->size() == 2);
8465 Expression
* arg1
= args
->front();
8466 Expression
* arg2
= args
->back();
8468 Type
* arg1_type
= arg1
->type();
8469 Array_type
* at
= arg1_type
->array_type();
8470 go_assert(arg1
->is_variable());
8471 Expression
* arg1_val
= at
->get_value_pointer(gogo
, arg1
);
8472 Expression
* arg1_len
= at
->get_length(gogo
, arg1
);
8474 Type
* arg2_type
= arg2
->type();
8475 go_assert(arg2
->is_variable());
8476 Expression
* arg2_val
;
8477 Expression
* arg2_len
;
8478 if (arg2_type
->is_slice_type())
8480 at
= arg2_type
->array_type();
8481 arg2_val
= at
->get_value_pointer(gogo
, arg2
);
8482 arg2_len
= at
->get_length(gogo
, arg2
);
8486 go_assert(arg2
->is_variable());
8487 arg2_val
= Expression::make_string_info(arg2
, STRING_INFO_DATA
,
8489 arg2_len
= Expression::make_string_info(arg2
, STRING_INFO_LENGTH
,
8493 Expression::make_binary(OPERATOR_LT
, arg1_len
, arg2_len
, location
);
8494 Expression
* length
=
8495 Expression::make_conditional(cond
, arg1_len
, arg2_len
, location
);
8497 Type
* element_type
= at
->element_type();
8498 int64_t element_size
;
8499 bool ok
= element_type
->backend_type_size(gogo
, &element_size
);
8502 go_assert(saw_errors());
8503 return gogo
->backend()->error_expression();
8506 Expression
* size_expr
= Expression::make_integer_int64(element_size
,
8509 Expression
* bytecount
=
8510 Expression::make_binary(OPERATOR_MULT
, size_expr
, length
, location
);
8511 Expression
* copy
= Runtime::make_call(Runtime::COPY
, location
, 3,
8512 arg1_val
, arg2_val
, bytecount
);
8514 Expression
* compound
= Expression::make_compound(copy
, length
, location
);
8515 return compound
->get_backend(context
);
8518 case BUILTIN_APPEND
:
8520 const Expression_list
* args
= this->args();
8521 go_assert(args
!= NULL
&& args
->size() == 2);
8522 Expression
* arg1
= args
->front();
8523 Expression
* arg2
= args
->back();
8525 Array_type
* at
= arg1
->type()->array_type();
8526 Type
* element_type
= at
->element_type()->forwarded();
8528 go_assert(arg2
->is_variable());
8529 Expression
* arg2_val
;
8530 Expression
* arg2_len
;
8532 if (arg2
->type()->is_string_type()
8533 && element_type
->integer_type() != NULL
8534 && element_type
->integer_type()->is_byte())
8536 arg2_val
= Expression::make_string_info(arg2
, STRING_INFO_DATA
,
8538 arg2_len
= Expression::make_string_info(arg2
, STRING_INFO_LENGTH
,
8544 arg2_val
= at
->get_value_pointer(gogo
, arg2
);
8545 arg2_len
= at
->get_length(gogo
, arg2
);
8546 bool ok
= element_type
->backend_type_size(gogo
, &size
);
8549 go_assert(saw_errors());
8550 return gogo
->backend()->error_expression();
8553 Expression
* element_size
=
8554 Expression::make_integer_int64(size
, NULL
, location
);
8556 Expression
* append
= Runtime::make_call(Runtime::APPEND
, location
, 4,
8557 arg1
, arg2_val
, arg2_len
,
8559 append
= Expression::make_unsafe_cast(arg1
->type(), append
, location
);
8560 return append
->get_backend(context
);
8566 const Expression_list
* args
= this->args();
8567 go_assert(args
!= NULL
&& args
->size() == 1);
8570 Bexpression
* bcomplex
= args
->front()->get_backend(context
);
8571 if (this->code_
== BUILTIN_REAL
)
8572 ret
= gogo
->backend()->real_part_expression(bcomplex
, location
);
8574 ret
= gogo
->backend()->imag_part_expression(bcomplex
, location
);
8578 case BUILTIN_COMPLEX
:
8580 const Expression_list
* args
= this->args();
8581 go_assert(args
!= NULL
&& args
->size() == 2);
8582 Bexpression
* breal
= args
->front()->get_backend(context
);
8583 Bexpression
* bimag
= args
->back()->get_backend(context
);
8584 return gogo
->backend()->complex_expression(breal
, bimag
, location
);
8592 // We have to support exporting a builtin call expression, because
8593 // code can set a constant to the result of a builtin expression.
8596 Builtin_call_expression::do_export(Export
* exp
) const
8598 Numeric_constant nc
;
8599 if (!this->numeric_constant_value(&nc
))
8601 error_at(this->location(), "value is not constant");
8609 Integer_expression::export_integer(exp
, val
);
8612 else if (nc
.is_float())
8615 nc
.get_float(&fval
);
8616 Float_expression::export_float(exp
, fval
);
8619 else if (nc
.is_complex())
8622 nc
.get_complex(&cval
);
8623 Complex_expression::export_complex(exp
, cval
);
8629 // A trailing space lets us reliably identify the end of the number.
8630 exp
->write_c_string(" ");
8633 // Class Call_expression.
8635 // A Go function can be viewed in a couple of different ways. The
8636 // code of a Go function becomes a backend function with parameters
8637 // whose types are simply the backend representation of the Go types.
8638 // If there are multiple results, they are returned as a backend
8641 // However, when Go code refers to a function other than simply
8642 // calling it, the backend type of that function is actually a struct.
8643 // The first field of the struct points to the Go function code
8644 // (sometimes a wrapper as described below). The remaining fields
8645 // hold addresses of closed-over variables. This struct is called a
8648 // There are a few cases to consider.
8650 // A direct function call of a known function in package scope. In
8651 // this case there are no closed-over variables, and we know the name
8652 // of the function code. We can simply produce a backend call to the
8653 // function directly, and not worry about the closure.
8655 // A direct function call of a known function literal. In this case
8656 // we know the function code and we know the closure. We generate the
8657 // function code such that it expects an additional final argument of
8658 // the closure type. We pass the closure as the last argument, after
8659 // the other arguments.
8661 // An indirect function call. In this case we have a closure. We
8662 // load the pointer to the function code from the first field of the
8663 // closure. We pass the address of the closure as the last argument.
8665 // A call to a method of an interface. Type methods are always at
8666 // package scope, so we call the function directly, and don't worry
8667 // about the closure.
8669 // This means that for a function at package scope we have two cases.
8670 // One is the direct call, which has no closure. The other is the
8671 // indirect call, which does have a closure. We can't simply ignore
8672 // the closure, even though it is the last argument, because that will
8673 // fail on targets where the function pops its arguments. So when
8674 // generating a closure for a package-scope function we set the
8675 // function code pointer in the closure to point to a wrapper
8676 // function. This wrapper function accepts a final argument that
8677 // points to the closure, ignores it, and calls the real function as a
8678 // direct function call. This wrapper will normally be efficient, and
8679 // can often simply be a tail call to the real function.
8681 // We don't use GCC's static chain pointer because 1) we don't need
8682 // it; 2) GCC only permits using a static chain to call a known
8683 // function, so we can't use it for an indirect call anyhow. Since we
8684 // can't use it for an indirect call, we may as well not worry about
8685 // using it for a direct call either.
8687 // We pass the closure last rather than first because it means that
8688 // the function wrapper we put into a closure for a package-scope
8689 // function can normally just be a tail call to the real function.
8691 // For method expressions we generate a wrapper that loads the
8692 // receiver from the closure and then calls the method. This
8693 // unfortunately forces reshuffling the arguments, since there is a
8694 // new first argument, but we can't avoid reshuffling either for
8695 // method expressions or for indirect calls of package-scope
8696 // functions, and since the latter are more common we reshuffle for
8697 // method expressions.
8699 // Note that the Go code retains the Go types. The extra final
8700 // argument only appears when we convert to the backend
8706 Call_expression::do_traverse(Traverse
* traverse
)
8708 // If we are calling a function in a different package that returns
8709 // an unnamed type, this may be the only chance we get to traverse
8710 // that type. We don't traverse this->type_ because it may be a
8711 // Call_multiple_result_type that will just lead back here.
8712 if (this->type_
!= NULL
&& !this->type_
->is_error_type())
8714 Function_type
*fntype
= this->get_function_type();
8715 if (fntype
!= NULL
&& Type::traverse(fntype
, traverse
) == TRAVERSE_EXIT
)
8716 return TRAVERSE_EXIT
;
8718 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8719 return TRAVERSE_EXIT
;
8720 if (this->args_
!= NULL
)
8722 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8723 return TRAVERSE_EXIT
;
8725 return TRAVERSE_CONTINUE
;
8728 // Lower a call statement.
8731 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
8732 Statement_inserter
* inserter
, int)
8734 Location loc
= this->location();
8736 // A type cast can look like a function call.
8737 if (this->fn_
->is_type_expression()
8738 && this->args_
!= NULL
8739 && this->args_
->size() == 1)
8740 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8743 // Because do_type will return an error type and thus prevent future
8744 // errors, check for that case now to ensure that the error gets
8746 Function_type
* fntype
= this->get_function_type();
8749 if (!this->fn_
->type()->is_error())
8750 this->report_error(_("expected function"));
8751 this->set_is_error();
8755 // Handle an argument which is a call to a function which returns
8756 // multiple results.
8757 if (this->args_
!= NULL
8758 && this->args_
->size() == 1
8759 && this->args_
->front()->call_expression() != NULL
)
8761 size_t rc
= this->args_
->front()->call_expression()->result_count();
8763 && ((fntype
->parameters() != NULL
8764 && (fntype
->parameters()->size() == rc
8765 || (fntype
->is_varargs()
8766 && fntype
->parameters()->size() - 1 <= rc
)))
8767 || fntype
->is_builtin()))
8769 Call_expression
* call
= this->args_
->front()->call_expression();
8770 call
->set_is_multi_value_arg();
8771 if (this->is_varargs_
)
8773 // It is not clear which result of a multiple result call
8774 // the ellipsis operator should be applied to. If we unpack the
8775 // the call into its individual results here, the ellipsis will be
8776 // applied to the last result.
8777 error_at(call
->location(),
8778 _("multiple-value argument in single-value context"));
8779 return Expression::make_error(call
->location());
8782 Expression_list
* args
= new Expression_list
;
8783 for (size_t i
= 0; i
< rc
; ++i
)
8784 args
->push_back(Expression::make_call_result(call
, i
));
8785 // We can't return a new call expression here, because this
8786 // one may be referenced by Call_result expressions. We
8787 // also can't delete the old arguments, because we may still
8788 // traverse them somewhere up the call stack. FIXME.
8793 // Recognize a call to a builtin function.
8794 if (fntype
->is_builtin())
8795 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8796 this->is_varargs_
, loc
);
8798 // If this call returns multiple results, create a temporary
8799 // variable for each result.
8800 size_t rc
= this->result_count();
8801 if (rc
> 1 && this->results_
== NULL
)
8803 std::vector
<Temporary_statement
*>* temps
=
8804 new std::vector
<Temporary_statement
*>;
8806 const Typed_identifier_list
* results
= fntype
->results();
8807 for (Typed_identifier_list::const_iterator p
= results
->begin();
8808 p
!= results
->end();
8811 Temporary_statement
* temp
= Statement::make_temporary(p
->type(),
8813 inserter
->insert(temp
);
8814 temps
->push_back(temp
);
8816 this->results_
= temps
;
8819 // Handle a call to a varargs function by packaging up the extra
8821 if (fntype
->is_varargs())
8823 const Typed_identifier_list
* parameters
= fntype
->parameters();
8824 go_assert(parameters
!= NULL
&& !parameters
->empty());
8825 Type
* varargs_type
= parameters
->back().type();
8826 this->lower_varargs(gogo
, function
, inserter
, varargs_type
,
8827 parameters
->size());
8830 // If this is call to a method, call the method directly passing the
8831 // object as the first parameter.
8832 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8835 Named_object
* methodfn
= bme
->function();
8836 Expression
* first_arg
= bme
->first_argument();
8838 // We always pass a pointer when calling a method.
8839 if (first_arg
->type()->points_to() == NULL
8840 && !first_arg
->type()->is_error())
8842 first_arg
= Expression::make_unary(OPERATOR_AND
, first_arg
, loc
);
8843 // We may need to create a temporary variable so that we can
8844 // take the address. We can't do that here because it will
8845 // mess up the order of evaluation.
8846 Unary_expression
* ue
= static_cast<Unary_expression
*>(first_arg
);
8847 ue
->set_create_temp();
8850 // If we are calling a method which was inherited from an
8851 // embedded struct, and the method did not get a stub, then the
8852 // first type may be wrong.
8853 Type
* fatype
= bme
->first_argument_type();
8856 if (fatype
->points_to() == NULL
)
8857 fatype
= Type::make_pointer_type(fatype
);
8858 first_arg
= Expression::make_unsafe_cast(fatype
, first_arg
, loc
);
8861 Expression_list
* new_args
= new Expression_list();
8862 new_args
->push_back(first_arg
);
8863 if (this->args_
!= NULL
)
8865 for (Expression_list::const_iterator p
= this->args_
->begin();
8866 p
!= this->args_
->end();
8868 new_args
->push_back(*p
);
8871 // We have to change in place because this structure may be
8872 // referenced by Call_result_expressions. We can't delete the
8873 // old arguments, because we may be traversing them up in some
8875 this->args_
= new_args
;
8876 this->fn_
= Expression::make_func_reference(methodfn
, NULL
,
8883 // Lower a call to a varargs function. FUNCTION is the function in
8884 // which the call occurs--it's not the function we are calling.
8885 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8886 // PARAM_COUNT is the number of parameters of the function we are
8887 // calling; the last of these parameters will be the varargs
8891 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8892 Statement_inserter
* inserter
,
8893 Type
* varargs_type
, size_t param_count
)
8895 if (this->varargs_are_lowered_
)
8898 Location loc
= this->location();
8900 go_assert(param_count
> 0);
8901 go_assert(varargs_type
->is_slice_type());
8903 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8904 if (arg_count
< param_count
- 1)
8906 // Not enough arguments; will be caught in check_types.
8910 Expression_list
* old_args
= this->args_
;
8911 Expression_list
* new_args
= new Expression_list();
8912 bool push_empty_arg
= false;
8913 if (old_args
== NULL
|| old_args
->empty())
8915 go_assert(param_count
== 1);
8916 push_empty_arg
= true;
8920 Expression_list::const_iterator pa
;
8922 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8924 if (static_cast<size_t>(i
) == param_count
)
8926 new_args
->push_back(*pa
);
8929 // We have reached the varargs parameter.
8931 bool issued_error
= false;
8932 if (pa
== old_args
->end())
8933 push_empty_arg
= true;
8934 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8935 new_args
->push_back(*pa
);
8936 else if (this->is_varargs_
)
8938 if ((*pa
)->type()->is_slice_type())
8939 this->report_error(_("too many arguments"));
8942 error_at(this->location(),
8943 _("invalid use of %<...%> with non-slice"));
8944 this->set_is_error();
8950 Type
* element_type
= varargs_type
->array_type()->element_type();
8951 Expression_list
* vals
= new Expression_list
;
8952 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8954 // Check types here so that we get a better message.
8955 Type
* patype
= (*pa
)->type();
8956 Location paloc
= (*pa
)->location();
8957 if (!this->check_argument_type(i
, element_type
, patype
,
8958 paloc
, issued_error
))
8960 vals
->push_back(*pa
);
8963 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8964 gogo
->lower_expression(function
, inserter
, &val
);
8965 new_args
->push_back(val
);
8970 new_args
->push_back(Expression::make_nil(loc
));
8972 // We can't return a new call expression here, because this one may
8973 // be referenced by Call_result expressions. FIXME. We can't
8974 // delete OLD_ARGS because we may have both a Call_expression and a
8975 // Builtin_call_expression which refer to them. FIXME.
8976 this->args_
= new_args
;
8977 this->varargs_are_lowered_
= true;
8980 // Flatten a call with multiple results into a temporary.
8983 Call_expression::do_flatten(Gogo
* gogo
, Named_object
*,
8984 Statement_inserter
* inserter
)
8986 if (this->is_erroneous_call())
8988 go_assert(saw_errors());
8989 return Expression::make_error(this->location());
8992 if (this->is_flattened_
)
8994 this->is_flattened_
= true;
8996 // Add temporary variables for all arguments that require type
8998 Function_type
* fntype
= this->get_function_type();
9001 go_assert(saw_errors());
9004 if (this->args_
!= NULL
&& !this->args_
->empty()
9005 && fntype
->parameters() != NULL
&& !fntype
->parameters()->empty())
9007 bool is_interface_method
=
9008 this->fn_
->interface_field_reference_expression() != NULL
;
9010 Expression_list
*args
= new Expression_list();
9011 Typed_identifier_list::const_iterator pp
= fntype
->parameters()->begin();
9012 Expression_list::const_iterator pa
= this->args_
->begin();
9013 if (!is_interface_method
&& fntype
->is_method())
9015 // The receiver argument.
9016 args
->push_back(*pa
);
9019 for (; pa
!= this->args_
->end(); ++pa
, ++pp
)
9021 go_assert(pp
!= fntype
->parameters()->end());
9022 if (Type::are_identical(pp
->type(), (*pa
)->type(), true, NULL
))
9023 args
->push_back(*pa
);
9026 Location loc
= (*pa
)->location();
9027 Expression
* arg
= *pa
;
9028 if (!arg
->is_variable())
9030 Temporary_statement
*temp
=
9031 Statement::make_temporary(NULL
, arg
, loc
);
9032 inserter
->insert(temp
);
9033 arg
= Expression::make_temporary_reference(temp
, loc
);
9035 arg
= Expression::convert_for_assignment(gogo
, pp
->type(), arg
,
9037 args
->push_back(arg
);
9044 size_t rc
= this->result_count();
9045 if (rc
> 1 && this->call_temp_
== NULL
)
9047 Struct_field_list
* sfl
= new Struct_field_list();
9048 Function_type
* fntype
= this->get_function_type();
9049 const Typed_identifier_list
* results
= fntype
->results();
9050 Location loc
= this->location();
9053 /* Buffer large enough for INT_MAX plus the prefix. */
9055 for (Typed_identifier_list::const_iterator p
= results
->begin();
9056 p
!= results
->end();
9059 snprintf(buf
, sizeof buf
, "res%d", i
);
9060 sfl
->push_back(Struct_field(Typed_identifier(buf
, p
->type(), loc
)));
9063 Struct_type
* st
= Type::make_struct_type(sfl
, loc
);
9064 this->call_temp_
= Statement::make_temporary(st
, NULL
, loc
);
9065 inserter
->insert(this->call_temp_
);
9071 // Get the function type. This can return NULL in error cases.
9074 Call_expression::get_function_type() const
9076 return this->fn_
->type()->function_type();
9079 // Return the number of values which this call will return.
9082 Call_expression::result_count() const
9084 const Function_type
* fntype
= this->get_function_type();
9087 if (fntype
->results() == NULL
)
9089 return fntype
->results()->size();
9092 // Return the temporary which holds a result.
9094 Temporary_statement
*
9095 Call_expression::result(size_t i
) const
9097 if (this->results_
== NULL
|| this->results_
->size() <= i
)
9099 go_assert(saw_errors());
9102 return (*this->results_
)[i
];
9105 // Set the number of results expected from a call expression.
9108 Call_expression::set_expected_result_count(size_t count
)
9110 go_assert(this->expected_result_count_
== 0);
9111 this->expected_result_count_
= count
;
9114 // Return whether this is a call to the predeclared function recover.
9117 Call_expression::is_recover_call() const
9119 return this->do_is_recover_call();
9122 // Set the argument to the recover function.
9125 Call_expression::set_recover_arg(Expression
* arg
)
9127 this->do_set_recover_arg(arg
);
9130 // Virtual functions also implemented by Builtin_call_expression.
9133 Call_expression::do_is_recover_call() const
9139 Call_expression::do_set_recover_arg(Expression
*)
9144 // We have found an error with this call expression; return true if
9145 // we should report it.
9148 Call_expression::issue_error()
9150 if (this->issued_error_
)
9154 this->issued_error_
= true;
9159 // Whether or not this call contains errors, either in the call or the
9160 // arguments to the call.
9163 Call_expression::is_erroneous_call()
9165 if (this->is_error_expression() || this->fn()->is_error_expression())
9168 if (this->args() == NULL
)
9170 for (Expression_list::iterator pa
= this->args()->begin();
9171 pa
!= this->args()->end();
9174 if ((*pa
)->type()->is_error_type() || (*pa
)->is_error_expression())
9183 Call_expression::do_type()
9185 if (this->type_
!= NULL
)
9189 Function_type
* fntype
= this->get_function_type();
9191 return Type::make_error_type();
9193 const Typed_identifier_list
* results
= fntype
->results();
9194 if (results
== NULL
)
9195 ret
= Type::make_void_type();
9196 else if (results
->size() == 1)
9197 ret
= results
->begin()->type();
9199 ret
= Type::make_call_multiple_result_type(this);
9206 // Determine types for a call expression. We can use the function
9207 // parameter types to set the types of the arguments.
9210 Call_expression::do_determine_type(const Type_context
*)
9212 if (!this->determining_types())
9215 this->fn_
->determine_type_no_context();
9216 Function_type
* fntype
= this->get_function_type();
9217 const Typed_identifier_list
* parameters
= NULL
;
9219 parameters
= fntype
->parameters();
9220 if (this->args_
!= NULL
)
9222 Typed_identifier_list::const_iterator pt
;
9223 if (parameters
!= NULL
)
9224 pt
= parameters
->begin();
9226 for (Expression_list::const_iterator pa
= this->args_
->begin();
9227 pa
!= this->args_
->end();
9233 // If this is a method, the first argument is the
9235 if (fntype
!= NULL
&& fntype
->is_method())
9237 Type
* rtype
= fntype
->receiver()->type();
9238 // The receiver is always passed as a pointer.
9239 if (rtype
->points_to() == NULL
)
9240 rtype
= Type::make_pointer_type(rtype
);
9241 Type_context
subcontext(rtype
, false);
9242 (*pa
)->determine_type(&subcontext
);
9247 if (parameters
!= NULL
&& pt
!= parameters
->end())
9249 Type_context
subcontext(pt
->type(), false);
9250 (*pa
)->determine_type(&subcontext
);
9254 (*pa
)->determine_type_no_context();
9259 // Called when determining types for a Call_expression. Return true
9260 // if we should go ahead, false if they have already been determined.
9263 Call_expression::determining_types()
9265 if (this->types_are_determined_
)
9269 this->types_are_determined_
= true;
9274 // Check types for parameter I.
9277 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
9278 const Type
* argument_type
,
9279 Location argument_location
,
9283 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
9288 error_at(argument_location
, "argument %d has incompatible type", i
);
9290 error_at(argument_location
,
9291 "argument %d has incompatible type (%s)",
9294 this->set_is_error();
9303 Call_expression::do_check_types(Gogo
*)
9305 if (this->classification() == EXPRESSION_ERROR
)
9308 Function_type
* fntype
= this->get_function_type();
9311 if (!this->fn_
->type()->is_error())
9312 this->report_error(_("expected function"));
9316 if (this->expected_result_count_
!= 0
9317 && this->expected_result_count_
!= this->result_count())
9319 if (this->issue_error())
9320 this->report_error(_("function result count mismatch"));
9321 this->set_is_error();
9325 bool is_method
= fntype
->is_method();
9328 go_assert(this->args_
!= NULL
&& !this->args_
->empty());
9329 Type
* rtype
= fntype
->receiver()->type();
9330 Expression
* first_arg
= this->args_
->front();
9331 // We dereference the values since receivers are always passed
9334 if (!Type::are_assignable(rtype
->deref(), first_arg
->type()->deref(),
9338 this->report_error(_("incompatible type for receiver"));
9341 error_at(this->location(),
9342 "incompatible type for receiver (%s)",
9344 this->set_is_error();
9349 // Note that varargs was handled by the lower_varargs() method, so
9350 // we don't have to worry about it here unless something is wrong.
9351 if (this->is_varargs_
&& !this->varargs_are_lowered_
)
9353 if (!fntype
->is_varargs())
9355 error_at(this->location(),
9356 _("invalid use of %<...%> calling non-variadic function"));
9357 this->set_is_error();
9362 const Typed_identifier_list
* parameters
= fntype
->parameters();
9363 if (this->args_
== NULL
)
9365 if (parameters
!= NULL
&& !parameters
->empty())
9366 this->report_error(_("not enough arguments"));
9368 else if (parameters
== NULL
)
9370 if (!is_method
|| this->args_
->size() > 1)
9371 this->report_error(_("too many arguments"));
9373 else if (this->args_
->size() == 1
9374 && this->args_
->front()->call_expression() != NULL
9375 && this->args_
->front()->call_expression()->result_count() > 1)
9377 // This is F(G()) when G returns more than one result. If the
9378 // results can be matched to parameters, it would have been
9379 // lowered in do_lower. If we get here we know there is a
9381 if (this->args_
->front()->call_expression()->result_count()
9382 < parameters
->size())
9383 this->report_error(_("not enough arguments"));
9385 this->report_error(_("too many arguments"));
9390 Expression_list::const_iterator pa
= this->args_
->begin();
9393 for (Typed_identifier_list::const_iterator pt
= parameters
->begin();
9394 pt
!= parameters
->end();
9397 if (pa
== this->args_
->end())
9399 this->report_error(_("not enough arguments"));
9402 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
9403 (*pa
)->location(), false);
9405 if (pa
!= this->args_
->end())
9406 this->report_error(_("too many arguments"));
9411 Call_expression::do_copy()
9413 Call_expression
* call
=
9414 Expression::make_call(this->fn_
->copy(),
9415 (this->args_
== NULL
9417 : this->args_
->copy()),
9418 this->is_varargs_
, this->location());
9420 if (this->varargs_are_lowered_
)
9421 call
->set_varargs_are_lowered();
9425 // Return whether we have to use a temporary variable to ensure that
9426 // we evaluate this call expression in order. If the call returns no
9427 // results then it will inevitably be executed last.
9430 Call_expression::do_must_eval_in_order() const
9432 return this->result_count() > 0;
9435 // Get the function and the first argument to use when calling an
9436 // interface method.
9439 Call_expression::interface_method_function(
9440 Interface_field_reference_expression
* interface_method
,
9441 Expression
** first_arg_ptr
)
9443 *first_arg_ptr
= interface_method
->get_underlying_object();
9444 return interface_method
->get_function();
9447 // Build the call expression.
9450 Call_expression::do_get_backend(Translate_context
* context
)
9452 if (this->call_
!= NULL
)
9455 Function_type
* fntype
= this->get_function_type();
9457 return context
->backend()->error_expression();
9459 if (this->fn_
->is_error_expression())
9460 return context
->backend()->error_expression();
9462 Gogo
* gogo
= context
->gogo();
9463 Location location
= this->location();
9465 Func_expression
* func
= this->fn_
->func_expression();
9466 Interface_field_reference_expression
* interface_method
=
9467 this->fn_
->interface_field_reference_expression();
9468 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
9469 const bool is_interface_method
= interface_method
!= NULL
;
9471 bool has_closure_arg
;
9473 has_closure_arg
= true;
9474 else if (func
!= NULL
)
9475 has_closure_arg
= false;
9476 else if (is_interface_method
)
9477 has_closure_arg
= false;
9479 has_closure_arg
= true;
9482 std::vector
<Bexpression
*> fn_args
;
9483 if (this->args_
== NULL
|| this->args_
->empty())
9485 nargs
= is_interface_method
? 1 : 0;
9489 else if (fntype
->parameters() == NULL
|| fntype
->parameters()->empty())
9491 // Passing a receiver parameter.
9492 go_assert(!is_interface_method
9493 && fntype
->is_method()
9494 && this->args_
->size() == 1);
9497 fn_args
[0] = this->args_
->front()->get_backend(context
);
9501 const Typed_identifier_list
* params
= fntype
->parameters();
9503 nargs
= this->args_
->size();
9504 int i
= is_interface_method
? 1 : 0;
9506 fn_args
.resize(nargs
);
9508 Typed_identifier_list::const_iterator pp
= params
->begin();
9509 Expression_list::const_iterator pe
= this->args_
->begin();
9510 if (!is_interface_method
&& fntype
->is_method())
9512 fn_args
[i
] = (*pe
)->get_backend(context
);
9516 for (; pe
!= this->args_
->end(); ++pe
, ++pp
, ++i
)
9518 go_assert(pp
!= params
->end());
9520 Expression::convert_for_assignment(gogo
, pp
->type(), *pe
,
9522 fn_args
[i
] = arg
->get_backend(context
);
9524 go_assert(pp
== params
->end());
9525 go_assert(i
== nargs
);
9529 Expression
* closure
= NULL
;
9532 Named_object
* no
= func
->named_object();
9533 fn
= Expression::make_func_code_reference(no
, location
);
9535 closure
= func
->closure();
9537 else if (!is_interface_method
)
9539 closure
= this->fn_
;
9541 // The backend representation of this function type is a pointer
9542 // to a struct whose first field is the actual function to call.
9544 Type::make_pointer_type(
9545 Type::make_pointer_type(Type::make_void_type()));
9546 fn
= Expression::make_unsafe_cast(pfntype
, this->fn_
, location
);
9547 fn
= Expression::make_unary(OPERATOR_MULT
, fn
, location
);
9551 Expression
* first_arg
;
9552 fn
= this->interface_method_function(interface_method
, &first_arg
);
9553 fn_args
[0] = first_arg
->get_backend(context
);
9556 Bexpression
* bclosure
= NULL
;
9557 if (has_closure_arg
)
9558 bclosure
= closure
->get_backend(context
);
9560 go_assert(closure
== NULL
);
9562 Bexpression
* bfn
= fn
->get_backend(context
);
9564 // When not calling a named function directly, use a type conversion
9565 // in case the type of the function is a recursive type which refers
9566 // to itself. We don't do this for an interface method because 1)
9567 // an interface method never refers to itself, so we always have a
9568 // function type here; 2) we pass an extra first argument to an
9569 // interface method, so fntype is not correct.
9570 if (func
== NULL
&& !is_interface_method
)
9572 Btype
* bft
= fntype
->get_backend_fntype(gogo
);
9573 bfn
= gogo
->backend()->convert_expression(bft
, bfn
, location
);
9576 Bexpression
* call
= gogo
->backend()->call_expression(bfn
, fn_args
,
9577 bclosure
, location
);
9579 if (this->results_
!= NULL
)
9581 go_assert(this->call_temp_
!= NULL
);
9582 Expression
* call_ref
=
9583 Expression::make_temporary_reference(this->call_temp_
, location
);
9584 Bexpression
* bcall_ref
= call_ref
->get_backend(context
);
9585 Bstatement
* assn_stmt
=
9586 gogo
->backend()->assignment_statement(bcall_ref
, call
, location
);
9588 this->call_
= this->set_results(context
, bcall_ref
);
9590 Bexpression
* set_and_call
=
9591 gogo
->backend()->compound_expression(assn_stmt
, this->call_
,
9593 return set_and_call
;
9600 // Set the result variables if this call returns multiple results.
9603 Call_expression::set_results(Translate_context
* context
, Bexpression
* call
)
9605 Gogo
* gogo
= context
->gogo();
9607 Bexpression
* results
= NULL
;
9608 Location loc
= this->location();
9610 size_t rc
= this->result_count();
9611 for (size_t i
= 0; i
< rc
; ++i
)
9613 Temporary_statement
* temp
= this->result(i
);
9616 go_assert(saw_errors());
9617 return gogo
->backend()->error_expression();
9619 Temporary_reference_expression
* ref
=
9620 Expression::make_temporary_reference(temp
, loc
);
9621 ref
->set_is_lvalue();
9623 Bexpression
* result_ref
= ref
->get_backend(context
);
9624 Bexpression
* call_result
=
9625 gogo
->backend()->struct_field_expression(call
, i
, loc
);
9626 Bstatement
* assn_stmt
=
9627 gogo
->backend()->assignment_statement(result_ref
, call_result
, loc
);
9629 Bexpression
* result
=
9630 gogo
->backend()->compound_expression(assn_stmt
, call_result
, loc
);
9632 if (results
== NULL
)
9636 Bstatement
* expr_stmt
= gogo
->backend()->expression_statement(result
);
9638 gogo
->backend()->compound_expression(expr_stmt
, results
, loc
);
9644 // Dump ast representation for a call expressin.
9647 Call_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
9649 this->fn_
->dump_expression(ast_dump_context
);
9650 ast_dump_context
->ostream() << "(";
9652 ast_dump_context
->dump_expression_list(this->args_
);
9654 ast_dump_context
->ostream() << ") ";
9657 // Make a call expression.
9660 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
9663 return new Call_expression(fn
, args
, is_varargs
, location
);
9666 // Class Call_result_expression.
9668 // Traverse a call result.
9671 Call_result_expression::do_traverse(Traverse
* traverse
)
9673 if (traverse
->remember_expression(this->call_
))
9675 // We have already traversed the call expression.
9676 return TRAVERSE_CONTINUE
;
9678 return Expression::traverse(&this->call_
, traverse
);
9684 Call_result_expression::do_type()
9686 if (this->classification() == EXPRESSION_ERROR
)
9687 return Type::make_error_type();
9689 // THIS->CALL_ can be replaced with a temporary reference due to
9690 // Call_expression::do_must_eval_in_order when there is an error.
9691 Call_expression
* ce
= this->call_
->call_expression();
9694 this->set_is_error();
9695 return Type::make_error_type();
9697 Function_type
* fntype
= ce
->get_function_type();
9700 if (ce
->issue_error())
9702 if (!ce
->fn()->type()->is_error())
9703 this->report_error(_("expected function"));
9705 this->set_is_error();
9706 return Type::make_error_type();
9708 const Typed_identifier_list
* results
= fntype
->results();
9709 if (results
== NULL
|| results
->size() < 2)
9711 if (ce
->issue_error())
9712 this->report_error(_("number of results does not match "
9713 "number of values"));
9714 return Type::make_error_type();
9716 Typed_identifier_list::const_iterator pr
= results
->begin();
9717 for (unsigned int i
= 0; i
< this->index_
; ++i
)
9719 if (pr
== results
->end())
9723 if (pr
== results
->end())
9725 if (ce
->issue_error())
9726 this->report_error(_("number of results does not match "
9727 "number of values"));
9728 return Type::make_error_type();
9733 // Check the type. Just make sure that we trigger the warning in
9737 Call_result_expression::do_check_types(Gogo
*)
9742 // Determine the type. We have nothing to do here, but the 0 result
9743 // needs to pass down to the caller.
9746 Call_result_expression::do_determine_type(const Type_context
*)
9748 this->call_
->determine_type_no_context();
9751 // Return the backend representation. We just refer to the temporary set by the
9752 // call expression. We don't do this at lowering time because it makes it
9753 // hard to evaluate the call at the right time.
9756 Call_result_expression::do_get_backend(Translate_context
* context
)
9758 Call_expression
* ce
= this->call_
->call_expression();
9761 go_assert(this->call_
->is_error_expression());
9762 return context
->backend()->error_expression();
9764 Temporary_statement
* ts
= ce
->result(this->index_
);
9767 go_assert(saw_errors());
9768 return context
->backend()->error_expression();
9770 Expression
* ref
= Expression::make_temporary_reference(ts
, this->location());
9771 return ref
->get_backend(context
);
9774 // Dump ast representation for a call result expression.
9777 Call_result_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
9780 // FIXME: Wouldn't it be better if the call is assigned to a temporary
9781 // (struct) and the fields are referenced instead.
9782 ast_dump_context
->ostream() << this->index_
<< "@(";
9783 ast_dump_context
->dump_expression(this->call_
);
9784 ast_dump_context
->ostream() << ")";
9787 // Make a reference to a single result of a call which returns
9788 // multiple results.
9791 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
9793 return new Call_result_expression(call
, index
);
9796 // Class Index_expression.
9801 Index_expression::do_traverse(Traverse
* traverse
)
9803 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
9804 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
9805 || (this->end_
!= NULL
9806 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9807 || (this->cap_
!= NULL
9808 && Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
))
9809 return TRAVERSE_EXIT
;
9810 return TRAVERSE_CONTINUE
;
9813 // Lower an index expression. This converts the generic index
9814 // expression into an array index, a string index, or a map index.
9817 Index_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
9819 Location location
= this->location();
9820 Expression
* left
= this->left_
;
9821 Expression
* start
= this->start_
;
9822 Expression
* end
= this->end_
;
9823 Expression
* cap
= this->cap_
;
9825 Type
* type
= left
->type();
9826 if (type
->is_error())
9828 go_assert(saw_errors());
9829 return Expression::make_error(location
);
9831 else if (left
->is_type_expression())
9833 error_at(location
, "attempt to index type expression");
9834 return Expression::make_error(location
);
9836 else if (type
->array_type() != NULL
)
9837 return Expression::make_array_index(left
, start
, end
, cap
, location
);
9838 else if (type
->points_to() != NULL
9839 && type
->points_to()->array_type() != NULL
9840 && !type
->points_to()->is_slice_type())
9842 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
9845 // For an ordinary index into the array, the pointer will be
9846 // dereferenced. For a slice it will not--the resulting slice
9847 // will simply reuse the pointer, which is incorrect if that
9849 if (end
!= NULL
|| cap
!= NULL
)
9850 deref
->issue_nil_check();
9852 return Expression::make_array_index(deref
, start
, end
, cap
, location
);
9854 else if (type
->is_string_type())
9858 error_at(location
, "invalid 3-index slice of string");
9859 return Expression::make_error(location
);
9861 return Expression::make_string_index(left
, start
, end
, location
);
9863 else if (type
->map_type() != NULL
)
9865 if (end
!= NULL
|| cap
!= NULL
)
9867 error_at(location
, "invalid slice of map");
9868 return Expression::make_error(location
);
9870 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
9872 if (this->is_lvalue_
)
9873 ret
->set_is_lvalue();
9879 "attempt to index object which is not array, string, or map");
9880 return Expression::make_error(location
);
9884 // Write an indexed expression
9885 // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context.
9888 Index_expression::dump_index_expression(Ast_dump_context
* ast_dump_context
,
9889 const Expression
* expr
,
9890 const Expression
* start
,
9891 const Expression
* end
,
9892 const Expression
* cap
)
9894 expr
->dump_expression(ast_dump_context
);
9895 ast_dump_context
->ostream() << "[";
9896 start
->dump_expression(ast_dump_context
);
9899 ast_dump_context
->ostream() << ":";
9900 end
->dump_expression(ast_dump_context
);
9904 ast_dump_context
->ostream() << ":";
9905 cap
->dump_expression(ast_dump_context
);
9907 ast_dump_context
->ostream() << "]";
9910 // Dump ast representation for an index expression.
9913 Index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
9916 Index_expression::dump_index_expression(ast_dump_context
, this->left_
,
9917 this->start_
, this->end_
, this->cap_
);
9920 // Make an index expression.
9923 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
9924 Expression
* cap
, Location location
)
9926 return new Index_expression(left
, start
, end
, cap
, location
);
9929 // Class Array_index_expression.
9931 // Array index traversal.
9934 Array_index_expression::do_traverse(Traverse
* traverse
)
9936 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9937 return TRAVERSE_EXIT
;
9938 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9939 return TRAVERSE_EXIT
;
9940 if (this->end_
!= NULL
)
9942 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9943 return TRAVERSE_EXIT
;
9945 if (this->cap_
!= NULL
)
9947 if (Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
)
9948 return TRAVERSE_EXIT
;
9950 return TRAVERSE_CONTINUE
;
9953 // Return the type of an array index.
9956 Array_index_expression::do_type()
9958 if (this->type_
== NULL
)
9960 Array_type
* type
= this->array_
->type()->array_type();
9962 this->type_
= Type::make_error_type();
9963 else if (this->end_
== NULL
)
9964 this->type_
= type
->element_type();
9965 else if (type
->is_slice_type())
9967 // A slice of a slice has the same type as the original
9969 this->type_
= this->array_
->type()->deref();
9973 // A slice of an array is a slice.
9974 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9980 // Set the type of an array index.
9983 Array_index_expression::do_determine_type(const Type_context
*)
9985 this->array_
->determine_type_no_context();
9987 Type_context
index_context(Type::lookup_integer_type("int"), false);
9988 if (this->start_
->is_constant())
9989 this->start_
->determine_type(&index_context
);
9991 this->start_
->determine_type_no_context();
9992 if (this->end_
!= NULL
)
9994 if (this->end_
->is_constant())
9995 this->end_
->determine_type(&index_context
);
9997 this->end_
->determine_type_no_context();
9999 if (this->cap_
!= NULL
)
10001 if (this->cap_
->is_constant())
10002 this->cap_
->determine_type(&index_context
);
10004 this->cap_
->determine_type_no_context();
10008 // Check types of an array index.
10011 Array_index_expression::do_check_types(Gogo
*)
10013 Numeric_constant nc
;
10015 if (this->start_
->type()->integer_type() == NULL
10016 && !this->start_
->type()->is_error()
10017 && (!this->start_
->numeric_constant_value(&nc
)
10018 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
10019 this->report_error(_("index must be integer"));
10020 if (this->end_
!= NULL
10021 && this->end_
->type()->integer_type() == NULL
10022 && !this->end_
->type()->is_error()
10023 && !this->end_
->is_nil_expression()
10024 && !this->end_
->is_error_expression()
10025 && (!this->end_
->numeric_constant_value(&nc
)
10026 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
10027 this->report_error(_("slice end must be integer"));
10028 if (this->cap_
!= NULL
10029 && this->cap_
->type()->integer_type() == NULL
10030 && !this->cap_
->type()->is_error()
10031 && !this->cap_
->is_nil_expression()
10032 && !this->cap_
->is_error_expression()
10033 && (!this->cap_
->numeric_constant_value(&nc
)
10034 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
10035 this->report_error(_("slice capacity must be integer"));
10037 Array_type
* array_type
= this->array_
->type()->array_type();
10038 if (array_type
== NULL
)
10040 go_assert(this->array_
->type()->is_error());
10044 unsigned int int_bits
=
10045 Type::lookup_integer_type("int")->integer_type()->bits();
10047 Numeric_constant lvalnc
;
10049 bool lval_valid
= (array_type
->length() != NULL
10050 && array_type
->length()->numeric_constant_value(&lvalnc
)
10051 && lvalnc
.to_int(&lval
));
10052 Numeric_constant inc
;
10054 bool ival_valid
= false;
10055 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
10058 if (mpz_sgn(ival
) < 0
10059 || mpz_sizeinbase(ival
, 2) >= int_bits
10061 && (this->end_
== NULL
10062 ? mpz_cmp(ival
, lval
) >= 0
10063 : mpz_cmp(ival
, lval
) > 0)))
10065 error_at(this->start_
->location(), "array index out of bounds");
10066 this->set_is_error();
10069 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
10071 Numeric_constant enc
;
10073 bool eval_valid
= false;
10074 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
10077 if (mpz_sgn(eval
) < 0
10078 || mpz_sizeinbase(eval
, 2) >= int_bits
10079 || (lval_valid
&& mpz_cmp(eval
, lval
) > 0))
10081 error_at(this->end_
->location(), "array index out of bounds");
10082 this->set_is_error();
10084 else if (ival_valid
&& mpz_cmp(ival
, eval
) > 0)
10085 this->report_error(_("inverted slice range"));
10088 Numeric_constant cnc
;
10090 if (this->cap_
!= NULL
10091 && this->cap_
->numeric_constant_value(&cnc
) && cnc
.to_int(&cval
))
10093 if (mpz_sgn(cval
) < 0
10094 || mpz_sizeinbase(cval
, 2) >= int_bits
10095 || (lval_valid
&& mpz_cmp(cval
, lval
) > 0))
10097 error_at(this->cap_
->location(), "array index out of bounds");
10098 this->set_is_error();
10100 else if (ival_valid
&& mpz_cmp(ival
, cval
) > 0)
10102 error_at(this->cap_
->location(),
10103 "invalid slice index: capacity less than start");
10104 this->set_is_error();
10106 else if (eval_valid
&& mpz_cmp(eval
, cval
) > 0)
10108 error_at(this->cap_
->location(),
10109 "invalid slice index: capacity less than length");
10110 this->set_is_error();
10123 // A slice of an array requires an addressable array. A slice of a
10124 // slice is always possible.
10125 if (this->end_
!= NULL
&& !array_type
->is_slice_type())
10127 if (!this->array_
->is_addressable())
10128 this->report_error(_("slice of unaddressable value"));
10130 this->array_
->address_taken(true);
10134 // Flatten array indexing by using temporary variables for slices and indexes.
10137 Array_index_expression::do_flatten(Gogo
*, Named_object
*,
10138 Statement_inserter
* inserter
)
10140 Location loc
= this->location();
10141 Expression
* array
= this->array_
;
10142 Expression
* start
= this->start_
;
10143 Expression
* end
= this->end_
;
10144 Expression
* cap
= this->cap_
;
10145 if (array
->is_error_expression()
10146 || array
->type()->is_error_type()
10147 || start
->is_error_expression()
10148 || start
->type()->is_error_type()
10150 && (end
->is_error_expression() || end
->type()->is_error_type()))
10152 && (cap
->is_error_expression() || cap
->type()->is_error_type())))
10154 go_assert(saw_errors());
10155 return Expression::make_error(loc
);
10158 Temporary_statement
* temp
;
10159 if (array
->type()->is_slice_type() && !array
->is_variable())
10161 temp
= Statement::make_temporary(NULL
, array
, loc
);
10162 inserter
->insert(temp
);
10163 this->array_
= Expression::make_temporary_reference(temp
, loc
);
10165 if (!start
->is_variable())
10167 temp
= Statement::make_temporary(NULL
, start
, loc
);
10168 inserter
->insert(temp
);
10169 this->start_
= Expression::make_temporary_reference(temp
, loc
);
10172 && !end
->is_nil_expression()
10173 && !end
->is_variable())
10175 temp
= Statement::make_temporary(NULL
, end
, loc
);
10176 inserter
->insert(temp
);
10177 this->end_
= Expression::make_temporary_reference(temp
, loc
);
10179 if (cap
!= NULL
&& !cap
->is_variable())
10181 temp
= Statement::make_temporary(NULL
, cap
, loc
);
10182 inserter
->insert(temp
);
10183 this->cap_
= Expression::make_temporary_reference(temp
, loc
);
10189 // Return whether this expression is addressable.
10192 Array_index_expression::do_is_addressable() const
10194 // A slice expression is not addressable.
10195 if (this->end_
!= NULL
)
10198 // An index into a slice is addressable.
10199 if (this->array_
->type()->is_slice_type())
10202 // An index into an array is addressable if the array is
10204 return this->array_
->is_addressable();
10207 // Get the backend representation for an array index.
10210 Array_index_expression::do_get_backend(Translate_context
* context
)
10212 Array_type
* array_type
= this->array_
->type()->array_type();
10213 if (array_type
== NULL
)
10215 go_assert(this->array_
->type()->is_error());
10216 return context
->backend()->error_expression();
10218 go_assert(!array_type
->is_slice_type() || this->array_
->is_variable());
10220 Location loc
= this->location();
10221 Gogo
* gogo
= context
->gogo();
10223 Type
* int_type
= Type::lookup_integer_type("int");
10224 Btype
* int_btype
= int_type
->get_backend(gogo
);
10226 // We need to convert the length and capacity to the Go "int" type here
10227 // because the length of a fixed-length array could be of type "uintptr"
10228 // and gimple disallows binary operations between "uintptr" and other
10229 // integer types. FIXME.
10230 Bexpression
* length
= NULL
;
10231 if (this->end_
== NULL
|| this->end_
->is_nil_expression())
10233 Expression
* len
= array_type
->get_length(gogo
, this->array_
);
10234 length
= len
->get_backend(context
);
10235 length
= gogo
->backend()->convert_expression(int_btype
, length
, loc
);
10238 Bexpression
* capacity
= NULL
;
10239 if (this->end_
!= NULL
)
10241 Expression
* cap
= array_type
->get_capacity(gogo
, this->array_
);
10242 capacity
= cap
->get_backend(context
);
10243 capacity
= gogo
->backend()->convert_expression(int_btype
, capacity
, loc
);
10246 Bexpression
* cap_arg
= capacity
;
10247 if (this->cap_
!= NULL
)
10249 cap_arg
= this->cap_
->get_backend(context
);
10250 cap_arg
= gogo
->backend()->convert_expression(int_btype
, cap_arg
, loc
);
10253 if (length
== NULL
)
10256 int code
= (array_type
->length() != NULL
10257 ? (this->end_
== NULL
10258 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
10259 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
10260 : (this->end_
== NULL
10261 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
10262 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
10263 Bexpression
* crash
= gogo
->runtime_error(code
, loc
)->get_backend(context
);
10265 if (this->start_
->type()->integer_type() == NULL
10266 && !Type::are_convertible(int_type
, this->start_
->type(), NULL
))
10268 go_assert(saw_errors());
10269 return context
->backend()->error_expression();
10272 Bexpression
* bad_index
=
10273 Expression::check_bounds(this->start_
, loc
)->get_backend(context
);
10275 Bexpression
* start
= this->start_
->get_backend(context
);
10276 start
= gogo
->backend()->convert_expression(int_btype
, start
, loc
);
10277 Bexpression
* start_too_large
=
10278 gogo
->backend()->binary_expression((this->end_
== NULL
10282 (this->end_
== NULL
10286 bad_index
= gogo
->backend()->binary_expression(OPERATOR_OROR
, start_too_large
,
10289 if (this->end_
== NULL
)
10291 // Simple array indexing. This has to return an l-value, so
10292 // wrap the index check into START.
10294 gogo
->backend()->conditional_expression(int_btype
, bad_index
,
10295 crash
, start
, loc
);
10298 if (array_type
->length() != NULL
)
10300 Bexpression
* array
= this->array_
->get_backend(context
);
10301 ret
= gogo
->backend()->array_index_expression(array
, start
, loc
);
10306 Expression
* valptr
=
10307 array_type
->get_value_pointer(gogo
, this->array_
);
10308 Bexpression
* ptr
= valptr
->get_backend(context
);
10309 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, start
, loc
);
10311 Type
* ele_type
= this->array_
->type()->array_type()->element_type();
10312 Btype
* ele_btype
= ele_type
->get_backend(gogo
);
10313 ret
= gogo
->backend()->indirect_expression(ele_btype
, ptr
, true, loc
);
10320 if (this->cap_
!= NULL
)
10322 Bexpression
* bounds_bcheck
=
10323 Expression::check_bounds(this->cap_
, loc
)->get_backend(context
);
10325 gogo
->backend()->binary_expression(OPERATOR_OROR
, bounds_bcheck
,
10327 cap_arg
= gogo
->backend()->convert_expression(int_btype
, cap_arg
, loc
);
10329 Bexpression
* cap_too_small
=
10330 gogo
->backend()->binary_expression(OPERATOR_LT
, cap_arg
, start
, loc
);
10331 Bexpression
* cap_too_large
=
10332 gogo
->backend()->binary_expression(OPERATOR_GT
, cap_arg
, capacity
, loc
);
10333 Bexpression
* bad_cap
=
10334 gogo
->backend()->binary_expression(OPERATOR_OROR
, cap_too_small
,
10335 cap_too_large
, loc
);
10336 bad_index
= gogo
->backend()->binary_expression(OPERATOR_OROR
, bad_cap
,
10341 if (this->end_
->is_nil_expression())
10345 Bexpression
* bounds_bcheck
=
10346 Expression::check_bounds(this->end_
, loc
)->get_backend(context
);
10349 gogo
->backend()->binary_expression(OPERATOR_OROR
, bounds_bcheck
,
10352 end
= this->end_
->get_backend(context
);
10353 end
= gogo
->backend()->convert_expression(int_btype
, end
, loc
);
10354 Bexpression
* end_too_small
=
10355 gogo
->backend()->binary_expression(OPERATOR_LT
, end
, start
, loc
);
10356 Bexpression
* end_too_large
=
10357 gogo
->backend()->binary_expression(OPERATOR_GT
, end
, cap_arg
, loc
);
10358 Bexpression
* bad_end
=
10359 gogo
->backend()->binary_expression(OPERATOR_OROR
, end_too_small
,
10360 end_too_large
, loc
);
10361 bad_index
= gogo
->backend()->binary_expression(OPERATOR_OROR
, bad_end
,
10365 Expression
* valptr
= array_type
->get_value_pointer(gogo
, this->array_
);
10366 Bexpression
* val
= valptr
->get_backend(context
);
10367 val
= gogo
->backend()->pointer_offset_expression(val
, start
, loc
);
10369 Bexpression
* result_length
=
10370 gogo
->backend()->binary_expression(OPERATOR_MINUS
, end
, start
, loc
);
10372 Bexpression
* result_capacity
=
10373 gogo
->backend()->binary_expression(OPERATOR_MINUS
, cap_arg
, start
, loc
);
10375 Btype
* struct_btype
= this->type()->get_backend(gogo
);
10376 std::vector
<Bexpression
*> init
;
10377 init
.push_back(val
);
10378 init
.push_back(result_length
);
10379 init
.push_back(result_capacity
);
10381 Bexpression
* ctor
=
10382 gogo
->backend()->constructor_expression(struct_btype
, init
, loc
);
10383 return gogo
->backend()->conditional_expression(struct_btype
, bad_index
,
10387 // Dump ast representation for an array index expression.
10390 Array_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10393 Index_expression::dump_index_expression(ast_dump_context
, this->array_
,
10394 this->start_
, this->end_
, this->cap_
);
10397 // Make an array index expression. END and CAP may be NULL.
10400 Expression::make_array_index(Expression
* array
, Expression
* start
,
10401 Expression
* end
, Expression
* cap
,
10404 return new Array_index_expression(array
, start
, end
, cap
, location
);
10407 // Class String_index_expression.
10409 // String index traversal.
10412 String_index_expression::do_traverse(Traverse
* traverse
)
10414 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
10415 return TRAVERSE_EXIT
;
10416 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
10417 return TRAVERSE_EXIT
;
10418 if (this->end_
!= NULL
)
10420 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
10421 return TRAVERSE_EXIT
;
10423 return TRAVERSE_CONTINUE
;
10427 String_index_expression::do_flatten(Gogo
*, Named_object
*,
10428 Statement_inserter
* inserter
)
10430 Location loc
= this->location();
10431 Expression
* string
= this->string_
;
10432 Expression
* start
= this->start_
;
10433 Expression
* end
= this->end_
;
10434 if (string
->is_error_expression()
10435 || string
->type()->is_error_type()
10436 || start
->is_error_expression()
10437 || start
->type()->is_error_type()
10439 && (end
->is_error_expression() || end
->type()->is_error_type())))
10441 go_assert(saw_errors());
10442 return Expression::make_error(loc
);
10445 Temporary_statement
* temp
;
10446 if (!this->string_
->is_variable())
10448 temp
= Statement::make_temporary(NULL
, this->string_
, loc
);
10449 inserter
->insert(temp
);
10450 this->string_
= Expression::make_temporary_reference(temp
, loc
);
10452 if (!this->start_
->is_variable())
10454 temp
= Statement::make_temporary(NULL
, this->start_
, loc
);
10455 inserter
->insert(temp
);
10456 this->start_
= Expression::make_temporary_reference(temp
, loc
);
10458 if (this->end_
!= NULL
10459 && !this->end_
->is_nil_expression()
10460 && !this->end_
->is_variable())
10462 temp
= Statement::make_temporary(NULL
, this->end_
, loc
);
10463 inserter
->insert(temp
);
10464 this->end_
= Expression::make_temporary_reference(temp
, loc
);
10470 // Return the type of a string index.
10473 String_index_expression::do_type()
10475 if (this->end_
== NULL
)
10476 return Type::lookup_integer_type("uint8");
10478 return this->string_
->type();
10481 // Determine the type of a string index.
10484 String_index_expression::do_determine_type(const Type_context
*)
10486 this->string_
->determine_type_no_context();
10488 Type_context
index_context(Type::lookup_integer_type("int"), false);
10489 if (this->start_
->is_constant())
10490 this->start_
->determine_type(&index_context
);
10492 this->start_
->determine_type_no_context();
10493 if (this->end_
!= NULL
)
10495 if (this->end_
->is_constant())
10496 this->end_
->determine_type(&index_context
);
10498 this->end_
->determine_type_no_context();
10502 // Check types of a string index.
10505 String_index_expression::do_check_types(Gogo
*)
10507 Numeric_constant nc
;
10509 if (this->start_
->type()->integer_type() == NULL
10510 && !this->start_
->type()->is_error()
10511 && (!this->start_
->numeric_constant_value(&nc
)
10512 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
10513 this->report_error(_("index must be integer"));
10514 if (this->end_
!= NULL
10515 && this->end_
->type()->integer_type() == NULL
10516 && !this->end_
->type()->is_error()
10517 && !this->end_
->is_nil_expression()
10518 && !this->end_
->is_error_expression()
10519 && (!this->end_
->numeric_constant_value(&nc
)
10520 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
10521 this->report_error(_("slice end must be integer"));
10524 bool sval_valid
= this->string_
->string_constant_value(&sval
);
10526 Numeric_constant inc
;
10528 bool ival_valid
= false;
10529 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
10532 if (mpz_sgn(ival
) < 0
10534 && (this->end_
== NULL
10535 ? mpz_cmp_ui(ival
, sval
.length()) >= 0
10536 : mpz_cmp_ui(ival
, sval
.length()) > 0)))
10538 error_at(this->start_
->location(), "string index out of bounds");
10539 this->set_is_error();
10542 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
10544 Numeric_constant enc
;
10546 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
10548 if (mpz_sgn(eval
) < 0
10549 || (sval_valid
&& mpz_cmp_ui(eval
, sval
.length()) > 0))
10551 error_at(this->end_
->location(), "string index out of bounds");
10552 this->set_is_error();
10554 else if (ival_valid
&& mpz_cmp(ival
, eval
) > 0)
10555 this->report_error(_("inverted slice range"));
10563 // Get the backend representation for a string index.
10566 String_index_expression::do_get_backend(Translate_context
* context
)
10568 Location loc
= this->location();
10569 Expression
* string_arg
= this->string_
;
10570 if (this->string_
->type()->points_to() != NULL
)
10571 string_arg
= Expression::make_unary(OPERATOR_MULT
, this->string_
, loc
);
10573 Expression
* bad_index
= Expression::check_bounds(this->start_
, loc
);
10575 int code
= (this->end_
== NULL
10576 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
10577 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
10579 Gogo
* gogo
= context
->gogo();
10580 Bexpression
* crash
= gogo
->runtime_error(code
, loc
)->get_backend(context
);
10582 Type
* int_type
= Type::lookup_integer_type("int");
10584 // It is possible that an error occurred earlier because the start index
10585 // cannot be represented as an integer type. In this case, we shouldn't
10586 // try casting the starting index into an integer since
10587 // Type_conversion_expression will fail to get the backend representation.
10589 if (this->start_
->type()->integer_type() == NULL
10590 && !Type::are_convertible(int_type
, this->start_
->type(), NULL
))
10592 go_assert(saw_errors());
10593 return context
->backend()->error_expression();
10596 Expression
* start
= Expression::make_cast(int_type
, this->start_
, loc
);
10598 if (this->end_
== NULL
)
10600 Expression
* length
=
10601 Expression::make_string_info(this->string_
, STRING_INFO_LENGTH
, loc
);
10603 Expression
* start_too_large
=
10604 Expression::make_binary(OPERATOR_GE
, start
, length
, loc
);
10605 bad_index
= Expression::make_binary(OPERATOR_OROR
, start_too_large
,
10607 Expression
* bytes
=
10608 Expression::make_string_info(this->string_
, STRING_INFO_DATA
, loc
);
10610 Bexpression
* bstart
= start
->get_backend(context
);
10611 Bexpression
* ptr
= bytes
->get_backend(context
);
10612 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, bstart
, loc
);
10613 Btype
* ubtype
= Type::lookup_integer_type("uint8")->get_backend(gogo
);
10614 Bexpression
* index
=
10615 gogo
->backend()->indirect_expression(ubtype
, ptr
, true, loc
);
10617 Btype
* byte_btype
= bytes
->type()->points_to()->get_backend(gogo
);
10618 Bexpression
* index_error
= bad_index
->get_backend(context
);
10619 return gogo
->backend()->conditional_expression(byte_btype
, index_error
,
10620 crash
, index
, loc
);
10623 Expression
* end
= NULL
;
10624 if (this->end_
->is_nil_expression())
10625 end
= Expression::make_integer_sl(-1, int_type
, loc
);
10628 Expression
* bounds_check
= Expression::check_bounds(this->end_
, loc
);
10630 Expression::make_binary(OPERATOR_OROR
, bounds_check
, bad_index
, loc
);
10631 end
= Expression::make_cast(int_type
, this->end_
, loc
);
10634 Expression
* strslice
= Runtime::make_call(Runtime::STRING_SLICE
, loc
, 3,
10635 string_arg
, start
, end
);
10636 Bexpression
* bstrslice
= strslice
->get_backend(context
);
10638 Btype
* str_btype
= strslice
->type()->get_backend(gogo
);
10639 Bexpression
* index_error
= bad_index
->get_backend(context
);
10640 return gogo
->backend()->conditional_expression(str_btype
, index_error
,
10641 crash
, bstrslice
, loc
);
10644 // Dump ast representation for a string index expression.
10647 String_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10650 Index_expression::dump_index_expression(ast_dump_context
, this->string_
,
10651 this->start_
, this->end_
, NULL
);
10654 // Make a string index expression. END may be NULL.
10657 Expression::make_string_index(Expression
* string
, Expression
* start
,
10658 Expression
* end
, Location location
)
10660 return new String_index_expression(string
, start
, end
, location
);
10663 // Class Map_index.
10665 // Get the type of the map.
10668 Map_index_expression::get_map_type() const
10670 Map_type
* mt
= this->map_
->type()->deref()->map_type();
10672 go_assert(saw_errors());
10676 // Map index traversal.
10679 Map_index_expression::do_traverse(Traverse
* traverse
)
10681 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
10682 return TRAVERSE_EXIT
;
10683 return Expression::traverse(&this->index_
, traverse
);
10686 // We need to pass in a pointer to the key, so flatten the index into a
10687 // temporary variable if it isn't already. The value pointer will be
10688 // dereferenced and checked for nil, so flatten into a temporary to avoid
10692 Map_index_expression::do_flatten(Gogo
* gogo
, Named_object
*,
10693 Statement_inserter
* inserter
)
10695 Location loc
= this->location();
10696 Map_type
* mt
= this->get_map_type();
10697 if (this->index()->is_error_expression()
10698 || this->index()->type()->is_error_type()
10699 || mt
->is_error_type())
10701 go_assert(saw_errors());
10702 return Expression::make_error(loc
);
10705 if (!Type::are_identical(mt
->key_type(), this->index_
->type(), false, NULL
))
10707 if (this->index_
->type()->interface_type() != NULL
10708 && !this->index_
->is_variable())
10710 Temporary_statement
* temp
=
10711 Statement::make_temporary(NULL
, this->index_
, loc
);
10712 inserter
->insert(temp
);
10713 this->index_
= Expression::make_temporary_reference(temp
, loc
);
10715 this->index_
= Expression::convert_for_assignment(gogo
, mt
->key_type(),
10716 this->index_
, loc
);
10719 if (!this->index_
->is_variable())
10721 Temporary_statement
* temp
= Statement::make_temporary(NULL
, this->index_
,
10723 inserter
->insert(temp
);
10724 this->index_
= Expression::make_temporary_reference(temp
, loc
);
10727 if (this->value_pointer_
== NULL
)
10728 this->get_value_pointer(this->is_lvalue_
);
10729 if (this->value_pointer_
->is_error_expression()
10730 || this->value_pointer_
->type()->is_error_type())
10731 return Expression::make_error(loc
);
10732 if (!this->value_pointer_
->is_variable())
10734 Temporary_statement
* temp
=
10735 Statement::make_temporary(NULL
, this->value_pointer_
, loc
);
10736 inserter
->insert(temp
);
10737 this->value_pointer_
= Expression::make_temporary_reference(temp
, loc
);
10743 // Return the type of a map index.
10746 Map_index_expression::do_type()
10748 Map_type
* mt
= this->get_map_type();
10750 return Type::make_error_type();
10751 Type
* type
= mt
->val_type();
10752 // If this map index is in a tuple assignment, we actually return a
10753 // pointer to the value type. Tuple_map_assignment_statement is
10754 // responsible for handling this correctly. We need to get the type
10755 // right in case this gets assigned to a temporary variable.
10756 if (this->is_in_tuple_assignment_
)
10757 type
= Type::make_pointer_type(type
);
10761 // Fix the type of a map index.
10764 Map_index_expression::do_determine_type(const Type_context
*)
10766 this->map_
->determine_type_no_context();
10767 Map_type
* mt
= this->get_map_type();
10768 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
10769 Type_context
subcontext(key_type
, false);
10770 this->index_
->determine_type(&subcontext
);
10773 // Check types of a map index.
10776 Map_index_expression::do_check_types(Gogo
*)
10778 std::string reason
;
10779 Map_type
* mt
= this->get_map_type();
10782 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
10784 if (reason
.empty())
10785 this->report_error(_("incompatible type for map index"));
10788 error_at(this->location(), "incompatible type for map index (%s)",
10790 this->set_is_error();
10795 // Get the backend representation for a map index.
10798 Map_index_expression::do_get_backend(Translate_context
* context
)
10800 Map_type
* type
= this->get_map_type();
10803 go_assert(saw_errors());
10804 return context
->backend()->error_expression();
10807 go_assert(this->value_pointer_
!= NULL
10808 && this->value_pointer_
->is_variable());
10811 if (this->is_lvalue_
)
10814 Expression::make_unary(OPERATOR_MULT
, this->value_pointer_
,
10816 ret
= val
->get_backend(context
);
10818 else if (this->is_in_tuple_assignment_
)
10820 // Tuple_map_assignment_statement is responsible for using this
10822 ret
= this->value_pointer_
->get_backend(context
);
10826 Location loc
= this->location();
10828 Expression
* nil_check
=
10829 Expression::make_binary(OPERATOR_EQEQ
, this->value_pointer_
,
10830 Expression::make_nil(loc
), loc
);
10831 Bexpression
* bnil_check
= nil_check
->get_backend(context
);
10833 Expression::make_unary(OPERATOR_MULT
, this->value_pointer_
, loc
);
10834 Bexpression
* bval
= val
->get_backend(context
);
10836 Gogo
* gogo
= context
->gogo();
10837 Btype
* val_btype
= type
->val_type()->get_backend(gogo
);
10838 Bexpression
* val_zero
= gogo
->backend()->zero_expression(val_btype
);
10839 ret
= gogo
->backend()->conditional_expression(val_btype
, bnil_check
,
10840 val_zero
, bval
, loc
);
10845 // Get an expression for the map index. This returns an expression which
10846 // evaluates to a pointer to a value. The pointer will be NULL if the key is
10850 Map_index_expression::get_value_pointer(bool insert
)
10852 if (this->value_pointer_
== NULL
)
10854 Map_type
* type
= this->get_map_type();
10857 go_assert(saw_errors());
10858 return Expression::make_error(this->location());
10861 Location loc
= this->location();
10862 Expression
* map_ref
= this->map_
;
10863 if (this->map_
->type()->points_to() != NULL
)
10864 map_ref
= Expression::make_unary(OPERATOR_MULT
, map_ref
, loc
);
10866 Expression
* index_ptr
= Expression::make_unary(OPERATOR_AND
, this->index_
,
10868 Expression
* map_index
=
10869 Runtime::make_call(Runtime::MAP_INDEX
, loc
, 3,
10870 map_ref
, index_ptr
,
10871 Expression::make_boolean(insert
, loc
));
10873 Type
* val_type
= type
->val_type();
10874 this->value_pointer_
=
10875 Expression::make_unsafe_cast(Type::make_pointer_type(val_type
),
10876 map_index
, this->location());
10878 return this->value_pointer_
;
10881 // Dump ast representation for a map index expression
10884 Map_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10887 Index_expression::dump_index_expression(ast_dump_context
, this->map_
,
10888 this->index_
, NULL
, NULL
);
10891 // Make a map index expression.
10893 Map_index_expression
*
10894 Expression::make_map_index(Expression
* map
, Expression
* index
,
10897 return new Map_index_expression(map
, index
, location
);
10900 // Class Field_reference_expression.
10902 // Lower a field reference expression. There is nothing to lower, but
10903 // this is where we generate the tracking information for fields with
10904 // the magic go:"track" tag.
10907 Field_reference_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
10908 Statement_inserter
* inserter
, int)
10910 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
10911 if (struct_type
== NULL
)
10913 // Error will be reported elsewhere.
10916 const Struct_field
* field
= struct_type
->field(this->field_index_
);
10919 if (!field
->has_tag())
10921 if (field
->tag().find("go:\"track\"") == std::string::npos
)
10924 // References from functions generated by the compiler don't count.
10925 if (function
!= NULL
&& function
->func_value()->is_type_specific_function())
10928 // We have found a reference to a tracked field. Build a call to
10929 // the runtime function __go_fieldtrack with a string that describes
10930 // the field. FIXME: We should only call this once per referenced
10931 // field per function, not once for each reference to the field.
10933 if (this->called_fieldtrack_
)
10935 this->called_fieldtrack_
= true;
10937 Location loc
= this->location();
10939 std::string s
= "fieldtrack \"";
10940 Named_type
* nt
= this->expr_
->type()->named_type();
10941 if (nt
== NULL
|| nt
->named_object()->package() == NULL
)
10942 s
.append(gogo
->pkgpath());
10944 s
.append(nt
->named_object()->package()->pkgpath());
10947 s
.append(Gogo::unpack_hidden_name(nt
->name()));
10949 s
.append(field
->field_name());
10952 // We can't use a string here, because internally a string holds a
10953 // pointer to the actual bytes; when the linker garbage collects the
10954 // string, it won't garbage collect the bytes. So we use a
10957 Expression
* length_expr
= Expression::make_integer_ul(s
.length(), NULL
, loc
);
10959 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
10960 Type
* array_type
= Type::make_array_type(byte_type
, length_expr
);
10962 Expression_list
* bytes
= new Expression_list();
10963 for (std::string::const_iterator p
= s
.begin(); p
!= s
.end(); p
++)
10965 unsigned char c
= static_cast<unsigned char>(*p
);
10966 bytes
->push_back(Expression::make_integer_ul(c
, NULL
, loc
));
10969 Expression
* e
= Expression::make_composite_literal(array_type
, 0, false,
10970 bytes
, false, loc
);
10972 Variable
* var
= new Variable(array_type
, e
, true, false, false, loc
);
10976 snprintf(buf
, sizeof buf
, "fieldtrack.%d", count
);
10979 Named_object
* no
= gogo
->add_variable(buf
, var
);
10980 e
= Expression::make_var_reference(no
, loc
);
10981 e
= Expression::make_unary(OPERATOR_AND
, e
, loc
);
10983 Expression
* call
= Runtime::make_call(Runtime::FIELDTRACK
, loc
, 1, e
);
10984 gogo
->lower_expression(function
, inserter
, &call
);
10985 inserter
->insert(Statement::make_statement(call
, false));
10987 // Put this function, and the global variable we just created, into
10988 // unique sections. This will permit the linker to garbage collect
10989 // them if they are not referenced. The effect is that the only
10990 // strings, indicating field references, that will wind up in the
10991 // executable will be those for functions that are actually needed.
10992 if (function
!= NULL
)
10993 function
->func_value()->set_in_unique_section();
10994 var
->set_in_unique_section();
10999 // Return the type of a field reference.
11002 Field_reference_expression::do_type()
11004 Type
* type
= this->expr_
->type();
11005 if (type
->is_error())
11007 Struct_type
* struct_type
= type
->struct_type();
11008 go_assert(struct_type
!= NULL
);
11009 return struct_type
->field(this->field_index_
)->type();
11012 // Check the types for a field reference.
11015 Field_reference_expression::do_check_types(Gogo
*)
11017 Type
* type
= this->expr_
->type();
11018 if (type
->is_error())
11020 Struct_type
* struct_type
= type
->struct_type();
11021 go_assert(struct_type
!= NULL
);
11022 go_assert(struct_type
->field(this->field_index_
) != NULL
);
11025 // Get the backend representation for a field reference.
11028 Field_reference_expression::do_get_backend(Translate_context
* context
)
11030 Bexpression
* bstruct
= this->expr_
->get_backend(context
);
11031 return context
->gogo()->backend()->struct_field_expression(bstruct
,
11032 this->field_index_
,
11036 // Dump ast representation for a field reference expression.
11039 Field_reference_expression::do_dump_expression(
11040 Ast_dump_context
* ast_dump_context
) const
11042 this->expr_
->dump_expression(ast_dump_context
);
11043 ast_dump_context
->ostream() << "." << this->field_index_
;
11046 // Make a reference to a qualified identifier in an expression.
11048 Field_reference_expression
*
11049 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
11052 return new Field_reference_expression(expr
, field_index
, location
);
11055 // Class Interface_field_reference_expression.
11057 // Return an expression for the pointer to the function to call.
11060 Interface_field_reference_expression::get_function()
11062 Expression
* ref
= this->expr_
;
11063 Location loc
= this->location();
11064 if (ref
->type()->points_to() != NULL
)
11065 ref
= Expression::make_unary(OPERATOR_MULT
, ref
, loc
);
11067 Expression
* mtable
=
11068 Expression::make_interface_info(ref
, INTERFACE_INFO_METHODS
, loc
);
11069 Struct_type
* mtable_type
= mtable
->type()->points_to()->struct_type();
11071 std::string name
= Gogo::unpack_hidden_name(this->name_
);
11072 unsigned int index
;
11073 const Struct_field
* field
= mtable_type
->find_local_field(name
, &index
);
11074 go_assert(field
!= NULL
);
11075 mtable
= Expression::make_unary(OPERATOR_MULT
, mtable
, loc
);
11076 return Expression::make_field_reference(mtable
, index
, loc
);
11079 // Return an expression for the first argument to pass to the interface
11083 Interface_field_reference_expression::get_underlying_object()
11085 Expression
* expr
= this->expr_
;
11086 if (expr
->type()->points_to() != NULL
)
11087 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, this->location());
11088 return Expression::make_interface_info(expr
, INTERFACE_INFO_OBJECT
,
11095 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
11097 return Expression::traverse(&this->expr_
, traverse
);
11100 // Lower the expression. If this expression is not called, we need to
11101 // evaluate the expression twice when converting to the backend
11102 // interface. So introduce a temporary variable if necessary.
11105 Interface_field_reference_expression::do_flatten(Gogo
*, Named_object
*,
11106 Statement_inserter
* inserter
)
11108 if (this->expr_
->is_error_expression()
11109 || this->expr_
->type()->is_error_type())
11111 go_assert(saw_errors());
11112 return Expression::make_error(this->location());
11115 if (!this->expr_
->is_variable())
11117 Temporary_statement
* temp
=
11118 Statement::make_temporary(this->expr_
->type(), NULL
, this->location());
11119 inserter
->insert(temp
);
11120 this->expr_
= Expression::make_set_and_use_temporary(temp
, this->expr_
,
11126 // Return the type of an interface field reference.
11129 Interface_field_reference_expression::do_type()
11131 Type
* expr_type
= this->expr_
->type();
11133 Type
* points_to
= expr_type
->points_to();
11134 if (points_to
!= NULL
)
11135 expr_type
= points_to
;
11137 Interface_type
* interface_type
= expr_type
->interface_type();
11138 if (interface_type
== NULL
)
11139 return Type::make_error_type();
11141 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
11142 if (method
== NULL
)
11143 return Type::make_error_type();
11145 return method
->type();
11148 // Determine types.
11151 Interface_field_reference_expression::do_determine_type(const Type_context
*)
11153 this->expr_
->determine_type_no_context();
11156 // Check the types for an interface field reference.
11159 Interface_field_reference_expression::do_check_types(Gogo
*)
11161 Type
* type
= this->expr_
->type();
11163 Type
* points_to
= type
->points_to();
11164 if (points_to
!= NULL
)
11167 Interface_type
* interface_type
= type
->interface_type();
11168 if (interface_type
== NULL
)
11170 if (!type
->is_error_type())
11171 this->report_error(_("expected interface or pointer to interface"));
11175 const Typed_identifier
* method
=
11176 interface_type
->find_method(this->name_
);
11177 if (method
== NULL
)
11179 error_at(this->location(), "method %qs not in interface",
11180 Gogo::message_name(this->name_
).c_str());
11181 this->set_is_error();
11186 // If an interface field reference is not simply called, then it is
11187 // represented as a closure. The closure will hold a single variable,
11188 // the value of the interface on which the method should be called.
11189 // The function will be a simple thunk that pulls the value from the
11190 // closure and calls the method with the remaining arguments.
11192 // Because method values are not common, we don't build all thunks for
11193 // all possible interface methods, but instead only build them as we
11194 // need them. In particular, we even build them on demand for
11195 // interface methods defined in other packages.
11197 Interface_field_reference_expression::Interface_method_thunks
11198 Interface_field_reference_expression::interface_method_thunks
;
11200 // Find or create the thunk to call method NAME on TYPE.
11203 Interface_field_reference_expression::create_thunk(Gogo
* gogo
,
11204 Interface_type
* type
,
11205 const std::string
& name
)
11207 std::pair
<Interface_type
*, Method_thunks
*> val(type
, NULL
);
11208 std::pair
<Interface_method_thunks::iterator
, bool> ins
=
11209 Interface_field_reference_expression::interface_method_thunks
.insert(val
);
11212 // This is the first time we have seen this interface.
11213 ins
.first
->second
= new Method_thunks();
11216 for (Method_thunks::const_iterator p
= ins
.first
->second
->begin();
11217 p
!= ins
.first
->second
->end();
11219 if (p
->first
== name
)
11222 Location loc
= type
->location();
11224 const Typed_identifier
* method_id
= type
->find_method(name
);
11225 if (method_id
== NULL
)
11226 return Named_object::make_erroneous_name(Gogo::thunk_name());
11228 Function_type
* orig_fntype
= method_id
->type()->function_type();
11229 if (orig_fntype
== NULL
)
11230 return Named_object::make_erroneous_name(Gogo::thunk_name());
11232 Struct_field_list
* sfl
= new Struct_field_list();
11233 // The type here is wrong--it should be the C function type. But it
11234 // doesn't really matter.
11235 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
11236 sfl
->push_back(Struct_field(Typed_identifier("fn.0", vt
, loc
)));
11237 sfl
->push_back(Struct_field(Typed_identifier("val.1", type
, loc
)));
11238 Type
* closure_type
= Type::make_struct_type(sfl
, loc
);
11239 closure_type
= Type::make_pointer_type(closure_type
);
11241 Function_type
* new_fntype
= orig_fntype
->copy_with_names();
11243 std::string thunk_name
= Gogo::thunk_name();
11244 Named_object
* new_no
= gogo
->start_function(thunk_name
, new_fntype
,
11247 Variable
* cvar
= new Variable(closure_type
, NULL
, false, false, false, loc
);
11248 cvar
->set_is_used();
11249 cvar
->set_is_closure();
11250 Named_object
* cp
= Named_object::make_variable("$closure" + thunk_name
,
11252 new_no
->func_value()->set_closure_var(cp
);
11254 gogo
->start_block(loc
);
11256 // Field 0 of the closure is the function code pointer, field 1 is
11257 // the value on which to invoke the method.
11258 Expression
* arg
= Expression::make_var_reference(cp
, loc
);
11259 arg
= Expression::make_unary(OPERATOR_MULT
, arg
, loc
);
11260 arg
= Expression::make_field_reference(arg
, 1, loc
);
11262 Expression
*ifre
= Expression::make_interface_field_reference(arg
, name
,
11265 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
11266 Expression_list
* args
;
11267 if (orig_params
== NULL
|| orig_params
->empty())
11271 const Typed_identifier_list
* new_params
= new_fntype
->parameters();
11272 args
= new Expression_list();
11273 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
11274 p
!= new_params
->end();
11277 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
11278 go_assert(p_no
!= NULL
11279 && p_no
->is_variable()
11280 && p_no
->var_value()->is_parameter());
11281 args
->push_back(Expression::make_var_reference(p_no
, loc
));
11285 Call_expression
* call
= Expression::make_call(ifre
, args
,
11286 orig_fntype
->is_varargs(),
11288 call
->set_varargs_are_lowered();
11290 Statement
* s
= Statement::make_return_from_call(call
, loc
);
11291 gogo
->add_statement(s
);
11292 Block
* b
= gogo
->finish_block(loc
);
11293 gogo
->add_block(b
, loc
);
11294 gogo
->lower_block(new_no
, b
);
11295 gogo
->flatten_block(new_no
, b
);
11296 gogo
->finish_function(loc
);
11298 ins
.first
->second
->push_back(std::make_pair(name
, new_no
));
11302 // Get the backend representation for a method value.
11305 Interface_field_reference_expression::do_get_backend(Translate_context
* context
)
11307 Interface_type
* type
= this->expr_
->type()->interface_type();
11310 go_assert(saw_errors());
11311 return context
->backend()->error_expression();
11314 Named_object
* thunk
=
11315 Interface_field_reference_expression::create_thunk(context
->gogo(),
11316 type
, this->name_
);
11317 if (thunk
->is_erroneous())
11319 go_assert(saw_errors());
11320 return context
->backend()->error_expression();
11323 // FIXME: We should lower this earlier, but we can't it lower it in
11324 // the lowering pass because at that point we don't know whether we
11325 // need to create the thunk or not. If the expression is called, we
11326 // don't need the thunk.
11328 Location loc
= this->location();
11330 Struct_field_list
* fields
= new Struct_field_list();
11331 fields
->push_back(Struct_field(Typed_identifier("fn.0",
11332 thunk
->func_value()->type(),
11334 fields
->push_back(Struct_field(Typed_identifier("val.1",
11335 this->expr_
->type(),
11337 Struct_type
* st
= Type::make_struct_type(fields
, loc
);
11339 Expression_list
* vals
= new Expression_list();
11340 vals
->push_back(Expression::make_func_code_reference(thunk
, loc
));
11341 vals
->push_back(this->expr_
);
11343 Expression
* expr
= Expression::make_struct_composite_literal(st
, vals
, loc
);
11344 Bexpression
* bclosure
=
11345 Expression::make_heap_expression(expr
, loc
)->get_backend(context
);
11347 Expression
* nil_check
=
11348 Expression::make_binary(OPERATOR_EQEQ
, this->expr_
,
11349 Expression::make_nil(loc
), loc
);
11350 Bexpression
* bnil_check
= nil_check
->get_backend(context
);
11352 Gogo
* gogo
= context
->gogo();
11353 Bexpression
* bcrash
= gogo
->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
11354 loc
)->get_backend(context
);
11356 Bexpression
* bcond
=
11357 gogo
->backend()->conditional_expression(NULL
, bnil_check
, bcrash
, NULL
, loc
);
11358 Bstatement
* cond_statement
= gogo
->backend()->expression_statement(bcond
);
11359 return gogo
->backend()->compound_expression(cond_statement
, bclosure
, loc
);
11362 // Dump ast representation for an interface field reference.
11365 Interface_field_reference_expression::do_dump_expression(
11366 Ast_dump_context
* ast_dump_context
) const
11368 this->expr_
->dump_expression(ast_dump_context
);
11369 ast_dump_context
->ostream() << "." << this->name_
;
11372 // Make a reference to a field in an interface.
11375 Expression::make_interface_field_reference(Expression
* expr
,
11376 const std::string
& field
,
11379 return new Interface_field_reference_expression(expr
, field
, location
);
11382 // A general selector. This is a Parser_expression for LEFT.NAME. It
11383 // is lowered after we know the type of the left hand side.
11385 class Selector_expression
: public Parser_expression
11388 Selector_expression(Expression
* left
, const std::string
& name
,
11390 : Parser_expression(EXPRESSION_SELECTOR
, location
),
11391 left_(left
), name_(name
)
11396 do_traverse(Traverse
* traverse
)
11397 { return Expression::traverse(&this->left_
, traverse
); }
11400 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
11405 return new Selector_expression(this->left_
->copy(), this->name_
,
11410 do_dump_expression(Ast_dump_context
* ast_dump_context
) const;
11414 lower_method_expression(Gogo
*);
11416 // The expression on the left hand side.
11418 // The name on the right hand side.
11422 // Lower a selector expression once we know the real type of the left
11426 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, Statement_inserter
*,
11429 Expression
* left
= this->left_
;
11430 if (left
->is_type_expression())
11431 return this->lower_method_expression(gogo
);
11432 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
11436 // Lower a method expression T.M or (*T).M. We turn this into a
11437 // function literal.
11440 Selector_expression::lower_method_expression(Gogo
* gogo
)
11442 Location location
= this->location();
11443 Type
* left_type
= this->left_
->type();
11444 Type
* type
= left_type
;
11445 const std::string
& name(this->name_
);
11448 if (type
->points_to() == NULL
)
11449 is_pointer
= false;
11453 type
= type
->points_to();
11455 Named_type
* nt
= type
->named_type();
11459 ("method expression requires named type or "
11460 "pointer to named type"));
11461 return Expression::make_error(location
);
11465 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
11466 const Typed_identifier
* imethod
= NULL
;
11467 if (method
== NULL
&& !is_pointer
)
11469 Interface_type
* it
= nt
->interface_type();
11471 imethod
= it
->find_method(name
);
11474 if ((method
== NULL
&& imethod
== NULL
)
11475 || (left_type
->named_type() != NULL
&& left_type
->points_to() != NULL
))
11478 error_at(location
, "type %<%s%s%> has no method %<%s%>",
11479 is_pointer
? "*" : "",
11480 nt
->message_name().c_str(),
11481 Gogo::message_name(name
).c_str());
11483 error_at(location
, "method %<%s%s%> is ambiguous in type %<%s%>",
11484 Gogo::message_name(name
).c_str(),
11485 is_pointer
? "*" : "",
11486 nt
->message_name().c_str());
11487 return Expression::make_error(location
);
11490 if (method
!= NULL
&& !is_pointer
&& !method
->is_value_method())
11492 error_at(location
, "method requires pointer (use %<(*%s).%s%>)",
11493 nt
->message_name().c_str(),
11494 Gogo::message_name(name
).c_str());
11495 return Expression::make_error(location
);
11498 // Build a new function type in which the receiver becomes the first
11500 Function_type
* method_type
;
11501 if (method
!= NULL
)
11503 method_type
= method
->type();
11504 go_assert(method_type
->is_method());
11508 method_type
= imethod
->type()->function_type();
11509 go_assert(method_type
!= NULL
&& !method_type
->is_method());
11512 const char* const receiver_name
= "$this";
11513 Typed_identifier_list
* parameters
= new Typed_identifier_list();
11514 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
11517 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
11518 if (method_parameters
!= NULL
)
11521 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
11522 p
!= method_parameters
->end();
11525 if (!p
->name().empty())
11526 parameters
->push_back(*p
);
11530 snprintf(buf
, sizeof buf
, "$param%d", i
);
11531 parameters
->push_back(Typed_identifier(buf
, p
->type(),
11537 const Typed_identifier_list
* method_results
= method_type
->results();
11538 Typed_identifier_list
* results
;
11539 if (method_results
== NULL
)
11543 results
= new Typed_identifier_list();
11544 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
11545 p
!= method_results
->end();
11547 results
->push_back(*p
);
11550 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
11552 if (method_type
->is_varargs())
11553 fntype
->set_is_varargs();
11555 // We generate methods which always takes a pointer to the receiver
11556 // as their first argument. If this is for a pointer type, we can
11557 // simply reuse the existing function. We use an internal hack to
11558 // get the right type.
11559 // FIXME: This optimization is disabled because it doesn't yet work
11560 // with function descriptors when the method expression is not
11561 // directly called.
11562 if (method
!= NULL
&& is_pointer
&& false)
11564 Named_object
* mno
= (method
->needs_stub_method()
11565 ? method
->stub_object()
11566 : method
->named_object());
11567 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
11568 f
= Expression::make_cast(fntype
, f
, location
);
11569 Type_conversion_expression
* tce
=
11570 static_cast<Type_conversion_expression
*>(f
);
11571 tce
->set_may_convert_function_types();
11575 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
11578 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
11579 go_assert(vno
!= NULL
);
11580 Expression
* ve
= Expression::make_var_reference(vno
, location
);
11582 if (method
!= NULL
)
11583 bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
11585 bm
= Expression::make_interface_field_reference(ve
, name
, location
);
11587 // Even though we found the method above, if it has an error type we
11588 // may see an error here.
11589 if (bm
->is_error_expression())
11591 gogo
->finish_function(location
);
11595 Expression_list
* args
;
11596 if (parameters
->size() <= 1)
11600 args
= new Expression_list();
11601 Typed_identifier_list::const_iterator p
= parameters
->begin();
11603 for (; p
!= parameters
->end(); ++p
)
11605 vno
= gogo
->lookup(p
->name(), NULL
);
11606 go_assert(vno
!= NULL
);
11607 args
->push_back(Expression::make_var_reference(vno
, location
));
11611 gogo
->start_block(location
);
11613 Call_expression
* call
= Expression::make_call(bm
, args
,
11614 method_type
->is_varargs(),
11617 Statement
* s
= Statement::make_return_from_call(call
, location
);
11618 gogo
->add_statement(s
);
11620 Block
* b
= gogo
->finish_block(location
);
11622 gogo
->add_block(b
, location
);
11624 // Lower the call in case there are multiple results.
11625 gogo
->lower_block(no
, b
);
11626 gogo
->flatten_block(no
, b
);
11628 gogo
->finish_function(location
);
11630 return Expression::make_func_reference(no
, NULL
, location
);
11633 // Dump the ast for a selector expression.
11636 Selector_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
11639 ast_dump_context
->dump_expression(this->left_
);
11640 ast_dump_context
->ostream() << ".";
11641 ast_dump_context
->ostream() << this->name_
;
11644 // Make a selector expression.
11647 Expression::make_selector(Expression
* left
, const std::string
& name
,
11650 return new Selector_expression(left
, name
, location
);
11653 // Class Allocation_expression.
11656 Allocation_expression::do_traverse(Traverse
* traverse
)
11658 return Type::traverse(this->type_
, traverse
);
11662 Allocation_expression::do_type()
11664 return Type::make_pointer_type(this->type_
);
11667 // Make a copy of an allocation expression.
11670 Allocation_expression::do_copy()
11672 Allocation_expression
* alloc
=
11673 new Allocation_expression(this->type_
, this->location());
11674 if (this->allocate_on_stack_
)
11675 alloc
->set_allocate_on_stack();
11679 // Return the backend representation for an allocation expression.
11682 Allocation_expression::do_get_backend(Translate_context
* context
)
11684 Gogo
* gogo
= context
->gogo();
11685 Location loc
= this->location();
11687 Node
* n
= Node::make_node(this);
11688 if (this->allocate_on_stack_
11689 || (n
->encoding() & ESCAPE_MASK
) == int(Node::ESCAPE_NONE
))
11692 bool ok
= this->type_
->backend_type_size(gogo
, &size
);
11695 go_assert(saw_errors());
11696 return gogo
->backend()->error_expression();
11698 return gogo
->backend()->stack_allocation_expression(size
, loc
);
11701 Btype
* btype
= this->type_
->get_backend(gogo
);
11702 Bexpression
* space
=
11703 gogo
->allocate_memory(this->type_
, loc
)->get_backend(context
);
11704 Btype
* pbtype
= gogo
->backend()->pointer_type(btype
);
11705 return gogo
->backend()->convert_expression(pbtype
, space
, loc
);
11708 // Dump ast representation for an allocation expression.
11711 Allocation_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
11714 ast_dump_context
->ostream() << "new(";
11715 ast_dump_context
->dump_type(this->type_
);
11716 ast_dump_context
->ostream() << ")";
11719 // Make an allocation expression.
11722 Expression::make_allocation(Type
* type
, Location location
)
11724 return new Allocation_expression(type
, location
);
11727 // Class Struct_construction_expression.
11732 Struct_construction_expression::do_traverse(Traverse
* traverse
)
11734 if (this->vals_
!= NULL
)
11736 if (this->traverse_order_
== NULL
)
11738 if (this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11739 return TRAVERSE_EXIT
;
11743 for (std::vector
<int>::const_iterator p
=
11744 this->traverse_order_
->begin();
11745 p
!= this->traverse_order_
->end();
11748 if (Expression::traverse(&this->vals_
->at(*p
), traverse
)
11750 return TRAVERSE_EXIT
;
11754 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11755 return TRAVERSE_EXIT
;
11756 return TRAVERSE_CONTINUE
;
11759 // Return whether this is a constant initializer.
11762 Struct_construction_expression::is_constant_struct() const
11764 if (this->vals_
== NULL
)
11766 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11767 pv
!= this->vals_
->end();
11771 && !(*pv
)->is_constant()
11772 && (!(*pv
)->is_composite_literal()
11773 || (*pv
)->is_nonconstant_composite_literal()))
11777 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11778 for (Struct_field_list::const_iterator pf
= fields
->begin();
11779 pf
!= fields
->end();
11782 // There are no constant constructors for interfaces.
11783 if (pf
->type()->interface_type() != NULL
)
11790 // Return whether this struct is immutable.
11793 Struct_construction_expression::do_is_immutable() const
11795 if (this->vals_
== NULL
)
11797 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11798 pv
!= this->vals_
->end();
11801 if (*pv
!= NULL
&& !(*pv
)->is_immutable())
11807 // Final type determination.
11810 Struct_construction_expression::do_determine_type(const Type_context
*)
11812 if (this->vals_
== NULL
)
11814 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11815 Expression_list::const_iterator pv
= this->vals_
->begin();
11816 for (Struct_field_list::const_iterator pf
= fields
->begin();
11817 pf
!= fields
->end();
11820 if (pv
== this->vals_
->end())
11824 Type_context
subcontext(pf
->type(), false);
11825 (*pv
)->determine_type(&subcontext
);
11828 // Extra values are an error we will report elsewhere; we still want
11829 // to determine the type to avoid knockon errors.
11830 for (; pv
!= this->vals_
->end(); ++pv
)
11831 (*pv
)->determine_type_no_context();
11837 Struct_construction_expression::do_check_types(Gogo
*)
11839 if (this->vals_
== NULL
)
11842 Struct_type
* st
= this->type_
->struct_type();
11843 if (this->vals_
->size() > st
->field_count())
11845 this->report_error(_("too many expressions for struct"));
11849 const Struct_field_list
* fields
= st
->fields();
11850 Expression_list::const_iterator pv
= this->vals_
->begin();
11852 for (Struct_field_list::const_iterator pf
= fields
->begin();
11853 pf
!= fields
->end();
11856 if (pv
== this->vals_
->end())
11858 this->report_error(_("too few expressions for struct"));
11865 std::string reason
;
11866 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
11868 if (reason
.empty())
11869 error_at((*pv
)->location(),
11870 "incompatible type for field %d in struct construction",
11873 error_at((*pv
)->location(),
11874 ("incompatible type for field %d in "
11875 "struct construction (%s)"),
11876 i
+ 1, reason
.c_str());
11877 this->set_is_error();
11880 go_assert(pv
== this->vals_
->end());
11883 // Flatten a struct construction expression. Store the values into
11884 // temporaries in case they need interface conversion.
11887 Struct_construction_expression::do_flatten(Gogo
*, Named_object
*,
11888 Statement_inserter
* inserter
)
11890 if (this->vals_
== NULL
)
11893 // If this is a constant struct, we don't need temporaries.
11894 if (this->is_constant_struct())
11897 Location loc
= this->location();
11898 for (Expression_list::iterator pv
= this->vals_
->begin();
11899 pv
!= this->vals_
->end();
11904 if ((*pv
)->is_error_expression() || (*pv
)->type()->is_error_type())
11906 go_assert(saw_errors());
11907 return Expression::make_error(loc
);
11909 if (!(*pv
)->is_variable())
11911 Temporary_statement
* temp
=
11912 Statement::make_temporary(NULL
, *pv
, loc
);
11913 inserter
->insert(temp
);
11914 *pv
= Expression::make_temporary_reference(temp
, loc
);
11921 // Return the backend representation for constructing a struct.
11924 Struct_construction_expression::do_get_backend(Translate_context
* context
)
11926 Gogo
* gogo
= context
->gogo();
11928 Btype
* btype
= this->type_
->get_backend(gogo
);
11929 if (this->vals_
== NULL
)
11930 return gogo
->backend()->zero_expression(btype
);
11932 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11933 Expression_list::const_iterator pv
= this->vals_
->begin();
11934 std::vector
<Bexpression
*> init
;
11935 for (Struct_field_list::const_iterator pf
= fields
->begin();
11936 pf
!= fields
->end();
11939 Btype
* fbtype
= pf
->type()->get_backend(gogo
);
11940 if (pv
== this->vals_
->end())
11941 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
11942 else if (*pv
== NULL
)
11944 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
11950 Expression::convert_for_assignment(gogo
, pf
->type(),
11951 *pv
, this->location());
11952 init
.push_back(val
->get_backend(context
));
11956 return gogo
->backend()->constructor_expression(btype
, init
, this->location());
11959 // Export a struct construction.
11962 Struct_construction_expression::do_export(Export
* exp
) const
11964 exp
->write_c_string("convert(");
11965 exp
->write_type(this->type_
);
11966 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11967 pv
!= this->vals_
->end();
11970 exp
->write_c_string(", ");
11972 (*pv
)->export_expression(exp
);
11974 exp
->write_c_string(")");
11977 // Dump ast representation of a struct construction expression.
11980 Struct_construction_expression::do_dump_expression(
11981 Ast_dump_context
* ast_dump_context
) const
11983 ast_dump_context
->dump_type(this->type_
);
11984 ast_dump_context
->ostream() << "{";
11985 ast_dump_context
->dump_expression_list(this->vals_
);
11986 ast_dump_context
->ostream() << "}";
11989 // Make a struct composite literal. This used by the thunk code.
11992 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
11995 go_assert(type
->struct_type() != NULL
);
11996 return new Struct_construction_expression(type
, vals
, location
);
11999 // Class Array_construction_expression.
12004 Array_construction_expression::do_traverse(Traverse
* traverse
)
12006 if (this->vals_
!= NULL
12007 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
12008 return TRAVERSE_EXIT
;
12009 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12010 return TRAVERSE_EXIT
;
12011 return TRAVERSE_CONTINUE
;
12014 // Return whether this is a constant initializer.
12017 Array_construction_expression::is_constant_array() const
12019 if (this->vals_
== NULL
)
12022 // There are no constant constructors for interfaces.
12023 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
12026 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12027 pv
!= this->vals_
->end();
12031 && !(*pv
)->is_constant()
12032 && (!(*pv
)->is_composite_literal()
12033 || (*pv
)->is_nonconstant_composite_literal()))
12039 // Return whether this is an immutable array initializer.
12042 Array_construction_expression::do_is_immutable() const
12044 if (this->vals_
== NULL
)
12046 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12047 pv
!= this->vals_
->end();
12050 if (*pv
!= NULL
&& !(*pv
)->is_immutable())
12056 // Final type determination.
12059 Array_construction_expression::do_determine_type(const Type_context
*)
12061 if (this->vals_
== NULL
)
12063 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
12064 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12065 pv
!= this->vals_
->end();
12069 (*pv
)->determine_type(&subcontext
);
12076 Array_construction_expression::do_check_types(Gogo
*)
12078 if (this->vals_
== NULL
)
12081 Array_type
* at
= this->type_
->array_type();
12083 Type
* element_type
= at
->element_type();
12084 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12085 pv
!= this->vals_
->end();
12089 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
12091 error_at((*pv
)->location(),
12092 "incompatible type for element %d in composite literal",
12094 this->set_is_error();
12099 // Flatten an array construction expression. Store the values into
12100 // temporaries in case they need interface conversion.
12103 Array_construction_expression::do_flatten(Gogo
*, Named_object
*,
12104 Statement_inserter
* inserter
)
12106 if (this->vals_
== NULL
)
12109 // If this is a constant array, we don't need temporaries.
12110 if (this->is_constant_array())
12113 Location loc
= this->location();
12114 for (Expression_list::iterator pv
= this->vals_
->begin();
12115 pv
!= this->vals_
->end();
12120 if ((*pv
)->is_error_expression() || (*pv
)->type()->is_error_type())
12122 go_assert(saw_errors());
12123 return Expression::make_error(loc
);
12125 if (!(*pv
)->is_variable())
12127 Temporary_statement
* temp
=
12128 Statement::make_temporary(NULL
, *pv
, loc
);
12129 inserter
->insert(temp
);
12130 *pv
= Expression::make_temporary_reference(temp
, loc
);
12137 // Get a constructor expression for the array values.
12140 Array_construction_expression::get_constructor(Translate_context
* context
,
12141 Btype
* array_btype
)
12143 Type
* element_type
= this->type_
->array_type()->element_type();
12145 std::vector
<unsigned long> indexes
;
12146 std::vector
<Bexpression
*> vals
;
12147 Gogo
* gogo
= context
->gogo();
12148 if (this->vals_
!= NULL
)
12151 std::vector
<unsigned long>::const_iterator pi
;
12152 if (this->indexes_
!= NULL
)
12153 pi
= this->indexes_
->begin();
12154 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12155 pv
!= this->vals_
->end();
12158 if (this->indexes_
!= NULL
)
12159 go_assert(pi
!= this->indexes_
->end());
12161 if (this->indexes_
== NULL
)
12162 indexes
.push_back(i
);
12164 indexes
.push_back(*pi
);
12167 Btype
* ebtype
= element_type
->get_backend(gogo
);
12168 Bexpression
*zv
= gogo
->backend()->zero_expression(ebtype
);
12169 vals
.push_back(zv
);
12173 Expression
* val_expr
=
12174 Expression::convert_for_assignment(gogo
, element_type
, *pv
,
12176 vals
.push_back(val_expr
->get_backend(context
));
12178 if (this->indexes_
!= NULL
)
12181 if (this->indexes_
!= NULL
)
12182 go_assert(pi
== this->indexes_
->end());
12184 return gogo
->backend()->array_constructor_expression(array_btype
, indexes
,
12185 vals
, this->location());
12188 // Export an array construction.
12191 Array_construction_expression::do_export(Export
* exp
) const
12193 exp
->write_c_string("convert(");
12194 exp
->write_type(this->type_
);
12195 if (this->vals_
!= NULL
)
12197 std::vector
<unsigned long>::const_iterator pi
;
12198 if (this->indexes_
!= NULL
)
12199 pi
= this->indexes_
->begin();
12200 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12201 pv
!= this->vals_
->end();
12204 exp
->write_c_string(", ");
12206 if (this->indexes_
!= NULL
)
12209 snprintf(buf
, sizeof buf
, "%lu", *pi
);
12210 exp
->write_c_string(buf
);
12211 exp
->write_c_string(":");
12215 (*pv
)->export_expression(exp
);
12217 if (this->indexes_
!= NULL
)
12221 exp
->write_c_string(")");
12224 // Dump ast representation of an array construction expressin.
12227 Array_construction_expression::do_dump_expression(
12228 Ast_dump_context
* ast_dump_context
) const
12230 Expression
* length
= this->type_
->array_type()->length();
12232 ast_dump_context
->ostream() << "[" ;
12233 if (length
!= NULL
)
12235 ast_dump_context
->dump_expression(length
);
12237 ast_dump_context
->ostream() << "]" ;
12238 ast_dump_context
->dump_type(this->type_
);
12239 ast_dump_context
->ostream() << "{" ;
12240 if (this->indexes_
== NULL
)
12241 ast_dump_context
->dump_expression_list(this->vals_
);
12244 Expression_list::const_iterator pv
= this->vals_
->begin();
12245 for (std::vector
<unsigned long>::const_iterator pi
=
12246 this->indexes_
->begin();
12247 pi
!= this->indexes_
->end();
12250 if (pi
!= this->indexes_
->begin())
12251 ast_dump_context
->ostream() << ", ";
12252 ast_dump_context
->ostream() << *pi
<< ':';
12253 ast_dump_context
->dump_expression(*pv
);
12256 ast_dump_context
->ostream() << "}" ;
12260 // Class Fixed_array_construction_expression.
12262 Fixed_array_construction_expression::Fixed_array_construction_expression(
12263 Type
* type
, const std::vector
<unsigned long>* indexes
,
12264 Expression_list
* vals
, Location location
)
12265 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
12266 type
, indexes
, vals
, location
)
12267 { go_assert(type
->array_type() != NULL
&& !type
->is_slice_type()); }
12269 // Return the backend representation for constructing a fixed array.
12272 Fixed_array_construction_expression::do_get_backend(Translate_context
* context
)
12274 Type
* type
= this->type();
12275 Btype
* btype
= type
->get_backend(context
->gogo());
12276 return this->get_constructor(context
, btype
);
12280 Expression::make_array_composite_literal(Type
* type
, Expression_list
* vals
,
12283 go_assert(type
->array_type() != NULL
&& !type
->is_slice_type());
12284 return new Fixed_array_construction_expression(type
, NULL
, vals
, location
);
12287 // Class Slice_construction_expression.
12289 Slice_construction_expression::Slice_construction_expression(
12290 Type
* type
, const std::vector
<unsigned long>* indexes
,
12291 Expression_list
* vals
, Location location
)
12292 : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION
,
12293 type
, indexes
, vals
, location
),
12296 go_assert(type
->is_slice_type());
12298 unsigned long lenval
;
12299 Expression
* length
;
12300 if (vals
== NULL
|| vals
->empty())
12304 if (this->indexes() == NULL
)
12305 lenval
= vals
->size();
12307 lenval
= indexes
->back() + 1;
12309 Type
* int_type
= Type::lookup_integer_type("int");
12310 length
= Expression::make_integer_ul(lenval
, int_type
, location
);
12311 Type
* element_type
= type
->array_type()->element_type();
12312 this->valtype_
= Type::make_array_type(element_type
, length
);
12319 Slice_construction_expression::do_traverse(Traverse
* traverse
)
12321 if (this->Array_construction_expression::do_traverse(traverse
)
12323 return TRAVERSE_EXIT
;
12324 if (Type::traverse(this->valtype_
, traverse
) == TRAVERSE_EXIT
)
12325 return TRAVERSE_EXIT
;
12326 return TRAVERSE_CONTINUE
;
12329 // Return the backend representation for constructing a slice.
12332 Slice_construction_expression::do_get_backend(Translate_context
* context
)
12334 Array_type
* array_type
= this->type()->array_type();
12335 if (array_type
== NULL
)
12337 go_assert(this->type()->is_error());
12338 return context
->backend()->error_expression();
12341 Location loc
= this->location();
12342 Type
* element_type
= array_type
->element_type();
12343 go_assert(this->valtype_
!= NULL
);
12345 Expression_list
* vals
= this->vals();
12346 if (this->vals() == NULL
|| this->vals()->empty())
12348 // We need to create a unique value for the empty array literal.
12349 vals
= new Expression_list
;
12350 vals
->push_back(NULL
);
12352 Expression
* array_val
=
12353 new Fixed_array_construction_expression(this->valtype_
, this->indexes(),
12356 bool is_constant_initializer
= array_val
->is_immutable();
12358 // We have to copy the initial values into heap memory if we are in
12359 // a function or if the values are not constants. We also have to
12360 // copy them if they may contain pointers in a non-constant context,
12361 // as otherwise the garbage collector won't see them.
12362 bool copy_to_heap
= (context
->function() != NULL
12363 || !is_constant_initializer
12364 || (element_type
->has_pointer()
12365 && !context
->is_const()));
12370 // The initializer will only run once.
12371 space
= Expression::make_unary(OPERATOR_AND
, array_val
, loc
);
12372 space
->unary_expression()->set_is_slice_init();
12376 space
= Expression::make_heap_expression(array_val
, loc
);
12377 Node
* n
= Node::make_node(this);
12378 if ((n
->encoding() & ESCAPE_MASK
) == int(Node::ESCAPE_NONE
))
12380 n
= Node::make_node(space
);
12381 n
->set_encoding(Node::ESCAPE_NONE
);
12385 // Build a constructor for the slice.
12387 Expression
* len
= this->valtype_
->array_type()->length();
12388 Expression
* slice_val
=
12389 Expression::make_slice_value(this->type(), space
, len
, len
, loc
);
12390 return slice_val
->get_backend(context
);
12393 // Make a slice composite literal. This is used by the type
12394 // descriptor code.
12397 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
12400 go_assert(type
->is_slice_type());
12401 return new Slice_construction_expression(type
, NULL
, vals
, location
);
12404 // Class Map_construction_expression.
12409 Map_construction_expression::do_traverse(Traverse
* traverse
)
12411 if (this->vals_
!= NULL
12412 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
12413 return TRAVERSE_EXIT
;
12414 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12415 return TRAVERSE_EXIT
;
12416 return TRAVERSE_CONTINUE
;
12419 // Flatten constructor initializer into a temporary variable since
12420 // we need to take its address for __go_construct_map.
12423 Map_construction_expression::do_flatten(Gogo
* gogo
, Named_object
*,
12424 Statement_inserter
* inserter
)
12426 if (!this->is_error_expression()
12427 && this->vals_
!= NULL
12428 && !this->vals_
->empty()
12429 && this->constructor_temp_
== NULL
)
12431 Map_type
* mt
= this->type_
->map_type();
12432 Type
* key_type
= mt
->key_type();
12433 Type
* val_type
= mt
->val_type();
12434 this->element_type_
= Type::make_builtin_struct_type(2,
12436 "__val", val_type
);
12438 Expression_list
* value_pairs
= new Expression_list();
12439 Location loc
= this->location();
12442 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12443 pv
!= this->vals_
->end();
12446 Expression_list
* key_value_pair
= new Expression_list();
12447 Expression
* key
= *pv
;
12448 if (key
->is_error_expression() || key
->type()->is_error_type())
12450 go_assert(saw_errors());
12451 return Expression::make_error(loc
);
12453 if (key
->type()->interface_type() != NULL
&& !key
->is_variable())
12455 Temporary_statement
* temp
=
12456 Statement::make_temporary(NULL
, key
, loc
);
12457 inserter
->insert(temp
);
12458 key
= Expression::make_temporary_reference(temp
, loc
);
12460 key
= Expression::convert_for_assignment(gogo
, key_type
, key
, loc
);
12463 Expression
* val
= *pv
;
12464 if (val
->is_error_expression() || val
->type()->is_error_type())
12466 go_assert(saw_errors());
12467 return Expression::make_error(loc
);
12469 if (val
->type()->interface_type() != NULL
&& !val
->is_variable())
12471 Temporary_statement
* temp
=
12472 Statement::make_temporary(NULL
, val
, loc
);
12473 inserter
->insert(temp
);
12474 val
= Expression::make_temporary_reference(temp
, loc
);
12476 val
= Expression::convert_for_assignment(gogo
, val_type
, val
, loc
);
12478 key_value_pair
->push_back(key
);
12479 key_value_pair
->push_back(val
);
12480 value_pairs
->push_back(
12481 Expression::make_struct_composite_literal(this->element_type_
,
12482 key_value_pair
, loc
));
12485 Expression
* element_count
= Expression::make_integer_ul(i
, NULL
, loc
);
12487 Type::make_array_type(this->element_type_
, element_count
);
12488 Expression
* constructor
=
12489 new Fixed_array_construction_expression(ctor_type
, NULL
,
12492 this->constructor_temp_
=
12493 Statement::make_temporary(NULL
, constructor
, loc
);
12494 constructor
->issue_nil_check();
12495 this->constructor_temp_
->set_is_address_taken();
12496 inserter
->insert(this->constructor_temp_
);
12502 // Final type determination.
12505 Map_construction_expression::do_determine_type(const Type_context
*)
12507 if (this->vals_
== NULL
)
12510 Map_type
* mt
= this->type_
->map_type();
12511 Type_context
key_context(mt
->key_type(), false);
12512 Type_context
val_context(mt
->val_type(), false);
12513 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12514 pv
!= this->vals_
->end();
12517 (*pv
)->determine_type(&key_context
);
12519 (*pv
)->determine_type(&val_context
);
12526 Map_construction_expression::do_check_types(Gogo
*)
12528 if (this->vals_
== NULL
)
12531 Map_type
* mt
= this->type_
->map_type();
12533 Type
* key_type
= mt
->key_type();
12534 Type
* val_type
= mt
->val_type();
12535 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12536 pv
!= this->vals_
->end();
12539 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
12541 error_at((*pv
)->location(),
12542 "incompatible type for element %d key in map construction",
12544 this->set_is_error();
12547 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
12549 error_at((*pv
)->location(),
12550 ("incompatible type for element %d value "
12551 "in map construction"),
12553 this->set_is_error();
12558 // Return the backend representation for constructing a map.
12561 Map_construction_expression::do_get_backend(Translate_context
* context
)
12563 if (this->is_error_expression())
12564 return context
->backend()->error_expression();
12565 Location loc
= this->location();
12568 Expression
* ventries
;
12569 if (this->vals_
== NULL
|| this->vals_
->empty())
12570 ventries
= Expression::make_nil(loc
);
12573 go_assert(this->constructor_temp_
!= NULL
);
12574 i
= this->vals_
->size() / 2;
12576 Expression
* ctor_ref
=
12577 Expression::make_temporary_reference(this->constructor_temp_
, loc
);
12578 ventries
= Expression::make_unary(OPERATOR_AND
, ctor_ref
, loc
);
12581 Map_type
* mt
= this->type_
->map_type();
12582 if (this->element_type_
== NULL
)
12583 this->element_type_
=
12584 Type::make_builtin_struct_type(2,
12585 "__key", mt
->key_type(),
12586 "__val", mt
->val_type());
12587 Expression
* descriptor
= Expression::make_map_descriptor(mt
, loc
);
12589 Type
* uintptr_t = Type::lookup_integer_type("uintptr");
12590 Expression
* count
= Expression::make_integer_ul(i
, uintptr_t, loc
);
12592 Expression
* entry_size
=
12593 Expression::make_type_info(this->element_type_
, TYPE_INFO_SIZE
);
12595 unsigned int field_index
;
12596 const Struct_field
* valfield
=
12597 this->element_type_
->find_local_field("__val", &field_index
);
12598 Expression
* val_offset
=
12599 Expression::make_struct_field_offset(this->element_type_
, valfield
);
12600 Expression
* val_size
=
12601 Expression::make_type_info(mt
->val_type(), TYPE_INFO_SIZE
);
12603 Expression
* map_ctor
=
12604 Runtime::make_call(Runtime::CONSTRUCT_MAP
, loc
, 6, descriptor
, count
,
12605 entry_size
, val_offset
, val_size
, ventries
);
12606 return map_ctor
->get_backend(context
);
12609 // Export an array construction.
12612 Map_construction_expression::do_export(Export
* exp
) const
12614 exp
->write_c_string("convert(");
12615 exp
->write_type(this->type_
);
12616 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12617 pv
!= this->vals_
->end();
12620 exp
->write_c_string(", ");
12621 (*pv
)->export_expression(exp
);
12623 exp
->write_c_string(")");
12626 // Dump ast representation for a map construction expression.
12629 Map_construction_expression::do_dump_expression(
12630 Ast_dump_context
* ast_dump_context
) const
12632 ast_dump_context
->ostream() << "{" ;
12633 ast_dump_context
->dump_expression_list(this->vals_
, true);
12634 ast_dump_context
->ostream() << "}";
12637 // Class Composite_literal_expression.
12642 Composite_literal_expression::do_traverse(Traverse
* traverse
)
12644 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12645 return TRAVERSE_EXIT
;
12647 // If this is a struct composite literal with keys, then the keys
12648 // are field names, not expressions. We don't want to traverse them
12649 // in that case. If we do, we can give an erroneous error "variable
12650 // initializer refers to itself." See bug482.go in the testsuite.
12651 if (this->has_keys_
&& this->vals_
!= NULL
)
12653 // The type may not be resolvable at this point.
12654 Type
* type
= this->type_
;
12656 for (int depth
= 0; depth
< this->depth_
; ++depth
)
12658 if (type
->array_type() != NULL
)
12659 type
= type
->array_type()->element_type();
12660 else if (type
->map_type() != NULL
)
12662 if (this->key_path_
[depth
])
12663 type
= type
->map_type()->key_type();
12665 type
= type
->map_type()->val_type();
12669 // This error will be reported during lowering.
12670 return TRAVERSE_CONTINUE
;
12676 if (type
->classification() == Type::TYPE_NAMED
)
12677 type
= type
->named_type()->real_type();
12678 else if (type
->classification() == Type::TYPE_FORWARD
)
12680 Type
* t
= type
->forwarded();
12689 if (type
->classification() == Type::TYPE_STRUCT
)
12691 Expression_list::iterator p
= this->vals_
->begin();
12692 while (p
!= this->vals_
->end())
12696 go_assert(p
!= this->vals_
->end());
12697 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12698 return TRAVERSE_EXIT
;
12701 return TRAVERSE_CONTINUE
;
12705 if (this->vals_
!= NULL
)
12706 return this->vals_
->traverse(traverse
);
12708 return TRAVERSE_CONTINUE
;
12711 // Lower a generic composite literal into a specific version based on
12715 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
12716 Statement_inserter
* inserter
, int)
12718 Type
* type
= this->type_
;
12720 for (int depth
= 0; depth
< this->depth_
; ++depth
)
12722 if (type
->array_type() != NULL
)
12723 type
= type
->array_type()->element_type();
12724 else if (type
->map_type() != NULL
)
12726 if (this->key_path_
[depth
])
12727 type
= type
->map_type()->key_type();
12729 type
= type
->map_type()->val_type();
12733 if (!type
->is_error())
12734 error_at(this->location(),
12735 ("may only omit types within composite literals "
12736 "of slice, array, or map type"));
12737 return Expression::make_error(this->location());
12741 Type
*pt
= type
->points_to();
12742 bool is_pointer
= false;
12750 if (type
->is_error())
12751 return Expression::make_error(this->location());
12752 else if (type
->struct_type() != NULL
)
12753 ret
= this->lower_struct(gogo
, type
);
12754 else if (type
->array_type() != NULL
)
12755 ret
= this->lower_array(type
);
12756 else if (type
->map_type() != NULL
)
12757 ret
= this->lower_map(gogo
, function
, inserter
, type
);
12760 error_at(this->location(),
12761 ("expected struct, slice, array, or map type "
12762 "for composite literal"));
12763 return Expression::make_error(this->location());
12767 ret
= Expression::make_heap_expression(ret
, this->location());
12772 // Lower a struct composite literal.
12775 Composite_literal_expression::lower_struct(Gogo
* gogo
, Type
* type
)
12777 Location location
= this->location();
12778 Struct_type
* st
= type
->struct_type();
12779 if (this->vals_
== NULL
|| !this->has_keys_
)
12781 if (this->vals_
!= NULL
12782 && !this->vals_
->empty()
12783 && type
->named_type() != NULL
12784 && type
->named_type()->named_object()->package() != NULL
)
12786 for (Struct_field_list::const_iterator pf
= st
->fields()->begin();
12787 pf
!= st
->fields()->end();
12790 if (Gogo::is_hidden_name(pf
->field_name())
12791 || pf
->is_embedded_builtin(gogo
))
12792 error_at(this->location(),
12793 "assignment of unexported field %qs in %qs literal",
12794 Gogo::message_name(pf
->field_name()).c_str(),
12795 type
->named_type()->message_name().c_str());
12799 return new Struct_construction_expression(type
, this->vals_
, location
);
12802 size_t field_count
= st
->field_count();
12803 std::vector
<Expression
*> vals(field_count
);
12804 std::vector
<int>* traverse_order
= new(std::vector
<int>);
12805 Expression_list::const_iterator p
= this->vals_
->begin();
12806 Expression
* external_expr
= NULL
;
12807 const Named_object
* external_no
= NULL
;
12808 while (p
!= this->vals_
->end())
12810 Expression
* name_expr
= *p
;
12813 go_assert(p
!= this->vals_
->end());
12814 Expression
* val
= *p
;
12818 if (name_expr
== NULL
)
12820 error_at(val
->location(), "mixture of field and value initializers");
12821 return Expression::make_error(location
);
12824 bool bad_key
= false;
12826 const Named_object
* no
= NULL
;
12827 switch (name_expr
->classification())
12829 case EXPRESSION_UNKNOWN_REFERENCE
:
12830 name
= name_expr
->unknown_expression()->name();
12831 if (type
->named_type() != NULL
)
12833 // If the named object found for this field name comes from a
12834 // different package than the struct it is a part of, do not count
12835 // this incorrect lookup as a usage of the object's package.
12836 no
= name_expr
->unknown_expression()->named_object();
12837 if (no
->package() != NULL
12838 && no
->package() != type
->named_type()->named_object()->package())
12839 no
->package()->forget_usage(name_expr
);
12843 case EXPRESSION_CONST_REFERENCE
:
12844 no
= static_cast<Const_expression
*>(name_expr
)->named_object();
12847 case EXPRESSION_TYPE
:
12849 Type
* t
= name_expr
->type();
12850 Named_type
* nt
= t
->named_type();
12854 no
= nt
->named_object();
12858 case EXPRESSION_VAR_REFERENCE
:
12859 no
= name_expr
->var_expression()->named_object();
12862 case EXPRESSION_ENCLOSED_VAR_REFERENCE
:
12863 no
= name_expr
->enclosed_var_expression()->variable();
12866 case EXPRESSION_FUNC_REFERENCE
:
12867 no
= name_expr
->func_expression()->named_object();
12876 error_at(name_expr
->location(), "expected struct field name");
12877 return Expression::make_error(location
);
12882 if (no
->package() != NULL
&& external_expr
== NULL
)
12884 external_expr
= name_expr
;
12890 // A predefined name won't be packed. If it starts with a
12891 // lower case letter we need to check for that case, because
12892 // the field name will be packed. FIXME.
12893 if (!Gogo::is_hidden_name(name
)
12897 Named_object
* gno
= gogo
->lookup_global(name
.c_str());
12899 name
= gogo
->pack_hidden_name(name
, false);
12903 unsigned int index
;
12904 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
12907 error_at(name_expr
->location(), "unknown field %qs in %qs",
12908 Gogo::message_name(name
).c_str(),
12909 (type
->named_type() != NULL
12910 ? type
->named_type()->message_name().c_str()
12911 : "unnamed struct"));
12912 return Expression::make_error(location
);
12914 if (vals
[index
] != NULL
)
12916 error_at(name_expr
->location(),
12917 "duplicate value for field %qs in %qs",
12918 Gogo::message_name(name
).c_str(),
12919 (type
->named_type() != NULL
12920 ? type
->named_type()->message_name().c_str()
12921 : "unnamed struct"));
12922 return Expression::make_error(location
);
12925 if (type
->named_type() != NULL
12926 && type
->named_type()->named_object()->package() != NULL
12927 && (Gogo::is_hidden_name(sf
->field_name())
12928 || sf
->is_embedded_builtin(gogo
)))
12929 error_at(name_expr
->location(),
12930 "assignment of unexported field %qs in %qs literal",
12931 Gogo::message_name(sf
->field_name()).c_str(),
12932 type
->named_type()->message_name().c_str());
12935 traverse_order
->push_back(index
);
12938 if (!this->all_are_names_
)
12940 // This is a weird case like bug462 in the testsuite.
12941 if (external_expr
== NULL
)
12942 error_at(this->location(), "unknown field in %qs literal",
12943 (type
->named_type() != NULL
12944 ? type
->named_type()->message_name().c_str()
12945 : "unnamed struct"));
12947 error_at(external_expr
->location(), "unknown field %qs in %qs",
12948 external_no
->message_name().c_str(),
12949 (type
->named_type() != NULL
12950 ? type
->named_type()->message_name().c_str()
12951 : "unnamed struct"));
12952 return Expression::make_error(location
);
12955 Expression_list
* list
= new Expression_list
;
12956 list
->reserve(field_count
);
12957 for (size_t i
= 0; i
< field_count
; ++i
)
12958 list
->push_back(vals
[i
]);
12960 Struct_construction_expression
* ret
=
12961 new Struct_construction_expression(type
, list
, location
);
12962 ret
->set_traverse_order(traverse_order
);
12966 // Used to sort an index/value array.
12968 class Index_value_compare
12972 operator()(const std::pair
<unsigned long, Expression
*>& a
,
12973 const std::pair
<unsigned long, Expression
*>& b
)
12974 { return a
.first
< b
.first
; }
12977 // Lower an array composite literal.
12980 Composite_literal_expression::lower_array(Type
* type
)
12982 Location location
= this->location();
12983 if (this->vals_
== NULL
|| !this->has_keys_
)
12984 return this->make_array(type
, NULL
, this->vals_
);
12986 std::vector
<unsigned long>* indexes
= new std::vector
<unsigned long>;
12987 indexes
->reserve(this->vals_
->size());
12988 bool indexes_out_of_order
= false;
12989 Expression_list
* vals
= new Expression_list();
12990 vals
->reserve(this->vals_
->size());
12991 unsigned long index
= 0;
12992 Expression_list::const_iterator p
= this->vals_
->begin();
12993 while (p
!= this->vals_
->end())
12995 Expression
* index_expr
= *p
;
12998 go_assert(p
!= this->vals_
->end());
12999 Expression
* val
= *p
;
13003 if (index_expr
== NULL
)
13005 if (!indexes
->empty())
13006 indexes
->push_back(index
);
13010 if (indexes
->empty() && !vals
->empty())
13012 for (size_t i
= 0; i
< vals
->size(); ++i
)
13013 indexes
->push_back(i
);
13016 Numeric_constant nc
;
13017 if (!index_expr
->numeric_constant_value(&nc
))
13019 error_at(index_expr
->location(),
13020 "index expression is not integer constant");
13021 return Expression::make_error(location
);
13024 switch (nc
.to_unsigned_long(&index
))
13026 case Numeric_constant::NC_UL_VALID
:
13028 case Numeric_constant::NC_UL_NOTINT
:
13029 error_at(index_expr
->location(),
13030 "index expression is not integer constant");
13031 return Expression::make_error(location
);
13032 case Numeric_constant::NC_UL_NEGATIVE
:
13033 error_at(index_expr
->location(), "index expression is negative");
13034 return Expression::make_error(location
);
13035 case Numeric_constant::NC_UL_BIG
:
13036 error_at(index_expr
->location(), "index value overflow");
13037 return Expression::make_error(location
);
13042 Named_type
* ntype
= Type::lookup_integer_type("int");
13043 Integer_type
* inttype
= ntype
->integer_type();
13044 if (sizeof(index
) <= static_cast<size_t>(inttype
->bits() * 8)
13045 && index
>> (inttype
->bits() - 1) != 0)
13047 error_at(index_expr
->location(), "index value overflow");
13048 return Expression::make_error(location
);
13051 if (std::find(indexes
->begin(), indexes
->end(), index
)
13054 error_at(index_expr
->location(), "duplicate value for index %lu",
13056 return Expression::make_error(location
);
13059 if (!indexes
->empty() && index
< indexes
->back())
13060 indexes_out_of_order
= true;
13062 indexes
->push_back(index
);
13065 vals
->push_back(val
);
13070 if (indexes
->empty())
13076 if (indexes_out_of_order
)
13078 typedef std::vector
<std::pair
<unsigned long, Expression
*> > V
;
13081 v
.reserve(indexes
->size());
13082 std::vector
<unsigned long>::const_iterator pi
= indexes
->begin();
13083 for (Expression_list::const_iterator pe
= vals
->begin();
13086 v
.push_back(std::make_pair(*pi
, *pe
));
13088 std::sort(v
.begin(), v
.end(), Index_value_compare());
13092 indexes
= new std::vector
<unsigned long>();
13093 indexes
->reserve(v
.size());
13094 vals
= new Expression_list();
13095 vals
->reserve(v
.size());
13097 for (V::const_iterator p
= v
.begin(); p
!= v
.end(); ++p
)
13099 indexes
->push_back(p
->first
);
13100 vals
->push_back(p
->second
);
13104 return this->make_array(type
, indexes
, vals
);
13107 // Actually build the array composite literal. This handles
13111 Composite_literal_expression::make_array(
13113 const std::vector
<unsigned long>* indexes
,
13114 Expression_list
* vals
)
13116 Location location
= this->location();
13117 Array_type
* at
= type
->array_type();
13119 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
13124 else if (indexes
!= NULL
)
13125 size
= indexes
->back() + 1;
13128 size
= vals
->size();
13129 Integer_type
* it
= Type::lookup_integer_type("int")->integer_type();
13130 if (sizeof(size
) <= static_cast<size_t>(it
->bits() * 8)
13131 && size
>> (it
->bits() - 1) != 0)
13133 error_at(location
, "too many elements in composite literal");
13134 return Expression::make_error(location
);
13138 Expression
* elen
= Expression::make_integer_ul(size
, NULL
, location
);
13139 at
= Type::make_array_type(at
->element_type(), elen
);
13142 else if (at
->length() != NULL
13143 && !at
->length()->is_error_expression()
13144 && this->vals_
!= NULL
)
13146 Numeric_constant nc
;
13148 if (at
->length()->numeric_constant_value(&nc
)
13149 && nc
.to_unsigned_long(&val
) == Numeric_constant::NC_UL_VALID
)
13151 if (indexes
== NULL
)
13153 if (this->vals_
->size() > val
)
13155 error_at(location
, "too many elements in composite literal");
13156 return Expression::make_error(location
);
13161 unsigned long max
= indexes
->back();
13165 ("some element keys in composite literal "
13166 "are out of range"));
13167 return Expression::make_error(location
);
13173 if (at
->length() != NULL
)
13174 return new Fixed_array_construction_expression(type
, indexes
, vals
,
13177 return new Slice_construction_expression(type
, indexes
, vals
, location
);
13180 // Lower a map composite literal.
13183 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
13184 Statement_inserter
* inserter
,
13187 Location location
= this->location();
13188 if (this->vals_
!= NULL
)
13190 if (!this->has_keys_
)
13192 error_at(location
, "map composite literal must have keys");
13193 return Expression::make_error(location
);
13196 for (Expression_list::iterator p
= this->vals_
->begin();
13197 p
!= this->vals_
->end();
13203 error_at((*p
)->location(),
13204 "map composite literal must have keys for every value");
13205 return Expression::make_error(location
);
13207 // Make sure we have lowered the key; it may not have been
13208 // lowered in order to handle keys for struct composite
13209 // literals. Lower it now to get the right error message.
13210 if ((*p
)->unknown_expression() != NULL
)
13212 (*p
)->unknown_expression()->clear_is_composite_literal_key();
13213 gogo
->lower_expression(function
, inserter
, &*p
);
13214 go_assert((*p
)->is_error_expression());
13215 return Expression::make_error(location
);
13220 return new Map_construction_expression(type
, this->vals_
, location
);
13223 // Dump ast representation for a composite literal expression.
13226 Composite_literal_expression::do_dump_expression(
13227 Ast_dump_context
* ast_dump_context
) const
13229 ast_dump_context
->ostream() << "composite(";
13230 ast_dump_context
->dump_type(this->type_
);
13231 ast_dump_context
->ostream() << ", {";
13232 ast_dump_context
->dump_expression_list(this->vals_
, this->has_keys_
);
13233 ast_dump_context
->ostream() << "})";
13236 // Make a composite literal expression.
13239 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
13240 Expression_list
* vals
, bool all_are_names
,
13243 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
13244 all_are_names
, location
);
13247 // Return whether this expression is a composite literal.
13250 Expression::is_composite_literal() const
13252 switch (this->classification_
)
13254 case EXPRESSION_COMPOSITE_LITERAL
:
13255 case EXPRESSION_STRUCT_CONSTRUCTION
:
13256 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
13257 case EXPRESSION_SLICE_CONSTRUCTION
:
13258 case EXPRESSION_MAP_CONSTRUCTION
:
13265 // Return whether this expression is a composite literal which is not
13269 Expression::is_nonconstant_composite_literal() const
13271 switch (this->classification_
)
13273 case EXPRESSION_STRUCT_CONSTRUCTION
:
13275 const Struct_construction_expression
*psce
=
13276 static_cast<const Struct_construction_expression
*>(this);
13277 return !psce
->is_constant_struct();
13279 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
13281 const Fixed_array_construction_expression
*pace
=
13282 static_cast<const Fixed_array_construction_expression
*>(this);
13283 return !pace
->is_constant_array();
13285 case EXPRESSION_SLICE_CONSTRUCTION
:
13287 const Slice_construction_expression
*pace
=
13288 static_cast<const Slice_construction_expression
*>(this);
13289 return !pace
->is_constant_array();
13291 case EXPRESSION_MAP_CONSTRUCTION
:
13298 // Return true if this is a variable or temporary_variable.
13301 Expression::is_variable() const
13303 switch (this->classification_
)
13305 case EXPRESSION_VAR_REFERENCE
:
13306 case EXPRESSION_TEMPORARY_REFERENCE
:
13307 case EXPRESSION_SET_AND_USE_TEMPORARY
:
13308 case EXPRESSION_ENCLOSED_VAR_REFERENCE
:
13315 // Return true if this is a reference to a local variable.
13318 Expression::is_local_variable() const
13320 const Var_expression
* ve
= this->var_expression();
13323 const Named_object
* no
= ve
->named_object();
13324 return (no
->is_result_variable()
13325 || (no
->is_variable() && !no
->var_value()->is_global()));
13328 // Class Type_guard_expression.
13333 Type_guard_expression::do_traverse(Traverse
* traverse
)
13335 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
13336 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
13337 return TRAVERSE_EXIT
;
13338 return TRAVERSE_CONTINUE
;
13342 Type_guard_expression::do_flatten(Gogo
*, Named_object
*,
13343 Statement_inserter
* inserter
)
13345 if (this->expr_
->is_error_expression()
13346 || this->expr_
->type()->is_error_type())
13348 go_assert(saw_errors());
13349 return Expression::make_error(this->location());
13352 if (!this->expr_
->is_variable())
13354 Temporary_statement
* temp
= Statement::make_temporary(NULL
, this->expr_
,
13356 inserter
->insert(temp
);
13358 Expression::make_temporary_reference(temp
, this->location());
13363 // Check types of a type guard expression. The expression must have
13364 // an interface type, but the actual type conversion is checked at run
13368 Type_guard_expression::do_check_types(Gogo
*)
13370 Type
* expr_type
= this->expr_
->type();
13371 if (expr_type
->interface_type() == NULL
)
13373 if (!expr_type
->is_error() && !this->type_
->is_error())
13374 this->report_error(_("type assertion only valid for interface types"));
13375 this->set_is_error();
13377 else if (this->type_
->interface_type() == NULL
)
13379 std::string reason
;
13380 if (!expr_type
->interface_type()->implements_interface(this->type_
,
13383 if (!this->type_
->is_error())
13385 if (reason
.empty())
13386 this->report_error(_("impossible type assertion: "
13387 "type does not implement interface"));
13389 error_at(this->location(),
13390 ("impossible type assertion: "
13391 "type does not implement interface (%s)"),
13394 this->set_is_error();
13399 // Return the backend representation for a type guard expression.
13402 Type_guard_expression::do_get_backend(Translate_context
* context
)
13404 Expression
* conversion
;
13405 if (this->type_
->interface_type() != NULL
)
13407 Expression::convert_interface_to_interface(this->type_
, this->expr_
,
13408 true, this->location());
13411 Expression::convert_for_assignment(context
->gogo(), this->type_
,
13412 this->expr_
, this->location());
13414 return conversion
->get_backend(context
);
13417 // Dump ast representation for a type guard expression.
13420 Type_guard_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
13423 this->expr_
->dump_expression(ast_dump_context
);
13424 ast_dump_context
->ostream() << ".";
13425 ast_dump_context
->dump_type(this->type_
);
13428 // Make a type guard expression.
13431 Expression::make_type_guard(Expression
* expr
, Type
* type
,
13434 return new Type_guard_expression(expr
, type
, location
);
13437 // Class Heap_expression.
13439 // Return the type of the expression stored on the heap.
13442 Heap_expression::do_type()
13443 { return Type::make_pointer_type(this->expr_
->type()); }
13445 // Return the backend representation for allocating an expression on the heap.
13448 Heap_expression::do_get_backend(Translate_context
* context
)
13450 if (this->expr_
->is_error_expression() || this->expr_
->type()->is_error())
13451 return context
->backend()->error_expression();
13453 Location loc
= this->location();
13454 Gogo
* gogo
= context
->gogo();
13455 Btype
* btype
= this->type()->get_backend(gogo
);
13457 Expression
* alloc
= Expression::make_allocation(this->expr_
->type(), loc
);
13458 Node
* n
= Node::make_node(this);
13459 if ((n
->encoding() & ESCAPE_MASK
) == int(Node::ESCAPE_NONE
))
13460 alloc
->allocation_expression()->set_allocate_on_stack();
13461 Bexpression
* space
= alloc
->get_backend(context
);
13464 Named_object
* fn
= context
->function();
13465 go_assert(fn
!= NULL
);
13466 Bfunction
* fndecl
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
13467 Bvariable
* space_temp
=
13468 gogo
->backend()->temporary_variable(fndecl
, context
->bblock(), btype
,
13469 space
, true, loc
, &decl
);
13470 space
= gogo
->backend()->var_expression(space_temp
, loc
);
13471 Btype
* expr_btype
= this->expr_
->type()->get_backend(gogo
);
13473 gogo
->backend()->indirect_expression(expr_btype
, space
, true, loc
);
13475 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
13476 Bstatement
* assn
= gogo
->backend()->assignment_statement(ref
, bexpr
, loc
);
13477 decl
= gogo
->backend()->compound_statement(decl
, assn
);
13478 space
= gogo
->backend()->var_expression(space_temp
, loc
);
13479 return gogo
->backend()->compound_expression(decl
, space
, loc
);
13482 // Dump ast representation for a heap expression.
13485 Heap_expression::do_dump_expression(
13486 Ast_dump_context
* ast_dump_context
) const
13488 ast_dump_context
->ostream() << "&(";
13489 ast_dump_context
->dump_expression(this->expr_
);
13490 ast_dump_context
->ostream() << ")";
13493 // Allocate an expression on the heap.
13496 Expression::make_heap_expression(Expression
* expr
, Location location
)
13498 return new Heap_expression(expr
, location
);
13501 // Class Receive_expression.
13503 // Return the type of a receive expression.
13506 Receive_expression::do_type()
13508 if (this->is_error_expression())
13509 return Type::make_error_type();
13510 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
13511 if (channel_type
== NULL
)
13513 this->report_error(_("expected channel"));
13514 return Type::make_error_type();
13516 return channel_type
->element_type();
13519 // Check types for a receive expression.
13522 Receive_expression::do_check_types(Gogo
*)
13524 Type
* type
= this->channel_
->type();
13525 if (type
->is_error())
13527 go_assert(saw_errors());
13528 this->set_is_error();
13531 if (type
->channel_type() == NULL
)
13533 this->report_error(_("expected channel"));
13536 if (!type
->channel_type()->may_receive())
13538 this->report_error(_("invalid receive on send-only channel"));
13543 // Flattening for receive expressions creates a temporary variable to store
13544 // received data in for receives.
13547 Receive_expression::do_flatten(Gogo
*, Named_object
*,
13548 Statement_inserter
* inserter
)
13550 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
13551 if (channel_type
== NULL
)
13553 go_assert(saw_errors());
13556 else if (this->channel_
->is_error_expression())
13558 go_assert(saw_errors());
13559 return Expression::make_error(this->location());
13562 Type
* element_type
= channel_type
->element_type();
13563 if (this->temp_receiver_
== NULL
)
13565 this->temp_receiver_
= Statement::make_temporary(element_type
, NULL
,
13567 this->temp_receiver_
->set_is_address_taken();
13568 inserter
->insert(this->temp_receiver_
);
13574 // Get the backend representation for a receive expression.
13577 Receive_expression::do_get_backend(Translate_context
* context
)
13579 Location loc
= this->location();
13581 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
13582 if (channel_type
== NULL
)
13584 go_assert(this->channel_
->type()->is_error());
13585 return context
->backend()->error_expression();
13587 Expression
* td
= Expression::make_type_descriptor(channel_type
, loc
);
13589 Expression
* recv_ref
=
13590 Expression::make_temporary_reference(this->temp_receiver_
, loc
);
13591 Expression
* recv_addr
=
13592 Expression::make_temporary_reference(this->temp_receiver_
, loc
);
13593 recv_addr
= Expression::make_unary(OPERATOR_AND
, recv_addr
, loc
);
13595 Runtime::make_call(Runtime::RECEIVE
, loc
, 3,
13596 td
, this->channel_
, recv_addr
);
13597 return Expression::make_compound(recv
, recv_ref
, loc
)->get_backend(context
);
13600 // Dump ast representation for a receive expression.
13603 Receive_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
13605 ast_dump_context
->ostream() << " <- " ;
13606 ast_dump_context
->dump_expression(channel_
);
13609 // Make a receive expression.
13611 Receive_expression
*
13612 Expression::make_receive(Expression
* channel
, Location location
)
13614 return new Receive_expression(channel
, location
);
13617 // An expression which evaluates to a pointer to the type descriptor
13620 class Type_descriptor_expression
: public Expression
13623 Type_descriptor_expression(Type
* type
, Location location
)
13624 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
13630 do_traverse(Traverse
*);
13634 { return Type::make_type_descriptor_ptr_type(); }
13637 do_is_immutable() const
13641 do_determine_type(const Type_context
*)
13649 do_get_backend(Translate_context
* context
)
13651 return this->type_
->type_descriptor_pointer(context
->gogo(),
13656 do_dump_expression(Ast_dump_context
*) const;
13659 // The type for which this is the descriptor.
13664 Type_descriptor_expression::do_traverse(Traverse
* traverse
)
13666 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
13667 return TRAVERSE_EXIT
;
13668 return TRAVERSE_CONTINUE
;
13671 // Dump ast representation for a type descriptor expression.
13674 Type_descriptor_expression::do_dump_expression(
13675 Ast_dump_context
* ast_dump_context
) const
13677 ast_dump_context
->dump_type(this->type_
);
13680 // Make a type descriptor expression.
13683 Expression::make_type_descriptor(Type
* type
, Location location
)
13685 return new Type_descriptor_expression(type
, location
);
13688 // An expression which evaluates to a pointer to the Garbage Collection symbol
13691 class GC_symbol_expression
: public Expression
13694 GC_symbol_expression(Type
* type
)
13695 : Expression(EXPRESSION_GC_SYMBOL
, Linemap::predeclared_location()),
13702 { return Type::lookup_integer_type("uintptr"); }
13705 do_is_immutable() const
13709 do_determine_type(const Type_context
*)
13717 do_get_backend(Translate_context
* context
)
13718 { return this->type_
->gc_symbol_pointer(context
->gogo()); }
13721 do_dump_expression(Ast_dump_context
*) const;
13724 // The type which this gc symbol describes.
13728 // Dump ast representation for a gc symbol expression.
13731 GC_symbol_expression::do_dump_expression(
13732 Ast_dump_context
* ast_dump_context
) const
13734 ast_dump_context
->ostream() << "gcdata(";
13735 ast_dump_context
->dump_type(this->type_
);
13736 ast_dump_context
->ostream() << ")";
13739 // Make a gc symbol expression.
13742 Expression::make_gc_symbol(Type
* type
)
13744 return new GC_symbol_expression(type
);
13747 // An expression which evaluates to some characteristic of a type.
13748 // This is only used to initialize fields of a type descriptor. Using
13749 // a new expression class is slightly inefficient but gives us a good
13750 // separation between the frontend and the middle-end with regard to
13751 // how types are laid out.
13753 class Type_info_expression
: public Expression
13756 Type_info_expression(Type
* type
, Type_info type_info
)
13757 : Expression(EXPRESSION_TYPE_INFO
, Linemap::predeclared_location()),
13758 type_(type
), type_info_(type_info
)
13763 do_is_immutable() const
13770 do_determine_type(const Type_context
*)
13778 do_get_backend(Translate_context
* context
);
13781 do_dump_expression(Ast_dump_context
*) const;
13784 // The type for which we are getting information.
13786 // What information we want.
13787 Type_info type_info_
;
13790 // The type is chosen to match what the type descriptor struct
13794 Type_info_expression::do_type()
13796 switch (this->type_info_
)
13798 case TYPE_INFO_SIZE
:
13799 return Type::lookup_integer_type("uintptr");
13800 case TYPE_INFO_ALIGNMENT
:
13801 case TYPE_INFO_FIELD_ALIGNMENT
:
13802 return Type::lookup_integer_type("uint8");
13808 // Return the backend representation for type information.
13811 Type_info_expression::do_get_backend(Translate_context
* context
)
13813 Gogo
* gogo
= context
->gogo();
13816 switch (this->type_info_
)
13818 case TYPE_INFO_SIZE
:
13819 ok
= this->type_
->backend_type_size(gogo
, &val
);
13821 case TYPE_INFO_ALIGNMENT
:
13822 ok
= this->type_
->backend_type_align(gogo
, &val
);
13824 case TYPE_INFO_FIELD_ALIGNMENT
:
13825 ok
= this->type_
->backend_type_field_align(gogo
, &val
);
13832 go_assert(saw_errors());
13833 return gogo
->backend()->error_expression();
13835 Expression
* e
= Expression::make_integer_int64(val
, this->type(),
13837 return e
->get_backend(context
);
13840 // Dump ast representation for a type info expression.
13843 Type_info_expression::do_dump_expression(
13844 Ast_dump_context
* ast_dump_context
) const
13846 ast_dump_context
->ostream() << "typeinfo(";
13847 ast_dump_context
->dump_type(this->type_
);
13848 ast_dump_context
->ostream() << ",";
13849 ast_dump_context
->ostream() <<
13850 (this->type_info_
== TYPE_INFO_ALIGNMENT
? "alignment"
13851 : this->type_info_
== TYPE_INFO_FIELD_ALIGNMENT
? "field alignment"
13852 : this->type_info_
== TYPE_INFO_SIZE
? "size "
13854 ast_dump_context
->ostream() << ")";
13857 // Make a type info expression.
13860 Expression::make_type_info(Type
* type
, Type_info type_info
)
13862 return new Type_info_expression(type
, type_info
);
13865 // An expression that evaluates to some characteristic of a slice.
13866 // This is used when indexing, bound-checking, or nil checking a slice.
13868 class Slice_info_expression
: public Expression
13871 Slice_info_expression(Expression
* slice
, Slice_info slice_info
,
13873 : Expression(EXPRESSION_SLICE_INFO
, location
),
13874 slice_(slice
), slice_info_(slice_info
)
13882 do_determine_type(const Type_context
*)
13888 return new Slice_info_expression(this->slice_
->copy(), this->slice_info_
,
13893 do_get_backend(Translate_context
* context
);
13896 do_dump_expression(Ast_dump_context
*) const;
13899 do_issue_nil_check()
13900 { this->slice_
->issue_nil_check(); }
13903 // The slice for which we are getting information.
13904 Expression
* slice_
;
13905 // What information we want.
13906 Slice_info slice_info_
;
13909 // Return the type of the slice info.
13912 Slice_info_expression::do_type()
13914 switch (this->slice_info_
)
13916 case SLICE_INFO_VALUE_POINTER
:
13917 return Type::make_pointer_type(
13918 this->slice_
->type()->array_type()->element_type());
13919 case SLICE_INFO_LENGTH
:
13920 case SLICE_INFO_CAPACITY
:
13921 return Type::lookup_integer_type("int");
13927 // Return the backend information for slice information.
13930 Slice_info_expression::do_get_backend(Translate_context
* context
)
13932 Gogo
* gogo
= context
->gogo();
13933 Bexpression
* bslice
= this->slice_
->get_backend(context
);
13934 switch (this->slice_info_
)
13936 case SLICE_INFO_VALUE_POINTER
:
13937 case SLICE_INFO_LENGTH
:
13938 case SLICE_INFO_CAPACITY
:
13939 return gogo
->backend()->struct_field_expression(bslice
, this->slice_info_
,
13947 // Dump ast representation for a type info expression.
13950 Slice_info_expression::do_dump_expression(
13951 Ast_dump_context
* ast_dump_context
) const
13953 ast_dump_context
->ostream() << "sliceinfo(";
13954 this->slice_
->dump_expression(ast_dump_context
);
13955 ast_dump_context
->ostream() << ",";
13956 ast_dump_context
->ostream() <<
13957 (this->slice_info_
== SLICE_INFO_VALUE_POINTER
? "values"
13958 : this->slice_info_
== SLICE_INFO_LENGTH
? "length"
13959 : this->slice_info_
== SLICE_INFO_CAPACITY
? "capacity "
13961 ast_dump_context
->ostream() << ")";
13964 // Make a slice info expression.
13967 Expression::make_slice_info(Expression
* slice
, Slice_info slice_info
,
13970 return new Slice_info_expression(slice
, slice_info
, location
);
13973 // An expression that represents a slice value: a struct with value pointer,
13974 // length, and capacity fields.
13976 class Slice_value_expression
: public Expression
13979 Slice_value_expression(Type
* type
, Expression
* valptr
, Expression
* len
,
13980 Expression
* cap
, Location location
)
13981 : Expression(EXPRESSION_SLICE_VALUE
, location
),
13982 type_(type
), valptr_(valptr
), len_(len
), cap_(cap
)
13987 do_traverse(Traverse
*);
13991 { return this->type_
; }
13994 do_determine_type(const Type_context
*)
13995 { go_unreachable(); }
14000 return new Slice_value_expression(this->type_
, this->valptr_
->copy(),
14001 this->len_
->copy(), this->cap_
->copy(),
14006 do_get_backend(Translate_context
* context
);
14009 do_dump_expression(Ast_dump_context
*) const;
14012 // The type of the slice value.
14014 // The pointer to the values in the slice.
14015 Expression
* valptr_
;
14016 // The length of the slice.
14018 // The capacity of the slice.
14023 Slice_value_expression::do_traverse(Traverse
* traverse
)
14025 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
14026 || Expression::traverse(&this->valptr_
, traverse
) == TRAVERSE_EXIT
14027 || Expression::traverse(&this->len_
, traverse
) == TRAVERSE_EXIT
14028 || Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
)
14029 return TRAVERSE_EXIT
;
14030 return TRAVERSE_CONTINUE
;
14034 Slice_value_expression::do_get_backend(Translate_context
* context
)
14036 std::vector
<Bexpression
*> vals(3);
14037 vals
[0] = this->valptr_
->get_backend(context
);
14038 vals
[1] = this->len_
->get_backend(context
);
14039 vals
[2] = this->cap_
->get_backend(context
);
14041 Gogo
* gogo
= context
->gogo();
14042 Btype
* btype
= this->type_
->get_backend(gogo
);
14043 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
14047 Slice_value_expression::do_dump_expression(
14048 Ast_dump_context
* ast_dump_context
) const
14050 ast_dump_context
->ostream() << "slicevalue(";
14051 ast_dump_context
->ostream() << "values: ";
14052 this->valptr_
->dump_expression(ast_dump_context
);
14053 ast_dump_context
->ostream() << ", length: ";
14054 this->len_
->dump_expression(ast_dump_context
);
14055 ast_dump_context
->ostream() << ", capacity: ";
14056 this->cap_
->dump_expression(ast_dump_context
);
14057 ast_dump_context
->ostream() << ")";
14061 Expression::make_slice_value(Type
* at
, Expression
* valptr
, Expression
* len
,
14062 Expression
* cap
, Location location
)
14064 go_assert(at
->is_slice_type());
14065 return new Slice_value_expression(at
, valptr
, len
, cap
, location
);
14068 // An expression that evaluates to some characteristic of a non-empty interface.
14069 // This is used to access the method table or underlying object of an interface.
14071 class Interface_info_expression
: public Expression
14074 Interface_info_expression(Expression
* iface
, Interface_info iface_info
,
14076 : Expression(EXPRESSION_INTERFACE_INFO
, location
),
14077 iface_(iface
), iface_info_(iface_info
)
14085 do_determine_type(const Type_context
*)
14091 return new Interface_info_expression(this->iface_
->copy(),
14092 this->iface_info_
, this->location());
14096 do_get_backend(Translate_context
* context
);
14099 do_dump_expression(Ast_dump_context
*) const;
14102 do_issue_nil_check()
14103 { this->iface_
->issue_nil_check(); }
14106 // The interface for which we are getting information.
14107 Expression
* iface_
;
14108 // What information we want.
14109 Interface_info iface_info_
;
14112 // Return the type of the interface info.
14115 Interface_info_expression::do_type()
14117 switch (this->iface_info_
)
14119 case INTERFACE_INFO_METHODS
:
14121 typedef Unordered_map(Interface_type
*, Type
*) Hashtable
;
14122 static Hashtable result_types
;
14124 Interface_type
* itype
= this->iface_
->type()->interface_type();
14126 Hashtable::const_iterator p
= result_types
.find(itype
);
14127 if (p
!= result_types
.end())
14130 Type
* pdt
= Type::make_type_descriptor_ptr_type();
14131 if (itype
->is_empty())
14133 result_types
[itype
] = pdt
;
14137 Location loc
= this->location();
14138 Struct_field_list
* sfl
= new Struct_field_list();
14140 Struct_field(Typed_identifier("__type_descriptor", pdt
, loc
)));
14142 for (Typed_identifier_list::const_iterator p
= itype
->methods()->begin();
14143 p
!= itype
->methods()->end();
14146 Function_type
* ft
= p
->type()->function_type();
14147 go_assert(ft
->receiver() == NULL
);
14149 const Typed_identifier_list
* params
= ft
->parameters();
14150 Typed_identifier_list
* mparams
= new Typed_identifier_list();
14151 if (params
!= NULL
)
14152 mparams
->reserve(params
->size() + 1);
14153 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
14154 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
14155 if (params
!= NULL
)
14157 for (Typed_identifier_list::const_iterator pp
= params
->begin();
14158 pp
!= params
->end();
14160 mparams
->push_back(*pp
);
14163 Typed_identifier_list
* mresults
= (ft
->results() == NULL
14165 : ft
->results()->copy());
14166 Backend_function_type
* mft
=
14167 Type::make_backend_function_type(NULL
, mparams
, mresults
,
14170 std::string fname
= Gogo::unpack_hidden_name(p
->name());
14171 sfl
->push_back(Struct_field(Typed_identifier(fname
, mft
, loc
)));
14174 Pointer_type
*pt
= Type::make_pointer_type(Type::make_struct_type(sfl
, loc
));
14175 result_types
[itype
] = pt
;
14178 case INTERFACE_INFO_OBJECT
:
14179 return Type::make_pointer_type(Type::make_void_type());
14185 // Return the backend representation for interface information.
14188 Interface_info_expression::do_get_backend(Translate_context
* context
)
14190 Gogo
* gogo
= context
->gogo();
14191 Bexpression
* biface
= this->iface_
->get_backend(context
);
14192 switch (this->iface_info_
)
14194 case INTERFACE_INFO_METHODS
:
14195 case INTERFACE_INFO_OBJECT
:
14196 return gogo
->backend()->struct_field_expression(biface
, this->iface_info_
,
14204 // Dump ast representation for an interface info expression.
14207 Interface_info_expression::do_dump_expression(
14208 Ast_dump_context
* ast_dump_context
) const
14210 bool is_empty
= this->iface_
->type()->interface_type()->is_empty();
14211 ast_dump_context
->ostream() << "interfaceinfo(";
14212 this->iface_
->dump_expression(ast_dump_context
);
14213 ast_dump_context
->ostream() << ",";
14214 ast_dump_context
->ostream() <<
14215 (this->iface_info_
== INTERFACE_INFO_METHODS
&& !is_empty
? "methods"
14216 : this->iface_info_
== INTERFACE_INFO_TYPE_DESCRIPTOR
? "type_descriptor"
14217 : this->iface_info_
== INTERFACE_INFO_OBJECT
? "object"
14219 ast_dump_context
->ostream() << ")";
14222 // Make an interface info expression.
14225 Expression::make_interface_info(Expression
* iface
, Interface_info iface_info
,
14228 return new Interface_info_expression(iface
, iface_info
, location
);
14231 // An expression that represents an interface value. The first field is either
14232 // a type descriptor for an empty interface or a pointer to the interface method
14233 // table for a non-empty interface. The second field is always the object.
14235 class Interface_value_expression
: public Expression
14238 Interface_value_expression(Type
* type
, Expression
* first_field
,
14239 Expression
* obj
, Location location
)
14240 : Expression(EXPRESSION_INTERFACE_VALUE
, location
),
14241 type_(type
), first_field_(first_field
), obj_(obj
)
14246 do_traverse(Traverse
*);
14250 { return this->type_
; }
14253 do_determine_type(const Type_context
*)
14254 { go_unreachable(); }
14259 return new Interface_value_expression(this->type_
,
14260 this->first_field_
->copy(),
14261 this->obj_
->copy(), this->location());
14265 do_get_backend(Translate_context
* context
);
14268 do_dump_expression(Ast_dump_context
*) const;
14271 // The type of the interface value.
14273 // The first field of the interface (either a type descriptor or a pointer
14274 // to the method table.
14275 Expression
* first_field_
;
14276 // The underlying object of the interface.
14281 Interface_value_expression::do_traverse(Traverse
* traverse
)
14283 if (Expression::traverse(&this->first_field_
, traverse
) == TRAVERSE_EXIT
14284 || Expression::traverse(&this->obj_
, traverse
) == TRAVERSE_EXIT
)
14285 return TRAVERSE_EXIT
;
14286 return TRAVERSE_CONTINUE
;
14290 Interface_value_expression::do_get_backend(Translate_context
* context
)
14292 std::vector
<Bexpression
*> vals(2);
14293 vals
[0] = this->first_field_
->get_backend(context
);
14294 vals
[1] = this->obj_
->get_backend(context
);
14296 Gogo
* gogo
= context
->gogo();
14297 Btype
* btype
= this->type_
->get_backend(gogo
);
14298 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
14302 Interface_value_expression::do_dump_expression(
14303 Ast_dump_context
* ast_dump_context
) const
14305 ast_dump_context
->ostream() << "interfacevalue(";
14306 ast_dump_context
->ostream() <<
14307 (this->type_
->interface_type()->is_empty()
14308 ? "type_descriptor: "
14310 this->first_field_
->dump_expression(ast_dump_context
);
14311 ast_dump_context
->ostream() << ", object: ";
14312 this->obj_
->dump_expression(ast_dump_context
);
14313 ast_dump_context
->ostream() << ")";
14317 Expression::make_interface_value(Type
* type
, Expression
* first_value
,
14318 Expression
* object
, Location location
)
14320 return new Interface_value_expression(type
, first_value
, object
, location
);
14323 // An interface method table for a pair of types: an interface type and a type
14324 // that implements that interface.
14326 class Interface_mtable_expression
: public Expression
14329 Interface_mtable_expression(Interface_type
* itype
, Type
* type
,
14330 bool is_pointer
, Location location
)
14331 : Expression(EXPRESSION_INTERFACE_MTABLE
, location
),
14332 itype_(itype
), type_(type
), is_pointer_(is_pointer
),
14333 method_table_type_(NULL
), bvar_(NULL
)
14338 do_traverse(Traverse
*);
14344 is_immutable() const
14348 do_determine_type(const Type_context
*)
14349 { go_unreachable(); }
14354 return new Interface_mtable_expression(this->itype_
, this->type_
,
14355 this->is_pointer_
, this->location());
14359 do_is_addressable() const
14363 do_get_backend(Translate_context
* context
);
14366 do_dump_expression(Ast_dump_context
*) const;
14369 // The interface type for which the methods are defined.
14370 Interface_type
* itype_
;
14371 // The type to construct the interface method table for.
14373 // Whether this table contains the method set for the receiver type or the
14374 // pointer receiver type.
14376 // The type of the method table.
14377 Type
* method_table_type_
;
14378 // The backend variable that refers to the interface method table.
14383 Interface_mtable_expression::do_traverse(Traverse
* traverse
)
14385 if (Type::traverse(this->itype_
, traverse
) == TRAVERSE_EXIT
14386 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
14387 return TRAVERSE_EXIT
;
14388 return TRAVERSE_CONTINUE
;
14392 Interface_mtable_expression::do_type()
14394 if (this->method_table_type_
!= NULL
)
14395 return this->method_table_type_
;
14397 const Typed_identifier_list
* interface_methods
= this->itype_
->methods();
14398 go_assert(!interface_methods
->empty());
14400 Struct_field_list
* sfl
= new Struct_field_list
;
14401 Typed_identifier
tid("__type_descriptor", Type::make_type_descriptor_ptr_type(),
14403 sfl
->push_back(Struct_field(tid
));
14404 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
14405 p
!= interface_methods
->end();
14407 sfl
->push_back(Struct_field(*p
));
14408 this->method_table_type_
= Type::make_struct_type(sfl
, this->location());
14409 return this->method_table_type_
;
14413 Interface_mtable_expression::do_get_backend(Translate_context
* context
)
14415 Gogo
* gogo
= context
->gogo();
14416 Location loc
= Linemap::predeclared_location();
14417 if (this->bvar_
!= NULL
)
14418 return gogo
->backend()->var_expression(this->bvar_
, this->location());
14420 const Typed_identifier_list
* interface_methods
= this->itype_
->methods();
14421 go_assert(!interface_methods
->empty());
14423 std::string mangled_name
= ((this->is_pointer_
? "__go_pimt__" : "__go_imt_")
14424 + this->itype_
->mangled_name(gogo
)
14426 + this->type_
->mangled_name(gogo
));
14428 // See whether this interface has any hidden methods.
14429 bool has_hidden_methods
= false;
14430 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
14431 p
!= interface_methods
->end();
14434 if (Gogo::is_hidden_name(p
->name()))
14436 has_hidden_methods
= true;
14441 // We already know that the named type is convertible to the
14442 // interface. If the interface has hidden methods, and the named
14443 // type is defined in a different package, then the interface
14444 // conversion table will be defined by that other package.
14445 if (has_hidden_methods
14446 && this->type_
->named_type() != NULL
14447 && this->type_
->named_type()->named_object()->package() != NULL
)
14449 Btype
* btype
= this->type()->get_backend(gogo
);
14451 gogo
->backend()->immutable_struct_reference(mangled_name
, btype
, loc
);
14452 return gogo
->backend()->var_expression(this->bvar_
, this->location());
14455 // The first element is the type descriptor.
14457 if (!this->is_pointer_
)
14458 td_type
= this->type_
;
14460 td_type
= Type::make_pointer_type(this->type_
);
14462 // Build an interface method table for a type: a type descriptor followed by a
14463 // list of function pointers, one for each interface method. This is used for
14465 Expression_list
* svals
= new Expression_list();
14466 svals
->push_back(Expression::make_type_descriptor(td_type
, loc
));
14468 Named_type
* nt
= this->type_
->named_type();
14469 Struct_type
* st
= this->type_
->struct_type();
14470 go_assert(nt
!= NULL
|| st
!= NULL
);
14472 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
14473 p
!= interface_methods
->end();
14479 m
= nt
->method_function(p
->name(), &is_ambiguous
);
14481 m
= st
->method_function(p
->name(), &is_ambiguous
);
14482 go_assert(m
!= NULL
);
14483 Named_object
* no
= m
->named_object();
14485 go_assert(no
->is_function() || no
->is_function_declaration());
14486 svals
->push_back(Expression::make_func_code_reference(no
, loc
));
14489 Btype
* btype
= this->type()->get_backend(gogo
);
14490 Expression
* mtable
= Expression::make_struct_composite_literal(this->type(),
14492 Bexpression
* ctor
= mtable
->get_backend(context
);
14494 bool is_public
= has_hidden_methods
&& this->type_
->named_type() != NULL
;
14495 this->bvar_
= gogo
->backend()->immutable_struct(mangled_name
, false,
14496 !is_public
, btype
, loc
);
14497 gogo
->backend()->immutable_struct_set_init(this->bvar_
, mangled_name
, false,
14498 !is_public
, btype
, loc
, ctor
);
14499 return gogo
->backend()->var_expression(this->bvar_
, loc
);
14503 Interface_mtable_expression::do_dump_expression(
14504 Ast_dump_context
* ast_dump_context
) const
14506 ast_dump_context
->ostream() << "__go_"
14507 << (this->is_pointer_
? "pimt__" : "imt_");
14508 ast_dump_context
->dump_type(this->itype_
);
14509 ast_dump_context
->ostream() << "__";
14510 ast_dump_context
->dump_type(this->type_
);
14514 Expression::make_interface_mtable_ref(Interface_type
* itype
, Type
* type
,
14515 bool is_pointer
, Location location
)
14517 return new Interface_mtable_expression(itype
, type
, is_pointer
, location
);
14520 // An expression which evaluates to the offset of a field within a
14521 // struct. This, like Type_info_expression, q.v., is only used to
14522 // initialize fields of a type descriptor.
14524 class Struct_field_offset_expression
: public Expression
14527 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
14528 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
,
14529 Linemap::predeclared_location()),
14530 type_(type
), field_(field
)
14535 do_is_immutable() const
14540 { return Type::lookup_integer_type("uintptr"); }
14543 do_determine_type(const Type_context
*)
14551 do_get_backend(Translate_context
* context
);
14554 do_dump_expression(Ast_dump_context
*) const;
14557 // The type of the struct.
14558 Struct_type
* type_
;
14560 const Struct_field
* field_
;
14563 // Return the backend representation for a struct field offset.
14566 Struct_field_offset_expression::do_get_backend(Translate_context
* context
)
14568 const Struct_field_list
* fields
= this->type_
->fields();
14569 Struct_field_list::const_iterator p
;
14571 for (p
= fields
->begin();
14572 p
!= fields
->end();
14574 if (&*p
== this->field_
)
14576 go_assert(&*p
== this->field_
);
14578 Gogo
* gogo
= context
->gogo();
14579 Btype
* btype
= this->type_
->get_backend(gogo
);
14581 int64_t offset
= gogo
->backend()->type_field_offset(btype
, i
);
14582 Type
* uptr_type
= Type::lookup_integer_type("uintptr");
14584 Expression::make_integer_int64(offset
, uptr_type
,
14585 Linemap::predeclared_location());
14586 return ret
->get_backend(context
);
14589 // Dump ast representation for a struct field offset expression.
14592 Struct_field_offset_expression::do_dump_expression(
14593 Ast_dump_context
* ast_dump_context
) const
14595 ast_dump_context
->ostream() << "unsafe.Offsetof(";
14596 ast_dump_context
->dump_type(this->type_
);
14597 ast_dump_context
->ostream() << '.';
14598 ast_dump_context
->ostream() <<
14599 Gogo::message_name(this->field_
->field_name());
14600 ast_dump_context
->ostream() << ")";
14603 // Make an expression for a struct field offset.
14606 Expression::make_struct_field_offset(Struct_type
* type
,
14607 const Struct_field
* field
)
14609 return new Struct_field_offset_expression(type
, field
);
14612 // An expression which evaluates to a pointer to the map descriptor of
14615 class Map_descriptor_expression
: public Expression
14618 Map_descriptor_expression(Map_type
* type
, Location location
)
14619 : Expression(EXPRESSION_MAP_DESCRIPTOR
, location
),
14626 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
14629 do_determine_type(const Type_context
*)
14637 do_get_backend(Translate_context
* context
)
14639 return this->type_
->map_descriptor_pointer(context
->gogo(),
14644 do_dump_expression(Ast_dump_context
*) const;
14647 // The type for which this is the descriptor.
14651 // Dump ast representation for a map descriptor expression.
14654 Map_descriptor_expression::do_dump_expression(
14655 Ast_dump_context
* ast_dump_context
) const
14657 ast_dump_context
->ostream() << "map_descriptor(";
14658 ast_dump_context
->dump_type(this->type_
);
14659 ast_dump_context
->ostream() << ")";
14662 // Make a map descriptor expression.
14665 Expression::make_map_descriptor(Map_type
* type
, Location location
)
14667 return new Map_descriptor_expression(type
, location
);
14670 // An expression which evaluates to the address of an unnamed label.
14672 class Label_addr_expression
: public Expression
14675 Label_addr_expression(Label
* label
, Location location
)
14676 : Expression(EXPRESSION_LABEL_ADDR
, location
),
14683 { return Type::make_pointer_type(Type::make_void_type()); }
14686 do_determine_type(const Type_context
*)
14691 { return new Label_addr_expression(this->label_
, this->location()); }
14694 do_get_backend(Translate_context
* context
)
14695 { return this->label_
->get_addr(context
, this->location()); }
14698 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
14699 { ast_dump_context
->ostream() << this->label_
->name(); }
14702 // The label whose address we are taking.
14706 // Make an expression for the address of an unnamed label.
14709 Expression::make_label_addr(Label
* label
, Location location
)
14711 return new Label_addr_expression(label
, location
);
14714 // Class Conditional_expression.
14719 Conditional_expression::do_traverse(Traverse
* traverse
)
14721 if (Expression::traverse(&this->cond_
, traverse
) == TRAVERSE_EXIT
14722 || Expression::traverse(&this->then_
, traverse
) == TRAVERSE_EXIT
14723 || Expression::traverse(&this->else_
, traverse
) == TRAVERSE_EXIT
)
14724 return TRAVERSE_EXIT
;
14725 return TRAVERSE_CONTINUE
;
14728 // Return the type of the conditional expression.
14731 Conditional_expression::do_type()
14733 Type
* result_type
= Type::make_void_type();
14734 if (Type::are_identical(this->then_
->type(), this->else_
->type(), false,
14736 result_type
= this->then_
->type();
14737 else if (this->then_
->is_nil_expression()
14738 || this->else_
->is_nil_expression())
14739 result_type
= (!this->then_
->is_nil_expression()
14740 ? this->then_
->type()
14741 : this->else_
->type());
14742 return result_type
;
14745 // Determine type for a conditional expression.
14748 Conditional_expression::do_determine_type(const Type_context
* context
)
14750 this->cond_
->determine_type_no_context();
14751 this->then_
->determine_type(context
);
14752 this->else_
->determine_type(context
);
14755 // Get the backend representation of a conditional expression.
14758 Conditional_expression::do_get_backend(Translate_context
* context
)
14760 Gogo
* gogo
= context
->gogo();
14761 Btype
* result_btype
= this->type()->get_backend(gogo
);
14762 Bexpression
* cond
= this->cond_
->get_backend(context
);
14763 Bexpression
* then
= this->then_
->get_backend(context
);
14764 Bexpression
* belse
= this->else_
->get_backend(context
);
14765 return gogo
->backend()->conditional_expression(result_btype
, cond
, then
,
14766 belse
, this->location());
14769 // Dump ast representation of a conditional expression.
14772 Conditional_expression::do_dump_expression(
14773 Ast_dump_context
* ast_dump_context
) const
14775 ast_dump_context
->ostream() << "(";
14776 ast_dump_context
->dump_expression(this->cond_
);
14777 ast_dump_context
->ostream() << " ? ";
14778 ast_dump_context
->dump_expression(this->then_
);
14779 ast_dump_context
->ostream() << " : ";
14780 ast_dump_context
->dump_expression(this->else_
);
14781 ast_dump_context
->ostream() << ") ";
14784 // Make a conditional expression.
14787 Expression::make_conditional(Expression
* cond
, Expression
* then
,
14788 Expression
* else_expr
, Location location
)
14790 return new Conditional_expression(cond
, then
, else_expr
, location
);
14793 // Class Compound_expression.
14798 Compound_expression::do_traverse(Traverse
* traverse
)
14800 if (Expression::traverse(&this->init_
, traverse
) == TRAVERSE_EXIT
14801 || Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
14802 return TRAVERSE_EXIT
;
14803 return TRAVERSE_CONTINUE
;
14806 // Return the type of the compound expression.
14809 Compound_expression::do_type()
14811 return this->expr_
->type();
14814 // Determine type for a compound expression.
14817 Compound_expression::do_determine_type(const Type_context
* context
)
14819 this->init_
->determine_type_no_context();
14820 this->expr_
->determine_type(context
);
14823 // Get the backend representation of a compound expression.
14826 Compound_expression::do_get_backend(Translate_context
* context
)
14828 Gogo
* gogo
= context
->gogo();
14829 Bexpression
* binit
= this->init_
->get_backend(context
);
14830 Bstatement
* init_stmt
= gogo
->backend()->expression_statement(binit
);
14831 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
14832 return gogo
->backend()->compound_expression(init_stmt
, bexpr
,
14836 // Dump ast representation of a conditional expression.
14839 Compound_expression::do_dump_expression(
14840 Ast_dump_context
* ast_dump_context
) const
14842 ast_dump_context
->ostream() << "(";
14843 ast_dump_context
->dump_expression(this->init_
);
14844 ast_dump_context
->ostream() << ",";
14845 ast_dump_context
->dump_expression(this->expr_
);
14846 ast_dump_context
->ostream() << ") ";
14849 // Make a compound expression.
14852 Expression::make_compound(Expression
* init
, Expression
* expr
, Location location
)
14854 return new Compound_expression(init
, expr
, location
);
14857 // Import an expression. This comes at the end in order to see the
14858 // various class definitions.
14861 Expression::import_expression(Import
* imp
)
14863 int c
= imp
->peek_char();
14864 if (imp
->match_c_string("- ")
14865 || imp
->match_c_string("! ")
14866 || imp
->match_c_string("^ "))
14867 return Unary_expression::do_import(imp
);
14869 return Binary_expression::do_import(imp
);
14870 else if (imp
->match_c_string("true")
14871 || imp
->match_c_string("false"))
14872 return Boolean_expression::do_import(imp
);
14874 return String_expression::do_import(imp
);
14875 else if (c
== '-' || (c
>= '0' && c
<= '9'))
14877 // This handles integers, floats and complex constants.
14878 return Integer_expression::do_import(imp
);
14880 else if (imp
->match_c_string("nil"))
14881 return Nil_expression::do_import(imp
);
14882 else if (imp
->match_c_string("convert"))
14883 return Type_conversion_expression::do_import(imp
);
14886 error_at(imp
->location(), "import error: expected expression");
14887 return Expression::make_error(imp
->location());
14891 // Class Expression_list.
14893 // Traverse the list.
14896 Expression_list::traverse(Traverse
* traverse
)
14898 for (Expression_list::iterator p
= this->begin();
14904 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
14905 return TRAVERSE_EXIT
;
14908 return TRAVERSE_CONTINUE
;
14914 Expression_list::copy()
14916 Expression_list
* ret
= new Expression_list();
14917 for (Expression_list::iterator p
= this->begin();
14922 ret
->push_back(NULL
);
14924 ret
->push_back((*p
)->copy());
14929 // Return whether an expression list has an error expression.
14932 Expression_list::contains_error() const
14934 for (Expression_list::const_iterator p
= this->begin();
14937 if (*p
!= NULL
&& (*p
)->is_error_expression())
14942 // Class Numeric_constant.
14946 Numeric_constant::~Numeric_constant()
14951 // Copy constructor.
14953 Numeric_constant::Numeric_constant(const Numeric_constant
& a
)
14954 : classification_(a
.classification_
), type_(a
.type_
)
14956 switch (a
.classification_
)
14962 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
14965 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, GMP_RNDN
);
14968 mpc_init2(this->u_
.complex_val
, mpc_precision
);
14969 mpc_set(this->u_
.complex_val
, a
.u_
.complex_val
, MPC_RNDNN
);
14976 // Assignment operator.
14979 Numeric_constant::operator=(const Numeric_constant
& a
)
14982 this->classification_
= a
.classification_
;
14983 this->type_
= a
.type_
;
14984 switch (a
.classification_
)
14990 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
14993 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, GMP_RNDN
);
14996 mpc_init2(this->u_
.complex_val
, mpc_precision
);
14997 mpc_set(this->u_
.complex_val
, a
.u_
.complex_val
, MPC_RNDNN
);
15005 // Clear the contents.
15008 Numeric_constant::clear()
15010 switch (this->classification_
)
15016 mpz_clear(this->u_
.int_val
);
15019 mpfr_clear(this->u_
.float_val
);
15022 mpc_clear(this->u_
.complex_val
);
15027 this->classification_
= NC_INVALID
;
15030 // Set to an unsigned long value.
15033 Numeric_constant::set_unsigned_long(Type
* type
, unsigned long val
)
15036 this->classification_
= NC_INT
;
15037 this->type_
= type
;
15038 mpz_init_set_ui(this->u_
.int_val
, val
);
15041 // Set to an integer value.
15044 Numeric_constant::set_int(Type
* type
, const mpz_t val
)
15047 this->classification_
= NC_INT
;
15048 this->type_
= type
;
15049 mpz_init_set(this->u_
.int_val
, val
);
15052 // Set to a rune value.
15055 Numeric_constant::set_rune(Type
* type
, const mpz_t val
)
15058 this->classification_
= NC_RUNE
;
15059 this->type_
= type
;
15060 mpz_init_set(this->u_
.int_val
, val
);
15063 // Set to a floating point value.
15066 Numeric_constant::set_float(Type
* type
, const mpfr_t val
)
15069 this->classification_
= NC_FLOAT
;
15070 this->type_
= type
;
15071 // Numeric constants do not have negative zero values, so remove
15072 // them here. They also don't have infinity or NaN values, but we
15073 // should never see them here.
15074 if (mpfr_zero_p(val
))
15075 mpfr_init_set_ui(this->u_
.float_val
, 0, GMP_RNDN
);
15077 mpfr_init_set(this->u_
.float_val
, val
, GMP_RNDN
);
15080 // Set to a complex value.
15083 Numeric_constant::set_complex(Type
* type
, const mpc_t val
)
15086 this->classification_
= NC_COMPLEX
;
15087 this->type_
= type
;
15088 mpc_init2(this->u_
.complex_val
, mpc_precision
);
15089 mpc_set(this->u_
.complex_val
, val
, MPC_RNDNN
);
15092 // Get an int value.
15095 Numeric_constant::get_int(mpz_t
* val
) const
15097 go_assert(this->is_int());
15098 mpz_init_set(*val
, this->u_
.int_val
);
15101 // Get a rune value.
15104 Numeric_constant::get_rune(mpz_t
* val
) const
15106 go_assert(this->is_rune());
15107 mpz_init_set(*val
, this->u_
.int_val
);
15110 // Get a floating point value.
15113 Numeric_constant::get_float(mpfr_t
* val
) const
15115 go_assert(this->is_float());
15116 mpfr_init_set(*val
, this->u_
.float_val
, GMP_RNDN
);
15119 // Get a complex value.
15122 Numeric_constant::get_complex(mpc_t
* val
) const
15124 go_assert(this->is_complex());
15125 mpc_init2(*val
, mpc_precision
);
15126 mpc_set(*val
, this->u_
.complex_val
, MPC_RNDNN
);
15129 // Express value as unsigned long if possible.
15131 Numeric_constant::To_unsigned_long
15132 Numeric_constant::to_unsigned_long(unsigned long* val
) const
15134 switch (this->classification_
)
15138 return this->mpz_to_unsigned_long(this->u_
.int_val
, val
);
15140 return this->mpfr_to_unsigned_long(this->u_
.float_val
, val
);
15142 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
15143 return NC_UL_NOTINT
;
15144 return this->mpfr_to_unsigned_long(mpc_realref(this->u_
.complex_val
),
15151 // Express integer value as unsigned long if possible.
15153 Numeric_constant::To_unsigned_long
15154 Numeric_constant::mpz_to_unsigned_long(const mpz_t ival
,
15155 unsigned long *val
) const
15157 if (mpz_sgn(ival
) < 0)
15158 return NC_UL_NEGATIVE
;
15159 unsigned long ui
= mpz_get_ui(ival
);
15160 if (mpz_cmp_ui(ival
, ui
) != 0)
15163 return NC_UL_VALID
;
15166 // Express floating point value as unsigned long if possible.
15168 Numeric_constant::To_unsigned_long
15169 Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval
,
15170 unsigned long *val
) const
15172 if (!mpfr_integer_p(fval
))
15173 return NC_UL_NOTINT
;
15176 mpfr_get_z(ival
, fval
, GMP_RNDN
);
15177 To_unsigned_long ret
= this->mpz_to_unsigned_long(ival
, val
);
15182 // Convert value to integer if possible.
15185 Numeric_constant::to_int(mpz_t
* val
) const
15187 switch (this->classification_
)
15191 mpz_init_set(*val
, this->u_
.int_val
);
15194 if (!mpfr_integer_p(this->u_
.float_val
))
15197 mpfr_get_z(*val
, this->u_
.float_val
, GMP_RNDN
);
15200 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
))
15201 || !mpfr_integer_p(mpc_realref(this->u_
.complex_val
)))
15204 mpfr_get_z(*val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15211 // Convert value to floating point if possible.
15214 Numeric_constant::to_float(mpfr_t
* val
) const
15216 switch (this->classification_
)
15220 mpfr_init_set_z(*val
, this->u_
.int_val
, GMP_RNDN
);
15223 mpfr_init_set(*val
, this->u_
.float_val
, GMP_RNDN
);
15226 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
15228 mpfr_init_set(*val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15235 // Convert value to complex.
15238 Numeric_constant::to_complex(mpc_t
* val
) const
15240 mpc_init2(*val
, mpc_precision
);
15241 switch (this->classification_
)
15245 mpc_set_z(*val
, this->u_
.int_val
, MPC_RNDNN
);
15248 mpc_set_fr(*val
, this->u_
.float_val
, MPC_RNDNN
);
15251 mpc_set(*val
, this->u_
.complex_val
, MPC_RNDNN
);
15261 Numeric_constant::type() const
15263 if (this->type_
!= NULL
)
15264 return this->type_
;
15265 switch (this->classification_
)
15268 return Type::make_abstract_integer_type();
15270 return Type::make_abstract_character_type();
15272 return Type::make_abstract_float_type();
15274 return Type::make_abstract_complex_type();
15280 // If the constant can be expressed in TYPE, then set the type of the
15281 // constant to TYPE and return true. Otherwise return false, and, if
15282 // ISSUE_ERROR is true, report an appropriate error message.
15285 Numeric_constant::set_type(Type
* type
, bool issue_error
, Location loc
)
15288 if (type
== NULL
|| type
->is_error())
15290 else if (type
->integer_type() != NULL
)
15291 ret
= this->check_int_type(type
->integer_type(), issue_error
, loc
);
15292 else if (type
->float_type() != NULL
)
15293 ret
= this->check_float_type(type
->float_type(), issue_error
, loc
);
15294 else if (type
->complex_type() != NULL
)
15295 ret
= this->check_complex_type(type
->complex_type(), issue_error
, loc
);
15300 go_assert(saw_errors());
15303 this->type_
= type
;
15307 // Check whether the constant can be expressed in an integer type.
15310 Numeric_constant::check_int_type(Integer_type
* type
, bool issue_error
,
15314 switch (this->classification_
)
15318 mpz_init_set(val
, this->u_
.int_val
);
15322 if (!mpfr_integer_p(this->u_
.float_val
))
15327 "floating point constant truncated to integer");
15328 this->set_invalid();
15333 mpfr_get_z(val
, this->u_
.float_val
, GMP_RNDN
);
15337 if (!mpfr_integer_p(mpc_realref(this->u_
.complex_val
))
15338 || !mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
15342 error_at(location
, "complex constant truncated to integer");
15343 this->set_invalid();
15348 mpfr_get_z(val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15356 if (type
->is_abstract())
15360 int bits
= mpz_sizeinbase(val
, 2);
15361 if (type
->is_unsigned())
15363 // For an unsigned type we can only accept a nonnegative
15364 // number, and we must be able to represents at least BITS.
15365 ret
= mpz_sgn(val
) >= 0 && bits
<= type
->bits();
15369 // For a signed type we need an extra bit to indicate the
15370 // sign. We have to handle the most negative integer
15372 ret
= (bits
+ 1 <= type
->bits()
15373 || (bits
<= type
->bits()
15374 && mpz_sgn(val
) < 0
15375 && (mpz_scan1(val
, 0)
15376 == static_cast<unsigned long>(type
->bits() - 1))
15377 && mpz_scan0(val
, type
->bits()) == ULONG_MAX
));
15381 if (!ret
&& issue_error
)
15383 error_at(location
, "integer constant overflow");
15384 this->set_invalid();
15390 // Check whether the constant can be expressed in a floating point
15394 Numeric_constant::check_float_type(Float_type
* type
, bool issue_error
,
15398 switch (this->classification_
)
15402 mpfr_init_set_z(val
, this->u_
.int_val
, GMP_RNDN
);
15406 mpfr_init_set(val
, this->u_
.float_val
, GMP_RNDN
);
15410 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
15414 this->set_invalid();
15415 error_at(location
, "complex constant truncated to float");
15419 mpfr_init_set(val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15427 if (type
->is_abstract())
15429 else if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
15431 // A NaN or Infinity always fits in the range of the type.
15436 mp_exp_t exp
= mpfr_get_exp(val
);
15438 switch (type
->bits())
15450 ret
= exp
<= max_exp
;
15454 // Round the constant to the desired type.
15457 switch (type
->bits())
15460 mpfr_set_prec(t
, 24);
15463 mpfr_set_prec(t
, 53);
15468 mpfr_set(t
, val
, GMP_RNDN
);
15469 mpfr_set(val
, t
, GMP_RNDN
);
15472 this->set_float(type
, val
);
15478 if (!ret
&& issue_error
)
15480 error_at(location
, "floating point constant overflow");
15481 this->set_invalid();
15487 // Check whether the constant can be expressed in a complex type.
15490 Numeric_constant::check_complex_type(Complex_type
* type
, bool issue_error
,
15493 if (type
->is_abstract())
15497 switch (type
->bits())
15510 mpc_init2(val
, mpc_precision
);
15511 switch (this->classification_
)
15515 mpc_set_z(val
, this->u_
.int_val
, MPC_RNDNN
);
15519 mpc_set_fr(val
, this->u_
.float_val
, MPC_RNDNN
);
15523 mpc_set(val
, this->u_
.complex_val
, MPC_RNDNN
);
15531 if (!mpfr_nan_p(mpc_realref(val
))
15532 && !mpfr_inf_p(mpc_realref(val
))
15533 && !mpfr_zero_p(mpc_realref(val
))
15534 && mpfr_get_exp(mpc_realref(val
)) > max_exp
)
15538 error_at(location
, "complex real part overflow");
15539 this->set_invalid();
15544 if (!mpfr_nan_p(mpc_imagref(val
))
15545 && !mpfr_inf_p(mpc_imagref(val
))
15546 && !mpfr_zero_p(mpc_imagref(val
))
15547 && mpfr_get_exp(mpc_imagref(val
)) > max_exp
)
15551 error_at(location
, "complex imaginary part overflow");
15552 this->set_invalid();
15559 // Round the constant to the desired type.
15561 switch (type
->bits())
15572 mpc_set(t
, val
, MPC_RNDNN
);
15573 mpc_set(val
, t
, MPC_RNDNN
);
15576 this->set_complex(type
, val
);
15584 // Return an Expression for this value.
15587 Numeric_constant::expression(Location loc
) const
15589 switch (this->classification_
)
15592 return Expression::make_integer_z(&this->u_
.int_val
, this->type_
, loc
);
15594 return Expression::make_character(&this->u_
.int_val
, this->type_
, loc
);
15596 return Expression::make_float(&this->u_
.float_val
, this->type_
, loc
);
15598 return Expression::make_complex(&this->u_
.complex_val
, this->type_
, loc
);
15600 go_assert(saw_errors());
15601 return Expression::make_error(loc
);