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.
256 obj
= Expression::make_heap_expression(rhs
, location
);
259 return Expression::make_interface_value(lhs_type
, first_field
, obj
, location
);
262 // Return an expression for the type descriptor of RHS.
265 Expression::get_interface_type_descriptor(Expression
* rhs
)
267 go_assert(rhs
->type()->interface_type() != NULL
);
268 Location location
= rhs
->location();
270 // The type descriptor is the first field of an empty interface.
271 if (rhs
->type()->interface_type()->is_empty())
272 return Expression::make_interface_info(rhs
, INTERFACE_INFO_TYPE_DESCRIPTOR
,
276 Expression::make_interface_info(rhs
, INTERFACE_INFO_METHODS
, location
);
278 Expression
* descriptor
=
279 Expression::make_unary(OPERATOR_MULT
, mtable
, location
);
280 descriptor
= Expression::make_field_reference(descriptor
, 0, location
);
281 Expression
* nil
= Expression::make_nil(location
);
284 Expression::make_binary(OPERATOR_EQEQ
, mtable
, nil
, location
);
285 return Expression::make_conditional(eq
, nil
, descriptor
, location
);
288 // Return an expression for the conversion of an interface type to an
292 Expression::convert_interface_to_interface(Type
*lhs_type
, Expression
* rhs
,
296 if (Type::are_identical(lhs_type
, rhs
->type(), false, NULL
))
299 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
300 bool lhs_is_empty
= lhs_interface_type
->is_empty();
302 // In the general case this requires runtime examination of the type
303 // method table to match it up with the interface methods.
305 // FIXME: If all of the methods in the right hand side interface
306 // also appear in the left hand side interface, then we don't need
307 // to do a runtime check, although we still need to build a new
310 // We are going to evaluate RHS multiple times.
311 go_assert(rhs
->is_variable());
313 // Get the type descriptor for the right hand side. This will be
314 // NULL for a nil interface.
315 Expression
* rhs_type_expr
= Expression::get_interface_type_descriptor(rhs
);
316 Expression
* lhs_type_expr
=
317 Expression::make_type_descriptor(lhs_type
, location
);
319 Expression
* first_field
;
322 // A type assertion fails when converting a nil interface.
324 Runtime::make_call(Runtime::ASSERT_INTERFACE
, location
, 2,
325 lhs_type_expr
, rhs_type_expr
);
327 else if (lhs_is_empty
)
329 // A conversion to an empty interface always succeeds, and the
330 // first field is just the type descriptor of the object.
331 first_field
= rhs_type_expr
;
335 // A conversion to a non-empty interface may fail, but unlike a
336 // type assertion converting nil will always succeed.
338 Runtime::make_call(Runtime::CONVERT_INTERFACE
, location
, 2,
339 lhs_type_expr
, rhs_type_expr
);
342 // The second field is simply the object pointer.
344 Expression::make_interface_info(rhs
, INTERFACE_INFO_OBJECT
, location
);
345 return Expression::make_interface_value(lhs_type
, first_field
, obj
, location
);
348 // Return an expression for the conversion of an interface type to a
349 // non-interface type.
352 Expression::convert_interface_to_type(Type
*lhs_type
, Expression
* rhs
,
355 // We are going to evaluate RHS multiple times.
356 go_assert(rhs
->is_variable());
358 // Call a function to check that the type is valid. The function
359 // will panic with an appropriate runtime type error if the type is
361 Expression
* lhs_type_expr
= Expression::make_type_descriptor(lhs_type
,
363 Expression
* rhs_descriptor
=
364 Expression::get_interface_type_descriptor(rhs
);
366 Type
* rhs_type
= rhs
->type();
367 Expression
* rhs_inter_expr
= Expression::make_type_descriptor(rhs_type
,
370 Expression
* check_iface
= Runtime::make_call(Runtime::CHECK_INTERFACE_TYPE
,
371 location
, 3, lhs_type_expr
,
372 rhs_descriptor
, rhs_inter_expr
);
374 // If the call succeeds, pull out the value.
375 Expression
* obj
= Expression::make_interface_info(rhs
, INTERFACE_INFO_OBJECT
,
378 // If the value is a pointer, then it is the value we want.
379 // Otherwise it points to the value.
380 if (lhs_type
->points_to() == NULL
)
382 obj
= Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type
), obj
,
384 obj
= Expression::make_unary(OPERATOR_MULT
, obj
, location
);
386 return Expression::make_compound(check_iface
, obj
, location
);
389 // Convert an expression to its backend representation. This is implemented by
390 // the child class. Not that it is not in general safe to call this multiple
391 // times for a single expression, but that we don't catch such errors.
394 Expression::get_backend(Translate_context
* context
)
396 // The child may have marked this expression as having an error.
397 if (this->classification_
== EXPRESSION_ERROR
)
398 return context
->backend()->error_expression();
400 return this->do_get_backend(context
);
403 // Return a backend expression for VAL.
405 Expression::backend_numeric_constant_expression(Translate_context
* context
,
406 Numeric_constant
* val
)
408 Gogo
* gogo
= context
->gogo();
409 Type
* type
= val
->type();
411 return gogo
->backend()->error_expression();
413 Btype
* btype
= type
->get_backend(gogo
);
415 if (type
->integer_type() != NULL
)
418 if (!val
->to_int(&ival
))
420 go_assert(saw_errors());
421 return gogo
->backend()->error_expression();
423 ret
= gogo
->backend()->integer_constant_expression(btype
, ival
);
426 else if (type
->float_type() != NULL
)
429 if (!val
->to_float(&fval
))
431 go_assert(saw_errors());
432 return gogo
->backend()->error_expression();
434 ret
= gogo
->backend()->float_constant_expression(btype
, fval
);
437 else if (type
->complex_type() != NULL
)
440 if (!val
->to_complex(&cval
))
442 go_assert(saw_errors());
443 return gogo
->backend()->error_expression();
445 ret
= gogo
->backend()->complex_constant_expression(btype
, cval
);
454 // Return an expression which evaluates to true if VAL, of arbitrary integer
455 // type, is negative or is more than the maximum value of the Go type "int".
458 Expression::check_bounds(Expression
* val
, Location loc
)
460 Type
* val_type
= val
->type();
461 Type
* bound_type
= Type::lookup_integer_type("int");
464 bool val_is_unsigned
= false;
465 if (val_type
->integer_type() != NULL
)
467 val_type_size
= val_type
->integer_type()->bits();
468 val_is_unsigned
= val_type
->integer_type()->is_unsigned();
472 if (!val_type
->is_numeric_type()
473 || !Type::are_convertible(bound_type
, val_type
, NULL
))
475 go_assert(saw_errors());
476 return Expression::make_boolean(true, loc
);
479 if (val_type
->complex_type() != NULL
)
480 val_type_size
= val_type
->complex_type()->bits();
482 val_type_size
= val_type
->float_type()->bits();
485 Expression
* negative_index
= Expression::make_boolean(false, loc
);
486 Expression
* index_overflows
= Expression::make_boolean(false, loc
);
487 if (!val_is_unsigned
)
489 Expression
* zero
= Expression::make_integer_ul(0, val_type
, loc
);
490 negative_index
= Expression::make_binary(OPERATOR_LT
, val
, zero
, loc
);
493 int bound_type_size
= bound_type
->integer_type()->bits();
494 if (val_type_size
> bound_type_size
495 || (val_type_size
== bound_type_size
499 mpz_init_set_ui(one
, 1UL);
501 // maxval = 2^(bound_type_size - 1) - 1
504 mpz_mul_2exp(maxval
, one
, bound_type_size
- 1);
505 mpz_sub_ui(maxval
, maxval
, 1);
506 Expression
* max
= Expression::make_integer_z(&maxval
, val_type
, loc
);
510 index_overflows
= Expression::make_binary(OPERATOR_GT
, val
, max
, loc
);
513 return Expression::make_binary(OPERATOR_OROR
, negative_index
, index_overflows
,
518 Expression::dump_expression(Ast_dump_context
* ast_dump_context
) const
520 this->do_dump_expression(ast_dump_context
);
523 // Error expressions. This are used to avoid cascading errors.
525 class Error_expression
: public Expression
528 Error_expression(Location location
)
529 : Expression(EXPRESSION_ERROR
, location
)
534 do_is_constant() const
538 do_is_immutable() const
542 do_numeric_constant_value(Numeric_constant
* nc
) const
544 nc
->set_unsigned_long(NULL
, 0);
549 do_discarding_value()
554 { return Type::make_error_type(); }
557 do_determine_type(const Type_context
*)
565 do_is_addressable() const
569 do_get_backend(Translate_context
* context
)
570 { return context
->backend()->error_expression(); }
573 do_dump_expression(Ast_dump_context
*) const;
576 // Dump the ast representation for an error expression to a dump context.
579 Error_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
581 ast_dump_context
->ostream() << "_Error_" ;
585 Expression::make_error(Location location
)
587 return new Error_expression(location
);
590 // An expression which is really a type. This is used during parsing.
591 // It is an error if these survive after lowering.
594 Type_expression
: public Expression
597 Type_expression(Type
* type
, Location location
)
598 : Expression(EXPRESSION_TYPE
, location
),
604 do_traverse(Traverse
* traverse
)
605 { return Type::traverse(this->type_
, traverse
); }
609 { return this->type_
; }
612 do_determine_type(const Type_context
*)
616 do_check_types(Gogo
*)
617 { this->report_error(_("invalid use of type")); }
624 do_get_backend(Translate_context
*)
625 { go_unreachable(); }
627 void do_dump_expression(Ast_dump_context
*) const;
630 // The type which we are representing as an expression.
635 Type_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
637 ast_dump_context
->dump_type(this->type_
);
641 Expression::make_type(Type
* type
, Location location
)
643 return new Type_expression(type
, location
);
646 // Class Parser_expression.
649 Parser_expression::do_type()
651 // We should never really ask for the type of a Parser_expression.
652 // However, it can happen, at least when we have an invalid const
653 // whose initializer refers to the const itself. In that case we
654 // may ask for the type when lowering the const itself.
655 go_assert(saw_errors());
656 return Type::make_error_type();
659 // Class Var_expression.
661 // Lower a variable expression. Here we just make sure that the
662 // initialization expression of the variable has been lowered. This
663 // ensures that we will be able to determine the type of the variable
667 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
668 Statement_inserter
* inserter
, int)
670 if (this->variable_
->is_variable())
672 Variable
* var
= this->variable_
->var_value();
673 // This is either a local variable or a global variable. A
674 // reference to a variable which is local to an enclosing
675 // function will be a reference to a field in a closure.
676 if (var
->is_global())
681 var
->lower_init_expression(gogo
, function
, inserter
);
686 // Return the type of a reference to a variable.
689 Var_expression::do_type()
691 if (this->variable_
->is_variable())
692 return this->variable_
->var_value()->type();
693 else if (this->variable_
->is_result_variable())
694 return this->variable_
->result_var_value()->type();
699 // Determine the type of a reference to a variable.
702 Var_expression::do_determine_type(const Type_context
*)
704 if (this->variable_
->is_variable())
705 this->variable_
->var_value()->determine_type();
708 // Something takes the address of this variable. This means that we
709 // may want to move the variable onto the heap.
712 Var_expression::do_address_taken(bool escapes
)
716 if (this->variable_
->is_variable())
717 this->variable_
->var_value()->set_non_escaping_address_taken();
718 else if (this->variable_
->is_result_variable())
719 this->variable_
->result_var_value()->set_non_escaping_address_taken();
725 if (this->variable_
->is_variable())
726 this->variable_
->var_value()->set_address_taken();
727 else if (this->variable_
->is_result_variable())
728 this->variable_
->result_var_value()->set_address_taken();
734 // Get the backend representation for a reference to a variable.
737 Var_expression::do_get_backend(Translate_context
* context
)
739 Bvariable
* bvar
= this->variable_
->get_backend_variable(context
->gogo(),
740 context
->function());
742 Location loc
= this->location();
744 Gogo
* gogo
= context
->gogo();
745 if (this->variable_
->is_variable())
747 is_in_heap
= this->variable_
->var_value()->is_in_heap();
748 btype
= this->variable_
->var_value()->type()->get_backend(gogo
);
750 else if (this->variable_
->is_result_variable())
752 is_in_heap
= this->variable_
->result_var_value()->is_in_heap();
753 btype
= this->variable_
->result_var_value()->type()->get_backend(gogo
);
758 Bexpression
* ret
= context
->backend()->var_expression(bvar
, loc
);
760 ret
= context
->backend()->indirect_expression(btype
, ret
, true, loc
);
764 // Ast dump for variable expression.
767 Var_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
769 ast_dump_context
->ostream() << this->variable_
->name() ;
772 // Make a reference to a variable in an expression.
775 Expression::make_var_reference(Named_object
* var
, Location location
)
778 return Expression::make_sink(location
);
780 // FIXME: Creating a new object for each reference to a variable is
782 return new Var_expression(var
, location
);
785 // Class Temporary_reference_expression.
790 Temporary_reference_expression::do_type()
792 return this->statement_
->type();
795 // Called if something takes the address of this temporary variable.
796 // We never have to move temporary variables to the heap, but we do
797 // need to know that they must live in the stack rather than in a
801 Temporary_reference_expression::do_address_taken(bool)
803 this->statement_
->set_is_address_taken();
806 // Get a backend expression referring to the variable.
809 Temporary_reference_expression::do_get_backend(Translate_context
* context
)
811 Gogo
* gogo
= context
->gogo();
812 Bvariable
* bvar
= this->statement_
->get_backend_variable(context
);
813 Bexpression
* ret
= gogo
->backend()->var_expression(bvar
, this->location());
815 // The backend can't always represent the same set of recursive types
816 // that the Go frontend can. In some cases this means that a
817 // temporary variable won't have the right backend type. Correct
818 // that here by adding a type cast. We need to use base() to push
819 // the circularity down one level.
820 Type
* stype
= this->statement_
->type();
821 if (!this->is_lvalue_
822 && stype
->has_pointer()
823 && stype
->deref()->is_void_type())
825 Btype
* btype
= this->type()->base()->get_backend(gogo
);
826 ret
= gogo
->backend()->convert_expression(btype
, ret
, this->location());
831 // Ast dump for temporary reference.
834 Temporary_reference_expression::do_dump_expression(
835 Ast_dump_context
* ast_dump_context
) const
837 ast_dump_context
->dump_temp_variable_name(this->statement_
);
840 // Make a reference to a temporary variable.
842 Temporary_reference_expression
*
843 Expression::make_temporary_reference(Temporary_statement
* statement
,
846 return new Temporary_reference_expression(statement
, location
);
849 // Class Set_and_use_temporary_expression.
854 Set_and_use_temporary_expression::do_type()
856 return this->statement_
->type();
859 // Determine the type of the expression.
862 Set_and_use_temporary_expression::do_determine_type(
863 const Type_context
* context
)
865 this->expr_
->determine_type(context
);
871 Set_and_use_temporary_expression::do_address_taken(bool)
873 this->statement_
->set_is_address_taken();
876 // Return the backend representation.
879 Set_and_use_temporary_expression::do_get_backend(Translate_context
* context
)
881 Location loc
= this->location();
882 Gogo
* gogo
= context
->gogo();
883 Bvariable
* bvar
= this->statement_
->get_backend_variable(context
);
884 Bexpression
* var_ref
= gogo
->backend()->var_expression(bvar
, loc
);
886 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
887 Bstatement
* set
= gogo
->backend()->assignment_statement(var_ref
, bexpr
, loc
);
888 var_ref
= gogo
->backend()->var_expression(bvar
, loc
);
889 Bexpression
* ret
= gogo
->backend()->compound_expression(set
, var_ref
, loc
);
896 Set_and_use_temporary_expression::do_dump_expression(
897 Ast_dump_context
* ast_dump_context
) const
899 ast_dump_context
->ostream() << '(';
900 ast_dump_context
->dump_temp_variable_name(this->statement_
);
901 ast_dump_context
->ostream() << " = ";
902 this->expr_
->dump_expression(ast_dump_context
);
903 ast_dump_context
->ostream() << ')';
906 // Make a set-and-use temporary.
908 Set_and_use_temporary_expression
*
909 Expression::make_set_and_use_temporary(Temporary_statement
* statement
,
910 Expression
* expr
, Location location
)
912 return new Set_and_use_temporary_expression(statement
, expr
, location
);
915 // A sink expression--a use of the blank identifier _.
917 class Sink_expression
: public Expression
920 Sink_expression(Location location
)
921 : Expression(EXPRESSION_SINK
, location
),
922 type_(NULL
), bvar_(NULL
)
927 do_discarding_value()
934 do_determine_type(const Type_context
*);
938 { return new Sink_expression(this->location()); }
941 do_get_backend(Translate_context
*);
944 do_dump_expression(Ast_dump_context
*) const;
947 // The type of this sink variable.
949 // The temporary variable we generate.
953 // Return the type of a sink expression.
956 Sink_expression::do_type()
958 if (this->type_
== NULL
)
959 return Type::make_sink_type();
963 // Determine the type of a sink expression.
966 Sink_expression::do_determine_type(const Type_context
* context
)
968 if (context
->type
!= NULL
)
969 this->type_
= context
->type
;
972 // Return a temporary variable for a sink expression. This will
973 // presumably be a write-only variable which the middle-end will drop.
976 Sink_expression::do_get_backend(Translate_context
* context
)
978 Location loc
= this->location();
979 Gogo
* gogo
= context
->gogo();
980 if (this->bvar_
== NULL
)
982 go_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
983 Named_object
* fn
= context
->function();
984 go_assert(fn
!= NULL
);
985 Bfunction
* fn_ctx
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
986 Btype
* bt
= this->type_
->get_backend(context
->gogo());
989 gogo
->backend()->temporary_variable(fn_ctx
, context
->bblock(), bt
, NULL
,
991 Bexpression
* var_ref
= gogo
->backend()->var_expression(this->bvar_
, loc
);
992 var_ref
= gogo
->backend()->compound_expression(decl
, var_ref
, loc
);
995 return gogo
->backend()->var_expression(this->bvar_
, loc
);
998 // Ast dump for sink expression.
1001 Sink_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1003 ast_dump_context
->ostream() << "_" ;
1006 // Make a sink expression.
1009 Expression::make_sink(Location location
)
1011 return new Sink_expression(location
);
1014 // Class Func_expression.
1016 // FIXME: Can a function expression appear in a constant expression?
1017 // The value is unchanging. Initializing a constant to the address of
1018 // a function seems like it could work, though there might be little
1024 Func_expression::do_traverse(Traverse
* traverse
)
1026 return (this->closure_
== NULL
1028 : Expression::traverse(&this->closure_
, traverse
));
1031 // Return the type of a function expression.
1034 Func_expression::do_type()
1036 if (this->function_
->is_function())
1037 return this->function_
->func_value()->type();
1038 else if (this->function_
->is_function_declaration())
1039 return this->function_
->func_declaration_value()->type();
1044 // Get the backend representation for the code of a function expression.
1047 Func_expression::get_code_pointer(Gogo
* gogo
, Named_object
* no
, Location loc
)
1049 Function_type
* fntype
;
1050 if (no
->is_function())
1051 fntype
= no
->func_value()->type();
1052 else if (no
->is_function_declaration())
1053 fntype
= no
->func_declaration_value()->type();
1057 // Builtin functions are handled specially by Call_expression. We
1058 // can't take their address.
1059 if (fntype
->is_builtin())
1062 "invalid use of special builtin function %qs; must be called",
1063 no
->message_name().c_str());
1064 return gogo
->backend()->error_expression();
1068 if (no
->is_function())
1069 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
);
1070 else if (no
->is_function_declaration())
1071 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
);
1075 return gogo
->backend()->function_code_expression(fndecl
, loc
);
1078 // Get the backend representation for a function expression. This is used when
1079 // we take the address of a function rather than simply calling it. A func
1080 // value is represented as a pointer to a block of memory. The first
1081 // word of that memory is a pointer to the function code. The
1082 // remaining parts of that memory are the addresses of variables that
1083 // the function closes over.
1086 Func_expression::do_get_backend(Translate_context
* context
)
1088 // If there is no closure, just use the function descriptor.
1089 if (this->closure_
== NULL
)
1091 Gogo
* gogo
= context
->gogo();
1092 Named_object
* no
= this->function_
;
1093 Expression
* descriptor
;
1094 if (no
->is_function())
1095 descriptor
= no
->func_value()->descriptor(gogo
, no
);
1096 else if (no
->is_function_declaration())
1098 if (no
->func_declaration_value()->type()->is_builtin())
1100 error_at(this->location(),
1101 ("invalid use of special builtin function %qs; "
1103 no
->message_name().c_str());
1104 return gogo
->backend()->error_expression();
1106 descriptor
= no
->func_declaration_value()->descriptor(gogo
, no
);
1111 Bexpression
* bdesc
= descriptor
->get_backend(context
);
1112 return gogo
->backend()->address_expression(bdesc
, this->location());
1115 go_assert(this->function_
->func_value()->enclosing() != NULL
);
1117 // If there is a closure, then the closure is itself the function
1118 // expression. It is a pointer to a struct whose first field points
1119 // to the function code and whose remaining fields are the addresses
1120 // of the closed-over variables.
1121 return this->closure_
->get_backend(context
);
1124 // Ast dump for function.
1127 Func_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1129 ast_dump_context
->ostream() << this->function_
->name();
1130 if (this->closure_
!= NULL
)
1132 ast_dump_context
->ostream() << " {closure = ";
1133 this->closure_
->dump_expression(ast_dump_context
);
1134 ast_dump_context
->ostream() << "}";
1138 // Make a reference to a function in an expression.
1141 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1144 return new Func_expression(function
, closure
, location
);
1147 // Class Func_descriptor_expression.
1151 Func_descriptor_expression::Func_descriptor_expression(Named_object
* fn
)
1152 : Expression(EXPRESSION_FUNC_DESCRIPTOR
, fn
->location()),
1153 fn_(fn
), dvar_(NULL
)
1155 go_assert(!fn
->is_function() || !fn
->func_value()->needs_closure());
1161 Func_descriptor_expression::do_traverse(Traverse
*)
1163 return TRAVERSE_CONTINUE
;
1166 // All function descriptors have the same type.
1168 Type
* Func_descriptor_expression::descriptor_type
;
1171 Func_descriptor_expression::make_func_descriptor_type()
1173 if (Func_descriptor_expression::descriptor_type
!= NULL
)
1175 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
1176 Type
* struct_type
= Type::make_builtin_struct_type(1, "code", uintptr_type
);
1177 Func_descriptor_expression::descriptor_type
=
1178 Type::make_builtin_named_type("functionDescriptor", struct_type
);
1182 Func_descriptor_expression::do_type()
1184 Func_descriptor_expression::make_func_descriptor_type();
1185 return Func_descriptor_expression::descriptor_type
;
1188 // The backend representation for a function descriptor.
1191 Func_descriptor_expression::do_get_backend(Translate_context
* context
)
1193 Named_object
* no
= this->fn_
;
1194 Location loc
= no
->location();
1195 if (this->dvar_
!= NULL
)
1196 return context
->backend()->var_expression(this->dvar_
, loc
);
1198 Gogo
* gogo
= context
->gogo();
1199 std::string var_name
;
1200 if (no
->package() == NULL
)
1201 var_name
= gogo
->pkgpath_symbol();
1203 var_name
= no
->package()->pkgpath_symbol();
1204 var_name
.push_back('.');
1205 var_name
.append(Gogo::unpack_hidden_name(no
->name()));
1206 var_name
.append("$descriptor");
1208 Btype
* btype
= this->type()->get_backend(gogo
);
1211 if (no
->package() != NULL
1212 || Linemap::is_predeclared_location(no
->location()))
1213 bvar
= context
->backend()->immutable_struct_reference(var_name
, btype
,
1217 Location bloc
= Linemap::predeclared_location();
1218 bool is_hidden
= ((no
->is_function()
1219 && no
->func_value()->enclosing() != NULL
)
1220 || Gogo::is_thunk(no
));
1221 bvar
= context
->backend()->immutable_struct(var_name
, is_hidden
, false,
1223 Expression_list
* vals
= new Expression_list();
1224 vals
->push_back(Expression::make_func_code_reference(this->fn_
, bloc
));
1226 Expression::make_struct_composite_literal(this->type(), vals
, bloc
);
1227 Translate_context
bcontext(gogo
, NULL
, NULL
, NULL
);
1228 bcontext
.set_is_const();
1229 Bexpression
* binit
= init
->get_backend(&bcontext
);
1230 context
->backend()->immutable_struct_set_init(bvar
, var_name
, is_hidden
,
1231 false, btype
, bloc
, binit
);
1235 return gogo
->backend()->var_expression(bvar
, loc
);
1238 // Print a function descriptor expression.
1241 Func_descriptor_expression::do_dump_expression(Ast_dump_context
* context
) const
1243 context
->ostream() << "[descriptor " << this->fn_
->name() << "]";
1246 // Make a function descriptor expression.
1248 Func_descriptor_expression
*
1249 Expression::make_func_descriptor(Named_object
* fn
)
1251 return new Func_descriptor_expression(fn
);
1254 // Make the function descriptor type, so that it can be converted.
1257 Expression::make_func_descriptor_type()
1259 Func_descriptor_expression::make_func_descriptor_type();
1262 // A reference to just the code of a function.
1264 class Func_code_reference_expression
: public Expression
1267 Func_code_reference_expression(Named_object
* function
, Location location
)
1268 : Expression(EXPRESSION_FUNC_CODE_REFERENCE
, location
),
1274 do_traverse(Traverse
*)
1275 { return TRAVERSE_CONTINUE
; }
1278 do_is_immutable() const
1283 { return Type::make_pointer_type(Type::make_void_type()); }
1286 do_determine_type(const Type_context
*)
1292 return Expression::make_func_code_reference(this->function_
,
1297 do_get_backend(Translate_context
*);
1300 do_dump_expression(Ast_dump_context
* context
) const
1301 { context
->ostream() << "[raw " << this->function_
->name() << "]" ; }
1305 Named_object
* function_
;
1308 // Get the backend representation for a reference to function code.
1311 Func_code_reference_expression::do_get_backend(Translate_context
* context
)
1313 return Func_expression::get_code_pointer(context
->gogo(), this->function_
,
1317 // Make a reference to the code of a function.
1320 Expression::make_func_code_reference(Named_object
* function
, Location location
)
1322 return new Func_code_reference_expression(function
, location
);
1325 // Class Unknown_expression.
1327 // Return the name of an unknown expression.
1330 Unknown_expression::name() const
1332 return this->named_object_
->name();
1335 // Lower a reference to an unknown name.
1338 Unknown_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
1340 Location location
= this->location();
1341 Named_object
* no
= this->named_object_
;
1343 if (!no
->is_unknown())
1347 real
= no
->unknown_value()->real_named_object();
1350 if (this->is_composite_literal_key_
)
1352 if (!this->no_error_message_
)
1353 error_at(location
, "reference to undefined name %qs",
1354 this->named_object_
->message_name().c_str());
1355 return Expression::make_error(location
);
1358 switch (real
->classification())
1360 case Named_object::NAMED_OBJECT_CONST
:
1361 return Expression::make_const_reference(real
, location
);
1362 case Named_object::NAMED_OBJECT_TYPE
:
1363 return Expression::make_type(real
->type_value(), location
);
1364 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1365 if (this->is_composite_literal_key_
)
1367 if (!this->no_error_message_
)
1368 error_at(location
, "reference to undefined type %qs",
1369 real
->message_name().c_str());
1370 return Expression::make_error(location
);
1371 case Named_object::NAMED_OBJECT_VAR
:
1372 real
->var_value()->set_is_used();
1373 return Expression::make_var_reference(real
, location
);
1374 case Named_object::NAMED_OBJECT_FUNC
:
1375 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1376 return Expression::make_func_reference(real
, NULL
, location
);
1377 case Named_object::NAMED_OBJECT_PACKAGE
:
1378 if (this->is_composite_literal_key_
)
1380 if (!this->no_error_message_
)
1381 error_at(location
, "unexpected reference to package");
1382 return Expression::make_error(location
);
1388 // Dump the ast representation for an unknown expression to a dump context.
1391 Unknown_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1393 ast_dump_context
->ostream() << "_Unknown_(" << this->named_object_
->name()
1397 // Make a reference to an unknown name.
1400 Expression::make_unknown_reference(Named_object
* no
, Location location
)
1402 return new Unknown_expression(no
, location
);
1405 // A boolean expression.
1407 class Boolean_expression
: public Expression
1410 Boolean_expression(bool val
, Location location
)
1411 : Expression(EXPRESSION_BOOLEAN
, location
),
1412 val_(val
), type_(NULL
)
1420 do_is_constant() const
1424 do_is_immutable() const
1431 do_determine_type(const Type_context
*);
1438 do_get_backend(Translate_context
* context
)
1439 { return context
->backend()->boolean_constant_expression(this->val_
); }
1442 do_export(Export
* exp
) const
1443 { exp
->write_c_string(this->val_
? "true" : "false"); }
1446 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1447 { ast_dump_context
->ostream() << (this->val_
? "true" : "false"); }
1452 // The type as determined by context.
1459 Boolean_expression::do_type()
1461 if (this->type_
== NULL
)
1462 this->type_
= Type::make_boolean_type();
1466 // Set the type from the context.
1469 Boolean_expression::do_determine_type(const Type_context
* context
)
1471 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1473 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1474 this->type_
= context
->type
;
1475 else if (!context
->may_be_abstract
)
1476 this->type_
= Type::lookup_bool_type();
1479 // Import a boolean constant.
1482 Boolean_expression::do_import(Import
* imp
)
1484 if (imp
->peek_char() == 't')
1486 imp
->require_c_string("true");
1487 return Expression::make_boolean(true, imp
->location());
1491 imp
->require_c_string("false");
1492 return Expression::make_boolean(false, imp
->location());
1496 // Make a boolean expression.
1499 Expression::make_boolean(bool val
, Location location
)
1501 return new Boolean_expression(val
, location
);
1504 // Class String_expression.
1509 String_expression::do_type()
1511 if (this->type_
== NULL
)
1512 this->type_
= Type::make_string_type();
1516 // Set the type from the context.
1519 String_expression::do_determine_type(const Type_context
* context
)
1521 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1523 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1524 this->type_
= context
->type
;
1525 else if (!context
->may_be_abstract
)
1526 this->type_
= Type::lookup_string_type();
1529 // Build a string constant.
1532 String_expression::do_get_backend(Translate_context
* context
)
1534 Gogo
* gogo
= context
->gogo();
1535 Btype
* btype
= Type::make_string_type()->get_backend(gogo
);
1537 Location loc
= this->location();
1538 std::vector
<Bexpression
*> init(2);
1539 Bexpression
* str_cst
=
1540 gogo
->backend()->string_constant_expression(this->val_
);
1541 init
[0] = gogo
->backend()->address_expression(str_cst
, loc
);
1543 Btype
* int_btype
= Type::lookup_integer_type("int")->get_backend(gogo
);
1545 mpz_init_set_ui(lenval
, this->val_
.length());
1546 init
[1] = gogo
->backend()->integer_constant_expression(int_btype
, lenval
);
1549 return gogo
->backend()->constructor_expression(btype
, init
, loc
);
1552 // Write string literal to string dump.
1555 String_expression::export_string(String_dump
* exp
,
1556 const String_expression
* str
)
1559 s
.reserve(str
->val_
.length() * 4 + 2);
1561 for (std::string::const_iterator p
= str
->val_
.begin();
1562 p
!= str
->val_
.end();
1565 if (*p
== '\\' || *p
== '"')
1570 else if (*p
>= 0x20 && *p
< 0x7f)
1572 else if (*p
== '\n')
1574 else if (*p
== '\t')
1579 unsigned char c
= *p
;
1580 unsigned int dig
= c
>> 4;
1581 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1583 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1587 exp
->write_string(s
);
1590 // Export a string expression.
1593 String_expression::do_export(Export
* exp
) const
1595 String_expression::export_string(exp
, this);
1598 // Import a string expression.
1601 String_expression::do_import(Import
* imp
)
1603 imp
->require_c_string("\"");
1607 int c
= imp
->get_char();
1608 if (c
== '"' || c
== -1)
1611 val
+= static_cast<char>(c
);
1614 c
= imp
->get_char();
1615 if (c
== '\\' || c
== '"')
1616 val
+= static_cast<char>(c
);
1623 c
= imp
->get_char();
1624 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1625 c
= imp
->get_char();
1626 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1627 char v
= (vh
<< 4) | vl
;
1632 error_at(imp
->location(), "bad string constant");
1633 return Expression::make_error(imp
->location());
1637 return Expression::make_string(val
, imp
->location());
1640 // Ast dump for string expression.
1643 String_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
1645 String_expression::export_string(ast_dump_context
, this);
1648 // Make a string expression.
1651 Expression::make_string(const std::string
& val
, Location location
)
1653 return new String_expression(val
, location
);
1656 // An expression that evaluates to some characteristic of a string.
1657 // This is used when indexing, bound-checking, or nil checking a string.
1659 class String_info_expression
: public Expression
1662 String_info_expression(Expression
* string
, String_info string_info
,
1664 : Expression(EXPRESSION_STRING_INFO
, location
),
1665 string_(string
), string_info_(string_info
)
1673 do_determine_type(const Type_context
*)
1674 { go_unreachable(); }
1679 return new String_info_expression(this->string_
->copy(), this->string_info_
,
1684 do_get_backend(Translate_context
* context
);
1687 do_dump_expression(Ast_dump_context
*) const;
1690 do_issue_nil_check()
1691 { this->string_
->issue_nil_check(); }
1694 // The string for which we are getting information.
1695 Expression
* string_
;
1696 // What information we want.
1697 String_info string_info_
;
1700 // Return the type of the string info.
1703 String_info_expression::do_type()
1705 switch (this->string_info_
)
1707 case STRING_INFO_DATA
:
1709 Type
* byte_type
= Type::lookup_integer_type("uint8");
1710 return Type::make_pointer_type(byte_type
);
1712 case STRING_INFO_LENGTH
:
1713 return Type::lookup_integer_type("int");
1719 // Return string information in GENERIC.
1722 String_info_expression::do_get_backend(Translate_context
* context
)
1724 Gogo
* gogo
= context
->gogo();
1726 Bexpression
* bstring
= this->string_
->get_backend(context
);
1727 switch (this->string_info_
)
1729 case STRING_INFO_DATA
:
1730 case STRING_INFO_LENGTH
:
1731 return gogo
->backend()->struct_field_expression(bstring
,
1740 // Dump ast representation for a type info expression.
1743 String_info_expression::do_dump_expression(
1744 Ast_dump_context
* ast_dump_context
) const
1746 ast_dump_context
->ostream() << "stringinfo(";
1747 this->string_
->dump_expression(ast_dump_context
);
1748 ast_dump_context
->ostream() << ",";
1749 ast_dump_context
->ostream() <<
1750 (this->string_info_
== STRING_INFO_DATA
? "data"
1751 : this->string_info_
== STRING_INFO_LENGTH
? "length"
1753 ast_dump_context
->ostream() << ")";
1756 // Make a string info expression.
1759 Expression::make_string_info(Expression
* string
, String_info string_info
,
1762 return new String_info_expression(string
, string_info
, location
);
1765 // Make an integer expression.
1767 class Integer_expression
: public Expression
1770 Integer_expression(const mpz_t
* val
, Type
* type
, bool is_character_constant
,
1772 : Expression(EXPRESSION_INTEGER
, location
),
1773 type_(type
), is_character_constant_(is_character_constant
)
1774 { mpz_init_set(this->val_
, *val
); }
1779 // Write VAL to string dump.
1781 export_integer(String_dump
* exp
, const mpz_t val
);
1783 // Write VAL to dump context.
1785 dump_integer(Ast_dump_context
* ast_dump_context
, const mpz_t val
);
1789 do_is_constant() const
1793 do_is_immutable() const
1797 do_numeric_constant_value(Numeric_constant
* nc
) const;
1803 do_determine_type(const Type_context
* context
);
1806 do_check_types(Gogo
*);
1809 do_get_backend(Translate_context
*);
1814 if (this->is_character_constant_
)
1815 return Expression::make_character(&this->val_
, this->type_
,
1818 return Expression::make_integer_z(&this->val_
, this->type_
,
1823 do_export(Export
*) const;
1826 do_dump_expression(Ast_dump_context
*) const;
1829 // The integer value.
1833 // Whether this is a character constant.
1834 bool is_character_constant_
;
1837 // Return a numeric constant for this expression. We have to mark
1838 // this as a character when appropriate.
1841 Integer_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
1843 if (this->is_character_constant_
)
1844 nc
->set_rune(this->type_
, this->val_
);
1846 nc
->set_int(this->type_
, this->val_
);
1850 // Return the current type. If we haven't set the type yet, we return
1851 // an abstract integer type.
1854 Integer_expression::do_type()
1856 if (this->type_
== NULL
)
1858 if (this->is_character_constant_
)
1859 this->type_
= Type::make_abstract_character_type();
1861 this->type_
= Type::make_abstract_integer_type();
1866 // Set the type of the integer value. Here we may switch from an
1867 // abstract type to a real type.
1870 Integer_expression::do_determine_type(const Type_context
* context
)
1872 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1874 else if (context
->type
!= NULL
&& context
->type
->is_numeric_type())
1875 this->type_
= context
->type
;
1876 else if (!context
->may_be_abstract
)
1878 if (this->is_character_constant_
)
1879 this->type_
= Type::lookup_integer_type("int32");
1881 this->type_
= Type::lookup_integer_type("int");
1885 // Check the type of an integer constant.
1888 Integer_expression::do_check_types(Gogo
*)
1890 Type
* type
= this->type_
;
1893 Numeric_constant nc
;
1894 if (this->is_character_constant_
)
1895 nc
.set_rune(NULL
, this->val_
);
1897 nc
.set_int(NULL
, this->val_
);
1898 if (!nc
.set_type(type
, true, this->location()))
1899 this->set_is_error();
1902 // Get the backend representation for an integer constant.
1905 Integer_expression::do_get_backend(Translate_context
* context
)
1907 Type
* resolved_type
= NULL
;
1908 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1909 resolved_type
= this->type_
;
1910 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1912 // We are converting to an abstract floating point type.
1913 resolved_type
= Type::lookup_float_type("float64");
1915 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1917 // We are converting to an abstract complex type.
1918 resolved_type
= Type::lookup_complex_type("complex128");
1922 // If we still have an abstract type here, then this is being
1923 // used in a constant expression which didn't get reduced for
1924 // some reason. Use a type which will fit the value. We use <,
1925 // not <=, because we need an extra bit for the sign bit.
1926 int bits
= mpz_sizeinbase(this->val_
, 2);
1927 Type
* int_type
= Type::lookup_integer_type("int");
1928 if (bits
< int_type
->integer_type()->bits())
1929 resolved_type
= int_type
;
1931 resolved_type
= Type::lookup_integer_type("int64");
1935 error_at(this->location(),
1936 "unknown type for large integer constant");
1937 return context
->gogo()->backend()->error_expression();
1940 Numeric_constant nc
;
1941 nc
.set_int(resolved_type
, this->val_
);
1942 return Expression::backend_numeric_constant_expression(context
, &nc
);
1945 // Write VAL to export data.
1948 Integer_expression::export_integer(String_dump
* exp
, const mpz_t val
)
1950 char* s
= mpz_get_str(NULL
, 10, val
);
1951 exp
->write_c_string(s
);
1955 // Export an integer in a constant expression.
1958 Integer_expression::do_export(Export
* exp
) const
1960 Integer_expression::export_integer(exp
, this->val_
);
1961 if (this->is_character_constant_
)
1962 exp
->write_c_string("'");
1963 // A trailing space lets us reliably identify the end of the number.
1964 exp
->write_c_string(" ");
1967 // Import an integer, floating point, or complex value. This handles
1968 // all these types because they all start with digits.
1971 Integer_expression::do_import(Import
* imp
)
1973 std::string num
= imp
->read_identifier();
1974 imp
->require_c_string(" ");
1975 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1978 size_t plus_pos
= num
.find('+', 1);
1979 size_t minus_pos
= num
.find('-', 1);
1981 if (plus_pos
== std::string::npos
)
1983 else if (minus_pos
== std::string::npos
)
1987 error_at(imp
->location(), "bad number in import data: %qs",
1989 return Expression::make_error(imp
->location());
1991 if (pos
== std::string::npos
)
1992 mpfr_set_ui(real
, 0, GMP_RNDN
);
1995 std::string real_str
= num
.substr(0, pos
);
1996 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1998 error_at(imp
->location(), "bad number in import data: %qs",
2000 return Expression::make_error(imp
->location());
2004 std::string imag_str
;
2005 if (pos
== std::string::npos
)
2008 imag_str
= num
.substr(pos
);
2009 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
2011 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
2013 error_at(imp
->location(), "bad number in import data: %qs",
2015 return Expression::make_error(imp
->location());
2018 mpc_init2(cval
, mpc_precision
);
2019 mpc_set_fr_fr(cval
, real
, imag
, MPC_RNDNN
);
2022 Expression
* ret
= Expression::make_complex(&cval
, NULL
, imp
->location());
2026 else if (num
.find('.') == std::string::npos
2027 && num
.find('E') == std::string::npos
)
2029 bool is_character_constant
= (!num
.empty()
2030 && num
[num
.length() - 1] == '\'');
2031 if (is_character_constant
)
2032 num
= num
.substr(0, num
.length() - 1);
2034 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
2036 error_at(imp
->location(), "bad number in import data: %qs",
2038 return Expression::make_error(imp
->location());
2041 if (is_character_constant
)
2042 ret
= Expression::make_character(&val
, NULL
, imp
->location());
2044 ret
= Expression::make_integer_z(&val
, NULL
, imp
->location());
2051 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
2053 error_at(imp
->location(), "bad number in import data: %qs",
2055 return Expression::make_error(imp
->location());
2057 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
2062 // Ast dump for integer expression.
2065 Integer_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2067 if (this->is_character_constant_
)
2068 ast_dump_context
->ostream() << '\'';
2069 Integer_expression::export_integer(ast_dump_context
, this->val_
);
2070 if (this->is_character_constant_
)
2071 ast_dump_context
->ostream() << '\'';
2074 // Build a new integer value from a multi-precision integer.
2077 Expression::make_integer_z(const mpz_t
* val
, Type
* type
, Location location
)
2079 return new Integer_expression(val
, type
, false, location
);
2082 // Build a new integer value from an unsigned long.
2085 Expression::make_integer_ul(unsigned long val
, Type
*type
, Location location
)
2088 mpz_init_set_ui(zval
, val
);
2089 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2094 // Build a new integer value from a signed long.
2097 Expression::make_integer_sl(long val
, Type
*type
, Location location
)
2100 mpz_init_set_si(zval
, val
);
2101 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2106 // Store an int64_t in an uninitialized mpz_t.
2109 set_mpz_from_int64(mpz_t
* zval
, int64_t val
)
2113 unsigned long ul
= static_cast<unsigned long>(val
);
2114 if (static_cast<int64_t>(ul
) == val
)
2116 mpz_init_set_ui(*zval
, ul
);
2122 uv
= static_cast<uint64_t>(val
);
2124 uv
= static_cast<uint64_t>(- val
);
2125 unsigned long ul
= uv
& 0xffffffffUL
;
2126 mpz_init_set_ui(*zval
, ul
);
2128 mpz_init_set_ui(hval
, static_cast<unsigned long>(uv
>> 32));
2129 mpz_mul_2exp(hval
, hval
, 32);
2130 mpz_add(*zval
, *zval
, hval
);
2133 mpz_neg(*zval
, *zval
);
2136 // Build a new integer value from an int64_t.
2139 Expression::make_integer_int64(int64_t val
, Type
* type
, Location location
)
2142 set_mpz_from_int64(&zval
, val
);
2143 Expression
* ret
= Expression::make_integer_z(&zval
, type
, location
);
2148 // Build a new character constant value.
2151 Expression::make_character(const mpz_t
* val
, Type
* type
, Location location
)
2153 return new Integer_expression(val
, type
, true, location
);
2158 class Float_expression
: public Expression
2161 Float_expression(const mpfr_t
* val
, Type
* type
, Location location
)
2162 : Expression(EXPRESSION_FLOAT
, location
),
2165 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
2168 // Write VAL to export data.
2170 export_float(String_dump
* exp
, const mpfr_t val
);
2172 // Write VAL to dump file.
2174 dump_float(Ast_dump_context
* ast_dump_context
, const mpfr_t val
);
2178 do_is_constant() const
2182 do_is_immutable() const
2186 do_numeric_constant_value(Numeric_constant
* nc
) const
2188 nc
->set_float(this->type_
, this->val_
);
2196 do_determine_type(const Type_context
*);
2199 do_check_types(Gogo
*);
2203 { return Expression::make_float(&this->val_
, this->type_
,
2204 this->location()); }
2207 do_get_backend(Translate_context
*);
2210 do_export(Export
*) const;
2213 do_dump_expression(Ast_dump_context
*) const;
2216 // The floating point value.
2222 // Return the current type. If we haven't set the type yet, we return
2223 // an abstract float type.
2226 Float_expression::do_type()
2228 if (this->type_
== NULL
)
2229 this->type_
= Type::make_abstract_float_type();
2233 // Set the type of the float value. Here we may switch from an
2234 // abstract type to a real type.
2237 Float_expression::do_determine_type(const Type_context
* context
)
2239 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2241 else if (context
->type
!= NULL
2242 && (context
->type
->integer_type() != NULL
2243 || context
->type
->float_type() != NULL
2244 || context
->type
->complex_type() != NULL
))
2245 this->type_
= context
->type
;
2246 else if (!context
->may_be_abstract
)
2247 this->type_
= Type::lookup_float_type("float64");
2250 // Check the type of a float value.
2253 Float_expression::do_check_types(Gogo
*)
2255 Type
* type
= this->type_
;
2258 Numeric_constant nc
;
2259 nc
.set_float(NULL
, this->val_
);
2260 if (!nc
.set_type(this->type_
, true, this->location()))
2261 this->set_is_error();
2264 // Get the backend representation for a float constant.
2267 Float_expression::do_get_backend(Translate_context
* context
)
2269 Type
* resolved_type
;
2270 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2271 resolved_type
= this->type_
;
2272 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2274 // We have an abstract integer type. We just hope for the best.
2275 resolved_type
= Type::lookup_integer_type("int");
2277 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
2279 // We are converting to an abstract complex type.
2280 resolved_type
= Type::lookup_complex_type("complex128");
2284 // If we still have an abstract type here, then this is being
2285 // used in a constant expression which didn't get reduced. We
2286 // just use float64 and hope for the best.
2287 resolved_type
= Type::lookup_float_type("float64");
2290 Numeric_constant nc
;
2291 nc
.set_float(resolved_type
, this->val_
);
2292 return Expression::backend_numeric_constant_expression(context
, &nc
);
2295 // Write a floating point number to a string dump.
2298 Float_expression::export_float(String_dump
*exp
, const mpfr_t val
)
2301 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2303 exp
->write_c_string("-");
2304 exp
->write_c_string("0.");
2305 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2308 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2309 exp
->write_c_string(buf
);
2312 // Export a floating point number in a constant expression.
2315 Float_expression::do_export(Export
* exp
) const
2317 Float_expression::export_float(exp
, this->val_
);
2318 // A trailing space lets us reliably identify the end of the number.
2319 exp
->write_c_string(" ");
2322 // Dump a floating point number to the dump file.
2325 Float_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2327 Float_expression::export_float(ast_dump_context
, this->val_
);
2330 // Make a float expression.
2333 Expression::make_float(const mpfr_t
* val
, Type
* type
, Location location
)
2335 return new Float_expression(val
, type
, location
);
2340 class Complex_expression
: public Expression
2343 Complex_expression(const mpc_t
* val
, Type
* type
, Location location
)
2344 : Expression(EXPRESSION_COMPLEX
, location
),
2347 mpc_init2(this->val_
, mpc_precision
);
2348 mpc_set(this->val_
, *val
, MPC_RNDNN
);
2351 // Write VAL to string dump.
2353 export_complex(String_dump
* exp
, const mpc_t val
);
2355 // Write REAL/IMAG to dump context.
2357 dump_complex(Ast_dump_context
* ast_dump_context
, const mpc_t val
);
2361 do_is_constant() const
2365 do_is_immutable() const
2369 do_numeric_constant_value(Numeric_constant
* nc
) const
2371 nc
->set_complex(this->type_
, this->val_
);
2379 do_determine_type(const Type_context
*);
2382 do_check_types(Gogo
*);
2387 return Expression::make_complex(&this->val_
, this->type_
,
2392 do_get_backend(Translate_context
*);
2395 do_export(Export
*) const;
2398 do_dump_expression(Ast_dump_context
*) const;
2401 // The complex value.
2403 // The type if known.
2407 // Return the current type. If we haven't set the type yet, we return
2408 // an abstract complex type.
2411 Complex_expression::do_type()
2413 if (this->type_
== NULL
)
2414 this->type_
= Type::make_abstract_complex_type();
2418 // Set the type of the complex value. Here we may switch from an
2419 // abstract type to a real type.
2422 Complex_expression::do_determine_type(const Type_context
* context
)
2424 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2426 else if (context
->type
!= NULL
&& context
->type
->is_numeric_type())
2427 this->type_
= context
->type
;
2428 else if (!context
->may_be_abstract
)
2429 this->type_
= Type::lookup_complex_type("complex128");
2432 // Check the type of a complex value.
2435 Complex_expression::do_check_types(Gogo
*)
2437 Type
* type
= this->type_
;
2440 Numeric_constant nc
;
2441 nc
.set_complex(NULL
, this->val_
);
2442 if (!nc
.set_type(this->type_
, true, this->location()))
2443 this->set_is_error();
2446 // Get the backend representation for a complex constant.
2449 Complex_expression::do_get_backend(Translate_context
* context
)
2451 Type
* resolved_type
;
2452 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2453 resolved_type
= this->type_
;
2454 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2456 // We are converting to an abstract integer type.
2457 resolved_type
= Type::lookup_integer_type("int");
2459 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
2461 // We are converting to an abstract float type.
2462 resolved_type
= Type::lookup_float_type("float64");
2466 // If we still have an abstract type here, this is being
2467 // used in a constant expression which didn't get reduced. We
2468 // just use complex128 and hope for the best.
2469 resolved_type
= Type::lookup_complex_type("complex128");
2472 Numeric_constant nc
;
2473 nc
.set_complex(resolved_type
, this->val_
);
2474 return Expression::backend_numeric_constant_expression(context
, &nc
);
2477 // Write REAL/IMAG to export data.
2480 Complex_expression::export_complex(String_dump
* exp
, const mpc_t val
)
2482 if (!mpfr_zero_p(mpc_realref(val
)))
2484 Float_expression::export_float(exp
, mpc_realref(val
));
2485 if (mpfr_sgn(mpc_imagref(val
)) >= 0)
2486 exp
->write_c_string("+");
2488 Float_expression::export_float(exp
, mpc_imagref(val
));
2489 exp
->write_c_string("i");
2492 // Export a complex number in a constant expression.
2495 Complex_expression::do_export(Export
* exp
) const
2497 Complex_expression::export_complex(exp
, this->val_
);
2498 // A trailing space lets us reliably identify the end of the number.
2499 exp
->write_c_string(" ");
2502 // Dump a complex expression to the dump file.
2505 Complex_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2507 Complex_expression::export_complex(ast_dump_context
, this->val_
);
2510 // Make a complex expression.
2513 Expression::make_complex(const mpc_t
* val
, Type
* type
, Location location
)
2515 return new Complex_expression(val
, type
, location
);
2518 // Find a named object in an expression.
2520 class Find_named_object
: public Traverse
2523 Find_named_object(Named_object
* no
)
2524 : Traverse(traverse_expressions
),
2525 no_(no
), found_(false)
2528 // Whether we found the object.
2531 { return this->found_
; }
2535 expression(Expression
**);
2538 // The object we are looking for.
2540 // Whether we found it.
2544 // A reference to a const in an expression.
2546 class Const_expression
: public Expression
2549 Const_expression(Named_object
* constant
, Location location
)
2550 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2551 constant_(constant
), type_(NULL
), seen_(false)
2556 { return this->constant_
; }
2558 // Check that the initializer does not refer to the constant itself.
2560 check_for_init_loop();
2564 do_traverse(Traverse
*);
2567 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2570 do_is_constant() const
2574 do_is_immutable() const
2578 do_numeric_constant_value(Numeric_constant
* nc
) const;
2581 do_string_constant_value(std::string
* val
) const;
2586 // The type of a const is set by the declaration, not the use.
2588 do_determine_type(const Type_context
*);
2591 do_check_types(Gogo
*);
2598 do_get_backend(Translate_context
* context
);
2600 // When exporting a reference to a const as part of a const
2601 // expression, we export the value. We ignore the fact that it has
2604 do_export(Export
* exp
) const
2605 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2608 do_dump_expression(Ast_dump_context
*) const;
2612 Named_object
* constant_
;
2613 // The type of this reference. This is used if the constant has an
2616 // Used to prevent infinite recursion when a constant incorrectly
2617 // refers to itself.
2624 Const_expression::do_traverse(Traverse
* traverse
)
2626 if (this->type_
!= NULL
)
2627 return Type::traverse(this->type_
, traverse
);
2628 return TRAVERSE_CONTINUE
;
2631 // Lower a constant expression. This is where we convert the
2632 // predeclared constant iota into an integer value.
2635 Const_expression::do_lower(Gogo
* gogo
, Named_object
*,
2636 Statement_inserter
*, int iota_value
)
2638 if (this->constant_
->const_value()->expr()->classification()
2641 if (iota_value
== -1)
2643 error_at(this->location(),
2644 "iota is only defined in const declarations");
2647 return Expression::make_integer_ul(iota_value
, NULL
, this->location());
2650 // Make sure that the constant itself has been lowered.
2651 gogo
->lower_constant(this->constant_
);
2656 // Return a numeric constant value.
2659 Const_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
2664 Expression
* e
= this->constant_
->const_value()->expr();
2668 bool r
= e
->numeric_constant_value(nc
);
2670 this->seen_
= false;
2673 if (this->type_
!= NULL
)
2674 ctype
= this->type_
;
2676 ctype
= this->constant_
->const_value()->type();
2677 if (r
&& ctype
!= NULL
)
2679 if (!nc
->set_type(ctype
, false, this->location()))
2687 Const_expression::do_string_constant_value(std::string
* val
) const
2692 Expression
* e
= this->constant_
->const_value()->expr();
2695 bool ok
= e
->string_constant_value(val
);
2696 this->seen_
= false;
2701 // Return the type of the const reference.
2704 Const_expression::do_type()
2706 if (this->type_
!= NULL
)
2709 Named_constant
* nc
= this->constant_
->const_value();
2711 if (this->seen_
|| nc
->lowering())
2713 this->report_error(_("constant refers to itself"));
2714 this->type_
= Type::make_error_type();
2720 Type
* ret
= nc
->type();
2724 this->seen_
= false;
2728 // During parsing, a named constant may have a NULL type, but we
2729 // must not return a NULL type here.
2730 ret
= nc
->expr()->type();
2732 this->seen_
= false;
2737 // Set the type of the const reference.
2740 Const_expression::do_determine_type(const Type_context
* context
)
2742 Type
* ctype
= this->constant_
->const_value()->type();
2743 Type
* cetype
= (ctype
!= NULL
2745 : this->constant_
->const_value()->expr()->type());
2746 if (ctype
!= NULL
&& !ctype
->is_abstract())
2748 else if (context
->type
!= NULL
2749 && context
->type
->is_numeric_type()
2750 && cetype
->is_numeric_type())
2751 this->type_
= context
->type
;
2752 else if (context
->type
!= NULL
2753 && context
->type
->is_string_type()
2754 && cetype
->is_string_type())
2755 this->type_
= context
->type
;
2756 else if (context
->type
!= NULL
2757 && context
->type
->is_boolean_type()
2758 && cetype
->is_boolean_type())
2759 this->type_
= context
->type
;
2760 else if (!context
->may_be_abstract
)
2762 if (cetype
->is_abstract())
2763 cetype
= cetype
->make_non_abstract_type();
2764 this->type_
= cetype
;
2768 // Check for a loop in which the initializer of a constant refers to
2769 // the constant itself.
2772 Const_expression::check_for_init_loop()
2774 if (this->type_
!= NULL
&& this->type_
->is_error())
2779 this->report_error(_("constant refers to itself"));
2780 this->type_
= Type::make_error_type();
2784 Expression
* init
= this->constant_
->const_value()->expr();
2785 Find_named_object
find_named_object(this->constant_
);
2788 Expression::traverse(&init
, &find_named_object
);
2789 this->seen_
= false;
2791 if (find_named_object
.found())
2793 if (this->type_
== NULL
|| !this->type_
->is_error())
2795 this->report_error(_("constant refers to itself"));
2796 this->type_
= Type::make_error_type();
2802 // Check types of a const reference.
2805 Const_expression::do_check_types(Gogo
*)
2807 if (this->type_
!= NULL
&& this->type_
->is_error())
2810 this->check_for_init_loop();
2812 // Check that numeric constant fits in type.
2813 if (this->type_
!= NULL
&& this->type_
->is_numeric_type())
2815 Numeric_constant nc
;
2816 if (this->constant_
->const_value()->expr()->numeric_constant_value(&nc
))
2818 if (!nc
.set_type(this->type_
, true, this->location()))
2819 this->set_is_error();
2824 // Return the backend representation for a const reference.
2827 Const_expression::do_get_backend(Translate_context
* context
)
2829 if (this->type_
!= NULL
&& this->type_
->is_error())
2830 return context
->backend()->error_expression();
2832 // If the type has been set for this expression, but the underlying
2833 // object is an abstract int or float, we try to get the abstract
2834 // value. Otherwise we may lose something in the conversion.
2835 Expression
* expr
= this->constant_
->const_value()->expr();
2836 if (this->type_
!= NULL
2837 && this->type_
->is_numeric_type()
2838 && (this->constant_
->const_value()->type() == NULL
2839 || this->constant_
->const_value()->type()->is_abstract()))
2841 Numeric_constant nc
;
2842 if (expr
->numeric_constant_value(&nc
)
2843 && nc
.set_type(this->type_
, false, this->location()))
2845 Expression
* e
= nc
.expression(this->location());
2846 return e
->get_backend(context
);
2850 if (this->type_
!= NULL
)
2851 expr
= Expression::make_cast(this->type_
, expr
, this->location());
2852 return expr
->get_backend(context
);
2855 // Dump ast representation for constant expression.
2858 Const_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2860 ast_dump_context
->ostream() << this->constant_
->name();
2863 // Make a reference to a constant in an expression.
2866 Expression::make_const_reference(Named_object
* constant
,
2869 return new Const_expression(constant
, location
);
2872 // Find a named object in an expression.
2875 Find_named_object::expression(Expression
** pexpr
)
2877 switch ((*pexpr
)->classification())
2879 case Expression::EXPRESSION_CONST_REFERENCE
:
2881 Const_expression
* ce
= static_cast<Const_expression
*>(*pexpr
);
2882 if (ce
->named_object() == this->no_
)
2885 // We need to check a constant initializer explicitly, as
2886 // loops here will not be caught by the loop checking for
2887 // variable initializers.
2888 ce
->check_for_init_loop();
2890 return TRAVERSE_CONTINUE
;
2893 case Expression::EXPRESSION_VAR_REFERENCE
:
2894 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2896 return TRAVERSE_CONTINUE
;
2897 case Expression::EXPRESSION_FUNC_REFERENCE
:
2898 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2900 return TRAVERSE_CONTINUE
;
2902 return TRAVERSE_CONTINUE
;
2904 this->found_
= true;
2905 return TRAVERSE_EXIT
;
2910 class Nil_expression
: public Expression
2913 Nil_expression(Location location
)
2914 : Expression(EXPRESSION_NIL
, location
)
2922 do_is_constant() const
2926 do_is_immutable() const
2931 { return Type::make_nil_type(); }
2934 do_determine_type(const Type_context
*)
2942 do_get_backend(Translate_context
* context
)
2943 { return context
->backend()->nil_pointer_expression(); }
2946 do_export(Export
* exp
) const
2947 { exp
->write_c_string("nil"); }
2950 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2951 { ast_dump_context
->ostream() << "nil"; }
2954 // Import a nil expression.
2957 Nil_expression::do_import(Import
* imp
)
2959 imp
->require_c_string("nil");
2960 return Expression::make_nil(imp
->location());
2963 // Make a nil expression.
2966 Expression::make_nil(Location location
)
2968 return new Nil_expression(location
);
2971 // The value of the predeclared constant iota. This is little more
2972 // than a marker. This will be lowered to an integer in
2973 // Const_expression::do_lower, which is where we know the value that
2976 class Iota_expression
: public Parser_expression
2979 Iota_expression(Location location
)
2980 : Parser_expression(EXPRESSION_IOTA
, location
)
2985 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
2986 { go_unreachable(); }
2988 // There should only ever be one of these.
2991 { go_unreachable(); }
2994 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2995 { ast_dump_context
->ostream() << "iota"; }
2998 // Make an iota expression. This is only called for one case: the
2999 // value of the predeclared constant iota.
3002 Expression::make_iota()
3004 static Iota_expression
iota_expression(Linemap::unknown_location());
3005 return &iota_expression
;
3008 // Class Type_conversion_expression.
3013 Type_conversion_expression::do_traverse(Traverse
* traverse
)
3015 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3016 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3017 return TRAVERSE_EXIT
;
3018 return TRAVERSE_CONTINUE
;
3021 // Convert to a constant at lowering time.
3024 Type_conversion_expression::do_lower(Gogo
*, Named_object
*,
3025 Statement_inserter
*, int)
3027 Type
* type
= this->type_
;
3028 Expression
* val
= this->expr_
;
3029 Location location
= this->location();
3031 if (type
->is_numeric_type())
3033 Numeric_constant nc
;
3034 if (val
->numeric_constant_value(&nc
))
3036 if (!nc
.set_type(type
, true, location
))
3037 return Expression::make_error(location
);
3038 return nc
.expression(location
);
3042 if (type
->is_slice_type())
3044 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3045 bool is_byte
= (element_type
->integer_type() != NULL
3046 && element_type
->integer_type()->is_byte());
3047 bool is_rune
= (element_type
->integer_type() != NULL
3048 && element_type
->integer_type()->is_rune());
3049 if (is_byte
|| is_rune
)
3052 if (val
->string_constant_value(&s
))
3054 Expression_list
* vals
= new Expression_list();
3057 for (std::string::const_iterator p
= s
.begin();
3061 unsigned char c
= static_cast<unsigned char>(*p
);
3062 vals
->push_back(Expression::make_integer_ul(c
,
3069 const char *p
= s
.data();
3070 const char *pend
= s
.data() + s
.length();
3074 int adv
= Lex::fetch_char(p
, &c
);
3077 warning_at(this->location(), 0,
3078 "invalid UTF-8 encoding");
3082 vals
->push_back(Expression::make_integer_ul(c
,
3088 return Expression::make_slice_composite_literal(type
, vals
,
3097 // Flatten a type conversion by using a temporary variable for the slice
3098 // in slice to string conversions.
3101 Type_conversion_expression::do_flatten(Gogo
*, Named_object
*,
3102 Statement_inserter
* inserter
)
3104 if (this->type()->is_error_type() || this->expr_
->is_error_expression())
3106 go_assert(saw_errors());
3107 return Expression::make_error(this->location());
3110 if (((this->type()->is_string_type()
3111 && this->expr_
->type()->is_slice_type())
3112 || this->expr_
->type()->interface_type() != NULL
)
3113 && !this->expr_
->is_variable())
3115 Temporary_statement
* temp
=
3116 Statement::make_temporary(NULL
, this->expr_
, this->location());
3117 inserter
->insert(temp
);
3118 this->expr_
= Expression::make_temporary_reference(temp
, this->location());
3123 // Return whether a type conversion is a constant.
3126 Type_conversion_expression::do_is_constant() const
3128 if (!this->expr_
->is_constant())
3131 // A conversion to a type that may not be used as a constant is not
3132 // a constant. For example, []byte(nil).
3133 Type
* type
= this->type_
;
3134 if (type
->integer_type() == NULL
3135 && type
->float_type() == NULL
3136 && type
->complex_type() == NULL
3137 && !type
->is_boolean_type()
3138 && !type
->is_string_type())
3144 // Return whether a type conversion is immutable.
3147 Type_conversion_expression::do_is_immutable() const
3149 Type
* type
= this->type_
;
3150 Type
* expr_type
= this->expr_
->type();
3152 if (type
->interface_type() != NULL
3153 || expr_type
->interface_type() != NULL
)
3156 if (!this->expr_
->is_immutable())
3159 if (Type::are_identical(type
, expr_type
, false, NULL
))
3162 return type
->is_basic_type() && expr_type
->is_basic_type();
3165 // Return the constant numeric value if there is one.
3168 Type_conversion_expression::do_numeric_constant_value(
3169 Numeric_constant
* nc
) const
3171 if (!this->type_
->is_numeric_type())
3173 if (!this->expr_
->numeric_constant_value(nc
))
3175 return nc
->set_type(this->type_
, false, this->location());
3178 // Return the constant string value if there is one.
3181 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3183 if (this->type_
->is_string_type()
3184 && this->expr_
->type()->integer_type() != NULL
)
3186 Numeric_constant nc
;
3187 if (this->expr_
->numeric_constant_value(&nc
))
3190 if (nc
.to_unsigned_long(&ival
) == Numeric_constant::NC_UL_VALID
)
3193 Lex::append_char(ival
, true, val
, this->location());
3199 // FIXME: Could handle conversion from const []int here.
3204 // Determine the resulting type of the conversion.
3207 Type_conversion_expression::do_determine_type(const Type_context
*)
3209 Type_context
subcontext(this->type_
, false);
3210 this->expr_
->determine_type(&subcontext
);
3213 // Check that types are convertible.
3216 Type_conversion_expression::do_check_types(Gogo
*)
3218 Type
* type
= this->type_
;
3219 Type
* expr_type
= this->expr_
->type();
3222 if (type
->is_error() || expr_type
->is_error())
3224 this->set_is_error();
3228 if (this->may_convert_function_types_
3229 && type
->function_type() != NULL
3230 && expr_type
->function_type() != NULL
)
3233 if (Type::are_convertible(type
, expr_type
, &reason
))
3236 error_at(this->location(), "%s", reason
.c_str());
3237 this->set_is_error();
3240 // Get the backend representation for a type conversion.
3243 Type_conversion_expression::do_get_backend(Translate_context
* context
)
3245 Type
* type
= this->type_
;
3246 Type
* expr_type
= this->expr_
->type();
3248 Gogo
* gogo
= context
->gogo();
3249 Btype
* btype
= type
->get_backend(gogo
);
3250 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
3251 Location loc
= this->location();
3253 if (Type::are_identical(type
, expr_type
, false, NULL
))
3254 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3255 else if (type
->interface_type() != NULL
3256 || expr_type
->interface_type() != NULL
)
3258 Expression
* conversion
=
3259 Expression::convert_for_assignment(gogo
, type
, this->expr_
,
3261 return conversion
->get_backend(context
);
3263 else if (type
->is_string_type()
3264 && expr_type
->integer_type() != NULL
)
3267 Numeric_constant nc
;
3268 if (this->expr_
->numeric_constant_value(&nc
)
3269 && nc
.to_int(&intval
)
3270 && mpz_fits_ushort_p(intval
))
3273 Lex::append_char(mpz_get_ui(intval
), true, &s
, loc
);
3275 Expression
* se
= Expression::make_string(s
, loc
);
3276 return se
->get_backend(context
);
3279 Expression
* i2s_expr
=
3280 Runtime::make_call(Runtime::INT_TO_STRING
, loc
, 1, this->expr_
);
3281 return Expression::make_cast(type
, i2s_expr
, loc
)->get_backend(context
);
3283 else if (type
->is_string_type() && expr_type
->is_slice_type())
3285 Array_type
* a
= expr_type
->array_type();
3286 Type
* e
= a
->element_type()->forwarded();
3287 go_assert(e
->integer_type() != NULL
);
3288 go_assert(this->expr_
->is_variable());
3290 Runtime::Function code
;
3291 if (e
->integer_type()->is_byte())
3292 code
= Runtime::BYTE_ARRAY_TO_STRING
;
3295 go_assert(e
->integer_type()->is_rune());
3296 code
= Runtime::INT_ARRAY_TO_STRING
;
3298 Expression
* valptr
= a
->get_value_pointer(gogo
, this->expr_
);
3299 Expression
* len
= a
->get_length(gogo
, this->expr_
);
3300 return Runtime::make_call(code
, loc
, 2, valptr
,
3301 len
)->get_backend(context
);
3303 else if (type
->is_slice_type() && expr_type
->is_string_type())
3305 Type
* e
= type
->array_type()->element_type()->forwarded();
3306 go_assert(e
->integer_type() != NULL
);
3308 Runtime::Function code
;
3309 if (e
->integer_type()->is_byte())
3310 code
= Runtime::STRING_TO_BYTE_ARRAY
;
3313 go_assert(e
->integer_type()->is_rune());
3314 code
= Runtime::STRING_TO_INT_ARRAY
;
3316 Expression
* s2a
= Runtime::make_call(code
, loc
, 1, this->expr_
);
3317 return Expression::make_unsafe_cast(type
, s2a
, loc
)->get_backend(context
);
3319 else if (type
->is_numeric_type())
3321 go_assert(Type::are_convertible(type
, expr_type
, NULL
));
3322 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3324 else if ((type
->is_unsafe_pointer_type()
3325 && (expr_type
->points_to() != NULL
3326 || expr_type
->integer_type()))
3327 || (expr_type
->is_unsafe_pointer_type()
3328 && type
->points_to() != NULL
)
3329 || (this->may_convert_function_types_
3330 && type
->function_type() != NULL
3331 && expr_type
->function_type() != NULL
))
3332 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3335 Expression
* conversion
=
3336 Expression::convert_for_assignment(gogo
, type
, this->expr_
, loc
);
3337 return conversion
->get_backend(context
);
3341 // Output a type conversion in a constant expression.
3344 Type_conversion_expression::do_export(Export
* exp
) const
3346 exp
->write_c_string("convert(");
3347 exp
->write_type(this->type_
);
3348 exp
->write_c_string(", ");
3349 this->expr_
->export_expression(exp
);
3350 exp
->write_c_string(")");
3353 // Import a type conversion or a struct construction.
3356 Type_conversion_expression::do_import(Import
* imp
)
3358 imp
->require_c_string("convert(");
3359 Type
* type
= imp
->read_type();
3360 imp
->require_c_string(", ");
3361 Expression
* val
= Expression::import_expression(imp
);
3362 imp
->require_c_string(")");
3363 return Expression::make_cast(type
, val
, imp
->location());
3366 // Dump ast representation for a type conversion expression.
3369 Type_conversion_expression::do_dump_expression(
3370 Ast_dump_context
* ast_dump_context
) const
3372 ast_dump_context
->dump_type(this->type_
);
3373 ast_dump_context
->ostream() << "(";
3374 ast_dump_context
->dump_expression(this->expr_
);
3375 ast_dump_context
->ostream() << ") ";
3378 // Make a type cast expression.
3381 Expression::make_cast(Type
* type
, Expression
* val
, Location location
)
3383 if (type
->is_error_type() || val
->is_error_expression())
3384 return Expression::make_error(location
);
3385 return new Type_conversion_expression(type
, val
, location
);
3388 // Class Unsafe_type_conversion_expression.
3393 Unsafe_type_conversion_expression::do_traverse(Traverse
* traverse
)
3395 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3396 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3397 return TRAVERSE_EXIT
;
3398 return TRAVERSE_CONTINUE
;
3401 // Return whether an unsafe type conversion is immutable.
3404 Unsafe_type_conversion_expression::do_is_immutable() const
3406 Type
* type
= this->type_
;
3407 Type
* expr_type
= this->expr_
->type();
3409 if (type
->interface_type() != NULL
3410 || expr_type
->interface_type() != NULL
)
3413 if (!this->expr_
->is_immutable())
3416 if (Type::are_convertible(type
, expr_type
, NULL
))
3419 return type
->is_basic_type() && expr_type
->is_basic_type();
3422 // Convert to backend representation.
3425 Unsafe_type_conversion_expression::do_get_backend(Translate_context
* context
)
3427 // We are only called for a limited number of cases.
3429 Type
* t
= this->type_
;
3430 Type
* et
= this->expr_
->type();
3431 if (t
->array_type() != NULL
)
3432 go_assert(et
->array_type() != NULL
3433 && t
->is_slice_type() == et
->is_slice_type());
3434 else if (t
->struct_type() != NULL
)
3436 if (t
->named_type() != NULL
3437 && et
->named_type() != NULL
3438 && !Type::are_convertible(t
, et
, NULL
))
3440 go_assert(saw_errors());
3441 return context
->backend()->error_expression();
3444 go_assert(et
->struct_type() != NULL
3445 && Type::are_convertible(t
, et
, NULL
));
3447 else if (t
->map_type() != NULL
)
3448 go_assert(et
->map_type() != NULL
);
3449 else if (t
->channel_type() != NULL
)
3450 go_assert(et
->channel_type() != NULL
);
3451 else if (t
->points_to() != NULL
)
3452 go_assert(et
->points_to() != NULL
3453 || et
->channel_type() != NULL
3454 || et
->map_type() != NULL
3455 || et
->function_type() != NULL
3456 || et
->is_nil_type());
3457 else if (et
->is_unsafe_pointer_type())
3458 go_assert(t
->points_to() != NULL
);
3459 else if (t
->interface_type() != NULL
)
3461 bool empty_iface
= t
->interface_type()->is_empty();
3462 go_assert(et
->interface_type() != NULL
3463 && et
->interface_type()->is_empty() == empty_iface
);
3465 else if (t
->integer_type() != NULL
)
3466 go_assert(et
->is_boolean_type()
3467 || et
->integer_type() != NULL
3468 || et
->function_type() != NULL
3469 || et
->points_to() != NULL
3470 || et
->map_type() != NULL
3471 || et
->channel_type() != NULL
3472 || et
->is_nil_type());
3476 Gogo
* gogo
= context
->gogo();
3477 Btype
* btype
= t
->get_backend(gogo
);
3478 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
3479 Location loc
= this->location();
3480 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3483 // Dump ast representation for an unsafe type conversion expression.
3486 Unsafe_type_conversion_expression::do_dump_expression(
3487 Ast_dump_context
* ast_dump_context
) const
3489 ast_dump_context
->dump_type(this->type_
);
3490 ast_dump_context
->ostream() << "(";
3491 ast_dump_context
->dump_expression(this->expr_
);
3492 ast_dump_context
->ostream() << ") ";
3495 // Make an unsafe type conversion expression.
3498 Expression::make_unsafe_cast(Type
* type
, Expression
* expr
,
3501 return new Unsafe_type_conversion_expression(type
, expr
, location
);
3504 // Class Unary_expression.
3506 // If we are taking the address of a composite literal, and the
3507 // contents are not constant, then we want to make a heap expression
3511 Unary_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
3513 Location loc
= this->location();
3514 Operator op
= this->op_
;
3515 Expression
* expr
= this->expr_
;
3517 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3518 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3520 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3521 // moving x to the heap. FIXME: Is it worth doing a real escape
3522 // analysis here? This case is found in math/unsafe.go and is
3523 // therefore worth special casing.
3524 if (op
== OPERATOR_MULT
)
3526 Expression
* e
= expr
;
3527 while (e
->classification() == EXPRESSION_CONVERSION
)
3529 Type_conversion_expression
* te
3530 = static_cast<Type_conversion_expression
*>(e
);
3534 if (e
->classification() == EXPRESSION_UNARY
)
3536 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3537 if (ue
->op_
== OPERATOR_AND
)
3542 if (!ue
->expr_
->is_addressable() && !ue
->create_temp_
)
3544 error_at(ue
->location(),
3545 "invalid operand for unary %<&%>");
3546 this->set_is_error();
3550 ue
->set_does_not_escape();
3555 // Catching an invalid indirection of unsafe.Pointer here avoid
3556 // having to deal with TYPE_VOID in other places.
3557 if (op
== OPERATOR_MULT
&& expr
->type()->is_unsafe_pointer_type())
3559 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3560 return Expression::make_error(this->location());
3563 // Check for an invalid pointer dereference. We need to do this
3564 // here because Unary_expression::do_type will return an error type
3565 // in this case. That can cause code to appear erroneous, and
3566 // therefore disappear at lowering time, without any error message.
3567 if (op
== OPERATOR_MULT
&& expr
->type()->points_to() == NULL
)
3569 this->report_error(_("expected pointer"));
3570 return Expression::make_error(this->location());
3573 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
|| op
== OPERATOR_XOR
)
3575 Numeric_constant nc
;
3576 if (expr
->numeric_constant_value(&nc
))
3578 Numeric_constant result
;
3579 if (Unary_expression::eval_constant(op
, &nc
, loc
, &result
))
3580 return result
.expression(loc
);
3587 // Flatten expression if a nil check must be performed and create temporary
3588 // variables if necessary.
3591 Unary_expression::do_flatten(Gogo
* gogo
, Named_object
*,
3592 Statement_inserter
* inserter
)
3594 if (this->is_error_expression()
3595 || this->expr_
->is_error_expression()
3596 || this->expr_
->type()->is_error_type())
3598 go_assert(saw_errors());
3599 return Expression::make_error(this->location());
3602 Location location
= this->location();
3603 if (this->op_
== OPERATOR_MULT
3604 && !this->expr_
->is_variable())
3606 go_assert(this->expr_
->type()->points_to() != NULL
);
3607 Type
* ptype
= this->expr_
->type()->points_to();
3608 if (!ptype
->is_void_type())
3610 Btype
* pbtype
= ptype
->get_backend(gogo
);
3611 int64_t s
= gogo
->backend()->type_size(pbtype
);
3612 if (s
>= 4096 || this->issue_nil_check_
)
3614 Temporary_statement
* temp
=
3615 Statement::make_temporary(NULL
, this->expr_
, location
);
3616 inserter
->insert(temp
);
3618 Expression::make_temporary_reference(temp
, location
);
3623 if (this->op_
== OPERATOR_AND
)
3625 // If this->escapes_ is false at this point, then it was set to
3626 // false by an explicit call to set_does_not_escape, and the
3627 // value does not escape. If this->escapes_ is true, we may be
3628 // able to set it to false if taking the address of a variable
3629 // that does not escape.
3630 if (this->escapes_
&& this->expr_
->var_expression() != NULL
)
3632 Named_object
* var
= this->expr_
->var_expression()->named_object();
3633 if (var
->is_variable())
3634 this->escapes_
= var
->var_value()->escapes();
3635 if (var
->is_result_variable())
3636 this->escapes_
= var
->result_var_value()->escapes();
3638 this->expr_
->address_taken(this->escapes_
);
3641 if (this->create_temp_
&& !this->expr_
->is_variable())
3643 Temporary_statement
* temp
=
3644 Statement::make_temporary(NULL
, this->expr_
, location
);
3645 inserter
->insert(temp
);
3646 this->expr_
= Expression::make_temporary_reference(temp
, location
);
3652 // Return whether a unary expression is a constant.
3655 Unary_expression::do_is_constant() const
3657 if (this->op_
== OPERATOR_MULT
)
3659 // Indirecting through a pointer is only constant if the object
3660 // to which the expression points is constant, but we currently
3661 // have no way to determine that.
3664 else if (this->op_
== OPERATOR_AND
)
3666 // Taking the address of a variable is constant if it is a
3667 // global variable, not constant otherwise. In other cases taking the
3668 // address is probably not a constant.
3669 Var_expression
* ve
= this->expr_
->var_expression();
3672 Named_object
* no
= ve
->named_object();
3673 return no
->is_variable() && no
->var_value()->is_global();
3678 return this->expr_
->is_constant();
3681 // Apply unary opcode OP to UNC, setting NC. Return true if this
3682 // could be done, false if not. Issue errors for overflow.
3685 Unary_expression::eval_constant(Operator op
, const Numeric_constant
* unc
,
3686 Location location
, Numeric_constant
* nc
)
3694 case OPERATOR_MINUS
:
3695 if (unc
->is_int() || unc
->is_rune())
3697 else if (unc
->is_float())
3700 unc
->get_float(&uval
);
3703 mpfr_neg(val
, uval
, GMP_RNDN
);
3704 nc
->set_float(unc
->type(), val
);
3709 else if (unc
->is_complex())
3712 unc
->get_complex(&uval
);
3714 mpc_init2(val
, mpc_precision
);
3715 mpc_neg(val
, uval
, MPC_RNDNN
);
3716 nc
->set_complex(unc
->type(), val
);
3736 if (!unc
->is_int() && !unc
->is_rune())
3741 unc
->get_rune(&uval
);
3743 unc
->get_int(&uval
);
3749 case OPERATOR_MINUS
:
3754 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3759 Type
* utype
= unc
->type();
3760 if (utype
->integer_type() == NULL
3761 || utype
->integer_type()->is_abstract())
3765 // The number of HOST_WIDE_INTs that it takes to represent
3767 size_t count
= ((mpz_sizeinbase(uval
, 2)
3768 + HOST_BITS_PER_WIDE_INT
3770 / HOST_BITS_PER_WIDE_INT
);
3772 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3773 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3775 size_t obits
= utype
->integer_type()->bits();
3777 if (!utype
->integer_type()->is_unsigned() && mpz_sgn(uval
) < 0)
3780 mpz_init_set_ui(adj
, 1);
3781 mpz_mul_2exp(adj
, adj
, obits
);
3782 mpz_add(uval
, uval
, adj
);
3787 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3788 go_assert(ecount
<= count
);
3790 // Trim down to the number of words required by the type.
3791 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3792 / HOST_BITS_PER_WIDE_INT
);
3793 go_assert(ocount
<= count
);
3795 for (size_t i
= 0; i
< ocount
; ++i
)
3798 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3800 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3803 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3805 if (!utype
->integer_type()->is_unsigned()
3806 && mpz_tstbit(val
, obits
- 1))
3809 mpz_init_set_ui(adj
, 1);
3810 mpz_mul_2exp(adj
, adj
, obits
);
3811 mpz_sub(val
, val
, adj
);
3825 nc
->set_rune(NULL
, val
);
3827 nc
->set_int(NULL
, val
);
3832 return nc
->set_type(unc
->type(), true, location
);
3835 // Return the integral constant value of a unary expression, if it has one.
3838 Unary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
3840 Numeric_constant unc
;
3841 if (!this->expr_
->numeric_constant_value(&unc
))
3843 return Unary_expression::eval_constant(this->op_
, &unc
, this->location(),
3847 // Return the type of a unary expression.
3850 Unary_expression::do_type()
3855 case OPERATOR_MINUS
:
3858 return this->expr_
->type();
3861 return Type::make_pointer_type(this->expr_
->type());
3865 Type
* subtype
= this->expr_
->type();
3866 Type
* points_to
= subtype
->points_to();
3867 if (points_to
== NULL
)
3868 return Type::make_error_type();
3877 // Determine abstract types for a unary expression.
3880 Unary_expression::do_determine_type(const Type_context
* context
)
3885 case OPERATOR_MINUS
:
3888 this->expr_
->determine_type(context
);
3892 // Taking the address of something.
3894 Type
* subtype
= (context
->type
== NULL
3896 : context
->type
->points_to());
3897 Type_context
subcontext(subtype
, false);
3898 this->expr_
->determine_type(&subcontext
);
3903 // Indirecting through a pointer.
3905 Type
* subtype
= (context
->type
== NULL
3907 : Type::make_pointer_type(context
->type
));
3908 Type_context
subcontext(subtype
, false);
3909 this->expr_
->determine_type(&subcontext
);
3918 // Check types for a unary expression.
3921 Unary_expression::do_check_types(Gogo
*)
3923 Type
* type
= this->expr_
->type();
3924 if (type
->is_error())
3926 this->set_is_error();
3933 case OPERATOR_MINUS
:
3934 if (type
->integer_type() == NULL
3935 && type
->float_type() == NULL
3936 && type
->complex_type() == NULL
)
3937 this->report_error(_("expected numeric type"));
3941 if (!type
->is_boolean_type())
3942 this->report_error(_("expected boolean type"));
3946 if (type
->integer_type() == NULL
)
3947 this->report_error(_("expected integer"));
3951 if (!this->expr_
->is_addressable())
3953 if (!this->create_temp_
)
3955 error_at(this->location(), "invalid operand for unary %<&%>");
3956 this->set_is_error();
3960 this->expr_
->issue_nil_check();
3964 // Indirecting through a pointer.
3965 if (type
->points_to() == NULL
)
3966 this->report_error(_("expected pointer"));
3967 if (type
->points_to()->is_error())
3968 this->set_is_error();
3976 // Get the backend representation for a unary expression.
3979 Unary_expression::do_get_backend(Translate_context
* context
)
3981 Gogo
* gogo
= context
->gogo();
3982 Location loc
= this->location();
3984 // Taking the address of a set-and-use-temporary expression requires
3985 // setting the temporary and then taking the address.
3986 if (this->op_
== OPERATOR_AND
)
3988 Set_and_use_temporary_expression
* sut
=
3989 this->expr_
->set_and_use_temporary_expression();
3992 Temporary_statement
* temp
= sut
->temporary();
3993 Bvariable
* bvar
= temp
->get_backend_variable(context
);
3994 Bexpression
* bvar_expr
= gogo
->backend()->var_expression(bvar
, loc
);
3995 Bexpression
* bval
= sut
->expression()->get_backend(context
);
3997 Bstatement
* bassign
=
3998 gogo
->backend()->assignment_statement(bvar_expr
, bval
, loc
);
3999 Bexpression
* bvar_addr
=
4000 gogo
->backend()->address_expression(bvar_expr
, loc
);
4001 return gogo
->backend()->compound_expression(bassign
, bvar_addr
, loc
);
4006 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
4007 Btype
* btype
= this->expr_
->type()->get_backend(gogo
);
4014 case OPERATOR_MINUS
:
4015 ret
= gogo
->backend()->unary_expression(this->op_
, bexpr
, loc
);
4016 ret
= gogo
->backend()->convert_expression(btype
, ret
, loc
);
4021 ret
= gogo
->backend()->unary_expression(this->op_
, bexpr
, loc
);
4025 if (!this->create_temp_
)
4027 // We should not see a non-constant constructor here; cases
4028 // where we would see one should have been moved onto the
4029 // heap at parse time. Taking the address of a nonconstant
4030 // constructor will not do what the programmer expects.
4032 go_assert(!this->expr_
->is_composite_literal()
4033 || this->expr_
->is_immutable());
4034 if (this->expr_
->classification() == EXPRESSION_UNARY
)
4036 Unary_expression
* ue
=
4037 static_cast<Unary_expression
*>(this->expr_
);
4038 go_assert(ue
->op() != OPERATOR_AND
);
4042 static unsigned int counter
;
4044 if (this->is_gc_root_
|| this->is_slice_init_
)
4046 bool copy_to_heap
= false;
4047 if (this->is_gc_root_
)
4049 // Build a decl for a GC root variable. GC roots are mutable, so
4050 // they cannot be represented as an immutable_struct in the
4052 static unsigned int root_counter
;
4053 snprintf(buf
, sizeof buf
, "gc%u", root_counter
);
4058 // Build a decl for a slice value initializer. An immutable slice
4059 // value initializer may have to be copied to the heap if it
4060 // contains pointers in a non-constant context.
4061 snprintf(buf
, sizeof buf
, "C%u", counter
);
4064 Array_type
* at
= this->expr_
->type()->array_type();
4065 go_assert(at
!= NULL
);
4067 // If we are not copying the value to the heap, we will only
4068 // initialize the value once, so we can use this directly
4069 // rather than copying it. In that case we can't make it
4070 // read-only, because the program is permitted to change it.
4071 copy_to_heap
= (at
->element_type()->has_pointer()
4072 && !context
->is_const());
4074 Bvariable
* implicit
=
4075 gogo
->backend()->implicit_variable(buf
, btype
, true, copy_to_heap
,
4077 gogo
->backend()->implicit_variable_set_init(implicit
, buf
, btype
,
4078 true, copy_to_heap
, false,
4080 bexpr
= gogo
->backend()->var_expression(implicit
, loc
);
4082 else if ((this->expr_
->is_composite_literal()
4083 || this->expr_
->string_expression() != NULL
)
4084 && this->expr_
->is_immutable())
4086 // Build a decl for a constant constructor.
4087 snprintf(buf
, sizeof buf
, "C%u", counter
);
4091 gogo
->backend()->immutable_struct(buf
, true, false, btype
, loc
);
4092 gogo
->backend()->immutable_struct_set_init(decl
, buf
, true, false,
4094 bexpr
= gogo
->backend()->var_expression(decl
, loc
);
4097 go_assert(!this->create_temp_
|| this->expr_
->is_variable());
4098 ret
= gogo
->backend()->address_expression(bexpr
, loc
);
4103 go_assert(this->expr_
->type()->points_to() != NULL
);
4105 // If we are dereferencing the pointer to a large struct, we
4106 // need to check for nil. We don't bother to check for small
4107 // structs because we expect the system to crash on a nil
4108 // pointer dereference. However, if we know the address of this
4109 // expression is being taken, we must always check for nil.
4111 Type
* ptype
= this->expr_
->type()->points_to();
4112 Btype
* pbtype
= ptype
->get_backend(gogo
);
4113 if (!ptype
->is_void_type())
4115 int64_t s
= gogo
->backend()->type_size(pbtype
);
4116 if (s
>= 4096 || this->issue_nil_check_
)
4118 go_assert(this->expr_
->is_variable());
4120 Expression::make_nil(loc
)->get_backend(context
);
4121 Bexpression
* compare
=
4122 gogo
->backend()->binary_expression(OPERATOR_EQEQ
, bexpr
,
4124 Bexpression
* crash
=
4125 gogo
->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4126 loc
)->get_backend(context
);
4127 bexpr
= gogo
->backend()->conditional_expression(btype
, compare
,
4133 ret
= gogo
->backend()->indirect_expression(pbtype
, bexpr
, false, loc
);
4144 // Export a unary expression.
4147 Unary_expression::do_export(Export
* exp
) const
4152 exp
->write_c_string("+ ");
4154 case OPERATOR_MINUS
:
4155 exp
->write_c_string("- ");
4158 exp
->write_c_string("! ");
4161 exp
->write_c_string("^ ");
4168 this->expr_
->export_expression(exp
);
4171 // Import a unary expression.
4174 Unary_expression::do_import(Import
* imp
)
4177 switch (imp
->get_char())
4183 op
= OPERATOR_MINUS
;
4194 imp
->require_c_string(" ");
4195 Expression
* expr
= Expression::import_expression(imp
);
4196 return Expression::make_unary(op
, expr
, imp
->location());
4199 // Dump ast representation of an unary expression.
4202 Unary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
4204 ast_dump_context
->dump_operator(this->op_
);
4205 ast_dump_context
->ostream() << "(";
4206 ast_dump_context
->dump_expression(this->expr_
);
4207 ast_dump_context
->ostream() << ") ";
4210 // Make a unary expression.
4213 Expression::make_unary(Operator op
, Expression
* expr
, Location location
)
4215 return new Unary_expression(op
, expr
, location
);
4218 // If this is an indirection through a pointer, return the expression
4219 // being pointed through. Otherwise return this.
4224 if (this->classification_
== EXPRESSION_UNARY
)
4226 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4227 if (ue
->op() == OPERATOR_MULT
)
4228 return ue
->operand();
4233 // Class Binary_expression.
4238 Binary_expression::do_traverse(Traverse
* traverse
)
4240 int t
= Expression::traverse(&this->left_
, traverse
);
4241 if (t
== TRAVERSE_EXIT
)
4242 return TRAVERSE_EXIT
;
4243 return Expression::traverse(&this->right_
, traverse
);
4246 // Return the type to use for a binary operation on operands of
4247 // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
4248 // such may be NULL or abstract.
4251 Binary_expression::operation_type(Operator op
, Type
* left_type
,
4252 Type
* right_type
, Type
** result_type
)
4254 if (left_type
!= right_type
4255 && !left_type
->is_abstract()
4256 && !right_type
->is_abstract()
4257 && left_type
->base() != right_type
->base()
4258 && op
!= OPERATOR_LSHIFT
4259 && op
!= OPERATOR_RSHIFT
)
4261 // May be a type error--let it be diagnosed elsewhere.
4265 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4267 if (left_type
->integer_type() != NULL
)
4268 *result_type
= left_type
;
4270 *result_type
= Type::make_abstract_integer_type();
4272 else if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
4273 *result_type
= left_type
;
4274 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
4275 *result_type
= right_type
;
4276 else if (!left_type
->is_abstract())
4277 *result_type
= left_type
;
4278 else if (!right_type
->is_abstract())
4279 *result_type
= right_type
;
4280 else if (left_type
->complex_type() != NULL
)
4281 *result_type
= left_type
;
4282 else if (right_type
->complex_type() != NULL
)
4283 *result_type
= right_type
;
4284 else if (left_type
->float_type() != NULL
)
4285 *result_type
= left_type
;
4286 else if (right_type
->float_type() != NULL
)
4287 *result_type
= right_type
;
4288 else if (left_type
->integer_type() != NULL
4289 && left_type
->integer_type()->is_rune())
4290 *result_type
= left_type
;
4291 else if (right_type
->integer_type() != NULL
4292 && right_type
->integer_type()->is_rune())
4293 *result_type
= right_type
;
4295 *result_type
= left_type
;
4300 // Convert an integer comparison code and an operator to a boolean
4304 Binary_expression::cmp_to_bool(Operator op
, int cmp
)
4311 case OPERATOR_NOTEQ
:
4328 // Compare constants according to OP.
4331 Binary_expression::compare_constant(Operator op
, Numeric_constant
* left_nc
,
4332 Numeric_constant
* right_nc
,
4333 Location location
, bool* result
)
4335 Type
* left_type
= left_nc
->type();
4336 Type
* right_type
= right_nc
->type();
4339 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
4342 // When comparing an untyped operand to a typed operand, we are
4343 // effectively coercing the untyped operand to the other operand's
4344 // type, so make sure that is valid.
4345 if (!left_nc
->set_type(type
, true, location
)
4346 || !right_nc
->set_type(type
, true, location
))
4351 if (type
->complex_type() != NULL
)
4353 if (op
!= OPERATOR_EQEQ
&& op
!= OPERATOR_NOTEQ
)
4355 ret
= Binary_expression::compare_complex(left_nc
, right_nc
, &cmp
);
4357 else if (type
->float_type() != NULL
)
4358 ret
= Binary_expression::compare_float(left_nc
, right_nc
, &cmp
);
4360 ret
= Binary_expression::compare_integer(left_nc
, right_nc
, &cmp
);
4363 *result
= Binary_expression::cmp_to_bool(op
, cmp
);
4368 // Compare integer constants.
4371 Binary_expression::compare_integer(const Numeric_constant
* left_nc
,
4372 const Numeric_constant
* right_nc
,
4376 if (!left_nc
->to_int(&left_val
))
4379 if (!right_nc
->to_int(&right_val
))
4381 mpz_clear(left_val
);
4385 *cmp
= mpz_cmp(left_val
, right_val
);
4387 mpz_clear(left_val
);
4388 mpz_clear(right_val
);
4393 // Compare floating point constants.
4396 Binary_expression::compare_float(const Numeric_constant
* left_nc
,
4397 const Numeric_constant
* right_nc
,
4401 if (!left_nc
->to_float(&left_val
))
4404 if (!right_nc
->to_float(&right_val
))
4406 mpfr_clear(left_val
);
4410 // We already coerced both operands to the same type. If that type
4411 // is not an abstract type, we need to round the values accordingly.
4412 Type
* type
= left_nc
->type();
4413 if (!type
->is_abstract() && type
->float_type() != NULL
)
4415 int bits
= type
->float_type()->bits();
4416 mpfr_prec_round(left_val
, bits
, GMP_RNDN
);
4417 mpfr_prec_round(right_val
, bits
, GMP_RNDN
);
4420 *cmp
= mpfr_cmp(left_val
, right_val
);
4422 mpfr_clear(left_val
);
4423 mpfr_clear(right_val
);
4428 // Compare complex constants. Complex numbers may only be compared
4432 Binary_expression::compare_complex(const Numeric_constant
* left_nc
,
4433 const Numeric_constant
* right_nc
,
4437 if (!left_nc
->to_complex(&left_val
))
4440 if (!right_nc
->to_complex(&right_val
))
4442 mpc_clear(left_val
);
4446 // We already coerced both operands to the same type. If that type
4447 // is not an abstract type, we need to round the values accordingly.
4448 Type
* type
= left_nc
->type();
4449 if (!type
->is_abstract() && type
->complex_type() != NULL
)
4451 int bits
= type
->complex_type()->bits();
4452 mpfr_prec_round(mpc_realref(left_val
), bits
/ 2, GMP_RNDN
);
4453 mpfr_prec_round(mpc_imagref(left_val
), bits
/ 2, GMP_RNDN
);
4454 mpfr_prec_round(mpc_realref(right_val
), bits
/ 2, GMP_RNDN
);
4455 mpfr_prec_round(mpc_imagref(right_val
), bits
/ 2, GMP_RNDN
);
4458 *cmp
= mpc_cmp(left_val
, right_val
) != 0;
4460 mpc_clear(left_val
);
4461 mpc_clear(right_val
);
4466 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
4467 // true if this could be done, false if not. Issue errors at LOCATION
4471 Binary_expression::eval_constant(Operator op
, Numeric_constant
* left_nc
,
4472 Numeric_constant
* right_nc
,
4473 Location location
, Numeric_constant
* nc
)
4478 case OPERATOR_ANDAND
:
4480 case OPERATOR_NOTEQ
:
4485 // These return boolean values, not numeric.
4491 Type
* left_type
= left_nc
->type();
4492 Type
* right_type
= right_nc
->type();
4495 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
4498 bool is_shift
= op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
;
4500 // When combining an untyped operand with a typed operand, we are
4501 // effectively coercing the untyped operand to the other operand's
4502 // type, so make sure that is valid.
4503 if (!left_nc
->set_type(type
, true, location
))
4505 if (!is_shift
&& !right_nc
->set_type(type
, true, location
))
4509 if (type
->complex_type() != NULL
)
4510 r
= Binary_expression::eval_complex(op
, left_nc
, right_nc
, location
, nc
);
4511 else if (type
->float_type() != NULL
)
4512 r
= Binary_expression::eval_float(op
, left_nc
, right_nc
, location
, nc
);
4514 r
= Binary_expression::eval_integer(op
, left_nc
, right_nc
, location
, nc
);
4517 r
= nc
->set_type(type
, true, location
);
4522 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4523 // integer operations. Return true if this could be done, false if
4527 Binary_expression::eval_integer(Operator op
, const Numeric_constant
* left_nc
,
4528 const Numeric_constant
* right_nc
,
4529 Location location
, Numeric_constant
* nc
)
4532 if (!left_nc
->to_int(&left_val
))
4535 if (!right_nc
->to_int(&right_val
))
4537 mpz_clear(left_val
);
4547 mpz_add(val
, left_val
, right_val
);
4548 if (mpz_sizeinbase(val
, 2) > 0x100000)
4550 error_at(location
, "constant addition overflow");
4554 case OPERATOR_MINUS
:
4555 mpz_sub(val
, left_val
, right_val
);
4556 if (mpz_sizeinbase(val
, 2) > 0x100000)
4558 error_at(location
, "constant subtraction overflow");
4563 mpz_ior(val
, left_val
, right_val
);
4566 mpz_xor(val
, left_val
, right_val
);
4569 mpz_mul(val
, left_val
, right_val
);
4570 if (mpz_sizeinbase(val
, 2) > 0x100000)
4572 error_at(location
, "constant multiplication overflow");
4577 if (mpz_sgn(right_val
) != 0)
4578 mpz_tdiv_q(val
, left_val
, right_val
);
4581 error_at(location
, "division by zero");
4586 if (mpz_sgn(right_val
) != 0)
4587 mpz_tdiv_r(val
, left_val
, right_val
);
4590 error_at(location
, "division by zero");
4594 case OPERATOR_LSHIFT
:
4596 unsigned long shift
= mpz_get_ui(right_val
);
4597 if (mpz_cmp_ui(right_val
, shift
) == 0 && shift
<= 0x100000)
4598 mpz_mul_2exp(val
, left_val
, shift
);
4601 error_at(location
, "shift count overflow");
4607 case OPERATOR_RSHIFT
:
4609 unsigned long shift
= mpz_get_ui(right_val
);
4610 if (mpz_cmp_ui(right_val
, shift
) != 0)
4612 error_at(location
, "shift count overflow");
4617 if (mpz_cmp_ui(left_val
, 0) >= 0)
4618 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4620 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4626 mpz_and(val
, left_val
, right_val
);
4628 case OPERATOR_BITCLEAR
:
4632 mpz_com(tval
, right_val
);
4633 mpz_and(val
, left_val
, tval
);
4641 mpz_clear(left_val
);
4642 mpz_clear(right_val
);
4644 if (left_nc
->is_rune()
4645 || (op
!= OPERATOR_LSHIFT
4646 && op
!= OPERATOR_RSHIFT
4647 && right_nc
->is_rune()))
4648 nc
->set_rune(NULL
, val
);
4650 nc
->set_int(NULL
, val
);
4657 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4658 // floating point operations. Return true if this could be done,
4662 Binary_expression::eval_float(Operator op
, const Numeric_constant
* left_nc
,
4663 const Numeric_constant
* right_nc
,
4664 Location location
, Numeric_constant
* nc
)
4667 if (!left_nc
->to_float(&left_val
))
4670 if (!right_nc
->to_float(&right_val
))
4672 mpfr_clear(left_val
);
4683 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4685 case OPERATOR_MINUS
:
4686 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4691 case OPERATOR_BITCLEAR
:
4693 case OPERATOR_LSHIFT
:
4694 case OPERATOR_RSHIFT
:
4695 mpfr_set_ui(val
, 0, GMP_RNDN
);
4699 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4702 if (!mpfr_zero_p(right_val
))
4703 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4706 error_at(location
, "division by zero");
4707 mpfr_set_ui(val
, 0, GMP_RNDN
);
4714 mpfr_clear(left_val
);
4715 mpfr_clear(right_val
);
4717 nc
->set_float(NULL
, val
);
4723 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4724 // complex operations. Return true if this could be done, false if
4728 Binary_expression::eval_complex(Operator op
, const Numeric_constant
* left_nc
,
4729 const Numeric_constant
* right_nc
,
4730 Location location
, Numeric_constant
* nc
)
4733 if (!left_nc
->to_complex(&left_val
))
4736 if (!right_nc
->to_complex(&right_val
))
4738 mpc_clear(left_val
);
4743 mpc_init2(val
, mpc_precision
);
4749 mpc_add(val
, left_val
, right_val
, MPC_RNDNN
);
4751 case OPERATOR_MINUS
:
4752 mpc_sub(val
, left_val
, right_val
, MPC_RNDNN
);
4757 case OPERATOR_BITCLEAR
:
4759 case OPERATOR_LSHIFT
:
4760 case OPERATOR_RSHIFT
:
4761 mpc_set_ui(val
, 0, MPC_RNDNN
);
4765 mpc_mul(val
, left_val
, right_val
, MPC_RNDNN
);
4768 if (mpc_cmp_si(right_val
, 0) == 0)
4770 error_at(location
, "division by zero");
4771 mpc_set_ui(val
, 0, MPC_RNDNN
);
4774 mpc_div(val
, left_val
, right_val
, MPC_RNDNN
);
4780 mpc_clear(left_val
);
4781 mpc_clear(right_val
);
4783 nc
->set_complex(NULL
, val
);
4789 // Lower a binary expression. We have to evaluate constant
4790 // expressions now, in order to implement Go's unlimited precision
4794 Binary_expression::do_lower(Gogo
* gogo
, Named_object
*,
4795 Statement_inserter
* inserter
, int)
4797 Location location
= this->location();
4798 Operator op
= this->op_
;
4799 Expression
* left
= this->left_
;
4800 Expression
* right
= this->right_
;
4802 const bool is_comparison
= (op
== OPERATOR_EQEQ
4803 || op
== OPERATOR_NOTEQ
4804 || op
== OPERATOR_LT
4805 || op
== OPERATOR_LE
4806 || op
== OPERATOR_GT
4807 || op
== OPERATOR_GE
);
4809 // Numeric constant expressions.
4811 Numeric_constant left_nc
;
4812 Numeric_constant right_nc
;
4813 if (left
->numeric_constant_value(&left_nc
)
4814 && right
->numeric_constant_value(&right_nc
))
4819 if (!Binary_expression::compare_constant(op
, &left_nc
,
4820 &right_nc
, location
,
4823 return Expression::make_cast(Type::make_boolean_type(),
4824 Expression::make_boolean(result
,
4830 Numeric_constant nc
;
4831 if (!Binary_expression::eval_constant(op
, &left_nc
, &right_nc
,
4834 return nc
.expression(location
);
4839 // String constant expressions.
4840 if (left
->type()->is_string_type() && right
->type()->is_string_type())
4842 std::string left_string
;
4843 std::string right_string
;
4844 if (left
->string_constant_value(&left_string
)
4845 && right
->string_constant_value(&right_string
))
4847 if (op
== OPERATOR_PLUS
)
4848 return Expression::make_string(left_string
+ right_string
,
4850 else if (is_comparison
)
4852 int cmp
= left_string
.compare(right_string
);
4853 bool r
= Binary_expression::cmp_to_bool(op
, cmp
);
4854 return Expression::make_boolean(r
, location
);
4859 // Lower struct, array, and some interface comparisons.
4860 if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
4862 if (left
->type()->struct_type() != NULL
4863 && right
->type()->struct_type() != NULL
)
4864 return this->lower_struct_comparison(gogo
, inserter
);
4865 else if (left
->type()->array_type() != NULL
4866 && !left
->type()->is_slice_type()
4867 && right
->type()->array_type() != NULL
4868 && !right
->type()->is_slice_type())
4869 return this->lower_array_comparison(gogo
, inserter
);
4870 else if ((left
->type()->interface_type() != NULL
4871 && right
->type()->interface_type() == NULL
)
4872 || (left
->type()->interface_type() == NULL
4873 && right
->type()->interface_type() != NULL
))
4874 return this->lower_interface_value_comparison(gogo
, inserter
);
4880 // Lower a struct comparison.
4883 Binary_expression::lower_struct_comparison(Gogo
* gogo
,
4884 Statement_inserter
* inserter
)
4886 Struct_type
* st
= this->left_
->type()->struct_type();
4887 Struct_type
* st2
= this->right_
->type()->struct_type();
4890 if (st
!= st2
&& !Type::are_identical(st
, st2
, false, NULL
))
4892 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
4893 this->right_
->type(), NULL
))
4896 // See if we can compare using memcmp. As a heuristic, we use
4897 // memcmp rather than field references and comparisons if there are
4898 // more than two fields.
4899 if (st
->compare_is_identity(gogo
) && st
->total_field_count() > 2)
4900 return this->lower_compare_to_memcmp(gogo
, inserter
);
4902 Location loc
= this->location();
4904 Expression
* left
= this->left_
;
4905 Temporary_statement
* left_temp
= NULL
;
4906 if (left
->var_expression() == NULL
4907 && left
->temporary_reference_expression() == NULL
)
4909 left_temp
= Statement::make_temporary(left
->type(), NULL
, loc
);
4910 inserter
->insert(left_temp
);
4911 left
= Expression::make_set_and_use_temporary(left_temp
, left
, loc
);
4914 Expression
* right
= this->right_
;
4915 Temporary_statement
* right_temp
= NULL
;
4916 if (right
->var_expression() == NULL
4917 && right
->temporary_reference_expression() == NULL
)
4919 right_temp
= Statement::make_temporary(right
->type(), NULL
, loc
);
4920 inserter
->insert(right_temp
);
4921 right
= Expression::make_set_and_use_temporary(right_temp
, right
, loc
);
4924 Expression
* ret
= Expression::make_boolean(true, loc
);
4925 const Struct_field_list
* fields
= st
->fields();
4926 unsigned int field_index
= 0;
4927 for (Struct_field_list::const_iterator pf
= fields
->begin();
4928 pf
!= fields
->end();
4929 ++pf
, ++field_index
)
4931 if (Gogo::is_sink_name(pf
->field_name()))
4934 if (field_index
> 0)
4936 if (left_temp
== NULL
)
4937 left
= left
->copy();
4939 left
= Expression::make_temporary_reference(left_temp
, loc
);
4940 if (right_temp
== NULL
)
4941 right
= right
->copy();
4943 right
= Expression::make_temporary_reference(right_temp
, loc
);
4945 Expression
* f1
= Expression::make_field_reference(left
, field_index
,
4947 Expression
* f2
= Expression::make_field_reference(right
, field_index
,
4949 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, f1
, f2
, loc
);
4950 ret
= Expression::make_binary(OPERATOR_ANDAND
, ret
, cond
, loc
);
4953 if (this->op_
== OPERATOR_NOTEQ
)
4954 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
4959 // Lower an array comparison.
4962 Binary_expression::lower_array_comparison(Gogo
* gogo
,
4963 Statement_inserter
* inserter
)
4965 Array_type
* at
= this->left_
->type()->array_type();
4966 Array_type
* at2
= this->right_
->type()->array_type();
4969 if (at
!= at2
&& !Type::are_identical(at
, at2
, false, NULL
))
4971 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
4972 this->right_
->type(), NULL
))
4975 // Call memcmp directly if possible. This may let the middle-end
4976 // optimize the call.
4977 if (at
->compare_is_identity(gogo
))
4978 return this->lower_compare_to_memcmp(gogo
, inserter
);
4980 // Call the array comparison function.
4981 Named_object
* hash_fn
;
4982 Named_object
* equal_fn
;
4983 at
->type_functions(gogo
, this->left_
->type()->named_type(), NULL
, NULL
,
4984 &hash_fn
, &equal_fn
);
4986 Location loc
= this->location();
4988 Expression
* func
= Expression::make_func_reference(equal_fn
, NULL
, loc
);
4990 Expression_list
* args
= new Expression_list();
4991 args
->push_back(this->operand_address(inserter
, this->left_
));
4992 args
->push_back(this->operand_address(inserter
, this->right_
));
4993 args
->push_back(Expression::make_type_info(at
, TYPE_INFO_SIZE
));
4995 Expression
* ret
= Expression::make_call(func
, args
, false, loc
);
4997 if (this->op_
== OPERATOR_NOTEQ
)
4998 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
5003 // Lower an interface to value comparison.
5006 Binary_expression::lower_interface_value_comparison(Gogo
*,
5007 Statement_inserter
* inserter
)
5009 Type
* left_type
= this->left_
->type();
5010 Type
* right_type
= this->right_
->type();
5011 Interface_type
* ift
;
5012 if (left_type
->interface_type() != NULL
)
5014 ift
= left_type
->interface_type();
5015 if (!ift
->implements_interface(right_type
, NULL
))
5020 ift
= right_type
->interface_type();
5021 if (!ift
->implements_interface(left_type
, NULL
))
5024 if (!Type::are_compatible_for_comparison(true, left_type
, right_type
, NULL
))
5027 Location loc
= this->location();
5029 if (left_type
->interface_type() == NULL
5030 && left_type
->points_to() == NULL
5031 && !this->left_
->is_addressable())
5033 Temporary_statement
* temp
=
5034 Statement::make_temporary(left_type
, NULL
, loc
);
5035 inserter
->insert(temp
);
5037 Expression::make_set_and_use_temporary(temp
, this->left_
, loc
);
5040 if (right_type
->interface_type() == NULL
5041 && right_type
->points_to() == NULL
5042 && !this->right_
->is_addressable())
5044 Temporary_statement
* temp
=
5045 Statement::make_temporary(right_type
, NULL
, loc
);
5046 inserter
->insert(temp
);
5048 Expression::make_set_and_use_temporary(temp
, this->right_
, loc
);
5054 // Lower a struct or array comparison to a call to memcmp.
5057 Binary_expression::lower_compare_to_memcmp(Gogo
*, Statement_inserter
* inserter
)
5059 Location loc
= this->location();
5061 Expression
* a1
= this->operand_address(inserter
, this->left_
);
5062 Expression
* a2
= this->operand_address(inserter
, this->right_
);
5063 Expression
* len
= Expression::make_type_info(this->left_
->type(),
5066 Expression
* call
= Runtime::make_call(Runtime::MEMCMP
, loc
, 3, a1
, a2
, len
);
5067 Expression
* zero
= Expression::make_integer_ul(0, NULL
, loc
);
5068 return Expression::make_binary(this->op_
, call
, zero
, loc
);
5072 Binary_expression::do_flatten(Gogo
* gogo
, Named_object
*,
5073 Statement_inserter
* inserter
)
5075 Location loc
= this->location();
5076 if (this->left_
->type()->is_error_type()
5077 || this->right_
->type()->is_error_type()
5078 || this->left_
->is_error_expression()
5079 || this->right_
->is_error_expression())
5081 go_assert(saw_errors());
5082 return Expression::make_error(loc
);
5085 Temporary_statement
* temp
;
5086 if (this->left_
->type()->is_string_type()
5087 && this->op_
== OPERATOR_PLUS
)
5089 if (!this->left_
->is_variable()
5090 && !this->left_
->is_constant())
5092 temp
= Statement::make_temporary(NULL
, this->left_
, loc
);
5093 inserter
->insert(temp
);
5094 this->left_
= Expression::make_temporary_reference(temp
, loc
);
5096 if (!this->right_
->is_variable()
5097 && !this->right_
->is_constant())
5100 Statement::make_temporary(this->left_
->type(), this->right_
, loc
);
5101 this->right_
= Expression::make_temporary_reference(temp
, loc
);
5102 inserter
->insert(temp
);
5106 Type
* left_type
= this->left_
->type();
5107 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5108 || this->op_
== OPERATOR_RSHIFT
);
5109 bool is_idiv_op
= ((this->op_
== OPERATOR_DIV
&&
5110 left_type
->integer_type() != NULL
)
5111 || this->op_
== OPERATOR_MOD
);
5115 && (gogo
->check_divide_by_zero() || gogo
->check_divide_overflow())))
5117 if (!this->left_
->is_variable())
5119 temp
= Statement::make_temporary(NULL
, this->left_
, loc
);
5120 inserter
->insert(temp
);
5121 this->left_
= Expression::make_temporary_reference(temp
, loc
);
5123 if (!this->right_
->is_variable())
5126 Statement::make_temporary(NULL
, this->right_
, loc
);
5127 this->right_
= Expression::make_temporary_reference(temp
, loc
);
5128 inserter
->insert(temp
);
5135 // Return the address of EXPR, cast to unsafe.Pointer.
5138 Binary_expression::operand_address(Statement_inserter
* inserter
,
5141 Location loc
= this->location();
5143 if (!expr
->is_addressable())
5145 Temporary_statement
* temp
= Statement::make_temporary(expr
->type(), NULL
,
5147 inserter
->insert(temp
);
5148 expr
= Expression::make_set_and_use_temporary(temp
, expr
, loc
);
5150 expr
= Expression::make_unary(OPERATOR_AND
, expr
, loc
);
5151 static_cast<Unary_expression
*>(expr
)->set_does_not_escape();
5152 Type
* void_type
= Type::make_void_type();
5153 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
5154 return Expression::make_cast(unsafe_pointer_type
, expr
, loc
);
5157 // Return the numeric constant value, if it has one.
5160 Binary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
5162 Numeric_constant left_nc
;
5163 if (!this->left_
->numeric_constant_value(&left_nc
))
5165 Numeric_constant right_nc
;
5166 if (!this->right_
->numeric_constant_value(&right_nc
))
5168 return Binary_expression::eval_constant(this->op_
, &left_nc
, &right_nc
,
5169 this->location(), nc
);
5172 // Note that the value is being discarded.
5175 Binary_expression::do_discarding_value()
5177 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5178 return this->right_
->discarding_value();
5181 this->unused_value_error();
5189 Binary_expression::do_type()
5191 if (this->classification() == EXPRESSION_ERROR
)
5192 return Type::make_error_type();
5197 case OPERATOR_NOTEQ
:
5202 if (this->type_
== NULL
)
5203 this->type_
= Type::make_boolean_type();
5207 case OPERATOR_MINUS
:
5214 case OPERATOR_BITCLEAR
:
5216 case OPERATOR_ANDAND
:
5219 if (!Binary_expression::operation_type(this->op_
,
5220 this->left_
->type(),
5221 this->right_
->type(),
5223 return Type::make_error_type();
5227 case OPERATOR_LSHIFT
:
5228 case OPERATOR_RSHIFT
:
5229 return this->left_
->type();
5236 // Set type for a binary expression.
5239 Binary_expression::do_determine_type(const Type_context
* context
)
5241 Type
* tleft
= this->left_
->type();
5242 Type
* tright
= this->right_
->type();
5244 // Both sides should have the same type, except for the shift
5245 // operations. For a comparison, we should ignore the incoming
5248 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5249 || this->op_
== OPERATOR_RSHIFT
);
5251 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5252 || this->op_
== OPERATOR_NOTEQ
5253 || this->op_
== OPERATOR_LT
5254 || this->op_
== OPERATOR_LE
5255 || this->op_
== OPERATOR_GT
5256 || this->op_
== OPERATOR_GE
);
5258 Type_context
subcontext(*context
);
5262 // In a comparison, the context does not determine the types of
5264 subcontext
.type
= NULL
;
5267 // Set the context for the left hand operand.
5270 // The right hand operand of a shift plays no role in
5271 // determining the type of the left hand operand.
5273 else if (!tleft
->is_abstract())
5274 subcontext
.type
= tleft
;
5275 else if (!tright
->is_abstract())
5276 subcontext
.type
= tright
;
5277 else if (subcontext
.type
== NULL
)
5279 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5280 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5281 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5283 // Both sides have an abstract integer, abstract float, or
5284 // abstract complex type. Just let CONTEXT determine
5285 // whether they may remain abstract or not.
5287 else if (tleft
->complex_type() != NULL
)
5288 subcontext
.type
= tleft
;
5289 else if (tright
->complex_type() != NULL
)
5290 subcontext
.type
= tright
;
5291 else if (tleft
->float_type() != NULL
)
5292 subcontext
.type
= tleft
;
5293 else if (tright
->float_type() != NULL
)
5294 subcontext
.type
= tright
;
5296 subcontext
.type
= tleft
;
5298 if (subcontext
.type
!= NULL
&& !context
->may_be_abstract
)
5299 subcontext
.type
= subcontext
.type
->make_non_abstract_type();
5302 this->left_
->determine_type(&subcontext
);
5306 // We may have inherited an unusable type for the shift operand.
5307 // Give a useful error if that happened.
5308 if (tleft
->is_abstract()
5309 && subcontext
.type
!= NULL
5310 && !subcontext
.may_be_abstract
5311 && subcontext
.type
->interface_type() == NULL
5312 && subcontext
.type
->integer_type() == NULL
)
5313 this->report_error(("invalid context-determined non-integer type "
5314 "for left operand of shift"));
5316 // The context for the right hand operand is the same as for the
5317 // left hand operand, except for a shift operator.
5318 subcontext
.type
= Type::lookup_integer_type("uint");
5319 subcontext
.may_be_abstract
= false;
5322 this->right_
->determine_type(&subcontext
);
5326 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
5328 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
5329 this->type_
= context
->type
;
5330 else if (!context
->may_be_abstract
)
5331 this->type_
= Type::lookup_bool_type();
5335 // Report an error if the binary operator OP does not support TYPE.
5336 // OTYPE is the type of the other operand. Return whether the
5337 // operation is OK. This should not be used for shift.
5340 Binary_expression::check_operator_type(Operator op
, Type
* type
, Type
* otype
,
5346 case OPERATOR_ANDAND
:
5347 if (!type
->is_boolean_type())
5349 error_at(location
, "expected boolean type");
5355 case OPERATOR_NOTEQ
:
5358 if (!Type::are_compatible_for_comparison(true, type
, otype
, &reason
))
5360 error_at(location
, "%s", reason
.c_str());
5372 if (!Type::are_compatible_for_comparison(false, type
, otype
, &reason
))
5374 error_at(location
, "%s", reason
.c_str());
5381 case OPERATOR_PLUSEQ
:
5382 if (type
->integer_type() == NULL
5383 && type
->float_type() == NULL
5384 && type
->complex_type() == NULL
5385 && !type
->is_string_type())
5388 "expected integer, floating, complex, or string type");
5393 case OPERATOR_MINUS
:
5394 case OPERATOR_MINUSEQ
:
5396 case OPERATOR_MULTEQ
:
5398 case OPERATOR_DIVEQ
:
5399 if (type
->integer_type() == NULL
5400 && type
->float_type() == NULL
5401 && type
->complex_type() == NULL
)
5403 error_at(location
, "expected integer, floating, or complex type");
5409 case OPERATOR_MODEQ
:
5413 case OPERATOR_ANDEQ
:
5415 case OPERATOR_XOREQ
:
5416 case OPERATOR_BITCLEAR
:
5417 case OPERATOR_BITCLEAREQ
:
5418 if (type
->integer_type() == NULL
)
5420 error_at(location
, "expected integer type");
5435 Binary_expression::do_check_types(Gogo
*)
5437 if (this->classification() == EXPRESSION_ERROR
)
5440 Type
* left_type
= this->left_
->type();
5441 Type
* right_type
= this->right_
->type();
5442 if (left_type
->is_error() || right_type
->is_error())
5444 this->set_is_error();
5448 if (this->op_
== OPERATOR_EQEQ
5449 || this->op_
== OPERATOR_NOTEQ
5450 || this->op_
== OPERATOR_LT
5451 || this->op_
== OPERATOR_LE
5452 || this->op_
== OPERATOR_GT
5453 || this->op_
== OPERATOR_GE
)
5455 if (left_type
->is_nil_type() && right_type
->is_nil_type())
5457 this->report_error(_("invalid comparison of nil with nil"));
5460 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5461 && !Type::are_assignable(right_type
, left_type
, NULL
))
5463 this->report_error(_("incompatible types in binary expression"));
5466 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5469 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5473 this->set_is_error();
5477 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5479 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5481 this->report_error(_("incompatible types in binary expression"));
5484 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5488 this->set_is_error();
5491 if (this->op_
== OPERATOR_DIV
|| this->op_
== OPERATOR_MOD
)
5493 // Division by a zero integer constant is an error.
5494 Numeric_constant rconst
;
5496 if (left_type
->integer_type() != NULL
5497 && this->right_
->numeric_constant_value(&rconst
)
5498 && rconst
.to_unsigned_long(&rval
) == Numeric_constant::NC_UL_VALID
5501 this->report_error(_("integer division by zero"));
5508 if (left_type
->integer_type() == NULL
)
5509 this->report_error(_("shift of non-integer operand"));
5511 if (!right_type
->is_abstract()
5512 && (right_type
->integer_type() == NULL
5513 || !right_type
->integer_type()->is_unsigned()))
5514 this->report_error(_("shift count not unsigned integer"));
5517 Numeric_constant nc
;
5518 if (this->right_
->numeric_constant_value(&nc
))
5521 if (!nc
.to_int(&val
))
5522 this->report_error(_("shift count not unsigned integer"));
5525 if (mpz_sgn(val
) < 0)
5527 this->report_error(_("negative shift count"));
5528 Location rloc
= this->right_
->location();
5529 this->right_
= Expression::make_integer_ul(0, right_type
,
5539 // Get the backend representation for a binary expression.
5542 Binary_expression::do_get_backend(Translate_context
* context
)
5544 Gogo
* gogo
= context
->gogo();
5545 Location loc
= this->location();
5546 Type
* left_type
= this->left_
->type();
5547 Type
* right_type
= this->right_
->type();
5549 bool use_left_type
= true;
5550 bool is_shift_op
= false;
5551 bool is_idiv_op
= false;
5555 case OPERATOR_NOTEQ
:
5560 return Expression::comparison(context
, this->type_
, this->op_
,
5561 this->left_
, this->right_
, loc
);
5564 case OPERATOR_ANDAND
:
5565 use_left_type
= false;
5568 case OPERATOR_MINUS
:
5574 if (left_type
->float_type() != NULL
|| left_type
->complex_type() != NULL
)
5579 case OPERATOR_LSHIFT
:
5580 case OPERATOR_RSHIFT
:
5583 case OPERATOR_BITCLEAR
:
5584 this->right_
= Expression::make_unary(OPERATOR_XOR
, this->right_
, loc
);
5591 if (left_type
->is_string_type())
5593 go_assert(this->op_
== OPERATOR_PLUS
);
5594 Expression
* string_plus
=
5595 Runtime::make_call(Runtime::STRING_PLUS
, loc
, 2,
5596 this->left_
, this->right_
);
5597 return string_plus
->get_backend(context
);
5600 // For complex division Go might want slightly different results than the
5601 // backend implementation provides, so we have our own runtime routine.
5602 if (this->op_
== OPERATOR_DIV
&& this->left_
->type()->complex_type() != NULL
)
5604 Runtime::Function complex_code
;
5605 switch (this->left_
->type()->complex_type()->bits())
5608 complex_code
= Runtime::COMPLEX64_DIV
;
5611 complex_code
= Runtime::COMPLEX128_DIV
;
5616 Expression
* complex_div
=
5617 Runtime::make_call(complex_code
, loc
, 2, this->left_
, this->right_
);
5618 return complex_div
->get_backend(context
);
5621 Bexpression
* left
= this->left_
->get_backend(context
);
5622 Bexpression
* right
= this->right_
->get_backend(context
);
5624 Type
* type
= use_left_type
? left_type
: right_type
;
5625 Btype
* btype
= type
->get_backend(gogo
);
5628 gogo
->backend()->binary_expression(this->op_
, left
, right
, loc
);
5629 ret
= gogo
->backend()->convert_expression(btype
, ret
, loc
);
5631 // Initialize overflow constants.
5632 Bexpression
* overflow
;
5634 mpz_init_set_ui(zero
, 0UL);
5636 mpz_init_set_ui(one
, 1UL);
5638 mpz_init_set_si(neg_one
, -1);
5640 Btype
* left_btype
= left_type
->get_backend(gogo
);
5641 Btype
* right_btype
= right_type
->get_backend(gogo
);
5643 // In Go, a shift larger than the size of the type is well-defined.
5644 // This is not true in C, so we need to insert a conditional.
5647 go_assert(left_type
->integer_type() != NULL
);
5650 int bits
= left_type
->integer_type()->bits();
5651 mpz_init_set_ui(bitsval
, bits
);
5652 Bexpression
* bits_expr
=
5653 gogo
->backend()->integer_constant_expression(right_btype
, bitsval
);
5654 Bexpression
* compare
=
5655 gogo
->backend()->binary_expression(OPERATOR_LT
,
5656 right
, bits_expr
, loc
);
5658 Bexpression
* zero_expr
=
5659 gogo
->backend()->integer_constant_expression(left_btype
, zero
);
5660 overflow
= zero_expr
;
5661 if (this->op_
== OPERATOR_RSHIFT
5662 && !left_type
->integer_type()->is_unsigned())
5664 Bexpression
* neg_expr
=
5665 gogo
->backend()->binary_expression(OPERATOR_LT
, left
,
5667 Bexpression
* neg_one_expr
=
5668 gogo
->backend()->integer_constant_expression(left_btype
, neg_one
);
5669 overflow
= gogo
->backend()->conditional_expression(btype
, neg_expr
,
5673 ret
= gogo
->backend()->conditional_expression(btype
, compare
, ret
,
5678 // Add checks for division by zero and division overflow as needed.
5681 if (gogo
->check_divide_by_zero())
5684 Bexpression
* zero_expr
=
5685 gogo
->backend()->integer_constant_expression(right_btype
, zero
);
5686 Bexpression
* check
=
5687 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
5688 right
, zero_expr
, loc
);
5690 // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO)
5691 int errcode
= RUNTIME_ERROR_DIVISION_BY_ZERO
;
5692 Bexpression
* crash
= gogo
->runtime_error(errcode
,
5693 loc
)->get_backend(context
);
5695 // right == 0 ? (__go_runtime_error(...), 0) : ret
5696 ret
= gogo
->backend()->conditional_expression(btype
, check
, crash
,
5700 if (gogo
->check_divide_overflow())
5703 // FIXME: It would be nice to say that this test is expected
5706 Bexpression
* neg_one_expr
=
5707 gogo
->backend()->integer_constant_expression(right_btype
, neg_one
);
5708 Bexpression
* check
=
5709 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
5710 right
, neg_one_expr
, loc
);
5712 Bexpression
* zero_expr
=
5713 gogo
->backend()->integer_constant_expression(btype
, zero
);
5714 Bexpression
* one_expr
=
5715 gogo
->backend()->integer_constant_expression(btype
, one
);
5717 if (type
->integer_type()->is_unsigned())
5719 // An unsigned -1 is the largest possible number, so
5720 // dividing is always 1 or 0.
5723 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
5725 if (this->op_
== OPERATOR_DIV
)
5727 gogo
->backend()->conditional_expression(btype
, cmp
,
5728 one_expr
, zero_expr
,
5732 gogo
->backend()->conditional_expression(btype
, cmp
,
5738 // Computing left / -1 is the same as computing - left,
5739 // which does not overflow since Go sets -fwrapv.
5740 if (this->op_
== OPERATOR_DIV
)
5742 Expression
* negate_expr
=
5743 Expression::make_unary(OPERATOR_MINUS
, this->left_
, loc
);
5744 overflow
= negate_expr
->get_backend(context
);
5747 overflow
= zero_expr
;
5749 overflow
= gogo
->backend()->convert_expression(btype
, overflow
, loc
);
5751 // right == -1 ? - left : ret
5752 ret
= gogo
->backend()->conditional_expression(btype
, check
, overflow
,
5763 // Export a binary expression.
5766 Binary_expression::do_export(Export
* exp
) const
5768 exp
->write_c_string("(");
5769 this->left_
->export_expression(exp
);
5773 exp
->write_c_string(" || ");
5775 case OPERATOR_ANDAND
:
5776 exp
->write_c_string(" && ");
5779 exp
->write_c_string(" == ");
5781 case OPERATOR_NOTEQ
:
5782 exp
->write_c_string(" != ");
5785 exp
->write_c_string(" < ");
5788 exp
->write_c_string(" <= ");
5791 exp
->write_c_string(" > ");
5794 exp
->write_c_string(" >= ");
5797 exp
->write_c_string(" + ");
5799 case OPERATOR_MINUS
:
5800 exp
->write_c_string(" - ");
5803 exp
->write_c_string(" | ");
5806 exp
->write_c_string(" ^ ");
5809 exp
->write_c_string(" * ");
5812 exp
->write_c_string(" / ");
5815 exp
->write_c_string(" % ");
5817 case OPERATOR_LSHIFT
:
5818 exp
->write_c_string(" << ");
5820 case OPERATOR_RSHIFT
:
5821 exp
->write_c_string(" >> ");
5824 exp
->write_c_string(" & ");
5826 case OPERATOR_BITCLEAR
:
5827 exp
->write_c_string(" &^ ");
5832 this->right_
->export_expression(exp
);
5833 exp
->write_c_string(")");
5836 // Import a binary expression.
5839 Binary_expression::do_import(Import
* imp
)
5841 imp
->require_c_string("(");
5843 Expression
* left
= Expression::import_expression(imp
);
5846 if (imp
->match_c_string(" || "))
5851 else if (imp
->match_c_string(" && "))
5853 op
= OPERATOR_ANDAND
;
5856 else if (imp
->match_c_string(" == "))
5861 else if (imp
->match_c_string(" != "))
5863 op
= OPERATOR_NOTEQ
;
5866 else if (imp
->match_c_string(" < "))
5871 else if (imp
->match_c_string(" <= "))
5876 else if (imp
->match_c_string(" > "))
5881 else if (imp
->match_c_string(" >= "))
5886 else if (imp
->match_c_string(" + "))
5891 else if (imp
->match_c_string(" - "))
5893 op
= OPERATOR_MINUS
;
5896 else if (imp
->match_c_string(" | "))
5901 else if (imp
->match_c_string(" ^ "))
5906 else if (imp
->match_c_string(" * "))
5911 else if (imp
->match_c_string(" / "))
5916 else if (imp
->match_c_string(" % "))
5921 else if (imp
->match_c_string(" << "))
5923 op
= OPERATOR_LSHIFT
;
5926 else if (imp
->match_c_string(" >> "))
5928 op
= OPERATOR_RSHIFT
;
5931 else if (imp
->match_c_string(" & "))
5936 else if (imp
->match_c_string(" &^ "))
5938 op
= OPERATOR_BITCLEAR
;
5943 error_at(imp
->location(), "unrecognized binary operator");
5944 return Expression::make_error(imp
->location());
5947 Expression
* right
= Expression::import_expression(imp
);
5949 imp
->require_c_string(")");
5951 return Expression::make_binary(op
, left
, right
, imp
->location());
5954 // Dump ast representation of a binary expression.
5957 Binary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
5959 ast_dump_context
->ostream() << "(";
5960 ast_dump_context
->dump_expression(this->left_
);
5961 ast_dump_context
->ostream() << " ";
5962 ast_dump_context
->dump_operator(this->op_
);
5963 ast_dump_context
->ostream() << " ";
5964 ast_dump_context
->dump_expression(this->right_
);
5965 ast_dump_context
->ostream() << ") ";
5968 // Make a binary expression.
5971 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
5974 return new Binary_expression(op
, left
, right
, location
);
5977 // Implement a comparison.
5980 Expression::comparison(Translate_context
* context
, Type
* result_type
,
5981 Operator op
, Expression
* left
, Expression
* right
,
5984 Type
* left_type
= left
->type();
5985 Type
* right_type
= right
->type();
5987 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, location
);
5989 if (left_type
->is_string_type() && right_type
->is_string_type())
5991 left
= Runtime::make_call(Runtime::STRCMP
, location
, 2,
5995 else if ((left_type
->interface_type() != NULL
5996 && right_type
->interface_type() == NULL
5997 && !right_type
->is_nil_type())
5998 || (left_type
->interface_type() == NULL
5999 && !left_type
->is_nil_type()
6000 && right_type
->interface_type() != NULL
))
6002 // Comparing an interface value to a non-interface value.
6003 if (left_type
->interface_type() == NULL
)
6005 std::swap(left_type
, right_type
);
6006 std::swap(left
, right
);
6009 // The right operand is not an interface. We need to take its
6010 // address if it is not a pointer.
6011 Expression
* pointer_arg
= NULL
;
6012 if (right_type
->points_to() != NULL
)
6013 pointer_arg
= right
;
6016 go_assert(right
->is_addressable());
6017 pointer_arg
= Expression::make_unary(OPERATOR_AND
, right
,
6021 Expression
* descriptor
=
6022 Expression::make_type_descriptor(right_type
, location
);
6024 Runtime::make_call((left_type
->interface_type()->is_empty()
6025 ? Runtime::EMPTY_INTERFACE_VALUE_COMPARE
6026 : Runtime::INTERFACE_VALUE_COMPARE
),
6027 location
, 3, left
, descriptor
,
6031 else if (left_type
->interface_type() != NULL
6032 && right_type
->interface_type() != NULL
)
6034 Runtime::Function compare_function
;
6035 if (left_type
->interface_type()->is_empty()
6036 && right_type
->interface_type()->is_empty())
6037 compare_function
= Runtime::EMPTY_INTERFACE_COMPARE
;
6038 else if (!left_type
->interface_type()->is_empty()
6039 && !right_type
->interface_type()->is_empty())
6040 compare_function
= Runtime::INTERFACE_COMPARE
;
6043 if (left_type
->interface_type()->is_empty())
6045 go_assert(op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
);
6046 std::swap(left_type
, right_type
);
6047 std::swap(left
, right
);
6049 go_assert(!left_type
->interface_type()->is_empty());
6050 go_assert(right_type
->interface_type()->is_empty());
6051 compare_function
= Runtime::INTERFACE_EMPTY_COMPARE
;
6054 left
= Runtime::make_call(compare_function
, location
, 2, left
, right
);
6058 if (left_type
->is_nil_type()
6059 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6061 std::swap(left_type
, right_type
);
6062 std::swap(left
, right
);
6065 if (right_type
->is_nil_type())
6067 right
= Expression::make_nil(location
);
6068 if (left_type
->array_type() != NULL
6069 && left_type
->array_type()->length() == NULL
)
6071 Array_type
* at
= left_type
->array_type();
6072 left
= at
->get_value_pointer(context
->gogo(), left
);
6074 else if (left_type
->interface_type() != NULL
)
6076 // An interface is nil if the first field is nil.
6077 left
= Expression::make_field_reference(left
, 0, location
);
6081 Bexpression
* left_bexpr
= left
->get_backend(context
);
6082 Bexpression
* right_bexpr
= right
->get_backend(context
);
6084 Gogo
* gogo
= context
->gogo();
6085 Bexpression
* ret
= gogo
->backend()->binary_expression(op
, left_bexpr
,
6086 right_bexpr
, location
);
6087 if (result_type
!= NULL
)
6088 ret
= gogo
->backend()->convert_expression(result_type
->get_backend(gogo
),
6093 // Class Bound_method_expression.
6098 Bound_method_expression::do_traverse(Traverse
* traverse
)
6100 return Expression::traverse(&this->expr_
, traverse
);
6103 // Lower the expression. If this is a method value rather than being
6104 // called, and the method is accessed via a pointer, we may need to
6105 // add nil checks. Introduce a temporary variable so that those nil
6106 // checks do not cause multiple evaluation.
6109 Bound_method_expression::do_lower(Gogo
*, Named_object
*,
6110 Statement_inserter
* inserter
, int)
6112 // For simplicity we use a temporary for every call to an embedded
6113 // method, even though some of them might be pure value methods and
6114 // not require a temporary.
6115 if (this->expr_
->var_expression() == NULL
6116 && this->expr_
->temporary_reference_expression() == NULL
6117 && this->expr_
->set_and_use_temporary_expression() == NULL
6118 && (this->method_
->field_indexes() != NULL
6119 || (this->method_
->is_value_method()
6120 && this->expr_
->type()->points_to() != NULL
)))
6122 Temporary_statement
* temp
=
6123 Statement::make_temporary(this->expr_
->type(), NULL
, this->location());
6124 inserter
->insert(temp
);
6125 this->expr_
= Expression::make_set_and_use_temporary(temp
, this->expr_
,
6131 // Return the type of a bound method expression. The type of this
6132 // object is simply the type of the method with no receiver.
6135 Bound_method_expression::do_type()
6137 Named_object
* fn
= this->method_
->named_object();
6138 Function_type
* fntype
;
6139 if (fn
->is_function())
6140 fntype
= fn
->func_value()->type();
6141 else if (fn
->is_function_declaration())
6142 fntype
= fn
->func_declaration_value()->type();
6144 return Type::make_error_type();
6145 return fntype
->copy_without_receiver();
6148 // Determine the types of a method expression.
6151 Bound_method_expression::do_determine_type(const Type_context
*)
6153 Named_object
* fn
= this->method_
->named_object();
6154 Function_type
* fntype
;
6155 if (fn
->is_function())
6156 fntype
= fn
->func_value()->type();
6157 else if (fn
->is_function_declaration())
6158 fntype
= fn
->func_declaration_value()->type();
6161 if (fntype
== NULL
|| !fntype
->is_method())
6162 this->expr_
->determine_type_no_context();
6165 Type_context
subcontext(fntype
->receiver()->type(), false);
6166 this->expr_
->determine_type(&subcontext
);
6170 // Check the types of a method expression.
6173 Bound_method_expression::do_check_types(Gogo
*)
6175 Named_object
* fn
= this->method_
->named_object();
6176 if (!fn
->is_function() && !fn
->is_function_declaration())
6178 this->report_error(_("object is not a method"));
6182 Function_type
* fntype
;
6183 if (fn
->is_function())
6184 fntype
= fn
->func_value()->type();
6185 else if (fn
->is_function_declaration())
6186 fntype
= fn
->func_declaration_value()->type();
6189 Type
* rtype
= fntype
->receiver()->type()->deref();
6190 Type
* etype
= (this->expr_type_
!= NULL
6192 : this->expr_
->type());
6193 etype
= etype
->deref();
6194 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6195 this->report_error(_("method type does not match object type"));
6198 // If a bound method expression is not simply called, then it is
6199 // represented as a closure. The closure will hold a single variable,
6200 // the receiver to pass to the method. The function will be a simple
6201 // thunk that pulls that value from the closure and calls the method
6202 // with the remaining arguments.
6204 // Because method values are not common, we don't build all thunks for
6205 // every methods, but instead only build them as we need them. In
6206 // particular, we even build them on demand for methods defined in
6209 Bound_method_expression::Method_value_thunks
6210 Bound_method_expression::method_value_thunks
;
6212 // Find or create the thunk for METHOD.
6215 Bound_method_expression::create_thunk(Gogo
* gogo
, const Method
* method
,
6218 std::pair
<Named_object
*, Named_object
*> val(fn
, NULL
);
6219 std::pair
<Method_value_thunks::iterator
, bool> ins
=
6220 Bound_method_expression::method_value_thunks
.insert(val
);
6223 // We have seen this method before.
6224 go_assert(ins
.first
->second
!= NULL
);
6225 return ins
.first
->second
;
6228 Location loc
= fn
->location();
6230 Function_type
* orig_fntype
;
6231 if (fn
->is_function())
6232 orig_fntype
= fn
->func_value()->type();
6233 else if (fn
->is_function_declaration())
6234 orig_fntype
= fn
->func_declaration_value()->type();
6238 if (orig_fntype
== NULL
|| !orig_fntype
->is_method())
6240 ins
.first
->second
= Named_object::make_erroneous_name(Gogo::thunk_name());
6241 return ins
.first
->second
;
6244 Struct_field_list
* sfl
= new Struct_field_list();
6245 // The type here is wrong--it should be the C function type. But it
6246 // doesn't really matter.
6247 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
6248 sfl
->push_back(Struct_field(Typed_identifier("fn.0", vt
, loc
)));
6249 sfl
->push_back(Struct_field(Typed_identifier("val.1",
6250 orig_fntype
->receiver()->type(),
6252 Type
* closure_type
= Type::make_struct_type(sfl
, loc
);
6253 closure_type
= Type::make_pointer_type(closure_type
);
6255 Function_type
* new_fntype
= orig_fntype
->copy_with_names();
6257 std::string thunk_name
= Gogo::thunk_name();
6258 Named_object
* new_no
= gogo
->start_function(thunk_name
, new_fntype
,
6261 Variable
* cvar
= new Variable(closure_type
, NULL
, false, false, false, loc
);
6262 cvar
->set_is_used();
6263 cvar
->set_is_closure();
6264 Named_object
* cp
= Named_object::make_variable("$closure" + thunk_name
,
6266 new_no
->func_value()->set_closure_var(cp
);
6268 gogo
->start_block(loc
);
6270 // Field 0 of the closure is the function code pointer, field 1 is
6271 // the value on which to invoke the method.
6272 Expression
* arg
= Expression::make_var_reference(cp
, loc
);
6273 arg
= Expression::make_unary(OPERATOR_MULT
, arg
, loc
);
6274 arg
= Expression::make_field_reference(arg
, 1, loc
);
6276 Expression
* bme
= Expression::make_bound_method(arg
, method
, fn
, loc
);
6278 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
6279 Expression_list
* args
;
6280 if (orig_params
== NULL
|| orig_params
->empty())
6284 const Typed_identifier_list
* new_params
= new_fntype
->parameters();
6285 args
= new Expression_list();
6286 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
6287 p
!= new_params
->end();
6290 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
6291 go_assert(p_no
!= NULL
6292 && p_no
->is_variable()
6293 && p_no
->var_value()->is_parameter());
6294 args
->push_back(Expression::make_var_reference(p_no
, loc
));
6298 Call_expression
* call
= Expression::make_call(bme
, args
,
6299 orig_fntype
->is_varargs(),
6301 call
->set_varargs_are_lowered();
6303 Statement
* s
= Statement::make_return_from_call(call
, loc
);
6304 gogo
->add_statement(s
);
6305 Block
* b
= gogo
->finish_block(loc
);
6306 gogo
->add_block(b
, loc
);
6307 gogo
->lower_block(new_no
, b
);
6308 gogo
->flatten_block(new_no
, b
);
6309 gogo
->finish_function(loc
);
6311 ins
.first
->second
= new_no
;
6315 // Return an expression to check *REF for nil while dereferencing
6316 // according to FIELD_INDEXES. Update *REF to build up the field
6317 // reference. This is a static function so that we don't have to
6318 // worry about declaring Field_indexes in expressions.h.
6321 bme_check_nil(const Method::Field_indexes
* field_indexes
, Location loc
,
6324 if (field_indexes
== NULL
)
6325 return Expression::make_boolean(false, loc
);
6326 Expression
* cond
= bme_check_nil(field_indexes
->next
, loc
, ref
);
6327 Struct_type
* stype
= (*ref
)->type()->deref()->struct_type();
6328 go_assert(stype
!= NULL
6329 && field_indexes
->field_index
< stype
->field_count());
6330 if ((*ref
)->type()->struct_type() == NULL
)
6332 go_assert((*ref
)->type()->points_to() != NULL
);
6333 Expression
* n
= Expression::make_binary(OPERATOR_EQEQ
, *ref
,
6334 Expression::make_nil(loc
),
6336 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, n
, loc
);
6337 *ref
= Expression::make_unary(OPERATOR_MULT
, *ref
, loc
);
6338 go_assert((*ref
)->type()->struct_type() == stype
);
6340 *ref
= Expression::make_field_reference(*ref
, field_indexes
->field_index
,
6345 // Get the backend representation for a method value.
6348 Bound_method_expression::do_get_backend(Translate_context
* context
)
6350 Named_object
* thunk
= Bound_method_expression::create_thunk(context
->gogo(),
6353 if (thunk
->is_erroneous())
6355 go_assert(saw_errors());
6356 return context
->backend()->error_expression();
6359 // FIXME: We should lower this earlier, but we can't lower it in the
6360 // lowering pass because at that point we don't know whether we need
6361 // to create the thunk or not. If the expression is called, we
6362 // don't need the thunk.
6364 Location loc
= this->location();
6366 // If the method expects a value, and we have a pointer, we need to
6367 // dereference the pointer.
6369 Named_object
* fn
= this->method_
->named_object();
6370 Function_type
* fntype
;
6371 if (fn
->is_function())
6372 fntype
= fn
->func_value()->type();
6373 else if (fn
->is_function_declaration())
6374 fntype
= fn
->func_declaration_value()->type();
6378 Expression
* val
= this->expr_
;
6379 if (fntype
->receiver()->type()->points_to() == NULL
6380 && val
->type()->points_to() != NULL
)
6381 val
= Expression::make_unary(OPERATOR_MULT
, val
, loc
);
6383 // Note that we are ignoring this->expr_type_ here. The thunk will
6384 // expect a closure whose second field has type this->expr_type_ (if
6385 // that is not NULL). We are going to pass it a closure whose
6386 // second field has type this->expr_->type(). Since
6387 // this->expr_type_ is only not-NULL for pointer types, we can get
6390 Struct_field_list
* fields
= new Struct_field_list();
6391 fields
->push_back(Struct_field(Typed_identifier("fn.0",
6392 thunk
->func_value()->type(),
6394 fields
->push_back(Struct_field(Typed_identifier("val.1", val
->type(), loc
)));
6395 Struct_type
* st
= Type::make_struct_type(fields
, loc
);
6397 Expression_list
* vals
= new Expression_list();
6398 vals
->push_back(Expression::make_func_code_reference(thunk
, loc
));
6399 vals
->push_back(val
);
6401 Expression
* ret
= Expression::make_struct_composite_literal(st
, vals
, loc
);
6402 ret
= Expression::make_heap_expression(ret
, loc
);
6404 // See whether the expression or any embedded pointers are nil.
6406 Expression
* nil_check
= NULL
;
6407 Expression
* expr
= this->expr_
;
6408 if (this->method_
->field_indexes() != NULL
)
6410 // Note that we are evaluating this->expr_ twice, but that is OK
6411 // because in the lowering pass we forced it into a temporary
6413 Expression
* ref
= expr
;
6414 nil_check
= bme_check_nil(this->method_
->field_indexes(), loc
, &ref
);
6418 if (this->method_
->is_value_method() && expr
->type()->points_to() != NULL
)
6420 Expression
* n
= Expression::make_binary(OPERATOR_EQEQ
, expr
,
6421 Expression::make_nil(loc
),
6423 if (nil_check
== NULL
)
6426 nil_check
= Expression::make_binary(OPERATOR_OROR
, nil_check
, n
, loc
);
6429 Bexpression
* bme
= ret
->get_backend(context
);
6430 if (nil_check
!= NULL
)
6432 Gogo
* gogo
= context
->gogo();
6433 Bexpression
* crash
=
6434 gogo
->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
6435 loc
)->get_backend(context
);
6436 Btype
* btype
= ret
->type()->get_backend(gogo
);
6437 Bexpression
* bcheck
= nil_check
->get_backend(context
);
6438 bme
= gogo
->backend()->conditional_expression(btype
, bcheck
, crash
,
6444 // Dump ast representation of a bound method expression.
6447 Bound_method_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
6450 if (this->expr_type_
!= NULL
)
6451 ast_dump_context
->ostream() << "(";
6452 ast_dump_context
->dump_expression(this->expr_
);
6453 if (this->expr_type_
!= NULL
)
6455 ast_dump_context
->ostream() << ":";
6456 ast_dump_context
->dump_type(this->expr_type_
);
6457 ast_dump_context
->ostream() << ")";
6460 ast_dump_context
->ostream() << "." << this->function_
->name();
6463 // Make a method expression.
6465 Bound_method_expression
*
6466 Expression::make_bound_method(Expression
* expr
, const Method
* method
,
6467 Named_object
* function
, Location location
)
6469 return new Bound_method_expression(expr
, method
, function
, location
);
6472 // Class Builtin_call_expression. This is used for a call to a
6473 // builtin function.
6475 class Builtin_call_expression
: public Call_expression
6478 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6479 bool is_varargs
, Location location
);
6482 // This overrides Call_expression::do_lower.
6484 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
6487 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
6490 do_is_constant() const;
6493 do_numeric_constant_value(Numeric_constant
*) const;
6496 do_discarding_value();
6502 do_determine_type(const Type_context
*);
6505 do_check_types(Gogo
*);
6511 do_get_backend(Translate_context
*);
6514 do_export(Export
*) const;
6517 do_is_recover_call() const;
6520 do_set_recover_arg(Expression
*);
6523 // The builtin functions.
6524 enum Builtin_function_code
6528 // Predeclared builtin functions.
6545 // Builtin functions from the unsafe package.
6558 real_imag_type(Type
*);
6561 complex_type(Type
*);
6567 check_int_value(Expression
*, bool is_length
);
6569 // A pointer back to the general IR structure. This avoids a global
6570 // variable, or passing it around everywhere.
6572 // The builtin function being called.
6573 Builtin_function_code code_
;
6574 // Used to stop endless loops when the length of an array uses len
6575 // or cap of the array itself.
6577 // Whether the argument is set for calls to BUILTIN_RECOVER.
6578 bool recover_arg_is_set_
;
6581 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6583 Expression_list
* args
,
6586 : Call_expression(fn
, args
, is_varargs
, location
),
6587 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false),
6588 recover_arg_is_set_(false)
6590 Func_expression
* fnexp
= this->fn()->func_expression();
6591 go_assert(fnexp
!= NULL
);
6592 const std::string
& name(fnexp
->named_object()->name());
6593 if (name
== "append")
6594 this->code_
= BUILTIN_APPEND
;
6595 else if (name
== "cap")
6596 this->code_
= BUILTIN_CAP
;
6597 else if (name
== "close")
6598 this->code_
= BUILTIN_CLOSE
;
6599 else if (name
== "complex")
6600 this->code_
= BUILTIN_COMPLEX
;
6601 else if (name
== "copy")
6602 this->code_
= BUILTIN_COPY
;
6603 else if (name
== "delete")
6604 this->code_
= BUILTIN_DELETE
;
6605 else if (name
== "imag")
6606 this->code_
= BUILTIN_IMAG
;
6607 else if (name
== "len")
6608 this->code_
= BUILTIN_LEN
;
6609 else if (name
== "make")
6610 this->code_
= BUILTIN_MAKE
;
6611 else if (name
== "new")
6612 this->code_
= BUILTIN_NEW
;
6613 else if (name
== "panic")
6614 this->code_
= BUILTIN_PANIC
;
6615 else if (name
== "print")
6616 this->code_
= BUILTIN_PRINT
;
6617 else if (name
== "println")
6618 this->code_
= BUILTIN_PRINTLN
;
6619 else if (name
== "real")
6620 this->code_
= BUILTIN_REAL
;
6621 else if (name
== "recover")
6622 this->code_
= BUILTIN_RECOVER
;
6623 else if (name
== "Alignof")
6624 this->code_
= BUILTIN_ALIGNOF
;
6625 else if (name
== "Offsetof")
6626 this->code_
= BUILTIN_OFFSETOF
;
6627 else if (name
== "Sizeof")
6628 this->code_
= BUILTIN_SIZEOF
;
6633 // Return whether this is a call to recover. This is a virtual
6634 // function called from the parent class.
6637 Builtin_call_expression::do_is_recover_call() const
6639 if (this->classification() == EXPRESSION_ERROR
)
6641 return this->code_
== BUILTIN_RECOVER
;
6644 // Set the argument for a call to recover.
6647 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6649 const Expression_list
* args
= this->args();
6650 go_assert(args
== NULL
|| args
->empty());
6651 Expression_list
* new_args
= new Expression_list();
6652 new_args
->push_back(arg
);
6653 this->set_args(new_args
);
6654 this->recover_arg_is_set_
= true;
6657 // Lower a builtin call expression. This turns new and make into
6658 // specific expressions. We also convert to a constant if we can.
6661 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
6662 Statement_inserter
* inserter
, int)
6664 if (this->classification() == EXPRESSION_ERROR
)
6667 Location loc
= this->location();
6669 if (this->is_varargs() && this->code_
!= BUILTIN_APPEND
)
6671 this->report_error(_("invalid use of %<...%> with builtin function"));
6672 return Expression::make_error(loc
);
6675 if (this->code_
== BUILTIN_OFFSETOF
)
6677 Expression
* arg
= this->one_arg();
6679 if (arg
->bound_method_expression() != NULL
6680 || arg
->interface_field_reference_expression() != NULL
)
6682 this->report_error(_("invalid use of method value as argument "
6687 Field_reference_expression
* farg
= arg
->field_reference_expression();
6688 while (farg
!= NULL
)
6690 if (!farg
->implicit())
6692 // When the selector refers to an embedded field,
6693 // it must not be reached through pointer indirections.
6694 if (farg
->expr()->deref() != farg
->expr())
6696 this->report_error(_("argument of Offsetof implies "
6697 "indirection of an embedded field"));
6700 // Go up until we reach the original base.
6701 farg
= farg
->expr()->field_reference_expression();
6705 if (this->is_constant())
6707 Numeric_constant nc
;
6708 if (this->numeric_constant_value(&nc
))
6709 return nc
.expression(loc
);
6712 switch (this->code_
)
6719 const Expression_list
* args
= this->args();
6720 if (args
== NULL
|| args
->size() < 1)
6721 this->report_error(_("not enough arguments"));
6722 else if (args
->size() > 1)
6723 this->report_error(_("too many arguments"));
6726 Expression
* arg
= args
->front();
6727 if (!arg
->is_type_expression())
6729 error_at(arg
->location(), "expected type");
6730 this->set_is_error();
6733 return Expression::make_allocation(arg
->type(), loc
);
6739 return this->lower_make();
6741 case BUILTIN_RECOVER
:
6742 if (function
!= NULL
)
6743 function
->func_value()->set_calls_recover();
6746 // Calling recover outside of a function always returns the
6747 // nil empty interface.
6748 Type
* eface
= Type::make_empty_interface_type(loc
);
6749 return Expression::make_cast(eface
, Expression::make_nil(loc
), loc
);
6753 case BUILTIN_APPEND
:
6755 // Lower the varargs.
6756 const Expression_list
* args
= this->args();
6757 if (args
== NULL
|| args
->empty())
6759 Type
* slice_type
= args
->front()->type();
6760 if (!slice_type
->is_slice_type())
6762 if (slice_type
->is_nil_type())
6763 error_at(args
->front()->location(), "use of untyped nil");
6765 error_at(args
->front()->location(),
6766 "argument 1 must be a slice");
6767 this->set_is_error();
6770 Type
* element_type
= slice_type
->array_type()->element_type();
6771 this->lower_varargs(gogo
, function
, inserter
,
6772 Type::make_array_type(element_type
, NULL
),
6777 case BUILTIN_DELETE
:
6779 // Lower to a runtime function call.
6780 const Expression_list
* args
= this->args();
6781 if (args
== NULL
|| args
->size() < 2)
6782 this->report_error(_("not enough arguments"));
6783 else if (args
->size() > 2)
6784 this->report_error(_("too many arguments"));
6785 else if (args
->front()->type()->map_type() == NULL
)
6786 this->report_error(_("argument 1 must be a map"));
6789 // Since this function returns no value it must appear in
6790 // a statement by itself, so we don't have to worry about
6791 // order of evaluation of values around it. Evaluate the
6792 // map first to get order of evaluation right.
6793 Map_type
* mt
= args
->front()->type()->map_type();
6794 Temporary_statement
* map_temp
=
6795 Statement::make_temporary(mt
, args
->front(), loc
);
6796 inserter
->insert(map_temp
);
6798 Temporary_statement
* key_temp
=
6799 Statement::make_temporary(mt
->key_type(), args
->back(), loc
);
6800 inserter
->insert(key_temp
);
6802 Expression
* e1
= Expression::make_temporary_reference(map_temp
,
6804 Expression
* e2
= Expression::make_temporary_reference(key_temp
,
6806 e2
= Expression::make_unary(OPERATOR_AND
, e2
, loc
);
6807 return Runtime::make_call(Runtime::MAPDELETE
, this->location(),
6817 // Flatten a builtin call expression. This turns the arguments of copy and
6818 // append into temporary expressions.
6821 Builtin_call_expression::do_flatten(Gogo
*, Named_object
*,
6822 Statement_inserter
* inserter
)
6824 Location loc
= this->location();
6825 if (this->is_erroneous_call())
6827 go_assert(saw_errors());
6828 return Expression::make_error(loc
);
6831 switch (this->code_
)
6836 case BUILTIN_APPEND
:
6839 Type
* at
= this->args()->front()->type();
6840 for (Expression_list::iterator pa
= this->args()->begin();
6841 pa
!= this->args()->end();
6844 if ((*pa
)->is_nil_expression())
6846 Expression
* nil
= Expression::make_nil(loc
);
6847 Expression
* zero
= Expression::make_integer_ul(0, NULL
, loc
);
6848 *pa
= Expression::make_slice_value(at
, nil
, zero
, zero
, loc
);
6850 if (!(*pa
)->is_variable())
6852 Temporary_statement
* temp
=
6853 Statement::make_temporary(NULL
, *pa
, loc
);
6854 inserter
->insert(temp
);
6855 *pa
= Expression::make_temporary_reference(temp
, loc
);
6862 for (Expression_list::iterator pa
= this->args()->begin();
6863 pa
!= this->args()->end();
6866 if (!(*pa
)->is_variable() && (*pa
)->type()->interface_type() != NULL
)
6868 Temporary_statement
* temp
=
6869 Statement::make_temporary(NULL
, *pa
, loc
);
6870 inserter
->insert(temp
);
6871 *pa
= Expression::make_temporary_reference(temp
, loc
);
6879 // Lower a make expression.
6882 Builtin_call_expression::lower_make()
6884 Location loc
= this->location();
6886 const Expression_list
* args
= this->args();
6887 if (args
== NULL
|| args
->size() < 1)
6889 this->report_error(_("not enough arguments"));
6890 return Expression::make_error(this->location());
6893 Expression_list::const_iterator parg
= args
->begin();
6895 Expression
* first_arg
= *parg
;
6896 if (!first_arg
->is_type_expression())
6898 error_at(first_arg
->location(), "expected type");
6899 this->set_is_error();
6900 return Expression::make_error(this->location());
6902 Type
* type
= first_arg
->type();
6904 bool is_slice
= false;
6905 bool is_map
= false;
6906 bool is_chan
= false;
6907 if (type
->is_slice_type())
6909 else if (type
->map_type() != NULL
)
6911 else if (type
->channel_type() != NULL
)
6915 this->report_error(_("invalid type for make function"));
6916 return Expression::make_error(this->location());
6919 bool have_big_args
= false;
6920 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6921 int uintptr_bits
= uintptr_type
->integer_type()->bits();
6923 Type_context
int_context(Type::lookup_integer_type("int"), false);
6926 Expression
* len_arg
;
6927 if (parg
== args
->end())
6931 this->report_error(_("length required when allocating a slice"));
6932 return Expression::make_error(this->location());
6934 len_arg
= Expression::make_integer_ul(0, NULL
, loc
);
6939 len_arg
->determine_type(&int_context
);
6940 if (!this->check_int_value(len_arg
, true))
6941 return Expression::make_error(this->location());
6942 if (len_arg
->type()->integer_type() != NULL
6943 && len_arg
->type()->integer_type()->bits() > uintptr_bits
)
6944 have_big_args
= true;
6948 Expression
* cap_arg
= NULL
;
6949 if (is_slice
&& parg
!= args
->end())
6952 cap_arg
->determine_type(&int_context
);
6953 if (!this->check_int_value(cap_arg
, false))
6954 return Expression::make_error(this->location());
6956 Numeric_constant nclen
;
6957 Numeric_constant nccap
;
6960 if (len_arg
->numeric_constant_value(&nclen
)
6961 && cap_arg
->numeric_constant_value(&nccap
)
6962 && nclen
.to_unsigned_long(&vlen
) == Numeric_constant::NC_UL_VALID
6963 && nccap
.to_unsigned_long(&vcap
) == Numeric_constant::NC_UL_VALID
6966 this->report_error(_("len larger than cap"));
6967 return Expression::make_error(this->location());
6970 if (cap_arg
->type()->integer_type() != NULL
6971 && cap_arg
->type()->integer_type()->bits() > uintptr_bits
)
6972 have_big_args
= true;
6976 if (parg
!= args
->end())
6978 this->report_error(_("too many arguments to make"));
6979 return Expression::make_error(this->location());
6982 Location type_loc
= first_arg
->location();
6983 Expression
* type_arg
;
6984 if (is_slice
|| is_chan
)
6985 type_arg
= Expression::make_type_descriptor(type
, type_loc
);
6987 type_arg
= Expression::make_map_descriptor(type
->map_type(), type_loc
);
6994 if (cap_arg
== NULL
)
6995 call
= Runtime::make_call((have_big_args
6996 ? Runtime::MAKESLICE1BIG
6997 : Runtime::MAKESLICE1
),
6998 loc
, 2, type_arg
, len_arg
);
7000 call
= Runtime::make_call((have_big_args
7001 ? Runtime::MAKESLICE2BIG
7002 : Runtime::MAKESLICE2
),
7003 loc
, 3, type_arg
, len_arg
, cap_arg
);
7006 call
= Runtime::make_call((have_big_args
7007 ? Runtime::MAKEMAPBIG
7008 : Runtime::MAKEMAP
),
7009 loc
, 2, type_arg
, len_arg
);
7011 call
= Runtime::make_call((have_big_args
7012 ? Runtime::MAKECHANBIG
7013 : Runtime::MAKECHAN
),
7014 loc
, 2, type_arg
, len_arg
);
7018 return Expression::make_unsafe_cast(type
, call
, loc
);
7021 // Return whether an expression has an integer value. Report an error
7022 // if not. This is used when handling calls to the predeclared make
7026 Builtin_call_expression::check_int_value(Expression
* e
, bool is_length
)
7028 Numeric_constant nc
;
7029 if (e
->numeric_constant_value(&nc
))
7032 switch (nc
.to_unsigned_long(&v
))
7034 case Numeric_constant::NC_UL_VALID
:
7036 case Numeric_constant::NC_UL_NOTINT
:
7037 error_at(e
->location(), "non-integer %s argument to make",
7038 is_length
? "len" : "cap");
7040 case Numeric_constant::NC_UL_NEGATIVE
:
7041 error_at(e
->location(), "negative %s argument to make",
7042 is_length
? "len" : "cap");
7044 case Numeric_constant::NC_UL_BIG
:
7045 // We don't want to give a compile-time error for a 64-bit
7046 // value on a 32-bit target.
7051 if (!nc
.to_int(&val
))
7053 int bits
= mpz_sizeinbase(val
, 2);
7055 Type
* int_type
= Type::lookup_integer_type("int");
7056 if (bits
>= int_type
->integer_type()->bits())
7058 error_at(e
->location(), "%s argument too large for make",
7059 is_length
? "len" : "cap");
7066 if (e
->type()->integer_type() != NULL
)
7069 error_at(e
->location(), "non-integer %s argument to make",
7070 is_length
? "len" : "cap");
7074 // Return the type of the real or imag functions, given the type of
7075 // the argument. We need to map complex64 to float32 and complex128
7076 // to float64, so it has to be done by name. This returns NULL if it
7077 // can't figure out the type.
7080 Builtin_call_expression::real_imag_type(Type
* arg_type
)
7082 if (arg_type
== NULL
|| arg_type
->is_abstract())
7084 Named_type
* nt
= arg_type
->named_type();
7087 while (nt
->real_type()->named_type() != NULL
)
7088 nt
= nt
->real_type()->named_type();
7089 if (nt
->name() == "complex64")
7090 return Type::lookup_float_type("float32");
7091 else if (nt
->name() == "complex128")
7092 return Type::lookup_float_type("float64");
7097 // Return the type of the complex function, given the type of one of the
7098 // argments. Like real_imag_type, we have to map by name.
7101 Builtin_call_expression::complex_type(Type
* arg_type
)
7103 if (arg_type
== NULL
|| arg_type
->is_abstract())
7105 Named_type
* nt
= arg_type
->named_type();
7108 while (nt
->real_type()->named_type() != NULL
)
7109 nt
= nt
->real_type()->named_type();
7110 if (nt
->name() == "float32")
7111 return Type::lookup_complex_type("complex64");
7112 else if (nt
->name() == "float64")
7113 return Type::lookup_complex_type("complex128");
7118 // Return a single argument, or NULL if there isn't one.
7121 Builtin_call_expression::one_arg() const
7123 const Expression_list
* args
= this->args();
7124 if (args
== NULL
|| args
->size() != 1)
7126 return args
->front();
7129 // A traversal class which looks for a call or receive expression.
7131 class Find_call_expression
: public Traverse
7134 Find_call_expression()
7135 : Traverse(traverse_expressions
),
7140 expression(Expression
**);
7144 { return this->found_
; }
7151 Find_call_expression::expression(Expression
** pexpr
)
7153 if ((*pexpr
)->call_expression() != NULL
7154 || (*pexpr
)->receive_expression() != NULL
)
7156 this->found_
= true;
7157 return TRAVERSE_EXIT
;
7159 return TRAVERSE_CONTINUE
;
7162 // Return whether this is constant: len of a string constant, or len
7163 // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof,
7167 Builtin_call_expression::do_is_constant() const
7169 if (this->is_error_expression())
7171 switch (this->code_
)
7179 Expression
* arg
= this->one_arg();
7182 Type
* arg_type
= arg
->type();
7184 if (arg_type
->points_to() != NULL
7185 && arg_type
->points_to()->array_type() != NULL
7186 && !arg_type
->points_to()->is_slice_type())
7187 arg_type
= arg_type
->points_to();
7189 // The len and cap functions are only constant if there are no
7190 // function calls or channel operations in the arguments.
7191 // Otherwise we have to make the call.
7192 if (!arg
->is_constant())
7194 Find_call_expression find_call
;
7195 Expression::traverse(&arg
, &find_call
);
7196 if (find_call
.found())
7200 if (arg_type
->array_type() != NULL
7201 && arg_type
->array_type()->length() != NULL
)
7204 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
7207 bool ret
= arg
->is_constant();
7208 this->seen_
= false;
7214 case BUILTIN_SIZEOF
:
7215 case BUILTIN_ALIGNOF
:
7216 return this->one_arg() != NULL
;
7218 case BUILTIN_OFFSETOF
:
7220 Expression
* arg
= this->one_arg();
7223 return arg
->field_reference_expression() != NULL
;
7226 case BUILTIN_COMPLEX
:
7228 const Expression_list
* args
= this->args();
7229 if (args
!= NULL
&& args
->size() == 2)
7230 return args
->front()->is_constant() && args
->back()->is_constant();
7237 Expression
* arg
= this->one_arg();
7238 return arg
!= NULL
&& arg
->is_constant();
7248 // Return a numeric constant if possible.
7251 Builtin_call_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
7253 if (this->code_
== BUILTIN_LEN
7254 || this->code_
== BUILTIN_CAP
)
7256 Expression
* arg
= this->one_arg();
7259 Type
* arg_type
= arg
->type();
7261 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
7264 if (arg
->string_constant_value(&sval
))
7266 nc
->set_unsigned_long(Type::lookup_integer_type("int"),
7272 if (arg_type
->points_to() != NULL
7273 && arg_type
->points_to()->array_type() != NULL
7274 && !arg_type
->points_to()->is_slice_type())
7275 arg_type
= arg_type
->points_to();
7277 if (arg_type
->array_type() != NULL
7278 && arg_type
->array_type()->length() != NULL
)
7282 Expression
* e
= arg_type
->array_type()->length();
7284 bool r
= e
->numeric_constant_value(nc
);
7285 this->seen_
= false;
7288 if (!nc
->set_type(Type::lookup_integer_type("int"), false,
7295 else if (this->code_
== BUILTIN_SIZEOF
7296 || this->code_
== BUILTIN_ALIGNOF
)
7298 Expression
* arg
= this->one_arg();
7301 Type
* arg_type
= arg
->type();
7302 if (arg_type
->is_error())
7304 if (arg_type
->is_abstract())
7310 if (this->code_
== BUILTIN_SIZEOF
)
7313 bool ok
= arg_type
->backend_type_size(this->gogo_
, &ret
);
7314 this->seen_
= false;
7318 else if (this->code_
== BUILTIN_ALIGNOF
)
7322 if (arg
->field_reference_expression() == NULL
)
7323 ok
= arg_type
->backend_type_align(this->gogo_
, &ret
);
7326 // Calling unsafe.Alignof(s.f) returns the alignment of
7327 // the type of f when it is used as a field in a struct.
7328 ok
= arg_type
->backend_type_field_align(this->gogo_
, &ret
);
7330 this->seen_
= false;
7338 set_mpz_from_int64(&zval
, ret
);
7339 nc
->set_int(Type::lookup_integer_type("uintptr"), zval
);
7343 else if (this->code_
== BUILTIN_OFFSETOF
)
7345 Expression
* arg
= this->one_arg();
7348 Field_reference_expression
* farg
= arg
->field_reference_expression();
7354 int64_t total_offset
= 0;
7357 Expression
* struct_expr
= farg
->expr();
7358 Type
* st
= struct_expr
->type();
7359 if (st
->struct_type() == NULL
)
7361 if (st
->named_type() != NULL
)
7362 st
->named_type()->convert(this->gogo_
);
7365 bool ok
= st
->struct_type()->backend_field_offset(this->gogo_
,
7366 farg
->field_index(),
7368 this->seen_
= false;
7371 total_offset
+= offset
;
7372 if (farg
->implicit() && struct_expr
->field_reference_expression() != NULL
)
7374 // Go up until we reach the original base.
7375 farg
= struct_expr
->field_reference_expression();
7381 set_mpz_from_int64(&zval
, total_offset
);
7382 nc
->set_int(Type::lookup_integer_type("uintptr"), zval
);
7386 else if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
7388 Expression
* arg
= this->one_arg();
7392 Numeric_constant argnc
;
7393 if (!arg
->numeric_constant_value(&argnc
))
7397 if (!argnc
.to_complex(&val
))
7400 Type
* type
= Builtin_call_expression::real_imag_type(argnc
.type());
7401 if (this->code_
== BUILTIN_REAL
)
7402 nc
->set_float(type
, mpc_realref(val
));
7404 nc
->set_float(type
, mpc_imagref(val
));
7408 else if (this->code_
== BUILTIN_COMPLEX
)
7410 const Expression_list
* args
= this->args();
7411 if (args
== NULL
|| args
->size() != 2)
7414 Numeric_constant rnc
;
7415 if (!args
->front()->numeric_constant_value(&rnc
))
7417 Numeric_constant inc
;
7418 if (!args
->back()->numeric_constant_value(&inc
))
7421 if (rnc
.type() != NULL
7422 && !rnc
.type()->is_abstract()
7423 && inc
.type() != NULL
7424 && !inc
.type()->is_abstract()
7425 && !Type::are_identical(rnc
.type(), inc
.type(), false, NULL
))
7429 if (!rnc
.to_float(&r
))
7432 if (!inc
.to_float(&i
))
7438 Type
* arg_type
= rnc
.type();
7439 if (arg_type
== NULL
|| arg_type
->is_abstract())
7440 arg_type
= inc
.type();
7443 mpc_init2(val
, mpc_precision
);
7444 mpc_set_fr_fr(val
, r
, i
, MPC_RNDNN
);
7448 Type
* type
= Builtin_call_expression::complex_type(arg_type
);
7449 nc
->set_complex(type
, val
);
7459 // Give an error if we are discarding the value of an expression which
7460 // should not normally be discarded. We don't give an error for
7461 // discarding the value of an ordinary function call, but we do for
7462 // builtin functions, purely for consistency with the gc compiler.
7465 Builtin_call_expression::do_discarding_value()
7467 switch (this->code_
)
7469 case BUILTIN_INVALID
:
7473 case BUILTIN_APPEND
:
7475 case BUILTIN_COMPLEX
:
7481 case BUILTIN_ALIGNOF
:
7482 case BUILTIN_OFFSETOF
:
7483 case BUILTIN_SIZEOF
:
7484 this->unused_value_error();
7489 case BUILTIN_DELETE
:
7492 case BUILTIN_PRINTLN
:
7493 case BUILTIN_RECOVER
:
7501 Builtin_call_expression::do_type()
7503 switch (this->code_
)
7505 case BUILTIN_INVALID
:
7512 const Expression_list
* args
= this->args();
7513 if (args
== NULL
|| args
->empty())
7514 return Type::make_error_type();
7515 return Type::make_pointer_type(args
->front()->type());
7521 return Type::lookup_integer_type("int");
7523 case BUILTIN_ALIGNOF
:
7524 case BUILTIN_OFFSETOF
:
7525 case BUILTIN_SIZEOF
:
7526 return Type::lookup_integer_type("uintptr");
7529 case BUILTIN_DELETE
:
7532 case BUILTIN_PRINTLN
:
7533 return Type::make_void_type();
7535 case BUILTIN_RECOVER
:
7536 return Type::make_empty_interface_type(Linemap::predeclared_location());
7538 case BUILTIN_APPEND
:
7540 const Expression_list
* args
= this->args();
7541 if (args
== NULL
|| args
->empty())
7542 return Type::make_error_type();
7543 Type
*ret
= args
->front()->type();
7544 if (!ret
->is_slice_type())
7545 return Type::make_error_type();
7552 Expression
* arg
= this->one_arg();
7554 return Type::make_error_type();
7555 Type
* t
= arg
->type();
7556 if (t
->is_abstract())
7557 t
= t
->make_non_abstract_type();
7558 t
= Builtin_call_expression::real_imag_type(t
);
7560 t
= Type::make_error_type();
7564 case BUILTIN_COMPLEX
:
7566 const Expression_list
* args
= this->args();
7567 if (args
== NULL
|| args
->size() != 2)
7568 return Type::make_error_type();
7569 Type
* t
= args
->front()->type();
7570 if (t
->is_abstract())
7572 t
= args
->back()->type();
7573 if (t
->is_abstract())
7574 t
= t
->make_non_abstract_type();
7576 t
= Builtin_call_expression::complex_type(t
);
7578 t
= Type::make_error_type();
7584 // Determine the type.
7587 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7589 if (!this->determining_types())
7592 this->fn()->determine_type_no_context();
7594 const Expression_list
* args
= this->args();
7597 Type
* arg_type
= NULL
;
7598 switch (this->code_
)
7601 case BUILTIN_PRINTLN
:
7602 // Do not force a large integer constant to "int".
7608 arg_type
= Builtin_call_expression::complex_type(context
->type
);
7609 if (arg_type
== NULL
)
7610 arg_type
= Type::lookup_complex_type("complex128");
7614 case BUILTIN_COMPLEX
:
7616 // For the complex function the type of one operand can
7617 // determine the type of the other, as in a binary expression.
7618 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7619 if (arg_type
== NULL
)
7620 arg_type
= Type::lookup_float_type("float64");
7621 if (args
!= NULL
&& args
->size() == 2)
7623 Type
* t1
= args
->front()->type();
7624 Type
* t2
= args
->back()->type();
7625 if (!t1
->is_abstract())
7627 else if (!t2
->is_abstract())
7641 for (Expression_list::const_iterator pa
= args
->begin();
7645 Type_context subcontext
;
7646 subcontext
.type
= arg_type
;
7650 // We want to print large constants, we so can't just
7651 // use the appropriate nonabstract type. Use uint64 for
7652 // an integer if we know it is nonnegative, otherwise
7653 // use int64 for a integer, otherwise use float64 for a
7654 // float or complex128 for a complex.
7655 Type
* want_type
= NULL
;
7656 Type
* atype
= (*pa
)->type();
7657 if (atype
->is_abstract())
7659 if (atype
->integer_type() != NULL
)
7661 Numeric_constant nc
;
7662 if (this->numeric_constant_value(&nc
))
7665 if (nc
.to_int(&val
))
7667 if (mpz_sgn(val
) >= 0)
7668 want_type
= Type::lookup_integer_type("uint64");
7672 if (want_type
== NULL
)
7673 want_type
= Type::lookup_integer_type("int64");
7675 else if (atype
->float_type() != NULL
)
7676 want_type
= Type::lookup_float_type("float64");
7677 else if (atype
->complex_type() != NULL
)
7678 want_type
= Type::lookup_complex_type("complex128");
7679 else if (atype
->is_abstract_string_type())
7680 want_type
= Type::lookup_string_type();
7681 else if (atype
->is_abstract_boolean_type())
7682 want_type
= Type::lookup_bool_type();
7685 subcontext
.type
= want_type
;
7689 (*pa
)->determine_type(&subcontext
);
7694 // If there is exactly one argument, return true. Otherwise give an
7695 // error message and return false.
7698 Builtin_call_expression::check_one_arg()
7700 const Expression_list
* args
= this->args();
7701 if (args
== NULL
|| args
->size() < 1)
7703 this->report_error(_("not enough arguments"));
7706 else if (args
->size() > 1)
7708 this->report_error(_("too many arguments"));
7711 if (args
->front()->is_error_expression()
7712 || args
->front()->type()->is_error())
7714 this->set_is_error();
7720 // Check argument types for a builtin function.
7723 Builtin_call_expression::do_check_types(Gogo
*)
7725 if (this->is_error_expression())
7727 switch (this->code_
)
7729 case BUILTIN_INVALID
:
7732 case BUILTIN_DELETE
:
7738 // The single argument may be either a string or an array or a
7739 // map or a channel, or a pointer to a closed array.
7740 if (this->check_one_arg())
7742 Type
* arg_type
= this->one_arg()->type();
7743 if (arg_type
->points_to() != NULL
7744 && arg_type
->points_to()->array_type() != NULL
7745 && !arg_type
->points_to()->is_slice_type())
7746 arg_type
= arg_type
->points_to();
7747 if (this->code_
== BUILTIN_CAP
)
7749 if (!arg_type
->is_error()
7750 && arg_type
->array_type() == NULL
7751 && arg_type
->channel_type() == NULL
)
7752 this->report_error(_("argument must be array or slice "
7757 if (!arg_type
->is_error()
7758 && !arg_type
->is_string_type()
7759 && arg_type
->array_type() == NULL
7760 && arg_type
->map_type() == NULL
7761 && arg_type
->channel_type() == NULL
)
7762 this->report_error(_("argument must be string or "
7763 "array or slice or map or channel"));
7770 case BUILTIN_PRINTLN
:
7772 const Expression_list
* args
= this->args();
7775 if (this->code_
== BUILTIN_PRINT
)
7776 warning_at(this->location(), 0,
7777 "no arguments for builtin function %<%s%>",
7778 (this->code_
== BUILTIN_PRINT
7784 for (Expression_list::const_iterator p
= args
->begin();
7788 Type
* type
= (*p
)->type();
7789 if (type
->is_error()
7790 || type
->is_string_type()
7791 || type
->integer_type() != NULL
7792 || type
->float_type() != NULL
7793 || type
->complex_type() != NULL
7794 || type
->is_boolean_type()
7795 || type
->points_to() != NULL
7796 || type
->interface_type() != NULL
7797 || type
->channel_type() != NULL
7798 || type
->map_type() != NULL
7799 || type
->function_type() != NULL
7800 || type
->is_slice_type())
7802 else if ((*p
)->is_type_expression())
7804 // If this is a type expression it's going to give
7805 // an error anyhow, so we don't need one here.
7808 this->report_error(_("unsupported argument type to "
7809 "builtin function"));
7816 if (this->check_one_arg())
7818 if (this->one_arg()->type()->channel_type() == NULL
)
7819 this->report_error(_("argument must be channel"));
7820 else if (!this->one_arg()->type()->channel_type()->may_send())
7821 this->report_error(_("cannot close receive-only channel"));
7826 case BUILTIN_SIZEOF
:
7827 case BUILTIN_ALIGNOF
:
7828 this->check_one_arg();
7831 case BUILTIN_RECOVER
:
7832 if (this->args() != NULL
7833 && !this->args()->empty()
7834 && !this->recover_arg_is_set_
)
7835 this->report_error(_("too many arguments"));
7838 case BUILTIN_OFFSETOF
:
7839 if (this->check_one_arg())
7841 Expression
* arg
= this->one_arg();
7842 if (arg
->field_reference_expression() == NULL
)
7843 this->report_error(_("argument must be a field reference"));
7849 const Expression_list
* args
= this->args();
7850 if (args
== NULL
|| args
->size() < 2)
7852 this->report_error(_("not enough arguments"));
7855 else if (args
->size() > 2)
7857 this->report_error(_("too many arguments"));
7860 Type
* arg1_type
= args
->front()->type();
7861 Type
* arg2_type
= args
->back()->type();
7862 if (arg1_type
->is_error() || arg2_type
->is_error())
7864 this->set_is_error();
7869 if (arg1_type
->is_slice_type())
7870 e1
= arg1_type
->array_type()->element_type();
7873 this->report_error(_("left argument must be a slice"));
7877 if (arg2_type
->is_slice_type())
7879 Type
* e2
= arg2_type
->array_type()->element_type();
7880 if (!Type::are_identical(e1
, e2
, true, NULL
))
7881 this->report_error(_("element types must be the same"));
7883 else if (arg2_type
->is_string_type())
7885 if (e1
->integer_type() == NULL
|| !e1
->integer_type()->is_byte())
7886 this->report_error(_("first argument must be []byte"));
7889 this->report_error(_("second argument must be slice or string"));
7893 case BUILTIN_APPEND
:
7895 const Expression_list
* args
= this->args();
7896 if (args
== NULL
|| args
->size() < 2)
7898 this->report_error(_("not enough arguments"));
7901 if (args
->size() > 2)
7903 this->report_error(_("too many arguments"));
7906 if (args
->front()->type()->is_error()
7907 || args
->back()->type()->is_error())
7909 this->set_is_error();
7913 Array_type
* at
= args
->front()->type()->array_type();
7914 Type
* e
= at
->element_type();
7916 // The language permits appending a string to a []byte, as a
7918 if (args
->back()->type()->is_string_type())
7920 if (e
->integer_type() != NULL
&& e
->integer_type()->is_byte())
7924 // The language says that the second argument must be
7925 // assignable to a slice of the element type of the first
7926 // argument. We already know the first argument is a slice
7928 Type
* arg2_type
= Type::make_array_type(e
, NULL
);
7930 if (!Type::are_assignable(arg2_type
, args
->back()->type(), &reason
))
7933 this->report_error(_("argument 2 has invalid type"));
7936 error_at(this->location(), "argument 2 has invalid type (%s)",
7938 this->set_is_error();
7946 if (this->check_one_arg())
7948 if (this->one_arg()->type()->complex_type() == NULL
)
7949 this->report_error(_("argument must have complex type"));
7953 case BUILTIN_COMPLEX
:
7955 const Expression_list
* args
= this->args();
7956 if (args
== NULL
|| args
->size() < 2)
7957 this->report_error(_("not enough arguments"));
7958 else if (args
->size() > 2)
7959 this->report_error(_("too many arguments"));
7960 else if (args
->front()->is_error_expression()
7961 || args
->front()->type()->is_error()
7962 || args
->back()->is_error_expression()
7963 || args
->back()->type()->is_error())
7964 this->set_is_error();
7965 else if (!Type::are_identical(args
->front()->type(),
7966 args
->back()->type(), true, NULL
))
7967 this->report_error(_("complex arguments must have identical types"));
7968 else if (args
->front()->type()->float_type() == NULL
)
7969 this->report_error(_("complex arguments must have "
7970 "floating-point type"));
7980 Builtin_call_expression::do_copy()
7982 Call_expression
* bce
=
7983 new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
7984 (this->args() == NULL
7986 : this->args()->copy()),
7990 if (this->varargs_are_lowered())
7991 bce
->set_varargs_are_lowered();
7995 // Return the backend representation for a builtin function.
7998 Builtin_call_expression::do_get_backend(Translate_context
* context
)
8000 Gogo
* gogo
= context
->gogo();
8001 Location location
= this->location();
8002 switch (this->code_
)
8004 case BUILTIN_INVALID
:
8012 const Expression_list
* args
= this->args();
8013 go_assert(args
!= NULL
&& args
->size() == 1);
8014 Expression
* arg
= args
->front();
8015 Type
* arg_type
= arg
->type();
8019 go_assert(saw_errors());
8020 return context
->backend()->error_expression();
8023 this->seen_
= false;
8024 if (arg_type
->points_to() != NULL
)
8026 arg_type
= arg_type
->points_to();
8027 go_assert(arg_type
->array_type() != NULL
8028 && !arg_type
->is_slice_type());
8029 arg
= Expression::make_unary(OPERATOR_MULT
, arg
, location
);
8032 Type
* int_type
= Type::lookup_integer_type("int");
8034 if (this->code_
== BUILTIN_LEN
)
8036 if (arg_type
->is_string_type())
8037 val
= Expression::make_string_info(arg
, STRING_INFO_LENGTH
,
8039 else if (arg_type
->array_type() != NULL
)
8043 go_assert(saw_errors());
8044 return context
->backend()->error_expression();
8047 val
= arg_type
->array_type()->get_length(gogo
, arg
);
8048 this->seen_
= false;
8050 else if (arg_type
->map_type() != NULL
)
8051 val
= Runtime::make_call(Runtime::MAP_LEN
, location
, 1, arg
);
8052 else if (arg_type
->channel_type() != NULL
)
8053 val
= Runtime::make_call(Runtime::CHAN_LEN
, location
, 1, arg
);
8059 if (arg_type
->array_type() != NULL
)
8063 go_assert(saw_errors());
8064 return context
->backend()->error_expression();
8067 val
= arg_type
->array_type()->get_capacity(gogo
, arg
);
8068 this->seen_
= false;
8070 else if (arg_type
->channel_type() != NULL
)
8071 val
= Runtime::make_call(Runtime::CHAN_CAP
, location
, 1, arg
);
8076 return Expression::make_cast(int_type
, val
,
8077 location
)->get_backend(context
);
8081 case BUILTIN_PRINTLN
:
8083 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
8084 Expression
* print_stmts
= NULL
;
8086 const Expression_list
* call_args
= this->args();
8087 if (call_args
!= NULL
)
8089 for (Expression_list::const_iterator p
= call_args
->begin();
8090 p
!= call_args
->end();
8093 if (is_ln
&& p
!= call_args
->begin())
8095 Expression
* print_space
=
8096 Runtime::make_call(Runtime::PRINT_SPACE
,
8097 this->location(), 0);
8100 Expression::make_compound(print_stmts
, print_space
,
8104 Expression
* arg
= *p
;
8105 Type
* type
= arg
->type();
8106 Runtime::Function code
;
8107 if (type
->is_string_type())
8108 code
= Runtime::PRINT_STRING
;
8109 else if (type
->integer_type() != NULL
8110 && type
->integer_type()->is_unsigned())
8112 Type
* itype
= Type::lookup_integer_type("uint64");
8113 arg
= Expression::make_cast(itype
, arg
, location
);
8114 code
= Runtime::PRINT_UINT64
;
8116 else if (type
->integer_type() != NULL
)
8118 Type
* itype
= Type::lookup_integer_type("int64");
8119 arg
= Expression::make_cast(itype
, arg
, location
);
8120 code
= Runtime::PRINT_INT64
;
8122 else if (type
->float_type() != NULL
)
8124 Type
* dtype
= Type::lookup_float_type("float64");
8125 arg
= Expression::make_cast(dtype
, arg
, location
);
8126 code
= Runtime::PRINT_DOUBLE
;
8128 else if (type
->complex_type() != NULL
)
8130 Type
* ctype
= Type::lookup_complex_type("complex128");
8131 arg
= Expression::make_cast(ctype
, arg
, location
);
8132 code
= Runtime::PRINT_COMPLEX
;
8134 else if (type
->is_boolean_type())
8135 code
= Runtime::PRINT_BOOL
;
8136 else if (type
->points_to() != NULL
8137 || type
->channel_type() != NULL
8138 || type
->map_type() != NULL
8139 || type
->function_type() != NULL
)
8141 arg
= Expression::make_cast(type
, arg
, location
);
8142 code
= Runtime::PRINT_POINTER
;
8144 else if (type
->interface_type() != NULL
)
8146 if (type
->interface_type()->is_empty())
8147 code
= Runtime::PRINT_EMPTY_INTERFACE
;
8149 code
= Runtime::PRINT_INTERFACE
;
8151 else if (type
->is_slice_type())
8152 code
= Runtime::PRINT_SLICE
;
8155 go_assert(saw_errors());
8156 return context
->backend()->error_expression();
8159 Expression
* call
= Runtime::make_call(code
, location
, 1, arg
);
8160 if (print_stmts
== NULL
)
8163 print_stmts
= Expression::make_compound(print_stmts
, call
,
8170 Expression
* print_nl
=
8171 Runtime::make_call(Runtime::PRINT_NL
, location
, 0);
8172 if (print_stmts
== NULL
)
8173 print_stmts
= print_nl
;
8175 print_stmts
= Expression::make_compound(print_stmts
, print_nl
,
8179 // There aren't any arguments to the print builtin. The compiler
8180 // issues a warning for this so we should avoid getting the backend
8181 // representation for this call. Instead, perform a no-op.
8182 if (print_stmts
== NULL
)
8183 return context
->backend()->boolean_constant_expression(false);
8185 return print_stmts
->get_backend(context
);
8190 const Expression_list
* args
= this->args();
8191 go_assert(args
!= NULL
&& args
->size() == 1);
8192 Expression
* arg
= args
->front();
8194 Type::make_empty_interface_type(Linemap::predeclared_location());
8195 arg
= Expression::convert_for_assignment(gogo
, empty
, arg
, location
);
8198 Runtime::make_call(Runtime::PANIC
, location
, 1, arg
);
8199 return panic
->get_backend(context
);
8202 case BUILTIN_RECOVER
:
8204 // The argument is set when building recover thunks. It's a
8205 // boolean value which is true if we can recover a value now.
8206 const Expression_list
* args
= this->args();
8207 go_assert(args
!= NULL
&& args
->size() == 1);
8208 Expression
* arg
= args
->front();
8210 Type::make_empty_interface_type(Linemap::predeclared_location());
8212 Expression
* nil
= Expression::make_nil(location
);
8213 nil
= Expression::convert_for_assignment(gogo
, empty
, nil
, location
);
8215 // We need to handle a deferred call to recover specially,
8216 // because it changes whether it can recover a panic or not.
8217 // See test7 in test/recover1.go.
8218 Expression
* recover
= Runtime::make_call((this->is_deferred()
8219 ? Runtime::DEFERRED_RECOVER
8220 : Runtime::RECOVER
),
8223 Expression::make_conditional(arg
, recover
, nil
, location
);
8224 return cond
->get_backend(context
);
8229 const Expression_list
* args
= this->args();
8230 go_assert(args
!= NULL
&& args
->size() == 1);
8231 Expression
* arg
= args
->front();
8232 Expression
* close
= Runtime::make_call(Runtime::CLOSE
, location
,
8234 return close
->get_backend(context
);
8237 case BUILTIN_SIZEOF
:
8238 case BUILTIN_OFFSETOF
:
8239 case BUILTIN_ALIGNOF
:
8241 Numeric_constant nc
;
8243 if (!this->numeric_constant_value(&nc
)
8244 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
8246 go_assert(saw_errors());
8247 return context
->backend()->error_expression();
8249 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8252 Expression
* int_cst
=
8253 Expression::make_integer_z(&ival
, uintptr_type
, location
);
8255 return int_cst
->get_backend(context
);
8260 const Expression_list
* args
= this->args();
8261 go_assert(args
!= NULL
&& args
->size() == 2);
8262 Expression
* arg1
= args
->front();
8263 Expression
* arg2
= args
->back();
8265 Type
* arg1_type
= arg1
->type();
8266 Array_type
* at
= arg1_type
->array_type();
8267 go_assert(arg1
->is_variable());
8268 Expression
* arg1_val
= at
->get_value_pointer(gogo
, arg1
);
8269 Expression
* arg1_len
= at
->get_length(gogo
, arg1
);
8271 Type
* arg2_type
= arg2
->type();
8272 go_assert(arg2
->is_variable());
8273 Expression
* arg2_val
;
8274 Expression
* arg2_len
;
8275 if (arg2_type
->is_slice_type())
8277 at
= arg2_type
->array_type();
8278 arg2_val
= at
->get_value_pointer(gogo
, arg2
);
8279 arg2_len
= at
->get_length(gogo
, arg2
);
8283 go_assert(arg2
->is_variable());
8284 arg2_val
= Expression::make_string_info(arg2
, STRING_INFO_DATA
,
8286 arg2_len
= Expression::make_string_info(arg2
, STRING_INFO_LENGTH
,
8290 Expression::make_binary(OPERATOR_LT
, arg1_len
, arg2_len
, location
);
8291 Expression
* length
=
8292 Expression::make_conditional(cond
, arg1_len
, arg2_len
, location
);
8294 Type
* element_type
= at
->element_type();
8295 Btype
* element_btype
= element_type
->get_backend(gogo
);
8296 int64_t element_size
= gogo
->backend()->type_size(element_btype
);
8297 Expression
* size_expr
= Expression::make_integer_int64(element_size
,
8300 Expression
* bytecount
=
8301 Expression::make_binary(OPERATOR_MULT
, size_expr
, length
, location
);
8302 Expression
* copy
= Runtime::make_call(Runtime::COPY
, location
, 3,
8303 arg1_val
, arg2_val
, bytecount
);
8305 Expression
* compound
= Expression::make_compound(copy
, length
, location
);
8306 return compound
->get_backend(context
);
8309 case BUILTIN_APPEND
:
8311 const Expression_list
* args
= this->args();
8312 go_assert(args
!= NULL
&& args
->size() == 2);
8313 Expression
* arg1
= args
->front();
8314 Expression
* arg2
= args
->back();
8316 Array_type
* at
= arg1
->type()->array_type();
8317 Type
* element_type
= at
->element_type()->forwarded();
8319 go_assert(arg2
->is_variable());
8320 Expression
* arg2_val
;
8321 Expression
* arg2_len
;
8323 if (arg2
->type()->is_string_type()
8324 && element_type
->integer_type() != NULL
8325 && element_type
->integer_type()->is_byte())
8327 arg2_val
= Expression::make_string_info(arg2
, STRING_INFO_DATA
,
8329 arg2_len
= Expression::make_string_info(arg2
, STRING_INFO_LENGTH
,
8335 arg2_val
= at
->get_value_pointer(gogo
, arg2
);
8336 arg2_len
= at
->get_length(gogo
, arg2
);
8337 Btype
* element_btype
= element_type
->get_backend(gogo
);
8338 size
= gogo
->backend()->type_size(element_btype
);
8340 Expression
* element_size
=
8341 Expression::make_integer_int64(size
, NULL
, location
);
8343 Expression
* append
= Runtime::make_call(Runtime::APPEND
, location
, 4,
8344 arg1
, arg2_val
, arg2_len
,
8346 append
= Expression::make_unsafe_cast(arg1
->type(), append
, location
);
8347 return append
->get_backend(context
);
8353 const Expression_list
* args
= this->args();
8354 go_assert(args
!= NULL
&& args
->size() == 1);
8357 Bexpression
* bcomplex
= args
->front()->get_backend(context
);
8358 if (this->code_
== BUILTIN_REAL
)
8359 ret
= gogo
->backend()->real_part_expression(bcomplex
, location
);
8361 ret
= gogo
->backend()->imag_part_expression(bcomplex
, location
);
8365 case BUILTIN_COMPLEX
:
8367 const Expression_list
* args
= this->args();
8368 go_assert(args
!= NULL
&& args
->size() == 2);
8369 Bexpression
* breal
= args
->front()->get_backend(context
);
8370 Bexpression
* bimag
= args
->back()->get_backend(context
);
8371 return gogo
->backend()->complex_expression(breal
, bimag
, location
);
8379 // We have to support exporting a builtin call expression, because
8380 // code can set a constant to the result of a builtin expression.
8383 Builtin_call_expression::do_export(Export
* exp
) const
8385 Numeric_constant nc
;
8386 if (!this->numeric_constant_value(&nc
))
8388 error_at(this->location(), "value is not constant");
8396 Integer_expression::export_integer(exp
, val
);
8399 else if (nc
.is_float())
8402 nc
.get_float(&fval
);
8403 Float_expression::export_float(exp
, fval
);
8406 else if (nc
.is_complex())
8409 nc
.get_complex(&cval
);
8410 Complex_expression::export_complex(exp
, cval
);
8416 // A trailing space lets us reliably identify the end of the number.
8417 exp
->write_c_string(" ");
8420 // Class Call_expression.
8422 // A Go function can be viewed in a couple of different ways. The
8423 // code of a Go function becomes a backend function with parameters
8424 // whose types are simply the backend representation of the Go types.
8425 // If there are multiple results, they are returned as a backend
8428 // However, when Go code refers to a function other than simply
8429 // calling it, the backend type of that function is actually a struct.
8430 // The first field of the struct points to the Go function code
8431 // (sometimes a wrapper as described below). The remaining fields
8432 // hold addresses of closed-over variables. This struct is called a
8435 // There are a few cases to consider.
8437 // A direct function call of a known function in package scope. In
8438 // this case there are no closed-over variables, and we know the name
8439 // of the function code. We can simply produce a backend call to the
8440 // function directly, and not worry about the closure.
8442 // A direct function call of a known function literal. In this case
8443 // we know the function code and we know the closure. We generate the
8444 // function code such that it expects an additional final argument of
8445 // the closure type. We pass the closure as the last argument, after
8446 // the other arguments.
8448 // An indirect function call. In this case we have a closure. We
8449 // load the pointer to the function code from the first field of the
8450 // closure. We pass the address of the closure as the last argument.
8452 // A call to a method of an interface. Type methods are always at
8453 // package scope, so we call the function directly, and don't worry
8454 // about the closure.
8456 // This means that for a function at package scope we have two cases.
8457 // One is the direct call, which has no closure. The other is the
8458 // indirect call, which does have a closure. We can't simply ignore
8459 // the closure, even though it is the last argument, because that will
8460 // fail on targets where the function pops its arguments. So when
8461 // generating a closure for a package-scope function we set the
8462 // function code pointer in the closure to point to a wrapper
8463 // function. This wrapper function accepts a final argument that
8464 // points to the closure, ignores it, and calls the real function as a
8465 // direct function call. This wrapper will normally be efficient, and
8466 // can often simply be a tail call to the real function.
8468 // We don't use GCC's static chain pointer because 1) we don't need
8469 // it; 2) GCC only permits using a static chain to call a known
8470 // function, so we can't use it for an indirect call anyhow. Since we
8471 // can't use it for an indirect call, we may as well not worry about
8472 // using it for a direct call either.
8474 // We pass the closure last rather than first because it means that
8475 // the function wrapper we put into a closure for a package-scope
8476 // function can normally just be a tail call to the real function.
8478 // For method expressions we generate a wrapper that loads the
8479 // receiver from the closure and then calls the method. This
8480 // unfortunately forces reshuffling the arguments, since there is a
8481 // new first argument, but we can't avoid reshuffling either for
8482 // method expressions or for indirect calls of package-scope
8483 // functions, and since the latter are more common we reshuffle for
8484 // method expressions.
8486 // Note that the Go code retains the Go types. The extra final
8487 // argument only appears when we convert to the backend
8493 Call_expression::do_traverse(Traverse
* traverse
)
8495 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8496 return TRAVERSE_EXIT
;
8497 if (this->args_
!= NULL
)
8499 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8500 return TRAVERSE_EXIT
;
8502 return TRAVERSE_CONTINUE
;
8505 // Lower a call statement.
8508 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
8509 Statement_inserter
* inserter
, int)
8511 Location loc
= this->location();
8513 // A type cast can look like a function call.
8514 if (this->fn_
->is_type_expression()
8515 && this->args_
!= NULL
8516 && this->args_
->size() == 1)
8517 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8520 // Because do_type will return an error type and thus prevent future
8521 // errors, check for that case now to ensure that the error gets
8523 Function_type
* fntype
= this->get_function_type();
8526 if (!this->fn_
->type()->is_error())
8527 this->report_error(_("expected function"));
8528 this->set_is_error();
8532 // Handle an argument which is a call to a function which returns
8533 // multiple results.
8534 if (this->args_
!= NULL
8535 && this->args_
->size() == 1
8536 && this->args_
->front()->call_expression() != NULL
)
8538 size_t rc
= this->args_
->front()->call_expression()->result_count();
8540 && ((fntype
->parameters() != NULL
8541 && (fntype
->parameters()->size() == rc
8542 || (fntype
->is_varargs()
8543 && fntype
->parameters()->size() - 1 <= rc
)))
8544 || fntype
->is_builtin()))
8546 Call_expression
* call
= this->args_
->front()->call_expression();
8547 call
->set_is_multi_value_arg();
8548 if (this->is_varargs_
)
8550 // It is not clear which result of a multiple result call
8551 // the ellipsis operator should be applied to. If we unpack the
8552 // the call into its individual results here, the ellipsis will be
8553 // applied to the last result.
8554 error_at(call
->location(),
8555 _("multiple-value argument in single-value context"));
8556 return Expression::make_error(call
->location());
8559 Expression_list
* args
= new Expression_list
;
8560 for (size_t i
= 0; i
< rc
; ++i
)
8561 args
->push_back(Expression::make_call_result(call
, i
));
8562 // We can't return a new call expression here, because this
8563 // one may be referenced by Call_result expressions. We
8564 // also can't delete the old arguments, because we may still
8565 // traverse them somewhere up the call stack. FIXME.
8570 // Recognize a call to a builtin function.
8571 if (fntype
->is_builtin())
8572 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8573 this->is_varargs_
, loc
);
8575 // If this call returns multiple results, create a temporary
8576 // variable for each result.
8577 size_t rc
= this->result_count();
8578 if (rc
> 1 && this->results_
== NULL
)
8580 std::vector
<Temporary_statement
*>* temps
=
8581 new std::vector
<Temporary_statement
*>;
8583 const Typed_identifier_list
* results
= fntype
->results();
8584 for (Typed_identifier_list::const_iterator p
= results
->begin();
8585 p
!= results
->end();
8588 Temporary_statement
* temp
= Statement::make_temporary(p
->type(),
8590 inserter
->insert(temp
);
8591 temps
->push_back(temp
);
8593 this->results_
= temps
;
8596 // Handle a call to a varargs function by packaging up the extra
8598 if (fntype
->is_varargs())
8600 const Typed_identifier_list
* parameters
= fntype
->parameters();
8601 go_assert(parameters
!= NULL
&& !parameters
->empty());
8602 Type
* varargs_type
= parameters
->back().type();
8603 this->lower_varargs(gogo
, function
, inserter
, varargs_type
,
8604 parameters
->size());
8607 // If this is call to a method, call the method directly passing the
8608 // object as the first parameter.
8609 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8612 Named_object
* methodfn
= bme
->function();
8613 Expression
* first_arg
= bme
->first_argument();
8615 // We always pass a pointer when calling a method.
8616 if (first_arg
->type()->points_to() == NULL
8617 && !first_arg
->type()->is_error())
8619 first_arg
= Expression::make_unary(OPERATOR_AND
, first_arg
, loc
);
8620 // We may need to create a temporary variable so that we can
8621 // take the address. We can't do that here because it will
8622 // mess up the order of evaluation.
8623 Unary_expression
* ue
= static_cast<Unary_expression
*>(first_arg
);
8624 ue
->set_create_temp();
8627 // If we are calling a method which was inherited from an
8628 // embedded struct, and the method did not get a stub, then the
8629 // first type may be wrong.
8630 Type
* fatype
= bme
->first_argument_type();
8633 if (fatype
->points_to() == NULL
)
8634 fatype
= Type::make_pointer_type(fatype
);
8635 first_arg
= Expression::make_unsafe_cast(fatype
, first_arg
, loc
);
8638 Expression_list
* new_args
= new Expression_list();
8639 new_args
->push_back(first_arg
);
8640 if (this->args_
!= NULL
)
8642 for (Expression_list::const_iterator p
= this->args_
->begin();
8643 p
!= this->args_
->end();
8645 new_args
->push_back(*p
);
8648 // We have to change in place because this structure may be
8649 // referenced by Call_result_expressions. We can't delete the
8650 // old arguments, because we may be traversing them up in some
8652 this->args_
= new_args
;
8653 this->fn_
= Expression::make_func_reference(methodfn
, NULL
,
8660 // Lower a call to a varargs function. FUNCTION is the function in
8661 // which the call occurs--it's not the function we are calling.
8662 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8663 // PARAM_COUNT is the number of parameters of the function we are
8664 // calling; the last of these parameters will be the varargs
8668 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8669 Statement_inserter
* inserter
,
8670 Type
* varargs_type
, size_t param_count
)
8672 if (this->varargs_are_lowered_
)
8675 Location loc
= this->location();
8677 go_assert(param_count
> 0);
8678 go_assert(varargs_type
->is_slice_type());
8680 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8681 if (arg_count
< param_count
- 1)
8683 // Not enough arguments; will be caught in check_types.
8687 Expression_list
* old_args
= this->args_
;
8688 Expression_list
* new_args
= new Expression_list();
8689 bool push_empty_arg
= false;
8690 if (old_args
== NULL
|| old_args
->empty())
8692 go_assert(param_count
== 1);
8693 push_empty_arg
= true;
8697 Expression_list::const_iterator pa
;
8699 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8701 if (static_cast<size_t>(i
) == param_count
)
8703 new_args
->push_back(*pa
);
8706 // We have reached the varargs parameter.
8708 bool issued_error
= false;
8709 if (pa
== old_args
->end())
8710 push_empty_arg
= true;
8711 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8712 new_args
->push_back(*pa
);
8713 else if (this->is_varargs_
)
8715 if ((*pa
)->type()->is_slice_type())
8716 this->report_error(_("too many arguments"));
8719 error_at(this->location(),
8720 _("invalid use of %<...%> with non-slice"));
8721 this->set_is_error();
8727 Type
* element_type
= varargs_type
->array_type()->element_type();
8728 Expression_list
* vals
= new Expression_list
;
8729 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8731 // Check types here so that we get a better message.
8732 Type
* patype
= (*pa
)->type();
8733 Location paloc
= (*pa
)->location();
8734 if (!this->check_argument_type(i
, element_type
, patype
,
8735 paloc
, issued_error
))
8737 vals
->push_back(*pa
);
8740 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8741 gogo
->lower_expression(function
, inserter
, &val
);
8742 new_args
->push_back(val
);
8747 new_args
->push_back(Expression::make_nil(loc
));
8749 // We can't return a new call expression here, because this one may
8750 // be referenced by Call_result expressions. FIXME. We can't
8751 // delete OLD_ARGS because we may have both a Call_expression and a
8752 // Builtin_call_expression which refer to them. FIXME.
8753 this->args_
= new_args
;
8754 this->varargs_are_lowered_
= true;
8757 // Flatten a call with multiple results into a temporary.
8760 Call_expression::do_flatten(Gogo
* gogo
, Named_object
*,
8761 Statement_inserter
* inserter
)
8763 if (this->is_erroneous_call())
8765 go_assert(saw_errors());
8766 return Expression::make_error(this->location());
8769 if (this->is_flattened_
)
8771 this->is_flattened_
= true;
8773 // Add temporary variables for all arguments that require type
8775 Function_type
* fntype
= this->get_function_type();
8778 go_assert(saw_errors());
8781 if (this->args_
!= NULL
&& !this->args_
->empty()
8782 && fntype
->parameters() != NULL
&& !fntype
->parameters()->empty())
8784 bool is_interface_method
=
8785 this->fn_
->interface_field_reference_expression() != NULL
;
8787 Expression_list
*args
= new Expression_list();
8788 Typed_identifier_list::const_iterator pp
= fntype
->parameters()->begin();
8789 Expression_list::const_iterator pa
= this->args_
->begin();
8790 if (!is_interface_method
&& fntype
->is_method())
8792 // The receiver argument.
8793 args
->push_back(*pa
);
8796 for (; pa
!= this->args_
->end(); ++pa
, ++pp
)
8798 go_assert(pp
!= fntype
->parameters()->end());
8799 if (Type::are_identical(pp
->type(), (*pa
)->type(), true, NULL
))
8800 args
->push_back(*pa
);
8803 Location loc
= (*pa
)->location();
8804 Expression
* arg
= *pa
;
8805 if (!arg
->is_variable())
8807 Temporary_statement
*temp
=
8808 Statement::make_temporary(NULL
, arg
, loc
);
8809 inserter
->insert(temp
);
8810 arg
= Expression::make_temporary_reference(temp
, loc
);
8812 arg
= Expression::convert_for_assignment(gogo
, pp
->type(), arg
,
8814 args
->push_back(arg
);
8821 size_t rc
= this->result_count();
8822 if (rc
> 1 && this->call_temp_
== NULL
)
8824 Struct_field_list
* sfl
= new Struct_field_list();
8825 Function_type
* fntype
= this->get_function_type();
8826 const Typed_identifier_list
* results
= fntype
->results();
8827 Location loc
= this->location();
8831 for (Typed_identifier_list::const_iterator p
= results
->begin();
8832 p
!= results
->end();
8835 snprintf(buf
, sizeof buf
, "res%d", i
);
8836 sfl
->push_back(Struct_field(Typed_identifier(buf
, p
->type(), loc
)));
8839 Struct_type
* st
= Type::make_struct_type(sfl
, loc
);
8840 this->call_temp_
= Statement::make_temporary(st
, NULL
, loc
);
8841 inserter
->insert(this->call_temp_
);
8847 // Get the function type. This can return NULL in error cases.
8850 Call_expression::get_function_type() const
8852 return this->fn_
->type()->function_type();
8855 // Return the number of values which this call will return.
8858 Call_expression::result_count() const
8860 const Function_type
* fntype
= this->get_function_type();
8863 if (fntype
->results() == NULL
)
8865 return fntype
->results()->size();
8868 // Return the temporary which holds a result.
8870 Temporary_statement
*
8871 Call_expression::result(size_t i
) const
8873 if (this->results_
== NULL
|| this->results_
->size() <= i
)
8875 go_assert(saw_errors());
8878 return (*this->results_
)[i
];
8881 // Set the number of results expected from a call expression.
8884 Call_expression::set_expected_result_count(size_t count
)
8886 go_assert(this->expected_result_count_
== 0);
8887 this->expected_result_count_
= count
;
8890 // Return whether this is a call to the predeclared function recover.
8893 Call_expression::is_recover_call() const
8895 return this->do_is_recover_call();
8898 // Set the argument to the recover function.
8901 Call_expression::set_recover_arg(Expression
* arg
)
8903 this->do_set_recover_arg(arg
);
8906 // Virtual functions also implemented by Builtin_call_expression.
8909 Call_expression::do_is_recover_call() const
8915 Call_expression::do_set_recover_arg(Expression
*)
8920 // We have found an error with this call expression; return true if
8921 // we should report it.
8924 Call_expression::issue_error()
8926 if (this->issued_error_
)
8930 this->issued_error_
= true;
8935 // Whether or not this call contains errors, either in the call or the
8936 // arguments to the call.
8939 Call_expression::is_erroneous_call()
8941 if (this->is_error_expression() || this->fn()->is_error_expression())
8944 if (this->args() == NULL
)
8946 for (Expression_list::iterator pa
= this->args()->begin();
8947 pa
!= this->args()->end();
8950 if ((*pa
)->type()->is_error_type() || (*pa
)->is_error_expression())
8959 Call_expression::do_type()
8961 if (this->type_
!= NULL
)
8965 Function_type
* fntype
= this->get_function_type();
8967 return Type::make_error_type();
8969 const Typed_identifier_list
* results
= fntype
->results();
8970 if (results
== NULL
)
8971 ret
= Type::make_void_type();
8972 else if (results
->size() == 1)
8973 ret
= results
->begin()->type();
8975 ret
= Type::make_call_multiple_result_type(this);
8982 // Determine types for a call expression. We can use the function
8983 // parameter types to set the types of the arguments.
8986 Call_expression::do_determine_type(const Type_context
*)
8988 if (!this->determining_types())
8991 this->fn_
->determine_type_no_context();
8992 Function_type
* fntype
= this->get_function_type();
8993 const Typed_identifier_list
* parameters
= NULL
;
8995 parameters
= fntype
->parameters();
8996 if (this->args_
!= NULL
)
8998 Typed_identifier_list::const_iterator pt
;
8999 if (parameters
!= NULL
)
9000 pt
= parameters
->begin();
9002 for (Expression_list::const_iterator pa
= this->args_
->begin();
9003 pa
!= this->args_
->end();
9009 // If this is a method, the first argument is the
9011 if (fntype
!= NULL
&& fntype
->is_method())
9013 Type
* rtype
= fntype
->receiver()->type();
9014 // The receiver is always passed as a pointer.
9015 if (rtype
->points_to() == NULL
)
9016 rtype
= Type::make_pointer_type(rtype
);
9017 Type_context
subcontext(rtype
, false);
9018 (*pa
)->determine_type(&subcontext
);
9023 if (parameters
!= NULL
&& pt
!= parameters
->end())
9025 Type_context
subcontext(pt
->type(), false);
9026 (*pa
)->determine_type(&subcontext
);
9030 (*pa
)->determine_type_no_context();
9035 // Called when determining types for a Call_expression. Return true
9036 // if we should go ahead, false if they have already been determined.
9039 Call_expression::determining_types()
9041 if (this->types_are_determined_
)
9045 this->types_are_determined_
= true;
9050 // Check types for parameter I.
9053 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
9054 const Type
* argument_type
,
9055 Location argument_location
,
9059 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
9064 error_at(argument_location
, "argument %d has incompatible type", i
);
9066 error_at(argument_location
,
9067 "argument %d has incompatible type (%s)",
9070 this->set_is_error();
9079 Call_expression::do_check_types(Gogo
*)
9081 if (this->classification() == EXPRESSION_ERROR
)
9084 Function_type
* fntype
= this->get_function_type();
9087 if (!this->fn_
->type()->is_error())
9088 this->report_error(_("expected function"));
9092 if (this->expected_result_count_
!= 0
9093 && this->expected_result_count_
!= this->result_count())
9095 if (this->issue_error())
9096 this->report_error(_("function result count mismatch"));
9097 this->set_is_error();
9101 bool is_method
= fntype
->is_method();
9104 go_assert(this->args_
!= NULL
&& !this->args_
->empty());
9105 Type
* rtype
= fntype
->receiver()->type();
9106 Expression
* first_arg
= this->args_
->front();
9107 // We dereference the values since receivers are always passed
9110 if (!Type::are_assignable(rtype
->deref(), first_arg
->type()->deref(),
9114 this->report_error(_("incompatible type for receiver"));
9117 error_at(this->location(),
9118 "incompatible type for receiver (%s)",
9120 this->set_is_error();
9125 // Note that varargs was handled by the lower_varargs() method, so
9126 // we don't have to worry about it here unless something is wrong.
9127 if (this->is_varargs_
&& !this->varargs_are_lowered_
)
9129 if (!fntype
->is_varargs())
9131 error_at(this->location(),
9132 _("invalid use of %<...%> calling non-variadic function"));
9133 this->set_is_error();
9138 const Typed_identifier_list
* parameters
= fntype
->parameters();
9139 if (this->args_
== NULL
)
9141 if (parameters
!= NULL
&& !parameters
->empty())
9142 this->report_error(_("not enough arguments"));
9144 else if (parameters
== NULL
)
9146 if (!is_method
|| this->args_
->size() > 1)
9147 this->report_error(_("too many arguments"));
9149 else if (this->args_
->size() == 1
9150 && this->args_
->front()->call_expression() != NULL
9151 && this->args_
->front()->call_expression()->result_count() > 1)
9153 // This is F(G()) when G returns more than one result. If the
9154 // results can be matched to parameters, it would have been
9155 // lowered in do_lower. If we get here we know there is a
9157 if (this->args_
->front()->call_expression()->result_count()
9158 < parameters
->size())
9159 this->report_error(_("not enough arguments"));
9161 this->report_error(_("too many arguments"));
9166 Expression_list::const_iterator pa
= this->args_
->begin();
9169 for (Typed_identifier_list::const_iterator pt
= parameters
->begin();
9170 pt
!= parameters
->end();
9173 if (pa
== this->args_
->end())
9175 this->report_error(_("not enough arguments"));
9178 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
9179 (*pa
)->location(), false);
9181 if (pa
!= this->args_
->end())
9182 this->report_error(_("too many arguments"));
9187 Call_expression::do_copy()
9189 Call_expression
* call
=
9190 Expression::make_call(this->fn_
->copy(),
9191 (this->args_
== NULL
9193 : this->args_
->copy()),
9194 this->is_varargs_
, this->location());
9196 if (this->varargs_are_lowered_
)
9197 call
->set_varargs_are_lowered();
9201 // Return whether we have to use a temporary variable to ensure that
9202 // we evaluate this call expression in order. If the call returns no
9203 // results then it will inevitably be executed last.
9206 Call_expression::do_must_eval_in_order() const
9208 return this->result_count() > 0;
9211 // Get the function and the first argument to use when calling an
9212 // interface method.
9215 Call_expression::interface_method_function(
9216 Interface_field_reference_expression
* interface_method
,
9217 Expression
** first_arg_ptr
)
9219 *first_arg_ptr
= interface_method
->get_underlying_object();
9220 return interface_method
->get_function();
9223 // Build the call expression.
9226 Call_expression::do_get_backend(Translate_context
* context
)
9228 if (this->call_
!= NULL
)
9231 Function_type
* fntype
= this->get_function_type();
9233 return context
->backend()->error_expression();
9235 if (this->fn_
->is_error_expression())
9236 return context
->backend()->error_expression();
9238 Gogo
* gogo
= context
->gogo();
9239 Location location
= this->location();
9241 Func_expression
* func
= this->fn_
->func_expression();
9242 Interface_field_reference_expression
* interface_method
=
9243 this->fn_
->interface_field_reference_expression();
9244 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
9245 const bool is_interface_method
= interface_method
!= NULL
;
9247 bool has_closure_arg
;
9249 has_closure_arg
= true;
9250 else if (func
!= NULL
)
9251 has_closure_arg
= false;
9252 else if (is_interface_method
)
9253 has_closure_arg
= false;
9255 has_closure_arg
= true;
9258 std::vector
<Bexpression
*> fn_args
;
9259 if (this->args_
== NULL
|| this->args_
->empty())
9261 nargs
= is_interface_method
? 1 : 0;
9265 else if (fntype
->parameters() == NULL
|| fntype
->parameters()->empty())
9267 // Passing a receiver parameter.
9268 go_assert(!is_interface_method
9269 && fntype
->is_method()
9270 && this->args_
->size() == 1);
9273 fn_args
[0] = this->args_
->front()->get_backend(context
);
9277 const Typed_identifier_list
* params
= fntype
->parameters();
9279 nargs
= this->args_
->size();
9280 int i
= is_interface_method
? 1 : 0;
9282 fn_args
.resize(nargs
);
9284 Typed_identifier_list::const_iterator pp
= params
->begin();
9285 Expression_list::const_iterator pe
= this->args_
->begin();
9286 if (!is_interface_method
&& fntype
->is_method())
9288 fn_args
[i
] = (*pe
)->get_backend(context
);
9292 for (; pe
!= this->args_
->end(); ++pe
, ++pp
, ++i
)
9294 go_assert(pp
!= params
->end());
9296 Expression::convert_for_assignment(gogo
, pp
->type(), *pe
,
9298 fn_args
[i
] = arg
->get_backend(context
);
9300 go_assert(pp
== params
->end());
9301 go_assert(i
== nargs
);
9305 Expression
* closure
= NULL
;
9308 Named_object
* no
= func
->named_object();
9309 fn
= Expression::make_func_code_reference(no
, location
);
9311 closure
= func
->closure();
9313 else if (!is_interface_method
)
9315 closure
= this->fn_
;
9317 // The backend representation of this function type is a pointer
9318 // to a struct whose first field is the actual function to call.
9320 Type::make_pointer_type(
9321 Type::make_pointer_type(Type::make_void_type()));
9322 fn
= Expression::make_unsafe_cast(pfntype
, this->fn_
, location
);
9323 fn
= Expression::make_unary(OPERATOR_MULT
, fn
, location
);
9327 Expression
* first_arg
;
9328 fn
= this->interface_method_function(interface_method
, &first_arg
);
9329 fn_args
[0] = first_arg
->get_backend(context
);
9332 Bexpression
* bclosure
= NULL
;
9333 if (has_closure_arg
)
9334 bclosure
= closure
->get_backend(context
);
9336 go_assert(closure
== NULL
);
9338 Bexpression
* bfn
= fn
->get_backend(context
);
9340 // When not calling a named function directly, use a type conversion
9341 // in case the type of the function is a recursive type which refers
9342 // to itself. We don't do this for an interface method because 1)
9343 // an interface method never refers to itself, so we always have a
9344 // function type here; 2) we pass an extra first argument to an
9345 // interface method, so fntype is not correct.
9346 if (func
== NULL
&& !is_interface_method
)
9348 Btype
* bft
= fntype
->get_backend_fntype(gogo
);
9349 bfn
= gogo
->backend()->convert_expression(bft
, bfn
, location
);
9352 Bexpression
* call
= gogo
->backend()->call_expression(bfn
, fn_args
,
9353 bclosure
, location
);
9355 if (this->results_
!= NULL
)
9357 go_assert(this->call_temp_
!= NULL
);
9358 Expression
* call_ref
=
9359 Expression::make_temporary_reference(this->call_temp_
, location
);
9360 Bexpression
* bcall_ref
= call_ref
->get_backend(context
);
9361 Bstatement
* assn_stmt
=
9362 gogo
->backend()->assignment_statement(bcall_ref
, call
, location
);
9364 this->call_
= this->set_results(context
, bcall_ref
);
9366 Bexpression
* set_and_call
=
9367 gogo
->backend()->compound_expression(assn_stmt
, this->call_
,
9369 return set_and_call
;
9376 // Set the result variables if this call returns multiple results.
9379 Call_expression::set_results(Translate_context
* context
, Bexpression
* call
)
9381 Gogo
* gogo
= context
->gogo();
9383 Bexpression
* results
= NULL
;
9384 Location loc
= this->location();
9386 size_t rc
= this->result_count();
9387 for (size_t i
= 0; i
< rc
; ++i
)
9389 Temporary_statement
* temp
= this->result(i
);
9392 go_assert(saw_errors());
9393 return gogo
->backend()->error_expression();
9395 Temporary_reference_expression
* ref
=
9396 Expression::make_temporary_reference(temp
, loc
);
9397 ref
->set_is_lvalue();
9399 Bexpression
* result_ref
= ref
->get_backend(context
);
9400 Bexpression
* call_result
=
9401 gogo
->backend()->struct_field_expression(call
, i
, loc
);
9402 Bstatement
* assn_stmt
=
9403 gogo
->backend()->assignment_statement(result_ref
, call_result
, loc
);
9405 Bexpression
* result
=
9406 gogo
->backend()->compound_expression(assn_stmt
, call_result
, loc
);
9408 if (results
== NULL
)
9412 Bstatement
* expr_stmt
= gogo
->backend()->expression_statement(result
);
9414 gogo
->backend()->compound_expression(expr_stmt
, results
, loc
);
9420 // Dump ast representation for a call expressin.
9423 Call_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
9425 this->fn_
->dump_expression(ast_dump_context
);
9426 ast_dump_context
->ostream() << "(";
9428 ast_dump_context
->dump_expression_list(this->args_
);
9430 ast_dump_context
->ostream() << ") ";
9433 // Make a call expression.
9436 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
9439 return new Call_expression(fn
, args
, is_varargs
, location
);
9442 // Class Call_result_expression.
9444 // Traverse a call result.
9447 Call_result_expression::do_traverse(Traverse
* traverse
)
9449 if (traverse
->remember_expression(this->call_
))
9451 // We have already traversed the call expression.
9452 return TRAVERSE_CONTINUE
;
9454 return Expression::traverse(&this->call_
, traverse
);
9460 Call_result_expression::do_type()
9462 if (this->classification() == EXPRESSION_ERROR
)
9463 return Type::make_error_type();
9465 // THIS->CALL_ can be replaced with a temporary reference due to
9466 // Call_expression::do_must_eval_in_order when there is an error.
9467 Call_expression
* ce
= this->call_
->call_expression();
9470 this->set_is_error();
9471 return Type::make_error_type();
9473 Function_type
* fntype
= ce
->get_function_type();
9476 if (ce
->issue_error())
9478 if (!ce
->fn()->type()->is_error())
9479 this->report_error(_("expected function"));
9481 this->set_is_error();
9482 return Type::make_error_type();
9484 const Typed_identifier_list
* results
= fntype
->results();
9485 if (results
== NULL
|| results
->size() < 2)
9487 if (ce
->issue_error())
9488 this->report_error(_("number of results does not match "
9489 "number of values"));
9490 return Type::make_error_type();
9492 Typed_identifier_list::const_iterator pr
= results
->begin();
9493 for (unsigned int i
= 0; i
< this->index_
; ++i
)
9495 if (pr
== results
->end())
9499 if (pr
== results
->end())
9501 if (ce
->issue_error())
9502 this->report_error(_("number of results does not match "
9503 "number of values"));
9504 return Type::make_error_type();
9509 // Check the type. Just make sure that we trigger the warning in
9513 Call_result_expression::do_check_types(Gogo
*)
9518 // Determine the type. We have nothing to do here, but the 0 result
9519 // needs to pass down to the caller.
9522 Call_result_expression::do_determine_type(const Type_context
*)
9524 this->call_
->determine_type_no_context();
9527 // Return the backend representation. We just refer to the temporary set by the
9528 // call expression. We don't do this at lowering time because it makes it
9529 // hard to evaluate the call at the right time.
9532 Call_result_expression::do_get_backend(Translate_context
* context
)
9534 Call_expression
* ce
= this->call_
->call_expression();
9537 go_assert(this->call_
->is_error_expression());
9538 return context
->backend()->error_expression();
9540 Temporary_statement
* ts
= ce
->result(this->index_
);
9543 go_assert(saw_errors());
9544 return context
->backend()->error_expression();
9546 Expression
* ref
= Expression::make_temporary_reference(ts
, this->location());
9547 return ref
->get_backend(context
);
9550 // Dump ast representation for a call result expression.
9553 Call_result_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
9556 // FIXME: Wouldn't it be better if the call is assigned to a temporary
9557 // (struct) and the fields are referenced instead.
9558 ast_dump_context
->ostream() << this->index_
<< "@(";
9559 ast_dump_context
->dump_expression(this->call_
);
9560 ast_dump_context
->ostream() << ")";
9563 // Make a reference to a single result of a call which returns
9564 // multiple results.
9567 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
9569 return new Call_result_expression(call
, index
);
9572 // Class Index_expression.
9577 Index_expression::do_traverse(Traverse
* traverse
)
9579 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
9580 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
9581 || (this->end_
!= NULL
9582 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9583 || (this->cap_
!= NULL
9584 && Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
))
9585 return TRAVERSE_EXIT
;
9586 return TRAVERSE_CONTINUE
;
9589 // Lower an index expression. This converts the generic index
9590 // expression into an array index, a string index, or a map index.
9593 Index_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
9595 Location location
= this->location();
9596 Expression
* left
= this->left_
;
9597 Expression
* start
= this->start_
;
9598 Expression
* end
= this->end_
;
9599 Expression
* cap
= this->cap_
;
9601 Type
* type
= left
->type();
9602 if (type
->is_error())
9604 go_assert(saw_errors());
9605 return Expression::make_error(location
);
9607 else if (left
->is_type_expression())
9609 error_at(location
, "attempt to index type expression");
9610 return Expression::make_error(location
);
9612 else if (type
->array_type() != NULL
)
9613 return Expression::make_array_index(left
, start
, end
, cap
, location
);
9614 else if (type
->points_to() != NULL
9615 && type
->points_to()->array_type() != NULL
9616 && !type
->points_to()->is_slice_type())
9618 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
9621 // For an ordinary index into the array, the pointer will be
9622 // dereferenced. For a slice it will not--the resulting slice
9623 // will simply reuse the pointer, which is incorrect if that
9625 if (end
!= NULL
|| cap
!= NULL
)
9626 deref
->issue_nil_check();
9628 return Expression::make_array_index(deref
, start
, end
, cap
, location
);
9630 else if (type
->is_string_type())
9634 error_at(location
, "invalid 3-index slice of string");
9635 return Expression::make_error(location
);
9637 return Expression::make_string_index(left
, start
, end
, location
);
9639 else if (type
->map_type() != NULL
)
9641 if (end
!= NULL
|| cap
!= NULL
)
9643 error_at(location
, "invalid slice of map");
9644 return Expression::make_error(location
);
9646 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
9648 if (this->is_lvalue_
)
9649 ret
->set_is_lvalue();
9655 "attempt to index object which is not array, string, or map");
9656 return Expression::make_error(location
);
9660 // Write an indexed expression
9661 // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context.
9664 Index_expression::dump_index_expression(Ast_dump_context
* ast_dump_context
,
9665 const Expression
* expr
,
9666 const Expression
* start
,
9667 const Expression
* end
,
9668 const Expression
* cap
)
9670 expr
->dump_expression(ast_dump_context
);
9671 ast_dump_context
->ostream() << "[";
9672 start
->dump_expression(ast_dump_context
);
9675 ast_dump_context
->ostream() << ":";
9676 end
->dump_expression(ast_dump_context
);
9680 ast_dump_context
->ostream() << ":";
9681 cap
->dump_expression(ast_dump_context
);
9683 ast_dump_context
->ostream() << "]";
9686 // Dump ast representation for an index expression.
9689 Index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
9692 Index_expression::dump_index_expression(ast_dump_context
, this->left_
,
9693 this->start_
, this->end_
, this->cap_
);
9696 // Make an index expression.
9699 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
9700 Expression
* cap
, Location location
)
9702 return new Index_expression(left
, start
, end
, cap
, location
);
9705 // Class Array_index_expression.
9707 // Array index traversal.
9710 Array_index_expression::do_traverse(Traverse
* traverse
)
9712 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9713 return TRAVERSE_EXIT
;
9714 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9715 return TRAVERSE_EXIT
;
9716 if (this->end_
!= NULL
)
9718 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9719 return TRAVERSE_EXIT
;
9721 if (this->cap_
!= NULL
)
9723 if (Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
)
9724 return TRAVERSE_EXIT
;
9726 return TRAVERSE_CONTINUE
;
9729 // Return the type of an array index.
9732 Array_index_expression::do_type()
9734 if (this->type_
== NULL
)
9736 Array_type
* type
= this->array_
->type()->array_type();
9738 this->type_
= Type::make_error_type();
9739 else if (this->end_
== NULL
)
9740 this->type_
= type
->element_type();
9741 else if (type
->is_slice_type())
9743 // A slice of a slice has the same type as the original
9745 this->type_
= this->array_
->type()->deref();
9749 // A slice of an array is a slice.
9750 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9756 // Set the type of an array index.
9759 Array_index_expression::do_determine_type(const Type_context
*)
9761 this->array_
->determine_type_no_context();
9762 this->start_
->determine_type_no_context();
9763 if (this->end_
!= NULL
)
9764 this->end_
->determine_type_no_context();
9765 if (this->cap_
!= NULL
)
9766 this->cap_
->determine_type_no_context();
9769 // Check types of an array index.
9772 Array_index_expression::do_check_types(Gogo
*)
9774 Numeric_constant nc
;
9776 if (this->start_
->type()->integer_type() == NULL
9777 && !this->start_
->type()->is_error()
9778 && (!this->start_
->numeric_constant_value(&nc
)
9779 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
9780 this->report_error(_("index must be integer"));
9781 if (this->end_
!= NULL
9782 && this->end_
->type()->integer_type() == NULL
9783 && !this->end_
->type()->is_error()
9784 && !this->end_
->is_nil_expression()
9785 && !this->end_
->is_error_expression()
9786 && (!this->end_
->numeric_constant_value(&nc
)
9787 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
9788 this->report_error(_("slice end must be integer"));
9789 if (this->cap_
!= NULL
9790 && this->cap_
->type()->integer_type() == NULL
9791 && !this->cap_
->type()->is_error()
9792 && !this->cap_
->is_nil_expression()
9793 && !this->cap_
->is_error_expression()
9794 && (!this->cap_
->numeric_constant_value(&nc
)
9795 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
9796 this->report_error(_("slice capacity must be integer"));
9798 Array_type
* array_type
= this->array_
->type()->array_type();
9799 if (array_type
== NULL
)
9801 go_assert(this->array_
->type()->is_error());
9805 unsigned int int_bits
=
9806 Type::lookup_integer_type("int")->integer_type()->bits();
9808 Numeric_constant lvalnc
;
9810 bool lval_valid
= (array_type
->length() != NULL
9811 && array_type
->length()->numeric_constant_value(&lvalnc
)
9812 && lvalnc
.to_int(&lval
));
9813 Numeric_constant inc
;
9815 bool ival_valid
= false;
9816 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
9819 if (mpz_sgn(ival
) < 0
9820 || mpz_sizeinbase(ival
, 2) >= int_bits
9822 && (this->end_
== NULL
9823 ? mpz_cmp(ival
, lval
) >= 0
9824 : mpz_cmp(ival
, lval
) > 0)))
9826 error_at(this->start_
->location(), "array index out of bounds");
9827 this->set_is_error();
9830 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9832 Numeric_constant enc
;
9834 bool eval_valid
= false;
9835 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
9838 if (mpz_sgn(eval
) < 0
9839 || mpz_sizeinbase(eval
, 2) >= int_bits
9840 || (lval_valid
&& mpz_cmp(eval
, lval
) > 0))
9842 error_at(this->end_
->location(), "array index out of bounds");
9843 this->set_is_error();
9845 else if (ival_valid
&& mpz_cmp(ival
, eval
) > 0)
9846 this->report_error(_("inverted slice range"));
9849 Numeric_constant cnc
;
9851 if (this->cap_
!= NULL
9852 && this->cap_
->numeric_constant_value(&cnc
) && cnc
.to_int(&cval
))
9854 if (mpz_sgn(cval
) < 0
9855 || mpz_sizeinbase(cval
, 2) >= int_bits
9856 || (lval_valid
&& mpz_cmp(cval
, lval
) > 0))
9858 error_at(this->cap_
->location(), "array index out of bounds");
9859 this->set_is_error();
9861 else if (ival_valid
&& mpz_cmp(ival
, cval
) > 0)
9863 error_at(this->cap_
->location(),
9864 "invalid slice index: capacity less than start");
9865 this->set_is_error();
9867 else if (eval_valid
&& mpz_cmp(eval
, cval
) > 0)
9869 error_at(this->cap_
->location(),
9870 "invalid slice index: capacity less than length");
9871 this->set_is_error();
9884 // A slice of an array requires an addressable array. A slice of a
9885 // slice is always possible.
9886 if (this->end_
!= NULL
&& !array_type
->is_slice_type())
9888 if (!this->array_
->is_addressable())
9889 this->report_error(_("slice of unaddressable value"));
9891 this->array_
->address_taken(true);
9895 // Flatten array indexing by using temporary variables for slices and indexes.
9898 Array_index_expression::do_flatten(Gogo
*, Named_object
*,
9899 Statement_inserter
* inserter
)
9901 Location loc
= this->location();
9902 Expression
* array
= this->array_
;
9903 Expression
* start
= this->start_
;
9904 Expression
* end
= this->end_
;
9905 Expression
* cap
= this->cap_
;
9906 if (array
->is_error_expression()
9907 || array
->type()->is_error_type()
9908 || start
->is_error_expression()
9909 || start
->type()->is_error_type()
9911 && (end
->is_error_expression() || end
->type()->is_error_type()))
9913 && (cap
->is_error_expression() || cap
->type()->is_error_type())))
9915 go_assert(saw_errors());
9916 return Expression::make_error(loc
);
9919 Temporary_statement
* temp
;
9920 if (array
->type()->is_slice_type() && !array
->is_variable())
9922 temp
= Statement::make_temporary(NULL
, array
, loc
);
9923 inserter
->insert(temp
);
9924 this->array_
= Expression::make_temporary_reference(temp
, loc
);
9926 if (!start
->is_variable())
9928 temp
= Statement::make_temporary(NULL
, start
, loc
);
9929 inserter
->insert(temp
);
9930 this->start_
= Expression::make_temporary_reference(temp
, loc
);
9933 && !end
->is_nil_expression()
9934 && !end
->is_variable())
9936 temp
= Statement::make_temporary(NULL
, end
, loc
);
9937 inserter
->insert(temp
);
9938 this->end_
= Expression::make_temporary_reference(temp
, loc
);
9940 if (cap
!= NULL
&& !cap
->is_variable())
9942 temp
= Statement::make_temporary(NULL
, cap
, loc
);
9943 inserter
->insert(temp
);
9944 this->cap_
= Expression::make_temporary_reference(temp
, loc
);
9950 // Return whether this expression is addressable.
9953 Array_index_expression::do_is_addressable() const
9955 // A slice expression is not addressable.
9956 if (this->end_
!= NULL
)
9959 // An index into a slice is addressable.
9960 if (this->array_
->type()->is_slice_type())
9963 // An index into an array is addressable if the array is
9965 return this->array_
->is_addressable();
9968 // Get the backend representation for an array index.
9971 Array_index_expression::do_get_backend(Translate_context
* context
)
9973 Array_type
* array_type
= this->array_
->type()->array_type();
9974 if (array_type
== NULL
)
9976 go_assert(this->array_
->type()->is_error());
9977 return context
->backend()->error_expression();
9979 go_assert(!array_type
->is_slice_type() || this->array_
->is_variable());
9981 Location loc
= this->location();
9982 Gogo
* gogo
= context
->gogo();
9984 Type
* int_type
= Type::lookup_integer_type("int");
9985 Btype
* int_btype
= int_type
->get_backend(gogo
);
9987 // We need to convert the length and capacity to the Go "int" type here
9988 // because the length of a fixed-length array could be of type "uintptr"
9989 // and gimple disallows binary operations between "uintptr" and other
9990 // integer types. FIXME.
9991 Bexpression
* length
= NULL
;
9992 if (this->end_
== NULL
|| this->end_
->is_nil_expression())
9994 Expression
* len
= array_type
->get_length(gogo
, this->array_
);
9995 length
= len
->get_backend(context
);
9996 length
= gogo
->backend()->convert_expression(int_btype
, length
, loc
);
9999 Bexpression
* capacity
= NULL
;
10000 if (this->end_
!= NULL
)
10002 Expression
* cap
= array_type
->get_capacity(gogo
, this->array_
);
10003 capacity
= cap
->get_backend(context
);
10004 capacity
= gogo
->backend()->convert_expression(int_btype
, capacity
, loc
);
10007 Bexpression
* cap_arg
= capacity
;
10008 if (this->cap_
!= NULL
)
10010 cap_arg
= this->cap_
->get_backend(context
);
10011 cap_arg
= gogo
->backend()->convert_expression(int_btype
, cap_arg
, loc
);
10014 if (length
== NULL
)
10017 int code
= (array_type
->length() != NULL
10018 ? (this->end_
== NULL
10019 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
10020 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
10021 : (this->end_
== NULL
10022 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
10023 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
10024 Bexpression
* crash
= gogo
->runtime_error(code
, loc
)->get_backend(context
);
10026 if (this->start_
->type()->integer_type() == NULL
10027 && !Type::are_convertible(int_type
, this->start_
->type(), NULL
))
10029 go_assert(saw_errors());
10030 return context
->backend()->error_expression();
10033 Bexpression
* bad_index
=
10034 Expression::check_bounds(this->start_
, loc
)->get_backend(context
);
10036 Bexpression
* start
= this->start_
->get_backend(context
);
10037 start
= gogo
->backend()->convert_expression(int_btype
, start
, loc
);
10038 Bexpression
* start_too_large
=
10039 gogo
->backend()->binary_expression((this->end_
== NULL
10043 (this->end_
== NULL
10047 bad_index
= gogo
->backend()->binary_expression(OPERATOR_OROR
, start_too_large
,
10050 if (this->end_
== NULL
)
10052 // Simple array indexing. This has to return an l-value, so
10053 // wrap the index check into START.
10055 gogo
->backend()->conditional_expression(int_btype
, bad_index
,
10056 crash
, start
, loc
);
10059 if (array_type
->length() != NULL
)
10061 Bexpression
* array
= this->array_
->get_backend(context
);
10062 ret
= gogo
->backend()->array_index_expression(array
, start
, loc
);
10067 Expression
* valptr
=
10068 array_type
->get_value_pointer(gogo
, this->array_
);
10069 Bexpression
* ptr
= valptr
->get_backend(context
);
10070 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, start
, loc
);
10072 Type
* ele_type
= this->array_
->type()->array_type()->element_type();
10073 Btype
* ele_btype
= ele_type
->get_backend(gogo
);
10074 ret
= gogo
->backend()->indirect_expression(ele_btype
, ptr
, true, loc
);
10081 if (this->cap_
!= NULL
)
10083 Bexpression
* bounds_bcheck
=
10084 Expression::check_bounds(this->cap_
, loc
)->get_backend(context
);
10086 gogo
->backend()->binary_expression(OPERATOR_OROR
, bounds_bcheck
,
10088 cap_arg
= gogo
->backend()->convert_expression(int_btype
, cap_arg
, loc
);
10090 Bexpression
* cap_too_small
=
10091 gogo
->backend()->binary_expression(OPERATOR_LT
, cap_arg
, start
, loc
);
10092 Bexpression
* cap_too_large
=
10093 gogo
->backend()->binary_expression(OPERATOR_GT
, cap_arg
, capacity
, loc
);
10094 Bexpression
* bad_cap
=
10095 gogo
->backend()->binary_expression(OPERATOR_OROR
, cap_too_small
,
10096 cap_too_large
, loc
);
10097 bad_index
= gogo
->backend()->binary_expression(OPERATOR_OROR
, bad_cap
,
10102 if (this->end_
->is_nil_expression())
10106 Bexpression
* bounds_bcheck
=
10107 Expression::check_bounds(this->end_
, loc
)->get_backend(context
);
10110 gogo
->backend()->binary_expression(OPERATOR_OROR
, bounds_bcheck
,
10113 end
= this->end_
->get_backend(context
);
10114 end
= gogo
->backend()->convert_expression(int_btype
, end
, loc
);
10115 Bexpression
* end_too_small
=
10116 gogo
->backend()->binary_expression(OPERATOR_LT
, end
, start
, loc
);
10117 Bexpression
* end_too_large
=
10118 gogo
->backend()->binary_expression(OPERATOR_GT
, end
, cap_arg
, loc
);
10119 Bexpression
* bad_end
=
10120 gogo
->backend()->binary_expression(OPERATOR_OROR
, end_too_small
,
10121 end_too_large
, loc
);
10122 bad_index
= gogo
->backend()->binary_expression(OPERATOR_OROR
, bad_end
,
10126 Expression
* valptr
= array_type
->get_value_pointer(gogo
, this->array_
);
10127 Bexpression
* val
= valptr
->get_backend(context
);
10128 val
= gogo
->backend()->pointer_offset_expression(val
, start
, loc
);
10130 Bexpression
* result_length
=
10131 gogo
->backend()->binary_expression(OPERATOR_MINUS
, end
, start
, loc
);
10133 Bexpression
* result_capacity
=
10134 gogo
->backend()->binary_expression(OPERATOR_MINUS
, cap_arg
, start
, loc
);
10136 Btype
* struct_btype
= this->type()->get_backend(gogo
);
10137 std::vector
<Bexpression
*> init
;
10138 init
.push_back(val
);
10139 init
.push_back(result_length
);
10140 init
.push_back(result_capacity
);
10142 Bexpression
* ctor
=
10143 gogo
->backend()->constructor_expression(struct_btype
, init
, loc
);
10144 return gogo
->backend()->conditional_expression(struct_btype
, bad_index
,
10148 // Dump ast representation for an array index expression.
10151 Array_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10154 Index_expression::dump_index_expression(ast_dump_context
, this->array_
,
10155 this->start_
, this->end_
, this->cap_
);
10158 // Make an array index expression. END and CAP may be NULL.
10161 Expression::make_array_index(Expression
* array
, Expression
* start
,
10162 Expression
* end
, Expression
* cap
,
10165 return new Array_index_expression(array
, start
, end
, cap
, location
);
10168 // A string index. This is used for both indexing and slicing.
10170 class String_index_expression
: public Expression
10173 String_index_expression(Expression
* string
, Expression
* start
,
10174 Expression
* end
, Location location
)
10175 : Expression(EXPRESSION_STRING_INDEX
, location
),
10176 string_(string
), start_(start
), end_(end
)
10181 do_traverse(Traverse
*);
10184 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
10190 do_determine_type(const Type_context
*);
10193 do_check_types(Gogo
*);
10198 return Expression::make_string_index(this->string_
->copy(),
10199 this->start_
->copy(),
10200 (this->end_
== NULL
10202 : this->end_
->copy()),
10207 do_must_eval_subexpressions_in_order(int* skip
) const
10214 do_get_backend(Translate_context
*);
10217 do_dump_expression(Ast_dump_context
*) const;
10220 // The string we are getting a value from.
10221 Expression
* string_
;
10222 // The start or only index.
10223 Expression
* start_
;
10224 // The end index of a slice. This may be NULL for a single index,
10225 // or it may be a nil expression for the length of the string.
10229 // String index traversal.
10232 String_index_expression::do_traverse(Traverse
* traverse
)
10234 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
10235 return TRAVERSE_EXIT
;
10236 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
10237 return TRAVERSE_EXIT
;
10238 if (this->end_
!= NULL
)
10240 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
10241 return TRAVERSE_EXIT
;
10243 return TRAVERSE_CONTINUE
;
10247 String_index_expression::do_flatten(Gogo
*, Named_object
*,
10248 Statement_inserter
* inserter
)
10250 Location loc
= this->location();
10251 Expression
* string
= this->string_
;
10252 Expression
* start
= this->start_
;
10253 Expression
* end
= this->end_
;
10254 if (string
->is_error_expression()
10255 || string
->type()->is_error_type()
10256 || start
->is_error_expression()
10257 || start
->type()->is_error_type()
10259 && (end
->is_error_expression() || end
->type()->is_error_type())))
10261 go_assert(saw_errors());
10262 return Expression::make_error(loc
);
10265 Temporary_statement
* temp
;
10266 if (!this->string_
->is_variable())
10268 temp
= Statement::make_temporary(NULL
, this->string_
, loc
);
10269 inserter
->insert(temp
);
10270 this->string_
= Expression::make_temporary_reference(temp
, loc
);
10272 if (!this->start_
->is_variable())
10274 temp
= Statement::make_temporary(NULL
, this->start_
, loc
);
10275 inserter
->insert(temp
);
10276 this->start_
= Expression::make_temporary_reference(temp
, loc
);
10278 if (this->end_
!= NULL
10279 && !this->end_
->is_nil_expression()
10280 && !this->end_
->is_variable())
10282 temp
= Statement::make_temporary(NULL
, this->end_
, loc
);
10283 inserter
->insert(temp
);
10284 this->end_
= Expression::make_temporary_reference(temp
, loc
);
10290 // Return the type of a string index.
10293 String_index_expression::do_type()
10295 if (this->end_
== NULL
)
10296 return Type::lookup_integer_type("uint8");
10298 return this->string_
->type();
10301 // Determine the type of a string index.
10304 String_index_expression::do_determine_type(const Type_context
*)
10306 this->string_
->determine_type_no_context();
10307 this->start_
->determine_type_no_context();
10308 if (this->end_
!= NULL
)
10309 this->end_
->determine_type_no_context();
10312 // Check types of a string index.
10315 String_index_expression::do_check_types(Gogo
*)
10317 Numeric_constant nc
;
10319 if (this->start_
->type()->integer_type() == NULL
10320 && !this->start_
->type()->is_error()
10321 && (!this->start_
->numeric_constant_value(&nc
)
10322 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
10323 this->report_error(_("index must be integer"));
10324 if (this->end_
!= NULL
10325 && this->end_
->type()->integer_type() == NULL
10326 && !this->end_
->type()->is_error()
10327 && !this->end_
->is_nil_expression()
10328 && !this->end_
->is_error_expression()
10329 && (!this->end_
->numeric_constant_value(&nc
)
10330 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
10331 this->report_error(_("slice end must be integer"));
10334 bool sval_valid
= this->string_
->string_constant_value(&sval
);
10336 Numeric_constant inc
;
10338 bool ival_valid
= false;
10339 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
10342 if (mpz_sgn(ival
) < 0
10344 && (this->end_
== NULL
10345 ? mpz_cmp_ui(ival
, sval
.length()) >= 0
10346 : mpz_cmp_ui(ival
, sval
.length()) > 0)))
10348 error_at(this->start_
->location(), "string index out of bounds");
10349 this->set_is_error();
10352 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
10354 Numeric_constant enc
;
10356 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
10358 if (mpz_sgn(eval
) < 0
10359 || (sval_valid
&& mpz_cmp_ui(eval
, sval
.length()) > 0))
10361 error_at(this->end_
->location(), "string index out of bounds");
10362 this->set_is_error();
10364 else if (ival_valid
&& mpz_cmp(ival
, eval
) > 0)
10365 this->report_error(_("inverted slice range"));
10373 // Get the backend representation for a string index.
10376 String_index_expression::do_get_backend(Translate_context
* context
)
10378 Location loc
= this->location();
10379 Expression
* string_arg
= this->string_
;
10380 if (this->string_
->type()->points_to() != NULL
)
10381 string_arg
= Expression::make_unary(OPERATOR_MULT
, this->string_
, loc
);
10383 Expression
* bad_index
= Expression::check_bounds(this->start_
, loc
);
10385 int code
= (this->end_
== NULL
10386 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
10387 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
10389 Gogo
* gogo
= context
->gogo();
10390 Bexpression
* crash
= gogo
->runtime_error(code
, loc
)->get_backend(context
);
10392 Type
* int_type
= Type::lookup_integer_type("int");
10394 // It is possible that an error occurred earlier because the start index
10395 // cannot be represented as an integer type. In this case, we shouldn't
10396 // try casting the starting index into an integer since
10397 // Type_conversion_expression will fail to get the backend representation.
10399 if (this->start_
->type()->integer_type() == NULL
10400 && !Type::are_convertible(int_type
, this->start_
->type(), NULL
))
10402 go_assert(saw_errors());
10403 return context
->backend()->error_expression();
10406 Expression
* start
= Expression::make_cast(int_type
, this->start_
, loc
);
10408 if (this->end_
== NULL
)
10410 Expression
* length
=
10411 Expression::make_string_info(this->string_
, STRING_INFO_LENGTH
, loc
);
10413 Expression
* start_too_large
=
10414 Expression::make_binary(OPERATOR_GE
, start
, length
, loc
);
10415 bad_index
= Expression::make_binary(OPERATOR_OROR
, start_too_large
,
10417 Expression
* bytes
=
10418 Expression::make_string_info(this->string_
, STRING_INFO_DATA
, loc
);
10420 Bexpression
* bstart
= start
->get_backend(context
);
10421 Bexpression
* ptr
= bytes
->get_backend(context
);
10422 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, bstart
, loc
);
10423 Btype
* ubtype
= Type::lookup_integer_type("uint8")->get_backend(gogo
);
10424 Bexpression
* index
=
10425 gogo
->backend()->indirect_expression(ubtype
, ptr
, true, loc
);
10427 Btype
* byte_btype
= bytes
->type()->points_to()->get_backend(gogo
);
10428 Bexpression
* index_error
= bad_index
->get_backend(context
);
10429 return gogo
->backend()->conditional_expression(byte_btype
, index_error
,
10430 crash
, index
, loc
);
10433 Expression
* end
= NULL
;
10434 if (this->end_
->is_nil_expression())
10435 end
= Expression::make_integer_sl(-1, int_type
, loc
);
10438 Expression
* bounds_check
= Expression::check_bounds(this->end_
, loc
);
10440 Expression::make_binary(OPERATOR_OROR
, bounds_check
, bad_index
, loc
);
10441 end
= Expression::make_cast(int_type
, this->end_
, loc
);
10444 Expression
* strslice
= Runtime::make_call(Runtime::STRING_SLICE
, loc
, 3,
10445 string_arg
, start
, end
);
10446 Bexpression
* bstrslice
= strslice
->get_backend(context
);
10448 Btype
* str_btype
= strslice
->type()->get_backend(gogo
);
10449 Bexpression
* index_error
= bad_index
->get_backend(context
);
10450 return gogo
->backend()->conditional_expression(str_btype
, index_error
,
10451 crash
, bstrslice
, loc
);
10454 // Dump ast representation for a string index expression.
10457 String_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10460 Index_expression::dump_index_expression(ast_dump_context
, this->string_
,
10461 this->start_
, this->end_
, NULL
);
10464 // Make a string index expression. END may be NULL.
10467 Expression::make_string_index(Expression
* string
, Expression
* start
,
10468 Expression
* end
, Location location
)
10470 return new String_index_expression(string
, start
, end
, location
);
10473 // Class Map_index.
10475 // Get the type of the map.
10478 Map_index_expression::get_map_type() const
10480 Map_type
* mt
= this->map_
->type()->deref()->map_type();
10482 go_assert(saw_errors());
10486 // Map index traversal.
10489 Map_index_expression::do_traverse(Traverse
* traverse
)
10491 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
10492 return TRAVERSE_EXIT
;
10493 return Expression::traverse(&this->index_
, traverse
);
10496 // We need to pass in a pointer to the key, so flatten the index into a
10497 // temporary variable if it isn't already. The value pointer will be
10498 // dereferenced and checked for nil, so flatten into a temporary to avoid
10502 Map_index_expression::do_flatten(Gogo
* gogo
, Named_object
*,
10503 Statement_inserter
* inserter
)
10505 Location loc
= this->location();
10506 Map_type
* mt
= this->get_map_type();
10507 if (this->index()->is_error_expression()
10508 || this->index()->type()->is_error_type()
10509 || mt
->is_error_type())
10511 go_assert(saw_errors());
10512 return Expression::make_error(loc
);
10515 if (!Type::are_identical(mt
->key_type(), this->index_
->type(), false, NULL
))
10517 if (this->index_
->type()->interface_type() != NULL
10518 && !this->index_
->is_variable())
10520 Temporary_statement
* temp
=
10521 Statement::make_temporary(NULL
, this->index_
, loc
);
10522 inserter
->insert(temp
);
10523 this->index_
= Expression::make_temporary_reference(temp
, loc
);
10525 this->index_
= Expression::convert_for_assignment(gogo
, mt
->key_type(),
10526 this->index_
, loc
);
10529 if (!this->index_
->is_variable())
10531 Temporary_statement
* temp
= Statement::make_temporary(NULL
, this->index_
,
10533 inserter
->insert(temp
);
10534 this->index_
= Expression::make_temporary_reference(temp
, loc
);
10537 if (this->value_pointer_
== NULL
)
10538 this->get_value_pointer(this->is_lvalue_
);
10539 if (this->value_pointer_
->is_error_expression()
10540 || this->value_pointer_
->type()->is_error_type())
10541 return Expression::make_error(loc
);
10542 if (!this->value_pointer_
->is_variable())
10544 Temporary_statement
* temp
=
10545 Statement::make_temporary(NULL
, this->value_pointer_
, loc
);
10546 inserter
->insert(temp
);
10547 this->value_pointer_
= Expression::make_temporary_reference(temp
, loc
);
10553 // Return the type of a map index.
10556 Map_index_expression::do_type()
10558 Map_type
* mt
= this->get_map_type();
10560 return Type::make_error_type();
10561 Type
* type
= mt
->val_type();
10562 // If this map index is in a tuple assignment, we actually return a
10563 // pointer to the value type. Tuple_map_assignment_statement is
10564 // responsible for handling this correctly. We need to get the type
10565 // right in case this gets assigned to a temporary variable.
10566 if (this->is_in_tuple_assignment_
)
10567 type
= Type::make_pointer_type(type
);
10571 // Fix the type of a map index.
10574 Map_index_expression::do_determine_type(const Type_context
*)
10576 this->map_
->determine_type_no_context();
10577 Map_type
* mt
= this->get_map_type();
10578 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
10579 Type_context
subcontext(key_type
, false);
10580 this->index_
->determine_type(&subcontext
);
10583 // Check types of a map index.
10586 Map_index_expression::do_check_types(Gogo
*)
10588 std::string reason
;
10589 Map_type
* mt
= this->get_map_type();
10592 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
10594 if (reason
.empty())
10595 this->report_error(_("incompatible type for map index"));
10598 error_at(this->location(), "incompatible type for map index (%s)",
10600 this->set_is_error();
10605 // Get the backend representation for a map index.
10608 Map_index_expression::do_get_backend(Translate_context
* context
)
10610 Map_type
* type
= this->get_map_type();
10613 go_assert(saw_errors());
10614 return context
->backend()->error_expression();
10617 go_assert(this->value_pointer_
!= NULL
10618 && this->value_pointer_
->is_variable());
10621 if (this->is_lvalue_
)
10624 Expression::make_unary(OPERATOR_MULT
, this->value_pointer_
,
10626 ret
= val
->get_backend(context
);
10628 else if (this->is_in_tuple_assignment_
)
10630 // Tuple_map_assignment_statement is responsible for using this
10632 ret
= this->value_pointer_
->get_backend(context
);
10636 Location loc
= this->location();
10638 Expression
* nil_check
=
10639 Expression::make_binary(OPERATOR_EQEQ
, this->value_pointer_
,
10640 Expression::make_nil(loc
), loc
);
10641 Bexpression
* bnil_check
= nil_check
->get_backend(context
);
10643 Expression::make_unary(OPERATOR_MULT
, this->value_pointer_
, loc
);
10644 Bexpression
* bval
= val
->get_backend(context
);
10646 Gogo
* gogo
= context
->gogo();
10647 Btype
* val_btype
= type
->val_type()->get_backend(gogo
);
10648 Bexpression
* val_zero
= gogo
->backend()->zero_expression(val_btype
);
10649 ret
= gogo
->backend()->conditional_expression(val_btype
, bnil_check
,
10650 val_zero
, bval
, loc
);
10655 // Get an expression for the map index. This returns an expression which
10656 // evaluates to a pointer to a value. The pointer will be NULL if the key is
10660 Map_index_expression::get_value_pointer(bool insert
)
10662 if (this->value_pointer_
== NULL
)
10664 Map_type
* type
= this->get_map_type();
10667 go_assert(saw_errors());
10668 return Expression::make_error(this->location());
10671 Location loc
= this->location();
10672 Expression
* map_ref
= this->map_
;
10673 if (this->map_
->type()->points_to() != NULL
)
10674 map_ref
= Expression::make_unary(OPERATOR_MULT
, map_ref
, loc
);
10676 Expression
* index_ptr
= Expression::make_unary(OPERATOR_AND
, this->index_
,
10678 Expression
* map_index
=
10679 Runtime::make_call(Runtime::MAP_INDEX
, loc
, 3,
10680 map_ref
, index_ptr
,
10681 Expression::make_boolean(insert
, loc
));
10683 Type
* val_type
= type
->val_type();
10684 this->value_pointer_
=
10685 Expression::make_unsafe_cast(Type::make_pointer_type(val_type
),
10686 map_index
, this->location());
10688 return this->value_pointer_
;
10691 // Dump ast representation for a map index expression
10694 Map_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10697 Index_expression::dump_index_expression(ast_dump_context
, this->map_
,
10698 this->index_
, NULL
, NULL
);
10701 // Make a map index expression.
10703 Map_index_expression
*
10704 Expression::make_map_index(Expression
* map
, Expression
* index
,
10707 return new Map_index_expression(map
, index
, location
);
10710 // Class Field_reference_expression.
10712 // Lower a field reference expression. There is nothing to lower, but
10713 // this is where we generate the tracking information for fields with
10714 // the magic go:"track" tag.
10717 Field_reference_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
10718 Statement_inserter
* inserter
, int)
10720 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
10721 if (struct_type
== NULL
)
10723 // Error will be reported elsewhere.
10726 const Struct_field
* field
= struct_type
->field(this->field_index_
);
10729 if (!field
->has_tag())
10731 if (field
->tag().find("go:\"track\"") == std::string::npos
)
10734 // References from functions generated by the compiler don't count.
10735 if (function
!= NULL
&& function
->func_value()->is_type_specific_function())
10738 // We have found a reference to a tracked field. Build a call to
10739 // the runtime function __go_fieldtrack with a string that describes
10740 // the field. FIXME: We should only call this once per referenced
10741 // field per function, not once for each reference to the field.
10743 if (this->called_fieldtrack_
)
10745 this->called_fieldtrack_
= true;
10747 Location loc
= this->location();
10749 std::string s
= "fieldtrack \"";
10750 Named_type
* nt
= this->expr_
->type()->named_type();
10751 if (nt
== NULL
|| nt
->named_object()->package() == NULL
)
10752 s
.append(gogo
->pkgpath());
10754 s
.append(nt
->named_object()->package()->pkgpath());
10757 s
.append(Gogo::unpack_hidden_name(nt
->name()));
10759 s
.append(field
->field_name());
10762 // We can't use a string here, because internally a string holds a
10763 // pointer to the actual bytes; when the linker garbage collects the
10764 // string, it won't garbage collect the bytes. So we use a
10767 Expression
* length_expr
= Expression::make_integer_ul(s
.length(), NULL
, loc
);
10769 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
10770 Type
* array_type
= Type::make_array_type(byte_type
, length_expr
);
10772 Expression_list
* bytes
= new Expression_list();
10773 for (std::string::const_iterator p
= s
.begin(); p
!= s
.end(); p
++)
10775 unsigned char c
= static_cast<unsigned char>(*p
);
10776 bytes
->push_back(Expression::make_integer_ul(c
, NULL
, loc
));
10779 Expression
* e
= Expression::make_composite_literal(array_type
, 0, false,
10780 bytes
, false, loc
);
10782 Variable
* var
= new Variable(array_type
, e
, true, false, false, loc
);
10786 snprintf(buf
, sizeof buf
, "fieldtrack.%d", count
);
10789 Named_object
* no
= gogo
->add_variable(buf
, var
);
10790 e
= Expression::make_var_reference(no
, loc
);
10791 e
= Expression::make_unary(OPERATOR_AND
, e
, loc
);
10793 Expression
* call
= Runtime::make_call(Runtime::FIELDTRACK
, loc
, 1, e
);
10794 gogo
->lower_expression(function
, inserter
, &call
);
10795 inserter
->insert(Statement::make_statement(call
, false));
10797 // Put this function, and the global variable we just created, into
10798 // unique sections. This will permit the linker to garbage collect
10799 // them if they are not referenced. The effect is that the only
10800 // strings, indicating field references, that will wind up in the
10801 // executable will be those for functions that are actually needed.
10802 if (function
!= NULL
)
10803 function
->func_value()->set_in_unique_section();
10804 var
->set_in_unique_section();
10809 // Return the type of a field reference.
10812 Field_reference_expression::do_type()
10814 Type
* type
= this->expr_
->type();
10815 if (type
->is_error())
10817 Struct_type
* struct_type
= type
->struct_type();
10818 go_assert(struct_type
!= NULL
);
10819 return struct_type
->field(this->field_index_
)->type();
10822 // Check the types for a field reference.
10825 Field_reference_expression::do_check_types(Gogo
*)
10827 Type
* type
= this->expr_
->type();
10828 if (type
->is_error())
10830 Struct_type
* struct_type
= type
->struct_type();
10831 go_assert(struct_type
!= NULL
);
10832 go_assert(struct_type
->field(this->field_index_
) != NULL
);
10835 // Get the backend representation for a field reference.
10838 Field_reference_expression::do_get_backend(Translate_context
* context
)
10840 Bexpression
* bstruct
= this->expr_
->get_backend(context
);
10841 return context
->gogo()->backend()->struct_field_expression(bstruct
,
10842 this->field_index_
,
10846 // Dump ast representation for a field reference expression.
10849 Field_reference_expression::do_dump_expression(
10850 Ast_dump_context
* ast_dump_context
) const
10852 this->expr_
->dump_expression(ast_dump_context
);
10853 ast_dump_context
->ostream() << "." << this->field_index_
;
10856 // Make a reference to a qualified identifier in an expression.
10858 Field_reference_expression
*
10859 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
10862 return new Field_reference_expression(expr
, field_index
, location
);
10865 // Class Interface_field_reference_expression.
10867 // Return an expression for the pointer to the function to call.
10870 Interface_field_reference_expression::get_function()
10872 Expression
* ref
= this->expr_
;
10873 Location loc
= this->location();
10874 if (ref
->type()->points_to() != NULL
)
10875 ref
= Expression::make_unary(OPERATOR_MULT
, ref
, loc
);
10877 Expression
* mtable
=
10878 Expression::make_interface_info(ref
, INTERFACE_INFO_METHODS
, loc
);
10879 Struct_type
* mtable_type
= mtable
->type()->points_to()->struct_type();
10881 std::string name
= Gogo::unpack_hidden_name(this->name_
);
10882 unsigned int index
;
10883 const Struct_field
* field
= mtable_type
->find_local_field(name
, &index
);
10884 go_assert(field
!= NULL
);
10885 mtable
= Expression::make_unary(OPERATOR_MULT
, mtable
, loc
);
10886 return Expression::make_field_reference(mtable
, index
, loc
);
10889 // Return an expression for the first argument to pass to the interface
10893 Interface_field_reference_expression::get_underlying_object()
10895 Expression
* expr
= this->expr_
;
10896 if (expr
->type()->points_to() != NULL
)
10897 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, this->location());
10898 return Expression::make_interface_info(expr
, INTERFACE_INFO_OBJECT
,
10905 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
10907 return Expression::traverse(&this->expr_
, traverse
);
10910 // Lower the expression. If this expression is not called, we need to
10911 // evaluate the expression twice when converting to the backend
10912 // interface. So introduce a temporary variable if necessary.
10915 Interface_field_reference_expression::do_flatten(Gogo
*, Named_object
*,
10916 Statement_inserter
* inserter
)
10918 if (this->expr_
->is_error_expression()
10919 || this->expr_
->type()->is_error_type())
10921 go_assert(saw_errors());
10922 return Expression::make_error(this->location());
10925 if (!this->expr_
->is_variable())
10927 Temporary_statement
* temp
=
10928 Statement::make_temporary(this->expr_
->type(), NULL
, this->location());
10929 inserter
->insert(temp
);
10930 this->expr_
= Expression::make_set_and_use_temporary(temp
, this->expr_
,
10936 // Return the type of an interface field reference.
10939 Interface_field_reference_expression::do_type()
10941 Type
* expr_type
= this->expr_
->type();
10943 Type
* points_to
= expr_type
->points_to();
10944 if (points_to
!= NULL
)
10945 expr_type
= points_to
;
10947 Interface_type
* interface_type
= expr_type
->interface_type();
10948 if (interface_type
== NULL
)
10949 return Type::make_error_type();
10951 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
10952 if (method
== NULL
)
10953 return Type::make_error_type();
10955 return method
->type();
10958 // Determine types.
10961 Interface_field_reference_expression::do_determine_type(const Type_context
*)
10963 this->expr_
->determine_type_no_context();
10966 // Check the types for an interface field reference.
10969 Interface_field_reference_expression::do_check_types(Gogo
*)
10971 Type
* type
= this->expr_
->type();
10973 Type
* points_to
= type
->points_to();
10974 if (points_to
!= NULL
)
10977 Interface_type
* interface_type
= type
->interface_type();
10978 if (interface_type
== NULL
)
10980 if (!type
->is_error_type())
10981 this->report_error(_("expected interface or pointer to interface"));
10985 const Typed_identifier
* method
=
10986 interface_type
->find_method(this->name_
);
10987 if (method
== NULL
)
10989 error_at(this->location(), "method %qs not in interface",
10990 Gogo::message_name(this->name_
).c_str());
10991 this->set_is_error();
10996 // If an interface field reference is not simply called, then it is
10997 // represented as a closure. The closure will hold a single variable,
10998 // the value of the interface on which the method should be called.
10999 // The function will be a simple thunk that pulls the value from the
11000 // closure and calls the method with the remaining arguments.
11002 // Because method values are not common, we don't build all thunks for
11003 // all possible interface methods, but instead only build them as we
11004 // need them. In particular, we even build them on demand for
11005 // interface methods defined in other packages.
11007 Interface_field_reference_expression::Interface_method_thunks
11008 Interface_field_reference_expression::interface_method_thunks
;
11010 // Find or create the thunk to call method NAME on TYPE.
11013 Interface_field_reference_expression::create_thunk(Gogo
* gogo
,
11014 Interface_type
* type
,
11015 const std::string
& name
)
11017 std::pair
<Interface_type
*, Method_thunks
*> val(type
, NULL
);
11018 std::pair
<Interface_method_thunks::iterator
, bool> ins
=
11019 Interface_field_reference_expression::interface_method_thunks
.insert(val
);
11022 // This is the first time we have seen this interface.
11023 ins
.first
->second
= new Method_thunks();
11026 for (Method_thunks::const_iterator p
= ins
.first
->second
->begin();
11027 p
!= ins
.first
->second
->end();
11029 if (p
->first
== name
)
11032 Location loc
= type
->location();
11034 const Typed_identifier
* method_id
= type
->find_method(name
);
11035 if (method_id
== NULL
)
11036 return Named_object::make_erroneous_name(Gogo::thunk_name());
11038 Function_type
* orig_fntype
= method_id
->type()->function_type();
11039 if (orig_fntype
== NULL
)
11040 return Named_object::make_erroneous_name(Gogo::thunk_name());
11042 Struct_field_list
* sfl
= new Struct_field_list();
11043 // The type here is wrong--it should be the C function type. But it
11044 // doesn't really matter.
11045 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
11046 sfl
->push_back(Struct_field(Typed_identifier("fn.0", vt
, loc
)));
11047 sfl
->push_back(Struct_field(Typed_identifier("val.1", type
, loc
)));
11048 Type
* closure_type
= Type::make_struct_type(sfl
, loc
);
11049 closure_type
= Type::make_pointer_type(closure_type
);
11051 Function_type
* new_fntype
= orig_fntype
->copy_with_names();
11053 std::string thunk_name
= Gogo::thunk_name();
11054 Named_object
* new_no
= gogo
->start_function(thunk_name
, new_fntype
,
11057 Variable
* cvar
= new Variable(closure_type
, NULL
, false, false, false, loc
);
11058 cvar
->set_is_used();
11059 cvar
->set_is_closure();
11060 Named_object
* cp
= Named_object::make_variable("$closure" + thunk_name
,
11062 new_no
->func_value()->set_closure_var(cp
);
11064 gogo
->start_block(loc
);
11066 // Field 0 of the closure is the function code pointer, field 1 is
11067 // the value on which to invoke the method.
11068 Expression
* arg
= Expression::make_var_reference(cp
, loc
);
11069 arg
= Expression::make_unary(OPERATOR_MULT
, arg
, loc
);
11070 arg
= Expression::make_field_reference(arg
, 1, loc
);
11072 Expression
*ifre
= Expression::make_interface_field_reference(arg
, name
,
11075 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
11076 Expression_list
* args
;
11077 if (orig_params
== NULL
|| orig_params
->empty())
11081 const Typed_identifier_list
* new_params
= new_fntype
->parameters();
11082 args
= new Expression_list();
11083 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
11084 p
!= new_params
->end();
11087 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
11088 go_assert(p_no
!= NULL
11089 && p_no
->is_variable()
11090 && p_no
->var_value()->is_parameter());
11091 args
->push_back(Expression::make_var_reference(p_no
, loc
));
11095 Call_expression
* call
= Expression::make_call(ifre
, args
,
11096 orig_fntype
->is_varargs(),
11098 call
->set_varargs_are_lowered();
11100 Statement
* s
= Statement::make_return_from_call(call
, loc
);
11101 gogo
->add_statement(s
);
11102 Block
* b
= gogo
->finish_block(loc
);
11103 gogo
->add_block(b
, loc
);
11104 gogo
->lower_block(new_no
, b
);
11105 gogo
->flatten_block(new_no
, b
);
11106 gogo
->finish_function(loc
);
11108 ins
.first
->second
->push_back(std::make_pair(name
, new_no
));
11112 // Get the backend representation for a method value.
11115 Interface_field_reference_expression::do_get_backend(Translate_context
* context
)
11117 Interface_type
* type
= this->expr_
->type()->interface_type();
11120 go_assert(saw_errors());
11121 return context
->backend()->error_expression();
11124 Named_object
* thunk
=
11125 Interface_field_reference_expression::create_thunk(context
->gogo(),
11126 type
, this->name_
);
11127 if (thunk
->is_erroneous())
11129 go_assert(saw_errors());
11130 return context
->backend()->error_expression();
11133 // FIXME: We should lower this earlier, but we can't it lower it in
11134 // the lowering pass because at that point we don't know whether we
11135 // need to create the thunk or not. If the expression is called, we
11136 // don't need the thunk.
11138 Location loc
= this->location();
11140 Struct_field_list
* fields
= new Struct_field_list();
11141 fields
->push_back(Struct_field(Typed_identifier("fn.0",
11142 thunk
->func_value()->type(),
11144 fields
->push_back(Struct_field(Typed_identifier("val.1",
11145 this->expr_
->type(),
11147 Struct_type
* st
= Type::make_struct_type(fields
, loc
);
11149 Expression_list
* vals
= new Expression_list();
11150 vals
->push_back(Expression::make_func_code_reference(thunk
, loc
));
11151 vals
->push_back(this->expr_
);
11153 Expression
* expr
= Expression::make_struct_composite_literal(st
, vals
, loc
);
11154 Bexpression
* bclosure
=
11155 Expression::make_heap_expression(expr
, loc
)->get_backend(context
);
11157 Expression
* nil_check
=
11158 Expression::make_binary(OPERATOR_EQEQ
, this->expr_
,
11159 Expression::make_nil(loc
), loc
);
11160 Bexpression
* bnil_check
= nil_check
->get_backend(context
);
11162 Gogo
* gogo
= context
->gogo();
11163 Bexpression
* bcrash
= gogo
->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
11164 loc
)->get_backend(context
);
11166 Bexpression
* bcond
=
11167 gogo
->backend()->conditional_expression(NULL
, bnil_check
, bcrash
, NULL
, loc
);
11168 Bstatement
* cond_statement
= gogo
->backend()->expression_statement(bcond
);
11169 return gogo
->backend()->compound_expression(cond_statement
, bclosure
, loc
);
11172 // Dump ast representation for an interface field reference.
11175 Interface_field_reference_expression::do_dump_expression(
11176 Ast_dump_context
* ast_dump_context
) const
11178 this->expr_
->dump_expression(ast_dump_context
);
11179 ast_dump_context
->ostream() << "." << this->name_
;
11182 // Make a reference to a field in an interface.
11185 Expression::make_interface_field_reference(Expression
* expr
,
11186 const std::string
& field
,
11189 return new Interface_field_reference_expression(expr
, field
, location
);
11192 // A general selector. This is a Parser_expression for LEFT.NAME. It
11193 // is lowered after we know the type of the left hand side.
11195 class Selector_expression
: public Parser_expression
11198 Selector_expression(Expression
* left
, const std::string
& name
,
11200 : Parser_expression(EXPRESSION_SELECTOR
, location
),
11201 left_(left
), name_(name
)
11206 do_traverse(Traverse
* traverse
)
11207 { return Expression::traverse(&this->left_
, traverse
); }
11210 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
11215 return new Selector_expression(this->left_
->copy(), this->name_
,
11220 do_dump_expression(Ast_dump_context
* ast_dump_context
) const;
11224 lower_method_expression(Gogo
*);
11226 // The expression on the left hand side.
11228 // The name on the right hand side.
11232 // Lower a selector expression once we know the real type of the left
11236 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, Statement_inserter
*,
11239 Expression
* left
= this->left_
;
11240 if (left
->is_type_expression())
11241 return this->lower_method_expression(gogo
);
11242 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
11246 // Lower a method expression T.M or (*T).M. We turn this into a
11247 // function literal.
11250 Selector_expression::lower_method_expression(Gogo
* gogo
)
11252 Location location
= this->location();
11253 Type
* type
= this->left_
->type();
11254 const std::string
& name(this->name_
);
11257 if (type
->points_to() == NULL
)
11258 is_pointer
= false;
11262 type
= type
->points_to();
11264 Named_type
* nt
= type
->named_type();
11268 ("method expression requires named type or "
11269 "pointer to named type"));
11270 return Expression::make_error(location
);
11274 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
11275 const Typed_identifier
* imethod
= NULL
;
11276 if (method
== NULL
&& !is_pointer
)
11278 Interface_type
* it
= nt
->interface_type();
11280 imethod
= it
->find_method(name
);
11283 if (method
== NULL
&& imethod
== NULL
)
11286 error_at(location
, "type %<%s%s%> has no method %<%s%>",
11287 is_pointer
? "*" : "",
11288 nt
->message_name().c_str(),
11289 Gogo::message_name(name
).c_str());
11291 error_at(location
, "method %<%s%s%> is ambiguous in type %<%s%>",
11292 Gogo::message_name(name
).c_str(),
11293 is_pointer
? "*" : "",
11294 nt
->message_name().c_str());
11295 return Expression::make_error(location
);
11298 if (method
!= NULL
&& !is_pointer
&& !method
->is_value_method())
11300 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
11301 nt
->message_name().c_str(),
11302 Gogo::message_name(name
).c_str());
11303 return Expression::make_error(location
);
11306 // Build a new function type in which the receiver becomes the first
11308 Function_type
* method_type
;
11309 if (method
!= NULL
)
11311 method_type
= method
->type();
11312 go_assert(method_type
->is_method());
11316 method_type
= imethod
->type()->function_type();
11317 go_assert(method_type
!= NULL
&& !method_type
->is_method());
11320 const char* const receiver_name
= "$this";
11321 Typed_identifier_list
* parameters
= new Typed_identifier_list();
11322 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
11325 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
11326 if (method_parameters
!= NULL
)
11329 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
11330 p
!= method_parameters
->end();
11333 if (!p
->name().empty())
11334 parameters
->push_back(*p
);
11338 snprintf(buf
, sizeof buf
, "$param%d", i
);
11339 parameters
->push_back(Typed_identifier(buf
, p
->type(),
11345 const Typed_identifier_list
* method_results
= method_type
->results();
11346 Typed_identifier_list
* results
;
11347 if (method_results
== NULL
)
11351 results
= new Typed_identifier_list();
11352 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
11353 p
!= method_results
->end();
11355 results
->push_back(*p
);
11358 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
11360 if (method_type
->is_varargs())
11361 fntype
->set_is_varargs();
11363 // We generate methods which always takes a pointer to the receiver
11364 // as their first argument. If this is for a pointer type, we can
11365 // simply reuse the existing function. We use an internal hack to
11366 // get the right type.
11367 // FIXME: This optimization is disabled because it doesn't yet work
11368 // with function descriptors when the method expression is not
11369 // directly called.
11370 if (method
!= NULL
&& is_pointer
&& false)
11372 Named_object
* mno
= (method
->needs_stub_method()
11373 ? method
->stub_object()
11374 : method
->named_object());
11375 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
11376 f
= Expression::make_cast(fntype
, f
, location
);
11377 Type_conversion_expression
* tce
=
11378 static_cast<Type_conversion_expression
*>(f
);
11379 tce
->set_may_convert_function_types();
11383 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
11386 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
11387 go_assert(vno
!= NULL
);
11388 Expression
* ve
= Expression::make_var_reference(vno
, location
);
11390 if (method
!= NULL
)
11391 bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
11393 bm
= Expression::make_interface_field_reference(ve
, name
, location
);
11395 // Even though we found the method above, if it has an error type we
11396 // may see an error here.
11397 if (bm
->is_error_expression())
11399 gogo
->finish_function(location
);
11403 Expression_list
* args
;
11404 if (parameters
->size() <= 1)
11408 args
= new Expression_list();
11409 Typed_identifier_list::const_iterator p
= parameters
->begin();
11411 for (; p
!= parameters
->end(); ++p
)
11413 vno
= gogo
->lookup(p
->name(), NULL
);
11414 go_assert(vno
!= NULL
);
11415 args
->push_back(Expression::make_var_reference(vno
, location
));
11419 gogo
->start_block(location
);
11421 Call_expression
* call
= Expression::make_call(bm
, args
,
11422 method_type
->is_varargs(),
11425 Statement
* s
= Statement::make_return_from_call(call
, location
);
11426 gogo
->add_statement(s
);
11428 Block
* b
= gogo
->finish_block(location
);
11430 gogo
->add_block(b
, location
);
11432 // Lower the call in case there are multiple results.
11433 gogo
->lower_block(no
, b
);
11434 gogo
->flatten_block(no
, b
);
11436 gogo
->finish_function(location
);
11438 return Expression::make_func_reference(no
, NULL
, location
);
11441 // Dump the ast for a selector expression.
11444 Selector_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
11447 ast_dump_context
->dump_expression(this->left_
);
11448 ast_dump_context
->ostream() << ".";
11449 ast_dump_context
->ostream() << this->name_
;
11452 // Make a selector expression.
11455 Expression::make_selector(Expression
* left
, const std::string
& name
,
11458 return new Selector_expression(left
, name
, location
);
11461 // Class Allocation_expression.
11464 Allocation_expression::do_traverse(Traverse
* traverse
)
11466 return Type::traverse(this->type_
, traverse
);
11470 Allocation_expression::do_type()
11472 return Type::make_pointer_type(this->type_
);
11475 // Make a copy of an allocation expression.
11478 Allocation_expression::do_copy()
11480 Allocation_expression
* alloc
=
11481 new Allocation_expression(this->type_
, this->location());
11482 if (this->allocate_on_stack_
)
11483 alloc
->set_allocate_on_stack();
11487 // Return the backend representation for an allocation expression.
11490 Allocation_expression::do_get_backend(Translate_context
* context
)
11492 Gogo
* gogo
= context
->gogo();
11493 Location loc
= this->location();
11495 Btype
* btype
= this->type_
->get_backend(gogo
);
11496 if (this->allocate_on_stack_
)
11498 int64_t size
= gogo
->backend()->type_size(btype
);
11499 return gogo
->backend()->stack_allocation_expression(size
, loc
);
11502 Bexpression
* space
=
11503 gogo
->allocate_memory(this->type_
, loc
)->get_backend(context
);
11504 Btype
* pbtype
= gogo
->backend()->pointer_type(btype
);
11505 return gogo
->backend()->convert_expression(pbtype
, space
, loc
);
11508 // Dump ast representation for an allocation expression.
11511 Allocation_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
11514 ast_dump_context
->ostream() << "new(";
11515 ast_dump_context
->dump_type(this->type_
);
11516 ast_dump_context
->ostream() << ")";
11519 // Make an allocation expression.
11522 Expression::make_allocation(Type
* type
, Location location
)
11524 return new Allocation_expression(type
, location
);
11527 // Class Struct_construction_expression.
11532 Struct_construction_expression::do_traverse(Traverse
* traverse
)
11534 if (this->vals_
!= NULL
)
11536 if (this->traverse_order_
== NULL
)
11538 if (this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11539 return TRAVERSE_EXIT
;
11543 for (std::vector
<int>::const_iterator p
=
11544 this->traverse_order_
->begin();
11545 p
!= this->traverse_order_
->end();
11548 if (Expression::traverse(&this->vals_
->at(*p
), traverse
)
11550 return TRAVERSE_EXIT
;
11554 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11555 return TRAVERSE_EXIT
;
11556 return TRAVERSE_CONTINUE
;
11559 // Return whether this is a constant initializer.
11562 Struct_construction_expression::is_constant_struct() const
11564 if (this->vals_
== NULL
)
11566 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11567 pv
!= this->vals_
->end();
11571 && !(*pv
)->is_constant()
11572 && (!(*pv
)->is_composite_literal()
11573 || (*pv
)->is_nonconstant_composite_literal()))
11577 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11578 for (Struct_field_list::const_iterator pf
= fields
->begin();
11579 pf
!= fields
->end();
11582 // There are no constant constructors for interfaces.
11583 if (pf
->type()->interface_type() != NULL
)
11590 // Return whether this struct is immutable.
11593 Struct_construction_expression::do_is_immutable() const
11595 if (this->vals_
== NULL
)
11597 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11598 pv
!= this->vals_
->end();
11601 if (*pv
!= NULL
&& !(*pv
)->is_immutable())
11607 // Final type determination.
11610 Struct_construction_expression::do_determine_type(const Type_context
*)
11612 if (this->vals_
== NULL
)
11614 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11615 Expression_list::const_iterator pv
= this->vals_
->begin();
11616 for (Struct_field_list::const_iterator pf
= fields
->begin();
11617 pf
!= fields
->end();
11620 if (pv
== this->vals_
->end())
11624 Type_context
subcontext(pf
->type(), false);
11625 (*pv
)->determine_type(&subcontext
);
11628 // Extra values are an error we will report elsewhere; we still want
11629 // to determine the type to avoid knockon errors.
11630 for (; pv
!= this->vals_
->end(); ++pv
)
11631 (*pv
)->determine_type_no_context();
11637 Struct_construction_expression::do_check_types(Gogo
*)
11639 if (this->vals_
== NULL
)
11642 Struct_type
* st
= this->type_
->struct_type();
11643 if (this->vals_
->size() > st
->field_count())
11645 this->report_error(_("too many expressions for struct"));
11649 const Struct_field_list
* fields
= st
->fields();
11650 Expression_list::const_iterator pv
= this->vals_
->begin();
11652 for (Struct_field_list::const_iterator pf
= fields
->begin();
11653 pf
!= fields
->end();
11656 if (pv
== this->vals_
->end())
11658 this->report_error(_("too few expressions for struct"));
11665 std::string reason
;
11666 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
11668 if (reason
.empty())
11669 error_at((*pv
)->location(),
11670 "incompatible type for field %d in struct construction",
11673 error_at((*pv
)->location(),
11674 ("incompatible type for field %d in "
11675 "struct construction (%s)"),
11676 i
+ 1, reason
.c_str());
11677 this->set_is_error();
11680 go_assert(pv
== this->vals_
->end());
11683 // Flatten a struct construction expression. Store the values into
11684 // temporaries in case they need interface conversion.
11687 Struct_construction_expression::do_flatten(Gogo
*, Named_object
*,
11688 Statement_inserter
* inserter
)
11690 if (this->vals_
== NULL
)
11693 // If this is a constant struct, we don't need temporaries.
11694 if (this->is_constant_struct())
11697 Location loc
= this->location();
11698 for (Expression_list::iterator pv
= this->vals_
->begin();
11699 pv
!= this->vals_
->end();
11704 if ((*pv
)->is_error_expression() || (*pv
)->type()->is_error_type())
11706 go_assert(saw_errors());
11707 return Expression::make_error(loc
);
11709 if (!(*pv
)->is_variable())
11711 Temporary_statement
* temp
=
11712 Statement::make_temporary(NULL
, *pv
, loc
);
11713 inserter
->insert(temp
);
11714 *pv
= Expression::make_temporary_reference(temp
, loc
);
11721 // Return the backend representation for constructing a struct.
11724 Struct_construction_expression::do_get_backend(Translate_context
* context
)
11726 Gogo
* gogo
= context
->gogo();
11728 Btype
* btype
= this->type_
->get_backend(gogo
);
11729 if (this->vals_
== NULL
)
11730 return gogo
->backend()->zero_expression(btype
);
11732 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11733 Expression_list::const_iterator pv
= this->vals_
->begin();
11734 std::vector
<Bexpression
*> init
;
11735 for (Struct_field_list::const_iterator pf
= fields
->begin();
11736 pf
!= fields
->end();
11739 Btype
* fbtype
= pf
->type()->get_backend(gogo
);
11740 if (pv
== this->vals_
->end())
11741 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
11742 else if (*pv
== NULL
)
11744 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
11750 Expression::convert_for_assignment(gogo
, pf
->type(),
11751 *pv
, this->location());
11752 init
.push_back(val
->get_backend(context
));
11756 return gogo
->backend()->constructor_expression(btype
, init
, this->location());
11759 // Export a struct construction.
11762 Struct_construction_expression::do_export(Export
* exp
) const
11764 exp
->write_c_string("convert(");
11765 exp
->write_type(this->type_
);
11766 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11767 pv
!= this->vals_
->end();
11770 exp
->write_c_string(", ");
11772 (*pv
)->export_expression(exp
);
11774 exp
->write_c_string(")");
11777 // Dump ast representation of a struct construction expression.
11780 Struct_construction_expression::do_dump_expression(
11781 Ast_dump_context
* ast_dump_context
) const
11783 ast_dump_context
->dump_type(this->type_
);
11784 ast_dump_context
->ostream() << "{";
11785 ast_dump_context
->dump_expression_list(this->vals_
);
11786 ast_dump_context
->ostream() << "}";
11789 // Make a struct composite literal. This used by the thunk code.
11792 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
11795 go_assert(type
->struct_type() != NULL
);
11796 return new Struct_construction_expression(type
, vals
, location
);
11799 // Class Array_construction_expression.
11804 Array_construction_expression::do_traverse(Traverse
* traverse
)
11806 if (this->vals_
!= NULL
11807 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11808 return TRAVERSE_EXIT
;
11809 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11810 return TRAVERSE_EXIT
;
11811 return TRAVERSE_CONTINUE
;
11814 // Return whether this is a constant initializer.
11817 Array_construction_expression::is_constant_array() const
11819 if (this->vals_
== NULL
)
11822 // There are no constant constructors for interfaces.
11823 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
11826 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11827 pv
!= this->vals_
->end();
11831 && !(*pv
)->is_constant()
11832 && (!(*pv
)->is_composite_literal()
11833 || (*pv
)->is_nonconstant_composite_literal()))
11839 // Return whether this is an immutable array initializer.
11842 Array_construction_expression::do_is_immutable() const
11844 if (this->vals_
== NULL
)
11846 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11847 pv
!= this->vals_
->end();
11850 if (*pv
!= NULL
&& !(*pv
)->is_immutable())
11856 // Final type determination.
11859 Array_construction_expression::do_determine_type(const Type_context
*)
11861 if (this->vals_
== NULL
)
11863 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
11864 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11865 pv
!= this->vals_
->end();
11869 (*pv
)->determine_type(&subcontext
);
11876 Array_construction_expression::do_check_types(Gogo
*)
11878 if (this->vals_
== NULL
)
11881 Array_type
* at
= this->type_
->array_type();
11883 Type
* element_type
= at
->element_type();
11884 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11885 pv
!= this->vals_
->end();
11889 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
11891 error_at((*pv
)->location(),
11892 "incompatible type for element %d in composite literal",
11894 this->set_is_error();
11899 // Flatten an array construction expression. Store the values into
11900 // temporaries in case they need interface conversion.
11903 Array_construction_expression::do_flatten(Gogo
*, Named_object
*,
11904 Statement_inserter
* inserter
)
11906 if (this->vals_
== NULL
)
11909 // If this is a constant array, we don't need temporaries.
11910 if (this->is_constant_array())
11913 Location loc
= this->location();
11914 for (Expression_list::iterator pv
= this->vals_
->begin();
11915 pv
!= this->vals_
->end();
11920 if ((*pv
)->is_error_expression() || (*pv
)->type()->is_error_type())
11922 go_assert(saw_errors());
11923 return Expression::make_error(loc
);
11925 if (!(*pv
)->is_variable())
11927 Temporary_statement
* temp
=
11928 Statement::make_temporary(NULL
, *pv
, loc
);
11929 inserter
->insert(temp
);
11930 *pv
= Expression::make_temporary_reference(temp
, loc
);
11937 // Get a constructor expression for the array values.
11940 Array_construction_expression::get_constructor(Translate_context
* context
,
11941 Btype
* array_btype
)
11943 Type
* element_type
= this->type_
->array_type()->element_type();
11945 std::vector
<unsigned long> indexes
;
11946 std::vector
<Bexpression
*> vals
;
11947 Gogo
* gogo
= context
->gogo();
11948 if (this->vals_
!= NULL
)
11951 std::vector
<unsigned long>::const_iterator pi
;
11952 if (this->indexes_
!= NULL
)
11953 pi
= this->indexes_
->begin();
11954 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11955 pv
!= this->vals_
->end();
11958 if (this->indexes_
!= NULL
)
11959 go_assert(pi
!= this->indexes_
->end());
11961 if (this->indexes_
== NULL
)
11962 indexes
.push_back(i
);
11964 indexes
.push_back(*pi
);
11967 Btype
* ebtype
= element_type
->get_backend(gogo
);
11968 Bexpression
*zv
= gogo
->backend()->zero_expression(ebtype
);
11969 vals
.push_back(zv
);
11973 Expression
* val_expr
=
11974 Expression::convert_for_assignment(gogo
, element_type
, *pv
,
11976 vals
.push_back(val_expr
->get_backend(context
));
11978 if (this->indexes_
!= NULL
)
11981 if (this->indexes_
!= NULL
)
11982 go_assert(pi
== this->indexes_
->end());
11984 return gogo
->backend()->array_constructor_expression(array_btype
, indexes
,
11985 vals
, this->location());
11988 // Export an array construction.
11991 Array_construction_expression::do_export(Export
* exp
) const
11993 exp
->write_c_string("convert(");
11994 exp
->write_type(this->type_
);
11995 if (this->vals_
!= NULL
)
11997 std::vector
<unsigned long>::const_iterator pi
;
11998 if (this->indexes_
!= NULL
)
11999 pi
= this->indexes_
->begin();
12000 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12001 pv
!= this->vals_
->end();
12004 exp
->write_c_string(", ");
12006 if (this->indexes_
!= NULL
)
12009 snprintf(buf
, sizeof buf
, "%lu", *pi
);
12010 exp
->write_c_string(buf
);
12011 exp
->write_c_string(":");
12015 (*pv
)->export_expression(exp
);
12017 if (this->indexes_
!= NULL
)
12021 exp
->write_c_string(")");
12024 // Dump ast representation of an array construction expressin.
12027 Array_construction_expression::do_dump_expression(
12028 Ast_dump_context
* ast_dump_context
) const
12030 Expression
* length
= this->type_
->array_type()->length();
12032 ast_dump_context
->ostream() << "[" ;
12033 if (length
!= NULL
)
12035 ast_dump_context
->dump_expression(length
);
12037 ast_dump_context
->ostream() << "]" ;
12038 ast_dump_context
->dump_type(this->type_
);
12039 ast_dump_context
->ostream() << "{" ;
12040 if (this->indexes_
== NULL
)
12041 ast_dump_context
->dump_expression_list(this->vals_
);
12044 Expression_list::const_iterator pv
= this->vals_
->begin();
12045 for (std::vector
<unsigned long>::const_iterator pi
=
12046 this->indexes_
->begin();
12047 pi
!= this->indexes_
->end();
12050 if (pi
!= this->indexes_
->begin())
12051 ast_dump_context
->ostream() << ", ";
12052 ast_dump_context
->ostream() << *pi
<< ':';
12053 ast_dump_context
->dump_expression(*pv
);
12056 ast_dump_context
->ostream() << "}" ;
12060 // Class Fixed_array_construction_expression.
12062 Fixed_array_construction_expression::Fixed_array_construction_expression(
12063 Type
* type
, const std::vector
<unsigned long>* indexes
,
12064 Expression_list
* vals
, Location location
)
12065 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
12066 type
, indexes
, vals
, location
)
12067 { go_assert(type
->array_type() != NULL
&& !type
->is_slice_type()); }
12069 // Return the backend representation for constructing a fixed array.
12072 Fixed_array_construction_expression::do_get_backend(Translate_context
* context
)
12074 Type
* type
= this->type();
12075 Btype
* btype
= type
->get_backend(context
->gogo());
12076 return this->get_constructor(context
, btype
);
12080 Expression::make_array_composite_literal(Type
* type
, Expression_list
* vals
,
12083 go_assert(type
->array_type() != NULL
&& !type
->is_slice_type());
12084 return new Fixed_array_construction_expression(type
, NULL
, vals
, location
);
12087 // Class Slice_construction_expression.
12089 Slice_construction_expression::Slice_construction_expression(
12090 Type
* type
, const std::vector
<unsigned long>* indexes
,
12091 Expression_list
* vals
, Location location
)
12092 : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION
,
12093 type
, indexes
, vals
, location
),
12096 go_assert(type
->is_slice_type());
12098 unsigned long lenval
;
12099 Expression
* length
;
12100 if (vals
== NULL
|| vals
->empty())
12104 if (this->indexes() == NULL
)
12105 lenval
= vals
->size();
12107 lenval
= indexes
->back() + 1;
12109 Type
* int_type
= Type::lookup_integer_type("int");
12110 length
= Expression::make_integer_ul(lenval
, int_type
, location
);
12111 Type
* element_type
= type
->array_type()->element_type();
12112 this->valtype_
= Type::make_array_type(element_type
, length
);
12119 Slice_construction_expression::do_traverse(Traverse
* traverse
)
12121 if (this->Array_construction_expression::do_traverse(traverse
)
12123 return TRAVERSE_EXIT
;
12124 if (Type::traverse(this->valtype_
, traverse
) == TRAVERSE_EXIT
)
12125 return TRAVERSE_EXIT
;
12126 return TRAVERSE_CONTINUE
;
12129 // Return the backend representation for constructing a slice.
12132 Slice_construction_expression::do_get_backend(Translate_context
* context
)
12134 Array_type
* array_type
= this->type()->array_type();
12135 if (array_type
== NULL
)
12137 go_assert(this->type()->is_error());
12138 return context
->backend()->error_expression();
12141 Location loc
= this->location();
12142 Type
* element_type
= array_type
->element_type();
12143 go_assert(this->valtype_
!= NULL
);
12145 Expression_list
* vals
= this->vals();
12146 if (this->vals() == NULL
|| this->vals()->empty())
12148 // We need to create a unique value for the empty array literal.
12149 vals
= new Expression_list
;
12150 vals
->push_back(NULL
);
12152 Expression
* array_val
=
12153 new Fixed_array_construction_expression(this->valtype_
, this->indexes(),
12156 bool is_constant_initializer
= array_val
->is_immutable();
12158 // We have to copy the initial values into heap memory if we are in
12159 // a function or if the values are not constants. We also have to
12160 // copy them if they may contain pointers in a non-constant context,
12161 // as otherwise the garbage collector won't see them.
12162 bool copy_to_heap
= (context
->function() != NULL
12163 || !is_constant_initializer
12164 || (element_type
->has_pointer()
12165 && !context
->is_const()));
12170 // The initializer will only run once.
12171 space
= Expression::make_unary(OPERATOR_AND
, array_val
, loc
);
12172 space
->unary_expression()->set_is_slice_init();
12175 space
= Expression::make_heap_expression(array_val
, loc
);
12177 // Build a constructor for the slice.
12179 Expression
* len
= this->valtype_
->array_type()->length();
12180 Expression
* slice_val
=
12181 Expression::make_slice_value(this->type(), space
, len
, len
, loc
);
12182 return slice_val
->get_backend(context
);
12185 // Make a slice composite literal. This is used by the type
12186 // descriptor code.
12189 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
12192 go_assert(type
->is_slice_type());
12193 return new Slice_construction_expression(type
, NULL
, vals
, location
);
12196 // Class Map_construction_expression.
12201 Map_construction_expression::do_traverse(Traverse
* traverse
)
12203 if (this->vals_
!= NULL
12204 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
12205 return TRAVERSE_EXIT
;
12206 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12207 return TRAVERSE_EXIT
;
12208 return TRAVERSE_CONTINUE
;
12211 // Flatten constructor initializer into a temporary variable since
12212 // we need to take its address for __go_construct_map.
12215 Map_construction_expression::do_flatten(Gogo
* gogo
, Named_object
*,
12216 Statement_inserter
* inserter
)
12218 if (!this->is_error_expression()
12219 && this->vals_
!= NULL
12220 && !this->vals_
->empty()
12221 && this->constructor_temp_
== NULL
)
12223 Map_type
* mt
= this->type_
->map_type();
12224 Type
* key_type
= mt
->key_type();
12225 Type
* val_type
= mt
->val_type();
12226 this->element_type_
= Type::make_builtin_struct_type(2,
12228 "__val", val_type
);
12230 Expression_list
* value_pairs
= new Expression_list();
12231 Location loc
= this->location();
12234 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12235 pv
!= this->vals_
->end();
12238 Expression_list
* key_value_pair
= new Expression_list();
12239 Expression
* key
= *pv
;
12240 if (key
->is_error_expression() || key
->type()->is_error_type())
12242 go_assert(saw_errors());
12243 return Expression::make_error(loc
);
12245 if (key
->type()->interface_type() != NULL
&& !key
->is_variable())
12247 Temporary_statement
* temp
=
12248 Statement::make_temporary(NULL
, key
, loc
);
12249 inserter
->insert(temp
);
12250 key
= Expression::make_temporary_reference(temp
, loc
);
12252 key
= Expression::convert_for_assignment(gogo
, key_type
, key
, loc
);
12255 Expression
* val
= *pv
;
12256 if (val
->is_error_expression() || val
->type()->is_error_type())
12258 go_assert(saw_errors());
12259 return Expression::make_error(loc
);
12261 if (val
->type()->interface_type() != NULL
&& !val
->is_variable())
12263 Temporary_statement
* temp
=
12264 Statement::make_temporary(NULL
, val
, loc
);
12265 inserter
->insert(temp
);
12266 val
= Expression::make_temporary_reference(temp
, loc
);
12268 val
= Expression::convert_for_assignment(gogo
, val_type
, val
, loc
);
12270 key_value_pair
->push_back(key
);
12271 key_value_pair
->push_back(val
);
12272 value_pairs
->push_back(
12273 Expression::make_struct_composite_literal(this->element_type_
,
12274 key_value_pair
, loc
));
12277 Expression
* element_count
= Expression::make_integer_ul(i
, NULL
, loc
);
12279 Type::make_array_type(this->element_type_
, element_count
);
12280 Expression
* constructor
=
12281 new Fixed_array_construction_expression(ctor_type
, NULL
,
12284 this->constructor_temp_
=
12285 Statement::make_temporary(NULL
, constructor
, loc
);
12286 constructor
->issue_nil_check();
12287 this->constructor_temp_
->set_is_address_taken();
12288 inserter
->insert(this->constructor_temp_
);
12294 // Final type determination.
12297 Map_construction_expression::do_determine_type(const Type_context
*)
12299 if (this->vals_
== NULL
)
12302 Map_type
* mt
= this->type_
->map_type();
12303 Type_context
key_context(mt
->key_type(), false);
12304 Type_context
val_context(mt
->val_type(), false);
12305 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12306 pv
!= this->vals_
->end();
12309 (*pv
)->determine_type(&key_context
);
12311 (*pv
)->determine_type(&val_context
);
12318 Map_construction_expression::do_check_types(Gogo
*)
12320 if (this->vals_
== NULL
)
12323 Map_type
* mt
= this->type_
->map_type();
12325 Type
* key_type
= mt
->key_type();
12326 Type
* val_type
= mt
->val_type();
12327 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12328 pv
!= this->vals_
->end();
12331 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
12333 error_at((*pv
)->location(),
12334 "incompatible type for element %d key in map construction",
12336 this->set_is_error();
12339 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
12341 error_at((*pv
)->location(),
12342 ("incompatible type for element %d value "
12343 "in map construction"),
12345 this->set_is_error();
12350 // Return the backend representation for constructing a map.
12353 Map_construction_expression::do_get_backend(Translate_context
* context
)
12355 if (this->is_error_expression())
12356 return context
->backend()->error_expression();
12357 Location loc
= this->location();
12360 Expression
* ventries
;
12361 if (this->vals_
== NULL
|| this->vals_
->empty())
12362 ventries
= Expression::make_nil(loc
);
12365 go_assert(this->constructor_temp_
!= NULL
);
12366 i
= this->vals_
->size() / 2;
12368 Expression
* ctor_ref
=
12369 Expression::make_temporary_reference(this->constructor_temp_
, loc
);
12370 ventries
= Expression::make_unary(OPERATOR_AND
, ctor_ref
, loc
);
12373 Map_type
* mt
= this->type_
->map_type();
12374 if (this->element_type_
== NULL
)
12375 this->element_type_
=
12376 Type::make_builtin_struct_type(2,
12377 "__key", mt
->key_type(),
12378 "__val", mt
->val_type());
12379 Expression
* descriptor
= Expression::make_map_descriptor(mt
, loc
);
12381 Type
* uintptr_t = Type::lookup_integer_type("uintptr");
12382 Expression
* count
= Expression::make_integer_ul(i
, uintptr_t, loc
);
12384 Expression
* entry_size
=
12385 Expression::make_type_info(this->element_type_
, TYPE_INFO_SIZE
);
12387 unsigned int field_index
;
12388 const Struct_field
* valfield
=
12389 this->element_type_
->find_local_field("__val", &field_index
);
12390 Expression
* val_offset
=
12391 Expression::make_struct_field_offset(this->element_type_
, valfield
);
12392 Expression
* val_size
=
12393 Expression::make_type_info(mt
->val_type(), TYPE_INFO_SIZE
);
12395 Expression
* map_ctor
=
12396 Runtime::make_call(Runtime::CONSTRUCT_MAP
, loc
, 6, descriptor
, count
,
12397 entry_size
, val_offset
, val_size
, ventries
);
12398 return map_ctor
->get_backend(context
);
12401 // Export an array construction.
12404 Map_construction_expression::do_export(Export
* exp
) const
12406 exp
->write_c_string("convert(");
12407 exp
->write_type(this->type_
);
12408 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12409 pv
!= this->vals_
->end();
12412 exp
->write_c_string(", ");
12413 (*pv
)->export_expression(exp
);
12415 exp
->write_c_string(")");
12418 // Dump ast representation for a map construction expression.
12421 Map_construction_expression::do_dump_expression(
12422 Ast_dump_context
* ast_dump_context
) const
12424 ast_dump_context
->ostream() << "{" ;
12425 ast_dump_context
->dump_expression_list(this->vals_
, true);
12426 ast_dump_context
->ostream() << "}";
12429 // A general composite literal. This is lowered to a type specific
12432 class Composite_literal_expression
: public Parser_expression
12435 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
12436 Expression_list
* vals
, bool all_are_names
,
12438 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
12439 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
),
12440 all_are_names_(all_are_names
)
12445 do_traverse(Traverse
* traverse
);
12448 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
12453 return new Composite_literal_expression(this->type_
, this->depth_
,
12455 (this->vals_
== NULL
12457 : this->vals_
->copy()),
12458 this->all_are_names_
,
12463 do_dump_expression(Ast_dump_context
*) const;
12467 lower_struct(Gogo
*, Type
*);
12470 lower_array(Type
*);
12473 make_array(Type
*, const std::vector
<unsigned long>*, Expression_list
*);
12476 lower_map(Gogo
*, Named_object
*, Statement_inserter
*, Type
*);
12478 // The type of the composite literal.
12480 // The depth within a list of composite literals within a composite
12481 // literal, when the type is omitted.
12483 // The values to put in the composite literal.
12484 Expression_list
* vals_
;
12485 // If this is true, then VALS_ is a list of pairs: a key and a
12486 // value. In an array initializer, a missing key will be NULL.
12488 // If this is true, then HAS_KEYS_ is true, and every key is a
12489 // simple identifier.
12490 bool all_are_names_
;
12496 Composite_literal_expression::do_traverse(Traverse
* traverse
)
12498 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12499 return TRAVERSE_EXIT
;
12501 // If this is a struct composite literal with keys, then the keys
12502 // are field names, not expressions. We don't want to traverse them
12503 // in that case. If we do, we can give an erroneous error "variable
12504 // initializer refers to itself." See bug482.go in the testsuite.
12505 if (this->has_keys_
&& this->vals_
!= NULL
)
12507 // The type may not be resolvable at this point.
12508 Type
* type
= this->type_
;
12510 for (int depth
= this->depth_
; depth
> 0; --depth
)
12512 if (type
->array_type() != NULL
)
12513 type
= type
->array_type()->element_type();
12514 else if (type
->map_type() != NULL
)
12515 type
= type
->map_type()->val_type();
12518 // This error will be reported during lowering.
12519 return TRAVERSE_CONTINUE
;
12525 if (type
->classification() == Type::TYPE_NAMED
)
12526 type
= type
->named_type()->real_type();
12527 else if (type
->classification() == Type::TYPE_FORWARD
)
12529 Type
* t
= type
->forwarded();
12538 if (type
->classification() == Type::TYPE_STRUCT
)
12540 Expression_list::iterator p
= this->vals_
->begin();
12541 while (p
!= this->vals_
->end())
12545 go_assert(p
!= this->vals_
->end());
12546 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12547 return TRAVERSE_EXIT
;
12550 return TRAVERSE_CONTINUE
;
12554 if (this->vals_
!= NULL
)
12555 return this->vals_
->traverse(traverse
);
12557 return TRAVERSE_CONTINUE
;
12560 // Lower a generic composite literal into a specific version based on
12564 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
12565 Statement_inserter
* inserter
, int)
12567 Type
* type
= this->type_
;
12569 for (int depth
= this->depth_
; depth
> 0; --depth
)
12571 if (type
->array_type() != NULL
)
12572 type
= type
->array_type()->element_type();
12573 else if (type
->map_type() != NULL
)
12574 type
= type
->map_type()->val_type();
12577 if (!type
->is_error())
12578 error_at(this->location(),
12579 ("may only omit types within composite literals "
12580 "of slice, array, or map type"));
12581 return Expression::make_error(this->location());
12585 Type
*pt
= type
->points_to();
12586 bool is_pointer
= false;
12594 if (type
->is_error())
12595 return Expression::make_error(this->location());
12596 else if (type
->struct_type() != NULL
)
12597 ret
= this->lower_struct(gogo
, type
);
12598 else if (type
->array_type() != NULL
)
12599 ret
= this->lower_array(type
);
12600 else if (type
->map_type() != NULL
)
12601 ret
= this->lower_map(gogo
, function
, inserter
, type
);
12604 error_at(this->location(),
12605 ("expected struct, slice, array, or map type "
12606 "for composite literal"));
12607 return Expression::make_error(this->location());
12611 ret
= Expression::make_heap_expression(ret
, this->location());
12616 // Lower a struct composite literal.
12619 Composite_literal_expression::lower_struct(Gogo
* gogo
, Type
* type
)
12621 Location location
= this->location();
12622 Struct_type
* st
= type
->struct_type();
12623 if (this->vals_
== NULL
|| !this->has_keys_
)
12625 if (this->vals_
!= NULL
12626 && !this->vals_
->empty()
12627 && type
->named_type() != NULL
12628 && type
->named_type()->named_object()->package() != NULL
)
12630 for (Struct_field_list::const_iterator pf
= st
->fields()->begin();
12631 pf
!= st
->fields()->end();
12634 if (Gogo::is_hidden_name(pf
->field_name())
12635 || pf
->is_embedded_builtin(gogo
))
12636 error_at(this->location(),
12637 "assignment of unexported field %qs in %qs literal",
12638 Gogo::message_name(pf
->field_name()).c_str(),
12639 type
->named_type()->message_name().c_str());
12643 return new Struct_construction_expression(type
, this->vals_
, location
);
12646 size_t field_count
= st
->field_count();
12647 std::vector
<Expression
*> vals(field_count
);
12648 std::vector
<int>* traverse_order
= new(std::vector
<int>);
12649 Expression_list::const_iterator p
= this->vals_
->begin();
12650 Expression
* external_expr
= NULL
;
12651 const Named_object
* external_no
= NULL
;
12652 while (p
!= this->vals_
->end())
12654 Expression
* name_expr
= *p
;
12657 go_assert(p
!= this->vals_
->end());
12658 Expression
* val
= *p
;
12662 if (name_expr
== NULL
)
12664 error_at(val
->location(), "mixture of field and value initializers");
12665 return Expression::make_error(location
);
12668 bool bad_key
= false;
12670 const Named_object
* no
= NULL
;
12671 switch (name_expr
->classification())
12673 case EXPRESSION_UNKNOWN_REFERENCE
:
12674 name
= name_expr
->unknown_expression()->name();
12675 if (type
->named_type() != NULL
)
12677 // If the named object found for this field name comes from a
12678 // different package than the struct it is a part of, do not count
12679 // this incorrect lookup as a usage of the object's package.
12680 no
= name_expr
->unknown_expression()->named_object();
12681 if (no
->package() != NULL
12682 && no
->package() != type
->named_type()->named_object()->package())
12683 no
->package()->forget_usage(name_expr
);
12687 case EXPRESSION_CONST_REFERENCE
:
12688 no
= static_cast<Const_expression
*>(name_expr
)->named_object();
12691 case EXPRESSION_TYPE
:
12693 Type
* t
= name_expr
->type();
12694 Named_type
* nt
= t
->named_type();
12698 no
= nt
->named_object();
12702 case EXPRESSION_VAR_REFERENCE
:
12703 no
= name_expr
->var_expression()->named_object();
12706 case EXPRESSION_FUNC_REFERENCE
:
12707 no
= name_expr
->func_expression()->named_object();
12710 case EXPRESSION_UNARY
:
12711 // If there is a local variable around with the same name as
12712 // the field, and this occurs in the closure, then the
12713 // parser may turn the field reference into an indirection
12714 // through the closure. FIXME: This is a mess.
12717 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
12718 if (ue
->op() == OPERATOR_MULT
)
12720 Field_reference_expression
* fre
=
12721 ue
->operand()->field_reference_expression();
12725 fre
->expr()->type()->deref()->struct_type();
12728 const Struct_field
* sf
= st
->field(fre
->field_index());
12729 name
= sf
->field_name();
12731 // See below. FIXME.
12732 if (!Gogo::is_hidden_name(name
)
12736 if (gogo
->lookup_global(name
.c_str()) != NULL
)
12737 name
= gogo
->pack_hidden_name(name
, false);
12741 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
12742 size_t buflen
= strlen(buf
);
12743 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
12746 name
= name
.substr(0, name
.length() - buflen
);
12761 error_at(name_expr
->location(), "expected struct field name");
12762 return Expression::make_error(location
);
12767 if (no
->package() != NULL
&& external_expr
== NULL
)
12769 external_expr
= name_expr
;
12775 // A predefined name won't be packed. If it starts with a
12776 // lower case letter we need to check for that case, because
12777 // the field name will be packed. FIXME.
12778 if (!Gogo::is_hidden_name(name
)
12782 Named_object
* gno
= gogo
->lookup_global(name
.c_str());
12784 name
= gogo
->pack_hidden_name(name
, false);
12788 unsigned int index
;
12789 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
12792 error_at(name_expr
->location(), "unknown field %qs in %qs",
12793 Gogo::message_name(name
).c_str(),
12794 (type
->named_type() != NULL
12795 ? type
->named_type()->message_name().c_str()
12796 : "unnamed struct"));
12797 return Expression::make_error(location
);
12799 if (vals
[index
] != NULL
)
12801 error_at(name_expr
->location(),
12802 "duplicate value for field %qs in %qs",
12803 Gogo::message_name(name
).c_str(),
12804 (type
->named_type() != NULL
12805 ? type
->named_type()->message_name().c_str()
12806 : "unnamed struct"));
12807 return Expression::make_error(location
);
12810 if (type
->named_type() != NULL
12811 && type
->named_type()->named_object()->package() != NULL
12812 && (Gogo::is_hidden_name(sf
->field_name())
12813 || sf
->is_embedded_builtin(gogo
)))
12814 error_at(name_expr
->location(),
12815 "assignment of unexported field %qs in %qs literal",
12816 Gogo::message_name(sf
->field_name()).c_str(),
12817 type
->named_type()->message_name().c_str());
12820 traverse_order
->push_back(index
);
12823 if (!this->all_are_names_
)
12825 // This is a weird case like bug462 in the testsuite.
12826 if (external_expr
== NULL
)
12827 error_at(this->location(), "unknown field in %qs literal",
12828 (type
->named_type() != NULL
12829 ? type
->named_type()->message_name().c_str()
12830 : "unnamed struct"));
12832 error_at(external_expr
->location(), "unknown field %qs in %qs",
12833 external_no
->message_name().c_str(),
12834 (type
->named_type() != NULL
12835 ? type
->named_type()->message_name().c_str()
12836 : "unnamed struct"));
12837 return Expression::make_error(location
);
12840 Expression_list
* list
= new Expression_list
;
12841 list
->reserve(field_count
);
12842 for (size_t i
= 0; i
< field_count
; ++i
)
12843 list
->push_back(vals
[i
]);
12845 Struct_construction_expression
* ret
=
12846 new Struct_construction_expression(type
, list
, location
);
12847 ret
->set_traverse_order(traverse_order
);
12851 // Used to sort an index/value array.
12853 class Index_value_compare
12857 operator()(const std::pair
<unsigned long, Expression
*>& a
,
12858 const std::pair
<unsigned long, Expression
*>& b
)
12859 { return a
.first
< b
.first
; }
12862 // Lower an array composite literal.
12865 Composite_literal_expression::lower_array(Type
* type
)
12867 Location location
= this->location();
12868 if (this->vals_
== NULL
|| !this->has_keys_
)
12869 return this->make_array(type
, NULL
, this->vals_
);
12871 std::vector
<unsigned long>* indexes
= new std::vector
<unsigned long>;
12872 indexes
->reserve(this->vals_
->size());
12873 bool indexes_out_of_order
= false;
12874 Expression_list
* vals
= new Expression_list();
12875 vals
->reserve(this->vals_
->size());
12876 unsigned long index
= 0;
12877 Expression_list::const_iterator p
= this->vals_
->begin();
12878 while (p
!= this->vals_
->end())
12880 Expression
* index_expr
= *p
;
12883 go_assert(p
!= this->vals_
->end());
12884 Expression
* val
= *p
;
12888 if (index_expr
== NULL
)
12890 if (!indexes
->empty())
12891 indexes
->push_back(index
);
12895 if (indexes
->empty() && !vals
->empty())
12897 for (size_t i
= 0; i
< vals
->size(); ++i
)
12898 indexes
->push_back(i
);
12901 Numeric_constant nc
;
12902 if (!index_expr
->numeric_constant_value(&nc
))
12904 error_at(index_expr
->location(),
12905 "index expression is not integer constant");
12906 return Expression::make_error(location
);
12909 switch (nc
.to_unsigned_long(&index
))
12911 case Numeric_constant::NC_UL_VALID
:
12913 case Numeric_constant::NC_UL_NOTINT
:
12914 error_at(index_expr
->location(),
12915 "index expression is not integer constant");
12916 return Expression::make_error(location
);
12917 case Numeric_constant::NC_UL_NEGATIVE
:
12918 error_at(index_expr
->location(), "index expression is negative");
12919 return Expression::make_error(location
);
12920 case Numeric_constant::NC_UL_BIG
:
12921 error_at(index_expr
->location(), "index value overflow");
12922 return Expression::make_error(location
);
12927 Named_type
* ntype
= Type::lookup_integer_type("int");
12928 Integer_type
* inttype
= ntype
->integer_type();
12929 if (sizeof(index
) <= static_cast<size_t>(inttype
->bits() * 8)
12930 && index
>> (inttype
->bits() - 1) != 0)
12932 error_at(index_expr
->location(), "index value overflow");
12933 return Expression::make_error(location
);
12936 if (std::find(indexes
->begin(), indexes
->end(), index
)
12939 error_at(index_expr
->location(), "duplicate value for index %lu",
12941 return Expression::make_error(location
);
12944 if (!indexes
->empty() && index
< indexes
->back())
12945 indexes_out_of_order
= true;
12947 indexes
->push_back(index
);
12950 vals
->push_back(val
);
12955 if (indexes
->empty())
12961 if (indexes_out_of_order
)
12963 typedef std::vector
<std::pair
<unsigned long, Expression
*> > V
;
12966 v
.reserve(indexes
->size());
12967 std::vector
<unsigned long>::const_iterator pi
= indexes
->begin();
12968 for (Expression_list::const_iterator pe
= vals
->begin();
12971 v
.push_back(std::make_pair(*pi
, *pe
));
12973 std::sort(v
.begin(), v
.end(), Index_value_compare());
12977 indexes
= new std::vector
<unsigned long>();
12978 indexes
->reserve(v
.size());
12979 vals
= new Expression_list();
12980 vals
->reserve(v
.size());
12982 for (V::const_iterator p
= v
.begin(); p
!= v
.end(); ++p
)
12984 indexes
->push_back(p
->first
);
12985 vals
->push_back(p
->second
);
12989 return this->make_array(type
, indexes
, vals
);
12992 // Actually build the array composite literal. This handles
12996 Composite_literal_expression::make_array(
12998 const std::vector
<unsigned long>* indexes
,
12999 Expression_list
* vals
)
13001 Location location
= this->location();
13002 Array_type
* at
= type
->array_type();
13004 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
13009 else if (indexes
!= NULL
)
13010 size
= indexes
->back() + 1;
13013 size
= vals
->size();
13014 Integer_type
* it
= Type::lookup_integer_type("int")->integer_type();
13015 if (sizeof(size
) <= static_cast<size_t>(it
->bits() * 8)
13016 && size
>> (it
->bits() - 1) != 0)
13018 error_at(location
, "too many elements in composite literal");
13019 return Expression::make_error(location
);
13023 Expression
* elen
= Expression::make_integer_ul(size
, NULL
, location
);
13024 at
= Type::make_array_type(at
->element_type(), elen
);
13027 else if (at
->length() != NULL
13028 && !at
->length()->is_error_expression()
13029 && this->vals_
!= NULL
)
13031 Numeric_constant nc
;
13033 if (at
->length()->numeric_constant_value(&nc
)
13034 && nc
.to_unsigned_long(&val
) == Numeric_constant::NC_UL_VALID
)
13036 if (indexes
== NULL
)
13038 if (this->vals_
->size() > val
)
13040 error_at(location
, "too many elements in composite literal");
13041 return Expression::make_error(location
);
13046 unsigned long max
= indexes
->back();
13050 ("some element keys in composite literal "
13051 "are out of range"));
13052 return Expression::make_error(location
);
13058 if (at
->length() != NULL
)
13059 return new Fixed_array_construction_expression(type
, indexes
, vals
,
13062 return new Slice_construction_expression(type
, indexes
, vals
, location
);
13065 // Lower a map composite literal.
13068 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
13069 Statement_inserter
* inserter
,
13072 Location location
= this->location();
13073 if (this->vals_
!= NULL
)
13075 if (!this->has_keys_
)
13077 error_at(location
, "map composite literal must have keys");
13078 return Expression::make_error(location
);
13081 for (Expression_list::iterator p
= this->vals_
->begin();
13082 p
!= this->vals_
->end();
13088 error_at((*p
)->location(),
13089 "map composite literal must have keys for every value");
13090 return Expression::make_error(location
);
13092 // Make sure we have lowered the key; it may not have been
13093 // lowered in order to handle keys for struct composite
13094 // literals. Lower it now to get the right error message.
13095 if ((*p
)->unknown_expression() != NULL
)
13097 (*p
)->unknown_expression()->clear_is_composite_literal_key();
13098 gogo
->lower_expression(function
, inserter
, &*p
);
13099 go_assert((*p
)->is_error_expression());
13100 return Expression::make_error(location
);
13105 return new Map_construction_expression(type
, this->vals_
, location
);
13108 // Dump ast representation for a composite literal expression.
13111 Composite_literal_expression::do_dump_expression(
13112 Ast_dump_context
* ast_dump_context
) const
13114 ast_dump_context
->ostream() << "composite(";
13115 ast_dump_context
->dump_type(this->type_
);
13116 ast_dump_context
->ostream() << ", {";
13117 ast_dump_context
->dump_expression_list(this->vals_
, this->has_keys_
);
13118 ast_dump_context
->ostream() << "})";
13121 // Make a composite literal expression.
13124 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
13125 Expression_list
* vals
, bool all_are_names
,
13128 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
13129 all_are_names
, location
);
13132 // Return whether this expression is a composite literal.
13135 Expression::is_composite_literal() const
13137 switch (this->classification_
)
13139 case EXPRESSION_COMPOSITE_LITERAL
:
13140 case EXPRESSION_STRUCT_CONSTRUCTION
:
13141 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
13142 case EXPRESSION_SLICE_CONSTRUCTION
:
13143 case EXPRESSION_MAP_CONSTRUCTION
:
13150 // Return whether this expression is a composite literal which is not
13154 Expression::is_nonconstant_composite_literal() const
13156 switch (this->classification_
)
13158 case EXPRESSION_STRUCT_CONSTRUCTION
:
13160 const Struct_construction_expression
*psce
=
13161 static_cast<const Struct_construction_expression
*>(this);
13162 return !psce
->is_constant_struct();
13164 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
13166 const Fixed_array_construction_expression
*pace
=
13167 static_cast<const Fixed_array_construction_expression
*>(this);
13168 return !pace
->is_constant_array();
13170 case EXPRESSION_SLICE_CONSTRUCTION
:
13172 const Slice_construction_expression
*pace
=
13173 static_cast<const Slice_construction_expression
*>(this);
13174 return !pace
->is_constant_array();
13176 case EXPRESSION_MAP_CONSTRUCTION
:
13183 // Return true if this is a variable or temporary_variable.
13186 Expression::is_variable() const
13188 switch (this->classification_
)
13190 case EXPRESSION_VAR_REFERENCE
:
13191 case EXPRESSION_TEMPORARY_REFERENCE
:
13192 case EXPRESSION_SET_AND_USE_TEMPORARY
:
13199 // Return true if this is a reference to a local variable.
13202 Expression::is_local_variable() const
13204 const Var_expression
* ve
= this->var_expression();
13207 const Named_object
* no
= ve
->named_object();
13208 return (no
->is_result_variable()
13209 || (no
->is_variable() && !no
->var_value()->is_global()));
13212 // Class Type_guard_expression.
13217 Type_guard_expression::do_traverse(Traverse
* traverse
)
13219 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
13220 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
13221 return TRAVERSE_EXIT
;
13222 return TRAVERSE_CONTINUE
;
13226 Type_guard_expression::do_flatten(Gogo
*, Named_object
*,
13227 Statement_inserter
* inserter
)
13229 if (this->expr_
->is_error_expression()
13230 || this->expr_
->type()->is_error_type())
13232 go_assert(saw_errors());
13233 return Expression::make_error(this->location());
13236 if (!this->expr_
->is_variable())
13238 Temporary_statement
* temp
= Statement::make_temporary(NULL
, this->expr_
,
13240 inserter
->insert(temp
);
13242 Expression::make_temporary_reference(temp
, this->location());
13247 // Check types of a type guard expression. The expression must have
13248 // an interface type, but the actual type conversion is checked at run
13252 Type_guard_expression::do_check_types(Gogo
*)
13254 Type
* expr_type
= this->expr_
->type();
13255 if (expr_type
->interface_type() == NULL
)
13257 if (!expr_type
->is_error() && !this->type_
->is_error())
13258 this->report_error(_("type assertion only valid for interface types"));
13259 this->set_is_error();
13261 else if (this->type_
->interface_type() == NULL
)
13263 std::string reason
;
13264 if (!expr_type
->interface_type()->implements_interface(this->type_
,
13267 if (!this->type_
->is_error())
13269 if (reason
.empty())
13270 this->report_error(_("impossible type assertion: "
13271 "type does not implement interface"));
13273 error_at(this->location(),
13274 ("impossible type assertion: "
13275 "type does not implement interface (%s)"),
13278 this->set_is_error();
13283 // Return the backend representation for a type guard expression.
13286 Type_guard_expression::do_get_backend(Translate_context
* context
)
13288 Expression
* conversion
;
13289 if (this->type_
->interface_type() != NULL
)
13291 Expression::convert_interface_to_interface(this->type_
, this->expr_
,
13292 true, this->location());
13295 Expression::convert_for_assignment(context
->gogo(), this->type_
,
13296 this->expr_
, this->location());
13298 return conversion
->get_backend(context
);
13301 // Dump ast representation for a type guard expression.
13304 Type_guard_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
13307 this->expr_
->dump_expression(ast_dump_context
);
13308 ast_dump_context
->ostream() << ".";
13309 ast_dump_context
->dump_type(this->type_
);
13312 // Make a type guard expression.
13315 Expression::make_type_guard(Expression
* expr
, Type
* type
,
13318 return new Type_guard_expression(expr
, type
, location
);
13321 // Class Heap_expression.
13323 // Return the type of the expression stored on the heap.
13326 Heap_expression::do_type()
13327 { return Type::make_pointer_type(this->expr_
->type()); }
13329 // Return the backend representation for allocating an expression on the heap.
13332 Heap_expression::do_get_backend(Translate_context
* context
)
13334 if (this->expr_
->is_error_expression() || this->expr_
->type()->is_error())
13335 return context
->backend()->error_expression();
13337 Location loc
= this->location();
13338 Gogo
* gogo
= context
->gogo();
13339 Btype
* btype
= this->type()->get_backend(gogo
);
13340 Bexpression
* space
= Expression::make_allocation(this->expr_
->type(),
13341 loc
)->get_backend(context
);
13344 Named_object
* fn
= context
->function();
13345 go_assert(fn
!= NULL
);
13346 Bfunction
* fndecl
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
13347 Bvariable
* space_temp
=
13348 gogo
->backend()->temporary_variable(fndecl
, context
->bblock(), btype
,
13349 space
, true, loc
, &decl
);
13350 space
= gogo
->backend()->var_expression(space_temp
, loc
);
13351 Btype
* expr_btype
= this->expr_
->type()->get_backend(gogo
);
13353 gogo
->backend()->indirect_expression(expr_btype
, space
, true, loc
);
13355 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
13356 Bstatement
* assn
= gogo
->backend()->assignment_statement(ref
, bexpr
, loc
);
13357 decl
= gogo
->backend()->compound_statement(decl
, assn
);
13358 space
= gogo
->backend()->var_expression(space_temp
, loc
);
13359 return gogo
->backend()->compound_expression(decl
, space
, loc
);
13362 // Dump ast representation for a heap expression.
13365 Heap_expression::do_dump_expression(
13366 Ast_dump_context
* ast_dump_context
) const
13368 ast_dump_context
->ostream() << "&(";
13369 ast_dump_context
->dump_expression(this->expr_
);
13370 ast_dump_context
->ostream() << ")";
13373 // Allocate an expression on the heap.
13376 Expression::make_heap_expression(Expression
* expr
, Location location
)
13378 return new Heap_expression(expr
, location
);
13381 // Class Receive_expression.
13383 // Return the type of a receive expression.
13386 Receive_expression::do_type()
13388 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
13389 if (channel_type
== NULL
)
13390 return Type::make_error_type();
13391 return channel_type
->element_type();
13394 // Check types for a receive expression.
13397 Receive_expression::do_check_types(Gogo
*)
13399 Type
* type
= this->channel_
->type();
13400 if (type
->is_error())
13402 this->set_is_error();
13405 if (type
->channel_type() == NULL
)
13407 this->report_error(_("expected channel"));
13410 if (!type
->channel_type()->may_receive())
13412 this->report_error(_("invalid receive on send-only channel"));
13417 // Flattening for receive expressions creates a temporary variable to store
13418 // received data in for receives.
13421 Receive_expression::do_flatten(Gogo
*, Named_object
*,
13422 Statement_inserter
* inserter
)
13424 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
13425 if (channel_type
== NULL
)
13427 go_assert(saw_errors());
13430 else if (this->channel_
->is_error_expression())
13432 go_assert(saw_errors());
13433 return Expression::make_error(this->location());
13436 Type
* element_type
= channel_type
->element_type();
13437 if (this->temp_receiver_
== NULL
)
13439 this->temp_receiver_
= Statement::make_temporary(element_type
, NULL
,
13441 this->temp_receiver_
->set_is_address_taken();
13442 inserter
->insert(this->temp_receiver_
);
13448 // Get the backend representation for a receive expression.
13451 Receive_expression::do_get_backend(Translate_context
* context
)
13453 Location loc
= this->location();
13455 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
13456 if (channel_type
== NULL
)
13458 go_assert(this->channel_
->type()->is_error());
13459 return context
->backend()->error_expression();
13461 Expression
* td
= Expression::make_type_descriptor(channel_type
, loc
);
13463 Expression
* recv_ref
=
13464 Expression::make_temporary_reference(this->temp_receiver_
, loc
);
13465 Expression
* recv_addr
=
13466 Expression::make_temporary_reference(this->temp_receiver_
, loc
);
13467 recv_addr
= Expression::make_unary(OPERATOR_AND
, recv_addr
, loc
);
13469 Runtime::make_call(Runtime::RECEIVE
, loc
, 3,
13470 td
, this->channel_
, recv_addr
);
13471 return Expression::make_compound(recv
, recv_ref
, loc
)->get_backend(context
);
13474 // Dump ast representation for a receive expression.
13477 Receive_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
13479 ast_dump_context
->ostream() << " <- " ;
13480 ast_dump_context
->dump_expression(channel_
);
13483 // Make a receive expression.
13485 Receive_expression
*
13486 Expression::make_receive(Expression
* channel
, Location location
)
13488 return new Receive_expression(channel
, location
);
13491 // An expression which evaluates to a pointer to the type descriptor
13494 class Type_descriptor_expression
: public Expression
13497 Type_descriptor_expression(Type
* type
, Location location
)
13498 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
13504 do_traverse(Traverse
*);
13508 { return Type::make_type_descriptor_ptr_type(); }
13511 do_is_immutable() const
13515 do_determine_type(const Type_context
*)
13523 do_get_backend(Translate_context
* context
)
13525 return this->type_
->type_descriptor_pointer(context
->gogo(),
13530 do_dump_expression(Ast_dump_context
*) const;
13533 // The type for which this is the descriptor.
13538 Type_descriptor_expression::do_traverse(Traverse
* traverse
)
13540 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
13541 return TRAVERSE_EXIT
;
13542 return TRAVERSE_CONTINUE
;
13545 // Dump ast representation for a type descriptor expression.
13548 Type_descriptor_expression::do_dump_expression(
13549 Ast_dump_context
* ast_dump_context
) const
13551 ast_dump_context
->dump_type(this->type_
);
13554 // Make a type descriptor expression.
13557 Expression::make_type_descriptor(Type
* type
, Location location
)
13559 return new Type_descriptor_expression(type
, location
);
13562 // An expression which evaluates to a pointer to the Garbage Collection symbol
13565 class GC_symbol_expression
: public Expression
13568 GC_symbol_expression(Type
* type
)
13569 : Expression(EXPRESSION_GC_SYMBOL
, Linemap::predeclared_location()),
13576 { return Type::lookup_integer_type("uintptr"); }
13579 do_is_immutable() const
13583 do_determine_type(const Type_context
*)
13591 do_get_backend(Translate_context
* context
)
13592 { return this->type_
->gc_symbol_pointer(context
->gogo()); }
13595 do_dump_expression(Ast_dump_context
*) const;
13598 // The type which this gc symbol describes.
13602 // Dump ast representation for a gc symbol expression.
13605 GC_symbol_expression::do_dump_expression(
13606 Ast_dump_context
* ast_dump_context
) const
13608 ast_dump_context
->ostream() << "gcdata(";
13609 ast_dump_context
->dump_type(this->type_
);
13610 ast_dump_context
->ostream() << ")";
13613 // Make a gc symbol expression.
13616 Expression::make_gc_symbol(Type
* type
)
13618 return new GC_symbol_expression(type
);
13621 // An expression which evaluates to some characteristic of a type.
13622 // This is only used to initialize fields of a type descriptor. Using
13623 // a new expression class is slightly inefficient but gives us a good
13624 // separation between the frontend and the middle-end with regard to
13625 // how types are laid out.
13627 class Type_info_expression
: public Expression
13630 Type_info_expression(Type
* type
, Type_info type_info
)
13631 : Expression(EXPRESSION_TYPE_INFO
, Linemap::predeclared_location()),
13632 type_(type
), type_info_(type_info
)
13637 do_is_immutable() const
13644 do_determine_type(const Type_context
*)
13652 do_get_backend(Translate_context
* context
);
13655 do_dump_expression(Ast_dump_context
*) const;
13658 // The type for which we are getting information.
13660 // What information we want.
13661 Type_info type_info_
;
13664 // The type is chosen to match what the type descriptor struct
13668 Type_info_expression::do_type()
13670 switch (this->type_info_
)
13672 case TYPE_INFO_SIZE
:
13673 return Type::lookup_integer_type("uintptr");
13674 case TYPE_INFO_ALIGNMENT
:
13675 case TYPE_INFO_FIELD_ALIGNMENT
:
13676 return Type::lookup_integer_type("uint8");
13682 // Return the backend representation for type information.
13685 Type_info_expression::do_get_backend(Translate_context
* context
)
13687 Btype
* btype
= this->type_
->get_backend(context
->gogo());
13688 Gogo
* gogo
= context
->gogo();
13690 switch (this->type_info_
)
13692 case TYPE_INFO_SIZE
:
13693 val
= gogo
->backend()->type_size(btype
);
13695 case TYPE_INFO_ALIGNMENT
:
13696 val
= gogo
->backend()->type_alignment(btype
);
13698 case TYPE_INFO_FIELD_ALIGNMENT
:
13699 val
= gogo
->backend()->type_field_alignment(btype
);
13704 Expression
* e
= Expression::make_integer_int64(val
, this->type(),
13706 return e
->get_backend(context
);
13709 // Dump ast representation for a type info expression.
13712 Type_info_expression::do_dump_expression(
13713 Ast_dump_context
* ast_dump_context
) const
13715 ast_dump_context
->ostream() << "typeinfo(";
13716 ast_dump_context
->dump_type(this->type_
);
13717 ast_dump_context
->ostream() << ",";
13718 ast_dump_context
->ostream() <<
13719 (this->type_info_
== TYPE_INFO_ALIGNMENT
? "alignment"
13720 : this->type_info_
== TYPE_INFO_FIELD_ALIGNMENT
? "field alignment"
13721 : this->type_info_
== TYPE_INFO_SIZE
? "size "
13723 ast_dump_context
->ostream() << ")";
13726 // Make a type info expression.
13729 Expression::make_type_info(Type
* type
, Type_info type_info
)
13731 return new Type_info_expression(type
, type_info
);
13734 // An expression that evaluates to some characteristic of a slice.
13735 // This is used when indexing, bound-checking, or nil checking a slice.
13737 class Slice_info_expression
: public Expression
13740 Slice_info_expression(Expression
* slice
, Slice_info slice_info
,
13742 : Expression(EXPRESSION_SLICE_INFO
, location
),
13743 slice_(slice
), slice_info_(slice_info
)
13751 do_determine_type(const Type_context
*)
13757 return new Slice_info_expression(this->slice_
->copy(), this->slice_info_
,
13762 do_get_backend(Translate_context
* context
);
13765 do_dump_expression(Ast_dump_context
*) const;
13768 do_issue_nil_check()
13769 { this->slice_
->issue_nil_check(); }
13772 // The slice for which we are getting information.
13773 Expression
* slice_
;
13774 // What information we want.
13775 Slice_info slice_info_
;
13778 // Return the type of the slice info.
13781 Slice_info_expression::do_type()
13783 switch (this->slice_info_
)
13785 case SLICE_INFO_VALUE_POINTER
:
13786 return Type::make_pointer_type(
13787 this->slice_
->type()->array_type()->element_type());
13788 case SLICE_INFO_LENGTH
:
13789 case SLICE_INFO_CAPACITY
:
13790 return Type::lookup_integer_type("int");
13796 // Return the backend information for slice information.
13799 Slice_info_expression::do_get_backend(Translate_context
* context
)
13801 Gogo
* gogo
= context
->gogo();
13802 Bexpression
* bslice
= this->slice_
->get_backend(context
);
13803 switch (this->slice_info_
)
13805 case SLICE_INFO_VALUE_POINTER
:
13806 case SLICE_INFO_LENGTH
:
13807 case SLICE_INFO_CAPACITY
:
13808 return gogo
->backend()->struct_field_expression(bslice
, this->slice_info_
,
13816 // Dump ast representation for a type info expression.
13819 Slice_info_expression::do_dump_expression(
13820 Ast_dump_context
* ast_dump_context
) const
13822 ast_dump_context
->ostream() << "sliceinfo(";
13823 this->slice_
->dump_expression(ast_dump_context
);
13824 ast_dump_context
->ostream() << ",";
13825 ast_dump_context
->ostream() <<
13826 (this->slice_info_
== SLICE_INFO_VALUE_POINTER
? "values"
13827 : this->slice_info_
== SLICE_INFO_LENGTH
? "length"
13828 : this->slice_info_
== SLICE_INFO_CAPACITY
? "capacity "
13830 ast_dump_context
->ostream() << ")";
13833 // Make a slice info expression.
13836 Expression::make_slice_info(Expression
* slice
, Slice_info slice_info
,
13839 return new Slice_info_expression(slice
, slice_info
, location
);
13842 // An expression that represents a slice value: a struct with value pointer,
13843 // length, and capacity fields.
13845 class Slice_value_expression
: public Expression
13848 Slice_value_expression(Type
* type
, Expression
* valptr
, Expression
* len
,
13849 Expression
* cap
, Location location
)
13850 : Expression(EXPRESSION_SLICE_VALUE
, location
),
13851 type_(type
), valptr_(valptr
), len_(len
), cap_(cap
)
13856 do_traverse(Traverse
*);
13860 { return this->type_
; }
13863 do_determine_type(const Type_context
*)
13864 { go_unreachable(); }
13869 return new Slice_value_expression(this->type_
, this->valptr_
->copy(),
13870 this->len_
->copy(), this->cap_
->copy(),
13875 do_get_backend(Translate_context
* context
);
13878 do_dump_expression(Ast_dump_context
*) const;
13881 // The type of the slice value.
13883 // The pointer to the values in the slice.
13884 Expression
* valptr_
;
13885 // The length of the slice.
13887 // The capacity of the slice.
13892 Slice_value_expression::do_traverse(Traverse
* traverse
)
13894 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
13895 || Expression::traverse(&this->valptr_
, traverse
) == TRAVERSE_EXIT
13896 || Expression::traverse(&this->len_
, traverse
) == TRAVERSE_EXIT
13897 || Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
)
13898 return TRAVERSE_EXIT
;
13899 return TRAVERSE_CONTINUE
;
13903 Slice_value_expression::do_get_backend(Translate_context
* context
)
13905 std::vector
<Bexpression
*> vals(3);
13906 vals
[0] = this->valptr_
->get_backend(context
);
13907 vals
[1] = this->len_
->get_backend(context
);
13908 vals
[2] = this->cap_
->get_backend(context
);
13910 Gogo
* gogo
= context
->gogo();
13911 Btype
* btype
= this->type_
->get_backend(gogo
);
13912 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
13916 Slice_value_expression::do_dump_expression(
13917 Ast_dump_context
* ast_dump_context
) const
13919 ast_dump_context
->ostream() << "slicevalue(";
13920 ast_dump_context
->ostream() << "values: ";
13921 this->valptr_
->dump_expression(ast_dump_context
);
13922 ast_dump_context
->ostream() << ", length: ";
13923 this->len_
->dump_expression(ast_dump_context
);
13924 ast_dump_context
->ostream() << ", capacity: ";
13925 this->cap_
->dump_expression(ast_dump_context
);
13926 ast_dump_context
->ostream() << ")";
13930 Expression::make_slice_value(Type
* at
, Expression
* valptr
, Expression
* len
,
13931 Expression
* cap
, Location location
)
13933 go_assert(at
->is_slice_type());
13934 return new Slice_value_expression(at
, valptr
, len
, cap
, location
);
13937 // An expression that evaluates to some characteristic of a non-empty interface.
13938 // This is used to access the method table or underlying object of an interface.
13940 class Interface_info_expression
: public Expression
13943 Interface_info_expression(Expression
* iface
, Interface_info iface_info
,
13945 : Expression(EXPRESSION_INTERFACE_INFO
, location
),
13946 iface_(iface
), iface_info_(iface_info
)
13954 do_determine_type(const Type_context
*)
13960 return new Interface_info_expression(this->iface_
->copy(),
13961 this->iface_info_
, this->location());
13965 do_get_backend(Translate_context
* context
);
13968 do_dump_expression(Ast_dump_context
*) const;
13971 do_issue_nil_check()
13972 { this->iface_
->issue_nil_check(); }
13975 // The interface for which we are getting information.
13976 Expression
* iface_
;
13977 // What information we want.
13978 Interface_info iface_info_
;
13981 // Return the type of the interface info.
13984 Interface_info_expression::do_type()
13986 switch (this->iface_info_
)
13988 case INTERFACE_INFO_METHODS
:
13990 Type
* pdt
= Type::make_type_descriptor_ptr_type();
13991 if (this->iface_
->type()->interface_type()->is_empty())
13994 Location loc
= this->location();
13995 Struct_field_list
* sfl
= new Struct_field_list();
13997 Struct_field(Typed_identifier("__type_descriptor", pdt
, loc
)));
13999 Interface_type
* itype
= this->iface_
->type()->interface_type();
14000 for (Typed_identifier_list::const_iterator p
= itype
->methods()->begin();
14001 p
!= itype
->methods()->end();
14004 Function_type
* ft
= p
->type()->function_type();
14005 go_assert(ft
->receiver() == NULL
);
14007 const Typed_identifier_list
* params
= ft
->parameters();
14008 Typed_identifier_list
* mparams
= new Typed_identifier_list();
14009 if (params
!= NULL
)
14010 mparams
->reserve(params
->size() + 1);
14011 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
14012 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
14013 if (params
!= NULL
)
14015 for (Typed_identifier_list::const_iterator pp
= params
->begin();
14016 pp
!= params
->end();
14018 mparams
->push_back(*pp
);
14021 Typed_identifier_list
* mresults
= (ft
->results() == NULL
14023 : ft
->results()->copy());
14024 Backend_function_type
* mft
=
14025 Type::make_backend_function_type(NULL
, mparams
, mresults
,
14028 std::string fname
= Gogo::unpack_hidden_name(p
->name());
14029 sfl
->push_back(Struct_field(Typed_identifier(fname
, mft
, loc
)));
14032 return Type::make_pointer_type(Type::make_struct_type(sfl
, loc
));
14034 case INTERFACE_INFO_OBJECT
:
14035 return Type::make_pointer_type(Type::make_void_type());
14041 // Return the backend representation for interface information.
14044 Interface_info_expression::do_get_backend(Translate_context
* context
)
14046 Gogo
* gogo
= context
->gogo();
14047 Bexpression
* biface
= this->iface_
->get_backend(context
);
14048 switch (this->iface_info_
)
14050 case INTERFACE_INFO_METHODS
:
14051 case INTERFACE_INFO_OBJECT
:
14052 return gogo
->backend()->struct_field_expression(biface
, this->iface_info_
,
14060 // Dump ast representation for an interface info expression.
14063 Interface_info_expression::do_dump_expression(
14064 Ast_dump_context
* ast_dump_context
) const
14066 bool is_empty
= this->iface_
->type()->interface_type()->is_empty();
14067 ast_dump_context
->ostream() << "interfaceinfo(";
14068 this->iface_
->dump_expression(ast_dump_context
);
14069 ast_dump_context
->ostream() << ",";
14070 ast_dump_context
->ostream() <<
14071 (this->iface_info_
== INTERFACE_INFO_METHODS
&& !is_empty
? "methods"
14072 : this->iface_info_
== INTERFACE_INFO_TYPE_DESCRIPTOR
? "type_descriptor"
14073 : this->iface_info_
== INTERFACE_INFO_OBJECT
? "object"
14075 ast_dump_context
->ostream() << ")";
14078 // Make an interface info expression.
14081 Expression::make_interface_info(Expression
* iface
, Interface_info iface_info
,
14084 return new Interface_info_expression(iface
, iface_info
, location
);
14087 // An expression that represents an interface value. The first field is either
14088 // a type descriptor for an empty interface or a pointer to the interface method
14089 // table for a non-empty interface. The second field is always the object.
14091 class Interface_value_expression
: public Expression
14094 Interface_value_expression(Type
* type
, Expression
* first_field
,
14095 Expression
* obj
, Location location
)
14096 : Expression(EXPRESSION_INTERFACE_VALUE
, location
),
14097 type_(type
), first_field_(first_field
), obj_(obj
)
14102 do_traverse(Traverse
*);
14106 { return this->type_
; }
14109 do_determine_type(const Type_context
*)
14110 { go_unreachable(); }
14115 return new Interface_value_expression(this->type_
,
14116 this->first_field_
->copy(),
14117 this->obj_
->copy(), this->location());
14121 do_get_backend(Translate_context
* context
);
14124 do_dump_expression(Ast_dump_context
*) const;
14127 // The type of the interface value.
14129 // The first field of the interface (either a type descriptor or a pointer
14130 // to the method table.
14131 Expression
* first_field_
;
14132 // The underlying object of the interface.
14137 Interface_value_expression::do_traverse(Traverse
* traverse
)
14139 if (Expression::traverse(&this->first_field_
, traverse
) == TRAVERSE_EXIT
14140 || Expression::traverse(&this->obj_
, traverse
) == TRAVERSE_EXIT
)
14141 return TRAVERSE_EXIT
;
14142 return TRAVERSE_CONTINUE
;
14146 Interface_value_expression::do_get_backend(Translate_context
* context
)
14148 std::vector
<Bexpression
*> vals(2);
14149 vals
[0] = this->first_field_
->get_backend(context
);
14150 vals
[1] = this->obj_
->get_backend(context
);
14152 Gogo
* gogo
= context
->gogo();
14153 Btype
* btype
= this->type_
->get_backend(gogo
);
14154 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
14158 Interface_value_expression::do_dump_expression(
14159 Ast_dump_context
* ast_dump_context
) const
14161 ast_dump_context
->ostream() << "interfacevalue(";
14162 ast_dump_context
->ostream() <<
14163 (this->type_
->interface_type()->is_empty()
14164 ? "type_descriptor: "
14166 this->first_field_
->dump_expression(ast_dump_context
);
14167 ast_dump_context
->ostream() << ", object: ";
14168 this->obj_
->dump_expression(ast_dump_context
);
14169 ast_dump_context
->ostream() << ")";
14173 Expression::make_interface_value(Type
* type
, Expression
* first_value
,
14174 Expression
* object
, Location location
)
14176 return new Interface_value_expression(type
, first_value
, object
, location
);
14179 // An interface method table for a pair of types: an interface type and a type
14180 // that implements that interface.
14182 class Interface_mtable_expression
: public Expression
14185 Interface_mtable_expression(Interface_type
* itype
, Type
* type
,
14186 bool is_pointer
, Location location
)
14187 : Expression(EXPRESSION_INTERFACE_MTABLE
, location
),
14188 itype_(itype
), type_(type
), is_pointer_(is_pointer
),
14189 method_table_type_(NULL
), bvar_(NULL
)
14194 do_traverse(Traverse
*);
14200 is_immutable() const
14204 do_determine_type(const Type_context
*)
14205 { go_unreachable(); }
14210 return new Interface_mtable_expression(this->itype_
, this->type_
,
14211 this->is_pointer_
, this->location());
14215 do_is_addressable() const
14219 do_get_backend(Translate_context
* context
);
14222 do_dump_expression(Ast_dump_context
*) const;
14225 // The interface type for which the methods are defined.
14226 Interface_type
* itype_
;
14227 // The type to construct the interface method table for.
14229 // Whether this table contains the method set for the receiver type or the
14230 // pointer receiver type.
14232 // The type of the method table.
14233 Type
* method_table_type_
;
14234 // The backend variable that refers to the interface method table.
14239 Interface_mtable_expression::do_traverse(Traverse
* traverse
)
14241 if (Type::traverse(this->itype_
, traverse
) == TRAVERSE_EXIT
14242 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
14243 return TRAVERSE_EXIT
;
14244 return TRAVERSE_CONTINUE
;
14248 Interface_mtable_expression::do_type()
14250 if (this->method_table_type_
!= NULL
)
14251 return this->method_table_type_
;
14253 const Typed_identifier_list
* interface_methods
= this->itype_
->methods();
14254 go_assert(!interface_methods
->empty());
14256 Struct_field_list
* sfl
= new Struct_field_list
;
14257 Typed_identifier
tid("__type_descriptor", Type::make_type_descriptor_ptr_type(),
14259 sfl
->push_back(Struct_field(tid
));
14260 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
14261 p
!= interface_methods
->end();
14263 sfl
->push_back(Struct_field(*p
));
14264 this->method_table_type_
= Type::make_struct_type(sfl
, this->location());
14265 return this->method_table_type_
;
14269 Interface_mtable_expression::do_get_backend(Translate_context
* context
)
14271 Gogo
* gogo
= context
->gogo();
14272 Location loc
= Linemap::predeclared_location();
14273 if (this->bvar_
!= NULL
)
14274 return gogo
->backend()->var_expression(this->bvar_
, this->location());
14276 const Typed_identifier_list
* interface_methods
= this->itype_
->methods();
14277 go_assert(!interface_methods
->empty());
14279 std::string mangled_name
= ((this->is_pointer_
? "__go_pimt__" : "__go_imt_")
14280 + this->itype_
->mangled_name(gogo
)
14282 + this->type_
->mangled_name(gogo
));
14284 // See whether this interface has any hidden methods.
14285 bool has_hidden_methods
= false;
14286 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
14287 p
!= interface_methods
->end();
14290 if (Gogo::is_hidden_name(p
->name()))
14292 has_hidden_methods
= true;
14297 // We already know that the named type is convertible to the
14298 // interface. If the interface has hidden methods, and the named
14299 // type is defined in a different package, then the interface
14300 // conversion table will be defined by that other package.
14301 if (has_hidden_methods
14302 && this->type_
->named_type() != NULL
14303 && this->type_
->named_type()->named_object()->package() != NULL
)
14305 Btype
* btype
= this->type()->get_backend(gogo
);
14307 gogo
->backend()->immutable_struct_reference(mangled_name
, btype
, loc
);
14308 return gogo
->backend()->var_expression(this->bvar_
, this->location());
14311 // The first element is the type descriptor.
14313 if (!this->is_pointer_
)
14314 td_type
= this->type_
;
14316 td_type
= Type::make_pointer_type(this->type_
);
14318 // Build an interface method table for a type: a type descriptor followed by a
14319 // list of function pointers, one for each interface method. This is used for
14321 Expression_list
* svals
= new Expression_list();
14322 svals
->push_back(Expression::make_type_descriptor(td_type
, loc
));
14324 Named_type
* nt
= this->type_
->named_type();
14325 Struct_type
* st
= this->type_
->struct_type();
14326 go_assert(nt
!= NULL
|| st
!= NULL
);
14328 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
14329 p
!= interface_methods
->end();
14335 m
= nt
->method_function(p
->name(), &is_ambiguous
);
14337 m
= st
->method_function(p
->name(), &is_ambiguous
);
14338 go_assert(m
!= NULL
);
14339 Named_object
* no
= m
->named_object();
14341 go_assert(no
->is_function() || no
->is_function_declaration());
14342 svals
->push_back(Expression::make_func_code_reference(no
, loc
));
14345 Btype
* btype
= this->type()->get_backend(gogo
);
14346 Expression
* mtable
= Expression::make_struct_composite_literal(this->type(),
14348 Bexpression
* ctor
= mtable
->get_backend(context
);
14350 bool is_public
= has_hidden_methods
&& this->type_
->named_type() != NULL
;
14351 this->bvar_
= gogo
->backend()->immutable_struct(mangled_name
, false,
14352 !is_public
, btype
, loc
);
14353 gogo
->backend()->immutable_struct_set_init(this->bvar_
, mangled_name
, false,
14354 !is_public
, btype
, loc
, ctor
);
14355 return gogo
->backend()->var_expression(this->bvar_
, loc
);
14359 Interface_mtable_expression::do_dump_expression(
14360 Ast_dump_context
* ast_dump_context
) const
14362 ast_dump_context
->ostream() << "__go_"
14363 << (this->is_pointer_
? "pimt__" : "imt_");
14364 ast_dump_context
->dump_type(this->itype_
);
14365 ast_dump_context
->ostream() << "__";
14366 ast_dump_context
->dump_type(this->type_
);
14370 Expression::make_interface_mtable_ref(Interface_type
* itype
, Type
* type
,
14371 bool is_pointer
, Location location
)
14373 return new Interface_mtable_expression(itype
, type
, is_pointer
, location
);
14376 // An expression which evaluates to the offset of a field within a
14377 // struct. This, like Type_info_expression, q.v., is only used to
14378 // initialize fields of a type descriptor.
14380 class Struct_field_offset_expression
: public Expression
14383 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
14384 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
,
14385 Linemap::predeclared_location()),
14386 type_(type
), field_(field
)
14391 do_is_immutable() const
14396 { return Type::lookup_integer_type("uintptr"); }
14399 do_determine_type(const Type_context
*)
14407 do_get_backend(Translate_context
* context
);
14410 do_dump_expression(Ast_dump_context
*) const;
14413 // The type of the struct.
14414 Struct_type
* type_
;
14416 const Struct_field
* field_
;
14419 // Return the backend representation for a struct field offset.
14422 Struct_field_offset_expression::do_get_backend(Translate_context
* context
)
14424 const Struct_field_list
* fields
= this->type_
->fields();
14425 Struct_field_list::const_iterator p
;
14427 for (p
= fields
->begin();
14428 p
!= fields
->end();
14430 if (&*p
== this->field_
)
14432 go_assert(&*p
== this->field_
);
14434 Gogo
* gogo
= context
->gogo();
14435 Btype
* btype
= this->type_
->get_backend(gogo
);
14437 int64_t offset
= gogo
->backend()->type_field_offset(btype
, i
);
14438 Type
* uptr_type
= Type::lookup_integer_type("uintptr");
14440 Expression::make_integer_int64(offset
, uptr_type
,
14441 Linemap::predeclared_location());
14442 return ret
->get_backend(context
);
14445 // Dump ast representation for a struct field offset expression.
14448 Struct_field_offset_expression::do_dump_expression(
14449 Ast_dump_context
* ast_dump_context
) const
14451 ast_dump_context
->ostream() << "unsafe.Offsetof(";
14452 ast_dump_context
->dump_type(this->type_
);
14453 ast_dump_context
->ostream() << '.';
14454 ast_dump_context
->ostream() <<
14455 Gogo::message_name(this->field_
->field_name());
14456 ast_dump_context
->ostream() << ")";
14459 // Make an expression for a struct field offset.
14462 Expression::make_struct_field_offset(Struct_type
* type
,
14463 const Struct_field
* field
)
14465 return new Struct_field_offset_expression(type
, field
);
14468 // An expression which evaluates to a pointer to the map descriptor of
14471 class Map_descriptor_expression
: public Expression
14474 Map_descriptor_expression(Map_type
* type
, Location location
)
14475 : Expression(EXPRESSION_MAP_DESCRIPTOR
, location
),
14482 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
14485 do_determine_type(const Type_context
*)
14493 do_get_backend(Translate_context
* context
)
14495 return this->type_
->map_descriptor_pointer(context
->gogo(),
14500 do_dump_expression(Ast_dump_context
*) const;
14503 // The type for which this is the descriptor.
14507 // Dump ast representation for a map descriptor expression.
14510 Map_descriptor_expression::do_dump_expression(
14511 Ast_dump_context
* ast_dump_context
) const
14513 ast_dump_context
->ostream() << "map_descriptor(";
14514 ast_dump_context
->dump_type(this->type_
);
14515 ast_dump_context
->ostream() << ")";
14518 // Make a map descriptor expression.
14521 Expression::make_map_descriptor(Map_type
* type
, Location location
)
14523 return new Map_descriptor_expression(type
, location
);
14526 // An expression which evaluates to the address of an unnamed label.
14528 class Label_addr_expression
: public Expression
14531 Label_addr_expression(Label
* label
, Location location
)
14532 : Expression(EXPRESSION_LABEL_ADDR
, location
),
14539 { return Type::make_pointer_type(Type::make_void_type()); }
14542 do_determine_type(const Type_context
*)
14547 { return new Label_addr_expression(this->label_
, this->location()); }
14550 do_get_backend(Translate_context
* context
)
14551 { return this->label_
->get_addr(context
, this->location()); }
14554 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
14555 { ast_dump_context
->ostream() << this->label_
->name(); }
14558 // The label whose address we are taking.
14562 // Make an expression for the address of an unnamed label.
14565 Expression::make_label_addr(Label
* label
, Location location
)
14567 return new Label_addr_expression(label
, location
);
14570 // Class Conditional_expression.
14575 Conditional_expression::do_traverse(Traverse
* traverse
)
14577 if (Expression::traverse(&this->cond_
, traverse
) == TRAVERSE_EXIT
14578 || Expression::traverse(&this->then_
, traverse
) == TRAVERSE_EXIT
14579 || Expression::traverse(&this->else_
, traverse
) == TRAVERSE_EXIT
)
14580 return TRAVERSE_EXIT
;
14581 return TRAVERSE_CONTINUE
;
14584 // Return the type of the conditional expression.
14587 Conditional_expression::do_type()
14589 Type
* result_type
= Type::make_void_type();
14590 if (Type::are_identical(this->then_
->type(), this->else_
->type(), false,
14592 result_type
= this->then_
->type();
14593 else if (this->then_
->is_nil_expression()
14594 || this->else_
->is_nil_expression())
14595 result_type
= (!this->then_
->is_nil_expression()
14596 ? this->then_
->type()
14597 : this->else_
->type());
14598 return result_type
;
14601 // Determine type for a conditional expression.
14604 Conditional_expression::do_determine_type(const Type_context
* context
)
14606 this->cond_
->determine_type_no_context();
14607 this->then_
->determine_type(context
);
14608 this->else_
->determine_type(context
);
14611 // Get the backend representation of a conditional expression.
14614 Conditional_expression::do_get_backend(Translate_context
* context
)
14616 Gogo
* gogo
= context
->gogo();
14617 Btype
* result_btype
= this->type()->get_backend(gogo
);
14618 Bexpression
* cond
= this->cond_
->get_backend(context
);
14619 Bexpression
* then
= this->then_
->get_backend(context
);
14620 Bexpression
* belse
= this->else_
->get_backend(context
);
14621 return gogo
->backend()->conditional_expression(result_btype
, cond
, then
,
14622 belse
, this->location());
14625 // Dump ast representation of a conditional expression.
14628 Conditional_expression::do_dump_expression(
14629 Ast_dump_context
* ast_dump_context
) const
14631 ast_dump_context
->ostream() << "(";
14632 ast_dump_context
->dump_expression(this->cond_
);
14633 ast_dump_context
->ostream() << " ? ";
14634 ast_dump_context
->dump_expression(this->then_
);
14635 ast_dump_context
->ostream() << " : ";
14636 ast_dump_context
->dump_expression(this->else_
);
14637 ast_dump_context
->ostream() << ") ";
14640 // Make a conditional expression.
14643 Expression::make_conditional(Expression
* cond
, Expression
* then
,
14644 Expression
* else_expr
, Location location
)
14646 return new Conditional_expression(cond
, then
, else_expr
, location
);
14649 // Class Compound_expression.
14654 Compound_expression::do_traverse(Traverse
* traverse
)
14656 if (Expression::traverse(&this->init_
, traverse
) == TRAVERSE_EXIT
14657 || Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
14658 return TRAVERSE_EXIT
;
14659 return TRAVERSE_CONTINUE
;
14662 // Return the type of the compound expression.
14665 Compound_expression::do_type()
14667 return this->expr_
->type();
14670 // Determine type for a compound expression.
14673 Compound_expression::do_determine_type(const Type_context
* context
)
14675 this->init_
->determine_type_no_context();
14676 this->expr_
->determine_type(context
);
14679 // Get the backend representation of a compound expression.
14682 Compound_expression::do_get_backend(Translate_context
* context
)
14684 Gogo
* gogo
= context
->gogo();
14685 Bexpression
* binit
= this->init_
->get_backend(context
);
14686 Bstatement
* init_stmt
= gogo
->backend()->expression_statement(binit
);
14687 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
14688 return gogo
->backend()->compound_expression(init_stmt
, bexpr
,
14692 // Dump ast representation of a conditional expression.
14695 Compound_expression::do_dump_expression(
14696 Ast_dump_context
* ast_dump_context
) const
14698 ast_dump_context
->ostream() << "(";
14699 ast_dump_context
->dump_expression(this->init_
);
14700 ast_dump_context
->ostream() << ",";
14701 ast_dump_context
->dump_expression(this->expr_
);
14702 ast_dump_context
->ostream() << ") ";
14705 // Make a compound expression.
14708 Expression::make_compound(Expression
* init
, Expression
* expr
, Location location
)
14710 return new Compound_expression(init
, expr
, location
);
14713 // Import an expression. This comes at the end in order to see the
14714 // various class definitions.
14717 Expression::import_expression(Import
* imp
)
14719 int c
= imp
->peek_char();
14720 if (imp
->match_c_string("- ")
14721 || imp
->match_c_string("! ")
14722 || imp
->match_c_string("^ "))
14723 return Unary_expression::do_import(imp
);
14725 return Binary_expression::do_import(imp
);
14726 else if (imp
->match_c_string("true")
14727 || imp
->match_c_string("false"))
14728 return Boolean_expression::do_import(imp
);
14730 return String_expression::do_import(imp
);
14731 else if (c
== '-' || (c
>= '0' && c
<= '9'))
14733 // This handles integers, floats and complex constants.
14734 return Integer_expression::do_import(imp
);
14736 else if (imp
->match_c_string("nil"))
14737 return Nil_expression::do_import(imp
);
14738 else if (imp
->match_c_string("convert"))
14739 return Type_conversion_expression::do_import(imp
);
14742 error_at(imp
->location(), "import error: expected expression");
14743 return Expression::make_error(imp
->location());
14747 // Class Expression_list.
14749 // Traverse the list.
14752 Expression_list::traverse(Traverse
* traverse
)
14754 for (Expression_list::iterator p
= this->begin();
14760 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
14761 return TRAVERSE_EXIT
;
14764 return TRAVERSE_CONTINUE
;
14770 Expression_list::copy()
14772 Expression_list
* ret
= new Expression_list();
14773 for (Expression_list::iterator p
= this->begin();
14778 ret
->push_back(NULL
);
14780 ret
->push_back((*p
)->copy());
14785 // Return whether an expression list has an error expression.
14788 Expression_list::contains_error() const
14790 for (Expression_list::const_iterator p
= this->begin();
14793 if (*p
!= NULL
&& (*p
)->is_error_expression())
14798 // Class Numeric_constant.
14802 Numeric_constant::~Numeric_constant()
14807 // Copy constructor.
14809 Numeric_constant::Numeric_constant(const Numeric_constant
& a
)
14810 : classification_(a
.classification_
), type_(a
.type_
)
14812 switch (a
.classification_
)
14818 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
14821 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, GMP_RNDN
);
14824 mpc_init2(this->u_
.complex_val
, mpc_precision
);
14825 mpc_set(this->u_
.complex_val
, a
.u_
.complex_val
, MPC_RNDNN
);
14832 // Assignment operator.
14835 Numeric_constant::operator=(const Numeric_constant
& a
)
14838 this->classification_
= a
.classification_
;
14839 this->type_
= a
.type_
;
14840 switch (a
.classification_
)
14846 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
14849 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, GMP_RNDN
);
14852 mpc_init2(this->u_
.complex_val
, mpc_precision
);
14853 mpc_set(this->u_
.complex_val
, a
.u_
.complex_val
, MPC_RNDNN
);
14861 // Clear the contents.
14864 Numeric_constant::clear()
14866 switch (this->classification_
)
14872 mpz_clear(this->u_
.int_val
);
14875 mpfr_clear(this->u_
.float_val
);
14878 mpc_clear(this->u_
.complex_val
);
14883 this->classification_
= NC_INVALID
;
14886 // Set to an unsigned long value.
14889 Numeric_constant::set_unsigned_long(Type
* type
, unsigned long val
)
14892 this->classification_
= NC_INT
;
14893 this->type_
= type
;
14894 mpz_init_set_ui(this->u_
.int_val
, val
);
14897 // Set to an integer value.
14900 Numeric_constant::set_int(Type
* type
, const mpz_t val
)
14903 this->classification_
= NC_INT
;
14904 this->type_
= type
;
14905 mpz_init_set(this->u_
.int_val
, val
);
14908 // Set to a rune value.
14911 Numeric_constant::set_rune(Type
* type
, const mpz_t val
)
14914 this->classification_
= NC_RUNE
;
14915 this->type_
= type
;
14916 mpz_init_set(this->u_
.int_val
, val
);
14919 // Set to a floating point value.
14922 Numeric_constant::set_float(Type
* type
, const mpfr_t val
)
14925 this->classification_
= NC_FLOAT
;
14926 this->type_
= type
;
14927 // Numeric constants do not have negative zero values, so remove
14928 // them here. They also don't have infinity or NaN values, but we
14929 // should never see them here.
14930 if (mpfr_zero_p(val
))
14931 mpfr_init_set_ui(this->u_
.float_val
, 0, GMP_RNDN
);
14933 mpfr_init_set(this->u_
.float_val
, val
, GMP_RNDN
);
14936 // Set to a complex value.
14939 Numeric_constant::set_complex(Type
* type
, const mpc_t val
)
14942 this->classification_
= NC_COMPLEX
;
14943 this->type_
= type
;
14944 mpc_init2(this->u_
.complex_val
, mpc_precision
);
14945 mpc_set(this->u_
.complex_val
, val
, MPC_RNDNN
);
14948 // Get an int value.
14951 Numeric_constant::get_int(mpz_t
* val
) const
14953 go_assert(this->is_int());
14954 mpz_init_set(*val
, this->u_
.int_val
);
14957 // Get a rune value.
14960 Numeric_constant::get_rune(mpz_t
* val
) const
14962 go_assert(this->is_rune());
14963 mpz_init_set(*val
, this->u_
.int_val
);
14966 // Get a floating point value.
14969 Numeric_constant::get_float(mpfr_t
* val
) const
14971 go_assert(this->is_float());
14972 mpfr_init_set(*val
, this->u_
.float_val
, GMP_RNDN
);
14975 // Get a complex value.
14978 Numeric_constant::get_complex(mpc_t
* val
) const
14980 go_assert(this->is_complex());
14981 mpc_init2(*val
, mpc_precision
);
14982 mpc_set(*val
, this->u_
.complex_val
, MPC_RNDNN
);
14985 // Express value as unsigned long if possible.
14987 Numeric_constant::To_unsigned_long
14988 Numeric_constant::to_unsigned_long(unsigned long* val
) const
14990 switch (this->classification_
)
14994 return this->mpz_to_unsigned_long(this->u_
.int_val
, val
);
14996 return this->mpfr_to_unsigned_long(this->u_
.float_val
, val
);
14998 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
14999 return NC_UL_NOTINT
;
15000 return this->mpfr_to_unsigned_long(mpc_realref(this->u_
.complex_val
),
15007 // Express integer value as unsigned long if possible.
15009 Numeric_constant::To_unsigned_long
15010 Numeric_constant::mpz_to_unsigned_long(const mpz_t ival
,
15011 unsigned long *val
) const
15013 if (mpz_sgn(ival
) < 0)
15014 return NC_UL_NEGATIVE
;
15015 unsigned long ui
= mpz_get_ui(ival
);
15016 if (mpz_cmp_ui(ival
, ui
) != 0)
15019 return NC_UL_VALID
;
15022 // Express floating point value as unsigned long if possible.
15024 Numeric_constant::To_unsigned_long
15025 Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval
,
15026 unsigned long *val
) const
15028 if (!mpfr_integer_p(fval
))
15029 return NC_UL_NOTINT
;
15032 mpfr_get_z(ival
, fval
, GMP_RNDN
);
15033 To_unsigned_long ret
= this->mpz_to_unsigned_long(ival
, val
);
15038 // Convert value to integer if possible.
15041 Numeric_constant::to_int(mpz_t
* val
) const
15043 switch (this->classification_
)
15047 mpz_init_set(*val
, this->u_
.int_val
);
15050 if (!mpfr_integer_p(this->u_
.float_val
))
15053 mpfr_get_z(*val
, this->u_
.float_val
, GMP_RNDN
);
15056 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
))
15057 || !mpfr_integer_p(mpc_realref(this->u_
.complex_val
)))
15060 mpfr_get_z(*val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15067 // Convert value to floating point if possible.
15070 Numeric_constant::to_float(mpfr_t
* val
) const
15072 switch (this->classification_
)
15076 mpfr_init_set_z(*val
, this->u_
.int_val
, GMP_RNDN
);
15079 mpfr_init_set(*val
, this->u_
.float_val
, GMP_RNDN
);
15082 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
15084 mpfr_init_set(*val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15091 // Convert value to complex.
15094 Numeric_constant::to_complex(mpc_t
* val
) const
15096 mpc_init2(*val
, mpc_precision
);
15097 switch (this->classification_
)
15101 mpc_set_z(*val
, this->u_
.int_val
, MPC_RNDNN
);
15104 mpc_set_fr(*val
, this->u_
.float_val
, MPC_RNDNN
);
15107 mpc_set(*val
, this->u_
.complex_val
, MPC_RNDNN
);
15117 Numeric_constant::type() const
15119 if (this->type_
!= NULL
)
15120 return this->type_
;
15121 switch (this->classification_
)
15124 return Type::make_abstract_integer_type();
15126 return Type::make_abstract_character_type();
15128 return Type::make_abstract_float_type();
15130 return Type::make_abstract_complex_type();
15136 // If the constant can be expressed in TYPE, then set the type of the
15137 // constant to TYPE and return true. Otherwise return false, and, if
15138 // ISSUE_ERROR is true, report an appropriate error message.
15141 Numeric_constant::set_type(Type
* type
, bool issue_error
, Location loc
)
15144 if (type
== NULL
|| type
->is_error())
15146 else if (type
->integer_type() != NULL
)
15147 ret
= this->check_int_type(type
->integer_type(), issue_error
, loc
);
15148 else if (type
->float_type() != NULL
)
15149 ret
= this->check_float_type(type
->float_type(), issue_error
, loc
);
15150 else if (type
->complex_type() != NULL
)
15151 ret
= this->check_complex_type(type
->complex_type(), issue_error
, loc
);
15156 go_assert(saw_errors());
15159 this->type_
= type
;
15163 // Check whether the constant can be expressed in an integer type.
15166 Numeric_constant::check_int_type(Integer_type
* type
, bool issue_error
,
15167 Location location
) const
15170 switch (this->classification_
)
15174 mpz_init_set(val
, this->u_
.int_val
);
15178 if (!mpfr_integer_p(this->u_
.float_val
))
15181 error_at(location
, "floating point constant truncated to integer");
15185 mpfr_get_z(val
, this->u_
.float_val
, GMP_RNDN
);
15189 if (!mpfr_integer_p(mpc_realref(this->u_
.complex_val
))
15190 || !mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
15193 error_at(location
, "complex constant truncated to integer");
15197 mpfr_get_z(val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15205 if (type
->is_abstract())
15209 int bits
= mpz_sizeinbase(val
, 2);
15210 if (type
->is_unsigned())
15212 // For an unsigned type we can only accept a nonnegative
15213 // number, and we must be able to represents at least BITS.
15214 ret
= mpz_sgn(val
) >= 0 && bits
<= type
->bits();
15218 // For a signed type we need an extra bit to indicate the
15219 // sign. We have to handle the most negative integer
15221 ret
= (bits
+ 1 <= type
->bits()
15222 || (bits
<= type
->bits()
15223 && mpz_sgn(val
) < 0
15224 && (mpz_scan1(val
, 0)
15225 == static_cast<unsigned long>(type
->bits() - 1))
15226 && mpz_scan0(val
, type
->bits()) == ULONG_MAX
));
15230 if (!ret
&& issue_error
)
15231 error_at(location
, "integer constant overflow");
15236 // Check whether the constant can be expressed in a floating point
15240 Numeric_constant::check_float_type(Float_type
* type
, bool issue_error
,
15244 switch (this->classification_
)
15248 mpfr_init_set_z(val
, this->u_
.int_val
, GMP_RNDN
);
15252 mpfr_init_set(val
, this->u_
.float_val
, GMP_RNDN
);
15256 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
15259 error_at(location
, "complex constant truncated to float");
15262 mpfr_init_set(val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15270 if (type
->is_abstract())
15272 else if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
15274 // A NaN or Infinity always fits in the range of the type.
15279 mp_exp_t exp
= mpfr_get_exp(val
);
15281 switch (type
->bits())
15293 ret
= exp
<= max_exp
;
15297 // Round the constant to the desired type.
15300 switch (type
->bits())
15303 mpfr_set_prec(t
, 24);
15306 mpfr_set_prec(t
, 53);
15311 mpfr_set(t
, val
, GMP_RNDN
);
15312 mpfr_set(val
, t
, GMP_RNDN
);
15315 this->set_float(type
, val
);
15321 if (!ret
&& issue_error
)
15322 error_at(location
, "floating point constant overflow");
15327 // Check whether the constant can be expressed in a complex type.
15330 Numeric_constant::check_complex_type(Complex_type
* type
, bool issue_error
,
15333 if (type
->is_abstract())
15337 switch (type
->bits())
15350 mpc_init2(val
, mpc_precision
);
15351 switch (this->classification_
)
15355 mpc_set_z(val
, this->u_
.int_val
, MPC_RNDNN
);
15359 mpc_set_fr(val
, this->u_
.float_val
, MPC_RNDNN
);
15363 mpc_set(val
, this->u_
.complex_val
, MPC_RNDNN
);
15371 if (!mpfr_nan_p(mpc_realref(val
))
15372 && !mpfr_inf_p(mpc_realref(val
))
15373 && !mpfr_zero_p(mpc_realref(val
))
15374 && mpfr_get_exp(mpc_realref(val
)) > max_exp
)
15377 error_at(location
, "complex real part overflow");
15381 if (!mpfr_nan_p(mpc_imagref(val
))
15382 && !mpfr_inf_p(mpc_imagref(val
))
15383 && !mpfr_zero_p(mpc_imagref(val
))
15384 && mpfr_get_exp(mpc_imagref(val
)) > max_exp
)
15387 error_at(location
, "complex imaginary part overflow");
15393 // Round the constant to the desired type.
15395 switch (type
->bits())
15406 mpc_set(t
, val
, MPC_RNDNN
);
15407 mpc_set(val
, t
, MPC_RNDNN
);
15410 this->set_complex(type
, val
);
15418 // Return an Expression for this value.
15421 Numeric_constant::expression(Location loc
) const
15423 switch (this->classification_
)
15426 return Expression::make_integer_z(&this->u_
.int_val
, this->type_
, loc
);
15428 return Expression::make_character(&this->u_
.int_val
, this->type_
, loc
);
15430 return Expression::make_float(&this->u_
.float_val
, this->type_
, loc
);
15432 return Expression::make_complex(&this->u_
.complex_val
, this->type_
, loc
);