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
* 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 // Avoid confusion from zero sized variables which may be
181 // represented as non-zero-sized.
182 // TODO(cmang): This check is for a GCC-specific issue, and should be
183 // removed from the frontend. FIXME.
184 size_t lhs_size
= gogo
->backend()->type_size(lhs_type
->get_backend(gogo
));
185 size_t rhs_size
= gogo
->backend()->type_size(rhs_type
->get_backend(gogo
));
186 if (rhs_size
== 0 || lhs_size
== 0)
189 // This conversion must be permitted by Go, or we wouldn't have
191 return Expression::make_unsafe_cast(lhs_type
, rhs
, location
);
197 // Return an expression for a conversion from a non-interface type to an
201 Expression::convert_type_to_interface(Type
* lhs_type
, Expression
* rhs
,
204 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
205 bool lhs_is_empty
= lhs_interface_type
->is_empty();
207 // Since RHS_TYPE is a static type, we can create the interface
208 // method table at compile time.
210 // When setting an interface to nil, we just set both fields to
212 Type
* rhs_type
= rhs
->type();
213 if (rhs_type
->is_nil_type())
215 Expression
* nil
= Expression::make_nil(location
);
216 return Expression::make_interface_value(lhs_type
, nil
, nil
, location
);
219 // This should have been checked already.
220 go_assert(lhs_interface_type
->implements_interface(rhs_type
, NULL
));
222 // An interface is a tuple. If LHS_TYPE is an empty interface type,
223 // then the first field is the type descriptor for RHS_TYPE.
224 // Otherwise it is the interface method table for RHS_TYPE.
225 Expression
* first_field
;
227 first_field
= Expression::make_type_descriptor(rhs_type
, location
);
230 // Build the interface method table for this interface and this
231 // object type: a list of function pointers for each interface
233 Named_type
* rhs_named_type
= rhs_type
->named_type();
234 Struct_type
* rhs_struct_type
= rhs_type
->struct_type();
235 bool is_pointer
= false;
236 if (rhs_named_type
== NULL
&& rhs_struct_type
== NULL
)
238 rhs_named_type
= rhs_type
->deref()->named_type();
239 rhs_struct_type
= rhs_type
->deref()->struct_type();
242 if (rhs_named_type
!= NULL
)
244 rhs_named_type
->interface_method_table(lhs_interface_type
,
246 else if (rhs_struct_type
!= NULL
)
248 rhs_struct_type
->interface_method_table(lhs_interface_type
,
251 first_field
= Expression::make_nil(location
);
255 if (rhs_type
->points_to() != NULL
)
257 // We are assigning a pointer to the interface; the interface
258 // holds the pointer itself.
263 // We are assigning a non-pointer value to the interface; the
264 // interface gets a copy of the value in the heap.
265 obj
= Expression::make_heap_expression(rhs
, location
);
268 return Expression::make_interface_value(lhs_type
, first_field
, obj
, location
);
271 // Return an expression for the type descriptor of RHS.
274 Expression::get_interface_type_descriptor(Expression
* rhs
)
276 go_assert(rhs
->type()->interface_type() != NULL
);
277 Location location
= rhs
->location();
279 // The type descriptor is the first field of an empty interface.
280 if (rhs
->type()->interface_type()->is_empty())
281 return Expression::make_interface_info(rhs
, INTERFACE_INFO_TYPE_DESCRIPTOR
,
285 Expression::make_interface_info(rhs
, INTERFACE_INFO_METHODS
, location
);
287 Expression
* descriptor
=
288 Expression::make_unary(OPERATOR_MULT
, mtable
, location
);
289 descriptor
= Expression::make_field_reference(descriptor
, 0, location
);
290 Expression
* nil
= Expression::make_nil(location
);
293 Expression::make_binary(OPERATOR_EQEQ
, mtable
, nil
, location
);
294 return Expression::make_conditional(eq
, nil
, descriptor
, location
);
297 // Return an expression for the conversion of an interface type to an
301 Expression::convert_interface_to_interface(Type
*lhs_type
, Expression
* rhs
,
305 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
306 bool lhs_is_empty
= lhs_interface_type
->is_empty();
308 // In the general case this requires runtime examination of the type
309 // method table to match it up with the interface methods.
311 // FIXME: If all of the methods in the right hand side interface
312 // also appear in the left hand side interface, then we don't need
313 // to do a runtime check, although we still need to build a new
316 // Get the type descriptor for the right hand side. This will be
317 // NULL for a nil interface.
318 Expression
* rhs_type_expr
= Expression::get_interface_type_descriptor(rhs
);
319 Expression
* lhs_type_expr
=
320 Expression::make_type_descriptor(lhs_type
, location
);
322 Expression
* first_field
;
325 // A type assertion fails when converting a nil interface.
327 Runtime::make_call(Runtime::ASSERT_INTERFACE
, location
, 2,
328 lhs_type_expr
, rhs_type_expr
);
330 else if (lhs_is_empty
)
332 // A conversion to an empty interface always succeeds, and the
333 // first field is just the type descriptor of the object.
334 first_field
= rhs_type_expr
;
338 // A conversion to a non-empty interface may fail, but unlike a
339 // type assertion converting nil will always succeed.
341 Runtime::make_call(Runtime::CONVERT_INTERFACE
, location
, 2,
342 lhs_type_expr
, rhs_type_expr
);
345 // The second field is simply the object pointer.
347 Expression::make_interface_info(rhs
, INTERFACE_INFO_OBJECT
, location
);
348 return Expression::make_interface_value(lhs_type
, first_field
, obj
, location
);
351 // Return an expression for the conversion of an interface type to a
352 // non-interface type.
355 Expression::convert_interface_to_type(Type
*lhs_type
, Expression
* rhs
,
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 // Build a new character constant value.
2109 Expression::make_character(const mpz_t
* val
, Type
* type
, Location location
)
2111 return new Integer_expression(val
, type
, true, location
);
2116 class Float_expression
: public Expression
2119 Float_expression(const mpfr_t
* val
, Type
* type
, Location location
)
2120 : Expression(EXPRESSION_FLOAT
, location
),
2123 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
2126 // Write VAL to export data.
2128 export_float(String_dump
* exp
, const mpfr_t val
);
2130 // Write VAL to dump file.
2132 dump_float(Ast_dump_context
* ast_dump_context
, const mpfr_t val
);
2136 do_is_constant() const
2140 do_is_immutable() const
2144 do_numeric_constant_value(Numeric_constant
* nc
) const
2146 nc
->set_float(this->type_
, this->val_
);
2154 do_determine_type(const Type_context
*);
2157 do_check_types(Gogo
*);
2161 { return Expression::make_float(&this->val_
, this->type_
,
2162 this->location()); }
2165 do_get_backend(Translate_context
*);
2168 do_export(Export
*) const;
2171 do_dump_expression(Ast_dump_context
*) const;
2174 // The floating point value.
2180 // Return the current type. If we haven't set the type yet, we return
2181 // an abstract float type.
2184 Float_expression::do_type()
2186 if (this->type_
== NULL
)
2187 this->type_
= Type::make_abstract_float_type();
2191 // Set the type of the float value. Here we may switch from an
2192 // abstract type to a real type.
2195 Float_expression::do_determine_type(const Type_context
* context
)
2197 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2199 else if (context
->type
!= NULL
2200 && (context
->type
->integer_type() != NULL
2201 || context
->type
->float_type() != NULL
2202 || context
->type
->complex_type() != NULL
))
2203 this->type_
= context
->type
;
2204 else if (!context
->may_be_abstract
)
2205 this->type_
= Type::lookup_float_type("float64");
2208 // Check the type of a float value.
2211 Float_expression::do_check_types(Gogo
*)
2213 Type
* type
= this->type_
;
2216 Numeric_constant nc
;
2217 nc
.set_float(NULL
, this->val_
);
2218 if (!nc
.set_type(this->type_
, true, this->location()))
2219 this->set_is_error();
2222 // Get the backend representation for a float constant.
2225 Float_expression::do_get_backend(Translate_context
* context
)
2227 Type
* resolved_type
;
2228 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2229 resolved_type
= this->type_
;
2230 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2232 // We have an abstract integer type. We just hope for the best.
2233 resolved_type
= Type::lookup_integer_type("int");
2235 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
2237 // We are converting to an abstract complex type.
2238 resolved_type
= Type::lookup_complex_type("complex128");
2242 // If we still have an abstract type here, then this is being
2243 // used in a constant expression which didn't get reduced. We
2244 // just use float64 and hope for the best.
2245 resolved_type
= Type::lookup_float_type("float64");
2248 Numeric_constant nc
;
2249 nc
.set_float(resolved_type
, this->val_
);
2250 return Expression::backend_numeric_constant_expression(context
, &nc
);
2253 // Write a floating point number to a string dump.
2256 Float_expression::export_float(String_dump
*exp
, const mpfr_t val
)
2259 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2261 exp
->write_c_string("-");
2262 exp
->write_c_string("0.");
2263 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2266 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2267 exp
->write_c_string(buf
);
2270 // Export a floating point number in a constant expression.
2273 Float_expression::do_export(Export
* exp
) const
2275 Float_expression::export_float(exp
, this->val_
);
2276 // A trailing space lets us reliably identify the end of the number.
2277 exp
->write_c_string(" ");
2280 // Dump a floating point number to the dump file.
2283 Float_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2285 Float_expression::export_float(ast_dump_context
, this->val_
);
2288 // Make a float expression.
2291 Expression::make_float(const mpfr_t
* val
, Type
* type
, Location location
)
2293 return new Float_expression(val
, type
, location
);
2298 class Complex_expression
: public Expression
2301 Complex_expression(const mpc_t
* val
, Type
* type
, Location location
)
2302 : Expression(EXPRESSION_COMPLEX
, location
),
2305 mpc_init2(this->val_
, mpc_precision
);
2306 mpc_set(this->val_
, *val
, MPC_RNDNN
);
2309 // Write VAL to string dump.
2311 export_complex(String_dump
* exp
, const mpc_t val
);
2313 // Write REAL/IMAG to dump context.
2315 dump_complex(Ast_dump_context
* ast_dump_context
, const mpc_t val
);
2319 do_is_constant() const
2323 do_is_immutable() const
2327 do_numeric_constant_value(Numeric_constant
* nc
) const
2329 nc
->set_complex(this->type_
, this->val_
);
2337 do_determine_type(const Type_context
*);
2340 do_check_types(Gogo
*);
2345 return Expression::make_complex(&this->val_
, this->type_
,
2350 do_get_backend(Translate_context
*);
2353 do_export(Export
*) const;
2356 do_dump_expression(Ast_dump_context
*) const;
2359 // The complex value.
2361 // The type if known.
2365 // Return the current type. If we haven't set the type yet, we return
2366 // an abstract complex type.
2369 Complex_expression::do_type()
2371 if (this->type_
== NULL
)
2372 this->type_
= Type::make_abstract_complex_type();
2376 // Set the type of the complex value. Here we may switch from an
2377 // abstract type to a real type.
2380 Complex_expression::do_determine_type(const Type_context
* context
)
2382 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2384 else if (context
->type
!= NULL
2385 && context
->type
->complex_type() != NULL
)
2386 this->type_
= context
->type
;
2387 else if (!context
->may_be_abstract
)
2388 this->type_
= Type::lookup_complex_type("complex128");
2391 // Check the type of a complex value.
2394 Complex_expression::do_check_types(Gogo
*)
2396 Type
* type
= this->type_
;
2399 Numeric_constant nc
;
2400 nc
.set_complex(NULL
, this->val_
);
2401 if (!nc
.set_type(this->type_
, true, this->location()))
2402 this->set_is_error();
2405 // Get the backend representation for a complex constant.
2408 Complex_expression::do_get_backend(Translate_context
* context
)
2410 Type
* resolved_type
;
2411 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2412 resolved_type
= this->type_
;
2413 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2415 // We are converting to an abstract integer type.
2416 resolved_type
= Type::lookup_integer_type("int");
2418 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
2420 // We are converting to an abstract float type.
2421 resolved_type
= Type::lookup_float_type("float64");
2425 // If we still have an abstract type here, this this is being
2426 // used in a constant expression which didn't get reduced. We
2427 // just use complex128 and hope for the best.
2428 resolved_type
= Type::lookup_complex_type("complex128");
2431 Numeric_constant nc
;
2432 nc
.set_complex(resolved_type
, this->val_
);
2433 return Expression::backend_numeric_constant_expression(context
, &nc
);
2436 // Write REAL/IMAG to export data.
2439 Complex_expression::export_complex(String_dump
* exp
, const mpc_t val
)
2441 if (!mpfr_zero_p(mpc_realref(val
)))
2443 Float_expression::export_float(exp
, mpc_realref(val
));
2444 if (mpfr_sgn(mpc_imagref(val
)) > 0)
2445 exp
->write_c_string("+");
2447 Float_expression::export_float(exp
, mpc_imagref(val
));
2448 exp
->write_c_string("i");
2451 // Export a complex number in a constant expression.
2454 Complex_expression::do_export(Export
* exp
) const
2456 Complex_expression::export_complex(exp
, this->val_
);
2457 // A trailing space lets us reliably identify the end of the number.
2458 exp
->write_c_string(" ");
2461 // Dump a complex expression to the dump file.
2464 Complex_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2466 Complex_expression::export_complex(ast_dump_context
, this->val_
);
2469 // Make a complex expression.
2472 Expression::make_complex(const mpc_t
* val
, Type
* type
, Location location
)
2474 return new Complex_expression(val
, type
, location
);
2477 // Find a named object in an expression.
2479 class Find_named_object
: public Traverse
2482 Find_named_object(Named_object
* no
)
2483 : Traverse(traverse_expressions
),
2484 no_(no
), found_(false)
2487 // Whether we found the object.
2490 { return this->found_
; }
2494 expression(Expression
**);
2497 // The object we are looking for.
2499 // Whether we found it.
2503 // A reference to a const in an expression.
2505 class Const_expression
: public Expression
2508 Const_expression(Named_object
* constant
, Location location
)
2509 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2510 constant_(constant
), type_(NULL
), seen_(false)
2515 { return this->constant_
; }
2517 // Check that the initializer does not refer to the constant itself.
2519 check_for_init_loop();
2523 do_traverse(Traverse
*);
2526 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2529 do_is_constant() const
2533 do_is_immutable() const
2537 do_numeric_constant_value(Numeric_constant
* nc
) const;
2540 do_string_constant_value(std::string
* val
) const;
2545 // The type of a const is set by the declaration, not the use.
2547 do_determine_type(const Type_context
*);
2550 do_check_types(Gogo
*);
2557 do_get_backend(Translate_context
* context
);
2559 // When exporting a reference to a const as part of a const
2560 // expression, we export the value. We ignore the fact that it has
2563 do_export(Export
* exp
) const
2564 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2567 do_dump_expression(Ast_dump_context
*) const;
2571 Named_object
* constant_
;
2572 // The type of this reference. This is used if the constant has an
2575 // Used to prevent infinite recursion when a constant incorrectly
2576 // refers to itself.
2583 Const_expression::do_traverse(Traverse
* traverse
)
2585 if (this->type_
!= NULL
)
2586 return Type::traverse(this->type_
, traverse
);
2587 return TRAVERSE_CONTINUE
;
2590 // Lower a constant expression. This is where we convert the
2591 // predeclared constant iota into an integer value.
2594 Const_expression::do_lower(Gogo
* gogo
, Named_object
*,
2595 Statement_inserter
*, int iota_value
)
2597 if (this->constant_
->const_value()->expr()->classification()
2600 if (iota_value
== -1)
2602 error_at(this->location(),
2603 "iota is only defined in const declarations");
2606 return Expression::make_integer_ul(iota_value
, NULL
, this->location());
2609 // Make sure that the constant itself has been lowered.
2610 gogo
->lower_constant(this->constant_
);
2615 // Return a numeric constant value.
2618 Const_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
2623 Expression
* e
= this->constant_
->const_value()->expr();
2627 bool r
= e
->numeric_constant_value(nc
);
2629 this->seen_
= false;
2632 if (this->type_
!= NULL
)
2633 ctype
= this->type_
;
2635 ctype
= this->constant_
->const_value()->type();
2636 if (r
&& ctype
!= NULL
)
2638 if (!nc
->set_type(ctype
, false, this->location()))
2646 Const_expression::do_string_constant_value(std::string
* val
) const
2651 Expression
* e
= this->constant_
->const_value()->expr();
2654 bool ok
= e
->string_constant_value(val
);
2655 this->seen_
= false;
2660 // Return the type of the const reference.
2663 Const_expression::do_type()
2665 if (this->type_
!= NULL
)
2668 Named_constant
* nc
= this->constant_
->const_value();
2670 if (this->seen_
|| nc
->lowering())
2672 this->report_error(_("constant refers to itself"));
2673 this->type_
= Type::make_error_type();
2679 Type
* ret
= nc
->type();
2683 this->seen_
= false;
2687 // During parsing, a named constant may have a NULL type, but we
2688 // must not return a NULL type here.
2689 ret
= nc
->expr()->type();
2691 this->seen_
= false;
2696 // Set the type of the const reference.
2699 Const_expression::do_determine_type(const Type_context
* context
)
2701 Type
* ctype
= this->constant_
->const_value()->type();
2702 Type
* cetype
= (ctype
!= NULL
2704 : this->constant_
->const_value()->expr()->type());
2705 if (ctype
!= NULL
&& !ctype
->is_abstract())
2707 else if (context
->type
!= NULL
2708 && context
->type
->is_numeric_type()
2709 && cetype
->is_numeric_type())
2710 this->type_
= context
->type
;
2711 else if (context
->type
!= NULL
2712 && context
->type
->is_string_type()
2713 && cetype
->is_string_type())
2714 this->type_
= context
->type
;
2715 else if (context
->type
!= NULL
2716 && context
->type
->is_boolean_type()
2717 && cetype
->is_boolean_type())
2718 this->type_
= context
->type
;
2719 else if (!context
->may_be_abstract
)
2721 if (cetype
->is_abstract())
2722 cetype
= cetype
->make_non_abstract_type();
2723 this->type_
= cetype
;
2727 // Check for a loop in which the initializer of a constant refers to
2728 // the constant itself.
2731 Const_expression::check_for_init_loop()
2733 if (this->type_
!= NULL
&& this->type_
->is_error())
2738 this->report_error(_("constant refers to itself"));
2739 this->type_
= Type::make_error_type();
2743 Expression
* init
= this->constant_
->const_value()->expr();
2744 Find_named_object
find_named_object(this->constant_
);
2747 Expression::traverse(&init
, &find_named_object
);
2748 this->seen_
= false;
2750 if (find_named_object
.found())
2752 if (this->type_
== NULL
|| !this->type_
->is_error())
2754 this->report_error(_("constant refers to itself"));
2755 this->type_
= Type::make_error_type();
2761 // Check types of a const reference.
2764 Const_expression::do_check_types(Gogo
*)
2766 if (this->type_
!= NULL
&& this->type_
->is_error())
2769 this->check_for_init_loop();
2771 // Check that numeric constant fits in type.
2772 if (this->type_
!= NULL
&& this->type_
->is_numeric_type())
2774 Numeric_constant nc
;
2775 if (this->constant_
->const_value()->expr()->numeric_constant_value(&nc
))
2777 if (!nc
.set_type(this->type_
, true, this->location()))
2778 this->set_is_error();
2783 // Return the backend representation for a const reference.
2786 Const_expression::do_get_backend(Translate_context
* context
)
2788 if (this->type_
!= NULL
&& this->type_
->is_error())
2789 return context
->backend()->error_expression();
2791 // If the type has been set for this expression, but the underlying
2792 // object is an abstract int or float, we try to get the abstract
2793 // value. Otherwise we may lose something in the conversion.
2794 Expression
* expr
= this->constant_
->const_value()->expr();
2795 if (this->type_
!= NULL
2796 && this->type_
->is_numeric_type()
2797 && (this->constant_
->const_value()->type() == NULL
2798 || this->constant_
->const_value()->type()->is_abstract()))
2800 Numeric_constant nc
;
2801 if (expr
->numeric_constant_value(&nc
)
2802 && nc
.set_type(this->type_
, false, this->location()))
2804 Expression
* e
= nc
.expression(this->location());
2805 return e
->get_backend(context
);
2809 if (this->type_
!= NULL
)
2810 expr
= Expression::make_cast(this->type_
, expr
, this->location());
2811 return expr
->get_backend(context
);
2814 // Dump ast representation for constant expression.
2817 Const_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2819 ast_dump_context
->ostream() << this->constant_
->name();
2822 // Make a reference to a constant in an expression.
2825 Expression::make_const_reference(Named_object
* constant
,
2828 return new Const_expression(constant
, location
);
2831 // Find a named object in an expression.
2834 Find_named_object::expression(Expression
** pexpr
)
2836 switch ((*pexpr
)->classification())
2838 case Expression::EXPRESSION_CONST_REFERENCE
:
2840 Const_expression
* ce
= static_cast<Const_expression
*>(*pexpr
);
2841 if (ce
->named_object() == this->no_
)
2844 // We need to check a constant initializer explicitly, as
2845 // loops here will not be caught by the loop checking for
2846 // variable initializers.
2847 ce
->check_for_init_loop();
2849 return TRAVERSE_CONTINUE
;
2852 case Expression::EXPRESSION_VAR_REFERENCE
:
2853 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2855 return TRAVERSE_CONTINUE
;
2856 case Expression::EXPRESSION_FUNC_REFERENCE
:
2857 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2859 return TRAVERSE_CONTINUE
;
2861 return TRAVERSE_CONTINUE
;
2863 this->found_
= true;
2864 return TRAVERSE_EXIT
;
2869 class Nil_expression
: public Expression
2872 Nil_expression(Location location
)
2873 : Expression(EXPRESSION_NIL
, location
)
2881 do_is_constant() const
2885 do_is_immutable() const
2890 { return Type::make_nil_type(); }
2893 do_determine_type(const Type_context
*)
2901 do_get_backend(Translate_context
* context
)
2902 { return context
->backend()->nil_pointer_expression(); }
2905 do_export(Export
* exp
) const
2906 { exp
->write_c_string("nil"); }
2909 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2910 { ast_dump_context
->ostream() << "nil"; }
2913 // Import a nil expression.
2916 Nil_expression::do_import(Import
* imp
)
2918 imp
->require_c_string("nil");
2919 return Expression::make_nil(imp
->location());
2922 // Make a nil expression.
2925 Expression::make_nil(Location location
)
2927 return new Nil_expression(location
);
2930 // The value of the predeclared constant iota. This is little more
2931 // than a marker. This will be lowered to an integer in
2932 // Const_expression::do_lower, which is where we know the value that
2935 class Iota_expression
: public Parser_expression
2938 Iota_expression(Location location
)
2939 : Parser_expression(EXPRESSION_IOTA
, location
)
2944 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
2945 { go_unreachable(); }
2947 // There should only ever be one of these.
2950 { go_unreachable(); }
2953 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
2954 { ast_dump_context
->ostream() << "iota"; }
2957 // Make an iota expression. This is only called for one case: the
2958 // value of the predeclared constant iota.
2961 Expression::make_iota()
2963 static Iota_expression
iota_expression(Linemap::unknown_location());
2964 return &iota_expression
;
2967 // A type conversion expression.
2969 class Type_conversion_expression
: public Expression
2972 Type_conversion_expression(Type
* type
, Expression
* expr
,
2974 : Expression(EXPRESSION_CONVERSION
, location
),
2975 type_(type
), expr_(expr
), may_convert_function_types_(false)
2978 // Return the type to which we are converting.
2981 { return this->type_
; }
2983 // Return the expression which we are converting.
2986 { return this->expr_
; }
2988 // Permit converting from one function type to another. This is
2989 // used internally for method expressions.
2991 set_may_convert_function_types()
2993 this->may_convert_function_types_
= true;
2996 // Import a type conversion expression.
3002 do_traverse(Traverse
* traverse
);
3005 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
3008 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3011 do_is_constant() const;
3014 do_is_immutable() const;
3017 do_numeric_constant_value(Numeric_constant
*) const;
3020 do_string_constant_value(std::string
*) const;
3024 { return this->type_
; }
3027 do_determine_type(const Type_context
*)
3029 Type_context
subcontext(this->type_
, false);
3030 this->expr_
->determine_type(&subcontext
);
3034 do_check_types(Gogo
*);
3039 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
3044 do_get_backend(Translate_context
* context
);
3047 do_export(Export
*) const;
3050 do_dump_expression(Ast_dump_context
*) const;
3053 // The type to convert to.
3055 // The expression to convert.
3057 // True if this is permitted to convert function types. This is
3058 // used internally for method expressions.
3059 bool may_convert_function_types_
;
3065 Type_conversion_expression::do_traverse(Traverse
* traverse
)
3067 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3068 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3069 return TRAVERSE_EXIT
;
3070 return TRAVERSE_CONTINUE
;
3073 // Convert to a constant at lowering time.
3076 Type_conversion_expression::do_lower(Gogo
*, Named_object
*,
3077 Statement_inserter
*, int)
3079 Type
* type
= this->type_
;
3080 Expression
* val
= this->expr_
;
3081 Location location
= this->location();
3083 if (type
->is_numeric_type())
3085 Numeric_constant nc
;
3086 if (val
->numeric_constant_value(&nc
))
3088 if (!nc
.set_type(type
, true, location
))
3089 return Expression::make_error(location
);
3090 return nc
.expression(location
);
3094 if (type
->is_slice_type())
3096 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3097 bool is_byte
= (element_type
->integer_type() != NULL
3098 && element_type
->integer_type()->is_byte());
3099 bool is_rune
= (element_type
->integer_type() != NULL
3100 && element_type
->integer_type()->is_rune());
3101 if (is_byte
|| is_rune
)
3104 if (val
->string_constant_value(&s
))
3106 Expression_list
* vals
= new Expression_list();
3109 for (std::string::const_iterator p
= s
.begin();
3113 unsigned char c
= static_cast<unsigned char>(*p
);
3114 vals
->push_back(Expression::make_integer_ul(c
,
3121 const char *p
= s
.data();
3122 const char *pend
= s
.data() + s
.length();
3126 int adv
= Lex::fetch_char(p
, &c
);
3129 warning_at(this->location(), 0,
3130 "invalid UTF-8 encoding");
3134 vals
->push_back(Expression::make_integer_ul(c
,
3140 return Expression::make_slice_composite_literal(type
, vals
,
3149 // Flatten a type conversion by using a temporary variable for the slice
3150 // in slice to string conversions.
3153 Type_conversion_expression::do_flatten(Gogo
*, Named_object
*,
3154 Statement_inserter
* inserter
)
3156 if (((this->type()->is_string_type()
3157 && this->expr_
->type()->is_slice_type())
3158 || (this->type()->interface_type() != NULL
3159 && this->expr_
->type()->interface_type() != NULL
))
3160 && !this->expr_
->is_variable())
3162 Temporary_statement
* temp
=
3163 Statement::make_temporary(NULL
, this->expr_
, this->location());
3164 inserter
->insert(temp
);
3165 this->expr_
= Expression::make_temporary_reference(temp
, this->location());
3170 // Return whether a type conversion is a constant.
3173 Type_conversion_expression::do_is_constant() const
3175 if (!this->expr_
->is_constant())
3178 // A conversion to a type that may not be used as a constant is not
3179 // a constant. For example, []byte(nil).
3180 Type
* type
= this->type_
;
3181 if (type
->integer_type() == NULL
3182 && type
->float_type() == NULL
3183 && type
->complex_type() == NULL
3184 && !type
->is_boolean_type()
3185 && !type
->is_string_type())
3191 // Return whether a type conversion is immutable.
3194 Type_conversion_expression::do_is_immutable() const
3196 Type
* type
= this->type_
;
3197 Type
* expr_type
= this->expr_
->type();
3199 if (type
->interface_type() != NULL
3200 || expr_type
->interface_type() != NULL
)
3203 if (!this->expr_
->is_immutable())
3206 if (Type::are_identical(type
, expr_type
, false, NULL
))
3209 return type
->is_basic_type() && expr_type
->is_basic_type();
3212 // Return the constant numeric value if there is one.
3215 Type_conversion_expression::do_numeric_constant_value(
3216 Numeric_constant
* nc
) const
3218 if (!this->type_
->is_numeric_type())
3220 if (!this->expr_
->numeric_constant_value(nc
))
3222 return nc
->set_type(this->type_
, false, this->location());
3225 // Return the constant string value if there is one.
3228 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3230 if (this->type_
->is_string_type()
3231 && this->expr_
->type()->integer_type() != NULL
)
3233 Numeric_constant nc
;
3234 if (this->expr_
->numeric_constant_value(&nc
))
3237 if (nc
.to_unsigned_long(&ival
) == Numeric_constant::NC_UL_VALID
)
3240 Lex::append_char(ival
, true, val
, this->location());
3246 // FIXME: Could handle conversion from const []int here.
3251 // Check that types are convertible.
3254 Type_conversion_expression::do_check_types(Gogo
*)
3256 Type
* type
= this->type_
;
3257 Type
* expr_type
= this->expr_
->type();
3260 if (type
->is_error() || expr_type
->is_error())
3262 this->set_is_error();
3266 if (this->may_convert_function_types_
3267 && type
->function_type() != NULL
3268 && expr_type
->function_type() != NULL
)
3271 if (Type::are_convertible(type
, expr_type
, &reason
))
3274 error_at(this->location(), "%s", reason
.c_str());
3275 this->set_is_error();
3278 // Get the backend representation for a type conversion.
3281 Type_conversion_expression::do_get_backend(Translate_context
* context
)
3283 Type
* type
= this->type_
;
3284 Type
* expr_type
= this->expr_
->type();
3286 Gogo
* gogo
= context
->gogo();
3287 Btype
* btype
= type
->get_backend(gogo
);
3288 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
3289 Location loc
= this->location();
3291 if (Type::are_identical(type
, expr_type
, false, NULL
))
3292 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3293 else if (type
->interface_type() != NULL
3294 || expr_type
->interface_type() != NULL
)
3296 Expression
* conversion
=
3297 Expression::convert_for_assignment(gogo
, type
, this->expr_
,
3299 return conversion
->get_backend(context
);
3301 else if (type
->is_string_type()
3302 && expr_type
->integer_type() != NULL
)
3305 Numeric_constant nc
;
3306 if (this->expr_
->numeric_constant_value(&nc
)
3307 && nc
.to_int(&intval
)
3308 && mpz_fits_ushort_p(intval
))
3311 Lex::append_char(mpz_get_ui(intval
), true, &s
, loc
);
3313 Expression
* se
= Expression::make_string(s
, loc
);
3314 return se
->get_backend(context
);
3317 Expression
* i2s_expr
=
3318 Runtime::make_call(Runtime::INT_TO_STRING
, loc
, 1, this->expr_
);
3319 return Expression::make_cast(type
, i2s_expr
, loc
)->get_backend(context
);
3321 else if (type
->is_string_type() && expr_type
->is_slice_type())
3323 Array_type
* a
= expr_type
->array_type();
3324 Type
* e
= a
->element_type()->forwarded();
3325 go_assert(e
->integer_type() != NULL
);
3326 go_assert(this->expr_
->is_variable());
3328 Runtime::Function code
;
3329 if (e
->integer_type()->is_byte())
3330 code
= Runtime::BYTE_ARRAY_TO_STRING
;
3333 go_assert(e
->integer_type()->is_rune());
3334 code
= Runtime::INT_ARRAY_TO_STRING
;
3336 Expression
* valptr
= a
->get_value_pointer(gogo
, this->expr_
);
3337 Expression
* len
= a
->get_length(gogo
, this->expr_
);
3338 return Runtime::make_call(code
, loc
, 2, valptr
,
3339 len
)->get_backend(context
);
3341 else if (type
->is_slice_type() && expr_type
->is_string_type())
3343 Type
* e
= type
->array_type()->element_type()->forwarded();
3344 go_assert(e
->integer_type() != NULL
);
3346 Runtime::Function code
;
3347 if (e
->integer_type()->is_byte())
3348 code
= Runtime::STRING_TO_BYTE_ARRAY
;
3351 go_assert(e
->integer_type()->is_rune());
3352 code
= Runtime::STRING_TO_INT_ARRAY
;
3354 Expression
* s2a
= Runtime::make_call(code
, loc
, 1, this->expr_
);
3355 return Expression::make_unsafe_cast(type
, s2a
, loc
)->get_backend(context
);
3357 else if (type
->is_numeric_type())
3359 go_assert(Type::are_convertible(type
, expr_type
, NULL
));
3360 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3362 else if ((type
->is_unsafe_pointer_type()
3363 && (expr_type
->points_to() != NULL
3364 || expr_type
->integer_type()))
3365 || (expr_type
->is_unsafe_pointer_type()
3366 && type
->points_to() != NULL
)
3367 || (this->may_convert_function_types_
3368 && type
->function_type() != NULL
3369 && expr_type
->function_type() != NULL
))
3370 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3373 Expression
* conversion
=
3374 Expression::convert_for_assignment(gogo
, type
, this->expr_
, loc
);
3375 return conversion
->get_backend(context
);
3379 // Output a type conversion in a constant expression.
3382 Type_conversion_expression::do_export(Export
* exp
) const
3384 exp
->write_c_string("convert(");
3385 exp
->write_type(this->type_
);
3386 exp
->write_c_string(", ");
3387 this->expr_
->export_expression(exp
);
3388 exp
->write_c_string(")");
3391 // Import a type conversion or a struct construction.
3394 Type_conversion_expression::do_import(Import
* imp
)
3396 imp
->require_c_string("convert(");
3397 Type
* type
= imp
->read_type();
3398 imp
->require_c_string(", ");
3399 Expression
* val
= Expression::import_expression(imp
);
3400 imp
->require_c_string(")");
3401 return Expression::make_cast(type
, val
, imp
->location());
3404 // Dump ast representation for a type conversion expression.
3407 Type_conversion_expression::do_dump_expression(
3408 Ast_dump_context
* ast_dump_context
) const
3410 ast_dump_context
->dump_type(this->type_
);
3411 ast_dump_context
->ostream() << "(";
3412 ast_dump_context
->dump_expression(this->expr_
);
3413 ast_dump_context
->ostream() << ") ";
3416 // Make a type cast expression.
3419 Expression::make_cast(Type
* type
, Expression
* val
, Location location
)
3421 if (type
->is_error_type() || val
->is_error_expression())
3422 return Expression::make_error(location
);
3423 return new Type_conversion_expression(type
, val
, location
);
3426 // An unsafe type conversion, used to pass values to builtin functions.
3428 class Unsafe_type_conversion_expression
: public Expression
3431 Unsafe_type_conversion_expression(Type
* type
, Expression
* expr
,
3433 : Expression(EXPRESSION_UNSAFE_CONVERSION
, location
),
3434 type_(type
), expr_(expr
)
3439 do_traverse(Traverse
* traverse
);
3442 do_is_immutable() const;
3446 { return this->type_
; }
3449 do_determine_type(const Type_context
*)
3450 { this->expr_
->determine_type_no_context(); }
3455 return new Unsafe_type_conversion_expression(this->type_
,
3456 this->expr_
->copy(),
3461 do_get_backend(Translate_context
*);
3464 do_dump_expression(Ast_dump_context
*) const;
3467 // The type to convert to.
3469 // The expression to convert.
3476 Unsafe_type_conversion_expression::do_traverse(Traverse
* traverse
)
3478 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3479 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3480 return TRAVERSE_EXIT
;
3481 return TRAVERSE_CONTINUE
;
3484 // Return whether an unsafe type conversion is immutable.
3487 Unsafe_type_conversion_expression::do_is_immutable() const
3489 Type
* type
= this->type_
;
3490 Type
* expr_type
= this->expr_
->type();
3492 if (type
->interface_type() != NULL
3493 || expr_type
->interface_type() != NULL
)
3496 if (!this->expr_
->is_immutable())
3499 if (Type::are_convertible(type
, expr_type
, NULL
))
3502 return type
->is_basic_type() && expr_type
->is_basic_type();
3505 // Convert to backend representation.
3508 Unsafe_type_conversion_expression::do_get_backend(Translate_context
* context
)
3510 // We are only called for a limited number of cases.
3512 Type
* t
= this->type_
;
3513 Type
* et
= this->expr_
->type();
3514 if (t
->array_type() != NULL
)
3515 go_assert(et
->array_type() != NULL
3516 && t
->is_slice_type() == et
->is_slice_type());
3517 else if (t
->struct_type() != NULL
)
3519 if (t
->named_type() != NULL
3520 && et
->named_type() != NULL
3521 && !Type::are_convertible(t
, et
, NULL
))
3523 go_assert(saw_errors());
3524 return context
->backend()->error_expression();
3527 go_assert(et
->struct_type() != NULL
3528 && Type::are_convertible(t
, et
, NULL
));
3530 else if (t
->map_type() != NULL
)
3531 go_assert(et
->map_type() != NULL
);
3532 else if (t
->channel_type() != NULL
)
3533 go_assert(et
->channel_type() != NULL
);
3534 else if (t
->points_to() != NULL
)
3535 go_assert(et
->points_to() != NULL
3536 || et
->channel_type() != NULL
3537 || et
->map_type() != NULL
3538 || et
->function_type() != NULL
3539 || et
->is_nil_type());
3540 else if (et
->is_unsafe_pointer_type())
3541 go_assert(t
->points_to() != NULL
);
3542 else if (t
->interface_type() != NULL
)
3544 bool empty_iface
= t
->interface_type()->is_empty();
3545 go_assert(et
->interface_type() != NULL
3546 && et
->interface_type()->is_empty() == empty_iface
);
3548 else if (t
->integer_type() != NULL
)
3549 go_assert(et
->is_boolean_type()
3550 || et
->integer_type() != NULL
3551 || et
->function_type() != NULL
3552 || et
->points_to() != NULL
3553 || et
->map_type() != NULL
3554 || et
->channel_type() != NULL
);
3558 Gogo
* gogo
= context
->gogo();
3559 Btype
* btype
= t
->get_backend(gogo
);
3560 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
3561 Location loc
= this->location();
3562 return gogo
->backend()->convert_expression(btype
, bexpr
, loc
);
3565 // Dump ast representation for an unsafe type conversion expression.
3568 Unsafe_type_conversion_expression::do_dump_expression(
3569 Ast_dump_context
* ast_dump_context
) const
3571 ast_dump_context
->dump_type(this->type_
);
3572 ast_dump_context
->ostream() << "(";
3573 ast_dump_context
->dump_expression(this->expr_
);
3574 ast_dump_context
->ostream() << ") ";
3577 // Make an unsafe type conversion expression.
3580 Expression::make_unsafe_cast(Type
* type
, Expression
* expr
,
3583 return new Unsafe_type_conversion_expression(type
, expr
, location
);
3586 // Class Unary_expression.
3588 // If we are taking the address of a composite literal, and the
3589 // contents are not constant, then we want to make a heap expression
3593 Unary_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
3595 Location loc
= this->location();
3596 Operator op
= this->op_
;
3597 Expression
* expr
= this->expr_
;
3599 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3600 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3602 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3603 // moving x to the heap. FIXME: Is it worth doing a real escape
3604 // analysis here? This case is found in math/unsafe.go and is
3605 // therefore worth special casing.
3606 if (op
== OPERATOR_MULT
)
3608 Expression
* e
= expr
;
3609 while (e
->classification() == EXPRESSION_CONVERSION
)
3611 Type_conversion_expression
* te
3612 = static_cast<Type_conversion_expression
*>(e
);
3616 if (e
->classification() == EXPRESSION_UNARY
)
3618 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3619 if (ue
->op_
== OPERATOR_AND
)
3624 if (!ue
->expr_
->is_addressable() && !ue
->create_temp_
)
3626 error_at(ue
->location(),
3627 "invalid operand for unary %<&%>");
3628 this->set_is_error();
3632 ue
->set_does_not_escape();
3637 // Catching an invalid indirection of unsafe.Pointer here avoid
3638 // having to deal with TYPE_VOID in other places.
3639 if (op
== OPERATOR_MULT
&& expr
->type()->is_unsafe_pointer_type())
3641 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3642 return Expression::make_error(this->location());
3645 // Check for an invalid pointer dereference. We need to do this
3646 // here because Unary_expression::do_type will return an error type
3647 // in this case. That can cause code to appear erroneous, and
3648 // therefore disappear at lowering time, without any error message.
3649 if (op
== OPERATOR_MULT
&& expr
->type()->points_to() == NULL
)
3651 this->report_error(_("expected pointer"));
3652 return Expression::make_error(this->location());
3655 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
|| op
== OPERATOR_XOR
)
3657 Numeric_constant nc
;
3658 if (expr
->numeric_constant_value(&nc
))
3660 Numeric_constant result
;
3661 if (Unary_expression::eval_constant(op
, &nc
, loc
, &result
))
3662 return result
.expression(loc
);
3669 // Flatten expression if a nil check must be performed and create temporary
3670 // variables if necessary.
3673 Unary_expression::do_flatten(Gogo
* gogo
, Named_object
*,
3674 Statement_inserter
* inserter
)
3676 if (this->is_error_expression() || this->expr_
->is_error_expression())
3677 return Expression::make_error(this->location());
3679 Location location
= this->location();
3680 if (this->op_
== OPERATOR_MULT
3681 && !this->expr_
->is_variable())
3683 go_assert(this->expr_
->type()->points_to() != NULL
);
3684 Type
* ptype
= this->expr_
->type()->points_to();
3685 if (!ptype
->is_void_type())
3687 Btype
* pbtype
= ptype
->get_backend(gogo
);
3688 size_t s
= gogo
->backend()->type_size(pbtype
);
3689 if (s
>= 4096 || this->issue_nil_check_
)
3691 Temporary_statement
* temp
=
3692 Statement::make_temporary(NULL
, this->expr_
, location
);
3693 inserter
->insert(temp
);
3695 Expression::make_temporary_reference(temp
, location
);
3700 if (this->create_temp_
&& !this->expr_
->is_variable())
3702 Temporary_statement
* temp
=
3703 Statement::make_temporary(NULL
, this->expr_
, location
);
3704 inserter
->insert(temp
);
3705 this->expr_
= Expression::make_temporary_reference(temp
, location
);
3711 // Return whether a unary expression is a constant.
3714 Unary_expression::do_is_constant() const
3716 if (this->op_
== OPERATOR_MULT
)
3718 // Indirecting through a pointer is only constant if the object
3719 // to which the expression points is constant, but we currently
3720 // have no way to determine that.
3723 else if (this->op_
== OPERATOR_AND
)
3725 // Taking the address of a variable is constant if it is a
3726 // global variable, not constant otherwise. In other cases taking the
3727 // address is probably not a constant.
3728 Var_expression
* ve
= this->expr_
->var_expression();
3731 Named_object
* no
= ve
->named_object();
3732 return no
->is_variable() && no
->var_value()->is_global();
3737 return this->expr_
->is_constant();
3740 // Apply unary opcode OP to UNC, setting NC. Return true if this
3741 // could be done, false if not. Issue errors for overflow.
3744 Unary_expression::eval_constant(Operator op
, const Numeric_constant
* unc
,
3745 Location location
, Numeric_constant
* nc
)
3753 case OPERATOR_MINUS
:
3754 if (unc
->is_int() || unc
->is_rune())
3756 else if (unc
->is_float())
3759 unc
->get_float(&uval
);
3762 mpfr_neg(val
, uval
, GMP_RNDN
);
3763 nc
->set_float(unc
->type(), val
);
3768 else if (unc
->is_complex())
3771 unc
->get_complex(&uval
);
3773 mpc_init2(val
, mpc_precision
);
3774 mpc_neg(val
, uval
, MPC_RNDNN
);
3775 nc
->set_complex(unc
->type(), val
);
3795 if (!unc
->is_int() && !unc
->is_rune())
3800 unc
->get_rune(&uval
);
3802 unc
->get_int(&uval
);
3808 case OPERATOR_MINUS
:
3813 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3818 Type
* utype
= unc
->type();
3819 if (utype
->integer_type() == NULL
3820 || utype
->integer_type()->is_abstract())
3824 // The number of HOST_WIDE_INTs that it takes to represent
3826 size_t count
= ((mpz_sizeinbase(uval
, 2)
3827 + HOST_BITS_PER_WIDE_INT
3829 / HOST_BITS_PER_WIDE_INT
);
3831 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3832 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3834 size_t obits
= utype
->integer_type()->bits();
3836 if (!utype
->integer_type()->is_unsigned() && mpz_sgn(uval
) < 0)
3839 mpz_init_set_ui(adj
, 1);
3840 mpz_mul_2exp(adj
, adj
, obits
);
3841 mpz_add(uval
, uval
, adj
);
3846 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3847 go_assert(ecount
<= count
);
3849 // Trim down to the number of words required by the type.
3850 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3851 / HOST_BITS_PER_WIDE_INT
);
3852 go_assert(ocount
<= count
);
3854 for (size_t i
= 0; i
< ocount
; ++i
)
3857 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3859 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3862 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3864 if (!utype
->integer_type()->is_unsigned()
3865 && mpz_tstbit(val
, obits
- 1))
3868 mpz_init_set_ui(adj
, 1);
3869 mpz_mul_2exp(adj
, adj
, obits
);
3870 mpz_sub(val
, val
, adj
);
3884 nc
->set_rune(NULL
, val
);
3886 nc
->set_int(NULL
, val
);
3891 return nc
->set_type(unc
->type(), true, location
);
3894 // Return the integral constant value of a unary expression, if it has one.
3897 Unary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
3899 Numeric_constant unc
;
3900 if (!this->expr_
->numeric_constant_value(&unc
))
3902 return Unary_expression::eval_constant(this->op_
, &unc
, this->location(),
3906 // Return the type of a unary expression.
3909 Unary_expression::do_type()
3914 case OPERATOR_MINUS
:
3917 return this->expr_
->type();
3920 return Type::make_pointer_type(this->expr_
->type());
3924 Type
* subtype
= this->expr_
->type();
3925 Type
* points_to
= subtype
->points_to();
3926 if (points_to
== NULL
)
3927 return Type::make_error_type();
3936 // Determine abstract types for a unary expression.
3939 Unary_expression::do_determine_type(const Type_context
* context
)
3944 case OPERATOR_MINUS
:
3947 this->expr_
->determine_type(context
);
3951 // Taking the address of something.
3953 Type
* subtype
= (context
->type
== NULL
3955 : context
->type
->points_to());
3956 Type_context
subcontext(subtype
, false);
3957 this->expr_
->determine_type(&subcontext
);
3962 // Indirecting through a pointer.
3964 Type
* subtype
= (context
->type
== NULL
3966 : Type::make_pointer_type(context
->type
));
3967 Type_context
subcontext(subtype
, false);
3968 this->expr_
->determine_type(&subcontext
);
3977 // Check types for a unary expression.
3980 Unary_expression::do_check_types(Gogo
*)
3982 Type
* type
= this->expr_
->type();
3983 if (type
->is_error())
3985 this->set_is_error();
3992 case OPERATOR_MINUS
:
3993 if (type
->integer_type() == NULL
3994 && type
->float_type() == NULL
3995 && type
->complex_type() == NULL
)
3996 this->report_error(_("expected numeric type"));
4000 if (!type
->is_boolean_type())
4001 this->report_error(_("expected boolean type"));
4005 if (type
->integer_type() == NULL
4006 && !type
->is_boolean_type())
4007 this->report_error(_("expected integer or boolean type"));
4011 if (!this->expr_
->is_addressable())
4013 if (!this->create_temp_
)
4015 error_at(this->location(), "invalid operand for unary %<&%>");
4016 this->set_is_error();
4021 this->expr_
->address_taken(this->escapes_
);
4022 this->expr_
->issue_nil_check();
4027 // Indirecting through a pointer.
4028 if (type
->points_to() == NULL
)
4029 this->report_error(_("expected pointer"));
4037 // Get the backend representation for a unary expression.
4040 Unary_expression::do_get_backend(Translate_context
* context
)
4042 Gogo
* gogo
= context
->gogo();
4043 Location loc
= this->location();
4045 // Taking the address of a set-and-use-temporary expression requires
4046 // setting the temporary and then taking the address.
4047 if (this->op_
== OPERATOR_AND
)
4049 Set_and_use_temporary_expression
* sut
=
4050 this->expr_
->set_and_use_temporary_expression();
4053 Temporary_statement
* temp
= sut
->temporary();
4054 Bvariable
* bvar
= temp
->get_backend_variable(context
);
4055 Bexpression
* bvar_expr
= gogo
->backend()->var_expression(bvar
, loc
);
4056 Bexpression
* bval
= sut
->expression()->get_backend(context
);
4058 Bstatement
* bassign
=
4059 gogo
->backend()->assignment_statement(bvar_expr
, bval
, loc
);
4060 Bexpression
* bvar_addr
=
4061 gogo
->backend()->address_expression(bvar_expr
, loc
);
4062 return gogo
->backend()->compound_expression(bassign
, bvar_addr
, loc
);
4067 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
4068 Btype
* btype
= this->expr_
->type()->get_backend(gogo
);
4075 case OPERATOR_MINUS
:
4076 ret
= gogo
->backend()->unary_expression(this->op_
, bexpr
, loc
);
4077 ret
= gogo
->backend()->convert_expression(btype
, ret
, loc
);
4082 ret
= gogo
->backend()->unary_expression(this->op_
, bexpr
, loc
);
4086 if (!this->create_temp_
)
4088 // We should not see a non-constant constructor here; cases
4089 // where we would see one should have been moved onto the
4090 // heap at parse time. Taking the address of a nonconstant
4091 // constructor will not do what the programmer expects.
4093 go_assert(!this->expr_
->is_composite_literal()
4094 || this->expr_
->is_immutable());
4095 if (this->expr_
->classification() == EXPRESSION_UNARY
)
4097 Unary_expression
* ue
=
4098 static_cast<Unary_expression
*>(this->expr_
);
4099 go_assert(ue
->op() != OPERATOR_AND
);
4103 static unsigned int counter
;
4105 if (this->is_gc_root_
|| this->is_slice_init_
)
4107 bool copy_to_heap
= false;
4108 if (this->is_gc_root_
)
4110 // Build a decl for a GC root variable. GC roots are mutable, so
4111 // they cannot be represented as an immutable_struct in the
4113 static unsigned int root_counter
;
4114 snprintf(buf
, sizeof buf
, "gc%u", root_counter
);
4119 // Build a decl for a slice value initializer. An immutable slice
4120 // value initializer may have to be copied to the heap if it
4121 // contains pointers in a non-constant context.
4122 snprintf(buf
, sizeof buf
, "C%u", counter
);
4125 Array_type
* at
= this->expr_
->type()->array_type();
4126 go_assert(at
!= NULL
);
4128 // If we are not copying the value to the heap, we will only
4129 // initialize the value once, so we can use this directly
4130 // rather than copying it. In that case we can't make it
4131 // read-only, because the program is permitted to change it.
4132 copy_to_heap
= (at
->element_type()->has_pointer()
4133 && !context
->is_const());
4135 Bvariable
* implicit
=
4136 gogo
->backend()->implicit_variable(buf
, btype
, true, copy_to_heap
,
4138 gogo
->backend()->implicit_variable_set_init(implicit
, buf
, btype
,
4139 true, copy_to_heap
, false,
4141 bexpr
= gogo
->backend()->var_expression(implicit
, loc
);
4143 else if ((this->expr_
->is_composite_literal()
4144 || this->expr_
->string_expression() != NULL
)
4145 && this->expr_
->is_immutable())
4147 // Build a decl for a constant constructor.
4148 snprintf(buf
, sizeof buf
, "C%u", counter
);
4152 gogo
->backend()->immutable_struct(buf
, true, false, btype
, loc
);
4153 gogo
->backend()->immutable_struct_set_init(decl
, buf
, true, false,
4155 bexpr
= gogo
->backend()->var_expression(decl
, loc
);
4158 go_assert(!this->create_temp_
|| this->expr_
->is_variable());
4159 ret
= gogo
->backend()->address_expression(bexpr
, loc
);
4164 go_assert(this->expr_
->type()->points_to() != NULL
);
4166 // If we are dereferencing the pointer to a large struct, we
4167 // need to check for nil. We don't bother to check for small
4168 // structs because we expect the system to crash on a nil
4169 // pointer dereference. However, if we know the address of this
4170 // expression is being taken, we must always check for nil.
4172 Type
* ptype
= this->expr_
->type()->points_to();
4173 Btype
* pbtype
= ptype
->get_backend(gogo
);
4174 if (!ptype
->is_void_type())
4176 size_t s
= gogo
->backend()->type_size(pbtype
);
4177 if (s
>= 4096 || this->issue_nil_check_
)
4179 go_assert(this->expr_
->is_variable());
4181 Expression::make_nil(loc
)->get_backend(context
);
4182 Bexpression
* compare
=
4183 gogo
->backend()->binary_expression(OPERATOR_EQEQ
, bexpr
,
4185 Bexpression
* crash
=
4186 gogo
->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4187 loc
)->get_backend(context
);
4188 bexpr
= gogo
->backend()->conditional_expression(btype
, compare
,
4194 ret
= gogo
->backend()->indirect_expression(pbtype
, bexpr
, false, loc
);
4205 // Export a unary expression.
4208 Unary_expression::do_export(Export
* exp
) const
4213 exp
->write_c_string("+ ");
4215 case OPERATOR_MINUS
:
4216 exp
->write_c_string("- ");
4219 exp
->write_c_string("! ");
4222 exp
->write_c_string("^ ");
4229 this->expr_
->export_expression(exp
);
4232 // Import a unary expression.
4235 Unary_expression::do_import(Import
* imp
)
4238 switch (imp
->get_char())
4244 op
= OPERATOR_MINUS
;
4255 imp
->require_c_string(" ");
4256 Expression
* expr
= Expression::import_expression(imp
);
4257 return Expression::make_unary(op
, expr
, imp
->location());
4260 // Dump ast representation of an unary expression.
4263 Unary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
4265 ast_dump_context
->dump_operator(this->op_
);
4266 ast_dump_context
->ostream() << "(";
4267 ast_dump_context
->dump_expression(this->expr_
);
4268 ast_dump_context
->ostream() << ") ";
4271 // Make a unary expression.
4274 Expression::make_unary(Operator op
, Expression
* expr
, Location location
)
4276 return new Unary_expression(op
, expr
, location
);
4279 // If this is an indirection through a pointer, return the expression
4280 // being pointed through. Otherwise return this.
4285 if (this->classification_
== EXPRESSION_UNARY
)
4287 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4288 if (ue
->op() == OPERATOR_MULT
)
4289 return ue
->operand();
4294 // Class Binary_expression.
4299 Binary_expression::do_traverse(Traverse
* traverse
)
4301 int t
= Expression::traverse(&this->left_
, traverse
);
4302 if (t
== TRAVERSE_EXIT
)
4303 return TRAVERSE_EXIT
;
4304 return Expression::traverse(&this->right_
, traverse
);
4307 // Return the type to use for a binary operation on operands of
4308 // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
4309 // such may be NULL or abstract.
4312 Binary_expression::operation_type(Operator op
, Type
* left_type
,
4313 Type
* right_type
, Type
** result_type
)
4315 if (left_type
!= right_type
4316 && !left_type
->is_abstract()
4317 && !right_type
->is_abstract()
4318 && left_type
->base() != right_type
->base()
4319 && op
!= OPERATOR_LSHIFT
4320 && op
!= OPERATOR_RSHIFT
)
4322 // May be a type error--let it be diagnosed elsewhere.
4326 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4328 if (left_type
->integer_type() != NULL
)
4329 *result_type
= left_type
;
4331 *result_type
= Type::make_abstract_integer_type();
4333 else if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
4334 *result_type
= left_type
;
4335 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
4336 *result_type
= right_type
;
4337 else if (!left_type
->is_abstract())
4338 *result_type
= left_type
;
4339 else if (!right_type
->is_abstract())
4340 *result_type
= right_type
;
4341 else if (left_type
->complex_type() != NULL
)
4342 *result_type
= left_type
;
4343 else if (right_type
->complex_type() != NULL
)
4344 *result_type
= right_type
;
4345 else if (left_type
->float_type() != NULL
)
4346 *result_type
= left_type
;
4347 else if (right_type
->float_type() != NULL
)
4348 *result_type
= right_type
;
4349 else if (left_type
->integer_type() != NULL
4350 && left_type
->integer_type()->is_rune())
4351 *result_type
= left_type
;
4352 else if (right_type
->integer_type() != NULL
4353 && right_type
->integer_type()->is_rune())
4354 *result_type
= right_type
;
4356 *result_type
= left_type
;
4361 // Convert an integer comparison code and an operator to a boolean
4365 Binary_expression::cmp_to_bool(Operator op
, int cmp
)
4372 case OPERATOR_NOTEQ
:
4389 // Compare constants according to OP.
4392 Binary_expression::compare_constant(Operator op
, Numeric_constant
* left_nc
,
4393 Numeric_constant
* right_nc
,
4394 Location location
, bool* result
)
4396 Type
* left_type
= left_nc
->type();
4397 Type
* right_type
= right_nc
->type();
4400 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
4403 // When comparing an untyped operand to a typed operand, we are
4404 // effectively coercing the untyped operand to the other operand's
4405 // type, so make sure that is valid.
4406 if (!left_nc
->set_type(type
, true, location
)
4407 || !right_nc
->set_type(type
, true, location
))
4412 if (type
->complex_type() != NULL
)
4414 if (op
!= OPERATOR_EQEQ
&& op
!= OPERATOR_NOTEQ
)
4416 ret
= Binary_expression::compare_complex(left_nc
, right_nc
, &cmp
);
4418 else if (type
->float_type() != NULL
)
4419 ret
= Binary_expression::compare_float(left_nc
, right_nc
, &cmp
);
4421 ret
= Binary_expression::compare_integer(left_nc
, right_nc
, &cmp
);
4424 *result
= Binary_expression::cmp_to_bool(op
, cmp
);
4429 // Compare integer constants.
4432 Binary_expression::compare_integer(const Numeric_constant
* left_nc
,
4433 const Numeric_constant
* right_nc
,
4437 if (!left_nc
->to_int(&left_val
))
4440 if (!right_nc
->to_int(&right_val
))
4442 mpz_clear(left_val
);
4446 *cmp
= mpz_cmp(left_val
, right_val
);
4448 mpz_clear(left_val
);
4449 mpz_clear(right_val
);
4454 // Compare floating point constants.
4457 Binary_expression::compare_float(const Numeric_constant
* left_nc
,
4458 const Numeric_constant
* right_nc
,
4462 if (!left_nc
->to_float(&left_val
))
4465 if (!right_nc
->to_float(&right_val
))
4467 mpfr_clear(left_val
);
4471 // We already coerced both operands to the same type. If that type
4472 // is not an abstract type, we need to round the values accordingly.
4473 Type
* type
= left_nc
->type();
4474 if (!type
->is_abstract() && type
->float_type() != NULL
)
4476 int bits
= type
->float_type()->bits();
4477 mpfr_prec_round(left_val
, bits
, GMP_RNDN
);
4478 mpfr_prec_round(right_val
, bits
, GMP_RNDN
);
4481 *cmp
= mpfr_cmp(left_val
, right_val
);
4483 mpfr_clear(left_val
);
4484 mpfr_clear(right_val
);
4489 // Compare complex constants. Complex numbers may only be compared
4493 Binary_expression::compare_complex(const Numeric_constant
* left_nc
,
4494 const Numeric_constant
* right_nc
,
4498 if (!left_nc
->to_complex(&left_val
))
4501 if (!right_nc
->to_complex(&right_val
))
4503 mpc_clear(left_val
);
4507 // We already coerced both operands to the same type. If that type
4508 // is not an abstract type, we need to round the values accordingly.
4509 Type
* type
= left_nc
->type();
4510 if (!type
->is_abstract() && type
->complex_type() != NULL
)
4512 int bits
= type
->complex_type()->bits();
4513 mpfr_prec_round(mpc_realref(left_val
), bits
/ 2, GMP_RNDN
);
4514 mpfr_prec_round(mpc_imagref(left_val
), bits
/ 2, GMP_RNDN
);
4515 mpfr_prec_round(mpc_realref(right_val
), bits
/ 2, GMP_RNDN
);
4516 mpfr_prec_round(mpc_imagref(right_val
), bits
/ 2, GMP_RNDN
);
4519 *cmp
= mpc_cmp(left_val
, right_val
) != 0;
4521 mpc_clear(left_val
);
4522 mpc_clear(right_val
);
4527 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
4528 // true if this could be done, false if not. Issue errors at LOCATION
4532 Binary_expression::eval_constant(Operator op
, Numeric_constant
* left_nc
,
4533 Numeric_constant
* right_nc
,
4534 Location location
, Numeric_constant
* nc
)
4539 case OPERATOR_ANDAND
:
4541 case OPERATOR_NOTEQ
:
4546 // These return boolean values, not numeric.
4552 Type
* left_type
= left_nc
->type();
4553 Type
* right_type
= right_nc
->type();
4556 if (!Binary_expression::operation_type(op
, left_type
, right_type
, &type
))
4559 bool is_shift
= op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
;
4561 // When combining an untyped operand with a typed operand, we are
4562 // effectively coercing the untyped operand to the other operand's
4563 // type, so make sure that is valid.
4564 if (!left_nc
->set_type(type
, true, location
))
4566 if (!is_shift
&& !right_nc
->set_type(type
, true, location
))
4570 if (type
->complex_type() != NULL
)
4571 r
= Binary_expression::eval_complex(op
, left_nc
, right_nc
, location
, nc
);
4572 else if (type
->float_type() != NULL
)
4573 r
= Binary_expression::eval_float(op
, left_nc
, right_nc
, location
, nc
);
4575 r
= Binary_expression::eval_integer(op
, left_nc
, right_nc
, location
, nc
);
4578 r
= nc
->set_type(type
, true, location
);
4583 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4584 // integer operations. Return true if this could be done, false if
4588 Binary_expression::eval_integer(Operator op
, const Numeric_constant
* left_nc
,
4589 const Numeric_constant
* right_nc
,
4590 Location location
, Numeric_constant
* nc
)
4593 if (!left_nc
->to_int(&left_val
))
4596 if (!right_nc
->to_int(&right_val
))
4598 mpz_clear(left_val
);
4608 mpz_add(val
, left_val
, right_val
);
4609 if (mpz_sizeinbase(val
, 2) > 0x100000)
4611 error_at(location
, "constant addition overflow");
4615 case OPERATOR_MINUS
:
4616 mpz_sub(val
, left_val
, right_val
);
4617 if (mpz_sizeinbase(val
, 2) > 0x100000)
4619 error_at(location
, "constant subtraction overflow");
4624 mpz_ior(val
, left_val
, right_val
);
4627 mpz_xor(val
, left_val
, right_val
);
4630 mpz_mul(val
, left_val
, right_val
);
4631 if (mpz_sizeinbase(val
, 2) > 0x100000)
4633 error_at(location
, "constant multiplication overflow");
4638 if (mpz_sgn(right_val
) != 0)
4639 mpz_tdiv_q(val
, left_val
, right_val
);
4642 error_at(location
, "division by zero");
4647 if (mpz_sgn(right_val
) != 0)
4648 mpz_tdiv_r(val
, left_val
, right_val
);
4651 error_at(location
, "division by zero");
4655 case OPERATOR_LSHIFT
:
4657 unsigned long shift
= mpz_get_ui(right_val
);
4658 if (mpz_cmp_ui(right_val
, shift
) == 0 && shift
<= 0x100000)
4659 mpz_mul_2exp(val
, left_val
, shift
);
4662 error_at(location
, "shift count overflow");
4668 case OPERATOR_RSHIFT
:
4670 unsigned long shift
= mpz_get_ui(right_val
);
4671 if (mpz_cmp_ui(right_val
, shift
) != 0)
4673 error_at(location
, "shift count overflow");
4678 if (mpz_cmp_ui(left_val
, 0) >= 0)
4679 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4681 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4687 mpz_and(val
, left_val
, right_val
);
4689 case OPERATOR_BITCLEAR
:
4693 mpz_com(tval
, right_val
);
4694 mpz_and(val
, left_val
, tval
);
4702 mpz_clear(left_val
);
4703 mpz_clear(right_val
);
4705 if (left_nc
->is_rune()
4706 || (op
!= OPERATOR_LSHIFT
4707 && op
!= OPERATOR_RSHIFT
4708 && right_nc
->is_rune()))
4709 nc
->set_rune(NULL
, val
);
4711 nc
->set_int(NULL
, val
);
4718 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4719 // floating point operations. Return true if this could be done,
4723 Binary_expression::eval_float(Operator op
, const Numeric_constant
* left_nc
,
4724 const Numeric_constant
* right_nc
,
4725 Location location
, Numeric_constant
* nc
)
4728 if (!left_nc
->to_float(&left_val
))
4731 if (!right_nc
->to_float(&right_val
))
4733 mpfr_clear(left_val
);
4744 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4746 case OPERATOR_MINUS
:
4747 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4752 case OPERATOR_BITCLEAR
:
4754 case OPERATOR_LSHIFT
:
4755 case OPERATOR_RSHIFT
:
4756 mpfr_set_ui(val
, 0, GMP_RNDN
);
4760 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4763 if (!mpfr_zero_p(right_val
))
4764 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4767 error_at(location
, "division by zero");
4768 mpfr_set_ui(val
, 0, GMP_RNDN
);
4775 mpfr_clear(left_val
);
4776 mpfr_clear(right_val
);
4778 nc
->set_float(NULL
, val
);
4784 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4785 // complex operations. Return true if this could be done, false if
4789 Binary_expression::eval_complex(Operator op
, const Numeric_constant
* left_nc
,
4790 const Numeric_constant
* right_nc
,
4791 Location location
, Numeric_constant
* nc
)
4794 if (!left_nc
->to_complex(&left_val
))
4797 if (!right_nc
->to_complex(&right_val
))
4799 mpc_clear(left_val
);
4804 mpc_init2(val
, mpc_precision
);
4810 mpc_add(val
, left_val
, right_val
, MPC_RNDNN
);
4812 case OPERATOR_MINUS
:
4813 mpc_sub(val
, left_val
, right_val
, MPC_RNDNN
);
4818 case OPERATOR_BITCLEAR
:
4820 case OPERATOR_LSHIFT
:
4821 case OPERATOR_RSHIFT
:
4822 mpc_set_ui(val
, 0, MPC_RNDNN
);
4826 mpc_mul(val
, left_val
, right_val
, MPC_RNDNN
);
4829 if (mpc_cmp_si(right_val
, 0) == 0)
4831 error_at(location
, "division by zero");
4832 mpc_set_ui(val
, 0, MPC_RNDNN
);
4835 mpc_div(val
, left_val
, right_val
, MPC_RNDNN
);
4841 mpc_clear(left_val
);
4842 mpc_clear(right_val
);
4844 nc
->set_complex(NULL
, val
);
4850 // Lower a binary expression. We have to evaluate constant
4851 // expressions now, in order to implement Go's unlimited precision
4855 Binary_expression::do_lower(Gogo
* gogo
, Named_object
*,
4856 Statement_inserter
* inserter
, int)
4858 Location location
= this->location();
4859 Operator op
= this->op_
;
4860 Expression
* left
= this->left_
;
4861 Expression
* right
= this->right_
;
4863 const bool is_comparison
= (op
== OPERATOR_EQEQ
4864 || op
== OPERATOR_NOTEQ
4865 || op
== OPERATOR_LT
4866 || op
== OPERATOR_LE
4867 || op
== OPERATOR_GT
4868 || op
== OPERATOR_GE
);
4870 // Numeric constant expressions.
4872 Numeric_constant left_nc
;
4873 Numeric_constant right_nc
;
4874 if (left
->numeric_constant_value(&left_nc
)
4875 && right
->numeric_constant_value(&right_nc
))
4880 if (!Binary_expression::compare_constant(op
, &left_nc
,
4881 &right_nc
, location
,
4884 return Expression::make_cast(Type::make_boolean_type(),
4885 Expression::make_boolean(result
,
4891 Numeric_constant nc
;
4892 if (!Binary_expression::eval_constant(op
, &left_nc
, &right_nc
,
4895 return nc
.expression(location
);
4900 // String constant expressions.
4901 if (left
->type()->is_string_type() && right
->type()->is_string_type())
4903 std::string left_string
;
4904 std::string right_string
;
4905 if (left
->string_constant_value(&left_string
)
4906 && right
->string_constant_value(&right_string
))
4908 if (op
== OPERATOR_PLUS
)
4909 return Expression::make_string(left_string
+ right_string
,
4911 else if (is_comparison
)
4913 int cmp
= left_string
.compare(right_string
);
4914 bool r
= Binary_expression::cmp_to_bool(op
, cmp
);
4915 return Expression::make_boolean(r
, location
);
4920 // Lower struct, array, and some interface comparisons.
4921 if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
4923 if (left
->type()->struct_type() != NULL
4924 && right
->type()->struct_type() != NULL
)
4925 return this->lower_struct_comparison(gogo
, inserter
);
4926 else if (left
->type()->array_type() != NULL
4927 && !left
->type()->is_slice_type()
4928 && right
->type()->array_type() != NULL
4929 && !right
->type()->is_slice_type())
4930 return this->lower_array_comparison(gogo
, inserter
);
4931 else if ((left
->type()->interface_type() != NULL
4932 && right
->type()->interface_type() == NULL
)
4933 || (left
->type()->interface_type() == NULL
4934 && right
->type()->interface_type() != NULL
))
4935 return this->lower_interface_value_comparison(gogo
, inserter
);
4941 // Lower a struct comparison.
4944 Binary_expression::lower_struct_comparison(Gogo
* gogo
,
4945 Statement_inserter
* inserter
)
4947 Struct_type
* st
= this->left_
->type()->struct_type();
4948 Struct_type
* st2
= this->right_
->type()->struct_type();
4951 if (st
!= st2
&& !Type::are_identical(st
, st2
, false, NULL
))
4953 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
4954 this->right_
->type(), NULL
))
4957 // See if we can compare using memcmp. As a heuristic, we use
4958 // memcmp rather than field references and comparisons if there are
4959 // more than two fields.
4960 if (st
->compare_is_identity(gogo
) && st
->total_field_count() > 2)
4961 return this->lower_compare_to_memcmp(gogo
, inserter
);
4963 Location loc
= this->location();
4965 Expression
* left
= this->left_
;
4966 Temporary_statement
* left_temp
= NULL
;
4967 if (left
->var_expression() == NULL
4968 && left
->temporary_reference_expression() == NULL
)
4970 left_temp
= Statement::make_temporary(left
->type(), NULL
, loc
);
4971 inserter
->insert(left_temp
);
4972 left
= Expression::make_set_and_use_temporary(left_temp
, left
, loc
);
4975 Expression
* right
= this->right_
;
4976 Temporary_statement
* right_temp
= NULL
;
4977 if (right
->var_expression() == NULL
4978 && right
->temporary_reference_expression() == NULL
)
4980 right_temp
= Statement::make_temporary(right
->type(), NULL
, loc
);
4981 inserter
->insert(right_temp
);
4982 right
= Expression::make_set_and_use_temporary(right_temp
, right
, loc
);
4985 Expression
* ret
= Expression::make_boolean(true, loc
);
4986 const Struct_field_list
* fields
= st
->fields();
4987 unsigned int field_index
= 0;
4988 for (Struct_field_list::const_iterator pf
= fields
->begin();
4989 pf
!= fields
->end();
4990 ++pf
, ++field_index
)
4992 if (Gogo::is_sink_name(pf
->field_name()))
4995 if (field_index
> 0)
4997 if (left_temp
== NULL
)
4998 left
= left
->copy();
5000 left
= Expression::make_temporary_reference(left_temp
, loc
);
5001 if (right_temp
== NULL
)
5002 right
= right
->copy();
5004 right
= Expression::make_temporary_reference(right_temp
, loc
);
5006 Expression
* f1
= Expression::make_field_reference(left
, field_index
,
5008 Expression
* f2
= Expression::make_field_reference(right
, field_index
,
5010 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, f1
, f2
, loc
);
5011 ret
= Expression::make_binary(OPERATOR_ANDAND
, ret
, cond
, loc
);
5014 if (this->op_
== OPERATOR_NOTEQ
)
5015 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
5020 // Lower an array comparison.
5023 Binary_expression::lower_array_comparison(Gogo
* gogo
,
5024 Statement_inserter
* inserter
)
5026 Array_type
* at
= this->left_
->type()->array_type();
5027 Array_type
* at2
= this->right_
->type()->array_type();
5030 if (at
!= at2
&& !Type::are_identical(at
, at2
, false, NULL
))
5032 if (!Type::are_compatible_for_comparison(true, this->left_
->type(),
5033 this->right_
->type(), NULL
))
5036 // Call memcmp directly if possible. This may let the middle-end
5037 // optimize the call.
5038 if (at
->compare_is_identity(gogo
))
5039 return this->lower_compare_to_memcmp(gogo
, inserter
);
5041 // Call the array comparison function.
5042 Named_object
* hash_fn
;
5043 Named_object
* equal_fn
;
5044 at
->type_functions(gogo
, this->left_
->type()->named_type(), NULL
, NULL
,
5045 &hash_fn
, &equal_fn
);
5047 Location loc
= this->location();
5049 Expression
* func
= Expression::make_func_reference(equal_fn
, NULL
, loc
);
5051 Expression_list
* args
= new Expression_list();
5052 args
->push_back(this->operand_address(inserter
, this->left_
));
5053 args
->push_back(this->operand_address(inserter
, this->right_
));
5054 args
->push_back(Expression::make_type_info(at
, TYPE_INFO_SIZE
));
5056 Expression
* ret
= Expression::make_call(func
, args
, false, loc
);
5058 if (this->op_
== OPERATOR_NOTEQ
)
5059 ret
= Expression::make_unary(OPERATOR_NOT
, ret
, loc
);
5064 // Lower an interface to value comparison.
5067 Binary_expression::lower_interface_value_comparison(Gogo
*,
5068 Statement_inserter
* inserter
)
5070 Type
* left_type
= this->left_
->type();
5071 Type
* right_type
= this->right_
->type();
5072 Interface_type
* ift
;
5073 if (left_type
->interface_type() != NULL
)
5075 ift
= left_type
->interface_type();
5076 if (!ift
->implements_interface(right_type
, NULL
))
5081 ift
= right_type
->interface_type();
5082 if (!ift
->implements_interface(left_type
, NULL
))
5085 if (!Type::are_compatible_for_comparison(true, left_type
, right_type
, NULL
))
5088 Location loc
= this->location();
5090 if (left_type
->interface_type() == NULL
5091 && left_type
->points_to() == NULL
5092 && !this->left_
->is_addressable())
5094 Temporary_statement
* temp
=
5095 Statement::make_temporary(left_type
, NULL
, loc
);
5096 inserter
->insert(temp
);
5098 Expression::make_set_and_use_temporary(temp
, this->left_
, loc
);
5101 if (right_type
->interface_type() == NULL
5102 && right_type
->points_to() == NULL
5103 && !this->right_
->is_addressable())
5105 Temporary_statement
* temp
=
5106 Statement::make_temporary(right_type
, NULL
, loc
);
5107 inserter
->insert(temp
);
5109 Expression::make_set_and_use_temporary(temp
, this->right_
, loc
);
5115 // Lower a struct or array comparison to a call to memcmp.
5118 Binary_expression::lower_compare_to_memcmp(Gogo
*, Statement_inserter
* inserter
)
5120 Location loc
= this->location();
5122 Expression
* a1
= this->operand_address(inserter
, this->left_
);
5123 Expression
* a2
= this->operand_address(inserter
, this->right_
);
5124 Expression
* len
= Expression::make_type_info(this->left_
->type(),
5127 Expression
* call
= Runtime::make_call(Runtime::MEMCMP
, loc
, 3, a1
, a2
, len
);
5128 Expression
* zero
= Expression::make_integer_ul(0, NULL
, loc
);
5129 return Expression::make_binary(this->op_
, call
, zero
, loc
);
5133 Binary_expression::do_flatten(Gogo
* gogo
, Named_object
*,
5134 Statement_inserter
* inserter
)
5136 Location loc
= this->location();
5137 Temporary_statement
* temp
;
5138 if (this->left_
->type()->is_string_type()
5139 && this->op_
== OPERATOR_PLUS
)
5141 if (!this->left_
->is_variable())
5143 temp
= Statement::make_temporary(NULL
, this->left_
, loc
);
5144 inserter
->insert(temp
);
5145 this->left_
= Expression::make_temporary_reference(temp
, loc
);
5147 if (!this->right_
->is_variable())
5150 Statement::make_temporary(this->left_
->type(), this->right_
, loc
);
5151 this->right_
= Expression::make_temporary_reference(temp
, loc
);
5152 inserter
->insert(temp
);
5156 Type
* left_type
= this->left_
->type();
5157 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5158 || this->op_
== OPERATOR_RSHIFT
);
5159 bool is_idiv_op
= ((this->op_
== OPERATOR_DIV
&&
5160 left_type
->integer_type() != NULL
)
5161 || this->op_
== OPERATOR_MOD
);
5165 && (gogo
->check_divide_by_zero() || gogo
->check_divide_overflow())))
5167 if (!this->left_
->is_variable())
5169 temp
= Statement::make_temporary(NULL
, this->left_
, loc
);
5170 inserter
->insert(temp
);
5171 this->left_
= Expression::make_temporary_reference(temp
, loc
);
5173 if (!this->right_
->is_variable())
5176 Statement::make_temporary(NULL
, this->right_
, loc
);
5177 this->right_
= Expression::make_temporary_reference(temp
, loc
);
5178 inserter
->insert(temp
);
5185 // Return the address of EXPR, cast to unsafe.Pointer.
5188 Binary_expression::operand_address(Statement_inserter
* inserter
,
5191 Location loc
= this->location();
5193 if (!expr
->is_addressable())
5195 Temporary_statement
* temp
= Statement::make_temporary(expr
->type(), NULL
,
5197 inserter
->insert(temp
);
5198 expr
= Expression::make_set_and_use_temporary(temp
, expr
, loc
);
5200 expr
= Expression::make_unary(OPERATOR_AND
, expr
, loc
);
5201 static_cast<Unary_expression
*>(expr
)->set_does_not_escape();
5202 Type
* void_type
= Type::make_void_type();
5203 Type
* unsafe_pointer_type
= Type::make_pointer_type(void_type
);
5204 return Expression::make_cast(unsafe_pointer_type
, expr
, loc
);
5207 // Return the numeric constant value, if it has one.
5210 Binary_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
5212 Numeric_constant left_nc
;
5213 if (!this->left_
->numeric_constant_value(&left_nc
))
5215 Numeric_constant right_nc
;
5216 if (!this->right_
->numeric_constant_value(&right_nc
))
5218 return Binary_expression::eval_constant(this->op_
, &left_nc
, &right_nc
,
5219 this->location(), nc
);
5222 // Note that the value is being discarded.
5225 Binary_expression::do_discarding_value()
5227 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5228 return this->right_
->discarding_value();
5231 this->unused_value_error();
5239 Binary_expression::do_type()
5241 if (this->classification() == EXPRESSION_ERROR
)
5242 return Type::make_error_type();
5247 case OPERATOR_NOTEQ
:
5252 if (this->type_
== NULL
)
5253 this->type_
= Type::make_boolean_type();
5257 case OPERATOR_MINUS
:
5264 case OPERATOR_BITCLEAR
:
5266 case OPERATOR_ANDAND
:
5269 if (!Binary_expression::operation_type(this->op_
,
5270 this->left_
->type(),
5271 this->right_
->type(),
5273 return Type::make_error_type();
5277 case OPERATOR_LSHIFT
:
5278 case OPERATOR_RSHIFT
:
5279 return this->left_
->type();
5286 // Set type for a binary expression.
5289 Binary_expression::do_determine_type(const Type_context
* context
)
5291 Type
* tleft
= this->left_
->type();
5292 Type
* tright
= this->right_
->type();
5294 // Both sides should have the same type, except for the shift
5295 // operations. For a comparison, we should ignore the incoming
5298 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5299 || this->op_
== OPERATOR_RSHIFT
);
5301 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5302 || this->op_
== OPERATOR_NOTEQ
5303 || this->op_
== OPERATOR_LT
5304 || this->op_
== OPERATOR_LE
5305 || this->op_
== OPERATOR_GT
5306 || this->op_
== OPERATOR_GE
);
5308 Type_context
subcontext(*context
);
5312 // In a comparison, the context does not determine the types of
5314 subcontext
.type
= NULL
;
5317 // Set the context for the left hand operand.
5320 // The right hand operand of a shift plays no role in
5321 // determining the type of the left hand operand.
5323 else if (!tleft
->is_abstract())
5324 subcontext
.type
= tleft
;
5325 else if (!tright
->is_abstract())
5326 subcontext
.type
= tright
;
5327 else if (subcontext
.type
== NULL
)
5329 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5330 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5331 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5333 // Both sides have an abstract integer, abstract float, or
5334 // abstract complex type. Just let CONTEXT determine
5335 // whether they may remain abstract or not.
5337 else if (tleft
->complex_type() != NULL
)
5338 subcontext
.type
= tleft
;
5339 else if (tright
->complex_type() != NULL
)
5340 subcontext
.type
= tright
;
5341 else if (tleft
->float_type() != NULL
)
5342 subcontext
.type
= tleft
;
5343 else if (tright
->float_type() != NULL
)
5344 subcontext
.type
= tright
;
5346 subcontext
.type
= tleft
;
5348 if (subcontext
.type
!= NULL
&& !context
->may_be_abstract
)
5349 subcontext
.type
= subcontext
.type
->make_non_abstract_type();
5352 this->left_
->determine_type(&subcontext
);
5356 // We may have inherited an unusable type for the shift operand.
5357 // Give a useful error if that happened.
5358 if (tleft
->is_abstract()
5359 && subcontext
.type
!= NULL
5360 && !subcontext
.may_be_abstract
5361 && subcontext
.type
->interface_type() == NULL
5362 && subcontext
.type
->integer_type() == NULL
)
5363 this->report_error(("invalid context-determined non-integer type "
5364 "for left operand of shift"));
5366 // The context for the right hand operand is the same as for the
5367 // left hand operand, except for a shift operator.
5368 subcontext
.type
= Type::lookup_integer_type("uint");
5369 subcontext
.may_be_abstract
= false;
5372 this->right_
->determine_type(&subcontext
);
5376 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
5378 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
5379 this->type_
= context
->type
;
5380 else if (!context
->may_be_abstract
)
5381 this->type_
= Type::lookup_bool_type();
5385 // Report an error if the binary operator OP does not support TYPE.
5386 // OTYPE is the type of the other operand. Return whether the
5387 // operation is OK. This should not be used for shift.
5390 Binary_expression::check_operator_type(Operator op
, Type
* type
, Type
* otype
,
5396 case OPERATOR_ANDAND
:
5397 if (!type
->is_boolean_type())
5399 error_at(location
, "expected boolean type");
5405 case OPERATOR_NOTEQ
:
5408 if (!Type::are_compatible_for_comparison(true, type
, otype
, &reason
))
5410 error_at(location
, "%s", reason
.c_str());
5422 if (!Type::are_compatible_for_comparison(false, type
, otype
, &reason
))
5424 error_at(location
, "%s", reason
.c_str());
5431 case OPERATOR_PLUSEQ
:
5432 if (type
->integer_type() == NULL
5433 && type
->float_type() == NULL
5434 && type
->complex_type() == NULL
5435 && !type
->is_string_type())
5438 "expected integer, floating, complex, or string type");
5443 case OPERATOR_MINUS
:
5444 case OPERATOR_MINUSEQ
:
5446 case OPERATOR_MULTEQ
:
5448 case OPERATOR_DIVEQ
:
5449 if (type
->integer_type() == NULL
5450 && type
->float_type() == NULL
5451 && type
->complex_type() == NULL
)
5453 error_at(location
, "expected integer, floating, or complex type");
5459 case OPERATOR_MODEQ
:
5463 case OPERATOR_ANDEQ
:
5465 case OPERATOR_XOREQ
:
5466 case OPERATOR_BITCLEAR
:
5467 case OPERATOR_BITCLEAREQ
:
5468 if (type
->integer_type() == NULL
)
5470 error_at(location
, "expected integer type");
5485 Binary_expression::do_check_types(Gogo
*)
5487 if (this->classification() == EXPRESSION_ERROR
)
5490 Type
* left_type
= this->left_
->type();
5491 Type
* right_type
= this->right_
->type();
5492 if (left_type
->is_error() || right_type
->is_error())
5494 this->set_is_error();
5498 if (this->op_
== OPERATOR_EQEQ
5499 || this->op_
== OPERATOR_NOTEQ
5500 || this->op_
== OPERATOR_LT
5501 || this->op_
== OPERATOR_LE
5502 || this->op_
== OPERATOR_GT
5503 || this->op_
== OPERATOR_GE
)
5505 if (left_type
->is_nil_type() && right_type
->is_nil_type())
5507 this->report_error(_("invalid comparison of nil with nil"));
5510 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5511 && !Type::are_assignable(right_type
, left_type
, NULL
))
5513 this->report_error(_("incompatible types in binary expression"));
5516 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5519 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5523 this->set_is_error();
5527 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5529 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5531 this->report_error(_("incompatible types in binary expression"));
5534 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5538 this->set_is_error();
5541 if (this->op_
== OPERATOR_DIV
|| this->op_
== OPERATOR_MOD
)
5543 // Division by a zero integer constant is an error.
5544 Numeric_constant rconst
;
5546 if (left_type
->integer_type() != NULL
5547 && this->right_
->numeric_constant_value(&rconst
)
5548 && rconst
.to_unsigned_long(&rval
) == Numeric_constant::NC_UL_VALID
5551 this->report_error(_("integer division by zero"));
5558 if (left_type
->integer_type() == NULL
)
5559 this->report_error(_("shift of non-integer operand"));
5561 if (!right_type
->is_abstract()
5562 && (right_type
->integer_type() == NULL
5563 || !right_type
->integer_type()->is_unsigned()))
5564 this->report_error(_("shift count not unsigned integer"));
5567 Numeric_constant nc
;
5568 if (this->right_
->numeric_constant_value(&nc
))
5571 if (!nc
.to_int(&val
))
5572 this->report_error(_("shift count not unsigned integer"));
5575 if (mpz_sgn(val
) < 0)
5577 this->report_error(_("negative shift count"));
5578 Location rloc
= this->right_
->location();
5579 this->right_
= Expression::make_integer_ul(0, right_type
,
5589 // Get the backend representation for a binary expression.
5592 Binary_expression::do_get_backend(Translate_context
* context
)
5594 Gogo
* gogo
= context
->gogo();
5595 Location loc
= this->location();
5596 Type
* left_type
= this->left_
->type();
5597 Type
* right_type
= this->right_
->type();
5599 bool use_left_type
= true;
5600 bool is_shift_op
= false;
5601 bool is_idiv_op
= false;
5605 case OPERATOR_NOTEQ
:
5610 return Expression::comparison(context
, this->type_
, this->op_
,
5611 this->left_
, this->right_
, loc
);
5614 case OPERATOR_ANDAND
:
5615 use_left_type
= false;
5618 case OPERATOR_MINUS
:
5624 if (left_type
->float_type() != NULL
|| left_type
->complex_type() != NULL
)
5629 case OPERATOR_LSHIFT
:
5630 case OPERATOR_RSHIFT
:
5633 case OPERATOR_BITCLEAR
:
5634 this->right_
= Expression::make_unary(OPERATOR_XOR
, this->right_
, loc
);
5641 if (left_type
->is_string_type())
5643 go_assert(this->op_
== OPERATOR_PLUS
);
5644 Expression
* string_plus
=
5645 Runtime::make_call(Runtime::STRING_PLUS
, loc
, 2,
5646 this->left_
, this->right_
);
5647 return string_plus
->get_backend(context
);
5650 // For complex division Go might want slightly different results than the
5651 // backend implementation provides, so we have our own runtime routine.
5652 if (this->op_
== OPERATOR_DIV
&& this->left_
->type()->complex_type() != NULL
)
5654 Runtime::Function complex_code
;
5655 switch (this->left_
->type()->complex_type()->bits())
5658 complex_code
= Runtime::COMPLEX64_DIV
;
5661 complex_code
= Runtime::COMPLEX128_DIV
;
5666 Expression
* complex_div
=
5667 Runtime::make_call(complex_code
, loc
, 2, this->left_
, this->right_
);
5668 return complex_div
->get_backend(context
);
5671 Bexpression
* left
= this->left_
->get_backend(context
);
5672 Bexpression
* right
= this->right_
->get_backend(context
);
5674 Type
* type
= use_left_type
? left_type
: right_type
;
5675 Btype
* btype
= type
->get_backend(gogo
);
5678 gogo
->backend()->binary_expression(this->op_
, left
, right
, loc
);
5679 ret
= gogo
->backend()->convert_expression(btype
, ret
, loc
);
5681 // Initialize overflow constants.
5682 Bexpression
* overflow
;
5684 mpz_init_set_ui(zero
, 0UL);
5686 mpz_init_set_ui(one
, 1UL);
5688 mpz_init_set_si(neg_one
, -1);
5690 Btype
* left_btype
= left_type
->get_backend(gogo
);
5691 Btype
* right_btype
= right_type
->get_backend(gogo
);
5693 // In Go, a shift larger than the size of the type is well-defined.
5694 // This is not true in C, so we need to insert a conditional.
5697 go_assert(left_type
->integer_type() != NULL
);
5700 int bits
= left_type
->integer_type()->bits();
5701 mpz_init_set_ui(bitsval
, bits
);
5702 Bexpression
* bits_expr
=
5703 gogo
->backend()->integer_constant_expression(right_btype
, bitsval
);
5704 Bexpression
* compare
=
5705 gogo
->backend()->binary_expression(OPERATOR_LT
,
5706 right
, bits_expr
, loc
);
5708 Bexpression
* zero_expr
=
5709 gogo
->backend()->integer_constant_expression(left_btype
, zero
);
5710 overflow
= zero_expr
;
5711 if (this->op_
== OPERATOR_RSHIFT
5712 && !left_type
->integer_type()->is_unsigned())
5714 Bexpression
* neg_expr
=
5715 gogo
->backend()->binary_expression(OPERATOR_LT
, left
,
5717 Bexpression
* neg_one_expr
=
5718 gogo
->backend()->integer_constant_expression(left_btype
, neg_one
);
5719 overflow
= gogo
->backend()->conditional_expression(btype
, neg_expr
,
5723 ret
= gogo
->backend()->conditional_expression(btype
, compare
, ret
,
5728 // Add checks for division by zero and division overflow as needed.
5731 if (gogo
->check_divide_by_zero())
5734 Bexpression
* zero_expr
=
5735 gogo
->backend()->integer_constant_expression(right_btype
, zero
);
5736 Bexpression
* check
=
5737 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
5738 right
, zero_expr
, loc
);
5740 // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO)
5741 int errcode
= RUNTIME_ERROR_DIVISION_BY_ZERO
;
5742 Bexpression
* crash
= gogo
->runtime_error(errcode
,
5743 loc
)->get_backend(context
);
5745 // right == 0 ? (__go_runtime_error(...), 0) : ret
5746 ret
= gogo
->backend()->conditional_expression(btype
, check
, crash
,
5750 if (gogo
->check_divide_overflow())
5753 // FIXME: It would be nice to say that this test is expected
5756 Bexpression
* neg_one_expr
=
5757 gogo
->backend()->integer_constant_expression(right_btype
, neg_one
);
5758 Bexpression
* check
=
5759 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
5760 right
, neg_one_expr
, loc
);
5762 Bexpression
* zero_expr
=
5763 gogo
->backend()->integer_constant_expression(btype
, zero
);
5764 Bexpression
* one_expr
=
5765 gogo
->backend()->integer_constant_expression(btype
, one
);
5767 if (type
->integer_type()->is_unsigned())
5769 // An unsigned -1 is the largest possible number, so
5770 // dividing is always 1 or 0.
5773 gogo
->backend()->binary_expression(OPERATOR_EQEQ
,
5775 if (this->op_
== OPERATOR_DIV
)
5777 gogo
->backend()->conditional_expression(btype
, cmp
,
5778 one_expr
, zero_expr
,
5782 gogo
->backend()->conditional_expression(btype
, cmp
,
5788 // Computing left / -1 is the same as computing - left,
5789 // which does not overflow since Go sets -fwrapv.
5790 if (this->op_
== OPERATOR_DIV
)
5792 Expression
* negate_expr
=
5793 Expression::make_unary(OPERATOR_MINUS
, this->left_
, loc
);
5794 overflow
= negate_expr
->get_backend(context
);
5797 overflow
= zero_expr
;
5799 overflow
= gogo
->backend()->convert_expression(btype
, overflow
, loc
);
5801 // right == -1 ? - left : ret
5802 ret
= gogo
->backend()->conditional_expression(btype
, check
, overflow
,
5813 // Export a binary expression.
5816 Binary_expression::do_export(Export
* exp
) const
5818 exp
->write_c_string("(");
5819 this->left_
->export_expression(exp
);
5823 exp
->write_c_string(" || ");
5825 case OPERATOR_ANDAND
:
5826 exp
->write_c_string(" && ");
5829 exp
->write_c_string(" == ");
5831 case OPERATOR_NOTEQ
:
5832 exp
->write_c_string(" != ");
5835 exp
->write_c_string(" < ");
5838 exp
->write_c_string(" <= ");
5841 exp
->write_c_string(" > ");
5844 exp
->write_c_string(" >= ");
5847 exp
->write_c_string(" + ");
5849 case OPERATOR_MINUS
:
5850 exp
->write_c_string(" - ");
5853 exp
->write_c_string(" | ");
5856 exp
->write_c_string(" ^ ");
5859 exp
->write_c_string(" * ");
5862 exp
->write_c_string(" / ");
5865 exp
->write_c_string(" % ");
5867 case OPERATOR_LSHIFT
:
5868 exp
->write_c_string(" << ");
5870 case OPERATOR_RSHIFT
:
5871 exp
->write_c_string(" >> ");
5874 exp
->write_c_string(" & ");
5876 case OPERATOR_BITCLEAR
:
5877 exp
->write_c_string(" &^ ");
5882 this->right_
->export_expression(exp
);
5883 exp
->write_c_string(")");
5886 // Import a binary expression.
5889 Binary_expression::do_import(Import
* imp
)
5891 imp
->require_c_string("(");
5893 Expression
* left
= Expression::import_expression(imp
);
5896 if (imp
->match_c_string(" || "))
5901 else if (imp
->match_c_string(" && "))
5903 op
= OPERATOR_ANDAND
;
5906 else if (imp
->match_c_string(" == "))
5911 else if (imp
->match_c_string(" != "))
5913 op
= OPERATOR_NOTEQ
;
5916 else if (imp
->match_c_string(" < "))
5921 else if (imp
->match_c_string(" <= "))
5926 else if (imp
->match_c_string(" > "))
5931 else if (imp
->match_c_string(" >= "))
5936 else if (imp
->match_c_string(" + "))
5941 else if (imp
->match_c_string(" - "))
5943 op
= OPERATOR_MINUS
;
5946 else if (imp
->match_c_string(" | "))
5951 else if (imp
->match_c_string(" ^ "))
5956 else if (imp
->match_c_string(" * "))
5961 else if (imp
->match_c_string(" / "))
5966 else if (imp
->match_c_string(" % "))
5971 else if (imp
->match_c_string(" << "))
5973 op
= OPERATOR_LSHIFT
;
5976 else if (imp
->match_c_string(" >> "))
5978 op
= OPERATOR_RSHIFT
;
5981 else if (imp
->match_c_string(" & "))
5986 else if (imp
->match_c_string(" &^ "))
5988 op
= OPERATOR_BITCLEAR
;
5993 error_at(imp
->location(), "unrecognized binary operator");
5994 return Expression::make_error(imp
->location());
5997 Expression
* right
= Expression::import_expression(imp
);
5999 imp
->require_c_string(")");
6001 return Expression::make_binary(op
, left
, right
, imp
->location());
6004 // Dump ast representation of a binary expression.
6007 Binary_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
6009 ast_dump_context
->ostream() << "(";
6010 ast_dump_context
->dump_expression(this->left_
);
6011 ast_dump_context
->ostream() << " ";
6012 ast_dump_context
->dump_operator(this->op_
);
6013 ast_dump_context
->ostream() << " ";
6014 ast_dump_context
->dump_expression(this->right_
);
6015 ast_dump_context
->ostream() << ") ";
6018 // Make a binary expression.
6021 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6024 return new Binary_expression(op
, left
, right
, location
);
6027 // Implement a comparison.
6030 Expression::comparison(Translate_context
* context
, Type
* result_type
,
6031 Operator op
, Expression
* left
, Expression
* right
,
6034 Type
* left_type
= left
->type();
6035 Type
* right_type
= right
->type();
6037 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, location
);
6039 if (left_type
->is_string_type() && right_type
->is_string_type())
6041 left
= Runtime::make_call(Runtime::STRCMP
, location
, 2,
6045 else if ((left_type
->interface_type() != NULL
6046 && right_type
->interface_type() == NULL
6047 && !right_type
->is_nil_type())
6048 || (left_type
->interface_type() == NULL
6049 && !left_type
->is_nil_type()
6050 && right_type
->interface_type() != NULL
))
6052 // Comparing an interface value to a non-interface value.
6053 if (left_type
->interface_type() == NULL
)
6055 std::swap(left_type
, right_type
);
6056 std::swap(left
, right
);
6059 // The right operand is not an interface. We need to take its
6060 // address if it is not a pointer.
6061 Expression
* pointer_arg
= NULL
;
6062 if (right_type
->points_to() != NULL
)
6063 pointer_arg
= right
;
6066 go_assert(right
->is_addressable());
6067 pointer_arg
= Expression::make_unary(OPERATOR_AND
, right
,
6071 Expression
* descriptor
=
6072 Expression::make_type_descriptor(right_type
, location
);
6074 Runtime::make_call((left_type
->interface_type()->is_empty()
6075 ? Runtime::EMPTY_INTERFACE_VALUE_COMPARE
6076 : Runtime::INTERFACE_VALUE_COMPARE
),
6077 location
, 3, left
, descriptor
,
6081 else if (left_type
->interface_type() != NULL
6082 && right_type
->interface_type() != NULL
)
6084 Runtime::Function compare_function
;
6085 if (left_type
->interface_type()->is_empty()
6086 && right_type
->interface_type()->is_empty())
6087 compare_function
= Runtime::EMPTY_INTERFACE_COMPARE
;
6088 else if (!left_type
->interface_type()->is_empty()
6089 && !right_type
->interface_type()->is_empty())
6090 compare_function
= Runtime::INTERFACE_COMPARE
;
6093 if (left_type
->interface_type()->is_empty())
6095 go_assert(op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
);
6096 std::swap(left_type
, right_type
);
6097 std::swap(left
, right
);
6099 go_assert(!left_type
->interface_type()->is_empty());
6100 go_assert(right_type
->interface_type()->is_empty());
6101 compare_function
= Runtime::INTERFACE_EMPTY_COMPARE
;
6104 left
= Runtime::make_call(compare_function
, location
, 2, left
, right
);
6108 if (left_type
->is_nil_type()
6109 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6111 std::swap(left_type
, right_type
);
6112 std::swap(left
, right
);
6115 if (right_type
->is_nil_type())
6117 right
= Expression::make_nil(location
);
6118 if (left_type
->array_type() != NULL
6119 && left_type
->array_type()->length() == NULL
)
6121 Array_type
* at
= left_type
->array_type();
6122 left
= at
->get_value_pointer(context
->gogo(), left
);
6124 else if (left_type
->interface_type() != NULL
)
6126 // An interface is nil if the first field is nil.
6127 left
= Expression::make_field_reference(left
, 0, location
);
6131 Bexpression
* left_bexpr
= left
->get_backend(context
);
6132 Bexpression
* right_bexpr
= right
->get_backend(context
);
6134 Gogo
* gogo
= context
->gogo();
6135 Bexpression
* ret
= gogo
->backend()->binary_expression(op
, left_bexpr
,
6136 right_bexpr
, location
);
6137 if (result_type
!= NULL
)
6138 ret
= gogo
->backend()->convert_expression(result_type
->get_backend(gogo
),
6143 // Class Bound_method_expression.
6148 Bound_method_expression::do_traverse(Traverse
* traverse
)
6150 return Expression::traverse(&this->expr_
, traverse
);
6153 // Lower the expression. If this is a method value rather than being
6154 // called, and the method is accessed via a pointer, we may need to
6155 // add nil checks. Introduce a temporary variable so that those nil
6156 // checks do not cause multiple evaluation.
6159 Bound_method_expression::do_lower(Gogo
*, Named_object
*,
6160 Statement_inserter
* inserter
, int)
6162 // For simplicity we use a temporary for every call to an embedded
6163 // method, even though some of them might be pure value methods and
6164 // not require a temporary.
6165 if (this->expr_
->var_expression() == NULL
6166 && this->expr_
->temporary_reference_expression() == NULL
6167 && this->expr_
->set_and_use_temporary_expression() == NULL
6168 && (this->method_
->field_indexes() != NULL
6169 || (this->method_
->is_value_method()
6170 && this->expr_
->type()->points_to() != NULL
)))
6172 Temporary_statement
* temp
=
6173 Statement::make_temporary(this->expr_
->type(), NULL
, this->location());
6174 inserter
->insert(temp
);
6175 this->expr_
= Expression::make_set_and_use_temporary(temp
, this->expr_
,
6181 // Return the type of a bound method expression. The type of this
6182 // object is simply the type of the method with no receiver.
6185 Bound_method_expression::do_type()
6187 Named_object
* fn
= this->method_
->named_object();
6188 Function_type
* fntype
;
6189 if (fn
->is_function())
6190 fntype
= fn
->func_value()->type();
6191 else if (fn
->is_function_declaration())
6192 fntype
= fn
->func_declaration_value()->type();
6194 return Type::make_error_type();
6195 return fntype
->copy_without_receiver();
6198 // Determine the types of a method expression.
6201 Bound_method_expression::do_determine_type(const Type_context
*)
6203 Named_object
* fn
= this->method_
->named_object();
6204 Function_type
* fntype
;
6205 if (fn
->is_function())
6206 fntype
= fn
->func_value()->type();
6207 else if (fn
->is_function_declaration())
6208 fntype
= fn
->func_declaration_value()->type();
6211 if (fntype
== NULL
|| !fntype
->is_method())
6212 this->expr_
->determine_type_no_context();
6215 Type_context
subcontext(fntype
->receiver()->type(), false);
6216 this->expr_
->determine_type(&subcontext
);
6220 // Check the types of a method expression.
6223 Bound_method_expression::do_check_types(Gogo
*)
6225 Named_object
* fn
= this->method_
->named_object();
6226 if (!fn
->is_function() && !fn
->is_function_declaration())
6228 this->report_error(_("object is not a method"));
6232 Function_type
* fntype
;
6233 if (fn
->is_function())
6234 fntype
= fn
->func_value()->type();
6235 else if (fn
->is_function_declaration())
6236 fntype
= fn
->func_declaration_value()->type();
6239 Type
* rtype
= fntype
->receiver()->type()->deref();
6240 Type
* etype
= (this->expr_type_
!= NULL
6242 : this->expr_
->type());
6243 etype
= etype
->deref();
6244 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6245 this->report_error(_("method type does not match object type"));
6248 // If a bound method expression is not simply called, then it is
6249 // represented as a closure. The closure will hold a single variable,
6250 // the receiver to pass to the method. The function will be a simple
6251 // thunk that pulls that value from the closure and calls the method
6252 // with the remaining arguments.
6254 // Because method values are not common, we don't build all thunks for
6255 // every methods, but instead only build them as we need them. In
6256 // particular, we even build them on demand for methods defined in
6259 Bound_method_expression::Method_value_thunks
6260 Bound_method_expression::method_value_thunks
;
6262 // Find or create the thunk for METHOD.
6265 Bound_method_expression::create_thunk(Gogo
* gogo
, const Method
* method
,
6268 std::pair
<Named_object
*, Named_object
*> val(fn
, NULL
);
6269 std::pair
<Method_value_thunks::iterator
, bool> ins
=
6270 Bound_method_expression::method_value_thunks
.insert(val
);
6273 // We have seen this method before.
6274 go_assert(ins
.first
->second
!= NULL
);
6275 return ins
.first
->second
;
6278 Location loc
= fn
->location();
6280 Function_type
* orig_fntype
;
6281 if (fn
->is_function())
6282 orig_fntype
= fn
->func_value()->type();
6283 else if (fn
->is_function_declaration())
6284 orig_fntype
= fn
->func_declaration_value()->type();
6288 if (orig_fntype
== NULL
|| !orig_fntype
->is_method())
6290 ins
.first
->second
= Named_object::make_erroneous_name(Gogo::thunk_name());
6291 return ins
.first
->second
;
6294 Struct_field_list
* sfl
= new Struct_field_list();
6295 // The type here is wrong--it should be the C function type. But it
6296 // doesn't really matter.
6297 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
6298 sfl
->push_back(Struct_field(Typed_identifier("fn.0", vt
, loc
)));
6299 sfl
->push_back(Struct_field(Typed_identifier("val.1",
6300 orig_fntype
->receiver()->type(),
6302 Type
* closure_type
= Type::make_struct_type(sfl
, loc
);
6303 closure_type
= Type::make_pointer_type(closure_type
);
6305 Function_type
* new_fntype
= orig_fntype
->copy_with_names();
6307 Named_object
* new_no
= gogo
->start_function(Gogo::thunk_name(), new_fntype
,
6310 Variable
* cvar
= new Variable(closure_type
, NULL
, false, false, false, loc
);
6311 cvar
->set_is_used();
6312 Named_object
* cp
= Named_object::make_variable("$closure", NULL
, cvar
);
6313 new_no
->func_value()->set_closure_var(cp
);
6315 gogo
->start_block(loc
);
6317 // Field 0 of the closure is the function code pointer, field 1 is
6318 // the value on which to invoke the method.
6319 Expression
* arg
= Expression::make_var_reference(cp
, loc
);
6320 arg
= Expression::make_unary(OPERATOR_MULT
, arg
, loc
);
6321 arg
= Expression::make_field_reference(arg
, 1, loc
);
6323 Expression
* bme
= Expression::make_bound_method(arg
, method
, fn
, loc
);
6325 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
6326 Expression_list
* args
;
6327 if (orig_params
== NULL
|| orig_params
->empty())
6331 const Typed_identifier_list
* new_params
= new_fntype
->parameters();
6332 args
= new Expression_list();
6333 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
6334 p
!= new_params
->end();
6337 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
6338 go_assert(p_no
!= NULL
6339 && p_no
->is_variable()
6340 && p_no
->var_value()->is_parameter());
6341 args
->push_back(Expression::make_var_reference(p_no
, loc
));
6345 Call_expression
* call
= Expression::make_call(bme
, args
,
6346 orig_fntype
->is_varargs(),
6348 call
->set_varargs_are_lowered();
6350 Statement
* s
= Statement::make_return_from_call(call
, loc
);
6351 gogo
->add_statement(s
);
6352 Block
* b
= gogo
->finish_block(loc
);
6353 gogo
->add_block(b
, loc
);
6354 gogo
->lower_block(new_no
, b
);
6355 gogo
->flatten_block(new_no
, b
);
6356 gogo
->finish_function(loc
);
6358 ins
.first
->second
= new_no
;
6362 // Return an expression to check *REF for nil while dereferencing
6363 // according to FIELD_INDEXES. Update *REF to build up the field
6364 // reference. This is a static function so that we don't have to
6365 // worry about declaring Field_indexes in expressions.h.
6368 bme_check_nil(const Method::Field_indexes
* field_indexes
, Location loc
,
6371 if (field_indexes
== NULL
)
6372 return Expression::make_boolean(false, loc
);
6373 Expression
* cond
= bme_check_nil(field_indexes
->next
, loc
, ref
);
6374 Struct_type
* stype
= (*ref
)->type()->deref()->struct_type();
6375 go_assert(stype
!= NULL
6376 && field_indexes
->field_index
< stype
->field_count());
6377 if ((*ref
)->type()->struct_type() == NULL
)
6379 go_assert((*ref
)->type()->points_to() != NULL
);
6380 Expression
* n
= Expression::make_binary(OPERATOR_EQEQ
, *ref
,
6381 Expression::make_nil(loc
),
6383 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, n
, loc
);
6384 *ref
= Expression::make_unary(OPERATOR_MULT
, *ref
, loc
);
6385 go_assert((*ref
)->type()->struct_type() == stype
);
6387 *ref
= Expression::make_field_reference(*ref
, field_indexes
->field_index
,
6392 // Get the backend representation for a method value.
6395 Bound_method_expression::do_get_backend(Translate_context
* context
)
6397 Named_object
* thunk
= Bound_method_expression::create_thunk(context
->gogo(),
6400 if (thunk
->is_erroneous())
6402 go_assert(saw_errors());
6403 return context
->backend()->error_expression();
6406 // FIXME: We should lower this earlier, but we can't lower it in the
6407 // lowering pass because at that point we don't know whether we need
6408 // to create the thunk or not. If the expression is called, we
6409 // don't need the thunk.
6411 Location loc
= this->location();
6413 // If the method expects a value, and we have a pointer, we need to
6414 // dereference the pointer.
6416 Named_object
* fn
= this->method_
->named_object();
6417 Function_type
* fntype
;
6418 if (fn
->is_function())
6419 fntype
= fn
->func_value()->type();
6420 else if (fn
->is_function_declaration())
6421 fntype
= fn
->func_declaration_value()->type();
6425 Expression
* val
= this->expr_
;
6426 if (fntype
->receiver()->type()->points_to() == NULL
6427 && val
->type()->points_to() != NULL
)
6428 val
= Expression::make_unary(OPERATOR_MULT
, val
, loc
);
6430 // Note that we are ignoring this->expr_type_ here. The thunk will
6431 // expect a closure whose second field has type this->expr_type_ (if
6432 // that is not NULL). We are going to pass it a closure whose
6433 // second field has type this->expr_->type(). Since
6434 // this->expr_type_ is only not-NULL for pointer types, we can get
6437 Struct_field_list
* fields
= new Struct_field_list();
6438 fields
->push_back(Struct_field(Typed_identifier("fn.0",
6439 thunk
->func_value()->type(),
6441 fields
->push_back(Struct_field(Typed_identifier("val.1", val
->type(), loc
)));
6442 Struct_type
* st
= Type::make_struct_type(fields
, loc
);
6444 Expression_list
* vals
= new Expression_list();
6445 vals
->push_back(Expression::make_func_code_reference(thunk
, loc
));
6446 vals
->push_back(val
);
6448 Expression
* ret
= Expression::make_struct_composite_literal(st
, vals
, loc
);
6449 ret
= Expression::make_heap_expression(ret
, loc
);
6451 // See whether the expression or any embedded pointers are nil.
6453 Expression
* nil_check
= NULL
;
6454 Expression
* expr
= this->expr_
;
6455 if (this->method_
->field_indexes() != NULL
)
6457 // Note that we are evaluating this->expr_ twice, but that is OK
6458 // because in the lowering pass we forced it into a temporary
6460 Expression
* ref
= expr
;
6461 nil_check
= bme_check_nil(this->method_
->field_indexes(), loc
, &ref
);
6465 if (this->method_
->is_value_method() && expr
->type()->points_to() != NULL
)
6467 Expression
* n
= Expression::make_binary(OPERATOR_EQEQ
, expr
,
6468 Expression::make_nil(loc
),
6470 if (nil_check
== NULL
)
6473 nil_check
= Expression::make_binary(OPERATOR_OROR
, nil_check
, n
, loc
);
6476 Bexpression
* bme
= ret
->get_backend(context
);
6477 if (nil_check
!= NULL
)
6479 Gogo
* gogo
= context
->gogo();
6480 Bexpression
* crash
=
6481 gogo
->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
6482 loc
)->get_backend(context
);
6483 Btype
* btype
= ret
->type()->get_backend(gogo
);
6484 Bexpression
* bcheck
= nil_check
->get_backend(context
);
6485 bme
= gogo
->backend()->conditional_expression(btype
, bcheck
, crash
,
6491 // Dump ast representation of a bound method expression.
6494 Bound_method_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
6497 if (this->expr_type_
!= NULL
)
6498 ast_dump_context
->ostream() << "(";
6499 ast_dump_context
->dump_expression(this->expr_
);
6500 if (this->expr_type_
!= NULL
)
6502 ast_dump_context
->ostream() << ":";
6503 ast_dump_context
->dump_type(this->expr_type_
);
6504 ast_dump_context
->ostream() << ")";
6507 ast_dump_context
->ostream() << "." << this->function_
->name();
6510 // Make a method expression.
6512 Bound_method_expression
*
6513 Expression::make_bound_method(Expression
* expr
, const Method
* method
,
6514 Named_object
* function
, Location location
)
6516 return new Bound_method_expression(expr
, method
, function
, location
);
6519 // Class Builtin_call_expression. This is used for a call to a
6520 // builtin function.
6522 class Builtin_call_expression
: public Call_expression
6525 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6526 bool is_varargs
, Location location
);
6529 // This overrides Call_expression::do_lower.
6531 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
6534 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
6537 do_is_constant() const;
6540 do_numeric_constant_value(Numeric_constant
*) const;
6543 do_discarding_value();
6549 do_determine_type(const Type_context
*);
6552 do_check_types(Gogo
*);
6558 do_get_backend(Translate_context
*);
6561 do_export(Export
*) const;
6564 do_is_recover_call() const;
6567 do_set_recover_arg(Expression
*);
6570 // The builtin functions.
6571 enum Builtin_function_code
6575 // Predeclared builtin functions.
6592 // Builtin functions from the unsafe package.
6605 real_imag_type(Type
*);
6608 complex_type(Type
*);
6614 check_int_value(Expression
*, bool is_length
);
6616 // A pointer back to the general IR structure. This avoids a global
6617 // variable, or passing it around everywhere.
6619 // The builtin function being called.
6620 Builtin_function_code code_
;
6621 // Used to stop endless loops when the length of an array uses len
6622 // or cap of the array itself.
6624 // Whether the argument is set for calls to BUILTIN_RECOVER.
6625 bool recover_arg_is_set_
;
6628 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6630 Expression_list
* args
,
6633 : Call_expression(fn
, args
, is_varargs
, location
),
6634 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false),
6635 recover_arg_is_set_(false)
6637 Func_expression
* fnexp
= this->fn()->func_expression();
6638 go_assert(fnexp
!= NULL
);
6639 const std::string
& name(fnexp
->named_object()->name());
6640 if (name
== "append")
6641 this->code_
= BUILTIN_APPEND
;
6642 else if (name
== "cap")
6643 this->code_
= BUILTIN_CAP
;
6644 else if (name
== "close")
6645 this->code_
= BUILTIN_CLOSE
;
6646 else if (name
== "complex")
6647 this->code_
= BUILTIN_COMPLEX
;
6648 else if (name
== "copy")
6649 this->code_
= BUILTIN_COPY
;
6650 else if (name
== "delete")
6651 this->code_
= BUILTIN_DELETE
;
6652 else if (name
== "imag")
6653 this->code_
= BUILTIN_IMAG
;
6654 else if (name
== "len")
6655 this->code_
= BUILTIN_LEN
;
6656 else if (name
== "make")
6657 this->code_
= BUILTIN_MAKE
;
6658 else if (name
== "new")
6659 this->code_
= BUILTIN_NEW
;
6660 else if (name
== "panic")
6661 this->code_
= BUILTIN_PANIC
;
6662 else if (name
== "print")
6663 this->code_
= BUILTIN_PRINT
;
6664 else if (name
== "println")
6665 this->code_
= BUILTIN_PRINTLN
;
6666 else if (name
== "real")
6667 this->code_
= BUILTIN_REAL
;
6668 else if (name
== "recover")
6669 this->code_
= BUILTIN_RECOVER
;
6670 else if (name
== "Alignof")
6671 this->code_
= BUILTIN_ALIGNOF
;
6672 else if (name
== "Offsetof")
6673 this->code_
= BUILTIN_OFFSETOF
;
6674 else if (name
== "Sizeof")
6675 this->code_
= BUILTIN_SIZEOF
;
6680 // Return whether this is a call to recover. This is a virtual
6681 // function called from the parent class.
6684 Builtin_call_expression::do_is_recover_call() const
6686 if (this->classification() == EXPRESSION_ERROR
)
6688 return this->code_
== BUILTIN_RECOVER
;
6691 // Set the argument for a call to recover.
6694 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6696 const Expression_list
* args
= this->args();
6697 go_assert(args
== NULL
|| args
->empty());
6698 Expression_list
* new_args
= new Expression_list();
6699 new_args
->push_back(arg
);
6700 this->set_args(new_args
);
6701 this->recover_arg_is_set_
= true;
6704 // Lower a builtin call expression. This turns new and make into
6705 // specific expressions. We also convert to a constant if we can.
6708 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
6709 Statement_inserter
* inserter
, int)
6711 if (this->classification() == EXPRESSION_ERROR
)
6714 Location loc
= this->location();
6716 if (this->is_varargs() && this->code_
!= BUILTIN_APPEND
)
6718 this->report_error(_("invalid use of %<...%> with builtin function"));
6719 return Expression::make_error(loc
);
6722 if (this->code_
== BUILTIN_OFFSETOF
)
6724 Expression
* arg
= this->one_arg();
6726 if (arg
->bound_method_expression() != NULL
6727 || arg
->interface_field_reference_expression() != NULL
)
6729 this->report_error(_("invalid use of method value as argument "
6734 Field_reference_expression
* farg
= arg
->field_reference_expression();
6735 while (farg
!= NULL
)
6737 if (!farg
->implicit())
6739 // When the selector refers to an embedded field,
6740 // it must not be reached through pointer indirections.
6741 if (farg
->expr()->deref() != farg
->expr())
6743 this->report_error(_("argument of Offsetof implies "
6744 "indirection of an embedded field"));
6747 // Go up until we reach the original base.
6748 farg
= farg
->expr()->field_reference_expression();
6752 if (this->is_constant())
6754 Numeric_constant nc
;
6755 if (this->numeric_constant_value(&nc
))
6756 return nc
.expression(loc
);
6759 switch (this->code_
)
6766 const Expression_list
* args
= this->args();
6767 if (args
== NULL
|| args
->size() < 1)
6768 this->report_error(_("not enough arguments"));
6769 else if (args
->size() > 1)
6770 this->report_error(_("too many arguments"));
6773 Expression
* arg
= args
->front();
6774 if (!arg
->is_type_expression())
6776 error_at(arg
->location(), "expected type");
6777 this->set_is_error();
6780 return Expression::make_allocation(arg
->type(), loc
);
6786 return this->lower_make();
6788 case BUILTIN_RECOVER
:
6789 if (function
!= NULL
)
6790 function
->func_value()->set_calls_recover();
6793 // Calling recover outside of a function always returns the
6794 // nil empty interface.
6795 Type
* eface
= Type::make_empty_interface_type(loc
);
6796 return Expression::make_cast(eface
, Expression::make_nil(loc
), loc
);
6800 case BUILTIN_APPEND
:
6802 // Lower the varargs.
6803 const Expression_list
* args
= this->args();
6804 if (args
== NULL
|| args
->empty())
6806 Type
* slice_type
= args
->front()->type();
6807 if (!slice_type
->is_slice_type())
6809 if (slice_type
->is_nil_type())
6810 error_at(args
->front()->location(), "use of untyped nil");
6812 error_at(args
->front()->location(),
6813 "argument 1 must be a slice");
6814 this->set_is_error();
6817 Type
* element_type
= slice_type
->array_type()->element_type();
6818 this->lower_varargs(gogo
, function
, inserter
,
6819 Type::make_array_type(element_type
, NULL
),
6824 case BUILTIN_DELETE
:
6826 // Lower to a runtime function call.
6827 const Expression_list
* args
= this->args();
6828 if (args
== NULL
|| args
->size() < 2)
6829 this->report_error(_("not enough arguments"));
6830 else if (args
->size() > 2)
6831 this->report_error(_("too many arguments"));
6832 else if (args
->front()->type()->map_type() == NULL
)
6833 this->report_error(_("argument 1 must be a map"));
6836 // Since this function returns no value it must appear in
6837 // a statement by itself, so we don't have to worry about
6838 // order of evaluation of values around it. Evaluate the
6839 // map first to get order of evaluation right.
6840 Map_type
* mt
= args
->front()->type()->map_type();
6841 Temporary_statement
* map_temp
=
6842 Statement::make_temporary(mt
, args
->front(), loc
);
6843 inserter
->insert(map_temp
);
6845 Temporary_statement
* key_temp
=
6846 Statement::make_temporary(mt
->key_type(), args
->back(), loc
);
6847 inserter
->insert(key_temp
);
6849 Expression
* e1
= Expression::make_temporary_reference(map_temp
,
6851 Expression
* e2
= Expression::make_temporary_reference(key_temp
,
6853 e2
= Expression::make_unary(OPERATOR_AND
, e2
, loc
);
6854 return Runtime::make_call(Runtime::MAPDELETE
, this->location(),
6864 // Flatten a builtin call expression. This turns the arguments of copy and
6865 // append into temporary expressions.
6868 Builtin_call_expression::do_flatten(Gogo
*, Named_object
*,
6869 Statement_inserter
* inserter
)
6871 if (this->code_
== BUILTIN_APPEND
6872 || this->code_
== BUILTIN_COPY
)
6874 Location loc
= this->location();
6875 Type
* at
= this->args()->front()->type();
6876 for (Expression_list::iterator pa
= this->args()->begin();
6877 pa
!= this->args()->end();
6880 if ((*pa
)->is_nil_expression())
6881 *pa
= Expression::make_slice_composite_literal(at
, NULL
, loc
);
6882 if (!(*pa
)->is_variable())
6884 Temporary_statement
* temp
=
6885 Statement::make_temporary(NULL
, *pa
, loc
);
6886 inserter
->insert(temp
);
6887 *pa
= Expression::make_temporary_reference(temp
, loc
);
6894 // Lower a make expression.
6897 Builtin_call_expression::lower_make()
6899 Location loc
= this->location();
6901 const Expression_list
* args
= this->args();
6902 if (args
== NULL
|| args
->size() < 1)
6904 this->report_error(_("not enough arguments"));
6905 return Expression::make_error(this->location());
6908 Expression_list::const_iterator parg
= args
->begin();
6910 Expression
* first_arg
= *parg
;
6911 if (!first_arg
->is_type_expression())
6913 error_at(first_arg
->location(), "expected type");
6914 this->set_is_error();
6915 return Expression::make_error(this->location());
6917 Type
* type
= first_arg
->type();
6919 bool is_slice
= false;
6920 bool is_map
= false;
6921 bool is_chan
= false;
6922 if (type
->is_slice_type())
6924 else if (type
->map_type() != NULL
)
6926 else if (type
->channel_type() != NULL
)
6930 this->report_error(_("invalid type for make function"));
6931 return Expression::make_error(this->location());
6934 bool have_big_args
= false;
6935 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
6936 int uintptr_bits
= uintptr_type
->integer_type()->bits();
6938 Type_context
int_context(Type::lookup_integer_type("int"), false);
6941 Expression
* len_arg
;
6942 if (parg
== args
->end())
6946 this->report_error(_("length required when allocating a slice"));
6947 return Expression::make_error(this->location());
6949 len_arg
= Expression::make_integer_ul(0, NULL
, loc
);
6954 len_arg
->determine_type(&int_context
);
6955 if (!this->check_int_value(len_arg
, true))
6956 return Expression::make_error(this->location());
6957 if (len_arg
->type()->integer_type() != NULL
6958 && len_arg
->type()->integer_type()->bits() > uintptr_bits
)
6959 have_big_args
= true;
6963 Expression
* cap_arg
= NULL
;
6964 if (is_slice
&& parg
!= args
->end())
6967 cap_arg
->determine_type(&int_context
);
6968 if (!this->check_int_value(cap_arg
, false))
6969 return Expression::make_error(this->location());
6971 Numeric_constant nclen
;
6972 Numeric_constant nccap
;
6975 if (len_arg
->numeric_constant_value(&nclen
)
6976 && cap_arg
->numeric_constant_value(&nccap
)
6977 && nclen
.to_unsigned_long(&vlen
) == Numeric_constant::NC_UL_VALID
6978 && nccap
.to_unsigned_long(&vcap
) == Numeric_constant::NC_UL_VALID
6981 this->report_error(_("len larger than cap"));
6982 return Expression::make_error(this->location());
6985 if (cap_arg
->type()->integer_type() != NULL
6986 && cap_arg
->type()->integer_type()->bits() > uintptr_bits
)
6987 have_big_args
= true;
6991 if (parg
!= args
->end())
6993 this->report_error(_("too many arguments to make"));
6994 return Expression::make_error(this->location());
6997 Location type_loc
= first_arg
->location();
6998 Expression
* type_arg
;
6999 if (is_slice
|| is_chan
)
7000 type_arg
= Expression::make_type_descriptor(type
, type_loc
);
7002 type_arg
= Expression::make_map_descriptor(type
->map_type(), type_loc
);
7009 if (cap_arg
== NULL
)
7010 call
= Runtime::make_call((have_big_args
7011 ? Runtime::MAKESLICE1BIG
7012 : Runtime::MAKESLICE1
),
7013 loc
, 2, type_arg
, len_arg
);
7015 call
= Runtime::make_call((have_big_args
7016 ? Runtime::MAKESLICE2BIG
7017 : Runtime::MAKESLICE2
),
7018 loc
, 3, type_arg
, len_arg
, cap_arg
);
7021 call
= Runtime::make_call((have_big_args
7022 ? Runtime::MAKEMAPBIG
7023 : Runtime::MAKEMAP
),
7024 loc
, 2, type_arg
, len_arg
);
7026 call
= Runtime::make_call((have_big_args
7027 ? Runtime::MAKECHANBIG
7028 : Runtime::MAKECHAN
),
7029 loc
, 2, type_arg
, len_arg
);
7033 return Expression::make_unsafe_cast(type
, call
, loc
);
7036 // Return whether an expression has an integer value. Report an error
7037 // if not. This is used when handling calls to the predeclared make
7041 Builtin_call_expression::check_int_value(Expression
* e
, bool is_length
)
7043 Numeric_constant nc
;
7044 if (e
->numeric_constant_value(&nc
))
7047 switch (nc
.to_unsigned_long(&v
))
7049 case Numeric_constant::NC_UL_VALID
:
7051 case Numeric_constant::NC_UL_NOTINT
:
7052 error_at(e
->location(), "non-integer %s argument to make",
7053 is_length
? "len" : "cap");
7055 case Numeric_constant::NC_UL_NEGATIVE
:
7056 error_at(e
->location(), "negative %s argument to make",
7057 is_length
? "len" : "cap");
7059 case Numeric_constant::NC_UL_BIG
:
7060 // We don't want to give a compile-time error for a 64-bit
7061 // value on a 32-bit target.
7066 if (!nc
.to_int(&val
))
7068 int bits
= mpz_sizeinbase(val
, 2);
7070 Type
* int_type
= Type::lookup_integer_type("int");
7071 if (bits
>= int_type
->integer_type()->bits())
7073 error_at(e
->location(), "%s argument too large for make",
7074 is_length
? "len" : "cap");
7081 if (e
->type()->integer_type() != NULL
)
7084 error_at(e
->location(), "non-integer %s argument to make",
7085 is_length
? "len" : "cap");
7089 // Return the type of the real or imag functions, given the type of
7090 // the argument. We need to map complex64 to float32 and complex128
7091 // to float64, so it has to be done by name. This returns NULL if it
7092 // can't figure out the type.
7095 Builtin_call_expression::real_imag_type(Type
* arg_type
)
7097 if (arg_type
== NULL
|| arg_type
->is_abstract())
7099 Named_type
* nt
= arg_type
->named_type();
7102 while (nt
->real_type()->named_type() != NULL
)
7103 nt
= nt
->real_type()->named_type();
7104 if (nt
->name() == "complex64")
7105 return Type::lookup_float_type("float32");
7106 else if (nt
->name() == "complex128")
7107 return Type::lookup_float_type("float64");
7112 // Return the type of the complex function, given the type of one of the
7113 // argments. Like real_imag_type, we have to map by name.
7116 Builtin_call_expression::complex_type(Type
* arg_type
)
7118 if (arg_type
== NULL
|| arg_type
->is_abstract())
7120 Named_type
* nt
= arg_type
->named_type();
7123 while (nt
->real_type()->named_type() != NULL
)
7124 nt
= nt
->real_type()->named_type();
7125 if (nt
->name() == "float32")
7126 return Type::lookup_complex_type("complex64");
7127 else if (nt
->name() == "float64")
7128 return Type::lookup_complex_type("complex128");
7133 // Return a single argument, or NULL if there isn't one.
7136 Builtin_call_expression::one_arg() const
7138 const Expression_list
* args
= this->args();
7139 if (args
== NULL
|| args
->size() != 1)
7141 return args
->front();
7144 // A traversal class which looks for a call or receive expression.
7146 class Find_call_expression
: public Traverse
7149 Find_call_expression()
7150 : Traverse(traverse_expressions
),
7155 expression(Expression
**);
7159 { return this->found_
; }
7166 Find_call_expression::expression(Expression
** pexpr
)
7168 if ((*pexpr
)->call_expression() != NULL
7169 || (*pexpr
)->receive_expression() != NULL
)
7171 this->found_
= true;
7172 return TRAVERSE_EXIT
;
7174 return TRAVERSE_CONTINUE
;
7177 // Return whether this is constant: len of a string constant, or len
7178 // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof,
7182 Builtin_call_expression::do_is_constant() const
7184 if (this->is_error_expression())
7186 switch (this->code_
)
7194 Expression
* arg
= this->one_arg();
7197 Type
* arg_type
= arg
->type();
7199 if (arg_type
->points_to() != NULL
7200 && arg_type
->points_to()->array_type() != NULL
7201 && !arg_type
->points_to()->is_slice_type())
7202 arg_type
= arg_type
->points_to();
7204 // The len and cap functions are only constant if there are no
7205 // function calls or channel operations in the arguments.
7206 // Otherwise we have to make the call.
7207 if (!arg
->is_constant())
7209 Find_call_expression find_call
;
7210 Expression::traverse(&arg
, &find_call
);
7211 if (find_call
.found())
7215 if (arg_type
->array_type() != NULL
7216 && arg_type
->array_type()->length() != NULL
)
7219 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
7222 bool ret
= arg
->is_constant();
7223 this->seen_
= false;
7229 case BUILTIN_SIZEOF
:
7230 case BUILTIN_ALIGNOF
:
7231 return this->one_arg() != NULL
;
7233 case BUILTIN_OFFSETOF
:
7235 Expression
* arg
= this->one_arg();
7238 return arg
->field_reference_expression() != NULL
;
7241 case BUILTIN_COMPLEX
:
7243 const Expression_list
* args
= this->args();
7244 if (args
!= NULL
&& args
->size() == 2)
7245 return args
->front()->is_constant() && args
->back()->is_constant();
7252 Expression
* arg
= this->one_arg();
7253 return arg
!= NULL
&& arg
->is_constant();
7263 // Return a numeric constant if possible.
7266 Builtin_call_expression::do_numeric_constant_value(Numeric_constant
* nc
) const
7268 if (this->code_
== BUILTIN_LEN
7269 || this->code_
== BUILTIN_CAP
)
7271 Expression
* arg
= this->one_arg();
7274 Type
* arg_type
= arg
->type();
7276 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
7279 if (arg
->string_constant_value(&sval
))
7281 nc
->set_unsigned_long(Type::lookup_integer_type("int"),
7287 if (arg_type
->points_to() != NULL
7288 && arg_type
->points_to()->array_type() != NULL
7289 && !arg_type
->points_to()->is_slice_type())
7290 arg_type
= arg_type
->points_to();
7292 if (arg_type
->array_type() != NULL
7293 && arg_type
->array_type()->length() != NULL
)
7297 Expression
* e
= arg_type
->array_type()->length();
7299 bool r
= e
->numeric_constant_value(nc
);
7300 this->seen_
= false;
7303 if (!nc
->set_type(Type::lookup_integer_type("int"), false,
7310 else if (this->code_
== BUILTIN_SIZEOF
7311 || this->code_
== BUILTIN_ALIGNOF
)
7313 Expression
* arg
= this->one_arg();
7316 Type
* arg_type
= arg
->type();
7317 if (arg_type
->is_error())
7319 if (arg_type
->is_abstract())
7325 if (this->code_
== BUILTIN_SIZEOF
)
7328 bool ok
= arg_type
->backend_type_size(this->gogo_
, &ret
);
7329 this->seen_
= false;
7333 else if (this->code_
== BUILTIN_ALIGNOF
)
7337 if (arg
->field_reference_expression() == NULL
)
7338 ok
= arg_type
->backend_type_align(this->gogo_
, &ret
);
7341 // Calling unsafe.Alignof(s.f) returns the alignment of
7342 // the type of f when it is used as a field in a struct.
7343 ok
= arg_type
->backend_type_field_align(this->gogo_
, &ret
);
7345 this->seen_
= false;
7352 nc
->set_unsigned_long(Type::lookup_integer_type("uintptr"), ret
);
7355 else if (this->code_
== BUILTIN_OFFSETOF
)
7357 Expression
* arg
= this->one_arg();
7360 Field_reference_expression
* farg
= arg
->field_reference_expression();
7366 unsigned int total_offset
= 0;
7369 Expression
* struct_expr
= farg
->expr();
7370 Type
* st
= struct_expr
->type();
7371 if (st
->struct_type() == NULL
)
7373 if (st
->named_type() != NULL
)
7374 st
->named_type()->convert(this->gogo_
);
7375 unsigned int offset
;
7377 bool ok
= st
->struct_type()->backend_field_offset(this->gogo_
,
7378 farg
->field_index(),
7380 this->seen_
= false;
7383 total_offset
+= offset
;
7384 if (farg
->implicit() && struct_expr
->field_reference_expression() != NULL
)
7386 // Go up until we reach the original base.
7387 farg
= struct_expr
->field_reference_expression();
7392 nc
->set_unsigned_long(Type::lookup_integer_type("uintptr"),
7393 static_cast<unsigned long>(total_offset
));
7396 else if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
7398 Expression
* arg
= this->one_arg();
7402 Numeric_constant argnc
;
7403 if (!arg
->numeric_constant_value(&argnc
))
7407 if (!argnc
.to_complex(&val
))
7410 Type
* type
= Builtin_call_expression::real_imag_type(argnc
.type());
7411 if (this->code_
== BUILTIN_REAL
)
7412 nc
->set_float(type
, mpc_realref(val
));
7414 nc
->set_float(type
, mpc_imagref(val
));
7418 else if (this->code_
== BUILTIN_COMPLEX
)
7420 const Expression_list
* args
= this->args();
7421 if (args
== NULL
|| args
->size() != 2)
7424 Numeric_constant rnc
;
7425 if (!args
->front()->numeric_constant_value(&rnc
))
7427 Numeric_constant inc
;
7428 if (!args
->back()->numeric_constant_value(&inc
))
7431 if (rnc
.type() != NULL
7432 && !rnc
.type()->is_abstract()
7433 && inc
.type() != NULL
7434 && !inc
.type()->is_abstract()
7435 && !Type::are_identical(rnc
.type(), inc
.type(), false, NULL
))
7439 if (!rnc
.to_float(&r
))
7442 if (!inc
.to_float(&i
))
7448 Type
* arg_type
= rnc
.type();
7449 if (arg_type
== NULL
|| arg_type
->is_abstract())
7450 arg_type
= inc
.type();
7453 mpc_init2(val
, mpc_precision
);
7454 mpc_set_fr_fr(val
, r
, i
, MPC_RNDNN
);
7458 Type
* type
= Builtin_call_expression::complex_type(arg_type
);
7459 nc
->set_complex(type
, val
);
7469 // Give an error if we are discarding the value of an expression which
7470 // should not normally be discarded. We don't give an error for
7471 // discarding the value of an ordinary function call, but we do for
7472 // builtin functions, purely for consistency with the gc compiler.
7475 Builtin_call_expression::do_discarding_value()
7477 switch (this->code_
)
7479 case BUILTIN_INVALID
:
7483 case BUILTIN_APPEND
:
7485 case BUILTIN_COMPLEX
:
7491 case BUILTIN_ALIGNOF
:
7492 case BUILTIN_OFFSETOF
:
7493 case BUILTIN_SIZEOF
:
7494 this->unused_value_error();
7499 case BUILTIN_DELETE
:
7502 case BUILTIN_PRINTLN
:
7503 case BUILTIN_RECOVER
:
7511 Builtin_call_expression::do_type()
7513 switch (this->code_
)
7515 case BUILTIN_INVALID
:
7522 const Expression_list
* args
= this->args();
7523 if (args
== NULL
|| args
->empty())
7524 return Type::make_error_type();
7525 return Type::make_pointer_type(args
->front()->type());
7531 return Type::lookup_integer_type("int");
7533 case BUILTIN_ALIGNOF
:
7534 case BUILTIN_OFFSETOF
:
7535 case BUILTIN_SIZEOF
:
7536 return Type::lookup_integer_type("uintptr");
7539 case BUILTIN_DELETE
:
7542 case BUILTIN_PRINTLN
:
7543 return Type::make_void_type();
7545 case BUILTIN_RECOVER
:
7546 return Type::make_empty_interface_type(Linemap::predeclared_location());
7548 case BUILTIN_APPEND
:
7550 const Expression_list
* args
= this->args();
7551 if (args
== NULL
|| args
->empty())
7552 return Type::make_error_type();
7553 Type
*ret
= args
->front()->type();
7554 if (!ret
->is_slice_type())
7555 return Type::make_error_type();
7562 Expression
* arg
= this->one_arg();
7564 return Type::make_error_type();
7565 Type
* t
= arg
->type();
7566 if (t
->is_abstract())
7567 t
= t
->make_non_abstract_type();
7568 t
= Builtin_call_expression::real_imag_type(t
);
7570 t
= Type::make_error_type();
7574 case BUILTIN_COMPLEX
:
7576 const Expression_list
* args
= this->args();
7577 if (args
== NULL
|| args
->size() != 2)
7578 return Type::make_error_type();
7579 Type
* t
= args
->front()->type();
7580 if (t
->is_abstract())
7582 t
= args
->back()->type();
7583 if (t
->is_abstract())
7584 t
= t
->make_non_abstract_type();
7586 t
= Builtin_call_expression::complex_type(t
);
7588 t
= Type::make_error_type();
7594 // Determine the type.
7597 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7599 if (!this->determining_types())
7602 this->fn()->determine_type_no_context();
7604 const Expression_list
* args
= this->args();
7607 Type
* arg_type
= NULL
;
7608 switch (this->code_
)
7611 case BUILTIN_PRINTLN
:
7612 // Do not force a large integer constant to "int".
7618 arg_type
= Builtin_call_expression::complex_type(context
->type
);
7619 if (arg_type
== NULL
)
7620 arg_type
= Type::lookup_complex_type("complex128");
7624 case BUILTIN_COMPLEX
:
7626 // For the complex function the type of one operand can
7627 // determine the type of the other, as in a binary expression.
7628 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7629 if (arg_type
== NULL
)
7630 arg_type
= Type::lookup_float_type("float64");
7631 if (args
!= NULL
&& args
->size() == 2)
7633 Type
* t1
= args
->front()->type();
7634 Type
* t2
= args
->back()->type();
7635 if (!t1
->is_abstract())
7637 else if (!t2
->is_abstract())
7651 for (Expression_list::const_iterator pa
= args
->begin();
7655 Type_context subcontext
;
7656 subcontext
.type
= arg_type
;
7660 // We want to print large constants, we so can't just
7661 // use the appropriate nonabstract type. Use uint64 for
7662 // an integer if we know it is nonnegative, otherwise
7663 // use int64 for a integer, otherwise use float64 for a
7664 // float or complex128 for a complex.
7665 Type
* want_type
= NULL
;
7666 Type
* atype
= (*pa
)->type();
7667 if (atype
->is_abstract())
7669 if (atype
->integer_type() != NULL
)
7671 Numeric_constant nc
;
7672 if (this->numeric_constant_value(&nc
))
7675 if (nc
.to_int(&val
))
7677 if (mpz_sgn(val
) >= 0)
7678 want_type
= Type::lookup_integer_type("uint64");
7682 if (want_type
== NULL
)
7683 want_type
= Type::lookup_integer_type("int64");
7685 else if (atype
->float_type() != NULL
)
7686 want_type
= Type::lookup_float_type("float64");
7687 else if (atype
->complex_type() != NULL
)
7688 want_type
= Type::lookup_complex_type("complex128");
7689 else if (atype
->is_abstract_string_type())
7690 want_type
= Type::lookup_string_type();
7691 else if (atype
->is_abstract_boolean_type())
7692 want_type
= Type::lookup_bool_type();
7695 subcontext
.type
= want_type
;
7699 (*pa
)->determine_type(&subcontext
);
7704 // If there is exactly one argument, return true. Otherwise give an
7705 // error message and return false.
7708 Builtin_call_expression::check_one_arg()
7710 const Expression_list
* args
= this->args();
7711 if (args
== NULL
|| args
->size() < 1)
7713 this->report_error(_("not enough arguments"));
7716 else if (args
->size() > 1)
7718 this->report_error(_("too many arguments"));
7721 if (args
->front()->is_error_expression()
7722 || args
->front()->type()->is_error())
7724 this->set_is_error();
7730 // Check argument types for a builtin function.
7733 Builtin_call_expression::do_check_types(Gogo
*)
7735 if (this->is_error_expression())
7737 switch (this->code_
)
7739 case BUILTIN_INVALID
:
7742 case BUILTIN_DELETE
:
7748 // The single argument may be either a string or an array or a
7749 // map or a channel, or a pointer to a closed array.
7750 if (this->check_one_arg())
7752 Type
* arg_type
= this->one_arg()->type();
7753 if (arg_type
->points_to() != NULL
7754 && arg_type
->points_to()->array_type() != NULL
7755 && !arg_type
->points_to()->is_slice_type())
7756 arg_type
= arg_type
->points_to();
7757 if (this->code_
== BUILTIN_CAP
)
7759 if (!arg_type
->is_error()
7760 && arg_type
->array_type() == NULL
7761 && arg_type
->channel_type() == NULL
)
7762 this->report_error(_("argument must be array or slice "
7767 if (!arg_type
->is_error()
7768 && !arg_type
->is_string_type()
7769 && arg_type
->array_type() == NULL
7770 && arg_type
->map_type() == NULL
7771 && arg_type
->channel_type() == NULL
)
7772 this->report_error(_("argument must be string or "
7773 "array or slice or map or channel"));
7780 case BUILTIN_PRINTLN
:
7782 const Expression_list
* args
= this->args();
7785 if (this->code_
== BUILTIN_PRINT
)
7786 warning_at(this->location(), 0,
7787 "no arguments for builtin function %<%s%>",
7788 (this->code_
== BUILTIN_PRINT
7794 for (Expression_list::const_iterator p
= args
->begin();
7798 Type
* type
= (*p
)->type();
7799 if (type
->is_error()
7800 || type
->is_string_type()
7801 || type
->integer_type() != NULL
7802 || type
->float_type() != NULL
7803 || type
->complex_type() != NULL
7804 || type
->is_boolean_type()
7805 || type
->points_to() != NULL
7806 || type
->interface_type() != NULL
7807 || type
->channel_type() != NULL
7808 || type
->map_type() != NULL
7809 || type
->function_type() != NULL
7810 || type
->is_slice_type())
7812 else if ((*p
)->is_type_expression())
7814 // If this is a type expression it's going to give
7815 // an error anyhow, so we don't need one here.
7818 this->report_error(_("unsupported argument type to "
7819 "builtin function"));
7826 if (this->check_one_arg())
7828 if (this->one_arg()->type()->channel_type() == NULL
)
7829 this->report_error(_("argument must be channel"));
7830 else if (!this->one_arg()->type()->channel_type()->may_send())
7831 this->report_error(_("cannot close receive-only channel"));
7836 case BUILTIN_SIZEOF
:
7837 case BUILTIN_ALIGNOF
:
7838 this->check_one_arg();
7841 case BUILTIN_RECOVER
:
7842 if (this->args() != NULL
7843 && !this->args()->empty()
7844 && !this->recover_arg_is_set_
)
7845 this->report_error(_("too many arguments"));
7848 case BUILTIN_OFFSETOF
:
7849 if (this->check_one_arg())
7851 Expression
* arg
= this->one_arg();
7852 if (arg
->field_reference_expression() == NULL
)
7853 this->report_error(_("argument must be a field reference"));
7859 const Expression_list
* args
= this->args();
7860 if (args
== NULL
|| args
->size() < 2)
7862 this->report_error(_("not enough arguments"));
7865 else if (args
->size() > 2)
7867 this->report_error(_("too many arguments"));
7870 Type
* arg1_type
= args
->front()->type();
7871 Type
* arg2_type
= args
->back()->type();
7872 if (arg1_type
->is_error() || arg2_type
->is_error())
7876 if (arg1_type
->is_slice_type())
7877 e1
= arg1_type
->array_type()->element_type();
7880 this->report_error(_("left argument must be a slice"));
7884 if (arg2_type
->is_slice_type())
7886 Type
* e2
= arg2_type
->array_type()->element_type();
7887 if (!Type::are_identical(e1
, e2
, true, NULL
))
7888 this->report_error(_("element types must be the same"));
7890 else if (arg2_type
->is_string_type())
7892 if (e1
->integer_type() == NULL
|| !e1
->integer_type()->is_byte())
7893 this->report_error(_("first argument must be []byte"));
7896 this->report_error(_("second argument must be slice or string"));
7900 case BUILTIN_APPEND
:
7902 const Expression_list
* args
= this->args();
7903 if (args
== NULL
|| args
->size() < 2)
7905 this->report_error(_("not enough arguments"));
7908 if (args
->size() > 2)
7910 this->report_error(_("too many arguments"));
7913 if (args
->front()->type()->is_error()
7914 || args
->back()->type()->is_error())
7917 Array_type
* at
= args
->front()->type()->array_type();
7918 Type
* e
= at
->element_type();
7920 // The language permits appending a string to a []byte, as a
7922 if (args
->back()->type()->is_string_type())
7924 if (e
->integer_type() != NULL
&& e
->integer_type()->is_byte())
7928 // The language says that the second argument must be
7929 // assignable to a slice of the element type of the first
7930 // argument. We already know the first argument is a slice
7932 Type
* arg2_type
= Type::make_array_type(e
, NULL
);
7934 if (!Type::are_assignable(arg2_type
, args
->back()->type(), &reason
))
7937 this->report_error(_("argument 2 has invalid type"));
7940 error_at(this->location(), "argument 2 has invalid type (%s)",
7942 this->set_is_error();
7950 if (this->check_one_arg())
7952 if (this->one_arg()->type()->complex_type() == NULL
)
7953 this->report_error(_("argument must have complex type"));
7957 case BUILTIN_COMPLEX
:
7959 const Expression_list
* args
= this->args();
7960 if (args
== NULL
|| args
->size() < 2)
7961 this->report_error(_("not enough arguments"));
7962 else if (args
->size() > 2)
7963 this->report_error(_("too many arguments"));
7964 else if (args
->front()->is_error_expression()
7965 || args
->front()->type()->is_error()
7966 || args
->back()->is_error_expression()
7967 || args
->back()->type()->is_error())
7968 this->set_is_error();
7969 else if (!Type::are_identical(args
->front()->type(),
7970 args
->back()->type(), true, NULL
))
7971 this->report_error(_("complex arguments must have identical types"));
7972 else if (args
->front()->type()->float_type() == NULL
)
7973 this->report_error(_("complex arguments must have "
7974 "floating-point type"));
7984 Builtin_call_expression::do_copy()
7986 Call_expression
* bce
=
7987 new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
7988 this->args()->copy(),
7992 if (this->varargs_are_lowered())
7993 bce
->set_varargs_are_lowered();
7997 // Return the backend representation for a builtin function.
8000 Builtin_call_expression::do_get_backend(Translate_context
* context
)
8002 Gogo
* gogo
= context
->gogo();
8003 Location location
= this->location();
8004 switch (this->code_
)
8006 case BUILTIN_INVALID
:
8014 const Expression_list
* args
= this->args();
8015 go_assert(args
!= NULL
&& args
->size() == 1);
8016 Expression
* arg
= args
->front();
8017 Type
* arg_type
= arg
->type();
8021 go_assert(saw_errors());
8022 return context
->backend()->error_expression();
8025 this->seen_
= false;
8026 if (arg_type
->points_to() != NULL
)
8028 arg_type
= arg_type
->points_to();
8029 go_assert(arg_type
->array_type() != NULL
8030 && !arg_type
->is_slice_type());
8031 arg
= Expression::make_unary(OPERATOR_MULT
, arg
, location
);
8034 Type
* int_type
= Type::lookup_integer_type("int");
8036 if (this->code_
== BUILTIN_LEN
)
8038 if (arg_type
->is_string_type())
8039 val
= Expression::make_string_info(arg
, STRING_INFO_LENGTH
,
8041 else if (arg_type
->array_type() != NULL
)
8045 go_assert(saw_errors());
8046 return context
->backend()->error_expression();
8049 val
= arg_type
->array_type()->get_length(gogo
, arg
);
8050 this->seen_
= false;
8052 else if (arg_type
->map_type() != NULL
)
8053 val
= Runtime::make_call(Runtime::MAP_LEN
, location
, 1, arg
);
8054 else if (arg_type
->channel_type() != NULL
)
8055 val
= Runtime::make_call(Runtime::CHAN_LEN
, location
, 1, arg
);
8061 if (arg_type
->array_type() != NULL
)
8065 go_assert(saw_errors());
8066 return context
->backend()->error_expression();
8069 val
= arg_type
->array_type()->get_capacity(gogo
, arg
);
8070 this->seen_
= false;
8072 else if (arg_type
->channel_type() != NULL
)
8073 val
= Runtime::make_call(Runtime::CHAN_CAP
, location
, 1, arg
);
8078 return Expression::make_cast(int_type
, val
,
8079 location
)->get_backend(context
);
8083 case BUILTIN_PRINTLN
:
8085 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
8086 Expression
* print_stmts
= NULL
;
8088 const Expression_list
* call_args
= this->args();
8089 if (call_args
!= NULL
)
8091 for (Expression_list::const_iterator p
= call_args
->begin();
8092 p
!= call_args
->end();
8095 if (is_ln
&& p
!= call_args
->begin())
8097 Expression
* print_space
=
8098 Runtime::make_call(Runtime::PRINT_SPACE
,
8099 this->location(), 0);
8102 Expression::make_compound(print_stmts
, print_space
,
8106 Expression
* arg
= *p
;
8107 Type
* type
= arg
->type();
8108 Runtime::Function code
;
8109 if (type
->is_string_type())
8110 code
= Runtime::PRINT_STRING
;
8111 else if (type
->integer_type() != NULL
8112 && type
->integer_type()->is_unsigned())
8114 Type
* itype
= Type::lookup_integer_type("uint64");
8115 arg
= Expression::make_cast(itype
, arg
, location
);
8116 code
= Runtime::PRINT_UINT64
;
8118 else if (type
->integer_type() != NULL
)
8120 Type
* itype
= Type::lookup_integer_type("int64");
8121 arg
= Expression::make_cast(itype
, arg
, location
);
8122 code
= Runtime::PRINT_INT64
;
8124 else if (type
->float_type() != NULL
)
8126 Type
* dtype
= Type::lookup_float_type("float64");
8127 arg
= Expression::make_cast(dtype
, arg
, location
);
8128 code
= Runtime::PRINT_DOUBLE
;
8130 else if (type
->complex_type() != NULL
)
8132 Type
* ctype
= Type::lookup_complex_type("complex128");
8133 arg
= Expression::make_cast(ctype
, arg
, location
);
8134 code
= Runtime::PRINT_COMPLEX
;
8136 else if (type
->is_boolean_type())
8137 code
= Runtime::PRINT_BOOL
;
8138 else if (type
->points_to() != NULL
8139 || type
->channel_type() != NULL
8140 || type
->map_type() != NULL
8141 || type
->function_type() != NULL
)
8143 arg
= Expression::make_cast(type
, arg
, location
);
8144 code
= Runtime::PRINT_POINTER
;
8146 else if (type
->interface_type() != NULL
)
8148 if (type
->interface_type()->is_empty())
8149 code
= Runtime::PRINT_EMPTY_INTERFACE
;
8151 code
= Runtime::PRINT_INTERFACE
;
8153 else if (type
->is_slice_type())
8154 code
= Runtime::PRINT_SLICE
;
8157 go_assert(saw_errors());
8158 return context
->backend()->error_expression();
8161 Expression
* call
= Runtime::make_call(code
, location
, 1, arg
);
8162 if (print_stmts
== NULL
)
8165 print_stmts
= Expression::make_compound(print_stmts
, call
,
8172 Expression
* print_nl
=
8173 Runtime::make_call(Runtime::PRINT_NL
, location
, 0);
8174 if (print_stmts
== NULL
)
8175 print_stmts
= print_nl
;
8177 print_stmts
= Expression::make_compound(print_stmts
, print_nl
,
8181 return print_stmts
->get_backend(context
);
8186 const Expression_list
* args
= this->args();
8187 go_assert(args
!= NULL
&& args
->size() == 1);
8188 Expression
* arg
= args
->front();
8190 Type::make_empty_interface_type(Linemap::predeclared_location());
8191 arg
= Expression::convert_for_assignment(gogo
, empty
, arg
, location
);
8194 Runtime::make_call(Runtime::PANIC
, location
, 1, arg
);
8195 return panic
->get_backend(context
);
8198 case BUILTIN_RECOVER
:
8200 // The argument is set when building recover thunks. It's a
8201 // boolean value which is true if we can recover a value now.
8202 const Expression_list
* args
= this->args();
8203 go_assert(args
!= NULL
&& args
->size() == 1);
8204 Expression
* arg
= args
->front();
8206 Type::make_empty_interface_type(Linemap::predeclared_location());
8208 Expression
* nil
= Expression::make_nil(location
);
8209 nil
= Expression::convert_for_assignment(gogo
, empty
, nil
, location
);
8211 // We need to handle a deferred call to recover specially,
8212 // because it changes whether it can recover a panic or not.
8213 // See test7 in test/recover1.go.
8214 Expression
* recover
= Runtime::make_call((this->is_deferred()
8215 ? Runtime::DEFERRED_RECOVER
8216 : Runtime::RECOVER
),
8219 Expression::make_conditional(arg
, recover
, nil
, location
);
8220 return cond
->get_backend(context
);
8225 const Expression_list
* args
= this->args();
8226 go_assert(args
!= NULL
&& args
->size() == 1);
8227 Expression
* arg
= args
->front();
8228 Expression
* close
= Runtime::make_call(Runtime::CLOSE
, location
,
8230 return close
->get_backend(context
);
8233 case BUILTIN_SIZEOF
:
8234 case BUILTIN_OFFSETOF
:
8235 case BUILTIN_ALIGNOF
:
8237 Numeric_constant nc
;
8239 if (!this->numeric_constant_value(&nc
)
8240 || nc
.to_unsigned_long(&val
) != Numeric_constant::NC_UL_VALID
)
8242 go_assert(saw_errors());
8243 return context
->backend()->error_expression();
8245 Type
* uintptr_type
= Type::lookup_integer_type("uintptr");
8248 Expression
* int_cst
=
8249 Expression::make_integer_z(&ival
, uintptr_type
, location
);
8251 return int_cst
->get_backend(context
);
8256 const Expression_list
* args
= this->args();
8257 go_assert(args
!= NULL
&& args
->size() == 2);
8258 Expression
* arg1
= args
->front();
8259 Expression
* arg2
= args
->back();
8261 Type
* arg1_type
= arg1
->type();
8262 Array_type
* at
= arg1_type
->array_type();
8263 go_assert(arg1
->is_variable());
8264 Expression
* arg1_val
= at
->get_value_pointer(gogo
, arg1
);
8265 Expression
* arg1_len
= at
->get_length(gogo
, arg1
);
8267 Type
* arg2_type
= arg2
->type();
8268 go_assert(arg2
->is_variable());
8269 Expression
* arg2_val
;
8270 Expression
* arg2_len
;
8271 if (arg2_type
->is_slice_type())
8273 at
= arg2_type
->array_type();
8274 arg2_val
= at
->get_value_pointer(gogo
, arg2
);
8275 arg2_len
= at
->get_length(gogo
, arg2
);
8279 go_assert(arg2
->is_variable());
8280 arg2_val
= Expression::make_string_info(arg2
, STRING_INFO_DATA
,
8282 arg2_len
= Expression::make_string_info(arg2
, STRING_INFO_LENGTH
,
8286 Expression::make_binary(OPERATOR_LT
, arg1_len
, arg2_len
, location
);
8287 Expression
* length
=
8288 Expression::make_conditional(cond
, arg1_len
, arg2_len
, location
);
8290 Type
* element_type
= at
->element_type();
8291 Btype
* element_btype
= element_type
->get_backend(gogo
);
8292 size_t element_size
= gogo
->backend()->type_size(element_btype
);
8293 Expression
* size_expr
= Expression::make_integer_ul(element_size
,
8296 Expression
* bytecount
=
8297 Expression::make_binary(OPERATOR_MULT
, size_expr
, length
, location
);
8298 Expression
* copy
= Runtime::make_call(Runtime::COPY
, location
, 3,
8299 arg1_val
, arg2_val
, bytecount
);
8301 Expression
* compound
= Expression::make_compound(copy
, length
, location
);
8302 return compound
->get_backend(context
);
8305 case BUILTIN_APPEND
:
8307 const Expression_list
* args
= this->args();
8308 go_assert(args
!= NULL
&& args
->size() == 2);
8309 Expression
* arg1
= args
->front();
8310 Expression
* arg2
= args
->back();
8312 Array_type
* at
= arg1
->type()->array_type();
8313 Type
* element_type
= at
->element_type()->forwarded();
8315 go_assert(arg2
->is_variable());
8316 Expression
* arg2_val
;
8317 Expression
* arg2_len
;
8319 if (arg2
->type()->is_string_type()
8320 && element_type
->integer_type() != NULL
8321 && element_type
->integer_type()->is_byte())
8323 arg2_val
= Expression::make_string_info(arg2
, STRING_INFO_DATA
,
8325 arg2_len
= Expression::make_string_info(arg2
, STRING_INFO_LENGTH
,
8331 arg2_val
= at
->get_value_pointer(gogo
, arg2
);
8332 arg2_len
= at
->get_length(gogo
, arg2
);
8333 Btype
* element_btype
= element_type
->get_backend(gogo
);
8334 size
= gogo
->backend()->type_size(element_btype
);
8336 Expression
* element_size
=
8337 Expression::make_integer_ul(size
, NULL
, location
);
8339 Expression
* append
= Runtime::make_call(Runtime::APPEND
, location
, 4,
8340 arg1
, arg2_val
, arg2_len
,
8342 append
= Expression::make_unsafe_cast(arg1
->type(), append
, location
);
8343 return append
->get_backend(context
);
8349 const Expression_list
* args
= this->args();
8350 go_assert(args
!= NULL
&& args
->size() == 1);
8353 Bexpression
* bcomplex
= args
->front()->get_backend(context
);
8354 if (this->code_
== BUILTIN_REAL
)
8355 ret
= gogo
->backend()->real_part_expression(bcomplex
, location
);
8357 ret
= gogo
->backend()->imag_part_expression(bcomplex
, location
);
8361 case BUILTIN_COMPLEX
:
8363 const Expression_list
* args
= this->args();
8364 go_assert(args
!= NULL
&& args
->size() == 2);
8365 Bexpression
* breal
= args
->front()->get_backend(context
);
8366 Bexpression
* bimag
= args
->back()->get_backend(context
);
8367 return gogo
->backend()->complex_expression(breal
, bimag
, location
);
8375 // We have to support exporting a builtin call expression, because
8376 // code can set a constant to the result of a builtin expression.
8379 Builtin_call_expression::do_export(Export
* exp
) const
8381 Numeric_constant nc
;
8382 if (!this->numeric_constant_value(&nc
))
8384 error_at(this->location(), "value is not constant");
8392 Integer_expression::export_integer(exp
, val
);
8395 else if (nc
.is_float())
8398 nc
.get_float(&fval
);
8399 Float_expression::export_float(exp
, fval
);
8402 else if (nc
.is_complex())
8405 nc
.get_complex(&cval
);
8406 Complex_expression::export_complex(exp
, cval
);
8412 // A trailing space lets us reliably identify the end of the number.
8413 exp
->write_c_string(" ");
8416 // Class Call_expression.
8418 // A Go function can be viewed in a couple of different ways. The
8419 // code of a Go function becomes a backend function with parameters
8420 // whose types are simply the backend representation of the Go types.
8421 // If there are multiple results, they are returned as a backend
8424 // However, when Go code refers to a function other than simply
8425 // calling it, the backend type of that function is actually a struct.
8426 // The first field of the struct points to the Go function code
8427 // (sometimes a wrapper as described below). The remaining fields
8428 // hold addresses of closed-over variables. This struct is called a
8431 // There are a few cases to consider.
8433 // A direct function call of a known function in package scope. In
8434 // this case there are no closed-over variables, and we know the name
8435 // of the function code. We can simply produce a backend call to the
8436 // function directly, and not worry about the closure.
8438 // A direct function call of a known function literal. In this case
8439 // we know the function code and we know the closure. We generate the
8440 // function code such that it expects an additional final argument of
8441 // the closure type. We pass the closure as the last argument, after
8442 // the other arguments.
8444 // An indirect function call. In this case we have a closure. We
8445 // load the pointer to the function code from the first field of the
8446 // closure. We pass the address of the closure as the last argument.
8448 // A call to a method of an interface. Type methods are always at
8449 // package scope, so we call the function directly, and don't worry
8450 // about the closure.
8452 // This means that for a function at package scope we have two cases.
8453 // One is the direct call, which has no closure. The other is the
8454 // indirect call, which does have a closure. We can't simply ignore
8455 // the closure, even though it is the last argument, because that will
8456 // fail on targets where the function pops its arguments. So when
8457 // generating a closure for a package-scope function we set the
8458 // function code pointer in the closure to point to a wrapper
8459 // function. This wrapper function accepts a final argument that
8460 // points to the closure, ignores it, and calls the real function as a
8461 // direct function call. This wrapper will normally be efficient, and
8462 // can often simply be a tail call to the real function.
8464 // We don't use GCC's static chain pointer because 1) we don't need
8465 // it; 2) GCC only permits using a static chain to call a known
8466 // function, so we can't use it for an indirect call anyhow. Since we
8467 // can't use it for an indirect call, we may as well not worry about
8468 // using it for a direct call either.
8470 // We pass the closure last rather than first because it means that
8471 // the function wrapper we put into a closure for a package-scope
8472 // function can normally just be a tail call to the real function.
8474 // For method expressions we generate a wrapper that loads the
8475 // receiver from the closure and then calls the method. This
8476 // unfortunately forces reshuffling the arguments, since there is a
8477 // new first argument, but we can't avoid reshuffling either for
8478 // method expressions or for indirect calls of package-scope
8479 // functions, and since the latter are more common we reshuffle for
8480 // method expressions.
8482 // Note that the Go code retains the Go types. The extra final
8483 // argument only appears when we convert to the backend
8489 Call_expression::do_traverse(Traverse
* traverse
)
8491 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8492 return TRAVERSE_EXIT
;
8493 if (this->args_
!= NULL
)
8495 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8496 return TRAVERSE_EXIT
;
8498 return TRAVERSE_CONTINUE
;
8501 // Lower a call statement.
8504 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
8505 Statement_inserter
* inserter
, int)
8507 Location loc
= this->location();
8509 // A type cast can look like a function call.
8510 if (this->fn_
->is_type_expression()
8511 && this->args_
!= NULL
8512 && this->args_
->size() == 1)
8513 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8516 // Because do_type will return an error type and thus prevent future
8517 // errors, check for that case now to ensure that the error gets
8519 Function_type
* fntype
= this->get_function_type();
8522 if (!this->fn_
->type()->is_error())
8523 this->report_error(_("expected function"));
8524 return Expression::make_error(loc
);
8527 // Handle an argument which is a call to a function which returns
8528 // multiple results.
8529 if (this->args_
!= NULL
8530 && this->args_
->size() == 1
8531 && this->args_
->front()->call_expression() != NULL
)
8533 size_t rc
= this->args_
->front()->call_expression()->result_count();
8535 && ((fntype
->parameters() != NULL
8536 && (fntype
->parameters()->size() == rc
8537 || (fntype
->is_varargs()
8538 && fntype
->parameters()->size() - 1 <= rc
)))
8539 || fntype
->is_builtin()))
8541 Call_expression
* call
= this->args_
->front()->call_expression();
8542 call
->set_is_multi_value_arg();
8543 Expression_list
* args
= new Expression_list
;
8544 for (size_t i
= 0; i
< rc
; ++i
)
8545 args
->push_back(Expression::make_call_result(call
, i
));
8546 // We can't return a new call expression here, because this
8547 // one may be referenced by Call_result expressions. We
8548 // also can't delete the old arguments, because we may still
8549 // traverse them somewhere up the call stack. FIXME.
8554 // Recognize a call to a builtin function.
8555 if (fntype
->is_builtin())
8556 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8557 this->is_varargs_
, loc
);
8559 // If this call returns multiple results, create a temporary
8560 // variable for each result.
8561 size_t rc
= this->result_count();
8562 if (rc
> 1 && this->results_
== NULL
)
8564 std::vector
<Temporary_statement
*>* temps
=
8565 new std::vector
<Temporary_statement
*>;
8567 const Typed_identifier_list
* results
= fntype
->results();
8568 for (Typed_identifier_list::const_iterator p
= results
->begin();
8569 p
!= results
->end();
8572 Temporary_statement
* temp
= Statement::make_temporary(p
->type(),
8574 inserter
->insert(temp
);
8575 temps
->push_back(temp
);
8577 this->results_
= temps
;
8580 // Handle a call to a varargs function by packaging up the extra
8582 if (fntype
->is_varargs())
8584 const Typed_identifier_list
* parameters
= fntype
->parameters();
8585 go_assert(parameters
!= NULL
&& !parameters
->empty());
8586 Type
* varargs_type
= parameters
->back().type();
8587 this->lower_varargs(gogo
, function
, inserter
, varargs_type
,
8588 parameters
->size());
8591 // If this is call to a method, call the method directly passing the
8592 // object as the first parameter.
8593 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8596 Named_object
* methodfn
= bme
->function();
8597 Expression
* first_arg
= bme
->first_argument();
8599 // We always pass a pointer when calling a method.
8600 if (first_arg
->type()->points_to() == NULL
8601 && !first_arg
->type()->is_error())
8603 first_arg
= Expression::make_unary(OPERATOR_AND
, first_arg
, loc
);
8604 // We may need to create a temporary variable so that we can
8605 // take the address. We can't do that here because it will
8606 // mess up the order of evaluation.
8607 Unary_expression
* ue
= static_cast<Unary_expression
*>(first_arg
);
8608 ue
->set_create_temp();
8611 // If we are calling a method which was inherited from an
8612 // embedded struct, and the method did not get a stub, then the
8613 // first type may be wrong.
8614 Type
* fatype
= bme
->first_argument_type();
8617 if (fatype
->points_to() == NULL
)
8618 fatype
= Type::make_pointer_type(fatype
);
8619 first_arg
= Expression::make_unsafe_cast(fatype
, first_arg
, loc
);
8622 Expression_list
* new_args
= new Expression_list();
8623 new_args
->push_back(first_arg
);
8624 if (this->args_
!= NULL
)
8626 for (Expression_list::const_iterator p
= this->args_
->begin();
8627 p
!= this->args_
->end();
8629 new_args
->push_back(*p
);
8632 // We have to change in place because this structure may be
8633 // referenced by Call_result_expressions. We can't delete the
8634 // old arguments, because we may be traversing them up in some
8636 this->args_
= new_args
;
8637 this->fn_
= Expression::make_func_reference(methodfn
, NULL
,
8644 // Lower a call to a varargs function. FUNCTION is the function in
8645 // which the call occurs--it's not the function we are calling.
8646 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8647 // PARAM_COUNT is the number of parameters of the function we are
8648 // calling; the last of these parameters will be the varargs
8652 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8653 Statement_inserter
* inserter
,
8654 Type
* varargs_type
, size_t param_count
)
8656 if (this->varargs_are_lowered_
)
8659 Location loc
= this->location();
8661 go_assert(param_count
> 0);
8662 go_assert(varargs_type
->is_slice_type());
8664 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8665 if (arg_count
< param_count
- 1)
8667 // Not enough arguments; will be caught in check_types.
8671 Expression_list
* old_args
= this->args_
;
8672 Expression_list
* new_args
= new Expression_list();
8673 bool push_empty_arg
= false;
8674 if (old_args
== NULL
|| old_args
->empty())
8676 go_assert(param_count
== 1);
8677 push_empty_arg
= true;
8681 Expression_list::const_iterator pa
;
8683 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8685 if (static_cast<size_t>(i
) == param_count
)
8687 new_args
->push_back(*pa
);
8690 // We have reached the varargs parameter.
8692 bool issued_error
= false;
8693 if (pa
== old_args
->end())
8694 push_empty_arg
= true;
8695 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8696 new_args
->push_back(*pa
);
8697 else if (this->is_varargs_
)
8699 if ((*pa
)->type()->is_slice_type())
8700 this->report_error(_("too many arguments"));
8703 error_at(this->location(),
8704 _("invalid use of %<...%> with non-slice"));
8705 this->set_is_error();
8711 Type
* element_type
= varargs_type
->array_type()->element_type();
8712 Expression_list
* vals
= new Expression_list
;
8713 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8715 // Check types here so that we get a better message.
8716 Type
* patype
= (*pa
)->type();
8717 Location paloc
= (*pa
)->location();
8718 if (!this->check_argument_type(i
, element_type
, patype
,
8719 paloc
, issued_error
))
8721 vals
->push_back(*pa
);
8724 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8725 gogo
->lower_expression(function
, inserter
, &val
);
8726 new_args
->push_back(val
);
8731 new_args
->push_back(Expression::make_nil(loc
));
8733 // We can't return a new call expression here, because this one may
8734 // be referenced by Call_result expressions. FIXME. We can't
8735 // delete OLD_ARGS because we may have both a Call_expression and a
8736 // Builtin_call_expression which refer to them. FIXME.
8737 this->args_
= new_args
;
8738 this->varargs_are_lowered_
= true;
8741 // Flatten a call with multiple results into a temporary.
8744 Call_expression::do_flatten(Gogo
* gogo
, Named_object
*,
8745 Statement_inserter
* inserter
)
8747 if (this->classification() == EXPRESSION_ERROR
)
8750 // Add temporary variables for all arguments that require type
8752 Function_type
* fntype
= this->get_function_type();
8755 go_assert(saw_errors());
8758 if (this->args_
!= NULL
&& !this->args_
->empty()
8759 && fntype
->parameters() != NULL
&& !fntype
->parameters()->empty())
8761 bool is_interface_method
=
8762 this->fn_
->interface_field_reference_expression() != NULL
;
8764 Expression_list
*args
= new Expression_list();
8765 Typed_identifier_list::const_iterator pp
= fntype
->parameters()->begin();
8766 Expression_list::const_iterator pa
= this->args_
->begin();
8767 if (!is_interface_method
&& fntype
->is_method())
8769 // The receiver argument.
8770 args
->push_back(*pa
);
8773 for (; pa
!= this->args_
->end(); ++pa
, ++pp
)
8775 go_assert(pp
!= fntype
->parameters()->end());
8776 if (Type::are_identical(pp
->type(), (*pa
)->type(), true, NULL
))
8777 args
->push_back(*pa
);
8780 Location loc
= (*pa
)->location();
8782 Expression::convert_for_assignment(gogo
, pp
->type(), *pa
, loc
);
8783 Temporary_statement
* temp
=
8784 Statement::make_temporary(pp
->type(), arg
, loc
);
8785 inserter
->insert(temp
);
8786 args
->push_back(Expression::make_temporary_reference(temp
, loc
));
8793 size_t rc
= this->result_count();
8794 if (rc
> 1 && this->call_temp_
== NULL
)
8796 Struct_field_list
* sfl
= new Struct_field_list();
8797 Function_type
* fntype
= this->get_function_type();
8798 const Typed_identifier_list
* results
= fntype
->results();
8799 Location loc
= this->location();
8803 for (Typed_identifier_list::const_iterator p
= results
->begin();
8804 p
!= results
->end();
8807 snprintf(buf
, sizeof buf
, "res%d", i
);
8808 sfl
->push_back(Struct_field(Typed_identifier(buf
, p
->type(), loc
)));
8811 Struct_type
* st
= Type::make_struct_type(sfl
, loc
);
8812 this->call_temp_
= Statement::make_temporary(st
, NULL
, loc
);
8813 inserter
->insert(this->call_temp_
);
8819 // Get the function type. This can return NULL in error cases.
8822 Call_expression::get_function_type() const
8824 return this->fn_
->type()->function_type();
8827 // Return the number of values which this call will return.
8830 Call_expression::result_count() const
8832 const Function_type
* fntype
= this->get_function_type();
8835 if (fntype
->results() == NULL
)
8837 return fntype
->results()->size();
8840 // Return the temporary which holds a result.
8842 Temporary_statement
*
8843 Call_expression::result(size_t i
) const
8845 if (this->results_
== NULL
|| this->results_
->size() <= i
)
8847 go_assert(saw_errors());
8850 return (*this->results_
)[i
];
8853 // Set the number of results expected from a call expression.
8856 Call_expression::set_expected_result_count(size_t count
)
8858 go_assert(this->expected_result_count_
== 0);
8859 this->expected_result_count_
= count
;
8862 // Return whether this is a call to the predeclared function recover.
8865 Call_expression::is_recover_call() const
8867 return this->do_is_recover_call();
8870 // Set the argument to the recover function.
8873 Call_expression::set_recover_arg(Expression
* arg
)
8875 this->do_set_recover_arg(arg
);
8878 // Virtual functions also implemented by Builtin_call_expression.
8881 Call_expression::do_is_recover_call() const
8887 Call_expression::do_set_recover_arg(Expression
*)
8892 // We have found an error with this call expression; return true if
8893 // we should report it.
8896 Call_expression::issue_error()
8898 if (this->issued_error_
)
8902 this->issued_error_
= true;
8910 Call_expression::do_type()
8912 if (this->type_
!= NULL
)
8916 Function_type
* fntype
= this->get_function_type();
8918 return Type::make_error_type();
8920 const Typed_identifier_list
* results
= fntype
->results();
8921 if (results
== NULL
)
8922 ret
= Type::make_void_type();
8923 else if (results
->size() == 1)
8924 ret
= results
->begin()->type();
8926 ret
= Type::make_call_multiple_result_type(this);
8933 // Determine types for a call expression. We can use the function
8934 // parameter types to set the types of the arguments.
8937 Call_expression::do_determine_type(const Type_context
*)
8939 if (!this->determining_types())
8942 this->fn_
->determine_type_no_context();
8943 Function_type
* fntype
= this->get_function_type();
8944 const Typed_identifier_list
* parameters
= NULL
;
8946 parameters
= fntype
->parameters();
8947 if (this->args_
!= NULL
)
8949 Typed_identifier_list::const_iterator pt
;
8950 if (parameters
!= NULL
)
8951 pt
= parameters
->begin();
8953 for (Expression_list::const_iterator pa
= this->args_
->begin();
8954 pa
!= this->args_
->end();
8960 // If this is a method, the first argument is the
8962 if (fntype
!= NULL
&& fntype
->is_method())
8964 Type
* rtype
= fntype
->receiver()->type();
8965 // The receiver is always passed as a pointer.
8966 if (rtype
->points_to() == NULL
)
8967 rtype
= Type::make_pointer_type(rtype
);
8968 Type_context
subcontext(rtype
, false);
8969 (*pa
)->determine_type(&subcontext
);
8974 if (parameters
!= NULL
&& pt
!= parameters
->end())
8976 Type_context
subcontext(pt
->type(), false);
8977 (*pa
)->determine_type(&subcontext
);
8981 (*pa
)->determine_type_no_context();
8986 // Called when determining types for a Call_expression. Return true
8987 // if we should go ahead, false if they have already been determined.
8990 Call_expression::determining_types()
8992 if (this->types_are_determined_
)
8996 this->types_are_determined_
= true;
9001 // Check types for parameter I.
9004 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
9005 const Type
* argument_type
,
9006 Location argument_location
,
9010 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
9015 error_at(argument_location
, "argument %d has incompatible type", i
);
9017 error_at(argument_location
,
9018 "argument %d has incompatible type (%s)",
9021 this->set_is_error();
9030 Call_expression::do_check_types(Gogo
*)
9032 if (this->classification() == EXPRESSION_ERROR
)
9035 Function_type
* fntype
= this->get_function_type();
9038 if (!this->fn_
->type()->is_error())
9039 this->report_error(_("expected function"));
9043 if (this->expected_result_count_
!= 0
9044 && this->expected_result_count_
!= this->result_count())
9046 if (this->issue_error())
9047 this->report_error(_("function result count mismatch"));
9048 this->set_is_error();
9052 bool is_method
= fntype
->is_method();
9055 go_assert(this->args_
!= NULL
&& !this->args_
->empty());
9056 Type
* rtype
= fntype
->receiver()->type();
9057 Expression
* first_arg
= this->args_
->front();
9058 // We dereference the values since receivers are always passed
9061 if (!Type::are_assignable(rtype
->deref(), first_arg
->type()->deref(),
9065 this->report_error(_("incompatible type for receiver"));
9068 error_at(this->location(),
9069 "incompatible type for receiver (%s)",
9071 this->set_is_error();
9076 // Note that varargs was handled by the lower_varargs() method, so
9077 // we don't have to worry about it here unless something is wrong.
9078 if (this->is_varargs_
&& !this->varargs_are_lowered_
)
9080 if (!fntype
->is_varargs())
9082 error_at(this->location(),
9083 _("invalid use of %<...%> calling non-variadic function"));
9084 this->set_is_error();
9089 const Typed_identifier_list
* parameters
= fntype
->parameters();
9090 if (this->args_
== NULL
)
9092 if (parameters
!= NULL
&& !parameters
->empty())
9093 this->report_error(_("not enough arguments"));
9095 else if (parameters
== NULL
)
9097 if (!is_method
|| this->args_
->size() > 1)
9098 this->report_error(_("too many arguments"));
9100 else if (this->args_
->size() == 1
9101 && this->args_
->front()->call_expression() != NULL
9102 && this->args_
->front()->call_expression()->result_count() > 1)
9104 // This is F(G()) when G returns more than one result. If the
9105 // results can be matched to parameters, it would have been
9106 // lowered in do_lower. If we get here we know there is a
9108 if (this->args_
->front()->call_expression()->result_count()
9109 < parameters
->size())
9110 this->report_error(_("not enough arguments"));
9112 this->report_error(_("too many arguments"));
9117 Expression_list::const_iterator pa
= this->args_
->begin();
9120 for (Typed_identifier_list::const_iterator pt
= parameters
->begin();
9121 pt
!= parameters
->end();
9124 if (pa
== this->args_
->end())
9126 this->report_error(_("not enough arguments"));
9129 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
9130 (*pa
)->location(), false);
9132 if (pa
!= this->args_
->end())
9133 this->report_error(_("too many arguments"));
9138 Call_expression::do_copy()
9140 Call_expression
* call
=
9141 Expression::make_call(this->fn_
->copy(),
9142 (this->args_
== NULL
9144 : this->args_
->copy()),
9145 this->is_varargs_
, this->location());
9147 if (this->varargs_are_lowered_
)
9148 call
->set_varargs_are_lowered();
9152 // Return whether we have to use a temporary variable to ensure that
9153 // we evaluate this call expression in order. If the call returns no
9154 // results then it will inevitably be executed last.
9157 Call_expression::do_must_eval_in_order() const
9159 return this->result_count() > 0;
9162 // Get the function and the first argument to use when calling an
9163 // interface method.
9166 Call_expression::interface_method_function(
9167 Interface_field_reference_expression
* interface_method
,
9168 Expression
** first_arg_ptr
)
9170 *first_arg_ptr
= interface_method
->get_underlying_object();
9171 return interface_method
->get_function();
9174 // Build the call expression.
9177 Call_expression::do_get_backend(Translate_context
* context
)
9179 if (this->call_
!= NULL
)
9182 Function_type
* fntype
= this->get_function_type();
9184 return context
->backend()->error_expression();
9186 if (this->fn_
->is_error_expression())
9187 return context
->backend()->error_expression();
9189 Gogo
* gogo
= context
->gogo();
9190 Location location
= this->location();
9192 Func_expression
* func
= this->fn_
->func_expression();
9193 Interface_field_reference_expression
* interface_method
=
9194 this->fn_
->interface_field_reference_expression();
9195 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
9196 const bool is_interface_method
= interface_method
!= NULL
;
9198 bool has_closure_arg
;
9200 has_closure_arg
= true;
9201 else if (func
!= NULL
)
9202 has_closure_arg
= false;
9203 else if (is_interface_method
)
9204 has_closure_arg
= false;
9206 has_closure_arg
= true;
9209 std::vector
<Bexpression
*> fn_args
;
9210 if (this->args_
== NULL
|| this->args_
->empty())
9212 nargs
= is_interface_method
? 1 : 0;
9216 else if (fntype
->parameters() == NULL
|| fntype
->parameters()->empty())
9218 // Passing a receiver parameter.
9219 go_assert(!is_interface_method
9220 && fntype
->is_method()
9221 && this->args_
->size() == 1);
9224 fn_args
[0] = this->args_
->front()->get_backend(context
);
9228 const Typed_identifier_list
* params
= fntype
->parameters();
9230 nargs
= this->args_
->size();
9231 int i
= is_interface_method
? 1 : 0;
9233 fn_args
.resize(nargs
);
9235 Typed_identifier_list::const_iterator pp
= params
->begin();
9236 Expression_list::const_iterator pe
= this->args_
->begin();
9237 if (!is_interface_method
&& fntype
->is_method())
9239 fn_args
[i
] = (*pe
)->get_backend(context
);
9243 for (; pe
!= this->args_
->end(); ++pe
, ++pp
, ++i
)
9245 go_assert(pp
!= params
->end());
9247 Expression::convert_for_assignment(gogo
, pp
->type(), *pe
,
9249 fn_args
[i
] = arg
->get_backend(context
);
9251 go_assert(pp
== params
->end());
9252 go_assert(i
== nargs
);
9256 Expression
* closure
= NULL
;
9259 Named_object
* no
= func
->named_object();
9260 fn
= Expression::make_func_code_reference(no
, location
);
9262 closure
= func
->closure();
9264 else if (!is_interface_method
)
9266 closure
= this->fn_
;
9268 // The backend representation of this function type is a pointer
9269 // to a struct whose first field is the actual function to call.
9271 Type::make_pointer_type(
9272 Type::make_pointer_type(Type::make_void_type()));
9273 fn
= Expression::make_unsafe_cast(pfntype
, this->fn_
, location
);
9274 fn
= Expression::make_unary(OPERATOR_MULT
, fn
, location
);
9278 Expression
* first_arg
;
9279 fn
= this->interface_method_function(interface_method
, &first_arg
);
9280 fn_args
[0] = first_arg
->get_backend(context
);
9283 if (!has_closure_arg
)
9284 go_assert(closure
== NULL
);
9287 // Pass the closure argument by calling the function function
9288 // __go_set_closure. In the order_evaluations pass we have
9289 // ensured that if any parameters contain call expressions, they
9290 // will have been moved out to temporary variables.
9291 go_assert(closure
!= NULL
);
9292 Expression
* set_closure
=
9293 Runtime::make_call(Runtime::SET_CLOSURE
, location
, 1, closure
);
9294 fn
= Expression::make_compound(set_closure
, fn
, location
);
9297 Bexpression
* bfn
= fn
->get_backend(context
);
9299 // When not calling a named function directly, use a type conversion
9300 // in case the type of the function is a recursive type which refers
9301 // to itself. We don't do this for an interface method because 1)
9302 // an interface method never refers to itself, so we always have a
9303 // function type here; 2) we pass an extra first argument to an
9304 // interface method, so fntype is not correct.
9305 if (func
== NULL
&& !is_interface_method
)
9307 Btype
* bft
= fntype
->get_backend_fntype(gogo
);
9308 bfn
= gogo
->backend()->convert_expression(bft
, bfn
, location
);
9311 Bexpression
* call
= gogo
->backend()->call_expression(bfn
, fn_args
, location
);
9313 if (this->results_
!= NULL
)
9315 go_assert(this->call_temp_
!= NULL
);
9316 Expression
* call_ref
=
9317 Expression::make_temporary_reference(this->call_temp_
, location
);
9318 Bexpression
* bcall_ref
= call_ref
->get_backend(context
);
9319 Bstatement
* assn_stmt
=
9320 gogo
->backend()->assignment_statement(bcall_ref
, call
, location
);
9322 this->call_
= this->set_results(context
, bcall_ref
);
9324 Bexpression
* set_and_call
=
9325 gogo
->backend()->compound_expression(assn_stmt
, this->call_
,
9327 return set_and_call
;
9334 // Set the result variables if this call returns multiple results.
9337 Call_expression::set_results(Translate_context
* context
, Bexpression
* call
)
9339 Gogo
* gogo
= context
->gogo();
9341 Bexpression
* results
= NULL
;
9342 Location loc
= this->location();
9344 size_t rc
= this->result_count();
9345 for (size_t i
= 0; i
< rc
; ++i
)
9347 Temporary_statement
* temp
= this->result(i
);
9350 go_assert(saw_errors());
9351 return gogo
->backend()->error_expression();
9353 Temporary_reference_expression
* ref
=
9354 Expression::make_temporary_reference(temp
, loc
);
9355 ref
->set_is_lvalue();
9357 Bexpression
* result_ref
= ref
->get_backend(context
);
9358 Bexpression
* call_result
=
9359 gogo
->backend()->struct_field_expression(call
, i
, loc
);
9360 Bstatement
* assn_stmt
=
9361 gogo
->backend()->assignment_statement(result_ref
, call_result
, loc
);
9363 Bexpression
* result
=
9364 gogo
->backend()->compound_expression(assn_stmt
, call_result
, loc
);
9366 if (results
== NULL
)
9370 Bstatement
* expr_stmt
= gogo
->backend()->expression_statement(result
);
9372 gogo
->backend()->compound_expression(expr_stmt
, results
, loc
);
9378 // Dump ast representation for a call expressin.
9381 Call_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
9383 this->fn_
->dump_expression(ast_dump_context
);
9384 ast_dump_context
->ostream() << "(";
9386 ast_dump_context
->dump_expression_list(this->args_
);
9388 ast_dump_context
->ostream() << ") ";
9391 // Make a call expression.
9394 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
9397 return new Call_expression(fn
, args
, is_varargs
, location
);
9400 // A single result from a call which returns multiple results.
9402 class Call_result_expression
: public Expression
9405 Call_result_expression(Call_expression
* call
, unsigned int index
)
9406 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
9407 call_(call
), index_(index
)
9412 do_traverse(Traverse
*);
9418 do_determine_type(const Type_context
*);
9421 do_check_types(Gogo
*);
9426 return new Call_result_expression(this->call_
->call_expression(),
9431 do_must_eval_in_order() const
9435 do_get_backend(Translate_context
*);
9438 do_dump_expression(Ast_dump_context
*) const;
9441 // The underlying call expression.
9443 // Which result we want.
9444 unsigned int index_
;
9447 // Traverse a call result.
9450 Call_result_expression::do_traverse(Traverse
* traverse
)
9452 if (traverse
->remember_expression(this->call_
))
9454 // We have already traversed the call expression.
9455 return TRAVERSE_CONTINUE
;
9457 return Expression::traverse(&this->call_
, traverse
);
9463 Call_result_expression::do_type()
9465 if (this->classification() == EXPRESSION_ERROR
)
9466 return Type::make_error_type();
9468 // THIS->CALL_ can be replaced with a temporary reference due to
9469 // Call_expression::do_must_eval_in_order when there is an error.
9470 Call_expression
* ce
= this->call_
->call_expression();
9473 this->set_is_error();
9474 return Type::make_error_type();
9476 Function_type
* fntype
= ce
->get_function_type();
9479 if (ce
->issue_error())
9481 if (!ce
->fn()->type()->is_error())
9482 this->report_error(_("expected function"));
9484 this->set_is_error();
9485 return Type::make_error_type();
9487 const Typed_identifier_list
* results
= fntype
->results();
9488 if (results
== NULL
|| results
->size() < 2)
9490 if (ce
->issue_error())
9491 this->report_error(_("number of results does not match "
9492 "number of values"));
9493 return Type::make_error_type();
9495 Typed_identifier_list::const_iterator pr
= results
->begin();
9496 for (unsigned int i
= 0; i
< this->index_
; ++i
)
9498 if (pr
== results
->end())
9502 if (pr
== results
->end())
9504 if (ce
->issue_error())
9505 this->report_error(_("number of results does not match "
9506 "number of values"));
9507 return Type::make_error_type();
9512 // Check the type. Just make sure that we trigger the warning in
9516 Call_result_expression::do_check_types(Gogo
*)
9521 // Determine the type. We have nothing to do here, but the 0 result
9522 // needs to pass down to the caller.
9525 Call_result_expression::do_determine_type(const Type_context
*)
9527 this->call_
->determine_type_no_context();
9530 // Return the backend representation. We just refer to the temporary set by the
9531 // call expression. We don't do this at lowering time because it makes it
9532 // hard to evaluate the call at the right time.
9535 Call_result_expression::do_get_backend(Translate_context
* context
)
9537 Call_expression
* ce
= this->call_
->call_expression();
9540 go_assert(this->call_
->is_error_expression());
9541 return context
->backend()->error_expression();
9543 Temporary_statement
* ts
= ce
->result(this->index_
);
9546 go_assert(saw_errors());
9547 return context
->backend()->error_expression();
9549 Expression
* ref
= Expression::make_temporary_reference(ts
, this->location());
9550 return ref
->get_backend(context
);
9553 // Dump ast representation for a call result expression.
9556 Call_result_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
9559 // FIXME: Wouldn't it be better if the call is assigned to a temporary
9560 // (struct) and the fields are referenced instead.
9561 ast_dump_context
->ostream() << this->index_
<< "@(";
9562 ast_dump_context
->dump_expression(this->call_
);
9563 ast_dump_context
->ostream() << ")";
9566 // Make a reference to a single result of a call which returns
9567 // multiple results.
9570 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
9572 return new Call_result_expression(call
, index
);
9575 // Class Index_expression.
9580 Index_expression::do_traverse(Traverse
* traverse
)
9582 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
9583 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
9584 || (this->end_
!= NULL
9585 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9586 || (this->cap_
!= NULL
9587 && Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
))
9588 return TRAVERSE_EXIT
;
9589 return TRAVERSE_CONTINUE
;
9592 // Lower an index expression. This converts the generic index
9593 // expression into an array index, a string index, or a map index.
9596 Index_expression::do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
9598 Location location
= this->location();
9599 Expression
* left
= this->left_
;
9600 Expression
* start
= this->start_
;
9601 Expression
* end
= this->end_
;
9602 Expression
* cap
= this->cap_
;
9604 Type
* type
= left
->type();
9605 if (type
->is_error())
9607 go_assert(saw_errors());
9608 return Expression::make_error(location
);
9610 else if (left
->is_type_expression())
9612 error_at(location
, "attempt to index type expression");
9613 return Expression::make_error(location
);
9615 else if (type
->array_type() != NULL
)
9616 return Expression::make_array_index(left
, start
, end
, cap
, location
);
9617 else if (type
->points_to() != NULL
9618 && type
->points_to()->array_type() != NULL
9619 && !type
->points_to()->is_slice_type())
9621 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
9624 // For an ordinary index into the array, the pointer will be
9625 // dereferenced. For a slice it will not--the resulting slice
9626 // will simply reuse the pointer, which is incorrect if that
9628 if (end
!= NULL
|| cap
!= NULL
)
9629 deref
->issue_nil_check();
9631 return Expression::make_array_index(deref
, start
, end
, cap
, location
);
9633 else if (type
->is_string_type())
9637 error_at(location
, "invalid 3-index slice of string");
9638 return Expression::make_error(location
);
9640 return Expression::make_string_index(left
, start
, end
, location
);
9642 else if (type
->map_type() != NULL
)
9644 if (end
!= NULL
|| cap
!= NULL
)
9646 error_at(location
, "invalid slice of map");
9647 return Expression::make_error(location
);
9649 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
9651 if (this->is_lvalue_
)
9652 ret
->set_is_lvalue();
9658 "attempt to index object which is not array, string, or map");
9659 return Expression::make_error(location
);
9663 // Write an indexed expression
9664 // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context.
9667 Index_expression::dump_index_expression(Ast_dump_context
* ast_dump_context
,
9668 const Expression
* expr
,
9669 const Expression
* start
,
9670 const Expression
* end
,
9671 const Expression
* cap
)
9673 expr
->dump_expression(ast_dump_context
);
9674 ast_dump_context
->ostream() << "[";
9675 start
->dump_expression(ast_dump_context
);
9678 ast_dump_context
->ostream() << ":";
9679 end
->dump_expression(ast_dump_context
);
9683 ast_dump_context
->ostream() << ":";
9684 cap
->dump_expression(ast_dump_context
);
9686 ast_dump_context
->ostream() << "]";
9689 // Dump ast representation for an index expression.
9692 Index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
9695 Index_expression::dump_index_expression(ast_dump_context
, this->left_
,
9696 this->start_
, this->end_
, this->cap_
);
9699 // Make an index expression.
9702 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
9703 Expression
* cap
, Location location
)
9705 return new Index_expression(left
, start
, end
, cap
, location
);
9708 // An array index. This is used for both indexing and slicing.
9710 class Array_index_expression
: public Expression
9713 Array_index_expression(Expression
* array
, Expression
* start
,
9714 Expression
* end
, Expression
* cap
, Location location
)
9715 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
9716 array_(array
), start_(start
), end_(end
), cap_(cap
), type_(NULL
)
9721 do_traverse(Traverse
*);
9724 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
9730 do_determine_type(const Type_context
*);
9733 do_check_types(Gogo
*);
9738 return Expression::make_array_index(this->array_
->copy(),
9739 this->start_
->copy(),
9742 : this->end_
->copy()),
9745 : this->cap_
->copy()),
9750 do_must_eval_subexpressions_in_order(int* skip
) const
9757 do_is_addressable() const;
9760 do_address_taken(bool escapes
)
9761 { this->array_
->address_taken(escapes
); }
9764 do_issue_nil_check()
9765 { this->array_
->issue_nil_check(); }
9768 do_get_backend(Translate_context
*);
9771 do_dump_expression(Ast_dump_context
*) const;
9774 // The array we are getting a value from.
9776 // The start or only index.
9778 // The end index of a slice. This may be NULL for a simple array
9779 // index, or it may be a nil expression for the length of the array.
9781 // The capacity argument of a slice. This may be NULL for an array index or
9784 // The type of the expression.
9788 // Array index traversal.
9791 Array_index_expression::do_traverse(Traverse
* traverse
)
9793 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9794 return TRAVERSE_EXIT
;
9795 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9796 return TRAVERSE_EXIT
;
9797 if (this->end_
!= NULL
)
9799 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9800 return TRAVERSE_EXIT
;
9802 if (this->cap_
!= NULL
)
9804 if (Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
)
9805 return TRAVERSE_EXIT
;
9807 return TRAVERSE_CONTINUE
;
9810 // Return the type of an array index.
9813 Array_index_expression::do_type()
9815 if (this->type_
== NULL
)
9817 Array_type
* type
= this->array_
->type()->array_type();
9819 this->type_
= Type::make_error_type();
9820 else if (this->end_
== NULL
)
9821 this->type_
= type
->element_type();
9822 else if (type
->is_slice_type())
9824 // A slice of a slice has the same type as the original
9826 this->type_
= this->array_
->type()->deref();
9830 // A slice of an array is a slice.
9831 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9837 // Set the type of an array index.
9840 Array_index_expression::do_determine_type(const Type_context
*)
9842 this->array_
->determine_type_no_context();
9843 this->start_
->determine_type_no_context();
9844 if (this->end_
!= NULL
)
9845 this->end_
->determine_type_no_context();
9846 if (this->cap_
!= NULL
)
9847 this->cap_
->determine_type_no_context();
9850 // Check types of an array index.
9853 Array_index_expression::do_check_types(Gogo
*)
9855 Numeric_constant nc
;
9857 if (this->start_
->type()->integer_type() == NULL
9858 && !this->start_
->type()->is_error()
9859 && (!this->start_
->numeric_constant_value(&nc
)
9860 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
9861 this->report_error(_("index must be integer"));
9862 if (this->end_
!= NULL
9863 && this->end_
->type()->integer_type() == NULL
9864 && !this->end_
->type()->is_error()
9865 && !this->end_
->is_nil_expression()
9866 && !this->end_
->is_error_expression()
9867 && (!this->end_
->numeric_constant_value(&nc
)
9868 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
9869 this->report_error(_("slice end must be integer"));
9870 if (this->cap_
!= NULL
9871 && this->cap_
->type()->integer_type() == NULL
9872 && !this->cap_
->type()->is_error()
9873 && !this->cap_
->is_nil_expression()
9874 && !this->cap_
->is_error_expression()
9875 && (!this->cap_
->numeric_constant_value(&nc
)
9876 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
9877 this->report_error(_("slice capacity must be integer"));
9879 Array_type
* array_type
= this->array_
->type()->array_type();
9880 if (array_type
== NULL
)
9882 go_assert(this->array_
->type()->is_error());
9886 unsigned int int_bits
=
9887 Type::lookup_integer_type("int")->integer_type()->bits();
9889 Numeric_constant lvalnc
;
9891 bool lval_valid
= (array_type
->length() != NULL
9892 && array_type
->length()->numeric_constant_value(&lvalnc
)
9893 && lvalnc
.to_int(&lval
));
9894 Numeric_constant inc
;
9896 bool ival_valid
= false;
9897 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
9900 if (mpz_sgn(ival
) < 0
9901 || mpz_sizeinbase(ival
, 2) >= int_bits
9903 && (this->end_
== NULL
9904 ? mpz_cmp(ival
, lval
) >= 0
9905 : mpz_cmp(ival
, lval
) > 0)))
9907 error_at(this->start_
->location(), "array index out of bounds");
9908 this->set_is_error();
9911 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9913 Numeric_constant enc
;
9915 bool eval_valid
= false;
9916 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
9919 if (mpz_sgn(eval
) < 0
9920 || mpz_sizeinbase(eval
, 2) >= int_bits
9921 || (lval_valid
&& mpz_cmp(eval
, lval
) > 0))
9923 error_at(this->end_
->location(), "array index out of bounds");
9924 this->set_is_error();
9926 else if (ival_valid
&& mpz_cmp(ival
, eval
) > 0)
9927 this->report_error(_("inverted slice range"));
9930 Numeric_constant cnc
;
9932 if (this->cap_
!= NULL
9933 && this->cap_
->numeric_constant_value(&cnc
) && cnc
.to_int(&cval
))
9935 if (mpz_sgn(cval
) < 0
9936 || mpz_sizeinbase(cval
, 2) >= int_bits
9937 || (lval_valid
&& mpz_cmp(cval
, lval
) > 0))
9939 error_at(this->cap_
->location(), "array index out of bounds");
9940 this->set_is_error();
9942 else if (ival_valid
&& mpz_cmp(ival
, cval
) > 0)
9944 error_at(this->cap_
->location(),
9945 "invalid slice index: capacity less than start");
9946 this->set_is_error();
9948 else if (eval_valid
&& mpz_cmp(eval
, cval
) > 0)
9950 error_at(this->cap_
->location(),
9951 "invalid slice index: capacity less than length");
9952 this->set_is_error();
9965 // A slice of an array requires an addressable array. A slice of a
9966 // slice is always possible.
9967 if (this->end_
!= NULL
&& !array_type
->is_slice_type())
9969 if (!this->array_
->is_addressable())
9970 this->report_error(_("slice of unaddressable value"));
9972 this->array_
->address_taken(true);
9976 // Flatten array indexing by using temporary variables for slices and indexes.
9979 Array_index_expression::do_flatten(Gogo
*, Named_object
*,
9980 Statement_inserter
* inserter
)
9982 Location loc
= this->location();
9983 Temporary_statement
* temp
;
9984 if (this->array_
->type()->is_slice_type() && !this->array_
->is_variable())
9986 temp
= Statement::make_temporary(NULL
, this->array_
, loc
);
9987 inserter
->insert(temp
);
9988 this->array_
= Expression::make_temporary_reference(temp
, loc
);
9990 if (!this->start_
->is_variable())
9992 temp
= Statement::make_temporary(NULL
, this->start_
, loc
);
9993 inserter
->insert(temp
);
9994 this->start_
= Expression::make_temporary_reference(temp
, loc
);
9996 if (this->end_
!= NULL
9997 && !this->end_
->is_nil_expression()
9998 && !this->end_
->is_variable())
10000 temp
= Statement::make_temporary(NULL
, this->end_
, loc
);
10001 inserter
->insert(temp
);
10002 this->end_
= Expression::make_temporary_reference(temp
, loc
);
10004 if (this->cap_
!= NULL
&& !this->cap_
->is_variable())
10006 temp
= Statement::make_temporary(NULL
, this->cap_
, loc
);
10007 inserter
->insert(temp
);
10008 this->cap_
= Expression::make_temporary_reference(temp
, loc
);
10014 // Return whether this expression is addressable.
10017 Array_index_expression::do_is_addressable() const
10019 // A slice expression is not addressable.
10020 if (this->end_
!= NULL
)
10023 // An index into a slice is addressable.
10024 if (this->array_
->type()->is_slice_type())
10027 // An index into an array is addressable if the array is
10029 return this->array_
->is_addressable();
10032 // Get the backend representation for an array index.
10035 Array_index_expression::do_get_backend(Translate_context
* context
)
10037 Array_type
* array_type
= this->array_
->type()->array_type();
10038 if (array_type
== NULL
)
10040 go_assert(this->array_
->type()->is_error());
10041 return context
->backend()->error_expression();
10043 go_assert(!array_type
->is_slice_type() || this->array_
->is_variable());
10045 Location loc
= this->location();
10046 Gogo
* gogo
= context
->gogo();
10048 Type
* int_type
= Type::lookup_integer_type("int");
10049 Btype
* int_btype
= int_type
->get_backend(gogo
);
10051 // We need to convert the length and capacity to the Go "int" type here
10052 // because the length of a fixed-length array could be of type "uintptr"
10053 // and gimple disallows binary operations between "uintptr" and other
10054 // integer types. FIXME.
10055 Bexpression
* length
= NULL
;
10056 if (this->end_
== NULL
|| this->end_
->is_nil_expression())
10058 Expression
* len
= array_type
->get_length(gogo
, this->array_
);
10059 length
= len
->get_backend(context
);
10060 length
= gogo
->backend()->convert_expression(int_btype
, length
, loc
);
10063 Bexpression
* capacity
= NULL
;
10064 if (this->end_
!= NULL
)
10066 Expression
* cap
= array_type
->get_capacity(gogo
, this->array_
);
10067 capacity
= cap
->get_backend(context
);
10068 capacity
= gogo
->backend()->convert_expression(int_btype
, capacity
, loc
);
10071 Bexpression
* cap_arg
= capacity
;
10072 if (this->cap_
!= NULL
)
10074 cap_arg
= this->cap_
->get_backend(context
);
10075 cap_arg
= gogo
->backend()->convert_expression(int_btype
, cap_arg
, loc
);
10078 if (length
== NULL
)
10081 int code
= (array_type
->length() != NULL
10082 ? (this->end_
== NULL
10083 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
10084 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
10085 : (this->end_
== NULL
10086 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
10087 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
10088 Bexpression
* crash
= gogo
->runtime_error(code
, loc
)->get_backend(context
);
10090 if (this->start_
->type()->integer_type() == NULL
10091 && !Type::are_convertible(int_type
, this->start_
->type(), NULL
))
10093 go_assert(saw_errors());
10094 return context
->backend()->error_expression();
10097 Bexpression
* bad_index
=
10098 Expression::check_bounds(this->start_
, loc
)->get_backend(context
);
10100 Bexpression
* start
= this->start_
->get_backend(context
);
10101 start
= gogo
->backend()->convert_expression(int_btype
, start
, loc
);
10102 Bexpression
* start_too_large
=
10103 gogo
->backend()->binary_expression((this->end_
== NULL
10107 (this->end_
== NULL
10111 bad_index
= gogo
->backend()->binary_expression(OPERATOR_OROR
, start_too_large
,
10114 if (this->end_
== NULL
)
10116 // Simple array indexing. This has to return an l-value, so
10117 // wrap the index check into START.
10119 gogo
->backend()->conditional_expression(int_btype
, bad_index
,
10120 crash
, start
, loc
);
10123 if (array_type
->length() != NULL
)
10125 Bexpression
* array
= this->array_
->get_backend(context
);
10126 ret
= gogo
->backend()->array_index_expression(array
, start
, loc
);
10131 Expression
* valptr
=
10132 array_type
->get_value_pointer(gogo
, this->array_
);
10133 Bexpression
* ptr
= valptr
->get_backend(context
);
10134 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, start
, loc
);
10136 Type
* ele_type
= this->array_
->type()->array_type()->element_type();
10137 Btype
* ele_btype
= ele_type
->get_backend(gogo
);
10138 ret
= gogo
->backend()->indirect_expression(ele_btype
, ptr
, true, loc
);
10145 if (this->cap_
!= NULL
)
10147 Bexpression
* bounds_bcheck
=
10148 Expression::check_bounds(this->cap_
, loc
)->get_backend(context
);
10150 gogo
->backend()->binary_expression(OPERATOR_OROR
, bounds_bcheck
,
10152 cap_arg
= gogo
->backend()->convert_expression(int_btype
, cap_arg
, loc
);
10154 Bexpression
* cap_too_small
=
10155 gogo
->backend()->binary_expression(OPERATOR_LT
, cap_arg
, start
, loc
);
10156 Bexpression
* cap_too_large
=
10157 gogo
->backend()->binary_expression(OPERATOR_GT
, cap_arg
, capacity
, loc
);
10158 Bexpression
* bad_cap
=
10159 gogo
->backend()->binary_expression(OPERATOR_OROR
, cap_too_small
,
10160 cap_too_large
, loc
);
10161 bad_index
= gogo
->backend()->binary_expression(OPERATOR_OROR
, bad_cap
,
10166 if (this->end_
->is_nil_expression())
10170 Bexpression
* bounds_bcheck
=
10171 Expression::check_bounds(this->end_
, loc
)->get_backend(context
);
10174 gogo
->backend()->binary_expression(OPERATOR_OROR
, bounds_bcheck
,
10177 end
= this->end_
->get_backend(context
);
10178 end
= gogo
->backend()->convert_expression(int_btype
, end
, loc
);
10179 Bexpression
* end_too_small
=
10180 gogo
->backend()->binary_expression(OPERATOR_LT
, end
, start
, loc
);
10181 Bexpression
* end_too_large
=
10182 gogo
->backend()->binary_expression(OPERATOR_GT
, end
, cap_arg
, loc
);
10183 Bexpression
* bad_end
=
10184 gogo
->backend()->binary_expression(OPERATOR_OROR
, end_too_small
,
10185 end_too_large
, loc
);
10186 bad_index
= gogo
->backend()->binary_expression(OPERATOR_OROR
, bad_end
,
10190 Expression
* valptr
= array_type
->get_value_pointer(gogo
, this->array_
);
10191 Bexpression
* val
= valptr
->get_backend(context
);
10192 val
= gogo
->backend()->pointer_offset_expression(val
, start
, loc
);
10194 Bexpression
* result_length
=
10195 gogo
->backend()->binary_expression(OPERATOR_MINUS
, end
, start
, loc
);
10197 Bexpression
* result_capacity
=
10198 gogo
->backend()->binary_expression(OPERATOR_MINUS
, cap_arg
, start
, loc
);
10200 Btype
* struct_btype
= this->type()->get_backend(gogo
);
10201 std::vector
<Bexpression
*> init
;
10202 init
.push_back(val
);
10203 init
.push_back(result_length
);
10204 init
.push_back(result_capacity
);
10206 Bexpression
* ctor
=
10207 gogo
->backend()->constructor_expression(struct_btype
, init
, loc
);
10208 return gogo
->backend()->conditional_expression(struct_btype
, bad_index
,
10212 // Dump ast representation for an array index expression.
10215 Array_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10218 Index_expression::dump_index_expression(ast_dump_context
, this->array_
,
10219 this->start_
, this->end_
, this->cap_
);
10222 // Make an array index expression. END and CAP may be NULL.
10225 Expression::make_array_index(Expression
* array
, Expression
* start
,
10226 Expression
* end
, Expression
* cap
,
10229 return new Array_index_expression(array
, start
, end
, cap
, location
);
10232 // A string index. This is used for both indexing and slicing.
10234 class String_index_expression
: public Expression
10237 String_index_expression(Expression
* string
, Expression
* start
,
10238 Expression
* end
, Location location
)
10239 : Expression(EXPRESSION_STRING_INDEX
, location
),
10240 string_(string
), start_(start
), end_(end
)
10245 do_traverse(Traverse
*);
10248 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
10254 do_determine_type(const Type_context
*);
10257 do_check_types(Gogo
*);
10262 return Expression::make_string_index(this->string_
->copy(),
10263 this->start_
->copy(),
10264 (this->end_
== NULL
10266 : this->end_
->copy()),
10271 do_must_eval_subexpressions_in_order(int* skip
) const
10278 do_get_backend(Translate_context
*);
10281 do_dump_expression(Ast_dump_context
*) const;
10284 // The string we are getting a value from.
10285 Expression
* string_
;
10286 // The start or only index.
10287 Expression
* start_
;
10288 // The end index of a slice. This may be NULL for a single index,
10289 // or it may be a nil expression for the length of the string.
10293 // String index traversal.
10296 String_index_expression::do_traverse(Traverse
* traverse
)
10298 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
10299 return TRAVERSE_EXIT
;
10300 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
10301 return TRAVERSE_EXIT
;
10302 if (this->end_
!= NULL
)
10304 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
10305 return TRAVERSE_EXIT
;
10307 return TRAVERSE_CONTINUE
;
10311 String_index_expression::do_flatten(Gogo
*, Named_object
*,
10312 Statement_inserter
* inserter
)
10314 Temporary_statement
* temp
;
10315 Location loc
= this->location();
10316 if (!this->string_
->is_variable())
10318 temp
= Statement::make_temporary(NULL
, this->string_
, loc
);
10319 inserter
->insert(temp
);
10320 this->string_
= Expression::make_temporary_reference(temp
, loc
);
10322 if (!this->start_
->is_variable())
10324 temp
= Statement::make_temporary(NULL
, this->start_
, loc
);
10325 inserter
->insert(temp
);
10326 this->start_
= Expression::make_temporary_reference(temp
, loc
);
10328 if (this->end_
!= NULL
10329 && !this->end_
->is_nil_expression()
10330 && !this->end_
->is_variable())
10332 temp
= Statement::make_temporary(NULL
, this->end_
, loc
);
10333 inserter
->insert(temp
);
10334 this->end_
= Expression::make_temporary_reference(temp
, loc
);
10340 // Return the type of a string index.
10343 String_index_expression::do_type()
10345 if (this->end_
== NULL
)
10346 return Type::lookup_integer_type("uint8");
10348 return this->string_
->type();
10351 // Determine the type of a string index.
10354 String_index_expression::do_determine_type(const Type_context
*)
10356 this->string_
->determine_type_no_context();
10357 this->start_
->determine_type_no_context();
10358 if (this->end_
!= NULL
)
10359 this->end_
->determine_type_no_context();
10362 // Check types of a string index.
10365 String_index_expression::do_check_types(Gogo
*)
10367 Numeric_constant nc
;
10369 if (this->start_
->type()->integer_type() == NULL
10370 && !this->start_
->type()->is_error()
10371 && (!this->start_
->numeric_constant_value(&nc
)
10372 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
10373 this->report_error(_("index must be integer"));
10374 if (this->end_
!= NULL
10375 && this->end_
->type()->integer_type() == NULL
10376 && !this->end_
->type()->is_error()
10377 && !this->end_
->is_nil_expression()
10378 && !this->end_
->is_error_expression()
10379 && (!this->end_
->numeric_constant_value(&nc
)
10380 || nc
.to_unsigned_long(&v
) == Numeric_constant::NC_UL_NOTINT
))
10381 this->report_error(_("slice end must be integer"));
10384 bool sval_valid
= this->string_
->string_constant_value(&sval
);
10386 Numeric_constant inc
;
10388 bool ival_valid
= false;
10389 if (this->start_
->numeric_constant_value(&inc
) && inc
.to_int(&ival
))
10392 if (mpz_sgn(ival
) < 0
10393 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
10395 error_at(this->start_
->location(), "string index out of bounds");
10396 this->set_is_error();
10399 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
10401 Numeric_constant enc
;
10403 if (this->end_
->numeric_constant_value(&enc
) && enc
.to_int(&eval
))
10405 if (mpz_sgn(eval
) < 0
10406 || (sval_valid
&& mpz_cmp_ui(eval
, sval
.length()) > 0))
10408 error_at(this->end_
->location(), "string index out of bounds");
10409 this->set_is_error();
10411 else if (ival_valid
&& mpz_cmp(ival
, eval
) > 0)
10412 this->report_error(_("inverted slice range"));
10420 // Get the backend representation for a string index.
10423 String_index_expression::do_get_backend(Translate_context
* context
)
10425 Location loc
= this->location();
10426 Expression
* string_arg
= this->string_
;
10427 if (this->string_
->type()->points_to() != NULL
)
10428 string_arg
= Expression::make_unary(OPERATOR_MULT
, this->string_
, loc
);
10430 Expression
* bad_index
= Expression::check_bounds(this->start_
, loc
);
10432 int code
= (this->end_
== NULL
10433 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
10434 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
10436 Gogo
* gogo
= context
->gogo();
10437 Bexpression
* crash
= gogo
->runtime_error(code
, loc
)->get_backend(context
);
10439 Type
* int_type
= Type::lookup_integer_type("int");
10441 // It is possible that an error occurred earlier because the start index
10442 // cannot be represented as an integer type. In this case, we shouldn't
10443 // try casting the starting index into an integer since
10444 // Type_conversion_expression will fail to get the backend representation.
10446 if (this->start_
->type()->integer_type() == NULL
10447 && !Type::are_convertible(int_type
, this->start_
->type(), NULL
))
10449 go_assert(saw_errors());
10450 return context
->backend()->error_expression();
10453 Expression
* start
= Expression::make_cast(int_type
, this->start_
, loc
);
10455 if (this->end_
== NULL
)
10457 Expression
* length
=
10458 Expression::make_string_info(this->string_
, STRING_INFO_LENGTH
, loc
);
10460 Expression
* start_too_large
=
10461 Expression::make_binary(OPERATOR_GE
, start
, length
, loc
);
10462 bad_index
= Expression::make_binary(OPERATOR_OROR
, start_too_large
,
10464 Expression
* bytes
=
10465 Expression::make_string_info(this->string_
, STRING_INFO_DATA
, loc
);
10467 Bexpression
* bstart
= start
->get_backend(context
);
10468 Bexpression
* ptr
= bytes
->get_backend(context
);
10469 ptr
= gogo
->backend()->pointer_offset_expression(ptr
, bstart
, loc
);
10470 Btype
* ubtype
= Type::lookup_integer_type("uint8")->get_backend(gogo
);
10471 Bexpression
* index
=
10472 gogo
->backend()->indirect_expression(ubtype
, ptr
, true, loc
);
10474 Btype
* byte_btype
= bytes
->type()->points_to()->get_backend(gogo
);
10475 Bexpression
* index_error
= bad_index
->get_backend(context
);
10476 return gogo
->backend()->conditional_expression(byte_btype
, index_error
,
10477 crash
, index
, loc
);
10480 Expression
* end
= NULL
;
10481 if (this->end_
->is_nil_expression())
10482 end
= Expression::make_integer_sl(-1, int_type
, loc
);
10485 Expression
* bounds_check
= Expression::check_bounds(this->end_
, loc
);
10487 Expression::make_binary(OPERATOR_OROR
, bounds_check
, bad_index
, loc
);
10488 end
= Expression::make_cast(int_type
, this->end_
, loc
);
10491 Expression
* strslice
= Runtime::make_call(Runtime::STRING_SLICE
, loc
, 3,
10492 string_arg
, start
, end
);
10493 Bexpression
* bstrslice
= strslice
->get_backend(context
);
10495 Btype
* str_btype
= strslice
->type()->get_backend(gogo
);
10496 Bexpression
* index_error
= bad_index
->get_backend(context
);
10497 return gogo
->backend()->conditional_expression(str_btype
, index_error
,
10498 crash
, bstrslice
, loc
);
10501 // Dump ast representation for a string index expression.
10504 String_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10507 Index_expression::dump_index_expression(ast_dump_context
, this->string_
,
10508 this->start_
, this->end_
, NULL
);
10511 // Make a string index expression. END may be NULL.
10514 Expression::make_string_index(Expression
* string
, Expression
* start
,
10515 Expression
* end
, Location location
)
10517 return new String_index_expression(string
, start
, end
, location
);
10520 // Class Map_index.
10522 // Get the type of the map.
10525 Map_index_expression::get_map_type() const
10527 Map_type
* mt
= this->map_
->type()->deref()->map_type();
10529 go_assert(saw_errors());
10533 // Map index traversal.
10536 Map_index_expression::do_traverse(Traverse
* traverse
)
10538 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
10539 return TRAVERSE_EXIT
;
10540 return Expression::traverse(&this->index_
, traverse
);
10543 // We need to pass in a pointer to the key, so flatten the index into a
10544 // temporary variable if it isn't already. The value pointer will be
10545 // dereferenced and checked for nil, so flatten into a temporary to avoid
10549 Map_index_expression::do_flatten(Gogo
*, Named_object
*,
10550 Statement_inserter
* inserter
)
10552 Map_type
* mt
= this->get_map_type();
10553 if (this->index_
->type() != mt
->key_type())
10554 this->index_
= Expression::make_cast(mt
->key_type(), this->index_
,
10557 if (!this->index_
->is_variable())
10559 Temporary_statement
* temp
= Statement::make_temporary(NULL
, this->index_
,
10561 inserter
->insert(temp
);
10562 this->index_
= Expression::make_temporary_reference(temp
,
10566 if (this->value_pointer_
== NULL
)
10567 this->get_value_pointer(this->is_lvalue_
);
10568 if (!this->value_pointer_
->is_variable())
10570 Temporary_statement
* temp
=
10571 Statement::make_temporary(NULL
, this->value_pointer_
,
10573 inserter
->insert(temp
);
10574 this->value_pointer_
=
10575 Expression::make_temporary_reference(temp
, this->location());
10581 // Return the type of a map index.
10584 Map_index_expression::do_type()
10586 Map_type
* mt
= this->get_map_type();
10588 return Type::make_error_type();
10589 Type
* type
= mt
->val_type();
10590 // If this map index is in a tuple assignment, we actually return a
10591 // pointer to the value type. Tuple_map_assignment_statement is
10592 // responsible for handling this correctly. We need to get the type
10593 // right in case this gets assigned to a temporary variable.
10594 if (this->is_in_tuple_assignment_
)
10595 type
= Type::make_pointer_type(type
);
10599 // Fix the type of a map index.
10602 Map_index_expression::do_determine_type(const Type_context
*)
10604 this->map_
->determine_type_no_context();
10605 Map_type
* mt
= this->get_map_type();
10606 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
10607 Type_context
subcontext(key_type
, false);
10608 this->index_
->determine_type(&subcontext
);
10611 // Check types of a map index.
10614 Map_index_expression::do_check_types(Gogo
*)
10616 std::string reason
;
10617 Map_type
* mt
= this->get_map_type();
10620 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
10622 if (reason
.empty())
10623 this->report_error(_("incompatible type for map index"));
10626 error_at(this->location(), "incompatible type for map index (%s)",
10628 this->set_is_error();
10633 // Get the backend representation for a map index.
10636 Map_index_expression::do_get_backend(Translate_context
* context
)
10638 Map_type
* type
= this->get_map_type();
10641 go_assert(saw_errors());
10642 return context
->backend()->error_expression();
10645 go_assert(this->value_pointer_
!= NULL
10646 && this->value_pointer_
->is_variable());
10649 if (this->is_lvalue_
)
10652 Expression::make_unary(OPERATOR_MULT
, this->value_pointer_
,
10654 ret
= val
->get_backend(context
);
10656 else if (this->is_in_tuple_assignment_
)
10658 // Tuple_map_assignment_statement is responsible for using this
10660 ret
= this->value_pointer_
->get_backend(context
);
10664 Location loc
= this->location();
10666 Expression
* nil_check
=
10667 Expression::make_binary(OPERATOR_EQEQ
, this->value_pointer_
,
10668 Expression::make_nil(loc
), loc
);
10669 Bexpression
* bnil_check
= nil_check
->get_backend(context
);
10671 Expression::make_unary(OPERATOR_MULT
, this->value_pointer_
, loc
);
10672 Bexpression
* bval
= val
->get_backend(context
);
10674 Gogo
* gogo
= context
->gogo();
10675 Btype
* val_btype
= type
->val_type()->get_backend(gogo
);
10676 Bexpression
* val_zero
= gogo
->backend()->zero_expression(val_btype
);
10677 ret
= gogo
->backend()->conditional_expression(val_btype
, bnil_check
,
10678 val_zero
, bval
, loc
);
10683 // Get an expression for the map index. This returns an expression which
10684 // evaluates to a pointer to a value. The pointer will be NULL if the key is
10688 Map_index_expression::get_value_pointer(bool insert
)
10690 if (this->value_pointer_
== NULL
)
10692 Map_type
* type
= this->get_map_type();
10695 go_assert(saw_errors());
10696 return Expression::make_error(this->location());
10699 Location loc
= this->location();
10700 Expression
* map_ref
= this->map_
;
10701 if (this->map_
->type()->points_to() != NULL
)
10702 map_ref
= Expression::make_unary(OPERATOR_MULT
, map_ref
, loc
);
10704 Expression
* index_ptr
= Expression::make_unary(OPERATOR_AND
, this->index_
,
10706 Expression
* map_index
=
10707 Runtime::make_call(Runtime::MAP_INDEX
, loc
, 3,
10708 map_ref
, index_ptr
,
10709 Expression::make_boolean(insert
, loc
));
10711 Type
* val_type
= type
->val_type();
10712 this->value_pointer_
=
10713 Expression::make_unsafe_cast(Type::make_pointer_type(val_type
),
10714 map_index
, this->location());
10716 return this->value_pointer_
;
10719 // Dump ast representation for a map index expression
10722 Map_index_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
10725 Index_expression::dump_index_expression(ast_dump_context
, this->map_
,
10726 this->index_
, NULL
, NULL
);
10729 // Make a map index expression.
10731 Map_index_expression
*
10732 Expression::make_map_index(Expression
* map
, Expression
* index
,
10735 return new Map_index_expression(map
, index
, location
);
10738 // Class Field_reference_expression.
10740 // Lower a field reference expression. There is nothing to lower, but
10741 // this is where we generate the tracking information for fields with
10742 // the magic go:"track" tag.
10745 Field_reference_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
10746 Statement_inserter
* inserter
, int)
10748 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
10749 if (struct_type
== NULL
)
10751 // Error will be reported elsewhere.
10754 const Struct_field
* field
= struct_type
->field(this->field_index_
);
10757 if (!field
->has_tag())
10759 if (field
->tag().find("go:\"track\"") == std::string::npos
)
10762 // References from functions generated by the compiler don't count.
10763 if (function
!= NULL
&& function
->func_value()->is_type_specific_function())
10766 // We have found a reference to a tracked field. Build a call to
10767 // the runtime function __go_fieldtrack with a string that describes
10768 // the field. FIXME: We should only call this once per referenced
10769 // field per function, not once for each reference to the field.
10771 if (this->called_fieldtrack_
)
10773 this->called_fieldtrack_
= true;
10775 Location loc
= this->location();
10777 std::string s
= "fieldtrack \"";
10778 Named_type
* nt
= this->expr_
->type()->named_type();
10779 if (nt
== NULL
|| nt
->named_object()->package() == NULL
)
10780 s
.append(gogo
->pkgpath());
10782 s
.append(nt
->named_object()->package()->pkgpath());
10785 s
.append(Gogo::unpack_hidden_name(nt
->name()));
10787 s
.append(field
->field_name());
10790 // We can't use a string here, because internally a string holds a
10791 // pointer to the actual bytes; when the linker garbage collects the
10792 // string, it won't garbage collect the bytes. So we use a
10795 Expression
* length_expr
= Expression::make_integer_ul(s
.length(), NULL
, loc
);
10797 Type
* byte_type
= gogo
->lookup_global("byte")->type_value();
10798 Type
* array_type
= Type::make_array_type(byte_type
, length_expr
);
10800 Expression_list
* bytes
= new Expression_list();
10801 for (std::string::const_iterator p
= s
.begin(); p
!= s
.end(); p
++)
10803 unsigned char c
= static_cast<unsigned char>(*p
);
10804 bytes
->push_back(Expression::make_integer_ul(c
, NULL
, loc
));
10807 Expression
* e
= Expression::make_composite_literal(array_type
, 0, false,
10808 bytes
, false, loc
);
10810 Variable
* var
= new Variable(array_type
, e
, true, false, false, loc
);
10814 snprintf(buf
, sizeof buf
, "fieldtrack.%d", count
);
10817 Named_object
* no
= gogo
->add_variable(buf
, var
);
10818 e
= Expression::make_var_reference(no
, loc
);
10819 e
= Expression::make_unary(OPERATOR_AND
, e
, loc
);
10821 Expression
* call
= Runtime::make_call(Runtime::FIELDTRACK
, loc
, 1, e
);
10822 gogo
->lower_expression(function
, inserter
, &call
);
10823 inserter
->insert(Statement::make_statement(call
, false));
10825 // Put this function, and the global variable we just created, into
10826 // unique sections. This will permit the linker to garbage collect
10827 // them if they are not referenced. The effect is that the only
10828 // strings, indicating field references, that will wind up in the
10829 // executable will be those for functions that are actually needed.
10830 if (function
!= NULL
)
10831 function
->func_value()->set_in_unique_section();
10832 var
->set_in_unique_section();
10837 // Return the type of a field reference.
10840 Field_reference_expression::do_type()
10842 Type
* type
= this->expr_
->type();
10843 if (type
->is_error())
10845 Struct_type
* struct_type
= type
->struct_type();
10846 go_assert(struct_type
!= NULL
);
10847 return struct_type
->field(this->field_index_
)->type();
10850 // Check the types for a field reference.
10853 Field_reference_expression::do_check_types(Gogo
*)
10855 Type
* type
= this->expr_
->type();
10856 if (type
->is_error())
10858 Struct_type
* struct_type
= type
->struct_type();
10859 go_assert(struct_type
!= NULL
);
10860 go_assert(struct_type
->field(this->field_index_
) != NULL
);
10863 // Get the backend representation for a field reference.
10866 Field_reference_expression::do_get_backend(Translate_context
* context
)
10868 Bexpression
* bstruct
= this->expr_
->get_backend(context
);
10869 return context
->gogo()->backend()->struct_field_expression(bstruct
,
10870 this->field_index_
,
10874 // Dump ast representation for a field reference expression.
10877 Field_reference_expression::do_dump_expression(
10878 Ast_dump_context
* ast_dump_context
) const
10880 this->expr_
->dump_expression(ast_dump_context
);
10881 ast_dump_context
->ostream() << "." << this->field_index_
;
10884 // Make a reference to a qualified identifier in an expression.
10886 Field_reference_expression
*
10887 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
10890 return new Field_reference_expression(expr
, field_index
, location
);
10893 // Class Interface_field_reference_expression.
10895 // Return an expression for the pointer to the function to call.
10898 Interface_field_reference_expression::get_function()
10900 Expression
* ref
= this->expr_
;
10901 Location loc
= this->location();
10902 if (ref
->type()->points_to() != NULL
)
10903 ref
= Expression::make_unary(OPERATOR_MULT
, ref
, loc
);
10905 Expression
* mtable
=
10906 Expression::make_interface_info(ref
, INTERFACE_INFO_METHODS
, loc
);
10907 Struct_type
* mtable_type
= mtable
->type()->points_to()->struct_type();
10909 std::string name
= Gogo::unpack_hidden_name(this->name_
);
10910 unsigned int index
;
10911 const Struct_field
* field
= mtable_type
->find_local_field(name
, &index
);
10912 go_assert(field
!= NULL
);
10913 mtable
= Expression::make_unary(OPERATOR_MULT
, mtable
, loc
);
10914 return Expression::make_field_reference(mtable
, index
, loc
);
10917 // Return an expression for the first argument to pass to the interface
10921 Interface_field_reference_expression::get_underlying_object()
10923 Expression
* expr
= this->expr_
;
10924 if (expr
->type()->points_to() != NULL
)
10925 expr
= Expression::make_unary(OPERATOR_MULT
, expr
, this->location());
10926 return Expression::make_interface_info(expr
, INTERFACE_INFO_OBJECT
,
10933 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
10935 return Expression::traverse(&this->expr_
, traverse
);
10938 // Lower the expression. If this expression is not called, we need to
10939 // evaluate the expression twice when converting to the backend
10940 // interface. So introduce a temporary variable if necessary.
10943 Interface_field_reference_expression::do_flatten(Gogo
*, Named_object
*,
10944 Statement_inserter
* inserter
)
10946 if (!this->expr_
->is_variable())
10948 Temporary_statement
* temp
=
10949 Statement::make_temporary(this->expr_
->type(), NULL
, this->location());
10950 inserter
->insert(temp
);
10951 this->expr_
= Expression::make_set_and_use_temporary(temp
, this->expr_
,
10957 // Return the type of an interface field reference.
10960 Interface_field_reference_expression::do_type()
10962 Type
* expr_type
= this->expr_
->type();
10964 Type
* points_to
= expr_type
->points_to();
10965 if (points_to
!= NULL
)
10966 expr_type
= points_to
;
10968 Interface_type
* interface_type
= expr_type
->interface_type();
10969 if (interface_type
== NULL
)
10970 return Type::make_error_type();
10972 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
10973 if (method
== NULL
)
10974 return Type::make_error_type();
10976 return method
->type();
10979 // Determine types.
10982 Interface_field_reference_expression::do_determine_type(const Type_context
*)
10984 this->expr_
->determine_type_no_context();
10987 // Check the types for an interface field reference.
10990 Interface_field_reference_expression::do_check_types(Gogo
*)
10992 Type
* type
= this->expr_
->type();
10994 Type
* points_to
= type
->points_to();
10995 if (points_to
!= NULL
)
10998 Interface_type
* interface_type
= type
->interface_type();
10999 if (interface_type
== NULL
)
11001 if (!type
->is_error_type())
11002 this->report_error(_("expected interface or pointer to interface"));
11006 const Typed_identifier
* method
=
11007 interface_type
->find_method(this->name_
);
11008 if (method
== NULL
)
11010 error_at(this->location(), "method %qs not in interface",
11011 Gogo::message_name(this->name_
).c_str());
11012 this->set_is_error();
11017 // If an interface field reference is not simply called, then it is
11018 // represented as a closure. The closure will hold a single variable,
11019 // the value of the interface on which the method should be called.
11020 // The function will be a simple thunk that pulls the value from the
11021 // closure and calls the method with the remaining arguments.
11023 // Because method values are not common, we don't build all thunks for
11024 // all possible interface methods, but instead only build them as we
11025 // need them. In particular, we even build them on demand for
11026 // interface methods defined in other packages.
11028 Interface_field_reference_expression::Interface_method_thunks
11029 Interface_field_reference_expression::interface_method_thunks
;
11031 // Find or create the thunk to call method NAME on TYPE.
11034 Interface_field_reference_expression::create_thunk(Gogo
* gogo
,
11035 Interface_type
* type
,
11036 const std::string
& name
)
11038 std::pair
<Interface_type
*, Method_thunks
*> val(type
, NULL
);
11039 std::pair
<Interface_method_thunks::iterator
, bool> ins
=
11040 Interface_field_reference_expression::interface_method_thunks
.insert(val
);
11043 // This is the first time we have seen this interface.
11044 ins
.first
->second
= new Method_thunks();
11047 for (Method_thunks::const_iterator p
= ins
.first
->second
->begin();
11048 p
!= ins
.first
->second
->end();
11050 if (p
->first
== name
)
11053 Location loc
= type
->location();
11055 const Typed_identifier
* method_id
= type
->find_method(name
);
11056 if (method_id
== NULL
)
11057 return Named_object::make_erroneous_name(Gogo::thunk_name());
11059 Function_type
* orig_fntype
= method_id
->type()->function_type();
11060 if (orig_fntype
== NULL
)
11061 return Named_object::make_erroneous_name(Gogo::thunk_name());
11063 Struct_field_list
* sfl
= new Struct_field_list();
11064 // The type here is wrong--it should be the C function type. But it
11065 // doesn't really matter.
11066 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
11067 sfl
->push_back(Struct_field(Typed_identifier("fn.0", vt
, loc
)));
11068 sfl
->push_back(Struct_field(Typed_identifier("val.1", type
, loc
)));
11069 Type
* closure_type
= Type::make_struct_type(sfl
, loc
);
11070 closure_type
= Type::make_pointer_type(closure_type
);
11072 Function_type
* new_fntype
= orig_fntype
->copy_with_names();
11074 Named_object
* new_no
= gogo
->start_function(Gogo::thunk_name(), new_fntype
,
11077 Variable
* cvar
= new Variable(closure_type
, NULL
, false, false, false, loc
);
11078 cvar
->set_is_used();
11079 Named_object
* cp
= Named_object::make_variable("$closure", NULL
, cvar
);
11080 new_no
->func_value()->set_closure_var(cp
);
11082 gogo
->start_block(loc
);
11084 // Field 0 of the closure is the function code pointer, field 1 is
11085 // the value on which to invoke the method.
11086 Expression
* arg
= Expression::make_var_reference(cp
, loc
);
11087 arg
= Expression::make_unary(OPERATOR_MULT
, arg
, loc
);
11088 arg
= Expression::make_field_reference(arg
, 1, loc
);
11090 Expression
*ifre
= Expression::make_interface_field_reference(arg
, name
,
11093 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
11094 Expression_list
* args
;
11095 if (orig_params
== NULL
|| orig_params
->empty())
11099 const Typed_identifier_list
* new_params
= new_fntype
->parameters();
11100 args
= new Expression_list();
11101 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
11102 p
!= new_params
->end();
11105 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
11106 go_assert(p_no
!= NULL
11107 && p_no
->is_variable()
11108 && p_no
->var_value()->is_parameter());
11109 args
->push_back(Expression::make_var_reference(p_no
, loc
));
11113 Call_expression
* call
= Expression::make_call(ifre
, args
,
11114 orig_fntype
->is_varargs(),
11116 call
->set_varargs_are_lowered();
11118 Statement
* s
= Statement::make_return_from_call(call
, loc
);
11119 gogo
->add_statement(s
);
11120 Block
* b
= gogo
->finish_block(loc
);
11121 gogo
->add_block(b
, loc
);
11122 gogo
->lower_block(new_no
, b
);
11123 gogo
->flatten_block(new_no
, b
);
11124 gogo
->finish_function(loc
);
11126 ins
.first
->second
->push_back(std::make_pair(name
, new_no
));
11130 // Get the backend representation for a method value.
11133 Interface_field_reference_expression::do_get_backend(Translate_context
* context
)
11135 Interface_type
* type
= this->expr_
->type()->interface_type();
11138 go_assert(saw_errors());
11139 return context
->backend()->error_expression();
11142 Named_object
* thunk
=
11143 Interface_field_reference_expression::create_thunk(context
->gogo(),
11144 type
, this->name_
);
11145 if (thunk
->is_erroneous())
11147 go_assert(saw_errors());
11148 return context
->backend()->error_expression();
11151 // FIXME: We should lower this earlier, but we can't it lower it in
11152 // the lowering pass because at that point we don't know whether we
11153 // need to create the thunk or not. If the expression is called, we
11154 // don't need the thunk.
11156 Location loc
= this->location();
11158 Struct_field_list
* fields
= new Struct_field_list();
11159 fields
->push_back(Struct_field(Typed_identifier("fn.0",
11160 thunk
->func_value()->type(),
11162 fields
->push_back(Struct_field(Typed_identifier("val.1",
11163 this->expr_
->type(),
11165 Struct_type
* st
= Type::make_struct_type(fields
, loc
);
11167 Expression_list
* vals
= new Expression_list();
11168 vals
->push_back(Expression::make_func_code_reference(thunk
, loc
));
11169 vals
->push_back(this->expr_
);
11171 Expression
* expr
= Expression::make_struct_composite_literal(st
, vals
, loc
);
11172 Bexpression
* bclosure
=
11173 Expression::make_heap_expression(expr
, loc
)->get_backend(context
);
11175 Expression
* nil_check
=
11176 Expression::make_binary(OPERATOR_EQEQ
, this->expr_
,
11177 Expression::make_nil(loc
), loc
);
11178 Bexpression
* bnil_check
= nil_check
->get_backend(context
);
11180 Gogo
* gogo
= context
->gogo();
11181 Bexpression
* bcrash
= gogo
->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
11182 loc
)->get_backend(context
);
11184 Bexpression
* bcond
=
11185 gogo
->backend()->conditional_expression(NULL
, bnil_check
, bcrash
, NULL
, loc
);
11186 Bstatement
* cond_statement
= gogo
->backend()->expression_statement(bcond
);
11187 return gogo
->backend()->compound_expression(cond_statement
, bclosure
, loc
);
11190 // Dump ast representation for an interface field reference.
11193 Interface_field_reference_expression::do_dump_expression(
11194 Ast_dump_context
* ast_dump_context
) const
11196 this->expr_
->dump_expression(ast_dump_context
);
11197 ast_dump_context
->ostream() << "." << this->name_
;
11200 // Make a reference to a field in an interface.
11203 Expression::make_interface_field_reference(Expression
* expr
,
11204 const std::string
& field
,
11207 return new Interface_field_reference_expression(expr
, field
, location
);
11210 // A general selector. This is a Parser_expression for LEFT.NAME. It
11211 // is lowered after we know the type of the left hand side.
11213 class Selector_expression
: public Parser_expression
11216 Selector_expression(Expression
* left
, const std::string
& name
,
11218 : Parser_expression(EXPRESSION_SELECTOR
, location
),
11219 left_(left
), name_(name
)
11224 do_traverse(Traverse
* traverse
)
11225 { return Expression::traverse(&this->left_
, traverse
); }
11228 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
11233 return new Selector_expression(this->left_
->copy(), this->name_
,
11238 do_dump_expression(Ast_dump_context
* ast_dump_context
) const;
11242 lower_method_expression(Gogo
*);
11244 // The expression on the left hand side.
11246 // The name on the right hand side.
11250 // Lower a selector expression once we know the real type of the left
11254 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, Statement_inserter
*,
11257 Expression
* left
= this->left_
;
11258 if (left
->is_type_expression())
11259 return this->lower_method_expression(gogo
);
11260 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
11264 // Lower a method expression T.M or (*T).M. We turn this into a
11265 // function literal.
11268 Selector_expression::lower_method_expression(Gogo
* gogo
)
11270 Location location
= this->location();
11271 Type
* type
= this->left_
->type();
11272 const std::string
& name(this->name_
);
11275 if (type
->points_to() == NULL
)
11276 is_pointer
= false;
11280 type
= type
->points_to();
11282 Named_type
* nt
= type
->named_type();
11286 ("method expression requires named type or "
11287 "pointer to named type"));
11288 return Expression::make_error(location
);
11292 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
11293 const Typed_identifier
* imethod
= NULL
;
11294 if (method
== NULL
&& !is_pointer
)
11296 Interface_type
* it
= nt
->interface_type();
11298 imethod
= it
->find_method(name
);
11301 if (method
== NULL
&& imethod
== NULL
)
11304 error_at(location
, "type %<%s%s%> has no method %<%s%>",
11305 is_pointer
? "*" : "",
11306 nt
->message_name().c_str(),
11307 Gogo::message_name(name
).c_str());
11309 error_at(location
, "method %<%s%s%> is ambiguous in type %<%s%>",
11310 Gogo::message_name(name
).c_str(),
11311 is_pointer
? "*" : "",
11312 nt
->message_name().c_str());
11313 return Expression::make_error(location
);
11316 if (method
!= NULL
&& !is_pointer
&& !method
->is_value_method())
11318 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
11319 nt
->message_name().c_str(),
11320 Gogo::message_name(name
).c_str());
11321 return Expression::make_error(location
);
11324 // Build a new function type in which the receiver becomes the first
11326 Function_type
* method_type
;
11327 if (method
!= NULL
)
11329 method_type
= method
->type();
11330 go_assert(method_type
->is_method());
11334 method_type
= imethod
->type()->function_type();
11335 go_assert(method_type
!= NULL
&& !method_type
->is_method());
11338 const char* const receiver_name
= "$this";
11339 Typed_identifier_list
* parameters
= new Typed_identifier_list();
11340 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
11343 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
11344 if (method_parameters
!= NULL
)
11347 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
11348 p
!= method_parameters
->end();
11351 if (!p
->name().empty())
11352 parameters
->push_back(*p
);
11356 snprintf(buf
, sizeof buf
, "$param%d", i
);
11357 parameters
->push_back(Typed_identifier(buf
, p
->type(),
11363 const Typed_identifier_list
* method_results
= method_type
->results();
11364 Typed_identifier_list
* results
;
11365 if (method_results
== NULL
)
11369 results
= new Typed_identifier_list();
11370 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
11371 p
!= method_results
->end();
11373 results
->push_back(*p
);
11376 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
11378 if (method_type
->is_varargs())
11379 fntype
->set_is_varargs();
11381 // We generate methods which always takes a pointer to the receiver
11382 // as their first argument. If this is for a pointer type, we can
11383 // simply reuse the existing function. We use an internal hack to
11384 // get the right type.
11385 // FIXME: This optimization is disabled because it doesn't yet work
11386 // with function descriptors when the method expression is not
11387 // directly called.
11388 if (method
!= NULL
&& is_pointer
&& false)
11390 Named_object
* mno
= (method
->needs_stub_method()
11391 ? method
->stub_object()
11392 : method
->named_object());
11393 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
11394 f
= Expression::make_cast(fntype
, f
, location
);
11395 Type_conversion_expression
* tce
=
11396 static_cast<Type_conversion_expression
*>(f
);
11397 tce
->set_may_convert_function_types();
11401 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
11404 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
11405 go_assert(vno
!= NULL
);
11406 Expression
* ve
= Expression::make_var_reference(vno
, location
);
11408 if (method
!= NULL
)
11409 bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
11411 bm
= Expression::make_interface_field_reference(ve
, name
, location
);
11413 // Even though we found the method above, if it has an error type we
11414 // may see an error here.
11415 if (bm
->is_error_expression())
11417 gogo
->finish_function(location
);
11421 Expression_list
* args
;
11422 if (parameters
->size() <= 1)
11426 args
= new Expression_list();
11427 Typed_identifier_list::const_iterator p
= parameters
->begin();
11429 for (; p
!= parameters
->end(); ++p
)
11431 vno
= gogo
->lookup(p
->name(), NULL
);
11432 go_assert(vno
!= NULL
);
11433 args
->push_back(Expression::make_var_reference(vno
, location
));
11437 gogo
->start_block(location
);
11439 Call_expression
* call
= Expression::make_call(bm
, args
,
11440 method_type
->is_varargs(),
11443 Statement
* s
= Statement::make_return_from_call(call
, location
);
11444 gogo
->add_statement(s
);
11446 Block
* b
= gogo
->finish_block(location
);
11448 gogo
->add_block(b
, location
);
11450 // Lower the call in case there are multiple results.
11451 gogo
->lower_block(no
, b
);
11452 gogo
->flatten_block(no
, b
);
11454 gogo
->finish_function(location
);
11456 return Expression::make_func_reference(no
, NULL
, location
);
11459 // Dump the ast for a selector expression.
11462 Selector_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
11465 ast_dump_context
->dump_expression(this->left_
);
11466 ast_dump_context
->ostream() << ".";
11467 ast_dump_context
->ostream() << this->name_
;
11470 // Make a selector expression.
11473 Expression::make_selector(Expression
* left
, const std::string
& name
,
11476 return new Selector_expression(left
, name
, location
);
11479 // Implement the builtin function new.
11481 class Allocation_expression
: public Expression
11484 Allocation_expression(Type
* type
, Location location
)
11485 : Expression(EXPRESSION_ALLOCATION
, location
),
11491 do_traverse(Traverse
* traverse
)
11492 { return Type::traverse(this->type_
, traverse
); }
11496 { return Type::make_pointer_type(this->type_
); }
11499 do_determine_type(const Type_context
*)
11504 { return new Allocation_expression(this->type_
, this->location()); }
11507 do_get_backend(Translate_context
*);
11510 do_dump_expression(Ast_dump_context
*) const;
11513 // The type we are allocating.
11517 // Return the backend representation for an allocation expression.
11520 Allocation_expression::do_get_backend(Translate_context
* context
)
11522 Gogo
* gogo
= context
->gogo();
11523 Location loc
= this->location();
11524 Bexpression
* space
=
11525 gogo
->allocate_memory(this->type_
, loc
)->get_backend(context
);
11526 Btype
* pbtype
= gogo
->backend()->pointer_type(this->type_
->get_backend(gogo
));
11527 return gogo
->backend()->convert_expression(pbtype
, space
, loc
);
11530 // Dump ast representation for an allocation expression.
11533 Allocation_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
11536 ast_dump_context
->ostream() << "new(";
11537 ast_dump_context
->dump_type(this->type_
);
11538 ast_dump_context
->ostream() << ")";
11541 // Make an allocation expression.
11544 Expression::make_allocation(Type
* type
, Location location
)
11546 return new Allocation_expression(type
, location
);
11549 // Construct a struct.
11551 class Struct_construction_expression
: public Expression
11554 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
11556 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
11557 type_(type
), vals_(vals
), traverse_order_(NULL
)
11560 // Set the traversal order, used to ensure that we implement the
11561 // order of evaluation rules. Takes ownership of the argument.
11563 set_traverse_order(std::vector
<int>* traverse_order
)
11564 { this->traverse_order_
= traverse_order
; }
11566 // Return whether this is a constant initializer.
11568 is_constant_struct() const;
11572 do_traverse(Traverse
* traverse
);
11575 do_is_immutable() const;
11579 { return this->type_
; }
11582 do_determine_type(const Type_context
*);
11585 do_check_types(Gogo
*);
11590 Struct_construction_expression
* ret
=
11591 new Struct_construction_expression(this->type_
,
11592 (this->vals_
== NULL
11594 : this->vals_
->copy()),
11596 if (this->traverse_order_
!= NULL
)
11597 ret
->set_traverse_order(this->traverse_order_
);
11602 do_get_backend(Translate_context
*);
11605 do_export(Export
*) const;
11608 do_dump_expression(Ast_dump_context
*) const;
11611 // The type of the struct to construct.
11613 // The list of values, in order of the fields in the struct. A NULL
11614 // entry means that the field should be zero-initialized.
11615 Expression_list
* vals_
;
11616 // If not NULL, the order in which to traverse vals_. This is used
11617 // so that we implement the order of evaluation rules correctly.
11618 std::vector
<int>* traverse_order_
;
11624 Struct_construction_expression::do_traverse(Traverse
* traverse
)
11626 if (this->vals_
!= NULL
)
11628 if (this->traverse_order_
== NULL
)
11630 if (this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11631 return TRAVERSE_EXIT
;
11635 for (std::vector
<int>::const_iterator p
=
11636 this->traverse_order_
->begin();
11637 p
!= this->traverse_order_
->end();
11640 if (Expression::traverse(&this->vals_
->at(*p
), traverse
)
11642 return TRAVERSE_EXIT
;
11646 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11647 return TRAVERSE_EXIT
;
11648 return TRAVERSE_CONTINUE
;
11651 // Return whether this is a constant initializer.
11654 Struct_construction_expression::is_constant_struct() const
11656 if (this->vals_
== NULL
)
11658 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11659 pv
!= this->vals_
->end();
11663 && !(*pv
)->is_constant()
11664 && (!(*pv
)->is_composite_literal()
11665 || (*pv
)->is_nonconstant_composite_literal()))
11669 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11670 for (Struct_field_list::const_iterator pf
= fields
->begin();
11671 pf
!= fields
->end();
11674 // There are no constant constructors for interfaces.
11675 if (pf
->type()->interface_type() != NULL
)
11682 // Return whether this struct is immutable.
11685 Struct_construction_expression::do_is_immutable() const
11687 if (this->vals_
== NULL
)
11689 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11690 pv
!= this->vals_
->end();
11693 if (*pv
!= NULL
&& !(*pv
)->is_immutable())
11699 // Final type determination.
11702 Struct_construction_expression::do_determine_type(const Type_context
*)
11704 if (this->vals_
== NULL
)
11706 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11707 Expression_list::const_iterator pv
= this->vals_
->begin();
11708 for (Struct_field_list::const_iterator pf
= fields
->begin();
11709 pf
!= fields
->end();
11712 if (pv
== this->vals_
->end())
11716 Type_context
subcontext(pf
->type(), false);
11717 (*pv
)->determine_type(&subcontext
);
11720 // Extra values are an error we will report elsewhere; we still want
11721 // to determine the type to avoid knockon errors.
11722 for (; pv
!= this->vals_
->end(); ++pv
)
11723 (*pv
)->determine_type_no_context();
11729 Struct_construction_expression::do_check_types(Gogo
*)
11731 if (this->vals_
== NULL
)
11734 Struct_type
* st
= this->type_
->struct_type();
11735 if (this->vals_
->size() > st
->field_count())
11737 this->report_error(_("too many expressions for struct"));
11741 const Struct_field_list
* fields
= st
->fields();
11742 Expression_list::const_iterator pv
= this->vals_
->begin();
11744 for (Struct_field_list::const_iterator pf
= fields
->begin();
11745 pf
!= fields
->end();
11748 if (pv
== this->vals_
->end())
11750 this->report_error(_("too few expressions for struct"));
11757 std::string reason
;
11758 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
11760 if (reason
.empty())
11761 error_at((*pv
)->location(),
11762 "incompatible type for field %d in struct construction",
11765 error_at((*pv
)->location(),
11766 ("incompatible type for field %d in "
11767 "struct construction (%s)"),
11768 i
+ 1, reason
.c_str());
11769 this->set_is_error();
11772 go_assert(pv
== this->vals_
->end());
11775 // Return the backend representation for constructing a struct.
11778 Struct_construction_expression::do_get_backend(Translate_context
* context
)
11780 Gogo
* gogo
= context
->gogo();
11782 Btype
* btype
= this->type_
->get_backend(gogo
);
11783 if (this->vals_
== NULL
)
11784 return gogo
->backend()->zero_expression(btype
);
11786 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
11787 Expression_list::const_iterator pv
= this->vals_
->begin();
11788 std::vector
<Bexpression
*> init
;
11789 for (Struct_field_list::const_iterator pf
= fields
->begin();
11790 pf
!= fields
->end();
11793 Btype
* fbtype
= pf
->type()->get_backend(gogo
);
11794 if (pv
== this->vals_
->end())
11795 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
11796 else if (*pv
== NULL
)
11798 init
.push_back(gogo
->backend()->zero_expression(fbtype
));
11804 Expression::convert_for_assignment(gogo
, pf
->type(),
11805 *pv
, this->location());
11806 init
.push_back(val
->get_backend(context
));
11810 return gogo
->backend()->constructor_expression(btype
, init
, this->location());
11813 // Export a struct construction.
11816 Struct_construction_expression::do_export(Export
* exp
) const
11818 exp
->write_c_string("convert(");
11819 exp
->write_type(this->type_
);
11820 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11821 pv
!= this->vals_
->end();
11824 exp
->write_c_string(", ");
11826 (*pv
)->export_expression(exp
);
11828 exp
->write_c_string(")");
11831 // Dump ast representation of a struct construction expression.
11834 Struct_construction_expression::do_dump_expression(
11835 Ast_dump_context
* ast_dump_context
) const
11837 ast_dump_context
->dump_type(this->type_
);
11838 ast_dump_context
->ostream() << "{";
11839 ast_dump_context
->dump_expression_list(this->vals_
);
11840 ast_dump_context
->ostream() << "}";
11843 // Make a struct composite literal. This used by the thunk code.
11846 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
11849 go_assert(type
->struct_type() != NULL
);
11850 return new Struct_construction_expression(type
, vals
, location
);
11853 // Construct an array. This class is not used directly; instead we
11854 // use the child classes, Fixed_array_construction_expression and
11855 // Slice_construction_expression.
11857 class Array_construction_expression
: public Expression
11860 Array_construction_expression(Expression_classification classification
,
11862 const std::vector
<unsigned long>* indexes
,
11863 Expression_list
* vals
, Location location
)
11864 : Expression(classification
, location
),
11865 type_(type
), indexes_(indexes
), vals_(vals
)
11866 { go_assert(indexes
== NULL
|| indexes
->size() == vals
->size()); }
11869 // Return whether this is a constant initializer.
11871 is_constant_array() const;
11873 // Return the number of elements.
11875 element_count() const
11876 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
11880 do_traverse(Traverse
* traverse
);
11883 do_is_immutable() const;
11887 { return this->type_
; }
11890 do_determine_type(const Type_context
*);
11893 do_check_types(Gogo
*);
11896 do_export(Export
*) const;
11899 const std::vector
<unsigned long>*
11901 { return this->indexes_
; }
11903 // The list of values.
11906 { return this->vals_
; }
11908 // Get the backend constructor for the array values.
11910 get_constructor(Translate_context
* context
, Btype
* btype
);
11913 do_dump_expression(Ast_dump_context
*) const;
11916 // The type of the array to construct.
11918 // The list of indexes into the array, one for each value. This may
11919 // be NULL, in which case the indexes start at zero and increment.
11920 const std::vector
<unsigned long>* indexes_
;
11921 // The list of values. This may be NULL if there are no values.
11922 Expression_list
* vals_
;
11928 Array_construction_expression::do_traverse(Traverse
* traverse
)
11930 if (this->vals_
!= NULL
11931 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11932 return TRAVERSE_EXIT
;
11933 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11934 return TRAVERSE_EXIT
;
11935 return TRAVERSE_CONTINUE
;
11938 // Return whether this is a constant initializer.
11941 Array_construction_expression::is_constant_array() const
11943 if (this->vals_
== NULL
)
11946 // There are no constant constructors for interfaces.
11947 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
11950 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11951 pv
!= this->vals_
->end();
11955 && !(*pv
)->is_constant()
11956 && (!(*pv
)->is_composite_literal()
11957 || (*pv
)->is_nonconstant_composite_literal()))
11963 // Return whether this is an immutable array initializer.
11966 Array_construction_expression::do_is_immutable() const
11968 if (this->vals_
== NULL
)
11970 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11971 pv
!= this->vals_
->end();
11974 if (*pv
!= NULL
&& !(*pv
)->is_immutable())
11980 // Final type determination.
11983 Array_construction_expression::do_determine_type(const Type_context
*)
11985 if (this->vals_
== NULL
)
11987 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
11988 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11989 pv
!= this->vals_
->end();
11993 (*pv
)->determine_type(&subcontext
);
12000 Array_construction_expression::do_check_types(Gogo
*)
12002 if (this->vals_
== NULL
)
12005 Array_type
* at
= this->type_
->array_type();
12007 Type
* element_type
= at
->element_type();
12008 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12009 pv
!= this->vals_
->end();
12013 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
12015 error_at((*pv
)->location(),
12016 "incompatible type for element %d in composite literal",
12018 this->set_is_error();
12023 // Get a constructor expression for the array values.
12026 Array_construction_expression::get_constructor(Translate_context
* context
,
12027 Btype
* array_btype
)
12029 Type
* element_type
= this->type_
->array_type()->element_type();
12031 std::vector
<unsigned long> indexes
;
12032 std::vector
<Bexpression
*> vals
;
12033 Gogo
* gogo
= context
->gogo();
12034 if (this->vals_
!= NULL
)
12037 std::vector
<unsigned long>::const_iterator pi
;
12038 if (this->indexes_
!= NULL
)
12039 pi
= this->indexes_
->begin();
12040 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12041 pv
!= this->vals_
->end();
12044 if (this->indexes_
!= NULL
)
12045 go_assert(pi
!= this->indexes_
->end());
12047 if (this->indexes_
== NULL
)
12048 indexes
.push_back(i
);
12050 indexes
.push_back(*pi
);
12053 Btype
* ebtype
= element_type
->get_backend(gogo
);
12054 Bexpression
*zv
= gogo
->backend()->zero_expression(ebtype
);
12055 vals
.push_back(zv
);
12059 Expression
* val_expr
=
12060 Expression::convert_for_assignment(gogo
, element_type
, *pv
,
12062 vals
.push_back(val_expr
->get_backend(context
));
12064 if (this->indexes_
!= NULL
)
12067 if (this->indexes_
!= NULL
)
12068 go_assert(pi
== this->indexes_
->end());
12070 return gogo
->backend()->array_constructor_expression(array_btype
, indexes
,
12071 vals
, this->location());
12074 // Export an array construction.
12077 Array_construction_expression::do_export(Export
* exp
) const
12079 exp
->write_c_string("convert(");
12080 exp
->write_type(this->type_
);
12081 if (this->vals_
!= NULL
)
12083 std::vector
<unsigned long>::const_iterator pi
;
12084 if (this->indexes_
!= NULL
)
12085 pi
= this->indexes_
->begin();
12086 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12087 pv
!= this->vals_
->end();
12090 exp
->write_c_string(", ");
12092 if (this->indexes_
!= NULL
)
12095 snprintf(buf
, sizeof buf
, "%lu", *pi
);
12096 exp
->write_c_string(buf
);
12097 exp
->write_c_string(":");
12101 (*pv
)->export_expression(exp
);
12103 if (this->indexes_
!= NULL
)
12107 exp
->write_c_string(")");
12110 // Dump ast representation of an array construction expressin.
12113 Array_construction_expression::do_dump_expression(
12114 Ast_dump_context
* ast_dump_context
) const
12116 Expression
* length
= this->type_
->array_type()->length();
12118 ast_dump_context
->ostream() << "[" ;
12119 if (length
!= NULL
)
12121 ast_dump_context
->dump_expression(length
);
12123 ast_dump_context
->ostream() << "]" ;
12124 ast_dump_context
->dump_type(this->type_
);
12125 ast_dump_context
->ostream() << "{" ;
12126 if (this->indexes_
== NULL
)
12127 ast_dump_context
->dump_expression_list(this->vals_
);
12130 Expression_list::const_iterator pv
= this->vals_
->begin();
12131 for (std::vector
<unsigned long>::const_iterator pi
=
12132 this->indexes_
->begin();
12133 pi
!= this->indexes_
->end();
12136 if (pi
!= this->indexes_
->begin())
12137 ast_dump_context
->ostream() << ", ";
12138 ast_dump_context
->ostream() << *pi
<< ':';
12139 ast_dump_context
->dump_expression(*pv
);
12142 ast_dump_context
->ostream() << "}" ;
12146 // Construct a fixed array.
12148 class Fixed_array_construction_expression
:
12149 public Array_construction_expression
12152 Fixed_array_construction_expression(Type
* type
,
12153 const std::vector
<unsigned long>* indexes
,
12154 Expression_list
* vals
, Location location
)
12155 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
12156 type
, indexes
, vals
, location
)
12157 { go_assert(type
->array_type() != NULL
&& !type
->is_slice_type()); }
12163 return new Fixed_array_construction_expression(this->type(),
12165 (this->vals() == NULL
12167 : this->vals()->copy()),
12172 do_get_backend(Translate_context
*);
12175 // Return the backend representation for constructing a fixed array.
12178 Fixed_array_construction_expression::do_get_backend(Translate_context
* context
)
12180 Type
* type
= this->type();
12181 Btype
* btype
= type
->get_backend(context
->gogo());
12182 return this->get_constructor(context
, btype
);
12186 Expression::make_array_composite_literal(Type
* type
, Expression_list
* vals
,
12189 go_assert(type
->array_type() != NULL
&& !type
->is_slice_type());
12190 return new Fixed_array_construction_expression(type
, NULL
, vals
, location
);
12193 // Construct a slice.
12195 class Slice_construction_expression
: public Array_construction_expression
12198 Slice_construction_expression(Type
* type
,
12199 const std::vector
<unsigned long>* indexes
,
12200 Expression_list
* vals
, Location location
)
12201 : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION
,
12202 type
, indexes
, vals
, location
),
12205 go_assert(type
->is_slice_type());
12207 unsigned long lenval
;
12208 Expression
* length
;
12209 if (vals
== NULL
|| vals
->empty())
12213 if (this->indexes() == NULL
)
12214 lenval
= vals
->size();
12216 lenval
= indexes
->back() + 1;
12218 Type
* int_type
= Type::lookup_integer_type("int");
12219 length
= Expression::make_integer_ul(lenval
, int_type
, location
);
12220 Type
* element_type
= type
->array_type()->element_type();
12221 this->valtype_
= Type::make_array_type(element_type
, length
);
12225 // Note that taking the address of a slice literal is invalid.
12228 do_traverse(Traverse
* traverse
);
12233 return new Slice_construction_expression(this->type(), this->indexes(),
12234 (this->vals() == NULL
12236 : this->vals()->copy()),
12241 do_get_backend(Translate_context
*);
12244 // The type of the values in this slice.
12251 Slice_construction_expression::do_traverse(Traverse
* traverse
)
12253 if (this->Array_construction_expression::do_traverse(traverse
)
12255 return TRAVERSE_EXIT
;
12256 if (Type::traverse(this->valtype_
, traverse
) == TRAVERSE_EXIT
)
12257 return TRAVERSE_EXIT
;
12258 return TRAVERSE_CONTINUE
;
12261 // Return the backend representation for constructing a slice.
12264 Slice_construction_expression::do_get_backend(Translate_context
* context
)
12266 Array_type
* array_type
= this->type()->array_type();
12267 if (array_type
== NULL
)
12269 go_assert(this->type()->is_error());
12270 return context
->backend()->error_expression();
12273 Location loc
= this->location();
12274 Type
* element_type
= array_type
->element_type();
12275 go_assert(this->valtype_
!= NULL
);
12277 Expression_list
* vals
= this->vals();
12278 if (this->vals() == NULL
|| this->vals()->empty())
12280 // We need to create a unique value for the empty array literal.
12281 vals
= new Expression_list
;
12282 vals
->push_back(NULL
);
12284 Expression
* array_val
=
12285 new Fixed_array_construction_expression(this->valtype_
, this->indexes(),
12288 bool is_constant_initializer
= array_val
->is_immutable();
12290 // We have to copy the initial values into heap memory if we are in
12291 // a function or if the values are not constants. We also have to
12292 // copy them if they may contain pointers in a non-constant context,
12293 // as otherwise the garbage collector won't see them.
12294 bool copy_to_heap
= (context
->function() != NULL
12295 || !is_constant_initializer
12296 || (element_type
->has_pointer()
12297 && !context
->is_const()));
12302 // The initializer will only run once.
12303 space
= Expression::make_unary(OPERATOR_AND
, array_val
, loc
);
12304 space
->unary_expression()->set_is_slice_init();
12307 space
= Expression::make_heap_expression(array_val
, loc
);
12309 // Build a constructor for the slice.
12311 Expression
* len
= this->valtype_
->array_type()->length();
12312 Expression
* slice_val
=
12313 Expression::make_slice_value(this->type(), space
, len
, len
, loc
);
12314 return slice_val
->get_backend(context
);
12317 // Make a slice composite literal. This is used by the type
12318 // descriptor code.
12321 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
12324 go_assert(type
->is_slice_type());
12325 return new Slice_construction_expression(type
, NULL
, vals
, location
);
12328 // Construct a map.
12330 class Map_construction_expression
: public Expression
12333 Map_construction_expression(Type
* type
, Expression_list
* vals
,
12335 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
12336 type_(type
), vals_(vals
), element_type_(NULL
), constructor_temp_(NULL
)
12337 { go_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
12341 do_traverse(Traverse
* traverse
);
12344 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
12348 { return this->type_
; }
12351 do_determine_type(const Type_context
*);
12354 do_check_types(Gogo
*);
12359 return new Map_construction_expression(this->type_
,
12360 (this->vals_
== NULL
12362 : this->vals_
->copy()),
12367 do_get_backend(Translate_context
*);
12370 do_export(Export
*) const;
12373 do_dump_expression(Ast_dump_context
*) const;
12376 // The type of the map to construct.
12378 // The list of values.
12379 Expression_list
* vals_
;
12380 // The type of the key-value pair struct for each map element.
12381 Struct_type
* element_type_
;
12382 // A temporary reference to the variable storing the constructor initializer.
12383 Temporary_statement
* constructor_temp_
;
12389 Map_construction_expression::do_traverse(Traverse
* traverse
)
12391 if (this->vals_
!= NULL
12392 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
12393 return TRAVERSE_EXIT
;
12394 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12395 return TRAVERSE_EXIT
;
12396 return TRAVERSE_CONTINUE
;
12399 // Flatten constructor initializer into a temporary variable since
12400 // we need to take its address for __go_construct_map.
12403 Map_construction_expression::do_flatten(Gogo
* gogo
, Named_object
*,
12404 Statement_inserter
* inserter
)
12406 if (!this->is_error_expression()
12407 && this->vals_
!= NULL
12408 && !this->vals_
->empty()
12409 && this->constructor_temp_
== NULL
)
12411 Map_type
* mt
= this->type_
->map_type();
12412 Type
* key_type
= mt
->key_type();
12413 Type
* val_type
= mt
->val_type();
12414 this->element_type_
= Type::make_builtin_struct_type(2,
12416 "__val", val_type
);
12418 Expression_list
* value_pairs
= new Expression_list();
12419 Location loc
= this->location();
12422 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12423 pv
!= this->vals_
->end();
12426 Expression_list
* key_value_pair
= new Expression_list();
12428 Expression::convert_for_assignment(gogo
, key_type
, *pv
, loc
);
12432 Expression::convert_for_assignment(gogo
, val_type
, *pv
, loc
);
12434 key_value_pair
->push_back(key
);
12435 key_value_pair
->push_back(val
);
12436 value_pairs
->push_back(
12437 Expression::make_struct_composite_literal(this->element_type_
,
12438 key_value_pair
, loc
));
12441 Expression
* element_count
= Expression::make_integer_ul(i
, NULL
, loc
);
12443 Type::make_array_type(this->element_type_
, element_count
);
12444 Expression
* constructor
=
12445 new Fixed_array_construction_expression(ctor_type
, NULL
,
12448 this->constructor_temp_
=
12449 Statement::make_temporary(NULL
, constructor
, loc
);
12450 constructor
->issue_nil_check();
12451 this->constructor_temp_
->set_is_address_taken();
12452 inserter
->insert(this->constructor_temp_
);
12458 // Final type determination.
12461 Map_construction_expression::do_determine_type(const Type_context
*)
12463 if (this->vals_
== NULL
)
12466 Map_type
* mt
= this->type_
->map_type();
12467 Type_context
key_context(mt
->key_type(), false);
12468 Type_context
val_context(mt
->val_type(), false);
12469 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12470 pv
!= this->vals_
->end();
12473 (*pv
)->determine_type(&key_context
);
12475 (*pv
)->determine_type(&val_context
);
12482 Map_construction_expression::do_check_types(Gogo
*)
12484 if (this->vals_
== NULL
)
12487 Map_type
* mt
= this->type_
->map_type();
12489 Type
* key_type
= mt
->key_type();
12490 Type
* val_type
= mt
->val_type();
12491 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12492 pv
!= this->vals_
->end();
12495 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
12497 error_at((*pv
)->location(),
12498 "incompatible type for element %d key in map construction",
12500 this->set_is_error();
12503 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
12505 error_at((*pv
)->location(),
12506 ("incompatible type for element %d value "
12507 "in map construction"),
12509 this->set_is_error();
12514 // Return the backend representation for constructing a map.
12517 Map_construction_expression::do_get_backend(Translate_context
* context
)
12519 if (this->is_error_expression())
12520 return context
->backend()->error_expression();
12521 Location loc
= this->location();
12524 Expression
* ventries
;
12525 if (this->vals_
== NULL
|| this->vals_
->empty())
12526 ventries
= Expression::make_nil(loc
);
12529 go_assert(this->constructor_temp_
!= NULL
);
12530 i
= this->vals_
->size() / 2;
12532 Expression
* ctor_ref
=
12533 Expression::make_temporary_reference(this->constructor_temp_
, loc
);
12534 ventries
= Expression::make_unary(OPERATOR_AND
, ctor_ref
, loc
);
12537 Map_type
* mt
= this->type_
->map_type();
12538 if (this->element_type_
== NULL
)
12539 this->element_type_
=
12540 Type::make_builtin_struct_type(2,
12541 "__key", mt
->key_type(),
12542 "__val", mt
->val_type());
12543 Expression
* descriptor
= Expression::make_map_descriptor(mt
, loc
);
12545 Type
* uintptr_t = Type::lookup_integer_type("uintptr");
12546 Expression
* count
= Expression::make_integer_ul(i
, uintptr_t, loc
);
12548 Expression
* entry_size
=
12549 Expression::make_type_info(this->element_type_
, TYPE_INFO_SIZE
);
12551 unsigned int field_index
;
12552 const Struct_field
* valfield
=
12553 this->element_type_
->find_local_field("__val", &field_index
);
12554 Expression
* val_offset
=
12555 Expression::make_struct_field_offset(this->element_type_
, valfield
);
12556 Expression
* val_size
=
12557 Expression::make_type_info(mt
->val_type(), TYPE_INFO_SIZE
);
12559 Expression
* map_ctor
=
12560 Runtime::make_call(Runtime::CONSTRUCT_MAP
, loc
, 6, descriptor
, count
,
12561 entry_size
, val_offset
, val_size
, ventries
);
12562 return map_ctor
->get_backend(context
);
12565 // Export an array construction.
12568 Map_construction_expression::do_export(Export
* exp
) const
12570 exp
->write_c_string("convert(");
12571 exp
->write_type(this->type_
);
12572 for (Expression_list::const_iterator pv
= this->vals_
->begin();
12573 pv
!= this->vals_
->end();
12576 exp
->write_c_string(", ");
12577 (*pv
)->export_expression(exp
);
12579 exp
->write_c_string(")");
12582 // Dump ast representation for a map construction expression.
12585 Map_construction_expression::do_dump_expression(
12586 Ast_dump_context
* ast_dump_context
) const
12588 ast_dump_context
->ostream() << "{" ;
12589 ast_dump_context
->dump_expression_list(this->vals_
, true);
12590 ast_dump_context
->ostream() << "}";
12593 // A general composite literal. This is lowered to a type specific
12596 class Composite_literal_expression
: public Parser_expression
12599 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
12600 Expression_list
* vals
, bool all_are_names
,
12602 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
12603 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
),
12604 all_are_names_(all_are_names
)
12609 do_traverse(Traverse
* traverse
);
12612 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
12617 return new Composite_literal_expression(this->type_
, this->depth_
,
12619 (this->vals_
== NULL
12621 : this->vals_
->copy()),
12622 this->all_are_names_
,
12627 do_dump_expression(Ast_dump_context
*) const;
12631 lower_struct(Gogo
*, Type
*);
12634 lower_array(Type
*);
12637 make_array(Type
*, const std::vector
<unsigned long>*, Expression_list
*);
12640 lower_map(Gogo
*, Named_object
*, Statement_inserter
*, Type
*);
12642 // The type of the composite literal.
12644 // The depth within a list of composite literals within a composite
12645 // literal, when the type is omitted.
12647 // The values to put in the composite literal.
12648 Expression_list
* vals_
;
12649 // If this is true, then VALS_ is a list of pairs: a key and a
12650 // value. In an array initializer, a missing key will be NULL.
12652 // If this is true, then HAS_KEYS_ is true, and every key is a
12653 // simple identifier.
12654 bool all_are_names_
;
12660 Composite_literal_expression::do_traverse(Traverse
* traverse
)
12662 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12663 return TRAVERSE_EXIT
;
12665 // If this is a struct composite literal with keys, then the keys
12666 // are field names, not expressions. We don't want to traverse them
12667 // in that case. If we do, we can give an erroneous error "variable
12668 // initializer refers to itself." See bug482.go in the testsuite.
12669 if (this->has_keys_
&& this->vals_
!= NULL
)
12671 // The type may not be resolvable at this point.
12672 Type
* type
= this->type_
;
12674 for (int depth
= this->depth_
; depth
> 0; --depth
)
12676 if (type
->array_type() != NULL
)
12677 type
= type
->array_type()->element_type();
12678 else if (type
->map_type() != NULL
)
12679 type
= type
->map_type()->val_type();
12682 // This error will be reported during lowering.
12683 return TRAVERSE_CONTINUE
;
12689 if (type
->classification() == Type::TYPE_NAMED
)
12690 type
= type
->named_type()->real_type();
12691 else if (type
->classification() == Type::TYPE_FORWARD
)
12693 Type
* t
= type
->forwarded();
12702 if (type
->classification() == Type::TYPE_STRUCT
)
12704 Expression_list::iterator p
= this->vals_
->begin();
12705 while (p
!= this->vals_
->end())
12709 go_assert(p
!= this->vals_
->end());
12710 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12711 return TRAVERSE_EXIT
;
12714 return TRAVERSE_CONTINUE
;
12718 if (this->vals_
!= NULL
)
12719 return this->vals_
->traverse(traverse
);
12721 return TRAVERSE_CONTINUE
;
12724 // Lower a generic composite literal into a specific version based on
12728 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
,
12729 Statement_inserter
* inserter
, int)
12731 Type
* type
= this->type_
;
12733 for (int depth
= this->depth_
; depth
> 0; --depth
)
12735 if (type
->array_type() != NULL
)
12736 type
= type
->array_type()->element_type();
12737 else if (type
->map_type() != NULL
)
12738 type
= type
->map_type()->val_type();
12741 if (!type
->is_error())
12742 error_at(this->location(),
12743 ("may only omit types within composite literals "
12744 "of slice, array, or map type"));
12745 return Expression::make_error(this->location());
12749 Type
*pt
= type
->points_to();
12750 bool is_pointer
= false;
12758 if (type
->is_error())
12759 return Expression::make_error(this->location());
12760 else if (type
->struct_type() != NULL
)
12761 ret
= this->lower_struct(gogo
, type
);
12762 else if (type
->array_type() != NULL
)
12763 ret
= this->lower_array(type
);
12764 else if (type
->map_type() != NULL
)
12765 ret
= this->lower_map(gogo
, function
, inserter
, type
);
12768 error_at(this->location(),
12769 ("expected struct, slice, array, or map type "
12770 "for composite literal"));
12771 return Expression::make_error(this->location());
12775 ret
= Expression::make_heap_expression(ret
, this->location());
12780 // Lower a struct composite literal.
12783 Composite_literal_expression::lower_struct(Gogo
* gogo
, Type
* type
)
12785 Location location
= this->location();
12786 Struct_type
* st
= type
->struct_type();
12787 if (this->vals_
== NULL
|| !this->has_keys_
)
12789 if (this->vals_
!= NULL
12790 && !this->vals_
->empty()
12791 && type
->named_type() != NULL
12792 && type
->named_type()->named_object()->package() != NULL
)
12794 for (Struct_field_list::const_iterator pf
= st
->fields()->begin();
12795 pf
!= st
->fields()->end();
12798 if (Gogo::is_hidden_name(pf
->field_name()))
12799 error_at(this->location(),
12800 "assignment of unexported field %qs in %qs literal",
12801 Gogo::message_name(pf
->field_name()).c_str(),
12802 type
->named_type()->message_name().c_str());
12806 return new Struct_construction_expression(type
, this->vals_
, location
);
12809 size_t field_count
= st
->field_count();
12810 std::vector
<Expression
*> vals(field_count
);
12811 std::vector
<int>* traverse_order
= new(std::vector
<int>);
12812 Expression_list::const_iterator p
= this->vals_
->begin();
12813 Expression
* external_expr
= NULL
;
12814 const Named_object
* external_no
= NULL
;
12815 while (p
!= this->vals_
->end())
12817 Expression
* name_expr
= *p
;
12820 go_assert(p
!= this->vals_
->end());
12821 Expression
* val
= *p
;
12825 if (name_expr
== NULL
)
12827 error_at(val
->location(), "mixture of field and value initializers");
12828 return Expression::make_error(location
);
12831 bool bad_key
= false;
12833 const Named_object
* no
= NULL
;
12834 switch (name_expr
->classification())
12836 case EXPRESSION_UNKNOWN_REFERENCE
:
12837 name
= name_expr
->unknown_expression()->name();
12838 if (type
->named_type() != NULL
)
12840 // If the named object found for this field name comes from a
12841 // different package than the struct it is a part of, do not count
12842 // this incorrect lookup as a usage of the object's package.
12843 no
= name_expr
->unknown_expression()->named_object();
12844 if (no
->package() != NULL
12845 && no
->package() != type
->named_type()->named_object()->package())
12846 no
->package()->forget_usage(name_expr
);
12850 case EXPRESSION_CONST_REFERENCE
:
12851 no
= static_cast<Const_expression
*>(name_expr
)->named_object();
12854 case EXPRESSION_TYPE
:
12856 Type
* t
= name_expr
->type();
12857 Named_type
* nt
= t
->named_type();
12861 no
= nt
->named_object();
12865 case EXPRESSION_VAR_REFERENCE
:
12866 no
= name_expr
->var_expression()->named_object();
12869 case EXPRESSION_FUNC_REFERENCE
:
12870 no
= name_expr
->func_expression()->named_object();
12873 case EXPRESSION_UNARY
:
12874 // If there is a local variable around with the same name as
12875 // the field, and this occurs in the closure, then the
12876 // parser may turn the field reference into an indirection
12877 // through the closure. FIXME: This is a mess.
12880 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
12881 if (ue
->op() == OPERATOR_MULT
)
12883 Field_reference_expression
* fre
=
12884 ue
->operand()->field_reference_expression();
12888 fre
->expr()->type()->deref()->struct_type();
12891 const Struct_field
* sf
= st
->field(fre
->field_index());
12892 name
= sf
->field_name();
12894 // See below. FIXME.
12895 if (!Gogo::is_hidden_name(name
)
12899 if (gogo
->lookup_global(name
.c_str()) != NULL
)
12900 name
= gogo
->pack_hidden_name(name
, false);
12904 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
12905 size_t buflen
= strlen(buf
);
12906 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
12909 name
= name
.substr(0, name
.length() - buflen
);
12924 error_at(name_expr
->location(), "expected struct field name");
12925 return Expression::make_error(location
);
12930 if (no
->package() != NULL
&& external_expr
== NULL
)
12932 external_expr
= name_expr
;
12938 // A predefined name won't be packed. If it starts with a
12939 // lower case letter we need to check for that case, because
12940 // the field name will be packed. FIXME.
12941 if (!Gogo::is_hidden_name(name
)
12945 Named_object
* gno
= gogo
->lookup_global(name
.c_str());
12947 name
= gogo
->pack_hidden_name(name
, false);
12951 unsigned int index
;
12952 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
12955 error_at(name_expr
->location(), "unknown field %qs in %qs",
12956 Gogo::message_name(name
).c_str(),
12957 (type
->named_type() != NULL
12958 ? type
->named_type()->message_name().c_str()
12959 : "unnamed struct"));
12960 return Expression::make_error(location
);
12962 if (vals
[index
] != NULL
)
12964 error_at(name_expr
->location(),
12965 "duplicate value for field %qs in %qs",
12966 Gogo::message_name(name
).c_str(),
12967 (type
->named_type() != NULL
12968 ? type
->named_type()->message_name().c_str()
12969 : "unnamed struct"));
12970 return Expression::make_error(location
);
12973 if (type
->named_type() != NULL
12974 && type
->named_type()->named_object()->package() != NULL
12975 && Gogo::is_hidden_name(sf
->field_name()))
12976 error_at(name_expr
->location(),
12977 "assignment of unexported field %qs in %qs literal",
12978 Gogo::message_name(sf
->field_name()).c_str(),
12979 type
->named_type()->message_name().c_str());
12982 traverse_order
->push_back(index
);
12985 if (!this->all_are_names_
)
12987 // This is a weird case like bug462 in the testsuite.
12988 if (external_expr
== NULL
)
12989 error_at(this->location(), "unknown field in %qs literal",
12990 (type
->named_type() != NULL
12991 ? type
->named_type()->message_name().c_str()
12992 : "unnamed struct"));
12994 error_at(external_expr
->location(), "unknown field %qs in %qs",
12995 external_no
->message_name().c_str(),
12996 (type
->named_type() != NULL
12997 ? type
->named_type()->message_name().c_str()
12998 : "unnamed struct"));
12999 return Expression::make_error(location
);
13002 Expression_list
* list
= new Expression_list
;
13003 list
->reserve(field_count
);
13004 for (size_t i
= 0; i
< field_count
; ++i
)
13005 list
->push_back(vals
[i
]);
13007 Struct_construction_expression
* ret
=
13008 new Struct_construction_expression(type
, list
, location
);
13009 ret
->set_traverse_order(traverse_order
);
13013 // Used to sort an index/value array.
13015 class Index_value_compare
13019 operator()(const std::pair
<unsigned long, Expression
*>& a
,
13020 const std::pair
<unsigned long, Expression
*>& b
)
13021 { return a
.first
< b
.first
; }
13024 // Lower an array composite literal.
13027 Composite_literal_expression::lower_array(Type
* type
)
13029 Location location
= this->location();
13030 if (this->vals_
== NULL
|| !this->has_keys_
)
13031 return this->make_array(type
, NULL
, this->vals_
);
13033 std::vector
<unsigned long>* indexes
= new std::vector
<unsigned long>;
13034 indexes
->reserve(this->vals_
->size());
13035 bool indexes_out_of_order
= false;
13036 Expression_list
* vals
= new Expression_list();
13037 vals
->reserve(this->vals_
->size());
13038 unsigned long index
= 0;
13039 Expression_list::const_iterator p
= this->vals_
->begin();
13040 while (p
!= this->vals_
->end())
13042 Expression
* index_expr
= *p
;
13045 go_assert(p
!= this->vals_
->end());
13046 Expression
* val
= *p
;
13050 if (index_expr
== NULL
)
13052 if (!indexes
->empty())
13053 indexes
->push_back(index
);
13057 if (indexes
->empty() && !vals
->empty())
13059 for (size_t i
= 0; i
< vals
->size(); ++i
)
13060 indexes
->push_back(i
);
13063 Numeric_constant nc
;
13064 if (!index_expr
->numeric_constant_value(&nc
))
13066 error_at(index_expr
->location(),
13067 "index expression is not integer constant");
13068 return Expression::make_error(location
);
13071 switch (nc
.to_unsigned_long(&index
))
13073 case Numeric_constant::NC_UL_VALID
:
13075 case Numeric_constant::NC_UL_NOTINT
:
13076 error_at(index_expr
->location(),
13077 "index expression is not integer constant");
13078 return Expression::make_error(location
);
13079 case Numeric_constant::NC_UL_NEGATIVE
:
13080 error_at(index_expr
->location(), "index expression is negative");
13081 return Expression::make_error(location
);
13082 case Numeric_constant::NC_UL_BIG
:
13083 error_at(index_expr
->location(), "index value overflow");
13084 return Expression::make_error(location
);
13089 Named_type
* ntype
= Type::lookup_integer_type("int");
13090 Integer_type
* inttype
= ntype
->integer_type();
13091 if (sizeof(index
) <= static_cast<size_t>(inttype
->bits() * 8)
13092 && index
>> (inttype
->bits() - 1) != 0)
13094 error_at(index_expr
->location(), "index value overflow");
13095 return Expression::make_error(location
);
13098 if (std::find(indexes
->begin(), indexes
->end(), index
)
13101 error_at(index_expr
->location(), "duplicate value for index %lu",
13103 return Expression::make_error(location
);
13106 if (!indexes
->empty() && index
< indexes
->back())
13107 indexes_out_of_order
= true;
13109 indexes
->push_back(index
);
13112 vals
->push_back(val
);
13117 if (indexes
->empty())
13123 if (indexes_out_of_order
)
13125 typedef std::vector
<std::pair
<unsigned long, Expression
*> > V
;
13128 v
.reserve(indexes
->size());
13129 std::vector
<unsigned long>::const_iterator pi
= indexes
->begin();
13130 for (Expression_list::const_iterator pe
= vals
->begin();
13133 v
.push_back(std::make_pair(*pi
, *pe
));
13135 std::sort(v
.begin(), v
.end(), Index_value_compare());
13139 indexes
= new std::vector
<unsigned long>();
13140 indexes
->reserve(v
.size());
13141 vals
= new Expression_list();
13142 vals
->reserve(v
.size());
13144 for (V::const_iterator p
= v
.begin(); p
!= v
.end(); ++p
)
13146 indexes
->push_back(p
->first
);
13147 vals
->push_back(p
->second
);
13151 return this->make_array(type
, indexes
, vals
);
13154 // Actually build the array composite literal. This handles
13158 Composite_literal_expression::make_array(
13160 const std::vector
<unsigned long>* indexes
,
13161 Expression_list
* vals
)
13163 Location location
= this->location();
13164 Array_type
* at
= type
->array_type();
13166 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
13171 else if (indexes
!= NULL
)
13172 size
= indexes
->back() + 1;
13175 size
= vals
->size();
13176 Integer_type
* it
= Type::lookup_integer_type("int")->integer_type();
13177 if (sizeof(size
) <= static_cast<size_t>(it
->bits() * 8)
13178 && size
>> (it
->bits() - 1) != 0)
13180 error_at(location
, "too many elements in composite literal");
13181 return Expression::make_error(location
);
13185 Expression
* elen
= Expression::make_integer_ul(size
, NULL
, location
);
13186 at
= Type::make_array_type(at
->element_type(), elen
);
13189 else if (at
->length() != NULL
13190 && !at
->length()->is_error_expression()
13191 && this->vals_
!= NULL
)
13193 Numeric_constant nc
;
13195 if (at
->length()->numeric_constant_value(&nc
)
13196 && nc
.to_unsigned_long(&val
) == Numeric_constant::NC_UL_VALID
)
13198 if (indexes
== NULL
)
13200 if (this->vals_
->size() > val
)
13202 error_at(location
, "too many elements in composite literal");
13203 return Expression::make_error(location
);
13208 unsigned long max
= indexes
->back();
13212 ("some element keys in composite literal "
13213 "are out of range"));
13214 return Expression::make_error(location
);
13220 if (at
->length() != NULL
)
13221 return new Fixed_array_construction_expression(type
, indexes
, vals
,
13224 return new Slice_construction_expression(type
, indexes
, vals
, location
);
13227 // Lower a map composite literal.
13230 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
13231 Statement_inserter
* inserter
,
13234 Location location
= this->location();
13235 if (this->vals_
!= NULL
)
13237 if (!this->has_keys_
)
13239 error_at(location
, "map composite literal must have keys");
13240 return Expression::make_error(location
);
13243 for (Expression_list::iterator p
= this->vals_
->begin();
13244 p
!= this->vals_
->end();
13250 error_at((*p
)->location(),
13251 "map composite literal must have keys for every value");
13252 return Expression::make_error(location
);
13254 // Make sure we have lowered the key; it may not have been
13255 // lowered in order to handle keys for struct composite
13256 // literals. Lower it now to get the right error message.
13257 if ((*p
)->unknown_expression() != NULL
)
13259 (*p
)->unknown_expression()->clear_is_composite_literal_key();
13260 gogo
->lower_expression(function
, inserter
, &*p
);
13261 go_assert((*p
)->is_error_expression());
13262 return Expression::make_error(location
);
13267 return new Map_construction_expression(type
, this->vals_
, location
);
13270 // Dump ast representation for a composite literal expression.
13273 Composite_literal_expression::do_dump_expression(
13274 Ast_dump_context
* ast_dump_context
) const
13276 ast_dump_context
->ostream() << "composite(";
13277 ast_dump_context
->dump_type(this->type_
);
13278 ast_dump_context
->ostream() << ", {";
13279 ast_dump_context
->dump_expression_list(this->vals_
, this->has_keys_
);
13280 ast_dump_context
->ostream() << "})";
13283 // Make a composite literal expression.
13286 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
13287 Expression_list
* vals
, bool all_are_names
,
13290 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
13291 all_are_names
, location
);
13294 // Return whether this expression is a composite literal.
13297 Expression::is_composite_literal() const
13299 switch (this->classification_
)
13301 case EXPRESSION_COMPOSITE_LITERAL
:
13302 case EXPRESSION_STRUCT_CONSTRUCTION
:
13303 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
13304 case EXPRESSION_SLICE_CONSTRUCTION
:
13305 case EXPRESSION_MAP_CONSTRUCTION
:
13312 // Return whether this expression is a composite literal which is not
13316 Expression::is_nonconstant_composite_literal() const
13318 switch (this->classification_
)
13320 case EXPRESSION_STRUCT_CONSTRUCTION
:
13322 const Struct_construction_expression
*psce
=
13323 static_cast<const Struct_construction_expression
*>(this);
13324 return !psce
->is_constant_struct();
13326 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
13328 const Fixed_array_construction_expression
*pace
=
13329 static_cast<const Fixed_array_construction_expression
*>(this);
13330 return !pace
->is_constant_array();
13332 case EXPRESSION_SLICE_CONSTRUCTION
:
13334 const Slice_construction_expression
*pace
=
13335 static_cast<const Slice_construction_expression
*>(this);
13336 return !pace
->is_constant_array();
13338 case EXPRESSION_MAP_CONSTRUCTION
:
13345 // Return true if this is a variable or temporary_variable.
13348 Expression::is_variable() const
13350 switch (this->classification_
)
13352 case EXPRESSION_VAR_REFERENCE
:
13353 case EXPRESSION_TEMPORARY_REFERENCE
:
13354 case EXPRESSION_SET_AND_USE_TEMPORARY
:
13361 // Return true if this is a reference to a local variable.
13364 Expression::is_local_variable() const
13366 const Var_expression
* ve
= this->var_expression();
13369 const Named_object
* no
= ve
->named_object();
13370 return (no
->is_result_variable()
13371 || (no
->is_variable() && !no
->var_value()->is_global()));
13374 // Class Type_guard_expression.
13379 Type_guard_expression::do_traverse(Traverse
* traverse
)
13381 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
13382 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
13383 return TRAVERSE_EXIT
;
13384 return TRAVERSE_CONTINUE
;
13388 Type_guard_expression::do_flatten(Gogo
*, Named_object
*,
13389 Statement_inserter
* inserter
)
13391 if (!this->expr_
->is_variable())
13393 Temporary_statement
* temp
= Statement::make_temporary(NULL
, this->expr_
,
13395 inserter
->insert(temp
);
13397 Expression::make_temporary_reference(temp
, this->location());
13402 // Check types of a type guard expression. The expression must have
13403 // an interface type, but the actual type conversion is checked at run
13407 Type_guard_expression::do_check_types(Gogo
*)
13409 Type
* expr_type
= this->expr_
->type();
13410 if (expr_type
->interface_type() == NULL
)
13412 if (!expr_type
->is_error() && !this->type_
->is_error())
13413 this->report_error(_("type assertion only valid for interface types"));
13414 this->set_is_error();
13416 else if (this->type_
->interface_type() == NULL
)
13418 std::string reason
;
13419 if (!expr_type
->interface_type()->implements_interface(this->type_
,
13422 if (!this->type_
->is_error())
13424 if (reason
.empty())
13425 this->report_error(_("impossible type assertion: "
13426 "type does not implement interface"));
13428 error_at(this->location(),
13429 ("impossible type assertion: "
13430 "type does not implement interface (%s)"),
13433 this->set_is_error();
13438 // Return the backend representation for a type guard expression.
13441 Type_guard_expression::do_get_backend(Translate_context
* context
)
13443 Expression
* conversion
;
13444 if (this->type_
->interface_type() != NULL
)
13446 Expression::convert_interface_to_interface(this->type_
, this->expr_
,
13447 true, this->location());
13450 Expression::convert_for_assignment(context
->gogo(), this->type_
,
13451 this->expr_
, this->location());
13453 return conversion
->get_backend(context
);
13456 // Dump ast representation for a type guard expression.
13459 Type_guard_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
)
13462 this->expr_
->dump_expression(ast_dump_context
);
13463 ast_dump_context
->ostream() << ".";
13464 ast_dump_context
->dump_type(this->type_
);
13467 // Make a type guard expression.
13470 Expression::make_type_guard(Expression
* expr
, Type
* type
,
13473 return new Type_guard_expression(expr
, type
, location
);
13476 // Class Heap_expression.
13478 // When you take the address of an escaping expression, it is allocated
13479 // on the heap. This class implements that.
13481 class Heap_expression
: public Expression
13484 Heap_expression(Expression
* expr
, Location location
)
13485 : Expression(EXPRESSION_HEAP
, location
),
13491 do_traverse(Traverse
* traverse
)
13492 { return Expression::traverse(&this->expr_
, traverse
); }
13496 { return Type::make_pointer_type(this->expr_
->type()); }
13499 do_determine_type(const Type_context
*)
13500 { this->expr_
->determine_type_no_context(); }
13505 return Expression::make_heap_expression(this->expr_
->copy(),
13510 do_get_backend(Translate_context
*);
13512 // We only export global objects, and the parser does not generate
13513 // this in global scope.
13515 do_export(Export
*) const
13516 { go_unreachable(); }
13519 do_dump_expression(Ast_dump_context
*) const;
13522 // The expression which is being put on the heap.
13526 // Return the backend representation for allocating an expression on the heap.
13529 Heap_expression::do_get_backend(Translate_context
* context
)
13531 if (this->expr_
->is_error_expression() || this->expr_
->type()->is_error())
13532 return context
->backend()->error_expression();
13534 Location loc
= this->location();
13535 Gogo
* gogo
= context
->gogo();
13536 Btype
* btype
= this->type()->get_backend(gogo
);
13537 Bexpression
* space
= Expression::make_allocation(this->expr_
->type(),
13538 loc
)->get_backend(context
);
13541 Named_object
* fn
= context
->function();
13542 go_assert(fn
!= NULL
);
13543 Bfunction
* fndecl
= fn
->func_value()->get_or_make_decl(gogo
, fn
);
13544 Bvariable
* space_temp
=
13545 gogo
->backend()->temporary_variable(fndecl
, context
->bblock(), btype
,
13546 space
, true, loc
, &decl
);
13547 space
= gogo
->backend()->var_expression(space_temp
, loc
);
13548 Btype
* expr_btype
= this->expr_
->type()->get_backend(gogo
);
13550 gogo
->backend()->indirect_expression(expr_btype
, space
, true, loc
);
13552 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
13553 Bstatement
* assn
= gogo
->backend()->assignment_statement(ref
, bexpr
, loc
);
13554 decl
= gogo
->backend()->compound_statement(decl
, assn
);
13555 space
= gogo
->backend()->var_expression(space_temp
, loc
);
13556 return gogo
->backend()->compound_expression(decl
, space
, loc
);
13559 // Dump ast representation for a heap expression.
13562 Heap_expression::do_dump_expression(
13563 Ast_dump_context
* ast_dump_context
) const
13565 ast_dump_context
->ostream() << "&(";
13566 ast_dump_context
->dump_expression(this->expr_
);
13567 ast_dump_context
->ostream() << ")";
13570 // Allocate an expression on the heap.
13573 Expression::make_heap_expression(Expression
* expr
, Location location
)
13575 return new Heap_expression(expr
, location
);
13578 // Class Receive_expression.
13580 // Return the type of a receive expression.
13583 Receive_expression::do_type()
13585 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
13586 if (channel_type
== NULL
)
13587 return Type::make_error_type();
13588 return channel_type
->element_type();
13591 // Check types for a receive expression.
13594 Receive_expression::do_check_types(Gogo
*)
13596 Type
* type
= this->channel_
->type();
13597 if (type
->is_error())
13599 this->set_is_error();
13602 if (type
->channel_type() == NULL
)
13604 this->report_error(_("expected channel"));
13607 if (!type
->channel_type()->may_receive())
13609 this->report_error(_("invalid receive on send-only channel"));
13614 // Flattening for receive expressions creates a temporary variable to store
13615 // received data in for receives.
13618 Receive_expression::do_flatten(Gogo
*, Named_object
*,
13619 Statement_inserter
* inserter
)
13621 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
13622 if (channel_type
== NULL
)
13624 go_assert(saw_errors());
13628 Type
* element_type
= channel_type
->element_type();
13629 if (this->temp_receiver_
== NULL
)
13631 this->temp_receiver_
= Statement::make_temporary(element_type
, NULL
,
13633 this->temp_receiver_
->set_is_address_taken();
13634 inserter
->insert(this->temp_receiver_
);
13640 // Get the backend representation for a receive expression.
13643 Receive_expression::do_get_backend(Translate_context
* context
)
13645 Location loc
= this->location();
13647 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
13648 if (channel_type
== NULL
)
13650 go_assert(this->channel_
->type()->is_error());
13651 return context
->backend()->error_expression();
13653 Expression
* td
= Expression::make_type_descriptor(channel_type
, loc
);
13655 Expression
* recv_ref
=
13656 Expression::make_temporary_reference(this->temp_receiver_
, loc
);
13657 Expression
* recv_addr
=
13658 Expression::make_temporary_reference(this->temp_receiver_
, loc
);
13659 recv_addr
= Expression::make_unary(OPERATOR_AND
, recv_addr
, loc
);
13661 Runtime::make_call(Runtime::RECEIVE
, loc
, 3,
13662 td
, this->channel_
, recv_addr
);
13663 return Expression::make_compound(recv
, recv_ref
, loc
)->get_backend(context
);
13666 // Dump ast representation for a receive expression.
13669 Receive_expression::do_dump_expression(Ast_dump_context
* ast_dump_context
) const
13671 ast_dump_context
->ostream() << " <- " ;
13672 ast_dump_context
->dump_expression(channel_
);
13675 // Make a receive expression.
13677 Receive_expression
*
13678 Expression::make_receive(Expression
* channel
, Location location
)
13680 return new Receive_expression(channel
, location
);
13683 // An expression which evaluates to a pointer to the type descriptor
13686 class Type_descriptor_expression
: public Expression
13689 Type_descriptor_expression(Type
* type
, Location location
)
13690 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
13696 do_traverse(Traverse
*);
13700 { return Type::make_type_descriptor_ptr_type(); }
13703 do_is_immutable() const
13707 do_determine_type(const Type_context
*)
13715 do_get_backend(Translate_context
* context
)
13717 return this->type_
->type_descriptor_pointer(context
->gogo(),
13722 do_dump_expression(Ast_dump_context
*) const;
13725 // The type for which this is the descriptor.
13730 Type_descriptor_expression::do_traverse(Traverse
* traverse
)
13732 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
13733 return TRAVERSE_EXIT
;
13734 return TRAVERSE_CONTINUE
;
13737 // Dump ast representation for a type descriptor expression.
13740 Type_descriptor_expression::do_dump_expression(
13741 Ast_dump_context
* ast_dump_context
) const
13743 ast_dump_context
->dump_type(this->type_
);
13746 // Make a type descriptor expression.
13749 Expression::make_type_descriptor(Type
* type
, Location location
)
13751 return new Type_descriptor_expression(type
, location
);
13754 // An expression which evaluates to a pointer to the Garbage Collection symbol
13757 class GC_symbol_expression
: public Expression
13760 GC_symbol_expression(Type
* type
)
13761 : Expression(EXPRESSION_GC_SYMBOL
, Linemap::predeclared_location()),
13768 { return Type::lookup_integer_type("uintptr"); }
13771 do_is_immutable() const
13775 do_determine_type(const Type_context
*)
13783 do_get_backend(Translate_context
* context
)
13784 { return this->type_
->gc_symbol_pointer(context
->gogo()); }
13787 do_dump_expression(Ast_dump_context
*) const;
13790 // The type which this gc symbol describes.
13794 // Dump ast representation for a gc symbol expression.
13797 GC_symbol_expression::do_dump_expression(
13798 Ast_dump_context
* ast_dump_context
) const
13800 ast_dump_context
->ostream() << "gcdata(";
13801 ast_dump_context
->dump_type(this->type_
);
13802 ast_dump_context
->ostream() << ")";
13805 // Make a gc symbol expression.
13808 Expression::make_gc_symbol(Type
* type
)
13810 return new GC_symbol_expression(type
);
13813 // An expression which evaluates to some characteristic of a type.
13814 // This is only used to initialize fields of a type descriptor. Using
13815 // a new expression class is slightly inefficient but gives us a good
13816 // separation between the frontend and the middle-end with regard to
13817 // how types are laid out.
13819 class Type_info_expression
: public Expression
13822 Type_info_expression(Type
* type
, Type_info type_info
)
13823 : Expression(EXPRESSION_TYPE_INFO
, Linemap::predeclared_location()),
13824 type_(type
), type_info_(type_info
)
13829 do_is_immutable() const
13836 do_determine_type(const Type_context
*)
13844 do_get_backend(Translate_context
* context
);
13847 do_dump_expression(Ast_dump_context
*) const;
13850 // The type for which we are getting information.
13852 // What information we want.
13853 Type_info type_info_
;
13856 // The type is chosen to match what the type descriptor struct
13860 Type_info_expression::do_type()
13862 switch (this->type_info_
)
13864 case TYPE_INFO_SIZE
:
13865 return Type::lookup_integer_type("uintptr");
13866 case TYPE_INFO_ALIGNMENT
:
13867 case TYPE_INFO_FIELD_ALIGNMENT
:
13868 return Type::lookup_integer_type("uint8");
13874 // Return the backend representation for type information.
13877 Type_info_expression::do_get_backend(Translate_context
* context
)
13879 Btype
* btype
= this->type_
->get_backend(context
->gogo());
13880 Gogo
* gogo
= context
->gogo();
13882 switch (this->type_info_
)
13884 case TYPE_INFO_SIZE
:
13885 val
= gogo
->backend()->type_size(btype
);
13887 case TYPE_INFO_ALIGNMENT
:
13888 val
= gogo
->backend()->type_alignment(btype
);
13890 case TYPE_INFO_FIELD_ALIGNMENT
:
13891 val
= gogo
->backend()->type_field_alignment(btype
);
13897 mpz_init_set_ui(cst
, val
);
13898 Btype
* int_btype
= this->type()->get_backend(gogo
);
13900 gogo
->backend()->integer_constant_expression(int_btype
, cst
);
13905 // Dump ast representation for a type info expression.
13908 Type_info_expression::do_dump_expression(
13909 Ast_dump_context
* ast_dump_context
) const
13911 ast_dump_context
->ostream() << "typeinfo(";
13912 ast_dump_context
->dump_type(this->type_
);
13913 ast_dump_context
->ostream() << ",";
13914 ast_dump_context
->ostream() <<
13915 (this->type_info_
== TYPE_INFO_ALIGNMENT
? "alignment"
13916 : this->type_info_
== TYPE_INFO_FIELD_ALIGNMENT
? "field alignment"
13917 : this->type_info_
== TYPE_INFO_SIZE
? "size "
13919 ast_dump_context
->ostream() << ")";
13922 // Make a type info expression.
13925 Expression::make_type_info(Type
* type
, Type_info type_info
)
13927 return new Type_info_expression(type
, type_info
);
13930 // An expression that evaluates to some characteristic of a slice.
13931 // This is used when indexing, bound-checking, or nil checking a slice.
13933 class Slice_info_expression
: public Expression
13936 Slice_info_expression(Expression
* slice
, Slice_info slice_info
,
13938 : Expression(EXPRESSION_SLICE_INFO
, location
),
13939 slice_(slice
), slice_info_(slice_info
)
13947 do_determine_type(const Type_context
*)
13953 return new Slice_info_expression(this->slice_
->copy(), this->slice_info_
,
13958 do_get_backend(Translate_context
* context
);
13961 do_dump_expression(Ast_dump_context
*) const;
13964 do_issue_nil_check()
13965 { this->slice_
->issue_nil_check(); }
13968 // The slice for which we are getting information.
13969 Expression
* slice_
;
13970 // What information we want.
13971 Slice_info slice_info_
;
13974 // Return the type of the slice info.
13977 Slice_info_expression::do_type()
13979 switch (this->slice_info_
)
13981 case SLICE_INFO_VALUE_POINTER
:
13982 return Type::make_pointer_type(
13983 this->slice_
->type()->array_type()->element_type());
13984 case SLICE_INFO_LENGTH
:
13985 case SLICE_INFO_CAPACITY
:
13986 return Type::lookup_integer_type("int");
13992 // Return the backend information for slice information.
13995 Slice_info_expression::do_get_backend(Translate_context
* context
)
13997 Gogo
* gogo
= context
->gogo();
13998 Bexpression
* bslice
= this->slice_
->get_backend(context
);
13999 switch (this->slice_info_
)
14001 case SLICE_INFO_VALUE_POINTER
:
14002 case SLICE_INFO_LENGTH
:
14003 case SLICE_INFO_CAPACITY
:
14004 return gogo
->backend()->struct_field_expression(bslice
, this->slice_info_
,
14012 // Dump ast representation for a type info expression.
14015 Slice_info_expression::do_dump_expression(
14016 Ast_dump_context
* ast_dump_context
) const
14018 ast_dump_context
->ostream() << "sliceinfo(";
14019 this->slice_
->dump_expression(ast_dump_context
);
14020 ast_dump_context
->ostream() << ",";
14021 ast_dump_context
->ostream() <<
14022 (this->slice_info_
== SLICE_INFO_VALUE_POINTER
? "values"
14023 : this->slice_info_
== SLICE_INFO_LENGTH
? "length"
14024 : this->slice_info_
== SLICE_INFO_CAPACITY
? "capacity "
14026 ast_dump_context
->ostream() << ")";
14029 // Make a slice info expression.
14032 Expression::make_slice_info(Expression
* slice
, Slice_info slice_info
,
14035 return new Slice_info_expression(slice
, slice_info
, location
);
14038 // An expression that represents a slice value: a struct with value pointer,
14039 // length, and capacity fields.
14041 class Slice_value_expression
: public Expression
14044 Slice_value_expression(Type
* type
, Expression
* valptr
, Expression
* len
,
14045 Expression
* cap
, Location location
)
14046 : Expression(EXPRESSION_SLICE_VALUE
, location
),
14047 type_(type
), valptr_(valptr
), len_(len
), cap_(cap
)
14052 do_traverse(Traverse
*);
14056 { return this->type_
; }
14059 do_determine_type(const Type_context
*)
14060 { go_unreachable(); }
14065 return new Slice_value_expression(this->type_
, this->valptr_
->copy(),
14066 this->len_
->copy(), this->cap_
->copy(),
14071 do_get_backend(Translate_context
* context
);
14074 do_dump_expression(Ast_dump_context
*) const;
14077 // The type of the slice value.
14079 // The pointer to the values in the slice.
14080 Expression
* valptr_
;
14081 // The length of the slice.
14083 // The capacity of the slice.
14088 Slice_value_expression::do_traverse(Traverse
* traverse
)
14090 if (Expression::traverse(&this->valptr_
, traverse
) == TRAVERSE_EXIT
14091 || Expression::traverse(&this->len_
, traverse
) == TRAVERSE_EXIT
14092 || Expression::traverse(&this->cap_
, traverse
) == TRAVERSE_EXIT
)
14093 return TRAVERSE_EXIT
;
14094 return TRAVERSE_CONTINUE
;
14098 Slice_value_expression::do_get_backend(Translate_context
* context
)
14100 std::vector
<Bexpression
*> vals(3);
14101 vals
[0] = this->valptr_
->get_backend(context
);
14102 vals
[1] = this->len_
->get_backend(context
);
14103 vals
[2] = this->cap_
->get_backend(context
);
14105 Gogo
* gogo
= context
->gogo();
14106 Btype
* btype
= this->type_
->get_backend(gogo
);
14107 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
14111 Slice_value_expression::do_dump_expression(
14112 Ast_dump_context
* ast_dump_context
) const
14114 ast_dump_context
->ostream() << "slicevalue(";
14115 ast_dump_context
->ostream() << "values: ";
14116 this->valptr_
->dump_expression(ast_dump_context
);
14117 ast_dump_context
->ostream() << ", length: ";
14118 this->len_
->dump_expression(ast_dump_context
);
14119 ast_dump_context
->ostream() << ", capacity: ";
14120 this->cap_
->dump_expression(ast_dump_context
);
14121 ast_dump_context
->ostream() << ")";
14125 Expression::make_slice_value(Type
* at
, Expression
* valptr
, Expression
* len
,
14126 Expression
* cap
, Location location
)
14128 go_assert(at
->is_slice_type());
14129 return new Slice_value_expression(at
, valptr
, len
, cap
, location
);
14132 // An expression that evaluates to some characteristic of a non-empty interface.
14133 // This is used to access the method table or underlying object of an interface.
14135 class Interface_info_expression
: public Expression
14138 Interface_info_expression(Expression
* iface
, Interface_info iface_info
,
14140 : Expression(EXPRESSION_INTERFACE_INFO
, location
),
14141 iface_(iface
), iface_info_(iface_info
)
14149 do_determine_type(const Type_context
*)
14155 return new Interface_info_expression(this->iface_
->copy(),
14156 this->iface_info_
, this->location());
14160 do_get_backend(Translate_context
* context
);
14163 do_dump_expression(Ast_dump_context
*) const;
14166 do_issue_nil_check()
14167 { this->iface_
->issue_nil_check(); }
14170 // The interface for which we are getting information.
14171 Expression
* iface_
;
14172 // What information we want.
14173 Interface_info iface_info_
;
14176 // Return the type of the interface info.
14179 Interface_info_expression::do_type()
14181 switch (this->iface_info_
)
14183 case INTERFACE_INFO_METHODS
:
14185 Type
* pdt
= Type::make_type_descriptor_ptr_type();
14186 if (this->iface_
->type()->interface_type()->is_empty())
14189 Location loc
= this->location();
14190 Struct_field_list
* sfl
= new Struct_field_list();
14192 Struct_field(Typed_identifier("__type_descriptor", pdt
, loc
)));
14194 Interface_type
* itype
= this->iface_
->type()->interface_type();
14195 for (Typed_identifier_list::const_iterator p
= itype
->methods()->begin();
14196 p
!= itype
->methods()->end();
14199 Function_type
* ft
= p
->type()->function_type();
14200 go_assert(ft
->receiver() == NULL
);
14202 const Typed_identifier_list
* params
= ft
->parameters();
14203 Typed_identifier_list
* mparams
= new Typed_identifier_list();
14204 if (params
!= NULL
)
14205 mparams
->reserve(params
->size() + 1);
14206 Type
* vt
= Type::make_pointer_type(Type::make_void_type());
14207 mparams
->push_back(Typed_identifier("", vt
, ft
->location()));
14208 if (params
!= NULL
)
14210 for (Typed_identifier_list::const_iterator pp
= params
->begin();
14211 pp
!= params
->end();
14213 mparams
->push_back(*pp
);
14216 Typed_identifier_list
* mresults
= (ft
->results() == NULL
14218 : ft
->results()->copy());
14219 Backend_function_type
* mft
=
14220 Type::make_backend_function_type(NULL
, mparams
, mresults
,
14223 std::string fname
= Gogo::unpack_hidden_name(p
->name());
14224 sfl
->push_back(Struct_field(Typed_identifier(fname
, mft
, loc
)));
14227 return Type::make_pointer_type(Type::make_struct_type(sfl
, loc
));
14229 case INTERFACE_INFO_OBJECT
:
14230 return Type::make_pointer_type(Type::make_void_type());
14236 // Return the backend representation for interface information.
14239 Interface_info_expression::do_get_backend(Translate_context
* context
)
14241 Gogo
* gogo
= context
->gogo();
14242 Bexpression
* biface
= this->iface_
->get_backend(context
);
14243 switch (this->iface_info_
)
14245 case INTERFACE_INFO_METHODS
:
14246 case INTERFACE_INFO_OBJECT
:
14247 return gogo
->backend()->struct_field_expression(biface
, this->iface_info_
,
14255 // Dump ast representation for an interface info expression.
14258 Interface_info_expression::do_dump_expression(
14259 Ast_dump_context
* ast_dump_context
) const
14261 bool is_empty
= this->iface_
->type()->interface_type()->is_empty();
14262 ast_dump_context
->ostream() << "interfaceinfo(";
14263 this->iface_
->dump_expression(ast_dump_context
);
14264 ast_dump_context
->ostream() << ",";
14265 ast_dump_context
->ostream() <<
14266 (this->iface_info_
== INTERFACE_INFO_METHODS
&& !is_empty
? "methods"
14267 : this->iface_info_
== INTERFACE_INFO_TYPE_DESCRIPTOR
? "type_descriptor"
14268 : this->iface_info_
== INTERFACE_INFO_OBJECT
? "object"
14270 ast_dump_context
->ostream() << ")";
14273 // Make an interface info expression.
14276 Expression::make_interface_info(Expression
* iface
, Interface_info iface_info
,
14279 return new Interface_info_expression(iface
, iface_info
, location
);
14282 // An expression that represents an interface value. The first field is either
14283 // a type descriptor for an empty interface or a pointer to the interface method
14284 // table for a non-empty interface. The second field is always the object.
14286 class Interface_value_expression
: public Expression
14289 Interface_value_expression(Type
* type
, Expression
* first_field
,
14290 Expression
* obj
, Location location
)
14291 : Expression(EXPRESSION_INTERFACE_VALUE
, location
),
14292 type_(type
), first_field_(first_field
), obj_(obj
)
14297 do_traverse(Traverse
*);
14301 { return this->type_
; }
14304 do_determine_type(const Type_context
*)
14305 { go_unreachable(); }
14310 return new Interface_value_expression(this->type_
,
14311 this->first_field_
->copy(),
14312 this->obj_
->copy(), this->location());
14316 do_get_backend(Translate_context
* context
);
14319 do_dump_expression(Ast_dump_context
*) const;
14322 // The type of the interface value.
14324 // The first field of the interface (either a type descriptor or a pointer
14325 // to the method table.
14326 Expression
* first_field_
;
14327 // The underlying object of the interface.
14332 Interface_value_expression::do_traverse(Traverse
* traverse
)
14334 if (Expression::traverse(&this->first_field_
, traverse
) == TRAVERSE_EXIT
14335 || Expression::traverse(&this->obj_
, traverse
) == TRAVERSE_EXIT
)
14336 return TRAVERSE_EXIT
;
14337 return TRAVERSE_CONTINUE
;
14341 Interface_value_expression::do_get_backend(Translate_context
* context
)
14343 std::vector
<Bexpression
*> vals(2);
14344 vals
[0] = this->first_field_
->get_backend(context
);
14345 vals
[1] = this->obj_
->get_backend(context
);
14347 Gogo
* gogo
= context
->gogo();
14348 Btype
* btype
= this->type_
->get_backend(gogo
);
14349 return gogo
->backend()->constructor_expression(btype
, vals
, this->location());
14353 Interface_value_expression::do_dump_expression(
14354 Ast_dump_context
* ast_dump_context
) const
14356 ast_dump_context
->ostream() << "interfacevalue(";
14357 ast_dump_context
->ostream() <<
14358 (this->type_
->interface_type()->is_empty()
14359 ? "type_descriptor: "
14361 this->first_field_
->dump_expression(ast_dump_context
);
14362 ast_dump_context
->ostream() << ", object: ";
14363 this->obj_
->dump_expression(ast_dump_context
);
14364 ast_dump_context
->ostream() << ")";
14368 Expression::make_interface_value(Type
* type
, Expression
* first_value
,
14369 Expression
* object
, Location location
)
14371 return new Interface_value_expression(type
, first_value
, object
, location
);
14374 // An interface method table for a pair of types: an interface type and a type
14375 // that implements that interface.
14377 class Interface_mtable_expression
: public Expression
14380 Interface_mtable_expression(Interface_type
* itype
, Type
* type
,
14381 bool is_pointer
, Location location
)
14382 : Expression(EXPRESSION_INTERFACE_MTABLE
, location
),
14383 itype_(itype
), type_(type
), is_pointer_(is_pointer
),
14384 method_table_type_(NULL
), bvar_(NULL
)
14389 do_traverse(Traverse
*);
14395 is_immutable() const
14399 do_determine_type(const Type_context
*)
14400 { go_unreachable(); }
14405 return new Interface_mtable_expression(this->itype_
, this->type_
,
14406 this->is_pointer_
, this->location());
14410 do_is_addressable() const
14414 do_get_backend(Translate_context
* context
);
14417 do_dump_expression(Ast_dump_context
*) const;
14420 // The interface type for which the methods are defined.
14421 Interface_type
* itype_
;
14422 // The type to construct the interface method table for.
14424 // Whether this table contains the method set for the receiver type or the
14425 // pointer receiver type.
14427 // The type of the method table.
14428 Type
* method_table_type_
;
14429 // The backend variable that refers to the interface method table.
14434 Interface_mtable_expression::do_traverse(Traverse
* traverse
)
14436 if (Type::traverse(this->itype_
, traverse
) == TRAVERSE_EXIT
14437 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
14438 return TRAVERSE_EXIT
;
14439 return TRAVERSE_CONTINUE
;
14443 Interface_mtable_expression::do_type()
14445 if (this->method_table_type_
!= NULL
)
14446 return this->method_table_type_
;
14448 const Typed_identifier_list
* interface_methods
= this->itype_
->methods();
14449 go_assert(!interface_methods
->empty());
14451 Struct_field_list
* sfl
= new Struct_field_list
;
14452 Typed_identifier
tid("__type_descriptor", Type::make_type_descriptor_ptr_type(),
14454 sfl
->push_back(Struct_field(tid
));
14455 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
14456 p
!= interface_methods
->end();
14458 sfl
->push_back(Struct_field(*p
));
14459 this->method_table_type_
= Type::make_struct_type(sfl
, this->location());
14460 return this->method_table_type_
;
14464 Interface_mtable_expression::do_get_backend(Translate_context
* context
)
14466 Gogo
* gogo
= context
->gogo();
14467 Location loc
= Linemap::predeclared_location();
14468 if (this->bvar_
!= NULL
)
14469 return gogo
->backend()->var_expression(this->bvar_
, this->location());
14471 const Typed_identifier_list
* interface_methods
= this->itype_
->methods();
14472 go_assert(!interface_methods
->empty());
14474 std::string mangled_name
= ((this->is_pointer_
? "__go_pimt__" : "__go_imt_")
14475 + this->itype_
->mangled_name(gogo
)
14477 + this->type_
->mangled_name(gogo
));
14479 // See whether this interface has any hidden methods.
14480 bool has_hidden_methods
= false;
14481 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
14482 p
!= interface_methods
->end();
14485 if (Gogo::is_hidden_name(p
->name()))
14487 has_hidden_methods
= true;
14492 // We already know that the named type is convertible to the
14493 // interface. If the interface has hidden methods, and the named
14494 // type is defined in a different package, then the interface
14495 // conversion table will be defined by that other package.
14496 if (has_hidden_methods
14497 && this->type_
->named_type() != NULL
14498 && this->type_
->named_type()->named_object()->package() != NULL
)
14500 Btype
* btype
= this->type()->get_backend(gogo
);
14502 gogo
->backend()->immutable_struct_reference(mangled_name
, btype
, loc
);
14503 return gogo
->backend()->var_expression(this->bvar_
, this->location());
14506 // The first element is the type descriptor.
14508 if (!this->is_pointer_
)
14509 td_type
= this->type_
;
14511 td_type
= Type::make_pointer_type(this->type_
);
14513 // Build an interface method table for a type: a type descriptor followed by a
14514 // list of function pointers, one for each interface method. This is used for
14516 Expression_list
* svals
= new Expression_list();
14517 svals
->push_back(Expression::make_type_descriptor(td_type
, loc
));
14519 Named_type
* nt
= this->type_
->named_type();
14520 Struct_type
* st
= this->type_
->struct_type();
14521 go_assert(nt
!= NULL
|| st
!= NULL
);
14523 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
14524 p
!= interface_methods
->end();
14530 m
= nt
->method_function(p
->name(), &is_ambiguous
);
14532 m
= st
->method_function(p
->name(), &is_ambiguous
);
14533 go_assert(m
!= NULL
);
14534 Named_object
* no
= m
->named_object();
14536 go_assert(no
->is_function() || no
->is_function_declaration());
14537 svals
->push_back(Expression::make_func_code_reference(no
, loc
));
14540 Btype
* btype
= this->type()->get_backend(gogo
);
14541 Expression
* mtable
= Expression::make_struct_composite_literal(this->type(),
14543 Bexpression
* ctor
= mtable
->get_backend(context
);
14545 bool is_public
= has_hidden_methods
&& this->type_
->named_type() != NULL
;
14546 this->bvar_
= gogo
->backend()->immutable_struct(mangled_name
, false,
14547 !is_public
, btype
, loc
);
14548 gogo
->backend()->immutable_struct_set_init(this->bvar_
, mangled_name
, false,
14549 !is_public
, btype
, loc
, ctor
);
14550 return gogo
->backend()->var_expression(this->bvar_
, loc
);
14554 Interface_mtable_expression::do_dump_expression(
14555 Ast_dump_context
* ast_dump_context
) const
14557 ast_dump_context
->ostream() << "__go_"
14558 << (this->is_pointer_
? "pimt__" : "imt_");
14559 ast_dump_context
->dump_type(this->itype_
);
14560 ast_dump_context
->ostream() << "__";
14561 ast_dump_context
->dump_type(this->type_
);
14565 Expression::make_interface_mtable_ref(Interface_type
* itype
, Type
* type
,
14566 bool is_pointer
, Location location
)
14568 return new Interface_mtable_expression(itype
, type
, is_pointer
, location
);
14571 // An expression which evaluates to the offset of a field within a
14572 // struct. This, like Type_info_expression, q.v., is only used to
14573 // initialize fields of a type descriptor.
14575 class Struct_field_offset_expression
: public Expression
14578 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
14579 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
,
14580 Linemap::predeclared_location()),
14581 type_(type
), field_(field
)
14586 do_is_immutable() const
14591 { return Type::lookup_integer_type("uintptr"); }
14594 do_determine_type(const Type_context
*)
14602 do_get_backend(Translate_context
* context
);
14605 do_dump_expression(Ast_dump_context
*) const;
14608 // The type of the struct.
14609 Struct_type
* type_
;
14611 const Struct_field
* field_
;
14614 // Return the backend representation for a struct field offset.
14617 Struct_field_offset_expression::do_get_backend(Translate_context
* context
)
14619 const Struct_field_list
* fields
= this->type_
->fields();
14620 Struct_field_list::const_iterator p
;
14622 for (p
= fields
->begin();
14623 p
!= fields
->end();
14625 if (&*p
== this->field_
)
14627 go_assert(&*p
== this->field_
);
14629 Gogo
* gogo
= context
->gogo();
14630 Btype
* btype
= this->type_
->get_backend(gogo
);
14632 size_t offset
= gogo
->backend()->type_field_offset(btype
, i
);
14633 Type
* uptr_type
= Type::lookup_integer_type("uintptr");
14635 Expression::make_integer_ul(offset
, uptr_type
,
14636 Linemap::predeclared_location());
14637 return ret
->get_backend(context
);
14640 // Dump ast representation for a struct field offset expression.
14643 Struct_field_offset_expression::do_dump_expression(
14644 Ast_dump_context
* ast_dump_context
) const
14646 ast_dump_context
->ostream() << "unsafe.Offsetof(";
14647 ast_dump_context
->dump_type(this->type_
);
14648 ast_dump_context
->ostream() << '.';
14649 ast_dump_context
->ostream() <<
14650 Gogo::message_name(this->field_
->field_name());
14651 ast_dump_context
->ostream() << ")";
14654 // Make an expression for a struct field offset.
14657 Expression::make_struct_field_offset(Struct_type
* type
,
14658 const Struct_field
* field
)
14660 return new Struct_field_offset_expression(type
, field
);
14663 // An expression which evaluates to a pointer to the map descriptor of
14666 class Map_descriptor_expression
: public Expression
14669 Map_descriptor_expression(Map_type
* type
, Location location
)
14670 : Expression(EXPRESSION_MAP_DESCRIPTOR
, location
),
14677 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
14680 do_determine_type(const Type_context
*)
14688 do_get_backend(Translate_context
* context
)
14690 return this->type_
->map_descriptor_pointer(context
->gogo(),
14695 do_dump_expression(Ast_dump_context
*) const;
14698 // The type for which this is the descriptor.
14702 // Dump ast representation for a map descriptor expression.
14705 Map_descriptor_expression::do_dump_expression(
14706 Ast_dump_context
* ast_dump_context
) const
14708 ast_dump_context
->ostream() << "map_descriptor(";
14709 ast_dump_context
->dump_type(this->type_
);
14710 ast_dump_context
->ostream() << ")";
14713 // Make a map descriptor expression.
14716 Expression::make_map_descriptor(Map_type
* type
, Location location
)
14718 return new Map_descriptor_expression(type
, location
);
14721 // An expression which evaluates to the address of an unnamed label.
14723 class Label_addr_expression
: public Expression
14726 Label_addr_expression(Label
* label
, Location location
)
14727 : Expression(EXPRESSION_LABEL_ADDR
, location
),
14734 { return Type::make_pointer_type(Type::make_void_type()); }
14737 do_determine_type(const Type_context
*)
14742 { return new Label_addr_expression(this->label_
, this->location()); }
14745 do_get_backend(Translate_context
* context
)
14746 { return this->label_
->get_addr(context
, this->location()); }
14749 do_dump_expression(Ast_dump_context
* ast_dump_context
) const
14750 { ast_dump_context
->ostream() << this->label_
->name(); }
14753 // The label whose address we are taking.
14757 // Make an expression for the address of an unnamed label.
14760 Expression::make_label_addr(Label
* label
, Location location
)
14762 return new Label_addr_expression(label
, location
);
14765 // Conditional expressions.
14767 class Conditional_expression
: public Expression
14770 Conditional_expression(Expression
* cond
, Expression
* then_expr
,
14771 Expression
* else_expr
, Location location
)
14772 : Expression(EXPRESSION_CONDITIONAL
, location
),
14773 cond_(cond
), then_(then_expr
), else_(else_expr
)
14778 do_traverse(Traverse
*);
14784 do_determine_type(const Type_context
*);
14789 return new Conditional_expression(this->cond_
->copy(), this->then_
->copy(),
14790 this->else_
->copy(), this->location());
14794 do_get_backend(Translate_context
* context
);
14797 do_dump_expression(Ast_dump_context
*) const;
14800 // The condition to be checked.
14802 // The expression to execute if the condition is true.
14804 // The expression to execute if the condition is false.
14811 Conditional_expression::do_traverse(Traverse
* traverse
)
14813 if (Expression::traverse(&this->cond_
, traverse
) == TRAVERSE_EXIT
14814 || Expression::traverse(&this->then_
, traverse
) == TRAVERSE_EXIT
14815 || Expression::traverse(&this->else_
, traverse
) == TRAVERSE_EXIT
)
14816 return TRAVERSE_EXIT
;
14817 return TRAVERSE_CONTINUE
;
14820 // Return the type of the conditional expression.
14823 Conditional_expression::do_type()
14825 Type
* result_type
= Type::make_void_type();
14826 if (Type::are_identical(this->then_
->type(), this->else_
->type(), false,
14828 result_type
= this->then_
->type();
14829 else if (this->then_
->is_nil_expression()
14830 || this->else_
->is_nil_expression())
14831 result_type
= (!this->then_
->is_nil_expression()
14832 ? this->then_
->type()
14833 : this->else_
->type());
14834 return result_type
;
14837 // Determine type for a conditional expression.
14840 Conditional_expression::do_determine_type(const Type_context
* context
)
14842 this->cond_
->determine_type_no_context();
14843 this->then_
->determine_type(context
);
14844 this->else_
->determine_type(context
);
14847 // Get the backend representation of a conditional expression.
14850 Conditional_expression::do_get_backend(Translate_context
* context
)
14852 Gogo
* gogo
= context
->gogo();
14853 Btype
* result_btype
= this->type()->get_backend(gogo
);
14854 Bexpression
* cond
= this->cond_
->get_backend(context
);
14855 Bexpression
* then
= this->then_
->get_backend(context
);
14856 Bexpression
* belse
= this->else_
->get_backend(context
);
14857 return gogo
->backend()->conditional_expression(result_btype
, cond
, then
,
14858 belse
, this->location());
14861 // Dump ast representation of a conditional expression.
14864 Conditional_expression::do_dump_expression(
14865 Ast_dump_context
* ast_dump_context
) const
14867 ast_dump_context
->ostream() << "(";
14868 ast_dump_context
->dump_expression(this->cond_
);
14869 ast_dump_context
->ostream() << " ? ";
14870 ast_dump_context
->dump_expression(this->then_
);
14871 ast_dump_context
->ostream() << " : ";
14872 ast_dump_context
->dump_expression(this->else_
);
14873 ast_dump_context
->ostream() << ") ";
14876 // Make a conditional expression.
14879 Expression::make_conditional(Expression
* cond
, Expression
* then
,
14880 Expression
* else_expr
, Location location
)
14882 return new Conditional_expression(cond
, then
, else_expr
, location
);
14885 // Compound expressions.
14887 class Compound_expression
: public Expression
14890 Compound_expression(Expression
* init
, Expression
* expr
, Location location
)
14891 : Expression(EXPRESSION_COMPOUND
, location
), init_(init
), expr_(expr
)
14896 do_traverse(Traverse
*);
14902 do_determine_type(const Type_context
*);
14907 return new Compound_expression(this->init_
->copy(), this->expr_
->copy(),
14912 do_get_backend(Translate_context
* context
);
14915 do_dump_expression(Ast_dump_context
*) const;
14918 // The expression that is evaluated first and discarded.
14920 // The expression that is evaluated and returned.
14927 Compound_expression::do_traverse(Traverse
* traverse
)
14929 if (Expression::traverse(&this->init_
, traverse
) == TRAVERSE_EXIT
14930 || Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
14931 return TRAVERSE_EXIT
;
14932 return TRAVERSE_CONTINUE
;
14935 // Return the type of the compound expression.
14938 Compound_expression::do_type()
14940 return this->expr_
->type();
14943 // Determine type for a compound expression.
14946 Compound_expression::do_determine_type(const Type_context
* context
)
14948 this->init_
->determine_type_no_context();
14949 this->expr_
->determine_type(context
);
14952 // Get the backend representation of a compound expression.
14955 Compound_expression::do_get_backend(Translate_context
* context
)
14957 Gogo
* gogo
= context
->gogo();
14958 Bexpression
* binit
= this->init_
->get_backend(context
);
14959 Bstatement
* init_stmt
= gogo
->backend()->expression_statement(binit
);
14960 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
14961 return gogo
->backend()->compound_expression(init_stmt
, bexpr
,
14965 // Dump ast representation of a conditional expression.
14968 Compound_expression::do_dump_expression(
14969 Ast_dump_context
* ast_dump_context
) const
14971 ast_dump_context
->ostream() << "(";
14972 ast_dump_context
->dump_expression(this->init_
);
14973 ast_dump_context
->ostream() << ",";
14974 ast_dump_context
->dump_expression(this->expr_
);
14975 ast_dump_context
->ostream() << ") ";
14978 // Make a compound expression.
14981 Expression::make_compound(Expression
* init
, Expression
* expr
, Location location
)
14983 return new Compound_expression(init
, expr
, location
);
14986 // Import an expression. This comes at the end in order to see the
14987 // various class definitions.
14990 Expression::import_expression(Import
* imp
)
14992 int c
= imp
->peek_char();
14993 if (imp
->match_c_string("- ")
14994 || imp
->match_c_string("! ")
14995 || imp
->match_c_string("^ "))
14996 return Unary_expression::do_import(imp
);
14998 return Binary_expression::do_import(imp
);
14999 else if (imp
->match_c_string("true")
15000 || imp
->match_c_string("false"))
15001 return Boolean_expression::do_import(imp
);
15003 return String_expression::do_import(imp
);
15004 else if (c
== '-' || (c
>= '0' && c
<= '9'))
15006 // This handles integers, floats and complex constants.
15007 return Integer_expression::do_import(imp
);
15009 else if (imp
->match_c_string("nil"))
15010 return Nil_expression::do_import(imp
);
15011 else if (imp
->match_c_string("convert"))
15012 return Type_conversion_expression::do_import(imp
);
15015 error_at(imp
->location(), "import error: expected expression");
15016 return Expression::make_error(imp
->location());
15020 // Class Expression_list.
15022 // Traverse the list.
15025 Expression_list::traverse(Traverse
* traverse
)
15027 for (Expression_list::iterator p
= this->begin();
15033 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
15034 return TRAVERSE_EXIT
;
15037 return TRAVERSE_CONTINUE
;
15043 Expression_list::copy()
15045 Expression_list
* ret
= new Expression_list();
15046 for (Expression_list::iterator p
= this->begin();
15051 ret
->push_back(NULL
);
15053 ret
->push_back((*p
)->copy());
15058 // Return whether an expression list has an error expression.
15061 Expression_list::contains_error() const
15063 for (Expression_list::const_iterator p
= this->begin();
15066 if (*p
!= NULL
&& (*p
)->is_error_expression())
15071 // Class Numeric_constant.
15075 Numeric_constant::~Numeric_constant()
15080 // Copy constructor.
15082 Numeric_constant::Numeric_constant(const Numeric_constant
& a
)
15083 : classification_(a
.classification_
), type_(a
.type_
)
15085 switch (a
.classification_
)
15091 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
15094 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, GMP_RNDN
);
15097 mpc_init2(this->u_
.complex_val
, mpc_precision
);
15098 mpc_set(this->u_
.complex_val
, a
.u_
.complex_val
, MPC_RNDNN
);
15105 // Assignment operator.
15108 Numeric_constant::operator=(const Numeric_constant
& a
)
15111 this->classification_
= a
.classification_
;
15112 this->type_
= a
.type_
;
15113 switch (a
.classification_
)
15119 mpz_init_set(this->u_
.int_val
, a
.u_
.int_val
);
15122 mpfr_init_set(this->u_
.float_val
, a
.u_
.float_val
, GMP_RNDN
);
15125 mpc_init2(this->u_
.complex_val
, mpc_precision
);
15126 mpc_set(this->u_
.complex_val
, a
.u_
.complex_val
, MPC_RNDNN
);
15134 // Clear the contents.
15137 Numeric_constant::clear()
15139 switch (this->classification_
)
15145 mpz_clear(this->u_
.int_val
);
15148 mpfr_clear(this->u_
.float_val
);
15151 mpc_clear(this->u_
.complex_val
);
15156 this->classification_
= NC_INVALID
;
15159 // Set to an unsigned long value.
15162 Numeric_constant::set_unsigned_long(Type
* type
, unsigned long val
)
15165 this->classification_
= NC_INT
;
15166 this->type_
= type
;
15167 mpz_init_set_ui(this->u_
.int_val
, val
);
15170 // Set to an integer value.
15173 Numeric_constant::set_int(Type
* type
, const mpz_t val
)
15176 this->classification_
= NC_INT
;
15177 this->type_
= type
;
15178 mpz_init_set(this->u_
.int_val
, val
);
15181 // Set to a rune value.
15184 Numeric_constant::set_rune(Type
* type
, const mpz_t val
)
15187 this->classification_
= NC_RUNE
;
15188 this->type_
= type
;
15189 mpz_init_set(this->u_
.int_val
, val
);
15192 // Set to a floating point value.
15195 Numeric_constant::set_float(Type
* type
, const mpfr_t val
)
15198 this->classification_
= NC_FLOAT
;
15199 this->type_
= type
;
15200 // Numeric constants do not have negative zero values, so remove
15201 // them here. They also don't have infinity or NaN values, but we
15202 // should never see them here.
15203 if (mpfr_zero_p(val
))
15204 mpfr_init_set_ui(this->u_
.float_val
, 0, GMP_RNDN
);
15206 mpfr_init_set(this->u_
.float_val
, val
, GMP_RNDN
);
15209 // Set to a complex value.
15212 Numeric_constant::set_complex(Type
* type
, const mpc_t val
)
15215 this->classification_
= NC_COMPLEX
;
15216 this->type_
= type
;
15217 mpc_init2(this->u_
.complex_val
, mpc_precision
);
15218 mpc_set(this->u_
.complex_val
, val
, MPC_RNDNN
);
15221 // Get an int value.
15224 Numeric_constant::get_int(mpz_t
* val
) const
15226 go_assert(this->is_int());
15227 mpz_init_set(*val
, this->u_
.int_val
);
15230 // Get a rune value.
15233 Numeric_constant::get_rune(mpz_t
* val
) const
15235 go_assert(this->is_rune());
15236 mpz_init_set(*val
, this->u_
.int_val
);
15239 // Get a floating point value.
15242 Numeric_constant::get_float(mpfr_t
* val
) const
15244 go_assert(this->is_float());
15245 mpfr_init_set(*val
, this->u_
.float_val
, GMP_RNDN
);
15248 // Get a complex value.
15251 Numeric_constant::get_complex(mpc_t
* val
) const
15253 go_assert(this->is_complex());
15254 mpc_init2(*val
, mpc_precision
);
15255 mpc_set(*val
, this->u_
.complex_val
, MPC_RNDNN
);
15258 // Express value as unsigned long if possible.
15260 Numeric_constant::To_unsigned_long
15261 Numeric_constant::to_unsigned_long(unsigned long* val
) const
15263 switch (this->classification_
)
15267 return this->mpz_to_unsigned_long(this->u_
.int_val
, val
);
15269 return this->mpfr_to_unsigned_long(this->u_
.float_val
, val
);
15271 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
15272 return NC_UL_NOTINT
;
15273 return this->mpfr_to_unsigned_long(mpc_realref(this->u_
.complex_val
),
15280 // Express integer value as unsigned long if possible.
15282 Numeric_constant::To_unsigned_long
15283 Numeric_constant::mpz_to_unsigned_long(const mpz_t ival
,
15284 unsigned long *val
) const
15286 if (mpz_sgn(ival
) < 0)
15287 return NC_UL_NEGATIVE
;
15288 unsigned long ui
= mpz_get_ui(ival
);
15289 if (mpz_cmp_ui(ival
, ui
) != 0)
15292 return NC_UL_VALID
;
15295 // Express floating point value as unsigned long if possible.
15297 Numeric_constant::To_unsigned_long
15298 Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval
,
15299 unsigned long *val
) const
15301 if (!mpfr_integer_p(fval
))
15302 return NC_UL_NOTINT
;
15305 mpfr_get_z(ival
, fval
, GMP_RNDN
);
15306 To_unsigned_long ret
= this->mpz_to_unsigned_long(ival
, val
);
15311 // Convert value to integer if possible.
15314 Numeric_constant::to_int(mpz_t
* val
) const
15316 switch (this->classification_
)
15320 mpz_init_set(*val
, this->u_
.int_val
);
15323 if (!mpfr_integer_p(this->u_
.float_val
))
15326 mpfr_get_z(*val
, this->u_
.float_val
, GMP_RNDN
);
15329 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
))
15330 || !mpfr_integer_p(mpc_realref(this->u_
.complex_val
)))
15333 mpfr_get_z(*val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15340 // Convert value to floating point if possible.
15343 Numeric_constant::to_float(mpfr_t
* val
) const
15345 switch (this->classification_
)
15349 mpfr_init_set_z(*val
, this->u_
.int_val
, GMP_RNDN
);
15352 mpfr_init_set(*val
, this->u_
.float_val
, GMP_RNDN
);
15355 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
15357 mpfr_init_set(*val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15364 // Convert value to complex.
15367 Numeric_constant::to_complex(mpc_t
* val
) const
15369 mpc_init2(*val
, mpc_precision
);
15370 switch (this->classification_
)
15374 mpc_set_z(*val
, this->u_
.int_val
, MPC_RNDNN
);
15377 mpc_set_fr(*val
, this->u_
.float_val
, MPC_RNDNN
);
15380 mpc_set(*val
, this->u_
.complex_val
, MPC_RNDNN
);
15390 Numeric_constant::type() const
15392 if (this->type_
!= NULL
)
15393 return this->type_
;
15394 switch (this->classification_
)
15397 return Type::make_abstract_integer_type();
15399 return Type::make_abstract_character_type();
15401 return Type::make_abstract_float_type();
15403 return Type::make_abstract_complex_type();
15409 // If the constant can be expressed in TYPE, then set the type of the
15410 // constant to TYPE and return true. Otherwise return false, and, if
15411 // ISSUE_ERROR is true, report an appropriate error message.
15414 Numeric_constant::set_type(Type
* type
, bool issue_error
, Location loc
)
15419 else if (type
->integer_type() != NULL
)
15420 ret
= this->check_int_type(type
->integer_type(), issue_error
, loc
);
15421 else if (type
->float_type() != NULL
)
15422 ret
= this->check_float_type(type
->float_type(), issue_error
, loc
);
15423 else if (type
->complex_type() != NULL
)
15424 ret
= this->check_complex_type(type
->complex_type(), issue_error
, loc
);
15428 this->type_
= type
;
15432 // Check whether the constant can be expressed in an integer type.
15435 Numeric_constant::check_int_type(Integer_type
* type
, bool issue_error
,
15436 Location location
) const
15439 switch (this->classification_
)
15443 mpz_init_set(val
, this->u_
.int_val
);
15447 if (!mpfr_integer_p(this->u_
.float_val
))
15450 error_at(location
, "floating point constant truncated to integer");
15454 mpfr_get_z(val
, this->u_
.float_val
, GMP_RNDN
);
15458 if (!mpfr_integer_p(mpc_realref(this->u_
.complex_val
))
15459 || !mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
15462 error_at(location
, "complex constant truncated to integer");
15466 mpfr_get_z(val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15474 if (type
->is_abstract())
15478 int bits
= mpz_sizeinbase(val
, 2);
15479 if (type
->is_unsigned())
15481 // For an unsigned type we can only accept a nonnegative
15482 // number, and we must be able to represents at least BITS.
15483 ret
= mpz_sgn(val
) >= 0 && bits
<= type
->bits();
15487 // For a signed type we need an extra bit to indicate the
15488 // sign. We have to handle the most negative integer
15490 ret
= (bits
+ 1 <= type
->bits()
15491 || (bits
<= type
->bits()
15492 && mpz_sgn(val
) < 0
15493 && (mpz_scan1(val
, 0)
15494 == static_cast<unsigned long>(type
->bits() - 1))
15495 && mpz_scan0(val
, type
->bits()) == ULONG_MAX
));
15499 if (!ret
&& issue_error
)
15500 error_at(location
, "integer constant overflow");
15505 // Check whether the constant can be expressed in a floating point
15509 Numeric_constant::check_float_type(Float_type
* type
, bool issue_error
,
15513 switch (this->classification_
)
15517 mpfr_init_set_z(val
, this->u_
.int_val
, GMP_RNDN
);
15521 mpfr_init_set(val
, this->u_
.float_val
, GMP_RNDN
);
15525 if (!mpfr_zero_p(mpc_imagref(this->u_
.complex_val
)))
15528 error_at(location
, "complex constant truncated to float");
15531 mpfr_init_set(val
, mpc_realref(this->u_
.complex_val
), GMP_RNDN
);
15539 if (type
->is_abstract())
15541 else if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
15543 // A NaN or Infinity always fits in the range of the type.
15548 mp_exp_t exp
= mpfr_get_exp(val
);
15550 switch (type
->bits())
15562 ret
= exp
<= max_exp
;
15566 // Round the constant to the desired type.
15569 switch (type
->bits())
15572 mpfr_set_prec(t
, 24);
15575 mpfr_set_prec(t
, 53);
15580 mpfr_set(t
, val
, GMP_RNDN
);
15581 mpfr_set(val
, t
, GMP_RNDN
);
15584 this->set_float(type
, val
);
15590 if (!ret
&& issue_error
)
15591 error_at(location
, "floating point constant overflow");
15596 // Check whether the constant can be expressed in a complex type.
15599 Numeric_constant::check_complex_type(Complex_type
* type
, bool issue_error
,
15602 if (type
->is_abstract())
15606 switch (type
->bits())
15619 mpc_init2(val
, mpc_precision
);
15620 switch (this->classification_
)
15624 mpc_set_z(val
, this->u_
.int_val
, MPC_RNDNN
);
15628 mpc_set_fr(val
, this->u_
.float_val
, MPC_RNDNN
);
15632 mpc_set(val
, this->u_
.complex_val
, MPC_RNDNN
);
15640 if (!mpfr_nan_p(mpc_realref(val
))
15641 && !mpfr_inf_p(mpc_realref(val
))
15642 && !mpfr_zero_p(mpc_realref(val
))
15643 && mpfr_get_exp(mpc_realref(val
)) > max_exp
)
15646 error_at(location
, "complex real part overflow");
15650 if (!mpfr_nan_p(mpc_imagref(val
))
15651 && !mpfr_inf_p(mpc_imagref(val
))
15652 && !mpfr_zero_p(mpc_imagref(val
))
15653 && mpfr_get_exp(mpc_imagref(val
)) > max_exp
)
15656 error_at(location
, "complex imaginary part overflow");
15662 // Round the constant to the desired type.
15664 switch (type
->bits())
15675 mpc_set(t
, val
, MPC_RNDNN
);
15676 mpc_set(val
, t
, MPC_RNDNN
);
15679 this->set_complex(type
, val
);
15687 // Return an Expression for this value.
15690 Numeric_constant::expression(Location loc
) const
15692 switch (this->classification_
)
15695 return Expression::make_integer_z(&this->u_
.int_val
, this->type_
, loc
);
15697 return Expression::make_character(&this->u_
.int_val
, this->type_
, loc
);
15699 return Expression::make_float(&this->u_
.float_val
, this->type_
, loc
);
15701 return Expression::make_complex(&this->u_
.complex_val
, this->type_
, loc
);