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 // Convert a variable declaration to the backend representation.
252 Variable_declaration_statement::do_get_backend(Translate_context
* context
)
254 Variable
* var
= this->var_
->var_value();
255 Bvariable
* bvar
= this->var_
->get_backend_variable(context
->gogo(),
256 context
->function());
257 tree init
= var
->get_init_tree(context
->gogo(), context
->function());
258 Bexpression
* binit
= init
== NULL
? NULL
: tree_to_expr(init
);
260 if (!var
->is_in_heap())
262 go_assert(binit
!= NULL
);
263 return context
->backend()->init_statement(bvar
, binit
);
266 // Something takes the address of this variable, so the value is
267 // stored in the heap. Initialize it to newly allocated memory
268 // space, and assign the initial value to the new space.
269 Location loc
= this->location();
270 Named_object
* newfn
= context
->gogo()->lookup_global("new");
271 go_assert(newfn
!= NULL
&& newfn
->is_function_declaration());
272 Expression
* func
= Expression::make_func_reference(newfn
, NULL
, loc
);
273 Expression_list
* params
= new Expression_list();
274 params
->push_back(Expression::make_type(var
->type(), loc
));
275 Expression
* call
= Expression::make_call(func
, params
, false, loc
);
276 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
277 Temporary_statement
* temp
= Statement::make_temporary(NULL
, call
, loc
);
278 Bstatement
* btemp
= temp
->get_backend(context
);
280 Bstatement
* set
= NULL
;
283 Expression
* e
= Expression::make_temporary_reference(temp
, loc
);
284 e
= Expression::make_unary(OPERATOR_MULT
, e
, loc
);
285 Bexpression
* be
= tree_to_expr(e
->get_tree(context
));
286 set
= context
->backend()->assignment_statement(be
, binit
, loc
);
289 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
290 Bexpression
* bref
= tree_to_expr(ref
->get_tree(context
));
291 Bstatement
* sinit
= context
->backend()->init_statement(bvar
, bref
);
293 std::vector
<Bstatement
*> stats
;
295 stats
.push_back(btemp
);
297 stats
.push_back(set
);
298 stats
.push_back(sinit
);
299 return context
->backend()->statement_list(stats
);
302 // Dump the AST representation for a variable declaration.
305 Variable_declaration_statement::do_dump_statement(
306 Ast_dump_context
* ast_dump_context
) const
308 ast_dump_context
->print_indent();
310 go_assert(var_
->is_variable());
311 ast_dump_context
->ostream() << "var " << this->var_
->name() << " ";
312 Variable
* var
= this->var_
->var_value();
315 ast_dump_context
->dump_type(var
->type());
316 ast_dump_context
->ostream() << " ";
318 if (var
->init() != NULL
)
320 ast_dump_context
->ostream() << "= ";
321 ast_dump_context
->dump_expression(var
->init());
323 ast_dump_context
->ostream() << std::endl
;
326 // Make a variable declaration.
329 Statement::make_variable_declaration(Named_object
* var
)
331 return new Variable_declaration_statement(var
);
334 // Class Temporary_statement.
336 // Return the type of the temporary variable.
339 Temporary_statement::type() const
341 return this->type_
!= NULL
? this->type_
: this->init_
->type();
347 Temporary_statement::do_traverse(Traverse
* traverse
)
349 if (this->type_
!= NULL
350 && this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
351 return TRAVERSE_EXIT
;
352 if (this->init_
== NULL
)
353 return TRAVERSE_CONTINUE
;
355 return this->traverse_expression(traverse
, &this->init_
);
358 // Traverse assignments.
361 Temporary_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
363 if (this->init_
== NULL
)
365 tassign
->value(&this->init_
, true, true);
372 Temporary_statement::do_determine_types()
374 if (this->type_
!= NULL
&& this->type_
->is_abstract())
375 this->type_
= this->type_
->make_non_abstract_type();
377 if (this->init_
!= NULL
)
379 if (this->type_
== NULL
)
380 this->init_
->determine_type_no_context();
383 Type_context
context(this->type_
, false);
384 this->init_
->determine_type(&context
);
388 if (this->type_
== NULL
)
390 this->type_
= this->init_
->type();
391 go_assert(!this->type_
->is_abstract());
398 Temporary_statement::do_check_types(Gogo
*)
400 if (this->type_
!= NULL
&& this->init_
!= NULL
)
404 if (this->are_hidden_fields_ok_
)
405 ok
= Type::are_assignable_hidden_ok(this->type_
, this->init_
->type(),
408 ok
= Type::are_assignable(this->type_
, this->init_
->type(), &reason
);
412 error_at(this->location(), "incompatible types in assignment");
414 error_at(this->location(), "incompatible types in assignment (%s)",
416 this->set_is_error();
421 // Convert to backend representation.
424 Temporary_statement::do_get_backend(Translate_context
* context
)
426 go_assert(this->bvariable_
== NULL
);
428 // FIXME: Permitting FUNCTION to be NULL here is a temporary measure
429 // until we have a better representation of the init function.
430 Named_object
* function
= context
->function();
431 Bfunction
* bfunction
;
432 if (function
== NULL
)
435 bfunction
= tree_to_function(function
->func_value()->get_decl());
437 Btype
* btype
= this->type()->get_backend(context
->gogo());
440 if (this->init_
== NULL
)
442 else if (this->type_
== NULL
)
443 binit
= tree_to_expr(this->init_
->get_tree(context
));
446 Expression
* init
= Expression::make_cast(this->type_
, this->init_
,
448 context
->gogo()->lower_expression(context
->function(), NULL
, &init
);
449 binit
= tree_to_expr(init
->get_tree(context
));
452 Bstatement
* statement
;
454 context
->backend()->temporary_variable(bfunction
, context
->bblock(),
456 this->is_address_taken_
,
457 this->location(), &statement
);
461 // Return the backend variable.
464 Temporary_statement::get_backend_variable(Translate_context
* context
) const
466 if (this->bvariable_
== NULL
)
468 go_assert(saw_errors());
469 return context
->backend()->error_variable();
471 return this->bvariable_
;
474 // Dump the AST represemtation for a temporary statement
477 Temporary_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
479 ast_dump_context
->print_indent();
480 ast_dump_context
->dump_temp_variable_name(this);
481 if (this->type_
!= NULL
)
483 ast_dump_context
->ostream() << " ";
484 ast_dump_context
->dump_type(this->type_
);
486 if (this->init_
!= NULL
)
488 ast_dump_context
->ostream() << " = ";
489 ast_dump_context
->dump_expression(this->init_
);
491 ast_dump_context
->ostream() << std::endl
;
494 // Make and initialize a temporary variable in BLOCK.
497 Statement::make_temporary(Type
* type
, Expression
* init
,
500 return new Temporary_statement(type
, init
, location
);
503 // An assignment statement.
505 class Assignment_statement
: public Statement
508 Assignment_statement(Expression
* lhs
, Expression
* rhs
,
510 : Statement(STATEMENT_ASSIGNMENT
, location
),
511 lhs_(lhs
), rhs_(rhs
), are_hidden_fields_ok_(false)
514 // Note that it is OK for this assignment statement to set hidden
517 set_hidden_fields_are_ok()
518 { this->are_hidden_fields_ok_
= true; }
522 do_traverse(Traverse
* traverse
);
525 do_traverse_assignments(Traverse_assignments
*);
528 do_determine_types();
531 do_check_types(Gogo
*);
534 do_get_backend(Translate_context
*);
537 do_dump_statement(Ast_dump_context
*) const;
540 // Left hand side--the lvalue.
542 // Right hand side--the rvalue.
544 // True if this statement may set hidden fields in the assignment
545 // statement. This is used for generated method stubs.
546 bool are_hidden_fields_ok_
;
552 Assignment_statement::do_traverse(Traverse
* traverse
)
554 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
555 return TRAVERSE_EXIT
;
556 return this->traverse_expression(traverse
, &this->rhs_
);
560 Assignment_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
562 tassign
->assignment(&this->lhs_
, &this->rhs_
);
566 // Set types for the assignment.
569 Assignment_statement::do_determine_types()
571 this->lhs_
->determine_type_no_context();
572 Type_context
context(this->lhs_
->type(), false);
573 this->rhs_
->determine_type(&context
);
576 // Check types for an assignment.
579 Assignment_statement::do_check_types(Gogo
*)
581 // The left hand side must be either addressable, a map index
582 // expression, or the blank identifier.
583 if (!this->lhs_
->is_addressable()
584 && this->lhs_
->map_index_expression() == NULL
585 && !this->lhs_
->is_sink_expression())
587 if (!this->lhs_
->type()->is_error())
588 this->report_error(_("invalid left hand side of assignment"));
592 Type
* lhs_type
= this->lhs_
->type();
593 Type
* rhs_type
= this->rhs_
->type();
596 if (this->are_hidden_fields_ok_
)
597 ok
= Type::are_assignable_hidden_ok(lhs_type
, rhs_type
, &reason
);
599 ok
= Type::are_assignable(lhs_type
, rhs_type
, &reason
);
603 error_at(this->location(), "incompatible types in assignment");
605 error_at(this->location(), "incompatible types in assignment (%s)",
607 this->set_is_error();
610 if (lhs_type
->is_error() || rhs_type
->is_error())
611 this->set_is_error();
614 // Convert an assignment statement to the backend representation.
617 Assignment_statement::do_get_backend(Translate_context
* context
)
619 tree rhs_tree
= this->rhs_
->get_tree(context
);
620 if (this->lhs_
->is_sink_expression())
621 return context
->backend()->expression_statement(tree_to_expr(rhs_tree
));
622 tree lhs_tree
= this->lhs_
->get_tree(context
);
623 rhs_tree
= Expression::convert_for_assignment(context
, this->lhs_
->type(),
624 this->rhs_
->type(), rhs_tree
,
626 return context
->backend()->assignment_statement(tree_to_expr(lhs_tree
),
627 tree_to_expr(rhs_tree
),
631 // Dump the AST representation for an assignment statement.
634 Assignment_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
637 ast_dump_context
->print_indent();
638 ast_dump_context
->dump_expression(this->lhs_
);
639 ast_dump_context
->ostream() << " = " ;
640 ast_dump_context
->dump_expression(this->rhs_
);
641 ast_dump_context
->ostream() << std::endl
;
644 // Make an assignment statement.
647 Statement::make_assignment(Expression
* lhs
, Expression
* rhs
,
650 return new Assignment_statement(lhs
, rhs
, location
);
653 // The Move_subexpressions class is used to move all top-level
654 // subexpressions of an expression. This is used for things like
655 // index expressions in which we must evaluate the index value before
656 // it can be changed by a multiple assignment.
658 class Move_subexpressions
: public Traverse
661 Move_subexpressions(int skip
, Block
* block
)
662 : Traverse(traverse_expressions
),
663 skip_(skip
), block_(block
)
668 expression(Expression
**);
671 // The number of subexpressions to skip moving. This is used to
672 // avoid moving the array itself, as we only need to move the index.
674 // The block where new temporary variables should be added.
679 Move_subexpressions::expression(Expression
** pexpr
)
683 else if ((*pexpr
)->temporary_reference_expression() == NULL
)
685 Location loc
= (*pexpr
)->location();
686 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
687 this->block_
->add_statement(temp
);
688 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
690 // We only need to move top-level subexpressions.
691 return TRAVERSE_SKIP_COMPONENTS
;
694 // The Move_ordered_evals class is used to find any subexpressions of
695 // an expression that have an evaluation order dependency. It creates
696 // temporary variables to hold them.
698 class Move_ordered_evals
: public Traverse
701 Move_ordered_evals(Block
* block
)
702 : Traverse(traverse_expressions
),
708 expression(Expression
**);
711 // The block where new temporary variables should be added.
716 Move_ordered_evals::expression(Expression
** pexpr
)
718 // We have to look at subexpressions first.
719 if ((*pexpr
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
720 return TRAVERSE_EXIT
;
723 if ((*pexpr
)->must_eval_subexpressions_in_order(&i
))
725 Move_subexpressions
ms(i
, this->block_
);
726 if ((*pexpr
)->traverse_subexpressions(&ms
) == TRAVERSE_EXIT
)
727 return TRAVERSE_EXIT
;
730 if ((*pexpr
)->must_eval_in_order())
732 Location loc
= (*pexpr
)->location();
733 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
734 this->block_
->add_statement(temp
);
735 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
737 return TRAVERSE_SKIP_COMPONENTS
;
740 // An assignment operation statement.
742 class Assignment_operation_statement
: public Statement
745 Assignment_operation_statement(Operator op
, Expression
* lhs
, Expression
* rhs
,
747 : Statement(STATEMENT_ASSIGNMENT_OPERATION
, location
),
748 op_(op
), lhs_(lhs
), rhs_(rhs
)
753 do_traverse(Traverse
*);
756 do_traverse_assignments(Traverse_assignments
*)
757 { go_unreachable(); }
760 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
763 do_get_backend(Translate_context
*)
764 { go_unreachable(); }
767 do_dump_statement(Ast_dump_context
*) const;
770 // The operator (OPERATOR_PLUSEQ, etc.).
781 Assignment_operation_statement::do_traverse(Traverse
* traverse
)
783 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
784 return TRAVERSE_EXIT
;
785 return this->traverse_expression(traverse
, &this->rhs_
);
788 // Lower an assignment operation statement to a regular assignment
792 Assignment_operation_statement::do_lower(Gogo
*, Named_object
*,
793 Block
* enclosing
, Statement_inserter
*)
795 Location loc
= this->location();
797 // We have to evaluate the left hand side expression only once. We
798 // do this by moving out any expression with side effects.
799 Block
* b
= new Block(enclosing
, loc
);
800 Move_ordered_evals
moe(b
);
801 this->lhs_
->traverse_subexpressions(&moe
);
803 Expression
* lval
= this->lhs_
->copy();
808 case OPERATOR_PLUSEQ
:
811 case OPERATOR_MINUSEQ
:
820 case OPERATOR_MULTEQ
:
829 case OPERATOR_LSHIFTEQ
:
830 op
= OPERATOR_LSHIFT
;
832 case OPERATOR_RSHIFTEQ
:
833 op
= OPERATOR_RSHIFT
;
838 case OPERATOR_BITCLEAREQ
:
839 op
= OPERATOR_BITCLEAR
;
845 Expression
* binop
= Expression::make_binary(op
, lval
, this->rhs_
, loc
);
846 Statement
* s
= Statement::make_assignment(this->lhs_
, binop
, loc
);
847 if (b
->statements()->empty())
855 return Statement::make_block_statement(b
, loc
);
859 // Dump the AST representation for an assignment operation statement
862 Assignment_operation_statement::do_dump_statement(
863 Ast_dump_context
* ast_dump_context
) const
865 ast_dump_context
->print_indent();
866 ast_dump_context
->dump_expression(this->lhs_
);
867 ast_dump_context
->dump_operator(this->op_
);
868 ast_dump_context
->dump_expression(this->rhs_
);
869 ast_dump_context
->ostream() << std::endl
;
872 // Make an assignment operation statement.
875 Statement::make_assignment_operation(Operator op
, Expression
* lhs
,
876 Expression
* rhs
, Location location
)
878 return new Assignment_operation_statement(op
, lhs
, rhs
, location
);
881 // A tuple assignment statement. This differs from an assignment
882 // statement in that the right-hand-side expressions are evaluated in
885 class Tuple_assignment_statement
: public Statement
888 Tuple_assignment_statement(Expression_list
* lhs
, Expression_list
* rhs
,
890 : Statement(STATEMENT_TUPLE_ASSIGNMENT
, location
),
891 lhs_(lhs
), rhs_(rhs
), are_hidden_fields_ok_(false)
894 // Note that it is OK for this assignment statement to set hidden
897 set_hidden_fields_are_ok()
898 { this->are_hidden_fields_ok_
= true; }
902 do_traverse(Traverse
* traverse
);
905 do_traverse_assignments(Traverse_assignments
*)
906 { go_unreachable(); }
909 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
912 do_get_backend(Translate_context
*)
913 { go_unreachable(); }
916 do_dump_statement(Ast_dump_context
*) const;
919 // Left hand side--a list of lvalues.
920 Expression_list
* lhs_
;
921 // Right hand side--a list of rvalues.
922 Expression_list
* rhs_
;
923 // True if this statement may set hidden fields in the assignment
924 // statement. This is used for generated method stubs.
925 bool are_hidden_fields_ok_
;
931 Tuple_assignment_statement::do_traverse(Traverse
* traverse
)
933 if (this->traverse_expression_list(traverse
, this->lhs_
) == TRAVERSE_EXIT
)
934 return TRAVERSE_EXIT
;
935 return this->traverse_expression_list(traverse
, this->rhs_
);
938 // Lower a tuple assignment. We use temporary variables to split it
939 // up into a set of single assignments.
942 Tuple_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
945 Location loc
= this->location();
947 Block
* b
= new Block(enclosing
, loc
);
949 // First move out any subexpressions on the left hand side. The
950 // right hand side will be evaluated in the required order anyhow.
951 Move_ordered_evals
moe(b
);
952 for (Expression_list::iterator plhs
= this->lhs_
->begin();
953 plhs
!= this->lhs_
->end();
955 Expression::traverse(&*plhs
, &moe
);
957 std::vector
<Temporary_statement
*> temps
;
958 temps
.reserve(this->lhs_
->size());
960 Expression_list::const_iterator prhs
= this->rhs_
->begin();
961 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
962 plhs
!= this->lhs_
->end();
965 go_assert(prhs
!= this->rhs_
->end());
967 if ((*plhs
)->is_error_expression()
968 || (*plhs
)->type()->is_error()
969 || (*prhs
)->is_error_expression()
970 || (*prhs
)->type()->is_error())
973 if ((*plhs
)->is_sink_expression())
975 b
->add_statement(Statement::make_statement(*prhs
, true));
979 Temporary_statement
* temp
= Statement::make_temporary((*plhs
)->type(),
981 if (this->are_hidden_fields_ok_
)
982 temp
->set_hidden_fields_are_ok();
983 b
->add_statement(temp
);
984 temps
.push_back(temp
);
987 go_assert(prhs
== this->rhs_
->end());
989 prhs
= this->rhs_
->begin();
990 std::vector
<Temporary_statement
*>::const_iterator ptemp
= temps
.begin();
991 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
992 plhs
!= this->lhs_
->end();
995 if ((*plhs
)->is_error_expression()
996 || (*plhs
)->type()->is_error()
997 || (*prhs
)->is_error_expression()
998 || (*prhs
)->type()->is_error())
1001 if ((*plhs
)->is_sink_expression())
1004 Expression
* ref
= Expression::make_temporary_reference(*ptemp
, loc
);
1005 Statement
* s
= Statement::make_assignment(*plhs
, ref
, loc
);
1006 if (this->are_hidden_fields_ok_
)
1008 Assignment_statement
* as
= static_cast<Assignment_statement
*>(s
);
1009 as
->set_hidden_fields_are_ok();
1011 b
->add_statement(s
);
1014 go_assert(ptemp
== temps
.end() || saw_errors());
1016 return Statement::make_block_statement(b
, loc
);
1019 // Dump the AST representation for a tuple assignment statement.
1022 Tuple_assignment_statement::do_dump_statement(
1023 Ast_dump_context
* ast_dump_context
) const
1025 ast_dump_context
->print_indent();
1026 ast_dump_context
->dump_expression_list(this->lhs_
);
1027 ast_dump_context
->ostream() << " = ";
1028 ast_dump_context
->dump_expression_list(this->rhs_
);
1029 ast_dump_context
->ostream() << std::endl
;
1032 // Make a tuple assignment statement.
1035 Statement::make_tuple_assignment(Expression_list
* lhs
, Expression_list
* rhs
,
1038 return new Tuple_assignment_statement(lhs
, rhs
, location
);
1041 // A tuple assignment from a map index expression.
1044 class Tuple_map_assignment_statement
: public Statement
1047 Tuple_map_assignment_statement(Expression
* val
, Expression
* present
,
1048 Expression
* map_index
,
1050 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT
, location
),
1051 val_(val
), present_(present
), map_index_(map_index
)
1056 do_traverse(Traverse
* traverse
);
1059 do_traverse_assignments(Traverse_assignments
*)
1060 { go_unreachable(); }
1063 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1066 do_get_backend(Translate_context
*)
1067 { go_unreachable(); }
1070 do_dump_statement(Ast_dump_context
*) const;
1073 // Lvalue which receives the value from the map.
1075 // Lvalue which receives whether the key value was present.
1076 Expression
* present_
;
1077 // The map index expression.
1078 Expression
* map_index_
;
1084 Tuple_map_assignment_statement::do_traverse(Traverse
* traverse
)
1086 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1087 || this->traverse_expression(traverse
, &this->present_
) == TRAVERSE_EXIT
)
1088 return TRAVERSE_EXIT
;
1089 return this->traverse_expression(traverse
, &this->map_index_
);
1092 // Lower a tuple map assignment.
1095 Tuple_map_assignment_statement::do_lower(Gogo
*, Named_object
*,
1096 Block
* enclosing
, Statement_inserter
*)
1098 Location loc
= this->location();
1100 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1101 if (map_index
== NULL
)
1103 this->report_error(_("expected map index on right hand side"));
1104 return Statement::make_error_statement(loc
);
1106 Map_type
* map_type
= map_index
->get_map_type();
1107 if (map_type
== NULL
)
1108 return Statement::make_error_statement(loc
);
1110 Block
* b
= new Block(enclosing
, loc
);
1112 // Move out any subexpressions to make sure that functions are
1113 // called in the required order.
1114 Move_ordered_evals
moe(b
);
1115 this->val_
->traverse_subexpressions(&moe
);
1116 this->present_
->traverse_subexpressions(&moe
);
1118 // Copy the key value into a temporary so that we can take its
1119 // address without pushing the value onto the heap.
1121 // var key_temp KEY_TYPE = MAP_INDEX
1122 Temporary_statement
* key_temp
=
1123 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1124 b
->add_statement(key_temp
);
1126 // var val_temp VAL_TYPE
1127 Temporary_statement
* val_temp
=
1128 Statement::make_temporary(map_type
->val_type(), NULL
, loc
);
1129 b
->add_statement(val_temp
);
1131 // var present_temp bool
1132 Temporary_statement
* present_temp
=
1133 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
1134 b
->add_statement(present_temp
);
1136 // present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp, &val_temp)
1137 Expression
* a1
= Expression::make_type_descriptor(map_type
, loc
);
1138 Expression
* a2
= map_index
->map();
1139 Temporary_reference_expression
* ref
=
1140 Expression::make_temporary_reference(key_temp
, loc
);
1141 Expression
* a3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1142 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1143 Expression
* a4
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1144 Expression
* call
= Runtime::make_call(Runtime::MAPACCESS2
, loc
, 4,
1147 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1148 ref
->set_is_lvalue();
1149 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1150 b
->add_statement(s
);
1153 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1154 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1155 b
->add_statement(s
);
1157 // present = present_temp
1158 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1159 s
= Statement::make_assignment(this->present_
, ref
, loc
);
1160 b
->add_statement(s
);
1162 return Statement::make_block_statement(b
, loc
);
1165 // Dump the AST representation for a tuple map assignment statement.
1168 Tuple_map_assignment_statement::do_dump_statement(
1169 Ast_dump_context
* ast_dump_context
) const
1171 ast_dump_context
->print_indent();
1172 ast_dump_context
->dump_expression(this->val_
);
1173 ast_dump_context
->ostream() << ", ";
1174 ast_dump_context
->dump_expression(this->present_
);
1175 ast_dump_context
->ostream() << " = ";
1176 ast_dump_context
->dump_expression(this->map_index_
);
1177 ast_dump_context
->ostream() << std::endl
;
1180 // Make a map assignment statement which returns a pair of values.
1183 Statement::make_tuple_map_assignment(Expression
* val
, Expression
* present
,
1184 Expression
* map_index
,
1187 return new Tuple_map_assignment_statement(val
, present
, map_index
, location
);
1190 // Assign a pair of entries to a map.
1193 class Map_assignment_statement
: public Statement
1196 Map_assignment_statement(Expression
* map_index
,
1197 Expression
* val
, Expression
* should_set
,
1199 : Statement(STATEMENT_MAP_ASSIGNMENT
, location
),
1200 map_index_(map_index
), val_(val
), should_set_(should_set
)
1205 do_traverse(Traverse
* traverse
);
1208 do_traverse_assignments(Traverse_assignments
*)
1209 { go_unreachable(); }
1212 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1215 do_get_backend(Translate_context
*)
1216 { go_unreachable(); }
1219 do_dump_statement(Ast_dump_context
*) const;
1222 // A reference to the map index which should be set or deleted.
1223 Expression
* map_index_
;
1224 // The value to add to the map.
1226 // Whether or not to add the value.
1227 Expression
* should_set_
;
1230 // Traverse a map assignment.
1233 Map_assignment_statement::do_traverse(Traverse
* traverse
)
1235 if (this->traverse_expression(traverse
, &this->map_index_
) == TRAVERSE_EXIT
1236 || this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
1237 return TRAVERSE_EXIT
;
1238 return this->traverse_expression(traverse
, &this->should_set_
);
1241 // Lower a map assignment to a function call.
1244 Map_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
1245 Statement_inserter
*)
1247 Location loc
= this->location();
1249 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1250 if (map_index
== NULL
)
1252 this->report_error(_("expected map index on left hand side"));
1253 return Statement::make_error_statement(loc
);
1255 Map_type
* map_type
= map_index
->get_map_type();
1256 if (map_type
== NULL
)
1257 return Statement::make_error_statement(loc
);
1259 Block
* b
= new Block(enclosing
, loc
);
1261 // Evaluate the map first to get order of evaluation right.
1262 // map_temp := m // we are evaluating m[k] = v, p
1263 Temporary_statement
* map_temp
= Statement::make_temporary(map_type
,
1266 b
->add_statement(map_temp
);
1268 // var key_temp MAP_KEY_TYPE = k
1269 Temporary_statement
* key_temp
=
1270 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1271 b
->add_statement(key_temp
);
1273 // var val_temp MAP_VAL_TYPE = v
1274 Temporary_statement
* val_temp
=
1275 Statement::make_temporary(map_type
->val_type(), this->val_
, loc
);
1276 b
->add_statement(val_temp
);
1278 // var insert_temp bool = p
1279 Temporary_statement
* insert_temp
=
1280 Statement::make_temporary(Type::lookup_bool_type(), this->should_set_
,
1282 b
->add_statement(insert_temp
);
1284 // mapassign2(map_temp, &key_temp, &val_temp, p)
1285 Expression
* p1
= Expression::make_temporary_reference(map_temp
, loc
);
1286 Expression
* ref
= Expression::make_temporary_reference(key_temp
, loc
);
1287 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1288 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1289 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1290 Expression
* p4
= Expression::make_temporary_reference(insert_temp
, loc
);
1291 Expression
* call
= Runtime::make_call(Runtime::MAPASSIGN2
, loc
, 4,
1293 Statement
* s
= Statement::make_statement(call
, true);
1294 b
->add_statement(s
);
1296 return Statement::make_block_statement(b
, loc
);
1299 // Dump the AST representation for a map assignment statement.
1302 Map_assignment_statement::do_dump_statement(
1303 Ast_dump_context
* ast_dump_context
) const
1305 ast_dump_context
->print_indent();
1306 ast_dump_context
->dump_expression(this->map_index_
);
1307 ast_dump_context
->ostream() << " = ";
1308 ast_dump_context
->dump_expression(this->val_
);
1309 ast_dump_context
->ostream() << ", ";
1310 ast_dump_context
->dump_expression(this->should_set_
);
1311 ast_dump_context
->ostream() << std::endl
;
1314 // Make a statement which assigns a pair of entries to a map.
1317 Statement::make_map_assignment(Expression
* map_index
,
1318 Expression
* val
, Expression
* should_set
,
1321 return new Map_assignment_statement(map_index
, val
, should_set
, location
);
1324 // A tuple assignment from a receive statement.
1326 class Tuple_receive_assignment_statement
: public Statement
1329 Tuple_receive_assignment_statement(Expression
* val
, Expression
* closed
,
1330 Expression
* channel
, Location location
)
1331 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
, location
),
1332 val_(val
), closed_(closed
), channel_(channel
)
1337 do_traverse(Traverse
* traverse
);
1340 do_traverse_assignments(Traverse_assignments
*)
1341 { go_unreachable(); }
1344 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1347 do_get_backend(Translate_context
*)
1348 { go_unreachable(); }
1351 do_dump_statement(Ast_dump_context
*) const;
1354 // Lvalue which receives the value from the channel.
1356 // Lvalue which receives whether the channel is closed.
1357 Expression
* closed_
;
1358 // The channel on which we receive the value.
1359 Expression
* channel_
;
1365 Tuple_receive_assignment_statement::do_traverse(Traverse
* traverse
)
1367 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1368 || this->traverse_expression(traverse
, &this->closed_
) == TRAVERSE_EXIT
)
1369 return TRAVERSE_EXIT
;
1370 return this->traverse_expression(traverse
, &this->channel_
);
1373 // Lower to a function call.
1376 Tuple_receive_assignment_statement::do_lower(Gogo
*, Named_object
*,
1378 Statement_inserter
*)
1380 Location loc
= this->location();
1382 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
1383 if (channel_type
== NULL
)
1385 this->report_error(_("expected channel"));
1386 return Statement::make_error_statement(loc
);
1388 if (!channel_type
->may_receive())
1390 this->report_error(_("invalid receive on send-only channel"));
1391 return Statement::make_error_statement(loc
);
1394 Block
* b
= new Block(enclosing
, loc
);
1396 // Make sure that any subexpressions on the left hand side are
1397 // evaluated in the right order.
1398 Move_ordered_evals
moe(b
);
1399 this->val_
->traverse_subexpressions(&moe
);
1400 this->closed_
->traverse_subexpressions(&moe
);
1402 // var val_temp ELEMENT_TYPE
1403 Temporary_statement
* val_temp
=
1404 Statement::make_temporary(channel_type
->element_type(), NULL
, loc
);
1405 b
->add_statement(val_temp
);
1407 // var closed_temp bool
1408 Temporary_statement
* closed_temp
=
1409 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
1410 b
->add_statement(closed_temp
);
1412 // closed_temp = chanrecv2(type, channel, &val_temp)
1413 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
1415 Temporary_reference_expression
* ref
=
1416 Expression::make_temporary_reference(val_temp
, loc
);
1417 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1418 Expression
* call
= Runtime::make_call(Runtime::CHANRECV2
,
1419 loc
, 3, td
, this->channel_
, p2
);
1420 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1421 ref
->set_is_lvalue();
1422 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1423 b
->add_statement(s
);
1426 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1427 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1428 b
->add_statement(s
);
1430 // closed = closed_temp
1431 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1432 s
= Statement::make_assignment(this->closed_
, ref
, loc
);
1433 b
->add_statement(s
);
1435 return Statement::make_block_statement(b
, loc
);
1438 // Dump the AST representation for a tuple receive statement.
1441 Tuple_receive_assignment_statement::do_dump_statement(
1442 Ast_dump_context
* ast_dump_context
) const
1444 ast_dump_context
->print_indent();
1445 ast_dump_context
->dump_expression(this->val_
);
1446 ast_dump_context
->ostream() << ", ";
1447 ast_dump_context
->dump_expression(this->closed_
);
1448 ast_dump_context
->ostream() << " <- ";
1449 ast_dump_context
->dump_expression(this->channel_
);
1450 ast_dump_context
->ostream() << std::endl
;
1453 // Make a nonblocking receive statement.
1456 Statement::make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
1457 Expression
* channel
,
1460 return new Tuple_receive_assignment_statement(val
, closed
, channel
,
1464 // An assignment to a pair of values from a type guard. This is a
1465 // conditional type guard. v, ok = i.(type).
1467 class Tuple_type_guard_assignment_statement
: public Statement
1470 Tuple_type_guard_assignment_statement(Expression
* val
, Expression
* ok
,
1471 Expression
* expr
, Type
* type
,
1473 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
, location
),
1474 val_(val
), ok_(ok
), expr_(expr
), type_(type
)
1479 do_traverse(Traverse
*);
1482 do_traverse_assignments(Traverse_assignments
*)
1483 { go_unreachable(); }
1486 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1489 do_get_backend(Translate_context
*)
1490 { go_unreachable(); }
1493 do_dump_statement(Ast_dump_context
*) const;
1497 lower_to_type(Runtime::Function
);
1500 lower_to_object_type(Block
*, Runtime::Function
);
1502 // The variable which recieves the converted value.
1504 // The variable which receives the indication of success.
1506 // The expression being converted.
1508 // The type to which the expression is being converted.
1512 // Traverse a type guard tuple assignment.
1515 Tuple_type_guard_assignment_statement::do_traverse(Traverse
* traverse
)
1517 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1518 || this->traverse_expression(traverse
, &this->ok_
) == TRAVERSE_EXIT
1519 || this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
1520 return TRAVERSE_EXIT
;
1521 return this->traverse_expression(traverse
, &this->expr_
);
1524 // Lower to a function call.
1527 Tuple_type_guard_assignment_statement::do_lower(Gogo
*, Named_object
*,
1529 Statement_inserter
*)
1531 Location loc
= this->location();
1533 Type
* expr_type
= this->expr_
->type();
1534 if (expr_type
->interface_type() == NULL
)
1536 if (!expr_type
->is_error() && !this->type_
->is_error())
1537 this->report_error(_("type assertion only valid for interface types"));
1538 return Statement::make_error_statement(loc
);
1541 Block
* b
= new Block(enclosing
, loc
);
1543 // Make sure that any subexpressions on the left hand side are
1544 // evaluated in the right order.
1545 Move_ordered_evals
moe(b
);
1546 this->val_
->traverse_subexpressions(&moe
);
1547 this->ok_
->traverse_subexpressions(&moe
);
1549 bool expr_is_empty
= expr_type
->interface_type()->is_empty();
1550 Call_expression
* call
;
1551 if (this->type_
->interface_type() != NULL
)
1553 if (this->type_
->interface_type()->is_empty())
1554 call
= Runtime::make_call((expr_is_empty
1555 ? Runtime::IFACEE2E2
1556 : Runtime::IFACEI2E2
),
1557 loc
, 1, this->expr_
);
1559 call
= this->lower_to_type(expr_is_empty
1560 ? Runtime::IFACEE2I2
1561 : Runtime::IFACEI2I2
);
1563 else if (this->type_
->points_to() != NULL
)
1564 call
= this->lower_to_type(expr_is_empty
1565 ? Runtime::IFACEE2T2P
1566 : Runtime::IFACEI2T2P
);
1569 this->lower_to_object_type(b
,
1571 ? Runtime::IFACEE2T2
1572 : Runtime::IFACEI2T2
));
1578 Expression
* res
= Expression::make_call_result(call
, 0);
1579 res
= Expression::make_unsafe_cast(this->type_
, res
, loc
);
1580 Statement
* s
= Statement::make_assignment(this->val_
, res
, loc
);
1581 b
->add_statement(s
);
1583 res
= Expression::make_call_result(call
, 1);
1584 s
= Statement::make_assignment(this->ok_
, res
, loc
);
1585 b
->add_statement(s
);
1588 return Statement::make_block_statement(b
, loc
);
1591 // Lower a conversion to a non-empty interface type or a pointer type.
1594 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code
)
1596 Location loc
= this->location();
1597 return Runtime::make_call(code
, loc
, 2,
1598 Expression::make_type_descriptor(this->type_
, loc
),
1602 // Lower a conversion to a non-interface non-pointer type.
1605 Tuple_type_guard_assignment_statement::lower_to_object_type(
1607 Runtime::Function code
)
1609 Location loc
= this->location();
1611 // var val_temp TYPE
1612 Temporary_statement
* val_temp
= Statement::make_temporary(this->type_
,
1614 b
->add_statement(val_temp
);
1616 // ok = CODE(type_descriptor, expr, &val_temp)
1617 Expression
* p1
= Expression::make_type_descriptor(this->type_
, loc
);
1618 Expression
* ref
= Expression::make_temporary_reference(val_temp
, loc
);
1619 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1620 Expression
* call
= Runtime::make_call(code
, loc
, 3, p1
, this->expr_
, p3
);
1621 Statement
* s
= Statement::make_assignment(this->ok_
, call
, loc
);
1622 b
->add_statement(s
);
1625 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1626 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1627 b
->add_statement(s
);
1630 // Dump the AST representation for a tuple type guard statement.
1633 Tuple_type_guard_assignment_statement::do_dump_statement(
1634 Ast_dump_context
* ast_dump_context
) const
1636 ast_dump_context
->print_indent();
1637 ast_dump_context
->dump_expression(this->val_
);
1638 ast_dump_context
->ostream() << ", ";
1639 ast_dump_context
->dump_expression(this->ok_
);
1640 ast_dump_context
->ostream() << " = ";
1641 ast_dump_context
->dump_expression(this->expr_
);
1642 ast_dump_context
->ostream() << " . ";
1643 ast_dump_context
->dump_type(this->type_
);
1644 ast_dump_context
->ostream() << std::endl
;
1647 // Make an assignment from a type guard to a pair of variables.
1650 Statement::make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
1651 Expression
* expr
, Type
* type
,
1654 return new Tuple_type_guard_assignment_statement(val
, ok
, expr
, type
,
1658 // An expression statement.
1660 class Expression_statement
: public Statement
1663 Expression_statement(Expression
* expr
, bool is_ignored
)
1664 : Statement(STATEMENT_EXPRESSION
, expr
->location()),
1665 expr_(expr
), is_ignored_(is_ignored
)
1670 { return this->expr_
; }
1674 do_traverse(Traverse
* traverse
)
1675 { return this->traverse_expression(traverse
, &this->expr_
); }
1678 do_determine_types()
1679 { this->expr_
->determine_type_no_context(); }
1682 do_check_types(Gogo
*);
1685 do_may_fall_through() const;
1688 do_get_backend(Translate_context
* context
);
1691 do_dump_statement(Ast_dump_context
*) const;
1695 // Whether the value of this expression is being explicitly ignored.
1699 // Check the types of an expression statement. The only check we do
1700 // is to possibly give an error about discarding the value of the
1704 Expression_statement::do_check_types(Gogo
*)
1706 if (!this->is_ignored_
)
1707 this->expr_
->discarding_value();
1710 // An expression statement may fall through unless it is a call to a
1711 // function which does not return.
1714 Expression_statement::do_may_fall_through() const
1716 const Call_expression
* call
= this->expr_
->call_expression();
1719 const Expression
* fn
= call
->fn();
1720 const Func_expression
* fe
= fn
->func_expression();
1723 const Named_object
* no
= fe
->named_object();
1725 Function_type
* fntype
;
1726 if (no
->is_function())
1727 fntype
= no
->func_value()->type();
1728 else if (no
->is_function_declaration())
1729 fntype
= no
->func_declaration_value()->type();
1733 // The builtin function panic does not return.
1734 if (fntype
!= NULL
&& fntype
->is_builtin() && no
->name() == "panic")
1741 // Convert to backend representation.
1744 Expression_statement::do_get_backend(Translate_context
* context
)
1746 tree expr_tree
= this->expr_
->get_tree(context
);
1747 return context
->backend()->expression_statement(tree_to_expr(expr_tree
));
1750 // Dump the AST representation for an expression statement
1753 Expression_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
1756 ast_dump_context
->print_indent();
1757 ast_dump_context
->dump_expression(expr_
);
1758 ast_dump_context
->ostream() << std::endl
;
1761 // Make an expression statement from an Expression.
1764 Statement::make_statement(Expression
* expr
, bool is_ignored
)
1766 return new Expression_statement(expr
, is_ignored
);
1769 // A block statement--a list of statements which may include variable
1772 class Block_statement
: public Statement
1775 Block_statement(Block
* block
, Location location
)
1776 : Statement(STATEMENT_BLOCK
, location
),
1782 do_traverse(Traverse
* traverse
)
1783 { return this->block_
->traverse(traverse
); }
1786 do_determine_types()
1787 { this->block_
->determine_types(); }
1790 do_may_fall_through() const
1791 { return this->block_
->may_fall_through(); }
1794 do_get_backend(Translate_context
* context
);
1797 do_dump_statement(Ast_dump_context
*) const;
1803 // Convert a block to the backend representation of a statement.
1806 Block_statement::do_get_backend(Translate_context
* context
)
1808 Bblock
* bblock
= this->block_
->get_backend(context
);
1809 return context
->backend()->block_statement(bblock
);
1812 // Dump the AST for a block statement
1815 Block_statement::do_dump_statement(Ast_dump_context
*) const
1817 // block statement braces are dumped when traversing.
1820 // Make a block statement.
1823 Statement::make_block_statement(Block
* block
, Location location
)
1825 return new Block_statement(block
, location
);
1828 // An increment or decrement statement.
1830 class Inc_dec_statement
: public Statement
1833 Inc_dec_statement(bool is_inc
, Expression
* expr
)
1834 : Statement(STATEMENT_INCDEC
, expr
->location()),
1835 expr_(expr
), is_inc_(is_inc
)
1840 do_traverse(Traverse
* traverse
)
1841 { return this->traverse_expression(traverse
, &this->expr_
); }
1844 do_traverse_assignments(Traverse_assignments
*)
1845 { go_unreachable(); }
1848 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1851 do_get_backend(Translate_context
*)
1852 { go_unreachable(); }
1855 do_dump_statement(Ast_dump_context
*) const;
1858 // The l-value to increment or decrement.
1860 // Whether to increment or decrement.
1864 // Lower to += or -=.
1867 Inc_dec_statement::do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
1869 Location loc
= this->location();
1872 mpz_init_set_ui(oval
, 1UL);
1873 Expression
* oexpr
= Expression::make_integer(&oval
, NULL
, loc
);
1876 Operator op
= this->is_inc_
? OPERATOR_PLUSEQ
: OPERATOR_MINUSEQ
;
1877 return Statement::make_assignment_operation(op
, this->expr_
, oexpr
, loc
);
1880 // Dump the AST representation for a inc/dec statement.
1883 Inc_dec_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
1885 ast_dump_context
->print_indent();
1886 ast_dump_context
->dump_expression(expr_
);
1887 ast_dump_context
->ostream() << (is_inc_
? "++": "--") << std::endl
;
1890 // Make an increment statement.
1893 Statement::make_inc_statement(Expression
* expr
)
1895 return new Inc_dec_statement(true, expr
);
1898 // Make a decrement statement.
1901 Statement::make_dec_statement(Expression
* expr
)
1903 return new Inc_dec_statement(false, expr
);
1906 // Class Thunk_statement. This is the base class for go and defer
1911 Thunk_statement::Thunk_statement(Statement_classification classification
,
1912 Call_expression
* call
,
1914 : Statement(classification
, location
),
1915 call_(call
), struct_type_(NULL
)
1919 // Return whether this is a simple statement which does not require a
1923 Thunk_statement::is_simple(Function_type
* fntype
) const
1925 // We need a thunk to call a method, or to pass a variable number of
1927 if (fntype
->is_method() || fntype
->is_varargs())
1930 // A defer statement requires a thunk to set up for whether the
1931 // function can call recover.
1932 if (this->classification() == STATEMENT_DEFER
)
1935 // We can only permit a single parameter of pointer type.
1936 const Typed_identifier_list
* parameters
= fntype
->parameters();
1937 if (parameters
!= NULL
1938 && (parameters
->size() > 1
1939 || (parameters
->size() == 1
1940 && parameters
->begin()->type()->points_to() == NULL
)))
1943 // If the function returns multiple values, or returns a type other
1944 // than integer, floating point, or pointer, then it may get a
1945 // hidden first parameter, in which case we need the more
1946 // complicated approach. This is true even though we are going to
1947 // ignore the return value.
1948 const Typed_identifier_list
* results
= fntype
->results();
1950 && (results
->size() > 1
1951 || (results
->size() == 1
1952 && !results
->begin()->type()->is_basic_type()
1953 && results
->begin()->type()->points_to() == NULL
)))
1956 // If this calls something which is not a simple function, then we
1958 Expression
* fn
= this->call_
->call_expression()->fn();
1959 if (fn
->interface_field_reference_expression() != NULL
)
1965 // Traverse a thunk statement.
1968 Thunk_statement::do_traverse(Traverse
* traverse
)
1970 return this->traverse_expression(traverse
, &this->call_
);
1973 // We implement traverse_assignment for a thunk statement because it
1974 // effectively copies the function call.
1977 Thunk_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
1979 Expression
* fn
= this->call_
->call_expression()->fn();
1980 Expression
* fn2
= fn
;
1981 tassign
->value(&fn2
, true, false);
1985 // Determine types in a thunk statement.
1988 Thunk_statement::do_determine_types()
1990 this->call_
->determine_type_no_context();
1992 // Now that we know the types of the call, build the struct used to
1994 Call_expression
* ce
= this->call_
->call_expression();
1997 Function_type
* fntype
= ce
->get_function_type();
1998 if (fntype
!= NULL
&& !this->is_simple(fntype
))
1999 this->struct_type_
= this->build_struct(fntype
);
2002 // Check types in a thunk statement.
2005 Thunk_statement::do_check_types(Gogo
*)
2007 if (!this->call_
->discarding_value())
2009 Call_expression
* ce
= this->call_
->call_expression();
2012 if (!this->call_
->is_error_expression())
2013 this->report_error("expected call expression");
2018 // The Traverse class used to find and simplify thunk statements.
2020 class Simplify_thunk_traverse
: public Traverse
2023 Simplify_thunk_traverse(Gogo
* gogo
)
2024 : Traverse(traverse_functions
| traverse_blocks
),
2025 gogo_(gogo
), function_(NULL
)
2029 function(Named_object
*);
2037 // The function we are traversing.
2038 Named_object
* function_
;
2041 // Keep track of the current function while looking for thunks.
2044 Simplify_thunk_traverse::function(Named_object
* no
)
2046 go_assert(this->function_
== NULL
);
2047 this->function_
= no
;
2048 int t
= no
->func_value()->traverse(this);
2049 this->function_
= NULL
;
2050 if (t
== TRAVERSE_EXIT
)
2052 return TRAVERSE_SKIP_COMPONENTS
;
2055 // Look for thunks in a block.
2058 Simplify_thunk_traverse::block(Block
* b
)
2060 // The parser ensures that thunk statements always appear at the end
2062 if (b
->statements()->size() < 1)
2063 return TRAVERSE_CONTINUE
;
2064 Thunk_statement
* stat
= b
->statements()->back()->thunk_statement();
2066 return TRAVERSE_CONTINUE
;
2067 if (stat
->simplify_statement(this->gogo_
, this->function_
, b
))
2068 return TRAVERSE_SKIP_COMPONENTS
;
2069 return TRAVERSE_CONTINUE
;
2072 // Simplify all thunk statements.
2075 Gogo::simplify_thunk_statements()
2077 Simplify_thunk_traverse
thunk_traverse(this);
2078 this->traverse(&thunk_traverse
);
2081 // Return true if the thunk function is a constant, which means that
2082 // it does not need to be passed to the thunk routine.
2085 Thunk_statement::is_constant_function() const
2087 Call_expression
* ce
= this->call_
->call_expression();
2088 Function_type
* fntype
= ce
->get_function_type();
2091 go_assert(saw_errors());
2094 if (fntype
->is_builtin())
2096 Expression
* fn
= ce
->fn();
2097 if (fn
->func_expression() != NULL
)
2098 return fn
->func_expression()->closure() == NULL
;
2099 if (fn
->interface_field_reference_expression() != NULL
)
2104 // Simplify complex thunk statements into simple ones. A complicated
2105 // thunk statement is one which takes anything other than zero
2106 // parameters or a single pointer parameter. We rewrite it into code
2107 // which allocates a struct, stores the parameter values into the
2108 // struct, and does a simple go or defer statement which passes the
2109 // struct to a thunk. The thunk does the real call.
2112 Thunk_statement::simplify_statement(Gogo
* gogo
, Named_object
* function
,
2115 if (this->classification() == STATEMENT_ERROR
)
2117 if (this->call_
->is_error_expression())
2120 if (this->classification() == STATEMENT_DEFER
)
2122 // Make sure that the defer stack exists for the function. We
2123 // will use when converting this statement to the backend
2124 // representation, but we want it to exist when we start
2125 // converting the function.
2126 function
->func_value()->defer_stack(this->location());
2129 Call_expression
* ce
= this->call_
->call_expression();
2130 Function_type
* fntype
= ce
->get_function_type();
2133 go_assert(saw_errors());
2134 this->set_is_error();
2137 if (this->is_simple(fntype
))
2140 Expression
* fn
= ce
->fn();
2141 Interface_field_reference_expression
* interface_method
=
2142 fn
->interface_field_reference_expression();
2144 Location location
= this->location();
2146 std::string thunk_name
= Gogo::thunk_name();
2149 this->build_thunk(gogo
, thunk_name
);
2151 // Generate code to call the thunk.
2153 // Get the values to store into the struct which is the single
2154 // argument to the thunk.
2156 Expression_list
* vals
= new Expression_list();
2157 if (!this->is_constant_function())
2158 vals
->push_back(fn
);
2160 if (interface_method
!= NULL
)
2161 vals
->push_back(interface_method
->expr());
2163 if (ce
->args() != NULL
)
2165 for (Expression_list::const_iterator p
= ce
->args()->begin();
2166 p
!= ce
->args()->end();
2168 vals
->push_back(*p
);
2171 // Build the struct.
2172 Expression
* constructor
=
2173 Expression::make_struct_composite_literal(this->struct_type_
, vals
,
2176 // Allocate the initialized struct on the heap.
2177 constructor
= Expression::make_heap_composite(constructor
, location
);
2179 // Look up the thunk.
2180 Named_object
* named_thunk
= gogo
->lookup(thunk_name
, NULL
);
2181 go_assert(named_thunk
!= NULL
&& named_thunk
->is_function());
2184 Expression
* func
= Expression::make_func_reference(named_thunk
, NULL
,
2186 Expression_list
* params
= new Expression_list();
2187 params
->push_back(constructor
);
2188 Call_expression
* call
= Expression::make_call(func
, params
, false, location
);
2190 // Build the simple go or defer statement.
2192 if (this->classification() == STATEMENT_GO
)
2193 s
= Statement::make_go_statement(call
, location
);
2194 else if (this->classification() == STATEMENT_DEFER
)
2195 s
= Statement::make_defer_statement(call
, location
);
2199 // The current block should end with the go statement.
2200 go_assert(block
->statements()->size() >= 1);
2201 go_assert(block
->statements()->back() == this);
2202 block
->replace_statement(block
->statements()->size() - 1, s
);
2204 // We already ran the determine_types pass, so we need to run it now
2205 // for the new statement.
2206 s
->determine_types();
2209 gogo
->check_types_in_block(block
);
2211 // Return true to tell the block not to keep looking at statements.
2215 // Set the name to use for thunk parameter N.
2218 Thunk_statement::thunk_field_param(int n
, char* buf
, size_t buflen
)
2220 snprintf(buf
, buflen
, "a%d", n
);
2223 // Build a new struct type to hold the parameters for a complicated
2224 // thunk statement. FNTYPE is the type of the function call.
2227 Thunk_statement::build_struct(Function_type
* fntype
)
2229 Location location
= this->location();
2231 Struct_field_list
* fields
= new Struct_field_list();
2233 Call_expression
* ce
= this->call_
->call_expression();
2234 Expression
* fn
= ce
->fn();
2236 if (!this->is_constant_function())
2238 // The function to call.
2239 fields
->push_back(Struct_field(Typed_identifier("fn", fntype
,
2243 // If this thunk statement calls a method on an interface, we pass
2244 // the interface object to the thunk.
2245 Interface_field_reference_expression
* interface_method
=
2246 fn
->interface_field_reference_expression();
2247 if (interface_method
!= NULL
)
2249 Typed_identifier
tid("object", interface_method
->expr()->type(),
2251 fields
->push_back(Struct_field(tid
));
2254 // The predeclared recover function has no argument. However, we
2255 // add an argument when building recover thunks. Handle that here.
2256 if (ce
->is_recover_call())
2258 fields
->push_back(Struct_field(Typed_identifier("can_recover",
2259 Type::lookup_bool_type(),
2263 const Expression_list
* args
= ce
->args();
2267 for (Expression_list::const_iterator p
= args
->begin();
2272 this->thunk_field_param(i
, buf
, sizeof buf
);
2273 fields
->push_back(Struct_field(Typed_identifier(buf
, (*p
)->type(),
2278 return Type::make_struct_type(fields
, location
);
2281 // Build the thunk we are going to call. This is a brand new, albeit
2282 // artificial, function.
2285 Thunk_statement::build_thunk(Gogo
* gogo
, const std::string
& thunk_name
)
2287 Location location
= this->location();
2289 Call_expression
* ce
= this->call_
->call_expression();
2291 bool may_call_recover
= false;
2292 if (this->classification() == STATEMENT_DEFER
)
2294 Func_expression
* fn
= ce
->fn()->func_expression();
2296 may_call_recover
= true;
2299 const Named_object
* no
= fn
->named_object();
2300 if (!no
->is_function())
2301 may_call_recover
= true;
2303 may_call_recover
= no
->func_value()->calls_recover();
2307 // Build the type of the thunk. The thunk takes a single parameter,
2308 // which is a pointer to the special structure we build.
2309 const char* const parameter_name
= "__go_thunk_parameter";
2310 Typed_identifier_list
* thunk_parameters
= new Typed_identifier_list();
2311 Type
* pointer_to_struct_type
= Type::make_pointer_type(this->struct_type_
);
2312 thunk_parameters
->push_back(Typed_identifier(parameter_name
,
2313 pointer_to_struct_type
,
2316 Typed_identifier_list
* thunk_results
= NULL
;
2317 if (may_call_recover
)
2319 // When deferring a function which may call recover, add a
2320 // return value, to disable tail call optimizations which will
2321 // break the way we check whether recover is permitted.
2322 thunk_results
= new Typed_identifier_list();
2323 thunk_results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
2327 Function_type
* thunk_type
= Type::make_function_type(NULL
, thunk_parameters
,
2331 // Start building the thunk.
2332 Named_object
* function
= gogo
->start_function(thunk_name
, thunk_type
, true,
2335 gogo
->start_block(location
);
2337 // For a defer statement, start with a call to
2338 // __go_set_defer_retaddr. */
2339 Label
* retaddr_label
= NULL
;
2340 if (may_call_recover
)
2342 retaddr_label
= gogo
->add_label_reference("retaddr", location
, false);
2343 Expression
* arg
= Expression::make_label_addr(retaddr_label
, location
);
2344 Expression
* call
= Runtime::make_call(Runtime::SET_DEFER_RETADDR
,
2347 // This is a hack to prevent the middle-end from deleting the
2349 gogo
->start_block(location
);
2350 gogo
->add_statement(Statement::make_goto_statement(retaddr_label
,
2352 Block
* then_block
= gogo
->finish_block(location
);
2353 then_block
->determine_types();
2355 Statement
* s
= Statement::make_if_statement(call
, then_block
, NULL
,
2357 s
->determine_types();
2358 gogo
->add_statement(s
);
2361 // Get a reference to the parameter.
2362 Named_object
* named_parameter
= gogo
->lookup(parameter_name
, NULL
);
2363 go_assert(named_parameter
!= NULL
&& named_parameter
->is_variable());
2365 // Build the call. Note that the field names are the same as the
2366 // ones used in build_struct.
2367 Expression
* thunk_parameter
= Expression::make_var_reference(named_parameter
,
2369 thunk_parameter
= Expression::make_unary(OPERATOR_MULT
, thunk_parameter
,
2372 Interface_field_reference_expression
* interface_method
=
2373 ce
->fn()->interface_field_reference_expression();
2375 Expression
* func_to_call
;
2376 unsigned int next_index
;
2377 if (this->is_constant_function())
2379 func_to_call
= ce
->fn();
2384 func_to_call
= Expression::make_field_reference(thunk_parameter
,
2389 if (interface_method
!= NULL
)
2391 // The main program passes the interface object.
2392 go_assert(next_index
== 0);
2393 Expression
* r
= Expression::make_field_reference(thunk_parameter
, 0,
2395 const std::string
& name(interface_method
->name());
2396 func_to_call
= Expression::make_interface_field_reference(r
, name
,
2401 Expression_list
* call_params
= new Expression_list();
2402 const Struct_field_list
* fields
= this->struct_type_
->fields();
2403 Struct_field_list::const_iterator p
= fields
->begin();
2404 for (unsigned int i
= 0; i
< next_index
; ++i
)
2406 bool is_recover_call
= ce
->is_recover_call();
2407 Expression
* recover_arg
= NULL
;
2408 for (; p
!= fields
->end(); ++p
, ++next_index
)
2410 Expression
* thunk_param
= Expression::make_var_reference(named_parameter
,
2412 thunk_param
= Expression::make_unary(OPERATOR_MULT
, thunk_param
,
2414 Expression
* param
= Expression::make_field_reference(thunk_param
,
2417 if (!is_recover_call
)
2418 call_params
->push_back(param
);
2421 go_assert(call_params
->empty());
2422 recover_arg
= param
;
2426 if (call_params
->empty())
2432 Call_expression
* call
= Expression::make_call(func_to_call
, call_params
,
2435 // This call expression was already lowered before entering the
2436 // thunk statement. Don't try to lower varargs again, as that will
2437 // cause confusion for, e.g., method calls which already have a
2438 // receiver parameter.
2439 call
->set_varargs_are_lowered();
2441 Statement
* call_statement
= Statement::make_statement(call
, true);
2443 gogo
->add_statement(call_statement
);
2445 // If this is a defer statement, the label comes immediately after
2447 if (may_call_recover
)
2449 gogo
->add_label_definition("retaddr", location
);
2451 Expression_list
* vals
= new Expression_list();
2452 vals
->push_back(Expression::make_boolean(false, location
));
2453 gogo
->add_statement(Statement::make_return_statement(vals
, location
));
2456 Block
* b
= gogo
->finish_block(location
);
2458 gogo
->add_block(b
, location
);
2460 gogo
->lower_block(function
, b
);
2462 // We already ran the determine_types pass, so we need to run it
2463 // just for the call statement now. The other types are known.
2464 call_statement
->determine_types();
2466 if (may_call_recover
|| recover_arg
!= NULL
)
2468 // Dig up the call expression, which may have been changed
2470 go_assert(call_statement
->classification() == STATEMENT_EXPRESSION
);
2471 Expression_statement
* es
=
2472 static_cast<Expression_statement
*>(call_statement
);
2473 Call_expression
* ce
= es
->expr()->call_expression();
2475 go_assert(saw_errors());
2478 if (may_call_recover
)
2479 ce
->set_is_deferred();
2480 if (recover_arg
!= NULL
)
2481 ce
->set_recover_arg(recover_arg
);
2485 // That is all the thunk has to do.
2486 gogo
->finish_function(location
);
2489 // Get the function and argument expressions.
2492 Thunk_statement::get_fn_and_arg(Expression
** pfn
, Expression
** parg
)
2494 if (this->call_
->is_error_expression())
2497 Call_expression
* ce
= this->call_
->call_expression();
2501 const Expression_list
* args
= ce
->args();
2502 if (args
== NULL
|| args
->empty())
2503 *parg
= Expression::make_nil(this->location());
2506 go_assert(args
->size() == 1);
2507 *parg
= args
->front();
2513 // Class Go_statement.
2516 Go_statement::do_get_backend(Translate_context
* context
)
2520 if (!this->get_fn_and_arg(&fn
, &arg
))
2521 return context
->backend()->error_statement();
2523 Expression
* call
= Runtime::make_call(Runtime::GO
, this->location(), 2,
2525 tree call_tree
= call
->get_tree(context
);
2526 Bexpression
* call_bexpr
= tree_to_expr(call_tree
);
2527 return context
->backend()->expression_statement(call_bexpr
);
2530 // Dump the AST representation for go statement.
2533 Go_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2535 ast_dump_context
->print_indent();
2536 ast_dump_context
->ostream() << "go ";
2537 ast_dump_context
->dump_expression(this->call());
2538 ast_dump_context
->ostream() << std::endl
;
2541 // Make a go statement.
2544 Statement::make_go_statement(Call_expression
* call
, Location location
)
2546 return new Go_statement(call
, location
);
2549 // Class Defer_statement.
2552 Defer_statement::do_get_backend(Translate_context
* context
)
2556 if (!this->get_fn_and_arg(&fn
, &arg
))
2557 return context
->backend()->error_statement();
2559 Location loc
= this->location();
2560 Expression
* ds
= context
->function()->func_value()->defer_stack(loc
);
2562 Expression
* call
= Runtime::make_call(Runtime::DEFER
, loc
, 3,
2564 tree call_tree
= call
->get_tree(context
);
2565 Bexpression
* call_bexpr
= tree_to_expr(call_tree
);
2566 return context
->backend()->expression_statement(call_bexpr
);
2569 // Dump the AST representation for defer statement.
2572 Defer_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2574 ast_dump_context
->print_indent();
2575 ast_dump_context
->ostream() << "defer ";
2576 ast_dump_context
->dump_expression(this->call());
2577 ast_dump_context
->ostream() << std::endl
;
2580 // Make a defer statement.
2583 Statement::make_defer_statement(Call_expression
* call
,
2586 return new Defer_statement(call
, location
);
2589 // Class Return_statement.
2591 // Traverse assignments. We treat each return value as a top level
2592 // RHS in an expression.
2595 Return_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
2597 Expression_list
* vals
= this->vals_
;
2600 for (Expression_list::iterator p
= vals
->begin();
2603 tassign
->value(&*p
, true, true);
2608 // Lower a return statement. If we are returning a function call
2609 // which returns multiple values which match the current function,
2610 // split up the call's results. If the return statement lists
2611 // explicit values, implement this statement by assigning the values
2612 // to the result variables and change this statement to a naked
2613 // return. This lets panic/recover work correctly.
2616 Return_statement::do_lower(Gogo
*, Named_object
* function
, Block
* enclosing
,
2617 Statement_inserter
*)
2619 if (this->is_lowered_
)
2622 Expression_list
* vals
= this->vals_
;
2624 this->is_lowered_
= true;
2626 Location loc
= this->location();
2628 size_t vals_count
= vals
== NULL
? 0 : vals
->size();
2629 Function::Results
* results
= function
->func_value()->result_variables();
2630 size_t results_count
= results
== NULL
? 0 : results
->size();
2632 if (vals_count
== 0)
2634 if (results_count
> 0 && !function
->func_value()->results_are_named())
2636 this->report_error(_("not enough arguments to return"));
2642 if (results_count
== 0)
2644 this->report_error(_("return with value in function "
2645 "with no return type"));
2649 // If the current function has multiple return values, and we are
2650 // returning a single call expression, split up the call expression.
2651 if (results_count
> 1
2652 && vals
->size() == 1
2653 && vals
->front()->call_expression() != NULL
)
2655 Call_expression
* call
= vals
->front()->call_expression();
2657 vals
= new Expression_list
;
2658 for (size_t i
= 0; i
< results_count
; ++i
)
2659 vals
->push_back(Expression::make_call_result(call
, i
));
2660 vals_count
= results_count
;
2663 if (vals_count
< results_count
)
2665 this->report_error(_("not enough arguments to return"));
2669 if (vals_count
> results_count
)
2671 this->report_error(_("too many values in return statement"));
2675 Block
* b
= new Block(enclosing
, loc
);
2677 Expression_list
* lhs
= new Expression_list();
2678 Expression_list
* rhs
= new Expression_list();
2680 Expression_list::const_iterator pe
= vals
->begin();
2682 for (Function::Results::const_iterator pr
= results
->begin();
2683 pr
!= results
->end();
2686 Named_object
* rv
= *pr
;
2687 Expression
* e
= *pe
;
2689 // Check types now so that we give a good error message. The
2690 // result type is known. We determine the expression type
2693 Type
*rvtype
= rv
->result_var_value()->type();
2694 Type_context
type_context(rvtype
, false);
2695 e
->determine_type(&type_context
);
2699 if (this->are_hidden_fields_ok_
)
2700 ok
= Type::are_assignable_hidden_ok(rvtype
, e
->type(), &reason
);
2702 ok
= Type::are_assignable(rvtype
, e
->type(), &reason
);
2705 Expression
* ve
= Expression::make_var_reference(rv
, e
->location());
2712 error_at(e
->location(), "incompatible type for return value %d", i
);
2714 error_at(e
->location(),
2715 "incompatible type for return value %d (%s)",
2719 go_assert(lhs
->size() == rhs
->size());
2723 else if (lhs
->size() == 1)
2725 Statement
* s
= Statement::make_assignment(lhs
->front(), rhs
->front(),
2727 if (this->are_hidden_fields_ok_
)
2729 Assignment_statement
* as
= static_cast<Assignment_statement
*>(s
);
2730 as
->set_hidden_fields_are_ok();
2732 b
->add_statement(s
);
2738 Statement
* s
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
2739 if (this->are_hidden_fields_ok_
)
2741 Tuple_assignment_statement
* tas
=
2742 static_cast<Tuple_assignment_statement
*>(s
);
2743 tas
->set_hidden_fields_are_ok();
2745 b
->add_statement(s
);
2748 b
->add_statement(this);
2752 return Statement::make_block_statement(b
, loc
);
2755 // Convert a return statement to the backend representation.
2758 Return_statement::do_get_backend(Translate_context
* context
)
2760 Location loc
= this->location();
2762 Function
* function
= context
->function()->func_value();
2763 tree fndecl
= function
->get_decl();
2765 Function::Results
* results
= function
->result_variables();
2766 std::vector
<Bexpression
*> retvals
;
2767 if (results
!= NULL
&& !results
->empty())
2769 retvals
.reserve(results
->size());
2770 for (Function::Results::const_iterator p
= results
->begin();
2771 p
!= results
->end();
2774 Expression
* vr
= Expression::make_var_reference(*p
, loc
);
2775 retvals
.push_back(tree_to_expr(vr
->get_tree(context
)));
2779 return context
->backend()->return_statement(tree_to_function(fndecl
),
2783 // Dump the AST representation for a return statement.
2786 Return_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2788 ast_dump_context
->print_indent();
2789 ast_dump_context
->ostream() << "return " ;
2790 ast_dump_context
->dump_expression_list(this->vals_
);
2791 ast_dump_context
->ostream() << std::endl
;
2794 // Make a return statement.
2797 Statement::make_return_statement(Expression_list
* vals
,
2800 return new Return_statement(vals
, location
);
2803 // A break or continue statement.
2805 class Bc_statement
: public Statement
2808 Bc_statement(bool is_break
, Unnamed_label
* label
, Location location
)
2809 : Statement(STATEMENT_BREAK_OR_CONTINUE
, location
),
2810 label_(label
), is_break_(is_break
)
2815 { return this->is_break_
; }
2819 do_traverse(Traverse
*)
2820 { return TRAVERSE_CONTINUE
; }
2823 do_may_fall_through() const
2827 do_get_backend(Translate_context
* context
)
2828 { return this->label_
->get_goto(context
, this->location()); }
2831 do_dump_statement(Ast_dump_context
*) const;
2834 // The label that this branches to.
2835 Unnamed_label
* label_
;
2836 // True if this is "break", false if it is "continue".
2840 // Dump the AST representation for a break/continue statement
2843 Bc_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2845 ast_dump_context
->print_indent();
2846 ast_dump_context
->ostream() << (this->is_break_
? "break" : "continue");
2847 if (this->label_
!= NULL
)
2849 ast_dump_context
->ostream() << " ";
2850 ast_dump_context
->dump_label_name(this->label_
);
2852 ast_dump_context
->ostream() << std::endl
;
2855 // Make a break statement.
2858 Statement::make_break_statement(Unnamed_label
* label
, Location location
)
2860 return new Bc_statement(true, label
, location
);
2863 // Make a continue statement.
2866 Statement::make_continue_statement(Unnamed_label
* label
,
2869 return new Bc_statement(false, label
, location
);
2872 // A goto statement.
2874 class Goto_statement
: public Statement
2877 Goto_statement(Label
* label
, Location location
)
2878 : Statement(STATEMENT_GOTO
, location
),
2884 do_traverse(Traverse
*)
2885 { return TRAVERSE_CONTINUE
; }
2888 do_check_types(Gogo
*);
2891 do_may_fall_through() const
2895 do_get_backend(Translate_context
*);
2898 do_dump_statement(Ast_dump_context
*) const;
2904 // Check types for a label. There aren't any types per se, but we use
2905 // this to give an error if the label was never defined.
2908 Goto_statement::do_check_types(Gogo
*)
2910 if (!this->label_
->is_defined())
2912 error_at(this->location(), "reference to undefined label %qs",
2913 Gogo::message_name(this->label_
->name()).c_str());
2914 this->set_is_error();
2918 // Convert the goto statement to the backend representation.
2921 Goto_statement::do_get_backend(Translate_context
* context
)
2923 Blabel
* blabel
= this->label_
->get_backend_label(context
);
2924 return context
->backend()->goto_statement(blabel
, this->location());
2927 // Dump the AST representation for a goto statement.
2930 Goto_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2932 ast_dump_context
->print_indent();
2933 ast_dump_context
->ostream() << "goto " << this->label_
->name() << std::endl
;
2936 // Make a goto statement.
2939 Statement::make_goto_statement(Label
* label
, Location location
)
2941 return new Goto_statement(label
, location
);
2944 // A goto statement to an unnamed label.
2946 class Goto_unnamed_statement
: public Statement
2949 Goto_unnamed_statement(Unnamed_label
* label
, Location location
)
2950 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
2956 do_traverse(Traverse
*)
2957 { return TRAVERSE_CONTINUE
; }
2960 do_may_fall_through() const
2964 do_get_backend(Translate_context
* context
)
2965 { return this->label_
->get_goto(context
, this->location()); }
2968 do_dump_statement(Ast_dump_context
*) const;
2971 Unnamed_label
* label_
;
2974 // Dump the AST representation for an unnamed goto statement
2977 Goto_unnamed_statement::do_dump_statement(
2978 Ast_dump_context
* ast_dump_context
) const
2980 ast_dump_context
->print_indent();
2981 ast_dump_context
->ostream() << "goto ";
2982 ast_dump_context
->dump_label_name(this->label_
);
2983 ast_dump_context
->ostream() << std::endl
;
2986 // Make a goto statement to an unnamed label.
2989 Statement::make_goto_unnamed_statement(Unnamed_label
* label
,
2992 return new Goto_unnamed_statement(label
, location
);
2995 // Class Label_statement.
3000 Label_statement::do_traverse(Traverse
*)
3002 return TRAVERSE_CONTINUE
;
3005 // Return the backend representation of the statement defining this
3009 Label_statement::do_get_backend(Translate_context
* context
)
3011 Blabel
* blabel
= this->label_
->get_backend_label(context
);
3012 return context
->backend()->label_definition_statement(blabel
);
3015 // Dump the AST for a label definition statement.
3018 Label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3020 ast_dump_context
->print_indent();
3021 ast_dump_context
->ostream() << this->label_
->name() << ":" << std::endl
;
3024 // Make a label statement.
3027 Statement::make_label_statement(Label
* label
, Location location
)
3029 return new Label_statement(label
, location
);
3032 // An unnamed label statement.
3034 class Unnamed_label_statement
: public Statement
3037 Unnamed_label_statement(Unnamed_label
* label
)
3038 : Statement(STATEMENT_UNNAMED_LABEL
, label
->location()),
3044 do_traverse(Traverse
*)
3045 { return TRAVERSE_CONTINUE
; }
3048 do_get_backend(Translate_context
* context
)
3049 { return this->label_
->get_definition(context
); }
3052 do_dump_statement(Ast_dump_context
*) const;
3056 Unnamed_label
* label_
;
3059 // Dump the AST representation for an unnamed label definition statement.
3062 Unnamed_label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3065 ast_dump_context
->print_indent();
3066 ast_dump_context
->dump_label_name(this->label_
);
3067 ast_dump_context
->ostream() << ":" << std::endl
;
3070 // Make an unnamed label statement.
3073 Statement::make_unnamed_label_statement(Unnamed_label
* label
)
3075 return new Unnamed_label_statement(label
);
3080 class If_statement
: public Statement
3083 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
3085 : Statement(STATEMENT_IF
, location
),
3086 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
3091 do_traverse(Traverse
*);
3094 do_determine_types();
3097 do_check_types(Gogo
*);
3100 do_may_fall_through() const;
3103 do_get_backend(Translate_context
*);
3106 do_dump_statement(Ast_dump_context
*) const;
3117 If_statement::do_traverse(Traverse
* traverse
)
3119 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
3120 || this->then_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3121 return TRAVERSE_EXIT
;
3122 if (this->else_block_
!= NULL
)
3124 if (this->else_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3125 return TRAVERSE_EXIT
;
3127 return TRAVERSE_CONTINUE
;
3131 If_statement::do_determine_types()
3133 Type_context
context(Type::lookup_bool_type(), false);
3134 this->cond_
->determine_type(&context
);
3135 this->then_block_
->determine_types();
3136 if (this->else_block_
!= NULL
)
3137 this->else_block_
->determine_types();
3143 If_statement::do_check_types(Gogo
*)
3145 Type
* type
= this->cond_
->type();
3146 if (type
->is_error())
3147 this->set_is_error();
3148 else if (!type
->is_boolean_type())
3149 this->report_error(_("expected boolean expression"));
3152 // Whether the overall statement may fall through.
3155 If_statement::do_may_fall_through() const
3157 return (this->else_block_
== NULL
3158 || this->then_block_
->may_fall_through()
3159 || this->else_block_
->may_fall_through());
3162 // Get the backend representation.
3165 If_statement::do_get_backend(Translate_context
* context
)
3167 go_assert(this->cond_
->type()->is_boolean_type()
3168 || this->cond_
->type()->is_error());
3169 tree cond_tree
= this->cond_
->get_tree(context
);
3170 Bexpression
* cond_expr
= tree_to_expr(cond_tree
);
3171 Bblock
* then_block
= this->then_block_
->get_backend(context
);
3172 Bblock
* else_block
= (this->else_block_
== NULL
3174 : this->else_block_
->get_backend(context
));
3175 return context
->backend()->if_statement(cond_expr
, then_block
,
3176 else_block
, this->location());
3179 // Dump the AST representation for an if statement
3182 If_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3184 ast_dump_context
->print_indent();
3185 ast_dump_context
->ostream() << "if ";
3186 ast_dump_context
->dump_expression(this->cond_
);
3187 ast_dump_context
->ostream() << std::endl
;
3188 if (ast_dump_context
->dump_subblocks())
3190 ast_dump_context
->dump_block(this->then_block_
);
3191 if (this->else_block_
!= NULL
)
3193 ast_dump_context
->print_indent();
3194 ast_dump_context
->ostream() << "else" << std::endl
;
3195 ast_dump_context
->dump_block(this->else_block_
);
3200 // Make an if statement.
3203 Statement::make_if_statement(Expression
* cond
, Block
* then_block
,
3204 Block
* else_block
, Location location
)
3206 return new If_statement(cond
, then_block
, else_block
, location
);
3209 // Class Case_clauses::Hash_integer_value.
3211 class Case_clauses::Hash_integer_value
3215 operator()(Expression
*) const;
3219 Case_clauses::Hash_integer_value::operator()(Expression
* pe
) const
3221 Numeric_constant nc
;
3223 if (!pe
->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3225 size_t ret
= mpz_get_ui(ival
);
3230 // Class Case_clauses::Eq_integer_value.
3232 class Case_clauses::Eq_integer_value
3236 operator()(Expression
*, Expression
*) const;
3240 Case_clauses::Eq_integer_value::operator()(Expression
* a
, Expression
* b
) const
3242 Numeric_constant anc
;
3244 Numeric_constant bnc
;
3246 if (!a
->numeric_constant_value(&anc
)
3247 || !anc
.to_int(&aval
)
3248 || !b
->numeric_constant_value(&bnc
)
3249 || !bnc
.to_int(&bval
))
3251 bool ret
= mpz_cmp(aval
, bval
) == 0;
3257 // Class Case_clauses::Case_clause.
3262 Case_clauses::Case_clause::traverse(Traverse
* traverse
)
3264 if (this->cases_
!= NULL
3265 && (traverse
->traverse_mask()
3266 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3268 if (this->cases_
->traverse(traverse
) == TRAVERSE_EXIT
)
3269 return TRAVERSE_EXIT
;
3271 if (this->statements_
!= NULL
)
3273 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
3274 return TRAVERSE_EXIT
;
3276 return TRAVERSE_CONTINUE
;
3279 // Check whether all the case expressions are integer constants.
3282 Case_clauses::Case_clause::is_constant() const
3284 if (this->cases_
!= NULL
)
3286 for (Expression_list::const_iterator p
= this->cases_
->begin();
3287 p
!= this->cases_
->end();
3289 if (!(*p
)->is_constant() || (*p
)->type()->integer_type() == NULL
)
3295 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3296 // value we are switching on; it may be NULL. If START_LABEL is not
3297 // NULL, it goes at the start of the statements, after the condition
3298 // test. We branch to FINISH_LABEL at the end of the statements.
3301 Case_clauses::Case_clause::lower(Block
* b
, Temporary_statement
* val_temp
,
3302 Unnamed_label
* start_label
,
3303 Unnamed_label
* finish_label
) const
3305 Location loc
= this->location_
;
3306 Unnamed_label
* next_case_label
;
3307 if (this->cases_
== NULL
|| this->cases_
->empty())
3309 go_assert(this->is_default_
);
3310 next_case_label
= NULL
;
3314 Expression
* cond
= NULL
;
3316 for (Expression_list::const_iterator p
= this->cases_
->begin();
3317 p
!= this->cases_
->end();
3320 Expression
* ref
= Expression::make_temporary_reference(val_temp
,
3322 Expression
* this_cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3327 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, this_cond
, loc
);
3330 Block
* then_block
= new Block(b
, loc
);
3331 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3332 Statement
* s
= Statement::make_goto_unnamed_statement(next_case_label
,
3334 then_block
->add_statement(s
);
3336 // if !COND { goto NEXT_CASE_LABEL }
3337 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3338 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
3339 b
->add_statement(s
);
3342 if (start_label
!= NULL
)
3343 b
->add_statement(Statement::make_unnamed_label_statement(start_label
));
3345 if (this->statements_
!= NULL
)
3346 b
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
3348 Statement
* s
= Statement::make_goto_unnamed_statement(finish_label
, loc
);
3349 b
->add_statement(s
);
3351 if (next_case_label
!= NULL
)
3352 b
->add_statement(Statement::make_unnamed_label_statement(next_case_label
));
3358 Case_clauses::Case_clause::determine_types(Type
* type
)
3360 if (this->cases_
!= NULL
)
3362 Type_context
case_context(type
, false);
3363 for (Expression_list::iterator p
= this->cases_
->begin();
3364 p
!= this->cases_
->end();
3366 (*p
)->determine_type(&case_context
);
3368 if (this->statements_
!= NULL
)
3369 this->statements_
->determine_types();
3372 // Check types. Returns false if there was an error.
3375 Case_clauses::Case_clause::check_types(Type
* type
)
3377 if (this->cases_
!= NULL
)
3379 for (Expression_list::iterator p
= this->cases_
->begin();
3380 p
!= this->cases_
->end();
3383 if (!Type::are_assignable(type
, (*p
)->type(), NULL
)
3384 && !Type::are_assignable((*p
)->type(), type
, NULL
))
3386 error_at((*p
)->location(),
3387 "type mismatch between switch value and case clause");
3395 // Return true if this clause may fall through to the following
3396 // statements. Note that this is not the same as whether the case
3397 // uses the "fallthrough" keyword.
3400 Case_clauses::Case_clause::may_fall_through() const
3402 if (this->statements_
== NULL
)
3404 return this->statements_
->may_fall_through();
3407 // Convert the case values and statements to the backend
3408 // representation. BREAK_LABEL is the label which break statements
3409 // should branch to. CASE_CONSTANTS is used to detect duplicate
3410 // constants. *CASES should be passed as an empty vector; the values
3411 // for this case will be added to it. If this is the default case,
3412 // *CASES will remain empty. This returns the statement to execute if
3413 // one of these cases is selected.
3416 Case_clauses::Case_clause::get_backend(Translate_context
* context
,
3417 Unnamed_label
* break_label
,
3418 Case_constants
* case_constants
,
3419 std::vector
<Bexpression
*>* cases
) const
3421 if (this->cases_
!= NULL
)
3423 go_assert(!this->is_default_
);
3424 for (Expression_list::const_iterator p
= this->cases_
->begin();
3425 p
!= this->cases_
->end();
3429 if (e
->classification() != Expression::EXPRESSION_INTEGER
)
3431 Numeric_constant nc
;
3433 if (!(*p
)->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3435 // Something went wrong. This can happen with a
3436 // negative constant and an unsigned switch value.
3437 go_assert(saw_errors());
3440 go_assert(nc
.type() != NULL
);
3441 e
= Expression::make_integer(&ival
, nc
.type(), e
->location());
3445 std::pair
<Case_constants::iterator
, bool> ins
=
3446 case_constants
->insert(e
);
3449 // Value was already present.
3450 error_at(this->location_
, "duplicate case in switch");
3451 e
= Expression::make_error(this->location_
);
3454 tree case_tree
= e
->get_tree(context
);
3455 Bexpression
* case_expr
= tree_to_expr(case_tree
);
3456 cases
->push_back(case_expr
);
3460 Bstatement
* statements
;
3461 if (this->statements_
== NULL
)
3465 Bblock
* bblock
= this->statements_
->get_backend(context
);
3466 statements
= context
->backend()->block_statement(bblock
);
3469 Bstatement
* break_stat
;
3470 if (this->is_fallthrough_
)
3473 break_stat
= break_label
->get_goto(context
, this->location_
);
3475 if (statements
== NULL
)
3477 else if (break_stat
== NULL
)
3480 return context
->backend()->compound_statement(statements
, break_stat
);
3483 // Dump the AST representation for a case clause
3486 Case_clauses::Case_clause::dump_clause(Ast_dump_context
* ast_dump_context
)
3489 ast_dump_context
->print_indent();
3490 if (this->is_default_
)
3492 ast_dump_context
->ostream() << "default:";
3496 ast_dump_context
->ostream() << "case ";
3497 ast_dump_context
->dump_expression_list(this->cases_
);
3498 ast_dump_context
->ostream() << ":" ;
3500 ast_dump_context
->dump_block(this->statements_
);
3501 if (this->is_fallthrough_
)
3503 ast_dump_context
->print_indent();
3504 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
3508 // Class Case_clauses.
3513 Case_clauses::traverse(Traverse
* traverse
)
3515 for (Clauses::iterator p
= this->clauses_
.begin();
3516 p
!= this->clauses_
.end();
3519 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
3520 return TRAVERSE_EXIT
;
3522 return TRAVERSE_CONTINUE
;
3525 // Check whether all the case expressions are constant.
3528 Case_clauses::is_constant() const
3530 for (Clauses::const_iterator p
= this->clauses_
.begin();
3531 p
!= this->clauses_
.end();
3533 if (!p
->is_constant())
3538 // Lower case clauses for a nonconstant switch.
3541 Case_clauses::lower(Block
* b
, Temporary_statement
* val_temp
,
3542 Unnamed_label
* break_label
) const
3544 // The default case.
3545 const Case_clause
* default_case
= NULL
;
3547 // The label for the fallthrough of the previous case.
3548 Unnamed_label
* last_fallthrough_label
= NULL
;
3550 // The label for the start of the default case. This is used if the
3551 // case before the default case falls through.
3552 Unnamed_label
* default_start_label
= NULL
;
3554 // The label for the end of the default case. This normally winds
3555 // up as BREAK_LABEL, but it will be different if the default case
3557 Unnamed_label
* default_finish_label
= NULL
;
3559 for (Clauses::const_iterator p
= this->clauses_
.begin();
3560 p
!= this->clauses_
.end();
3563 // The label to use for the start of the statements for this
3564 // case. This is NULL unless the previous case falls through.
3565 Unnamed_label
* start_label
= last_fallthrough_label
;
3567 // The label to jump to after the end of the statements for this
3569 Unnamed_label
* finish_label
= break_label
;
3571 last_fallthrough_label
= NULL
;
3572 if (p
->is_fallthrough() && p
+ 1 != this->clauses_
.end())
3574 finish_label
= new Unnamed_label(p
->location());
3575 last_fallthrough_label
= finish_label
;
3578 if (!p
->is_default())
3579 p
->lower(b
, val_temp
, start_label
, finish_label
);
3582 // We have to move the default case to the end, so that we
3583 // only use it if all the other tests fail.
3585 default_start_label
= start_label
;
3586 default_finish_label
= finish_label
;
3590 if (default_case
!= NULL
)
3591 default_case
->lower(b
, val_temp
, default_start_label
,
3592 default_finish_label
);
3598 Case_clauses::determine_types(Type
* type
)
3600 for (Clauses::iterator p
= this->clauses_
.begin();
3601 p
!= this->clauses_
.end();
3603 p
->determine_types(type
);
3606 // Check types. Returns false if there was an error.
3609 Case_clauses::check_types(Type
* type
)
3612 for (Clauses::iterator p
= this->clauses_
.begin();
3613 p
!= this->clauses_
.end();
3616 if (!p
->check_types(type
))
3622 // Return true if these clauses may fall through to the statements
3623 // following the switch statement.
3626 Case_clauses::may_fall_through() const
3628 bool found_default
= false;
3629 for (Clauses::const_iterator p
= this->clauses_
.begin();
3630 p
!= this->clauses_
.end();
3633 if (p
->may_fall_through() && !p
->is_fallthrough())
3635 if (p
->is_default())
3636 found_default
= true;
3638 return !found_default
;
3641 // Convert the cases to the backend representation. This sets
3642 // *ALL_CASES and *ALL_STATEMENTS.
3645 Case_clauses::get_backend(Translate_context
* context
,
3646 Unnamed_label
* break_label
,
3647 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
3648 std::vector
<Bstatement
*>* all_statements
) const
3650 Case_constants case_constants
;
3652 size_t c
= this->clauses_
.size();
3653 all_cases
->resize(c
);
3654 all_statements
->resize(c
);
3657 for (Clauses::const_iterator p
= this->clauses_
.begin();
3658 p
!= this->clauses_
.end();
3661 std::vector
<Bexpression
*> cases
;
3662 Bstatement
* stat
= p
->get_backend(context
, break_label
, &case_constants
,
3664 (*all_cases
)[i
].swap(cases
);
3665 (*all_statements
)[i
] = stat
;
3669 // Dump the AST representation for case clauses (from a switch statement)
3672 Case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
3674 for (Clauses::const_iterator p
= this->clauses_
.begin();
3675 p
!= this->clauses_
.end();
3677 p
->dump_clause(ast_dump_context
);
3680 // A constant switch statement. A Switch_statement is lowered to this
3681 // when all the cases are constants.
3683 class Constant_switch_statement
: public Statement
3686 Constant_switch_statement(Expression
* val
, Case_clauses
* clauses
,
3687 Unnamed_label
* break_label
,
3689 : Statement(STATEMENT_CONSTANT_SWITCH
, location
),
3690 val_(val
), clauses_(clauses
), break_label_(break_label
)
3695 do_traverse(Traverse
*);
3698 do_determine_types();
3701 do_check_types(Gogo
*);
3704 do_may_fall_through() const;
3707 do_get_backend(Translate_context
*);
3710 do_dump_statement(Ast_dump_context
*) const;
3713 // The value to switch on.
3715 // The case clauses.
3716 Case_clauses
* clauses_
;
3717 // The break label, if needed.
3718 Unnamed_label
* break_label_
;
3724 Constant_switch_statement::do_traverse(Traverse
* traverse
)
3726 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3727 return TRAVERSE_EXIT
;
3728 return this->clauses_
->traverse(traverse
);
3734 Constant_switch_statement::do_determine_types()
3736 this->val_
->determine_type_no_context();
3737 this->clauses_
->determine_types(this->val_
->type());
3743 Constant_switch_statement::do_check_types(Gogo
*)
3745 if (!this->clauses_
->check_types(this->val_
->type()))
3746 this->set_is_error();
3749 // Return whether this switch may fall through.
3752 Constant_switch_statement::do_may_fall_through() const
3754 if (this->clauses_
== NULL
)
3757 // If we have a break label, then some case needed it. That implies
3758 // that the switch statement as a whole can fall through.
3759 if (this->break_label_
!= NULL
)
3762 return this->clauses_
->may_fall_through();
3765 // Convert to GENERIC.
3768 Constant_switch_statement::do_get_backend(Translate_context
* context
)
3770 tree switch_val_tree
= this->val_
->get_tree(context
);
3771 Bexpression
* switch_val_expr
= tree_to_expr(switch_val_tree
);
3773 Unnamed_label
* break_label
= this->break_label_
;
3774 if (break_label
== NULL
)
3775 break_label
= new Unnamed_label(this->location());
3777 std::vector
<std::vector
<Bexpression
*> > all_cases
;
3778 std::vector
<Bstatement
*> all_statements
;
3779 this->clauses_
->get_backend(context
, break_label
, &all_cases
,
3782 Bstatement
* switch_statement
;
3783 switch_statement
= context
->backend()->switch_statement(switch_val_expr
,
3787 Bstatement
* ldef
= break_label
->get_definition(context
);
3788 return context
->backend()->compound_statement(switch_statement
, ldef
);
3791 // Dump the AST representation for a constant switch statement.
3794 Constant_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3797 ast_dump_context
->print_indent();
3798 ast_dump_context
->ostream() << "switch ";
3799 ast_dump_context
->dump_expression(this->val_
);
3801 if (ast_dump_context
->dump_subblocks())
3803 ast_dump_context
->ostream() << " {" << std::endl
;
3804 this->clauses_
->dump_clauses(ast_dump_context
);
3805 ast_dump_context
->ostream() << "}";
3808 ast_dump_context
->ostream() << std::endl
;
3811 // Class Switch_statement.
3816 Switch_statement::do_traverse(Traverse
* traverse
)
3818 if (this->val_
!= NULL
)
3820 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3821 return TRAVERSE_EXIT
;
3823 return this->clauses_
->traverse(traverse
);
3826 // Lower a Switch_statement to a Constant_switch_statement or a series
3827 // of if statements.
3830 Switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
3831 Statement_inserter
*)
3833 Location loc
= this->location();
3835 if (this->val_
!= NULL
3836 && (this->val_
->is_error_expression()
3837 || this->val_
->type()->is_error()))
3838 return Statement::make_error_statement(loc
);
3840 if (this->val_
!= NULL
3841 && this->val_
->type()->integer_type() != NULL
3842 && !this->clauses_
->empty()
3843 && this->clauses_
->is_constant())
3844 return new Constant_switch_statement(this->val_
, this->clauses_
,
3845 this->break_label_
, loc
);
3847 if (this->val_
!= NULL
3848 && !this->val_
->type()->is_comparable()
3849 && !Type::are_compatible_for_comparison(true, this->val_
->type(),
3850 Type::make_nil_type(), NULL
))
3852 error_at(this->val_
->location(),
3853 "cannot switch on value whose type that may not be compared");
3854 return Statement::make_error_statement(loc
);
3857 Block
* b
= new Block(enclosing
, loc
);
3859 if (this->clauses_
->empty())
3861 Expression
* val
= this->val_
;
3863 val
= Expression::make_boolean(true, loc
);
3864 return Statement::make_statement(val
, true);
3867 // var val_temp VAL_TYPE = VAL
3868 Expression
* val
= this->val_
;
3870 val
= Expression::make_boolean(true, loc
);
3871 Temporary_statement
* val_temp
= Statement::make_temporary(NULL
, val
, loc
);
3872 b
->add_statement(val_temp
);
3874 this->clauses_
->lower(b
, val_temp
, this->break_label());
3876 Statement
* s
= Statement::make_unnamed_label_statement(this->break_label_
);
3877 b
->add_statement(s
);
3879 return Statement::make_block_statement(b
, loc
);
3882 // Return the break label for this switch statement, creating it if
3886 Switch_statement::break_label()
3888 if (this->break_label_
== NULL
)
3889 this->break_label_
= new Unnamed_label(this->location());
3890 return this->break_label_
;
3893 // Dump the AST representation for a switch statement.
3896 Switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3898 ast_dump_context
->print_indent();
3899 ast_dump_context
->ostream() << "switch ";
3900 if (this->val_
!= NULL
)
3902 ast_dump_context
->dump_expression(this->val_
);
3904 if (ast_dump_context
->dump_subblocks())
3906 ast_dump_context
->ostream() << " {" << std::endl
;
3907 this->clauses_
->dump_clauses(ast_dump_context
);
3908 ast_dump_context
->print_indent();
3909 ast_dump_context
->ostream() << "}";
3911 ast_dump_context
->ostream() << std::endl
;
3914 // Make a switch statement.
3917 Statement::make_switch_statement(Expression
* val
, Location location
)
3919 return new Switch_statement(val
, location
);
3922 // Class Type_case_clauses::Type_case_clause.
3927 Type_case_clauses::Type_case_clause::traverse(Traverse
* traverse
)
3929 if (!this->is_default_
3930 && ((traverse
->traverse_mask()
3931 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3932 && Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3933 return TRAVERSE_EXIT
;
3934 if (this->statements_
!= NULL
)
3935 return this->statements_
->traverse(traverse
);
3936 return TRAVERSE_CONTINUE
;
3939 // Lower one clause in a type switch. Add statements to the block B.
3940 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3941 // BREAK_LABEL is the label at the end of the type switch.
3942 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3946 Type_case_clauses::Type_case_clause::lower(Type
* switch_val_type
,
3948 Temporary_statement
* descriptor_temp
,
3949 Unnamed_label
* break_label
,
3950 Unnamed_label
** stmts_label
) const
3952 Location loc
= this->location_
;
3954 Unnamed_label
* next_case_label
= NULL
;
3955 if (!this->is_default_
)
3957 Type
* type
= this->type_
;
3960 if (switch_val_type
->interface_type() != NULL
3961 && !type
->is_nil_constant_as_type()
3962 && type
->interface_type() == NULL
3963 && !switch_val_type
->interface_type()->implements_interface(type
,
3967 error_at(this->location_
, "impossible type switch case");
3969 error_at(this->location_
, "impossible type switch case (%s)",
3973 Expression
* ref
= Expression::make_temporary_reference(descriptor_temp
,
3977 // The language permits case nil, which is of course a constant
3978 // rather than a type. It will appear here as an invalid
3980 if (type
->is_nil_constant_as_type())
3981 cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3982 Expression::make_nil(loc
),
3985 cond
= Runtime::make_call((type
->interface_type() == NULL
3986 ? Runtime::IFACETYPEEQ
3987 : Runtime::IFACEI2TP
),
3989 Expression::make_type_descriptor(type
, loc
),
3992 Unnamed_label
* dest
;
3993 if (!this->is_fallthrough_
)
3995 // if !COND { goto NEXT_CASE_LABEL }
3996 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3997 dest
= next_case_label
;
3998 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
4002 // if COND { goto STMTS_LABEL }
4003 go_assert(stmts_label
!= NULL
);
4004 if (*stmts_label
== NULL
)
4005 *stmts_label
= new Unnamed_label(Linemap::unknown_location());
4006 dest
= *stmts_label
;
4008 Block
* then_block
= new Block(b
, loc
);
4009 Statement
* s
= Statement::make_goto_unnamed_statement(dest
, loc
);
4010 then_block
->add_statement(s
);
4011 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
4012 b
->add_statement(s
);
4015 if (this->statements_
!= NULL
4016 || (!this->is_fallthrough_
4017 && stmts_label
!= NULL
4018 && *stmts_label
!= NULL
))
4020 go_assert(!this->is_fallthrough_
);
4021 if (stmts_label
!= NULL
&& *stmts_label
!= NULL
)
4023 go_assert(!this->is_default_
);
4024 if (this->statements_
!= NULL
)
4025 (*stmts_label
)->set_location(this->statements_
->start_location());
4026 Statement
* s
= Statement::make_unnamed_label_statement(*stmts_label
);
4027 b
->add_statement(s
);
4028 *stmts_label
= NULL
;
4030 if (this->statements_
!= NULL
)
4031 b
->add_statement(Statement::make_block_statement(this->statements_
,
4035 if (this->is_fallthrough_
)
4036 go_assert(next_case_label
== NULL
);
4039 Location gloc
= (this->statements_
== NULL
4041 : this->statements_
->end_location());
4042 b
->add_statement(Statement::make_goto_unnamed_statement(break_label
,
4044 if (next_case_label
!= NULL
)
4047 Statement::make_unnamed_label_statement(next_case_label
);
4048 b
->add_statement(s
);
4053 // Dump the AST representation for a type case clause
4056 Type_case_clauses::Type_case_clause::dump_clause(
4057 Ast_dump_context
* ast_dump_context
) const
4059 ast_dump_context
->print_indent();
4060 if (this->is_default_
)
4062 ast_dump_context
->ostream() << "default:";
4066 ast_dump_context
->ostream() << "case ";
4067 ast_dump_context
->dump_type(this->type_
);
4068 ast_dump_context
->ostream() << ":" ;
4070 ast_dump_context
->dump_block(this->statements_
);
4071 if (this->is_fallthrough_
)
4073 ast_dump_context
->print_indent();
4074 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
4078 // Class Type_case_clauses.
4083 Type_case_clauses::traverse(Traverse
* traverse
)
4085 for (Type_clauses::iterator p
= this->clauses_
.begin();
4086 p
!= this->clauses_
.end();
4089 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4090 return TRAVERSE_EXIT
;
4092 return TRAVERSE_CONTINUE
;
4095 // Check for duplicate types.
4098 Type_case_clauses::check_duplicates() const
4100 typedef Unordered_set_hash(const Type
*, Type_hash_identical
,
4101 Type_identical
) Types_seen
;
4102 Types_seen types_seen
;
4103 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4104 p
!= this->clauses_
.end();
4107 Type
* t
= p
->type();
4110 if (t
->is_nil_constant_as_type())
4111 t
= Type::make_nil_type();
4112 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
.insert(t
);
4114 error_at(p
->location(), "duplicate type in switch");
4118 // Lower the clauses in a type switch. Add statements to the block B.
4119 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4120 // BREAK_LABEL is the label at the end of the type switch.
4123 Type_case_clauses::lower(Type
* switch_val_type
, Block
* b
,
4124 Temporary_statement
* descriptor_temp
,
4125 Unnamed_label
* break_label
) const
4127 const Type_case_clause
* default_case
= NULL
;
4129 Unnamed_label
* stmts_label
= NULL
;
4130 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4131 p
!= this->clauses_
.end();
4134 if (!p
->is_default())
4135 p
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4139 // We are generating a series of tests, which means that we
4140 // need to move the default case to the end.
4144 go_assert(stmts_label
== NULL
);
4146 if (default_case
!= NULL
)
4147 default_case
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4151 // Dump the AST representation for case clauses (from a switch statement)
4154 Type_case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4156 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4157 p
!= this->clauses_
.end();
4159 p
->dump_clause(ast_dump_context
);
4162 // Class Type_switch_statement.
4167 Type_switch_statement::do_traverse(Traverse
* traverse
)
4169 if (this->var_
== NULL
)
4171 if (this->traverse_expression(traverse
, &this->expr_
) == TRAVERSE_EXIT
)
4172 return TRAVERSE_EXIT
;
4174 if (this->clauses_
!= NULL
)
4175 return this->clauses_
->traverse(traverse
);
4176 return TRAVERSE_CONTINUE
;
4179 // Lower a type switch statement to a series of if statements. The gc
4180 // compiler is able to generate a table in some cases. However, that
4181 // does not work for us because we may have type descriptors in
4182 // different shared libraries, so we can't compare them with simple
4183 // equality testing.
4186 Type_switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
4187 Statement_inserter
*)
4189 const Location loc
= this->location();
4191 if (this->clauses_
!= NULL
)
4192 this->clauses_
->check_duplicates();
4194 Block
* b
= new Block(enclosing
, loc
);
4196 Type
* val_type
= (this->var_
!= NULL
4197 ? this->var_
->var_value()->type()
4198 : this->expr_
->type());
4200 if (val_type
->interface_type() == NULL
)
4202 if (!val_type
->is_error())
4203 this->report_error(_("cannot type switch on non-interface value"));
4204 return Statement::make_error_statement(loc
);
4207 // var descriptor_temp DESCRIPTOR_TYPE
4208 Type
* descriptor_type
= Type::make_type_descriptor_ptr_type();
4209 Temporary_statement
* descriptor_temp
=
4210 Statement::make_temporary(descriptor_type
, NULL
, loc
);
4211 b
->add_statement(descriptor_temp
);
4213 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4215 bool is_empty
= val_type
->interface_type()->is_empty();
4217 if (this->var_
== NULL
)
4220 ref
= Expression::make_var_reference(this->var_
, loc
);
4221 Expression
* call
= Runtime::make_call((is_empty
4222 ? Runtime::EFACETYPE
4223 : Runtime::IFACETYPE
),
4225 Temporary_reference_expression
* lhs
=
4226 Expression::make_temporary_reference(descriptor_temp
, loc
);
4227 lhs
->set_is_lvalue();
4228 Statement
* s
= Statement::make_assignment(lhs
, call
, loc
);
4229 b
->add_statement(s
);
4231 if (this->clauses_
!= NULL
)
4232 this->clauses_
->lower(val_type
, b
, descriptor_temp
, this->break_label());
4234 s
= Statement::make_unnamed_label_statement(this->break_label_
);
4235 b
->add_statement(s
);
4237 return Statement::make_block_statement(b
, loc
);
4240 // Return the break label for this type switch statement, creating it
4244 Type_switch_statement::break_label()
4246 if (this->break_label_
== NULL
)
4247 this->break_label_
= new Unnamed_label(this->location());
4248 return this->break_label_
;
4251 // Dump the AST representation for a type switch statement
4254 Type_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
4257 ast_dump_context
->print_indent();
4258 ast_dump_context
->ostream() << "switch " << this->var_
->name() << " = ";
4259 ast_dump_context
->dump_expression(this->expr_
);
4260 ast_dump_context
->ostream() << " .(type)";
4261 if (ast_dump_context
->dump_subblocks())
4263 ast_dump_context
->ostream() << " {" << std::endl
;
4264 this->clauses_
->dump_clauses(ast_dump_context
);
4265 ast_dump_context
->ostream() << "}";
4267 ast_dump_context
->ostream() << std::endl
;
4270 // Make a type switch statement.
4272 Type_switch_statement
*
4273 Statement::make_type_switch_statement(Named_object
* var
, Expression
* expr
,
4276 return new Type_switch_statement(var
, expr
, location
);
4279 // Class Send_statement.
4284 Send_statement::do_traverse(Traverse
* traverse
)
4286 if (this->traverse_expression(traverse
, &this->channel_
) == TRAVERSE_EXIT
)
4287 return TRAVERSE_EXIT
;
4288 return this->traverse_expression(traverse
, &this->val_
);
4294 Send_statement::do_determine_types()
4296 this->channel_
->determine_type_no_context();
4297 Type
* type
= this->channel_
->type();
4298 Type_context context
;
4299 if (type
->channel_type() != NULL
)
4300 context
.type
= type
->channel_type()->element_type();
4301 this->val_
->determine_type(&context
);
4307 Send_statement::do_check_types(Gogo
*)
4309 Type
* type
= this->channel_
->type();
4310 if (type
->is_error())
4312 this->set_is_error();
4315 Channel_type
* channel_type
= type
->channel_type();
4316 if (channel_type
== NULL
)
4318 error_at(this->location(), "left operand of %<<-%> must be channel");
4319 this->set_is_error();
4322 Type
* element_type
= channel_type
->element_type();
4323 if (!Type::are_assignable(element_type
, this->val_
->type(), NULL
))
4325 this->report_error(_("incompatible types in send"));
4328 if (!channel_type
->may_send())
4330 this->report_error(_("invalid send on receive-only channel"));
4335 // Convert a send statement to the backend representation.
4338 Send_statement::do_get_backend(Translate_context
* context
)
4340 Location loc
= this->location();
4342 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
4343 Type
* element_type
= channel_type
->element_type();
4344 Expression
* val
= Expression::make_cast(element_type
, this->val_
, loc
);
4347 bool can_take_address
;
4348 switch (element_type
->base()->classification())
4350 case Type::TYPE_BOOLEAN
:
4351 case Type::TYPE_INTEGER
:
4352 case Type::TYPE_FUNCTION
:
4353 case Type::TYPE_POINTER
:
4354 case Type::TYPE_MAP
:
4355 case Type::TYPE_CHANNEL
:
4357 can_take_address
= false;
4360 case Type::TYPE_FLOAT
:
4361 case Type::TYPE_COMPLEX
:
4362 case Type::TYPE_STRING
:
4363 case Type::TYPE_INTERFACE
:
4365 can_take_address
= false;
4368 case Type::TYPE_STRUCT
:
4370 can_take_address
= true;
4373 case Type::TYPE_ARRAY
:
4375 can_take_address
= !element_type
->is_slice_type();
4379 case Type::TYPE_ERROR
:
4380 case Type::TYPE_VOID
:
4381 case Type::TYPE_SINK
:
4382 case Type::TYPE_NIL
:
4383 case Type::TYPE_NAMED
:
4384 case Type::TYPE_FORWARD
:
4385 go_assert(saw_errors());
4386 return context
->backend()->error_statement();
4389 // Only try to take the address of a variable. We have already
4390 // moved variables to the heap, so this should not cause that to
4391 // happen unnecessarily.
4392 if (can_take_address
4393 && val
->var_expression() == NULL
4394 && val
->temporary_reference_expression() == NULL
)
4395 can_take_address
= false;
4397 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
4400 Runtime::Function code
;
4401 Bstatement
* btemp
= NULL
;
4404 // Type is small enough to handle as uint64.
4405 code
= Runtime::SEND_SMALL
;
4406 val
= Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
4409 else if (can_take_address
)
4411 // Must pass address of value. The function doesn't change the
4412 // value, so just take its address directly.
4413 code
= Runtime::SEND_BIG
;
4414 val
= Expression::make_unary(OPERATOR_AND
, val
, loc
);
4418 // Must pass address of value, but the value is small enough
4419 // that it might be in registers. Copy value into temporary
4420 // variable to take address.
4421 code
= Runtime::SEND_BIG
;
4422 Temporary_statement
* temp
= Statement::make_temporary(element_type
,
4424 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
4425 val
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
4426 btemp
= temp
->get_backend(context
);
4429 Expression
* call
= Runtime::make_call(code
, loc
, 3, td
, this->channel_
, val
);
4431 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4432 Bexpression
* bcall
= tree_to_expr(call
->get_tree(context
));
4433 Bstatement
* s
= context
->backend()->expression_statement(bcall
);
4438 return context
->backend()->compound_statement(btemp
, s
);
4441 // Dump the AST representation for a send statement
4444 Send_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4446 ast_dump_context
->print_indent();
4447 ast_dump_context
->dump_expression(this->channel_
);
4448 ast_dump_context
->ostream() << " <- ";
4449 ast_dump_context
->dump_expression(this->val_
);
4450 ast_dump_context
->ostream() << std::endl
;
4453 // Make a send statement.
4456 Statement::make_send_statement(Expression
* channel
, Expression
* val
,
4459 return new Send_statement(channel
, val
, location
);
4462 // Class Select_clauses::Select_clause.
4467 Select_clauses::Select_clause::traverse(Traverse
* traverse
)
4469 if (!this->is_lowered_
4470 && (traverse
->traverse_mask()
4471 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
4473 if (this->channel_
!= NULL
)
4475 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
4476 return TRAVERSE_EXIT
;
4478 if (this->val_
!= NULL
)
4480 if (Expression::traverse(&this->val_
, traverse
) == TRAVERSE_EXIT
)
4481 return TRAVERSE_EXIT
;
4483 if (this->closed_
!= NULL
)
4485 if (Expression::traverse(&this->closed_
, traverse
) == TRAVERSE_EXIT
)
4486 return TRAVERSE_EXIT
;
4489 if (this->statements_
!= NULL
)
4491 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
4492 return TRAVERSE_EXIT
;
4494 return TRAVERSE_CONTINUE
;
4497 // Lowering. We call a function to register this clause, and arrange
4498 // to set any variables in any receive clause.
4501 Select_clauses::Select_clause::lower(Gogo
* gogo
, Named_object
* function
,
4502 Block
* b
, Temporary_statement
* sel
)
4504 Location loc
= this->location_
;
4506 Expression
* selref
= Expression::make_temporary_reference(sel
, loc
);
4509 mpz_init_set_ui(ival
, this->index_
);
4510 Expression
* index_expr
= Expression::make_integer(&ival
, NULL
, loc
);
4513 if (this->is_default_
)
4515 go_assert(this->channel_
== NULL
&& this->val_
== NULL
);
4516 this->lower_default(b
, selref
, index_expr
);
4517 this->is_lowered_
= true;
4521 // Evaluate the channel before the select statement.
4522 Temporary_statement
* channel_temp
= Statement::make_temporary(NULL
,
4525 b
->add_statement(channel_temp
);
4526 Expression
* chanref
= Expression::make_temporary_reference(channel_temp
,
4530 this->lower_send(b
, selref
, chanref
, index_expr
);
4532 this->lower_recv(gogo
, function
, b
, selref
, chanref
, index_expr
);
4534 // Now all references should be handled through the statements, not
4536 this->is_lowered_
= true;
4541 // Lower a default clause in a select statement.
4544 Select_clauses::Select_clause::lower_default(Block
* b
, Expression
* selref
,
4545 Expression
* index_expr
)
4547 Location loc
= this->location_
;
4548 Expression
* call
= Runtime::make_call(Runtime::SELECTDEFAULT
, loc
, 2, selref
,
4550 b
->add_statement(Statement::make_statement(call
, true));
4553 // Lower a send clause in a select statement.
4556 Select_clauses::Select_clause::lower_send(Block
* b
, Expression
* selref
,
4557 Expression
* chanref
,
4558 Expression
* index_expr
)
4560 Location loc
= this->location_
;
4562 Channel_type
* ct
= this->channel_
->type()->channel_type();
4566 Type
* valtype
= ct
->element_type();
4568 // Note that copying the value to a temporary here means that we
4569 // evaluate the send values in the required order.
4570 Temporary_statement
* val
= Statement::make_temporary(valtype
, this->val_
,
4572 b
->add_statement(val
);
4574 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4575 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4577 Expression
* call
= Runtime::make_call(Runtime::SELECTSEND
, loc
, 4, selref
,
4578 chanref
, valaddr
, index_expr
);
4579 b
->add_statement(Statement::make_statement(call
, true));
4582 // Lower a receive clause in a select statement.
4585 Select_clauses::Select_clause::lower_recv(Gogo
* gogo
, Named_object
* function
,
4586 Block
* b
, Expression
* selref
,
4587 Expression
* chanref
,
4588 Expression
* index_expr
)
4590 Location loc
= this->location_
;
4592 Channel_type
* ct
= this->channel_
->type()->channel_type();
4596 Type
* valtype
= ct
->element_type();
4597 Temporary_statement
* val
= Statement::make_temporary(valtype
, NULL
, loc
);
4598 b
->add_statement(val
);
4600 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4601 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4603 Temporary_statement
* closed_temp
= NULL
;
4606 if (this->closed_
== NULL
&& this->closedvar_
== NULL
)
4607 call
= Runtime::make_call(Runtime::SELECTRECV
, loc
, 4, selref
, chanref
,
4608 valaddr
, index_expr
);
4611 closed_temp
= Statement::make_temporary(Type::lookup_bool_type(), NULL
,
4613 b
->add_statement(closed_temp
);
4614 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4616 Expression
* caddr
= Expression::make_unary(OPERATOR_AND
, cref
, loc
);
4617 call
= Runtime::make_call(Runtime::SELECTRECV2
, loc
, 5, selref
, chanref
,
4618 valaddr
, caddr
, index_expr
);
4621 b
->add_statement(Statement::make_statement(call
, true));
4623 // If the block of statements is executed, arrange for the received
4624 // value to move from VAL to the place where the statements expect
4629 if (this->var_
!= NULL
)
4631 go_assert(this->val_
== NULL
);
4632 valref
= Expression::make_temporary_reference(val
, loc
);
4633 this->var_
->var_value()->set_init(valref
);
4634 this->var_
->var_value()->clear_type_from_chan_element();
4636 else if (this->val_
!= NULL
&& !this->val_
->is_sink_expression())
4638 init
= new Block(b
, loc
);
4639 valref
= Expression::make_temporary_reference(val
, loc
);
4640 init
->add_statement(Statement::make_assignment(this->val_
, valref
, loc
));
4643 if (this->closedvar_
!= NULL
)
4645 go_assert(this->closed_
== NULL
);
4646 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4648 this->closedvar_
->var_value()->set_init(cref
);
4650 else if (this->closed_
!= NULL
&& !this->closed_
->is_sink_expression())
4653 init
= new Block(b
, loc
);
4654 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4656 init
->add_statement(Statement::make_assignment(this->closed_
, cref
,
4662 gogo
->lower_block(function
, init
);
4664 if (this->statements_
!= NULL
)
4665 init
->add_statement(Statement::make_block_statement(this->statements_
,
4667 this->statements_
= init
;
4674 Select_clauses::Select_clause::determine_types()
4676 go_assert(this->is_lowered_
);
4677 if (this->statements_
!= NULL
)
4678 this->statements_
->determine_types();
4684 Select_clauses::Select_clause::check_types()
4686 if (this->is_default_
)
4689 Channel_type
* ct
= this->channel_
->type()->channel_type();
4692 error_at(this->channel_
->location(), "expected channel");
4696 if (this->is_send_
&& !ct
->may_send())
4697 error_at(this->location(), "invalid send on receive-only channel");
4698 else if (!this->is_send_
&& !ct
->may_receive())
4699 error_at(this->location(), "invalid receive on send-only channel");
4702 // Whether this clause may fall through to the statement which follows
4703 // the overall select statement.
4706 Select_clauses::Select_clause::may_fall_through() const
4708 if (this->statements_
== NULL
)
4710 return this->statements_
->may_fall_through();
4713 // Return the backend representation for the statements to execute.
4716 Select_clauses::Select_clause::get_statements_backend(
4717 Translate_context
* context
)
4719 if (this->statements_
== NULL
)
4721 Bblock
* bblock
= this->statements_
->get_backend(context
);
4722 return context
->backend()->block_statement(bblock
);
4725 // Dump the AST representation for a select case clause
4728 Select_clauses::Select_clause::dump_clause(
4729 Ast_dump_context
* ast_dump_context
) const
4731 ast_dump_context
->print_indent();
4732 if (this->is_default_
)
4734 ast_dump_context
->ostream() << "default:";
4738 ast_dump_context
->ostream() << "case " ;
4741 ast_dump_context
->dump_expression(this->channel_
);
4742 ast_dump_context
->ostream() << " <- " ;
4743 if (this->val_
!= NULL
)
4744 ast_dump_context
->dump_expression(this->val_
);
4748 if (this->val_
!= NULL
)
4749 ast_dump_context
->dump_expression(this->val_
);
4750 if (this->closed_
!= NULL
)
4752 // FIXME: can val_ == NULL and closed_ ! = NULL?
4753 ast_dump_context
->ostream() << " , " ;
4754 ast_dump_context
->dump_expression(this->closed_
);
4756 if (this->closedvar_
!= NULL
|| this->var_
!= NULL
)
4757 ast_dump_context
->ostream() << " := " ;
4759 ast_dump_context
->ostream() << " <- " ;
4760 ast_dump_context
->dump_expression(this->channel_
);
4762 ast_dump_context
->ostream() << ":" ;
4764 ast_dump_context
->dump_block(this->statements_
);
4767 // Class Select_clauses.
4772 Select_clauses::traverse(Traverse
* traverse
)
4774 for (Clauses::iterator p
= this->clauses_
.begin();
4775 p
!= this->clauses_
.end();
4778 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4779 return TRAVERSE_EXIT
;
4781 return TRAVERSE_CONTINUE
;
4784 // Lowering. Here we pull out the channel and the send values, to
4785 // enforce the order of evaluation. We also add explicit send and
4786 // receive statements to the clauses.
4789 Select_clauses::lower(Gogo
* gogo
, Named_object
* function
, Block
* b
,
4790 Temporary_statement
* sel
)
4792 for (Clauses::iterator p
= this->clauses_
.begin();
4793 p
!= this->clauses_
.end();
4795 p
->lower(gogo
, function
, b
, sel
);
4801 Select_clauses::determine_types()
4803 for (Clauses::iterator p
= this->clauses_
.begin();
4804 p
!= this->clauses_
.end();
4806 p
->determine_types();
4812 Select_clauses::check_types()
4814 for (Clauses::iterator p
= this->clauses_
.begin();
4815 p
!= this->clauses_
.end();
4820 // Return whether these select clauses fall through to the statement
4821 // following the overall select statement.
4824 Select_clauses::may_fall_through() const
4826 for (Clauses::const_iterator p
= this->clauses_
.begin();
4827 p
!= this->clauses_
.end();
4829 if (p
->may_fall_through())
4834 // Convert to the backend representation. We have already accumulated
4835 // all the select information. Now we call selectgo, which will
4836 // return the index of the clause to execute.
4839 Select_clauses::get_backend(Translate_context
* context
,
4840 Temporary_statement
* sel
,
4841 Unnamed_label
*break_label
,
4844 size_t count
= this->clauses_
.size();
4845 std::vector
<std::vector
<Bexpression
*> > cases(count
);
4846 std::vector
<Bstatement
*> clauses(count
);
4848 Type
* int32_type
= Type::lookup_integer_type("int32");
4851 for (Clauses::iterator p
= this->clauses_
.begin();
4852 p
!= this->clauses_
.end();
4855 int index
= p
->index();
4857 mpz_init_set_ui(ival
, index
);
4858 Expression
* index_expr
= Expression::make_integer(&ival
, int32_type
,
4861 cases
[i
].push_back(tree_to_expr(index_expr
->get_tree(context
)));
4863 Bstatement
* s
= p
->get_statements_backend(context
);
4864 Location gloc
= (p
->statements() == NULL
4866 : p
->statements()->end_location());
4867 Bstatement
* g
= break_label
->get_goto(context
, gloc
);
4872 clauses
[i
] = context
->backend()->compound_statement(s
, g
);
4875 Expression
* selref
= Expression::make_temporary_reference(sel
, location
);
4876 Expression
* call
= Runtime::make_call(Runtime::SELECTGO
, location
, 1,
4878 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4879 Bexpression
* bcall
= tree_to_expr(call
->get_tree(context
));
4882 return context
->backend()->expression_statement(bcall
);
4884 std::vector
<Bstatement
*> statements
;
4885 statements
.reserve(2);
4887 Bstatement
* switch_stmt
= context
->backend()->switch_statement(bcall
,
4891 statements
.push_back(switch_stmt
);
4893 Bstatement
* ldef
= break_label
->get_definition(context
);
4894 statements
.push_back(ldef
);
4896 return context
->backend()->statement_list(statements
);
4898 // Dump the AST representation for select clauses.
4901 Select_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4903 for (Clauses::const_iterator p
= this->clauses_
.begin();
4904 p
!= this->clauses_
.end();
4906 p
->dump_clause(ast_dump_context
);
4909 // Class Select_statement.
4911 // Return the break label for this switch statement, creating it if
4915 Select_statement::break_label()
4917 if (this->break_label_
== NULL
)
4918 this->break_label_
= new Unnamed_label(this->location());
4919 return this->break_label_
;
4922 // Lower a select statement. This will still return a select
4923 // statement, but it will be modified to implement the order of
4924 // evaluation rules, and to include the send and receive statements as
4925 // explicit statements in the clauses.
4928 Select_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
4929 Block
* enclosing
, Statement_inserter
*)
4931 if (this->is_lowered_
)
4934 Location loc
= this->location();
4936 Block
* b
= new Block(enclosing
, loc
);
4938 go_assert(this->sel_
== NULL
);
4941 mpz_init_set_ui(ival
, this->clauses_
->size());
4942 Expression
* size_expr
= Expression::make_integer(&ival
, NULL
, loc
);
4945 Expression
* call
= Runtime::make_call(Runtime::NEWSELECT
, loc
, 1, size_expr
);
4947 this->sel_
= Statement::make_temporary(NULL
, call
, loc
);
4948 b
->add_statement(this->sel_
);
4950 this->clauses_
->lower(gogo
, function
, b
, this->sel_
);
4951 this->is_lowered_
= true;
4952 b
->add_statement(this);
4954 return Statement::make_block_statement(b
, loc
);
4957 // Return the backend representation for a select statement.
4960 Select_statement::do_get_backend(Translate_context
* context
)
4962 return this->clauses_
->get_backend(context
, this->sel_
, this->break_label(),
4966 // Dump the AST representation for a select statement.
4969 Select_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4971 ast_dump_context
->print_indent();
4972 ast_dump_context
->ostream() << "select";
4973 if (ast_dump_context
->dump_subblocks())
4975 ast_dump_context
->ostream() << " {" << std::endl
;
4976 this->clauses_
->dump_clauses(ast_dump_context
);
4977 ast_dump_context
->ostream() << "}";
4979 ast_dump_context
->ostream() << std::endl
;
4982 // Make a select statement.
4985 Statement::make_select_statement(Location location
)
4987 return new Select_statement(location
);
4990 // Class For_statement.
4995 For_statement::do_traverse(Traverse
* traverse
)
4997 if (this->init_
!= NULL
)
4999 if (this->init_
->traverse(traverse
) == TRAVERSE_EXIT
)
5000 return TRAVERSE_EXIT
;
5002 if (this->cond_
!= NULL
)
5004 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
)
5005 return TRAVERSE_EXIT
;
5007 if (this->post_
!= NULL
)
5009 if (this->post_
->traverse(traverse
) == TRAVERSE_EXIT
)
5010 return TRAVERSE_EXIT
;
5012 return this->statements_
->traverse(traverse
);
5015 // Lower a For_statement into if statements and gotos. Getting rid of
5016 // complex statements make it easier to handle garbage collection.
5019 For_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
5020 Statement_inserter
*)
5023 Location loc
= this->location();
5025 Block
* b
= new Block(enclosing
, this->location());
5026 if (this->init_
!= NULL
)
5028 s
= Statement::make_block_statement(this->init_
,
5029 this->init_
->start_location());
5030 b
->add_statement(s
);
5033 Unnamed_label
* entry
= NULL
;
5034 if (this->cond_
!= NULL
)
5036 entry
= new Unnamed_label(this->location());
5037 b
->add_statement(Statement::make_goto_unnamed_statement(entry
, loc
));
5040 Unnamed_label
* top
= new Unnamed_label(this->location());
5041 b
->add_statement(Statement::make_unnamed_label_statement(top
));
5043 s
= Statement::make_block_statement(this->statements_
,
5044 this->statements_
->start_location());
5045 b
->add_statement(s
);
5047 Location end_loc
= this->statements_
->end_location();
5049 Unnamed_label
* cont
= this->continue_label_
;
5051 b
->add_statement(Statement::make_unnamed_label_statement(cont
));
5053 if (this->post_
!= NULL
)
5055 s
= Statement::make_block_statement(this->post_
,
5056 this->post_
->start_location());
5057 b
->add_statement(s
);
5058 end_loc
= this->post_
->end_location();
5061 if (this->cond_
== NULL
)
5062 b
->add_statement(Statement::make_goto_unnamed_statement(top
, end_loc
));
5065 b
->add_statement(Statement::make_unnamed_label_statement(entry
));
5067 Location cond_loc
= this->cond_
->location();
5068 Block
* then_block
= new Block(b
, cond_loc
);
5069 s
= Statement::make_goto_unnamed_statement(top
, cond_loc
);
5070 then_block
->add_statement(s
);
5072 s
= Statement::make_if_statement(this->cond_
, then_block
, NULL
, cond_loc
);
5073 b
->add_statement(s
);
5076 Unnamed_label
* brk
= this->break_label_
;
5078 b
->add_statement(Statement::make_unnamed_label_statement(brk
));
5080 b
->set_end_location(end_loc
);
5082 return Statement::make_block_statement(b
, loc
);
5085 // Return the break label, creating it if necessary.
5088 For_statement::break_label()
5090 if (this->break_label_
== NULL
)
5091 this->break_label_
= new Unnamed_label(this->location());
5092 return this->break_label_
;
5095 // Return the continue LABEL_EXPR.
5098 For_statement::continue_label()
5100 if (this->continue_label_
== NULL
)
5101 this->continue_label_
= new Unnamed_label(this->location());
5102 return this->continue_label_
;
5105 // Set the break and continue labels a for statement. This is used
5106 // when lowering a for range statement.
5109 For_statement::set_break_continue_labels(Unnamed_label
* break_label
,
5110 Unnamed_label
* continue_label
)
5112 go_assert(this->break_label_
== NULL
&& this->continue_label_
== NULL
);
5113 this->break_label_
= break_label
;
5114 this->continue_label_
= continue_label
;
5117 // Dump the AST representation for a for statement.
5120 For_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5122 if (this->init_
!= NULL
&& ast_dump_context
->dump_subblocks())
5124 ast_dump_context
->print_indent();
5125 ast_dump_context
->indent();
5126 ast_dump_context
->ostream() << "// INIT " << std::endl
;
5127 ast_dump_context
->dump_block(this->init_
);
5128 ast_dump_context
->unindent();
5130 ast_dump_context
->print_indent();
5131 ast_dump_context
->ostream() << "for ";
5132 if (this->cond_
!= NULL
)
5133 ast_dump_context
->dump_expression(this->cond_
);
5135 if (ast_dump_context
->dump_subblocks())
5137 ast_dump_context
->ostream() << " {" << std::endl
;
5138 ast_dump_context
->dump_block(this->statements_
);
5139 if (this->init_
!= NULL
)
5141 ast_dump_context
->print_indent();
5142 ast_dump_context
->ostream() << "// POST " << std::endl
;
5143 ast_dump_context
->dump_block(this->post_
);
5145 ast_dump_context
->unindent();
5147 ast_dump_context
->print_indent();
5148 ast_dump_context
->ostream() << "}";
5151 ast_dump_context
->ostream() << std::endl
;
5154 // Make a for statement.
5157 Statement::make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
5160 return new For_statement(init
, cond
, post
, location
);
5163 // Class For_range_statement.
5168 For_range_statement::do_traverse(Traverse
* traverse
)
5170 if (this->traverse_expression(traverse
, &this->index_var_
) == TRAVERSE_EXIT
)
5171 return TRAVERSE_EXIT
;
5172 if (this->value_var_
!= NULL
)
5174 if (this->traverse_expression(traverse
, &this->value_var_
)
5176 return TRAVERSE_EXIT
;
5178 if (this->traverse_expression(traverse
, &this->range_
) == TRAVERSE_EXIT
)
5179 return TRAVERSE_EXIT
;
5180 return this->statements_
->traverse(traverse
);
5183 // Lower a for range statement. For simplicity we lower this into a
5184 // for statement, which will then be lowered in turn to goto
5188 For_range_statement::do_lower(Gogo
* gogo
, Named_object
*, Block
* enclosing
,
5189 Statement_inserter
*)
5191 Type
* range_type
= this->range_
->type();
5192 if (range_type
->points_to() != NULL
5193 && range_type
->points_to()->array_type() != NULL
5194 && !range_type
->points_to()->is_slice_type())
5195 range_type
= range_type
->points_to();
5198 Type
* value_type
= NULL
;
5199 if (range_type
->array_type() != NULL
)
5201 index_type
= Type::lookup_integer_type("int");
5202 value_type
= range_type
->array_type()->element_type();
5204 else if (range_type
->is_string_type())
5206 index_type
= Type::lookup_integer_type("int");
5207 value_type
= Type::lookup_integer_type("int32");
5209 else if (range_type
->map_type() != NULL
)
5211 index_type
= range_type
->map_type()->key_type();
5212 value_type
= range_type
->map_type()->val_type();
5214 else if (range_type
->channel_type() != NULL
)
5216 index_type
= range_type
->channel_type()->element_type();
5217 if (this->value_var_
!= NULL
)
5219 if (!this->value_var_
->type()->is_error())
5220 this->report_error(_("too many variables for range clause "
5222 return Statement::make_error_statement(this->location());
5227 this->report_error(_("range clause must have "
5228 "array, slice, string, map, or channel type"));
5229 return Statement::make_error_statement(this->location());
5232 Location loc
= this->location();
5233 Block
* temp_block
= new Block(enclosing
, loc
);
5235 Named_object
* range_object
= NULL
;
5236 Temporary_statement
* range_temp
= NULL
;
5237 Var_expression
* ve
= this->range_
->var_expression();
5239 range_object
= ve
->named_object();
5242 range_temp
= Statement::make_temporary(NULL
, this->range_
, loc
);
5243 temp_block
->add_statement(range_temp
);
5244 this->range_
= NULL
;
5247 Temporary_statement
* index_temp
= Statement::make_temporary(index_type
,
5249 temp_block
->add_statement(index_temp
);
5251 Temporary_statement
* value_temp
= NULL
;
5252 if (this->value_var_
!= NULL
)
5254 value_temp
= Statement::make_temporary(value_type
, NULL
, loc
);
5255 temp_block
->add_statement(value_temp
);
5258 Block
* body
= new Block(temp_block
, loc
);
5265 // Arrange to do a loop appropriate for the type. We will produce
5266 // for INIT ; COND ; POST {
5268 // INDEX = INDEX_TEMP
5269 // VALUE = VALUE_TEMP // If there is a value
5270 // original statements
5273 if (range_type
->is_slice_type())
5274 this->lower_range_slice(gogo
, temp_block
, body
, range_object
, range_temp
,
5275 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5277 else if (range_type
->array_type() != NULL
)
5278 this->lower_range_array(gogo
, temp_block
, body
, range_object
, range_temp
,
5279 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5281 else if (range_type
->is_string_type())
5282 this->lower_range_string(gogo
, temp_block
, body
, range_object
, range_temp
,
5283 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5285 else if (range_type
->map_type() != NULL
)
5286 this->lower_range_map(gogo
, temp_block
, body
, range_object
, range_temp
,
5287 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5289 else if (range_type
->channel_type() != NULL
)
5290 this->lower_range_channel(gogo
, temp_block
, body
, range_object
, range_temp
,
5291 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5296 if (iter_init
!= NULL
)
5297 body
->add_statement(Statement::make_block_statement(iter_init
, loc
));
5300 Expression
* index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5301 if (this->value_var_
== NULL
)
5303 assign
= Statement::make_assignment(this->index_var_
, index_ref
, loc
);
5307 Expression_list
* lhs
= new Expression_list();
5308 lhs
->push_back(this->index_var_
);
5309 lhs
->push_back(this->value_var_
);
5311 Expression_list
* rhs
= new Expression_list();
5312 rhs
->push_back(index_ref
);
5313 rhs
->push_back(Expression::make_temporary_reference(value_temp
, loc
));
5315 assign
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5317 body
->add_statement(assign
);
5319 body
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
5321 body
->set_end_location(this->statements_
->end_location());
5323 For_statement
* loop
= Statement::make_for_statement(init
, cond
, post
,
5325 loop
->add_statements(body
);
5326 loop
->set_break_continue_labels(this->break_label_
, this->continue_label_
);
5328 temp_block
->add_statement(loop
);
5330 return Statement::make_block_statement(temp_block
, loc
);
5333 // Return a reference to the range, which may be in RANGE_OBJECT or in
5337 For_range_statement::make_range_ref(Named_object
* range_object
,
5338 Temporary_statement
* range_temp
,
5341 if (range_object
!= NULL
)
5342 return Expression::make_var_reference(range_object
, loc
);
5344 return Expression::make_temporary_reference(range_temp
, loc
);
5347 // Return a call to the predeclared function FUNCNAME passing a
5348 // reference to the temporary variable ARG.
5351 For_range_statement::call_builtin(Gogo
* gogo
, const char* funcname
,
5355 Named_object
* no
= gogo
->lookup_global(funcname
);
5356 go_assert(no
!= NULL
&& no
->is_function_declaration());
5357 Expression
* func
= Expression::make_func_reference(no
, NULL
, loc
);
5358 Expression_list
* params
= new Expression_list();
5359 params
->push_back(arg
);
5360 return Expression::make_call(func
, params
, false, loc
);
5363 // Lower a for range over an array.
5366 For_range_statement::lower_range_array(Gogo
* gogo
,
5369 Named_object
* range_object
,
5370 Temporary_statement
* range_temp
,
5371 Temporary_statement
* index_temp
,
5372 Temporary_statement
* value_temp
,
5378 Location loc
= this->location();
5380 // The loop we generate:
5381 // len_temp := len(range)
5382 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5383 // value_temp = range[index_temp]
5384 // index = index_temp
5385 // value = value_temp
5391 // len_temp = len(range)
5394 Block
* init
= new Block(enclosing
, loc
);
5396 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5397 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5398 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5400 init
->add_statement(len_temp
);
5403 mpz_init_set_ui(zval
, 0UL);
5404 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5407 Temporary_reference_expression
* tref
=
5408 Expression::make_temporary_reference(index_temp
, loc
);
5409 tref
->set_is_lvalue();
5410 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5411 init
->add_statement(s
);
5416 // index_temp < len_temp
5418 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5419 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5420 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5424 // Set *PITER_INIT to
5425 // value_temp = range[index_temp]
5427 Block
* iter_init
= NULL
;
5428 if (value_temp
!= NULL
)
5430 iter_init
= new Block(body_block
, loc
);
5432 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5433 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5434 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, loc
);
5436 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5437 tref
->set_is_lvalue();
5438 s
= Statement::make_assignment(tref
, index
, loc
);
5440 iter_init
->add_statement(s
);
5442 *piter_init
= iter_init
;
5447 Block
* post
= new Block(enclosing
, loc
);
5448 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5449 tref
->set_is_lvalue();
5450 s
= Statement::make_inc_statement(tref
);
5451 post
->add_statement(s
);
5455 // Lower a for range over a slice.
5458 For_range_statement::lower_range_slice(Gogo
* gogo
,
5461 Named_object
* range_object
,
5462 Temporary_statement
* range_temp
,
5463 Temporary_statement
* index_temp
,
5464 Temporary_statement
* value_temp
,
5470 Location loc
= this->location();
5472 // The loop we generate:
5473 // for_temp := range
5474 // len_temp := len(for_temp)
5475 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5476 // value_temp = for_temp[index_temp]
5477 // index = index_temp
5478 // value = value_temp
5482 // Using for_temp means that we don't need to check bounds when
5483 // fetching range_temp[index_temp].
5486 // range_temp := range
5488 // len_temp = len(range_temp)
5491 Block
* init
= new Block(enclosing
, loc
);
5493 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5494 Temporary_statement
* for_temp
= Statement::make_temporary(NULL
, ref
, loc
);
5495 init
->add_statement(for_temp
);
5497 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5498 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5499 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5501 init
->add_statement(len_temp
);
5504 mpz_init_set_ui(zval
, 0UL);
5505 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5508 Temporary_reference_expression
* tref
=
5509 Expression::make_temporary_reference(index_temp
, loc
);
5510 tref
->set_is_lvalue();
5511 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5512 init
->add_statement(s
);
5517 // index_temp < len_temp
5519 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5520 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5521 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5525 // Set *PITER_INIT to
5526 // value_temp = range[index_temp]
5528 Block
* iter_init
= NULL
;
5529 if (value_temp
!= NULL
)
5531 iter_init
= new Block(body_block
, loc
);
5533 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5534 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5535 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, loc
);
5537 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5538 tref
->set_is_lvalue();
5539 s
= Statement::make_assignment(tref
, index
, loc
);
5541 iter_init
->add_statement(s
);
5543 *piter_init
= iter_init
;
5548 Block
* post
= new Block(enclosing
, loc
);
5549 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5550 tref
->set_is_lvalue();
5551 s
= Statement::make_inc_statement(tref
);
5552 post
->add_statement(s
);
5556 // Lower a for range over a string.
5559 For_range_statement::lower_range_string(Gogo
*,
5562 Named_object
* range_object
,
5563 Temporary_statement
* range_temp
,
5564 Temporary_statement
* index_temp
,
5565 Temporary_statement
* value_temp
,
5571 Location loc
= this->location();
5573 // The loop we generate:
5574 // var next_index_temp int
5575 // for index_temp = 0; ; index_temp = next_index_temp {
5576 // next_index_temp, value_temp = stringiter2(range, index_temp)
5577 // if next_index_temp == 0 {
5580 // index = index_temp
5581 // value = value_temp
5586 // var next_index_temp int
5589 Block
* init
= new Block(enclosing
, loc
);
5591 Temporary_statement
* next_index_temp
=
5592 Statement::make_temporary(index_temp
->type(), NULL
, loc
);
5593 init
->add_statement(next_index_temp
);
5596 mpz_init_set_ui(zval
, 0UL);
5597 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5599 Temporary_reference_expression
* ref
=
5600 Expression::make_temporary_reference(index_temp
, loc
);
5601 ref
->set_is_lvalue();
5602 Statement
* s
= Statement::make_assignment(ref
, zexpr
, loc
);
5604 init
->add_statement(s
);
5607 // The loop has no condition.
5611 // Set *PITER_INIT to
5612 // next_index_temp = runtime.stringiter(range, index_temp)
5614 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
5616 // if next_index_temp == 0 {
5620 Block
* iter_init
= new Block(body_block
, loc
);
5622 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5623 Expression
* p2
= Expression::make_temporary_reference(index_temp
, loc
);
5624 Call_expression
* call
= Runtime::make_call((value_temp
== NULL
5625 ? Runtime::STRINGITER
5626 : Runtime::STRINGITER2
),
5629 if (value_temp
== NULL
)
5631 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5632 ref
->set_is_lvalue();
5633 s
= Statement::make_assignment(ref
, call
, loc
);
5637 Expression_list
* lhs
= new Expression_list();
5639 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5640 ref
->set_is_lvalue();
5641 lhs
->push_back(ref
);
5643 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5644 ref
->set_is_lvalue();
5645 lhs
->push_back(ref
);
5647 Expression_list
* rhs
= new Expression_list();
5648 rhs
->push_back(Expression::make_call_result(call
, 0));
5649 rhs
->push_back(Expression::make_call_result(call
, 1));
5651 s
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5653 iter_init
->add_statement(s
);
5655 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5656 zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5658 Expression
* equals
= Expression::make_binary(OPERATOR_EQEQ
, ref
, zexpr
, loc
);
5660 Block
* then_block
= new Block(iter_init
, loc
);
5661 s
= Statement::make_break_statement(this->break_label(), loc
);
5662 then_block
->add_statement(s
);
5664 s
= Statement::make_if_statement(equals
, then_block
, NULL
, loc
);
5665 iter_init
->add_statement(s
);
5667 *piter_init
= iter_init
;
5670 // index_temp = next_index_temp
5672 Block
* post
= new Block(enclosing
, loc
);
5674 Temporary_reference_expression
* lhs
=
5675 Expression::make_temporary_reference(index_temp
, loc
);
5676 lhs
->set_is_lvalue();
5677 Expression
* rhs
= Expression::make_temporary_reference(next_index_temp
, loc
);
5678 s
= Statement::make_assignment(lhs
, rhs
, loc
);
5680 post
->add_statement(s
);
5684 // Lower a for range over a map.
5687 For_range_statement::lower_range_map(Gogo
*,
5690 Named_object
* range_object
,
5691 Temporary_statement
* range_temp
,
5692 Temporary_statement
* index_temp
,
5693 Temporary_statement
* value_temp
,
5699 Location loc
= this->location();
5701 // The runtime uses a struct to handle ranges over a map. The
5702 // struct is four pointers long. The first pointer is NULL when we
5703 // have completed the iteration.
5705 // The loop we generate:
5706 // var hiter map_iteration_struct
5707 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
5708 // mapiter2(hiter, &index_temp, &value_temp)
5709 // index = index_temp
5710 // value = value_temp
5715 // var hiter map_iteration_struct
5716 // runtime.mapiterinit(range, &hiter)
5718 Block
* init
= new Block(enclosing
, loc
);
5720 Type
* map_iteration_type
= Runtime::map_iteration_type();
5721 Temporary_statement
* hiter
= Statement::make_temporary(map_iteration_type
,
5723 init
->add_statement(hiter
);
5725 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5726 Expression
* ref
= Expression::make_temporary_reference(hiter
, loc
);
5727 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5728 Expression
* call
= Runtime::make_call(Runtime::MAPITERINIT
, loc
, 2, p1
, p2
);
5729 init
->add_statement(Statement::make_statement(call
, true));
5736 ref
= Expression::make_temporary_reference(hiter
, loc
);
5739 mpz_init_set_ui(zval
, 0UL);
5740 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5743 Expression
* index
= Expression::make_index(ref
, zexpr
, NULL
, loc
);
5745 Expression
* ne
= Expression::make_binary(OPERATOR_NOTEQ
, index
,
5746 Expression::make_nil(loc
),
5751 // Set *PITER_INIT to
5752 // mapiter1(hiter, &index_temp)
5754 // mapiter2(hiter, &index_temp, &value_temp)
5756 Block
* iter_init
= new Block(body_block
, loc
);
5758 ref
= Expression::make_temporary_reference(hiter
, loc
);
5759 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5760 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5761 p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5762 if (value_temp
== NULL
)
5763 call
= Runtime::make_call(Runtime::MAPITER1
, loc
, 2, p1
, p2
);
5766 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5767 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5768 call
= Runtime::make_call(Runtime::MAPITER2
, loc
, 3, p1
, p2
, p3
);
5770 iter_init
->add_statement(Statement::make_statement(call
, true));
5772 *piter_init
= iter_init
;
5775 // mapiternext(&hiter)
5777 Block
* post
= new Block(enclosing
, loc
);
5779 ref
= Expression::make_temporary_reference(hiter
, loc
);
5780 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5781 call
= Runtime::make_call(Runtime::MAPITERNEXT
, loc
, 1, p1
);
5782 post
->add_statement(Statement::make_statement(call
, true));
5787 // Lower a for range over a channel.
5790 For_range_statement::lower_range_channel(Gogo
*,
5793 Named_object
* range_object
,
5794 Temporary_statement
* range_temp
,
5795 Temporary_statement
* index_temp
,
5796 Temporary_statement
* value_temp
,
5802 go_assert(value_temp
== NULL
);
5804 Location loc
= this->location();
5806 // The loop we generate:
5808 // index_temp, ok_temp = <-range
5812 // index = index_temp
5816 // We have no initialization code, no condition, and no post code.
5822 // Set *PITER_INIT to
5823 // index_temp, ok_temp = <-range
5828 Block
* iter_init
= new Block(body_block
, loc
);
5830 Temporary_statement
* ok_temp
=
5831 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
5832 iter_init
->add_statement(ok_temp
);
5834 Expression
* cref
= this->make_range_ref(range_object
, range_temp
, loc
);
5835 Temporary_reference_expression
* iref
=
5836 Expression::make_temporary_reference(index_temp
, loc
);
5837 iref
->set_is_lvalue();
5838 Temporary_reference_expression
* oref
=
5839 Expression::make_temporary_reference(ok_temp
, loc
);
5840 oref
->set_is_lvalue();
5841 Statement
* s
= Statement::make_tuple_receive_assignment(iref
, oref
, cref
,
5843 iter_init
->add_statement(s
);
5845 Block
* then_block
= new Block(iter_init
, loc
);
5846 s
= Statement::make_break_statement(this->break_label(), loc
);
5847 then_block
->add_statement(s
);
5849 oref
= Expression::make_temporary_reference(ok_temp
, loc
);
5850 Expression
* cond
= Expression::make_unary(OPERATOR_NOT
, oref
, loc
);
5851 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
5852 iter_init
->add_statement(s
);
5854 *piter_init
= iter_init
;
5857 // Return the break LABEL_EXPR.
5860 For_range_statement::break_label()
5862 if (this->break_label_
== NULL
)
5863 this->break_label_
= new Unnamed_label(this->location());
5864 return this->break_label_
;
5867 // Return the continue LABEL_EXPR.
5870 For_range_statement::continue_label()
5872 if (this->continue_label_
== NULL
)
5873 this->continue_label_
= new Unnamed_label(this->location());
5874 return this->continue_label_
;
5877 // Dump the AST representation for a for range statement.
5880 For_range_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5883 ast_dump_context
->print_indent();
5884 ast_dump_context
->ostream() << "for ";
5885 ast_dump_context
->dump_expression(this->index_var_
);
5886 if (this->value_var_
!= NULL
)
5888 ast_dump_context
->ostream() << ", ";
5889 ast_dump_context
->dump_expression(this->value_var_
);
5892 ast_dump_context
->ostream() << " = range ";
5893 ast_dump_context
->dump_expression(this->range_
);
5894 if (ast_dump_context
->dump_subblocks())
5896 ast_dump_context
->ostream() << " {" << std::endl
;
5898 ast_dump_context
->indent();
5900 ast_dump_context
->dump_block(this->statements_
);
5902 ast_dump_context
->unindent();
5903 ast_dump_context
->print_indent();
5904 ast_dump_context
->ostream() << "}";
5906 ast_dump_context
->ostream() << std::endl
;
5909 // Make a for statement with a range clause.
5911 For_range_statement
*
5912 Statement::make_for_range_statement(Expression
* index_var
,
5913 Expression
* value_var
,
5917 return new For_range_statement(index_var
, value_var
, range
, location
);