1 // statements.cc -- Go frontend statements.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
13 #include "expressions.h"
17 #include "statements.h"
22 Statement::Statement(Statement_classification classification
,
24 : classification_(classification
), location_(location
)
28 Statement::~Statement()
32 // Traverse the tree. The work of walking the components is handled
36 Statement::traverse(Block
* block
, size_t* pindex
, Traverse
* traverse
)
38 if (this->classification_
== STATEMENT_ERROR
)
39 return TRAVERSE_CONTINUE
;
41 unsigned int traverse_mask
= traverse
->traverse_mask();
43 if ((traverse_mask
& Traverse::traverse_statements
) != 0)
45 int t
= traverse
->statement(block
, pindex
, this);
46 if (t
== TRAVERSE_EXIT
)
48 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
49 return TRAVERSE_CONTINUE
;
52 // No point in checking traverse_mask here--a statement may contain
53 // other blocks or statements, and if we got here we always want to
55 return this->do_traverse(traverse
);
58 // Traverse the contents of a statement.
61 Statement::traverse_contents(Traverse
* traverse
)
63 return this->do_traverse(traverse
);
66 // Traverse assignments.
69 Statement::traverse_assignments(Traverse_assignments
* tassign
)
71 if (this->classification_
== STATEMENT_ERROR
)
73 return this->do_traverse_assignments(tassign
);
76 // Traverse an expression in a statement. This is a helper function
80 Statement::traverse_expression(Traverse
* traverse
, Expression
** expr
)
82 if ((traverse
->traverse_mask()
83 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
84 return TRAVERSE_CONTINUE
;
85 return Expression::traverse(expr
, traverse
);
88 // Traverse an expression list in a statement. This is a helper
89 // function for child classes.
92 Statement::traverse_expression_list(Traverse
* traverse
,
93 Expression_list
* expr_list
)
95 if (expr_list
== NULL
)
96 return TRAVERSE_CONTINUE
;
97 if ((traverse
->traverse_mask()
98 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
99 return TRAVERSE_CONTINUE
;
100 return expr_list
->traverse(traverse
);
103 // Traverse a type in a statement. This is a helper function for
107 Statement::traverse_type(Traverse
* traverse
, Type
* type
)
109 if ((traverse
->traverse_mask()
110 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
111 return TRAVERSE_CONTINUE
;
112 return Type::traverse(type
, traverse
);
115 // Set type information for unnamed constants. This is really done by
119 Statement::determine_types()
121 this->do_determine_types();
124 // If this is a thunk statement, return it.
127 Statement::thunk_statement()
129 Thunk_statement
* ret
= this->convert
<Thunk_statement
, STATEMENT_GO
>();
131 ret
= this->convert
<Thunk_statement
, STATEMENT_DEFER
>();
135 // Convert a Statement to the backend representation. This is really
136 // done by the child class.
139 Statement::get_backend(Translate_context
* context
)
141 if (this->classification_
== STATEMENT_ERROR
)
142 return context
->backend()->error_statement();
143 return this->do_get_backend(context
);
146 // Dump AST representation for a statement to a dump context.
149 Statement::dump_statement(Ast_dump_context
* ast_dump_context
) const
151 this->do_dump_statement(ast_dump_context
);
154 // Note that this statement is erroneous. This is called by children
155 // when they discover an error.
158 Statement::set_is_error()
160 this->classification_
= STATEMENT_ERROR
;
163 // For children to call to report an error conveniently.
166 Statement::report_error(const char* msg
)
168 error_at(this->location_
, "%s", msg
);
169 this->set_is_error();
172 // An error statement, used to avoid crashing after we report an
175 class Error_statement
: public Statement
178 Error_statement(Location location
)
179 : Statement(STATEMENT_ERROR
, location
)
184 do_traverse(Traverse
*)
185 { return TRAVERSE_CONTINUE
; }
188 do_get_backend(Translate_context
*)
189 { go_unreachable(); }
192 do_dump_statement(Ast_dump_context
*) const;
195 // Dump the AST representation for an error statement.
198 Error_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
200 ast_dump_context
->print_indent();
201 ast_dump_context
->ostream() << "Error statement" << std::endl
;
204 // Make an error statement.
207 Statement::make_error_statement(Location location
)
209 return new Error_statement(location
);
212 // Class Variable_declaration_statement.
214 Variable_declaration_statement::Variable_declaration_statement(
216 : Statement(STATEMENT_VARIABLE_DECLARATION
, var
->var_value()->location()),
221 // We don't actually traverse the variable here; it was traversed
222 // while traversing the Block.
225 Variable_declaration_statement::do_traverse(Traverse
*)
227 return TRAVERSE_CONTINUE
;
230 // Traverse the assignments in a variable declaration. Note that this
231 // traversal is different from the usual traversal.
234 Variable_declaration_statement::do_traverse_assignments(
235 Traverse_assignments
* tassign
)
237 tassign
->initialize_variable(this->var_
);
241 // Lower the variable's initialization expression.
244 Variable_declaration_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
245 Block
*, Statement_inserter
* inserter
)
247 this->var_
->var_value()->lower_init_expression(gogo
, function
, inserter
);
251 // Convert a variable declaration to the backend representation.
254 Variable_declaration_statement::do_get_backend(Translate_context
* context
)
256 Variable
* var
= this->var_
->var_value();
257 Bvariable
* bvar
= this->var_
->get_backend_variable(context
->gogo(),
258 context
->function());
259 tree init
= var
->get_init_tree(context
->gogo(), context
->function());
260 Bexpression
* binit
= init
== NULL
? NULL
: tree_to_expr(init
);
262 if (!var
->is_in_heap())
264 go_assert(binit
!= NULL
);
265 return context
->backend()->init_statement(bvar
, binit
);
268 // Something takes the address of this variable, so the value is
269 // stored in the heap. Initialize it to newly allocated memory
270 // space, and assign the initial value to the new space.
271 Location loc
= this->location();
272 Named_object
* newfn
= context
->gogo()->lookup_global("new");
273 go_assert(newfn
!= NULL
&& newfn
->is_function_declaration());
274 Expression
* func
= Expression::make_func_reference(newfn
, NULL
, loc
);
275 Expression_list
* params
= new Expression_list();
276 params
->push_back(Expression::make_type(var
->type(), loc
));
277 Expression
* call
= Expression::make_call(func
, params
, false, loc
);
278 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
279 Temporary_statement
* temp
= Statement::make_temporary(NULL
, call
, loc
);
280 Bstatement
* btemp
= temp
->get_backend(context
);
282 Bstatement
* set
= NULL
;
285 Expression
* e
= Expression::make_temporary_reference(temp
, loc
);
286 e
= Expression::make_unary(OPERATOR_MULT
, e
, loc
);
287 Bexpression
* be
= tree_to_expr(e
->get_tree(context
));
288 set
= context
->backend()->assignment_statement(be
, binit
, loc
);
291 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
292 Bexpression
* bref
= tree_to_expr(ref
->get_tree(context
));
293 Bstatement
* sinit
= context
->backend()->init_statement(bvar
, bref
);
295 std::vector
<Bstatement
*> stats
;
297 stats
.push_back(btemp
);
299 stats
.push_back(set
);
300 stats
.push_back(sinit
);
301 return context
->backend()->statement_list(stats
);
304 // Dump the AST representation for a variable declaration.
307 Variable_declaration_statement::do_dump_statement(
308 Ast_dump_context
* ast_dump_context
) const
310 ast_dump_context
->print_indent();
312 go_assert(var_
->is_variable());
313 ast_dump_context
->ostream() << "var " << this->var_
->name() << " ";
314 Variable
* var
= this->var_
->var_value();
317 ast_dump_context
->dump_type(var
->type());
318 ast_dump_context
->ostream() << " ";
320 if (var
->init() != NULL
)
322 ast_dump_context
->ostream() << "= ";
323 ast_dump_context
->dump_expression(var
->init());
325 ast_dump_context
->ostream() << std::endl
;
328 // Make a variable declaration.
331 Statement::make_variable_declaration(Named_object
* var
)
333 return new Variable_declaration_statement(var
);
336 // Class Temporary_statement.
338 // Return the type of the temporary variable.
341 Temporary_statement::type() const
343 return this->type_
!= NULL
? this->type_
: this->init_
->type();
349 Temporary_statement::do_traverse(Traverse
* traverse
)
351 if (this->type_
!= NULL
352 && this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
353 return TRAVERSE_EXIT
;
354 if (this->init_
== NULL
)
355 return TRAVERSE_CONTINUE
;
357 return this->traverse_expression(traverse
, &this->init_
);
360 // Traverse assignments.
363 Temporary_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
365 if (this->init_
== NULL
)
367 tassign
->value(&this->init_
, true, true);
374 Temporary_statement::do_determine_types()
376 if (this->type_
!= NULL
&& this->type_
->is_abstract())
377 this->type_
= this->type_
->make_non_abstract_type();
379 if (this->init_
!= NULL
)
381 if (this->type_
== NULL
)
382 this->init_
->determine_type_no_context();
385 Type_context
context(this->type_
, false);
386 this->init_
->determine_type(&context
);
390 if (this->type_
== NULL
)
392 this->type_
= this->init_
->type();
393 go_assert(!this->type_
->is_abstract());
400 Temporary_statement::do_check_types(Gogo
*)
402 if (this->type_
!= NULL
&& this->init_
!= NULL
)
406 if (this->are_hidden_fields_ok_
)
407 ok
= Type::are_assignable_hidden_ok(this->type_
, this->init_
->type(),
410 ok
= Type::are_assignable(this->type_
, this->init_
->type(), &reason
);
414 error_at(this->location(), "incompatible types in assignment");
416 error_at(this->location(), "incompatible types in assignment (%s)",
418 this->set_is_error();
423 // Convert to backend representation.
426 Temporary_statement::do_get_backend(Translate_context
* context
)
428 go_assert(this->bvariable_
== NULL
);
430 // FIXME: Permitting FUNCTION to be NULL here is a temporary measure
431 // until we have a better representation of the init function.
432 Named_object
* function
= context
->function();
433 Bfunction
* bfunction
;
434 if (function
== NULL
)
437 bfunction
= tree_to_function(function
->func_value()->get_decl());
439 Btype
* btype
= this->type()->get_backend(context
->gogo());
442 if (this->init_
== NULL
)
444 else if (this->type_
== NULL
)
445 binit
= tree_to_expr(this->init_
->get_tree(context
));
448 Expression
* init
= Expression::make_cast(this->type_
, this->init_
,
450 context
->gogo()->lower_expression(context
->function(), NULL
, &init
);
451 binit
= tree_to_expr(init
->get_tree(context
));
454 Bstatement
* statement
;
456 context
->backend()->temporary_variable(bfunction
, context
->bblock(),
458 this->is_address_taken_
,
459 this->location(), &statement
);
463 // Return the backend variable.
466 Temporary_statement::get_backend_variable(Translate_context
* context
) const
468 if (this->bvariable_
== NULL
)
470 go_assert(saw_errors());
471 return context
->backend()->error_variable();
473 return this->bvariable_
;
476 // Dump the AST represemtation for a temporary statement
479 Temporary_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
481 ast_dump_context
->print_indent();
482 ast_dump_context
->dump_temp_variable_name(this);
483 if (this->type_
!= NULL
)
485 ast_dump_context
->ostream() << " ";
486 ast_dump_context
->dump_type(this->type_
);
488 if (this->init_
!= NULL
)
490 ast_dump_context
->ostream() << " = ";
491 ast_dump_context
->dump_expression(this->init_
);
493 ast_dump_context
->ostream() << std::endl
;
496 // Make and initialize a temporary variable in BLOCK.
499 Statement::make_temporary(Type
* type
, Expression
* init
,
502 return new Temporary_statement(type
, init
, location
);
505 // An assignment statement.
507 class Assignment_statement
: public Statement
510 Assignment_statement(Expression
* lhs
, Expression
* rhs
,
512 : Statement(STATEMENT_ASSIGNMENT
, location
),
513 lhs_(lhs
), rhs_(rhs
), are_hidden_fields_ok_(false)
516 // Note that it is OK for this assignment statement to set hidden
519 set_hidden_fields_are_ok()
520 { this->are_hidden_fields_ok_
= true; }
524 do_traverse(Traverse
* traverse
);
527 do_traverse_assignments(Traverse_assignments
*);
530 do_determine_types();
533 do_check_types(Gogo
*);
536 do_get_backend(Translate_context
*);
539 do_dump_statement(Ast_dump_context
*) const;
542 // Left hand side--the lvalue.
544 // Right hand side--the rvalue.
546 // True if this statement may set hidden fields in the assignment
547 // statement. This is used for generated method stubs.
548 bool are_hidden_fields_ok_
;
554 Assignment_statement::do_traverse(Traverse
* traverse
)
556 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
557 return TRAVERSE_EXIT
;
558 return this->traverse_expression(traverse
, &this->rhs_
);
562 Assignment_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
564 tassign
->assignment(&this->lhs_
, &this->rhs_
);
568 // Set types for the assignment.
571 Assignment_statement::do_determine_types()
573 this->lhs_
->determine_type_no_context();
574 Type_context
context(this->lhs_
->type(), false);
575 this->rhs_
->determine_type(&context
);
578 // Check types for an assignment.
581 Assignment_statement::do_check_types(Gogo
*)
583 // The left hand side must be either addressable, a map index
584 // expression, or the blank identifier.
585 if (!this->lhs_
->is_addressable()
586 && this->lhs_
->map_index_expression() == NULL
587 && !this->lhs_
->is_sink_expression())
589 if (!this->lhs_
->type()->is_error())
590 this->report_error(_("invalid left hand side of assignment"));
594 Type
* lhs_type
= this->lhs_
->type();
595 Type
* rhs_type
= this->rhs_
->type();
598 if (this->are_hidden_fields_ok_
)
599 ok
= Type::are_assignable_hidden_ok(lhs_type
, rhs_type
, &reason
);
601 ok
= Type::are_assignable(lhs_type
, rhs_type
, &reason
);
605 error_at(this->location(), "incompatible types in assignment");
607 error_at(this->location(), "incompatible types in assignment (%s)",
609 this->set_is_error();
612 if (lhs_type
->is_error() || rhs_type
->is_error())
613 this->set_is_error();
616 // Convert an assignment statement to the backend representation.
619 Assignment_statement::do_get_backend(Translate_context
* context
)
621 tree rhs_tree
= this->rhs_
->get_tree(context
);
622 if (this->lhs_
->is_sink_expression())
623 return context
->backend()->expression_statement(tree_to_expr(rhs_tree
));
624 tree lhs_tree
= this->lhs_
->get_tree(context
);
625 rhs_tree
= Expression::convert_for_assignment(context
, this->lhs_
->type(),
626 this->rhs_
->type(), rhs_tree
,
628 return context
->backend()->assignment_statement(tree_to_expr(lhs_tree
),
629 tree_to_expr(rhs_tree
),
633 // Dump the AST representation for an assignment statement.
636 Assignment_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
639 ast_dump_context
->print_indent();
640 ast_dump_context
->dump_expression(this->lhs_
);
641 ast_dump_context
->ostream() << " = " ;
642 ast_dump_context
->dump_expression(this->rhs_
);
643 ast_dump_context
->ostream() << std::endl
;
646 // Make an assignment statement.
649 Statement::make_assignment(Expression
* lhs
, Expression
* rhs
,
652 return new Assignment_statement(lhs
, rhs
, location
);
655 // The Move_subexpressions class is used to move all top-level
656 // subexpressions of an expression. This is used for things like
657 // index expressions in which we must evaluate the index value before
658 // it can be changed by a multiple assignment.
660 class Move_subexpressions
: public Traverse
663 Move_subexpressions(int skip
, Block
* block
)
664 : Traverse(traverse_expressions
),
665 skip_(skip
), block_(block
)
670 expression(Expression
**);
673 // The number of subexpressions to skip moving. This is used to
674 // avoid moving the array itself, as we only need to move the index.
676 // The block where new temporary variables should be added.
681 Move_subexpressions::expression(Expression
** pexpr
)
685 else if ((*pexpr
)->temporary_reference_expression() == NULL
)
687 Location loc
= (*pexpr
)->location();
688 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
689 this->block_
->add_statement(temp
);
690 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
692 // We only need to move top-level subexpressions.
693 return TRAVERSE_SKIP_COMPONENTS
;
696 // The Move_ordered_evals class is used to find any subexpressions of
697 // an expression that have an evaluation order dependency. It creates
698 // temporary variables to hold them.
700 class Move_ordered_evals
: public Traverse
703 Move_ordered_evals(Block
* block
)
704 : Traverse(traverse_expressions
),
710 expression(Expression
**);
713 // The block where new temporary variables should be added.
718 Move_ordered_evals::expression(Expression
** pexpr
)
720 // We have to look at subexpressions first.
721 if ((*pexpr
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
722 return TRAVERSE_EXIT
;
725 if ((*pexpr
)->must_eval_subexpressions_in_order(&i
))
727 Move_subexpressions
ms(i
, this->block_
);
728 if ((*pexpr
)->traverse_subexpressions(&ms
) == TRAVERSE_EXIT
)
729 return TRAVERSE_EXIT
;
732 if ((*pexpr
)->must_eval_in_order())
734 Location loc
= (*pexpr
)->location();
735 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
736 this->block_
->add_statement(temp
);
737 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
739 return TRAVERSE_SKIP_COMPONENTS
;
742 // An assignment operation statement.
744 class Assignment_operation_statement
: public Statement
747 Assignment_operation_statement(Operator op
, Expression
* lhs
, Expression
* rhs
,
749 : Statement(STATEMENT_ASSIGNMENT_OPERATION
, location
),
750 op_(op
), lhs_(lhs
), rhs_(rhs
)
755 do_traverse(Traverse
*);
758 do_traverse_assignments(Traverse_assignments
*)
759 { go_unreachable(); }
762 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
765 do_get_backend(Translate_context
*)
766 { go_unreachable(); }
769 do_dump_statement(Ast_dump_context
*) const;
772 // The operator (OPERATOR_PLUSEQ, etc.).
783 Assignment_operation_statement::do_traverse(Traverse
* traverse
)
785 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
786 return TRAVERSE_EXIT
;
787 return this->traverse_expression(traverse
, &this->rhs_
);
790 // Lower an assignment operation statement to a regular assignment
794 Assignment_operation_statement::do_lower(Gogo
*, Named_object
*,
795 Block
* enclosing
, Statement_inserter
*)
797 Location loc
= this->location();
799 // We have to evaluate the left hand side expression only once. We
800 // do this by moving out any expression with side effects.
801 Block
* b
= new Block(enclosing
, loc
);
802 Move_ordered_evals
moe(b
);
803 this->lhs_
->traverse_subexpressions(&moe
);
805 Expression
* lval
= this->lhs_
->copy();
810 case OPERATOR_PLUSEQ
:
813 case OPERATOR_MINUSEQ
:
822 case OPERATOR_MULTEQ
:
831 case OPERATOR_LSHIFTEQ
:
832 op
= OPERATOR_LSHIFT
;
834 case OPERATOR_RSHIFTEQ
:
835 op
= OPERATOR_RSHIFT
;
840 case OPERATOR_BITCLEAREQ
:
841 op
= OPERATOR_BITCLEAR
;
847 Expression
* binop
= Expression::make_binary(op
, lval
, this->rhs_
, loc
);
848 Statement
* s
= Statement::make_assignment(this->lhs_
, binop
, loc
);
849 if (b
->statements()->empty())
857 return Statement::make_block_statement(b
, loc
);
861 // Dump the AST representation for an assignment operation statement
864 Assignment_operation_statement::do_dump_statement(
865 Ast_dump_context
* ast_dump_context
) const
867 ast_dump_context
->print_indent();
868 ast_dump_context
->dump_expression(this->lhs_
);
869 ast_dump_context
->dump_operator(this->op_
);
870 ast_dump_context
->dump_expression(this->rhs_
);
871 ast_dump_context
->ostream() << std::endl
;
874 // Make an assignment operation statement.
877 Statement::make_assignment_operation(Operator op
, Expression
* lhs
,
878 Expression
* rhs
, Location location
)
880 return new Assignment_operation_statement(op
, lhs
, rhs
, location
);
883 // A tuple assignment statement. This differs from an assignment
884 // statement in that the right-hand-side expressions are evaluated in
887 class Tuple_assignment_statement
: public Statement
890 Tuple_assignment_statement(Expression_list
* lhs
, Expression_list
* rhs
,
892 : Statement(STATEMENT_TUPLE_ASSIGNMENT
, location
),
893 lhs_(lhs
), rhs_(rhs
), are_hidden_fields_ok_(false)
896 // Note that it is OK for this assignment statement to set hidden
899 set_hidden_fields_are_ok()
900 { this->are_hidden_fields_ok_
= true; }
904 do_traverse(Traverse
* traverse
);
907 do_traverse_assignments(Traverse_assignments
*)
908 { go_unreachable(); }
911 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
914 do_get_backend(Translate_context
*)
915 { go_unreachable(); }
918 do_dump_statement(Ast_dump_context
*) const;
921 // Left hand side--a list of lvalues.
922 Expression_list
* lhs_
;
923 // Right hand side--a list of rvalues.
924 Expression_list
* rhs_
;
925 // True if this statement may set hidden fields in the assignment
926 // statement. This is used for generated method stubs.
927 bool are_hidden_fields_ok_
;
933 Tuple_assignment_statement::do_traverse(Traverse
* traverse
)
935 if (this->traverse_expression_list(traverse
, this->lhs_
) == TRAVERSE_EXIT
)
936 return TRAVERSE_EXIT
;
937 return this->traverse_expression_list(traverse
, this->rhs_
);
940 // Lower a tuple assignment. We use temporary variables to split it
941 // up into a set of single assignments.
944 Tuple_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
947 Location loc
= this->location();
949 Block
* b
= new Block(enclosing
, loc
);
951 // First move out any subexpressions on the left hand side. The
952 // right hand side will be evaluated in the required order anyhow.
953 Move_ordered_evals
moe(b
);
954 for (Expression_list::iterator plhs
= this->lhs_
->begin();
955 plhs
!= this->lhs_
->end();
957 Expression::traverse(&*plhs
, &moe
);
959 std::vector
<Temporary_statement
*> temps
;
960 temps
.reserve(this->lhs_
->size());
962 Expression_list::const_iterator prhs
= this->rhs_
->begin();
963 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
964 plhs
!= this->lhs_
->end();
967 go_assert(prhs
!= this->rhs_
->end());
969 if ((*plhs
)->is_error_expression()
970 || (*plhs
)->type()->is_error()
971 || (*prhs
)->is_error_expression()
972 || (*prhs
)->type()->is_error())
975 if ((*plhs
)->is_sink_expression())
977 b
->add_statement(Statement::make_statement(*prhs
, true));
981 Temporary_statement
* temp
= Statement::make_temporary((*plhs
)->type(),
983 if (this->are_hidden_fields_ok_
)
984 temp
->set_hidden_fields_are_ok();
985 b
->add_statement(temp
);
986 temps
.push_back(temp
);
989 go_assert(prhs
== this->rhs_
->end());
991 prhs
= this->rhs_
->begin();
992 std::vector
<Temporary_statement
*>::const_iterator ptemp
= temps
.begin();
993 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
994 plhs
!= this->lhs_
->end();
997 if ((*plhs
)->is_error_expression()
998 || (*plhs
)->type()->is_error()
999 || (*prhs
)->is_error_expression()
1000 || (*prhs
)->type()->is_error())
1003 if ((*plhs
)->is_sink_expression())
1006 Expression
* ref
= Expression::make_temporary_reference(*ptemp
, loc
);
1007 Statement
* s
= Statement::make_assignment(*plhs
, ref
, loc
);
1008 if (this->are_hidden_fields_ok_
)
1010 Assignment_statement
* as
= static_cast<Assignment_statement
*>(s
);
1011 as
->set_hidden_fields_are_ok();
1013 b
->add_statement(s
);
1016 go_assert(ptemp
== temps
.end() || saw_errors());
1018 return Statement::make_block_statement(b
, loc
);
1021 // Dump the AST representation for a tuple assignment statement.
1024 Tuple_assignment_statement::do_dump_statement(
1025 Ast_dump_context
* ast_dump_context
) const
1027 ast_dump_context
->print_indent();
1028 ast_dump_context
->dump_expression_list(this->lhs_
);
1029 ast_dump_context
->ostream() << " = ";
1030 ast_dump_context
->dump_expression_list(this->rhs_
);
1031 ast_dump_context
->ostream() << std::endl
;
1034 // Make a tuple assignment statement.
1037 Statement::make_tuple_assignment(Expression_list
* lhs
, Expression_list
* rhs
,
1040 return new Tuple_assignment_statement(lhs
, rhs
, location
);
1043 // A tuple assignment from a map index expression.
1046 class Tuple_map_assignment_statement
: public Statement
1049 Tuple_map_assignment_statement(Expression
* val
, Expression
* present
,
1050 Expression
* map_index
,
1052 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT
, location
),
1053 val_(val
), present_(present
), map_index_(map_index
)
1058 do_traverse(Traverse
* traverse
);
1061 do_traverse_assignments(Traverse_assignments
*)
1062 { go_unreachable(); }
1065 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1068 do_get_backend(Translate_context
*)
1069 { go_unreachable(); }
1072 do_dump_statement(Ast_dump_context
*) const;
1075 // Lvalue which receives the value from the map.
1077 // Lvalue which receives whether the key value was present.
1078 Expression
* present_
;
1079 // The map index expression.
1080 Expression
* map_index_
;
1086 Tuple_map_assignment_statement::do_traverse(Traverse
* traverse
)
1088 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1089 || this->traverse_expression(traverse
, &this->present_
) == TRAVERSE_EXIT
)
1090 return TRAVERSE_EXIT
;
1091 return this->traverse_expression(traverse
, &this->map_index_
);
1094 // Lower a tuple map assignment.
1097 Tuple_map_assignment_statement::do_lower(Gogo
*, Named_object
*,
1098 Block
* enclosing
, Statement_inserter
*)
1100 Location loc
= this->location();
1102 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1103 if (map_index
== NULL
)
1105 this->report_error(_("expected map index on right hand side"));
1106 return Statement::make_error_statement(loc
);
1108 Map_type
* map_type
= map_index
->get_map_type();
1109 if (map_type
== NULL
)
1110 return Statement::make_error_statement(loc
);
1112 Block
* b
= new Block(enclosing
, loc
);
1114 // Move out any subexpressions to make sure that functions are
1115 // called in the required order.
1116 Move_ordered_evals
moe(b
);
1117 this->val_
->traverse_subexpressions(&moe
);
1118 this->present_
->traverse_subexpressions(&moe
);
1120 // Copy the key value into a temporary so that we can take its
1121 // address without pushing the value onto the heap.
1123 // var key_temp KEY_TYPE = MAP_INDEX
1124 Temporary_statement
* key_temp
=
1125 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1126 b
->add_statement(key_temp
);
1128 // var val_temp VAL_TYPE
1129 Temporary_statement
* val_temp
=
1130 Statement::make_temporary(map_type
->val_type(), NULL
, loc
);
1131 b
->add_statement(val_temp
);
1133 // var present_temp bool
1134 Temporary_statement
* present_temp
=
1135 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
1136 b
->add_statement(present_temp
);
1138 // present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp, &val_temp)
1139 Expression
* a1
= Expression::make_type_descriptor(map_type
, loc
);
1140 Expression
* a2
= map_index
->map();
1141 Temporary_reference_expression
* ref
=
1142 Expression::make_temporary_reference(key_temp
, loc
);
1143 Expression
* a3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1144 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1145 Expression
* a4
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1146 Expression
* call
= Runtime::make_call(Runtime::MAPACCESS2
, loc
, 4,
1149 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1150 ref
->set_is_lvalue();
1151 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1152 b
->add_statement(s
);
1155 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1156 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1157 b
->add_statement(s
);
1159 // present = present_temp
1160 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1161 s
= Statement::make_assignment(this->present_
, ref
, loc
);
1162 b
->add_statement(s
);
1164 return Statement::make_block_statement(b
, loc
);
1167 // Dump the AST representation for a tuple map assignment statement.
1170 Tuple_map_assignment_statement::do_dump_statement(
1171 Ast_dump_context
* ast_dump_context
) const
1173 ast_dump_context
->print_indent();
1174 ast_dump_context
->dump_expression(this->val_
);
1175 ast_dump_context
->ostream() << ", ";
1176 ast_dump_context
->dump_expression(this->present_
);
1177 ast_dump_context
->ostream() << " = ";
1178 ast_dump_context
->dump_expression(this->map_index_
);
1179 ast_dump_context
->ostream() << std::endl
;
1182 // Make a map assignment statement which returns a pair of values.
1185 Statement::make_tuple_map_assignment(Expression
* val
, Expression
* present
,
1186 Expression
* map_index
,
1189 return new Tuple_map_assignment_statement(val
, present
, map_index
, location
);
1192 // Assign a pair of entries to a map.
1195 class Map_assignment_statement
: public Statement
1198 Map_assignment_statement(Expression
* map_index
,
1199 Expression
* val
, Expression
* should_set
,
1201 : Statement(STATEMENT_MAP_ASSIGNMENT
, location
),
1202 map_index_(map_index
), val_(val
), should_set_(should_set
)
1207 do_traverse(Traverse
* traverse
);
1210 do_traverse_assignments(Traverse_assignments
*)
1211 { go_unreachable(); }
1214 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1217 do_get_backend(Translate_context
*)
1218 { go_unreachable(); }
1221 do_dump_statement(Ast_dump_context
*) const;
1224 // A reference to the map index which should be set or deleted.
1225 Expression
* map_index_
;
1226 // The value to add to the map.
1228 // Whether or not to add the value.
1229 Expression
* should_set_
;
1232 // Traverse a map assignment.
1235 Map_assignment_statement::do_traverse(Traverse
* traverse
)
1237 if (this->traverse_expression(traverse
, &this->map_index_
) == TRAVERSE_EXIT
1238 || this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
1239 return TRAVERSE_EXIT
;
1240 return this->traverse_expression(traverse
, &this->should_set_
);
1243 // Lower a map assignment to a function call.
1246 Map_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
1247 Statement_inserter
*)
1249 Location loc
= this->location();
1251 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1252 if (map_index
== NULL
)
1254 this->report_error(_("expected map index on left hand side"));
1255 return Statement::make_error_statement(loc
);
1257 Map_type
* map_type
= map_index
->get_map_type();
1258 if (map_type
== NULL
)
1259 return Statement::make_error_statement(loc
);
1261 Block
* b
= new Block(enclosing
, loc
);
1263 // Evaluate the map first to get order of evaluation right.
1264 // map_temp := m // we are evaluating m[k] = v, p
1265 Temporary_statement
* map_temp
= Statement::make_temporary(map_type
,
1268 b
->add_statement(map_temp
);
1270 // var key_temp MAP_KEY_TYPE = k
1271 Temporary_statement
* key_temp
=
1272 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1273 b
->add_statement(key_temp
);
1275 // var val_temp MAP_VAL_TYPE = v
1276 Temporary_statement
* val_temp
=
1277 Statement::make_temporary(map_type
->val_type(), this->val_
, loc
);
1278 b
->add_statement(val_temp
);
1280 // var insert_temp bool = p
1281 Temporary_statement
* insert_temp
=
1282 Statement::make_temporary(Type::lookup_bool_type(), this->should_set_
,
1284 b
->add_statement(insert_temp
);
1286 // mapassign2(map_temp, &key_temp, &val_temp, p)
1287 Expression
* p1
= Expression::make_temporary_reference(map_temp
, loc
);
1288 Expression
* ref
= Expression::make_temporary_reference(key_temp
, loc
);
1289 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1290 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1291 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1292 Expression
* p4
= Expression::make_temporary_reference(insert_temp
, loc
);
1293 Expression
* call
= Runtime::make_call(Runtime::MAPASSIGN2
, loc
, 4,
1295 Statement
* s
= Statement::make_statement(call
, true);
1296 b
->add_statement(s
);
1298 return Statement::make_block_statement(b
, loc
);
1301 // Dump the AST representation for a map assignment statement.
1304 Map_assignment_statement::do_dump_statement(
1305 Ast_dump_context
* ast_dump_context
) const
1307 ast_dump_context
->print_indent();
1308 ast_dump_context
->dump_expression(this->map_index_
);
1309 ast_dump_context
->ostream() << " = ";
1310 ast_dump_context
->dump_expression(this->val_
);
1311 ast_dump_context
->ostream() << ", ";
1312 ast_dump_context
->dump_expression(this->should_set_
);
1313 ast_dump_context
->ostream() << std::endl
;
1316 // Make a statement which assigns a pair of entries to a map.
1319 Statement::make_map_assignment(Expression
* map_index
,
1320 Expression
* val
, Expression
* should_set
,
1323 return new Map_assignment_statement(map_index
, val
, should_set
, location
);
1326 // A tuple assignment from a receive statement.
1328 class Tuple_receive_assignment_statement
: public Statement
1331 Tuple_receive_assignment_statement(Expression
* val
, Expression
* closed
,
1332 Expression
* channel
, Location location
)
1333 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
, location
),
1334 val_(val
), closed_(closed
), channel_(channel
)
1339 do_traverse(Traverse
* traverse
);
1342 do_traverse_assignments(Traverse_assignments
*)
1343 { go_unreachable(); }
1346 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1349 do_get_backend(Translate_context
*)
1350 { go_unreachable(); }
1353 do_dump_statement(Ast_dump_context
*) const;
1356 // Lvalue which receives the value from the channel.
1358 // Lvalue which receives whether the channel is closed.
1359 Expression
* closed_
;
1360 // The channel on which we receive the value.
1361 Expression
* channel_
;
1367 Tuple_receive_assignment_statement::do_traverse(Traverse
* traverse
)
1369 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1370 || this->traverse_expression(traverse
, &this->closed_
) == TRAVERSE_EXIT
)
1371 return TRAVERSE_EXIT
;
1372 return this->traverse_expression(traverse
, &this->channel_
);
1375 // Lower to a function call.
1378 Tuple_receive_assignment_statement::do_lower(Gogo
*, Named_object
*,
1380 Statement_inserter
*)
1382 Location loc
= this->location();
1384 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
1385 if (channel_type
== NULL
)
1387 this->report_error(_("expected channel"));
1388 return Statement::make_error_statement(loc
);
1390 if (!channel_type
->may_receive())
1392 this->report_error(_("invalid receive on send-only channel"));
1393 return Statement::make_error_statement(loc
);
1396 Block
* b
= new Block(enclosing
, loc
);
1398 // Make sure that any subexpressions on the left hand side are
1399 // evaluated in the right order.
1400 Move_ordered_evals
moe(b
);
1401 this->val_
->traverse_subexpressions(&moe
);
1402 this->closed_
->traverse_subexpressions(&moe
);
1404 // var val_temp ELEMENT_TYPE
1405 Temporary_statement
* val_temp
=
1406 Statement::make_temporary(channel_type
->element_type(), NULL
, loc
);
1407 b
->add_statement(val_temp
);
1409 // var closed_temp bool
1410 Temporary_statement
* closed_temp
=
1411 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
1412 b
->add_statement(closed_temp
);
1414 // closed_temp = chanrecv2(type, channel, &val_temp)
1415 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
1417 Temporary_reference_expression
* ref
=
1418 Expression::make_temporary_reference(val_temp
, loc
);
1419 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1420 Expression
* call
= Runtime::make_call(Runtime::CHANRECV2
,
1421 loc
, 3, td
, this->channel_
, p2
);
1422 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1423 ref
->set_is_lvalue();
1424 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1425 b
->add_statement(s
);
1428 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1429 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1430 b
->add_statement(s
);
1432 // closed = closed_temp
1433 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1434 s
= Statement::make_assignment(this->closed_
, ref
, loc
);
1435 b
->add_statement(s
);
1437 return Statement::make_block_statement(b
, loc
);
1440 // Dump the AST representation for a tuple receive statement.
1443 Tuple_receive_assignment_statement::do_dump_statement(
1444 Ast_dump_context
* ast_dump_context
) const
1446 ast_dump_context
->print_indent();
1447 ast_dump_context
->dump_expression(this->val_
);
1448 ast_dump_context
->ostream() << ", ";
1449 ast_dump_context
->dump_expression(this->closed_
);
1450 ast_dump_context
->ostream() << " <- ";
1451 ast_dump_context
->dump_expression(this->channel_
);
1452 ast_dump_context
->ostream() << std::endl
;
1455 // Make a nonblocking receive statement.
1458 Statement::make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
1459 Expression
* channel
,
1462 return new Tuple_receive_assignment_statement(val
, closed
, channel
,
1466 // An assignment to a pair of values from a type guard. This is a
1467 // conditional type guard. v, ok = i.(type).
1469 class Tuple_type_guard_assignment_statement
: public Statement
1472 Tuple_type_guard_assignment_statement(Expression
* val
, Expression
* ok
,
1473 Expression
* expr
, Type
* type
,
1475 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
, location
),
1476 val_(val
), ok_(ok
), expr_(expr
), type_(type
)
1481 do_traverse(Traverse
*);
1484 do_traverse_assignments(Traverse_assignments
*)
1485 { go_unreachable(); }
1488 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1491 do_get_backend(Translate_context
*)
1492 { go_unreachable(); }
1495 do_dump_statement(Ast_dump_context
*) const;
1499 lower_to_type(Runtime::Function
);
1502 lower_to_object_type(Block
*, Runtime::Function
);
1504 // The variable which recieves the converted value.
1506 // The variable which receives the indication of success.
1508 // The expression being converted.
1510 // The type to which the expression is being converted.
1514 // Traverse a type guard tuple assignment.
1517 Tuple_type_guard_assignment_statement::do_traverse(Traverse
* traverse
)
1519 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1520 || this->traverse_expression(traverse
, &this->ok_
) == TRAVERSE_EXIT
1521 || this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
1522 return TRAVERSE_EXIT
;
1523 return this->traverse_expression(traverse
, &this->expr_
);
1526 // Lower to a function call.
1529 Tuple_type_guard_assignment_statement::do_lower(Gogo
*, Named_object
*,
1531 Statement_inserter
*)
1533 Location loc
= this->location();
1535 Type
* expr_type
= this->expr_
->type();
1536 if (expr_type
->interface_type() == NULL
)
1538 if (!expr_type
->is_error() && !this->type_
->is_error())
1539 this->report_error(_("type assertion only valid for interface types"));
1540 return Statement::make_error_statement(loc
);
1543 Block
* b
= new Block(enclosing
, loc
);
1545 // Make sure that any subexpressions on the left hand side are
1546 // evaluated in the right order.
1547 Move_ordered_evals
moe(b
);
1548 this->val_
->traverse_subexpressions(&moe
);
1549 this->ok_
->traverse_subexpressions(&moe
);
1551 bool expr_is_empty
= expr_type
->interface_type()->is_empty();
1552 Call_expression
* call
;
1553 if (this->type_
->interface_type() != NULL
)
1555 if (this->type_
->interface_type()->is_empty())
1556 call
= Runtime::make_call((expr_is_empty
1557 ? Runtime::IFACEE2E2
1558 : Runtime::IFACEI2E2
),
1559 loc
, 1, this->expr_
);
1561 call
= this->lower_to_type(expr_is_empty
1562 ? Runtime::IFACEE2I2
1563 : Runtime::IFACEI2I2
);
1565 else if (this->type_
->points_to() != NULL
)
1566 call
= this->lower_to_type(expr_is_empty
1567 ? Runtime::IFACEE2T2P
1568 : Runtime::IFACEI2T2P
);
1571 this->lower_to_object_type(b
,
1573 ? Runtime::IFACEE2T2
1574 : Runtime::IFACEI2T2
));
1580 Expression
* res
= Expression::make_call_result(call
, 0);
1581 res
= Expression::make_unsafe_cast(this->type_
, res
, loc
);
1582 Statement
* s
= Statement::make_assignment(this->val_
, res
, loc
);
1583 b
->add_statement(s
);
1585 res
= Expression::make_call_result(call
, 1);
1586 s
= Statement::make_assignment(this->ok_
, res
, loc
);
1587 b
->add_statement(s
);
1590 return Statement::make_block_statement(b
, loc
);
1593 // Lower a conversion to a non-empty interface type or a pointer type.
1596 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code
)
1598 Location loc
= this->location();
1599 return Runtime::make_call(code
, loc
, 2,
1600 Expression::make_type_descriptor(this->type_
, loc
),
1604 // Lower a conversion to a non-interface non-pointer type.
1607 Tuple_type_guard_assignment_statement::lower_to_object_type(
1609 Runtime::Function code
)
1611 Location loc
= this->location();
1613 // var val_temp TYPE
1614 Temporary_statement
* val_temp
= Statement::make_temporary(this->type_
,
1616 b
->add_statement(val_temp
);
1618 // ok = CODE(type_descriptor, expr, &val_temp)
1619 Expression
* p1
= Expression::make_type_descriptor(this->type_
, loc
);
1620 Expression
* ref
= Expression::make_temporary_reference(val_temp
, loc
);
1621 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1622 Expression
* call
= Runtime::make_call(code
, loc
, 3, p1
, this->expr_
, p3
);
1623 Statement
* s
= Statement::make_assignment(this->ok_
, call
, loc
);
1624 b
->add_statement(s
);
1627 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1628 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1629 b
->add_statement(s
);
1632 // Dump the AST representation for a tuple type guard statement.
1635 Tuple_type_guard_assignment_statement::do_dump_statement(
1636 Ast_dump_context
* ast_dump_context
) const
1638 ast_dump_context
->print_indent();
1639 ast_dump_context
->dump_expression(this->val_
);
1640 ast_dump_context
->ostream() << ", ";
1641 ast_dump_context
->dump_expression(this->ok_
);
1642 ast_dump_context
->ostream() << " = ";
1643 ast_dump_context
->dump_expression(this->expr_
);
1644 ast_dump_context
->ostream() << " . ";
1645 ast_dump_context
->dump_type(this->type_
);
1646 ast_dump_context
->ostream() << std::endl
;
1649 // Make an assignment from a type guard to a pair of variables.
1652 Statement::make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
1653 Expression
* expr
, Type
* type
,
1656 return new Tuple_type_guard_assignment_statement(val
, ok
, expr
, type
,
1660 // An expression statement.
1662 class Expression_statement
: public Statement
1665 Expression_statement(Expression
* expr
, bool is_ignored
)
1666 : Statement(STATEMENT_EXPRESSION
, expr
->location()),
1667 expr_(expr
), is_ignored_(is_ignored
)
1672 { return this->expr_
; }
1676 do_traverse(Traverse
* traverse
)
1677 { return this->traverse_expression(traverse
, &this->expr_
); }
1680 do_determine_types()
1681 { this->expr_
->determine_type_no_context(); }
1684 do_check_types(Gogo
*);
1687 do_may_fall_through() const;
1690 do_get_backend(Translate_context
* context
);
1693 do_dump_statement(Ast_dump_context
*) const;
1697 // Whether the value of this expression is being explicitly ignored.
1701 // Check the types of an expression statement. The only check we do
1702 // is to possibly give an error about discarding the value of the
1706 Expression_statement::do_check_types(Gogo
*)
1708 if (!this->is_ignored_
)
1709 this->expr_
->discarding_value();
1712 // An expression statement may fall through unless it is a call to a
1713 // function which does not return.
1716 Expression_statement::do_may_fall_through() const
1718 const Call_expression
* call
= this->expr_
->call_expression();
1721 const Expression
* fn
= call
->fn();
1722 const Func_expression
* fe
= fn
->func_expression();
1725 const Named_object
* no
= fe
->named_object();
1727 Function_type
* fntype
;
1728 if (no
->is_function())
1729 fntype
= no
->func_value()->type();
1730 else if (no
->is_function_declaration())
1731 fntype
= no
->func_declaration_value()->type();
1735 // The builtin function panic does not return.
1736 if (fntype
!= NULL
&& fntype
->is_builtin() && no
->name() == "panic")
1743 // Convert to backend representation.
1746 Expression_statement::do_get_backend(Translate_context
* context
)
1748 tree expr_tree
= this->expr_
->get_tree(context
);
1749 return context
->backend()->expression_statement(tree_to_expr(expr_tree
));
1752 // Dump the AST representation for an expression statement
1755 Expression_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
1758 ast_dump_context
->print_indent();
1759 ast_dump_context
->dump_expression(expr_
);
1760 ast_dump_context
->ostream() << std::endl
;
1763 // Make an expression statement from an Expression.
1766 Statement::make_statement(Expression
* expr
, bool is_ignored
)
1768 return new Expression_statement(expr
, is_ignored
);
1771 // A block statement--a list of statements which may include variable
1774 class Block_statement
: public Statement
1777 Block_statement(Block
* block
, Location location
)
1778 : Statement(STATEMENT_BLOCK
, location
),
1784 do_traverse(Traverse
* traverse
)
1785 { return this->block_
->traverse(traverse
); }
1788 do_determine_types()
1789 { this->block_
->determine_types(); }
1792 do_may_fall_through() const
1793 { return this->block_
->may_fall_through(); }
1796 do_get_backend(Translate_context
* context
);
1799 do_dump_statement(Ast_dump_context
*) const;
1805 // Convert a block to the backend representation of a statement.
1808 Block_statement::do_get_backend(Translate_context
* context
)
1810 Bblock
* bblock
= this->block_
->get_backend(context
);
1811 return context
->backend()->block_statement(bblock
);
1814 // Dump the AST for a block statement
1817 Block_statement::do_dump_statement(Ast_dump_context
*) const
1819 // block statement braces are dumped when traversing.
1822 // Make a block statement.
1825 Statement::make_block_statement(Block
* block
, Location location
)
1827 return new Block_statement(block
, location
);
1830 // An increment or decrement statement.
1832 class Inc_dec_statement
: public Statement
1835 Inc_dec_statement(bool is_inc
, Expression
* expr
)
1836 : Statement(STATEMENT_INCDEC
, expr
->location()),
1837 expr_(expr
), is_inc_(is_inc
)
1842 do_traverse(Traverse
* traverse
)
1843 { return this->traverse_expression(traverse
, &this->expr_
); }
1846 do_traverse_assignments(Traverse_assignments
*)
1847 { go_unreachable(); }
1850 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1853 do_get_backend(Translate_context
*)
1854 { go_unreachable(); }
1857 do_dump_statement(Ast_dump_context
*) const;
1860 // The l-value to increment or decrement.
1862 // Whether to increment or decrement.
1866 // Lower to += or -=.
1869 Inc_dec_statement::do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
1871 Location loc
= this->location();
1874 mpz_init_set_ui(oval
, 1UL);
1875 Expression
* oexpr
= Expression::make_integer(&oval
, NULL
, loc
);
1878 Operator op
= this->is_inc_
? OPERATOR_PLUSEQ
: OPERATOR_MINUSEQ
;
1879 return Statement::make_assignment_operation(op
, this->expr_
, oexpr
, loc
);
1882 // Dump the AST representation for a inc/dec statement.
1885 Inc_dec_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
1887 ast_dump_context
->print_indent();
1888 ast_dump_context
->dump_expression(expr_
);
1889 ast_dump_context
->ostream() << (is_inc_
? "++": "--") << std::endl
;
1892 // Make an increment statement.
1895 Statement::make_inc_statement(Expression
* expr
)
1897 return new Inc_dec_statement(true, expr
);
1900 // Make a decrement statement.
1903 Statement::make_dec_statement(Expression
* expr
)
1905 return new Inc_dec_statement(false, expr
);
1908 // Class Thunk_statement. This is the base class for go and defer
1913 Thunk_statement::Thunk_statement(Statement_classification classification
,
1914 Call_expression
* call
,
1916 : Statement(classification
, location
),
1917 call_(call
), struct_type_(NULL
)
1921 // Return whether this is a simple statement which does not require a
1925 Thunk_statement::is_simple(Function_type
* fntype
) const
1927 // We need a thunk to call a method, or to pass a variable number of
1929 if (fntype
->is_method() || fntype
->is_varargs())
1932 // A defer statement requires a thunk to set up for whether the
1933 // function can call recover.
1934 if (this->classification() == STATEMENT_DEFER
)
1937 // We can only permit a single parameter of pointer type.
1938 const Typed_identifier_list
* parameters
= fntype
->parameters();
1939 if (parameters
!= NULL
1940 && (parameters
->size() > 1
1941 || (parameters
->size() == 1
1942 && parameters
->begin()->type()->points_to() == NULL
)))
1945 // If the function returns multiple values, or returns a type other
1946 // than integer, floating point, or pointer, then it may get a
1947 // hidden first parameter, in which case we need the more
1948 // complicated approach. This is true even though we are going to
1949 // ignore the return value.
1950 const Typed_identifier_list
* results
= fntype
->results();
1952 && (results
->size() > 1
1953 || (results
->size() == 1
1954 && !results
->begin()->type()->is_basic_type()
1955 && results
->begin()->type()->points_to() == NULL
)))
1958 // If this calls something which is not a simple function, then we
1960 Expression
* fn
= this->call_
->call_expression()->fn();
1961 if (fn
->interface_field_reference_expression() != NULL
)
1967 // Traverse a thunk statement.
1970 Thunk_statement::do_traverse(Traverse
* traverse
)
1972 return this->traverse_expression(traverse
, &this->call_
);
1975 // We implement traverse_assignment for a thunk statement because it
1976 // effectively copies the function call.
1979 Thunk_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
1981 Expression
* fn
= this->call_
->call_expression()->fn();
1982 Expression
* fn2
= fn
;
1983 tassign
->value(&fn2
, true, false);
1987 // Determine types in a thunk statement.
1990 Thunk_statement::do_determine_types()
1992 this->call_
->determine_type_no_context();
1994 // Now that we know the types of the call, build the struct used to
1996 Call_expression
* ce
= this->call_
->call_expression();
1999 Function_type
* fntype
= ce
->get_function_type();
2000 if (fntype
!= NULL
&& !this->is_simple(fntype
))
2001 this->struct_type_
= this->build_struct(fntype
);
2004 // Check types in a thunk statement.
2007 Thunk_statement::do_check_types(Gogo
*)
2009 Call_expression
* ce
= this->call_
->call_expression();
2012 if (!this->call_
->is_error_expression())
2013 this->report_error("expected call expression");
2018 // The Traverse class used to find and simplify thunk statements.
2020 class Simplify_thunk_traverse
: public Traverse
2023 Simplify_thunk_traverse(Gogo
* gogo
)
2024 : Traverse(traverse_functions
| traverse_blocks
),
2025 gogo_(gogo
), function_(NULL
)
2029 function(Named_object
*);
2037 // The function we are traversing.
2038 Named_object
* function_
;
2041 // Keep track of the current function while looking for thunks.
2044 Simplify_thunk_traverse::function(Named_object
* no
)
2046 go_assert(this->function_
== NULL
);
2047 this->function_
= no
;
2048 int t
= no
->func_value()->traverse(this);
2049 this->function_
= NULL
;
2050 if (t
== TRAVERSE_EXIT
)
2052 return TRAVERSE_SKIP_COMPONENTS
;
2055 // Look for thunks in a block.
2058 Simplify_thunk_traverse::block(Block
* b
)
2060 // The parser ensures that thunk statements always appear at the end
2062 if (b
->statements()->size() < 1)
2063 return TRAVERSE_CONTINUE
;
2064 Thunk_statement
* stat
= b
->statements()->back()->thunk_statement();
2066 return TRAVERSE_CONTINUE
;
2067 if (stat
->simplify_statement(this->gogo_
, this->function_
, b
))
2068 return TRAVERSE_SKIP_COMPONENTS
;
2069 return TRAVERSE_CONTINUE
;
2072 // Simplify all thunk statements.
2075 Gogo::simplify_thunk_statements()
2077 Simplify_thunk_traverse
thunk_traverse(this);
2078 this->traverse(&thunk_traverse
);
2081 // Return true if the thunk function is a constant, which means that
2082 // it does not need to be passed to the thunk routine.
2085 Thunk_statement::is_constant_function() const
2087 Call_expression
* ce
= this->call_
->call_expression();
2088 Function_type
* fntype
= ce
->get_function_type();
2091 go_assert(saw_errors());
2094 if (fntype
->is_builtin())
2096 Expression
* fn
= ce
->fn();
2097 if (fn
->func_expression() != NULL
)
2098 return fn
->func_expression()->closure() == NULL
;
2099 if (fn
->interface_field_reference_expression() != NULL
)
2104 // Simplify complex thunk statements into simple ones. A complicated
2105 // thunk statement is one which takes anything other than zero
2106 // parameters or a single pointer parameter. We rewrite it into code
2107 // which allocates a struct, stores the parameter values into the
2108 // struct, and does a simple go or defer statement which passes the
2109 // struct to a thunk. The thunk does the real call.
2112 Thunk_statement::simplify_statement(Gogo
* gogo
, Named_object
* function
,
2115 if (this->classification() == STATEMENT_ERROR
)
2117 if (this->call_
->is_error_expression())
2120 if (this->classification() == STATEMENT_DEFER
)
2122 // Make sure that the defer stack exists for the function. We
2123 // will use when converting this statement to the backend
2124 // representation, but we want it to exist when we start
2125 // converting the function.
2126 function
->func_value()->defer_stack(this->location());
2129 Call_expression
* ce
= this->call_
->call_expression();
2130 Function_type
* fntype
= ce
->get_function_type();
2133 go_assert(saw_errors());
2134 this->set_is_error();
2137 if (this->is_simple(fntype
))
2140 Expression
* fn
= ce
->fn();
2141 Interface_field_reference_expression
* interface_method
=
2142 fn
->interface_field_reference_expression();
2144 Location location
= this->location();
2146 std::string thunk_name
= Gogo::thunk_name();
2149 this->build_thunk(gogo
, thunk_name
);
2151 // Generate code to call the thunk.
2153 // Get the values to store into the struct which is the single
2154 // argument to the thunk.
2156 Expression_list
* vals
= new Expression_list();
2157 if (!this->is_constant_function())
2158 vals
->push_back(fn
);
2160 if (interface_method
!= NULL
)
2161 vals
->push_back(interface_method
->expr());
2163 if (ce
->args() != NULL
)
2165 for (Expression_list::const_iterator p
= ce
->args()->begin();
2166 p
!= ce
->args()->end();
2168 vals
->push_back(*p
);
2171 // Build the struct.
2172 Expression
* constructor
=
2173 Expression::make_struct_composite_literal(this->struct_type_
, vals
,
2176 // Allocate the initialized struct on the heap.
2177 constructor
= Expression::make_heap_composite(constructor
, location
);
2179 // Look up the thunk.
2180 Named_object
* named_thunk
= gogo
->lookup(thunk_name
, NULL
);
2181 go_assert(named_thunk
!= NULL
&& named_thunk
->is_function());
2184 Expression
* func
= Expression::make_func_reference(named_thunk
, NULL
,
2186 Expression_list
* params
= new Expression_list();
2187 params
->push_back(constructor
);
2188 Call_expression
* call
= Expression::make_call(func
, params
, false, location
);
2190 // Build the simple go or defer statement.
2192 if (this->classification() == STATEMENT_GO
)
2193 s
= Statement::make_go_statement(call
, location
);
2194 else if (this->classification() == STATEMENT_DEFER
)
2195 s
= Statement::make_defer_statement(call
, location
);
2199 // The current block should end with the go statement.
2200 go_assert(block
->statements()->size() >= 1);
2201 go_assert(block
->statements()->back() == this);
2202 block
->replace_statement(block
->statements()->size() - 1, s
);
2204 // We already ran the determine_types pass, so we need to run it now
2205 // for the new statement.
2206 s
->determine_types();
2209 gogo
->check_types_in_block(block
);
2211 // Return true to tell the block not to keep looking at statements.
2215 // Set the name to use for thunk parameter N.
2218 Thunk_statement::thunk_field_param(int n
, char* buf
, size_t buflen
)
2220 snprintf(buf
, buflen
, "a%d", n
);
2223 // Build a new struct type to hold the parameters for a complicated
2224 // thunk statement. FNTYPE is the type of the function call.
2227 Thunk_statement::build_struct(Function_type
* fntype
)
2229 Location location
= this->location();
2231 Struct_field_list
* fields
= new Struct_field_list();
2233 Call_expression
* ce
= this->call_
->call_expression();
2234 Expression
* fn
= ce
->fn();
2236 if (!this->is_constant_function())
2238 // The function to call.
2239 fields
->push_back(Struct_field(Typed_identifier("fn", fntype
,
2243 // If this thunk statement calls a method on an interface, we pass
2244 // the interface object to the thunk.
2245 Interface_field_reference_expression
* interface_method
=
2246 fn
->interface_field_reference_expression();
2247 if (interface_method
!= NULL
)
2249 Typed_identifier
tid("object", interface_method
->expr()->type(),
2251 fields
->push_back(Struct_field(tid
));
2254 // The predeclared recover function has no argument. However, we
2255 // add an argument when building recover thunks. Handle that here.
2256 if (ce
->is_recover_call())
2258 fields
->push_back(Struct_field(Typed_identifier("can_recover",
2259 Type::lookup_bool_type(),
2263 const Expression_list
* args
= ce
->args();
2267 for (Expression_list::const_iterator p
= args
->begin();
2272 this->thunk_field_param(i
, buf
, sizeof buf
);
2273 fields
->push_back(Struct_field(Typed_identifier(buf
, (*p
)->type(),
2278 return Type::make_struct_type(fields
, location
);
2281 // Build the thunk we are going to call. This is a brand new, albeit
2282 // artificial, function.
2285 Thunk_statement::build_thunk(Gogo
* gogo
, const std::string
& thunk_name
)
2287 Location location
= this->location();
2289 Call_expression
* ce
= this->call_
->call_expression();
2291 bool may_call_recover
= false;
2292 if (this->classification() == STATEMENT_DEFER
)
2294 Func_expression
* fn
= ce
->fn()->func_expression();
2296 may_call_recover
= true;
2299 const Named_object
* no
= fn
->named_object();
2300 if (!no
->is_function())
2301 may_call_recover
= true;
2303 may_call_recover
= no
->func_value()->calls_recover();
2307 // Build the type of the thunk. The thunk takes a single parameter,
2308 // which is a pointer to the special structure we build.
2309 const char* const parameter_name
= "__go_thunk_parameter";
2310 Typed_identifier_list
* thunk_parameters
= new Typed_identifier_list();
2311 Type
* pointer_to_struct_type
= Type::make_pointer_type(this->struct_type_
);
2312 thunk_parameters
->push_back(Typed_identifier(parameter_name
,
2313 pointer_to_struct_type
,
2316 Typed_identifier_list
* thunk_results
= NULL
;
2317 if (may_call_recover
)
2319 // When deferring a function which may call recover, add a
2320 // return value, to disable tail call optimizations which will
2321 // break the way we check whether recover is permitted.
2322 thunk_results
= new Typed_identifier_list();
2323 thunk_results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
2327 Function_type
* thunk_type
= Type::make_function_type(NULL
, thunk_parameters
,
2331 // Start building the thunk.
2332 Named_object
* function
= gogo
->start_function(thunk_name
, thunk_type
, true,
2335 gogo
->start_block(location
);
2337 // For a defer statement, start with a call to
2338 // __go_set_defer_retaddr. */
2339 Label
* retaddr_label
= NULL
;
2340 if (may_call_recover
)
2342 retaddr_label
= gogo
->add_label_reference("retaddr", location
, false);
2343 Expression
* arg
= Expression::make_label_addr(retaddr_label
, location
);
2344 Expression
* call
= Runtime::make_call(Runtime::SET_DEFER_RETADDR
,
2347 // This is a hack to prevent the middle-end from deleting the
2349 gogo
->start_block(location
);
2350 gogo
->add_statement(Statement::make_goto_statement(retaddr_label
,
2352 Block
* then_block
= gogo
->finish_block(location
);
2353 then_block
->determine_types();
2355 Statement
* s
= Statement::make_if_statement(call
, then_block
, NULL
,
2357 s
->determine_types();
2358 gogo
->add_statement(s
);
2361 // Get a reference to the parameter.
2362 Named_object
* named_parameter
= gogo
->lookup(parameter_name
, NULL
);
2363 go_assert(named_parameter
!= NULL
&& named_parameter
->is_variable());
2365 // Build the call. Note that the field names are the same as the
2366 // ones used in build_struct.
2367 Expression
* thunk_parameter
= Expression::make_var_reference(named_parameter
,
2369 thunk_parameter
= Expression::make_unary(OPERATOR_MULT
, thunk_parameter
,
2372 Interface_field_reference_expression
* interface_method
=
2373 ce
->fn()->interface_field_reference_expression();
2375 Expression
* func_to_call
;
2376 unsigned int next_index
;
2377 if (this->is_constant_function())
2379 func_to_call
= ce
->fn();
2384 func_to_call
= Expression::make_field_reference(thunk_parameter
,
2389 if (interface_method
!= NULL
)
2391 // The main program passes the interface object.
2392 go_assert(next_index
== 0);
2393 Expression
* r
= Expression::make_field_reference(thunk_parameter
, 0,
2395 const std::string
& name(interface_method
->name());
2396 func_to_call
= Expression::make_interface_field_reference(r
, name
,
2401 Expression_list
* call_params
= new Expression_list();
2402 const Struct_field_list
* fields
= this->struct_type_
->fields();
2403 Struct_field_list::const_iterator p
= fields
->begin();
2404 for (unsigned int i
= 0; i
< next_index
; ++i
)
2406 bool is_recover_call
= ce
->is_recover_call();
2407 Expression
* recover_arg
= NULL
;
2408 for (; p
!= fields
->end(); ++p
, ++next_index
)
2410 Expression
* thunk_param
= Expression::make_var_reference(named_parameter
,
2412 thunk_param
= Expression::make_unary(OPERATOR_MULT
, thunk_param
,
2414 Expression
* param
= Expression::make_field_reference(thunk_param
,
2417 if (!is_recover_call
)
2418 call_params
->push_back(param
);
2421 go_assert(call_params
->empty());
2422 recover_arg
= param
;
2426 if (call_params
->empty())
2432 Call_expression
* call
= Expression::make_call(func_to_call
, call_params
,
2435 // This call expression was already lowered before entering the
2436 // thunk statement. Don't try to lower varargs again, as that will
2437 // cause confusion for, e.g., method calls which already have a
2438 // receiver parameter.
2439 call
->set_varargs_are_lowered();
2441 Statement
* call_statement
= Statement::make_statement(call
, true);
2443 gogo
->add_statement(call_statement
);
2445 // If this is a defer statement, the label comes immediately after
2447 if (may_call_recover
)
2449 gogo
->add_label_definition("retaddr", location
);
2451 Expression_list
* vals
= new Expression_list();
2452 vals
->push_back(Expression::make_boolean(false, location
));
2453 gogo
->add_statement(Statement::make_return_statement(vals
, location
));
2456 Block
* b
= gogo
->finish_block(location
);
2458 gogo
->add_block(b
, location
);
2460 gogo
->lower_block(function
, b
);
2462 // We already ran the determine_types pass, so we need to run it
2463 // just for the call statement now. The other types are known.
2464 call_statement
->determine_types();
2466 if (may_call_recover
|| recover_arg
!= NULL
)
2468 // Dig up the call expression, which may have been changed
2470 go_assert(call_statement
->classification() == STATEMENT_EXPRESSION
);
2471 Expression_statement
* es
=
2472 static_cast<Expression_statement
*>(call_statement
);
2473 Call_expression
* ce
= es
->expr()->call_expression();
2474 go_assert(ce
!= NULL
);
2475 if (may_call_recover
)
2476 ce
->set_is_deferred();
2477 if (recover_arg
!= NULL
)
2478 ce
->set_recover_arg(recover_arg
);
2481 // That is all the thunk has to do.
2482 gogo
->finish_function(location
);
2485 // Get the function and argument expressions.
2488 Thunk_statement::get_fn_and_arg(Expression
** pfn
, Expression
** parg
)
2490 if (this->call_
->is_error_expression())
2493 Call_expression
* ce
= this->call_
->call_expression();
2497 const Expression_list
* args
= ce
->args();
2498 if (args
== NULL
|| args
->empty())
2499 *parg
= Expression::make_nil(this->location());
2502 go_assert(args
->size() == 1);
2503 *parg
= args
->front();
2509 // Class Go_statement.
2512 Go_statement::do_get_backend(Translate_context
* context
)
2516 if (!this->get_fn_and_arg(&fn
, &arg
))
2517 return context
->backend()->error_statement();
2519 Expression
* call
= Runtime::make_call(Runtime::GO
, this->location(), 2,
2521 tree call_tree
= call
->get_tree(context
);
2522 Bexpression
* call_bexpr
= tree_to_expr(call_tree
);
2523 return context
->backend()->expression_statement(call_bexpr
);
2526 // Dump the AST representation for go statement.
2529 Go_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2531 ast_dump_context
->print_indent();
2532 ast_dump_context
->ostream() << "go ";
2533 ast_dump_context
->dump_expression(this->call());
2534 ast_dump_context
->ostream() << std::endl
;
2537 // Make a go statement.
2540 Statement::make_go_statement(Call_expression
* call
, Location location
)
2542 return new Go_statement(call
, location
);
2545 // Class Defer_statement.
2548 Defer_statement::do_get_backend(Translate_context
* context
)
2552 if (!this->get_fn_and_arg(&fn
, &arg
))
2553 return context
->backend()->error_statement();
2555 Location loc
= this->location();
2556 Expression
* ds
= context
->function()->func_value()->defer_stack(loc
);
2558 Expression
* call
= Runtime::make_call(Runtime::DEFER
, loc
, 3,
2560 tree call_tree
= call
->get_tree(context
);
2561 Bexpression
* call_bexpr
= tree_to_expr(call_tree
);
2562 return context
->backend()->expression_statement(call_bexpr
);
2565 // Dump the AST representation for defer statement.
2568 Defer_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2570 ast_dump_context
->print_indent();
2571 ast_dump_context
->ostream() << "defer ";
2572 ast_dump_context
->dump_expression(this->call());
2573 ast_dump_context
->ostream() << std::endl
;
2576 // Make a defer statement.
2579 Statement::make_defer_statement(Call_expression
* call
,
2582 return new Defer_statement(call
, location
);
2585 // Class Return_statement.
2587 // Traverse assignments. We treat each return value as a top level
2588 // RHS in an expression.
2591 Return_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
2593 Expression_list
* vals
= this->vals_
;
2596 for (Expression_list::iterator p
= vals
->begin();
2599 tassign
->value(&*p
, true, true);
2604 // Lower a return statement. If we are returning a function call
2605 // which returns multiple values which match the current function,
2606 // split up the call's results. If the return statement lists
2607 // explicit values, implement this statement by assigning the values
2608 // to the result variables and change this statement to a naked
2609 // return. This lets panic/recover work correctly.
2612 Return_statement::do_lower(Gogo
*, Named_object
* function
, Block
* enclosing
,
2613 Statement_inserter
*)
2615 if (this->is_lowered_
)
2618 Expression_list
* vals
= this->vals_
;
2620 this->is_lowered_
= true;
2622 Location loc
= this->location();
2624 size_t vals_count
= vals
== NULL
? 0 : vals
->size();
2625 Function::Results
* results
= function
->func_value()->result_variables();
2626 size_t results_count
= results
== NULL
? 0 : results
->size();
2628 if (vals_count
== 0)
2630 if (results_count
> 0 && !function
->func_value()->results_are_named())
2632 this->report_error(_("not enough arguments to return"));
2638 if (results_count
== 0)
2640 this->report_error(_("return with value in function "
2641 "with no return type"));
2645 // If the current function has multiple return values, and we are
2646 // returning a single call expression, split up the call expression.
2647 if (results_count
> 1
2648 && vals
->size() == 1
2649 && vals
->front()->call_expression() != NULL
)
2651 Call_expression
* call
= vals
->front()->call_expression();
2653 vals
= new Expression_list
;
2654 for (size_t i
= 0; i
< results_count
; ++i
)
2655 vals
->push_back(Expression::make_call_result(call
, i
));
2656 vals_count
= results_count
;
2659 if (vals_count
< results_count
)
2661 this->report_error(_("not enough arguments to return"));
2665 if (vals_count
> results_count
)
2667 this->report_error(_("too many values in return statement"));
2671 Block
* b
= new Block(enclosing
, loc
);
2673 Expression_list
* lhs
= new Expression_list();
2674 Expression_list
* rhs
= new Expression_list();
2676 Expression_list::const_iterator pe
= vals
->begin();
2678 for (Function::Results::const_iterator pr
= results
->begin();
2679 pr
!= results
->end();
2682 Named_object
* rv
= *pr
;
2683 Expression
* e
= *pe
;
2685 // Check types now so that we give a good error message. The
2686 // result type is known. We determine the expression type
2689 Type
*rvtype
= rv
->result_var_value()->type();
2690 Type_context
type_context(rvtype
, false);
2691 e
->determine_type(&type_context
);
2695 if (this->are_hidden_fields_ok_
)
2696 ok
= Type::are_assignable_hidden_ok(rvtype
, e
->type(), &reason
);
2698 ok
= Type::are_assignable(rvtype
, e
->type(), &reason
);
2701 Expression
* ve
= Expression::make_var_reference(rv
, e
->location());
2708 error_at(e
->location(), "incompatible type for return value %d", i
);
2710 error_at(e
->location(),
2711 "incompatible type for return value %d (%s)",
2715 go_assert(lhs
->size() == rhs
->size());
2719 else if (lhs
->size() == 1)
2721 Statement
* s
= Statement::make_assignment(lhs
->front(), rhs
->front(),
2723 if (this->are_hidden_fields_ok_
)
2725 Assignment_statement
* as
= static_cast<Assignment_statement
*>(s
);
2726 as
->set_hidden_fields_are_ok();
2728 b
->add_statement(s
);
2734 Statement
* s
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
2735 if (this->are_hidden_fields_ok_
)
2737 Tuple_assignment_statement
* tas
=
2738 static_cast<Tuple_assignment_statement
*>(s
);
2739 tas
->set_hidden_fields_are_ok();
2741 b
->add_statement(s
);
2744 b
->add_statement(this);
2748 return Statement::make_block_statement(b
, loc
);
2751 // Convert a return statement to the backend representation.
2754 Return_statement::do_get_backend(Translate_context
* context
)
2756 Location loc
= this->location();
2758 Function
* function
= context
->function()->func_value();
2759 tree fndecl
= function
->get_decl();
2761 Function::Results
* results
= function
->result_variables();
2762 std::vector
<Bexpression
*> retvals
;
2763 if (results
!= NULL
&& !results
->empty())
2765 retvals
.reserve(results
->size());
2766 for (Function::Results::const_iterator p
= results
->begin();
2767 p
!= results
->end();
2770 Expression
* vr
= Expression::make_var_reference(*p
, loc
);
2771 retvals
.push_back(tree_to_expr(vr
->get_tree(context
)));
2775 return context
->backend()->return_statement(tree_to_function(fndecl
),
2779 // Dump the AST representation for a return statement.
2782 Return_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2784 ast_dump_context
->print_indent();
2785 ast_dump_context
->ostream() << "return " ;
2786 ast_dump_context
->dump_expression_list(this->vals_
);
2787 ast_dump_context
->ostream() << std::endl
;
2790 // Make a return statement.
2793 Statement::make_return_statement(Expression_list
* vals
,
2796 return new Return_statement(vals
, location
);
2799 // A break or continue statement.
2801 class Bc_statement
: public Statement
2804 Bc_statement(bool is_break
, Unnamed_label
* label
, Location location
)
2805 : Statement(STATEMENT_BREAK_OR_CONTINUE
, location
),
2806 label_(label
), is_break_(is_break
)
2811 { return this->is_break_
; }
2815 do_traverse(Traverse
*)
2816 { return TRAVERSE_CONTINUE
; }
2819 do_may_fall_through() const
2823 do_get_backend(Translate_context
* context
)
2824 { return this->label_
->get_goto(context
, this->location()); }
2827 do_dump_statement(Ast_dump_context
*) const;
2830 // The label that this branches to.
2831 Unnamed_label
* label_
;
2832 // True if this is "break", false if it is "continue".
2836 // Dump the AST representation for a break/continue statement
2839 Bc_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2841 ast_dump_context
->print_indent();
2842 ast_dump_context
->ostream() << (this->is_break_
? "break" : "continue");
2843 if (this->label_
!= NULL
)
2845 ast_dump_context
->ostream() << " ";
2846 ast_dump_context
->dump_label_name(this->label_
);
2848 ast_dump_context
->ostream() << std::endl
;
2851 // Make a break statement.
2854 Statement::make_break_statement(Unnamed_label
* label
, Location location
)
2856 return new Bc_statement(true, label
, location
);
2859 // Make a continue statement.
2862 Statement::make_continue_statement(Unnamed_label
* label
,
2865 return new Bc_statement(false, label
, location
);
2868 // A goto statement.
2870 class Goto_statement
: public Statement
2873 Goto_statement(Label
* label
, Location location
)
2874 : Statement(STATEMENT_GOTO
, location
),
2880 do_traverse(Traverse
*)
2881 { return TRAVERSE_CONTINUE
; }
2884 do_check_types(Gogo
*);
2887 do_may_fall_through() const
2891 do_get_backend(Translate_context
*);
2894 do_dump_statement(Ast_dump_context
*) const;
2900 // Check types for a label. There aren't any types per se, but we use
2901 // this to give an error if the label was never defined.
2904 Goto_statement::do_check_types(Gogo
*)
2906 if (!this->label_
->is_defined())
2908 error_at(this->location(), "reference to undefined label %qs",
2909 Gogo::message_name(this->label_
->name()).c_str());
2910 this->set_is_error();
2914 // Convert the goto statement to the backend representation.
2917 Goto_statement::do_get_backend(Translate_context
* context
)
2919 Blabel
* blabel
= this->label_
->get_backend_label(context
);
2920 return context
->backend()->goto_statement(blabel
, this->location());
2923 // Dump the AST representation for a goto statement.
2926 Goto_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2928 ast_dump_context
->print_indent();
2929 ast_dump_context
->ostream() << "goto " << this->label_
->name() << std::endl
;
2932 // Make a goto statement.
2935 Statement::make_goto_statement(Label
* label
, Location location
)
2937 return new Goto_statement(label
, location
);
2940 // A goto statement to an unnamed label.
2942 class Goto_unnamed_statement
: public Statement
2945 Goto_unnamed_statement(Unnamed_label
* label
, Location location
)
2946 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
2952 do_traverse(Traverse
*)
2953 { return TRAVERSE_CONTINUE
; }
2956 do_may_fall_through() const
2960 do_get_backend(Translate_context
* context
)
2961 { return this->label_
->get_goto(context
, this->location()); }
2964 do_dump_statement(Ast_dump_context
*) const;
2967 Unnamed_label
* label_
;
2970 // Dump the AST representation for an unnamed goto statement
2973 Goto_unnamed_statement::do_dump_statement(
2974 Ast_dump_context
* ast_dump_context
) const
2976 ast_dump_context
->print_indent();
2977 ast_dump_context
->ostream() << "goto ";
2978 ast_dump_context
->dump_label_name(this->label_
);
2979 ast_dump_context
->ostream() << std::endl
;
2982 // Make a goto statement to an unnamed label.
2985 Statement::make_goto_unnamed_statement(Unnamed_label
* label
,
2988 return new Goto_unnamed_statement(label
, location
);
2991 // Class Label_statement.
2996 Label_statement::do_traverse(Traverse
*)
2998 return TRAVERSE_CONTINUE
;
3001 // Return the backend representation of the statement defining this
3005 Label_statement::do_get_backend(Translate_context
* context
)
3007 Blabel
* blabel
= this->label_
->get_backend_label(context
);
3008 return context
->backend()->label_definition_statement(blabel
);
3011 // Dump the AST for a label definition statement.
3014 Label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3016 ast_dump_context
->print_indent();
3017 ast_dump_context
->ostream() << this->label_
->name() << ":" << std::endl
;
3020 // Make a label statement.
3023 Statement::make_label_statement(Label
* label
, Location location
)
3025 return new Label_statement(label
, location
);
3028 // An unnamed label statement.
3030 class Unnamed_label_statement
: public Statement
3033 Unnamed_label_statement(Unnamed_label
* label
)
3034 : Statement(STATEMENT_UNNAMED_LABEL
, label
->location()),
3040 do_traverse(Traverse
*)
3041 { return TRAVERSE_CONTINUE
; }
3044 do_get_backend(Translate_context
* context
)
3045 { return this->label_
->get_definition(context
); }
3048 do_dump_statement(Ast_dump_context
*) const;
3052 Unnamed_label
* label_
;
3055 // Dump the AST representation for an unnamed label definition statement.
3058 Unnamed_label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3061 ast_dump_context
->print_indent();
3062 ast_dump_context
->dump_label_name(this->label_
);
3063 ast_dump_context
->ostream() << ":" << std::endl
;
3066 // Make an unnamed label statement.
3069 Statement::make_unnamed_label_statement(Unnamed_label
* label
)
3071 return new Unnamed_label_statement(label
);
3076 class If_statement
: public Statement
3079 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
3081 : Statement(STATEMENT_IF
, location
),
3082 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
3087 do_traverse(Traverse
*);
3090 do_determine_types();
3093 do_check_types(Gogo
*);
3096 do_may_fall_through() const;
3099 do_get_backend(Translate_context
*);
3102 do_dump_statement(Ast_dump_context
*) const;
3113 If_statement::do_traverse(Traverse
* traverse
)
3115 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
3116 || this->then_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3117 return TRAVERSE_EXIT
;
3118 if (this->else_block_
!= NULL
)
3120 if (this->else_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3121 return TRAVERSE_EXIT
;
3123 return TRAVERSE_CONTINUE
;
3127 If_statement::do_determine_types()
3129 Type_context
context(Type::lookup_bool_type(), false);
3130 this->cond_
->determine_type(&context
);
3131 this->then_block_
->determine_types();
3132 if (this->else_block_
!= NULL
)
3133 this->else_block_
->determine_types();
3139 If_statement::do_check_types(Gogo
*)
3141 Type
* type
= this->cond_
->type();
3142 if (type
->is_error())
3143 this->set_is_error();
3144 else if (!type
->is_boolean_type())
3145 this->report_error(_("expected boolean expression"));
3148 // Whether the overall statement may fall through.
3151 If_statement::do_may_fall_through() const
3153 return (this->else_block_
== NULL
3154 || this->then_block_
->may_fall_through()
3155 || this->else_block_
->may_fall_through());
3158 // Get the backend representation.
3161 If_statement::do_get_backend(Translate_context
* context
)
3163 go_assert(this->cond_
->type()->is_boolean_type()
3164 || this->cond_
->type()->is_error());
3165 tree cond_tree
= this->cond_
->get_tree(context
);
3166 Bexpression
* cond_expr
= tree_to_expr(cond_tree
);
3167 Bblock
* then_block
= this->then_block_
->get_backend(context
);
3168 Bblock
* else_block
= (this->else_block_
== NULL
3170 : this->else_block_
->get_backend(context
));
3171 return context
->backend()->if_statement(cond_expr
, then_block
,
3172 else_block
, this->location());
3175 // Dump the AST representation for an if statement
3178 If_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3180 ast_dump_context
->print_indent();
3181 ast_dump_context
->ostream() << "if ";
3182 ast_dump_context
->dump_expression(this->cond_
);
3183 ast_dump_context
->ostream() << std::endl
;
3184 if (ast_dump_context
->dump_subblocks())
3186 ast_dump_context
->dump_block(this->then_block_
);
3187 if (this->else_block_
!= NULL
)
3189 ast_dump_context
->print_indent();
3190 ast_dump_context
->ostream() << "else" << std::endl
;
3191 ast_dump_context
->dump_block(this->else_block_
);
3196 // Make an if statement.
3199 Statement::make_if_statement(Expression
* cond
, Block
* then_block
,
3200 Block
* else_block
, Location location
)
3202 return new If_statement(cond
, then_block
, else_block
, location
);
3205 // Class Case_clauses::Hash_integer_value.
3207 class Case_clauses::Hash_integer_value
3211 operator()(Expression
*) const;
3215 Case_clauses::Hash_integer_value::operator()(Expression
* pe
) const
3217 Numeric_constant nc
;
3219 if (!pe
->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3221 size_t ret
= mpz_get_ui(ival
);
3226 // Class Case_clauses::Eq_integer_value.
3228 class Case_clauses::Eq_integer_value
3232 operator()(Expression
*, Expression
*) const;
3236 Case_clauses::Eq_integer_value::operator()(Expression
* a
, Expression
* b
) const
3238 Numeric_constant anc
;
3240 Numeric_constant bnc
;
3242 if (!a
->numeric_constant_value(&anc
)
3243 || !anc
.to_int(&aval
)
3244 || !b
->numeric_constant_value(&bnc
)
3245 || !bnc
.to_int(&bval
))
3247 bool ret
= mpz_cmp(aval
, bval
) == 0;
3253 // Class Case_clauses::Case_clause.
3258 Case_clauses::Case_clause::traverse(Traverse
* traverse
)
3260 if (this->cases_
!= NULL
3261 && (traverse
->traverse_mask()
3262 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3264 if (this->cases_
->traverse(traverse
) == TRAVERSE_EXIT
)
3265 return TRAVERSE_EXIT
;
3267 if (this->statements_
!= NULL
)
3269 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
3270 return TRAVERSE_EXIT
;
3272 return TRAVERSE_CONTINUE
;
3275 // Check whether all the case expressions are integer constants.
3278 Case_clauses::Case_clause::is_constant() const
3280 if (this->cases_
!= NULL
)
3282 for (Expression_list::const_iterator p
= this->cases_
->begin();
3283 p
!= this->cases_
->end();
3285 if (!(*p
)->is_constant() || (*p
)->type()->integer_type() == NULL
)
3291 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3292 // value we are switching on; it may be NULL. If START_LABEL is not
3293 // NULL, it goes at the start of the statements, after the condition
3294 // test. We branch to FINISH_LABEL at the end of the statements.
3297 Case_clauses::Case_clause::lower(Block
* b
, Temporary_statement
* val_temp
,
3298 Unnamed_label
* start_label
,
3299 Unnamed_label
* finish_label
) const
3301 Location loc
= this->location_
;
3302 Unnamed_label
* next_case_label
;
3303 if (this->cases_
== NULL
|| this->cases_
->empty())
3305 go_assert(this->is_default_
);
3306 next_case_label
= NULL
;
3310 Expression
* cond
= NULL
;
3312 for (Expression_list::const_iterator p
= this->cases_
->begin();
3313 p
!= this->cases_
->end();
3316 Expression
* ref
= Expression::make_temporary_reference(val_temp
,
3318 Expression
* this_cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3323 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, this_cond
, loc
);
3326 Block
* then_block
= new Block(b
, loc
);
3327 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3328 Statement
* s
= Statement::make_goto_unnamed_statement(next_case_label
,
3330 then_block
->add_statement(s
);
3332 // if !COND { goto NEXT_CASE_LABEL }
3333 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3334 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
3335 b
->add_statement(s
);
3338 if (start_label
!= NULL
)
3339 b
->add_statement(Statement::make_unnamed_label_statement(start_label
));
3341 if (this->statements_
!= NULL
)
3342 b
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
3344 Statement
* s
= Statement::make_goto_unnamed_statement(finish_label
, loc
);
3345 b
->add_statement(s
);
3347 if (next_case_label
!= NULL
)
3348 b
->add_statement(Statement::make_unnamed_label_statement(next_case_label
));
3354 Case_clauses::Case_clause::determine_types(Type
* type
)
3356 if (this->cases_
!= NULL
)
3358 Type_context
case_context(type
, false);
3359 for (Expression_list::iterator p
= this->cases_
->begin();
3360 p
!= this->cases_
->end();
3362 (*p
)->determine_type(&case_context
);
3364 if (this->statements_
!= NULL
)
3365 this->statements_
->determine_types();
3368 // Check types. Returns false if there was an error.
3371 Case_clauses::Case_clause::check_types(Type
* type
)
3373 if (this->cases_
!= NULL
)
3375 for (Expression_list::iterator p
= this->cases_
->begin();
3376 p
!= this->cases_
->end();
3379 if (!Type::are_assignable(type
, (*p
)->type(), NULL
)
3380 && !Type::are_assignable((*p
)->type(), type
, NULL
))
3382 error_at((*p
)->location(),
3383 "type mismatch between switch value and case clause");
3391 // Return true if this clause may fall through to the following
3392 // statements. Note that this is not the same as whether the case
3393 // uses the "fallthrough" keyword.
3396 Case_clauses::Case_clause::may_fall_through() const
3398 if (this->statements_
== NULL
)
3400 return this->statements_
->may_fall_through();
3403 // Convert the case values and statements to the backend
3404 // representation. BREAK_LABEL is the label which break statements
3405 // should branch to. CASE_CONSTANTS is used to detect duplicate
3406 // constants. *CASES should be passed as an empty vector; the values
3407 // for this case will be added to it. If this is the default case,
3408 // *CASES will remain empty. This returns the statement to execute if
3409 // one of these cases is selected.
3412 Case_clauses::Case_clause::get_backend(Translate_context
* context
,
3413 Unnamed_label
* break_label
,
3414 Case_constants
* case_constants
,
3415 std::vector
<Bexpression
*>* cases
) const
3417 if (this->cases_
!= NULL
)
3419 go_assert(!this->is_default_
);
3420 for (Expression_list::const_iterator p
= this->cases_
->begin();
3421 p
!= this->cases_
->end();
3425 if (e
->classification() != Expression::EXPRESSION_INTEGER
)
3427 Numeric_constant nc
;
3429 if (!(*p
)->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3431 // Something went wrong. This can happen with a
3432 // negative constant and an unsigned switch value.
3433 go_assert(saw_errors());
3436 go_assert(nc
.type() != NULL
);
3437 e
= Expression::make_integer(&ival
, nc
.type(), e
->location());
3441 std::pair
<Case_constants::iterator
, bool> ins
=
3442 case_constants
->insert(e
);
3445 // Value was already present.
3446 error_at(this->location_
, "duplicate case in switch");
3447 e
= Expression::make_error(this->location_
);
3450 tree case_tree
= e
->get_tree(context
);
3451 Bexpression
* case_expr
= tree_to_expr(case_tree
);
3452 cases
->push_back(case_expr
);
3456 Bstatement
* statements
;
3457 if (this->statements_
== NULL
)
3461 Bblock
* bblock
= this->statements_
->get_backend(context
);
3462 statements
= context
->backend()->block_statement(bblock
);
3465 Bstatement
* break_stat
;
3466 if (this->is_fallthrough_
)
3469 break_stat
= break_label
->get_goto(context
, this->location_
);
3471 if (statements
== NULL
)
3473 else if (break_stat
== NULL
)
3476 return context
->backend()->compound_statement(statements
, break_stat
);
3479 // Dump the AST representation for a case clause
3482 Case_clauses::Case_clause::dump_clause(Ast_dump_context
* ast_dump_context
)
3485 ast_dump_context
->print_indent();
3486 if (this->is_default_
)
3488 ast_dump_context
->ostream() << "default:";
3492 ast_dump_context
->ostream() << "case ";
3493 ast_dump_context
->dump_expression_list(this->cases_
);
3494 ast_dump_context
->ostream() << ":" ;
3496 ast_dump_context
->dump_block(this->statements_
);
3497 if (this->is_fallthrough_
)
3499 ast_dump_context
->print_indent();
3500 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
3504 // Class Case_clauses.
3509 Case_clauses::traverse(Traverse
* traverse
)
3511 for (Clauses::iterator p
= this->clauses_
.begin();
3512 p
!= this->clauses_
.end();
3515 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
3516 return TRAVERSE_EXIT
;
3518 return TRAVERSE_CONTINUE
;
3521 // Check whether all the case expressions are constant.
3524 Case_clauses::is_constant() const
3526 for (Clauses::const_iterator p
= this->clauses_
.begin();
3527 p
!= this->clauses_
.end();
3529 if (!p
->is_constant())
3534 // Lower case clauses for a nonconstant switch.
3537 Case_clauses::lower(Block
* b
, Temporary_statement
* val_temp
,
3538 Unnamed_label
* break_label
) const
3540 // The default case.
3541 const Case_clause
* default_case
= NULL
;
3543 // The label for the fallthrough of the previous case.
3544 Unnamed_label
* last_fallthrough_label
= NULL
;
3546 // The label for the start of the default case. This is used if the
3547 // case before the default case falls through.
3548 Unnamed_label
* default_start_label
= NULL
;
3550 // The label for the end of the default case. This normally winds
3551 // up as BREAK_LABEL, but it will be different if the default case
3553 Unnamed_label
* default_finish_label
= NULL
;
3555 for (Clauses::const_iterator p
= this->clauses_
.begin();
3556 p
!= this->clauses_
.end();
3559 // The label to use for the start of the statements for this
3560 // case. This is NULL unless the previous case falls through.
3561 Unnamed_label
* start_label
= last_fallthrough_label
;
3563 // The label to jump to after the end of the statements for this
3565 Unnamed_label
* finish_label
= break_label
;
3567 last_fallthrough_label
= NULL
;
3568 if (p
->is_fallthrough() && p
+ 1 != this->clauses_
.end())
3570 finish_label
= new Unnamed_label(p
->location());
3571 last_fallthrough_label
= finish_label
;
3574 if (!p
->is_default())
3575 p
->lower(b
, val_temp
, start_label
, finish_label
);
3578 // We have to move the default case to the end, so that we
3579 // only use it if all the other tests fail.
3581 default_start_label
= start_label
;
3582 default_finish_label
= finish_label
;
3586 if (default_case
!= NULL
)
3587 default_case
->lower(b
, val_temp
, default_start_label
,
3588 default_finish_label
);
3594 Case_clauses::determine_types(Type
* type
)
3596 for (Clauses::iterator p
= this->clauses_
.begin();
3597 p
!= this->clauses_
.end();
3599 p
->determine_types(type
);
3602 // Check types. Returns false if there was an error.
3605 Case_clauses::check_types(Type
* type
)
3608 for (Clauses::iterator p
= this->clauses_
.begin();
3609 p
!= this->clauses_
.end();
3612 if (!p
->check_types(type
))
3618 // Return true if these clauses may fall through to the statements
3619 // following the switch statement.
3622 Case_clauses::may_fall_through() const
3624 bool found_default
= false;
3625 for (Clauses::const_iterator p
= this->clauses_
.begin();
3626 p
!= this->clauses_
.end();
3629 if (p
->may_fall_through() && !p
->is_fallthrough())
3631 if (p
->is_default())
3632 found_default
= true;
3634 return !found_default
;
3637 // Convert the cases to the backend representation. This sets
3638 // *ALL_CASES and *ALL_STATEMENTS.
3641 Case_clauses::get_backend(Translate_context
* context
,
3642 Unnamed_label
* break_label
,
3643 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
3644 std::vector
<Bstatement
*>* all_statements
) const
3646 Case_constants case_constants
;
3648 size_t c
= this->clauses_
.size();
3649 all_cases
->resize(c
);
3650 all_statements
->resize(c
);
3653 for (Clauses::const_iterator p
= this->clauses_
.begin();
3654 p
!= this->clauses_
.end();
3657 std::vector
<Bexpression
*> cases
;
3658 Bstatement
* stat
= p
->get_backend(context
, break_label
, &case_constants
,
3660 (*all_cases
)[i
].swap(cases
);
3661 (*all_statements
)[i
] = stat
;
3665 // Dump the AST representation for case clauses (from a switch statement)
3668 Case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
3670 for (Clauses::const_iterator p
= this->clauses_
.begin();
3671 p
!= this->clauses_
.end();
3673 p
->dump_clause(ast_dump_context
);
3676 // A constant switch statement. A Switch_statement is lowered to this
3677 // when all the cases are constants.
3679 class Constant_switch_statement
: public Statement
3682 Constant_switch_statement(Expression
* val
, Case_clauses
* clauses
,
3683 Unnamed_label
* break_label
,
3685 : Statement(STATEMENT_CONSTANT_SWITCH
, location
),
3686 val_(val
), clauses_(clauses
), break_label_(break_label
)
3691 do_traverse(Traverse
*);
3694 do_determine_types();
3697 do_check_types(Gogo
*);
3700 do_may_fall_through() const;
3703 do_get_backend(Translate_context
*);
3706 do_dump_statement(Ast_dump_context
*) const;
3709 // The value to switch on.
3711 // The case clauses.
3712 Case_clauses
* clauses_
;
3713 // The break label, if needed.
3714 Unnamed_label
* break_label_
;
3720 Constant_switch_statement::do_traverse(Traverse
* traverse
)
3722 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3723 return TRAVERSE_EXIT
;
3724 return this->clauses_
->traverse(traverse
);
3730 Constant_switch_statement::do_determine_types()
3732 this->val_
->determine_type_no_context();
3733 this->clauses_
->determine_types(this->val_
->type());
3739 Constant_switch_statement::do_check_types(Gogo
*)
3741 if (!this->clauses_
->check_types(this->val_
->type()))
3742 this->set_is_error();
3745 // Return whether this switch may fall through.
3748 Constant_switch_statement::do_may_fall_through() const
3750 if (this->clauses_
== NULL
)
3753 // If we have a break label, then some case needed it. That implies
3754 // that the switch statement as a whole can fall through.
3755 if (this->break_label_
!= NULL
)
3758 return this->clauses_
->may_fall_through();
3761 // Convert to GENERIC.
3764 Constant_switch_statement::do_get_backend(Translate_context
* context
)
3766 tree switch_val_tree
= this->val_
->get_tree(context
);
3767 Bexpression
* switch_val_expr
= tree_to_expr(switch_val_tree
);
3769 Unnamed_label
* break_label
= this->break_label_
;
3770 if (break_label
== NULL
)
3771 break_label
= new Unnamed_label(this->location());
3773 std::vector
<std::vector
<Bexpression
*> > all_cases
;
3774 std::vector
<Bstatement
*> all_statements
;
3775 this->clauses_
->get_backend(context
, break_label
, &all_cases
,
3778 Bstatement
* switch_statement
;
3779 switch_statement
= context
->backend()->switch_statement(switch_val_expr
,
3783 Bstatement
* ldef
= break_label
->get_definition(context
);
3784 return context
->backend()->compound_statement(switch_statement
, ldef
);
3787 // Dump the AST representation for a constant switch statement.
3790 Constant_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3793 ast_dump_context
->print_indent();
3794 ast_dump_context
->ostream() << "switch ";
3795 ast_dump_context
->dump_expression(this->val_
);
3797 if (ast_dump_context
->dump_subblocks())
3799 ast_dump_context
->ostream() << " {" << std::endl
;
3800 this->clauses_
->dump_clauses(ast_dump_context
);
3801 ast_dump_context
->ostream() << "}";
3804 ast_dump_context
->ostream() << std::endl
;
3807 // Class Switch_statement.
3812 Switch_statement::do_traverse(Traverse
* traverse
)
3814 if (this->val_
!= NULL
)
3816 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3817 return TRAVERSE_EXIT
;
3819 return this->clauses_
->traverse(traverse
);
3822 // Lower a Switch_statement to a Constant_switch_statement or a series
3823 // of if statements.
3826 Switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
3827 Statement_inserter
*)
3829 Location loc
= this->location();
3831 if (this->val_
!= NULL
3832 && (this->val_
->is_error_expression()
3833 || this->val_
->type()->is_error()))
3834 return Statement::make_error_statement(loc
);
3836 if (this->val_
!= NULL
3837 && this->val_
->type()->integer_type() != NULL
3838 && !this->clauses_
->empty()
3839 && this->clauses_
->is_constant())
3840 return new Constant_switch_statement(this->val_
, this->clauses_
,
3841 this->break_label_
, loc
);
3843 if (this->val_
!= NULL
3844 && !this->val_
->type()->is_comparable()
3845 && !Type::are_compatible_for_comparison(true, this->val_
->type(),
3846 Type::make_nil_type(), NULL
))
3848 error_at(this->val_
->location(),
3849 "cannot switch on value whose type that may not be compared");
3850 return Statement::make_error_statement(loc
);
3853 Block
* b
= new Block(enclosing
, loc
);
3855 if (this->clauses_
->empty())
3857 Expression
* val
= this->val_
;
3859 val
= Expression::make_boolean(true, loc
);
3860 return Statement::make_statement(val
, true);
3863 // var val_temp VAL_TYPE = VAL
3864 Expression
* val
= this->val_
;
3866 val
= Expression::make_boolean(true, loc
);
3867 Temporary_statement
* val_temp
= Statement::make_temporary(NULL
, val
, loc
);
3868 b
->add_statement(val_temp
);
3870 this->clauses_
->lower(b
, val_temp
, this->break_label());
3872 Statement
* s
= Statement::make_unnamed_label_statement(this->break_label_
);
3873 b
->add_statement(s
);
3875 return Statement::make_block_statement(b
, loc
);
3878 // Return the break label for this switch statement, creating it if
3882 Switch_statement::break_label()
3884 if (this->break_label_
== NULL
)
3885 this->break_label_
= new Unnamed_label(this->location());
3886 return this->break_label_
;
3889 // Dump the AST representation for a switch statement.
3892 Switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3894 ast_dump_context
->print_indent();
3895 ast_dump_context
->ostream() << "switch ";
3896 if (this->val_
!= NULL
)
3898 ast_dump_context
->dump_expression(this->val_
);
3900 if (ast_dump_context
->dump_subblocks())
3902 ast_dump_context
->ostream() << " {" << std::endl
;
3903 this->clauses_
->dump_clauses(ast_dump_context
);
3904 ast_dump_context
->print_indent();
3905 ast_dump_context
->ostream() << "}";
3907 ast_dump_context
->ostream() << std::endl
;
3910 // Make a switch statement.
3913 Statement::make_switch_statement(Expression
* val
, Location location
)
3915 return new Switch_statement(val
, location
);
3918 // Class Type_case_clauses::Type_case_clause.
3923 Type_case_clauses::Type_case_clause::traverse(Traverse
* traverse
)
3925 if (!this->is_default_
3926 && ((traverse
->traverse_mask()
3927 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3928 && Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3929 return TRAVERSE_EXIT
;
3930 if (this->statements_
!= NULL
)
3931 return this->statements_
->traverse(traverse
);
3932 return TRAVERSE_CONTINUE
;
3935 // Lower one clause in a type switch. Add statements to the block B.
3936 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3937 // BREAK_LABEL is the label at the end of the type switch.
3938 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3942 Type_case_clauses::Type_case_clause::lower(Type
* switch_val_type
,
3944 Temporary_statement
* descriptor_temp
,
3945 Unnamed_label
* break_label
,
3946 Unnamed_label
** stmts_label
) const
3948 Location loc
= this->location_
;
3950 Unnamed_label
* next_case_label
= NULL
;
3951 if (!this->is_default_
)
3953 Type
* type
= this->type_
;
3956 if (switch_val_type
->interface_type() != NULL
3957 && !type
->is_nil_constant_as_type()
3958 && type
->interface_type() == NULL
3959 && !switch_val_type
->interface_type()->implements_interface(type
,
3963 error_at(this->location_
, "impossible type switch case");
3965 error_at(this->location_
, "impossible type switch case (%s)",
3969 Expression
* ref
= Expression::make_temporary_reference(descriptor_temp
,
3973 // The language permits case nil, which is of course a constant
3974 // rather than a type. It will appear here as an invalid
3976 if (type
->is_nil_constant_as_type())
3977 cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3978 Expression::make_nil(loc
),
3981 cond
= Runtime::make_call((type
->interface_type() == NULL
3982 ? Runtime::IFACETYPEEQ
3983 : Runtime::IFACEI2TP
),
3985 Expression::make_type_descriptor(type
, loc
),
3988 Unnamed_label
* dest
;
3989 if (!this->is_fallthrough_
)
3991 // if !COND { goto NEXT_CASE_LABEL }
3992 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3993 dest
= next_case_label
;
3994 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3998 // if COND { goto STMTS_LABEL }
3999 go_assert(stmts_label
!= NULL
);
4000 if (*stmts_label
== NULL
)
4001 *stmts_label
= new Unnamed_label(Linemap::unknown_location());
4002 dest
= *stmts_label
;
4004 Block
* then_block
= new Block(b
, loc
);
4005 Statement
* s
= Statement::make_goto_unnamed_statement(dest
, loc
);
4006 then_block
->add_statement(s
);
4007 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
4008 b
->add_statement(s
);
4011 if (this->statements_
!= NULL
4012 || (!this->is_fallthrough_
4013 && stmts_label
!= NULL
4014 && *stmts_label
!= NULL
))
4016 go_assert(!this->is_fallthrough_
);
4017 if (stmts_label
!= NULL
&& *stmts_label
!= NULL
)
4019 go_assert(!this->is_default_
);
4020 if (this->statements_
!= NULL
)
4021 (*stmts_label
)->set_location(this->statements_
->start_location());
4022 Statement
* s
= Statement::make_unnamed_label_statement(*stmts_label
);
4023 b
->add_statement(s
);
4024 *stmts_label
= NULL
;
4026 if (this->statements_
!= NULL
)
4027 b
->add_statement(Statement::make_block_statement(this->statements_
,
4031 if (this->is_fallthrough_
)
4032 go_assert(next_case_label
== NULL
);
4035 Location gloc
= (this->statements_
== NULL
4037 : this->statements_
->end_location());
4038 b
->add_statement(Statement::make_goto_unnamed_statement(break_label
,
4040 if (next_case_label
!= NULL
)
4043 Statement::make_unnamed_label_statement(next_case_label
);
4044 b
->add_statement(s
);
4049 // Dump the AST representation for a type case clause
4052 Type_case_clauses::Type_case_clause::dump_clause(
4053 Ast_dump_context
* ast_dump_context
) const
4055 ast_dump_context
->print_indent();
4056 if (this->is_default_
)
4058 ast_dump_context
->ostream() << "default:";
4062 ast_dump_context
->ostream() << "case ";
4063 ast_dump_context
->dump_type(this->type_
);
4064 ast_dump_context
->ostream() << ":" ;
4066 ast_dump_context
->dump_block(this->statements_
);
4067 if (this->is_fallthrough_
)
4069 ast_dump_context
->print_indent();
4070 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
4074 // Class Type_case_clauses.
4079 Type_case_clauses::traverse(Traverse
* traverse
)
4081 for (Type_clauses::iterator p
= this->clauses_
.begin();
4082 p
!= this->clauses_
.end();
4085 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4086 return TRAVERSE_EXIT
;
4088 return TRAVERSE_CONTINUE
;
4091 // Check for duplicate types.
4094 Type_case_clauses::check_duplicates() const
4096 typedef Unordered_set_hash(const Type
*, Type_hash_identical
,
4097 Type_identical
) Types_seen
;
4098 Types_seen types_seen
;
4099 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4100 p
!= this->clauses_
.end();
4103 Type
* t
= p
->type();
4106 if (t
->is_nil_constant_as_type())
4107 t
= Type::make_nil_type();
4108 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
.insert(t
);
4110 error_at(p
->location(), "duplicate type in switch");
4114 // Lower the clauses in a type switch. Add statements to the block B.
4115 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4116 // BREAK_LABEL is the label at the end of the type switch.
4119 Type_case_clauses::lower(Type
* switch_val_type
, Block
* b
,
4120 Temporary_statement
* descriptor_temp
,
4121 Unnamed_label
* break_label
) const
4123 const Type_case_clause
* default_case
= NULL
;
4125 Unnamed_label
* stmts_label
= NULL
;
4126 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4127 p
!= this->clauses_
.end();
4130 if (!p
->is_default())
4131 p
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4135 // We are generating a series of tests, which means that we
4136 // need to move the default case to the end.
4140 go_assert(stmts_label
== NULL
);
4142 if (default_case
!= NULL
)
4143 default_case
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4147 // Dump the AST representation for case clauses (from a switch statement)
4150 Type_case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4152 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4153 p
!= this->clauses_
.end();
4155 p
->dump_clause(ast_dump_context
);
4158 // Class Type_switch_statement.
4163 Type_switch_statement::do_traverse(Traverse
* traverse
)
4165 if (this->var_
== NULL
)
4167 if (this->traverse_expression(traverse
, &this->expr_
) == TRAVERSE_EXIT
)
4168 return TRAVERSE_EXIT
;
4170 if (this->clauses_
!= NULL
)
4171 return this->clauses_
->traverse(traverse
);
4172 return TRAVERSE_CONTINUE
;
4175 // Lower a type switch statement to a series of if statements. The gc
4176 // compiler is able to generate a table in some cases. However, that
4177 // does not work for us because we may have type descriptors in
4178 // different shared libraries, so we can't compare them with simple
4179 // equality testing.
4182 Type_switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
4183 Statement_inserter
*)
4185 const Location loc
= this->location();
4187 if (this->clauses_
!= NULL
)
4188 this->clauses_
->check_duplicates();
4190 Block
* b
= new Block(enclosing
, loc
);
4192 Type
* val_type
= (this->var_
!= NULL
4193 ? this->var_
->var_value()->type()
4194 : this->expr_
->type());
4196 if (val_type
->interface_type() == NULL
)
4198 if (!val_type
->is_error())
4199 this->report_error(_("cannot type switch on non-interface value"));
4200 return Statement::make_error_statement(loc
);
4203 // var descriptor_temp DESCRIPTOR_TYPE
4204 Type
* descriptor_type
= Type::make_type_descriptor_ptr_type();
4205 Temporary_statement
* descriptor_temp
=
4206 Statement::make_temporary(descriptor_type
, NULL
, loc
);
4207 b
->add_statement(descriptor_temp
);
4209 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4211 bool is_empty
= val_type
->interface_type()->is_empty();
4213 if (this->var_
== NULL
)
4216 ref
= Expression::make_var_reference(this->var_
, loc
);
4217 Expression
* call
= Runtime::make_call((is_empty
4218 ? Runtime::EFACETYPE
4219 : Runtime::IFACETYPE
),
4221 Temporary_reference_expression
* lhs
=
4222 Expression::make_temporary_reference(descriptor_temp
, loc
);
4223 lhs
->set_is_lvalue();
4224 Statement
* s
= Statement::make_assignment(lhs
, call
, loc
);
4225 b
->add_statement(s
);
4227 if (this->clauses_
!= NULL
)
4228 this->clauses_
->lower(val_type
, b
, descriptor_temp
, this->break_label());
4230 s
= Statement::make_unnamed_label_statement(this->break_label_
);
4231 b
->add_statement(s
);
4233 return Statement::make_block_statement(b
, loc
);
4236 // Return the break label for this type switch statement, creating it
4240 Type_switch_statement::break_label()
4242 if (this->break_label_
== NULL
)
4243 this->break_label_
= new Unnamed_label(this->location());
4244 return this->break_label_
;
4247 // Dump the AST representation for a type switch statement
4250 Type_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
4253 ast_dump_context
->print_indent();
4254 ast_dump_context
->ostream() << "switch " << this->var_
->name() << " = ";
4255 ast_dump_context
->dump_expression(this->expr_
);
4256 ast_dump_context
->ostream() << " .(type)";
4257 if (ast_dump_context
->dump_subblocks())
4259 ast_dump_context
->ostream() << " {" << std::endl
;
4260 this->clauses_
->dump_clauses(ast_dump_context
);
4261 ast_dump_context
->ostream() << "}";
4263 ast_dump_context
->ostream() << std::endl
;
4266 // Make a type switch statement.
4268 Type_switch_statement
*
4269 Statement::make_type_switch_statement(Named_object
* var
, Expression
* expr
,
4272 return new Type_switch_statement(var
, expr
, location
);
4275 // Class Send_statement.
4280 Send_statement::do_traverse(Traverse
* traverse
)
4282 if (this->traverse_expression(traverse
, &this->channel_
) == TRAVERSE_EXIT
)
4283 return TRAVERSE_EXIT
;
4284 return this->traverse_expression(traverse
, &this->val_
);
4290 Send_statement::do_determine_types()
4292 this->channel_
->determine_type_no_context();
4293 Type
* type
= this->channel_
->type();
4294 Type_context context
;
4295 if (type
->channel_type() != NULL
)
4296 context
.type
= type
->channel_type()->element_type();
4297 this->val_
->determine_type(&context
);
4303 Send_statement::do_check_types(Gogo
*)
4305 Type
* type
= this->channel_
->type();
4306 if (type
->is_error())
4308 this->set_is_error();
4311 Channel_type
* channel_type
= type
->channel_type();
4312 if (channel_type
== NULL
)
4314 error_at(this->location(), "left operand of %<<-%> must be channel");
4315 this->set_is_error();
4318 Type
* element_type
= channel_type
->element_type();
4319 if (!Type::are_assignable(element_type
, this->val_
->type(), NULL
))
4321 this->report_error(_("incompatible types in send"));
4324 if (!channel_type
->may_send())
4326 this->report_error(_("invalid send on receive-only channel"));
4331 // Convert a send statement to the backend representation.
4334 Send_statement::do_get_backend(Translate_context
* context
)
4336 Location loc
= this->location();
4338 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
4339 Type
* element_type
= channel_type
->element_type();
4340 Expression
* val
= Expression::make_cast(element_type
, this->val_
, loc
);
4343 bool can_take_address
;
4344 switch (element_type
->base()->classification())
4346 case Type::TYPE_BOOLEAN
:
4347 case Type::TYPE_INTEGER
:
4348 case Type::TYPE_FUNCTION
:
4349 case Type::TYPE_POINTER
:
4350 case Type::TYPE_MAP
:
4351 case Type::TYPE_CHANNEL
:
4353 can_take_address
= false;
4356 case Type::TYPE_FLOAT
:
4357 case Type::TYPE_COMPLEX
:
4358 case Type::TYPE_STRING
:
4359 case Type::TYPE_INTERFACE
:
4361 can_take_address
= false;
4364 case Type::TYPE_STRUCT
:
4366 can_take_address
= true;
4369 case Type::TYPE_ARRAY
:
4371 can_take_address
= !element_type
->is_slice_type();
4375 case Type::TYPE_ERROR
:
4376 case Type::TYPE_VOID
:
4377 case Type::TYPE_SINK
:
4378 case Type::TYPE_NIL
:
4379 case Type::TYPE_NAMED
:
4380 case Type::TYPE_FORWARD
:
4381 go_assert(saw_errors());
4382 return context
->backend()->error_statement();
4385 // Only try to take the address of a variable. We have already
4386 // moved variables to the heap, so this should not cause that to
4387 // happen unnecessarily.
4388 if (can_take_address
4389 && val
->var_expression() == NULL
4390 && val
->temporary_reference_expression() == NULL
)
4391 can_take_address
= false;
4393 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
4396 Runtime::Function code
;
4397 Bstatement
* btemp
= NULL
;
4400 // Type is small enough to handle as uint64.
4401 code
= Runtime::SEND_SMALL
;
4402 val
= Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
4405 else if (can_take_address
)
4407 // Must pass address of value. The function doesn't change the
4408 // value, so just take its address directly.
4409 code
= Runtime::SEND_BIG
;
4410 val
= Expression::make_unary(OPERATOR_AND
, val
, loc
);
4414 // Must pass address of value, but the value is small enough
4415 // that it might be in registers. Copy value into temporary
4416 // variable to take address.
4417 code
= Runtime::SEND_BIG
;
4418 Temporary_statement
* temp
= Statement::make_temporary(element_type
,
4420 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
4421 val
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
4422 btemp
= temp
->get_backend(context
);
4425 Expression
* call
= Runtime::make_call(code
, loc
, 3, td
, this->channel_
, val
);
4427 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4428 Bexpression
* bcall
= tree_to_expr(call
->get_tree(context
));
4429 Bstatement
* s
= context
->backend()->expression_statement(bcall
);
4434 return context
->backend()->compound_statement(btemp
, s
);
4437 // Dump the AST representation for a send statement
4440 Send_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4442 ast_dump_context
->print_indent();
4443 ast_dump_context
->dump_expression(this->channel_
);
4444 ast_dump_context
->ostream() << " <- ";
4445 ast_dump_context
->dump_expression(this->val_
);
4446 ast_dump_context
->ostream() << std::endl
;
4449 // Make a send statement.
4452 Statement::make_send_statement(Expression
* channel
, Expression
* val
,
4455 return new Send_statement(channel
, val
, location
);
4458 // Class Select_clauses::Select_clause.
4463 Select_clauses::Select_clause::traverse(Traverse
* traverse
)
4465 if (!this->is_lowered_
4466 && (traverse
->traverse_mask()
4467 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
4469 if (this->channel_
!= NULL
)
4471 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
4472 return TRAVERSE_EXIT
;
4474 if (this->val_
!= NULL
)
4476 if (Expression::traverse(&this->val_
, traverse
) == TRAVERSE_EXIT
)
4477 return TRAVERSE_EXIT
;
4479 if (this->closed_
!= NULL
)
4481 if (Expression::traverse(&this->closed_
, traverse
) == TRAVERSE_EXIT
)
4482 return TRAVERSE_EXIT
;
4485 if (this->statements_
!= NULL
)
4487 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
4488 return TRAVERSE_EXIT
;
4490 return TRAVERSE_CONTINUE
;
4493 // Lowering. We call a function to register this clause, and arrange
4494 // to set any variables in any receive clause.
4497 Select_clauses::Select_clause::lower(Gogo
* gogo
, Named_object
* function
,
4498 Block
* b
, Temporary_statement
* sel
)
4500 Location loc
= this->location_
;
4502 Expression
* selref
= Expression::make_temporary_reference(sel
, loc
);
4505 mpz_init_set_ui(ival
, this->index_
);
4506 Expression
* index_expr
= Expression::make_integer(&ival
, NULL
, loc
);
4509 if (this->is_default_
)
4511 go_assert(this->channel_
== NULL
&& this->val_
== NULL
);
4512 this->lower_default(b
, selref
, index_expr
);
4513 this->is_lowered_
= true;
4517 // Evaluate the channel before the select statement.
4518 Temporary_statement
* channel_temp
= Statement::make_temporary(NULL
,
4521 b
->add_statement(channel_temp
);
4522 Expression
* chanref
= Expression::make_temporary_reference(channel_temp
,
4526 this->lower_send(b
, selref
, chanref
, index_expr
);
4528 this->lower_recv(gogo
, function
, b
, selref
, chanref
, index_expr
);
4530 // Now all references should be handled through the statements, not
4532 this->is_lowered_
= true;
4537 // Lower a default clause in a select statement.
4540 Select_clauses::Select_clause::lower_default(Block
* b
, Expression
* selref
,
4541 Expression
* index_expr
)
4543 Location loc
= this->location_
;
4544 Expression
* call
= Runtime::make_call(Runtime::SELECTDEFAULT
, loc
, 2, selref
,
4546 b
->add_statement(Statement::make_statement(call
, true));
4549 // Lower a send clause in a select statement.
4552 Select_clauses::Select_clause::lower_send(Block
* b
, Expression
* selref
,
4553 Expression
* chanref
,
4554 Expression
* index_expr
)
4556 Location loc
= this->location_
;
4558 Channel_type
* ct
= this->channel_
->type()->channel_type();
4562 Type
* valtype
= ct
->element_type();
4564 // Note that copying the value to a temporary here means that we
4565 // evaluate the send values in the required order.
4566 Temporary_statement
* val
= Statement::make_temporary(valtype
, this->val_
,
4568 b
->add_statement(val
);
4570 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4571 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4573 Expression
* call
= Runtime::make_call(Runtime::SELECTSEND
, loc
, 4, selref
,
4574 chanref
, valaddr
, index_expr
);
4575 b
->add_statement(Statement::make_statement(call
, true));
4578 // Lower a receive clause in a select statement.
4581 Select_clauses::Select_clause::lower_recv(Gogo
* gogo
, Named_object
* function
,
4582 Block
* b
, Expression
* selref
,
4583 Expression
* chanref
,
4584 Expression
* index_expr
)
4586 Location loc
= this->location_
;
4588 Channel_type
* ct
= this->channel_
->type()->channel_type();
4592 Type
* valtype
= ct
->element_type();
4593 Temporary_statement
* val
= Statement::make_temporary(valtype
, NULL
, loc
);
4594 b
->add_statement(val
);
4596 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4597 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4599 Temporary_statement
* closed_temp
= NULL
;
4602 if (this->closed_
== NULL
&& this->closedvar_
== NULL
)
4603 call
= Runtime::make_call(Runtime::SELECTRECV
, loc
, 4, selref
, chanref
,
4604 valaddr
, index_expr
);
4607 closed_temp
= Statement::make_temporary(Type::lookup_bool_type(), NULL
,
4609 b
->add_statement(closed_temp
);
4610 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4612 Expression
* caddr
= Expression::make_unary(OPERATOR_AND
, cref
, loc
);
4613 call
= Runtime::make_call(Runtime::SELECTRECV2
, loc
, 5, selref
, chanref
,
4614 valaddr
, caddr
, index_expr
);
4617 b
->add_statement(Statement::make_statement(call
, true));
4619 // If the block of statements is executed, arrange for the received
4620 // value to move from VAL to the place where the statements expect
4625 if (this->var_
!= NULL
)
4627 go_assert(this->val_
== NULL
);
4628 valref
= Expression::make_temporary_reference(val
, loc
);
4629 this->var_
->var_value()->set_init(valref
);
4630 this->var_
->var_value()->clear_type_from_chan_element();
4632 else if (this->val_
!= NULL
&& !this->val_
->is_sink_expression())
4634 init
= new Block(b
, loc
);
4635 valref
= Expression::make_temporary_reference(val
, loc
);
4636 init
->add_statement(Statement::make_assignment(this->val_
, valref
, loc
));
4639 if (this->closedvar_
!= NULL
)
4641 go_assert(this->closed_
== NULL
);
4642 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4644 this->closedvar_
->var_value()->set_init(cref
);
4646 else if (this->closed_
!= NULL
&& !this->closed_
->is_sink_expression())
4649 init
= new Block(b
, loc
);
4650 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4652 init
->add_statement(Statement::make_assignment(this->closed_
, cref
,
4658 gogo
->lower_block(function
, init
);
4660 if (this->statements_
!= NULL
)
4661 init
->add_statement(Statement::make_block_statement(this->statements_
,
4663 this->statements_
= init
;
4670 Select_clauses::Select_clause::determine_types()
4672 go_assert(this->is_lowered_
);
4673 if (this->statements_
!= NULL
)
4674 this->statements_
->determine_types();
4680 Select_clauses::Select_clause::check_types()
4682 if (this->is_default_
)
4685 Channel_type
* ct
= this->channel_
->type()->channel_type();
4688 error_at(this->channel_
->location(), "expected channel");
4692 if (this->is_send_
&& !ct
->may_send())
4693 error_at(this->location(), "invalid send on receive-only channel");
4694 else if (!this->is_send_
&& !ct
->may_receive())
4695 error_at(this->location(), "invalid receive on send-only channel");
4698 // Whether this clause may fall through to the statement which follows
4699 // the overall select statement.
4702 Select_clauses::Select_clause::may_fall_through() const
4704 if (this->statements_
== NULL
)
4706 return this->statements_
->may_fall_through();
4709 // Return the backend representation for the statements to execute.
4712 Select_clauses::Select_clause::get_statements_backend(
4713 Translate_context
* context
)
4715 if (this->statements_
== NULL
)
4717 Bblock
* bblock
= this->statements_
->get_backend(context
);
4718 return context
->backend()->block_statement(bblock
);
4721 // Dump the AST representation for a select case clause
4724 Select_clauses::Select_clause::dump_clause(
4725 Ast_dump_context
* ast_dump_context
) const
4727 ast_dump_context
->print_indent();
4728 if (this->is_default_
)
4730 ast_dump_context
->ostream() << "default:";
4734 ast_dump_context
->ostream() << "case " ;
4737 ast_dump_context
->dump_expression(this->channel_
);
4738 ast_dump_context
->ostream() << " <- " ;
4739 if (this->val_
!= NULL
)
4740 ast_dump_context
->dump_expression(this->val_
);
4744 if (this->val_
!= NULL
)
4745 ast_dump_context
->dump_expression(this->val_
);
4746 if (this->closed_
!= NULL
)
4748 // FIXME: can val_ == NULL and closed_ ! = NULL?
4749 ast_dump_context
->ostream() << " , " ;
4750 ast_dump_context
->dump_expression(this->closed_
);
4752 if (this->closedvar_
!= NULL
|| this->var_
!= NULL
)
4753 ast_dump_context
->ostream() << " := " ;
4755 ast_dump_context
->ostream() << " <- " ;
4756 ast_dump_context
->dump_expression(this->channel_
);
4758 ast_dump_context
->ostream() << ":" ;
4760 ast_dump_context
->dump_block(this->statements_
);
4763 // Class Select_clauses.
4768 Select_clauses::traverse(Traverse
* traverse
)
4770 for (Clauses::iterator p
= this->clauses_
.begin();
4771 p
!= this->clauses_
.end();
4774 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4775 return TRAVERSE_EXIT
;
4777 return TRAVERSE_CONTINUE
;
4780 // Lowering. Here we pull out the channel and the send values, to
4781 // enforce the order of evaluation. We also add explicit send and
4782 // receive statements to the clauses.
4785 Select_clauses::lower(Gogo
* gogo
, Named_object
* function
, Block
* b
,
4786 Temporary_statement
* sel
)
4788 for (Clauses::iterator p
= this->clauses_
.begin();
4789 p
!= this->clauses_
.end();
4791 p
->lower(gogo
, function
, b
, sel
);
4797 Select_clauses::determine_types()
4799 for (Clauses::iterator p
= this->clauses_
.begin();
4800 p
!= this->clauses_
.end();
4802 p
->determine_types();
4808 Select_clauses::check_types()
4810 for (Clauses::iterator p
= this->clauses_
.begin();
4811 p
!= this->clauses_
.end();
4816 // Return whether these select clauses fall through to the statement
4817 // following the overall select statement.
4820 Select_clauses::may_fall_through() const
4822 for (Clauses::const_iterator p
= this->clauses_
.begin();
4823 p
!= this->clauses_
.end();
4825 if (p
->may_fall_through())
4830 // Convert to the backend representation. We have already accumulated
4831 // all the select information. Now we call selectgo, which will
4832 // return the index of the clause to execute.
4835 Select_clauses::get_backend(Translate_context
* context
,
4836 Temporary_statement
* sel
,
4837 Unnamed_label
*break_label
,
4840 size_t count
= this->clauses_
.size();
4841 std::vector
<std::vector
<Bexpression
*> > cases(count
);
4842 std::vector
<Bstatement
*> clauses(count
);
4844 Type
* int32_type
= Type::lookup_integer_type("int32");
4847 for (Clauses::iterator p
= this->clauses_
.begin();
4848 p
!= this->clauses_
.end();
4851 int index
= p
->index();
4853 mpz_init_set_ui(ival
, index
);
4854 Expression
* index_expr
= Expression::make_integer(&ival
, int32_type
,
4857 cases
[i
].push_back(tree_to_expr(index_expr
->get_tree(context
)));
4859 Bstatement
* s
= p
->get_statements_backend(context
);
4860 Location gloc
= (p
->statements() == NULL
4862 : p
->statements()->end_location());
4863 Bstatement
* g
= break_label
->get_goto(context
, gloc
);
4868 clauses
[i
] = context
->backend()->compound_statement(s
, g
);
4871 Expression
* selref
= Expression::make_temporary_reference(sel
, location
);
4872 Expression
* call
= Runtime::make_call(Runtime::SELECTGO
, location
, 1,
4874 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4875 Bexpression
* bcall
= tree_to_expr(call
->get_tree(context
));
4878 return context
->backend()->expression_statement(bcall
);
4880 std::vector
<Bstatement
*> statements
;
4881 statements
.reserve(2);
4883 Bstatement
* switch_stmt
= context
->backend()->switch_statement(bcall
,
4887 statements
.push_back(switch_stmt
);
4889 Bstatement
* ldef
= break_label
->get_definition(context
);
4890 statements
.push_back(ldef
);
4892 return context
->backend()->statement_list(statements
);
4894 // Dump the AST representation for select clauses.
4897 Select_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4899 for (Clauses::const_iterator p
= this->clauses_
.begin();
4900 p
!= this->clauses_
.end();
4902 p
->dump_clause(ast_dump_context
);
4905 // Class Select_statement.
4907 // Return the break label for this switch statement, creating it if
4911 Select_statement::break_label()
4913 if (this->break_label_
== NULL
)
4914 this->break_label_
= new Unnamed_label(this->location());
4915 return this->break_label_
;
4918 // Lower a select statement. This will still return a select
4919 // statement, but it will be modified to implement the order of
4920 // evaluation rules, and to include the send and receive statements as
4921 // explicit statements in the clauses.
4924 Select_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
4925 Block
* enclosing
, Statement_inserter
*)
4927 if (this->is_lowered_
)
4930 Location loc
= this->location();
4932 Block
* b
= new Block(enclosing
, loc
);
4934 go_assert(this->sel_
== NULL
);
4937 mpz_init_set_ui(ival
, this->clauses_
->size());
4938 Expression
* size_expr
= Expression::make_integer(&ival
, NULL
, loc
);
4941 Expression
* call
= Runtime::make_call(Runtime::NEWSELECT
, loc
, 1, size_expr
);
4943 this->sel_
= Statement::make_temporary(NULL
, call
, loc
);
4944 b
->add_statement(this->sel_
);
4946 this->clauses_
->lower(gogo
, function
, b
, this->sel_
);
4947 this->is_lowered_
= true;
4948 b
->add_statement(this);
4950 return Statement::make_block_statement(b
, loc
);
4953 // Return the backend representation for a select statement.
4956 Select_statement::do_get_backend(Translate_context
* context
)
4958 return this->clauses_
->get_backend(context
, this->sel_
, this->break_label(),
4962 // Dump the AST representation for a select statement.
4965 Select_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4967 ast_dump_context
->print_indent();
4968 ast_dump_context
->ostream() << "select";
4969 if (ast_dump_context
->dump_subblocks())
4971 ast_dump_context
->ostream() << " {" << std::endl
;
4972 this->clauses_
->dump_clauses(ast_dump_context
);
4973 ast_dump_context
->ostream() << "}";
4975 ast_dump_context
->ostream() << std::endl
;
4978 // Make a select statement.
4981 Statement::make_select_statement(Location location
)
4983 return new Select_statement(location
);
4986 // Class For_statement.
4991 For_statement::do_traverse(Traverse
* traverse
)
4993 if (this->init_
!= NULL
)
4995 if (this->init_
->traverse(traverse
) == TRAVERSE_EXIT
)
4996 return TRAVERSE_EXIT
;
4998 if (this->cond_
!= NULL
)
5000 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
)
5001 return TRAVERSE_EXIT
;
5003 if (this->post_
!= NULL
)
5005 if (this->post_
->traverse(traverse
) == TRAVERSE_EXIT
)
5006 return TRAVERSE_EXIT
;
5008 return this->statements_
->traverse(traverse
);
5011 // Lower a For_statement into if statements and gotos. Getting rid of
5012 // complex statements make it easier to handle garbage collection.
5015 For_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
5016 Statement_inserter
*)
5019 Location loc
= this->location();
5021 Block
* b
= new Block(enclosing
, this->location());
5022 if (this->init_
!= NULL
)
5024 s
= Statement::make_block_statement(this->init_
,
5025 this->init_
->start_location());
5026 b
->add_statement(s
);
5029 Unnamed_label
* entry
= NULL
;
5030 if (this->cond_
!= NULL
)
5032 entry
= new Unnamed_label(this->location());
5033 b
->add_statement(Statement::make_goto_unnamed_statement(entry
, loc
));
5036 Unnamed_label
* top
= new Unnamed_label(this->location());
5037 b
->add_statement(Statement::make_unnamed_label_statement(top
));
5039 s
= Statement::make_block_statement(this->statements_
,
5040 this->statements_
->start_location());
5041 b
->add_statement(s
);
5043 Location end_loc
= this->statements_
->end_location();
5045 Unnamed_label
* cont
= this->continue_label_
;
5047 b
->add_statement(Statement::make_unnamed_label_statement(cont
));
5049 if (this->post_
!= NULL
)
5051 s
= Statement::make_block_statement(this->post_
,
5052 this->post_
->start_location());
5053 b
->add_statement(s
);
5054 end_loc
= this->post_
->end_location();
5057 if (this->cond_
== NULL
)
5058 b
->add_statement(Statement::make_goto_unnamed_statement(top
, end_loc
));
5061 b
->add_statement(Statement::make_unnamed_label_statement(entry
));
5063 Location cond_loc
= this->cond_
->location();
5064 Block
* then_block
= new Block(b
, cond_loc
);
5065 s
= Statement::make_goto_unnamed_statement(top
, cond_loc
);
5066 then_block
->add_statement(s
);
5068 s
= Statement::make_if_statement(this->cond_
, then_block
, NULL
, cond_loc
);
5069 b
->add_statement(s
);
5072 Unnamed_label
* brk
= this->break_label_
;
5074 b
->add_statement(Statement::make_unnamed_label_statement(brk
));
5076 b
->set_end_location(end_loc
);
5078 return Statement::make_block_statement(b
, loc
);
5081 // Return the break label, creating it if necessary.
5084 For_statement::break_label()
5086 if (this->break_label_
== NULL
)
5087 this->break_label_
= new Unnamed_label(this->location());
5088 return this->break_label_
;
5091 // Return the continue LABEL_EXPR.
5094 For_statement::continue_label()
5096 if (this->continue_label_
== NULL
)
5097 this->continue_label_
= new Unnamed_label(this->location());
5098 return this->continue_label_
;
5101 // Set the break and continue labels a for statement. This is used
5102 // when lowering a for range statement.
5105 For_statement::set_break_continue_labels(Unnamed_label
* break_label
,
5106 Unnamed_label
* continue_label
)
5108 go_assert(this->break_label_
== NULL
&& this->continue_label_
== NULL
);
5109 this->break_label_
= break_label
;
5110 this->continue_label_
= continue_label
;
5113 // Dump the AST representation for a for statement.
5116 For_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5118 if (this->init_
!= NULL
&& ast_dump_context
->dump_subblocks())
5120 ast_dump_context
->print_indent();
5121 ast_dump_context
->indent();
5122 ast_dump_context
->ostream() << "// INIT " << std::endl
;
5123 ast_dump_context
->dump_block(this->init_
);
5124 ast_dump_context
->unindent();
5126 ast_dump_context
->print_indent();
5127 ast_dump_context
->ostream() << "for ";
5128 if (this->cond_
!= NULL
)
5129 ast_dump_context
->dump_expression(this->cond_
);
5131 if (ast_dump_context
->dump_subblocks())
5133 ast_dump_context
->ostream() << " {" << std::endl
;
5134 ast_dump_context
->dump_block(this->statements_
);
5135 if (this->init_
!= NULL
)
5137 ast_dump_context
->print_indent();
5138 ast_dump_context
->ostream() << "// POST " << std::endl
;
5139 ast_dump_context
->dump_block(this->post_
);
5141 ast_dump_context
->unindent();
5143 ast_dump_context
->print_indent();
5144 ast_dump_context
->ostream() << "}";
5147 ast_dump_context
->ostream() << std::endl
;
5150 // Make a for statement.
5153 Statement::make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
5156 return new For_statement(init
, cond
, post
, location
);
5159 // Class For_range_statement.
5164 For_range_statement::do_traverse(Traverse
* traverse
)
5166 if (this->traverse_expression(traverse
, &this->index_var_
) == TRAVERSE_EXIT
)
5167 return TRAVERSE_EXIT
;
5168 if (this->value_var_
!= NULL
)
5170 if (this->traverse_expression(traverse
, &this->value_var_
)
5172 return TRAVERSE_EXIT
;
5174 if (this->traverse_expression(traverse
, &this->range_
) == TRAVERSE_EXIT
)
5175 return TRAVERSE_EXIT
;
5176 return this->statements_
->traverse(traverse
);
5179 // Lower a for range statement. For simplicity we lower this into a
5180 // for statement, which will then be lowered in turn to goto
5184 For_range_statement::do_lower(Gogo
* gogo
, Named_object
*, Block
* enclosing
,
5185 Statement_inserter
*)
5187 Type
* range_type
= this->range_
->type();
5188 if (range_type
->points_to() != NULL
5189 && range_type
->points_to()->array_type() != NULL
5190 && !range_type
->points_to()->is_slice_type())
5191 range_type
= range_type
->points_to();
5194 Type
* value_type
= NULL
;
5195 if (range_type
->array_type() != NULL
)
5197 index_type
= Type::lookup_integer_type("int");
5198 value_type
= range_type
->array_type()->element_type();
5200 else if (range_type
->is_string_type())
5202 index_type
= Type::lookup_integer_type("int");
5203 value_type
= Type::lookup_integer_type("int32");
5205 else if (range_type
->map_type() != NULL
)
5207 index_type
= range_type
->map_type()->key_type();
5208 value_type
= range_type
->map_type()->val_type();
5210 else if (range_type
->channel_type() != NULL
)
5212 index_type
= range_type
->channel_type()->element_type();
5213 if (this->value_var_
!= NULL
)
5215 if (!this->value_var_
->type()->is_error())
5216 this->report_error(_("too many variables for range clause "
5218 return Statement::make_error_statement(this->location());
5223 this->report_error(_("range clause must have "
5224 "array, slice, string, map, or channel type"));
5225 return Statement::make_error_statement(this->location());
5228 Location loc
= this->location();
5229 Block
* temp_block
= new Block(enclosing
, loc
);
5231 Named_object
* range_object
= NULL
;
5232 Temporary_statement
* range_temp
= NULL
;
5233 Var_expression
* ve
= this->range_
->var_expression();
5235 range_object
= ve
->named_object();
5238 range_temp
= Statement::make_temporary(NULL
, this->range_
, loc
);
5239 temp_block
->add_statement(range_temp
);
5240 this->range_
= NULL
;
5243 Temporary_statement
* index_temp
= Statement::make_temporary(index_type
,
5245 temp_block
->add_statement(index_temp
);
5247 Temporary_statement
* value_temp
= NULL
;
5248 if (this->value_var_
!= NULL
)
5250 value_temp
= Statement::make_temporary(value_type
, NULL
, loc
);
5251 temp_block
->add_statement(value_temp
);
5254 Block
* body
= new Block(temp_block
, loc
);
5261 // Arrange to do a loop appropriate for the type. We will produce
5262 // for INIT ; COND ; POST {
5264 // INDEX = INDEX_TEMP
5265 // VALUE = VALUE_TEMP // If there is a value
5266 // original statements
5269 if (range_type
->is_slice_type())
5270 this->lower_range_slice(gogo
, temp_block
, body
, range_object
, range_temp
,
5271 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5273 else if (range_type
->array_type() != NULL
)
5274 this->lower_range_array(gogo
, temp_block
, body
, range_object
, range_temp
,
5275 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5277 else if (range_type
->is_string_type())
5278 this->lower_range_string(gogo
, temp_block
, body
, range_object
, range_temp
,
5279 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5281 else if (range_type
->map_type() != NULL
)
5282 this->lower_range_map(gogo
, temp_block
, body
, range_object
, range_temp
,
5283 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5285 else if (range_type
->channel_type() != NULL
)
5286 this->lower_range_channel(gogo
, temp_block
, body
, range_object
, range_temp
,
5287 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5292 if (iter_init
!= NULL
)
5293 body
->add_statement(Statement::make_block_statement(iter_init
, loc
));
5296 Expression
* index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5297 if (this->value_var_
== NULL
)
5299 assign
= Statement::make_assignment(this->index_var_
, index_ref
, loc
);
5303 Expression_list
* lhs
= new Expression_list();
5304 lhs
->push_back(this->index_var_
);
5305 lhs
->push_back(this->value_var_
);
5307 Expression_list
* rhs
= new Expression_list();
5308 rhs
->push_back(index_ref
);
5309 rhs
->push_back(Expression::make_temporary_reference(value_temp
, loc
));
5311 assign
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5313 body
->add_statement(assign
);
5315 body
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
5317 body
->set_end_location(this->statements_
->end_location());
5319 For_statement
* loop
= Statement::make_for_statement(init
, cond
, post
,
5321 loop
->add_statements(body
);
5322 loop
->set_break_continue_labels(this->break_label_
, this->continue_label_
);
5324 temp_block
->add_statement(loop
);
5326 return Statement::make_block_statement(temp_block
, loc
);
5329 // Return a reference to the range, which may be in RANGE_OBJECT or in
5333 For_range_statement::make_range_ref(Named_object
* range_object
,
5334 Temporary_statement
* range_temp
,
5337 if (range_object
!= NULL
)
5338 return Expression::make_var_reference(range_object
, loc
);
5340 return Expression::make_temporary_reference(range_temp
, loc
);
5343 // Return a call to the predeclared function FUNCNAME passing a
5344 // reference to the temporary variable ARG.
5347 For_range_statement::call_builtin(Gogo
* gogo
, const char* funcname
,
5351 Named_object
* no
= gogo
->lookup_global(funcname
);
5352 go_assert(no
!= NULL
&& no
->is_function_declaration());
5353 Expression
* func
= Expression::make_func_reference(no
, NULL
, loc
);
5354 Expression_list
* params
= new Expression_list();
5355 params
->push_back(arg
);
5356 return Expression::make_call(func
, params
, false, loc
);
5359 // Lower a for range over an array.
5362 For_range_statement::lower_range_array(Gogo
* gogo
,
5365 Named_object
* range_object
,
5366 Temporary_statement
* range_temp
,
5367 Temporary_statement
* index_temp
,
5368 Temporary_statement
* value_temp
,
5374 Location loc
= this->location();
5376 // The loop we generate:
5377 // len_temp := len(range)
5378 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5379 // value_temp = range[index_temp]
5380 // index = index_temp
5381 // value = value_temp
5387 // len_temp = len(range)
5390 Block
* init
= new Block(enclosing
, loc
);
5392 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5393 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5394 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5396 init
->add_statement(len_temp
);
5399 mpz_init_set_ui(zval
, 0UL);
5400 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5403 Temporary_reference_expression
* tref
=
5404 Expression::make_temporary_reference(index_temp
, loc
);
5405 tref
->set_is_lvalue();
5406 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5407 init
->add_statement(s
);
5412 // index_temp < len_temp
5414 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5415 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5416 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5420 // Set *PITER_INIT to
5421 // value_temp = range[index_temp]
5423 Block
* iter_init
= NULL
;
5424 if (value_temp
!= NULL
)
5426 iter_init
= new Block(body_block
, loc
);
5428 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5429 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5430 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, loc
);
5432 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5433 tref
->set_is_lvalue();
5434 s
= Statement::make_assignment(tref
, index
, loc
);
5436 iter_init
->add_statement(s
);
5438 *piter_init
= iter_init
;
5443 Block
* post
= new Block(enclosing
, loc
);
5444 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5445 tref
->set_is_lvalue();
5446 s
= Statement::make_inc_statement(tref
);
5447 post
->add_statement(s
);
5451 // Lower a for range over a slice.
5454 For_range_statement::lower_range_slice(Gogo
* gogo
,
5457 Named_object
* range_object
,
5458 Temporary_statement
* range_temp
,
5459 Temporary_statement
* index_temp
,
5460 Temporary_statement
* value_temp
,
5466 Location loc
= this->location();
5468 // The loop we generate:
5469 // for_temp := range
5470 // len_temp := len(for_temp)
5471 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5472 // value_temp = for_temp[index_temp]
5473 // index = index_temp
5474 // value = value_temp
5478 // Using for_temp means that we don't need to check bounds when
5479 // fetching range_temp[index_temp].
5482 // range_temp := range
5484 // len_temp = len(range_temp)
5487 Block
* init
= new Block(enclosing
, loc
);
5489 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5490 Temporary_statement
* for_temp
= Statement::make_temporary(NULL
, ref
, loc
);
5491 init
->add_statement(for_temp
);
5493 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5494 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5495 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5497 init
->add_statement(len_temp
);
5500 mpz_init_set_ui(zval
, 0UL);
5501 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5504 Temporary_reference_expression
* tref
=
5505 Expression::make_temporary_reference(index_temp
, loc
);
5506 tref
->set_is_lvalue();
5507 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5508 init
->add_statement(s
);
5513 // index_temp < len_temp
5515 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5516 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5517 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5521 // Set *PITER_INIT to
5522 // value_temp = range[index_temp]
5524 Block
* iter_init
= NULL
;
5525 if (value_temp
!= NULL
)
5527 iter_init
= new Block(body_block
, loc
);
5529 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5530 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5531 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, loc
);
5533 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5534 tref
->set_is_lvalue();
5535 s
= Statement::make_assignment(tref
, index
, loc
);
5537 iter_init
->add_statement(s
);
5539 *piter_init
= iter_init
;
5544 Block
* post
= new Block(enclosing
, loc
);
5545 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5546 tref
->set_is_lvalue();
5547 s
= Statement::make_inc_statement(tref
);
5548 post
->add_statement(s
);
5552 // Lower a for range over a string.
5555 For_range_statement::lower_range_string(Gogo
*,
5558 Named_object
* range_object
,
5559 Temporary_statement
* range_temp
,
5560 Temporary_statement
* index_temp
,
5561 Temporary_statement
* value_temp
,
5567 Location loc
= this->location();
5569 // The loop we generate:
5570 // var next_index_temp int
5571 // for index_temp = 0; ; index_temp = next_index_temp {
5572 // next_index_temp, value_temp = stringiter2(range, index_temp)
5573 // if next_index_temp == 0 {
5576 // index = index_temp
5577 // value = value_temp
5582 // var next_index_temp int
5585 Block
* init
= new Block(enclosing
, loc
);
5587 Temporary_statement
* next_index_temp
=
5588 Statement::make_temporary(index_temp
->type(), NULL
, loc
);
5589 init
->add_statement(next_index_temp
);
5592 mpz_init_set_ui(zval
, 0UL);
5593 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5595 Temporary_reference_expression
* ref
=
5596 Expression::make_temporary_reference(index_temp
, loc
);
5597 ref
->set_is_lvalue();
5598 Statement
* s
= Statement::make_assignment(ref
, zexpr
, loc
);
5600 init
->add_statement(s
);
5603 // The loop has no condition.
5607 // Set *PITER_INIT to
5608 // next_index_temp = runtime.stringiter(range, index_temp)
5610 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
5612 // if next_index_temp == 0 {
5616 Block
* iter_init
= new Block(body_block
, loc
);
5618 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5619 Expression
* p2
= Expression::make_temporary_reference(index_temp
, loc
);
5620 Call_expression
* call
= Runtime::make_call((value_temp
== NULL
5621 ? Runtime::STRINGITER
5622 : Runtime::STRINGITER2
),
5625 if (value_temp
== NULL
)
5627 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5628 ref
->set_is_lvalue();
5629 s
= Statement::make_assignment(ref
, call
, loc
);
5633 Expression_list
* lhs
= new Expression_list();
5635 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5636 ref
->set_is_lvalue();
5637 lhs
->push_back(ref
);
5639 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5640 ref
->set_is_lvalue();
5641 lhs
->push_back(ref
);
5643 Expression_list
* rhs
= new Expression_list();
5644 rhs
->push_back(Expression::make_call_result(call
, 0));
5645 rhs
->push_back(Expression::make_call_result(call
, 1));
5647 s
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5649 iter_init
->add_statement(s
);
5651 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5652 zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5654 Expression
* equals
= Expression::make_binary(OPERATOR_EQEQ
, ref
, zexpr
, loc
);
5656 Block
* then_block
= new Block(iter_init
, loc
);
5657 s
= Statement::make_break_statement(this->break_label(), loc
);
5658 then_block
->add_statement(s
);
5660 s
= Statement::make_if_statement(equals
, then_block
, NULL
, loc
);
5661 iter_init
->add_statement(s
);
5663 *piter_init
= iter_init
;
5666 // index_temp = next_index_temp
5668 Block
* post
= new Block(enclosing
, loc
);
5670 Temporary_reference_expression
* lhs
=
5671 Expression::make_temporary_reference(index_temp
, loc
);
5672 lhs
->set_is_lvalue();
5673 Expression
* rhs
= Expression::make_temporary_reference(next_index_temp
, loc
);
5674 s
= Statement::make_assignment(lhs
, rhs
, loc
);
5676 post
->add_statement(s
);
5680 // Lower a for range over a map.
5683 For_range_statement::lower_range_map(Gogo
*,
5686 Named_object
* range_object
,
5687 Temporary_statement
* range_temp
,
5688 Temporary_statement
* index_temp
,
5689 Temporary_statement
* value_temp
,
5695 Location loc
= this->location();
5697 // The runtime uses a struct to handle ranges over a map. The
5698 // struct is four pointers long. The first pointer is NULL when we
5699 // have completed the iteration.
5701 // The loop we generate:
5702 // var hiter map_iteration_struct
5703 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
5704 // mapiter2(hiter, &index_temp, &value_temp)
5705 // index = index_temp
5706 // value = value_temp
5711 // var hiter map_iteration_struct
5712 // runtime.mapiterinit(range, &hiter)
5714 Block
* init
= new Block(enclosing
, loc
);
5716 Type
* map_iteration_type
= Runtime::map_iteration_type();
5717 Temporary_statement
* hiter
= Statement::make_temporary(map_iteration_type
,
5719 init
->add_statement(hiter
);
5721 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5722 Expression
* ref
= Expression::make_temporary_reference(hiter
, loc
);
5723 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5724 Expression
* call
= Runtime::make_call(Runtime::MAPITERINIT
, loc
, 2, p1
, p2
);
5725 init
->add_statement(Statement::make_statement(call
, true));
5732 ref
= Expression::make_temporary_reference(hiter
, loc
);
5735 mpz_init_set_ui(zval
, 0UL);
5736 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5739 Expression
* index
= Expression::make_index(ref
, zexpr
, NULL
, loc
);
5741 Expression
* ne
= Expression::make_binary(OPERATOR_NOTEQ
, index
,
5742 Expression::make_nil(loc
),
5747 // Set *PITER_INIT to
5748 // mapiter1(hiter, &index_temp)
5750 // mapiter2(hiter, &index_temp, &value_temp)
5752 Block
* iter_init
= new Block(body_block
, loc
);
5754 ref
= Expression::make_temporary_reference(hiter
, loc
);
5755 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5756 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5757 p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5758 if (value_temp
== NULL
)
5759 call
= Runtime::make_call(Runtime::MAPITER1
, loc
, 2, p1
, p2
);
5762 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5763 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5764 call
= Runtime::make_call(Runtime::MAPITER2
, loc
, 3, p1
, p2
, p3
);
5766 iter_init
->add_statement(Statement::make_statement(call
, true));
5768 *piter_init
= iter_init
;
5771 // mapiternext(&hiter)
5773 Block
* post
= new Block(enclosing
, loc
);
5775 ref
= Expression::make_temporary_reference(hiter
, loc
);
5776 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5777 call
= Runtime::make_call(Runtime::MAPITERNEXT
, loc
, 1, p1
);
5778 post
->add_statement(Statement::make_statement(call
, true));
5783 // Lower a for range over a channel.
5786 For_range_statement::lower_range_channel(Gogo
*,
5789 Named_object
* range_object
,
5790 Temporary_statement
* range_temp
,
5791 Temporary_statement
* index_temp
,
5792 Temporary_statement
* value_temp
,
5798 go_assert(value_temp
== NULL
);
5800 Location loc
= this->location();
5802 // The loop we generate:
5804 // index_temp, ok_temp = <-range
5808 // index = index_temp
5812 // We have no initialization code, no condition, and no post code.
5818 // Set *PITER_INIT to
5819 // index_temp, ok_temp = <-range
5824 Block
* iter_init
= new Block(body_block
, loc
);
5826 Temporary_statement
* ok_temp
=
5827 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
5828 iter_init
->add_statement(ok_temp
);
5830 Expression
* cref
= this->make_range_ref(range_object
, range_temp
, loc
);
5831 Temporary_reference_expression
* iref
=
5832 Expression::make_temporary_reference(index_temp
, loc
);
5833 iref
->set_is_lvalue();
5834 Temporary_reference_expression
* oref
=
5835 Expression::make_temporary_reference(ok_temp
, loc
);
5836 oref
->set_is_lvalue();
5837 Statement
* s
= Statement::make_tuple_receive_assignment(iref
, oref
, cref
,
5839 iter_init
->add_statement(s
);
5841 Block
* then_block
= new Block(iter_init
, loc
);
5842 s
= Statement::make_break_statement(this->break_label(), loc
);
5843 then_block
->add_statement(s
);
5845 oref
= Expression::make_temporary_reference(ok_temp
, loc
);
5846 Expression
* cond
= Expression::make_unary(OPERATOR_NOT
, oref
, loc
);
5847 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
5848 iter_init
->add_statement(s
);
5850 *piter_init
= iter_init
;
5853 // Return the break LABEL_EXPR.
5856 For_range_statement::break_label()
5858 if (this->break_label_
== NULL
)
5859 this->break_label_
= new Unnamed_label(this->location());
5860 return this->break_label_
;
5863 // Return the continue LABEL_EXPR.
5866 For_range_statement::continue_label()
5868 if (this->continue_label_
== NULL
)
5869 this->continue_label_
= new Unnamed_label(this->location());
5870 return this->continue_label_
;
5873 // Dump the AST representation for a for range statement.
5876 For_range_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5879 ast_dump_context
->print_indent();
5880 ast_dump_context
->ostream() << "for ";
5881 ast_dump_context
->dump_expression(this->index_var_
);
5882 if (this->value_var_
!= NULL
)
5884 ast_dump_context
->ostream() << ", ";
5885 ast_dump_context
->dump_expression(this->value_var_
);
5888 ast_dump_context
->ostream() << " = range ";
5889 ast_dump_context
->dump_expression(this->range_
);
5890 if (ast_dump_context
->dump_subblocks())
5892 ast_dump_context
->ostream() << " {" << std::endl
;
5894 ast_dump_context
->indent();
5896 ast_dump_context
->dump_block(this->statements_
);
5898 ast_dump_context
->unindent();
5899 ast_dump_context
->print_indent();
5900 ast_dump_context
->ostream() << "}";
5902 ast_dump_context
->ostream() << std::endl
;
5905 // Make a for statement with a range clause.
5907 For_range_statement
*
5908 Statement::make_for_range_statement(Expression
* index_var
,
5909 Expression
* value_var
,
5913 return new For_range_statement(index_var
, value_var
, range
, location
);