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.
13 #include "expressions.h"
17 #include "statements.h"
22 Statement::Statement(Statement_classification classification
,
23 source_location location
)
24 : classification_(classification
), location_(location
)
28 Statement::~Statement()
32 // Traverse the tree. The work of walking the components is handled
36 Statement::traverse(Block
* block
, size_t* pindex
, Traverse
* traverse
)
38 if (this->classification_
== STATEMENT_ERROR
)
39 return TRAVERSE_CONTINUE
;
41 unsigned int traverse_mask
= traverse
->traverse_mask();
43 if ((traverse_mask
& Traverse::traverse_statements
) != 0)
45 int t
= traverse
->statement(block
, pindex
, this);
46 if (t
== TRAVERSE_EXIT
)
48 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
49 return TRAVERSE_CONTINUE
;
52 // No point in checking traverse_mask here--a statement may contain
53 // other blocks or statements, and if we got here we always want to
55 return this->do_traverse(traverse
);
58 // Traverse the contents of a statement.
61 Statement::traverse_contents(Traverse
* traverse
)
63 return this->do_traverse(traverse
);
66 // Traverse assignments.
69 Statement::traverse_assignments(Traverse_assignments
* tassign
)
71 if (this->classification_
== STATEMENT_ERROR
)
73 return this->do_traverse_assignments(tassign
);
76 // Traverse an expression in a statement. This is a helper function
80 Statement::traverse_expression(Traverse
* traverse
, Expression
** expr
)
82 if ((traverse
->traverse_mask()
83 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
84 return TRAVERSE_CONTINUE
;
85 return Expression::traverse(expr
, traverse
);
88 // Traverse an expression list in a statement. This is a helper
89 // function for child classes.
92 Statement::traverse_expression_list(Traverse
* traverse
,
93 Expression_list
* expr_list
)
95 if (expr_list
== NULL
)
96 return TRAVERSE_CONTINUE
;
97 if ((traverse
->traverse_mask()
98 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
99 return TRAVERSE_CONTINUE
;
100 return expr_list
->traverse(traverse
);
103 // Traverse a type in a statement. This is a helper function for
107 Statement::traverse_type(Traverse
* traverse
, Type
* type
)
109 if ((traverse
->traverse_mask()
110 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
111 return TRAVERSE_CONTINUE
;
112 return Type::traverse(type
, traverse
);
115 // Set type information for unnamed constants. This is really done by
119 Statement::determine_types()
121 this->do_determine_types();
124 // If this is a thunk statement, return it.
127 Statement::thunk_statement()
129 Thunk_statement
* ret
= this->convert
<Thunk_statement
, STATEMENT_GO
>();
131 ret
= this->convert
<Thunk_statement
, STATEMENT_DEFER
>();
135 // Convert a Statement to the backend representation. This is really
136 // done by the child class.
139 Statement::get_backend(Translate_context
* context
)
141 if (this->classification_
== STATEMENT_ERROR
)
142 return context
->backend()->error_statement();
143 return this->do_get_backend(context
);
146 // Dump AST representation for a statement to a dump context.
149 Statement::dump_statement(Ast_dump_context
* ast_dump_context
) const
151 this->do_dump_statement(ast_dump_context
);
154 // Note that this statement is erroneous. This is called by children
155 // when they discover an error.
158 Statement::set_is_error()
160 this->classification_
= STATEMENT_ERROR
;
163 // For children to call to report an error conveniently.
166 Statement::report_error(const char* msg
)
168 error_at(this->location_
, "%s", msg
);
169 this->set_is_error();
172 // An error statement, used to avoid crashing after we report an
175 class Error_statement
: public Statement
178 Error_statement(source_location location
)
179 : Statement(STATEMENT_ERROR
, location
)
184 do_traverse(Traverse
*)
185 { return TRAVERSE_CONTINUE
; }
188 do_get_backend(Translate_context
*)
189 { go_unreachable(); }
192 do_dump_statement(Ast_dump_context
*) const;
195 // Dump the AST representation for an error statement.
198 Error_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
200 ast_dump_context
->print_indent();
201 ast_dump_context
->ostream() << "Error statement" << std::endl
;
204 // Make an error statement.
207 Statement::make_error_statement(source_location location
)
209 return new Error_statement(location
);
212 // Class Variable_declaration_statement.
214 Variable_declaration_statement::Variable_declaration_statement(
216 : Statement(STATEMENT_VARIABLE_DECLARATION
, var
->var_value()->location()),
221 // We don't actually traverse the variable here; it was traversed
222 // while traversing the Block.
225 Variable_declaration_statement::do_traverse(Traverse
*)
227 return TRAVERSE_CONTINUE
;
230 // Traverse the assignments in a variable declaration. Note that this
231 // traversal is different from the usual traversal.
234 Variable_declaration_statement::do_traverse_assignments(
235 Traverse_assignments
* tassign
)
237 tassign
->initialize_variable(this->var_
);
241 // Lower the variable's initialization expression.
244 Variable_declaration_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
245 Block
*, Statement_inserter
* inserter
)
247 this->var_
->var_value()->lower_init_expression(gogo
, function
, inserter
);
251 // Convert a variable declaration to the backend representation.
254 Variable_declaration_statement::do_get_backend(Translate_context
* context
)
256 Variable
* var
= this->var_
->var_value();
257 Bvariable
* bvar
= this->var_
->get_backend_variable(context
->gogo(),
258 context
->function());
259 tree init
= var
->get_init_tree(context
->gogo(), context
->function());
260 Bexpression
* binit
= init
== NULL
? NULL
: tree_to_expr(init
);
262 if (!var
->is_in_heap())
264 go_assert(binit
!= NULL
);
265 return context
->backend()->init_statement(bvar
, binit
);
268 // Something takes the address of this variable, so the value is
269 // stored in the heap. Initialize it to newly allocated memory
270 // space, and assign the initial value to the new space.
271 source_location loc
= this->location();
272 Named_object
* newfn
= context
->gogo()->lookup_global("new");
273 go_assert(newfn
!= NULL
&& newfn
->is_function_declaration());
274 Expression
* func
= Expression::make_func_reference(newfn
, NULL
, loc
);
275 Expression_list
* params
= new Expression_list();
276 params
->push_back(Expression::make_type(var
->type(), loc
));
277 Expression
* call
= Expression::make_call(func
, params
, false, loc
);
278 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
279 Temporary_statement
* temp
= Statement::make_temporary(NULL
, call
, loc
);
280 Bstatement
* btemp
= temp
->get_backend(context
);
282 Bstatement
* set
= NULL
;
285 Expression
* e
= Expression::make_temporary_reference(temp
, loc
);
286 e
= Expression::make_unary(OPERATOR_MULT
, e
, loc
);
287 Bexpression
* be
= tree_to_expr(e
->get_tree(context
));
288 set
= context
->backend()->assignment_statement(be
, binit
, loc
);
291 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
292 Bexpression
* bref
= tree_to_expr(ref
->get_tree(context
));
293 Bstatement
* sinit
= context
->backend()->init_statement(bvar
, bref
);
295 std::vector
<Bstatement
*> stats
;
297 stats
.push_back(btemp
);
299 stats
.push_back(set
);
300 stats
.push_back(sinit
);
301 return context
->backend()->statement_list(stats
);
304 // Dump the AST representation for a variable declaration.
307 Variable_declaration_statement::do_dump_statement(
308 Ast_dump_context
* ast_dump_context
) const
310 ast_dump_context
->print_indent();
312 go_assert(var_
->is_variable());
313 ast_dump_context
->ostream() << "var " << this->var_
->name() << " ";
314 Variable
* var
= this->var_
->var_value();
317 ast_dump_context
->dump_type(var
->type());
318 ast_dump_context
->ostream() << " ";
320 if (var
->init() != NULL
)
322 ast_dump_context
->ostream() << "= ";
323 ast_dump_context
->dump_expression(var
->init());
325 ast_dump_context
->ostream() << std::endl
;
328 // Make a variable declaration.
331 Statement::make_variable_declaration(Named_object
* var
)
333 return new Variable_declaration_statement(var
);
336 // Class Temporary_statement.
338 // Return the type of the temporary variable.
341 Temporary_statement::type() const
343 return this->type_
!= NULL
? this->type_
: this->init_
->type();
349 Temporary_statement::do_traverse(Traverse
* traverse
)
351 if (this->type_
!= NULL
352 && this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
353 return TRAVERSE_EXIT
;
354 if (this->init_
== NULL
)
355 return TRAVERSE_CONTINUE
;
357 return this->traverse_expression(traverse
, &this->init_
);
360 // Traverse assignments.
363 Temporary_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
365 if (this->init_
== NULL
)
367 tassign
->value(&this->init_
, true, true);
374 Temporary_statement::do_determine_types()
376 if (this->type_
!= NULL
&& this->type_
->is_abstract())
377 this->type_
= this->type_
->make_non_abstract_type();
379 if (this->init_
!= NULL
)
381 if (this->type_
== NULL
)
382 this->init_
->determine_type_no_context();
385 Type_context
context(this->type_
, false);
386 this->init_
->determine_type(&context
);
390 if (this->type_
== NULL
)
392 this->type_
= this->init_
->type();
393 go_assert(!this->type_
->is_abstract());
400 Temporary_statement::do_check_types(Gogo
*)
402 if (this->type_
!= NULL
&& this->init_
!= NULL
)
405 if (!Type::are_assignable(this->type_
, this->init_
->type(), &reason
))
408 error_at(this->location(), "incompatible types in assignment");
410 error_at(this->location(), "incompatible types in assignment (%s)",
412 this->set_is_error();
417 // Convert to backend representation.
420 Temporary_statement::do_get_backend(Translate_context
* context
)
422 go_assert(this->bvariable_
== NULL
);
424 // FIXME: Permitting FUNCTION to be NULL here is a temporary measure
425 // until we have a better representation of the init function.
426 Named_object
* function
= context
->function();
427 Bfunction
* bfunction
;
428 if (function
== NULL
)
431 bfunction
= tree_to_function(function
->func_value()->get_decl());
433 Btype
* btype
= this->type()->get_backend(context
->gogo());
436 if (this->init_
== NULL
)
438 else if (this->type_
== NULL
)
439 binit
= tree_to_expr(this->init_
->get_tree(context
));
442 Expression
* init
= Expression::make_cast(this->type_
, this->init_
,
444 context
->gogo()->lower_expression(context
->function(), NULL
, &init
);
445 binit
= tree_to_expr(init
->get_tree(context
));
448 Bstatement
* statement
;
450 context
->backend()->temporary_variable(bfunction
, context
->bblock(),
452 this->is_address_taken_
,
453 this->location(), &statement
);
457 // Return the backend variable.
460 Temporary_statement::get_backend_variable(Translate_context
* context
) const
462 if (this->bvariable_
== NULL
)
464 go_assert(saw_errors());
465 return context
->backend()->error_variable();
467 return this->bvariable_
;
470 // Dump the AST represemtation for a temporary statement
473 Temporary_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
475 ast_dump_context
->print_indent();
476 ast_dump_context
->dump_temp_variable_name(this);
477 if (this->type_
!= NULL
)
479 ast_dump_context
->ostream() << " ";
480 ast_dump_context
->dump_type(this->type_
);
482 if (this->init_
!= NULL
)
484 ast_dump_context
->ostream() << " = ";
485 ast_dump_context
->dump_expression(this->init_
);
487 ast_dump_context
->ostream() << std::endl
;
490 // Make and initialize a temporary variable in BLOCK.
493 Statement::make_temporary(Type
* type
, Expression
* init
,
494 source_location location
)
496 return new Temporary_statement(type
, init
, location
);
499 // An assignment statement.
501 class Assignment_statement
: public Statement
504 Assignment_statement(Expression
* lhs
, Expression
* rhs
,
505 source_location location
)
506 : Statement(STATEMENT_ASSIGNMENT
, location
),
512 do_traverse(Traverse
* traverse
);
515 do_traverse_assignments(Traverse_assignments
*);
518 do_determine_types();
521 do_check_types(Gogo
*);
524 do_get_backend(Translate_context
*);
527 do_dump_statement(Ast_dump_context
*) const;
530 // Left hand side--the lvalue.
532 // Right hand side--the rvalue.
539 Assignment_statement::do_traverse(Traverse
* traverse
)
541 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
542 return TRAVERSE_EXIT
;
543 return this->traverse_expression(traverse
, &this->rhs_
);
547 Assignment_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
549 tassign
->assignment(&this->lhs_
, &this->rhs_
);
553 // Set types for the assignment.
556 Assignment_statement::do_determine_types()
558 this->lhs_
->determine_type_no_context();
559 Type_context
context(this->lhs_
->type(), false);
560 this->rhs_
->determine_type(&context
);
563 // Check types for an assignment.
566 Assignment_statement::do_check_types(Gogo
*)
568 // The left hand side must be either addressable, a map index
569 // expression, or the blank identifier.
570 if (!this->lhs_
->is_addressable()
571 && this->lhs_
->map_index_expression() == NULL
572 && !this->lhs_
->is_sink_expression())
574 if (!this->lhs_
->type()->is_error())
575 this->report_error(_("invalid left hand side of assignment"));
579 Type
* lhs_type
= this->lhs_
->type();
580 Type
* rhs_type
= this->rhs_
->type();
582 if (!Type::are_assignable(lhs_type
, rhs_type
, &reason
))
585 error_at(this->location(), "incompatible types in assignment");
587 error_at(this->location(), "incompatible types in assignment (%s)",
589 this->set_is_error();
592 if (lhs_type
->is_error() || rhs_type
->is_error())
593 this->set_is_error();
596 // Convert an assignment statement to the backend representation.
599 Assignment_statement::do_get_backend(Translate_context
* context
)
601 tree rhs_tree
= this->rhs_
->get_tree(context
);
602 if (this->lhs_
->is_sink_expression())
603 return context
->backend()->expression_statement(tree_to_expr(rhs_tree
));
604 tree lhs_tree
= this->lhs_
->get_tree(context
);
605 rhs_tree
= Expression::convert_for_assignment(context
, this->lhs_
->type(),
606 this->rhs_
->type(), rhs_tree
,
608 return context
->backend()->assignment_statement(tree_to_expr(lhs_tree
),
609 tree_to_expr(rhs_tree
),
613 // Dump the AST representation for an assignment statement.
616 Assignment_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
619 ast_dump_context
->print_indent();
620 ast_dump_context
->dump_expression(this->lhs_
);
621 ast_dump_context
->ostream() << " = " ;
622 ast_dump_context
->dump_expression(this->rhs_
);
623 ast_dump_context
->ostream() << std::endl
;
626 // Make an assignment statement.
629 Statement::make_assignment(Expression
* lhs
, Expression
* rhs
,
630 source_location location
)
632 return new Assignment_statement(lhs
, rhs
, location
);
635 // The Move_ordered_evals class is used to find any subexpressions of
636 // an expression that have an evaluation order dependency. It creates
637 // temporary variables to hold them.
639 class Move_ordered_evals
: public Traverse
642 Move_ordered_evals(Block
* block
)
643 : Traverse(traverse_expressions
),
649 expression(Expression
**);
652 // The block where new temporary variables should be added.
657 Move_ordered_evals::expression(Expression
** pexpr
)
659 // We have to look at subexpressions first.
660 if ((*pexpr
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
661 return TRAVERSE_EXIT
;
662 if ((*pexpr
)->must_eval_in_order())
664 source_location loc
= (*pexpr
)->location();
665 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
666 this->block_
->add_statement(temp
);
667 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
669 return TRAVERSE_SKIP_COMPONENTS
;
672 // An assignment operation statement.
674 class Assignment_operation_statement
: public Statement
677 Assignment_operation_statement(Operator op
, Expression
* lhs
, Expression
* rhs
,
678 source_location location
)
679 : Statement(STATEMENT_ASSIGNMENT_OPERATION
, location
),
680 op_(op
), lhs_(lhs
), rhs_(rhs
)
685 do_traverse(Traverse
*);
688 do_traverse_assignments(Traverse_assignments
*)
689 { go_unreachable(); }
692 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
695 do_get_backend(Translate_context
*)
696 { go_unreachable(); }
699 do_dump_statement(Ast_dump_context
*) const;
702 // The operator (OPERATOR_PLUSEQ, etc.).
713 Assignment_operation_statement::do_traverse(Traverse
* traverse
)
715 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
716 return TRAVERSE_EXIT
;
717 return this->traverse_expression(traverse
, &this->rhs_
);
720 // Lower an assignment operation statement to a regular assignment
724 Assignment_operation_statement::do_lower(Gogo
*, Named_object
*,
725 Block
* enclosing
, Statement_inserter
*)
727 source_location loc
= this->location();
729 // We have to evaluate the left hand side expression only once. We
730 // do this by moving out any expression with side effects.
731 Block
* b
= new Block(enclosing
, loc
);
732 Move_ordered_evals
moe(b
);
733 this->lhs_
->traverse_subexpressions(&moe
);
735 Expression
* lval
= this->lhs_
->copy();
740 case OPERATOR_PLUSEQ
:
743 case OPERATOR_MINUSEQ
:
752 case OPERATOR_MULTEQ
:
761 case OPERATOR_LSHIFTEQ
:
762 op
= OPERATOR_LSHIFT
;
764 case OPERATOR_RSHIFTEQ
:
765 op
= OPERATOR_RSHIFT
;
770 case OPERATOR_BITCLEAREQ
:
771 op
= OPERATOR_BITCLEAR
;
777 Expression
* binop
= Expression::make_binary(op
, lval
, this->rhs_
, loc
);
778 Statement
* s
= Statement::make_assignment(this->lhs_
, binop
, loc
);
779 if (b
->statements()->empty())
787 return Statement::make_block_statement(b
, loc
);
791 // Dump the AST representation for an assignment operation statement
794 Assignment_operation_statement::do_dump_statement(
795 Ast_dump_context
* ast_dump_context
) const
797 ast_dump_context
->print_indent();
798 ast_dump_context
->dump_expression(this->lhs_
);
799 ast_dump_context
->dump_operator(this->op_
);
800 ast_dump_context
->dump_expression(this->rhs_
);
801 ast_dump_context
->ostream() << std::endl
;
804 // Make an assignment operation statement.
807 Statement::make_assignment_operation(Operator op
, Expression
* lhs
,
808 Expression
* rhs
, source_location location
)
810 return new Assignment_operation_statement(op
, lhs
, rhs
, location
);
813 // A tuple assignment statement. This differs from an assignment
814 // statement in that the right-hand-side expressions are evaluated in
817 class Tuple_assignment_statement
: public Statement
820 Tuple_assignment_statement(Expression_list
* lhs
, Expression_list
* rhs
,
821 source_location location
)
822 : Statement(STATEMENT_TUPLE_ASSIGNMENT
, location
),
828 do_traverse(Traverse
* traverse
);
831 do_traverse_assignments(Traverse_assignments
*)
832 { go_unreachable(); }
835 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
838 do_get_backend(Translate_context
*)
839 { go_unreachable(); }
842 do_dump_statement(Ast_dump_context
*) const;
845 // Left hand side--a list of lvalues.
846 Expression_list
* lhs_
;
847 // Right hand side--a list of rvalues.
848 Expression_list
* rhs_
;
854 Tuple_assignment_statement::do_traverse(Traverse
* traverse
)
856 if (this->traverse_expression_list(traverse
, this->lhs_
) == TRAVERSE_EXIT
)
857 return TRAVERSE_EXIT
;
858 return this->traverse_expression_list(traverse
, this->rhs_
);
861 // Lower a tuple assignment. We use temporary variables to split it
862 // up into a set of single assignments.
865 Tuple_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
868 source_location loc
= this->location();
870 Block
* b
= new Block(enclosing
, loc
);
872 // First move out any subexpressions on the left hand side. The
873 // right hand side will be evaluated in the required order anyhow.
874 Move_ordered_evals
moe(b
);
875 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
876 plhs
!= this->lhs_
->end();
878 (*plhs
)->traverse_subexpressions(&moe
);
880 std::vector
<Temporary_statement
*> temps
;
881 temps
.reserve(this->lhs_
->size());
883 Expression_list::const_iterator prhs
= this->rhs_
->begin();
884 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
885 plhs
!= this->lhs_
->end();
888 go_assert(prhs
!= this->rhs_
->end());
890 if ((*plhs
)->is_error_expression()
891 || (*plhs
)->type()->is_error()
892 || (*prhs
)->is_error_expression()
893 || (*prhs
)->type()->is_error())
896 if ((*plhs
)->is_sink_expression())
898 b
->add_statement(Statement::make_statement(*prhs
));
902 Temporary_statement
* temp
= Statement::make_temporary((*plhs
)->type(),
904 b
->add_statement(temp
);
905 temps
.push_back(temp
);
908 go_assert(prhs
== this->rhs_
->end());
910 prhs
= this->rhs_
->begin();
911 std::vector
<Temporary_statement
*>::const_iterator ptemp
= temps
.begin();
912 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
913 plhs
!= this->lhs_
->end();
916 if ((*plhs
)->is_error_expression()
917 || (*plhs
)->type()->is_error()
918 || (*prhs
)->is_error_expression()
919 || (*prhs
)->type()->is_error())
922 if ((*plhs
)->is_sink_expression())
925 Expression
* ref
= Expression::make_temporary_reference(*ptemp
, loc
);
926 Statement
* s
= Statement::make_assignment(*plhs
, ref
, loc
);
930 go_assert(ptemp
== temps
.end());
932 return Statement::make_block_statement(b
, loc
);
935 // Dump the AST representation for a tuple assignment statement.
938 Tuple_assignment_statement::do_dump_statement(
939 Ast_dump_context
* ast_dump_context
) const
941 ast_dump_context
->print_indent();
942 ast_dump_context
->dump_expression_list(this->lhs_
);
943 ast_dump_context
->ostream() << " = ";
944 ast_dump_context
->dump_expression_list(this->rhs_
);
945 ast_dump_context
->ostream() << std::endl
;
948 // Make a tuple assignment statement.
951 Statement::make_tuple_assignment(Expression_list
* lhs
, Expression_list
* rhs
,
952 source_location location
)
954 return new Tuple_assignment_statement(lhs
, rhs
, location
);
957 // A tuple assignment from a map index expression.
960 class Tuple_map_assignment_statement
: public Statement
963 Tuple_map_assignment_statement(Expression
* val
, Expression
* present
,
964 Expression
* map_index
,
965 source_location location
)
966 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT
, location
),
967 val_(val
), present_(present
), map_index_(map_index
)
972 do_traverse(Traverse
* traverse
);
975 do_traverse_assignments(Traverse_assignments
*)
976 { go_unreachable(); }
979 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
982 do_get_backend(Translate_context
*)
983 { go_unreachable(); }
986 do_dump_statement(Ast_dump_context
*) const;
989 // Lvalue which receives the value from the map.
991 // Lvalue which receives whether the key value was present.
992 Expression
* present_
;
993 // The map index expression.
994 Expression
* map_index_
;
1000 Tuple_map_assignment_statement::do_traverse(Traverse
* traverse
)
1002 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1003 || this->traverse_expression(traverse
, &this->present_
) == TRAVERSE_EXIT
)
1004 return TRAVERSE_EXIT
;
1005 return this->traverse_expression(traverse
, &this->map_index_
);
1008 // Lower a tuple map assignment.
1011 Tuple_map_assignment_statement::do_lower(Gogo
*, Named_object
*,
1012 Block
* enclosing
, Statement_inserter
*)
1014 source_location loc
= this->location();
1016 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1017 if (map_index
== NULL
)
1019 this->report_error(_("expected map index on right hand side"));
1020 return Statement::make_error_statement(loc
);
1022 Map_type
* map_type
= map_index
->get_map_type();
1023 if (map_type
== NULL
)
1024 return Statement::make_error_statement(loc
);
1026 Block
* b
= new Block(enclosing
, loc
);
1028 // Move out any subexpressions to make sure that functions are
1029 // called in the required order.
1030 Move_ordered_evals
moe(b
);
1031 this->val_
->traverse_subexpressions(&moe
);
1032 this->present_
->traverse_subexpressions(&moe
);
1034 // Copy the key value into a temporary so that we can take its
1035 // address without pushing the value onto the heap.
1037 // var key_temp KEY_TYPE = MAP_INDEX
1038 Temporary_statement
* key_temp
=
1039 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1040 b
->add_statement(key_temp
);
1042 // var val_temp VAL_TYPE
1043 Temporary_statement
* val_temp
=
1044 Statement::make_temporary(map_type
->val_type(), NULL
, loc
);
1045 b
->add_statement(val_temp
);
1047 // var present_temp bool
1048 Temporary_statement
* present_temp
=
1049 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
1050 b
->add_statement(present_temp
);
1052 // present_temp = mapaccess2(MAP, &key_temp, &val_temp)
1053 Temporary_reference_expression
* ref
=
1054 Expression::make_temporary_reference(key_temp
, loc
);
1055 Expression
* a1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1056 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1057 Expression
* a2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1058 Expression
* call
= Runtime::make_call(Runtime::MAPACCESS2
, loc
, 3,
1059 map_index
->map(), a1
, a2
);
1061 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1062 ref
->set_is_lvalue();
1063 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1064 b
->add_statement(s
);
1067 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1068 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1069 b
->add_statement(s
);
1071 // present = present_temp
1072 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1073 s
= Statement::make_assignment(this->present_
, ref
, loc
);
1074 b
->add_statement(s
);
1076 return Statement::make_block_statement(b
, loc
);
1079 // Dump the AST representation for a tuple map assignment statement.
1082 Tuple_map_assignment_statement::do_dump_statement(
1083 Ast_dump_context
* ast_dump_context
) const
1085 ast_dump_context
->print_indent();
1086 ast_dump_context
->dump_expression(this->val_
);
1087 ast_dump_context
->ostream() << ", ";
1088 ast_dump_context
->dump_expression(this->present_
);
1089 ast_dump_context
->ostream() << " = ";
1090 ast_dump_context
->dump_expression(this->map_index_
);
1091 ast_dump_context
->ostream() << std::endl
;
1094 // Make a map assignment statement which returns a pair of values.
1097 Statement::make_tuple_map_assignment(Expression
* val
, Expression
* present
,
1098 Expression
* map_index
,
1099 source_location location
)
1101 return new Tuple_map_assignment_statement(val
, present
, map_index
, location
);
1104 // Assign a pair of entries to a map.
1107 class Map_assignment_statement
: public Statement
1110 Map_assignment_statement(Expression
* map_index
,
1111 Expression
* val
, Expression
* should_set
,
1112 source_location location
)
1113 : Statement(STATEMENT_MAP_ASSIGNMENT
, location
),
1114 map_index_(map_index
), val_(val
), should_set_(should_set
)
1119 do_traverse(Traverse
* traverse
);
1122 do_traverse_assignments(Traverse_assignments
*)
1123 { go_unreachable(); }
1126 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1129 do_get_backend(Translate_context
*)
1130 { go_unreachable(); }
1133 do_dump_statement(Ast_dump_context
*) const;
1136 // A reference to the map index which should be set or deleted.
1137 Expression
* map_index_
;
1138 // The value to add to the map.
1140 // Whether or not to add the value.
1141 Expression
* should_set_
;
1144 // Traverse a map assignment.
1147 Map_assignment_statement::do_traverse(Traverse
* traverse
)
1149 if (this->traverse_expression(traverse
, &this->map_index_
) == TRAVERSE_EXIT
1150 || this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
1151 return TRAVERSE_EXIT
;
1152 return this->traverse_expression(traverse
, &this->should_set_
);
1155 // Lower a map assignment to a function call.
1158 Map_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
1159 Statement_inserter
*)
1161 source_location loc
= this->location();
1163 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1164 if (map_index
== NULL
)
1166 this->report_error(_("expected map index on left hand side"));
1167 return Statement::make_error_statement(loc
);
1169 Map_type
* map_type
= map_index
->get_map_type();
1170 if (map_type
== NULL
)
1171 return Statement::make_error_statement(loc
);
1173 Block
* b
= new Block(enclosing
, loc
);
1175 // Evaluate the map first to get order of evaluation right.
1176 // map_temp := m // we are evaluating m[k] = v, p
1177 Temporary_statement
* map_temp
= Statement::make_temporary(map_type
,
1180 b
->add_statement(map_temp
);
1182 // var key_temp MAP_KEY_TYPE = k
1183 Temporary_statement
* key_temp
=
1184 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1185 b
->add_statement(key_temp
);
1187 // var val_temp MAP_VAL_TYPE = v
1188 Temporary_statement
* val_temp
=
1189 Statement::make_temporary(map_type
->val_type(), this->val_
, loc
);
1190 b
->add_statement(val_temp
);
1192 // var insert_temp bool = p
1193 Temporary_statement
* insert_temp
=
1194 Statement::make_temporary(Type::lookup_bool_type(), this->should_set_
,
1196 b
->add_statement(insert_temp
);
1198 // mapassign2(map_temp, &key_temp, &val_temp, p)
1199 Expression
* p1
= Expression::make_temporary_reference(map_temp
, loc
);
1200 Expression
* ref
= Expression::make_temporary_reference(key_temp
, loc
);
1201 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1202 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1203 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1204 Expression
* p4
= Expression::make_temporary_reference(insert_temp
, loc
);
1205 Expression
* call
= Runtime::make_call(Runtime::MAPASSIGN2
, loc
, 4,
1207 Statement
* s
= Statement::make_statement(call
);
1208 b
->add_statement(s
);
1210 return Statement::make_block_statement(b
, loc
);
1213 // Dump the AST representation for a map assignment statement.
1216 Map_assignment_statement::do_dump_statement(
1217 Ast_dump_context
* ast_dump_context
) const
1219 ast_dump_context
->print_indent();
1220 ast_dump_context
->dump_expression(this->map_index_
);
1221 ast_dump_context
->ostream() << " = ";
1222 ast_dump_context
->dump_expression(this->val_
);
1223 ast_dump_context
->ostream() << ", ";
1224 ast_dump_context
->dump_expression(this->should_set_
);
1225 ast_dump_context
->ostream() << std::endl
;
1228 // Make a statement which assigns a pair of entries to a map.
1231 Statement::make_map_assignment(Expression
* map_index
,
1232 Expression
* val
, Expression
* should_set
,
1233 source_location location
)
1235 return new Map_assignment_statement(map_index
, val
, should_set
, location
);
1238 // A tuple assignment from a receive statement.
1240 class Tuple_receive_assignment_statement
: public Statement
1243 Tuple_receive_assignment_statement(Expression
* val
, Expression
* closed
,
1244 Expression
* channel
, bool for_select
,
1245 source_location location
)
1246 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
, location
),
1247 val_(val
), closed_(closed
), channel_(channel
), for_select_(for_select
)
1252 do_traverse(Traverse
* traverse
);
1255 do_traverse_assignments(Traverse_assignments
*)
1256 { go_unreachable(); }
1259 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1262 do_get_backend(Translate_context
*)
1263 { go_unreachable(); }
1266 do_dump_statement(Ast_dump_context
*) const;
1269 // Lvalue which receives the value from the channel.
1271 // Lvalue which receives whether the channel is closed.
1272 Expression
* closed_
;
1273 // The channel on which we receive the value.
1274 Expression
* channel_
;
1275 // Whether this is for a select statement.
1282 Tuple_receive_assignment_statement::do_traverse(Traverse
* traverse
)
1284 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1285 || this->traverse_expression(traverse
, &this->closed_
) == TRAVERSE_EXIT
)
1286 return TRAVERSE_EXIT
;
1287 return this->traverse_expression(traverse
, &this->channel_
);
1290 // Lower to a function call.
1293 Tuple_receive_assignment_statement::do_lower(Gogo
*, Named_object
*,
1295 Statement_inserter
*)
1297 source_location loc
= this->location();
1299 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
1300 if (channel_type
== NULL
)
1302 this->report_error(_("expected channel"));
1303 return Statement::make_error_statement(loc
);
1305 if (!channel_type
->may_receive())
1307 this->report_error(_("invalid receive on send-only channel"));
1308 return Statement::make_error_statement(loc
);
1311 Block
* b
= new Block(enclosing
, loc
);
1313 // Make sure that any subexpressions on the left hand side are
1314 // evaluated in the right order.
1315 Move_ordered_evals
moe(b
);
1316 this->val_
->traverse_subexpressions(&moe
);
1317 this->closed_
->traverse_subexpressions(&moe
);
1319 // var val_temp ELEMENT_TYPE
1320 Temporary_statement
* val_temp
=
1321 Statement::make_temporary(channel_type
->element_type(), NULL
, loc
);
1322 b
->add_statement(val_temp
);
1324 // var closed_temp bool
1325 Temporary_statement
* closed_temp
=
1326 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
1327 b
->add_statement(closed_temp
);
1329 // closed_temp = chanrecv[23](channel, &val_temp)
1330 Temporary_reference_expression
* ref
=
1331 Expression::make_temporary_reference(val_temp
, loc
);
1332 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1333 Expression
* call
= Runtime::make_call((this->for_select_
1334 ? Runtime::CHANRECV3
1335 : Runtime::CHANRECV2
),
1336 loc
, 2, this->channel_
, p2
);
1337 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1338 ref
->set_is_lvalue();
1339 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1340 b
->add_statement(s
);
1343 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1344 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1345 b
->add_statement(s
);
1347 // closed = closed_temp
1348 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1349 s
= Statement::make_assignment(this->closed_
, ref
, loc
);
1350 b
->add_statement(s
);
1352 return Statement::make_block_statement(b
, loc
);
1355 // Dump the AST representation for a tuple receive statement.
1358 Tuple_receive_assignment_statement::do_dump_statement(
1359 Ast_dump_context
* ast_dump_context
) const
1361 ast_dump_context
->print_indent();
1362 ast_dump_context
->dump_expression(this->val_
);
1363 ast_dump_context
->ostream() << ", ";
1364 ast_dump_context
->dump_expression(this->closed_
);
1365 ast_dump_context
->ostream() << " <- ";
1366 ast_dump_context
->dump_expression(this->channel_
);
1367 ast_dump_context
->ostream() << std::endl
;
1370 // Make a nonblocking receive statement.
1373 Statement::make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
1374 Expression
* channel
,
1376 source_location location
)
1378 return new Tuple_receive_assignment_statement(val
, closed
, channel
,
1379 for_select
, location
);
1382 // An assignment to a pair of values from a type guard. This is a
1383 // conditional type guard. v, ok = i.(type).
1385 class Tuple_type_guard_assignment_statement
: public Statement
1388 Tuple_type_guard_assignment_statement(Expression
* val
, Expression
* ok
,
1389 Expression
* expr
, Type
* type
,
1390 source_location location
)
1391 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
, location
),
1392 val_(val
), ok_(ok
), expr_(expr
), type_(type
)
1397 do_traverse(Traverse
*);
1400 do_traverse_assignments(Traverse_assignments
*)
1401 { go_unreachable(); }
1404 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1407 do_get_backend(Translate_context
*)
1408 { go_unreachable(); }
1411 do_dump_statement(Ast_dump_context
*) const;
1415 lower_to_type(Runtime::Function
);
1418 lower_to_object_type(Block
*, Runtime::Function
);
1420 // The variable which recieves the converted value.
1422 // The variable which receives the indication of success.
1424 // The expression being converted.
1426 // The type to which the expression is being converted.
1430 // Traverse a type guard tuple assignment.
1433 Tuple_type_guard_assignment_statement::do_traverse(Traverse
* traverse
)
1435 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1436 || this->traverse_expression(traverse
, &this->ok_
) == TRAVERSE_EXIT
1437 || this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
1438 return TRAVERSE_EXIT
;
1439 return this->traverse_expression(traverse
, &this->expr_
);
1442 // Lower to a function call.
1445 Tuple_type_guard_assignment_statement::do_lower(Gogo
*, Named_object
*,
1447 Statement_inserter
*)
1449 source_location loc
= this->location();
1451 Type
* expr_type
= this->expr_
->type();
1452 if (expr_type
->interface_type() == NULL
)
1454 if (!expr_type
->is_error() && !this->type_
->is_error())
1455 this->report_error(_("type assertion only valid for interface types"));
1456 return Statement::make_error_statement(loc
);
1459 Block
* b
= new Block(enclosing
, loc
);
1461 // Make sure that any subexpressions on the left hand side are
1462 // evaluated in the right order.
1463 Move_ordered_evals
moe(b
);
1464 this->val_
->traverse_subexpressions(&moe
);
1465 this->ok_
->traverse_subexpressions(&moe
);
1467 bool expr_is_empty
= expr_type
->interface_type()->is_empty();
1468 Call_expression
* call
;
1469 if (this->type_
->interface_type() != NULL
)
1471 if (this->type_
->interface_type()->is_empty())
1472 call
= Runtime::make_call((expr_is_empty
1473 ? Runtime::IFACEE2E2
1474 : Runtime::IFACEI2E2
),
1475 loc
, 1, this->expr_
);
1477 call
= this->lower_to_type(expr_is_empty
1478 ? Runtime::IFACEE2I2
1479 : Runtime::IFACEI2I2
);
1481 else if (this->type_
->points_to() != NULL
)
1482 call
= this->lower_to_type(expr_is_empty
1483 ? Runtime::IFACEE2T2P
1484 : Runtime::IFACEI2T2P
);
1487 this->lower_to_object_type(b
,
1489 ? Runtime::IFACEE2T2
1490 : Runtime::IFACEI2T2
));
1496 Expression
* res
= Expression::make_call_result(call
, 0);
1497 res
= Expression::make_unsafe_cast(this->type_
, res
, loc
);
1498 Statement
* s
= Statement::make_assignment(this->val_
, res
, loc
);
1499 b
->add_statement(s
);
1501 res
= Expression::make_call_result(call
, 1);
1502 s
= Statement::make_assignment(this->ok_
, res
, loc
);
1503 b
->add_statement(s
);
1506 return Statement::make_block_statement(b
, loc
);
1509 // Lower a conversion to a non-empty interface type or a pointer type.
1512 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code
)
1514 source_location loc
= this->location();
1515 return Runtime::make_call(code
, loc
, 2,
1516 Expression::make_type_descriptor(this->type_
, loc
),
1520 // Lower a conversion to a non-interface non-pointer type.
1523 Tuple_type_guard_assignment_statement::lower_to_object_type(
1525 Runtime::Function code
)
1527 source_location loc
= this->location();
1529 // var val_temp TYPE
1530 Temporary_statement
* val_temp
= Statement::make_temporary(this->type_
,
1532 b
->add_statement(val_temp
);
1534 // ok = CODE(type_descriptor, expr, &val_temp)
1535 Expression
* p1
= Expression::make_type_descriptor(this->type_
, loc
);
1536 Expression
* ref
= Expression::make_temporary_reference(val_temp
, loc
);
1537 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1538 Expression
* call
= Runtime::make_call(code
, loc
, 3, p1
, this->expr_
, p3
);
1539 Statement
* s
= Statement::make_assignment(this->ok_
, call
, loc
);
1540 b
->add_statement(s
);
1543 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1544 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1545 b
->add_statement(s
);
1548 // Dump the AST representation for a tuple type guard statement.
1551 Tuple_type_guard_assignment_statement::do_dump_statement(
1552 Ast_dump_context
* ast_dump_context
) const
1554 ast_dump_context
->print_indent();
1555 ast_dump_context
->dump_expression(this->val_
);
1556 ast_dump_context
->ostream() << ", ";
1557 ast_dump_context
->dump_expression(this->ok_
);
1558 ast_dump_context
->ostream() << " = ";
1559 ast_dump_context
->dump_expression(this->expr_
);
1560 ast_dump_context
->ostream() << " . ";
1561 ast_dump_context
->dump_type(this->type_
);
1562 ast_dump_context
->ostream() << std::endl
;
1565 // Make an assignment from a type guard to a pair of variables.
1568 Statement::make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
1569 Expression
* expr
, Type
* type
,
1570 source_location location
)
1572 return new Tuple_type_guard_assignment_statement(val
, ok
, expr
, type
,
1576 // An expression statement.
1578 class Expression_statement
: public Statement
1581 Expression_statement(Expression
* expr
)
1582 : Statement(STATEMENT_EXPRESSION
, expr
->location()),
1588 { return this->expr_
; }
1592 do_traverse(Traverse
* traverse
)
1593 { return this->traverse_expression(traverse
, &this->expr_
); }
1596 do_determine_types()
1597 { this->expr_
->determine_type_no_context(); }
1600 do_may_fall_through() const;
1603 do_get_backend(Translate_context
* context
);
1606 do_dump_statement(Ast_dump_context
*) const;
1612 // An expression statement may fall through unless it is a call to a
1613 // function which does not return.
1616 Expression_statement::do_may_fall_through() const
1618 const Call_expression
* call
= this->expr_
->call_expression();
1621 const Expression
* fn
= call
->fn();
1622 const Func_expression
* fe
= fn
->func_expression();
1625 const Named_object
* no
= fe
->named_object();
1627 Function_type
* fntype
;
1628 if (no
->is_function())
1629 fntype
= no
->func_value()->type();
1630 else if (no
->is_function_declaration())
1631 fntype
= no
->func_declaration_value()->type();
1635 // The builtin function panic does not return.
1636 if (fntype
!= NULL
&& fntype
->is_builtin() && no
->name() == "panic")
1643 // Convert to backend representation.
1646 Expression_statement::do_get_backend(Translate_context
* context
)
1648 tree expr_tree
= this->expr_
->get_tree(context
);
1649 return context
->backend()->expression_statement(tree_to_expr(expr_tree
));
1652 // Dump the AST representation for an expression statement
1655 Expression_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
1658 ast_dump_context
->print_indent();
1659 ast_dump_context
->dump_expression(expr_
);
1660 ast_dump_context
->ostream() << std::endl
;
1663 // Make an expression statement from an Expression.
1666 Statement::make_statement(Expression
* expr
)
1668 return new Expression_statement(expr
);
1671 // A block statement--a list of statements which may include variable
1674 class Block_statement
: public Statement
1677 Block_statement(Block
* block
, source_location location
)
1678 : Statement(STATEMENT_BLOCK
, location
),
1684 do_traverse(Traverse
* traverse
)
1685 { return this->block_
->traverse(traverse
); }
1688 do_determine_types()
1689 { this->block_
->determine_types(); }
1692 do_may_fall_through() const
1693 { return this->block_
->may_fall_through(); }
1696 do_get_backend(Translate_context
* context
);
1699 do_dump_statement(Ast_dump_context
*) const;
1705 // Convert a block to the backend representation of a statement.
1708 Block_statement::do_get_backend(Translate_context
* context
)
1710 Bblock
* bblock
= this->block_
->get_backend(context
);
1711 return context
->backend()->block_statement(bblock
);
1714 // Dump the AST for a block statement
1717 Block_statement::do_dump_statement(Ast_dump_context
*) const
1719 // block statement braces are dumped when traversing.
1722 // Make a block statement.
1725 Statement::make_block_statement(Block
* block
, source_location location
)
1727 return new Block_statement(block
, location
);
1730 // An increment or decrement statement.
1732 class Inc_dec_statement
: public Statement
1735 Inc_dec_statement(bool is_inc
, Expression
* expr
)
1736 : Statement(STATEMENT_INCDEC
, expr
->location()),
1737 expr_(expr
), is_inc_(is_inc
)
1742 do_traverse(Traverse
* traverse
)
1743 { return this->traverse_expression(traverse
, &this->expr_
); }
1746 do_traverse_assignments(Traverse_assignments
*)
1747 { go_unreachable(); }
1750 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1753 do_get_backend(Translate_context
*)
1754 { go_unreachable(); }
1757 do_dump_statement(Ast_dump_context
*) const;
1760 // The l-value to increment or decrement.
1762 // Whether to increment or decrement.
1766 // Lower to += or -=.
1769 Inc_dec_statement::do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
1771 source_location loc
= this->location();
1774 mpz_init_set_ui(oval
, 1UL);
1775 Expression
* oexpr
= Expression::make_integer(&oval
, NULL
, loc
);
1778 Operator op
= this->is_inc_
? OPERATOR_PLUSEQ
: OPERATOR_MINUSEQ
;
1779 return Statement::make_assignment_operation(op
, this->expr_
, oexpr
, loc
);
1782 // Dump the AST representation for a inc/dec statement.
1785 Inc_dec_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
1787 ast_dump_context
->print_indent();
1788 ast_dump_context
->dump_expression(expr_
);
1789 ast_dump_context
->ostream() << (is_inc_
? "++": "--") << std::endl
;
1792 // Make an increment statement.
1795 Statement::make_inc_statement(Expression
* expr
)
1797 return new Inc_dec_statement(true, expr
);
1800 // Make a decrement statement.
1803 Statement::make_dec_statement(Expression
* expr
)
1805 return new Inc_dec_statement(false, expr
);
1808 // Class Thunk_statement. This is the base class for go and defer
1813 Thunk_statement::Thunk_statement(Statement_classification classification
,
1814 Call_expression
* call
,
1815 source_location location
)
1816 : Statement(classification
, location
),
1817 call_(call
), struct_type_(NULL
)
1821 // Return whether this is a simple statement which does not require a
1825 Thunk_statement::is_simple(Function_type
* fntype
) const
1827 // We need a thunk to call a method, or to pass a variable number of
1829 if (fntype
->is_method() || fntype
->is_varargs())
1832 // A defer statement requires a thunk to set up for whether the
1833 // function can call recover.
1834 if (this->classification() == STATEMENT_DEFER
)
1837 // We can only permit a single parameter of pointer type.
1838 const Typed_identifier_list
* parameters
= fntype
->parameters();
1839 if (parameters
!= NULL
1840 && (parameters
->size() > 1
1841 || (parameters
->size() == 1
1842 && parameters
->begin()->type()->points_to() == NULL
)))
1845 // If the function returns multiple values, or returns a type other
1846 // than integer, floating point, or pointer, then it may get a
1847 // hidden first parameter, in which case we need the more
1848 // complicated approach. This is true even though we are going to
1849 // ignore the return value.
1850 const Typed_identifier_list
* results
= fntype
->results();
1852 && (results
->size() > 1
1853 || (results
->size() == 1
1854 && !results
->begin()->type()->is_basic_type()
1855 && results
->begin()->type()->points_to() == NULL
)))
1858 // If this calls something which is not a simple function, then we
1860 Expression
* fn
= this->call_
->call_expression()->fn();
1861 if (fn
->bound_method_expression() != NULL
1862 || fn
->interface_field_reference_expression() != NULL
)
1868 // Traverse a thunk statement.
1871 Thunk_statement::do_traverse(Traverse
* traverse
)
1873 return this->traverse_expression(traverse
, &this->call_
);
1876 // We implement traverse_assignment for a thunk statement because it
1877 // effectively copies the function call.
1880 Thunk_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
1882 Expression
* fn
= this->call_
->call_expression()->fn();
1883 Expression
* fn2
= fn
;
1884 tassign
->value(&fn2
, true, false);
1888 // Determine types in a thunk statement.
1891 Thunk_statement::do_determine_types()
1893 this->call_
->determine_type_no_context();
1895 // Now that we know the types of the call, build the struct used to
1897 Call_expression
* ce
= this->call_
->call_expression();
1900 Function_type
* fntype
= ce
->get_function_type();
1901 if (fntype
!= NULL
&& !this->is_simple(fntype
))
1902 this->struct_type_
= this->build_struct(fntype
);
1905 // Check types in a thunk statement.
1908 Thunk_statement::do_check_types(Gogo
*)
1910 Call_expression
* ce
= this->call_
->call_expression();
1913 if (!this->call_
->is_error_expression())
1914 this->report_error("expected call expression");
1917 Function_type
* fntype
= ce
->get_function_type();
1918 if (fntype
!= NULL
&& fntype
->is_method())
1920 Expression
* fn
= ce
->fn();
1921 if (fn
->bound_method_expression() == NULL
1922 && fn
->interface_field_reference_expression() == NULL
)
1923 this->report_error(_("no object for method call"));
1927 // The Traverse class used to find and simplify thunk statements.
1929 class Simplify_thunk_traverse
: public Traverse
1932 Simplify_thunk_traverse(Gogo
* gogo
)
1933 : Traverse(traverse_functions
| traverse_blocks
),
1934 gogo_(gogo
), function_(NULL
)
1938 function(Named_object
*);
1946 // The function we are traversing.
1947 Named_object
* function_
;
1950 // Keep track of the current function while looking for thunks.
1953 Simplify_thunk_traverse::function(Named_object
* no
)
1955 go_assert(this->function_
== NULL
);
1956 this->function_
= no
;
1957 int t
= no
->func_value()->traverse(this);
1958 this->function_
= NULL
;
1959 if (t
== TRAVERSE_EXIT
)
1961 return TRAVERSE_SKIP_COMPONENTS
;
1964 // Look for thunks in a block.
1967 Simplify_thunk_traverse::block(Block
* b
)
1969 // The parser ensures that thunk statements always appear at the end
1971 if (b
->statements()->size() < 1)
1972 return TRAVERSE_CONTINUE
;
1973 Thunk_statement
* stat
= b
->statements()->back()->thunk_statement();
1975 return TRAVERSE_CONTINUE
;
1976 if (stat
->simplify_statement(this->gogo_
, this->function_
, b
))
1977 return TRAVERSE_SKIP_COMPONENTS
;
1978 return TRAVERSE_CONTINUE
;
1981 // Simplify all thunk statements.
1984 Gogo::simplify_thunk_statements()
1986 Simplify_thunk_traverse
thunk_traverse(this);
1987 this->traverse(&thunk_traverse
);
1990 // Return true if the thunk function is a constant, which means that
1991 // it does not need to be passed to the thunk routine.
1994 Thunk_statement::is_constant_function() const
1996 Call_expression
* ce
= this->call_
->call_expression();
1997 Function_type
* fntype
= ce
->get_function_type();
2000 go_assert(saw_errors());
2003 if (fntype
->is_builtin())
2005 Expression
* fn
= ce
->fn();
2006 if (fn
->func_expression() != NULL
)
2007 return fn
->func_expression()->closure() == NULL
;
2008 if (fn
->bound_method_expression() != NULL
2009 || fn
->interface_field_reference_expression() != NULL
)
2014 // Simplify complex thunk statements into simple ones. A complicated
2015 // thunk statement is one which takes anything other than zero
2016 // parameters or a single pointer parameter. We rewrite it into code
2017 // which allocates a struct, stores the parameter values into the
2018 // struct, and does a simple go or defer statement which passes the
2019 // struct to a thunk. The thunk does the real call.
2022 Thunk_statement::simplify_statement(Gogo
* gogo
, Named_object
* function
,
2025 if (this->classification() == STATEMENT_ERROR
)
2027 if (this->call_
->is_error_expression())
2030 if (this->classification() == STATEMENT_DEFER
)
2032 // Make sure that the defer stack exists for the function. We
2033 // will use when converting this statement to the backend
2034 // representation, but we want it to exist when we start
2035 // converting the function.
2036 function
->func_value()->defer_stack(this->location());
2039 Call_expression
* ce
= this->call_
->call_expression();
2040 Function_type
* fntype
= ce
->get_function_type();
2043 go_assert(saw_errors());
2044 this->set_is_error();
2047 if (this->is_simple(fntype
))
2050 Expression
* fn
= ce
->fn();
2051 Bound_method_expression
* bound_method
= fn
->bound_method_expression();
2052 Interface_field_reference_expression
* interface_method
=
2053 fn
->interface_field_reference_expression();
2055 source_location location
= this->location();
2057 std::string thunk_name
= Gogo::thunk_name();
2060 this->build_thunk(gogo
, thunk_name
);
2062 // Generate code to call the thunk.
2064 // Get the values to store into the struct which is the single
2065 // argument to the thunk.
2067 Expression_list
* vals
= new Expression_list();
2068 if (!this->is_constant_function())
2069 vals
->push_back(fn
);
2071 if (interface_method
!= NULL
)
2072 vals
->push_back(interface_method
->expr());
2074 if (bound_method
!= NULL
)
2076 Expression
* first_arg
= bound_method
->first_argument();
2078 // We always pass a pointer when calling a method.
2079 if (first_arg
->type()->points_to() == NULL
)
2080 first_arg
= Expression::make_unary(OPERATOR_AND
, first_arg
, location
);
2082 // If we are calling a method which was inherited from an
2083 // embedded struct, and the method did not get a stub, then the
2084 // first type may be wrong.
2085 Type
* fatype
= bound_method
->first_argument_type();
2088 if (fatype
->points_to() == NULL
)
2089 fatype
= Type::make_pointer_type(fatype
);
2090 Type
* unsafe
= Type::make_pointer_type(Type::make_void_type());
2091 first_arg
= Expression::make_cast(unsafe
, first_arg
, location
);
2092 first_arg
= Expression::make_cast(fatype
, first_arg
, location
);
2095 vals
->push_back(first_arg
);
2098 if (ce
->args() != NULL
)
2100 for (Expression_list::const_iterator p
= ce
->args()->begin();
2101 p
!= ce
->args()->end();
2103 vals
->push_back(*p
);
2106 // Build the struct.
2107 Expression
* constructor
=
2108 Expression::make_struct_composite_literal(this->struct_type_
, vals
,
2111 // Allocate the initialized struct on the heap.
2112 constructor
= Expression::make_heap_composite(constructor
, location
);
2114 // Look up the thunk.
2115 Named_object
* named_thunk
= gogo
->lookup(thunk_name
, NULL
);
2116 go_assert(named_thunk
!= NULL
&& named_thunk
->is_function());
2119 Expression
* func
= Expression::make_func_reference(named_thunk
, NULL
,
2121 Expression_list
* params
= new Expression_list();
2122 params
->push_back(constructor
);
2123 Call_expression
* call
= Expression::make_call(func
, params
, false, location
);
2125 // Build the simple go or defer statement.
2127 if (this->classification() == STATEMENT_GO
)
2128 s
= Statement::make_go_statement(call
, location
);
2129 else if (this->classification() == STATEMENT_DEFER
)
2130 s
= Statement::make_defer_statement(call
, location
);
2134 // The current block should end with the go statement.
2135 go_assert(block
->statements()->size() >= 1);
2136 go_assert(block
->statements()->back() == this);
2137 block
->replace_statement(block
->statements()->size() - 1, s
);
2139 // We already ran the determine_types pass, so we need to run it now
2140 // for the new statement.
2141 s
->determine_types();
2144 gogo
->check_types_in_block(block
);
2146 // Return true to tell the block not to keep looking at statements.
2150 // Set the name to use for thunk parameter N.
2153 Thunk_statement::thunk_field_param(int n
, char* buf
, size_t buflen
)
2155 snprintf(buf
, buflen
, "a%d", n
);
2158 // Build a new struct type to hold the parameters for a complicated
2159 // thunk statement. FNTYPE is the type of the function call.
2162 Thunk_statement::build_struct(Function_type
* fntype
)
2164 source_location location
= this->location();
2166 Struct_field_list
* fields
= new Struct_field_list();
2168 Call_expression
* ce
= this->call_
->call_expression();
2169 Expression
* fn
= ce
->fn();
2171 if (!this->is_constant_function())
2173 // The function to call.
2174 fields
->push_back(Struct_field(Typed_identifier("fn", fntype
,
2178 // If this thunk statement calls a method on an interface, we pass
2179 // the interface object to the thunk.
2180 Interface_field_reference_expression
* interface_method
=
2181 fn
->interface_field_reference_expression();
2182 if (interface_method
!= NULL
)
2184 Typed_identifier
tid("object", interface_method
->expr()->type(),
2186 fields
->push_back(Struct_field(tid
));
2189 // If this is a method call, pass down the expression we are
2191 if (fn
->bound_method_expression() != NULL
)
2193 go_assert(fntype
->is_method());
2194 Type
* rtype
= fntype
->receiver()->type();
2195 // We always pass the receiver as a pointer.
2196 if (rtype
->points_to() == NULL
)
2197 rtype
= Type::make_pointer_type(rtype
);
2198 Typed_identifier
tid("receiver", rtype
, location
);
2199 fields
->push_back(Struct_field(tid
));
2202 // The predeclared recover function has no argument. However, we
2203 // add an argument when building recover thunks. Handle that here.
2204 if (ce
->is_recover_call())
2206 fields
->push_back(Struct_field(Typed_identifier("can_recover",
2207 Type::lookup_bool_type(),
2211 const Expression_list
* args
= ce
->args();
2215 for (Expression_list::const_iterator p
= args
->begin();
2220 this->thunk_field_param(i
, buf
, sizeof buf
);
2221 fields
->push_back(Struct_field(Typed_identifier(buf
, (*p
)->type(),
2226 return Type::make_struct_type(fields
, location
);
2229 // Build the thunk we are going to call. This is a brand new, albeit
2230 // artificial, function.
2233 Thunk_statement::build_thunk(Gogo
* gogo
, const std::string
& thunk_name
)
2235 source_location location
= this->location();
2237 Call_expression
* ce
= this->call_
->call_expression();
2239 bool may_call_recover
= false;
2240 if (this->classification() == STATEMENT_DEFER
)
2242 Func_expression
* fn
= ce
->fn()->func_expression();
2244 may_call_recover
= true;
2247 const Named_object
* no
= fn
->named_object();
2248 if (!no
->is_function())
2249 may_call_recover
= true;
2251 may_call_recover
= no
->func_value()->calls_recover();
2255 // Build the type of the thunk. The thunk takes a single parameter,
2256 // which is a pointer to the special structure we build.
2257 const char* const parameter_name
= "__go_thunk_parameter";
2258 Typed_identifier_list
* thunk_parameters
= new Typed_identifier_list();
2259 Type
* pointer_to_struct_type
= Type::make_pointer_type(this->struct_type_
);
2260 thunk_parameters
->push_back(Typed_identifier(parameter_name
,
2261 pointer_to_struct_type
,
2264 Typed_identifier_list
* thunk_results
= NULL
;
2265 if (may_call_recover
)
2267 // When deferring a function which may call recover, add a
2268 // return value, to disable tail call optimizations which will
2269 // break the way we check whether recover is permitted.
2270 thunk_results
= new Typed_identifier_list();
2271 thunk_results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
2275 Function_type
* thunk_type
= Type::make_function_type(NULL
, thunk_parameters
,
2279 // Start building the thunk.
2280 Named_object
* function
= gogo
->start_function(thunk_name
, thunk_type
, true,
2283 gogo
->start_block(location
);
2285 // For a defer statement, start with a call to
2286 // __go_set_defer_retaddr. */
2287 Label
* retaddr_label
= NULL
;
2288 if (may_call_recover
)
2290 retaddr_label
= gogo
->add_label_reference("retaddr");
2291 Expression
* arg
= Expression::make_label_addr(retaddr_label
, location
);
2292 Expression
* call
= Runtime::make_call(Runtime::SET_DEFER_RETADDR
,
2295 // This is a hack to prevent the middle-end from deleting the
2297 gogo
->start_block(location
);
2298 gogo
->add_statement(Statement::make_goto_statement(retaddr_label
,
2300 Block
* then_block
= gogo
->finish_block(location
);
2301 then_block
->determine_types();
2303 Statement
* s
= Statement::make_if_statement(call
, then_block
, NULL
,
2305 s
->determine_types();
2306 gogo
->add_statement(s
);
2309 // Get a reference to the parameter.
2310 Named_object
* named_parameter
= gogo
->lookup(parameter_name
, NULL
);
2311 go_assert(named_parameter
!= NULL
&& named_parameter
->is_variable());
2313 // Build the call. Note that the field names are the same as the
2314 // ones used in build_struct.
2315 Expression
* thunk_parameter
= Expression::make_var_reference(named_parameter
,
2317 thunk_parameter
= Expression::make_unary(OPERATOR_MULT
, thunk_parameter
,
2320 Bound_method_expression
* bound_method
= ce
->fn()->bound_method_expression();
2321 Interface_field_reference_expression
* interface_method
=
2322 ce
->fn()->interface_field_reference_expression();
2324 Expression
* func_to_call
;
2325 unsigned int next_index
;
2326 if (this->is_constant_function())
2328 func_to_call
= ce
->fn();
2333 func_to_call
= Expression::make_field_reference(thunk_parameter
,
2338 if (bound_method
!= NULL
)
2340 go_assert(next_index
== 0);
2341 Expression
* r
= Expression::make_field_reference(thunk_parameter
, 0,
2343 func_to_call
= Expression::make_bound_method(r
, bound_method
->method(),
2347 else if (interface_method
!= NULL
)
2349 // The main program passes the interface object.
2350 go_assert(next_index
== 0);
2351 Expression
* r
= Expression::make_field_reference(thunk_parameter
, 0,
2353 const std::string
& name(interface_method
->name());
2354 func_to_call
= Expression::make_interface_field_reference(r
, name
,
2359 Expression_list
* call_params
= new Expression_list();
2360 const Struct_field_list
* fields
= this->struct_type_
->fields();
2361 Struct_field_list::const_iterator p
= fields
->begin();
2362 for (unsigned int i
= 0; i
< next_index
; ++i
)
2364 bool is_recover_call
= ce
->is_recover_call();
2365 Expression
* recover_arg
= NULL
;
2366 for (; p
!= fields
->end(); ++p
, ++next_index
)
2368 Expression
* thunk_param
= Expression::make_var_reference(named_parameter
,
2370 thunk_param
= Expression::make_unary(OPERATOR_MULT
, thunk_param
,
2372 Expression
* param
= Expression::make_field_reference(thunk_param
,
2375 if (!is_recover_call
)
2376 call_params
->push_back(param
);
2379 go_assert(call_params
->empty());
2380 recover_arg
= param
;
2384 if (call_params
->empty())
2390 Call_expression
* call
= Expression::make_call(func_to_call
, call_params
,
2392 Statement
* call_statement
= Statement::make_statement(call
);
2394 gogo
->add_statement(call_statement
);
2396 // If this is a defer statement, the label comes immediately after
2398 if (may_call_recover
)
2400 gogo
->add_label_definition("retaddr", location
);
2402 Expression_list
* vals
= new Expression_list();
2403 vals
->push_back(Expression::make_boolean(false, location
));
2404 gogo
->add_statement(Statement::make_return_statement(vals
, location
));
2407 Block
* b
= gogo
->finish_block(location
);
2409 gogo
->add_block(b
, location
);
2411 gogo
->lower_block(function
, b
);
2413 // We already ran the determine_types pass, so we need to run it
2414 // just for the call statement now. The other types are known.
2415 call_statement
->determine_types();
2417 if (may_call_recover
|| recover_arg
!= NULL
)
2419 // Dig up the call expression, which may have been changed
2421 go_assert(call_statement
->classification() == STATEMENT_EXPRESSION
);
2422 Expression_statement
* es
=
2423 static_cast<Expression_statement
*>(call_statement
);
2424 Call_expression
* ce
= es
->expr()->call_expression();
2425 go_assert(ce
!= NULL
);
2426 if (may_call_recover
)
2427 ce
->set_is_deferred();
2428 if (recover_arg
!= NULL
)
2429 ce
->set_recover_arg(recover_arg
);
2432 // That is all the thunk has to do.
2433 gogo
->finish_function(location
);
2436 // Get the function and argument expressions.
2439 Thunk_statement::get_fn_and_arg(Expression
** pfn
, Expression
** parg
)
2441 if (this->call_
->is_error_expression())
2444 Call_expression
* ce
= this->call_
->call_expression();
2448 const Expression_list
* args
= ce
->args();
2449 if (args
== NULL
|| args
->empty())
2450 *parg
= Expression::make_nil(this->location());
2453 go_assert(args
->size() == 1);
2454 *parg
= args
->front();
2460 // Class Go_statement.
2463 Go_statement::do_get_backend(Translate_context
* context
)
2467 if (!this->get_fn_and_arg(&fn
, &arg
))
2468 return context
->backend()->error_statement();
2470 Expression
* call
= Runtime::make_call(Runtime::GO
, this->location(), 2,
2472 tree call_tree
= call
->get_tree(context
);
2473 Bexpression
* call_bexpr
= tree_to_expr(call_tree
);
2474 return context
->backend()->expression_statement(call_bexpr
);
2477 // Dump the AST representation for go statement.
2480 Go_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2482 ast_dump_context
->print_indent();
2483 ast_dump_context
->ostream() << "go ";
2484 ast_dump_context
->dump_expression(this->call());
2485 ast_dump_context
->ostream() << std::endl
;
2488 // Make a go statement.
2491 Statement::make_go_statement(Call_expression
* call
, source_location location
)
2493 return new Go_statement(call
, location
);
2496 // Class Defer_statement.
2499 Defer_statement::do_get_backend(Translate_context
* context
)
2503 if (!this->get_fn_and_arg(&fn
, &arg
))
2504 return context
->backend()->error_statement();
2506 source_location loc
= this->location();
2507 Expression
* ds
= context
->function()->func_value()->defer_stack(loc
);
2509 Expression
* call
= Runtime::make_call(Runtime::DEFER
, loc
, 3,
2511 tree call_tree
= call
->get_tree(context
);
2512 Bexpression
* call_bexpr
= tree_to_expr(call_tree
);
2513 return context
->backend()->expression_statement(call_bexpr
);
2516 // Dump the AST representation for defer statement.
2519 Defer_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2521 ast_dump_context
->print_indent();
2522 ast_dump_context
->ostream() << "defer ";
2523 ast_dump_context
->dump_expression(this->call());
2524 ast_dump_context
->ostream() << std::endl
;
2527 // Make a defer statement.
2530 Statement::make_defer_statement(Call_expression
* call
,
2531 source_location location
)
2533 return new Defer_statement(call
, location
);
2536 // Class Return_statement.
2538 // Traverse assignments. We treat each return value as a top level
2539 // RHS in an expression.
2542 Return_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
2544 Expression_list
* vals
= this->vals_
;
2547 for (Expression_list::iterator p
= vals
->begin();
2550 tassign
->value(&*p
, true, true);
2555 // Lower a return statement. If we are returning a function call
2556 // which returns multiple values which match the current function,
2557 // split up the call's results. If the function has named result
2558 // variables, and the return statement lists explicit values, then
2559 // implement it by assigning the values to the result variables and
2560 // changing the statement to not list any values. This lets
2561 // panic/recover work correctly.
2564 Return_statement::do_lower(Gogo
*, Named_object
* function
, Block
* enclosing
,
2565 Statement_inserter
*)
2567 if (this->is_lowered_
)
2570 Expression_list
* vals
= this->vals_
;
2572 this->is_lowered_
= true;
2574 source_location loc
= this->location();
2576 size_t vals_count
= vals
== NULL
? 0 : vals
->size();
2577 Function::Results
* results
= function
->func_value()->result_variables();
2578 size_t results_count
= results
== NULL
? 0 : results
->size();
2580 if (vals_count
== 0)
2582 if (results_count
> 0 && !function
->func_value()->results_are_named())
2584 this->report_error(_("not enough arguments to return"));
2590 if (results_count
== 0)
2592 this->report_error(_("return with value in function "
2593 "with no return type"));
2597 // If the current function has multiple return values, and we are
2598 // returning a single call expression, split up the call expression.
2599 if (results_count
> 1
2600 && vals
->size() == 1
2601 && vals
->front()->call_expression() != NULL
)
2603 Call_expression
* call
= vals
->front()->call_expression();
2605 vals
= new Expression_list
;
2606 for (size_t i
= 0; i
< results_count
; ++i
)
2607 vals
->push_back(Expression::make_call_result(call
, i
));
2608 vals_count
= results_count
;
2611 if (vals_count
< results_count
)
2613 this->report_error(_("not enough arguments to return"));
2617 if (vals_count
> results_count
)
2619 this->report_error(_("too many values in return statement"));
2623 Block
* b
= new Block(enclosing
, loc
);
2625 Expression_list
* lhs
= new Expression_list();
2626 Expression_list
* rhs
= new Expression_list();
2628 Expression_list::const_iterator pe
= vals
->begin();
2630 for (Function::Results::const_iterator pr
= results
->begin();
2631 pr
!= results
->end();
2634 Named_object
* rv
= *pr
;
2635 Expression
* e
= *pe
;
2637 // Check types now so that we give a good error message. The
2638 // result type is known. We determine the expression type
2641 Type
*rvtype
= rv
->result_var_value()->type();
2642 Type_context
type_context(rvtype
, false);
2643 e
->determine_type(&type_context
);
2646 if (Type::are_assignable(rvtype
, e
->type(), &reason
))
2648 Expression
* ve
= Expression::make_var_reference(rv
, e
->location());
2655 error_at(e
->location(), "incompatible type for return value %d", i
);
2657 error_at(e
->location(),
2658 "incompatible type for return value %d (%s)",
2662 go_assert(lhs
->size() == rhs
->size());
2666 else if (lhs
->size() == 1)
2668 b
->add_statement(Statement::make_assignment(lhs
->front(), rhs
->front(),
2674 b
->add_statement(Statement::make_tuple_assignment(lhs
, rhs
, loc
));
2676 b
->add_statement(this);
2680 return Statement::make_block_statement(b
, loc
);
2683 // Convert a return statement to the backend representation.
2686 Return_statement::do_get_backend(Translate_context
* context
)
2688 source_location loc
= this->location();
2690 Function
* function
= context
->function()->func_value();
2691 tree fndecl
= function
->get_decl();
2693 Function::Results
* results
= function
->result_variables();
2694 std::vector
<Bexpression
*> retvals
;
2695 if (results
!= NULL
&& !results
->empty())
2697 retvals
.reserve(results
->size());
2698 for (Function::Results::const_iterator p
= results
->begin();
2699 p
!= results
->end();
2702 Expression
* vr
= Expression::make_var_reference(*p
, loc
);
2703 retvals
.push_back(tree_to_expr(vr
->get_tree(context
)));
2707 return context
->backend()->return_statement(tree_to_function(fndecl
),
2711 // Dump the AST representation for a return statement.
2714 Return_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2716 ast_dump_context
->print_indent();
2717 ast_dump_context
->ostream() << "return " ;
2718 ast_dump_context
->dump_expression_list(this->vals_
);
2719 ast_dump_context
->ostream() << std::endl
;
2722 // Make a return statement.
2725 Statement::make_return_statement(Expression_list
* vals
,
2726 source_location location
)
2728 return new Return_statement(vals
, location
);
2731 // A break or continue statement.
2733 class Bc_statement
: public Statement
2736 Bc_statement(bool is_break
, Unnamed_label
* label
, source_location location
)
2737 : Statement(STATEMENT_BREAK_OR_CONTINUE
, location
),
2738 label_(label
), is_break_(is_break
)
2743 { return this->is_break_
; }
2747 do_traverse(Traverse
*)
2748 { return TRAVERSE_CONTINUE
; }
2751 do_may_fall_through() const
2755 do_get_backend(Translate_context
* context
)
2756 { return this->label_
->get_goto(context
, this->location()); }
2759 do_dump_statement(Ast_dump_context
*) const;
2762 // The label that this branches to.
2763 Unnamed_label
* label_
;
2764 // True if this is "break", false if it is "continue".
2768 // Dump the AST representation for a break/continue statement
2771 Bc_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2773 ast_dump_context
->print_indent();
2774 ast_dump_context
->ostream() << (this->is_break_
? "break" : "continue");
2775 if (this->label_
!= NULL
)
2777 ast_dump_context
->ostream() << " ";
2778 ast_dump_context
->dump_label_name(this->label_
);
2780 ast_dump_context
->ostream() << std::endl
;
2783 // Make a break statement.
2786 Statement::make_break_statement(Unnamed_label
* label
, source_location location
)
2788 return new Bc_statement(true, label
, location
);
2791 // Make a continue statement.
2794 Statement::make_continue_statement(Unnamed_label
* label
,
2795 source_location location
)
2797 return new Bc_statement(false, label
, location
);
2800 // A goto statement.
2802 class Goto_statement
: public Statement
2805 Goto_statement(Label
* label
, source_location location
)
2806 : Statement(STATEMENT_GOTO
, location
),
2812 do_traverse(Traverse
*)
2813 { return TRAVERSE_CONTINUE
; }
2816 do_check_types(Gogo
*);
2819 do_may_fall_through() const
2823 do_get_backend(Translate_context
*);
2826 do_dump_statement(Ast_dump_context
*) const;
2832 // Check types for a label. There aren't any types per se, but we use
2833 // this to give an error if the label was never defined.
2836 Goto_statement::do_check_types(Gogo
*)
2838 if (!this->label_
->is_defined())
2840 error_at(this->location(), "reference to undefined label %qs",
2841 Gogo::message_name(this->label_
->name()).c_str());
2842 this->set_is_error();
2846 // Convert the goto statement to the backend representation.
2849 Goto_statement::do_get_backend(Translate_context
* context
)
2851 Blabel
* blabel
= this->label_
->get_backend_label(context
);
2852 return context
->backend()->goto_statement(blabel
, this->location());
2855 // Dump the AST representation for a goto statement.
2858 Goto_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2860 ast_dump_context
->print_indent();
2861 ast_dump_context
->ostream() << "goto " << this->label_
->name() << std::endl
;
2864 // Make a goto statement.
2867 Statement::make_goto_statement(Label
* label
, source_location location
)
2869 return new Goto_statement(label
, location
);
2872 // A goto statement to an unnamed label.
2874 class Goto_unnamed_statement
: public Statement
2877 Goto_unnamed_statement(Unnamed_label
* label
, source_location location
)
2878 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
2884 do_traverse(Traverse
*)
2885 { return TRAVERSE_CONTINUE
; }
2888 do_may_fall_through() const
2892 do_get_backend(Translate_context
* context
)
2893 { return this->label_
->get_goto(context
, this->location()); }
2896 do_dump_statement(Ast_dump_context
*) const;
2899 Unnamed_label
* label_
;
2902 // Dump the AST representation for an unnamed goto statement
2905 Goto_unnamed_statement::do_dump_statement(
2906 Ast_dump_context
* ast_dump_context
) const
2908 ast_dump_context
->print_indent();
2909 ast_dump_context
->ostream() << "goto ";
2910 ast_dump_context
->dump_label_name(this->label_
);
2911 ast_dump_context
->ostream() << std::endl
;
2914 // Make a goto statement to an unnamed label.
2917 Statement::make_goto_unnamed_statement(Unnamed_label
* label
,
2918 source_location location
)
2920 return new Goto_unnamed_statement(label
, location
);
2923 // Class Label_statement.
2928 Label_statement::do_traverse(Traverse
*)
2930 return TRAVERSE_CONTINUE
;
2933 // Return the backend representation of the statement defining this
2937 Label_statement::do_get_backend(Translate_context
* context
)
2939 Blabel
* blabel
= this->label_
->get_backend_label(context
);
2940 return context
->backend()->label_definition_statement(blabel
);
2943 // Dump the AST for a label definition statement.
2946 Label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2948 ast_dump_context
->print_indent();
2949 ast_dump_context
->ostream() << this->label_
->name() << ":" << std::endl
;
2952 // Make a label statement.
2955 Statement::make_label_statement(Label
* label
, source_location location
)
2957 return new Label_statement(label
, location
);
2960 // An unnamed label statement.
2962 class Unnamed_label_statement
: public Statement
2965 Unnamed_label_statement(Unnamed_label
* label
)
2966 : Statement(STATEMENT_UNNAMED_LABEL
, label
->location()),
2972 do_traverse(Traverse
*)
2973 { return TRAVERSE_CONTINUE
; }
2976 do_get_backend(Translate_context
* context
)
2977 { return this->label_
->get_definition(context
); }
2980 do_dump_statement(Ast_dump_context
*) const;
2984 Unnamed_label
* label_
;
2987 // Dump the AST representation for an unnamed label definition statement.
2990 Unnamed_label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
2993 ast_dump_context
->print_indent();
2994 ast_dump_context
->dump_label_name(this->label_
);
2995 ast_dump_context
->ostream() << ":" << std::endl
;
2998 // Make an unnamed label statement.
3001 Statement::make_unnamed_label_statement(Unnamed_label
* label
)
3003 return new Unnamed_label_statement(label
);
3008 class If_statement
: public Statement
3011 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
3012 source_location location
)
3013 : Statement(STATEMENT_IF
, location
),
3014 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
3019 do_traverse(Traverse
*);
3022 do_determine_types();
3025 do_check_types(Gogo
*);
3028 do_may_fall_through() const;
3031 do_get_backend(Translate_context
*);
3034 do_dump_statement(Ast_dump_context
*) const;
3045 If_statement::do_traverse(Traverse
* traverse
)
3047 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
3048 || this->then_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3049 return TRAVERSE_EXIT
;
3050 if (this->else_block_
!= NULL
)
3052 if (this->else_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3053 return TRAVERSE_EXIT
;
3055 return TRAVERSE_CONTINUE
;
3059 If_statement::do_determine_types()
3061 Type_context
context(Type::lookup_bool_type(), false);
3062 this->cond_
->determine_type(&context
);
3063 this->then_block_
->determine_types();
3064 if (this->else_block_
!= NULL
)
3065 this->else_block_
->determine_types();
3071 If_statement::do_check_types(Gogo
*)
3073 Type
* type
= this->cond_
->type();
3074 if (type
->is_error())
3075 this->set_is_error();
3076 else if (!type
->is_boolean_type())
3077 this->report_error(_("expected boolean expression"));
3080 // Whether the overall statement may fall through.
3083 If_statement::do_may_fall_through() const
3085 return (this->else_block_
== NULL
3086 || this->then_block_
->may_fall_through()
3087 || this->else_block_
->may_fall_through());
3090 // Get the backend representation.
3093 If_statement::do_get_backend(Translate_context
* context
)
3095 go_assert(this->cond_
->type()->is_boolean_type()
3096 || this->cond_
->type()->is_error());
3097 tree cond_tree
= this->cond_
->get_tree(context
);
3098 Bexpression
* cond_expr
= tree_to_expr(cond_tree
);
3099 Bblock
* then_block
= this->then_block_
->get_backend(context
);
3100 Bblock
* else_block
= (this->else_block_
== NULL
3102 : this->else_block_
->get_backend(context
));
3103 return context
->backend()->if_statement(cond_expr
, then_block
,
3104 else_block
, this->location());
3107 // Dump the AST representation for an if statement
3110 If_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3112 ast_dump_context
->print_indent();
3113 ast_dump_context
->ostream() << "if ";
3114 ast_dump_context
->dump_expression(this->cond_
);
3115 ast_dump_context
->ostream() << std::endl
;
3116 if (ast_dump_context
->dump_subblocks())
3118 ast_dump_context
->dump_block(this->then_block_
);
3119 if (this->else_block_
!= NULL
)
3121 ast_dump_context
->print_indent();
3122 ast_dump_context
->ostream() << "else" << std::endl
;
3123 ast_dump_context
->dump_block(this->else_block_
);
3128 // Make an if statement.
3131 Statement::make_if_statement(Expression
* cond
, Block
* then_block
,
3132 Block
* else_block
, source_location location
)
3134 return new If_statement(cond
, then_block
, else_block
, location
);
3137 // Class Case_clauses::Hash_integer_value.
3139 class Case_clauses::Hash_integer_value
3143 operator()(Expression
*) const;
3147 Case_clauses::Hash_integer_value::operator()(Expression
* pe
) const
3152 if (!pe
->integer_constant_value(true, ival
, &itype
))
3154 size_t ret
= mpz_get_ui(ival
);
3159 // Class Case_clauses::Eq_integer_value.
3161 class Case_clauses::Eq_integer_value
3165 operator()(Expression
*, Expression
*) const;
3169 Case_clauses::Eq_integer_value::operator()(Expression
* a
, Expression
* b
) const
3177 if (!a
->integer_constant_value(true, aval
, &atype
)
3178 || !b
->integer_constant_value(true, bval
, &btype
))
3180 bool ret
= mpz_cmp(aval
, bval
) == 0;
3186 // Class Case_clauses::Case_clause.
3191 Case_clauses::Case_clause::traverse(Traverse
* traverse
)
3193 if (this->cases_
!= NULL
3194 && (traverse
->traverse_mask()
3195 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3197 if (this->cases_
->traverse(traverse
) == TRAVERSE_EXIT
)
3198 return TRAVERSE_EXIT
;
3200 if (this->statements_
!= NULL
)
3202 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
3203 return TRAVERSE_EXIT
;
3205 return TRAVERSE_CONTINUE
;
3208 // Check whether all the case expressions are integer constants.
3211 Case_clauses::Case_clause::is_constant() const
3213 if (this->cases_
!= NULL
)
3215 for (Expression_list::const_iterator p
= this->cases_
->begin();
3216 p
!= this->cases_
->end();
3218 if (!(*p
)->is_constant() || (*p
)->type()->integer_type() == NULL
)
3224 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3225 // value we are switching on; it may be NULL. If START_LABEL is not
3226 // NULL, it goes at the start of the statements, after the condition
3227 // test. We branch to FINISH_LABEL at the end of the statements.
3230 Case_clauses::Case_clause::lower(Block
* b
, Temporary_statement
* val_temp
,
3231 Unnamed_label
* start_label
,
3232 Unnamed_label
* finish_label
) const
3234 source_location loc
= this->location_
;
3235 Unnamed_label
* next_case_label
;
3236 if (this->cases_
== NULL
|| this->cases_
->empty())
3238 go_assert(this->is_default_
);
3239 next_case_label
= NULL
;
3243 Expression
* cond
= NULL
;
3245 for (Expression_list::const_iterator p
= this->cases_
->begin();
3246 p
!= this->cases_
->end();
3249 Expression
* this_cond
;
3250 if (val_temp
== NULL
)
3254 Expression
* ref
= Expression::make_temporary_reference(val_temp
,
3256 this_cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
, *p
, loc
);
3262 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, this_cond
, loc
);
3265 Block
* then_block
= new Block(b
, loc
);
3266 next_case_label
= new Unnamed_label(UNKNOWN_LOCATION
);
3267 Statement
* s
= Statement::make_goto_unnamed_statement(next_case_label
,
3269 then_block
->add_statement(s
);
3271 // if !COND { goto NEXT_CASE_LABEL }
3272 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3273 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
3274 b
->add_statement(s
);
3277 if (start_label
!= NULL
)
3278 b
->add_statement(Statement::make_unnamed_label_statement(start_label
));
3280 if (this->statements_
!= NULL
)
3281 b
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
3283 Statement
* s
= Statement::make_goto_unnamed_statement(finish_label
, loc
);
3284 b
->add_statement(s
);
3286 if (next_case_label
!= NULL
)
3287 b
->add_statement(Statement::make_unnamed_label_statement(next_case_label
));
3293 Case_clauses::Case_clause::determine_types(Type
* type
)
3295 if (this->cases_
!= NULL
)
3297 Type_context
case_context(type
, false);
3298 for (Expression_list::iterator p
= this->cases_
->begin();
3299 p
!= this->cases_
->end();
3301 (*p
)->determine_type(&case_context
);
3303 if (this->statements_
!= NULL
)
3304 this->statements_
->determine_types();
3307 // Check types. Returns false if there was an error.
3310 Case_clauses::Case_clause::check_types(Type
* type
)
3312 if (this->cases_
!= NULL
)
3314 for (Expression_list::iterator p
= this->cases_
->begin();
3315 p
!= this->cases_
->end();
3318 if (!Type::are_assignable(type
, (*p
)->type(), NULL
)
3319 && !Type::are_assignable((*p
)->type(), type
, NULL
))
3321 error_at((*p
)->location(),
3322 "type mismatch between switch value and case clause");
3330 // Return true if this clause may fall through to the following
3331 // statements. Note that this is not the same as whether the case
3332 // uses the "fallthrough" keyword.
3335 Case_clauses::Case_clause::may_fall_through() const
3337 if (this->statements_
== NULL
)
3339 return this->statements_
->may_fall_through();
3342 // Convert the case values and statements to the backend
3343 // representation. BREAK_LABEL is the label which break statements
3344 // should branch to. CASE_CONSTANTS is used to detect duplicate
3345 // constants. *CASES should be passed as an empty vector; the values
3346 // for this case will be added to it. If this is the default case,
3347 // *CASES will remain empty. This returns the statement to execute if
3348 // one of these cases is selected.
3351 Case_clauses::Case_clause::get_backend(Translate_context
* context
,
3352 Unnamed_label
* break_label
,
3353 Case_constants
* case_constants
,
3354 std::vector
<Bexpression
*>* cases
) const
3356 if (this->cases_
!= NULL
)
3358 go_assert(!this->is_default_
);
3359 for (Expression_list::const_iterator p
= this->cases_
->begin();
3360 p
!= this->cases_
->end();
3364 if (e
->classification() != Expression::EXPRESSION_INTEGER
)
3369 if (!(*p
)->integer_constant_value(true, ival
, &itype
))
3371 // Something went wrong. This can happen with a
3372 // negative constant and an unsigned switch value.
3373 go_assert(saw_errors());
3376 go_assert(itype
!= NULL
);
3377 e
= Expression::make_integer(&ival
, itype
, e
->location());
3381 std::pair
<Case_constants::iterator
, bool> ins
=
3382 case_constants
->insert(e
);
3385 // Value was already present.
3386 error_at(this->location_
, "duplicate case in switch");
3390 tree case_tree
= e
->get_tree(context
);
3391 Bexpression
* case_expr
= tree_to_expr(case_tree
);
3392 cases
->push_back(case_expr
);
3396 Bstatement
* statements
;
3397 if (this->statements_
== NULL
)
3401 Bblock
* bblock
= this->statements_
->get_backend(context
);
3402 statements
= context
->backend()->block_statement(bblock
);
3405 Bstatement
* break_stat
;
3406 if (this->is_fallthrough_
)
3409 break_stat
= break_label
->get_goto(context
, this->location_
);
3411 if (statements
== NULL
)
3413 else if (break_stat
== NULL
)
3416 return context
->backend()->compound_statement(statements
, break_stat
);
3419 // Dump the AST representation for a case clause
3422 Case_clauses::Case_clause::dump_clause(Ast_dump_context
* ast_dump_context
)
3425 ast_dump_context
->print_indent();
3426 if (this->is_default_
)
3428 ast_dump_context
->ostream() << "default:";
3432 ast_dump_context
->ostream() << "case ";
3433 ast_dump_context
->dump_expression_list(this->cases_
);
3434 ast_dump_context
->ostream() << ":" ;
3436 ast_dump_context
->dump_block(this->statements_
);
3437 if (this->is_fallthrough_
)
3439 ast_dump_context
->print_indent();
3440 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
3444 // Class Case_clauses.
3449 Case_clauses::traverse(Traverse
* traverse
)
3451 for (Clauses::iterator p
= this->clauses_
.begin();
3452 p
!= this->clauses_
.end();
3455 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
3456 return TRAVERSE_EXIT
;
3458 return TRAVERSE_CONTINUE
;
3461 // Check whether all the case expressions are constant.
3464 Case_clauses::is_constant() const
3466 for (Clauses::const_iterator p
= this->clauses_
.begin();
3467 p
!= this->clauses_
.end();
3469 if (!p
->is_constant())
3474 // Lower case clauses for a nonconstant switch.
3477 Case_clauses::lower(Block
* b
, Temporary_statement
* val_temp
,
3478 Unnamed_label
* break_label
) const
3480 // The default case.
3481 const Case_clause
* default_case
= NULL
;
3483 // The label for the fallthrough of the previous case.
3484 Unnamed_label
* last_fallthrough_label
= NULL
;
3486 // The label for the start of the default case. This is used if the
3487 // case before the default case falls through.
3488 Unnamed_label
* default_start_label
= NULL
;
3490 // The label for the end of the default case. This normally winds
3491 // up as BREAK_LABEL, but it will be different if the default case
3493 Unnamed_label
* default_finish_label
= NULL
;
3495 for (Clauses::const_iterator p
= this->clauses_
.begin();
3496 p
!= this->clauses_
.end();
3499 // The label to use for the start of the statements for this
3500 // case. This is NULL unless the previous case falls through.
3501 Unnamed_label
* start_label
= last_fallthrough_label
;
3503 // The label to jump to after the end of the statements for this
3505 Unnamed_label
* finish_label
= break_label
;
3507 last_fallthrough_label
= NULL
;
3508 if (p
->is_fallthrough() && p
+ 1 != this->clauses_
.end())
3510 finish_label
= new Unnamed_label(p
->location());
3511 last_fallthrough_label
= finish_label
;
3514 if (!p
->is_default())
3515 p
->lower(b
, val_temp
, start_label
, finish_label
);
3518 // We have to move the default case to the end, so that we
3519 // only use it if all the other tests fail.
3521 default_start_label
= start_label
;
3522 default_finish_label
= finish_label
;
3526 if (default_case
!= NULL
)
3527 default_case
->lower(b
, val_temp
, default_start_label
,
3528 default_finish_label
);
3534 Case_clauses::determine_types(Type
* type
)
3536 for (Clauses::iterator p
= this->clauses_
.begin();
3537 p
!= this->clauses_
.end();
3539 p
->determine_types(type
);
3542 // Check types. Returns false if there was an error.
3545 Case_clauses::check_types(Type
* type
)
3548 for (Clauses::iterator p
= this->clauses_
.begin();
3549 p
!= this->clauses_
.end();
3552 if (!p
->check_types(type
))
3558 // Return true if these clauses may fall through to the statements
3559 // following the switch statement.
3562 Case_clauses::may_fall_through() const
3564 bool found_default
= false;
3565 for (Clauses::const_iterator p
= this->clauses_
.begin();
3566 p
!= this->clauses_
.end();
3569 if (p
->may_fall_through() && !p
->is_fallthrough())
3571 if (p
->is_default())
3572 found_default
= true;
3574 return !found_default
;
3577 // Convert the cases to the backend representation. This sets
3578 // *ALL_CASES and *ALL_STATEMENTS.
3581 Case_clauses::get_backend(Translate_context
* context
,
3582 Unnamed_label
* break_label
,
3583 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
3584 std::vector
<Bstatement
*>* all_statements
) const
3586 Case_constants case_constants
;
3588 size_t c
= this->clauses_
.size();
3589 all_cases
->resize(c
);
3590 all_statements
->resize(c
);
3593 for (Clauses::const_iterator p
= this->clauses_
.begin();
3594 p
!= this->clauses_
.end();
3597 std::vector
<Bexpression
*> cases
;
3598 Bstatement
* stat
= p
->get_backend(context
, break_label
, &case_constants
,
3600 (*all_cases
)[i
].swap(cases
);
3601 (*all_statements
)[i
] = stat
;
3605 // Dump the AST representation for case clauses (from a switch statement)
3608 Case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
3610 for (Clauses::const_iterator p
= this->clauses_
.begin();
3611 p
!= this->clauses_
.end();
3613 p
->dump_clause(ast_dump_context
);
3616 // A constant switch statement. A Switch_statement is lowered to this
3617 // when all the cases are constants.
3619 class Constant_switch_statement
: public Statement
3622 Constant_switch_statement(Expression
* val
, Case_clauses
* clauses
,
3623 Unnamed_label
* break_label
,
3624 source_location location
)
3625 : Statement(STATEMENT_CONSTANT_SWITCH
, location
),
3626 val_(val
), clauses_(clauses
), break_label_(break_label
)
3631 do_traverse(Traverse
*);
3634 do_determine_types();
3637 do_check_types(Gogo
*);
3640 do_may_fall_through() const;
3643 do_get_backend(Translate_context
*);
3646 do_dump_statement(Ast_dump_context
*) const;
3649 // The value to switch on.
3651 // The case clauses.
3652 Case_clauses
* clauses_
;
3653 // The break label, if needed.
3654 Unnamed_label
* break_label_
;
3660 Constant_switch_statement::do_traverse(Traverse
* traverse
)
3662 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3663 return TRAVERSE_EXIT
;
3664 return this->clauses_
->traverse(traverse
);
3670 Constant_switch_statement::do_determine_types()
3672 this->val_
->determine_type_no_context();
3673 this->clauses_
->determine_types(this->val_
->type());
3679 Constant_switch_statement::do_check_types(Gogo
*)
3681 if (!this->clauses_
->check_types(this->val_
->type()))
3682 this->set_is_error();
3685 // Return whether this switch may fall through.
3688 Constant_switch_statement::do_may_fall_through() const
3690 if (this->clauses_
== NULL
)
3693 // If we have a break label, then some case needed it. That implies
3694 // that the switch statement as a whole can fall through.
3695 if (this->break_label_
!= NULL
)
3698 return this->clauses_
->may_fall_through();
3701 // Convert to GENERIC.
3704 Constant_switch_statement::do_get_backend(Translate_context
* context
)
3706 tree switch_val_tree
= this->val_
->get_tree(context
);
3707 Bexpression
* switch_val_expr
= tree_to_expr(switch_val_tree
);
3709 Unnamed_label
* break_label
= this->break_label_
;
3710 if (break_label
== NULL
)
3711 break_label
= new Unnamed_label(this->location());
3713 std::vector
<std::vector
<Bexpression
*> > all_cases
;
3714 std::vector
<Bstatement
*> all_statements
;
3715 this->clauses_
->get_backend(context
, break_label
, &all_cases
,
3718 Bstatement
* switch_statement
;
3719 switch_statement
= context
->backend()->switch_statement(switch_val_expr
,
3723 Bstatement
* ldef
= break_label
->get_definition(context
);
3724 return context
->backend()->compound_statement(switch_statement
, ldef
);
3727 // Dump the AST representation for a constant switch statement.
3730 Constant_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3733 ast_dump_context
->print_indent();
3734 ast_dump_context
->ostream() << "switch ";
3735 ast_dump_context
->dump_expression(this->val_
);
3737 if (ast_dump_context
->dump_subblocks())
3739 ast_dump_context
->ostream() << " {" << std::endl
;
3740 this->clauses_
->dump_clauses(ast_dump_context
);
3741 ast_dump_context
->ostream() << "}";
3744 ast_dump_context
->ostream() << std::endl
;
3747 // Class Switch_statement.
3752 Switch_statement::do_traverse(Traverse
* traverse
)
3754 if (this->val_
!= NULL
)
3756 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3757 return TRAVERSE_EXIT
;
3759 return this->clauses_
->traverse(traverse
);
3762 // Lower a Switch_statement to a Constant_switch_statement or a series
3763 // of if statements.
3766 Switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
3767 Statement_inserter
*)
3769 source_location loc
= this->location();
3771 if (this->val_
!= NULL
3772 && (this->val_
->is_error_expression()
3773 || this->val_
->type()->is_error()))
3774 return Statement::make_error_statement(loc
);
3776 if (this->val_
!= NULL
3777 && this->val_
->type()->integer_type() != NULL
3778 && !this->clauses_
->empty()
3779 && this->clauses_
->is_constant())
3780 return new Constant_switch_statement(this->val_
, this->clauses_
,
3781 this->break_label_
, loc
);
3783 Block
* b
= new Block(enclosing
, loc
);
3785 if (this->clauses_
->empty())
3787 Expression
* val
= this->val_
;
3789 val
= Expression::make_boolean(true, loc
);
3790 return Statement::make_statement(val
);
3793 Temporary_statement
* val_temp
;
3794 if (this->val_
== NULL
)
3798 // var val_temp VAL_TYPE = VAL
3799 val_temp
= Statement::make_temporary(NULL
, this->val_
, loc
);
3800 b
->add_statement(val_temp
);
3803 this->clauses_
->lower(b
, val_temp
, this->break_label());
3805 Statement
* s
= Statement::make_unnamed_label_statement(this->break_label_
);
3806 b
->add_statement(s
);
3808 return Statement::make_block_statement(b
, loc
);
3811 // Return the break label for this switch statement, creating it if
3815 Switch_statement::break_label()
3817 if (this->break_label_
== NULL
)
3818 this->break_label_
= new Unnamed_label(this->location());
3819 return this->break_label_
;
3822 // Dump the AST representation for a switch statement.
3825 Switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3827 ast_dump_context
->print_indent();
3828 ast_dump_context
->ostream() << "switch ";
3829 if (this->val_
!= NULL
)
3831 ast_dump_context
->dump_expression(this->val_
);
3833 if (ast_dump_context
->dump_subblocks())
3835 ast_dump_context
->ostream() << " {" << std::endl
;
3836 this->clauses_
->dump_clauses(ast_dump_context
);
3837 ast_dump_context
->print_indent();
3838 ast_dump_context
->ostream() << "}";
3840 ast_dump_context
->ostream() << std::endl
;
3843 // Make a switch statement.
3846 Statement::make_switch_statement(Expression
* val
, source_location location
)
3848 return new Switch_statement(val
, location
);
3851 // Class Type_case_clauses::Type_case_clause.
3856 Type_case_clauses::Type_case_clause::traverse(Traverse
* traverse
)
3858 if (!this->is_default_
3859 && ((traverse
->traverse_mask()
3860 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3861 && Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3862 return TRAVERSE_EXIT
;
3863 if (this->statements_
!= NULL
)
3864 return this->statements_
->traverse(traverse
);
3865 return TRAVERSE_CONTINUE
;
3868 // Lower one clause in a type switch. Add statements to the block B.
3869 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3870 // BREAK_LABEL is the label at the end of the type switch.
3871 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3875 Type_case_clauses::Type_case_clause::lower(Block
* b
,
3876 Temporary_statement
* descriptor_temp
,
3877 Unnamed_label
* break_label
,
3878 Unnamed_label
** stmts_label
) const
3880 source_location loc
= this->location_
;
3882 Unnamed_label
* next_case_label
= NULL
;
3883 if (!this->is_default_
)
3885 Type
* type
= this->type_
;
3887 Expression
* ref
= Expression::make_temporary_reference(descriptor_temp
,
3891 // The language permits case nil, which is of course a constant
3892 // rather than a type. It will appear here as an invalid
3894 if (type
->is_nil_constant_as_type())
3895 cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3896 Expression::make_nil(loc
),
3899 cond
= Runtime::make_call((type
->interface_type() == NULL
3900 ? Runtime::IFACETYPEEQ
3901 : Runtime::IFACEI2TP
),
3903 Expression::make_type_descriptor(type
, loc
),
3906 Unnamed_label
* dest
;
3907 if (!this->is_fallthrough_
)
3909 // if !COND { goto NEXT_CASE_LABEL }
3910 next_case_label
= new Unnamed_label(UNKNOWN_LOCATION
);
3911 dest
= next_case_label
;
3912 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3916 // if COND { goto STMTS_LABEL }
3917 go_assert(stmts_label
!= NULL
);
3918 if (*stmts_label
== NULL
)
3919 *stmts_label
= new Unnamed_label(UNKNOWN_LOCATION
);
3920 dest
= *stmts_label
;
3922 Block
* then_block
= new Block(b
, loc
);
3923 Statement
* s
= Statement::make_goto_unnamed_statement(dest
, loc
);
3924 then_block
->add_statement(s
);
3925 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
3926 b
->add_statement(s
);
3929 if (this->statements_
!= NULL
3930 || (!this->is_fallthrough_
3931 && stmts_label
!= NULL
3932 && *stmts_label
!= NULL
))
3934 go_assert(!this->is_fallthrough_
);
3935 if (stmts_label
!= NULL
&& *stmts_label
!= NULL
)
3937 go_assert(!this->is_default_
);
3938 if (this->statements_
!= NULL
)
3939 (*stmts_label
)->set_location(this->statements_
->start_location());
3940 Statement
* s
= Statement::make_unnamed_label_statement(*stmts_label
);
3941 b
->add_statement(s
);
3942 *stmts_label
= NULL
;
3944 if (this->statements_
!= NULL
)
3945 b
->add_statement(Statement::make_block_statement(this->statements_
,
3949 if (this->is_fallthrough_
)
3950 go_assert(next_case_label
== NULL
);
3953 source_location gloc
= (this->statements_
== NULL
3955 : this->statements_
->end_location());
3956 b
->add_statement(Statement::make_goto_unnamed_statement(break_label
,
3958 if (next_case_label
!= NULL
)
3961 Statement::make_unnamed_label_statement(next_case_label
);
3962 b
->add_statement(s
);
3967 // Dump the AST representation for a type case clause
3970 Type_case_clauses::Type_case_clause::dump_clause(
3971 Ast_dump_context
* ast_dump_context
) const
3973 ast_dump_context
->print_indent();
3974 if (this->is_default_
)
3976 ast_dump_context
->ostream() << "default:";
3980 ast_dump_context
->ostream() << "case ";
3981 ast_dump_context
->dump_type(this->type_
);
3982 ast_dump_context
->ostream() << ":" ;
3984 ast_dump_context
->dump_block(this->statements_
);
3985 if (this->is_fallthrough_
)
3987 ast_dump_context
->print_indent();
3988 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
3992 // Class Type_case_clauses.
3997 Type_case_clauses::traverse(Traverse
* traverse
)
3999 for (Type_clauses::iterator p
= this->clauses_
.begin();
4000 p
!= this->clauses_
.end();
4003 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4004 return TRAVERSE_EXIT
;
4006 return TRAVERSE_CONTINUE
;
4009 // Check for duplicate types.
4012 Type_case_clauses::check_duplicates() const
4014 typedef Unordered_set_hash(const Type
*, Type_hash_identical
,
4015 Type_identical
) Types_seen
;
4016 Types_seen types_seen
;
4017 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4018 p
!= this->clauses_
.end();
4021 Type
* t
= p
->type();
4024 if (t
->is_nil_constant_as_type())
4025 t
= Type::make_nil_type();
4026 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
.insert(t
);
4028 error_at(p
->location(), "duplicate type in switch");
4032 // Lower the clauses in a type switch. Add statements to the block B.
4033 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4034 // BREAK_LABEL is the label at the end of the type switch.
4037 Type_case_clauses::lower(Block
* b
, Temporary_statement
* descriptor_temp
,
4038 Unnamed_label
* break_label
) const
4040 const Type_case_clause
* default_case
= NULL
;
4042 Unnamed_label
* stmts_label
= NULL
;
4043 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4044 p
!= this->clauses_
.end();
4047 if (!p
->is_default())
4048 p
->lower(b
, descriptor_temp
, break_label
, &stmts_label
);
4051 // We are generating a series of tests, which means that we
4052 // need to move the default case to the end.
4056 go_assert(stmts_label
== NULL
);
4058 if (default_case
!= NULL
)
4059 default_case
->lower(b
, descriptor_temp
, break_label
, NULL
);
4062 // Dump the AST representation for case clauses (from a switch statement)
4065 Type_case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4067 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4068 p
!= this->clauses_
.end();
4070 p
->dump_clause(ast_dump_context
);
4073 // Class Type_switch_statement.
4078 Type_switch_statement::do_traverse(Traverse
* traverse
)
4080 if (this->var_
== NULL
)
4082 if (this->traverse_expression(traverse
, &this->expr_
) == TRAVERSE_EXIT
)
4083 return TRAVERSE_EXIT
;
4085 if (this->clauses_
!= NULL
)
4086 return this->clauses_
->traverse(traverse
);
4087 return TRAVERSE_CONTINUE
;
4090 // Lower a type switch statement to a series of if statements. The gc
4091 // compiler is able to generate a table in some cases. However, that
4092 // does not work for us because we may have type descriptors in
4093 // different shared libraries, so we can't compare them with simple
4094 // equality testing.
4097 Type_switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
4098 Statement_inserter
*)
4100 const source_location loc
= this->location();
4102 if (this->clauses_
!= NULL
)
4103 this->clauses_
->check_duplicates();
4105 Block
* b
= new Block(enclosing
, loc
);
4107 Type
* val_type
= (this->var_
!= NULL
4108 ? this->var_
->var_value()->type()
4109 : this->expr_
->type());
4111 // var descriptor_temp DESCRIPTOR_TYPE
4112 Type
* descriptor_type
= Type::make_type_descriptor_ptr_type();
4113 Temporary_statement
* descriptor_temp
=
4114 Statement::make_temporary(descriptor_type
, NULL
, loc
);
4115 b
->add_statement(descriptor_temp
);
4117 if (val_type
->interface_type() == NULL
)
4119 // Doing a type switch on a non-interface type. Should we issue
4120 // a warning for this case?
4121 Expression
* lhs
= Expression::make_temporary_reference(descriptor_temp
,
4124 if (val_type
->is_nil_type())
4125 rhs
= Expression::make_nil(loc
);
4128 if (val_type
->is_abstract())
4129 val_type
= val_type
->make_non_abstract_type();
4130 rhs
= Expression::make_type_descriptor(val_type
, loc
);
4132 Statement
* s
= Statement::make_assignment(lhs
, rhs
, loc
);
4133 b
->add_statement(s
);
4137 // descriptor_temp = ifacetype(val_temp)
4138 // FIXME: This should be inlined.
4139 bool is_empty
= val_type
->interface_type()->is_empty();
4141 if (this->var_
== NULL
)
4144 ref
= Expression::make_var_reference(this->var_
, loc
);
4145 Expression
* call
= Runtime::make_call((is_empty
4146 ? Runtime::EFACETYPE
4147 : Runtime::IFACETYPE
),
4149 Temporary_reference_expression
* lhs
=
4150 Expression::make_temporary_reference(descriptor_temp
, loc
);
4151 lhs
->set_is_lvalue();
4152 Statement
* s
= Statement::make_assignment(lhs
, call
, loc
);
4153 b
->add_statement(s
);
4156 if (this->clauses_
!= NULL
)
4157 this->clauses_
->lower(b
, descriptor_temp
, this->break_label());
4159 Statement
* s
= Statement::make_unnamed_label_statement(this->break_label_
);
4160 b
->add_statement(s
);
4162 return Statement::make_block_statement(b
, loc
);
4165 // Return the break label for this type switch statement, creating it
4169 Type_switch_statement::break_label()
4171 if (this->break_label_
== NULL
)
4172 this->break_label_
= new Unnamed_label(this->location());
4173 return this->break_label_
;
4176 // Dump the AST representation for a type switch statement
4179 Type_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
4182 ast_dump_context
->print_indent();
4183 ast_dump_context
->ostream() << "switch " << this->var_
->name() << " = ";
4184 ast_dump_context
->dump_expression(this->expr_
);
4185 ast_dump_context
->ostream() << " .(type)";
4186 if (ast_dump_context
->dump_subblocks())
4188 ast_dump_context
->ostream() << " {" << std::endl
;
4189 this->clauses_
->dump_clauses(ast_dump_context
);
4190 ast_dump_context
->ostream() << "}";
4192 ast_dump_context
->ostream() << std::endl
;
4195 // Make a type switch statement.
4197 Type_switch_statement
*
4198 Statement::make_type_switch_statement(Named_object
* var
, Expression
* expr
,
4199 source_location location
)
4201 return new Type_switch_statement(var
, expr
, location
);
4204 // Class Send_statement.
4209 Send_statement::do_traverse(Traverse
* traverse
)
4211 if (this->traverse_expression(traverse
, &this->channel_
) == TRAVERSE_EXIT
)
4212 return TRAVERSE_EXIT
;
4213 return this->traverse_expression(traverse
, &this->val_
);
4219 Send_statement::do_determine_types()
4221 this->channel_
->determine_type_no_context();
4222 Type
* type
= this->channel_
->type();
4223 Type_context context
;
4224 if (type
->channel_type() != NULL
)
4225 context
.type
= type
->channel_type()->element_type();
4226 this->val_
->determine_type(&context
);
4232 Send_statement::do_check_types(Gogo
*)
4234 Type
* type
= this->channel_
->type();
4235 if (type
->is_error())
4237 this->set_is_error();
4240 Channel_type
* channel_type
= type
->channel_type();
4241 if (channel_type
== NULL
)
4243 error_at(this->location(), "left operand of %<<-%> must be channel");
4244 this->set_is_error();
4247 Type
* element_type
= channel_type
->element_type();
4248 if (!Type::are_assignable(element_type
, this->val_
->type(), NULL
))
4250 this->report_error(_("incompatible types in send"));
4253 if (!channel_type
->may_send())
4255 this->report_error(_("invalid send on receive-only channel"));
4260 // Convert a send statement to the backend representation.
4263 Send_statement::do_get_backend(Translate_context
* context
)
4265 source_location loc
= this->location();
4267 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
4268 Type
* element_type
= channel_type
->element_type();
4269 Expression
* val
= Expression::make_cast(element_type
, this->val_
, loc
);
4272 bool can_take_address
;
4273 switch (element_type
->base()->classification())
4275 case Type::TYPE_BOOLEAN
:
4276 case Type::TYPE_INTEGER
:
4277 case Type::TYPE_FUNCTION
:
4278 case Type::TYPE_POINTER
:
4279 case Type::TYPE_MAP
:
4280 case Type::TYPE_CHANNEL
:
4282 can_take_address
= false;
4285 case Type::TYPE_FLOAT
:
4286 case Type::TYPE_COMPLEX
:
4287 case Type::TYPE_STRING
:
4288 case Type::TYPE_INTERFACE
:
4290 can_take_address
= false;
4293 case Type::TYPE_STRUCT
:
4295 can_take_address
= true;
4298 case Type::TYPE_ARRAY
:
4300 can_take_address
= !element_type
->is_open_array_type();
4304 case Type::TYPE_ERROR
:
4305 case Type::TYPE_VOID
:
4306 case Type::TYPE_SINK
:
4307 case Type::TYPE_NIL
:
4308 case Type::TYPE_NAMED
:
4309 case Type::TYPE_FORWARD
:
4310 go_assert(saw_errors());
4311 return context
->backend()->error_statement();
4314 // Only try to take the address of a variable. We have already
4315 // moved variables to the heap, so this should not cause that to
4316 // happen unnecessarily.
4317 if (can_take_address
4318 && val
->var_expression() == NULL
4319 && val
->temporary_reference_expression() == NULL
)
4320 can_take_address
= false;
4322 Runtime::Function code
;
4323 Bstatement
* btemp
= NULL
;
4327 // Type is small enough to handle as uint64.
4328 code
= Runtime::SEND_SMALL
;
4329 val
= Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
4332 else if (can_take_address
)
4334 // Must pass address of value. The function doesn't change the
4335 // value, so just take its address directly.
4336 code
= Runtime::SEND_BIG
;
4337 val
= Expression::make_unary(OPERATOR_AND
, val
, loc
);
4341 // Must pass address of value, but the value is small enough
4342 // that it might be in registers. Copy value into temporary
4343 // variable to take address.
4344 code
= Runtime::SEND_BIG
;
4345 Temporary_statement
* temp
= Statement::make_temporary(element_type
,
4347 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
4348 val
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
4349 btemp
= temp
->get_backend(context
);
4352 call
= Runtime::make_call(code
, loc
, 3, this->channel_
, val
,
4353 Expression::make_boolean(this->for_select_
, loc
));
4355 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4356 Bexpression
* bcall
= tree_to_expr(call
->get_tree(context
));
4357 Bstatement
* s
= context
->backend()->expression_statement(bcall
);
4362 return context
->backend()->compound_statement(btemp
, s
);
4365 // Dump the AST representation for a send statement
4368 Send_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4370 ast_dump_context
->print_indent();
4371 ast_dump_context
->dump_expression(this->channel_
);
4372 ast_dump_context
->ostream() << " <- ";
4373 ast_dump_context
->dump_expression(this->val_
);
4374 ast_dump_context
->ostream() << std::endl
;
4377 // Make a send statement.
4380 Statement::make_send_statement(Expression
* channel
, Expression
* val
,
4381 source_location location
)
4383 return new Send_statement(channel
, val
, location
);
4386 // Class Select_clauses::Select_clause.
4391 Select_clauses::Select_clause::traverse(Traverse
* traverse
)
4393 if (!this->is_lowered_
4394 && (traverse
->traverse_mask()
4395 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
4397 if (this->channel_
!= NULL
)
4399 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
4400 return TRAVERSE_EXIT
;
4402 if (this->val_
!= NULL
)
4404 if (Expression::traverse(&this->val_
, traverse
) == TRAVERSE_EXIT
)
4405 return TRAVERSE_EXIT
;
4407 if (this->closed_
!= NULL
)
4409 if (Expression::traverse(&this->closed_
, traverse
) == TRAVERSE_EXIT
)
4410 return TRAVERSE_EXIT
;
4413 if (this->statements_
!= NULL
)
4415 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
4416 return TRAVERSE_EXIT
;
4418 return TRAVERSE_CONTINUE
;
4421 // Lowering. Here we pull out the channel and the send values, to
4422 // enforce the order of evaluation. We also add explicit send and
4423 // receive statements to the clauses.
4426 Select_clauses::Select_clause::lower(Gogo
* gogo
, Named_object
* function
,
4429 if (this->is_default_
)
4431 go_assert(this->channel_
== NULL
&& this->val_
== NULL
);
4432 this->is_lowered_
= true;
4436 source_location loc
= this->location_
;
4438 // Evaluate the channel before the select statement.
4439 Temporary_statement
* channel_temp
= Statement::make_temporary(NULL
,
4442 b
->add_statement(channel_temp
);
4443 this->channel_
= Expression::make_temporary_reference(channel_temp
, loc
);
4445 // If this is a send clause, evaluate the value to send before the
4446 // select statement.
4447 Temporary_statement
* val_temp
= NULL
;
4448 if (this->is_send_
&& !this->val_
->is_constant())
4450 val_temp
= Statement::make_temporary(NULL
, this->val_
, loc
);
4451 b
->add_statement(val_temp
);
4454 // Add the send or receive before the rest of the statements if any.
4455 Block
*init
= new Block(b
, loc
);
4456 Expression
* ref
= Expression::make_temporary_reference(channel_temp
, loc
);
4460 if (val_temp
== NULL
)
4463 ref2
= Expression::make_temporary_reference(val_temp
, loc
);
4464 Send_statement
* send
= Statement::make_send_statement(ref
, ref2
, loc
);
4465 send
->set_for_select();
4466 init
->add_statement(send
);
4468 else if (this->closed_
!= NULL
&& !this->closed_
->is_sink_expression())
4470 go_assert(this->var_
== NULL
&& this->closedvar_
== NULL
);
4471 if (this->val_
== NULL
)
4472 this->val_
= Expression::make_sink(loc
);
4473 Statement
* s
= Statement::make_tuple_receive_assignment(this->val_
,
4476 init
->add_statement(s
);
4478 else if (this->closedvar_
!= NULL
)
4480 go_assert(this->val_
== NULL
);
4482 if (this->var_
== NULL
)
4483 val
= Expression::make_sink(loc
);
4485 val
= Expression::make_var_reference(this->var_
, loc
);
4486 Expression
* closed
= Expression::make_var_reference(this->closedvar_
,
4488 Statement
* s
= Statement::make_tuple_receive_assignment(val
, closed
, ref
,
4490 // We have to put S in STATEMENTS_, because that is where the
4491 // variables are declared.
4492 go_assert(this->statements_
!= NULL
);
4493 this->statements_
->add_statement_at_front(s
);
4494 // We have to lower STATEMENTS_ again, to lower the tuple
4495 // receive assignment we just added.
4496 gogo
->lower_block(function
, this->statements_
);
4500 Receive_expression
* recv
= Expression::make_receive(ref
, loc
);
4501 recv
->set_for_select();
4502 if (this->val_
!= NULL
)
4504 go_assert(this->var_
== NULL
);
4505 init
->add_statement(Statement::make_assignment(this->val_
, recv
,
4508 else if (this->var_
!= NULL
)
4510 this->var_
->var_value()->set_init(recv
);
4511 this->var_
->var_value()->clear_type_from_chan_element();
4515 init
->add_statement(Statement::make_statement(recv
));
4519 // Lower any statements we just created.
4520 gogo
->lower_block(function
, init
);
4522 if (this->statements_
!= NULL
)
4523 init
->add_statement(Statement::make_block_statement(this->statements_
,
4526 this->statements_
= init
;
4528 // Now all references should be handled through the statements, not
4530 this->is_lowered_
= true;
4538 Select_clauses::Select_clause::determine_types()
4540 go_assert(this->is_lowered_
);
4541 if (this->statements_
!= NULL
)
4542 this->statements_
->determine_types();
4545 // Whether this clause may fall through to the statement which follows
4546 // the overall select statement.
4549 Select_clauses::Select_clause::may_fall_through() const
4551 if (this->statements_
== NULL
)
4553 return this->statements_
->may_fall_through();
4556 // Return the backend representation for the statements to execute.
4559 Select_clauses::Select_clause::get_statements_backend(
4560 Translate_context
* context
)
4562 if (this->statements_
== NULL
)
4564 Bblock
* bblock
= this->statements_
->get_backend(context
);
4565 return context
->backend()->block_statement(bblock
);
4568 // Dump the AST representation for a select case clause
4571 Select_clauses::Select_clause::dump_clause(
4572 Ast_dump_context
* ast_dump_context
) const
4574 ast_dump_context
->print_indent();
4575 if (this->is_default_
)
4577 ast_dump_context
->ostream() << "default:";
4581 ast_dump_context
->ostream() << "case " ;
4584 ast_dump_context
->dump_expression(this->channel_
);
4585 ast_dump_context
->ostream() << " <- " ;
4586 ast_dump_context
->dump_expression(this->val_
);
4590 if (this->val_
!= NULL
)
4591 ast_dump_context
->dump_expression(this->val_
);
4592 if (this->closed_
!= NULL
)
4594 // FIXME: can val_ == NULL and closed_ ! = NULL?
4595 ast_dump_context
->ostream() << " , " ;
4596 ast_dump_context
->dump_expression(this->closed_
);
4598 if (this->closedvar_
!= NULL
||
4600 ast_dump_context
->ostream() << " := " ;
4602 ast_dump_context
->ostream() << " <- " ;
4603 ast_dump_context
->dump_expression(this->channel_
);
4605 ast_dump_context
->ostream() << ":" ;
4607 ast_dump_context
->dump_block(this->statements_
);
4610 // Class Select_clauses.
4615 Select_clauses::traverse(Traverse
* traverse
)
4617 for (Clauses::iterator p
= this->clauses_
.begin();
4618 p
!= this->clauses_
.end();
4621 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4622 return TRAVERSE_EXIT
;
4624 return TRAVERSE_CONTINUE
;
4627 // Lowering. Here we pull out the channel and the send values, to
4628 // enforce the order of evaluation. We also add explicit send and
4629 // receive statements to the clauses.
4632 Select_clauses::lower(Gogo
* gogo
, Named_object
* function
, Block
* b
)
4634 for (Clauses::iterator p
= this->clauses_
.begin();
4635 p
!= this->clauses_
.end();
4637 p
->lower(gogo
, function
, b
);
4643 Select_clauses::determine_types()
4645 for (Clauses::iterator p
= this->clauses_
.begin();
4646 p
!= this->clauses_
.end();
4648 p
->determine_types();
4651 // Return whether these select clauses fall through to the statement
4652 // following the overall select statement.
4655 Select_clauses::may_fall_through() const
4657 for (Clauses::const_iterator p
= this->clauses_
.begin();
4658 p
!= this->clauses_
.end();
4660 if (p
->may_fall_through())
4665 // Convert to the backend representation. We build a call to
4666 // size_t __go_select(size_t count, _Bool has_default,
4667 // channel* channels, _Bool* is_send)
4669 // There are COUNT entries in the CHANNELS and IS_SEND arrays. The
4670 // value in the IS_SEND array is true for send, false for receive.
4671 // __go_select returns an integer from 0 to COUNT, inclusive. A
4672 // return of 0 means that the default case should be run; this only
4673 // happens if HAS_DEFAULT is non-zero. Otherwise the number indicates
4676 // FIXME: This doesn't handle channels which send interface types
4677 // where the receiver has a static type which matches that interface.
4680 Select_clauses::get_backend(Translate_context
* context
,
4681 Unnamed_label
*break_label
,
4682 source_location location
)
4684 size_t count
= this->clauses_
.size();
4686 Expression_list
* chan_init
= new Expression_list();
4687 chan_init
->reserve(count
);
4689 Expression_list
* is_send_init
= new Expression_list();
4690 is_send_init
->reserve(count
);
4692 Select_clause
*default_clause
= NULL
;
4694 Type
* runtime_chanptr_type
= Runtime::chanptr_type();
4695 Type
* runtime_chan_type
= runtime_chanptr_type
->points_to();
4697 for (Clauses::iterator p
= this->clauses_
.begin();
4698 p
!= this->clauses_
.end();
4701 if (p
->is_default())
4703 default_clause
= &*p
;
4708 if (p
->channel()->type()->channel_type() == NULL
)
4710 // We should have given an error in the send or receive
4711 // statement we created via lowering.
4712 go_assert(saw_errors());
4713 return context
->backend()->error_statement();
4716 Expression
* c
= p
->channel();
4717 c
= Expression::make_unsafe_cast(runtime_chan_type
, c
, p
->location());
4718 chan_init
->push_back(c
);
4720 is_send_init
->push_back(Expression::make_boolean(p
->is_send(),
4724 if (chan_init
->empty())
4726 go_assert(count
== 0);
4728 Bstatement
* ldef
= break_label
->get_definition(context
);
4729 if (default_clause
!= NULL
)
4731 // There is a default clause and no cases. Just execute the
4733 s
= default_clause
->get_statements_backend(context
);
4737 // There isn't even a default clause. In this case select
4738 // pauses forever. Call the runtime function with nils.
4740 mpz_init_set_ui(zval
, 0);
4741 Expression
* zero
= Expression::make_integer(&zval
, NULL
, location
);
4743 Expression
* default_arg
= Expression::make_boolean(false, location
);
4744 Expression
* nil1
= Expression::make_nil(location
);
4745 Expression
* nil2
= nil1
->copy();
4746 Expression
* call
= Runtime::make_call(Runtime::SELECT
, location
, 4,
4747 zero
, default_arg
, nil1
, nil2
);
4748 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4749 Bexpression
* bcall
= tree_to_expr(call
->get_tree(context
));
4750 s
= context
->backend()->expression_statement(bcall
);
4754 return context
->backend()->compound_statement(s
, ldef
);
4756 go_assert(count
> 0);
4758 std::vector
<Bstatement
*> statements
;
4761 mpz_init_set_ui(ival
, count
);
4762 Expression
* ecount
= Expression::make_integer(&ival
, NULL
, location
);
4765 Type
* chan_array_type
= Type::make_array_type(runtime_chan_type
, ecount
);
4766 Expression
* chans
= Expression::make_composite_literal(chan_array_type
, 0,
4769 context
->gogo()->lower_expression(context
->function(), NULL
, &chans
);
4770 Temporary_statement
* chan_temp
= Statement::make_temporary(chan_array_type
,
4773 statements
.push_back(chan_temp
->get_backend(context
));
4775 Type
* is_send_array_type
= Type::make_array_type(Type::lookup_bool_type(),
4777 Expression
* is_sends
= Expression::make_composite_literal(is_send_array_type
,
4781 context
->gogo()->lower_expression(context
->function(), NULL
, &is_sends
);
4782 Temporary_statement
* is_send_temp
=
4783 Statement::make_temporary(is_send_array_type
, is_sends
, location
);
4784 statements
.push_back(is_send_temp
->get_backend(context
));
4786 mpz_init_set_ui(ival
, 0);
4787 Expression
* zero
= Expression::make_integer(&ival
, NULL
, location
);
4790 Expression
* ref
= Expression::make_temporary_reference(chan_temp
, location
);
4791 Expression
* chan_arg
= Expression::make_array_index(ref
, zero
, NULL
,
4793 chan_arg
= Expression::make_unary(OPERATOR_AND
, chan_arg
, location
);
4794 chan_arg
= Expression::make_unsafe_cast(runtime_chanptr_type
, chan_arg
,
4797 ref
= Expression::make_temporary_reference(is_send_temp
, location
);
4798 Expression
* is_send_arg
= Expression::make_array_index(ref
, zero
->copy(),
4800 is_send_arg
= Expression::make_unary(OPERATOR_AND
, is_send_arg
, location
);
4802 Expression
* default_arg
= Expression::make_boolean(default_clause
!= NULL
,
4804 Expression
* call
= Runtime::make_call(Runtime::SELECT
, location
, 4,
4805 ecount
->copy(), default_arg
,
4806 chan_arg
, is_send_arg
);
4807 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4808 Bexpression
* bcall
= tree_to_expr(call
->get_tree(context
));
4810 std::vector
<std::vector
<Bexpression
*> > cases
;
4811 std::vector
<Bstatement
*> clauses
;
4813 cases
.resize(count
+ (default_clause
!= NULL
? 1 : 0));
4814 clauses
.resize(count
+ (default_clause
!= NULL
? 1 : 0));
4818 if (default_clause
!= NULL
)
4820 this->add_clause_backend(context
, location
, index
, 0, default_clause
,
4821 break_label
, &cases
, &clauses
);
4826 for (Clauses::iterator p
= this->clauses_
.begin();
4827 p
!= this->clauses_
.end();
4830 if (!p
->is_default())
4832 this->add_clause_backend(context
, location
, index
, i
, &*p
,
4833 break_label
, &cases
, &clauses
);
4839 Bstatement
* switch_stmt
= context
->backend()->switch_statement(bcall
,
4843 statements
.push_back(switch_stmt
);
4845 Bstatement
* ldef
= break_label
->get_definition(context
);
4846 statements
.push_back(ldef
);
4848 return context
->backend()->statement_list(statements
);
4851 // Add CLAUSE to CASES/CLAUSES at INDEX.
4854 Select_clauses::add_clause_backend(
4855 Translate_context
* context
,
4856 source_location location
,
4859 Select_clause
* clause
,
4860 Unnamed_label
* bottom_label
,
4861 std::vector
<std::vector
<Bexpression
*> > *cases
,
4862 std::vector
<Bstatement
*>* clauses
)
4865 mpz_init_set_ui(ival
, case_value
);
4866 Expression
* e
= Expression::make_integer(&ival
, NULL
, location
);
4868 (*cases
)[index
].push_back(tree_to_expr(e
->get_tree(context
)));
4870 Bstatement
* s
= clause
->get_statements_backend(context
);
4872 source_location gloc
= (clause
->statements() == NULL
4873 ? clause
->location()
4874 : clause
->statements()->end_location());
4875 Bstatement
* g
= bottom_label
->get_goto(context
, gloc
);
4878 (*clauses
)[index
] = g
;
4880 (*clauses
)[index
] = context
->backend()->compound_statement(s
, g
);
4883 // Dump the AST representation for select clauses.
4886 Select_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4888 for (Clauses::const_iterator p
= this->clauses_
.begin();
4889 p
!= this->clauses_
.end();
4891 p
->dump_clause(ast_dump_context
);
4894 // Class Select_statement.
4896 // Return the break label for this switch statement, creating it if
4900 Select_statement::break_label()
4902 if (this->break_label_
== NULL
)
4903 this->break_label_
= new Unnamed_label(this->location());
4904 return this->break_label_
;
4907 // Lower a select statement. This will still return a select
4908 // statement, but it will be modified to implement the order of
4909 // evaluation rules, and to include the send and receive statements as
4910 // explicit statements in the clauses.
4913 Select_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
4914 Block
* enclosing
, Statement_inserter
*)
4916 if (this->is_lowered_
)
4918 Block
* b
= new Block(enclosing
, this->location());
4919 this->clauses_
->lower(gogo
, function
, b
);
4920 this->is_lowered_
= true;
4921 b
->add_statement(this);
4922 return Statement::make_block_statement(b
, this->location());
4925 // Return the backend representation for a select statement.
4928 Select_statement::do_get_backend(Translate_context
* context
)
4930 return this->clauses_
->get_backend(context
, this->break_label(),
4934 // Dump the AST representation for a select statement.
4937 Select_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4939 ast_dump_context
->print_indent();
4940 ast_dump_context
->ostream() << "select";
4941 if (ast_dump_context
->dump_subblocks())
4943 ast_dump_context
->ostream() << " {" << std::endl
;
4944 this->clauses_
->dump_clauses(ast_dump_context
);
4945 ast_dump_context
->ostream() << "}";
4947 ast_dump_context
->ostream() << std::endl
;
4950 // Make a select statement.
4953 Statement::make_select_statement(source_location location
)
4955 return new Select_statement(location
);
4958 // Class For_statement.
4963 For_statement::do_traverse(Traverse
* traverse
)
4965 if (this->init_
!= NULL
)
4967 if (this->init_
->traverse(traverse
) == TRAVERSE_EXIT
)
4968 return TRAVERSE_EXIT
;
4970 if (this->cond_
!= NULL
)
4972 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
)
4973 return TRAVERSE_EXIT
;
4975 if (this->post_
!= NULL
)
4977 if (this->post_
->traverse(traverse
) == TRAVERSE_EXIT
)
4978 return TRAVERSE_EXIT
;
4980 return this->statements_
->traverse(traverse
);
4983 // Lower a For_statement into if statements and gotos. Getting rid of
4984 // complex statements make it easier to handle garbage collection.
4987 For_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
4988 Statement_inserter
*)
4991 source_location loc
= this->location();
4993 Block
* b
= new Block(enclosing
, this->location());
4994 if (this->init_
!= NULL
)
4996 s
= Statement::make_block_statement(this->init_
,
4997 this->init_
->start_location());
4998 b
->add_statement(s
);
5001 Unnamed_label
* entry
= NULL
;
5002 if (this->cond_
!= NULL
)
5004 entry
= new Unnamed_label(this->location());
5005 b
->add_statement(Statement::make_goto_unnamed_statement(entry
, loc
));
5008 Unnamed_label
* top
= new Unnamed_label(this->location());
5009 b
->add_statement(Statement::make_unnamed_label_statement(top
));
5011 s
= Statement::make_block_statement(this->statements_
,
5012 this->statements_
->start_location());
5013 b
->add_statement(s
);
5015 source_location end_loc
= this->statements_
->end_location();
5017 Unnamed_label
* cont
= this->continue_label_
;
5019 b
->add_statement(Statement::make_unnamed_label_statement(cont
));
5021 if (this->post_
!= NULL
)
5023 s
= Statement::make_block_statement(this->post_
,
5024 this->post_
->start_location());
5025 b
->add_statement(s
);
5026 end_loc
= this->post_
->end_location();
5029 if (this->cond_
== NULL
)
5030 b
->add_statement(Statement::make_goto_unnamed_statement(top
, end_loc
));
5033 b
->add_statement(Statement::make_unnamed_label_statement(entry
));
5035 source_location cond_loc
= this->cond_
->location();
5036 Block
* then_block
= new Block(b
, cond_loc
);
5037 s
= Statement::make_goto_unnamed_statement(top
, cond_loc
);
5038 then_block
->add_statement(s
);
5040 s
= Statement::make_if_statement(this->cond_
, then_block
, NULL
, cond_loc
);
5041 b
->add_statement(s
);
5044 Unnamed_label
* brk
= this->break_label_
;
5046 b
->add_statement(Statement::make_unnamed_label_statement(brk
));
5048 b
->set_end_location(end_loc
);
5050 return Statement::make_block_statement(b
, loc
);
5053 // Return the break label, creating it if necessary.
5056 For_statement::break_label()
5058 if (this->break_label_
== NULL
)
5059 this->break_label_
= new Unnamed_label(this->location());
5060 return this->break_label_
;
5063 // Return the continue LABEL_EXPR.
5066 For_statement::continue_label()
5068 if (this->continue_label_
== NULL
)
5069 this->continue_label_
= new Unnamed_label(this->location());
5070 return this->continue_label_
;
5073 // Set the break and continue labels a for statement. This is used
5074 // when lowering a for range statement.
5077 For_statement::set_break_continue_labels(Unnamed_label
* break_label
,
5078 Unnamed_label
* continue_label
)
5080 go_assert(this->break_label_
== NULL
&& this->continue_label_
== NULL
);
5081 this->break_label_
= break_label
;
5082 this->continue_label_
= continue_label
;
5085 // Dump the AST representation for a for statement.
5088 For_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5090 if (this->init_
!= NULL
&& ast_dump_context
->dump_subblocks())
5092 ast_dump_context
->print_indent();
5093 ast_dump_context
->indent();
5094 ast_dump_context
->ostream() << "// INIT " << std::endl
;
5095 ast_dump_context
->dump_block(this->init_
);
5096 ast_dump_context
->unindent();
5098 ast_dump_context
->print_indent();
5099 ast_dump_context
->ostream() << "for ";
5100 if (this->cond_
!= NULL
)
5101 ast_dump_context
->dump_expression(this->cond_
);
5103 if (ast_dump_context
->dump_subblocks())
5105 ast_dump_context
->ostream() << " {" << std::endl
;
5106 ast_dump_context
->dump_block(this->statements_
);
5107 if (this->init_
!= NULL
)
5109 ast_dump_context
->print_indent();
5110 ast_dump_context
->ostream() << "// POST " << std::endl
;
5111 ast_dump_context
->dump_block(this->post_
);
5113 ast_dump_context
->unindent();
5115 ast_dump_context
->print_indent();
5116 ast_dump_context
->ostream() << "}";
5119 ast_dump_context
->ostream() << std::endl
;
5122 // Make a for statement.
5125 Statement::make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
5126 source_location location
)
5128 return new For_statement(init
, cond
, post
, location
);
5131 // Class For_range_statement.
5136 For_range_statement::do_traverse(Traverse
* traverse
)
5138 if (this->traverse_expression(traverse
, &this->index_var_
) == TRAVERSE_EXIT
)
5139 return TRAVERSE_EXIT
;
5140 if (this->value_var_
!= NULL
)
5142 if (this->traverse_expression(traverse
, &this->value_var_
)
5144 return TRAVERSE_EXIT
;
5146 if (this->traverse_expression(traverse
, &this->range_
) == TRAVERSE_EXIT
)
5147 return TRAVERSE_EXIT
;
5148 return this->statements_
->traverse(traverse
);
5151 // Lower a for range statement. For simplicity we lower this into a
5152 // for statement, which will then be lowered in turn to goto
5156 For_range_statement::do_lower(Gogo
* gogo
, Named_object
*, Block
* enclosing
,
5157 Statement_inserter
*)
5159 Type
* range_type
= this->range_
->type();
5160 if (range_type
->points_to() != NULL
5161 && range_type
->points_to()->array_type() != NULL
5162 && !range_type
->points_to()->is_open_array_type())
5163 range_type
= range_type
->points_to();
5166 Type
* value_type
= NULL
;
5167 if (range_type
->array_type() != NULL
)
5169 index_type
= Type::lookup_integer_type("int");
5170 value_type
= range_type
->array_type()->element_type();
5172 else if (range_type
->is_string_type())
5174 index_type
= Type::lookup_integer_type("int");
5175 value_type
= index_type
;
5177 else if (range_type
->map_type() != NULL
)
5179 index_type
= range_type
->map_type()->key_type();
5180 value_type
= range_type
->map_type()->val_type();
5182 else if (range_type
->channel_type() != NULL
)
5184 index_type
= range_type
->channel_type()->element_type();
5185 if (this->value_var_
!= NULL
)
5187 if (!this->value_var_
->type()->is_error())
5188 this->report_error(_("too many variables for range clause "
5190 return Statement::make_error_statement(this->location());
5195 this->report_error(_("range clause must have "
5196 "array, slice, string, map, or channel type"));
5197 return Statement::make_error_statement(this->location());
5200 source_location loc
= this->location();
5201 Block
* temp_block
= new Block(enclosing
, loc
);
5203 Named_object
* range_object
= NULL
;
5204 Temporary_statement
* range_temp
= NULL
;
5205 Var_expression
* ve
= this->range_
->var_expression();
5207 range_object
= ve
->named_object();
5210 range_temp
= Statement::make_temporary(NULL
, this->range_
, loc
);
5211 temp_block
->add_statement(range_temp
);
5212 this->range_
= NULL
;
5215 Temporary_statement
* index_temp
= Statement::make_temporary(index_type
,
5217 temp_block
->add_statement(index_temp
);
5219 Temporary_statement
* value_temp
= NULL
;
5220 if (this->value_var_
!= NULL
)
5222 value_temp
= Statement::make_temporary(value_type
, NULL
, loc
);
5223 temp_block
->add_statement(value_temp
);
5226 Block
* body
= new Block(temp_block
, loc
);
5233 // Arrange to do a loop appropriate for the type. We will produce
5234 // for INIT ; COND ; POST {
5236 // INDEX = INDEX_TEMP
5237 // VALUE = VALUE_TEMP // If there is a value
5238 // original statements
5241 if (range_type
->array_type() != NULL
)
5242 this->lower_range_array(gogo
, temp_block
, body
, range_object
, range_temp
,
5243 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5245 else if (range_type
->is_string_type())
5246 this->lower_range_string(gogo
, temp_block
, body
, range_object
, range_temp
,
5247 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5249 else if (range_type
->map_type() != NULL
)
5250 this->lower_range_map(gogo
, temp_block
, body
, range_object
, range_temp
,
5251 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5253 else if (range_type
->channel_type() != NULL
)
5254 this->lower_range_channel(gogo
, temp_block
, body
, range_object
, range_temp
,
5255 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5260 if (iter_init
!= NULL
)
5261 body
->add_statement(Statement::make_block_statement(iter_init
, loc
));
5264 Expression
* index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5265 if (this->value_var_
== NULL
)
5267 assign
= Statement::make_assignment(this->index_var_
, index_ref
, loc
);
5271 Expression_list
* lhs
= new Expression_list();
5272 lhs
->push_back(this->index_var_
);
5273 lhs
->push_back(this->value_var_
);
5275 Expression_list
* rhs
= new Expression_list();
5276 rhs
->push_back(index_ref
);
5277 rhs
->push_back(Expression::make_temporary_reference(value_temp
, loc
));
5279 assign
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5281 body
->add_statement(assign
);
5283 body
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
5285 body
->set_end_location(this->statements_
->end_location());
5287 For_statement
* loop
= Statement::make_for_statement(init
, cond
, post
,
5289 loop
->add_statements(body
);
5290 loop
->set_break_continue_labels(this->break_label_
, this->continue_label_
);
5292 temp_block
->add_statement(loop
);
5294 return Statement::make_block_statement(temp_block
, loc
);
5297 // Return a reference to the range, which may be in RANGE_OBJECT or in
5301 For_range_statement::make_range_ref(Named_object
* range_object
,
5302 Temporary_statement
* range_temp
,
5303 source_location loc
)
5305 if (range_object
!= NULL
)
5306 return Expression::make_var_reference(range_object
, loc
);
5308 return Expression::make_temporary_reference(range_temp
, loc
);
5311 // Return a call to the predeclared function FUNCNAME passing a
5312 // reference to the temporary variable ARG.
5315 For_range_statement::call_builtin(Gogo
* gogo
, const char* funcname
,
5317 source_location loc
)
5319 Named_object
* no
= gogo
->lookup_global(funcname
);
5320 go_assert(no
!= NULL
&& no
->is_function_declaration());
5321 Expression
* func
= Expression::make_func_reference(no
, NULL
, loc
);
5322 Expression_list
* params
= new Expression_list();
5323 params
->push_back(arg
);
5324 return Expression::make_call(func
, params
, false, loc
);
5327 // Lower a for range over an array or slice.
5330 For_range_statement::lower_range_array(Gogo
* gogo
,
5333 Named_object
* range_object
,
5334 Temporary_statement
* range_temp
,
5335 Temporary_statement
* index_temp
,
5336 Temporary_statement
* value_temp
,
5342 source_location loc
= this->location();
5344 // The loop we generate:
5345 // len_temp := len(range)
5346 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5347 // value_temp = range[index_temp]
5348 // index = index_temp
5349 // value = value_temp
5355 // len_temp = len(range)
5358 Block
* init
= new Block(enclosing
, loc
);
5360 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5361 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5362 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5364 init
->add_statement(len_temp
);
5367 mpz_init_set_ui(zval
, 0UL);
5368 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5371 Temporary_reference_expression
* tref
=
5372 Expression::make_temporary_reference(index_temp
, loc
);
5373 tref
->set_is_lvalue();
5374 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5375 init
->add_statement(s
);
5380 // index_temp < len_temp
5382 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5383 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5384 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5388 // Set *PITER_INIT to
5389 // value_temp = range[index_temp]
5391 Block
* iter_init
= NULL
;
5392 if (value_temp
!= NULL
)
5394 iter_init
= new Block(body_block
, loc
);
5396 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5397 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5398 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, loc
);
5400 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5401 tref
->set_is_lvalue();
5402 s
= Statement::make_assignment(tref
, index
, loc
);
5404 iter_init
->add_statement(s
);
5406 *piter_init
= iter_init
;
5411 Block
* post
= new Block(enclosing
, loc
);
5412 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5413 tref
->set_is_lvalue();
5414 s
= Statement::make_inc_statement(tref
);
5415 post
->add_statement(s
);
5419 // Lower a for range over a string.
5422 For_range_statement::lower_range_string(Gogo
*,
5425 Named_object
* range_object
,
5426 Temporary_statement
* range_temp
,
5427 Temporary_statement
* index_temp
,
5428 Temporary_statement
* value_temp
,
5434 source_location loc
= this->location();
5436 // The loop we generate:
5437 // var next_index_temp int
5438 // for index_temp = 0; ; index_temp = next_index_temp {
5439 // next_index_temp, value_temp = stringiter2(range, index_temp)
5440 // if next_index_temp == 0 {
5443 // index = index_temp
5444 // value = value_temp
5449 // var next_index_temp int
5452 Block
* init
= new Block(enclosing
, loc
);
5454 Temporary_statement
* next_index_temp
=
5455 Statement::make_temporary(index_temp
->type(), NULL
, loc
);
5456 init
->add_statement(next_index_temp
);
5459 mpz_init_set_ui(zval
, 0UL);
5460 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5462 Temporary_reference_expression
* ref
=
5463 Expression::make_temporary_reference(index_temp
, loc
);
5464 ref
->set_is_lvalue();
5465 Statement
* s
= Statement::make_assignment(ref
, zexpr
, loc
);
5467 init
->add_statement(s
);
5470 // The loop has no condition.
5474 // Set *PITER_INIT to
5475 // next_index_temp = runtime.stringiter(range, index_temp)
5477 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
5479 // if next_index_temp == 0 {
5483 Block
* iter_init
= new Block(body_block
, loc
);
5485 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5486 Expression
* p2
= Expression::make_temporary_reference(index_temp
, loc
);
5487 Call_expression
* call
= Runtime::make_call((value_temp
== NULL
5488 ? Runtime::STRINGITER
5489 : Runtime::STRINGITER2
),
5492 if (value_temp
== NULL
)
5494 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5495 ref
->set_is_lvalue();
5496 s
= Statement::make_assignment(ref
, call
, loc
);
5500 Expression_list
* lhs
= new Expression_list();
5502 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5503 ref
->set_is_lvalue();
5504 lhs
->push_back(ref
);
5506 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5507 ref
->set_is_lvalue();
5508 lhs
->push_back(ref
);
5510 Expression_list
* rhs
= new Expression_list();
5511 rhs
->push_back(Expression::make_call_result(call
, 0));
5512 rhs
->push_back(Expression::make_call_result(call
, 1));
5514 s
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5516 iter_init
->add_statement(s
);
5518 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5519 zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5521 Expression
* equals
= Expression::make_binary(OPERATOR_EQEQ
, ref
, zexpr
, loc
);
5523 Block
* then_block
= new Block(iter_init
, loc
);
5524 s
= Statement::make_break_statement(this->break_label(), loc
);
5525 then_block
->add_statement(s
);
5527 s
= Statement::make_if_statement(equals
, then_block
, NULL
, loc
);
5528 iter_init
->add_statement(s
);
5530 *piter_init
= iter_init
;
5533 // index_temp = next_index_temp
5535 Block
* post
= new Block(enclosing
, loc
);
5537 Temporary_reference_expression
* lhs
=
5538 Expression::make_temporary_reference(index_temp
, loc
);
5539 lhs
->set_is_lvalue();
5540 Expression
* rhs
= Expression::make_temporary_reference(next_index_temp
, loc
);
5541 s
= Statement::make_assignment(lhs
, rhs
, loc
);
5543 post
->add_statement(s
);
5547 // Lower a for range over a map.
5550 For_range_statement::lower_range_map(Gogo
*,
5553 Named_object
* range_object
,
5554 Temporary_statement
* range_temp
,
5555 Temporary_statement
* index_temp
,
5556 Temporary_statement
* value_temp
,
5562 source_location loc
= this->location();
5564 // The runtime uses a struct to handle ranges over a map. The
5565 // struct is four pointers long. The first pointer is NULL when we
5566 // have completed the iteration.
5568 // The loop we generate:
5569 // var hiter map_iteration_struct
5570 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
5571 // mapiter2(hiter, &index_temp, &value_temp)
5572 // index = index_temp
5573 // value = value_temp
5578 // var hiter map_iteration_struct
5579 // runtime.mapiterinit(range, &hiter)
5581 Block
* init
= new Block(enclosing
, loc
);
5583 Type
* map_iteration_type
= Runtime::map_iteration_type();
5584 Temporary_statement
* hiter
= Statement::make_temporary(map_iteration_type
,
5586 init
->add_statement(hiter
);
5588 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5589 Expression
* ref
= Expression::make_temporary_reference(hiter
, loc
);
5590 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5591 Expression
* call
= Runtime::make_call(Runtime::MAPITERINIT
, loc
, 2, p1
, p2
);
5592 init
->add_statement(Statement::make_statement(call
));
5599 ref
= Expression::make_temporary_reference(hiter
, loc
);
5602 mpz_init_set_ui(zval
, 0UL);
5603 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5606 Expression
* index
= Expression::make_index(ref
, zexpr
, NULL
, loc
);
5608 Expression
* ne
= Expression::make_binary(OPERATOR_NOTEQ
, index
,
5609 Expression::make_nil(loc
),
5614 // Set *PITER_INIT to
5615 // mapiter1(hiter, &index_temp)
5617 // mapiter2(hiter, &index_temp, &value_temp)
5619 Block
* iter_init
= new Block(body_block
, loc
);
5621 ref
= Expression::make_temporary_reference(hiter
, loc
);
5622 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5623 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5624 p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5625 if (value_temp
== NULL
)
5626 call
= Runtime::make_call(Runtime::MAPITER1
, loc
, 2, p1
, p2
);
5629 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5630 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5631 call
= Runtime::make_call(Runtime::MAPITER2
, loc
, 3, p1
, p2
, p3
);
5633 iter_init
->add_statement(Statement::make_statement(call
));
5635 *piter_init
= iter_init
;
5638 // mapiternext(&hiter)
5640 Block
* post
= new Block(enclosing
, loc
);
5642 ref
= Expression::make_temporary_reference(hiter
, loc
);
5643 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5644 call
= Runtime::make_call(Runtime::MAPITERNEXT
, loc
, 1, p1
);
5645 post
->add_statement(Statement::make_statement(call
));
5650 // Lower a for range over a channel.
5653 For_range_statement::lower_range_channel(Gogo
*,
5656 Named_object
* range_object
,
5657 Temporary_statement
* range_temp
,
5658 Temporary_statement
* index_temp
,
5659 Temporary_statement
* value_temp
,
5665 go_assert(value_temp
== NULL
);
5667 source_location loc
= this->location();
5669 // The loop we generate:
5671 // index_temp, ok_temp = <-range
5675 // index = index_temp
5679 // We have no initialization code, no condition, and no post code.
5685 // Set *PITER_INIT to
5686 // index_temp, ok_temp = <-range
5691 Block
* iter_init
= new Block(body_block
, loc
);
5693 Temporary_statement
* ok_temp
=
5694 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
5695 iter_init
->add_statement(ok_temp
);
5697 Expression
* cref
= this->make_range_ref(range_object
, range_temp
, loc
);
5698 Temporary_reference_expression
* iref
=
5699 Expression::make_temporary_reference(index_temp
, loc
);
5700 iref
->set_is_lvalue();
5701 Temporary_reference_expression
* oref
=
5702 Expression::make_temporary_reference(ok_temp
, loc
);
5703 oref
->set_is_lvalue();
5704 Statement
* s
= Statement::make_tuple_receive_assignment(iref
, oref
, cref
,
5706 iter_init
->add_statement(s
);
5708 Block
* then_block
= new Block(iter_init
, loc
);
5709 s
= Statement::make_break_statement(this->break_label(), loc
);
5710 then_block
->add_statement(s
);
5712 oref
= Expression::make_temporary_reference(ok_temp
, loc
);
5713 Expression
* cond
= Expression::make_unary(OPERATOR_NOT
, oref
, loc
);
5714 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
5715 iter_init
->add_statement(s
);
5717 *piter_init
= iter_init
;
5720 // Return the break LABEL_EXPR.
5723 For_range_statement::break_label()
5725 if (this->break_label_
== NULL
)
5726 this->break_label_
= new Unnamed_label(this->location());
5727 return this->break_label_
;
5730 // Return the continue LABEL_EXPR.
5733 For_range_statement::continue_label()
5735 if (this->continue_label_
== NULL
)
5736 this->continue_label_
= new Unnamed_label(this->location());
5737 return this->continue_label_
;
5740 // Dump the AST representation for a for range statement.
5743 For_range_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5746 ast_dump_context
->print_indent();
5747 ast_dump_context
->ostream() << "for ";
5748 ast_dump_context
->dump_expression(this->index_var_
);
5749 if (this->value_var_
!= NULL
)
5751 ast_dump_context
->ostream() << ", ";
5752 ast_dump_context
->dump_expression(this->value_var_
);
5755 ast_dump_context
->ostream() << " = range ";
5756 ast_dump_context
->dump_expression(this->range_
);
5757 if (ast_dump_context
->dump_subblocks())
5759 ast_dump_context
->ostream() << " {" << std::endl
;
5761 ast_dump_context
->indent();
5763 ast_dump_context
->dump_block(this->statements_
);
5765 ast_dump_context
->unindent();
5766 ast_dump_context
->print_indent();
5767 ast_dump_context
->ostream() << "}";
5769 ast_dump_context
->ostream() << std::endl
;
5772 // Make a for statement with a range clause.
5774 For_range_statement
*
5775 Statement::make_for_range_statement(Expression
* index_var
,
5776 Expression
* value_var
,
5778 source_location location
)
5780 return new For_range_statement(index_var
, value_var
, range
, location
);