1 // statements.cc -- Go frontend statements.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #include "expressions.h"
15 #include "statements.h"
20 Statement::Statement(Statement_classification classification
,
22 : classification_(classification
), location_(location
)
26 Statement::~Statement()
30 // Traverse the tree. The work of walking the components is handled
34 Statement::traverse(Block
* block
, size_t* pindex
, Traverse
* traverse
)
36 if (this->classification_
== STATEMENT_ERROR
)
37 return TRAVERSE_CONTINUE
;
39 unsigned int traverse_mask
= traverse
->traverse_mask();
41 if ((traverse_mask
& Traverse::traverse_statements
) != 0)
43 int t
= traverse
->statement(block
, pindex
, this);
44 if (t
== TRAVERSE_EXIT
)
46 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
47 return TRAVERSE_CONTINUE
;
50 // No point in checking traverse_mask here--a statement may contain
51 // other blocks or statements, and if we got here we always want to
53 return this->do_traverse(traverse
);
56 // Traverse the contents of a statement.
59 Statement::traverse_contents(Traverse
* traverse
)
61 return this->do_traverse(traverse
);
64 // Traverse assignments.
67 Statement::traverse_assignments(Traverse_assignments
* tassign
)
69 if (this->classification_
== STATEMENT_ERROR
)
71 return this->do_traverse_assignments(tassign
);
74 // Traverse an expression in a statement. This is a helper function
78 Statement::traverse_expression(Traverse
* traverse
, Expression
** expr
)
80 if ((traverse
->traverse_mask()
81 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
82 return TRAVERSE_CONTINUE
;
83 return Expression::traverse(expr
, traverse
);
86 // Traverse an expression list in a statement. This is a helper
87 // function for child classes.
90 Statement::traverse_expression_list(Traverse
* traverse
,
91 Expression_list
* expr_list
)
93 if (expr_list
== NULL
)
94 return TRAVERSE_CONTINUE
;
95 if ((traverse
->traverse_mask()
96 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
97 return TRAVERSE_CONTINUE
;
98 return expr_list
->traverse(traverse
);
101 // Traverse a type in a statement. This is a helper function for
105 Statement::traverse_type(Traverse
* traverse
, Type
* type
)
107 if ((traverse
->traverse_mask()
108 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
109 return TRAVERSE_CONTINUE
;
110 return Type::traverse(type
, traverse
);
113 // Set type information for unnamed constants. This is really done by
117 Statement::determine_types()
119 this->do_determine_types();
122 // If this is a thunk statement, return it.
125 Statement::thunk_statement()
127 Thunk_statement
* ret
= this->convert
<Thunk_statement
, STATEMENT_GO
>();
129 ret
= this->convert
<Thunk_statement
, STATEMENT_DEFER
>();
133 // Convert a Statement to the backend representation. This is really
134 // done by the child class.
137 Statement::get_backend(Translate_context
* context
)
139 if (this->classification_
== STATEMENT_ERROR
)
140 return context
->backend()->error_statement();
141 return this->do_get_backend(context
);
144 // Dump AST representation for a statement to a dump context.
147 Statement::dump_statement(Ast_dump_context
* ast_dump_context
) const
149 this->do_dump_statement(ast_dump_context
);
152 // Note that this statement is erroneous. This is called by children
153 // when they discover an error.
156 Statement::set_is_error()
158 this->classification_
= STATEMENT_ERROR
;
161 // For children to call to report an error conveniently.
164 Statement::report_error(const char* msg
)
166 error_at(this->location_
, "%s", msg
);
167 this->set_is_error();
170 // An error statement, used to avoid crashing after we report an
173 class Error_statement
: public Statement
176 Error_statement(Location location
)
177 : Statement(STATEMENT_ERROR
, location
)
182 do_traverse(Traverse
*)
183 { return TRAVERSE_CONTINUE
; }
186 do_get_backend(Translate_context
*)
187 { go_unreachable(); }
190 do_dump_statement(Ast_dump_context
*) const;
193 // Dump the AST representation for an error statement.
196 Error_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
198 ast_dump_context
->print_indent();
199 ast_dump_context
->ostream() << "Error statement" << std::endl
;
202 // Make an error statement.
205 Statement::make_error_statement(Location location
)
207 return new Error_statement(location
);
210 // Class Variable_declaration_statement.
212 Variable_declaration_statement::Variable_declaration_statement(
214 : Statement(STATEMENT_VARIABLE_DECLARATION
, var
->var_value()->location()),
219 // We don't actually traverse the variable here; it was traversed
220 // while traversing the Block.
223 Variable_declaration_statement::do_traverse(Traverse
*)
225 return TRAVERSE_CONTINUE
;
228 // Traverse the assignments in a variable declaration. Note that this
229 // traversal is different from the usual traversal.
232 Variable_declaration_statement::do_traverse_assignments(
233 Traverse_assignments
* tassign
)
235 tassign
->initialize_variable(this->var_
);
239 // Lower the variable's initialization expression.
242 Variable_declaration_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
243 Block
*, Statement_inserter
* inserter
)
245 this->var_
->var_value()->lower_init_expression(gogo
, function
, inserter
);
249 // Convert a variable declaration to the backend representation.
252 Variable_declaration_statement::do_get_backend(Translate_context
* context
)
254 Variable
* var
= this->var_
->var_value();
255 Bvariable
* bvar
= this->var_
->get_backend_variable(context
->gogo(),
256 context
->function());
257 tree init
= var
->get_init_tree(context
->gogo(), context
->function());
258 Bexpression
* binit
= init
== NULL
? NULL
: tree_to_expr(init
);
260 if (!var
->is_in_heap())
262 go_assert(binit
!= NULL
);
263 return context
->backend()->init_statement(bvar
, binit
);
266 // Something takes the address of this variable, so the value is
267 // stored in the heap. Initialize it to newly allocated memory
268 // space, and assign the initial value to the new space.
269 Location loc
= this->location();
270 Named_object
* newfn
= context
->gogo()->lookup_global("new");
271 go_assert(newfn
!= NULL
&& newfn
->is_function_declaration());
272 Expression
* func
= Expression::make_func_reference(newfn
, NULL
, loc
);
273 Expression_list
* params
= new Expression_list();
274 params
->push_back(Expression::make_type(var
->type(), loc
));
275 Expression
* call
= Expression::make_call(func
, params
, false, loc
);
276 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
277 Temporary_statement
* temp
= Statement::make_temporary(NULL
, call
, loc
);
278 Bstatement
* btemp
= temp
->get_backend(context
);
280 Bstatement
* set
= NULL
;
283 Expression
* e
= Expression::make_temporary_reference(temp
, loc
);
284 e
= Expression::make_unary(OPERATOR_MULT
, e
, loc
);
285 Bexpression
* be
= tree_to_expr(e
->get_tree(context
));
286 set
= context
->backend()->assignment_statement(be
, binit
, loc
);
289 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
290 Bexpression
* bref
= tree_to_expr(ref
->get_tree(context
));
291 Bstatement
* sinit
= context
->backend()->init_statement(bvar
, bref
);
293 std::vector
<Bstatement
*> stats
;
295 stats
.push_back(btemp
);
297 stats
.push_back(set
);
298 stats
.push_back(sinit
);
299 return context
->backend()->statement_list(stats
);
302 // Dump the AST representation for a variable declaration.
305 Variable_declaration_statement::do_dump_statement(
306 Ast_dump_context
* ast_dump_context
) const
308 ast_dump_context
->print_indent();
310 go_assert(var_
->is_variable());
311 ast_dump_context
->ostream() << "var " << this->var_
->name() << " ";
312 Variable
* var
= this->var_
->var_value();
315 ast_dump_context
->dump_type(var
->type());
316 ast_dump_context
->ostream() << " ";
318 if (var
->init() != NULL
)
320 ast_dump_context
->ostream() << "= ";
321 ast_dump_context
->dump_expression(var
->init());
323 ast_dump_context
->ostream() << std::endl
;
326 // Make a variable declaration.
329 Statement::make_variable_declaration(Named_object
* var
)
331 return new Variable_declaration_statement(var
);
334 // Class Temporary_statement.
336 // Return the type of the temporary variable.
339 Temporary_statement::type() const
341 return this->type_
!= NULL
? this->type_
: this->init_
->type();
347 Temporary_statement::do_traverse(Traverse
* traverse
)
349 if (this->type_
!= NULL
350 && this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
351 return TRAVERSE_EXIT
;
352 if (this->init_
== NULL
)
353 return TRAVERSE_CONTINUE
;
355 return this->traverse_expression(traverse
, &this->init_
);
358 // Traverse assignments.
361 Temporary_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
363 if (this->init_
== NULL
)
365 tassign
->value(&this->init_
, true, true);
372 Temporary_statement::do_determine_types()
374 if (this->type_
!= NULL
&& this->type_
->is_abstract())
375 this->type_
= this->type_
->make_non_abstract_type();
377 if (this->init_
!= NULL
)
379 if (this->type_
== NULL
)
380 this->init_
->determine_type_no_context();
383 Type_context
context(this->type_
, false);
384 this->init_
->determine_type(&context
);
388 if (this->type_
== NULL
)
390 this->type_
= this->init_
->type();
391 go_assert(!this->type_
->is_abstract());
398 Temporary_statement::do_check_types(Gogo
*)
400 if (this->type_
!= NULL
&& this->init_
!= NULL
)
404 if (this->are_hidden_fields_ok_
)
405 ok
= Type::are_assignable_hidden_ok(this->type_
, this->init_
->type(),
408 ok
= Type::are_assignable(this->type_
, this->init_
->type(), &reason
);
412 error_at(this->location(), "incompatible types in assignment");
414 error_at(this->location(), "incompatible types in assignment (%s)",
416 this->set_is_error();
421 // Convert to backend representation.
424 Temporary_statement::do_get_backend(Translate_context
* context
)
426 go_assert(this->bvariable_
== NULL
);
428 // FIXME: Permitting FUNCTION to be NULL here is a temporary measure
429 // until we have a better representation of the init function.
430 Named_object
* function
= context
->function();
431 Bfunction
* bfunction
;
432 if (function
== NULL
)
435 bfunction
= tree_to_function(function
->func_value()->get_decl());
437 Btype
* btype
= this->type()->get_backend(context
->gogo());
440 if (this->init_
== NULL
)
442 else if (this->type_
== NULL
)
443 binit
= tree_to_expr(this->init_
->get_tree(context
));
446 Expression
* init
= Expression::make_cast(this->type_
, this->init_
,
448 context
->gogo()->lower_expression(context
->function(), NULL
, &init
);
449 binit
= tree_to_expr(init
->get_tree(context
));
452 Bstatement
* statement
;
454 context
->backend()->temporary_variable(bfunction
, context
->bblock(),
456 this->is_address_taken_
,
457 this->location(), &statement
);
461 // Return the backend variable.
464 Temporary_statement::get_backend_variable(Translate_context
* context
) const
466 if (this->bvariable_
== NULL
)
468 go_assert(saw_errors());
469 return context
->backend()->error_variable();
471 return this->bvariable_
;
474 // Dump the AST represemtation for a temporary statement
477 Temporary_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
479 ast_dump_context
->print_indent();
480 ast_dump_context
->dump_temp_variable_name(this);
481 if (this->type_
!= NULL
)
483 ast_dump_context
->ostream() << " ";
484 ast_dump_context
->dump_type(this->type_
);
486 if (this->init_
!= NULL
)
488 ast_dump_context
->ostream() << " = ";
489 ast_dump_context
->dump_expression(this->init_
);
491 ast_dump_context
->ostream() << std::endl
;
494 // Make and initialize a temporary variable in BLOCK.
497 Statement::make_temporary(Type
* type
, Expression
* init
,
500 return new Temporary_statement(type
, init
, location
);
503 // An assignment statement.
505 class Assignment_statement
: public Statement
508 Assignment_statement(Expression
* lhs
, Expression
* rhs
,
510 : Statement(STATEMENT_ASSIGNMENT
, location
),
511 lhs_(lhs
), rhs_(rhs
), are_hidden_fields_ok_(false)
514 // Note that it is OK for this assignment statement to set hidden
517 set_hidden_fields_are_ok()
518 { this->are_hidden_fields_ok_
= true; }
522 do_traverse(Traverse
* traverse
);
525 do_traverse_assignments(Traverse_assignments
*);
528 do_determine_types();
531 do_check_types(Gogo
*);
534 do_get_backend(Translate_context
*);
537 do_dump_statement(Ast_dump_context
*) const;
540 // Left hand side--the lvalue.
542 // Right hand side--the rvalue.
544 // True if this statement may set hidden fields in the assignment
545 // statement. This is used for generated method stubs.
546 bool are_hidden_fields_ok_
;
552 Assignment_statement::do_traverse(Traverse
* traverse
)
554 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
555 return TRAVERSE_EXIT
;
556 return this->traverse_expression(traverse
, &this->rhs_
);
560 Assignment_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
562 tassign
->assignment(&this->lhs_
, &this->rhs_
);
566 // Set types for the assignment.
569 Assignment_statement::do_determine_types()
571 this->lhs_
->determine_type_no_context();
572 Type_context
context(this->lhs_
->type(), false);
573 this->rhs_
->determine_type(&context
);
576 // Check types for an assignment.
579 Assignment_statement::do_check_types(Gogo
*)
581 // The left hand side must be either addressable, a map index
582 // expression, or the blank identifier.
583 if (!this->lhs_
->is_addressable()
584 && this->lhs_
->map_index_expression() == NULL
585 && !this->lhs_
->is_sink_expression())
587 if (!this->lhs_
->type()->is_error())
588 this->report_error(_("invalid left hand side of assignment"));
592 Type
* lhs_type
= this->lhs_
->type();
593 Type
* rhs_type
= this->rhs_
->type();
596 if (this->are_hidden_fields_ok_
)
597 ok
= Type::are_assignable_hidden_ok(lhs_type
, rhs_type
, &reason
);
599 ok
= Type::are_assignable(lhs_type
, rhs_type
, &reason
);
603 error_at(this->location(), "incompatible types in assignment");
605 error_at(this->location(), "incompatible types in assignment (%s)",
607 this->set_is_error();
610 if (lhs_type
->is_error() || rhs_type
->is_error())
611 this->set_is_error();
614 // Convert an assignment statement to the backend representation.
617 Assignment_statement::do_get_backend(Translate_context
* context
)
619 tree rhs_tree
= this->rhs_
->get_tree(context
);
620 if (this->lhs_
->is_sink_expression())
621 return context
->backend()->expression_statement(tree_to_expr(rhs_tree
));
622 tree lhs_tree
= this->lhs_
->get_tree(context
);
623 rhs_tree
= Expression::convert_for_assignment(context
, this->lhs_
->type(),
624 this->rhs_
->type(), rhs_tree
,
626 return context
->backend()->assignment_statement(tree_to_expr(lhs_tree
),
627 tree_to_expr(rhs_tree
),
631 // Dump the AST representation for an assignment statement.
634 Assignment_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
637 ast_dump_context
->print_indent();
638 ast_dump_context
->dump_expression(this->lhs_
);
639 ast_dump_context
->ostream() << " = " ;
640 ast_dump_context
->dump_expression(this->rhs_
);
641 ast_dump_context
->ostream() << std::endl
;
644 // Make an assignment statement.
647 Statement::make_assignment(Expression
* lhs
, Expression
* rhs
,
650 return new Assignment_statement(lhs
, rhs
, location
);
653 // The Move_subexpressions class is used to move all top-level
654 // subexpressions of an expression. This is used for things like
655 // index expressions in which we must evaluate the index value before
656 // it can be changed by a multiple assignment.
658 class Move_subexpressions
: public Traverse
661 Move_subexpressions(int skip
, Block
* block
)
662 : Traverse(traverse_expressions
),
663 skip_(skip
), block_(block
)
668 expression(Expression
**);
671 // The number of subexpressions to skip moving. This is used to
672 // avoid moving the array itself, as we only need to move the index.
674 // The block where new temporary variables should be added.
679 Move_subexpressions::expression(Expression
** pexpr
)
683 else if ((*pexpr
)->temporary_reference_expression() == NULL
)
685 Location loc
= (*pexpr
)->location();
686 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
687 this->block_
->add_statement(temp
);
688 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
690 // We only need to move top-level subexpressions.
691 return TRAVERSE_SKIP_COMPONENTS
;
694 // The Move_ordered_evals class is used to find any subexpressions of
695 // an expression that have an evaluation order dependency. It creates
696 // temporary variables to hold them.
698 class Move_ordered_evals
: public Traverse
701 Move_ordered_evals(Block
* block
)
702 : Traverse(traverse_expressions
),
708 expression(Expression
**);
711 // The block where new temporary variables should be added.
716 Move_ordered_evals::expression(Expression
** pexpr
)
718 // We have to look at subexpressions first.
719 if ((*pexpr
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
720 return TRAVERSE_EXIT
;
723 if ((*pexpr
)->must_eval_subexpressions_in_order(&i
))
725 Move_subexpressions
ms(i
, this->block_
);
726 if ((*pexpr
)->traverse_subexpressions(&ms
) == TRAVERSE_EXIT
)
727 return TRAVERSE_EXIT
;
730 if ((*pexpr
)->must_eval_in_order())
732 Location loc
= (*pexpr
)->location();
733 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
734 this->block_
->add_statement(temp
);
735 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
737 return TRAVERSE_SKIP_COMPONENTS
;
740 // An assignment operation statement.
742 class Assignment_operation_statement
: public Statement
745 Assignment_operation_statement(Operator op
, Expression
* lhs
, Expression
* rhs
,
747 : Statement(STATEMENT_ASSIGNMENT_OPERATION
, location
),
748 op_(op
), lhs_(lhs
), rhs_(rhs
)
753 do_traverse(Traverse
*);
756 do_traverse_assignments(Traverse_assignments
*)
757 { go_unreachable(); }
760 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
763 do_get_backend(Translate_context
*)
764 { go_unreachable(); }
767 do_dump_statement(Ast_dump_context
*) const;
770 // The operator (OPERATOR_PLUSEQ, etc.).
781 Assignment_operation_statement::do_traverse(Traverse
* traverse
)
783 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
784 return TRAVERSE_EXIT
;
785 return this->traverse_expression(traverse
, &this->rhs_
);
788 // Lower an assignment operation statement to a regular assignment
792 Assignment_operation_statement::do_lower(Gogo
*, Named_object
*,
793 Block
* enclosing
, Statement_inserter
*)
795 Location loc
= this->location();
797 // We have to evaluate the left hand side expression only once. We
798 // do this by moving out any expression with side effects.
799 Block
* b
= new Block(enclosing
, loc
);
800 Move_ordered_evals
moe(b
);
801 this->lhs_
->traverse_subexpressions(&moe
);
803 Expression
* lval
= this->lhs_
->copy();
808 case OPERATOR_PLUSEQ
:
811 case OPERATOR_MINUSEQ
:
820 case OPERATOR_MULTEQ
:
829 case OPERATOR_LSHIFTEQ
:
830 op
= OPERATOR_LSHIFT
;
832 case OPERATOR_RSHIFTEQ
:
833 op
= OPERATOR_RSHIFT
;
838 case OPERATOR_BITCLEAREQ
:
839 op
= OPERATOR_BITCLEAR
;
845 Expression
* binop
= Expression::make_binary(op
, lval
, this->rhs_
, loc
);
846 Statement
* s
= Statement::make_assignment(this->lhs_
, binop
, loc
);
847 if (b
->statements()->empty())
855 return Statement::make_block_statement(b
, loc
);
859 // Dump the AST representation for an assignment operation statement
862 Assignment_operation_statement::do_dump_statement(
863 Ast_dump_context
* ast_dump_context
) const
865 ast_dump_context
->print_indent();
866 ast_dump_context
->dump_expression(this->lhs_
);
867 ast_dump_context
->dump_operator(this->op_
);
868 ast_dump_context
->dump_expression(this->rhs_
);
869 ast_dump_context
->ostream() << std::endl
;
872 // Make an assignment operation statement.
875 Statement::make_assignment_operation(Operator op
, Expression
* lhs
,
876 Expression
* rhs
, Location location
)
878 return new Assignment_operation_statement(op
, lhs
, rhs
, location
);
881 // A tuple assignment statement. This differs from an assignment
882 // statement in that the right-hand-side expressions are evaluated in
885 class Tuple_assignment_statement
: public Statement
888 Tuple_assignment_statement(Expression_list
* lhs
, Expression_list
* rhs
,
890 : Statement(STATEMENT_TUPLE_ASSIGNMENT
, location
),
891 lhs_(lhs
), rhs_(rhs
), are_hidden_fields_ok_(false)
894 // Note that it is OK for this assignment statement to set hidden
897 set_hidden_fields_are_ok()
898 { this->are_hidden_fields_ok_
= true; }
902 do_traverse(Traverse
* traverse
);
905 do_traverse_assignments(Traverse_assignments
*)
906 { go_unreachable(); }
909 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
912 do_get_backend(Translate_context
*)
913 { go_unreachable(); }
916 do_dump_statement(Ast_dump_context
*) const;
919 // Left hand side--a list of lvalues.
920 Expression_list
* lhs_
;
921 // Right hand side--a list of rvalues.
922 Expression_list
* rhs_
;
923 // True if this statement may set hidden fields in the assignment
924 // statement. This is used for generated method stubs.
925 bool are_hidden_fields_ok_
;
931 Tuple_assignment_statement::do_traverse(Traverse
* traverse
)
933 if (this->traverse_expression_list(traverse
, this->lhs_
) == TRAVERSE_EXIT
)
934 return TRAVERSE_EXIT
;
935 return this->traverse_expression_list(traverse
, this->rhs_
);
938 // Lower a tuple assignment. We use temporary variables to split it
939 // up into a set of single assignments.
942 Tuple_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
945 Location loc
= this->location();
947 Block
* b
= new Block(enclosing
, loc
);
949 // First move out any subexpressions on the left hand side. The
950 // right hand side will be evaluated in the required order anyhow.
951 Move_ordered_evals
moe(b
);
952 for (Expression_list::iterator plhs
= this->lhs_
->begin();
953 plhs
!= this->lhs_
->end();
955 Expression::traverse(&*plhs
, &moe
);
957 std::vector
<Temporary_statement
*> temps
;
958 temps
.reserve(this->lhs_
->size());
960 Expression_list::const_iterator prhs
= this->rhs_
->begin();
961 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
962 plhs
!= this->lhs_
->end();
965 go_assert(prhs
!= this->rhs_
->end());
967 if ((*plhs
)->is_error_expression()
968 || (*plhs
)->type()->is_error()
969 || (*prhs
)->is_error_expression()
970 || (*prhs
)->type()->is_error())
973 if ((*plhs
)->is_sink_expression())
975 b
->add_statement(Statement::make_statement(*prhs
, true));
979 Temporary_statement
* temp
= Statement::make_temporary((*plhs
)->type(),
981 if (this->are_hidden_fields_ok_
)
982 temp
->set_hidden_fields_are_ok();
983 b
->add_statement(temp
);
984 temps
.push_back(temp
);
987 go_assert(prhs
== this->rhs_
->end());
989 prhs
= this->rhs_
->begin();
990 std::vector
<Temporary_statement
*>::const_iterator ptemp
= temps
.begin();
991 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
992 plhs
!= this->lhs_
->end();
995 if ((*plhs
)->is_error_expression()
996 || (*plhs
)->type()->is_error()
997 || (*prhs
)->is_error_expression()
998 || (*prhs
)->type()->is_error())
1001 if ((*plhs
)->is_sink_expression())
1004 Expression
* ref
= Expression::make_temporary_reference(*ptemp
, loc
);
1005 Statement
* s
= Statement::make_assignment(*plhs
, ref
, loc
);
1006 if (this->are_hidden_fields_ok_
)
1008 Assignment_statement
* as
= static_cast<Assignment_statement
*>(s
);
1009 as
->set_hidden_fields_are_ok();
1011 b
->add_statement(s
);
1014 go_assert(ptemp
== temps
.end() || saw_errors());
1016 return Statement::make_block_statement(b
, loc
);
1019 // Dump the AST representation for a tuple assignment statement.
1022 Tuple_assignment_statement::do_dump_statement(
1023 Ast_dump_context
* ast_dump_context
) const
1025 ast_dump_context
->print_indent();
1026 ast_dump_context
->dump_expression_list(this->lhs_
);
1027 ast_dump_context
->ostream() << " = ";
1028 ast_dump_context
->dump_expression_list(this->rhs_
);
1029 ast_dump_context
->ostream() << std::endl
;
1032 // Make a tuple assignment statement.
1035 Statement::make_tuple_assignment(Expression_list
* lhs
, Expression_list
* rhs
,
1038 return new Tuple_assignment_statement(lhs
, rhs
, location
);
1041 // A tuple assignment from a map index expression.
1044 class Tuple_map_assignment_statement
: public Statement
1047 Tuple_map_assignment_statement(Expression
* val
, Expression
* present
,
1048 Expression
* map_index
,
1050 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT
, location
),
1051 val_(val
), present_(present
), map_index_(map_index
)
1056 do_traverse(Traverse
* traverse
);
1059 do_traverse_assignments(Traverse_assignments
*)
1060 { go_unreachable(); }
1063 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1066 do_get_backend(Translate_context
*)
1067 { go_unreachable(); }
1070 do_dump_statement(Ast_dump_context
*) const;
1073 // Lvalue which receives the value from the map.
1075 // Lvalue which receives whether the key value was present.
1076 Expression
* present_
;
1077 // The map index expression.
1078 Expression
* map_index_
;
1084 Tuple_map_assignment_statement::do_traverse(Traverse
* traverse
)
1086 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1087 || this->traverse_expression(traverse
, &this->present_
) == TRAVERSE_EXIT
)
1088 return TRAVERSE_EXIT
;
1089 return this->traverse_expression(traverse
, &this->map_index_
);
1092 // Lower a tuple map assignment.
1095 Tuple_map_assignment_statement::do_lower(Gogo
*, Named_object
*,
1096 Block
* enclosing
, Statement_inserter
*)
1098 Location loc
= this->location();
1100 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1101 if (map_index
== NULL
)
1103 this->report_error(_("expected map index on right hand side"));
1104 return Statement::make_error_statement(loc
);
1106 Map_type
* map_type
= map_index
->get_map_type();
1107 if (map_type
== NULL
)
1108 return Statement::make_error_statement(loc
);
1110 Block
* b
= new Block(enclosing
, loc
);
1112 // Move out any subexpressions to make sure that functions are
1113 // called in the required order.
1114 Move_ordered_evals
moe(b
);
1115 this->val_
->traverse_subexpressions(&moe
);
1116 this->present_
->traverse_subexpressions(&moe
);
1118 // Copy the key value into a temporary so that we can take its
1119 // address without pushing the value onto the heap.
1121 // var key_temp KEY_TYPE = MAP_INDEX
1122 Temporary_statement
* key_temp
=
1123 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1124 b
->add_statement(key_temp
);
1126 // var val_temp VAL_TYPE
1127 Temporary_statement
* val_temp
=
1128 Statement::make_temporary(map_type
->val_type(), NULL
, loc
);
1129 b
->add_statement(val_temp
);
1131 // var present_temp bool
1132 Temporary_statement
* present_temp
=
1133 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
1134 b
->add_statement(present_temp
);
1136 // present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp, &val_temp)
1137 Expression
* a1
= Expression::make_type_descriptor(map_type
, loc
);
1138 Expression
* a2
= map_index
->map();
1139 Temporary_reference_expression
* ref
=
1140 Expression::make_temporary_reference(key_temp
, loc
);
1141 Expression
* a3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1142 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1143 Expression
* a4
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1144 Expression
* call
= Runtime::make_call(Runtime::MAPACCESS2
, loc
, 4,
1147 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1148 ref
->set_is_lvalue();
1149 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1150 b
->add_statement(s
);
1153 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1154 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1155 b
->add_statement(s
);
1157 // present = present_temp
1158 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1159 s
= Statement::make_assignment(this->present_
, ref
, loc
);
1160 b
->add_statement(s
);
1162 return Statement::make_block_statement(b
, loc
);
1165 // Dump the AST representation for a tuple map assignment statement.
1168 Tuple_map_assignment_statement::do_dump_statement(
1169 Ast_dump_context
* ast_dump_context
) const
1171 ast_dump_context
->print_indent();
1172 ast_dump_context
->dump_expression(this->val_
);
1173 ast_dump_context
->ostream() << ", ";
1174 ast_dump_context
->dump_expression(this->present_
);
1175 ast_dump_context
->ostream() << " = ";
1176 ast_dump_context
->dump_expression(this->map_index_
);
1177 ast_dump_context
->ostream() << std::endl
;
1180 // Make a map assignment statement which returns a pair of values.
1183 Statement::make_tuple_map_assignment(Expression
* val
, Expression
* present
,
1184 Expression
* map_index
,
1187 return new Tuple_map_assignment_statement(val
, present
, map_index
, location
);
1190 // Assign a pair of entries to a map.
1193 class Map_assignment_statement
: public Statement
1196 Map_assignment_statement(Expression
* map_index
,
1197 Expression
* val
, Expression
* should_set
,
1199 : Statement(STATEMENT_MAP_ASSIGNMENT
, location
),
1200 map_index_(map_index
), val_(val
), should_set_(should_set
)
1205 do_traverse(Traverse
* traverse
);
1208 do_traverse_assignments(Traverse_assignments
*)
1209 { go_unreachable(); }
1212 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1215 do_get_backend(Translate_context
*)
1216 { go_unreachable(); }
1219 do_dump_statement(Ast_dump_context
*) const;
1222 // A reference to the map index which should be set or deleted.
1223 Expression
* map_index_
;
1224 // The value to add to the map.
1226 // Whether or not to add the value.
1227 Expression
* should_set_
;
1230 // Traverse a map assignment.
1233 Map_assignment_statement::do_traverse(Traverse
* traverse
)
1235 if (this->traverse_expression(traverse
, &this->map_index_
) == TRAVERSE_EXIT
1236 || this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
1237 return TRAVERSE_EXIT
;
1238 return this->traverse_expression(traverse
, &this->should_set_
);
1241 // Lower a map assignment to a function call.
1244 Map_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
1245 Statement_inserter
*)
1247 Location loc
= this->location();
1249 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1250 if (map_index
== NULL
)
1252 this->report_error(_("expected map index on left hand side"));
1253 return Statement::make_error_statement(loc
);
1255 Map_type
* map_type
= map_index
->get_map_type();
1256 if (map_type
== NULL
)
1257 return Statement::make_error_statement(loc
);
1259 Block
* b
= new Block(enclosing
, loc
);
1261 // Evaluate the map first to get order of evaluation right.
1262 // map_temp := m // we are evaluating m[k] = v, p
1263 Temporary_statement
* map_temp
= Statement::make_temporary(map_type
,
1266 b
->add_statement(map_temp
);
1268 // var key_temp MAP_KEY_TYPE = k
1269 Temporary_statement
* key_temp
=
1270 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1271 b
->add_statement(key_temp
);
1273 // var val_temp MAP_VAL_TYPE = v
1274 Temporary_statement
* val_temp
=
1275 Statement::make_temporary(map_type
->val_type(), this->val_
, loc
);
1276 b
->add_statement(val_temp
);
1278 // var insert_temp bool = p
1279 Temporary_statement
* insert_temp
=
1280 Statement::make_temporary(Type::lookup_bool_type(), this->should_set_
,
1282 b
->add_statement(insert_temp
);
1284 // mapassign2(map_temp, &key_temp, &val_temp, p)
1285 Expression
* p1
= Expression::make_temporary_reference(map_temp
, loc
);
1286 Expression
* ref
= Expression::make_temporary_reference(key_temp
, loc
);
1287 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1288 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1289 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1290 Expression
* p4
= Expression::make_temporary_reference(insert_temp
, loc
);
1291 Expression
* call
= Runtime::make_call(Runtime::MAPASSIGN2
, loc
, 4,
1293 Statement
* s
= Statement::make_statement(call
, true);
1294 b
->add_statement(s
);
1296 return Statement::make_block_statement(b
, loc
);
1299 // Dump the AST representation for a map assignment statement.
1302 Map_assignment_statement::do_dump_statement(
1303 Ast_dump_context
* ast_dump_context
) const
1305 ast_dump_context
->print_indent();
1306 ast_dump_context
->dump_expression(this->map_index_
);
1307 ast_dump_context
->ostream() << " = ";
1308 ast_dump_context
->dump_expression(this->val_
);
1309 ast_dump_context
->ostream() << ", ";
1310 ast_dump_context
->dump_expression(this->should_set_
);
1311 ast_dump_context
->ostream() << std::endl
;
1314 // Make a statement which assigns a pair of entries to a map.
1317 Statement::make_map_assignment(Expression
* map_index
,
1318 Expression
* val
, Expression
* should_set
,
1321 return new Map_assignment_statement(map_index
, val
, should_set
, location
);
1324 // A tuple assignment from a receive statement.
1326 class Tuple_receive_assignment_statement
: public Statement
1329 Tuple_receive_assignment_statement(Expression
* val
, Expression
* closed
,
1330 Expression
* channel
, Location location
)
1331 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
, location
),
1332 val_(val
), closed_(closed
), channel_(channel
)
1337 do_traverse(Traverse
* traverse
);
1340 do_traverse_assignments(Traverse_assignments
*)
1341 { go_unreachable(); }
1344 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1347 do_get_backend(Translate_context
*)
1348 { go_unreachable(); }
1351 do_dump_statement(Ast_dump_context
*) const;
1354 // Lvalue which receives the value from the channel.
1356 // Lvalue which receives whether the channel is closed.
1357 Expression
* closed_
;
1358 // The channel on which we receive the value.
1359 Expression
* channel_
;
1365 Tuple_receive_assignment_statement::do_traverse(Traverse
* traverse
)
1367 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1368 || this->traverse_expression(traverse
, &this->closed_
) == TRAVERSE_EXIT
)
1369 return TRAVERSE_EXIT
;
1370 return this->traverse_expression(traverse
, &this->channel_
);
1373 // Lower to a function call.
1376 Tuple_receive_assignment_statement::do_lower(Gogo
*, Named_object
*,
1378 Statement_inserter
*)
1380 Location loc
= this->location();
1382 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
1383 if (channel_type
== NULL
)
1385 this->report_error(_("expected channel"));
1386 return Statement::make_error_statement(loc
);
1388 if (!channel_type
->may_receive())
1390 this->report_error(_("invalid receive on send-only channel"));
1391 return Statement::make_error_statement(loc
);
1394 Block
* b
= new Block(enclosing
, loc
);
1396 // Make sure that any subexpressions on the left hand side are
1397 // evaluated in the right order.
1398 Move_ordered_evals
moe(b
);
1399 this->val_
->traverse_subexpressions(&moe
);
1400 this->closed_
->traverse_subexpressions(&moe
);
1402 // var val_temp ELEMENT_TYPE
1403 Temporary_statement
* val_temp
=
1404 Statement::make_temporary(channel_type
->element_type(), NULL
, loc
);
1405 b
->add_statement(val_temp
);
1407 // var closed_temp bool
1408 Temporary_statement
* closed_temp
=
1409 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
1410 b
->add_statement(closed_temp
);
1412 // closed_temp = chanrecv2(type, channel, &val_temp)
1413 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
1415 Temporary_reference_expression
* ref
=
1416 Expression::make_temporary_reference(val_temp
, loc
);
1417 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1418 Expression
* call
= Runtime::make_call(Runtime::CHANRECV2
,
1419 loc
, 3, td
, this->channel_
, p2
);
1420 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1421 ref
->set_is_lvalue();
1422 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1423 b
->add_statement(s
);
1426 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1427 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1428 b
->add_statement(s
);
1430 // closed = closed_temp
1431 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1432 s
= Statement::make_assignment(this->closed_
, ref
, loc
);
1433 b
->add_statement(s
);
1435 return Statement::make_block_statement(b
, loc
);
1438 // Dump the AST representation for a tuple receive statement.
1441 Tuple_receive_assignment_statement::do_dump_statement(
1442 Ast_dump_context
* ast_dump_context
) const
1444 ast_dump_context
->print_indent();
1445 ast_dump_context
->dump_expression(this->val_
);
1446 ast_dump_context
->ostream() << ", ";
1447 ast_dump_context
->dump_expression(this->closed_
);
1448 ast_dump_context
->ostream() << " <- ";
1449 ast_dump_context
->dump_expression(this->channel_
);
1450 ast_dump_context
->ostream() << std::endl
;
1453 // Make a nonblocking receive statement.
1456 Statement::make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
1457 Expression
* channel
,
1460 return new Tuple_receive_assignment_statement(val
, closed
, channel
,
1464 // An assignment to a pair of values from a type guard. This is a
1465 // conditional type guard. v, ok = i.(type).
1467 class Tuple_type_guard_assignment_statement
: public Statement
1470 Tuple_type_guard_assignment_statement(Expression
* val
, Expression
* ok
,
1471 Expression
* expr
, Type
* type
,
1473 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
, location
),
1474 val_(val
), ok_(ok
), expr_(expr
), type_(type
)
1479 do_traverse(Traverse
*);
1482 do_traverse_assignments(Traverse_assignments
*)
1483 { go_unreachable(); }
1486 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1489 do_get_backend(Translate_context
*)
1490 { go_unreachable(); }
1493 do_dump_statement(Ast_dump_context
*) const;
1497 lower_to_type(Runtime::Function
);
1500 lower_to_object_type(Block
*, Runtime::Function
);
1502 // The variable which recieves the converted value.
1504 // The variable which receives the indication of success.
1506 // The expression being converted.
1508 // The type to which the expression is being converted.
1512 // Traverse a type guard tuple assignment.
1515 Tuple_type_guard_assignment_statement::do_traverse(Traverse
* traverse
)
1517 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1518 || this->traverse_expression(traverse
, &this->ok_
) == TRAVERSE_EXIT
1519 || this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
1520 return TRAVERSE_EXIT
;
1521 return this->traverse_expression(traverse
, &this->expr_
);
1524 // Lower to a function call.
1527 Tuple_type_guard_assignment_statement::do_lower(Gogo
*, Named_object
*,
1529 Statement_inserter
*)
1531 Location loc
= this->location();
1533 Type
* expr_type
= this->expr_
->type();
1534 if (expr_type
->interface_type() == NULL
)
1536 if (!expr_type
->is_error() && !this->type_
->is_error())
1537 this->report_error(_("type assertion only valid for interface types"));
1538 return Statement::make_error_statement(loc
);
1541 Block
* b
= new Block(enclosing
, loc
);
1543 // Make sure that any subexpressions on the left hand side are
1544 // evaluated in the right order.
1545 Move_ordered_evals
moe(b
);
1546 this->val_
->traverse_subexpressions(&moe
);
1547 this->ok_
->traverse_subexpressions(&moe
);
1549 bool expr_is_empty
= expr_type
->interface_type()->is_empty();
1550 Call_expression
* call
;
1551 if (this->type_
->interface_type() != NULL
)
1553 if (this->type_
->interface_type()->is_empty())
1554 call
= Runtime::make_call((expr_is_empty
1555 ? Runtime::IFACEE2E2
1556 : Runtime::IFACEI2E2
),
1557 loc
, 1, this->expr_
);
1559 call
= this->lower_to_type(expr_is_empty
1560 ? Runtime::IFACEE2I2
1561 : Runtime::IFACEI2I2
);
1563 else if (this->type_
->points_to() != NULL
)
1564 call
= this->lower_to_type(expr_is_empty
1565 ? Runtime::IFACEE2T2P
1566 : Runtime::IFACEI2T2P
);
1569 this->lower_to_object_type(b
,
1571 ? Runtime::IFACEE2T2
1572 : Runtime::IFACEI2T2
));
1578 Expression
* res
= Expression::make_call_result(call
, 0);
1579 res
= Expression::make_unsafe_cast(this->type_
, res
, loc
);
1580 Statement
* s
= Statement::make_assignment(this->val_
, res
, loc
);
1581 b
->add_statement(s
);
1583 res
= Expression::make_call_result(call
, 1);
1584 s
= Statement::make_assignment(this->ok_
, res
, loc
);
1585 b
->add_statement(s
);
1588 return Statement::make_block_statement(b
, loc
);
1591 // Lower a conversion to a non-empty interface type or a pointer type.
1594 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code
)
1596 Location loc
= this->location();
1597 return Runtime::make_call(code
, loc
, 2,
1598 Expression::make_type_descriptor(this->type_
, loc
),
1602 // Lower a conversion to a non-interface non-pointer type.
1605 Tuple_type_guard_assignment_statement::lower_to_object_type(
1607 Runtime::Function code
)
1609 Location loc
= this->location();
1611 // var val_temp TYPE
1612 Temporary_statement
* val_temp
= Statement::make_temporary(this->type_
,
1614 b
->add_statement(val_temp
);
1616 // ok = CODE(type_descriptor, expr, &val_temp)
1617 Expression
* p1
= Expression::make_type_descriptor(this->type_
, loc
);
1618 Expression
* ref
= Expression::make_temporary_reference(val_temp
, loc
);
1619 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1620 Expression
* call
= Runtime::make_call(code
, loc
, 3, p1
, this->expr_
, p3
);
1621 Statement
* s
= Statement::make_assignment(this->ok_
, call
, loc
);
1622 b
->add_statement(s
);
1625 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1626 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1627 b
->add_statement(s
);
1630 // Dump the AST representation for a tuple type guard statement.
1633 Tuple_type_guard_assignment_statement::do_dump_statement(
1634 Ast_dump_context
* ast_dump_context
) const
1636 ast_dump_context
->print_indent();
1637 ast_dump_context
->dump_expression(this->val_
);
1638 ast_dump_context
->ostream() << ", ";
1639 ast_dump_context
->dump_expression(this->ok_
);
1640 ast_dump_context
->ostream() << " = ";
1641 ast_dump_context
->dump_expression(this->expr_
);
1642 ast_dump_context
->ostream() << " . ";
1643 ast_dump_context
->dump_type(this->type_
);
1644 ast_dump_context
->ostream() << std::endl
;
1647 // Make an assignment from a type guard to a pair of variables.
1650 Statement::make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
1651 Expression
* expr
, Type
* type
,
1654 return new Tuple_type_guard_assignment_statement(val
, ok
, expr
, type
,
1658 // An expression statement.
1660 class Expression_statement
: public Statement
1663 Expression_statement(Expression
* expr
, bool is_ignored
)
1664 : Statement(STATEMENT_EXPRESSION
, expr
->location()),
1665 expr_(expr
), is_ignored_(is_ignored
)
1670 { return this->expr_
; }
1674 do_traverse(Traverse
* traverse
)
1675 { return this->traverse_expression(traverse
, &this->expr_
); }
1678 do_determine_types()
1679 { this->expr_
->determine_type_no_context(); }
1682 do_check_types(Gogo
*);
1685 do_may_fall_through() const;
1688 do_get_backend(Translate_context
* context
);
1691 do_dump_statement(Ast_dump_context
*) const;
1695 // Whether the value of this expression is being explicitly ignored.
1699 // Check the types of an expression statement. The only check we do
1700 // is to possibly give an error about discarding the value of the
1704 Expression_statement::do_check_types(Gogo
*)
1706 if (!this->is_ignored_
)
1707 this->expr_
->discarding_value();
1710 // An expression statement is only a terminating statement if it is
1714 Expression_statement::do_may_fall_through() const
1716 const Call_expression
* call
= this->expr_
->call_expression();
1719 const Expression
* fn
= call
->fn();
1720 // panic is still an unknown named object.
1721 const Unknown_expression
* ue
= fn
->unknown_expression();
1724 Named_object
* no
= ue
->named_object();
1726 if (no
->is_unknown())
1727 no
= no
->unknown_value()->real_named_object();
1730 Function_type
* fntype
;
1731 if (no
->is_function())
1732 fntype
= no
->func_value()->type();
1733 else if (no
->is_function_declaration())
1734 fntype
= no
->func_declaration_value()->type();
1738 // The builtin function panic does not return.
1739 if (fntype
!= NULL
&& fntype
->is_builtin() && no
->name() == "panic")
1747 // Convert to backend representation.
1750 Expression_statement::do_get_backend(Translate_context
* context
)
1752 tree expr_tree
= this->expr_
->get_tree(context
);
1753 return context
->backend()->expression_statement(tree_to_expr(expr_tree
));
1756 // Dump the AST representation for an expression statement
1759 Expression_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
1762 ast_dump_context
->print_indent();
1763 ast_dump_context
->dump_expression(expr_
);
1764 ast_dump_context
->ostream() << std::endl
;
1767 // Make an expression statement from an Expression.
1770 Statement::make_statement(Expression
* expr
, bool is_ignored
)
1772 return new Expression_statement(expr
, is_ignored
);
1775 // A block statement--a list of statements which may include variable
1778 class Block_statement
: public Statement
1781 Block_statement(Block
* block
, Location location
)
1782 : Statement(STATEMENT_BLOCK
, location
),
1788 do_traverse(Traverse
* traverse
)
1789 { return this->block_
->traverse(traverse
); }
1792 do_determine_types()
1793 { this->block_
->determine_types(); }
1796 do_may_fall_through() const
1797 { return this->block_
->may_fall_through(); }
1800 do_get_backend(Translate_context
* context
);
1803 do_dump_statement(Ast_dump_context
*) const;
1809 // Convert a block to the backend representation of a statement.
1812 Block_statement::do_get_backend(Translate_context
* context
)
1814 Bblock
* bblock
= this->block_
->get_backend(context
);
1815 return context
->backend()->block_statement(bblock
);
1818 // Dump the AST for a block statement
1821 Block_statement::do_dump_statement(Ast_dump_context
*) const
1823 // block statement braces are dumped when traversing.
1826 // Make a block statement.
1829 Statement::make_block_statement(Block
* block
, Location location
)
1831 return new Block_statement(block
, location
);
1834 // An increment or decrement statement.
1836 class Inc_dec_statement
: public Statement
1839 Inc_dec_statement(bool is_inc
, Expression
* expr
)
1840 : Statement(STATEMENT_INCDEC
, expr
->location()),
1841 expr_(expr
), is_inc_(is_inc
)
1846 do_traverse(Traverse
* traverse
)
1847 { return this->traverse_expression(traverse
, &this->expr_
); }
1850 do_traverse_assignments(Traverse_assignments
*)
1851 { go_unreachable(); }
1854 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1857 do_get_backend(Translate_context
*)
1858 { go_unreachable(); }
1861 do_dump_statement(Ast_dump_context
*) const;
1864 // The l-value to increment or decrement.
1866 // Whether to increment or decrement.
1870 // Lower to += or -=.
1873 Inc_dec_statement::do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
1875 Location loc
= this->location();
1878 mpz_init_set_ui(oval
, 1UL);
1879 Expression
* oexpr
= Expression::make_integer(&oval
, NULL
, loc
);
1882 Operator op
= this->is_inc_
? OPERATOR_PLUSEQ
: OPERATOR_MINUSEQ
;
1883 return Statement::make_assignment_operation(op
, this->expr_
, oexpr
, loc
);
1886 // Dump the AST representation for a inc/dec statement.
1889 Inc_dec_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
1891 ast_dump_context
->print_indent();
1892 ast_dump_context
->dump_expression(expr_
);
1893 ast_dump_context
->ostream() << (is_inc_
? "++": "--") << std::endl
;
1896 // Make an increment statement.
1899 Statement::make_inc_statement(Expression
* expr
)
1901 return new Inc_dec_statement(true, expr
);
1904 // Make a decrement statement.
1907 Statement::make_dec_statement(Expression
* expr
)
1909 return new Inc_dec_statement(false, expr
);
1912 // Class Thunk_statement. This is the base class for go and defer
1917 Thunk_statement::Thunk_statement(Statement_classification classification
,
1918 Call_expression
* call
,
1920 : Statement(classification
, location
),
1921 call_(call
), struct_type_(NULL
)
1925 // Return whether this is a simple statement which does not require a
1929 Thunk_statement::is_simple(Function_type
* fntype
) const
1931 // We need a thunk to call a method, or to pass a variable number of
1933 if (fntype
->is_method() || fntype
->is_varargs())
1936 // A defer statement requires a thunk to set up for whether the
1937 // function can call recover.
1938 if (this->classification() == STATEMENT_DEFER
)
1941 // We can only permit a single parameter of pointer type.
1942 const Typed_identifier_list
* parameters
= fntype
->parameters();
1943 if (parameters
!= NULL
1944 && (parameters
->size() > 1
1945 || (parameters
->size() == 1
1946 && parameters
->begin()->type()->points_to() == NULL
)))
1949 // If the function returns multiple values, or returns a type other
1950 // than integer, floating point, or pointer, then it may get a
1951 // hidden first parameter, in which case we need the more
1952 // complicated approach. This is true even though we are going to
1953 // ignore the return value.
1954 const Typed_identifier_list
* results
= fntype
->results();
1956 && (results
->size() > 1
1957 || (results
->size() == 1
1958 && !results
->begin()->type()->is_basic_type()
1959 && results
->begin()->type()->points_to() == NULL
)))
1962 // If this calls something that is not a simple function, then we
1964 Expression
* fn
= this->call_
->call_expression()->fn();
1965 if (fn
->func_expression() == NULL
)
1968 // If the function uses a closure, then we need a thunk. FIXME: We
1969 // could accept a zero argument function with a closure.
1970 if (fn
->func_expression()->closure() != NULL
)
1976 // Traverse a thunk statement.
1979 Thunk_statement::do_traverse(Traverse
* traverse
)
1981 return this->traverse_expression(traverse
, &this->call_
);
1984 // We implement traverse_assignment for a thunk statement because it
1985 // effectively copies the function call.
1988 Thunk_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
1990 Expression
* fn
= this->call_
->call_expression()->fn();
1991 Expression
* fn2
= fn
;
1992 tassign
->value(&fn2
, true, false);
1996 // Determine types in a thunk statement.
1999 Thunk_statement::do_determine_types()
2001 this->call_
->determine_type_no_context();
2003 // Now that we know the types of the call, build the struct used to
2005 Call_expression
* ce
= this->call_
->call_expression();
2008 Function_type
* fntype
= ce
->get_function_type();
2009 if (fntype
!= NULL
&& !this->is_simple(fntype
))
2010 this->struct_type_
= this->build_struct(fntype
);
2013 // Check types in a thunk statement.
2016 Thunk_statement::do_check_types(Gogo
*)
2018 if (!this->call_
->discarding_value())
2020 Call_expression
* ce
= this->call_
->call_expression();
2023 if (!this->call_
->is_error_expression())
2024 this->report_error("expected call expression");
2029 // The Traverse class used to find and simplify thunk statements.
2031 class Simplify_thunk_traverse
: public Traverse
2034 Simplify_thunk_traverse(Gogo
* gogo
)
2035 : Traverse(traverse_functions
| traverse_blocks
),
2036 gogo_(gogo
), function_(NULL
)
2040 function(Named_object
*);
2048 // The function we are traversing.
2049 Named_object
* function_
;
2052 // Keep track of the current function while looking for thunks.
2055 Simplify_thunk_traverse::function(Named_object
* no
)
2057 go_assert(this->function_
== NULL
);
2058 this->function_
= no
;
2059 int t
= no
->func_value()->traverse(this);
2060 this->function_
= NULL
;
2061 if (t
== TRAVERSE_EXIT
)
2063 return TRAVERSE_SKIP_COMPONENTS
;
2066 // Look for thunks in a block.
2069 Simplify_thunk_traverse::block(Block
* b
)
2071 // The parser ensures that thunk statements always appear at the end
2073 if (b
->statements()->size() < 1)
2074 return TRAVERSE_CONTINUE
;
2075 Thunk_statement
* stat
= b
->statements()->back()->thunk_statement();
2077 return TRAVERSE_CONTINUE
;
2078 if (stat
->simplify_statement(this->gogo_
, this->function_
, b
))
2079 return TRAVERSE_SKIP_COMPONENTS
;
2080 return TRAVERSE_CONTINUE
;
2083 // Simplify all thunk statements.
2086 Gogo::simplify_thunk_statements()
2088 Simplify_thunk_traverse
thunk_traverse(this);
2089 this->traverse(&thunk_traverse
);
2092 // Return true if the thunk function is a constant, which means that
2093 // it does not need to be passed to the thunk routine.
2096 Thunk_statement::is_constant_function() const
2098 Call_expression
* ce
= this->call_
->call_expression();
2099 Function_type
* fntype
= ce
->get_function_type();
2102 go_assert(saw_errors());
2105 if (fntype
->is_builtin())
2107 Expression
* fn
= ce
->fn();
2108 if (fn
->func_expression() != NULL
)
2109 return fn
->func_expression()->closure() == NULL
;
2110 if (fn
->interface_field_reference_expression() != NULL
)
2115 // Simplify complex thunk statements into simple ones. A complicated
2116 // thunk statement is one which takes anything other than zero
2117 // parameters or a single pointer parameter. We rewrite it into code
2118 // which allocates a struct, stores the parameter values into the
2119 // struct, and does a simple go or defer statement which passes the
2120 // struct to a thunk. The thunk does the real call.
2123 Thunk_statement::simplify_statement(Gogo
* gogo
, Named_object
* function
,
2126 if (this->classification() == STATEMENT_ERROR
)
2128 if (this->call_
->is_error_expression())
2131 if (this->classification() == STATEMENT_DEFER
)
2133 // Make sure that the defer stack exists for the function. We
2134 // will use when converting this statement to the backend
2135 // representation, but we want it to exist when we start
2136 // converting the function.
2137 function
->func_value()->defer_stack(this->location());
2140 Call_expression
* ce
= this->call_
->call_expression();
2141 Function_type
* fntype
= ce
->get_function_type();
2144 go_assert(saw_errors());
2145 this->set_is_error();
2148 if (this->is_simple(fntype
))
2151 Expression
* fn
= ce
->fn();
2152 Interface_field_reference_expression
* interface_method
=
2153 fn
->interface_field_reference_expression();
2155 Location location
= this->location();
2157 std::string thunk_name
= Gogo::thunk_name();
2160 this->build_thunk(gogo
, thunk_name
);
2162 // Generate code to call the thunk.
2164 // Get the values to store into the struct which is the single
2165 // argument to the thunk.
2167 Expression_list
* vals
= new Expression_list();
2168 if (!this->is_constant_function())
2169 vals
->push_back(fn
);
2171 if (interface_method
!= NULL
)
2172 vals
->push_back(interface_method
->expr());
2174 if (ce
->args() != NULL
)
2176 for (Expression_list::const_iterator p
= ce
->args()->begin();
2177 p
!= ce
->args()->end();
2179 vals
->push_back(*p
);
2182 // Build the struct.
2183 Expression
* constructor
=
2184 Expression::make_struct_composite_literal(this->struct_type_
, vals
,
2187 // Allocate the initialized struct on the heap.
2188 constructor
= Expression::make_heap_composite(constructor
, location
);
2190 // Look up the thunk.
2191 Named_object
* named_thunk
= gogo
->lookup(thunk_name
, NULL
);
2192 go_assert(named_thunk
!= NULL
&& named_thunk
->is_function());
2195 Expression
* func
= Expression::make_func_reference(named_thunk
, NULL
,
2197 Expression_list
* params
= new Expression_list();
2198 params
->push_back(constructor
);
2199 Call_expression
* call
= Expression::make_call(func
, params
, false, location
);
2201 // Build the simple go or defer statement.
2203 if (this->classification() == STATEMENT_GO
)
2204 s
= Statement::make_go_statement(call
, location
);
2205 else if (this->classification() == STATEMENT_DEFER
)
2206 s
= Statement::make_defer_statement(call
, location
);
2210 // The current block should end with the go statement.
2211 go_assert(block
->statements()->size() >= 1);
2212 go_assert(block
->statements()->back() == this);
2213 block
->replace_statement(block
->statements()->size() - 1, s
);
2215 // We already ran the determine_types pass, so we need to run it now
2216 // for the new statement.
2217 s
->determine_types();
2220 gogo
->check_types_in_block(block
);
2222 // Return true to tell the block not to keep looking at statements.
2226 // Set the name to use for thunk parameter N.
2229 Thunk_statement::thunk_field_param(int n
, char* buf
, size_t buflen
)
2231 snprintf(buf
, buflen
, "a%d", n
);
2234 // Build a new struct type to hold the parameters for a complicated
2235 // thunk statement. FNTYPE is the type of the function call.
2238 Thunk_statement::build_struct(Function_type
* fntype
)
2240 Location location
= this->location();
2242 Struct_field_list
* fields
= new Struct_field_list();
2244 Call_expression
* ce
= this->call_
->call_expression();
2245 Expression
* fn
= ce
->fn();
2247 if (!this->is_constant_function())
2249 // The function to call.
2250 fields
->push_back(Struct_field(Typed_identifier("fn", fntype
,
2254 // If this thunk statement calls a method on an interface, we pass
2255 // the interface object to the thunk.
2256 Interface_field_reference_expression
* interface_method
=
2257 fn
->interface_field_reference_expression();
2258 if (interface_method
!= NULL
)
2260 Typed_identifier
tid("object", interface_method
->expr()->type(),
2262 fields
->push_back(Struct_field(tid
));
2265 // The predeclared recover function has no argument. However, we
2266 // add an argument when building recover thunks. Handle that here.
2267 if (ce
->is_recover_call())
2269 fields
->push_back(Struct_field(Typed_identifier("can_recover",
2270 Type::lookup_bool_type(),
2274 const Expression_list
* args
= ce
->args();
2278 for (Expression_list::const_iterator p
= args
->begin();
2283 this->thunk_field_param(i
, buf
, sizeof buf
);
2284 fields
->push_back(Struct_field(Typed_identifier(buf
, (*p
)->type(),
2289 return Type::make_struct_type(fields
, location
);
2292 // Build the thunk we are going to call. This is a brand new, albeit
2293 // artificial, function.
2296 Thunk_statement::build_thunk(Gogo
* gogo
, const std::string
& thunk_name
)
2298 Location location
= this->location();
2300 Call_expression
* ce
= this->call_
->call_expression();
2302 bool may_call_recover
= false;
2303 if (this->classification() == STATEMENT_DEFER
)
2305 Func_expression
* fn
= ce
->fn()->func_expression();
2307 may_call_recover
= true;
2310 const Named_object
* no
= fn
->named_object();
2311 if (!no
->is_function())
2312 may_call_recover
= true;
2314 may_call_recover
= no
->func_value()->calls_recover();
2318 // Build the type of the thunk. The thunk takes a single parameter,
2319 // which is a pointer to the special structure we build.
2320 const char* const parameter_name
= "__go_thunk_parameter";
2321 Typed_identifier_list
* thunk_parameters
= new Typed_identifier_list();
2322 Type
* pointer_to_struct_type
= Type::make_pointer_type(this->struct_type_
);
2323 thunk_parameters
->push_back(Typed_identifier(parameter_name
,
2324 pointer_to_struct_type
,
2327 Typed_identifier_list
* thunk_results
= NULL
;
2328 if (may_call_recover
)
2330 // When deferring a function which may call recover, add a
2331 // return value, to disable tail call optimizations which will
2332 // break the way we check whether recover is permitted.
2333 thunk_results
= new Typed_identifier_list();
2334 thunk_results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
2338 Function_type
* thunk_type
= Type::make_function_type(NULL
, thunk_parameters
,
2342 // Start building the thunk.
2343 Named_object
* function
= gogo
->start_function(thunk_name
, thunk_type
, true,
2346 gogo
->start_block(location
);
2348 // For a defer statement, start with a call to
2349 // __go_set_defer_retaddr. */
2350 Label
* retaddr_label
= NULL
;
2351 if (may_call_recover
)
2353 retaddr_label
= gogo
->add_label_reference("retaddr", location
, false);
2354 Expression
* arg
= Expression::make_label_addr(retaddr_label
, location
);
2355 Expression
* call
= Runtime::make_call(Runtime::SET_DEFER_RETADDR
,
2358 // This is a hack to prevent the middle-end from deleting the
2360 gogo
->start_block(location
);
2361 gogo
->add_statement(Statement::make_goto_statement(retaddr_label
,
2363 Block
* then_block
= gogo
->finish_block(location
);
2364 then_block
->determine_types();
2366 Statement
* s
= Statement::make_if_statement(call
, then_block
, NULL
,
2368 s
->determine_types();
2369 gogo
->add_statement(s
);
2372 // Get a reference to the parameter.
2373 Named_object
* named_parameter
= gogo
->lookup(parameter_name
, NULL
);
2374 go_assert(named_parameter
!= NULL
&& named_parameter
->is_variable());
2376 // Build the call. Note that the field names are the same as the
2377 // ones used in build_struct.
2378 Expression
* thunk_parameter
= Expression::make_var_reference(named_parameter
,
2380 thunk_parameter
= Expression::make_unary(OPERATOR_MULT
, thunk_parameter
,
2383 Interface_field_reference_expression
* interface_method
=
2384 ce
->fn()->interface_field_reference_expression();
2386 Expression
* func_to_call
;
2387 unsigned int next_index
;
2388 if (this->is_constant_function())
2390 func_to_call
= ce
->fn();
2395 func_to_call
= Expression::make_field_reference(thunk_parameter
,
2400 if (interface_method
!= NULL
)
2402 // The main program passes the interface object.
2403 go_assert(next_index
== 0);
2404 Expression
* r
= Expression::make_field_reference(thunk_parameter
, 0,
2406 const std::string
& name(interface_method
->name());
2407 func_to_call
= Expression::make_interface_field_reference(r
, name
,
2412 Expression_list
* call_params
= new Expression_list();
2413 const Struct_field_list
* fields
= this->struct_type_
->fields();
2414 Struct_field_list::const_iterator p
= fields
->begin();
2415 for (unsigned int i
= 0; i
< next_index
; ++i
)
2417 bool is_recover_call
= ce
->is_recover_call();
2418 Expression
* recover_arg
= NULL
;
2419 for (; p
!= fields
->end(); ++p
, ++next_index
)
2421 Expression
* thunk_param
= Expression::make_var_reference(named_parameter
,
2423 thunk_param
= Expression::make_unary(OPERATOR_MULT
, thunk_param
,
2425 Expression
* param
= Expression::make_field_reference(thunk_param
,
2428 if (!is_recover_call
)
2429 call_params
->push_back(param
);
2432 go_assert(call_params
->empty());
2433 recover_arg
= param
;
2437 if (call_params
->empty())
2443 Call_expression
* call
= Expression::make_call(func_to_call
, call_params
,
2446 // This call expression was already lowered before entering the
2447 // thunk statement. Don't try to lower varargs again, as that will
2448 // cause confusion for, e.g., method calls which already have a
2449 // receiver parameter.
2450 call
->set_varargs_are_lowered();
2452 Statement
* call_statement
= Statement::make_statement(call
, true);
2454 gogo
->add_statement(call_statement
);
2456 // If this is a defer statement, the label comes immediately after
2458 if (may_call_recover
)
2460 gogo
->add_label_definition("retaddr", location
);
2462 Expression_list
* vals
= new Expression_list();
2463 vals
->push_back(Expression::make_boolean(false, location
));
2464 gogo
->add_statement(Statement::make_return_statement(vals
, location
));
2467 Block
* b
= gogo
->finish_block(location
);
2469 gogo
->add_block(b
, location
);
2471 gogo
->lower_block(function
, b
);
2473 // We already ran the determine_types pass, so we need to run it
2474 // just for the call statement now. The other types are known.
2475 call_statement
->determine_types();
2477 if (may_call_recover
|| recover_arg
!= NULL
)
2479 // Dig up the call expression, which may have been changed
2481 go_assert(call_statement
->classification() == STATEMENT_EXPRESSION
);
2482 Expression_statement
* es
=
2483 static_cast<Expression_statement
*>(call_statement
);
2484 Call_expression
* ce
= es
->expr()->call_expression();
2486 go_assert(saw_errors());
2489 if (may_call_recover
)
2490 ce
->set_is_deferred();
2491 if (recover_arg
!= NULL
)
2492 ce
->set_recover_arg(recover_arg
);
2496 // That is all the thunk has to do.
2497 gogo
->finish_function(location
);
2500 // Get the function and argument expressions.
2503 Thunk_statement::get_fn_and_arg(Expression
** pfn
, Expression
** parg
)
2505 if (this->call_
->is_error_expression())
2508 Call_expression
* ce
= this->call_
->call_expression();
2510 Expression
* fn
= ce
->fn();
2511 Func_expression
* fe
= fn
->func_expression();
2512 go_assert(fe
!= NULL
);
2513 *pfn
= Expression::make_func_code_reference(fe
->named_object(),
2516 const Expression_list
* args
= ce
->args();
2517 if (args
== NULL
|| args
->empty())
2518 *parg
= Expression::make_nil(this->location());
2521 go_assert(args
->size() == 1);
2522 *parg
= args
->front();
2528 // Class Go_statement.
2531 Go_statement::do_get_backend(Translate_context
* context
)
2535 if (!this->get_fn_and_arg(&fn
, &arg
))
2536 return context
->backend()->error_statement();
2538 Expression
* call
= Runtime::make_call(Runtime::GO
, this->location(), 2,
2540 tree call_tree
= call
->get_tree(context
);
2541 Bexpression
* call_bexpr
= tree_to_expr(call_tree
);
2542 return context
->backend()->expression_statement(call_bexpr
);
2545 // Dump the AST representation for go statement.
2548 Go_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2550 ast_dump_context
->print_indent();
2551 ast_dump_context
->ostream() << "go ";
2552 ast_dump_context
->dump_expression(this->call());
2553 ast_dump_context
->ostream() << std::endl
;
2556 // Make a go statement.
2559 Statement::make_go_statement(Call_expression
* call
, Location location
)
2561 return new Go_statement(call
, location
);
2564 // Class Defer_statement.
2567 Defer_statement::do_get_backend(Translate_context
* context
)
2571 if (!this->get_fn_and_arg(&fn
, &arg
))
2572 return context
->backend()->error_statement();
2574 Location loc
= this->location();
2575 Expression
* ds
= context
->function()->func_value()->defer_stack(loc
);
2577 Expression
* call
= Runtime::make_call(Runtime::DEFER
, loc
, 3,
2579 tree call_tree
= call
->get_tree(context
);
2580 Bexpression
* call_bexpr
= tree_to_expr(call_tree
);
2581 return context
->backend()->expression_statement(call_bexpr
);
2584 // Dump the AST representation for defer statement.
2587 Defer_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2589 ast_dump_context
->print_indent();
2590 ast_dump_context
->ostream() << "defer ";
2591 ast_dump_context
->dump_expression(this->call());
2592 ast_dump_context
->ostream() << std::endl
;
2595 // Make a defer statement.
2598 Statement::make_defer_statement(Call_expression
* call
,
2601 return new Defer_statement(call
, location
);
2604 // Class Return_statement.
2606 // Traverse assignments. We treat each return value as a top level
2607 // RHS in an expression.
2610 Return_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
2612 Expression_list
* vals
= this->vals_
;
2615 for (Expression_list::iterator p
= vals
->begin();
2618 tassign
->value(&*p
, true, true);
2623 // Lower a return statement. If we are returning a function call
2624 // which returns multiple values which match the current function,
2625 // split up the call's results. If the return statement lists
2626 // explicit values, implement this statement by assigning the values
2627 // to the result variables and change this statement to a naked
2628 // return. This lets panic/recover work correctly.
2631 Return_statement::do_lower(Gogo
*, Named_object
* function
, Block
* enclosing
,
2632 Statement_inserter
*)
2634 if (this->is_lowered_
)
2637 Expression_list
* vals
= this->vals_
;
2639 this->is_lowered_
= true;
2641 Location loc
= this->location();
2643 size_t vals_count
= vals
== NULL
? 0 : vals
->size();
2644 Function::Results
* results
= function
->func_value()->result_variables();
2645 size_t results_count
= results
== NULL
? 0 : results
->size();
2647 if (vals_count
== 0)
2649 if (results_count
> 0 && !function
->func_value()->results_are_named())
2651 this->report_error(_("not enough arguments to return"));
2657 if (results_count
== 0)
2659 this->report_error(_("return with value in function "
2660 "with no return type"));
2664 // If the current function has multiple return values, and we are
2665 // returning a single call expression, split up the call expression.
2666 if (results_count
> 1
2667 && vals
->size() == 1
2668 && vals
->front()->call_expression() != NULL
)
2670 Call_expression
* call
= vals
->front()->call_expression();
2672 vals
= new Expression_list
;
2673 for (size_t i
= 0; i
< results_count
; ++i
)
2674 vals
->push_back(Expression::make_call_result(call
, i
));
2675 vals_count
= results_count
;
2678 if (vals_count
< results_count
)
2680 this->report_error(_("not enough arguments to return"));
2684 if (vals_count
> results_count
)
2686 this->report_error(_("too many values in return statement"));
2690 Block
* b
= new Block(enclosing
, loc
);
2692 Expression_list
* lhs
= new Expression_list();
2693 Expression_list
* rhs
= new Expression_list();
2695 Expression_list::const_iterator pe
= vals
->begin();
2697 for (Function::Results::const_iterator pr
= results
->begin();
2698 pr
!= results
->end();
2701 Named_object
* rv
= *pr
;
2702 Expression
* e
= *pe
;
2704 // Check types now so that we give a good error message. The
2705 // result type is known. We determine the expression type
2708 Type
*rvtype
= rv
->result_var_value()->type();
2709 Type_context
type_context(rvtype
, false);
2710 e
->determine_type(&type_context
);
2714 if (this->are_hidden_fields_ok_
)
2715 ok
= Type::are_assignable_hidden_ok(rvtype
, e
->type(), &reason
);
2717 ok
= Type::are_assignable(rvtype
, e
->type(), &reason
);
2720 Expression
* ve
= Expression::make_var_reference(rv
, e
->location());
2727 error_at(e
->location(), "incompatible type for return value %d", i
);
2729 error_at(e
->location(),
2730 "incompatible type for return value %d (%s)",
2734 go_assert(lhs
->size() == rhs
->size());
2738 else if (lhs
->size() == 1)
2740 Statement
* s
= Statement::make_assignment(lhs
->front(), rhs
->front(),
2742 if (this->are_hidden_fields_ok_
)
2744 Assignment_statement
* as
= static_cast<Assignment_statement
*>(s
);
2745 as
->set_hidden_fields_are_ok();
2747 b
->add_statement(s
);
2753 Statement
* s
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
2754 if (this->are_hidden_fields_ok_
)
2756 Tuple_assignment_statement
* tas
=
2757 static_cast<Tuple_assignment_statement
*>(s
);
2758 tas
->set_hidden_fields_are_ok();
2760 b
->add_statement(s
);
2763 b
->add_statement(this);
2767 return Statement::make_block_statement(b
, loc
);
2770 // Convert a return statement to the backend representation.
2773 Return_statement::do_get_backend(Translate_context
* context
)
2775 Location loc
= this->location();
2777 Function
* function
= context
->function()->func_value();
2778 tree fndecl
= function
->get_decl();
2780 Function::Results
* results
= function
->result_variables();
2781 std::vector
<Bexpression
*> retvals
;
2782 if (results
!= NULL
&& !results
->empty())
2784 retvals
.reserve(results
->size());
2785 for (Function::Results::const_iterator p
= results
->begin();
2786 p
!= results
->end();
2789 Expression
* vr
= Expression::make_var_reference(*p
, loc
);
2790 retvals
.push_back(tree_to_expr(vr
->get_tree(context
)));
2794 return context
->backend()->return_statement(tree_to_function(fndecl
),
2798 // Dump the AST representation for a return statement.
2801 Return_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2803 ast_dump_context
->print_indent();
2804 ast_dump_context
->ostream() << "return " ;
2805 ast_dump_context
->dump_expression_list(this->vals_
);
2806 ast_dump_context
->ostream() << std::endl
;
2809 // Make a return statement.
2812 Statement::make_return_statement(Expression_list
* vals
,
2815 return new Return_statement(vals
, location
);
2818 // Make a statement that returns the result of a call expression.
2821 Statement::make_return_from_call(Call_expression
* call
, Location location
)
2823 size_t rc
= call
->result_count();
2825 return Statement::make_statement(call
, true);
2828 Expression_list
* vals
= new Expression_list();
2830 vals
->push_back(call
);
2833 for (size_t i
= 0; i
< rc
; ++i
)
2834 vals
->push_back(Expression::make_call_result(call
, i
));
2836 return Statement::make_return_statement(vals
, location
);
2840 // A break or continue statement.
2842 class Bc_statement
: public Statement
2845 Bc_statement(bool is_break
, Unnamed_label
* label
, Location location
)
2846 : Statement(STATEMENT_BREAK_OR_CONTINUE
, location
),
2847 label_(label
), is_break_(is_break
)
2852 { return this->is_break_
; }
2856 do_traverse(Traverse
*)
2857 { return TRAVERSE_CONTINUE
; }
2860 do_may_fall_through() const
2864 do_get_backend(Translate_context
* context
)
2865 { return this->label_
->get_goto(context
, this->location()); }
2868 do_dump_statement(Ast_dump_context
*) const;
2871 // The label that this branches to.
2872 Unnamed_label
* label_
;
2873 // True if this is "break", false if it is "continue".
2877 // Dump the AST representation for a break/continue statement
2880 Bc_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2882 ast_dump_context
->print_indent();
2883 ast_dump_context
->ostream() << (this->is_break_
? "break" : "continue");
2884 if (this->label_
!= NULL
)
2886 ast_dump_context
->ostream() << " ";
2887 ast_dump_context
->dump_label_name(this->label_
);
2889 ast_dump_context
->ostream() << std::endl
;
2892 // Make a break statement.
2895 Statement::make_break_statement(Unnamed_label
* label
, Location location
)
2897 return new Bc_statement(true, label
, location
);
2900 // Make a continue statement.
2903 Statement::make_continue_statement(Unnamed_label
* label
,
2906 return new Bc_statement(false, label
, location
);
2909 // A goto statement.
2911 class Goto_statement
: public Statement
2914 Goto_statement(Label
* label
, Location location
)
2915 : Statement(STATEMENT_GOTO
, location
),
2921 do_traverse(Traverse
*)
2922 { return TRAVERSE_CONTINUE
; }
2925 do_check_types(Gogo
*);
2928 do_may_fall_through() const
2932 do_get_backend(Translate_context
*);
2935 do_dump_statement(Ast_dump_context
*) const;
2941 // Check types for a label. There aren't any types per se, but we use
2942 // this to give an error if the label was never defined.
2945 Goto_statement::do_check_types(Gogo
*)
2947 if (!this->label_
->is_defined())
2949 error_at(this->location(), "reference to undefined label %qs",
2950 Gogo::message_name(this->label_
->name()).c_str());
2951 this->set_is_error();
2955 // Convert the goto statement to the backend representation.
2958 Goto_statement::do_get_backend(Translate_context
* context
)
2960 Blabel
* blabel
= this->label_
->get_backend_label(context
);
2961 return context
->backend()->goto_statement(blabel
, this->location());
2964 // Dump the AST representation for a goto statement.
2967 Goto_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2969 ast_dump_context
->print_indent();
2970 ast_dump_context
->ostream() << "goto " << this->label_
->name() << std::endl
;
2973 // Make a goto statement.
2976 Statement::make_goto_statement(Label
* label
, Location location
)
2978 return new Goto_statement(label
, location
);
2981 // A goto statement to an unnamed label.
2983 class Goto_unnamed_statement
: public Statement
2986 Goto_unnamed_statement(Unnamed_label
* label
, Location location
)
2987 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
2993 do_traverse(Traverse
*)
2994 { return TRAVERSE_CONTINUE
; }
2997 do_may_fall_through() const
3001 do_get_backend(Translate_context
* context
)
3002 { return this->label_
->get_goto(context
, this->location()); }
3005 do_dump_statement(Ast_dump_context
*) const;
3008 Unnamed_label
* label_
;
3011 // Dump the AST representation for an unnamed goto statement
3014 Goto_unnamed_statement::do_dump_statement(
3015 Ast_dump_context
* ast_dump_context
) const
3017 ast_dump_context
->print_indent();
3018 ast_dump_context
->ostream() << "goto ";
3019 ast_dump_context
->dump_label_name(this->label_
);
3020 ast_dump_context
->ostream() << std::endl
;
3023 // Make a goto statement to an unnamed label.
3026 Statement::make_goto_unnamed_statement(Unnamed_label
* label
,
3029 return new Goto_unnamed_statement(label
, location
);
3032 // Class Label_statement.
3037 Label_statement::do_traverse(Traverse
*)
3039 return TRAVERSE_CONTINUE
;
3042 // Return the backend representation of the statement defining this
3046 Label_statement::do_get_backend(Translate_context
* context
)
3048 Blabel
* blabel
= this->label_
->get_backend_label(context
);
3049 return context
->backend()->label_definition_statement(blabel
);
3052 // Dump the AST for a label definition statement.
3055 Label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3057 ast_dump_context
->print_indent();
3058 ast_dump_context
->ostream() << this->label_
->name() << ":" << std::endl
;
3061 // Make a label statement.
3064 Statement::make_label_statement(Label
* label
, Location location
)
3066 return new Label_statement(label
, location
);
3069 // An unnamed label statement.
3071 class Unnamed_label_statement
: public Statement
3074 Unnamed_label_statement(Unnamed_label
* label
)
3075 : Statement(STATEMENT_UNNAMED_LABEL
, label
->location()),
3081 do_traverse(Traverse
*)
3082 { return TRAVERSE_CONTINUE
; }
3085 do_get_backend(Translate_context
* context
)
3086 { return this->label_
->get_definition(context
); }
3089 do_dump_statement(Ast_dump_context
*) const;
3093 Unnamed_label
* label_
;
3096 // Dump the AST representation for an unnamed label definition statement.
3099 Unnamed_label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3102 ast_dump_context
->print_indent();
3103 ast_dump_context
->dump_label_name(this->label_
);
3104 ast_dump_context
->ostream() << ":" << std::endl
;
3107 // Make an unnamed label statement.
3110 Statement::make_unnamed_label_statement(Unnamed_label
* label
)
3112 return new Unnamed_label_statement(label
);
3117 class If_statement
: public Statement
3120 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
3122 : Statement(STATEMENT_IF
, location
),
3123 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
3128 do_traverse(Traverse
*);
3131 do_determine_types();
3134 do_check_types(Gogo
*);
3137 do_may_fall_through() const;
3140 do_get_backend(Translate_context
*);
3143 do_dump_statement(Ast_dump_context
*) const;
3154 If_statement::do_traverse(Traverse
* traverse
)
3156 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
3157 || this->then_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3158 return TRAVERSE_EXIT
;
3159 if (this->else_block_
!= NULL
)
3161 if (this->else_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3162 return TRAVERSE_EXIT
;
3164 return TRAVERSE_CONTINUE
;
3168 If_statement::do_determine_types()
3170 Type_context
context(Type::lookup_bool_type(), false);
3171 this->cond_
->determine_type(&context
);
3172 this->then_block_
->determine_types();
3173 if (this->else_block_
!= NULL
)
3174 this->else_block_
->determine_types();
3180 If_statement::do_check_types(Gogo
*)
3182 Type
* type
= this->cond_
->type();
3183 if (type
->is_error())
3184 this->set_is_error();
3185 else if (!type
->is_boolean_type())
3186 this->report_error(_("expected boolean expression"));
3189 // Whether the overall statement may fall through.
3192 If_statement::do_may_fall_through() const
3194 return (this->else_block_
== NULL
3195 || this->then_block_
->may_fall_through()
3196 || this->else_block_
->may_fall_through());
3199 // Get the backend representation.
3202 If_statement::do_get_backend(Translate_context
* context
)
3204 go_assert(this->cond_
->type()->is_boolean_type()
3205 || this->cond_
->type()->is_error());
3206 tree cond_tree
= this->cond_
->get_tree(context
);
3207 Bexpression
* cond_expr
= tree_to_expr(cond_tree
);
3208 Bblock
* then_block
= this->then_block_
->get_backend(context
);
3209 Bblock
* else_block
= (this->else_block_
== NULL
3211 : this->else_block_
->get_backend(context
));
3212 return context
->backend()->if_statement(cond_expr
, then_block
,
3213 else_block
, this->location());
3216 // Dump the AST representation for an if statement
3219 If_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3221 ast_dump_context
->print_indent();
3222 ast_dump_context
->ostream() << "if ";
3223 ast_dump_context
->dump_expression(this->cond_
);
3224 ast_dump_context
->ostream() << std::endl
;
3225 if (ast_dump_context
->dump_subblocks())
3227 ast_dump_context
->dump_block(this->then_block_
);
3228 if (this->else_block_
!= NULL
)
3230 ast_dump_context
->print_indent();
3231 ast_dump_context
->ostream() << "else" << std::endl
;
3232 ast_dump_context
->dump_block(this->else_block_
);
3237 // Make an if statement.
3240 Statement::make_if_statement(Expression
* cond
, Block
* then_block
,
3241 Block
* else_block
, Location location
)
3243 return new If_statement(cond
, then_block
, else_block
, location
);
3246 // Class Case_clauses::Hash_integer_value.
3248 class Case_clauses::Hash_integer_value
3252 operator()(Expression
*) const;
3256 Case_clauses::Hash_integer_value::operator()(Expression
* pe
) const
3258 Numeric_constant nc
;
3260 if (!pe
->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3262 size_t ret
= mpz_get_ui(ival
);
3267 // Class Case_clauses::Eq_integer_value.
3269 class Case_clauses::Eq_integer_value
3273 operator()(Expression
*, Expression
*) const;
3277 Case_clauses::Eq_integer_value::operator()(Expression
* a
, Expression
* b
) const
3279 Numeric_constant anc
;
3281 Numeric_constant bnc
;
3283 if (!a
->numeric_constant_value(&anc
)
3284 || !anc
.to_int(&aval
)
3285 || !b
->numeric_constant_value(&bnc
)
3286 || !bnc
.to_int(&bval
))
3288 bool ret
= mpz_cmp(aval
, bval
) == 0;
3294 // Class Case_clauses::Case_clause.
3299 Case_clauses::Case_clause::traverse(Traverse
* traverse
)
3301 if (this->cases_
!= NULL
3302 && (traverse
->traverse_mask()
3303 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3305 if (this->cases_
->traverse(traverse
) == TRAVERSE_EXIT
)
3306 return TRAVERSE_EXIT
;
3308 if (this->statements_
!= NULL
)
3310 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
3311 return TRAVERSE_EXIT
;
3313 return TRAVERSE_CONTINUE
;
3316 // Check whether all the case expressions are integer constants.
3319 Case_clauses::Case_clause::is_constant() const
3321 if (this->cases_
!= NULL
)
3323 for (Expression_list::const_iterator p
= this->cases_
->begin();
3324 p
!= this->cases_
->end();
3326 if (!(*p
)->is_constant() || (*p
)->type()->integer_type() == NULL
)
3332 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3333 // value we are switching on; it may be NULL. If START_LABEL is not
3334 // NULL, it goes at the start of the statements, after the condition
3335 // test. We branch to FINISH_LABEL at the end of the statements.
3338 Case_clauses::Case_clause::lower(Block
* b
, Temporary_statement
* val_temp
,
3339 Unnamed_label
* start_label
,
3340 Unnamed_label
* finish_label
) const
3342 Location loc
= this->location_
;
3343 Unnamed_label
* next_case_label
;
3344 if (this->cases_
== NULL
|| this->cases_
->empty())
3346 go_assert(this->is_default_
);
3347 next_case_label
= NULL
;
3351 Expression
* cond
= NULL
;
3353 for (Expression_list::const_iterator p
= this->cases_
->begin();
3354 p
!= this->cases_
->end();
3357 Expression
* ref
= Expression::make_temporary_reference(val_temp
,
3359 Expression
* this_cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3364 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, this_cond
, loc
);
3367 Block
* then_block
= new Block(b
, loc
);
3368 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3369 Statement
* s
= Statement::make_goto_unnamed_statement(next_case_label
,
3371 then_block
->add_statement(s
);
3373 // if !COND { goto NEXT_CASE_LABEL }
3374 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3375 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
3376 b
->add_statement(s
);
3379 if (start_label
!= NULL
)
3380 b
->add_statement(Statement::make_unnamed_label_statement(start_label
));
3382 if (this->statements_
!= NULL
)
3383 b
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
3385 Statement
* s
= Statement::make_goto_unnamed_statement(finish_label
, loc
);
3386 b
->add_statement(s
);
3388 if (next_case_label
!= NULL
)
3389 b
->add_statement(Statement::make_unnamed_label_statement(next_case_label
));
3395 Case_clauses::Case_clause::determine_types(Type
* type
)
3397 if (this->cases_
!= NULL
)
3399 Type_context
case_context(type
, false);
3400 for (Expression_list::iterator p
= this->cases_
->begin();
3401 p
!= this->cases_
->end();
3403 (*p
)->determine_type(&case_context
);
3405 if (this->statements_
!= NULL
)
3406 this->statements_
->determine_types();
3409 // Check types. Returns false if there was an error.
3412 Case_clauses::Case_clause::check_types(Type
* type
)
3414 if (this->cases_
!= NULL
)
3416 for (Expression_list::iterator p
= this->cases_
->begin();
3417 p
!= this->cases_
->end();
3420 if (!Type::are_assignable(type
, (*p
)->type(), NULL
)
3421 && !Type::are_assignable((*p
)->type(), type
, NULL
))
3423 error_at((*p
)->location(),
3424 "type mismatch between switch value and case clause");
3432 // Return true if this clause may fall through to the following
3433 // statements. Note that this is not the same as whether the case
3434 // uses the "fallthrough" keyword.
3437 Case_clauses::Case_clause::may_fall_through() const
3439 if (this->statements_
== NULL
)
3441 return this->statements_
->may_fall_through();
3444 // Convert the case values and statements to the backend
3445 // representation. BREAK_LABEL is the label which break statements
3446 // should branch to. CASE_CONSTANTS is used to detect duplicate
3447 // constants. *CASES should be passed as an empty vector; the values
3448 // for this case will be added to it. If this is the default case,
3449 // *CASES will remain empty. This returns the statement to execute if
3450 // one of these cases is selected.
3453 Case_clauses::Case_clause::get_backend(Translate_context
* context
,
3454 Unnamed_label
* break_label
,
3455 Case_constants
* case_constants
,
3456 std::vector
<Bexpression
*>* cases
) const
3458 if (this->cases_
!= NULL
)
3460 go_assert(!this->is_default_
);
3461 for (Expression_list::const_iterator p
= this->cases_
->begin();
3462 p
!= this->cases_
->end();
3466 if (e
->classification() != Expression::EXPRESSION_INTEGER
)
3468 Numeric_constant nc
;
3470 if (!(*p
)->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3472 // Something went wrong. This can happen with a
3473 // negative constant and an unsigned switch value.
3474 go_assert(saw_errors());
3477 go_assert(nc
.type() != NULL
);
3478 e
= Expression::make_integer(&ival
, nc
.type(), e
->location());
3482 std::pair
<Case_constants::iterator
, bool> ins
=
3483 case_constants
->insert(e
);
3486 // Value was already present.
3487 error_at(this->location_
, "duplicate case in switch");
3488 e
= Expression::make_error(this->location_
);
3491 tree case_tree
= e
->get_tree(context
);
3492 Bexpression
* case_expr
= tree_to_expr(case_tree
);
3493 cases
->push_back(case_expr
);
3497 Bstatement
* statements
;
3498 if (this->statements_
== NULL
)
3502 Bblock
* bblock
= this->statements_
->get_backend(context
);
3503 statements
= context
->backend()->block_statement(bblock
);
3506 Bstatement
* break_stat
;
3507 if (this->is_fallthrough_
)
3510 break_stat
= break_label
->get_goto(context
, this->location_
);
3512 if (statements
== NULL
)
3514 else if (break_stat
== NULL
)
3517 return context
->backend()->compound_statement(statements
, break_stat
);
3520 // Dump the AST representation for a case clause
3523 Case_clauses::Case_clause::dump_clause(Ast_dump_context
* ast_dump_context
)
3526 ast_dump_context
->print_indent();
3527 if (this->is_default_
)
3529 ast_dump_context
->ostream() << "default:";
3533 ast_dump_context
->ostream() << "case ";
3534 ast_dump_context
->dump_expression_list(this->cases_
);
3535 ast_dump_context
->ostream() << ":" ;
3537 ast_dump_context
->dump_block(this->statements_
);
3538 if (this->is_fallthrough_
)
3540 ast_dump_context
->print_indent();
3541 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
3545 // Class Case_clauses.
3550 Case_clauses::traverse(Traverse
* traverse
)
3552 for (Clauses::iterator p
= this->clauses_
.begin();
3553 p
!= this->clauses_
.end();
3556 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
3557 return TRAVERSE_EXIT
;
3559 return TRAVERSE_CONTINUE
;
3562 // Check whether all the case expressions are constant.
3565 Case_clauses::is_constant() const
3567 for (Clauses::const_iterator p
= this->clauses_
.begin();
3568 p
!= this->clauses_
.end();
3570 if (!p
->is_constant())
3575 // Lower case clauses for a nonconstant switch.
3578 Case_clauses::lower(Block
* b
, Temporary_statement
* val_temp
,
3579 Unnamed_label
* break_label
) const
3581 // The default case.
3582 const Case_clause
* default_case
= NULL
;
3584 // The label for the fallthrough of the previous case.
3585 Unnamed_label
* last_fallthrough_label
= NULL
;
3587 // The label for the start of the default case. This is used if the
3588 // case before the default case falls through.
3589 Unnamed_label
* default_start_label
= NULL
;
3591 // The label for the end of the default case. This normally winds
3592 // up as BREAK_LABEL, but it will be different if the default case
3594 Unnamed_label
* default_finish_label
= NULL
;
3596 for (Clauses::const_iterator p
= this->clauses_
.begin();
3597 p
!= this->clauses_
.end();
3600 // The label to use for the start of the statements for this
3601 // case. This is NULL unless the previous case falls through.
3602 Unnamed_label
* start_label
= last_fallthrough_label
;
3604 // The label to jump to after the end of the statements for this
3606 Unnamed_label
* finish_label
= break_label
;
3608 last_fallthrough_label
= NULL
;
3609 if (p
->is_fallthrough() && p
+ 1 != this->clauses_
.end())
3611 finish_label
= new Unnamed_label(p
->location());
3612 last_fallthrough_label
= finish_label
;
3615 if (!p
->is_default())
3616 p
->lower(b
, val_temp
, start_label
, finish_label
);
3619 // We have to move the default case to the end, so that we
3620 // only use it if all the other tests fail.
3622 default_start_label
= start_label
;
3623 default_finish_label
= finish_label
;
3627 if (default_case
!= NULL
)
3628 default_case
->lower(b
, val_temp
, default_start_label
,
3629 default_finish_label
);
3635 Case_clauses::determine_types(Type
* type
)
3637 for (Clauses::iterator p
= this->clauses_
.begin();
3638 p
!= this->clauses_
.end();
3640 p
->determine_types(type
);
3643 // Check types. Returns false if there was an error.
3646 Case_clauses::check_types(Type
* type
)
3649 for (Clauses::iterator p
= this->clauses_
.begin();
3650 p
!= this->clauses_
.end();
3653 if (!p
->check_types(type
))
3659 // Return true if these clauses may fall through to the statements
3660 // following the switch statement.
3663 Case_clauses::may_fall_through() const
3665 bool found_default
= false;
3666 for (Clauses::const_iterator p
= this->clauses_
.begin();
3667 p
!= this->clauses_
.end();
3670 if (p
->may_fall_through() && !p
->is_fallthrough())
3672 if (p
->is_default())
3673 found_default
= true;
3675 return !found_default
;
3678 // Convert the cases to the backend representation. This sets
3679 // *ALL_CASES and *ALL_STATEMENTS.
3682 Case_clauses::get_backend(Translate_context
* context
,
3683 Unnamed_label
* break_label
,
3684 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
3685 std::vector
<Bstatement
*>* all_statements
) const
3687 Case_constants case_constants
;
3689 size_t c
= this->clauses_
.size();
3690 all_cases
->resize(c
);
3691 all_statements
->resize(c
);
3694 for (Clauses::const_iterator p
= this->clauses_
.begin();
3695 p
!= this->clauses_
.end();
3698 std::vector
<Bexpression
*> cases
;
3699 Bstatement
* stat
= p
->get_backend(context
, break_label
, &case_constants
,
3701 (*all_cases
)[i
].swap(cases
);
3702 (*all_statements
)[i
] = stat
;
3706 // Dump the AST representation for case clauses (from a switch statement)
3709 Case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
3711 for (Clauses::const_iterator p
= this->clauses_
.begin();
3712 p
!= this->clauses_
.end();
3714 p
->dump_clause(ast_dump_context
);
3717 // A constant switch statement. A Switch_statement is lowered to this
3718 // when all the cases are constants.
3720 class Constant_switch_statement
: public Statement
3723 Constant_switch_statement(Expression
* val
, Case_clauses
* clauses
,
3724 Unnamed_label
* break_label
,
3726 : Statement(STATEMENT_CONSTANT_SWITCH
, location
),
3727 val_(val
), clauses_(clauses
), break_label_(break_label
)
3732 do_traverse(Traverse
*);
3735 do_determine_types();
3738 do_check_types(Gogo
*);
3741 do_get_backend(Translate_context
*);
3744 do_dump_statement(Ast_dump_context
*) const;
3747 // The value to switch on.
3749 // The case clauses.
3750 Case_clauses
* clauses_
;
3751 // The break label, if needed.
3752 Unnamed_label
* break_label_
;
3758 Constant_switch_statement::do_traverse(Traverse
* traverse
)
3760 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3761 return TRAVERSE_EXIT
;
3762 return this->clauses_
->traverse(traverse
);
3768 Constant_switch_statement::do_determine_types()
3770 this->val_
->determine_type_no_context();
3771 this->clauses_
->determine_types(this->val_
->type());
3777 Constant_switch_statement::do_check_types(Gogo
*)
3779 if (!this->clauses_
->check_types(this->val_
->type()))
3780 this->set_is_error();
3783 // Convert to GENERIC.
3786 Constant_switch_statement::do_get_backend(Translate_context
* context
)
3788 tree switch_val_tree
= this->val_
->get_tree(context
);
3789 Bexpression
* switch_val_expr
= tree_to_expr(switch_val_tree
);
3791 Unnamed_label
* break_label
= this->break_label_
;
3792 if (break_label
== NULL
)
3793 break_label
= new Unnamed_label(this->location());
3795 std::vector
<std::vector
<Bexpression
*> > all_cases
;
3796 std::vector
<Bstatement
*> all_statements
;
3797 this->clauses_
->get_backend(context
, break_label
, &all_cases
,
3800 Bstatement
* switch_statement
;
3801 switch_statement
= context
->backend()->switch_statement(switch_val_expr
,
3805 Bstatement
* ldef
= break_label
->get_definition(context
);
3806 return context
->backend()->compound_statement(switch_statement
, ldef
);
3809 // Dump the AST representation for a constant switch statement.
3812 Constant_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3815 ast_dump_context
->print_indent();
3816 ast_dump_context
->ostream() << "switch ";
3817 ast_dump_context
->dump_expression(this->val_
);
3819 if (ast_dump_context
->dump_subblocks())
3821 ast_dump_context
->ostream() << " {" << std::endl
;
3822 this->clauses_
->dump_clauses(ast_dump_context
);
3823 ast_dump_context
->ostream() << "}";
3826 ast_dump_context
->ostream() << std::endl
;
3829 // Class Switch_statement.
3834 Switch_statement::do_traverse(Traverse
* traverse
)
3836 if (this->val_
!= NULL
)
3838 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3839 return TRAVERSE_EXIT
;
3841 return this->clauses_
->traverse(traverse
);
3844 // Lower a Switch_statement to a Constant_switch_statement or a series
3845 // of if statements.
3848 Switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
3849 Statement_inserter
*)
3851 Location loc
= this->location();
3853 if (this->val_
!= NULL
3854 && (this->val_
->is_error_expression()
3855 || this->val_
->type()->is_error()))
3856 return Statement::make_error_statement(loc
);
3858 if (this->val_
!= NULL
3859 && this->val_
->type()->integer_type() != NULL
3860 && !this->clauses_
->empty()
3861 && this->clauses_
->is_constant())
3862 return new Constant_switch_statement(this->val_
, this->clauses_
,
3863 this->break_label_
, loc
);
3865 if (this->val_
!= NULL
3866 && !this->val_
->type()->is_comparable()
3867 && !Type::are_compatible_for_comparison(true, this->val_
->type(),
3868 Type::make_nil_type(), NULL
))
3870 error_at(this->val_
->location(),
3871 "cannot switch on value whose type that may not be compared");
3872 return Statement::make_error_statement(loc
);
3875 Block
* b
= new Block(enclosing
, loc
);
3877 if (this->clauses_
->empty())
3879 Expression
* val
= this->val_
;
3881 val
= Expression::make_boolean(true, loc
);
3882 return Statement::make_statement(val
, true);
3885 // var val_temp VAL_TYPE = VAL
3886 Expression
* val
= this->val_
;
3888 val
= Expression::make_boolean(true, loc
);
3889 Temporary_statement
* val_temp
= Statement::make_temporary(NULL
, val
, loc
);
3890 b
->add_statement(val_temp
);
3892 this->clauses_
->lower(b
, val_temp
, this->break_label());
3894 Statement
* s
= Statement::make_unnamed_label_statement(this->break_label_
);
3895 b
->add_statement(s
);
3897 return Statement::make_block_statement(b
, loc
);
3900 // Return the break label for this switch statement, creating it if
3904 Switch_statement::break_label()
3906 if (this->break_label_
== NULL
)
3907 this->break_label_
= new Unnamed_label(this->location());
3908 return this->break_label_
;
3911 // Dump the AST representation for a switch statement.
3914 Switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3916 ast_dump_context
->print_indent();
3917 ast_dump_context
->ostream() << "switch ";
3918 if (this->val_
!= NULL
)
3920 ast_dump_context
->dump_expression(this->val_
);
3922 if (ast_dump_context
->dump_subblocks())
3924 ast_dump_context
->ostream() << " {" << std::endl
;
3925 this->clauses_
->dump_clauses(ast_dump_context
);
3926 ast_dump_context
->print_indent();
3927 ast_dump_context
->ostream() << "}";
3929 ast_dump_context
->ostream() << std::endl
;
3932 // Return whether this switch may fall through.
3935 Switch_statement::do_may_fall_through() const
3937 if (this->clauses_
== NULL
)
3940 // If we have a break label, then some case needed it. That implies
3941 // that the switch statement as a whole can fall through.
3942 if (this->break_label_
!= NULL
)
3945 return this->clauses_
->may_fall_through();
3948 // Make a switch statement.
3951 Statement::make_switch_statement(Expression
* val
, Location location
)
3953 return new Switch_statement(val
, location
);
3956 // Class Type_case_clauses::Type_case_clause.
3961 Type_case_clauses::Type_case_clause::traverse(Traverse
* traverse
)
3963 if (!this->is_default_
3964 && ((traverse
->traverse_mask()
3965 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3966 && Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3967 return TRAVERSE_EXIT
;
3968 if (this->statements_
!= NULL
)
3969 return this->statements_
->traverse(traverse
);
3970 return TRAVERSE_CONTINUE
;
3973 // Lower one clause in a type switch. Add statements to the block B.
3974 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3975 // BREAK_LABEL is the label at the end of the type switch.
3976 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3980 Type_case_clauses::Type_case_clause::lower(Type
* switch_val_type
,
3982 Temporary_statement
* descriptor_temp
,
3983 Unnamed_label
* break_label
,
3984 Unnamed_label
** stmts_label
) const
3986 Location loc
= this->location_
;
3988 Unnamed_label
* next_case_label
= NULL
;
3989 if (!this->is_default_
)
3991 Type
* type
= this->type_
;
3994 if (switch_val_type
->interface_type() != NULL
3995 && !type
->is_nil_constant_as_type()
3996 && type
->interface_type() == NULL
3997 && !switch_val_type
->interface_type()->implements_interface(type
,
4001 error_at(this->location_
, "impossible type switch case");
4003 error_at(this->location_
, "impossible type switch case (%s)",
4007 Expression
* ref
= Expression::make_temporary_reference(descriptor_temp
,
4011 // The language permits case nil, which is of course a constant
4012 // rather than a type. It will appear here as an invalid
4014 if (type
->is_nil_constant_as_type())
4015 cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
4016 Expression::make_nil(loc
),
4019 cond
= Runtime::make_call((type
->interface_type() == NULL
4020 ? Runtime::IFACETYPEEQ
4021 : Runtime::IFACEI2TP
),
4023 Expression::make_type_descriptor(type
, loc
),
4026 Unnamed_label
* dest
;
4027 if (!this->is_fallthrough_
)
4029 // if !COND { goto NEXT_CASE_LABEL }
4030 next_case_label
= new Unnamed_label(Linemap::unknown_location());
4031 dest
= next_case_label
;
4032 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
4036 // if COND { goto STMTS_LABEL }
4037 go_assert(stmts_label
!= NULL
);
4038 if (*stmts_label
== NULL
)
4039 *stmts_label
= new Unnamed_label(Linemap::unknown_location());
4040 dest
= *stmts_label
;
4042 Block
* then_block
= new Block(b
, loc
);
4043 Statement
* s
= Statement::make_goto_unnamed_statement(dest
, loc
);
4044 then_block
->add_statement(s
);
4045 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
4046 b
->add_statement(s
);
4049 if (this->statements_
!= NULL
4050 || (!this->is_fallthrough_
4051 && stmts_label
!= NULL
4052 && *stmts_label
!= NULL
))
4054 go_assert(!this->is_fallthrough_
);
4055 if (stmts_label
!= NULL
&& *stmts_label
!= NULL
)
4057 go_assert(!this->is_default_
);
4058 if (this->statements_
!= NULL
)
4059 (*stmts_label
)->set_location(this->statements_
->start_location());
4060 Statement
* s
= Statement::make_unnamed_label_statement(*stmts_label
);
4061 b
->add_statement(s
);
4062 *stmts_label
= NULL
;
4064 if (this->statements_
!= NULL
)
4065 b
->add_statement(Statement::make_block_statement(this->statements_
,
4069 if (this->is_fallthrough_
)
4070 go_assert(next_case_label
== NULL
);
4073 Location gloc
= (this->statements_
== NULL
4075 : this->statements_
->end_location());
4076 b
->add_statement(Statement::make_goto_unnamed_statement(break_label
,
4078 if (next_case_label
!= NULL
)
4081 Statement::make_unnamed_label_statement(next_case_label
);
4082 b
->add_statement(s
);
4087 // Return true if this type clause may fall through to the statements
4088 // following the switch.
4091 Type_case_clauses::Type_case_clause::may_fall_through() const
4093 if (this->statements_
== NULL
)
4095 return this->statements_
->may_fall_through();
4098 // Dump the AST representation for a type case clause
4101 Type_case_clauses::Type_case_clause::dump_clause(
4102 Ast_dump_context
* ast_dump_context
) const
4104 ast_dump_context
->print_indent();
4105 if (this->is_default_
)
4107 ast_dump_context
->ostream() << "default:";
4111 ast_dump_context
->ostream() << "case ";
4112 ast_dump_context
->dump_type(this->type_
);
4113 ast_dump_context
->ostream() << ":" ;
4115 ast_dump_context
->dump_block(this->statements_
);
4116 if (this->is_fallthrough_
)
4118 ast_dump_context
->print_indent();
4119 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
4123 // Class Type_case_clauses.
4128 Type_case_clauses::traverse(Traverse
* traverse
)
4130 for (Type_clauses::iterator p
= this->clauses_
.begin();
4131 p
!= this->clauses_
.end();
4134 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4135 return TRAVERSE_EXIT
;
4137 return TRAVERSE_CONTINUE
;
4140 // Check for duplicate types.
4143 Type_case_clauses::check_duplicates() const
4145 typedef Unordered_set_hash(const Type
*, Type_hash_identical
,
4146 Type_identical
) Types_seen
;
4147 Types_seen types_seen
;
4148 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4149 p
!= this->clauses_
.end();
4152 Type
* t
= p
->type();
4155 if (t
->is_nil_constant_as_type())
4156 t
= Type::make_nil_type();
4157 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
.insert(t
);
4159 error_at(p
->location(), "duplicate type in switch");
4163 // Lower the clauses in a type switch. Add statements to the block B.
4164 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4165 // BREAK_LABEL is the label at the end of the type switch.
4168 Type_case_clauses::lower(Type
* switch_val_type
, Block
* b
,
4169 Temporary_statement
* descriptor_temp
,
4170 Unnamed_label
* break_label
) const
4172 const Type_case_clause
* default_case
= NULL
;
4174 Unnamed_label
* stmts_label
= NULL
;
4175 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4176 p
!= this->clauses_
.end();
4179 if (!p
->is_default())
4180 p
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4184 // We are generating a series of tests, which means that we
4185 // need to move the default case to the end.
4189 go_assert(stmts_label
== NULL
);
4191 if (default_case
!= NULL
)
4192 default_case
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4196 // Return true if these clauses may fall through to the statements
4197 // following the switch statement.
4200 Type_case_clauses::may_fall_through() const
4202 bool found_default
= false;
4203 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4204 p
!= this->clauses_
.end();
4207 if (p
->may_fall_through())
4209 if (p
->is_default())
4210 found_default
= true;
4212 return !found_default
;
4215 // Dump the AST representation for case clauses (from a switch statement)
4218 Type_case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4220 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4221 p
!= this->clauses_
.end();
4223 p
->dump_clause(ast_dump_context
);
4226 // Class Type_switch_statement.
4231 Type_switch_statement::do_traverse(Traverse
* traverse
)
4233 if (this->var_
== NULL
)
4235 if (this->traverse_expression(traverse
, &this->expr_
) == TRAVERSE_EXIT
)
4236 return TRAVERSE_EXIT
;
4238 if (this->clauses_
!= NULL
)
4239 return this->clauses_
->traverse(traverse
);
4240 return TRAVERSE_CONTINUE
;
4243 // Lower a type switch statement to a series of if statements. The gc
4244 // compiler is able to generate a table in some cases. However, that
4245 // does not work for us because we may have type descriptors in
4246 // different shared libraries, so we can't compare them with simple
4247 // equality testing.
4250 Type_switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
4251 Statement_inserter
*)
4253 const Location loc
= this->location();
4255 if (this->clauses_
!= NULL
)
4256 this->clauses_
->check_duplicates();
4258 Block
* b
= new Block(enclosing
, loc
);
4260 Type
* val_type
= (this->var_
!= NULL
4261 ? this->var_
->var_value()->type()
4262 : this->expr_
->type());
4264 if (val_type
->interface_type() == NULL
)
4266 if (!val_type
->is_error())
4267 this->report_error(_("cannot type switch on non-interface value"));
4268 return Statement::make_error_statement(loc
);
4271 // var descriptor_temp DESCRIPTOR_TYPE
4272 Type
* descriptor_type
= Type::make_type_descriptor_ptr_type();
4273 Temporary_statement
* descriptor_temp
=
4274 Statement::make_temporary(descriptor_type
, NULL
, loc
);
4275 b
->add_statement(descriptor_temp
);
4277 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4279 bool is_empty
= val_type
->interface_type()->is_empty();
4281 if (this->var_
== NULL
)
4284 ref
= Expression::make_var_reference(this->var_
, loc
);
4285 Expression
* call
= Runtime::make_call((is_empty
4286 ? Runtime::EFACETYPE
4287 : Runtime::IFACETYPE
),
4289 Temporary_reference_expression
* lhs
=
4290 Expression::make_temporary_reference(descriptor_temp
, loc
);
4291 lhs
->set_is_lvalue();
4292 Statement
* s
= Statement::make_assignment(lhs
, call
, loc
);
4293 b
->add_statement(s
);
4295 if (this->clauses_
!= NULL
)
4296 this->clauses_
->lower(val_type
, b
, descriptor_temp
, this->break_label());
4298 s
= Statement::make_unnamed_label_statement(this->break_label_
);
4299 b
->add_statement(s
);
4301 return Statement::make_block_statement(b
, loc
);
4304 // Return whether this switch may fall through.
4307 Type_switch_statement::do_may_fall_through() const
4309 if (this->clauses_
== NULL
)
4312 // If we have a break label, then some case needed it. That implies
4313 // that the switch statement as a whole can fall through.
4314 if (this->break_label_
!= NULL
)
4317 return this->clauses_
->may_fall_through();
4320 // Return the break label for this type switch statement, creating it
4324 Type_switch_statement::break_label()
4326 if (this->break_label_
== NULL
)
4327 this->break_label_
= new Unnamed_label(this->location());
4328 return this->break_label_
;
4331 // Dump the AST representation for a type switch statement
4334 Type_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
4337 ast_dump_context
->print_indent();
4338 ast_dump_context
->ostream() << "switch " << this->var_
->name() << " = ";
4339 ast_dump_context
->dump_expression(this->expr_
);
4340 ast_dump_context
->ostream() << " .(type)";
4341 if (ast_dump_context
->dump_subblocks())
4343 ast_dump_context
->ostream() << " {" << std::endl
;
4344 this->clauses_
->dump_clauses(ast_dump_context
);
4345 ast_dump_context
->ostream() << "}";
4347 ast_dump_context
->ostream() << std::endl
;
4350 // Make a type switch statement.
4352 Type_switch_statement
*
4353 Statement::make_type_switch_statement(Named_object
* var
, Expression
* expr
,
4356 return new Type_switch_statement(var
, expr
, location
);
4359 // Class Send_statement.
4364 Send_statement::do_traverse(Traverse
* traverse
)
4366 if (this->traverse_expression(traverse
, &this->channel_
) == TRAVERSE_EXIT
)
4367 return TRAVERSE_EXIT
;
4368 return this->traverse_expression(traverse
, &this->val_
);
4374 Send_statement::do_determine_types()
4376 this->channel_
->determine_type_no_context();
4377 Type
* type
= this->channel_
->type();
4378 Type_context context
;
4379 if (type
->channel_type() != NULL
)
4380 context
.type
= type
->channel_type()->element_type();
4381 this->val_
->determine_type(&context
);
4387 Send_statement::do_check_types(Gogo
*)
4389 Type
* type
= this->channel_
->type();
4390 if (type
->is_error())
4392 this->set_is_error();
4395 Channel_type
* channel_type
= type
->channel_type();
4396 if (channel_type
== NULL
)
4398 error_at(this->location(), "left operand of %<<-%> must be channel");
4399 this->set_is_error();
4402 Type
* element_type
= channel_type
->element_type();
4403 if (!Type::are_assignable(element_type
, this->val_
->type(), NULL
))
4405 this->report_error(_("incompatible types in send"));
4408 if (!channel_type
->may_send())
4410 this->report_error(_("invalid send on receive-only channel"));
4415 // Convert a send statement to the backend representation.
4418 Send_statement::do_get_backend(Translate_context
* context
)
4420 Location loc
= this->location();
4422 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
4423 Type
* element_type
= channel_type
->element_type();
4424 Expression
* val
= Expression::make_cast(element_type
, this->val_
, loc
);
4427 bool can_take_address
;
4428 switch (element_type
->base()->classification())
4430 case Type::TYPE_BOOLEAN
:
4431 case Type::TYPE_INTEGER
:
4432 case Type::TYPE_FUNCTION
:
4433 case Type::TYPE_POINTER
:
4434 case Type::TYPE_MAP
:
4435 case Type::TYPE_CHANNEL
:
4437 can_take_address
= false;
4440 case Type::TYPE_FLOAT
:
4441 case Type::TYPE_COMPLEX
:
4442 case Type::TYPE_STRING
:
4443 case Type::TYPE_INTERFACE
:
4445 can_take_address
= false;
4448 case Type::TYPE_STRUCT
:
4450 can_take_address
= true;
4453 case Type::TYPE_ARRAY
:
4455 can_take_address
= !element_type
->is_slice_type();
4459 case Type::TYPE_ERROR
:
4460 case Type::TYPE_VOID
:
4461 case Type::TYPE_SINK
:
4462 case Type::TYPE_NIL
:
4463 case Type::TYPE_NAMED
:
4464 case Type::TYPE_FORWARD
:
4465 go_assert(saw_errors());
4466 return context
->backend()->error_statement();
4469 // Only try to take the address of a variable. We have already
4470 // moved variables to the heap, so this should not cause that to
4471 // happen unnecessarily.
4472 if (can_take_address
4473 && val
->var_expression() == NULL
4474 && val
->temporary_reference_expression() == NULL
)
4475 can_take_address
= false;
4477 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
4480 Runtime::Function code
;
4481 Bstatement
* btemp
= NULL
;
4484 // Type is small enough to handle as uint64.
4485 code
= Runtime::SEND_SMALL
;
4486 val
= Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
4489 else if (can_take_address
)
4491 // Must pass address of value. The function doesn't change the
4492 // value, so just take its address directly.
4493 code
= Runtime::SEND_BIG
;
4494 val
= Expression::make_unary(OPERATOR_AND
, val
, loc
);
4498 // Must pass address of value, but the value is small enough
4499 // that it might be in registers. Copy value into temporary
4500 // variable to take address.
4501 code
= Runtime::SEND_BIG
;
4502 Temporary_statement
* temp
= Statement::make_temporary(element_type
,
4504 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
4505 val
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
4506 btemp
= temp
->get_backend(context
);
4509 Expression
* call
= Runtime::make_call(code
, loc
, 3, td
, this->channel_
, val
);
4511 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4512 Bexpression
* bcall
= tree_to_expr(call
->get_tree(context
));
4513 Bstatement
* s
= context
->backend()->expression_statement(bcall
);
4518 return context
->backend()->compound_statement(btemp
, s
);
4521 // Dump the AST representation for a send statement
4524 Send_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4526 ast_dump_context
->print_indent();
4527 ast_dump_context
->dump_expression(this->channel_
);
4528 ast_dump_context
->ostream() << " <- ";
4529 ast_dump_context
->dump_expression(this->val_
);
4530 ast_dump_context
->ostream() << std::endl
;
4533 // Make a send statement.
4536 Statement::make_send_statement(Expression
* channel
, Expression
* val
,
4539 return new Send_statement(channel
, val
, location
);
4542 // Class Select_clauses::Select_clause.
4547 Select_clauses::Select_clause::traverse(Traverse
* traverse
)
4549 if (!this->is_lowered_
4550 && (traverse
->traverse_mask()
4551 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
4553 if (this->channel_
!= NULL
)
4555 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
4556 return TRAVERSE_EXIT
;
4558 if (this->val_
!= NULL
)
4560 if (Expression::traverse(&this->val_
, traverse
) == TRAVERSE_EXIT
)
4561 return TRAVERSE_EXIT
;
4563 if (this->closed_
!= NULL
)
4565 if (Expression::traverse(&this->closed_
, traverse
) == TRAVERSE_EXIT
)
4566 return TRAVERSE_EXIT
;
4569 if (this->statements_
!= NULL
)
4571 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
4572 return TRAVERSE_EXIT
;
4574 return TRAVERSE_CONTINUE
;
4577 // Lowering. We call a function to register this clause, and arrange
4578 // to set any variables in any receive clause.
4581 Select_clauses::Select_clause::lower(Gogo
* gogo
, Named_object
* function
,
4582 Block
* b
, Temporary_statement
* sel
)
4584 Location loc
= this->location_
;
4586 Expression
* selref
= Expression::make_temporary_reference(sel
, loc
);
4589 mpz_init_set_ui(ival
, this->index_
);
4590 Expression
* index_expr
= Expression::make_integer(&ival
, NULL
, loc
);
4593 if (this->is_default_
)
4595 go_assert(this->channel_
== NULL
&& this->val_
== NULL
);
4596 this->lower_default(b
, selref
, index_expr
);
4597 this->is_lowered_
= true;
4601 // Evaluate the channel before the select statement.
4602 Temporary_statement
* channel_temp
= Statement::make_temporary(NULL
,
4605 b
->add_statement(channel_temp
);
4606 Expression
* chanref
= Expression::make_temporary_reference(channel_temp
,
4610 this->lower_send(b
, selref
, chanref
, index_expr
);
4612 this->lower_recv(gogo
, function
, b
, selref
, chanref
, index_expr
);
4614 // Now all references should be handled through the statements, not
4616 this->is_lowered_
= true;
4621 // Lower a default clause in a select statement.
4624 Select_clauses::Select_clause::lower_default(Block
* b
, Expression
* selref
,
4625 Expression
* index_expr
)
4627 Location loc
= this->location_
;
4628 Expression
* call
= Runtime::make_call(Runtime::SELECTDEFAULT
, loc
, 2, selref
,
4630 b
->add_statement(Statement::make_statement(call
, true));
4633 // Lower a send clause in a select statement.
4636 Select_clauses::Select_clause::lower_send(Block
* b
, Expression
* selref
,
4637 Expression
* chanref
,
4638 Expression
* index_expr
)
4640 Location loc
= this->location_
;
4642 Channel_type
* ct
= this->channel_
->type()->channel_type();
4646 Type
* valtype
= ct
->element_type();
4648 // Note that copying the value to a temporary here means that we
4649 // evaluate the send values in the required order.
4650 Temporary_statement
* val
= Statement::make_temporary(valtype
, this->val_
,
4652 b
->add_statement(val
);
4654 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4655 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4657 Expression
* call
= Runtime::make_call(Runtime::SELECTSEND
, loc
, 4, selref
,
4658 chanref
, valaddr
, index_expr
);
4659 b
->add_statement(Statement::make_statement(call
, true));
4662 // Lower a receive clause in a select statement.
4665 Select_clauses::Select_clause::lower_recv(Gogo
* gogo
, Named_object
* function
,
4666 Block
* b
, Expression
* selref
,
4667 Expression
* chanref
,
4668 Expression
* index_expr
)
4670 Location loc
= this->location_
;
4672 Channel_type
* ct
= this->channel_
->type()->channel_type();
4676 Type
* valtype
= ct
->element_type();
4677 Temporary_statement
* val
= Statement::make_temporary(valtype
, NULL
, loc
);
4678 b
->add_statement(val
);
4680 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4681 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4683 Temporary_statement
* closed_temp
= NULL
;
4686 if (this->closed_
== NULL
&& this->closedvar_
== NULL
)
4687 call
= Runtime::make_call(Runtime::SELECTRECV
, loc
, 4, selref
, chanref
,
4688 valaddr
, index_expr
);
4691 closed_temp
= Statement::make_temporary(Type::lookup_bool_type(), NULL
,
4693 b
->add_statement(closed_temp
);
4694 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4696 Expression
* caddr
= Expression::make_unary(OPERATOR_AND
, cref
, loc
);
4697 call
= Runtime::make_call(Runtime::SELECTRECV2
, loc
, 5, selref
, chanref
,
4698 valaddr
, caddr
, index_expr
);
4701 b
->add_statement(Statement::make_statement(call
, true));
4703 // If the block of statements is executed, arrange for the received
4704 // value to move from VAL to the place where the statements expect
4709 if (this->var_
!= NULL
)
4711 go_assert(this->val_
== NULL
);
4712 valref
= Expression::make_temporary_reference(val
, loc
);
4713 this->var_
->var_value()->set_init(valref
);
4714 this->var_
->var_value()->clear_type_from_chan_element();
4716 else if (this->val_
!= NULL
&& !this->val_
->is_sink_expression())
4718 init
= new Block(b
, loc
);
4719 valref
= Expression::make_temporary_reference(val
, loc
);
4720 init
->add_statement(Statement::make_assignment(this->val_
, valref
, loc
));
4723 if (this->closedvar_
!= NULL
)
4725 go_assert(this->closed_
== NULL
);
4726 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4728 this->closedvar_
->var_value()->set_init(cref
);
4730 else if (this->closed_
!= NULL
&& !this->closed_
->is_sink_expression())
4733 init
= new Block(b
, loc
);
4734 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4736 init
->add_statement(Statement::make_assignment(this->closed_
, cref
,
4742 gogo
->lower_block(function
, init
);
4744 if (this->statements_
!= NULL
)
4745 init
->add_statement(Statement::make_block_statement(this->statements_
,
4747 this->statements_
= init
;
4754 Select_clauses::Select_clause::determine_types()
4756 go_assert(this->is_lowered_
);
4757 if (this->statements_
!= NULL
)
4758 this->statements_
->determine_types();
4764 Select_clauses::Select_clause::check_types()
4766 if (this->is_default_
)
4769 Channel_type
* ct
= this->channel_
->type()->channel_type();
4772 error_at(this->channel_
->location(), "expected channel");
4776 if (this->is_send_
&& !ct
->may_send())
4777 error_at(this->location(), "invalid send on receive-only channel");
4778 else if (!this->is_send_
&& !ct
->may_receive())
4779 error_at(this->location(), "invalid receive on send-only channel");
4782 // Whether this clause may fall through to the statement which follows
4783 // the overall select statement.
4786 Select_clauses::Select_clause::may_fall_through() const
4788 if (this->statements_
== NULL
)
4790 return this->statements_
->may_fall_through();
4793 // Return the backend representation for the statements to execute.
4796 Select_clauses::Select_clause::get_statements_backend(
4797 Translate_context
* context
)
4799 if (this->statements_
== NULL
)
4801 Bblock
* bblock
= this->statements_
->get_backend(context
);
4802 return context
->backend()->block_statement(bblock
);
4805 // Dump the AST representation for a select case clause
4808 Select_clauses::Select_clause::dump_clause(
4809 Ast_dump_context
* ast_dump_context
) const
4811 ast_dump_context
->print_indent();
4812 if (this->is_default_
)
4814 ast_dump_context
->ostream() << "default:";
4818 ast_dump_context
->ostream() << "case " ;
4821 ast_dump_context
->dump_expression(this->channel_
);
4822 ast_dump_context
->ostream() << " <- " ;
4823 if (this->val_
!= NULL
)
4824 ast_dump_context
->dump_expression(this->val_
);
4828 if (this->val_
!= NULL
)
4829 ast_dump_context
->dump_expression(this->val_
);
4830 if (this->closed_
!= NULL
)
4832 // FIXME: can val_ == NULL and closed_ ! = NULL?
4833 ast_dump_context
->ostream() << " , " ;
4834 ast_dump_context
->dump_expression(this->closed_
);
4836 if (this->closedvar_
!= NULL
|| this->var_
!= NULL
)
4837 ast_dump_context
->ostream() << " := " ;
4839 ast_dump_context
->ostream() << " <- " ;
4840 ast_dump_context
->dump_expression(this->channel_
);
4842 ast_dump_context
->ostream() << ":" ;
4844 ast_dump_context
->dump_block(this->statements_
);
4847 // Class Select_clauses.
4852 Select_clauses::traverse(Traverse
* traverse
)
4854 for (Clauses::iterator p
= this->clauses_
.begin();
4855 p
!= this->clauses_
.end();
4858 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4859 return TRAVERSE_EXIT
;
4861 return TRAVERSE_CONTINUE
;
4864 // Lowering. Here we pull out the channel and the send values, to
4865 // enforce the order of evaluation. We also add explicit send and
4866 // receive statements to the clauses.
4869 Select_clauses::lower(Gogo
* gogo
, Named_object
* function
, Block
* b
,
4870 Temporary_statement
* sel
)
4872 for (Clauses::iterator p
= this->clauses_
.begin();
4873 p
!= this->clauses_
.end();
4875 p
->lower(gogo
, function
, b
, sel
);
4881 Select_clauses::determine_types()
4883 for (Clauses::iterator p
= this->clauses_
.begin();
4884 p
!= this->clauses_
.end();
4886 p
->determine_types();
4892 Select_clauses::check_types()
4894 for (Clauses::iterator p
= this->clauses_
.begin();
4895 p
!= this->clauses_
.end();
4900 // Return whether these select clauses fall through to the statement
4901 // following the overall select statement.
4904 Select_clauses::may_fall_through() const
4906 for (Clauses::const_iterator p
= this->clauses_
.begin();
4907 p
!= this->clauses_
.end();
4909 if (p
->may_fall_through())
4914 // Convert to the backend representation. We have already accumulated
4915 // all the select information. Now we call selectgo, which will
4916 // return the index of the clause to execute.
4919 Select_clauses::get_backend(Translate_context
* context
,
4920 Temporary_statement
* sel
,
4921 Unnamed_label
*break_label
,
4924 size_t count
= this->clauses_
.size();
4925 std::vector
<std::vector
<Bexpression
*> > cases(count
);
4926 std::vector
<Bstatement
*> clauses(count
);
4928 Type
* int32_type
= Type::lookup_integer_type("int32");
4931 for (Clauses::iterator p
= this->clauses_
.begin();
4932 p
!= this->clauses_
.end();
4935 int index
= p
->index();
4937 mpz_init_set_ui(ival
, index
);
4938 Expression
* index_expr
= Expression::make_integer(&ival
, int32_type
,
4941 cases
[i
].push_back(tree_to_expr(index_expr
->get_tree(context
)));
4943 Bstatement
* s
= p
->get_statements_backend(context
);
4944 Location gloc
= (p
->statements() == NULL
4946 : p
->statements()->end_location());
4947 Bstatement
* g
= break_label
->get_goto(context
, gloc
);
4952 clauses
[i
] = context
->backend()->compound_statement(s
, g
);
4955 Expression
* selref
= Expression::make_temporary_reference(sel
, location
);
4956 Expression
* call
= Runtime::make_call(Runtime::SELECTGO
, location
, 1,
4958 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4959 Bexpression
* bcall
= tree_to_expr(call
->get_tree(context
));
4962 return context
->backend()->expression_statement(bcall
);
4964 std::vector
<Bstatement
*> statements
;
4965 statements
.reserve(2);
4967 Bstatement
* switch_stmt
= context
->backend()->switch_statement(bcall
,
4971 statements
.push_back(switch_stmt
);
4973 Bstatement
* ldef
= break_label
->get_definition(context
);
4974 statements
.push_back(ldef
);
4976 return context
->backend()->statement_list(statements
);
4978 // Dump the AST representation for select clauses.
4981 Select_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4983 for (Clauses::const_iterator p
= this->clauses_
.begin();
4984 p
!= this->clauses_
.end();
4986 p
->dump_clause(ast_dump_context
);
4989 // Class Select_statement.
4991 // Return the break label for this switch statement, creating it if
4995 Select_statement::break_label()
4997 if (this->break_label_
== NULL
)
4998 this->break_label_
= new Unnamed_label(this->location());
4999 return this->break_label_
;
5002 // Lower a select statement. This will still return a select
5003 // statement, but it will be modified to implement the order of
5004 // evaluation rules, and to include the send and receive statements as
5005 // explicit statements in the clauses.
5008 Select_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
5009 Block
* enclosing
, Statement_inserter
*)
5011 if (this->is_lowered_
)
5014 Location loc
= this->location();
5016 Block
* b
= new Block(enclosing
, loc
);
5018 go_assert(this->sel_
== NULL
);
5021 mpz_init_set_ui(ival
, this->clauses_
->size());
5022 Expression
* size_expr
= Expression::make_integer(&ival
, NULL
, loc
);
5025 Expression
* call
= Runtime::make_call(Runtime::NEWSELECT
, loc
, 1, size_expr
);
5027 this->sel_
= Statement::make_temporary(NULL
, call
, loc
);
5028 b
->add_statement(this->sel_
);
5030 this->clauses_
->lower(gogo
, function
, b
, this->sel_
);
5031 this->is_lowered_
= true;
5032 b
->add_statement(this);
5034 return Statement::make_block_statement(b
, loc
);
5037 // Whether the select statement itself may fall through to the following
5041 Select_statement::do_may_fall_through() const
5043 // A select statement is terminating if no break statement
5044 // refers to it and all of its clauses are terminating.
5045 if (this->break_label_
!= NULL
)
5047 return this->clauses_
->may_fall_through();
5050 // Return the backend representation for a select statement.
5053 Select_statement::do_get_backend(Translate_context
* context
)
5055 return this->clauses_
->get_backend(context
, this->sel_
, this->break_label(),
5059 // Dump the AST representation for a select statement.
5062 Select_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5064 ast_dump_context
->print_indent();
5065 ast_dump_context
->ostream() << "select";
5066 if (ast_dump_context
->dump_subblocks())
5068 ast_dump_context
->ostream() << " {" << std::endl
;
5069 this->clauses_
->dump_clauses(ast_dump_context
);
5070 ast_dump_context
->ostream() << "}";
5072 ast_dump_context
->ostream() << std::endl
;
5075 // Make a select statement.
5078 Statement::make_select_statement(Location location
)
5080 return new Select_statement(location
);
5083 // Class For_statement.
5088 For_statement::do_traverse(Traverse
* traverse
)
5090 if (this->init_
!= NULL
)
5092 if (this->init_
->traverse(traverse
) == TRAVERSE_EXIT
)
5093 return TRAVERSE_EXIT
;
5095 if (this->cond_
!= NULL
)
5097 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
)
5098 return TRAVERSE_EXIT
;
5100 if (this->post_
!= NULL
)
5102 if (this->post_
->traverse(traverse
) == TRAVERSE_EXIT
)
5103 return TRAVERSE_EXIT
;
5105 return this->statements_
->traverse(traverse
);
5108 // Lower a For_statement into if statements and gotos. Getting rid of
5109 // complex statements make it easier to handle garbage collection.
5112 For_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
5113 Statement_inserter
*)
5116 Location loc
= this->location();
5118 Block
* b
= new Block(enclosing
, this->location());
5119 if (this->init_
!= NULL
)
5121 s
= Statement::make_block_statement(this->init_
,
5122 this->init_
->start_location());
5123 b
->add_statement(s
);
5126 Unnamed_label
* entry
= NULL
;
5127 if (this->cond_
!= NULL
)
5129 entry
= new Unnamed_label(this->location());
5130 b
->add_statement(Statement::make_goto_unnamed_statement(entry
, loc
));
5133 Unnamed_label
* top
= new Unnamed_label(this->location());
5134 b
->add_statement(Statement::make_unnamed_label_statement(top
));
5136 s
= Statement::make_block_statement(this->statements_
,
5137 this->statements_
->start_location());
5138 b
->add_statement(s
);
5140 Location end_loc
= this->statements_
->end_location();
5142 Unnamed_label
* cont
= this->continue_label_
;
5144 b
->add_statement(Statement::make_unnamed_label_statement(cont
));
5146 if (this->post_
!= NULL
)
5148 s
= Statement::make_block_statement(this->post_
,
5149 this->post_
->start_location());
5150 b
->add_statement(s
);
5151 end_loc
= this->post_
->end_location();
5154 if (this->cond_
== NULL
)
5155 b
->add_statement(Statement::make_goto_unnamed_statement(top
, end_loc
));
5158 b
->add_statement(Statement::make_unnamed_label_statement(entry
));
5160 Location cond_loc
= this->cond_
->location();
5161 Block
* then_block
= new Block(b
, cond_loc
);
5162 s
= Statement::make_goto_unnamed_statement(top
, cond_loc
);
5163 then_block
->add_statement(s
);
5165 s
= Statement::make_if_statement(this->cond_
, then_block
, NULL
, cond_loc
);
5166 b
->add_statement(s
);
5169 Unnamed_label
* brk
= this->break_label_
;
5171 b
->add_statement(Statement::make_unnamed_label_statement(brk
));
5173 b
->set_end_location(end_loc
);
5175 return Statement::make_block_statement(b
, loc
);
5178 // Return the break label, creating it if necessary.
5181 For_statement::break_label()
5183 if (this->break_label_
== NULL
)
5184 this->break_label_
= new Unnamed_label(this->location());
5185 return this->break_label_
;
5188 // Return the continue LABEL_EXPR.
5191 For_statement::continue_label()
5193 if (this->continue_label_
== NULL
)
5194 this->continue_label_
= new Unnamed_label(this->location());
5195 return this->continue_label_
;
5198 // Set the break and continue labels a for statement. This is used
5199 // when lowering a for range statement.
5202 For_statement::set_break_continue_labels(Unnamed_label
* break_label
,
5203 Unnamed_label
* continue_label
)
5205 go_assert(this->break_label_
== NULL
&& this->continue_label_
== NULL
);
5206 this->break_label_
= break_label
;
5207 this->continue_label_
= continue_label
;
5210 // Whether the overall statement may fall through.
5213 For_statement::do_may_fall_through() const
5215 // A for loop is terminating if it has no condition and
5216 // no break statement.
5217 if(this->cond_
!= NULL
)
5219 if(this->break_label_
!= NULL
)
5224 // Dump the AST representation for a for statement.
5227 For_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5229 if (this->init_
!= NULL
&& ast_dump_context
->dump_subblocks())
5231 ast_dump_context
->print_indent();
5232 ast_dump_context
->indent();
5233 ast_dump_context
->ostream() << "// INIT " << std::endl
;
5234 ast_dump_context
->dump_block(this->init_
);
5235 ast_dump_context
->unindent();
5237 ast_dump_context
->print_indent();
5238 ast_dump_context
->ostream() << "for ";
5239 if (this->cond_
!= NULL
)
5240 ast_dump_context
->dump_expression(this->cond_
);
5242 if (ast_dump_context
->dump_subblocks())
5244 ast_dump_context
->ostream() << " {" << std::endl
;
5245 ast_dump_context
->dump_block(this->statements_
);
5246 if (this->init_
!= NULL
)
5248 ast_dump_context
->print_indent();
5249 ast_dump_context
->ostream() << "// POST " << std::endl
;
5250 ast_dump_context
->dump_block(this->post_
);
5252 ast_dump_context
->unindent();
5254 ast_dump_context
->print_indent();
5255 ast_dump_context
->ostream() << "}";
5258 ast_dump_context
->ostream() << std::endl
;
5261 // Make a for statement.
5264 Statement::make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
5267 return new For_statement(init
, cond
, post
, location
);
5270 // Class For_range_statement.
5275 For_range_statement::do_traverse(Traverse
* traverse
)
5277 if (this->traverse_expression(traverse
, &this->index_var_
) == TRAVERSE_EXIT
)
5278 return TRAVERSE_EXIT
;
5279 if (this->value_var_
!= NULL
)
5281 if (this->traverse_expression(traverse
, &this->value_var_
)
5283 return TRAVERSE_EXIT
;
5285 if (this->traverse_expression(traverse
, &this->range_
) == TRAVERSE_EXIT
)
5286 return TRAVERSE_EXIT
;
5287 return this->statements_
->traverse(traverse
);
5290 // Lower a for range statement. For simplicity we lower this into a
5291 // for statement, which will then be lowered in turn to goto
5295 For_range_statement::do_lower(Gogo
* gogo
, Named_object
*, Block
* enclosing
,
5296 Statement_inserter
*)
5298 Type
* range_type
= this->range_
->type();
5299 if (range_type
->points_to() != NULL
5300 && range_type
->points_to()->array_type() != NULL
5301 && !range_type
->points_to()->is_slice_type())
5302 range_type
= range_type
->points_to();
5305 Type
* value_type
= NULL
;
5306 if (range_type
->array_type() != NULL
)
5308 index_type
= Type::lookup_integer_type("int");
5309 value_type
= range_type
->array_type()->element_type();
5311 else if (range_type
->is_string_type())
5313 index_type
= Type::lookup_integer_type("int");
5314 value_type
= Type::lookup_integer_type("int32");
5316 else if (range_type
->map_type() != NULL
)
5318 index_type
= range_type
->map_type()->key_type();
5319 value_type
= range_type
->map_type()->val_type();
5321 else if (range_type
->channel_type() != NULL
)
5323 index_type
= range_type
->channel_type()->element_type();
5324 if (this->value_var_
!= NULL
)
5326 if (!this->value_var_
->type()->is_error())
5327 this->report_error(_("too many variables for range clause "
5329 return Statement::make_error_statement(this->location());
5334 this->report_error(_("range clause must have "
5335 "array, slice, string, map, or channel type"));
5336 return Statement::make_error_statement(this->location());
5339 Location loc
= this->location();
5340 Block
* temp_block
= new Block(enclosing
, loc
);
5342 Named_object
* range_object
= NULL
;
5343 Temporary_statement
* range_temp
= NULL
;
5344 Var_expression
* ve
= this->range_
->var_expression();
5346 range_object
= ve
->named_object();
5349 range_temp
= Statement::make_temporary(NULL
, this->range_
, loc
);
5350 temp_block
->add_statement(range_temp
);
5351 this->range_
= NULL
;
5354 Temporary_statement
* index_temp
= Statement::make_temporary(index_type
,
5356 temp_block
->add_statement(index_temp
);
5358 Temporary_statement
* value_temp
= NULL
;
5359 if (this->value_var_
!= NULL
)
5361 value_temp
= Statement::make_temporary(value_type
, NULL
, loc
);
5362 temp_block
->add_statement(value_temp
);
5365 Block
* body
= new Block(temp_block
, loc
);
5372 // Arrange to do a loop appropriate for the type. We will produce
5373 // for INIT ; COND ; POST {
5375 // INDEX = INDEX_TEMP
5376 // VALUE = VALUE_TEMP // If there is a value
5377 // original statements
5380 if (range_type
->is_slice_type())
5381 this->lower_range_slice(gogo
, temp_block
, body
, range_object
, range_temp
,
5382 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5384 else if (range_type
->array_type() != NULL
)
5385 this->lower_range_array(gogo
, temp_block
, body
, range_object
, range_temp
,
5386 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5388 else if (range_type
->is_string_type())
5389 this->lower_range_string(gogo
, temp_block
, body
, range_object
, range_temp
,
5390 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5392 else if (range_type
->map_type() != NULL
)
5393 this->lower_range_map(gogo
, temp_block
, body
, range_object
, range_temp
,
5394 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5396 else if (range_type
->channel_type() != NULL
)
5397 this->lower_range_channel(gogo
, temp_block
, body
, range_object
, range_temp
,
5398 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5403 if (iter_init
!= NULL
)
5404 body
->add_statement(Statement::make_block_statement(iter_init
, loc
));
5407 Expression
* index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5408 if (this->value_var_
== NULL
)
5410 assign
= Statement::make_assignment(this->index_var_
, index_ref
, loc
);
5414 Expression_list
* lhs
= new Expression_list();
5415 lhs
->push_back(this->index_var_
);
5416 lhs
->push_back(this->value_var_
);
5418 Expression_list
* rhs
= new Expression_list();
5419 rhs
->push_back(index_ref
);
5420 rhs
->push_back(Expression::make_temporary_reference(value_temp
, loc
));
5422 assign
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5424 body
->add_statement(assign
);
5426 body
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
5428 body
->set_end_location(this->statements_
->end_location());
5430 For_statement
* loop
= Statement::make_for_statement(init
, cond
, post
,
5432 loop
->add_statements(body
);
5433 loop
->set_break_continue_labels(this->break_label_
, this->continue_label_
);
5435 temp_block
->add_statement(loop
);
5437 return Statement::make_block_statement(temp_block
, loc
);
5440 // Return a reference to the range, which may be in RANGE_OBJECT or in
5444 For_range_statement::make_range_ref(Named_object
* range_object
,
5445 Temporary_statement
* range_temp
,
5448 if (range_object
!= NULL
)
5449 return Expression::make_var_reference(range_object
, loc
);
5451 return Expression::make_temporary_reference(range_temp
, loc
);
5454 // Return a call to the predeclared function FUNCNAME passing a
5455 // reference to the temporary variable ARG.
5458 For_range_statement::call_builtin(Gogo
* gogo
, const char* funcname
,
5462 Named_object
* no
= gogo
->lookup_global(funcname
);
5463 go_assert(no
!= NULL
&& no
->is_function_declaration());
5464 Expression
* func
= Expression::make_func_reference(no
, NULL
, loc
);
5465 Expression_list
* params
= new Expression_list();
5466 params
->push_back(arg
);
5467 return Expression::make_call(func
, params
, false, loc
);
5470 // Lower a for range over an array.
5473 For_range_statement::lower_range_array(Gogo
* gogo
,
5476 Named_object
* range_object
,
5477 Temporary_statement
* range_temp
,
5478 Temporary_statement
* index_temp
,
5479 Temporary_statement
* value_temp
,
5485 Location loc
= this->location();
5487 // The loop we generate:
5488 // len_temp := len(range)
5489 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5490 // value_temp = range[index_temp]
5491 // index = index_temp
5492 // value = value_temp
5498 // len_temp = len(range)
5501 Block
* init
= new Block(enclosing
, loc
);
5503 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5504 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5505 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5507 init
->add_statement(len_temp
);
5510 mpz_init_set_ui(zval
, 0UL);
5511 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5514 Temporary_reference_expression
* tref
=
5515 Expression::make_temporary_reference(index_temp
, loc
);
5516 tref
->set_is_lvalue();
5517 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5518 init
->add_statement(s
);
5523 // index_temp < len_temp
5525 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5526 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5527 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5531 // Set *PITER_INIT to
5532 // value_temp = range[index_temp]
5534 Block
* iter_init
= NULL
;
5535 if (value_temp
!= NULL
)
5537 iter_init
= new Block(body_block
, loc
);
5539 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5540 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5541 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, loc
);
5543 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5544 tref
->set_is_lvalue();
5545 s
= Statement::make_assignment(tref
, index
, loc
);
5547 iter_init
->add_statement(s
);
5549 *piter_init
= iter_init
;
5554 Block
* post
= new Block(enclosing
, loc
);
5555 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5556 tref
->set_is_lvalue();
5557 s
= Statement::make_inc_statement(tref
);
5558 post
->add_statement(s
);
5562 // Lower a for range over a slice.
5565 For_range_statement::lower_range_slice(Gogo
* gogo
,
5568 Named_object
* range_object
,
5569 Temporary_statement
* range_temp
,
5570 Temporary_statement
* index_temp
,
5571 Temporary_statement
* value_temp
,
5577 Location loc
= this->location();
5579 // The loop we generate:
5580 // for_temp := range
5581 // len_temp := len(for_temp)
5582 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5583 // value_temp = for_temp[index_temp]
5584 // index = index_temp
5585 // value = value_temp
5589 // Using for_temp means that we don't need to check bounds when
5590 // fetching range_temp[index_temp].
5593 // range_temp := range
5595 // len_temp = len(range_temp)
5598 Block
* init
= new Block(enclosing
, loc
);
5600 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5601 Temporary_statement
* for_temp
= Statement::make_temporary(NULL
, ref
, loc
);
5602 init
->add_statement(for_temp
);
5604 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5605 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5606 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5608 init
->add_statement(len_temp
);
5611 mpz_init_set_ui(zval
, 0UL);
5612 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5615 Temporary_reference_expression
* tref
=
5616 Expression::make_temporary_reference(index_temp
, loc
);
5617 tref
->set_is_lvalue();
5618 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5619 init
->add_statement(s
);
5624 // index_temp < len_temp
5626 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5627 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5628 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5632 // Set *PITER_INIT to
5633 // value_temp = range[index_temp]
5635 Block
* iter_init
= NULL
;
5636 if (value_temp
!= NULL
)
5638 iter_init
= new Block(body_block
, loc
);
5640 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5641 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5642 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, loc
);
5644 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5645 tref
->set_is_lvalue();
5646 s
= Statement::make_assignment(tref
, index
, loc
);
5648 iter_init
->add_statement(s
);
5650 *piter_init
= iter_init
;
5655 Block
* post
= new Block(enclosing
, loc
);
5656 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5657 tref
->set_is_lvalue();
5658 s
= Statement::make_inc_statement(tref
);
5659 post
->add_statement(s
);
5663 // Lower a for range over a string.
5666 For_range_statement::lower_range_string(Gogo
*,
5669 Named_object
* range_object
,
5670 Temporary_statement
* range_temp
,
5671 Temporary_statement
* index_temp
,
5672 Temporary_statement
* value_temp
,
5678 Location loc
= this->location();
5680 // The loop we generate:
5681 // var next_index_temp int
5682 // for index_temp = 0; ; index_temp = next_index_temp {
5683 // next_index_temp, value_temp = stringiter2(range, index_temp)
5684 // if next_index_temp == 0 {
5687 // index = index_temp
5688 // value = value_temp
5693 // var next_index_temp int
5696 Block
* init
= new Block(enclosing
, loc
);
5698 Temporary_statement
* next_index_temp
=
5699 Statement::make_temporary(index_temp
->type(), NULL
, loc
);
5700 init
->add_statement(next_index_temp
);
5703 mpz_init_set_ui(zval
, 0UL);
5704 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5706 Temporary_reference_expression
* ref
=
5707 Expression::make_temporary_reference(index_temp
, loc
);
5708 ref
->set_is_lvalue();
5709 Statement
* s
= Statement::make_assignment(ref
, zexpr
, loc
);
5711 init
->add_statement(s
);
5714 // The loop has no condition.
5718 // Set *PITER_INIT to
5719 // next_index_temp = runtime.stringiter(range, index_temp)
5721 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
5723 // if next_index_temp == 0 {
5727 Block
* iter_init
= new Block(body_block
, loc
);
5729 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5730 Expression
* p2
= Expression::make_temporary_reference(index_temp
, loc
);
5731 Call_expression
* call
= Runtime::make_call((value_temp
== NULL
5732 ? Runtime::STRINGITER
5733 : Runtime::STRINGITER2
),
5736 if (value_temp
== NULL
)
5738 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5739 ref
->set_is_lvalue();
5740 s
= Statement::make_assignment(ref
, call
, loc
);
5744 Expression_list
* lhs
= new Expression_list();
5746 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5747 ref
->set_is_lvalue();
5748 lhs
->push_back(ref
);
5750 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5751 ref
->set_is_lvalue();
5752 lhs
->push_back(ref
);
5754 Expression_list
* rhs
= new Expression_list();
5755 rhs
->push_back(Expression::make_call_result(call
, 0));
5756 rhs
->push_back(Expression::make_call_result(call
, 1));
5758 s
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5760 iter_init
->add_statement(s
);
5762 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5763 zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5765 Expression
* equals
= Expression::make_binary(OPERATOR_EQEQ
, ref
, zexpr
, loc
);
5767 Block
* then_block
= new Block(iter_init
, loc
);
5768 s
= Statement::make_break_statement(this->break_label(), loc
);
5769 then_block
->add_statement(s
);
5771 s
= Statement::make_if_statement(equals
, then_block
, NULL
, loc
);
5772 iter_init
->add_statement(s
);
5774 *piter_init
= iter_init
;
5777 // index_temp = next_index_temp
5779 Block
* post
= new Block(enclosing
, loc
);
5781 Temporary_reference_expression
* lhs
=
5782 Expression::make_temporary_reference(index_temp
, loc
);
5783 lhs
->set_is_lvalue();
5784 Expression
* rhs
= Expression::make_temporary_reference(next_index_temp
, loc
);
5785 s
= Statement::make_assignment(lhs
, rhs
, loc
);
5787 post
->add_statement(s
);
5791 // Lower a for range over a map.
5794 For_range_statement::lower_range_map(Gogo
*,
5797 Named_object
* range_object
,
5798 Temporary_statement
* range_temp
,
5799 Temporary_statement
* index_temp
,
5800 Temporary_statement
* value_temp
,
5806 Location loc
= this->location();
5808 // The runtime uses a struct to handle ranges over a map. The
5809 // struct is four pointers long. The first pointer is NULL when we
5810 // have completed the iteration.
5812 // The loop we generate:
5813 // var hiter map_iteration_struct
5814 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
5815 // mapiter2(hiter, &index_temp, &value_temp)
5816 // index = index_temp
5817 // value = value_temp
5822 // var hiter map_iteration_struct
5823 // runtime.mapiterinit(range, &hiter)
5825 Block
* init
= new Block(enclosing
, loc
);
5827 Type
* map_iteration_type
= Runtime::map_iteration_type();
5828 Temporary_statement
* hiter
= Statement::make_temporary(map_iteration_type
,
5830 init
->add_statement(hiter
);
5832 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5833 Expression
* ref
= Expression::make_temporary_reference(hiter
, loc
);
5834 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5835 Expression
* call
= Runtime::make_call(Runtime::MAPITERINIT
, loc
, 2, p1
, p2
);
5836 init
->add_statement(Statement::make_statement(call
, true));
5843 ref
= Expression::make_temporary_reference(hiter
, loc
);
5846 mpz_init_set_ui(zval
, 0UL);
5847 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5850 Expression
* index
= Expression::make_index(ref
, zexpr
, NULL
, loc
);
5852 Expression
* ne
= Expression::make_binary(OPERATOR_NOTEQ
, index
,
5853 Expression::make_nil(loc
),
5858 // Set *PITER_INIT to
5859 // mapiter1(hiter, &index_temp)
5861 // mapiter2(hiter, &index_temp, &value_temp)
5863 Block
* iter_init
= new Block(body_block
, loc
);
5865 ref
= Expression::make_temporary_reference(hiter
, loc
);
5866 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5867 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5868 p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5869 if (value_temp
== NULL
)
5870 call
= Runtime::make_call(Runtime::MAPITER1
, loc
, 2, p1
, p2
);
5873 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5874 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5875 call
= Runtime::make_call(Runtime::MAPITER2
, loc
, 3, p1
, p2
, p3
);
5877 iter_init
->add_statement(Statement::make_statement(call
, true));
5879 *piter_init
= iter_init
;
5882 // mapiternext(&hiter)
5884 Block
* post
= new Block(enclosing
, loc
);
5886 ref
= Expression::make_temporary_reference(hiter
, loc
);
5887 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5888 call
= Runtime::make_call(Runtime::MAPITERNEXT
, loc
, 1, p1
);
5889 post
->add_statement(Statement::make_statement(call
, true));
5894 // Lower a for range over a channel.
5897 For_range_statement::lower_range_channel(Gogo
*,
5900 Named_object
* range_object
,
5901 Temporary_statement
* range_temp
,
5902 Temporary_statement
* index_temp
,
5903 Temporary_statement
* value_temp
,
5909 go_assert(value_temp
== NULL
);
5911 Location loc
= this->location();
5913 // The loop we generate:
5915 // index_temp, ok_temp = <-range
5919 // index = index_temp
5923 // We have no initialization code, no condition, and no post code.
5929 // Set *PITER_INIT to
5930 // index_temp, ok_temp = <-range
5935 Block
* iter_init
= new Block(body_block
, loc
);
5937 Temporary_statement
* ok_temp
=
5938 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
5939 iter_init
->add_statement(ok_temp
);
5941 Expression
* cref
= this->make_range_ref(range_object
, range_temp
, loc
);
5942 Temporary_reference_expression
* iref
=
5943 Expression::make_temporary_reference(index_temp
, loc
);
5944 iref
->set_is_lvalue();
5945 Temporary_reference_expression
* oref
=
5946 Expression::make_temporary_reference(ok_temp
, loc
);
5947 oref
->set_is_lvalue();
5948 Statement
* s
= Statement::make_tuple_receive_assignment(iref
, oref
, cref
,
5950 iter_init
->add_statement(s
);
5952 Block
* then_block
= new Block(iter_init
, loc
);
5953 s
= Statement::make_break_statement(this->break_label(), loc
);
5954 then_block
->add_statement(s
);
5956 oref
= Expression::make_temporary_reference(ok_temp
, loc
);
5957 Expression
* cond
= Expression::make_unary(OPERATOR_NOT
, oref
, loc
);
5958 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
5959 iter_init
->add_statement(s
);
5961 *piter_init
= iter_init
;
5964 // Return the break LABEL_EXPR.
5967 For_range_statement::break_label()
5969 if (this->break_label_
== NULL
)
5970 this->break_label_
= new Unnamed_label(this->location());
5971 return this->break_label_
;
5974 // Return the continue LABEL_EXPR.
5977 For_range_statement::continue_label()
5979 if (this->continue_label_
== NULL
)
5980 this->continue_label_
= new Unnamed_label(this->location());
5981 return this->continue_label_
;
5984 // Dump the AST representation for a for range statement.
5987 For_range_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5990 ast_dump_context
->print_indent();
5991 ast_dump_context
->ostream() << "for ";
5992 ast_dump_context
->dump_expression(this->index_var_
);
5993 if (this->value_var_
!= NULL
)
5995 ast_dump_context
->ostream() << ", ";
5996 ast_dump_context
->dump_expression(this->value_var_
);
5999 ast_dump_context
->ostream() << " = range ";
6000 ast_dump_context
->dump_expression(this->range_
);
6001 if (ast_dump_context
->dump_subblocks())
6003 ast_dump_context
->ostream() << " {" << std::endl
;
6005 ast_dump_context
->indent();
6007 ast_dump_context
->dump_block(this->statements_
);
6009 ast_dump_context
->unindent();
6010 ast_dump_context
->print_indent();
6011 ast_dump_context
->ostream() << "}";
6013 ast_dump_context
->ostream() << std::endl
;
6016 // Make a for statement with a range clause.
6018 For_range_statement
*
6019 Statement::make_for_range_statement(Expression
* index_var
,
6020 Expression
* value_var
,
6024 return new For_range_statement(index_var
, value_var
, range
, location
);