1 // statements.cc -- Go frontend statements.
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.
11 #include "expressions.h"
15 #include "statements.h"
20 Statement::Statement(Statement_classification classification
,
22 : classification_(classification
), location_(location
)
26 Statement::~Statement()
30 // Traverse the tree. The work of walking the components is handled
34 Statement::traverse(Block
* block
, size_t* pindex
, Traverse
* traverse
)
36 if (this->classification_
== STATEMENT_ERROR
)
37 return TRAVERSE_CONTINUE
;
39 unsigned int traverse_mask
= traverse
->traverse_mask();
41 if ((traverse_mask
& Traverse::traverse_statements
) != 0)
43 int t
= traverse
->statement(block
, pindex
, this);
44 if (t
== TRAVERSE_EXIT
)
46 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
47 return TRAVERSE_CONTINUE
;
50 // No point in checking traverse_mask here--a statement may contain
51 // other blocks or statements, and if we got here we always want to
53 return this->do_traverse(traverse
);
56 // Traverse the contents of a statement.
59 Statement::traverse_contents(Traverse
* traverse
)
61 return this->do_traverse(traverse
);
64 // Traverse assignments.
67 Statement::traverse_assignments(Traverse_assignments
* tassign
)
69 if (this->classification_
== STATEMENT_ERROR
)
71 return this->do_traverse_assignments(tassign
);
74 // Traverse an expression in a statement. This is a helper function
78 Statement::traverse_expression(Traverse
* traverse
, Expression
** expr
)
80 if ((traverse
->traverse_mask()
81 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
82 return TRAVERSE_CONTINUE
;
83 return Expression::traverse(expr
, traverse
);
86 // Traverse an expression list in a statement. This is a helper
87 // function for child classes.
90 Statement::traverse_expression_list(Traverse
* traverse
,
91 Expression_list
* expr_list
)
93 if (expr_list
== NULL
)
94 return TRAVERSE_CONTINUE
;
95 if ((traverse
->traverse_mask()
96 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
97 return TRAVERSE_CONTINUE
;
98 return expr_list
->traverse(traverse
);
101 // Traverse a type in a statement. This is a helper function for
105 Statement::traverse_type(Traverse
* traverse
, Type
* type
)
107 if ((traverse
->traverse_mask()
108 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
109 return TRAVERSE_CONTINUE
;
110 return Type::traverse(type
, traverse
);
113 // Set type information for unnamed constants. This is really done by
117 Statement::determine_types()
119 this->do_determine_types();
122 // If this is a thunk statement, return it.
125 Statement::thunk_statement()
127 Thunk_statement
* ret
= this->convert
<Thunk_statement
, STATEMENT_GO
>();
129 ret
= this->convert
<Thunk_statement
, STATEMENT_DEFER
>();
133 // Convert a Statement to the backend representation. This is really
134 // done by the child class.
137 Statement::get_backend(Translate_context
* context
)
139 if (this->classification_
== STATEMENT_ERROR
)
140 return context
->backend()->error_statement();
141 return this->do_get_backend(context
);
144 // Dump AST representation for a statement to a dump context.
147 Statement::dump_statement(Ast_dump_context
* ast_dump_context
) const
149 this->do_dump_statement(ast_dump_context
);
152 // Note that this statement is erroneous. This is called by children
153 // when they discover an error.
156 Statement::set_is_error()
158 this->classification_
= STATEMENT_ERROR
;
161 // For children to call to report an error conveniently.
164 Statement::report_error(const char* msg
)
166 error_at(this->location_
, "%s", msg
);
167 this->set_is_error();
170 // An error statement, used to avoid crashing after we report an
173 class Error_statement
: public Statement
176 Error_statement(Location location
)
177 : Statement(STATEMENT_ERROR
, location
)
182 do_traverse(Traverse
*)
183 { return TRAVERSE_CONTINUE
; }
186 do_get_backend(Translate_context
*)
187 { go_unreachable(); }
190 do_dump_statement(Ast_dump_context
*) const;
193 // Dump the AST representation for an error statement.
196 Error_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
198 ast_dump_context
->print_indent();
199 ast_dump_context
->ostream() << "Error statement" << std::endl
;
202 // Make an error statement.
205 Statement::make_error_statement(Location location
)
207 return new Error_statement(location
);
210 // Class Variable_declaration_statement.
212 Variable_declaration_statement::Variable_declaration_statement(
214 : Statement(STATEMENT_VARIABLE_DECLARATION
, var
->var_value()->location()),
219 // We don't actually traverse the variable here; it was traversed
220 // while traversing the Block.
223 Variable_declaration_statement::do_traverse(Traverse
*)
225 return TRAVERSE_CONTINUE
;
228 // Traverse the assignments in a variable declaration. Note that this
229 // traversal is different from the usual traversal.
232 Variable_declaration_statement::do_traverse_assignments(
233 Traverse_assignments
* tassign
)
235 tassign
->initialize_variable(this->var_
);
239 // Lower the variable's initialization expression.
242 Variable_declaration_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
243 Block
*, Statement_inserter
* inserter
)
245 this->var_
->var_value()->lower_init_expression(gogo
, function
, inserter
);
249 // Flatten the variable's initialization expression.
252 Variable_declaration_statement::do_flatten(Gogo
* gogo
, Named_object
* function
,
253 Block
*, Statement_inserter
* inserter
)
255 this->var_
->var_value()->flatten_init_expression(gogo
, function
, inserter
);
259 // Convert a variable declaration to the backend representation.
262 Variable_declaration_statement::do_get_backend(Translate_context
* context
)
264 Variable
* var
= this->var_
->var_value();
265 Bvariable
* bvar
= this->var_
->get_backend_variable(context
->gogo(),
266 context
->function());
267 Bexpression
* binit
= var
->get_init(context
->gogo(), context
->function());
269 if (!var
->is_in_heap())
271 go_assert(binit
!= NULL
);
272 return context
->backend()->init_statement(bvar
, binit
);
275 // Something takes the address of this variable, so the value is
276 // stored in the heap. Initialize it to newly allocated memory
277 // space, and assign the initial value to the new space.
278 Location loc
= this->location();
279 Named_object
* newfn
= context
->gogo()->lookup_global("new");
280 go_assert(newfn
!= NULL
&& newfn
->is_function_declaration());
281 Expression
* func
= Expression::make_func_reference(newfn
, NULL
, loc
);
282 Expression_list
* params
= new Expression_list();
283 params
->push_back(Expression::make_type(var
->type(), loc
));
284 Expression
* call
= Expression::make_call(func
, params
, false, loc
);
285 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
286 Temporary_statement
* temp
= Statement::make_temporary(NULL
, call
, loc
);
287 Bstatement
* btemp
= temp
->get_backend(context
);
289 Bstatement
* set
= NULL
;
292 Expression
* e
= Expression::make_temporary_reference(temp
, loc
);
293 e
= Expression::make_unary(OPERATOR_MULT
, e
, loc
);
294 Bexpression
* be
= e
->get_backend(context
);
295 set
= context
->backend()->assignment_statement(be
, binit
, loc
);
298 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
299 Bexpression
* bref
= ref
->get_backend(context
);
300 Bstatement
* sinit
= context
->backend()->init_statement(bvar
, bref
);
302 std::vector
<Bstatement
*> stats
;
304 stats
.push_back(btemp
);
306 stats
.push_back(set
);
307 stats
.push_back(sinit
);
308 return context
->backend()->statement_list(stats
);
311 // Dump the AST representation for a variable declaration.
314 Variable_declaration_statement::do_dump_statement(
315 Ast_dump_context
* ast_dump_context
) const
317 ast_dump_context
->print_indent();
319 go_assert(var_
->is_variable());
320 ast_dump_context
->ostream() << "var " << this->var_
->name() << " ";
321 Variable
* var
= this->var_
->var_value();
324 ast_dump_context
->dump_type(var
->type());
325 ast_dump_context
->ostream() << " ";
327 if (var
->init() != NULL
)
329 ast_dump_context
->ostream() << "= ";
330 ast_dump_context
->dump_expression(var
->init());
332 ast_dump_context
->ostream() << std::endl
;
335 // Make a variable declaration.
338 Statement::make_variable_declaration(Named_object
* var
)
340 return new Variable_declaration_statement(var
);
343 // Class Temporary_statement.
345 // Return the type of the temporary variable.
348 Temporary_statement::type() const
350 return this->type_
!= NULL
? this->type_
: this->init_
->type();
356 Temporary_statement::do_traverse(Traverse
* traverse
)
358 if (this->type_
!= NULL
359 && this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
360 return TRAVERSE_EXIT
;
361 if (this->init_
== NULL
)
362 return TRAVERSE_CONTINUE
;
364 return this->traverse_expression(traverse
, &this->init_
);
367 // Traverse assignments.
370 Temporary_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
372 if (this->init_
== NULL
)
374 tassign
->value(&this->init_
, true, true);
381 Temporary_statement::do_determine_types()
383 if (this->type_
!= NULL
&& this->type_
->is_abstract())
384 this->type_
= this->type_
->make_non_abstract_type();
386 if (this->init_
!= NULL
)
388 if (this->type_
== NULL
)
389 this->init_
->determine_type_no_context();
392 Type_context
context(this->type_
, false);
393 this->init_
->determine_type(&context
);
397 if (this->type_
== NULL
)
399 this->type_
= this->init_
->type();
400 go_assert(!this->type_
->is_abstract());
407 Temporary_statement::do_check_types(Gogo
*)
409 if (this->type_
!= NULL
&& this->init_
!= NULL
)
412 if (!Type::are_assignable(this->type_
, this->init_
->type(), &reason
))
415 error_at(this->location(), "incompatible types in assignment");
417 error_at(this->location(), "incompatible types in assignment (%s)",
419 this->set_is_error();
424 // Convert to backend representation.
427 Temporary_statement::do_get_backend(Translate_context
* context
)
429 go_assert(this->bvariable_
== NULL
);
431 Named_object
* function
= context
->function();
432 go_assert(function
!= NULL
);
433 Bfunction
* bfunction
= function
->func_value()->get_decl();
434 Btype
* btype
= this->type()->get_backend(context
->gogo());
437 if (this->init_
== NULL
)
439 else if (this->type_
== NULL
)
440 binit
= this->init_
->get_backend(context
);
443 Expression
* init
= Expression::make_cast(this->type_
, this->init_
,
445 context
->gogo()->lower_expression(context
->function(), NULL
, &init
);
446 binit
= init
->get_backend(context
);
449 Bstatement
* statement
;
451 context
->backend()->temporary_variable(bfunction
, context
->bblock(),
453 this->is_address_taken_
,
454 this->location(), &statement
);
458 // Return the backend variable.
461 Temporary_statement::get_backend_variable(Translate_context
* context
) const
463 if (this->bvariable_
== NULL
)
465 go_assert(saw_errors());
466 return context
->backend()->error_variable();
468 return this->bvariable_
;
471 // Dump the AST represemtation for a temporary statement
474 Temporary_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
476 ast_dump_context
->print_indent();
477 ast_dump_context
->dump_temp_variable_name(this);
478 if (this->type_
!= NULL
)
480 ast_dump_context
->ostream() << " ";
481 ast_dump_context
->dump_type(this->type_
);
483 if (this->init_
!= NULL
)
485 ast_dump_context
->ostream() << " = ";
486 ast_dump_context
->dump_expression(this->init_
);
488 ast_dump_context
->ostream() << std::endl
;
491 // Make and initialize a temporary variable in BLOCK.
494 Statement::make_temporary(Type
* type
, Expression
* init
,
497 return new Temporary_statement(type
, init
, location
);
500 // An assignment statement.
502 class Assignment_statement
: public Statement
505 Assignment_statement(Expression
* lhs
, Expression
* rhs
,
507 : Statement(STATEMENT_ASSIGNMENT
, location
),
513 do_traverse(Traverse
* traverse
);
516 do_traverse_assignments(Traverse_assignments
*);
519 do_determine_types();
522 do_check_types(Gogo
*);
525 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
528 do_get_backend(Translate_context
*);
531 do_dump_statement(Ast_dump_context
*) const;
534 // Left hand side--the lvalue.
536 // Right hand side--the rvalue.
543 Assignment_statement::do_traverse(Traverse
* traverse
)
545 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
546 return TRAVERSE_EXIT
;
547 return this->traverse_expression(traverse
, &this->rhs_
);
551 Assignment_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
553 tassign
->assignment(&this->lhs_
, &this->rhs_
);
557 // Set types for the assignment.
560 Assignment_statement::do_determine_types()
562 this->lhs_
->determine_type_no_context();
563 Type
* rhs_context_type
= this->lhs_
->type();
564 if (rhs_context_type
->is_sink_type())
565 rhs_context_type
= NULL
;
566 Type_context
context(rhs_context_type
, false);
567 this->rhs_
->determine_type(&context
);
570 // Check types for an assignment.
573 Assignment_statement::do_check_types(Gogo
*)
575 // The left hand side must be either addressable, a map index
576 // expression, or the blank identifier.
577 if (!this->lhs_
->is_addressable()
578 && this->lhs_
->map_index_expression() == NULL
579 && !this->lhs_
->is_sink_expression())
581 if (!this->lhs_
->type()->is_error())
582 this->report_error(_("invalid left hand side of assignment"));
586 Type
* lhs_type
= this->lhs_
->type();
587 Type
* rhs_type
= this->rhs_
->type();
589 // Invalid assignment of nil to the blank identifier.
590 if (lhs_type
->is_sink_type()
591 && rhs_type
->is_nil_type())
593 this->report_error(_("use of untyped nil"));
598 if (!Type::are_assignable(lhs_type
, rhs_type
, &reason
))
601 error_at(this->location(), "incompatible types in assignment");
603 error_at(this->location(), "incompatible types in assignment (%s)",
605 this->set_is_error();
608 if (lhs_type
->is_error() || rhs_type
->is_error())
609 this->set_is_error();
612 // Flatten an assignment statement. We may need a temporary for
613 // interface conversion.
616 Assignment_statement::do_flatten(Gogo
*, Named_object
*, Block
*,
617 Statement_inserter
* inserter
)
619 if (!this->lhs_
->is_sink_expression()
620 && !Type::are_identical(this->lhs_
->type(), this->rhs_
->type(),
622 && this->rhs_
->type()->interface_type() != NULL
623 && !this->rhs_
->is_variable())
625 Temporary_statement
* temp
=
626 Statement::make_temporary(NULL
, this->rhs_
, this->location());
627 inserter
->insert(temp
);
628 this->rhs_
= Expression::make_temporary_reference(temp
,
634 // Convert an assignment statement to the backend representation.
637 Assignment_statement::do_get_backend(Translate_context
* context
)
639 if (this->lhs_
->is_sink_expression())
641 Bexpression
* rhs
= this->rhs_
->get_backend(context
);
642 return context
->backend()->expression_statement(rhs
);
645 Bexpression
* lhs
= this->lhs_
->get_backend(context
);
647 Expression::convert_for_assignment(context
->gogo(), this->lhs_
->type(),
648 this->rhs_
, this->location());
649 Bexpression
* rhs
= conv
->get_backend(context
);
650 return context
->backend()->assignment_statement(lhs
, rhs
, this->location());
653 // Dump the AST representation for an assignment statement.
656 Assignment_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
659 ast_dump_context
->print_indent();
660 ast_dump_context
->dump_expression(this->lhs_
);
661 ast_dump_context
->ostream() << " = " ;
662 ast_dump_context
->dump_expression(this->rhs_
);
663 ast_dump_context
->ostream() << std::endl
;
666 // Make an assignment statement.
669 Statement::make_assignment(Expression
* lhs
, Expression
* rhs
,
672 return new Assignment_statement(lhs
, rhs
, location
);
675 // The Move_subexpressions class is used to move all top-level
676 // subexpressions of an expression. This is used for things like
677 // index expressions in which we must evaluate the index value before
678 // it can be changed by a multiple assignment.
680 class Move_subexpressions
: public Traverse
683 Move_subexpressions(int skip
, Block
* block
)
684 : Traverse(traverse_expressions
),
685 skip_(skip
), block_(block
)
690 expression(Expression
**);
693 // The number of subexpressions to skip moving. This is used to
694 // avoid moving the array itself, as we only need to move the index.
696 // The block where new temporary variables should be added.
701 Move_subexpressions::expression(Expression
** pexpr
)
705 else if ((*pexpr
)->temporary_reference_expression() == NULL
706 && !(*pexpr
)->is_nil_expression())
708 Location loc
= (*pexpr
)->location();
709 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
710 this->block_
->add_statement(temp
);
711 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
713 // We only need to move top-level subexpressions.
714 return TRAVERSE_SKIP_COMPONENTS
;
717 // The Move_ordered_evals class is used to find any subexpressions of
718 // an expression that have an evaluation order dependency. It creates
719 // temporary variables to hold them.
721 class Move_ordered_evals
: public Traverse
724 Move_ordered_evals(Block
* block
)
725 : Traverse(traverse_expressions
),
731 expression(Expression
**);
734 // The block where new temporary variables should be added.
739 Move_ordered_evals::expression(Expression
** pexpr
)
741 // We have to look at subexpressions first.
742 if ((*pexpr
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
743 return TRAVERSE_EXIT
;
746 if ((*pexpr
)->must_eval_subexpressions_in_order(&i
))
748 Move_subexpressions
ms(i
, this->block_
);
749 if ((*pexpr
)->traverse_subexpressions(&ms
) == TRAVERSE_EXIT
)
750 return TRAVERSE_EXIT
;
753 if ((*pexpr
)->must_eval_in_order())
755 Call_expression
* call
= (*pexpr
)->call_expression();
756 if (call
!= NULL
&& call
->is_multi_value_arg())
758 // A call expression which returns multiple results as an argument
759 // to another call must be handled specially. We can't create a
760 // temporary because there is no type to give it. Instead, group
761 // the caller and this multi-valued call argument and use a temporary
762 // variable to hold them.
763 return TRAVERSE_SKIP_COMPONENTS
;
766 Location loc
= (*pexpr
)->location();
767 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
768 this->block_
->add_statement(temp
);
769 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
771 return TRAVERSE_SKIP_COMPONENTS
;
774 // An assignment operation statement.
776 class Assignment_operation_statement
: public Statement
779 Assignment_operation_statement(Operator op
, Expression
* lhs
, Expression
* rhs
,
781 : Statement(STATEMENT_ASSIGNMENT_OPERATION
, location
),
782 op_(op
), lhs_(lhs
), rhs_(rhs
)
787 do_traverse(Traverse
*);
790 do_traverse_assignments(Traverse_assignments
*)
791 { go_unreachable(); }
794 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
797 do_get_backend(Translate_context
*)
798 { go_unreachable(); }
801 do_dump_statement(Ast_dump_context
*) const;
804 // The operator (OPERATOR_PLUSEQ, etc.).
815 Assignment_operation_statement::do_traverse(Traverse
* traverse
)
817 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
818 return TRAVERSE_EXIT
;
819 return this->traverse_expression(traverse
, &this->rhs_
);
822 // Lower an assignment operation statement to a regular assignment
826 Assignment_operation_statement::do_lower(Gogo
*, Named_object
*,
827 Block
* enclosing
, Statement_inserter
*)
829 Location loc
= this->location();
831 // We have to evaluate the left hand side expression only once. We
832 // do this by moving out any expression with side effects.
833 Block
* b
= new Block(enclosing
, loc
);
834 Move_ordered_evals
moe(b
);
835 this->lhs_
->traverse_subexpressions(&moe
);
837 Expression
* lval
= this->lhs_
->copy();
842 case OPERATOR_PLUSEQ
:
845 case OPERATOR_MINUSEQ
:
854 case OPERATOR_MULTEQ
:
863 case OPERATOR_LSHIFTEQ
:
864 op
= OPERATOR_LSHIFT
;
866 case OPERATOR_RSHIFTEQ
:
867 op
= OPERATOR_RSHIFT
;
872 case OPERATOR_BITCLEAREQ
:
873 op
= OPERATOR_BITCLEAR
;
879 Expression
* binop
= Expression::make_binary(op
, lval
, this->rhs_
, loc
);
880 Statement
* s
= Statement::make_assignment(this->lhs_
, binop
, loc
);
881 if (b
->statements()->empty())
889 return Statement::make_block_statement(b
, loc
);
893 // Dump the AST representation for an assignment operation statement
896 Assignment_operation_statement::do_dump_statement(
897 Ast_dump_context
* ast_dump_context
) const
899 ast_dump_context
->print_indent();
900 ast_dump_context
->dump_expression(this->lhs_
);
901 ast_dump_context
->dump_operator(this->op_
);
902 ast_dump_context
->dump_expression(this->rhs_
);
903 ast_dump_context
->ostream() << std::endl
;
906 // Make an assignment operation statement.
909 Statement::make_assignment_operation(Operator op
, Expression
* lhs
,
910 Expression
* rhs
, Location location
)
912 return new Assignment_operation_statement(op
, lhs
, rhs
, location
);
915 // A tuple assignment statement. This differs from an assignment
916 // statement in that the right-hand-side expressions are evaluated in
919 class Tuple_assignment_statement
: public Statement
922 Tuple_assignment_statement(Expression_list
* lhs
, Expression_list
* rhs
,
924 : Statement(STATEMENT_TUPLE_ASSIGNMENT
, location
),
930 do_traverse(Traverse
* traverse
);
933 do_traverse_assignments(Traverse_assignments
*)
934 { go_unreachable(); }
937 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
940 do_get_backend(Translate_context
*)
941 { go_unreachable(); }
944 do_dump_statement(Ast_dump_context
*) const;
947 // Left hand side--a list of lvalues.
948 Expression_list
* lhs_
;
949 // Right hand side--a list of rvalues.
950 Expression_list
* rhs_
;
956 Tuple_assignment_statement::do_traverse(Traverse
* traverse
)
958 if (this->traverse_expression_list(traverse
, this->lhs_
) == TRAVERSE_EXIT
)
959 return TRAVERSE_EXIT
;
960 return this->traverse_expression_list(traverse
, this->rhs_
);
963 // Lower a tuple assignment. We use temporary variables to split it
964 // up into a set of single assignments.
967 Tuple_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
970 Location loc
= this->location();
972 Block
* b
= new Block(enclosing
, loc
);
974 // First move out any subexpressions on the left hand side. The
975 // right hand side will be evaluated in the required order anyhow.
976 Move_ordered_evals
moe(b
);
977 for (Expression_list::iterator plhs
= this->lhs_
->begin();
978 plhs
!= this->lhs_
->end();
980 Expression::traverse(&*plhs
, &moe
);
982 std::vector
<Temporary_statement
*> temps
;
983 temps
.reserve(this->lhs_
->size());
985 Expression_list::const_iterator prhs
= this->rhs_
->begin();
986 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
987 plhs
!= this->lhs_
->end();
990 go_assert(prhs
!= this->rhs_
->end());
992 if ((*plhs
)->is_error_expression()
993 || (*plhs
)->type()->is_error()
994 || (*prhs
)->is_error_expression()
995 || (*prhs
)->type()->is_error())
998 if ((*plhs
)->is_sink_expression())
1000 if ((*prhs
)->type()->is_nil_type())
1001 this->report_error(_("use of untyped nil"));
1003 b
->add_statement(Statement::make_statement(*prhs
, true));
1007 Temporary_statement
* temp
= Statement::make_temporary((*plhs
)->type(),
1009 b
->add_statement(temp
);
1010 temps
.push_back(temp
);
1013 go_assert(prhs
== this->rhs_
->end());
1015 prhs
= this->rhs_
->begin();
1016 std::vector
<Temporary_statement
*>::const_iterator ptemp
= temps
.begin();
1017 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
1018 plhs
!= this->lhs_
->end();
1021 if ((*plhs
)->is_error_expression()
1022 || (*plhs
)->type()->is_error()
1023 || (*prhs
)->is_error_expression()
1024 || (*prhs
)->type()->is_error())
1027 if ((*plhs
)->is_sink_expression())
1030 Expression
* ref
= Expression::make_temporary_reference(*ptemp
, loc
);
1031 b
->add_statement(Statement::make_assignment(*plhs
, ref
, loc
));
1034 go_assert(ptemp
== temps
.end() || saw_errors());
1036 return Statement::make_block_statement(b
, loc
);
1039 // Dump the AST representation for a tuple assignment statement.
1042 Tuple_assignment_statement::do_dump_statement(
1043 Ast_dump_context
* ast_dump_context
) const
1045 ast_dump_context
->print_indent();
1046 ast_dump_context
->dump_expression_list(this->lhs_
);
1047 ast_dump_context
->ostream() << " = ";
1048 ast_dump_context
->dump_expression_list(this->rhs_
);
1049 ast_dump_context
->ostream() << std::endl
;
1052 // Make a tuple assignment statement.
1055 Statement::make_tuple_assignment(Expression_list
* lhs
, Expression_list
* rhs
,
1058 return new Tuple_assignment_statement(lhs
, rhs
, location
);
1061 // A tuple assignment from a map index expression.
1064 class Tuple_map_assignment_statement
: public Statement
1067 Tuple_map_assignment_statement(Expression
* val
, Expression
* present
,
1068 Expression
* map_index
,
1070 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT
, location
),
1071 val_(val
), present_(present
), map_index_(map_index
)
1076 do_traverse(Traverse
* traverse
);
1079 do_traverse_assignments(Traverse_assignments
*)
1080 { go_unreachable(); }
1083 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1086 do_get_backend(Translate_context
*)
1087 { go_unreachable(); }
1090 do_dump_statement(Ast_dump_context
*) const;
1093 // Lvalue which receives the value from the map.
1095 // Lvalue which receives whether the key value was present.
1096 Expression
* present_
;
1097 // The map index expression.
1098 Expression
* map_index_
;
1104 Tuple_map_assignment_statement::do_traverse(Traverse
* traverse
)
1106 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1107 || this->traverse_expression(traverse
, &this->present_
) == TRAVERSE_EXIT
)
1108 return TRAVERSE_EXIT
;
1109 return this->traverse_expression(traverse
, &this->map_index_
);
1112 // Lower a tuple map assignment.
1115 Tuple_map_assignment_statement::do_lower(Gogo
*, Named_object
*,
1116 Block
* enclosing
, Statement_inserter
*)
1118 Location loc
= this->location();
1120 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1121 if (map_index
== NULL
)
1123 this->report_error(_("expected map index on right hand side"));
1124 return Statement::make_error_statement(loc
);
1126 Map_type
* map_type
= map_index
->get_map_type();
1127 if (map_type
== NULL
)
1128 return Statement::make_error_statement(loc
);
1130 Block
* b
= new Block(enclosing
, loc
);
1132 // Move out any subexpressions to make sure that functions are
1133 // called in the required order.
1134 Move_ordered_evals
moe(b
);
1135 this->val_
->traverse_subexpressions(&moe
);
1136 this->present_
->traverse_subexpressions(&moe
);
1138 // Copy the key value into a temporary so that we can take its
1139 // address without pushing the value onto the heap.
1141 // var key_temp KEY_TYPE = MAP_INDEX
1142 Temporary_statement
* key_temp
=
1143 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1144 b
->add_statement(key_temp
);
1146 // var val_temp VAL_TYPE
1147 Temporary_statement
* val_temp
=
1148 Statement::make_temporary(map_type
->val_type(), NULL
, loc
);
1149 b
->add_statement(val_temp
);
1151 // var present_temp bool
1152 Temporary_statement
* present_temp
=
1153 Statement::make_temporary((this->present_
->type()->is_sink_type())
1154 ? Type::make_boolean_type()
1155 : this->present_
->type(),
1157 b
->add_statement(present_temp
);
1159 // present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp, &val_temp)
1160 Expression
* a1
= Expression::make_type_descriptor(map_type
, loc
);
1161 Expression
* a2
= map_index
->map();
1162 Temporary_reference_expression
* ref
=
1163 Expression::make_temporary_reference(key_temp
, loc
);
1164 Expression
* a3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1165 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1166 Expression
* a4
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1167 Expression
* call
= Runtime::make_call(Runtime::MAPACCESS2
, loc
, 4,
1169 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1170 ref
->set_is_lvalue();
1171 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1172 b
->add_statement(s
);
1175 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1176 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1177 b
->add_statement(s
);
1179 // present = present_temp
1180 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1181 s
= Statement::make_assignment(this->present_
, ref
, loc
);
1182 b
->add_statement(s
);
1184 return Statement::make_block_statement(b
, loc
);
1187 // Dump the AST representation for a tuple map assignment statement.
1190 Tuple_map_assignment_statement::do_dump_statement(
1191 Ast_dump_context
* ast_dump_context
) const
1193 ast_dump_context
->print_indent();
1194 ast_dump_context
->dump_expression(this->val_
);
1195 ast_dump_context
->ostream() << ", ";
1196 ast_dump_context
->dump_expression(this->present_
);
1197 ast_dump_context
->ostream() << " = ";
1198 ast_dump_context
->dump_expression(this->map_index_
);
1199 ast_dump_context
->ostream() << std::endl
;
1202 // Make a map assignment statement which returns a pair of values.
1205 Statement::make_tuple_map_assignment(Expression
* val
, Expression
* present
,
1206 Expression
* map_index
,
1209 return new Tuple_map_assignment_statement(val
, present
, map_index
, location
);
1212 // Assign a pair of entries to a map.
1215 class Map_assignment_statement
: public Statement
1218 Map_assignment_statement(Expression
* map_index
,
1219 Expression
* val
, Expression
* should_set
,
1221 : Statement(STATEMENT_MAP_ASSIGNMENT
, location
),
1222 map_index_(map_index
), val_(val
), should_set_(should_set
)
1227 do_traverse(Traverse
* traverse
);
1230 do_traverse_assignments(Traverse_assignments
*)
1231 { go_unreachable(); }
1234 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1237 do_get_backend(Translate_context
*)
1238 { go_unreachable(); }
1241 do_dump_statement(Ast_dump_context
*) const;
1244 // A reference to the map index which should be set or deleted.
1245 Expression
* map_index_
;
1246 // The value to add to the map.
1248 // Whether or not to add the value.
1249 Expression
* should_set_
;
1252 // Traverse a map assignment.
1255 Map_assignment_statement::do_traverse(Traverse
* traverse
)
1257 if (this->traverse_expression(traverse
, &this->map_index_
) == TRAVERSE_EXIT
1258 || this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
1259 return TRAVERSE_EXIT
;
1260 return this->traverse_expression(traverse
, &this->should_set_
);
1263 // Lower a map assignment to a function call.
1266 Map_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
1267 Statement_inserter
*)
1269 Location loc
= this->location();
1271 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1272 if (map_index
== NULL
)
1274 this->report_error(_("expected map index on left hand side"));
1275 return Statement::make_error_statement(loc
);
1277 Map_type
* map_type
= map_index
->get_map_type();
1278 if (map_type
== NULL
)
1279 return Statement::make_error_statement(loc
);
1281 Block
* b
= new Block(enclosing
, loc
);
1283 // Evaluate the map first to get order of evaluation right.
1284 // map_temp := m // we are evaluating m[k] = v, p
1285 Temporary_statement
* map_temp
= Statement::make_temporary(map_type
,
1288 b
->add_statement(map_temp
);
1290 // var key_temp MAP_KEY_TYPE = k
1291 Temporary_statement
* key_temp
=
1292 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1293 b
->add_statement(key_temp
);
1295 // var val_temp MAP_VAL_TYPE = v
1296 Temporary_statement
* val_temp
=
1297 Statement::make_temporary(map_type
->val_type(), this->val_
, loc
);
1298 b
->add_statement(val_temp
);
1300 // var insert_temp bool = p
1301 Temporary_statement
* insert_temp
=
1302 Statement::make_temporary(Type::lookup_bool_type(), this->should_set_
,
1304 b
->add_statement(insert_temp
);
1306 // mapassign2(map_temp, &key_temp, &val_temp, p)
1307 Expression
* p1
= Expression::make_temporary_reference(map_temp
, loc
);
1308 Expression
* ref
= Expression::make_temporary_reference(key_temp
, loc
);
1309 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1310 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1311 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1312 Expression
* p4
= Expression::make_temporary_reference(insert_temp
, loc
);
1313 Expression
* call
= Runtime::make_call(Runtime::MAPASSIGN2
, loc
, 4,
1315 Statement
* s
= Statement::make_statement(call
, true);
1316 b
->add_statement(s
);
1318 return Statement::make_block_statement(b
, loc
);
1321 // Dump the AST representation for a map assignment statement.
1324 Map_assignment_statement::do_dump_statement(
1325 Ast_dump_context
* ast_dump_context
) const
1327 ast_dump_context
->print_indent();
1328 ast_dump_context
->dump_expression(this->map_index_
);
1329 ast_dump_context
->ostream() << " = ";
1330 ast_dump_context
->dump_expression(this->val_
);
1331 ast_dump_context
->ostream() << ", ";
1332 ast_dump_context
->dump_expression(this->should_set_
);
1333 ast_dump_context
->ostream() << std::endl
;
1336 // Make a statement which assigns a pair of entries to a map.
1339 Statement::make_map_assignment(Expression
* map_index
,
1340 Expression
* val
, Expression
* should_set
,
1343 return new Map_assignment_statement(map_index
, val
, should_set
, location
);
1346 // A tuple assignment from a receive statement.
1348 class Tuple_receive_assignment_statement
: public Statement
1351 Tuple_receive_assignment_statement(Expression
* val
, Expression
* closed
,
1352 Expression
* channel
, Location location
)
1353 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
, location
),
1354 val_(val
), closed_(closed
), channel_(channel
)
1359 do_traverse(Traverse
* traverse
);
1362 do_traverse_assignments(Traverse_assignments
*)
1363 { go_unreachable(); }
1366 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1369 do_get_backend(Translate_context
*)
1370 { go_unreachable(); }
1373 do_dump_statement(Ast_dump_context
*) const;
1376 // Lvalue which receives the value from the channel.
1378 // Lvalue which receives whether the channel is closed.
1379 Expression
* closed_
;
1380 // The channel on which we receive the value.
1381 Expression
* channel_
;
1387 Tuple_receive_assignment_statement::do_traverse(Traverse
* traverse
)
1389 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1390 || this->traverse_expression(traverse
, &this->closed_
) == TRAVERSE_EXIT
)
1391 return TRAVERSE_EXIT
;
1392 return this->traverse_expression(traverse
, &this->channel_
);
1395 // Lower to a function call.
1398 Tuple_receive_assignment_statement::do_lower(Gogo
*, Named_object
*,
1400 Statement_inserter
*)
1402 Location loc
= this->location();
1404 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
1405 if (channel_type
== NULL
)
1407 this->report_error(_("expected channel"));
1408 return Statement::make_error_statement(loc
);
1410 if (!channel_type
->may_receive())
1412 this->report_error(_("invalid receive on send-only channel"));
1413 return Statement::make_error_statement(loc
);
1416 Block
* b
= new Block(enclosing
, loc
);
1418 // Make sure that any subexpressions on the left hand side are
1419 // evaluated in the right order.
1420 Move_ordered_evals
moe(b
);
1421 this->val_
->traverse_subexpressions(&moe
);
1422 this->closed_
->traverse_subexpressions(&moe
);
1424 // var val_temp ELEMENT_TYPE
1425 Temporary_statement
* val_temp
=
1426 Statement::make_temporary(channel_type
->element_type(), NULL
, loc
);
1427 b
->add_statement(val_temp
);
1429 // var closed_temp bool
1430 Temporary_statement
* closed_temp
=
1431 Statement::make_temporary((this->closed_
->type()->is_sink_type())
1432 ? Type::make_boolean_type()
1433 : this->closed_
->type(),
1435 b
->add_statement(closed_temp
);
1437 // closed_temp = chanrecv2(type, channel, &val_temp)
1438 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
1440 Temporary_reference_expression
* ref
=
1441 Expression::make_temporary_reference(val_temp
, loc
);
1442 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1443 Expression
* call
= Runtime::make_call(Runtime::CHANRECV2
,
1444 loc
, 3, td
, this->channel_
, p2
);
1445 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1446 ref
->set_is_lvalue();
1447 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1448 b
->add_statement(s
);
1451 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1452 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1453 b
->add_statement(s
);
1455 // closed = closed_temp
1456 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1457 s
= Statement::make_assignment(this->closed_
, ref
, loc
);
1458 b
->add_statement(s
);
1460 return Statement::make_block_statement(b
, loc
);
1463 // Dump the AST representation for a tuple receive statement.
1466 Tuple_receive_assignment_statement::do_dump_statement(
1467 Ast_dump_context
* ast_dump_context
) const
1469 ast_dump_context
->print_indent();
1470 ast_dump_context
->dump_expression(this->val_
);
1471 ast_dump_context
->ostream() << ", ";
1472 ast_dump_context
->dump_expression(this->closed_
);
1473 ast_dump_context
->ostream() << " <- ";
1474 ast_dump_context
->dump_expression(this->channel_
);
1475 ast_dump_context
->ostream() << std::endl
;
1478 // Make a nonblocking receive statement.
1481 Statement::make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
1482 Expression
* channel
,
1485 return new Tuple_receive_assignment_statement(val
, closed
, channel
,
1489 // An assignment to a pair of values from a type guard. This is a
1490 // conditional type guard. v, ok = i.(type).
1492 class Tuple_type_guard_assignment_statement
: public Statement
1495 Tuple_type_guard_assignment_statement(Expression
* val
, Expression
* ok
,
1496 Expression
* expr
, Type
* type
,
1498 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
, location
),
1499 val_(val
), ok_(ok
), expr_(expr
), type_(type
)
1504 do_traverse(Traverse
*);
1507 do_traverse_assignments(Traverse_assignments
*)
1508 { go_unreachable(); }
1511 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1514 do_get_backend(Translate_context
*)
1515 { go_unreachable(); }
1518 do_dump_statement(Ast_dump_context
*) const;
1522 lower_to_type(Runtime::Function
);
1525 lower_to_object_type(Block
*, Runtime::Function
);
1527 // The variable which recieves the converted value.
1529 // The variable which receives the indication of success.
1531 // The expression being converted.
1533 // The type to which the expression is being converted.
1537 // Traverse a type guard tuple assignment.
1540 Tuple_type_guard_assignment_statement::do_traverse(Traverse
* traverse
)
1542 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1543 || this->traverse_expression(traverse
, &this->ok_
) == TRAVERSE_EXIT
1544 || this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
1545 return TRAVERSE_EXIT
;
1546 return this->traverse_expression(traverse
, &this->expr_
);
1549 // Lower to a function call.
1552 Tuple_type_guard_assignment_statement::do_lower(Gogo
*, Named_object
*,
1554 Statement_inserter
*)
1556 Location loc
= this->location();
1558 Type
* expr_type
= this->expr_
->type();
1559 if (expr_type
->interface_type() == NULL
)
1561 if (!expr_type
->is_error() && !this->type_
->is_error())
1562 this->report_error(_("type assertion only valid for interface types"));
1563 return Statement::make_error_statement(loc
);
1566 Block
* b
= new Block(enclosing
, loc
);
1568 // Make sure that any subexpressions on the left hand side are
1569 // evaluated in the right order.
1570 Move_ordered_evals
moe(b
);
1571 this->val_
->traverse_subexpressions(&moe
);
1572 this->ok_
->traverse_subexpressions(&moe
);
1574 bool expr_is_empty
= expr_type
->interface_type()->is_empty();
1575 Call_expression
* call
;
1576 if (this->type_
->interface_type() != NULL
)
1578 if (this->type_
->interface_type()->is_empty())
1579 call
= Runtime::make_call((expr_is_empty
1580 ? Runtime::IFACEE2E2
1581 : Runtime::IFACEI2E2
),
1582 loc
, 1, this->expr_
);
1584 call
= this->lower_to_type(expr_is_empty
1585 ? Runtime::IFACEE2I2
1586 : Runtime::IFACEI2I2
);
1588 else if (this->type_
->points_to() != NULL
)
1589 call
= this->lower_to_type(expr_is_empty
1590 ? Runtime::IFACEE2T2P
1591 : Runtime::IFACEI2T2P
);
1594 this->lower_to_object_type(b
,
1596 ? Runtime::IFACEE2T2
1597 : Runtime::IFACEI2T2
));
1603 Expression
* res
= Expression::make_call_result(call
, 0);
1604 res
= Expression::make_unsafe_cast(this->type_
, res
, loc
);
1605 Statement
* s
= Statement::make_assignment(this->val_
, res
, loc
);
1606 b
->add_statement(s
);
1608 res
= Expression::make_call_result(call
, 1);
1609 s
= Statement::make_assignment(this->ok_
, res
, loc
);
1610 b
->add_statement(s
);
1613 return Statement::make_block_statement(b
, loc
);
1616 // Lower a conversion to a non-empty interface type or a pointer type.
1619 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code
)
1621 Location loc
= this->location();
1622 return Runtime::make_call(code
, loc
, 2,
1623 Expression::make_type_descriptor(this->type_
, loc
),
1627 // Lower a conversion to a non-interface non-pointer type.
1630 Tuple_type_guard_assignment_statement::lower_to_object_type(
1632 Runtime::Function code
)
1634 Location loc
= this->location();
1636 // var val_temp TYPE
1637 Temporary_statement
* val_temp
= Statement::make_temporary(this->type_
,
1639 b
->add_statement(val_temp
);
1641 // ok = CODE(type_descriptor, expr, &val_temp)
1642 Expression
* p1
= Expression::make_type_descriptor(this->type_
, loc
);
1643 Expression
* ref
= Expression::make_temporary_reference(val_temp
, loc
);
1644 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1645 Expression
* call
= Runtime::make_call(code
, loc
, 3, p1
, this->expr_
, p3
);
1646 Statement
* s
= Statement::make_assignment(this->ok_
, call
, loc
);
1647 b
->add_statement(s
);
1650 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1651 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1652 b
->add_statement(s
);
1655 // Dump the AST representation for a tuple type guard statement.
1658 Tuple_type_guard_assignment_statement::do_dump_statement(
1659 Ast_dump_context
* ast_dump_context
) const
1661 ast_dump_context
->print_indent();
1662 ast_dump_context
->dump_expression(this->val_
);
1663 ast_dump_context
->ostream() << ", ";
1664 ast_dump_context
->dump_expression(this->ok_
);
1665 ast_dump_context
->ostream() << " = ";
1666 ast_dump_context
->dump_expression(this->expr_
);
1667 ast_dump_context
->ostream() << " . ";
1668 ast_dump_context
->dump_type(this->type_
);
1669 ast_dump_context
->ostream() << std::endl
;
1672 // Make an assignment from a type guard to a pair of variables.
1675 Statement::make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
1676 Expression
* expr
, Type
* type
,
1679 return new Tuple_type_guard_assignment_statement(val
, ok
, expr
, type
,
1683 // Class Expression_statement.
1687 Expression_statement::Expression_statement(Expression
* expr
, bool is_ignored
)
1688 : Statement(STATEMENT_EXPRESSION
, expr
->location()),
1689 expr_(expr
), is_ignored_(is_ignored
)
1696 Expression_statement::do_determine_types()
1698 this->expr_
->determine_type_no_context();
1701 // Check the types of an expression statement. The only check we do
1702 // is to possibly give an error about discarding the value of the
1706 Expression_statement::do_check_types(Gogo
*)
1708 if (!this->is_ignored_
)
1709 this->expr_
->discarding_value();
1712 // An expression statement is only a terminating statement if it is
1716 Expression_statement::do_may_fall_through() const
1718 const Call_expression
* call
= this->expr_
->call_expression();
1721 const Expression
* fn
= call
->fn();
1722 // panic is still an unknown named object.
1723 const Unknown_expression
* ue
= fn
->unknown_expression();
1726 Named_object
* no
= ue
->named_object();
1728 if (no
->is_unknown())
1729 no
= no
->unknown_value()->real_named_object();
1732 Function_type
* fntype
;
1733 if (no
->is_function())
1734 fntype
= no
->func_value()->type();
1735 else if (no
->is_function_declaration())
1736 fntype
= no
->func_declaration_value()->type();
1740 // The builtin function panic does not return.
1741 if (fntype
!= NULL
&& fntype
->is_builtin() && no
->name() == "panic")
1749 // Convert to backend representation.
1752 Expression_statement::do_get_backend(Translate_context
* context
)
1754 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
1755 return context
->backend()->expression_statement(bexpr
);
1758 // Dump the AST representation for an expression statement
1761 Expression_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
1764 ast_dump_context
->print_indent();
1765 ast_dump_context
->dump_expression(expr_
);
1766 ast_dump_context
->ostream() << std::endl
;
1769 // Make an expression statement from an Expression.
1772 Statement::make_statement(Expression
* expr
, bool is_ignored
)
1774 return new Expression_statement(expr
, is_ignored
);
1777 // A block statement--a list of statements which may include variable
1780 class Block_statement
: public Statement
1783 Block_statement(Block
* block
, Location location
)
1784 : Statement(STATEMENT_BLOCK
, location
),
1790 do_traverse(Traverse
* traverse
)
1791 { return this->block_
->traverse(traverse
); }
1794 do_determine_types()
1795 { this->block_
->determine_types(); }
1798 do_may_fall_through() const
1799 { return this->block_
->may_fall_through(); }
1802 do_get_backend(Translate_context
* context
);
1805 do_dump_statement(Ast_dump_context
*) const;
1811 // Convert a block to the backend representation of a statement.
1814 Block_statement::do_get_backend(Translate_context
* context
)
1816 Bblock
* bblock
= this->block_
->get_backend(context
);
1817 return context
->backend()->block_statement(bblock
);
1820 // Dump the AST for a block statement
1823 Block_statement::do_dump_statement(Ast_dump_context
*) const
1825 // block statement braces are dumped when traversing.
1828 // Make a block statement.
1831 Statement::make_block_statement(Block
* block
, Location location
)
1833 return new Block_statement(block
, location
);
1836 // An increment or decrement statement.
1838 class Inc_dec_statement
: public Statement
1841 Inc_dec_statement(bool is_inc
, Expression
* expr
)
1842 : Statement(STATEMENT_INCDEC
, expr
->location()),
1843 expr_(expr
), is_inc_(is_inc
)
1848 do_traverse(Traverse
* traverse
)
1849 { return this->traverse_expression(traverse
, &this->expr_
); }
1852 do_traverse_assignments(Traverse_assignments
*)
1853 { go_unreachable(); }
1856 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1859 do_get_backend(Translate_context
*)
1860 { go_unreachable(); }
1863 do_dump_statement(Ast_dump_context
*) const;
1866 // The l-value to increment or decrement.
1868 // Whether to increment or decrement.
1872 // Lower to += or -=.
1875 Inc_dec_statement::do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
1877 Location loc
= this->location();
1878 Expression
* oexpr
= Expression::make_integer_ul(1, this->expr_
->type(), loc
);
1879 Operator op
= this->is_inc_
? OPERATOR_PLUSEQ
: OPERATOR_MINUSEQ
;
1880 return Statement::make_assignment_operation(op
, this->expr_
, oexpr
, loc
);
1883 // Dump the AST representation for a inc/dec statement.
1886 Inc_dec_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
1888 ast_dump_context
->print_indent();
1889 ast_dump_context
->dump_expression(expr_
);
1890 ast_dump_context
->ostream() << (is_inc_
? "++": "--") << std::endl
;
1893 // Make an increment statement.
1896 Statement::make_inc_statement(Expression
* expr
)
1898 return new Inc_dec_statement(true, expr
);
1901 // Make a decrement statement.
1904 Statement::make_dec_statement(Expression
* expr
)
1906 return new Inc_dec_statement(false, expr
);
1909 // Class Thunk_statement. This is the base class for go and defer
1912 Unordered_set(const Struct_type
*) Thunk_statement::thunk_types
;
1916 Thunk_statement::Thunk_statement(Statement_classification classification
,
1917 Call_expression
* call
,
1919 : Statement(classification
, location
),
1920 call_(call
), struct_type_(NULL
)
1924 // Return whether this is a simple statement which does not require a
1928 Thunk_statement::is_simple(Function_type
* fntype
) const
1930 // We need a thunk to call a method, or to pass a variable number of
1932 if (fntype
->is_method() || fntype
->is_varargs())
1935 // A defer statement requires a thunk to set up for whether the
1936 // function can call recover.
1937 if (this->classification() == STATEMENT_DEFER
)
1940 // We can only permit a single parameter of pointer type.
1941 const Typed_identifier_list
* parameters
= fntype
->parameters();
1942 if (parameters
!= NULL
1943 && (parameters
->size() > 1
1944 || (parameters
->size() == 1
1945 && parameters
->begin()->type()->points_to() == NULL
)))
1948 // If the function returns multiple values, or returns a type other
1949 // than integer, floating point, or pointer, then it may get a
1950 // hidden first parameter, in which case we need the more
1951 // complicated approach. This is true even though we are going to
1952 // ignore the return value.
1953 const Typed_identifier_list
* results
= fntype
->results();
1955 && (results
->size() > 1
1956 || (results
->size() == 1
1957 && !results
->begin()->type()->is_basic_type()
1958 && results
->begin()->type()->points_to() == NULL
)))
1961 // If this calls something that is not a simple function, then we
1963 Expression
* fn
= this->call_
->call_expression()->fn();
1964 if (fn
->func_expression() == NULL
)
1967 // If the function uses a closure, then we need a thunk. FIXME: We
1968 // could accept a zero argument function with a closure.
1969 if (fn
->func_expression()->closure() != NULL
)
1975 // Traverse a thunk statement.
1978 Thunk_statement::do_traverse(Traverse
* traverse
)
1980 return this->traverse_expression(traverse
, &this->call_
);
1983 // We implement traverse_assignment for a thunk statement because it
1984 // effectively copies the function call.
1987 Thunk_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
1989 Expression
* fn
= this->call_
->call_expression()->fn();
1990 Expression
* fn2
= fn
;
1991 tassign
->value(&fn2
, true, false);
1995 // Determine types in a thunk statement.
1998 Thunk_statement::do_determine_types()
2000 this->call_
->determine_type_no_context();
2002 // Now that we know the types of the call, build the struct used to
2004 Call_expression
* ce
= this->call_
->call_expression();
2007 Function_type
* fntype
= ce
->get_function_type();
2008 if (fntype
!= NULL
&& !this->is_simple(fntype
))
2009 this->struct_type_
= this->build_struct(fntype
);
2012 // Check types in a thunk statement.
2015 Thunk_statement::do_check_types(Gogo
*)
2017 if (!this->call_
->discarding_value())
2019 Call_expression
* ce
= this->call_
->call_expression();
2022 if (!this->call_
->is_error_expression())
2023 this->report_error("expected call expression");
2028 // The Traverse class used to find and simplify thunk statements.
2030 class Simplify_thunk_traverse
: public Traverse
2033 Simplify_thunk_traverse(Gogo
* gogo
)
2034 : Traverse(traverse_functions
| traverse_blocks
),
2035 gogo_(gogo
), function_(NULL
)
2039 function(Named_object
*);
2047 // The function we are traversing.
2048 Named_object
* function_
;
2051 // Keep track of the current function while looking for thunks.
2054 Simplify_thunk_traverse::function(Named_object
* no
)
2056 go_assert(this->function_
== NULL
);
2057 this->function_
= no
;
2058 int t
= no
->func_value()->traverse(this);
2059 this->function_
= NULL
;
2060 if (t
== TRAVERSE_EXIT
)
2062 return TRAVERSE_SKIP_COMPONENTS
;
2065 // Look for thunks in a block.
2068 Simplify_thunk_traverse::block(Block
* b
)
2070 // The parser ensures that thunk statements always appear at the end
2072 if (b
->statements()->size() < 1)
2073 return TRAVERSE_CONTINUE
;
2074 Thunk_statement
* stat
= b
->statements()->back()->thunk_statement();
2076 return TRAVERSE_CONTINUE
;
2077 if (stat
->simplify_statement(this->gogo_
, this->function_
, b
))
2078 return TRAVERSE_SKIP_COMPONENTS
;
2079 return TRAVERSE_CONTINUE
;
2082 // Simplify all thunk statements.
2085 Gogo::simplify_thunk_statements()
2087 Simplify_thunk_traverse
thunk_traverse(this);
2088 this->traverse(&thunk_traverse
);
2091 // Return true if the thunk function is a constant, which means that
2092 // it does not need to be passed to the thunk routine.
2095 Thunk_statement::is_constant_function() const
2097 Call_expression
* ce
= this->call_
->call_expression();
2098 Function_type
* fntype
= ce
->get_function_type();
2101 go_assert(saw_errors());
2104 if (fntype
->is_builtin())
2106 Expression
* fn
= ce
->fn();
2107 if (fn
->func_expression() != NULL
)
2108 return fn
->func_expression()->closure() == NULL
;
2109 if (fn
->interface_field_reference_expression() != NULL
)
2114 // Simplify complex thunk statements into simple ones. A complicated
2115 // thunk statement is one which takes anything other than zero
2116 // parameters or a single pointer parameter. We rewrite it into code
2117 // which allocates a struct, stores the parameter values into the
2118 // struct, and does a simple go or defer statement which passes the
2119 // struct to a thunk. The thunk does the real call.
2122 Thunk_statement::simplify_statement(Gogo
* gogo
, Named_object
* function
,
2125 if (this->classification() == STATEMENT_ERROR
)
2127 if (this->call_
->is_error_expression())
2130 if (this->classification() == STATEMENT_DEFER
)
2132 // Make sure that the defer stack exists for the function. We
2133 // will use when converting this statement to the backend
2134 // representation, but we want it to exist when we start
2135 // converting the function.
2136 function
->func_value()->defer_stack(this->location());
2139 Call_expression
* ce
= this->call_
->call_expression();
2140 Function_type
* fntype
= ce
->get_function_type();
2143 go_assert(saw_errors());
2144 this->set_is_error();
2147 if (this->is_simple(fntype
))
2150 Expression
* fn
= ce
->fn();
2151 Interface_field_reference_expression
* interface_method
=
2152 fn
->interface_field_reference_expression();
2154 Location location
= this->location();
2156 std::string thunk_name
= Gogo::thunk_name();
2159 this->build_thunk(gogo
, thunk_name
);
2161 // Generate code to call the thunk.
2163 // Get the values to store into the struct which is the single
2164 // argument to the thunk.
2166 Expression_list
* vals
= new Expression_list();
2167 if (!this->is_constant_function())
2168 vals
->push_back(fn
);
2170 if (interface_method
!= NULL
)
2171 vals
->push_back(interface_method
->expr());
2173 if (ce
->args() != NULL
)
2175 for (Expression_list::const_iterator p
= ce
->args()->begin();
2176 p
!= ce
->args()->end();
2179 if ((*p
)->is_constant())
2181 vals
->push_back(*p
);
2185 // Build the struct.
2186 Expression
* constructor
=
2187 Expression::make_struct_composite_literal(this->struct_type_
, vals
,
2190 // Allocate the initialized struct on the heap.
2191 constructor
= Expression::make_heap_expression(constructor
, location
);
2193 // Look up the thunk.
2194 Named_object
* named_thunk
= gogo
->lookup(thunk_name
, NULL
);
2195 go_assert(named_thunk
!= NULL
&& named_thunk
->is_function());
2198 Expression
* func
= Expression::make_func_reference(named_thunk
, NULL
,
2200 Expression_list
* params
= new Expression_list();
2201 params
->push_back(constructor
);
2202 Call_expression
* call
= Expression::make_call(func
, params
, false, location
);
2204 // Build the simple go or defer statement.
2206 if (this->classification() == STATEMENT_GO
)
2207 s
= Statement::make_go_statement(call
, location
);
2208 else if (this->classification() == STATEMENT_DEFER
)
2209 s
= Statement::make_defer_statement(call
, location
);
2213 // The current block should end with the go statement.
2214 go_assert(block
->statements()->size() >= 1);
2215 go_assert(block
->statements()->back() == this);
2216 block
->replace_statement(block
->statements()->size() - 1, s
);
2218 // We already ran the determine_types pass, so we need to run it now
2219 // for the new statement.
2220 s
->determine_types();
2223 gogo
->check_types_in_block(block
);
2225 // Return true to tell the block not to keep looking at statements.
2229 // Set the name to use for thunk parameter N.
2232 Thunk_statement::thunk_field_param(int n
, char* buf
, size_t buflen
)
2234 snprintf(buf
, buflen
, "a%d", n
);
2237 // Build a new struct type to hold the parameters for a complicated
2238 // thunk statement. FNTYPE is the type of the function call.
2241 Thunk_statement::build_struct(Function_type
* fntype
)
2243 Location location
= this->location();
2245 Struct_field_list
* fields
= new Struct_field_list();
2247 Call_expression
* ce
= this->call_
->call_expression();
2248 Expression
* fn
= ce
->fn();
2250 if (!this->is_constant_function())
2252 // The function to call.
2253 fields
->push_back(Struct_field(Typed_identifier("fn", fntype
,
2257 // If this thunk statement calls a method on an interface, we pass
2258 // the interface object to the thunk.
2259 Interface_field_reference_expression
* interface_method
=
2260 fn
->interface_field_reference_expression();
2261 if (interface_method
!= NULL
)
2263 Typed_identifier
tid("object", interface_method
->expr()->type(),
2265 fields
->push_back(Struct_field(tid
));
2268 // The predeclared recover function has no argument. However, we
2269 // add an argument when building recover thunks. Handle that here.
2270 if (ce
->is_recover_call())
2272 fields
->push_back(Struct_field(Typed_identifier("can_recover",
2273 Type::lookup_bool_type(),
2277 const Expression_list
* args
= ce
->args();
2281 for (Expression_list::const_iterator p
= args
->begin();
2285 if ((*p
)->is_constant())
2289 this->thunk_field_param(i
, buf
, sizeof buf
);
2290 fields
->push_back(Struct_field(Typed_identifier(buf
, (*p
)->type(),
2295 Struct_type
*st
= Type::make_struct_type(fields
, location
);
2297 Thunk_statement::thunk_types
.insert(st
);
2302 // Return whether ST is a type created to hold thunk parameters.
2305 Thunk_statement::is_thunk_struct(const Struct_type
* st
)
2307 return (Thunk_statement::thunk_types
.find(st
)
2308 != Thunk_statement::thunk_types
.end());
2311 // Build the thunk we are going to call. This is a brand new, albeit
2312 // artificial, function.
2315 Thunk_statement::build_thunk(Gogo
* gogo
, const std::string
& thunk_name
)
2317 Location location
= this->location();
2319 Call_expression
* ce
= this->call_
->call_expression();
2321 bool may_call_recover
= false;
2322 if (this->classification() == STATEMENT_DEFER
)
2324 Func_expression
* fn
= ce
->fn()->func_expression();
2326 may_call_recover
= true;
2329 const Named_object
* no
= fn
->named_object();
2330 if (!no
->is_function())
2331 may_call_recover
= true;
2333 may_call_recover
= no
->func_value()->calls_recover();
2337 // Build the type of the thunk. The thunk takes a single parameter,
2338 // which is a pointer to the special structure we build.
2339 const char* const parameter_name
= "__go_thunk_parameter";
2340 Typed_identifier_list
* thunk_parameters
= new Typed_identifier_list();
2341 Type
* pointer_to_struct_type
= Type::make_pointer_type(this->struct_type_
);
2342 thunk_parameters
->push_back(Typed_identifier(parameter_name
,
2343 pointer_to_struct_type
,
2346 Typed_identifier_list
* thunk_results
= NULL
;
2347 if (may_call_recover
)
2349 // When deferring a function which may call recover, add a
2350 // return value, to disable tail call optimizations which will
2351 // break the way we check whether recover is permitted.
2352 thunk_results
= new Typed_identifier_list();
2353 thunk_results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
2357 Function_type
* thunk_type
= Type::make_function_type(NULL
, thunk_parameters
,
2361 // Start building the thunk.
2362 Named_object
* function
= gogo
->start_function(thunk_name
, thunk_type
, true,
2365 gogo
->start_block(location
);
2367 // For a defer statement, start with a call to
2368 // __go_set_defer_retaddr. */
2369 Label
* retaddr_label
= NULL
;
2370 if (may_call_recover
)
2372 retaddr_label
= gogo
->add_label_reference("retaddr", location
, false);
2373 Expression
* arg
= Expression::make_label_addr(retaddr_label
, location
);
2374 Expression
* call
= Runtime::make_call(Runtime::SET_DEFER_RETADDR
,
2377 // This is a hack to prevent the middle-end from deleting the
2379 gogo
->start_block(location
);
2380 gogo
->add_statement(Statement::make_goto_statement(retaddr_label
,
2382 Block
* then_block
= gogo
->finish_block(location
);
2383 then_block
->determine_types();
2385 Statement
* s
= Statement::make_if_statement(call
, then_block
, NULL
,
2387 s
->determine_types();
2388 gogo
->add_statement(s
);
2390 function
->func_value()->set_calls_defer_retaddr();
2393 // Get a reference to the parameter.
2394 Named_object
* named_parameter
= gogo
->lookup(parameter_name
, NULL
);
2395 go_assert(named_parameter
!= NULL
&& named_parameter
->is_variable());
2397 // Build the call. Note that the field names are the same as the
2398 // ones used in build_struct.
2399 Expression
* thunk_parameter
= Expression::make_var_reference(named_parameter
,
2401 thunk_parameter
= Expression::make_unary(OPERATOR_MULT
, thunk_parameter
,
2404 Interface_field_reference_expression
* interface_method
=
2405 ce
->fn()->interface_field_reference_expression();
2407 Expression
* func_to_call
;
2408 unsigned int next_index
;
2409 if (this->is_constant_function())
2411 func_to_call
= ce
->fn();
2416 func_to_call
= Expression::make_field_reference(thunk_parameter
,
2421 if (interface_method
!= NULL
)
2423 // The main program passes the interface object.
2424 go_assert(next_index
== 0);
2425 Expression
* r
= Expression::make_field_reference(thunk_parameter
, 0,
2427 const std::string
& name(interface_method
->name());
2428 func_to_call
= Expression::make_interface_field_reference(r
, name
,
2433 Expression_list
* call_params
= new Expression_list();
2434 const Struct_field_list
* fields
= this->struct_type_
->fields();
2435 Struct_field_list::const_iterator p
= fields
->begin();
2436 for (unsigned int i
= 0; i
< next_index
; ++i
)
2438 bool is_recover_call
= ce
->is_recover_call();
2439 Expression
* recover_arg
= NULL
;
2441 const Expression_list
* args
= ce
->args();
2444 for (Expression_list::const_iterator arg
= args
->begin();
2449 if ((*arg
)->is_constant())
2453 Expression
* thunk_param
=
2454 Expression::make_var_reference(named_parameter
, location
);
2456 Expression::make_unary(OPERATOR_MULT
, thunk_param
, location
);
2457 param
= Expression::make_field_reference(thunk_param
,
2463 if (!is_recover_call
)
2464 call_params
->push_back(param
);
2467 go_assert(call_params
->empty());
2468 recover_arg
= param
;
2473 if (call_params
->empty())
2479 Call_expression
* call
= Expression::make_call(func_to_call
, call_params
,
2482 // This call expression was already lowered before entering the
2483 // thunk statement. Don't try to lower varargs again, as that will
2484 // cause confusion for, e.g., method calls which already have a
2485 // receiver parameter.
2486 call
->set_varargs_are_lowered();
2488 Statement
* call_statement
= Statement::make_statement(call
, true);
2490 gogo
->add_statement(call_statement
);
2492 // If this is a defer statement, the label comes immediately after
2494 if (may_call_recover
)
2496 gogo
->add_label_definition("retaddr", location
);
2498 Expression_list
* vals
= new Expression_list();
2499 vals
->push_back(Expression::make_boolean(false, location
));
2500 gogo
->add_statement(Statement::make_return_statement(vals
, location
));
2503 Block
* b
= gogo
->finish_block(location
);
2505 gogo
->add_block(b
, location
);
2507 gogo
->lower_block(function
, b
);
2509 // We already ran the determine_types pass, so we need to run it
2510 // just for the call statement now. The other types are known.
2511 call_statement
->determine_types();
2513 gogo
->flatten_block(function
, b
);
2515 if (may_call_recover
|| recover_arg
!= NULL
)
2517 // Dig up the call expression, which may have been changed
2519 go_assert(call_statement
->classification() == STATEMENT_EXPRESSION
);
2520 Expression_statement
* es
=
2521 static_cast<Expression_statement
*>(call_statement
);
2522 Call_expression
* ce
= es
->expr()->call_expression();
2524 go_assert(saw_errors());
2527 if (may_call_recover
)
2528 ce
->set_is_deferred();
2529 if (recover_arg
!= NULL
)
2530 ce
->set_recover_arg(recover_arg
);
2534 // That is all the thunk has to do.
2535 gogo
->finish_function(location
);
2538 // Get the function and argument expressions.
2541 Thunk_statement::get_fn_and_arg(Expression
** pfn
, Expression
** parg
)
2543 if (this->call_
->is_error_expression())
2546 Call_expression
* ce
= this->call_
->call_expression();
2548 Expression
* fn
= ce
->fn();
2549 Func_expression
* fe
= fn
->func_expression();
2550 go_assert(fe
!= NULL
);
2551 *pfn
= Expression::make_func_code_reference(fe
->named_object(),
2554 const Expression_list
* args
= ce
->args();
2555 if (args
== NULL
|| args
->empty())
2556 *parg
= Expression::make_nil(this->location());
2559 go_assert(args
->size() == 1);
2560 *parg
= args
->front();
2566 // Class Go_statement.
2569 Go_statement::do_get_backend(Translate_context
* context
)
2573 if (!this->get_fn_and_arg(&fn
, &arg
))
2574 return context
->backend()->error_statement();
2576 Expression
* call
= Runtime::make_call(Runtime::GO
, this->location(), 2,
2578 Bexpression
* bcall
= call
->get_backend(context
);
2579 return context
->backend()->expression_statement(bcall
);
2582 // Dump the AST representation for go statement.
2585 Go_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2587 ast_dump_context
->print_indent();
2588 ast_dump_context
->ostream() << "go ";
2589 ast_dump_context
->dump_expression(this->call());
2590 ast_dump_context
->ostream() << std::endl
;
2593 // Make a go statement.
2596 Statement::make_go_statement(Call_expression
* call
, Location location
)
2598 return new Go_statement(call
, location
);
2601 // Class Defer_statement.
2604 Defer_statement::do_get_backend(Translate_context
* context
)
2608 if (!this->get_fn_and_arg(&fn
, &arg
))
2609 return context
->backend()->error_statement();
2611 Location loc
= this->location();
2612 Expression
* ds
= context
->function()->func_value()->defer_stack(loc
);
2614 Expression
* call
= Runtime::make_call(Runtime::DEFER
, loc
, 3,
2616 Bexpression
* bcall
= call
->get_backend(context
);
2617 return context
->backend()->expression_statement(bcall
);
2620 // Dump the AST representation for defer statement.
2623 Defer_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2625 ast_dump_context
->print_indent();
2626 ast_dump_context
->ostream() << "defer ";
2627 ast_dump_context
->dump_expression(this->call());
2628 ast_dump_context
->ostream() << std::endl
;
2631 // Make a defer statement.
2634 Statement::make_defer_statement(Call_expression
* call
,
2637 return new Defer_statement(call
, location
);
2640 // Class Return_statement.
2642 // Traverse assignments. We treat each return value as a top level
2643 // RHS in an expression.
2646 Return_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
2648 Expression_list
* vals
= this->vals_
;
2651 for (Expression_list::iterator p
= vals
->begin();
2654 tassign
->value(&*p
, true, true);
2659 // Lower a return statement. If we are returning a function call
2660 // which returns multiple values which match the current function,
2661 // split up the call's results. If the return statement lists
2662 // explicit values, implement this statement by assigning the values
2663 // to the result variables and change this statement to a naked
2664 // return. This lets panic/recover work correctly.
2667 Return_statement::do_lower(Gogo
*, Named_object
* function
, Block
* enclosing
,
2668 Statement_inserter
*)
2670 if (this->is_lowered_
)
2673 Expression_list
* vals
= this->vals_
;
2675 this->is_lowered_
= true;
2677 Location loc
= this->location();
2679 size_t vals_count
= vals
== NULL
? 0 : vals
->size();
2680 Function::Results
* results
= function
->func_value()->result_variables();
2681 size_t results_count
= results
== NULL
? 0 : results
->size();
2683 if (vals_count
== 0)
2685 if (results_count
> 0 && !function
->func_value()->results_are_named())
2687 this->report_error(_("not enough arguments to return"));
2693 if (results_count
== 0)
2695 this->report_error(_("return with value in function "
2696 "with no return type"));
2700 // If the current function has multiple return values, and we are
2701 // returning a single call expression, split up the call expression.
2702 if (results_count
> 1
2703 && vals
->size() == 1
2704 && vals
->front()->call_expression() != NULL
)
2706 Call_expression
* call
= vals
->front()->call_expression();
2707 call
->set_expected_result_count(results_count
);
2709 vals
= new Expression_list
;
2710 for (size_t i
= 0; i
< results_count
; ++i
)
2711 vals
->push_back(Expression::make_call_result(call
, i
));
2712 vals_count
= results_count
;
2715 if (vals_count
< results_count
)
2717 this->report_error(_("not enough arguments to return"));
2721 if (vals_count
> results_count
)
2723 this->report_error(_("too many values in return statement"));
2727 Block
* b
= new Block(enclosing
, loc
);
2729 Expression_list
* lhs
= new Expression_list();
2730 Expression_list
* rhs
= new Expression_list();
2732 Expression_list::const_iterator pe
= vals
->begin();
2734 for (Function::Results::const_iterator pr
= results
->begin();
2735 pr
!= results
->end();
2738 Named_object
* rv
= *pr
;
2739 Expression
* e
= *pe
;
2741 // Check types now so that we give a good error message. The
2742 // result type is known. We determine the expression type
2745 Type
*rvtype
= rv
->result_var_value()->type();
2746 Type_context
type_context(rvtype
, false);
2747 e
->determine_type(&type_context
);
2750 if (Type::are_assignable(rvtype
, e
->type(), &reason
))
2752 Expression
* ve
= Expression::make_var_reference(rv
, e
->location());
2759 error_at(e
->location(), "incompatible type for return value %d", i
);
2761 error_at(e
->location(),
2762 "incompatible type for return value %d (%s)",
2766 go_assert(lhs
->size() == rhs
->size());
2770 else if (lhs
->size() == 1)
2772 b
->add_statement(Statement::make_assignment(lhs
->front(), rhs
->front(),
2778 b
->add_statement(Statement::make_tuple_assignment(lhs
, rhs
, loc
));
2780 b
->add_statement(this);
2784 return Statement::make_block_statement(b
, loc
);
2787 // Convert a return statement to the backend representation.
2790 Return_statement::do_get_backend(Translate_context
* context
)
2792 Location loc
= this->location();
2794 Function
* function
= context
->function()->func_value();
2795 Function::Results
* results
= function
->result_variables();
2796 std::vector
<Bexpression
*> retvals
;
2797 if (results
!= NULL
&& !results
->empty())
2799 retvals
.reserve(results
->size());
2800 for (Function::Results::const_iterator p
= results
->begin();
2801 p
!= results
->end();
2804 Expression
* vr
= Expression::make_var_reference(*p
, loc
);
2805 retvals
.push_back(vr
->get_backend(context
));
2809 return context
->backend()->return_statement(function
->get_decl(),
2813 // Dump the AST representation for a return statement.
2816 Return_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2818 ast_dump_context
->print_indent();
2819 ast_dump_context
->ostream() << "return " ;
2820 ast_dump_context
->dump_expression_list(this->vals_
);
2821 ast_dump_context
->ostream() << std::endl
;
2824 // Make a return statement.
2827 Statement::make_return_statement(Expression_list
* vals
,
2830 return new Return_statement(vals
, location
);
2833 // Make a statement that returns the result of a call expression.
2836 Statement::make_return_from_call(Call_expression
* call
, Location location
)
2838 size_t rc
= call
->result_count();
2840 return Statement::make_statement(call
, true);
2843 Expression_list
* vals
= new Expression_list();
2845 vals
->push_back(call
);
2848 for (size_t i
= 0; i
< rc
; ++i
)
2849 vals
->push_back(Expression::make_call_result(call
, i
));
2851 return Statement::make_return_statement(vals
, location
);
2855 // A break or continue statement.
2857 class Bc_statement
: public Statement
2860 Bc_statement(bool is_break
, Unnamed_label
* label
, Location location
)
2861 : Statement(STATEMENT_BREAK_OR_CONTINUE
, location
),
2862 label_(label
), is_break_(is_break
)
2867 { return this->is_break_
; }
2871 do_traverse(Traverse
*)
2872 { return TRAVERSE_CONTINUE
; }
2875 do_may_fall_through() const
2879 do_get_backend(Translate_context
* context
)
2880 { return this->label_
->get_goto(context
, this->location()); }
2883 do_dump_statement(Ast_dump_context
*) const;
2886 // The label that this branches to.
2887 Unnamed_label
* label_
;
2888 // True if this is "break", false if it is "continue".
2892 // Dump the AST representation for a break/continue statement
2895 Bc_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2897 ast_dump_context
->print_indent();
2898 ast_dump_context
->ostream() << (this->is_break_
? "break" : "continue");
2899 if (this->label_
!= NULL
)
2901 ast_dump_context
->ostream() << " ";
2902 ast_dump_context
->dump_label_name(this->label_
);
2904 ast_dump_context
->ostream() << std::endl
;
2907 // Make a break statement.
2910 Statement::make_break_statement(Unnamed_label
* label
, Location location
)
2912 return new Bc_statement(true, label
, location
);
2915 // Make a continue statement.
2918 Statement::make_continue_statement(Unnamed_label
* label
,
2921 return new Bc_statement(false, label
, location
);
2924 // A goto statement.
2926 class Goto_statement
: public Statement
2929 Goto_statement(Label
* label
, Location location
)
2930 : Statement(STATEMENT_GOTO
, location
),
2936 do_traverse(Traverse
*)
2937 { return TRAVERSE_CONTINUE
; }
2940 do_check_types(Gogo
*);
2943 do_may_fall_through() const
2947 do_get_backend(Translate_context
*);
2950 do_dump_statement(Ast_dump_context
*) const;
2956 // Check types for a label. There aren't any types per se, but we use
2957 // this to give an error if the label was never defined.
2960 Goto_statement::do_check_types(Gogo
*)
2962 if (!this->label_
->is_defined())
2964 error_at(this->location(), "reference to undefined label %qs",
2965 Gogo::message_name(this->label_
->name()).c_str());
2966 this->set_is_error();
2970 // Convert the goto statement to the backend representation.
2973 Goto_statement::do_get_backend(Translate_context
* context
)
2975 Blabel
* blabel
= this->label_
->get_backend_label(context
);
2976 return context
->backend()->goto_statement(blabel
, this->location());
2979 // Dump the AST representation for a goto statement.
2982 Goto_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2984 ast_dump_context
->print_indent();
2985 ast_dump_context
->ostream() << "goto " << this->label_
->name() << std::endl
;
2988 // Make a goto statement.
2991 Statement::make_goto_statement(Label
* label
, Location location
)
2993 return new Goto_statement(label
, location
);
2996 // A goto statement to an unnamed label.
2998 class Goto_unnamed_statement
: public Statement
3001 Goto_unnamed_statement(Unnamed_label
* label
, Location location
)
3002 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
3008 do_traverse(Traverse
*)
3009 { return TRAVERSE_CONTINUE
; }
3012 do_may_fall_through() const
3016 do_get_backend(Translate_context
* context
)
3017 { return this->label_
->get_goto(context
, this->location()); }
3020 do_dump_statement(Ast_dump_context
*) const;
3023 Unnamed_label
* label_
;
3026 // Dump the AST representation for an unnamed goto statement
3029 Goto_unnamed_statement::do_dump_statement(
3030 Ast_dump_context
* ast_dump_context
) const
3032 ast_dump_context
->print_indent();
3033 ast_dump_context
->ostream() << "goto ";
3034 ast_dump_context
->dump_label_name(this->label_
);
3035 ast_dump_context
->ostream() << std::endl
;
3038 // Make a goto statement to an unnamed label.
3041 Statement::make_goto_unnamed_statement(Unnamed_label
* label
,
3044 return new Goto_unnamed_statement(label
, location
);
3047 // Class Label_statement.
3052 Label_statement::do_traverse(Traverse
*)
3054 return TRAVERSE_CONTINUE
;
3057 // Return the backend representation of the statement defining this
3061 Label_statement::do_get_backend(Translate_context
* context
)
3063 Blabel
* blabel
= this->label_
->get_backend_label(context
);
3064 return context
->backend()->label_definition_statement(blabel
);
3067 // Dump the AST for a label definition statement.
3070 Label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3072 ast_dump_context
->print_indent();
3073 ast_dump_context
->ostream() << this->label_
->name() << ":" << std::endl
;
3076 // Make a label statement.
3079 Statement::make_label_statement(Label
* label
, Location location
)
3081 return new Label_statement(label
, location
);
3084 // An unnamed label statement.
3086 class Unnamed_label_statement
: public Statement
3089 Unnamed_label_statement(Unnamed_label
* label
)
3090 : Statement(STATEMENT_UNNAMED_LABEL
, label
->location()),
3096 do_traverse(Traverse
*)
3097 { return TRAVERSE_CONTINUE
; }
3100 do_get_backend(Translate_context
* context
)
3101 { return this->label_
->get_definition(context
); }
3104 do_dump_statement(Ast_dump_context
*) const;
3108 Unnamed_label
* label_
;
3111 // Dump the AST representation for an unnamed label definition statement.
3114 Unnamed_label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3117 ast_dump_context
->print_indent();
3118 ast_dump_context
->dump_label_name(this->label_
);
3119 ast_dump_context
->ostream() << ":" << std::endl
;
3122 // Make an unnamed label statement.
3125 Statement::make_unnamed_label_statement(Unnamed_label
* label
)
3127 return new Unnamed_label_statement(label
);
3132 class If_statement
: public Statement
3135 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
3137 : Statement(STATEMENT_IF
, location
),
3138 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
3143 do_traverse(Traverse
*);
3146 do_determine_types();
3149 do_check_types(Gogo
*);
3152 do_may_fall_through() const;
3155 do_get_backend(Translate_context
*);
3158 do_dump_statement(Ast_dump_context
*) const;
3169 If_statement::do_traverse(Traverse
* traverse
)
3171 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
3172 || this->then_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3173 return TRAVERSE_EXIT
;
3174 if (this->else_block_
!= NULL
)
3176 if (this->else_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3177 return TRAVERSE_EXIT
;
3179 return TRAVERSE_CONTINUE
;
3183 If_statement::do_determine_types()
3185 Type_context
context(Type::lookup_bool_type(), false);
3186 this->cond_
->determine_type(&context
);
3187 this->then_block_
->determine_types();
3188 if (this->else_block_
!= NULL
)
3189 this->else_block_
->determine_types();
3195 If_statement::do_check_types(Gogo
*)
3197 Type
* type
= this->cond_
->type();
3198 if (type
->is_error())
3199 this->set_is_error();
3200 else if (!type
->is_boolean_type())
3201 this->report_error(_("expected boolean expression"));
3204 // Whether the overall statement may fall through.
3207 If_statement::do_may_fall_through() const
3209 return (this->else_block_
== NULL
3210 || this->then_block_
->may_fall_through()
3211 || this->else_block_
->may_fall_through());
3214 // Get the backend representation.
3217 If_statement::do_get_backend(Translate_context
* context
)
3219 go_assert(this->cond_
->type()->is_boolean_type()
3220 || this->cond_
->type()->is_error());
3221 Bexpression
* cond
= this->cond_
->get_backend(context
);
3222 Bblock
* then_block
= this->then_block_
->get_backend(context
);
3223 Bblock
* else_block
= (this->else_block_
== NULL
3225 : this->else_block_
->get_backend(context
));
3226 return context
->backend()->if_statement(cond
, then_block
, else_block
,
3230 // Dump the AST representation for an if statement
3233 If_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3235 ast_dump_context
->print_indent();
3236 ast_dump_context
->ostream() << "if ";
3237 ast_dump_context
->dump_expression(this->cond_
);
3238 ast_dump_context
->ostream() << std::endl
;
3239 if (ast_dump_context
->dump_subblocks())
3241 ast_dump_context
->dump_block(this->then_block_
);
3242 if (this->else_block_
!= NULL
)
3244 ast_dump_context
->print_indent();
3245 ast_dump_context
->ostream() << "else" << std::endl
;
3246 ast_dump_context
->dump_block(this->else_block_
);
3251 // Make an if statement.
3254 Statement::make_if_statement(Expression
* cond
, Block
* then_block
,
3255 Block
* else_block
, Location location
)
3257 return new If_statement(cond
, then_block
, else_block
, location
);
3260 // Class Case_clauses::Hash_integer_value.
3262 class Case_clauses::Hash_integer_value
3266 operator()(Expression
*) const;
3270 Case_clauses::Hash_integer_value::operator()(Expression
* pe
) const
3272 Numeric_constant nc
;
3274 if (!pe
->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3276 size_t ret
= mpz_get_ui(ival
);
3281 // Class Case_clauses::Eq_integer_value.
3283 class Case_clauses::Eq_integer_value
3287 operator()(Expression
*, Expression
*) const;
3291 Case_clauses::Eq_integer_value::operator()(Expression
* a
, Expression
* b
) const
3293 Numeric_constant anc
;
3295 Numeric_constant bnc
;
3297 if (!a
->numeric_constant_value(&anc
)
3298 || !anc
.to_int(&aval
)
3299 || !b
->numeric_constant_value(&bnc
)
3300 || !bnc
.to_int(&bval
))
3302 bool ret
= mpz_cmp(aval
, bval
) == 0;
3308 // Class Case_clauses::Case_clause.
3313 Case_clauses::Case_clause::traverse(Traverse
* traverse
)
3315 if (this->cases_
!= NULL
3316 && (traverse
->traverse_mask()
3317 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3319 if (this->cases_
->traverse(traverse
) == TRAVERSE_EXIT
)
3320 return TRAVERSE_EXIT
;
3322 if (this->statements_
!= NULL
)
3324 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
3325 return TRAVERSE_EXIT
;
3327 return TRAVERSE_CONTINUE
;
3330 // Check whether all the case expressions are integer constants.
3333 Case_clauses::Case_clause::is_constant() const
3335 if (this->cases_
!= NULL
)
3337 for (Expression_list::const_iterator p
= this->cases_
->begin();
3338 p
!= this->cases_
->end();
3340 if (!(*p
)->is_constant() || (*p
)->type()->integer_type() == NULL
)
3346 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3347 // value we are switching on; it may be NULL. If START_LABEL is not
3348 // NULL, it goes at the start of the statements, after the condition
3349 // test. We branch to FINISH_LABEL at the end of the statements.
3352 Case_clauses::Case_clause::lower(Block
* b
, Temporary_statement
* val_temp
,
3353 Unnamed_label
* start_label
,
3354 Unnamed_label
* finish_label
) const
3356 Location loc
= this->location_
;
3357 Unnamed_label
* next_case_label
;
3358 if (this->cases_
== NULL
|| this->cases_
->empty())
3360 go_assert(this->is_default_
);
3361 next_case_label
= NULL
;
3365 Expression
* cond
= NULL
;
3367 for (Expression_list::const_iterator p
= this->cases_
->begin();
3368 p
!= this->cases_
->end();
3371 Expression
* ref
= Expression::make_temporary_reference(val_temp
,
3373 Expression
* this_cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3378 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, this_cond
, loc
);
3381 Block
* then_block
= new Block(b
, loc
);
3382 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3383 Statement
* s
= Statement::make_goto_unnamed_statement(next_case_label
,
3385 then_block
->add_statement(s
);
3387 // if !COND { goto NEXT_CASE_LABEL }
3388 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3389 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
3390 b
->add_statement(s
);
3393 if (start_label
!= NULL
)
3394 b
->add_statement(Statement::make_unnamed_label_statement(start_label
));
3396 if (this->statements_
!= NULL
)
3397 b
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
3399 Statement
* s
= Statement::make_goto_unnamed_statement(finish_label
, loc
);
3400 b
->add_statement(s
);
3402 if (next_case_label
!= NULL
)
3403 b
->add_statement(Statement::make_unnamed_label_statement(next_case_label
));
3409 Case_clauses::Case_clause::determine_types(Type
* type
)
3411 if (this->cases_
!= NULL
)
3413 Type_context
case_context(type
, false);
3414 for (Expression_list::iterator p
= this->cases_
->begin();
3415 p
!= this->cases_
->end();
3417 (*p
)->determine_type(&case_context
);
3419 if (this->statements_
!= NULL
)
3420 this->statements_
->determine_types();
3423 // Check types. Returns false if there was an error.
3426 Case_clauses::Case_clause::check_types(Type
* type
)
3428 if (this->cases_
!= NULL
)
3430 for (Expression_list::iterator p
= this->cases_
->begin();
3431 p
!= this->cases_
->end();
3434 if (!Type::are_assignable(type
, (*p
)->type(), NULL
)
3435 && !Type::are_assignable((*p
)->type(), type
, NULL
))
3437 error_at((*p
)->location(),
3438 "type mismatch between switch value and case clause");
3446 // Return true if this clause may fall through to the following
3447 // statements. Note that this is not the same as whether the case
3448 // uses the "fallthrough" keyword.
3451 Case_clauses::Case_clause::may_fall_through() const
3453 if (this->statements_
== NULL
)
3455 return this->statements_
->may_fall_through();
3458 // Convert the case values and statements to the backend
3459 // representation. BREAK_LABEL is the label which break statements
3460 // should branch to. CASE_CONSTANTS is used to detect duplicate
3461 // constants. *CASES should be passed as an empty vector; the values
3462 // for this case will be added to it. If this is the default case,
3463 // *CASES will remain empty. This returns the statement to execute if
3464 // one of these cases is selected.
3467 Case_clauses::Case_clause::get_backend(Translate_context
* context
,
3468 Unnamed_label
* break_label
,
3469 Case_constants
* case_constants
,
3470 std::vector
<Bexpression
*>* cases
) const
3472 if (this->cases_
!= NULL
)
3474 go_assert(!this->is_default_
);
3475 for (Expression_list::const_iterator p
= this->cases_
->begin();
3476 p
!= this->cases_
->end();
3480 if (e
->classification() != Expression::EXPRESSION_INTEGER
)
3482 Numeric_constant nc
;
3484 if (!(*p
)->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3486 // Something went wrong. This can happen with a
3487 // negative constant and an unsigned switch value.
3488 go_assert(saw_errors());
3491 go_assert(nc
.type() != NULL
);
3492 e
= Expression::make_integer_z(&ival
, nc
.type(), e
->location());
3496 std::pair
<Case_constants::iterator
, bool> ins
=
3497 case_constants
->insert(e
);
3500 // Value was already present.
3501 error_at(this->location_
, "duplicate case in switch");
3502 e
= Expression::make_error(this->location_
);
3504 cases
->push_back(e
->get_backend(context
));
3508 Bstatement
* statements
;
3509 if (this->statements_
== NULL
)
3513 Bblock
* bblock
= this->statements_
->get_backend(context
);
3514 statements
= context
->backend()->block_statement(bblock
);
3517 Bstatement
* break_stat
;
3518 if (this->is_fallthrough_
)
3521 break_stat
= break_label
->get_goto(context
, this->location_
);
3523 if (statements
== NULL
)
3525 else if (break_stat
== NULL
)
3528 return context
->backend()->compound_statement(statements
, break_stat
);
3531 // Dump the AST representation for a case clause
3534 Case_clauses::Case_clause::dump_clause(Ast_dump_context
* ast_dump_context
)
3537 ast_dump_context
->print_indent();
3538 if (this->is_default_
)
3540 ast_dump_context
->ostream() << "default:";
3544 ast_dump_context
->ostream() << "case ";
3545 ast_dump_context
->dump_expression_list(this->cases_
);
3546 ast_dump_context
->ostream() << ":" ;
3548 ast_dump_context
->dump_block(this->statements_
);
3549 if (this->is_fallthrough_
)
3551 ast_dump_context
->print_indent();
3552 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
3556 // Class Case_clauses.
3561 Case_clauses::traverse(Traverse
* traverse
)
3563 for (Clauses::iterator p
= this->clauses_
.begin();
3564 p
!= this->clauses_
.end();
3567 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
3568 return TRAVERSE_EXIT
;
3570 return TRAVERSE_CONTINUE
;
3573 // Check whether all the case expressions are constant.
3576 Case_clauses::is_constant() const
3578 for (Clauses::const_iterator p
= this->clauses_
.begin();
3579 p
!= this->clauses_
.end();
3581 if (!p
->is_constant())
3586 // Lower case clauses for a nonconstant switch.
3589 Case_clauses::lower(Block
* b
, Temporary_statement
* val_temp
,
3590 Unnamed_label
* break_label
) const
3592 // The default case.
3593 const Case_clause
* default_case
= NULL
;
3595 // The label for the fallthrough of the previous case.
3596 Unnamed_label
* last_fallthrough_label
= NULL
;
3598 // The label for the start of the default case. This is used if the
3599 // case before the default case falls through.
3600 Unnamed_label
* default_start_label
= NULL
;
3602 // The label for the end of the default case. This normally winds
3603 // up as BREAK_LABEL, but it will be different if the default case
3605 Unnamed_label
* default_finish_label
= NULL
;
3607 for (Clauses::const_iterator p
= this->clauses_
.begin();
3608 p
!= this->clauses_
.end();
3611 // The label to use for the start of the statements for this
3612 // case. This is NULL unless the previous case falls through.
3613 Unnamed_label
* start_label
= last_fallthrough_label
;
3615 // The label to jump to after the end of the statements for this
3617 Unnamed_label
* finish_label
= break_label
;
3619 last_fallthrough_label
= NULL
;
3620 if (p
->is_fallthrough() && p
+ 1 != this->clauses_
.end())
3622 finish_label
= new Unnamed_label(p
->location());
3623 last_fallthrough_label
= finish_label
;
3626 if (!p
->is_default())
3627 p
->lower(b
, val_temp
, start_label
, finish_label
);
3630 // We have to move the default case to the end, so that we
3631 // only use it if all the other tests fail.
3633 default_start_label
= start_label
;
3634 default_finish_label
= finish_label
;
3638 if (default_case
!= NULL
)
3639 default_case
->lower(b
, val_temp
, default_start_label
,
3640 default_finish_label
);
3646 Case_clauses::determine_types(Type
* type
)
3648 for (Clauses::iterator p
= this->clauses_
.begin();
3649 p
!= this->clauses_
.end();
3651 p
->determine_types(type
);
3654 // Check types. Returns false if there was an error.
3657 Case_clauses::check_types(Type
* type
)
3660 for (Clauses::iterator p
= this->clauses_
.begin();
3661 p
!= this->clauses_
.end();
3664 if (!p
->check_types(type
))
3670 // Return true if these clauses may fall through to the statements
3671 // following the switch statement.
3674 Case_clauses::may_fall_through() const
3676 bool found_default
= false;
3677 for (Clauses::const_iterator p
= this->clauses_
.begin();
3678 p
!= this->clauses_
.end();
3681 if (p
->may_fall_through() && !p
->is_fallthrough())
3683 if (p
->is_default())
3684 found_default
= true;
3686 return !found_default
;
3689 // Convert the cases to the backend representation. This sets
3690 // *ALL_CASES and *ALL_STATEMENTS.
3693 Case_clauses::get_backend(Translate_context
* context
,
3694 Unnamed_label
* break_label
,
3695 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
3696 std::vector
<Bstatement
*>* all_statements
) const
3698 Case_constants case_constants
;
3700 size_t c
= this->clauses_
.size();
3701 all_cases
->resize(c
);
3702 all_statements
->resize(c
);
3705 for (Clauses::const_iterator p
= this->clauses_
.begin();
3706 p
!= this->clauses_
.end();
3709 std::vector
<Bexpression
*> cases
;
3710 Bstatement
* stat
= p
->get_backend(context
, break_label
, &case_constants
,
3712 (*all_cases
)[i
].swap(cases
);
3713 (*all_statements
)[i
] = stat
;
3717 // Dump the AST representation for case clauses (from a switch statement)
3720 Case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
3722 for (Clauses::const_iterator p
= this->clauses_
.begin();
3723 p
!= this->clauses_
.end();
3725 p
->dump_clause(ast_dump_context
);
3728 // A constant switch statement. A Switch_statement is lowered to this
3729 // when all the cases are constants.
3731 class Constant_switch_statement
: public Statement
3734 Constant_switch_statement(Expression
* val
, Case_clauses
* clauses
,
3735 Unnamed_label
* break_label
,
3737 : Statement(STATEMENT_CONSTANT_SWITCH
, location
),
3738 val_(val
), clauses_(clauses
), break_label_(break_label
)
3743 do_traverse(Traverse
*);
3746 do_determine_types();
3749 do_check_types(Gogo
*);
3752 do_get_backend(Translate_context
*);
3755 do_dump_statement(Ast_dump_context
*) const;
3758 // The value to switch on.
3760 // The case clauses.
3761 Case_clauses
* clauses_
;
3762 // The break label, if needed.
3763 Unnamed_label
* break_label_
;
3769 Constant_switch_statement::do_traverse(Traverse
* traverse
)
3771 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3772 return TRAVERSE_EXIT
;
3773 return this->clauses_
->traverse(traverse
);
3779 Constant_switch_statement::do_determine_types()
3781 this->val_
->determine_type_no_context();
3782 this->clauses_
->determine_types(this->val_
->type());
3788 Constant_switch_statement::do_check_types(Gogo
*)
3790 if (!this->clauses_
->check_types(this->val_
->type()))
3791 this->set_is_error();
3794 // Convert to GENERIC.
3797 Constant_switch_statement::do_get_backend(Translate_context
* context
)
3799 Bexpression
* switch_val_expr
= this->val_
->get_backend(context
);
3801 Unnamed_label
* break_label
= this->break_label_
;
3802 if (break_label
== NULL
)
3803 break_label
= new Unnamed_label(this->location());
3805 std::vector
<std::vector
<Bexpression
*> > all_cases
;
3806 std::vector
<Bstatement
*> all_statements
;
3807 this->clauses_
->get_backend(context
, break_label
, &all_cases
,
3810 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
3811 Bstatement
* switch_statement
;
3812 switch_statement
= context
->backend()->switch_statement(bfunction
,
3817 Bstatement
* ldef
= break_label
->get_definition(context
);
3818 return context
->backend()->compound_statement(switch_statement
, ldef
);
3821 // Dump the AST representation for a constant switch statement.
3824 Constant_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3827 ast_dump_context
->print_indent();
3828 ast_dump_context
->ostream() << "switch ";
3829 ast_dump_context
->dump_expression(this->val_
);
3831 if (ast_dump_context
->dump_subblocks())
3833 ast_dump_context
->ostream() << " {" << std::endl
;
3834 this->clauses_
->dump_clauses(ast_dump_context
);
3835 ast_dump_context
->ostream() << "}";
3838 ast_dump_context
->ostream() << std::endl
;
3841 // Class Switch_statement.
3846 Switch_statement::do_traverse(Traverse
* traverse
)
3848 if (this->val_
!= NULL
)
3850 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3851 return TRAVERSE_EXIT
;
3853 return this->clauses_
->traverse(traverse
);
3856 // Lower a Switch_statement to a Constant_switch_statement or a series
3857 // of if statements.
3860 Switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
3861 Statement_inserter
*)
3863 Location loc
= this->location();
3865 if (this->val_
!= NULL
3866 && (this->val_
->is_error_expression()
3867 || this->val_
->type()->is_error()))
3868 return Statement::make_error_statement(loc
);
3870 if (this->val_
!= NULL
3871 && this->val_
->type()->integer_type() != NULL
3872 && !this->clauses_
->empty()
3873 && this->clauses_
->is_constant())
3874 return new Constant_switch_statement(this->val_
, this->clauses_
,
3875 this->break_label_
, loc
);
3877 if (this->val_
!= NULL
3878 && !this->val_
->type()->is_comparable()
3879 && !Type::are_compatible_for_comparison(true, this->val_
->type(),
3880 Type::make_nil_type(), NULL
))
3882 error_at(this->val_
->location(),
3883 "cannot switch on value whose type that may not be compared");
3884 return Statement::make_error_statement(loc
);
3887 Block
* b
= new Block(enclosing
, loc
);
3889 if (this->clauses_
->empty())
3891 Expression
* val
= this->val_
;
3893 val
= Expression::make_boolean(true, loc
);
3894 return Statement::make_statement(val
, true);
3897 // var val_temp VAL_TYPE = VAL
3898 Expression
* val
= this->val_
;
3900 val
= Expression::make_boolean(true, loc
);
3902 Type
* type
= val
->type();
3903 if (type
->is_abstract())
3904 type
= type
->make_non_abstract_type();
3905 Temporary_statement
* val_temp
= Statement::make_temporary(type
, val
, loc
);
3906 b
->add_statement(val_temp
);
3908 this->clauses_
->lower(b
, val_temp
, this->break_label());
3910 Statement
* s
= Statement::make_unnamed_label_statement(this->break_label_
);
3911 b
->add_statement(s
);
3913 return Statement::make_block_statement(b
, loc
);
3916 // Return the break label for this switch statement, creating it if
3920 Switch_statement::break_label()
3922 if (this->break_label_
== NULL
)
3923 this->break_label_
= new Unnamed_label(this->location());
3924 return this->break_label_
;
3927 // Dump the AST representation for a switch statement.
3930 Switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3932 ast_dump_context
->print_indent();
3933 ast_dump_context
->ostream() << "switch ";
3934 if (this->val_
!= NULL
)
3936 ast_dump_context
->dump_expression(this->val_
);
3938 if (ast_dump_context
->dump_subblocks())
3940 ast_dump_context
->ostream() << " {" << std::endl
;
3941 this->clauses_
->dump_clauses(ast_dump_context
);
3942 ast_dump_context
->print_indent();
3943 ast_dump_context
->ostream() << "}";
3945 ast_dump_context
->ostream() << std::endl
;
3948 // Return whether this switch may fall through.
3951 Switch_statement::do_may_fall_through() const
3953 if (this->clauses_
== NULL
)
3956 // If we have a break label, then some case needed it. That implies
3957 // that the switch statement as a whole can fall through.
3958 if (this->break_label_
!= NULL
)
3961 return this->clauses_
->may_fall_through();
3964 // Make a switch statement.
3967 Statement::make_switch_statement(Expression
* val
, Location location
)
3969 return new Switch_statement(val
, location
);
3972 // Class Type_case_clauses::Type_case_clause.
3977 Type_case_clauses::Type_case_clause::traverse(Traverse
* traverse
)
3979 if (!this->is_default_
3980 && ((traverse
->traverse_mask()
3981 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3982 && Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3983 return TRAVERSE_EXIT
;
3984 if (this->statements_
!= NULL
)
3985 return this->statements_
->traverse(traverse
);
3986 return TRAVERSE_CONTINUE
;
3989 // Lower one clause in a type switch. Add statements to the block B.
3990 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3991 // BREAK_LABEL is the label at the end of the type switch.
3992 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3996 Type_case_clauses::Type_case_clause::lower(Type
* switch_val_type
,
3998 Temporary_statement
* descriptor_temp
,
3999 Unnamed_label
* break_label
,
4000 Unnamed_label
** stmts_label
) const
4002 Location loc
= this->location_
;
4004 Unnamed_label
* next_case_label
= NULL
;
4005 if (!this->is_default_
)
4007 Type
* type
= this->type_
;
4010 if (switch_val_type
->interface_type() != NULL
4011 && !type
->is_nil_constant_as_type()
4012 && type
->interface_type() == NULL
4013 && !switch_val_type
->interface_type()->implements_interface(type
,
4017 error_at(this->location_
, "impossible type switch case");
4019 error_at(this->location_
, "impossible type switch case (%s)",
4023 Expression
* ref
= Expression::make_temporary_reference(descriptor_temp
,
4027 // The language permits case nil, which is of course a constant
4028 // rather than a type. It will appear here as an invalid
4030 if (type
->is_nil_constant_as_type())
4031 cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
4032 Expression::make_nil(loc
),
4035 cond
= Runtime::make_call((type
->interface_type() == NULL
4036 ? Runtime::IFACETYPEEQ
4037 : Runtime::IFACEI2TP
),
4039 Expression::make_type_descriptor(type
, loc
),
4042 Unnamed_label
* dest
;
4043 if (!this->is_fallthrough_
)
4045 // if !COND { goto NEXT_CASE_LABEL }
4046 next_case_label
= new Unnamed_label(Linemap::unknown_location());
4047 dest
= next_case_label
;
4048 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
4052 // if COND { goto STMTS_LABEL }
4053 go_assert(stmts_label
!= NULL
);
4054 if (*stmts_label
== NULL
)
4055 *stmts_label
= new Unnamed_label(Linemap::unknown_location());
4056 dest
= *stmts_label
;
4058 Block
* then_block
= new Block(b
, loc
);
4059 Statement
* s
= Statement::make_goto_unnamed_statement(dest
, loc
);
4060 then_block
->add_statement(s
);
4061 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
4062 b
->add_statement(s
);
4065 if (this->statements_
!= NULL
4066 || (!this->is_fallthrough_
4067 && stmts_label
!= NULL
4068 && *stmts_label
!= NULL
))
4070 go_assert(!this->is_fallthrough_
);
4071 if (stmts_label
!= NULL
&& *stmts_label
!= NULL
)
4073 go_assert(!this->is_default_
);
4074 if (this->statements_
!= NULL
)
4075 (*stmts_label
)->set_location(this->statements_
->start_location());
4076 Statement
* s
= Statement::make_unnamed_label_statement(*stmts_label
);
4077 b
->add_statement(s
);
4078 *stmts_label
= NULL
;
4080 if (this->statements_
!= NULL
)
4081 b
->add_statement(Statement::make_block_statement(this->statements_
,
4085 if (this->is_fallthrough_
)
4086 go_assert(next_case_label
== NULL
);
4089 Location gloc
= (this->statements_
== NULL
4091 : this->statements_
->end_location());
4092 b
->add_statement(Statement::make_goto_unnamed_statement(break_label
,
4094 if (next_case_label
!= NULL
)
4097 Statement::make_unnamed_label_statement(next_case_label
);
4098 b
->add_statement(s
);
4103 // Return true if this type clause may fall through to the statements
4104 // following the switch.
4107 Type_case_clauses::Type_case_clause::may_fall_through() const
4109 if (this->is_fallthrough_
)
4111 // This case means that we automatically fall through to the
4112 // next case (it's used for T1 in case T1, T2:). It does not
4113 // mean that we fall through to the end of the type switch as a
4114 // whole. There is sure to be a next case and that next case
4115 // will determine whether we fall through to the statements
4116 // after the type switch.
4119 if (this->statements_
== NULL
)
4121 return this->statements_
->may_fall_through();
4124 // Dump the AST representation for a type case clause
4127 Type_case_clauses::Type_case_clause::dump_clause(
4128 Ast_dump_context
* ast_dump_context
) const
4130 ast_dump_context
->print_indent();
4131 if (this->is_default_
)
4133 ast_dump_context
->ostream() << "default:";
4137 ast_dump_context
->ostream() << "case ";
4138 ast_dump_context
->dump_type(this->type_
);
4139 ast_dump_context
->ostream() << ":" ;
4141 ast_dump_context
->dump_block(this->statements_
);
4142 if (this->is_fallthrough_
)
4144 ast_dump_context
->print_indent();
4145 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
4149 // Class Type_case_clauses.
4154 Type_case_clauses::traverse(Traverse
* traverse
)
4156 for (Type_clauses::iterator p
= this->clauses_
.begin();
4157 p
!= this->clauses_
.end();
4160 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4161 return TRAVERSE_EXIT
;
4163 return TRAVERSE_CONTINUE
;
4166 // Check for duplicate types.
4169 Type_case_clauses::check_duplicates() const
4171 typedef Unordered_set_hash(const Type
*, Type_hash_identical
,
4172 Type_identical
) Types_seen
;
4173 Types_seen types_seen
;
4174 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4175 p
!= this->clauses_
.end();
4178 Type
* t
= p
->type();
4181 if (t
->is_nil_constant_as_type())
4182 t
= Type::make_nil_type();
4183 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
.insert(t
);
4185 error_at(p
->location(), "duplicate type in switch");
4189 // Lower the clauses in a type switch. Add statements to the block B.
4190 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4191 // BREAK_LABEL is the label at the end of the type switch.
4194 Type_case_clauses::lower(Type
* switch_val_type
, Block
* b
,
4195 Temporary_statement
* descriptor_temp
,
4196 Unnamed_label
* break_label
) const
4198 const Type_case_clause
* default_case
= NULL
;
4200 Unnamed_label
* stmts_label
= NULL
;
4201 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4202 p
!= this->clauses_
.end();
4205 if (!p
->is_default())
4206 p
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4210 // We are generating a series of tests, which means that we
4211 // need to move the default case to the end.
4215 go_assert(stmts_label
== NULL
);
4217 if (default_case
!= NULL
)
4218 default_case
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4222 // Return true if these clauses may fall through to the statements
4223 // following the switch statement.
4226 Type_case_clauses::may_fall_through() const
4228 bool found_default
= false;
4229 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4230 p
!= this->clauses_
.end();
4233 if (p
->may_fall_through())
4235 if (p
->is_default())
4236 found_default
= true;
4238 return !found_default
;
4241 // Dump the AST representation for case clauses (from a switch statement)
4244 Type_case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4246 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4247 p
!= this->clauses_
.end();
4249 p
->dump_clause(ast_dump_context
);
4252 // Class Type_switch_statement.
4257 Type_switch_statement::do_traverse(Traverse
* traverse
)
4259 if (this->var_
== NULL
)
4261 if (this->traverse_expression(traverse
, &this->expr_
) == TRAVERSE_EXIT
)
4262 return TRAVERSE_EXIT
;
4264 if (this->clauses_
!= NULL
)
4265 return this->clauses_
->traverse(traverse
);
4266 return TRAVERSE_CONTINUE
;
4269 // Lower a type switch statement to a series of if statements. The gc
4270 // compiler is able to generate a table in some cases. However, that
4271 // does not work for us because we may have type descriptors in
4272 // different shared libraries, so we can't compare them with simple
4273 // equality testing.
4276 Type_switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
4277 Statement_inserter
*)
4279 const Location loc
= this->location();
4281 if (this->clauses_
!= NULL
)
4282 this->clauses_
->check_duplicates();
4284 Block
* b
= new Block(enclosing
, loc
);
4286 Type
* val_type
= (this->var_
!= NULL
4287 ? this->var_
->var_value()->type()
4288 : this->expr_
->type());
4290 if (val_type
->interface_type() == NULL
)
4292 if (!val_type
->is_error())
4293 this->report_error(_("cannot type switch on non-interface value"));
4294 return Statement::make_error_statement(loc
);
4297 // var descriptor_temp DESCRIPTOR_TYPE
4298 Type
* descriptor_type
= Type::make_type_descriptor_ptr_type();
4299 Temporary_statement
* descriptor_temp
=
4300 Statement::make_temporary(descriptor_type
, NULL
, loc
);
4301 b
->add_statement(descriptor_temp
);
4303 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4305 bool is_empty
= val_type
->interface_type()->is_empty();
4307 if (this->var_
== NULL
)
4310 ref
= Expression::make_var_reference(this->var_
, loc
);
4311 Expression
* call
= Runtime::make_call((is_empty
4312 ? Runtime::EFACETYPE
4313 : Runtime::IFACETYPE
),
4315 Temporary_reference_expression
* lhs
=
4316 Expression::make_temporary_reference(descriptor_temp
, loc
);
4317 lhs
->set_is_lvalue();
4318 Statement
* s
= Statement::make_assignment(lhs
, call
, loc
);
4319 b
->add_statement(s
);
4321 if (this->clauses_
!= NULL
)
4322 this->clauses_
->lower(val_type
, b
, descriptor_temp
, this->break_label());
4324 s
= Statement::make_unnamed_label_statement(this->break_label_
);
4325 b
->add_statement(s
);
4327 return Statement::make_block_statement(b
, loc
);
4330 // Return whether this switch may fall through.
4333 Type_switch_statement::do_may_fall_through() const
4335 if (this->clauses_
== NULL
)
4338 // If we have a break label, then some case needed it. That implies
4339 // that the switch statement as a whole can fall through.
4340 if (this->break_label_
!= NULL
)
4343 return this->clauses_
->may_fall_through();
4346 // Return the break label for this type switch statement, creating it
4350 Type_switch_statement::break_label()
4352 if (this->break_label_
== NULL
)
4353 this->break_label_
= new Unnamed_label(this->location());
4354 return this->break_label_
;
4357 // Dump the AST representation for a type switch statement
4360 Type_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
4363 ast_dump_context
->print_indent();
4364 ast_dump_context
->ostream() << "switch " << this->var_
->name() << " = ";
4365 ast_dump_context
->dump_expression(this->expr_
);
4366 ast_dump_context
->ostream() << " .(type)";
4367 if (ast_dump_context
->dump_subblocks())
4369 ast_dump_context
->ostream() << " {" << std::endl
;
4370 this->clauses_
->dump_clauses(ast_dump_context
);
4371 ast_dump_context
->ostream() << "}";
4373 ast_dump_context
->ostream() << std::endl
;
4376 // Make a type switch statement.
4378 Type_switch_statement
*
4379 Statement::make_type_switch_statement(Named_object
* var
, Expression
* expr
,
4382 return new Type_switch_statement(var
, expr
, location
);
4385 // Class Send_statement.
4390 Send_statement::do_traverse(Traverse
* traverse
)
4392 if (this->traverse_expression(traverse
, &this->channel_
) == TRAVERSE_EXIT
)
4393 return TRAVERSE_EXIT
;
4394 return this->traverse_expression(traverse
, &this->val_
);
4400 Send_statement::do_determine_types()
4402 this->channel_
->determine_type_no_context();
4403 Type
* type
= this->channel_
->type();
4404 Type_context context
;
4405 if (type
->channel_type() != NULL
)
4406 context
.type
= type
->channel_type()->element_type();
4407 this->val_
->determine_type(&context
);
4413 Send_statement::do_check_types(Gogo
*)
4415 Type
* type
= this->channel_
->type();
4416 if (type
->is_error())
4418 this->set_is_error();
4421 Channel_type
* channel_type
= type
->channel_type();
4422 if (channel_type
== NULL
)
4424 error_at(this->location(), "left operand of %<<-%> must be channel");
4425 this->set_is_error();
4428 Type
* element_type
= channel_type
->element_type();
4429 if (!Type::are_assignable(element_type
, this->val_
->type(), NULL
))
4431 this->report_error(_("incompatible types in send"));
4434 if (!channel_type
->may_send())
4436 this->report_error(_("invalid send on receive-only channel"));
4441 // Flatten a send statement. We may need a temporary for interface
4445 Send_statement::do_flatten(Gogo
*, Named_object
*, Block
*,
4446 Statement_inserter
* inserter
)
4448 Type
* element_type
= this->channel_
->type()->channel_type()->element_type();
4449 if (!Type::are_identical(element_type
, this->val_
->type(), false, NULL
)
4450 && this->val_
->type()->interface_type() != NULL
4451 && !this->val_
->is_variable())
4453 Temporary_statement
* temp
=
4454 Statement::make_temporary(NULL
, this->val_
, this->location());
4455 inserter
->insert(temp
);
4456 this->val_
= Expression::make_temporary_reference(temp
,
4462 // Convert a send statement to the backend representation.
4465 Send_statement::do_get_backend(Translate_context
* context
)
4467 Location loc
= this->location();
4469 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
4470 Type
* element_type
= channel_type
->element_type();
4471 Expression
* val
= Expression::convert_for_assignment(context
->gogo(),
4476 bool can_take_address
;
4477 switch (element_type
->base()->classification())
4479 case Type::TYPE_BOOLEAN
:
4480 case Type::TYPE_INTEGER
:
4481 case Type::TYPE_FUNCTION
:
4482 case Type::TYPE_POINTER
:
4483 case Type::TYPE_MAP
:
4484 case Type::TYPE_CHANNEL
:
4486 can_take_address
= false;
4489 case Type::TYPE_FLOAT
:
4490 case Type::TYPE_COMPLEX
:
4491 case Type::TYPE_STRING
:
4492 case Type::TYPE_INTERFACE
:
4494 can_take_address
= false;
4497 case Type::TYPE_STRUCT
:
4499 can_take_address
= true;
4502 case Type::TYPE_ARRAY
:
4504 can_take_address
= !element_type
->is_slice_type();
4508 case Type::TYPE_ERROR
:
4509 case Type::TYPE_VOID
:
4510 case Type::TYPE_SINK
:
4511 case Type::TYPE_NIL
:
4512 case Type::TYPE_NAMED
:
4513 case Type::TYPE_FORWARD
:
4514 go_assert(saw_errors());
4515 return context
->backend()->error_statement();
4518 // Only try to take the address of a variable. We have already
4519 // moved variables to the heap, so this should not cause that to
4520 // happen unnecessarily.
4521 if (can_take_address
4522 && val
->var_expression() == NULL
4523 && val
->temporary_reference_expression() == NULL
)
4524 can_take_address
= false;
4526 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
4529 Runtime::Function code
;
4530 Bstatement
* btemp
= NULL
;
4533 // Type is small enough to handle as uint64.
4534 code
= Runtime::SEND_SMALL
;
4535 val
= Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
4538 else if (can_take_address
)
4540 // Must pass address of value. The function doesn't change the
4541 // value, so just take its address directly.
4542 code
= Runtime::SEND_BIG
;
4543 val
= Expression::make_unary(OPERATOR_AND
, val
, loc
);
4547 // Must pass address of value, but the value is small enough
4548 // that it might be in registers. Copy value into temporary
4549 // variable to take address.
4550 code
= Runtime::SEND_BIG
;
4551 Temporary_statement
* temp
= Statement::make_temporary(element_type
,
4553 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
4554 val
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
4555 btemp
= temp
->get_backend(context
);
4558 Expression
* call
= Runtime::make_call(code
, loc
, 3, td
, this->channel_
, val
);
4560 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4561 Bexpression
* bcall
= call
->get_backend(context
);
4562 Bstatement
* s
= context
->backend()->expression_statement(bcall
);
4567 return context
->backend()->compound_statement(btemp
, s
);
4570 // Dump the AST representation for a send statement
4573 Send_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4575 ast_dump_context
->print_indent();
4576 ast_dump_context
->dump_expression(this->channel_
);
4577 ast_dump_context
->ostream() << " <- ";
4578 ast_dump_context
->dump_expression(this->val_
);
4579 ast_dump_context
->ostream() << std::endl
;
4582 // Make a send statement.
4585 Statement::make_send_statement(Expression
* channel
, Expression
* val
,
4588 return new Send_statement(channel
, val
, location
);
4591 // Class Select_clauses::Select_clause.
4596 Select_clauses::Select_clause::traverse(Traverse
* traverse
)
4598 if (!this->is_lowered_
4599 && (traverse
->traverse_mask()
4600 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
4602 if (this->channel_
!= NULL
)
4604 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
4605 return TRAVERSE_EXIT
;
4607 if (this->val_
!= NULL
)
4609 if (Expression::traverse(&this->val_
, traverse
) == TRAVERSE_EXIT
)
4610 return TRAVERSE_EXIT
;
4612 if (this->closed_
!= NULL
)
4614 if (Expression::traverse(&this->closed_
, traverse
) == TRAVERSE_EXIT
)
4615 return TRAVERSE_EXIT
;
4618 if (this->statements_
!= NULL
)
4620 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
4621 return TRAVERSE_EXIT
;
4623 return TRAVERSE_CONTINUE
;
4626 // Lowering. We call a function to register this clause, and arrange
4627 // to set any variables in any receive clause.
4630 Select_clauses::Select_clause::lower(Gogo
* gogo
, Named_object
* function
,
4631 Block
* b
, Temporary_statement
* sel
)
4633 Location loc
= this->location_
;
4635 Expression
* selref
= Expression::make_temporary_reference(sel
, loc
);
4637 Expression
* index_expr
= Expression::make_integer_ul(this->index_
, NULL
,
4640 if (this->is_default_
)
4642 go_assert(this->channel_
== NULL
&& this->val_
== NULL
);
4643 this->lower_default(b
, selref
, index_expr
);
4644 this->is_lowered_
= true;
4648 // Evaluate the channel before the select statement.
4649 Temporary_statement
* channel_temp
= Statement::make_temporary(NULL
,
4652 b
->add_statement(channel_temp
);
4653 Expression
* chanref
= Expression::make_temporary_reference(channel_temp
,
4657 this->lower_send(b
, selref
, chanref
, index_expr
);
4659 this->lower_recv(gogo
, function
, b
, selref
, chanref
, index_expr
);
4661 // Now all references should be handled through the statements, not
4663 this->is_lowered_
= true;
4668 // Lower a default clause in a select statement.
4671 Select_clauses::Select_clause::lower_default(Block
* b
, Expression
* selref
,
4672 Expression
* index_expr
)
4674 Location loc
= this->location_
;
4675 Expression
* call
= Runtime::make_call(Runtime::SELECTDEFAULT
, loc
, 2, selref
,
4677 b
->add_statement(Statement::make_statement(call
, true));
4680 // Lower a send clause in a select statement.
4683 Select_clauses::Select_clause::lower_send(Block
* b
, Expression
* selref
,
4684 Expression
* chanref
,
4685 Expression
* index_expr
)
4687 Location loc
= this->location_
;
4689 Channel_type
* ct
= this->channel_
->type()->channel_type();
4693 Type
* valtype
= ct
->element_type();
4695 // Note that copying the value to a temporary here means that we
4696 // evaluate the send values in the required order.
4697 Temporary_statement
* val
= Statement::make_temporary(valtype
, this->val_
,
4699 b
->add_statement(val
);
4701 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4702 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4704 Expression
* call
= Runtime::make_call(Runtime::SELECTSEND
, loc
, 4, selref
,
4705 chanref
, valaddr
, index_expr
);
4706 b
->add_statement(Statement::make_statement(call
, true));
4709 // Lower a receive clause in a select statement.
4712 Select_clauses::Select_clause::lower_recv(Gogo
* gogo
, Named_object
* function
,
4713 Block
* b
, Expression
* selref
,
4714 Expression
* chanref
,
4715 Expression
* index_expr
)
4717 Location loc
= this->location_
;
4719 Channel_type
* ct
= this->channel_
->type()->channel_type();
4723 Type
* valtype
= ct
->element_type();
4724 Temporary_statement
* val
= Statement::make_temporary(valtype
, NULL
, loc
);
4725 b
->add_statement(val
);
4727 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4728 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4730 Temporary_statement
* closed_temp
= NULL
;
4733 if (this->closed_
== NULL
&& this->closedvar_
== NULL
)
4734 call
= Runtime::make_call(Runtime::SELECTRECV
, loc
, 4, selref
, chanref
,
4735 valaddr
, index_expr
);
4738 closed_temp
= Statement::make_temporary(Type::lookup_bool_type(), NULL
,
4740 b
->add_statement(closed_temp
);
4741 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4743 Expression
* caddr
= Expression::make_unary(OPERATOR_AND
, cref
, loc
);
4744 call
= Runtime::make_call(Runtime::SELECTRECV2
, loc
, 5, selref
, chanref
,
4745 valaddr
, caddr
, index_expr
);
4748 b
->add_statement(Statement::make_statement(call
, true));
4750 // If the block of statements is executed, arrange for the received
4751 // value to move from VAL to the place where the statements expect
4756 if (this->var_
!= NULL
)
4758 go_assert(this->val_
== NULL
);
4759 valref
= Expression::make_temporary_reference(val
, loc
);
4760 this->var_
->var_value()->set_init(valref
);
4761 this->var_
->var_value()->clear_type_from_chan_element();
4763 else if (this->val_
!= NULL
&& !this->val_
->is_sink_expression())
4765 init
= new Block(b
, loc
);
4766 valref
= Expression::make_temporary_reference(val
, loc
);
4767 init
->add_statement(Statement::make_assignment(this->val_
, valref
, loc
));
4770 if (this->closedvar_
!= NULL
)
4772 go_assert(this->closed_
== NULL
);
4773 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4775 this->closedvar_
->var_value()->set_init(cref
);
4777 else if (this->closed_
!= NULL
&& !this->closed_
->is_sink_expression())
4780 init
= new Block(b
, loc
);
4781 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4783 init
->add_statement(Statement::make_assignment(this->closed_
, cref
,
4789 gogo
->lower_block(function
, init
);
4791 if (this->statements_
!= NULL
)
4792 init
->add_statement(Statement::make_block_statement(this->statements_
,
4794 this->statements_
= init
;
4801 Select_clauses::Select_clause::determine_types()
4803 go_assert(this->is_lowered_
);
4804 if (this->statements_
!= NULL
)
4805 this->statements_
->determine_types();
4811 Select_clauses::Select_clause::check_types()
4813 if (this->is_default_
)
4816 Channel_type
* ct
= this->channel_
->type()->channel_type();
4819 error_at(this->channel_
->location(), "expected channel");
4823 if (this->is_send_
&& !ct
->may_send())
4824 error_at(this->location(), "invalid send on receive-only channel");
4825 else if (!this->is_send_
&& !ct
->may_receive())
4826 error_at(this->location(), "invalid receive on send-only channel");
4829 // Whether this clause may fall through to the statement which follows
4830 // the overall select statement.
4833 Select_clauses::Select_clause::may_fall_through() const
4835 if (this->statements_
== NULL
)
4837 return this->statements_
->may_fall_through();
4840 // Return the backend representation for the statements to execute.
4843 Select_clauses::Select_clause::get_statements_backend(
4844 Translate_context
* context
)
4846 if (this->statements_
== NULL
)
4848 Bblock
* bblock
= this->statements_
->get_backend(context
);
4849 return context
->backend()->block_statement(bblock
);
4852 // Dump the AST representation for a select case clause
4855 Select_clauses::Select_clause::dump_clause(
4856 Ast_dump_context
* ast_dump_context
) const
4858 ast_dump_context
->print_indent();
4859 if (this->is_default_
)
4861 ast_dump_context
->ostream() << "default:";
4865 ast_dump_context
->ostream() << "case " ;
4868 ast_dump_context
->dump_expression(this->channel_
);
4869 ast_dump_context
->ostream() << " <- " ;
4870 if (this->val_
!= NULL
)
4871 ast_dump_context
->dump_expression(this->val_
);
4875 if (this->val_
!= NULL
)
4876 ast_dump_context
->dump_expression(this->val_
);
4877 if (this->closed_
!= NULL
)
4879 // FIXME: can val_ == NULL and closed_ ! = NULL?
4880 ast_dump_context
->ostream() << " , " ;
4881 ast_dump_context
->dump_expression(this->closed_
);
4883 if (this->closedvar_
!= NULL
|| this->var_
!= NULL
)
4884 ast_dump_context
->ostream() << " := " ;
4886 ast_dump_context
->ostream() << " <- " ;
4887 ast_dump_context
->dump_expression(this->channel_
);
4889 ast_dump_context
->ostream() << ":" ;
4891 ast_dump_context
->dump_block(this->statements_
);
4894 // Class Select_clauses.
4899 Select_clauses::traverse(Traverse
* traverse
)
4901 for (Clauses::iterator p
= this->clauses_
.begin();
4902 p
!= this->clauses_
.end();
4905 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4906 return TRAVERSE_EXIT
;
4908 return TRAVERSE_CONTINUE
;
4911 // Lowering. Here we pull out the channel and the send values, to
4912 // enforce the order of evaluation. We also add explicit send and
4913 // receive statements to the clauses.
4916 Select_clauses::lower(Gogo
* gogo
, Named_object
* function
, Block
* b
,
4917 Temporary_statement
* sel
)
4919 for (Clauses::iterator p
= this->clauses_
.begin();
4920 p
!= this->clauses_
.end();
4922 p
->lower(gogo
, function
, b
, sel
);
4928 Select_clauses::determine_types()
4930 for (Clauses::iterator p
= this->clauses_
.begin();
4931 p
!= this->clauses_
.end();
4933 p
->determine_types();
4939 Select_clauses::check_types()
4941 for (Clauses::iterator p
= this->clauses_
.begin();
4942 p
!= this->clauses_
.end();
4947 // Return whether these select clauses fall through to the statement
4948 // following the overall select statement.
4951 Select_clauses::may_fall_through() const
4953 for (Clauses::const_iterator p
= this->clauses_
.begin();
4954 p
!= this->clauses_
.end();
4956 if (p
->may_fall_through())
4961 // Convert to the backend representation. We have already accumulated
4962 // all the select information. Now we call selectgo, which will
4963 // return the index of the clause to execute.
4966 Select_clauses::get_backend(Translate_context
* context
,
4967 Temporary_statement
* sel
,
4968 Unnamed_label
*break_label
,
4971 size_t count
= this->clauses_
.size();
4972 std::vector
<std::vector
<Bexpression
*> > cases(count
);
4973 std::vector
<Bstatement
*> clauses(count
);
4975 Type
* int32_type
= Type::lookup_integer_type("int32");
4978 for (Clauses::iterator p
= this->clauses_
.begin();
4979 p
!= this->clauses_
.end();
4982 int index
= p
->index();
4983 Expression
* index_expr
= Expression::make_integer_ul(index
, int32_type
,
4985 cases
[i
].push_back(index_expr
->get_backend(context
));
4987 Bstatement
* s
= p
->get_statements_backend(context
);
4988 Location gloc
= (p
->statements() == NULL
4990 : p
->statements()->end_location());
4991 Bstatement
* g
= break_label
->get_goto(context
, gloc
);
4996 clauses
[i
] = context
->backend()->compound_statement(s
, g
);
4999 Expression
* selref
= Expression::make_temporary_reference(sel
, location
);
5000 Expression
* call
= Runtime::make_call(Runtime::SELECTGO
, location
, 1,
5002 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
5003 Bexpression
* bcall
= call
->get_backend(context
);
5006 return context
->backend()->expression_statement(bcall
);
5008 std::vector
<Bstatement
*> statements
;
5009 statements
.reserve(2);
5011 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
5012 Bstatement
* switch_stmt
= context
->backend()->switch_statement(bfunction
,
5017 statements
.push_back(switch_stmt
);
5019 Bstatement
* ldef
= break_label
->get_definition(context
);
5020 statements
.push_back(ldef
);
5022 return context
->backend()->statement_list(statements
);
5024 // Dump the AST representation for select clauses.
5027 Select_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
5029 for (Clauses::const_iterator p
= this->clauses_
.begin();
5030 p
!= this->clauses_
.end();
5032 p
->dump_clause(ast_dump_context
);
5035 // Class Select_statement.
5037 // Return the break label for this switch statement, creating it if
5041 Select_statement::break_label()
5043 if (this->break_label_
== NULL
)
5044 this->break_label_
= new Unnamed_label(this->location());
5045 return this->break_label_
;
5048 // Lower a select statement. This will still return a select
5049 // statement, but it will be modified to implement the order of
5050 // evaluation rules, and to include the send and receive statements as
5051 // explicit statements in the clauses.
5054 Select_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
5055 Block
* enclosing
, Statement_inserter
*)
5057 if (this->is_lowered_
)
5060 Location loc
= this->location();
5062 Block
* b
= new Block(enclosing
, loc
);
5064 go_assert(this->sel_
== NULL
);
5066 Expression
* size_expr
= Expression::make_integer_ul(this->clauses_
->size(),
5068 Expression
* call
= Runtime::make_call(Runtime::NEWSELECT
, loc
, 1, size_expr
);
5070 this->sel_
= Statement::make_temporary(NULL
, call
, loc
);
5071 b
->add_statement(this->sel_
);
5073 this->clauses_
->lower(gogo
, function
, b
, this->sel_
);
5074 this->is_lowered_
= true;
5075 b
->add_statement(this);
5077 return Statement::make_block_statement(b
, loc
);
5080 // Whether the select statement itself may fall through to the following
5084 Select_statement::do_may_fall_through() const
5086 // A select statement is terminating if no break statement
5087 // refers to it and all of its clauses are terminating.
5088 if (this->break_label_
!= NULL
)
5090 return this->clauses_
->may_fall_through();
5093 // Return the backend representation for a select statement.
5096 Select_statement::do_get_backend(Translate_context
* context
)
5098 return this->clauses_
->get_backend(context
, this->sel_
, this->break_label(),
5102 // Dump the AST representation for a select statement.
5105 Select_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5107 ast_dump_context
->print_indent();
5108 ast_dump_context
->ostream() << "select";
5109 if (ast_dump_context
->dump_subblocks())
5111 ast_dump_context
->ostream() << " {" << std::endl
;
5112 this->clauses_
->dump_clauses(ast_dump_context
);
5113 ast_dump_context
->ostream() << "}";
5115 ast_dump_context
->ostream() << std::endl
;
5118 // Make a select statement.
5121 Statement::make_select_statement(Location location
)
5123 return new Select_statement(location
);
5126 // Class For_statement.
5131 For_statement::do_traverse(Traverse
* traverse
)
5133 if (this->init_
!= NULL
)
5135 if (this->init_
->traverse(traverse
) == TRAVERSE_EXIT
)
5136 return TRAVERSE_EXIT
;
5138 if (this->cond_
!= NULL
)
5140 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
)
5141 return TRAVERSE_EXIT
;
5143 if (this->post_
!= NULL
)
5145 if (this->post_
->traverse(traverse
) == TRAVERSE_EXIT
)
5146 return TRAVERSE_EXIT
;
5148 return this->statements_
->traverse(traverse
);
5151 // Lower a For_statement into if statements and gotos. Getting rid of
5152 // complex statements make it easier to handle garbage collection.
5155 For_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
5156 Statement_inserter
*)
5159 Location loc
= this->location();
5161 Block
* b
= new Block(enclosing
, this->location());
5162 if (this->init_
!= NULL
)
5164 s
= Statement::make_block_statement(this->init_
,
5165 this->init_
->start_location());
5166 b
->add_statement(s
);
5169 Unnamed_label
* entry
= NULL
;
5170 if (this->cond_
!= NULL
)
5172 entry
= new Unnamed_label(this->location());
5173 b
->add_statement(Statement::make_goto_unnamed_statement(entry
, loc
));
5176 Unnamed_label
* top
= new Unnamed_label(this->location());
5177 b
->add_statement(Statement::make_unnamed_label_statement(top
));
5179 s
= Statement::make_block_statement(this->statements_
,
5180 this->statements_
->start_location());
5181 b
->add_statement(s
);
5183 Location end_loc
= this->statements_
->end_location();
5185 Unnamed_label
* cont
= this->continue_label_
;
5187 b
->add_statement(Statement::make_unnamed_label_statement(cont
));
5189 if (this->post_
!= NULL
)
5191 s
= Statement::make_block_statement(this->post_
,
5192 this->post_
->start_location());
5193 b
->add_statement(s
);
5194 end_loc
= this->post_
->end_location();
5197 if (this->cond_
== NULL
)
5198 b
->add_statement(Statement::make_goto_unnamed_statement(top
, end_loc
));
5201 b
->add_statement(Statement::make_unnamed_label_statement(entry
));
5203 Location cond_loc
= this->cond_
->location();
5204 Block
* then_block
= new Block(b
, cond_loc
);
5205 s
= Statement::make_goto_unnamed_statement(top
, cond_loc
);
5206 then_block
->add_statement(s
);
5208 s
= Statement::make_if_statement(this->cond_
, then_block
, NULL
, cond_loc
);
5209 b
->add_statement(s
);
5212 Unnamed_label
* brk
= this->break_label_
;
5214 b
->add_statement(Statement::make_unnamed_label_statement(brk
));
5216 b
->set_end_location(end_loc
);
5218 return Statement::make_block_statement(b
, loc
);
5221 // Return the break label, creating it if necessary.
5224 For_statement::break_label()
5226 if (this->break_label_
== NULL
)
5227 this->break_label_
= new Unnamed_label(this->location());
5228 return this->break_label_
;
5231 // Return the continue LABEL_EXPR.
5234 For_statement::continue_label()
5236 if (this->continue_label_
== NULL
)
5237 this->continue_label_
= new Unnamed_label(this->location());
5238 return this->continue_label_
;
5241 // Set the break and continue labels a for statement. This is used
5242 // when lowering a for range statement.
5245 For_statement::set_break_continue_labels(Unnamed_label
* break_label
,
5246 Unnamed_label
* continue_label
)
5248 go_assert(this->break_label_
== NULL
&& this->continue_label_
== NULL
);
5249 this->break_label_
= break_label
;
5250 this->continue_label_
= continue_label
;
5253 // Whether the overall statement may fall through.
5256 For_statement::do_may_fall_through() const
5258 // A for loop is terminating if it has no condition and
5259 // no break statement.
5260 if(this->cond_
!= NULL
)
5262 if(this->break_label_
!= NULL
)
5267 // Dump the AST representation for a for statement.
5270 For_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5272 if (this->init_
!= NULL
&& ast_dump_context
->dump_subblocks())
5274 ast_dump_context
->print_indent();
5275 ast_dump_context
->indent();
5276 ast_dump_context
->ostream() << "// INIT " << std::endl
;
5277 ast_dump_context
->dump_block(this->init_
);
5278 ast_dump_context
->unindent();
5280 ast_dump_context
->print_indent();
5281 ast_dump_context
->ostream() << "for ";
5282 if (this->cond_
!= NULL
)
5283 ast_dump_context
->dump_expression(this->cond_
);
5285 if (ast_dump_context
->dump_subblocks())
5287 ast_dump_context
->ostream() << " {" << std::endl
;
5288 ast_dump_context
->dump_block(this->statements_
);
5289 if (this->init_
!= NULL
)
5291 ast_dump_context
->print_indent();
5292 ast_dump_context
->ostream() << "// POST " << std::endl
;
5293 ast_dump_context
->dump_block(this->post_
);
5295 ast_dump_context
->unindent();
5297 ast_dump_context
->print_indent();
5298 ast_dump_context
->ostream() << "}";
5301 ast_dump_context
->ostream() << std::endl
;
5304 // Make a for statement.
5307 Statement::make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
5310 return new For_statement(init
, cond
, post
, location
);
5313 // Class For_range_statement.
5318 For_range_statement::do_traverse(Traverse
* traverse
)
5320 if (this->index_var_
!= NULL
)
5322 if (this->traverse_expression(traverse
, &this->index_var_
)
5324 return TRAVERSE_EXIT
;
5326 if (this->value_var_
!= NULL
)
5328 if (this->traverse_expression(traverse
, &this->value_var_
)
5330 return TRAVERSE_EXIT
;
5332 if (this->traverse_expression(traverse
, &this->range_
) == TRAVERSE_EXIT
)
5333 return TRAVERSE_EXIT
;
5334 return this->statements_
->traverse(traverse
);
5337 // Lower a for range statement. For simplicity we lower this into a
5338 // for statement, which will then be lowered in turn to goto
5342 For_range_statement::do_lower(Gogo
* gogo
, Named_object
*, Block
* enclosing
,
5343 Statement_inserter
*)
5345 Type
* range_type
= this->range_
->type();
5346 if (range_type
->points_to() != NULL
5347 && range_type
->points_to()->array_type() != NULL
5348 && !range_type
->points_to()->is_slice_type())
5349 range_type
= range_type
->points_to();
5352 Type
* value_type
= NULL
;
5353 if (range_type
->array_type() != NULL
)
5355 index_type
= Type::lookup_integer_type("int");
5356 value_type
= range_type
->array_type()->element_type();
5358 else if (range_type
->is_string_type())
5360 index_type
= Type::lookup_integer_type("int");
5361 value_type
= Type::lookup_integer_type("int32");
5363 else if (range_type
->map_type() != NULL
)
5365 index_type
= range_type
->map_type()->key_type();
5366 value_type
= range_type
->map_type()->val_type();
5368 else if (range_type
->channel_type() != NULL
)
5370 index_type
= range_type
->channel_type()->element_type();
5371 if (this->value_var_
!= NULL
)
5373 if (!this->value_var_
->type()->is_error())
5374 this->report_error(_("too many variables for range clause "
5376 return Statement::make_error_statement(this->location());
5381 this->report_error(_("range clause must have "
5382 "array, slice, string, map, or channel type"));
5383 return Statement::make_error_statement(this->location());
5386 Location loc
= this->location();
5387 Block
* temp_block
= new Block(enclosing
, loc
);
5389 Named_object
* range_object
= NULL
;
5390 Temporary_statement
* range_temp
= NULL
;
5391 Var_expression
* ve
= this->range_
->var_expression();
5393 range_object
= ve
->named_object();
5396 range_temp
= Statement::make_temporary(NULL
, this->range_
, loc
);
5397 temp_block
->add_statement(range_temp
);
5398 this->range_
= NULL
;
5401 Temporary_statement
* index_temp
= Statement::make_temporary(index_type
,
5403 temp_block
->add_statement(index_temp
);
5405 Temporary_statement
* value_temp
= NULL
;
5406 if (this->value_var_
!= NULL
)
5408 value_temp
= Statement::make_temporary(value_type
, NULL
, loc
);
5409 temp_block
->add_statement(value_temp
);
5412 Block
* body
= new Block(temp_block
, loc
);
5419 // Arrange to do a loop appropriate for the type. We will produce
5420 // for INIT ; COND ; POST {
5422 // INDEX = INDEX_TEMP
5423 // VALUE = VALUE_TEMP // If there is a value
5424 // original statements
5427 if (range_type
->is_slice_type())
5428 this->lower_range_slice(gogo
, temp_block
, body
, range_object
, range_temp
,
5429 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5431 else if (range_type
->array_type() != NULL
)
5432 this->lower_range_array(gogo
, temp_block
, body
, range_object
, range_temp
,
5433 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5435 else if (range_type
->is_string_type())
5436 this->lower_range_string(gogo
, temp_block
, body
, range_object
, range_temp
,
5437 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5439 else if (range_type
->map_type() != NULL
)
5440 this->lower_range_map(gogo
, temp_block
, body
, range_object
, range_temp
,
5441 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5443 else if (range_type
->channel_type() != NULL
)
5444 this->lower_range_channel(gogo
, temp_block
, body
, range_object
, range_temp
,
5445 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5450 if (iter_init
!= NULL
)
5451 body
->add_statement(Statement::make_block_statement(iter_init
, loc
));
5453 if (this->index_var_
!= NULL
)
5456 Expression
* index_ref
=
5457 Expression::make_temporary_reference(index_temp
, loc
);
5458 if (this->value_var_
== NULL
)
5459 assign
= Statement::make_assignment(this->index_var_
, index_ref
, loc
);
5462 Expression_list
* lhs
= new Expression_list();
5463 lhs
->push_back(this->index_var_
);
5464 lhs
->push_back(this->value_var_
);
5466 Expression_list
* rhs
= new Expression_list();
5467 rhs
->push_back(index_ref
);
5468 rhs
->push_back(Expression::make_temporary_reference(value_temp
, loc
));
5470 assign
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5472 body
->add_statement(assign
);
5475 body
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
5477 body
->set_end_location(this->statements_
->end_location());
5479 For_statement
* loop
= Statement::make_for_statement(init
, cond
, post
,
5481 loop
->add_statements(body
);
5482 loop
->set_break_continue_labels(this->break_label_
, this->continue_label_
);
5484 temp_block
->add_statement(loop
);
5486 return Statement::make_block_statement(temp_block
, loc
);
5489 // Return a reference to the range, which may be in RANGE_OBJECT or in
5493 For_range_statement::make_range_ref(Named_object
* range_object
,
5494 Temporary_statement
* range_temp
,
5497 if (range_object
!= NULL
)
5498 return Expression::make_var_reference(range_object
, loc
);
5500 return Expression::make_temporary_reference(range_temp
, loc
);
5503 // Return a call to the predeclared function FUNCNAME passing a
5504 // reference to the temporary variable ARG.
5507 For_range_statement::call_builtin(Gogo
* gogo
, const char* funcname
,
5511 Named_object
* no
= gogo
->lookup_global(funcname
);
5512 go_assert(no
!= NULL
&& no
->is_function_declaration());
5513 Expression
* func
= Expression::make_func_reference(no
, NULL
, loc
);
5514 Expression_list
* params
= new Expression_list();
5515 params
->push_back(arg
);
5516 return Expression::make_call(func
, params
, false, loc
);
5519 // Lower a for range over an array.
5522 For_range_statement::lower_range_array(Gogo
* gogo
,
5525 Named_object
* range_object
,
5526 Temporary_statement
* range_temp
,
5527 Temporary_statement
* index_temp
,
5528 Temporary_statement
* value_temp
,
5534 Location loc
= this->location();
5536 // The loop we generate:
5537 // len_temp := len(range)
5538 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5539 // value_temp = range[index_temp]
5540 // index = index_temp
5541 // value = value_temp
5547 // len_temp = len(range)
5550 Block
* init
= new Block(enclosing
, loc
);
5552 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5553 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5554 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5556 init
->add_statement(len_temp
);
5558 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, loc
);
5560 Temporary_reference_expression
* tref
=
5561 Expression::make_temporary_reference(index_temp
, loc
);
5562 tref
->set_is_lvalue();
5563 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5564 init
->add_statement(s
);
5569 // index_temp < len_temp
5571 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5572 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5573 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5577 // Set *PITER_INIT to
5578 // value_temp = range[index_temp]
5580 Block
* iter_init
= NULL
;
5581 if (value_temp
!= NULL
)
5583 iter_init
= new Block(body_block
, loc
);
5585 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5586 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5587 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, NULL
, loc
);
5589 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5590 tref
->set_is_lvalue();
5591 s
= Statement::make_assignment(tref
, index
, loc
);
5593 iter_init
->add_statement(s
);
5595 *piter_init
= iter_init
;
5600 Block
* post
= new Block(enclosing
, loc
);
5601 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5602 tref
->set_is_lvalue();
5603 s
= Statement::make_inc_statement(tref
);
5604 post
->add_statement(s
);
5608 // Lower a for range over a slice.
5611 For_range_statement::lower_range_slice(Gogo
* gogo
,
5614 Named_object
* range_object
,
5615 Temporary_statement
* range_temp
,
5616 Temporary_statement
* index_temp
,
5617 Temporary_statement
* value_temp
,
5623 Location loc
= this->location();
5625 // The loop we generate:
5626 // for_temp := range
5627 // len_temp := len(for_temp)
5628 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5629 // value_temp = for_temp[index_temp]
5630 // index = index_temp
5631 // value = value_temp
5635 // Using for_temp means that we don't need to check bounds when
5636 // fetching range_temp[index_temp].
5639 // range_temp := range
5641 // len_temp = len(range_temp)
5644 Block
* init
= new Block(enclosing
, loc
);
5646 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5647 Temporary_statement
* for_temp
= Statement::make_temporary(NULL
, ref
, loc
);
5648 init
->add_statement(for_temp
);
5650 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5651 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5652 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5654 init
->add_statement(len_temp
);
5656 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, loc
);
5658 Temporary_reference_expression
* tref
=
5659 Expression::make_temporary_reference(index_temp
, loc
);
5660 tref
->set_is_lvalue();
5661 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5662 init
->add_statement(s
);
5667 // index_temp < len_temp
5669 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5670 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5671 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5675 // Set *PITER_INIT to
5676 // value_temp = range[index_temp]
5678 Block
* iter_init
= NULL
;
5679 if (value_temp
!= NULL
)
5681 iter_init
= new Block(body_block
, loc
);
5683 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5684 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5685 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, NULL
, loc
);
5687 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5688 tref
->set_is_lvalue();
5689 s
= Statement::make_assignment(tref
, index
, loc
);
5691 iter_init
->add_statement(s
);
5693 *piter_init
= iter_init
;
5698 Block
* post
= new Block(enclosing
, loc
);
5699 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5700 tref
->set_is_lvalue();
5701 s
= Statement::make_inc_statement(tref
);
5702 post
->add_statement(s
);
5706 // Lower a for range over a string.
5709 For_range_statement::lower_range_string(Gogo
*,
5712 Named_object
* range_object
,
5713 Temporary_statement
* range_temp
,
5714 Temporary_statement
* index_temp
,
5715 Temporary_statement
* value_temp
,
5721 Location loc
= this->location();
5723 // The loop we generate:
5724 // var next_index_temp int
5725 // for index_temp = 0; ; index_temp = next_index_temp {
5726 // next_index_temp, value_temp = stringiter2(range, index_temp)
5727 // if next_index_temp == 0 {
5730 // index = index_temp
5731 // value = value_temp
5736 // var next_index_temp int
5739 Block
* init
= new Block(enclosing
, loc
);
5741 Temporary_statement
* next_index_temp
=
5742 Statement::make_temporary(index_temp
->type(), NULL
, loc
);
5743 init
->add_statement(next_index_temp
);
5745 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, loc
);
5747 Temporary_reference_expression
* ref
=
5748 Expression::make_temporary_reference(index_temp
, loc
);
5749 ref
->set_is_lvalue();
5750 Statement
* s
= Statement::make_assignment(ref
, zexpr
, loc
);
5752 init
->add_statement(s
);
5755 // The loop has no condition.
5759 // Set *PITER_INIT to
5760 // next_index_temp = runtime.stringiter(range, index_temp)
5762 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
5764 // if next_index_temp == 0 {
5768 Block
* iter_init
= new Block(body_block
, loc
);
5770 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5771 Expression
* p2
= Expression::make_temporary_reference(index_temp
, loc
);
5772 Call_expression
* call
= Runtime::make_call((value_temp
== NULL
5773 ? Runtime::STRINGITER
5774 : Runtime::STRINGITER2
),
5777 if (value_temp
== NULL
)
5779 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5780 ref
->set_is_lvalue();
5781 s
= Statement::make_assignment(ref
, call
, loc
);
5785 Expression_list
* lhs
= new Expression_list();
5787 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5788 ref
->set_is_lvalue();
5789 lhs
->push_back(ref
);
5791 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5792 ref
->set_is_lvalue();
5793 lhs
->push_back(ref
);
5795 Expression_list
* rhs
= new Expression_list();
5796 rhs
->push_back(Expression::make_call_result(call
, 0));
5797 rhs
->push_back(Expression::make_call_result(call
, 1));
5799 s
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5801 iter_init
->add_statement(s
);
5803 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5804 zexpr
= Expression::make_integer_ul(0, NULL
, loc
);
5805 Expression
* equals
= Expression::make_binary(OPERATOR_EQEQ
, ref
, zexpr
, loc
);
5807 Block
* then_block
= new Block(iter_init
, loc
);
5808 s
= Statement::make_break_statement(this->break_label(), loc
);
5809 then_block
->add_statement(s
);
5811 s
= Statement::make_if_statement(equals
, then_block
, NULL
, loc
);
5812 iter_init
->add_statement(s
);
5814 *piter_init
= iter_init
;
5817 // index_temp = next_index_temp
5819 Block
* post
= new Block(enclosing
, loc
);
5821 Temporary_reference_expression
* lhs
=
5822 Expression::make_temporary_reference(index_temp
, loc
);
5823 lhs
->set_is_lvalue();
5824 Expression
* rhs
= Expression::make_temporary_reference(next_index_temp
, loc
);
5825 s
= Statement::make_assignment(lhs
, rhs
, loc
);
5827 post
->add_statement(s
);
5831 // Lower a for range over a map.
5834 For_range_statement::lower_range_map(Gogo
*,
5837 Named_object
* range_object
,
5838 Temporary_statement
* range_temp
,
5839 Temporary_statement
* index_temp
,
5840 Temporary_statement
* value_temp
,
5846 Location loc
= this->location();
5848 // The runtime uses a struct to handle ranges over a map. The
5849 // struct is four pointers long. The first pointer is NULL when we
5850 // have completed the iteration.
5852 // The loop we generate:
5853 // var hiter map_iteration_struct
5854 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
5855 // mapiter2(hiter, &index_temp, &value_temp)
5856 // index = index_temp
5857 // value = value_temp
5862 // var hiter map_iteration_struct
5863 // runtime.mapiterinit(range, &hiter)
5865 Block
* init
= new Block(enclosing
, loc
);
5867 Type
* map_iteration_type
= Runtime::map_iteration_type();
5868 Temporary_statement
* hiter
= Statement::make_temporary(map_iteration_type
,
5870 init
->add_statement(hiter
);
5872 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5873 Expression
* ref
= Expression::make_temporary_reference(hiter
, loc
);
5874 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5875 Expression
* call
= Runtime::make_call(Runtime::MAPITERINIT
, loc
, 2, p1
, p2
);
5876 init
->add_statement(Statement::make_statement(call
, true));
5883 ref
= Expression::make_temporary_reference(hiter
, loc
);
5884 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, loc
);
5885 Expression
* index
= Expression::make_index(ref
, zexpr
, NULL
, NULL
, loc
);
5886 Expression
* ne
= Expression::make_binary(OPERATOR_NOTEQ
, index
,
5887 Expression::make_nil(loc
),
5891 // Set *PITER_INIT to
5892 // mapiter1(hiter, &index_temp)
5894 // mapiter2(hiter, &index_temp, &value_temp)
5896 Block
* iter_init
= new Block(body_block
, loc
);
5898 ref
= Expression::make_temporary_reference(hiter
, loc
);
5899 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5900 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5901 p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5902 if (value_temp
== NULL
)
5903 call
= Runtime::make_call(Runtime::MAPITER1
, loc
, 2, p1
, p2
);
5906 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5907 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5908 call
= Runtime::make_call(Runtime::MAPITER2
, loc
, 3, p1
, p2
, p3
);
5910 iter_init
->add_statement(Statement::make_statement(call
, true));
5912 *piter_init
= iter_init
;
5915 // mapiternext(&hiter)
5917 Block
* post
= new Block(enclosing
, loc
);
5919 ref
= Expression::make_temporary_reference(hiter
, loc
);
5920 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5921 call
= Runtime::make_call(Runtime::MAPITERNEXT
, loc
, 1, p1
);
5922 post
->add_statement(Statement::make_statement(call
, true));
5927 // Lower a for range over a channel.
5930 For_range_statement::lower_range_channel(Gogo
*,
5933 Named_object
* range_object
,
5934 Temporary_statement
* range_temp
,
5935 Temporary_statement
* index_temp
,
5936 Temporary_statement
* value_temp
,
5942 go_assert(value_temp
== NULL
);
5944 Location loc
= this->location();
5946 // The loop we generate:
5948 // index_temp, ok_temp = <-range
5952 // index = index_temp
5956 // We have no initialization code, no condition, and no post code.
5962 // Set *PITER_INIT to
5963 // index_temp, ok_temp = <-range
5968 Block
* iter_init
= new Block(body_block
, loc
);
5970 Temporary_statement
* ok_temp
=
5971 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
5972 iter_init
->add_statement(ok_temp
);
5974 Expression
* cref
= this->make_range_ref(range_object
, range_temp
, loc
);
5975 Temporary_reference_expression
* iref
=
5976 Expression::make_temporary_reference(index_temp
, loc
);
5977 iref
->set_is_lvalue();
5978 Temporary_reference_expression
* oref
=
5979 Expression::make_temporary_reference(ok_temp
, loc
);
5980 oref
->set_is_lvalue();
5981 Statement
* s
= Statement::make_tuple_receive_assignment(iref
, oref
, cref
,
5983 iter_init
->add_statement(s
);
5985 Block
* then_block
= new Block(iter_init
, loc
);
5986 s
= Statement::make_break_statement(this->break_label(), loc
);
5987 then_block
->add_statement(s
);
5989 oref
= Expression::make_temporary_reference(ok_temp
, loc
);
5990 Expression
* cond
= Expression::make_unary(OPERATOR_NOT
, oref
, loc
);
5991 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
5992 iter_init
->add_statement(s
);
5994 *piter_init
= iter_init
;
5997 // Return the break LABEL_EXPR.
6000 For_range_statement::break_label()
6002 if (this->break_label_
== NULL
)
6003 this->break_label_
= new Unnamed_label(this->location());
6004 return this->break_label_
;
6007 // Return the continue LABEL_EXPR.
6010 For_range_statement::continue_label()
6012 if (this->continue_label_
== NULL
)
6013 this->continue_label_
= new Unnamed_label(this->location());
6014 return this->continue_label_
;
6017 // Dump the AST representation for a for range statement.
6020 For_range_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
6023 ast_dump_context
->print_indent();
6024 ast_dump_context
->ostream() << "for ";
6025 ast_dump_context
->dump_expression(this->index_var_
);
6026 if (this->value_var_
!= NULL
)
6028 ast_dump_context
->ostream() << ", ";
6029 ast_dump_context
->dump_expression(this->value_var_
);
6032 ast_dump_context
->ostream() << " = range ";
6033 ast_dump_context
->dump_expression(this->range_
);
6034 if (ast_dump_context
->dump_subblocks())
6036 ast_dump_context
->ostream() << " {" << std::endl
;
6038 ast_dump_context
->indent();
6040 ast_dump_context
->dump_block(this->statements_
);
6042 ast_dump_context
->unindent();
6043 ast_dump_context
->print_indent();
6044 ast_dump_context
->ostream() << "}";
6046 ast_dump_context
->ostream() << std::endl
;
6049 // Make a for statement with a range clause.
6051 For_range_statement
*
6052 Statement::make_for_range_statement(Expression
* index_var
,
6053 Expression
* value_var
,
6057 return new For_range_statement(index_var
, value_var
, range
, location
);