1 // statements.cc -- Go frontend statements.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #include "expressions.h"
15 #include "statements.h"
20 Statement::Statement(Statement_classification classification
,
22 : classification_(classification
), location_(location
)
26 Statement::~Statement()
30 // Traverse the tree. The work of walking the components is handled
34 Statement::traverse(Block
* block
, size_t* pindex
, Traverse
* traverse
)
36 if (this->classification_
== STATEMENT_ERROR
)
37 return TRAVERSE_CONTINUE
;
39 unsigned int traverse_mask
= traverse
->traverse_mask();
41 if ((traverse_mask
& Traverse::traverse_statements
) != 0)
43 int t
= traverse
->statement(block
, pindex
, this);
44 if (t
== TRAVERSE_EXIT
)
46 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
47 return TRAVERSE_CONTINUE
;
50 // No point in checking traverse_mask here--a statement may contain
51 // other blocks or statements, and if we got here we always want to
53 return this->do_traverse(traverse
);
56 // Traverse the contents of a statement.
59 Statement::traverse_contents(Traverse
* traverse
)
61 return this->do_traverse(traverse
);
64 // Traverse assignments.
67 Statement::traverse_assignments(Traverse_assignments
* tassign
)
69 if (this->classification_
== STATEMENT_ERROR
)
71 return this->do_traverse_assignments(tassign
);
74 // Traverse an expression in a statement. This is a helper function
78 Statement::traverse_expression(Traverse
* traverse
, Expression
** expr
)
80 if ((traverse
->traverse_mask()
81 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
82 return TRAVERSE_CONTINUE
;
83 return Expression::traverse(expr
, traverse
);
86 // Traverse an expression list in a statement. This is a helper
87 // function for child classes.
90 Statement::traverse_expression_list(Traverse
* traverse
,
91 Expression_list
* expr_list
)
93 if (expr_list
== NULL
)
94 return TRAVERSE_CONTINUE
;
95 if ((traverse
->traverse_mask()
96 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
97 return TRAVERSE_CONTINUE
;
98 return expr_list
->traverse(traverse
);
101 // Traverse a type in a statement. This is a helper function for
105 Statement::traverse_type(Traverse
* traverse
, Type
* type
)
107 if ((traverse
->traverse_mask()
108 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
109 return TRAVERSE_CONTINUE
;
110 return Type::traverse(type
, traverse
);
113 // Set type information for unnamed constants. This is really done by
117 Statement::determine_types()
119 this->do_determine_types();
122 // If this is a thunk statement, return it.
125 Statement::thunk_statement()
127 Thunk_statement
* ret
= this->convert
<Thunk_statement
, STATEMENT_GO
>();
129 ret
= this->convert
<Thunk_statement
, STATEMENT_DEFER
>();
133 // Convert a Statement to the backend representation. This is really
134 // done by the child class.
137 Statement::get_backend(Translate_context
* context
)
139 if (this->classification_
== STATEMENT_ERROR
)
140 return context
->backend()->error_statement();
141 return this->do_get_backend(context
);
144 // Dump AST representation for a statement to a dump context.
147 Statement::dump_statement(Ast_dump_context
* ast_dump_context
) const
149 this->do_dump_statement(ast_dump_context
);
152 // Note that this statement is erroneous. This is called by children
153 // when they discover an error.
156 Statement::set_is_error()
158 this->classification_
= STATEMENT_ERROR
;
161 // For children to call to report an error conveniently.
164 Statement::report_error(const char* msg
)
166 error_at(this->location_
, "%s", msg
);
167 this->set_is_error();
170 // An error statement, used to avoid crashing after we report an
173 class Error_statement
: public Statement
176 Error_statement(Location location
)
177 : Statement(STATEMENT_ERROR
, location
)
182 do_traverse(Traverse
*)
183 { return TRAVERSE_CONTINUE
; }
186 do_get_backend(Translate_context
*)
187 { go_unreachable(); }
190 do_dump_statement(Ast_dump_context
*) const;
193 // Dump the AST representation for an error statement.
196 Error_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
198 ast_dump_context
->print_indent();
199 ast_dump_context
->ostream() << "Error statement" << std::endl
;
202 // Make an error statement.
205 Statement::make_error_statement(Location location
)
207 return new Error_statement(location
);
210 // Class Variable_declaration_statement.
212 Variable_declaration_statement::Variable_declaration_statement(
214 : Statement(STATEMENT_VARIABLE_DECLARATION
, var
->var_value()->location()),
219 // We don't actually traverse the variable here; it was traversed
220 // while traversing the Block.
223 Variable_declaration_statement::do_traverse(Traverse
*)
225 return TRAVERSE_CONTINUE
;
228 // Traverse the assignments in a variable declaration. Note that this
229 // traversal is different from the usual traversal.
232 Variable_declaration_statement::do_traverse_assignments(
233 Traverse_assignments
* tassign
)
235 tassign
->initialize_variable(this->var_
);
239 // Lower the variable's initialization expression.
242 Variable_declaration_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
243 Block
*, Statement_inserter
* inserter
)
245 this->var_
->var_value()->lower_init_expression(gogo
, function
, inserter
);
249 // Flatten the variable's initialization expression.
252 Variable_declaration_statement::do_flatten(Gogo
* gogo
, Named_object
* function
,
253 Block
*, Statement_inserter
* inserter
)
255 this->var_
->var_value()->flatten_init_expression(gogo
, function
, inserter
);
259 // Convert a variable declaration to the backend representation.
262 Variable_declaration_statement::do_get_backend(Translate_context
* context
)
264 Variable
* var
= this->var_
->var_value();
265 Bvariable
* bvar
= this->var_
->get_backend_variable(context
->gogo(),
266 context
->function());
267 Bexpression
* binit
= var
->get_init(context
->gogo(), context
->function());
269 if (!var
->is_in_heap())
271 go_assert(binit
!= NULL
);
272 return context
->backend()->init_statement(bvar
, binit
);
275 // Something takes the address of this variable, so the value is
276 // stored in the heap. Initialize it to newly allocated memory
277 // space, and assign the initial value to the new space.
278 Location loc
= this->location();
279 Named_object
* newfn
= context
->gogo()->lookup_global("new");
280 go_assert(newfn
!= NULL
&& newfn
->is_function_declaration());
281 Expression
* func
= Expression::make_func_reference(newfn
, NULL
, loc
);
282 Expression_list
* params
= new Expression_list();
283 params
->push_back(Expression::make_type(var
->type(), loc
));
284 Expression
* call
= Expression::make_call(func
, params
, false, loc
);
285 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
286 Temporary_statement
* temp
= Statement::make_temporary(NULL
, call
, loc
);
287 Bstatement
* btemp
= temp
->get_backend(context
);
289 Bstatement
* set
= NULL
;
292 Expression
* e
= Expression::make_temporary_reference(temp
, loc
);
293 e
= Expression::make_unary(OPERATOR_MULT
, e
, loc
);
294 Bexpression
* be
= e
->get_backend(context
);
295 set
= context
->backend()->assignment_statement(be
, binit
, loc
);
298 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
299 Bexpression
* bref
= ref
->get_backend(context
);
300 Bstatement
* sinit
= context
->backend()->init_statement(bvar
, bref
);
302 std::vector
<Bstatement
*> stats
;
304 stats
.push_back(btemp
);
306 stats
.push_back(set
);
307 stats
.push_back(sinit
);
308 return context
->backend()->statement_list(stats
);
311 // Dump the AST representation for a variable declaration.
314 Variable_declaration_statement::do_dump_statement(
315 Ast_dump_context
* ast_dump_context
) const
317 ast_dump_context
->print_indent();
319 go_assert(var_
->is_variable());
320 ast_dump_context
->ostream() << "var " << this->var_
->name() << " ";
321 Variable
* var
= this->var_
->var_value();
324 ast_dump_context
->dump_type(var
->type());
325 ast_dump_context
->ostream() << " ";
327 if (var
->init() != NULL
)
329 ast_dump_context
->ostream() << "= ";
330 ast_dump_context
->dump_expression(var
->init());
332 ast_dump_context
->ostream() << std::endl
;
335 // Make a variable declaration.
338 Statement::make_variable_declaration(Named_object
* var
)
340 return new Variable_declaration_statement(var
);
343 // Class Temporary_statement.
345 // Return the type of the temporary variable.
348 Temporary_statement::type() const
350 return this->type_
!= NULL
? this->type_
: this->init_
->type();
356 Temporary_statement::do_traverse(Traverse
* traverse
)
358 if (this->type_
!= NULL
359 && this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
360 return TRAVERSE_EXIT
;
361 if (this->init_
== NULL
)
362 return TRAVERSE_CONTINUE
;
364 return this->traverse_expression(traverse
, &this->init_
);
367 // Traverse assignments.
370 Temporary_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
372 if (this->init_
== NULL
)
374 tassign
->value(&this->init_
, true, true);
381 Temporary_statement::do_determine_types()
383 if (this->type_
!= NULL
&& this->type_
->is_abstract())
384 this->type_
= this->type_
->make_non_abstract_type();
386 if (this->init_
!= NULL
)
388 if (this->type_
== NULL
)
389 this->init_
->determine_type_no_context();
392 Type_context
context(this->type_
, false);
393 this->init_
->determine_type(&context
);
397 if (this->type_
== NULL
)
399 this->type_
= this->init_
->type();
400 go_assert(!this->type_
->is_abstract());
407 Temporary_statement::do_check_types(Gogo
*)
409 if (this->type_
!= NULL
&& this->init_
!= NULL
)
413 if (this->are_hidden_fields_ok_
)
414 ok
= Type::are_assignable_hidden_ok(this->type_
, this->init_
->type(),
417 ok
= Type::are_assignable(this->type_
, this->init_
->type(), &reason
);
421 error_at(this->location(), "incompatible types in assignment");
423 error_at(this->location(), "incompatible types in assignment (%s)",
425 this->set_is_error();
430 // Convert to backend representation.
433 Temporary_statement::do_get_backend(Translate_context
* context
)
435 go_assert(this->bvariable_
== NULL
);
437 Named_object
* function
= context
->function();
438 go_assert(function
!= NULL
);
439 Bfunction
* bfunction
= function
->func_value()->get_decl();
440 Btype
* btype
= this->type()->get_backend(context
->gogo());
443 if (this->init_
== NULL
)
445 else if (this->type_
== NULL
)
446 binit
= this->init_
->get_backend(context
);
449 Expression
* init
= Expression::make_cast(this->type_
, this->init_
,
451 context
->gogo()->lower_expression(context
->function(), NULL
, &init
);
452 binit
= init
->get_backend(context
);
455 Bstatement
* statement
;
457 context
->backend()->temporary_variable(bfunction
, context
->bblock(),
459 this->is_address_taken_
,
460 this->location(), &statement
);
464 // Return the backend variable.
467 Temporary_statement::get_backend_variable(Translate_context
* context
) const
469 if (this->bvariable_
== NULL
)
471 go_assert(saw_errors());
472 return context
->backend()->error_variable();
474 return this->bvariable_
;
477 // Dump the AST represemtation for a temporary statement
480 Temporary_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
482 ast_dump_context
->print_indent();
483 ast_dump_context
->dump_temp_variable_name(this);
484 if (this->type_
!= NULL
)
486 ast_dump_context
->ostream() << " ";
487 ast_dump_context
->dump_type(this->type_
);
489 if (this->init_
!= NULL
)
491 ast_dump_context
->ostream() << " = ";
492 ast_dump_context
->dump_expression(this->init_
);
494 ast_dump_context
->ostream() << std::endl
;
497 // Make and initialize a temporary variable in BLOCK.
500 Statement::make_temporary(Type
* type
, Expression
* init
,
503 return new Temporary_statement(type
, init
, location
);
506 // An assignment statement.
508 class Assignment_statement
: public Statement
511 Assignment_statement(Expression
* lhs
, Expression
* rhs
,
513 : Statement(STATEMENT_ASSIGNMENT
, location
),
514 lhs_(lhs
), rhs_(rhs
), are_hidden_fields_ok_(false)
517 // Note that it is OK for this assignment statement to set hidden
520 set_hidden_fields_are_ok()
521 { this->are_hidden_fields_ok_
= true; }
525 do_traverse(Traverse
* traverse
);
528 do_traverse_assignments(Traverse_assignments
*);
531 do_determine_types();
534 do_check_types(Gogo
*);
537 do_get_backend(Translate_context
*);
540 do_dump_statement(Ast_dump_context
*) const;
543 // Left hand side--the lvalue.
545 // Right hand side--the rvalue.
547 // True if this statement may set hidden fields in the assignment
548 // statement. This is used for generated method stubs.
549 bool are_hidden_fields_ok_
;
555 Assignment_statement::do_traverse(Traverse
* traverse
)
557 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
558 return TRAVERSE_EXIT
;
559 return this->traverse_expression(traverse
, &this->rhs_
);
563 Assignment_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
565 tassign
->assignment(&this->lhs_
, &this->rhs_
);
569 // Set types for the assignment.
572 Assignment_statement::do_determine_types()
574 this->lhs_
->determine_type_no_context();
575 Type
* rhs_context_type
= this->lhs_
->type();
576 if (rhs_context_type
->is_sink_type())
577 rhs_context_type
= NULL
;
578 Type_context
context(rhs_context_type
, false);
579 this->rhs_
->determine_type(&context
);
582 // Check types for an assignment.
585 Assignment_statement::do_check_types(Gogo
*)
587 // The left hand side must be either addressable, a map index
588 // expression, or the blank identifier.
589 if (!this->lhs_
->is_addressable()
590 && this->lhs_
->map_index_expression() == NULL
591 && !this->lhs_
->is_sink_expression())
593 if (!this->lhs_
->type()->is_error())
594 this->report_error(_("invalid left hand side of assignment"));
598 Type
* lhs_type
= this->lhs_
->type();
599 Type
* rhs_type
= this->rhs_
->type();
601 // Invalid assignment of nil to the blank identifier.
602 if (lhs_type
->is_sink_type()
603 && rhs_type
->is_nil_type())
605 this->report_error(_("use of untyped nil"));
611 if (this->are_hidden_fields_ok_
)
612 ok
= Type::are_assignable_hidden_ok(lhs_type
, rhs_type
, &reason
);
614 ok
= Type::are_assignable(lhs_type
, rhs_type
, &reason
);
618 error_at(this->location(), "incompatible types in assignment");
620 error_at(this->location(), "incompatible types in assignment (%s)",
622 this->set_is_error();
625 if (lhs_type
->is_error() || rhs_type
->is_error())
626 this->set_is_error();
629 // Convert an assignment statement to the backend representation.
632 Assignment_statement::do_get_backend(Translate_context
* context
)
634 if (this->lhs_
->is_sink_expression())
636 Bexpression
* rhs
= this->rhs_
->get_backend(context
);
637 return context
->backend()->expression_statement(rhs
);
640 Bexpression
* lhs
= this->lhs_
->get_backend(context
);
642 Expression::convert_for_assignment(context
->gogo(), this->lhs_
->type(),
643 this->rhs_
, this->location());
644 Bexpression
* rhs
= conv
->get_backend(context
);
645 return context
->backend()->assignment_statement(lhs
, rhs
, this->location());
648 // Dump the AST representation for an assignment statement.
651 Assignment_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
654 ast_dump_context
->print_indent();
655 ast_dump_context
->dump_expression(this->lhs_
);
656 ast_dump_context
->ostream() << " = " ;
657 ast_dump_context
->dump_expression(this->rhs_
);
658 ast_dump_context
->ostream() << std::endl
;
661 // Make an assignment statement.
664 Statement::make_assignment(Expression
* lhs
, Expression
* rhs
,
667 return new Assignment_statement(lhs
, rhs
, location
);
670 // The Move_subexpressions class is used to move all top-level
671 // subexpressions of an expression. This is used for things like
672 // index expressions in which we must evaluate the index value before
673 // it can be changed by a multiple assignment.
675 class Move_subexpressions
: public Traverse
678 Move_subexpressions(int skip
, Block
* block
)
679 : Traverse(traverse_expressions
),
680 skip_(skip
), block_(block
)
685 expression(Expression
**);
688 // The number of subexpressions to skip moving. This is used to
689 // avoid moving the array itself, as we only need to move the index.
691 // The block where new temporary variables should be added.
696 Move_subexpressions::expression(Expression
** pexpr
)
700 else if ((*pexpr
)->temporary_reference_expression() == NULL
)
702 Location loc
= (*pexpr
)->location();
703 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
704 this->block_
->add_statement(temp
);
705 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
707 // We only need to move top-level subexpressions.
708 return TRAVERSE_SKIP_COMPONENTS
;
711 // The Move_ordered_evals class is used to find any subexpressions of
712 // an expression that have an evaluation order dependency. It creates
713 // temporary variables to hold them.
715 class Move_ordered_evals
: public Traverse
718 Move_ordered_evals(Block
* block
)
719 : Traverse(traverse_expressions
),
725 expression(Expression
**);
728 // The block where new temporary variables should be added.
733 Move_ordered_evals::expression(Expression
** pexpr
)
735 // We have to look at subexpressions first.
736 if ((*pexpr
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
737 return TRAVERSE_EXIT
;
740 if ((*pexpr
)->must_eval_subexpressions_in_order(&i
))
742 Move_subexpressions
ms(i
, this->block_
);
743 if ((*pexpr
)->traverse_subexpressions(&ms
) == TRAVERSE_EXIT
)
744 return TRAVERSE_EXIT
;
747 if ((*pexpr
)->must_eval_in_order())
749 Location loc
= (*pexpr
)->location();
750 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
751 this->block_
->add_statement(temp
);
752 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
754 return TRAVERSE_SKIP_COMPONENTS
;
757 // An assignment operation statement.
759 class Assignment_operation_statement
: public Statement
762 Assignment_operation_statement(Operator op
, Expression
* lhs
, Expression
* rhs
,
764 : Statement(STATEMENT_ASSIGNMENT_OPERATION
, location
),
765 op_(op
), lhs_(lhs
), rhs_(rhs
)
770 do_traverse(Traverse
*);
773 do_traverse_assignments(Traverse_assignments
*)
774 { go_unreachable(); }
777 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
780 do_get_backend(Translate_context
*)
781 { go_unreachable(); }
784 do_dump_statement(Ast_dump_context
*) const;
787 // The operator (OPERATOR_PLUSEQ, etc.).
798 Assignment_operation_statement::do_traverse(Traverse
* traverse
)
800 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
801 return TRAVERSE_EXIT
;
802 return this->traverse_expression(traverse
, &this->rhs_
);
805 // Lower an assignment operation statement to a regular assignment
809 Assignment_operation_statement::do_lower(Gogo
*, Named_object
*,
810 Block
* enclosing
, Statement_inserter
*)
812 Location loc
= this->location();
814 // We have to evaluate the left hand side expression only once. We
815 // do this by moving out any expression with side effects.
816 Block
* b
= new Block(enclosing
, loc
);
817 Move_ordered_evals
moe(b
);
818 this->lhs_
->traverse_subexpressions(&moe
);
820 Expression
* lval
= this->lhs_
->copy();
825 case OPERATOR_PLUSEQ
:
828 case OPERATOR_MINUSEQ
:
837 case OPERATOR_MULTEQ
:
846 case OPERATOR_LSHIFTEQ
:
847 op
= OPERATOR_LSHIFT
;
849 case OPERATOR_RSHIFTEQ
:
850 op
= OPERATOR_RSHIFT
;
855 case OPERATOR_BITCLEAREQ
:
856 op
= OPERATOR_BITCLEAR
;
862 Expression
* binop
= Expression::make_binary(op
, lval
, this->rhs_
, loc
);
863 Statement
* s
= Statement::make_assignment(this->lhs_
, binop
, loc
);
864 if (b
->statements()->empty())
872 return Statement::make_block_statement(b
, loc
);
876 // Dump the AST representation for an assignment operation statement
879 Assignment_operation_statement::do_dump_statement(
880 Ast_dump_context
* ast_dump_context
) const
882 ast_dump_context
->print_indent();
883 ast_dump_context
->dump_expression(this->lhs_
);
884 ast_dump_context
->dump_operator(this->op_
);
885 ast_dump_context
->dump_expression(this->rhs_
);
886 ast_dump_context
->ostream() << std::endl
;
889 // Make an assignment operation statement.
892 Statement::make_assignment_operation(Operator op
, Expression
* lhs
,
893 Expression
* rhs
, Location location
)
895 return new Assignment_operation_statement(op
, lhs
, rhs
, location
);
898 // A tuple assignment statement. This differs from an assignment
899 // statement in that the right-hand-side expressions are evaluated in
902 class Tuple_assignment_statement
: public Statement
905 Tuple_assignment_statement(Expression_list
* lhs
, Expression_list
* rhs
,
907 : Statement(STATEMENT_TUPLE_ASSIGNMENT
, location
),
908 lhs_(lhs
), rhs_(rhs
), are_hidden_fields_ok_(false)
911 // Note that it is OK for this assignment statement to set hidden
914 set_hidden_fields_are_ok()
915 { this->are_hidden_fields_ok_
= true; }
919 do_traverse(Traverse
* traverse
);
922 do_traverse_assignments(Traverse_assignments
*)
923 { go_unreachable(); }
926 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
929 do_get_backend(Translate_context
*)
930 { go_unreachable(); }
933 do_dump_statement(Ast_dump_context
*) const;
936 // Left hand side--a list of lvalues.
937 Expression_list
* lhs_
;
938 // Right hand side--a list of rvalues.
939 Expression_list
* rhs_
;
940 // True if this statement may set hidden fields in the assignment
941 // statement. This is used for generated method stubs.
942 bool are_hidden_fields_ok_
;
948 Tuple_assignment_statement::do_traverse(Traverse
* traverse
)
950 if (this->traverse_expression_list(traverse
, this->lhs_
) == TRAVERSE_EXIT
)
951 return TRAVERSE_EXIT
;
952 return this->traverse_expression_list(traverse
, this->rhs_
);
955 // Lower a tuple assignment. We use temporary variables to split it
956 // up into a set of single assignments.
959 Tuple_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
962 Location loc
= this->location();
964 Block
* b
= new Block(enclosing
, loc
);
966 // First move out any subexpressions on the left hand side. The
967 // right hand side will be evaluated in the required order anyhow.
968 Move_ordered_evals
moe(b
);
969 for (Expression_list::iterator plhs
= this->lhs_
->begin();
970 plhs
!= this->lhs_
->end();
972 Expression::traverse(&*plhs
, &moe
);
974 std::vector
<Temporary_statement
*> temps
;
975 temps
.reserve(this->lhs_
->size());
977 Expression_list::const_iterator prhs
= this->rhs_
->begin();
978 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
979 plhs
!= this->lhs_
->end();
982 go_assert(prhs
!= this->rhs_
->end());
984 if ((*plhs
)->is_error_expression()
985 || (*plhs
)->type()->is_error()
986 || (*prhs
)->is_error_expression()
987 || (*prhs
)->type()->is_error())
990 if ((*plhs
)->is_sink_expression())
992 if ((*prhs
)->type()->is_nil_type())
993 this->report_error(_("use of untyped nil"));
995 b
->add_statement(Statement::make_statement(*prhs
, true));
999 Temporary_statement
* temp
= Statement::make_temporary((*plhs
)->type(),
1001 if (this->are_hidden_fields_ok_
)
1002 temp
->set_hidden_fields_are_ok();
1003 b
->add_statement(temp
);
1004 temps
.push_back(temp
);
1007 go_assert(prhs
== this->rhs_
->end());
1009 prhs
= this->rhs_
->begin();
1010 std::vector
<Temporary_statement
*>::const_iterator ptemp
= temps
.begin();
1011 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
1012 plhs
!= this->lhs_
->end();
1015 if ((*plhs
)->is_error_expression()
1016 || (*plhs
)->type()->is_error()
1017 || (*prhs
)->is_error_expression()
1018 || (*prhs
)->type()->is_error())
1021 if ((*plhs
)->is_sink_expression())
1024 Expression
* ref
= Expression::make_temporary_reference(*ptemp
, loc
);
1025 Statement
* s
= Statement::make_assignment(*plhs
, ref
, loc
);
1026 if (this->are_hidden_fields_ok_
)
1028 Assignment_statement
* as
= static_cast<Assignment_statement
*>(s
);
1029 as
->set_hidden_fields_are_ok();
1031 b
->add_statement(s
);
1034 go_assert(ptemp
== temps
.end() || saw_errors());
1036 return Statement::make_block_statement(b
, loc
);
1039 // Dump the AST representation for a tuple assignment statement.
1042 Tuple_assignment_statement::do_dump_statement(
1043 Ast_dump_context
* ast_dump_context
) const
1045 ast_dump_context
->print_indent();
1046 ast_dump_context
->dump_expression_list(this->lhs_
);
1047 ast_dump_context
->ostream() << " = ";
1048 ast_dump_context
->dump_expression_list(this->rhs_
);
1049 ast_dump_context
->ostream() << std::endl
;
1052 // Make a tuple assignment statement.
1055 Statement::make_tuple_assignment(Expression_list
* lhs
, Expression_list
* rhs
,
1058 return new Tuple_assignment_statement(lhs
, rhs
, location
);
1061 // A tuple assignment from a map index expression.
1064 class Tuple_map_assignment_statement
: public Statement
1067 Tuple_map_assignment_statement(Expression
* val
, Expression
* present
,
1068 Expression
* map_index
,
1070 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT
, location
),
1071 val_(val
), present_(present
), map_index_(map_index
)
1076 do_traverse(Traverse
* traverse
);
1079 do_traverse_assignments(Traverse_assignments
*)
1080 { go_unreachable(); }
1083 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1086 do_get_backend(Translate_context
*)
1087 { go_unreachable(); }
1090 do_dump_statement(Ast_dump_context
*) const;
1093 // Lvalue which receives the value from the map.
1095 // Lvalue which receives whether the key value was present.
1096 Expression
* present_
;
1097 // The map index expression.
1098 Expression
* map_index_
;
1104 Tuple_map_assignment_statement::do_traverse(Traverse
* traverse
)
1106 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1107 || this->traverse_expression(traverse
, &this->present_
) == TRAVERSE_EXIT
)
1108 return TRAVERSE_EXIT
;
1109 return this->traverse_expression(traverse
, &this->map_index_
);
1112 // Lower a tuple map assignment.
1115 Tuple_map_assignment_statement::do_lower(Gogo
*, Named_object
*,
1116 Block
* enclosing
, Statement_inserter
*)
1118 Location loc
= this->location();
1120 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1121 if (map_index
== NULL
)
1123 this->report_error(_("expected map index on right hand side"));
1124 return Statement::make_error_statement(loc
);
1126 Map_type
* map_type
= map_index
->get_map_type();
1127 if (map_type
== NULL
)
1128 return Statement::make_error_statement(loc
);
1130 Block
* b
= new Block(enclosing
, loc
);
1132 // Move out any subexpressions to make sure that functions are
1133 // called in the required order.
1134 Move_ordered_evals
moe(b
);
1135 this->val_
->traverse_subexpressions(&moe
);
1136 this->present_
->traverse_subexpressions(&moe
);
1138 // Copy the key value into a temporary so that we can take its
1139 // address without pushing the value onto the heap.
1141 // var key_temp KEY_TYPE = MAP_INDEX
1142 Temporary_statement
* key_temp
=
1143 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1144 b
->add_statement(key_temp
);
1146 // var val_temp VAL_TYPE
1147 Temporary_statement
* val_temp
=
1148 Statement::make_temporary(map_type
->val_type(), NULL
, loc
);
1149 b
->add_statement(val_temp
);
1151 // var present_temp bool
1152 Temporary_statement
* present_temp
=
1153 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
1154 b
->add_statement(present_temp
);
1156 // present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp, &val_temp)
1157 Expression
* a1
= Expression::make_type_descriptor(map_type
, loc
);
1158 Expression
* a2
= map_index
->map();
1159 Temporary_reference_expression
* ref
=
1160 Expression::make_temporary_reference(key_temp
, loc
);
1161 Expression
* a3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1162 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1163 Expression
* a4
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1164 Expression
* call
= Runtime::make_call(Runtime::MAPACCESS2
, loc
, 4,
1167 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1168 ref
->set_is_lvalue();
1169 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1170 b
->add_statement(s
);
1173 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1174 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1175 b
->add_statement(s
);
1177 // present = present_temp
1178 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1179 s
= Statement::make_assignment(this->present_
, ref
, loc
);
1180 b
->add_statement(s
);
1182 return Statement::make_block_statement(b
, loc
);
1185 // Dump the AST representation for a tuple map assignment statement.
1188 Tuple_map_assignment_statement::do_dump_statement(
1189 Ast_dump_context
* ast_dump_context
) const
1191 ast_dump_context
->print_indent();
1192 ast_dump_context
->dump_expression(this->val_
);
1193 ast_dump_context
->ostream() << ", ";
1194 ast_dump_context
->dump_expression(this->present_
);
1195 ast_dump_context
->ostream() << " = ";
1196 ast_dump_context
->dump_expression(this->map_index_
);
1197 ast_dump_context
->ostream() << std::endl
;
1200 // Make a map assignment statement which returns a pair of values.
1203 Statement::make_tuple_map_assignment(Expression
* val
, Expression
* present
,
1204 Expression
* map_index
,
1207 return new Tuple_map_assignment_statement(val
, present
, map_index
, location
);
1210 // Assign a pair of entries to a map.
1213 class Map_assignment_statement
: public Statement
1216 Map_assignment_statement(Expression
* map_index
,
1217 Expression
* val
, Expression
* should_set
,
1219 : Statement(STATEMENT_MAP_ASSIGNMENT
, location
),
1220 map_index_(map_index
), val_(val
), should_set_(should_set
)
1225 do_traverse(Traverse
* traverse
);
1228 do_traverse_assignments(Traverse_assignments
*)
1229 { go_unreachable(); }
1232 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1235 do_get_backend(Translate_context
*)
1236 { go_unreachable(); }
1239 do_dump_statement(Ast_dump_context
*) const;
1242 // A reference to the map index which should be set or deleted.
1243 Expression
* map_index_
;
1244 // The value to add to the map.
1246 // Whether or not to add the value.
1247 Expression
* should_set_
;
1250 // Traverse a map assignment.
1253 Map_assignment_statement::do_traverse(Traverse
* traverse
)
1255 if (this->traverse_expression(traverse
, &this->map_index_
) == TRAVERSE_EXIT
1256 || this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
1257 return TRAVERSE_EXIT
;
1258 return this->traverse_expression(traverse
, &this->should_set_
);
1261 // Lower a map assignment to a function call.
1264 Map_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
1265 Statement_inserter
*)
1267 Location loc
= this->location();
1269 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1270 if (map_index
== NULL
)
1272 this->report_error(_("expected map index on left hand side"));
1273 return Statement::make_error_statement(loc
);
1275 Map_type
* map_type
= map_index
->get_map_type();
1276 if (map_type
== NULL
)
1277 return Statement::make_error_statement(loc
);
1279 Block
* b
= new Block(enclosing
, loc
);
1281 // Evaluate the map first to get order of evaluation right.
1282 // map_temp := m // we are evaluating m[k] = v, p
1283 Temporary_statement
* map_temp
= Statement::make_temporary(map_type
,
1286 b
->add_statement(map_temp
);
1288 // var key_temp MAP_KEY_TYPE = k
1289 Temporary_statement
* key_temp
=
1290 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1291 b
->add_statement(key_temp
);
1293 // var val_temp MAP_VAL_TYPE = v
1294 Temporary_statement
* val_temp
=
1295 Statement::make_temporary(map_type
->val_type(), this->val_
, loc
);
1296 b
->add_statement(val_temp
);
1298 // var insert_temp bool = p
1299 Temporary_statement
* insert_temp
=
1300 Statement::make_temporary(Type::lookup_bool_type(), this->should_set_
,
1302 b
->add_statement(insert_temp
);
1304 // mapassign2(map_temp, &key_temp, &val_temp, p)
1305 Expression
* p1
= Expression::make_temporary_reference(map_temp
, loc
);
1306 Expression
* ref
= Expression::make_temporary_reference(key_temp
, loc
);
1307 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1308 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1309 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1310 Expression
* p4
= Expression::make_temporary_reference(insert_temp
, loc
);
1311 Expression
* call
= Runtime::make_call(Runtime::MAPASSIGN2
, loc
, 4,
1313 Statement
* s
= Statement::make_statement(call
, true);
1314 b
->add_statement(s
);
1316 return Statement::make_block_statement(b
, loc
);
1319 // Dump the AST representation for a map assignment statement.
1322 Map_assignment_statement::do_dump_statement(
1323 Ast_dump_context
* ast_dump_context
) const
1325 ast_dump_context
->print_indent();
1326 ast_dump_context
->dump_expression(this->map_index_
);
1327 ast_dump_context
->ostream() << " = ";
1328 ast_dump_context
->dump_expression(this->val_
);
1329 ast_dump_context
->ostream() << ", ";
1330 ast_dump_context
->dump_expression(this->should_set_
);
1331 ast_dump_context
->ostream() << std::endl
;
1334 // Make a statement which assigns a pair of entries to a map.
1337 Statement::make_map_assignment(Expression
* map_index
,
1338 Expression
* val
, Expression
* should_set
,
1341 return new Map_assignment_statement(map_index
, val
, should_set
, location
);
1344 // A tuple assignment from a receive statement.
1346 class Tuple_receive_assignment_statement
: public Statement
1349 Tuple_receive_assignment_statement(Expression
* val
, Expression
* closed
,
1350 Expression
* channel
, Location location
)
1351 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
, location
),
1352 val_(val
), closed_(closed
), channel_(channel
)
1357 do_traverse(Traverse
* traverse
);
1360 do_traverse_assignments(Traverse_assignments
*)
1361 { go_unreachable(); }
1364 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1367 do_get_backend(Translate_context
*)
1368 { go_unreachable(); }
1371 do_dump_statement(Ast_dump_context
*) const;
1374 // Lvalue which receives the value from the channel.
1376 // Lvalue which receives whether the channel is closed.
1377 Expression
* closed_
;
1378 // The channel on which we receive the value.
1379 Expression
* channel_
;
1385 Tuple_receive_assignment_statement::do_traverse(Traverse
* traverse
)
1387 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1388 || this->traverse_expression(traverse
, &this->closed_
) == TRAVERSE_EXIT
)
1389 return TRAVERSE_EXIT
;
1390 return this->traverse_expression(traverse
, &this->channel_
);
1393 // Lower to a function call.
1396 Tuple_receive_assignment_statement::do_lower(Gogo
*, Named_object
*,
1398 Statement_inserter
*)
1400 Location loc
= this->location();
1402 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
1403 if (channel_type
== NULL
)
1405 this->report_error(_("expected channel"));
1406 return Statement::make_error_statement(loc
);
1408 if (!channel_type
->may_receive())
1410 this->report_error(_("invalid receive on send-only channel"));
1411 return Statement::make_error_statement(loc
);
1414 Block
* b
= new Block(enclosing
, loc
);
1416 // Make sure that any subexpressions on the left hand side are
1417 // evaluated in the right order.
1418 Move_ordered_evals
moe(b
);
1419 this->val_
->traverse_subexpressions(&moe
);
1420 this->closed_
->traverse_subexpressions(&moe
);
1422 // var val_temp ELEMENT_TYPE
1423 Temporary_statement
* val_temp
=
1424 Statement::make_temporary(channel_type
->element_type(), NULL
, loc
);
1425 b
->add_statement(val_temp
);
1427 // var closed_temp bool
1428 Temporary_statement
* closed_temp
=
1429 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
1430 b
->add_statement(closed_temp
);
1432 // closed_temp = chanrecv2(type, channel, &val_temp)
1433 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
1435 Temporary_reference_expression
* ref
=
1436 Expression::make_temporary_reference(val_temp
, loc
);
1437 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1438 Expression
* call
= Runtime::make_call(Runtime::CHANRECV2
,
1439 loc
, 3, td
, this->channel_
, p2
);
1440 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1441 ref
->set_is_lvalue();
1442 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1443 b
->add_statement(s
);
1446 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1447 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1448 b
->add_statement(s
);
1450 // closed = closed_temp
1451 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1452 s
= Statement::make_assignment(this->closed_
, ref
, loc
);
1453 b
->add_statement(s
);
1455 return Statement::make_block_statement(b
, loc
);
1458 // Dump the AST representation for a tuple receive statement.
1461 Tuple_receive_assignment_statement::do_dump_statement(
1462 Ast_dump_context
* ast_dump_context
) const
1464 ast_dump_context
->print_indent();
1465 ast_dump_context
->dump_expression(this->val_
);
1466 ast_dump_context
->ostream() << ", ";
1467 ast_dump_context
->dump_expression(this->closed_
);
1468 ast_dump_context
->ostream() << " <- ";
1469 ast_dump_context
->dump_expression(this->channel_
);
1470 ast_dump_context
->ostream() << std::endl
;
1473 // Make a nonblocking receive statement.
1476 Statement::make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
1477 Expression
* channel
,
1480 return new Tuple_receive_assignment_statement(val
, closed
, channel
,
1484 // An assignment to a pair of values from a type guard. This is a
1485 // conditional type guard. v, ok = i.(type).
1487 class Tuple_type_guard_assignment_statement
: public Statement
1490 Tuple_type_guard_assignment_statement(Expression
* val
, Expression
* ok
,
1491 Expression
* expr
, Type
* type
,
1493 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
, location
),
1494 val_(val
), ok_(ok
), expr_(expr
), type_(type
)
1499 do_traverse(Traverse
*);
1502 do_traverse_assignments(Traverse_assignments
*)
1503 { go_unreachable(); }
1506 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1509 do_get_backend(Translate_context
*)
1510 { go_unreachable(); }
1513 do_dump_statement(Ast_dump_context
*) const;
1517 lower_to_type(Runtime::Function
);
1520 lower_to_object_type(Block
*, Runtime::Function
);
1522 // The variable which recieves the converted value.
1524 // The variable which receives the indication of success.
1526 // The expression being converted.
1528 // The type to which the expression is being converted.
1532 // Traverse a type guard tuple assignment.
1535 Tuple_type_guard_assignment_statement::do_traverse(Traverse
* traverse
)
1537 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1538 || this->traverse_expression(traverse
, &this->ok_
) == TRAVERSE_EXIT
1539 || this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
1540 return TRAVERSE_EXIT
;
1541 return this->traverse_expression(traverse
, &this->expr_
);
1544 // Lower to a function call.
1547 Tuple_type_guard_assignment_statement::do_lower(Gogo
*, Named_object
*,
1549 Statement_inserter
*)
1551 Location loc
= this->location();
1553 Type
* expr_type
= this->expr_
->type();
1554 if (expr_type
->interface_type() == NULL
)
1556 if (!expr_type
->is_error() && !this->type_
->is_error())
1557 this->report_error(_("type assertion only valid for interface types"));
1558 return Statement::make_error_statement(loc
);
1561 Block
* b
= new Block(enclosing
, loc
);
1563 // Make sure that any subexpressions on the left hand side are
1564 // evaluated in the right order.
1565 Move_ordered_evals
moe(b
);
1566 this->val_
->traverse_subexpressions(&moe
);
1567 this->ok_
->traverse_subexpressions(&moe
);
1569 bool expr_is_empty
= expr_type
->interface_type()->is_empty();
1570 Call_expression
* call
;
1571 if (this->type_
->interface_type() != NULL
)
1573 if (this->type_
->interface_type()->is_empty())
1574 call
= Runtime::make_call((expr_is_empty
1575 ? Runtime::IFACEE2E2
1576 : Runtime::IFACEI2E2
),
1577 loc
, 1, this->expr_
);
1579 call
= this->lower_to_type(expr_is_empty
1580 ? Runtime::IFACEE2I2
1581 : Runtime::IFACEI2I2
);
1583 else if (this->type_
->points_to() != NULL
)
1584 call
= this->lower_to_type(expr_is_empty
1585 ? Runtime::IFACEE2T2P
1586 : Runtime::IFACEI2T2P
);
1589 this->lower_to_object_type(b
,
1591 ? Runtime::IFACEE2T2
1592 : Runtime::IFACEI2T2
));
1598 Expression
* res
= Expression::make_call_result(call
, 0);
1599 res
= Expression::make_unsafe_cast(this->type_
, res
, loc
);
1600 Statement
* s
= Statement::make_assignment(this->val_
, res
, loc
);
1601 b
->add_statement(s
);
1603 res
= Expression::make_call_result(call
, 1);
1604 s
= Statement::make_assignment(this->ok_
, res
, loc
);
1605 b
->add_statement(s
);
1608 return Statement::make_block_statement(b
, loc
);
1611 // Lower a conversion to a non-empty interface type or a pointer type.
1614 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code
)
1616 Location loc
= this->location();
1617 return Runtime::make_call(code
, loc
, 2,
1618 Expression::make_type_descriptor(this->type_
, loc
),
1622 // Lower a conversion to a non-interface non-pointer type.
1625 Tuple_type_guard_assignment_statement::lower_to_object_type(
1627 Runtime::Function code
)
1629 Location loc
= this->location();
1631 // var val_temp TYPE
1632 Temporary_statement
* val_temp
= Statement::make_temporary(this->type_
,
1634 b
->add_statement(val_temp
);
1636 // ok = CODE(type_descriptor, expr, &val_temp)
1637 Expression
* p1
= Expression::make_type_descriptor(this->type_
, loc
);
1638 Expression
* ref
= Expression::make_temporary_reference(val_temp
, loc
);
1639 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1640 Expression
* call
= Runtime::make_call(code
, loc
, 3, p1
, this->expr_
, p3
);
1641 Statement
* s
= Statement::make_assignment(this->ok_
, call
, loc
);
1642 b
->add_statement(s
);
1645 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1646 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1647 b
->add_statement(s
);
1650 // Dump the AST representation for a tuple type guard statement.
1653 Tuple_type_guard_assignment_statement::do_dump_statement(
1654 Ast_dump_context
* ast_dump_context
) const
1656 ast_dump_context
->print_indent();
1657 ast_dump_context
->dump_expression(this->val_
);
1658 ast_dump_context
->ostream() << ", ";
1659 ast_dump_context
->dump_expression(this->ok_
);
1660 ast_dump_context
->ostream() << " = ";
1661 ast_dump_context
->dump_expression(this->expr_
);
1662 ast_dump_context
->ostream() << " . ";
1663 ast_dump_context
->dump_type(this->type_
);
1664 ast_dump_context
->ostream() << std::endl
;
1667 // Make an assignment from a type guard to a pair of variables.
1670 Statement::make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
1671 Expression
* expr
, Type
* type
,
1674 return new Tuple_type_guard_assignment_statement(val
, ok
, expr
, type
,
1678 // Class Expression_statement.
1682 Expression_statement::Expression_statement(Expression
* expr
, bool is_ignored
)
1683 : Statement(STATEMENT_EXPRESSION
, expr
->location()),
1684 expr_(expr
), is_ignored_(is_ignored
)
1691 Expression_statement::do_determine_types()
1693 this->expr_
->determine_type_no_context();
1696 // Check the types of an expression statement. The only check we do
1697 // is to possibly give an error about discarding the value of the
1701 Expression_statement::do_check_types(Gogo
*)
1703 if (!this->is_ignored_
)
1704 this->expr_
->discarding_value();
1707 // An expression statement is only a terminating statement if it is
1711 Expression_statement::do_may_fall_through() const
1713 const Call_expression
* call
= this->expr_
->call_expression();
1716 const Expression
* fn
= call
->fn();
1717 // panic is still an unknown named object.
1718 const Unknown_expression
* ue
= fn
->unknown_expression();
1721 Named_object
* no
= ue
->named_object();
1723 if (no
->is_unknown())
1724 no
= no
->unknown_value()->real_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")
1744 // Convert to backend representation.
1747 Expression_statement::do_get_backend(Translate_context
* context
)
1749 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
1750 return context
->backend()->expression_statement(bexpr
);
1753 // Dump the AST representation for an expression statement
1756 Expression_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
1759 ast_dump_context
->print_indent();
1760 ast_dump_context
->dump_expression(expr_
);
1761 ast_dump_context
->ostream() << std::endl
;
1764 // Make an expression statement from an Expression.
1767 Statement::make_statement(Expression
* expr
, bool is_ignored
)
1769 return new Expression_statement(expr
, is_ignored
);
1772 // A block statement--a list of statements which may include variable
1775 class Block_statement
: public Statement
1778 Block_statement(Block
* block
, Location location
)
1779 : Statement(STATEMENT_BLOCK
, location
),
1785 do_traverse(Traverse
* traverse
)
1786 { return this->block_
->traverse(traverse
); }
1789 do_determine_types()
1790 { this->block_
->determine_types(); }
1793 do_may_fall_through() const
1794 { return this->block_
->may_fall_through(); }
1797 do_get_backend(Translate_context
* context
);
1800 do_dump_statement(Ast_dump_context
*) const;
1806 // Convert a block to the backend representation of a statement.
1809 Block_statement::do_get_backend(Translate_context
* context
)
1811 Bblock
* bblock
= this->block_
->get_backend(context
);
1812 return context
->backend()->block_statement(bblock
);
1815 // Dump the AST for a block statement
1818 Block_statement::do_dump_statement(Ast_dump_context
*) const
1820 // block statement braces are dumped when traversing.
1823 // Make a block statement.
1826 Statement::make_block_statement(Block
* block
, Location location
)
1828 return new Block_statement(block
, location
);
1831 // An increment or decrement statement.
1833 class Inc_dec_statement
: public Statement
1836 Inc_dec_statement(bool is_inc
, Expression
* expr
)
1837 : Statement(STATEMENT_INCDEC
, expr
->location()),
1838 expr_(expr
), is_inc_(is_inc
)
1843 do_traverse(Traverse
* traverse
)
1844 { return this->traverse_expression(traverse
, &this->expr_
); }
1847 do_traverse_assignments(Traverse_assignments
*)
1848 { go_unreachable(); }
1851 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1854 do_get_backend(Translate_context
*)
1855 { go_unreachable(); }
1858 do_dump_statement(Ast_dump_context
*) const;
1861 // The l-value to increment or decrement.
1863 // Whether to increment or decrement.
1867 // Lower to += or -=.
1870 Inc_dec_statement::do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
1872 Location loc
= this->location();
1875 mpz_init_set_ui(oval
, 1UL);
1876 Expression
* oexpr
= Expression::make_integer(&oval
, NULL
, loc
);
1879 Operator op
= this->is_inc_
? OPERATOR_PLUSEQ
: OPERATOR_MINUSEQ
;
1880 return Statement::make_assignment_operation(op
, this->expr_
, oexpr
, loc
);
1883 // Dump the AST representation for a inc/dec statement.
1886 Inc_dec_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
1888 ast_dump_context
->print_indent();
1889 ast_dump_context
->dump_expression(expr_
);
1890 ast_dump_context
->ostream() << (is_inc_
? "++": "--") << std::endl
;
1893 // Make an increment statement.
1896 Statement::make_inc_statement(Expression
* expr
)
1898 return new Inc_dec_statement(true, expr
);
1901 // Make a decrement statement.
1904 Statement::make_dec_statement(Expression
* expr
)
1906 return new Inc_dec_statement(false, expr
);
1909 // Class Thunk_statement. This is the base class for go and defer
1914 Thunk_statement::Thunk_statement(Statement_classification classification
,
1915 Call_expression
* call
,
1917 : Statement(classification
, location
),
1918 call_(call
), struct_type_(NULL
)
1922 // Return whether this is a simple statement which does not require a
1926 Thunk_statement::is_simple(Function_type
* fntype
) const
1928 // We need a thunk to call a method, or to pass a variable number of
1930 if (fntype
->is_method() || fntype
->is_varargs())
1933 // A defer statement requires a thunk to set up for whether the
1934 // function can call recover.
1935 if (this->classification() == STATEMENT_DEFER
)
1938 // We can only permit a single parameter of pointer type.
1939 const Typed_identifier_list
* parameters
= fntype
->parameters();
1940 if (parameters
!= NULL
1941 && (parameters
->size() > 1
1942 || (parameters
->size() == 1
1943 && parameters
->begin()->type()->points_to() == NULL
)))
1946 // If the function returns multiple values, or returns a type other
1947 // than integer, floating point, or pointer, then it may get a
1948 // hidden first parameter, in which case we need the more
1949 // complicated approach. This is true even though we are going to
1950 // ignore the return value.
1951 const Typed_identifier_list
* results
= fntype
->results();
1953 && (results
->size() > 1
1954 || (results
->size() == 1
1955 && !results
->begin()->type()->is_basic_type()
1956 && results
->begin()->type()->points_to() == NULL
)))
1959 // If this calls something that is not a simple function, then we
1961 Expression
* fn
= this->call_
->call_expression()->fn();
1962 if (fn
->func_expression() == NULL
)
1965 // If the function uses a closure, then we need a thunk. FIXME: We
1966 // could accept a zero argument function with a closure.
1967 if (fn
->func_expression()->closure() != NULL
)
1973 // Traverse a thunk statement.
1976 Thunk_statement::do_traverse(Traverse
* traverse
)
1978 return this->traverse_expression(traverse
, &this->call_
);
1981 // We implement traverse_assignment for a thunk statement because it
1982 // effectively copies the function call.
1985 Thunk_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
1987 Expression
* fn
= this->call_
->call_expression()->fn();
1988 Expression
* fn2
= fn
;
1989 tassign
->value(&fn2
, true, false);
1993 // Determine types in a thunk statement.
1996 Thunk_statement::do_determine_types()
1998 this->call_
->determine_type_no_context();
2000 // Now that we know the types of the call, build the struct used to
2002 Call_expression
* ce
= this->call_
->call_expression();
2005 Function_type
* fntype
= ce
->get_function_type();
2006 if (fntype
!= NULL
&& !this->is_simple(fntype
))
2007 this->struct_type_
= this->build_struct(fntype
);
2010 // Check types in a thunk statement.
2013 Thunk_statement::do_check_types(Gogo
*)
2015 if (!this->call_
->discarding_value())
2017 Call_expression
* ce
= this->call_
->call_expression();
2020 if (!this->call_
->is_error_expression())
2021 this->report_error("expected call expression");
2026 // The Traverse class used to find and simplify thunk statements.
2028 class Simplify_thunk_traverse
: public Traverse
2031 Simplify_thunk_traverse(Gogo
* gogo
)
2032 : Traverse(traverse_functions
| traverse_blocks
),
2033 gogo_(gogo
), function_(NULL
)
2037 function(Named_object
*);
2045 // The function we are traversing.
2046 Named_object
* function_
;
2049 // Keep track of the current function while looking for thunks.
2052 Simplify_thunk_traverse::function(Named_object
* no
)
2054 go_assert(this->function_
== NULL
);
2055 this->function_
= no
;
2056 int t
= no
->func_value()->traverse(this);
2057 this->function_
= NULL
;
2058 if (t
== TRAVERSE_EXIT
)
2060 return TRAVERSE_SKIP_COMPONENTS
;
2063 // Look for thunks in a block.
2066 Simplify_thunk_traverse::block(Block
* b
)
2068 // The parser ensures that thunk statements always appear at the end
2070 if (b
->statements()->size() < 1)
2071 return TRAVERSE_CONTINUE
;
2072 Thunk_statement
* stat
= b
->statements()->back()->thunk_statement();
2074 return TRAVERSE_CONTINUE
;
2075 if (stat
->simplify_statement(this->gogo_
, this->function_
, b
))
2076 return TRAVERSE_SKIP_COMPONENTS
;
2077 return TRAVERSE_CONTINUE
;
2080 // Simplify all thunk statements.
2083 Gogo::simplify_thunk_statements()
2085 Simplify_thunk_traverse
thunk_traverse(this);
2086 this->traverse(&thunk_traverse
);
2089 // Return true if the thunk function is a constant, which means that
2090 // it does not need to be passed to the thunk routine.
2093 Thunk_statement::is_constant_function() const
2095 Call_expression
* ce
= this->call_
->call_expression();
2096 Function_type
* fntype
= ce
->get_function_type();
2099 go_assert(saw_errors());
2102 if (fntype
->is_builtin())
2104 Expression
* fn
= ce
->fn();
2105 if (fn
->func_expression() != NULL
)
2106 return fn
->func_expression()->closure() == NULL
;
2107 if (fn
->interface_field_reference_expression() != NULL
)
2112 // Simplify complex thunk statements into simple ones. A complicated
2113 // thunk statement is one which takes anything other than zero
2114 // parameters or a single pointer parameter. We rewrite it into code
2115 // which allocates a struct, stores the parameter values into the
2116 // struct, and does a simple go or defer statement which passes the
2117 // struct to a thunk. The thunk does the real call.
2120 Thunk_statement::simplify_statement(Gogo
* gogo
, Named_object
* function
,
2123 if (this->classification() == STATEMENT_ERROR
)
2125 if (this->call_
->is_error_expression())
2128 if (this->classification() == STATEMENT_DEFER
)
2130 // Make sure that the defer stack exists for the function. We
2131 // will use when converting this statement to the backend
2132 // representation, but we want it to exist when we start
2133 // converting the function.
2134 function
->func_value()->defer_stack(this->location());
2137 Call_expression
* ce
= this->call_
->call_expression();
2138 Function_type
* fntype
= ce
->get_function_type();
2141 go_assert(saw_errors());
2142 this->set_is_error();
2145 if (this->is_simple(fntype
))
2148 Expression
* fn
= ce
->fn();
2149 Interface_field_reference_expression
* interface_method
=
2150 fn
->interface_field_reference_expression();
2152 Location location
= this->location();
2154 std::string thunk_name
= Gogo::thunk_name();
2157 this->build_thunk(gogo
, thunk_name
);
2159 // Generate code to call the thunk.
2161 // Get the values to store into the struct which is the single
2162 // argument to the thunk.
2164 Expression_list
* vals
= new Expression_list();
2165 if (!this->is_constant_function())
2166 vals
->push_back(fn
);
2168 if (interface_method
!= NULL
)
2169 vals
->push_back(interface_method
->expr());
2171 if (ce
->args() != NULL
)
2173 for (Expression_list::const_iterator p
= ce
->args()->begin();
2174 p
!= ce
->args()->end();
2176 vals
->push_back(*p
);
2179 // Build the struct.
2180 Expression
* constructor
=
2181 Expression::make_struct_composite_literal(this->struct_type_
, vals
,
2184 // Allocate the initialized struct on the heap.
2185 constructor
= Expression::make_heap_expression(constructor
, location
);
2187 // Look up the thunk.
2188 Named_object
* named_thunk
= gogo
->lookup(thunk_name
, NULL
);
2189 go_assert(named_thunk
!= NULL
&& named_thunk
->is_function());
2192 Expression
* func
= Expression::make_func_reference(named_thunk
, NULL
,
2194 Expression_list
* params
= new Expression_list();
2195 params
->push_back(constructor
);
2196 Call_expression
* call
= Expression::make_call(func
, params
, false, location
);
2198 // Build the simple go or defer statement.
2200 if (this->classification() == STATEMENT_GO
)
2201 s
= Statement::make_go_statement(call
, location
);
2202 else if (this->classification() == STATEMENT_DEFER
)
2203 s
= Statement::make_defer_statement(call
, location
);
2207 // The current block should end with the go statement.
2208 go_assert(block
->statements()->size() >= 1);
2209 go_assert(block
->statements()->back() == this);
2210 block
->replace_statement(block
->statements()->size() - 1, s
);
2212 // We already ran the determine_types pass, so we need to run it now
2213 // for the new statement.
2214 s
->determine_types();
2217 gogo
->check_types_in_block(block
);
2219 // Return true to tell the block not to keep looking at statements.
2223 // Set the name to use for thunk parameter N.
2226 Thunk_statement::thunk_field_param(int n
, char* buf
, size_t buflen
)
2228 snprintf(buf
, buflen
, "a%d", n
);
2231 // Build a new struct type to hold the parameters for a complicated
2232 // thunk statement. FNTYPE is the type of the function call.
2235 Thunk_statement::build_struct(Function_type
* fntype
)
2237 Location location
= this->location();
2239 Struct_field_list
* fields
= new Struct_field_list();
2241 Call_expression
* ce
= this->call_
->call_expression();
2242 Expression
* fn
= ce
->fn();
2244 if (!this->is_constant_function())
2246 // The function to call.
2247 fields
->push_back(Struct_field(Typed_identifier("fn", fntype
,
2251 // If this thunk statement calls a method on an interface, we pass
2252 // the interface object to the thunk.
2253 Interface_field_reference_expression
* interface_method
=
2254 fn
->interface_field_reference_expression();
2255 if (interface_method
!= NULL
)
2257 Typed_identifier
tid("object", interface_method
->expr()->type(),
2259 fields
->push_back(Struct_field(tid
));
2262 // The predeclared recover function has no argument. However, we
2263 // add an argument when building recover thunks. Handle that here.
2264 if (ce
->is_recover_call())
2266 fields
->push_back(Struct_field(Typed_identifier("can_recover",
2267 Type::lookup_bool_type(),
2271 const Expression_list
* args
= ce
->args();
2275 for (Expression_list::const_iterator p
= args
->begin();
2280 this->thunk_field_param(i
, buf
, sizeof buf
);
2281 fields
->push_back(Struct_field(Typed_identifier(buf
, (*p
)->type(),
2286 return Type::make_struct_type(fields
, location
);
2289 // Build the thunk we are going to call. This is a brand new, albeit
2290 // artificial, function.
2293 Thunk_statement::build_thunk(Gogo
* gogo
, const std::string
& thunk_name
)
2295 Location location
= this->location();
2297 Call_expression
* ce
= this->call_
->call_expression();
2299 bool may_call_recover
= false;
2300 if (this->classification() == STATEMENT_DEFER
)
2302 Func_expression
* fn
= ce
->fn()->func_expression();
2304 may_call_recover
= true;
2307 const Named_object
* no
= fn
->named_object();
2308 if (!no
->is_function())
2309 may_call_recover
= true;
2311 may_call_recover
= no
->func_value()->calls_recover();
2315 // Build the type of the thunk. The thunk takes a single parameter,
2316 // which is a pointer to the special structure we build.
2317 const char* const parameter_name
= "__go_thunk_parameter";
2318 Typed_identifier_list
* thunk_parameters
= new Typed_identifier_list();
2319 Type
* pointer_to_struct_type
= Type::make_pointer_type(this->struct_type_
);
2320 thunk_parameters
->push_back(Typed_identifier(parameter_name
,
2321 pointer_to_struct_type
,
2324 Typed_identifier_list
* thunk_results
= NULL
;
2325 if (may_call_recover
)
2327 // When deferring a function which may call recover, add a
2328 // return value, to disable tail call optimizations which will
2329 // break the way we check whether recover is permitted.
2330 thunk_results
= new Typed_identifier_list();
2331 thunk_results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
2335 Function_type
* thunk_type
= Type::make_function_type(NULL
, thunk_parameters
,
2339 // Start building the thunk.
2340 Named_object
* function
= gogo
->start_function(thunk_name
, thunk_type
, true,
2343 gogo
->start_block(location
);
2345 // For a defer statement, start with a call to
2346 // __go_set_defer_retaddr. */
2347 Label
* retaddr_label
= NULL
;
2348 if (may_call_recover
)
2350 retaddr_label
= gogo
->add_label_reference("retaddr", location
, false);
2351 Expression
* arg
= Expression::make_label_addr(retaddr_label
, location
);
2352 Expression
* call
= Runtime::make_call(Runtime::SET_DEFER_RETADDR
,
2355 // This is a hack to prevent the middle-end from deleting the
2357 gogo
->start_block(location
);
2358 gogo
->add_statement(Statement::make_goto_statement(retaddr_label
,
2360 Block
* then_block
= gogo
->finish_block(location
);
2361 then_block
->determine_types();
2363 Statement
* s
= Statement::make_if_statement(call
, then_block
, NULL
,
2365 s
->determine_types();
2366 gogo
->add_statement(s
);
2369 // Get a reference to the parameter.
2370 Named_object
* named_parameter
= gogo
->lookup(parameter_name
, NULL
);
2371 go_assert(named_parameter
!= NULL
&& named_parameter
->is_variable());
2373 // Build the call. Note that the field names are the same as the
2374 // ones used in build_struct.
2375 Expression
* thunk_parameter
= Expression::make_var_reference(named_parameter
,
2377 thunk_parameter
= Expression::make_unary(OPERATOR_MULT
, thunk_parameter
,
2380 Interface_field_reference_expression
* interface_method
=
2381 ce
->fn()->interface_field_reference_expression();
2383 Expression
* func_to_call
;
2384 unsigned int next_index
;
2385 if (this->is_constant_function())
2387 func_to_call
= ce
->fn();
2392 func_to_call
= Expression::make_field_reference(thunk_parameter
,
2397 if (interface_method
!= NULL
)
2399 // The main program passes the interface object.
2400 go_assert(next_index
== 0);
2401 Expression
* r
= Expression::make_field_reference(thunk_parameter
, 0,
2403 const std::string
& name(interface_method
->name());
2404 func_to_call
= Expression::make_interface_field_reference(r
, name
,
2409 Expression_list
* call_params
= new Expression_list();
2410 const Struct_field_list
* fields
= this->struct_type_
->fields();
2411 Struct_field_list::const_iterator p
= fields
->begin();
2412 for (unsigned int i
= 0; i
< next_index
; ++i
)
2414 bool is_recover_call
= ce
->is_recover_call();
2415 Expression
* recover_arg
= NULL
;
2416 for (; p
!= fields
->end(); ++p
, ++next_index
)
2418 Expression
* thunk_param
= Expression::make_var_reference(named_parameter
,
2420 thunk_param
= Expression::make_unary(OPERATOR_MULT
, thunk_param
,
2422 Expression
* param
= Expression::make_field_reference(thunk_param
,
2425 if (!is_recover_call
)
2426 call_params
->push_back(param
);
2429 go_assert(call_params
->empty());
2430 recover_arg
= param
;
2434 if (call_params
->empty())
2440 Call_expression
* call
= Expression::make_call(func_to_call
, call_params
,
2443 // This call expression was already lowered before entering the
2444 // thunk statement. Don't try to lower varargs again, as that will
2445 // cause confusion for, e.g., method calls which already have a
2446 // receiver parameter.
2447 call
->set_varargs_are_lowered();
2449 Statement
* call_statement
= Statement::make_statement(call
, true);
2451 gogo
->add_statement(call_statement
);
2453 // If this is a defer statement, the label comes immediately after
2455 if (may_call_recover
)
2457 gogo
->add_label_definition("retaddr", location
);
2459 Expression_list
* vals
= new Expression_list();
2460 vals
->push_back(Expression::make_boolean(false, location
));
2461 gogo
->add_statement(Statement::make_return_statement(vals
, location
));
2464 Block
* b
= gogo
->finish_block(location
);
2466 gogo
->add_block(b
, location
);
2468 gogo
->lower_block(function
, b
);
2469 gogo
->flatten_block(function
, b
);
2471 // We already ran the determine_types pass, so we need to run it
2472 // just for the call statement now. The other types are known.
2473 call_statement
->determine_types();
2475 if (may_call_recover
|| recover_arg
!= NULL
)
2477 // Dig up the call expression, which may have been changed
2479 go_assert(call_statement
->classification() == STATEMENT_EXPRESSION
);
2480 Expression_statement
* es
=
2481 static_cast<Expression_statement
*>(call_statement
);
2482 Call_expression
* ce
= es
->expr()->call_expression();
2484 go_assert(saw_errors());
2487 if (may_call_recover
)
2488 ce
->set_is_deferred();
2489 if (recover_arg
!= NULL
)
2490 ce
->set_recover_arg(recover_arg
);
2494 // That is all the thunk has to do.
2495 gogo
->finish_function(location
);
2498 // Get the function and argument expressions.
2501 Thunk_statement::get_fn_and_arg(Expression
** pfn
, Expression
** parg
)
2503 if (this->call_
->is_error_expression())
2506 Call_expression
* ce
= this->call_
->call_expression();
2508 Expression
* fn
= ce
->fn();
2509 Func_expression
* fe
= fn
->func_expression();
2510 go_assert(fe
!= NULL
);
2511 *pfn
= Expression::make_func_code_reference(fe
->named_object(),
2514 const Expression_list
* args
= ce
->args();
2515 if (args
== NULL
|| args
->empty())
2516 *parg
= Expression::make_nil(this->location());
2519 go_assert(args
->size() == 1);
2520 *parg
= args
->front();
2526 // Class Go_statement.
2529 Go_statement::do_get_backend(Translate_context
* context
)
2533 if (!this->get_fn_and_arg(&fn
, &arg
))
2534 return context
->backend()->error_statement();
2536 Expression
* call
= Runtime::make_call(Runtime::GO
, this->location(), 2,
2538 Bexpression
* bcall
= call
->get_backend(context
);
2539 return context
->backend()->expression_statement(bcall
);
2542 // Dump the AST representation for go statement.
2545 Go_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2547 ast_dump_context
->print_indent();
2548 ast_dump_context
->ostream() << "go ";
2549 ast_dump_context
->dump_expression(this->call());
2550 ast_dump_context
->ostream() << std::endl
;
2553 // Make a go statement.
2556 Statement::make_go_statement(Call_expression
* call
, Location location
)
2558 return new Go_statement(call
, location
);
2561 // Class Defer_statement.
2564 Defer_statement::do_get_backend(Translate_context
* context
)
2568 if (!this->get_fn_and_arg(&fn
, &arg
))
2569 return context
->backend()->error_statement();
2571 Location loc
= this->location();
2572 Expression
* ds
= context
->function()->func_value()->defer_stack(loc
);
2574 Expression
* call
= Runtime::make_call(Runtime::DEFER
, loc
, 3,
2576 Bexpression
* bcall
= call
->get_backend(context
);
2577 return context
->backend()->expression_statement(bcall
);
2580 // Dump the AST representation for defer statement.
2583 Defer_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2585 ast_dump_context
->print_indent();
2586 ast_dump_context
->ostream() << "defer ";
2587 ast_dump_context
->dump_expression(this->call());
2588 ast_dump_context
->ostream() << std::endl
;
2591 // Make a defer statement.
2594 Statement::make_defer_statement(Call_expression
* call
,
2597 return new Defer_statement(call
, location
);
2600 // Class Return_statement.
2602 // Traverse assignments. We treat each return value as a top level
2603 // RHS in an expression.
2606 Return_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
2608 Expression_list
* vals
= this->vals_
;
2611 for (Expression_list::iterator p
= vals
->begin();
2614 tassign
->value(&*p
, true, true);
2619 // Lower a return statement. If we are returning a function call
2620 // which returns multiple values which match the current function,
2621 // split up the call's results. If the return statement lists
2622 // explicit values, implement this statement by assigning the values
2623 // to the result variables and change this statement to a naked
2624 // return. This lets panic/recover work correctly.
2627 Return_statement::do_lower(Gogo
*, Named_object
* function
, Block
* enclosing
,
2628 Statement_inserter
*)
2630 if (this->is_lowered_
)
2633 Expression_list
* vals
= this->vals_
;
2635 this->is_lowered_
= true;
2637 Location loc
= this->location();
2639 size_t vals_count
= vals
== NULL
? 0 : vals
->size();
2640 Function::Results
* results
= function
->func_value()->result_variables();
2641 size_t results_count
= results
== NULL
? 0 : results
->size();
2643 if (vals_count
== 0)
2645 if (results_count
> 0 && !function
->func_value()->results_are_named())
2647 this->report_error(_("not enough arguments to return"));
2653 if (results_count
== 0)
2655 this->report_error(_("return with value in function "
2656 "with no return type"));
2660 // If the current function has multiple return values, and we are
2661 // returning a single call expression, split up the call expression.
2662 if (results_count
> 1
2663 && vals
->size() == 1
2664 && vals
->front()->call_expression() != NULL
)
2666 Call_expression
* call
= vals
->front()->call_expression();
2667 call
->set_expected_result_count(results_count
);
2669 vals
= new Expression_list
;
2670 for (size_t i
= 0; i
< results_count
; ++i
)
2671 vals
->push_back(Expression::make_call_result(call
, i
));
2672 vals_count
= results_count
;
2675 if (vals_count
< results_count
)
2677 this->report_error(_("not enough arguments to return"));
2681 if (vals_count
> results_count
)
2683 this->report_error(_("too many values in return statement"));
2687 Block
* b
= new Block(enclosing
, loc
);
2689 Expression_list
* lhs
= new Expression_list();
2690 Expression_list
* rhs
= new Expression_list();
2692 Expression_list::const_iterator pe
= vals
->begin();
2694 for (Function::Results::const_iterator pr
= results
->begin();
2695 pr
!= results
->end();
2698 Named_object
* rv
= *pr
;
2699 Expression
* e
= *pe
;
2701 // Check types now so that we give a good error message. The
2702 // result type is known. We determine the expression type
2705 Type
*rvtype
= rv
->result_var_value()->type();
2706 Type_context
type_context(rvtype
, false);
2707 e
->determine_type(&type_context
);
2711 if (this->are_hidden_fields_ok_
)
2712 ok
= Type::are_assignable_hidden_ok(rvtype
, e
->type(), &reason
);
2714 ok
= Type::are_assignable(rvtype
, e
->type(), &reason
);
2717 Expression
* ve
= Expression::make_var_reference(rv
, e
->location());
2724 error_at(e
->location(), "incompatible type for return value %d", i
);
2726 error_at(e
->location(),
2727 "incompatible type for return value %d (%s)",
2731 go_assert(lhs
->size() == rhs
->size());
2735 else if (lhs
->size() == 1)
2737 Statement
* s
= Statement::make_assignment(lhs
->front(), rhs
->front(),
2739 if (this->are_hidden_fields_ok_
)
2741 Assignment_statement
* as
= static_cast<Assignment_statement
*>(s
);
2742 as
->set_hidden_fields_are_ok();
2744 b
->add_statement(s
);
2750 Statement
* s
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
2751 if (this->are_hidden_fields_ok_
)
2753 Tuple_assignment_statement
* tas
=
2754 static_cast<Tuple_assignment_statement
*>(s
);
2755 tas
->set_hidden_fields_are_ok();
2757 b
->add_statement(s
);
2760 b
->add_statement(this);
2764 return Statement::make_block_statement(b
, loc
);
2767 // Convert a return statement to the backend representation.
2770 Return_statement::do_get_backend(Translate_context
* context
)
2772 Location loc
= this->location();
2774 Function
* function
= context
->function()->func_value();
2775 Function::Results
* results
= function
->result_variables();
2776 std::vector
<Bexpression
*> retvals
;
2777 if (results
!= NULL
&& !results
->empty())
2779 retvals
.reserve(results
->size());
2780 for (Function::Results::const_iterator p
= results
->begin();
2781 p
!= results
->end();
2784 Expression
* vr
= Expression::make_var_reference(*p
, loc
);
2785 retvals
.push_back(vr
->get_backend(context
));
2789 return context
->backend()->return_statement(function
->get_decl(),
2793 // Dump the AST representation for a return statement.
2796 Return_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2798 ast_dump_context
->print_indent();
2799 ast_dump_context
->ostream() << "return " ;
2800 ast_dump_context
->dump_expression_list(this->vals_
);
2801 ast_dump_context
->ostream() << std::endl
;
2804 // Make a return statement.
2807 Statement::make_return_statement(Expression_list
* vals
,
2810 return new Return_statement(vals
, location
);
2813 // Make a statement that returns the result of a call expression.
2816 Statement::make_return_from_call(Call_expression
* call
, Location location
)
2818 size_t rc
= call
->result_count();
2820 return Statement::make_statement(call
, true);
2823 Expression_list
* vals
= new Expression_list();
2825 vals
->push_back(call
);
2828 for (size_t i
= 0; i
< rc
; ++i
)
2829 vals
->push_back(Expression::make_call_result(call
, i
));
2831 return Statement::make_return_statement(vals
, location
);
2835 // A break or continue statement.
2837 class Bc_statement
: public Statement
2840 Bc_statement(bool is_break
, Unnamed_label
* label
, Location location
)
2841 : Statement(STATEMENT_BREAK_OR_CONTINUE
, location
),
2842 label_(label
), is_break_(is_break
)
2847 { return this->is_break_
; }
2851 do_traverse(Traverse
*)
2852 { return TRAVERSE_CONTINUE
; }
2855 do_may_fall_through() const
2859 do_get_backend(Translate_context
* context
)
2860 { return this->label_
->get_goto(context
, this->location()); }
2863 do_dump_statement(Ast_dump_context
*) const;
2866 // The label that this branches to.
2867 Unnamed_label
* label_
;
2868 // True if this is "break", false if it is "continue".
2872 // Dump the AST representation for a break/continue statement
2875 Bc_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2877 ast_dump_context
->print_indent();
2878 ast_dump_context
->ostream() << (this->is_break_
? "break" : "continue");
2879 if (this->label_
!= NULL
)
2881 ast_dump_context
->ostream() << " ";
2882 ast_dump_context
->dump_label_name(this->label_
);
2884 ast_dump_context
->ostream() << std::endl
;
2887 // Make a break statement.
2890 Statement::make_break_statement(Unnamed_label
* label
, Location location
)
2892 return new Bc_statement(true, label
, location
);
2895 // Make a continue statement.
2898 Statement::make_continue_statement(Unnamed_label
* label
,
2901 return new Bc_statement(false, label
, location
);
2904 // A goto statement.
2906 class Goto_statement
: public Statement
2909 Goto_statement(Label
* label
, Location location
)
2910 : Statement(STATEMENT_GOTO
, location
),
2916 do_traverse(Traverse
*)
2917 { return TRAVERSE_CONTINUE
; }
2920 do_check_types(Gogo
*);
2923 do_may_fall_through() const
2927 do_get_backend(Translate_context
*);
2930 do_dump_statement(Ast_dump_context
*) const;
2936 // Check types for a label. There aren't any types per se, but we use
2937 // this to give an error if the label was never defined.
2940 Goto_statement::do_check_types(Gogo
*)
2942 if (!this->label_
->is_defined())
2944 error_at(this->location(), "reference to undefined label %qs",
2945 Gogo::message_name(this->label_
->name()).c_str());
2946 this->set_is_error();
2950 // Convert the goto statement to the backend representation.
2953 Goto_statement::do_get_backend(Translate_context
* context
)
2955 Blabel
* blabel
= this->label_
->get_backend_label(context
);
2956 return context
->backend()->goto_statement(blabel
, this->location());
2959 // Dump the AST representation for a goto statement.
2962 Goto_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2964 ast_dump_context
->print_indent();
2965 ast_dump_context
->ostream() << "goto " << this->label_
->name() << std::endl
;
2968 // Make a goto statement.
2971 Statement::make_goto_statement(Label
* label
, Location location
)
2973 return new Goto_statement(label
, location
);
2976 // A goto statement to an unnamed label.
2978 class Goto_unnamed_statement
: public Statement
2981 Goto_unnamed_statement(Unnamed_label
* label
, Location location
)
2982 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
2988 do_traverse(Traverse
*)
2989 { return TRAVERSE_CONTINUE
; }
2992 do_may_fall_through() const
2996 do_get_backend(Translate_context
* context
)
2997 { return this->label_
->get_goto(context
, this->location()); }
3000 do_dump_statement(Ast_dump_context
*) const;
3003 Unnamed_label
* label_
;
3006 // Dump the AST representation for an unnamed goto statement
3009 Goto_unnamed_statement::do_dump_statement(
3010 Ast_dump_context
* ast_dump_context
) const
3012 ast_dump_context
->print_indent();
3013 ast_dump_context
->ostream() << "goto ";
3014 ast_dump_context
->dump_label_name(this->label_
);
3015 ast_dump_context
->ostream() << std::endl
;
3018 // Make a goto statement to an unnamed label.
3021 Statement::make_goto_unnamed_statement(Unnamed_label
* label
,
3024 return new Goto_unnamed_statement(label
, location
);
3027 // Class Label_statement.
3032 Label_statement::do_traverse(Traverse
*)
3034 return TRAVERSE_CONTINUE
;
3037 // Return the backend representation of the statement defining this
3041 Label_statement::do_get_backend(Translate_context
* context
)
3043 Blabel
* blabel
= this->label_
->get_backend_label(context
);
3044 return context
->backend()->label_definition_statement(blabel
);
3047 // Dump the AST for a label definition statement.
3050 Label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3052 ast_dump_context
->print_indent();
3053 ast_dump_context
->ostream() << this->label_
->name() << ":" << std::endl
;
3056 // Make a label statement.
3059 Statement::make_label_statement(Label
* label
, Location location
)
3061 return new Label_statement(label
, location
);
3064 // An unnamed label statement.
3066 class Unnamed_label_statement
: public Statement
3069 Unnamed_label_statement(Unnamed_label
* label
)
3070 : Statement(STATEMENT_UNNAMED_LABEL
, label
->location()),
3076 do_traverse(Traverse
*)
3077 { return TRAVERSE_CONTINUE
; }
3080 do_get_backend(Translate_context
* context
)
3081 { return this->label_
->get_definition(context
); }
3084 do_dump_statement(Ast_dump_context
*) const;
3088 Unnamed_label
* label_
;
3091 // Dump the AST representation for an unnamed label definition statement.
3094 Unnamed_label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3097 ast_dump_context
->print_indent();
3098 ast_dump_context
->dump_label_name(this->label_
);
3099 ast_dump_context
->ostream() << ":" << std::endl
;
3102 // Make an unnamed label statement.
3105 Statement::make_unnamed_label_statement(Unnamed_label
* label
)
3107 return new Unnamed_label_statement(label
);
3112 class If_statement
: public Statement
3115 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
3117 : Statement(STATEMENT_IF
, location
),
3118 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
3123 do_traverse(Traverse
*);
3126 do_determine_types();
3129 do_check_types(Gogo
*);
3132 do_may_fall_through() const;
3135 do_get_backend(Translate_context
*);
3138 do_dump_statement(Ast_dump_context
*) const;
3149 If_statement::do_traverse(Traverse
* traverse
)
3151 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
3152 || this->then_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3153 return TRAVERSE_EXIT
;
3154 if (this->else_block_
!= NULL
)
3156 if (this->else_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3157 return TRAVERSE_EXIT
;
3159 return TRAVERSE_CONTINUE
;
3163 If_statement::do_determine_types()
3165 Type_context
context(Type::lookup_bool_type(), false);
3166 this->cond_
->determine_type(&context
);
3167 this->then_block_
->determine_types();
3168 if (this->else_block_
!= NULL
)
3169 this->else_block_
->determine_types();
3175 If_statement::do_check_types(Gogo
*)
3177 Type
* type
= this->cond_
->type();
3178 if (type
->is_error())
3179 this->set_is_error();
3180 else if (!type
->is_boolean_type())
3181 this->report_error(_("expected boolean expression"));
3184 // Whether the overall statement may fall through.
3187 If_statement::do_may_fall_through() const
3189 return (this->else_block_
== NULL
3190 || this->then_block_
->may_fall_through()
3191 || this->else_block_
->may_fall_through());
3194 // Get the backend representation.
3197 If_statement::do_get_backend(Translate_context
* context
)
3199 go_assert(this->cond_
->type()->is_boolean_type()
3200 || this->cond_
->type()->is_error());
3201 Bexpression
* cond
= this->cond_
->get_backend(context
);
3202 Bblock
* then_block
= this->then_block_
->get_backend(context
);
3203 Bblock
* else_block
= (this->else_block_
== NULL
3205 : this->else_block_
->get_backend(context
));
3206 return context
->backend()->if_statement(cond
, then_block
, else_block
,
3210 // Dump the AST representation for an if statement
3213 If_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3215 ast_dump_context
->print_indent();
3216 ast_dump_context
->ostream() << "if ";
3217 ast_dump_context
->dump_expression(this->cond_
);
3218 ast_dump_context
->ostream() << std::endl
;
3219 if (ast_dump_context
->dump_subblocks())
3221 ast_dump_context
->dump_block(this->then_block_
);
3222 if (this->else_block_
!= NULL
)
3224 ast_dump_context
->print_indent();
3225 ast_dump_context
->ostream() << "else" << std::endl
;
3226 ast_dump_context
->dump_block(this->else_block_
);
3231 // Make an if statement.
3234 Statement::make_if_statement(Expression
* cond
, Block
* then_block
,
3235 Block
* else_block
, Location location
)
3237 return new If_statement(cond
, then_block
, else_block
, location
);
3240 // Class Case_clauses::Hash_integer_value.
3242 class Case_clauses::Hash_integer_value
3246 operator()(Expression
*) const;
3250 Case_clauses::Hash_integer_value::operator()(Expression
* pe
) const
3252 Numeric_constant nc
;
3254 if (!pe
->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3256 size_t ret
= mpz_get_ui(ival
);
3261 // Class Case_clauses::Eq_integer_value.
3263 class Case_clauses::Eq_integer_value
3267 operator()(Expression
*, Expression
*) const;
3271 Case_clauses::Eq_integer_value::operator()(Expression
* a
, Expression
* b
) const
3273 Numeric_constant anc
;
3275 Numeric_constant bnc
;
3277 if (!a
->numeric_constant_value(&anc
)
3278 || !anc
.to_int(&aval
)
3279 || !b
->numeric_constant_value(&bnc
)
3280 || !bnc
.to_int(&bval
))
3282 bool ret
= mpz_cmp(aval
, bval
) == 0;
3288 // Class Case_clauses::Case_clause.
3293 Case_clauses::Case_clause::traverse(Traverse
* traverse
)
3295 if (this->cases_
!= NULL
3296 && (traverse
->traverse_mask()
3297 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3299 if (this->cases_
->traverse(traverse
) == TRAVERSE_EXIT
)
3300 return TRAVERSE_EXIT
;
3302 if (this->statements_
!= NULL
)
3304 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
3305 return TRAVERSE_EXIT
;
3307 return TRAVERSE_CONTINUE
;
3310 // Check whether all the case expressions are integer constants.
3313 Case_clauses::Case_clause::is_constant() const
3315 if (this->cases_
!= NULL
)
3317 for (Expression_list::const_iterator p
= this->cases_
->begin();
3318 p
!= this->cases_
->end();
3320 if (!(*p
)->is_constant() || (*p
)->type()->integer_type() == NULL
)
3326 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3327 // value we are switching on; it may be NULL. If START_LABEL is not
3328 // NULL, it goes at the start of the statements, after the condition
3329 // test. We branch to FINISH_LABEL at the end of the statements.
3332 Case_clauses::Case_clause::lower(Block
* b
, Temporary_statement
* val_temp
,
3333 Unnamed_label
* start_label
,
3334 Unnamed_label
* finish_label
) const
3336 Location loc
= this->location_
;
3337 Unnamed_label
* next_case_label
;
3338 if (this->cases_
== NULL
|| this->cases_
->empty())
3340 go_assert(this->is_default_
);
3341 next_case_label
= NULL
;
3345 Expression
* cond
= NULL
;
3347 for (Expression_list::const_iterator p
= this->cases_
->begin();
3348 p
!= this->cases_
->end();
3351 Expression
* ref
= Expression::make_temporary_reference(val_temp
,
3353 Expression
* this_cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3358 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, this_cond
, loc
);
3361 Block
* then_block
= new Block(b
, loc
);
3362 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3363 Statement
* s
= Statement::make_goto_unnamed_statement(next_case_label
,
3365 then_block
->add_statement(s
);
3367 // if !COND { goto NEXT_CASE_LABEL }
3368 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3369 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
3370 b
->add_statement(s
);
3373 if (start_label
!= NULL
)
3374 b
->add_statement(Statement::make_unnamed_label_statement(start_label
));
3376 if (this->statements_
!= NULL
)
3377 b
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
3379 Statement
* s
= Statement::make_goto_unnamed_statement(finish_label
, loc
);
3380 b
->add_statement(s
);
3382 if (next_case_label
!= NULL
)
3383 b
->add_statement(Statement::make_unnamed_label_statement(next_case_label
));
3389 Case_clauses::Case_clause::determine_types(Type
* type
)
3391 if (this->cases_
!= NULL
)
3393 Type_context
case_context(type
, false);
3394 for (Expression_list::iterator p
= this->cases_
->begin();
3395 p
!= this->cases_
->end();
3397 (*p
)->determine_type(&case_context
);
3399 if (this->statements_
!= NULL
)
3400 this->statements_
->determine_types();
3403 // Check types. Returns false if there was an error.
3406 Case_clauses::Case_clause::check_types(Type
* type
)
3408 if (this->cases_
!= NULL
)
3410 for (Expression_list::iterator p
= this->cases_
->begin();
3411 p
!= this->cases_
->end();
3414 if (!Type::are_assignable(type
, (*p
)->type(), NULL
)
3415 && !Type::are_assignable((*p
)->type(), type
, NULL
))
3417 error_at((*p
)->location(),
3418 "type mismatch between switch value and case clause");
3426 // Return true if this clause may fall through to the following
3427 // statements. Note that this is not the same as whether the case
3428 // uses the "fallthrough" keyword.
3431 Case_clauses::Case_clause::may_fall_through() const
3433 if (this->statements_
== NULL
)
3435 return this->statements_
->may_fall_through();
3438 // Convert the case values and statements to the backend
3439 // representation. BREAK_LABEL is the label which break statements
3440 // should branch to. CASE_CONSTANTS is used to detect duplicate
3441 // constants. *CASES should be passed as an empty vector; the values
3442 // for this case will be added to it. If this is the default case,
3443 // *CASES will remain empty. This returns the statement to execute if
3444 // one of these cases is selected.
3447 Case_clauses::Case_clause::get_backend(Translate_context
* context
,
3448 Unnamed_label
* break_label
,
3449 Case_constants
* case_constants
,
3450 std::vector
<Bexpression
*>* cases
) const
3452 if (this->cases_
!= NULL
)
3454 go_assert(!this->is_default_
);
3455 for (Expression_list::const_iterator p
= this->cases_
->begin();
3456 p
!= this->cases_
->end();
3460 if (e
->classification() != Expression::EXPRESSION_INTEGER
)
3462 Numeric_constant nc
;
3464 if (!(*p
)->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3466 // Something went wrong. This can happen with a
3467 // negative constant and an unsigned switch value.
3468 go_assert(saw_errors());
3471 go_assert(nc
.type() != NULL
);
3472 e
= Expression::make_integer(&ival
, nc
.type(), e
->location());
3476 std::pair
<Case_constants::iterator
, bool> ins
=
3477 case_constants
->insert(e
);
3480 // Value was already present.
3481 error_at(this->location_
, "duplicate case in switch");
3482 e
= Expression::make_error(this->location_
);
3484 cases
->push_back(e
->get_backend(context
));
3488 Bstatement
* statements
;
3489 if (this->statements_
== NULL
)
3493 Bblock
* bblock
= this->statements_
->get_backend(context
);
3494 statements
= context
->backend()->block_statement(bblock
);
3497 Bstatement
* break_stat
;
3498 if (this->is_fallthrough_
)
3501 break_stat
= break_label
->get_goto(context
, this->location_
);
3503 if (statements
== NULL
)
3505 else if (break_stat
== NULL
)
3508 return context
->backend()->compound_statement(statements
, break_stat
);
3511 // Dump the AST representation for a case clause
3514 Case_clauses::Case_clause::dump_clause(Ast_dump_context
* ast_dump_context
)
3517 ast_dump_context
->print_indent();
3518 if (this->is_default_
)
3520 ast_dump_context
->ostream() << "default:";
3524 ast_dump_context
->ostream() << "case ";
3525 ast_dump_context
->dump_expression_list(this->cases_
);
3526 ast_dump_context
->ostream() << ":" ;
3528 ast_dump_context
->dump_block(this->statements_
);
3529 if (this->is_fallthrough_
)
3531 ast_dump_context
->print_indent();
3532 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
3536 // Class Case_clauses.
3541 Case_clauses::traverse(Traverse
* traverse
)
3543 for (Clauses::iterator p
= this->clauses_
.begin();
3544 p
!= this->clauses_
.end();
3547 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
3548 return TRAVERSE_EXIT
;
3550 return TRAVERSE_CONTINUE
;
3553 // Check whether all the case expressions are constant.
3556 Case_clauses::is_constant() const
3558 for (Clauses::const_iterator p
= this->clauses_
.begin();
3559 p
!= this->clauses_
.end();
3561 if (!p
->is_constant())
3566 // Lower case clauses for a nonconstant switch.
3569 Case_clauses::lower(Block
* b
, Temporary_statement
* val_temp
,
3570 Unnamed_label
* break_label
) const
3572 // The default case.
3573 const Case_clause
* default_case
= NULL
;
3575 // The label for the fallthrough of the previous case.
3576 Unnamed_label
* last_fallthrough_label
= NULL
;
3578 // The label for the start of the default case. This is used if the
3579 // case before the default case falls through.
3580 Unnamed_label
* default_start_label
= NULL
;
3582 // The label for the end of the default case. This normally winds
3583 // up as BREAK_LABEL, but it will be different if the default case
3585 Unnamed_label
* default_finish_label
= NULL
;
3587 for (Clauses::const_iterator p
= this->clauses_
.begin();
3588 p
!= this->clauses_
.end();
3591 // The label to use for the start of the statements for this
3592 // case. This is NULL unless the previous case falls through.
3593 Unnamed_label
* start_label
= last_fallthrough_label
;
3595 // The label to jump to after the end of the statements for this
3597 Unnamed_label
* finish_label
= break_label
;
3599 last_fallthrough_label
= NULL
;
3600 if (p
->is_fallthrough() && p
+ 1 != this->clauses_
.end())
3602 finish_label
= new Unnamed_label(p
->location());
3603 last_fallthrough_label
= finish_label
;
3606 if (!p
->is_default())
3607 p
->lower(b
, val_temp
, start_label
, finish_label
);
3610 // We have to move the default case to the end, so that we
3611 // only use it if all the other tests fail.
3613 default_start_label
= start_label
;
3614 default_finish_label
= finish_label
;
3618 if (default_case
!= NULL
)
3619 default_case
->lower(b
, val_temp
, default_start_label
,
3620 default_finish_label
);
3626 Case_clauses::determine_types(Type
* type
)
3628 for (Clauses::iterator p
= this->clauses_
.begin();
3629 p
!= this->clauses_
.end();
3631 p
->determine_types(type
);
3634 // Check types. Returns false if there was an error.
3637 Case_clauses::check_types(Type
* type
)
3640 for (Clauses::iterator p
= this->clauses_
.begin();
3641 p
!= this->clauses_
.end();
3644 if (!p
->check_types(type
))
3650 // Return true if these clauses may fall through to the statements
3651 // following the switch statement.
3654 Case_clauses::may_fall_through() const
3656 bool found_default
= false;
3657 for (Clauses::const_iterator p
= this->clauses_
.begin();
3658 p
!= this->clauses_
.end();
3661 if (p
->may_fall_through() && !p
->is_fallthrough())
3663 if (p
->is_default())
3664 found_default
= true;
3666 return !found_default
;
3669 // Convert the cases to the backend representation. This sets
3670 // *ALL_CASES and *ALL_STATEMENTS.
3673 Case_clauses::get_backend(Translate_context
* context
,
3674 Unnamed_label
* break_label
,
3675 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
3676 std::vector
<Bstatement
*>* all_statements
) const
3678 Case_constants case_constants
;
3680 size_t c
= this->clauses_
.size();
3681 all_cases
->resize(c
);
3682 all_statements
->resize(c
);
3685 for (Clauses::const_iterator p
= this->clauses_
.begin();
3686 p
!= this->clauses_
.end();
3689 std::vector
<Bexpression
*> cases
;
3690 Bstatement
* stat
= p
->get_backend(context
, break_label
, &case_constants
,
3692 (*all_cases
)[i
].swap(cases
);
3693 (*all_statements
)[i
] = stat
;
3697 // Dump the AST representation for case clauses (from a switch statement)
3700 Case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
3702 for (Clauses::const_iterator p
= this->clauses_
.begin();
3703 p
!= this->clauses_
.end();
3705 p
->dump_clause(ast_dump_context
);
3708 // A constant switch statement. A Switch_statement is lowered to this
3709 // when all the cases are constants.
3711 class Constant_switch_statement
: public Statement
3714 Constant_switch_statement(Expression
* val
, Case_clauses
* clauses
,
3715 Unnamed_label
* break_label
,
3717 : Statement(STATEMENT_CONSTANT_SWITCH
, location
),
3718 val_(val
), clauses_(clauses
), break_label_(break_label
)
3723 do_traverse(Traverse
*);
3726 do_determine_types();
3729 do_check_types(Gogo
*);
3732 do_get_backend(Translate_context
*);
3735 do_dump_statement(Ast_dump_context
*) const;
3738 // The value to switch on.
3740 // The case clauses.
3741 Case_clauses
* clauses_
;
3742 // The break label, if needed.
3743 Unnamed_label
* break_label_
;
3749 Constant_switch_statement::do_traverse(Traverse
* traverse
)
3751 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3752 return TRAVERSE_EXIT
;
3753 return this->clauses_
->traverse(traverse
);
3759 Constant_switch_statement::do_determine_types()
3761 this->val_
->determine_type_no_context();
3762 this->clauses_
->determine_types(this->val_
->type());
3768 Constant_switch_statement::do_check_types(Gogo
*)
3770 if (!this->clauses_
->check_types(this->val_
->type()))
3771 this->set_is_error();
3774 // Convert to GENERIC.
3777 Constant_switch_statement::do_get_backend(Translate_context
* context
)
3779 Bexpression
* switch_val_expr
= this->val_
->get_backend(context
);
3781 Unnamed_label
* break_label
= this->break_label_
;
3782 if (break_label
== NULL
)
3783 break_label
= new Unnamed_label(this->location());
3785 std::vector
<std::vector
<Bexpression
*> > all_cases
;
3786 std::vector
<Bstatement
*> all_statements
;
3787 this->clauses_
->get_backend(context
, break_label
, &all_cases
,
3790 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
3791 Bstatement
* switch_statement
;
3792 switch_statement
= context
->backend()->switch_statement(bfunction
,
3797 Bstatement
* ldef
= break_label
->get_definition(context
);
3798 return context
->backend()->compound_statement(switch_statement
, ldef
);
3801 // Dump the AST representation for a constant switch statement.
3804 Constant_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3807 ast_dump_context
->print_indent();
3808 ast_dump_context
->ostream() << "switch ";
3809 ast_dump_context
->dump_expression(this->val_
);
3811 if (ast_dump_context
->dump_subblocks())
3813 ast_dump_context
->ostream() << " {" << std::endl
;
3814 this->clauses_
->dump_clauses(ast_dump_context
);
3815 ast_dump_context
->ostream() << "}";
3818 ast_dump_context
->ostream() << std::endl
;
3821 // Class Switch_statement.
3826 Switch_statement::do_traverse(Traverse
* traverse
)
3828 if (this->val_
!= NULL
)
3830 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3831 return TRAVERSE_EXIT
;
3833 return this->clauses_
->traverse(traverse
);
3836 // Lower a Switch_statement to a Constant_switch_statement or a series
3837 // of if statements.
3840 Switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
3841 Statement_inserter
*)
3843 Location loc
= this->location();
3845 if (this->val_
!= NULL
3846 && (this->val_
->is_error_expression()
3847 || this->val_
->type()->is_error()))
3848 return Statement::make_error_statement(loc
);
3850 if (this->val_
!= NULL
3851 && this->val_
->type()->integer_type() != NULL
3852 && !this->clauses_
->empty()
3853 && this->clauses_
->is_constant())
3854 return new Constant_switch_statement(this->val_
, this->clauses_
,
3855 this->break_label_
, loc
);
3857 if (this->val_
!= NULL
3858 && !this->val_
->type()->is_comparable()
3859 && !Type::are_compatible_for_comparison(true, this->val_
->type(),
3860 Type::make_nil_type(), NULL
))
3862 error_at(this->val_
->location(),
3863 "cannot switch on value whose type that may not be compared");
3864 return Statement::make_error_statement(loc
);
3867 Block
* b
= new Block(enclosing
, loc
);
3869 if (this->clauses_
->empty())
3871 Expression
* val
= this->val_
;
3873 val
= Expression::make_boolean(true, loc
);
3874 return Statement::make_statement(val
, true);
3877 // var val_temp VAL_TYPE = VAL
3878 Expression
* val
= this->val_
;
3880 val
= Expression::make_boolean(true, loc
);
3881 Temporary_statement
* val_temp
= Statement::make_temporary(NULL
, val
, loc
);
3882 b
->add_statement(val_temp
);
3884 this->clauses_
->lower(b
, val_temp
, this->break_label());
3886 Statement
* s
= Statement::make_unnamed_label_statement(this->break_label_
);
3887 b
->add_statement(s
);
3889 return Statement::make_block_statement(b
, loc
);
3892 // Return the break label for this switch statement, creating it if
3896 Switch_statement::break_label()
3898 if (this->break_label_
== NULL
)
3899 this->break_label_
= new Unnamed_label(this->location());
3900 return this->break_label_
;
3903 // Dump the AST representation for a switch statement.
3906 Switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3908 ast_dump_context
->print_indent();
3909 ast_dump_context
->ostream() << "switch ";
3910 if (this->val_
!= NULL
)
3912 ast_dump_context
->dump_expression(this->val_
);
3914 if (ast_dump_context
->dump_subblocks())
3916 ast_dump_context
->ostream() << " {" << std::endl
;
3917 this->clauses_
->dump_clauses(ast_dump_context
);
3918 ast_dump_context
->print_indent();
3919 ast_dump_context
->ostream() << "}";
3921 ast_dump_context
->ostream() << std::endl
;
3924 // Return whether this switch may fall through.
3927 Switch_statement::do_may_fall_through() const
3929 if (this->clauses_
== NULL
)
3932 // If we have a break label, then some case needed it. That implies
3933 // that the switch statement as a whole can fall through.
3934 if (this->break_label_
!= NULL
)
3937 return this->clauses_
->may_fall_through();
3940 // Make a switch statement.
3943 Statement::make_switch_statement(Expression
* val
, Location location
)
3945 return new Switch_statement(val
, location
);
3948 // Class Type_case_clauses::Type_case_clause.
3953 Type_case_clauses::Type_case_clause::traverse(Traverse
* traverse
)
3955 if (!this->is_default_
3956 && ((traverse
->traverse_mask()
3957 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3958 && Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3959 return TRAVERSE_EXIT
;
3960 if (this->statements_
!= NULL
)
3961 return this->statements_
->traverse(traverse
);
3962 return TRAVERSE_CONTINUE
;
3965 // Lower one clause in a type switch. Add statements to the block B.
3966 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3967 // BREAK_LABEL is the label at the end of the type switch.
3968 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3972 Type_case_clauses::Type_case_clause::lower(Type
* switch_val_type
,
3974 Temporary_statement
* descriptor_temp
,
3975 Unnamed_label
* break_label
,
3976 Unnamed_label
** stmts_label
) const
3978 Location loc
= this->location_
;
3980 Unnamed_label
* next_case_label
= NULL
;
3981 if (!this->is_default_
)
3983 Type
* type
= this->type_
;
3986 if (switch_val_type
->interface_type() != NULL
3987 && !type
->is_nil_constant_as_type()
3988 && type
->interface_type() == NULL
3989 && !switch_val_type
->interface_type()->implements_interface(type
,
3993 error_at(this->location_
, "impossible type switch case");
3995 error_at(this->location_
, "impossible type switch case (%s)",
3999 Expression
* ref
= Expression::make_temporary_reference(descriptor_temp
,
4003 // The language permits case nil, which is of course a constant
4004 // rather than a type. It will appear here as an invalid
4006 if (type
->is_nil_constant_as_type())
4007 cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
4008 Expression::make_nil(loc
),
4011 cond
= Runtime::make_call((type
->interface_type() == NULL
4012 ? Runtime::IFACETYPEEQ
4013 : Runtime::IFACEI2TP
),
4015 Expression::make_type_descriptor(type
, loc
),
4018 Unnamed_label
* dest
;
4019 if (!this->is_fallthrough_
)
4021 // if !COND { goto NEXT_CASE_LABEL }
4022 next_case_label
= new Unnamed_label(Linemap::unknown_location());
4023 dest
= next_case_label
;
4024 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
4028 // if COND { goto STMTS_LABEL }
4029 go_assert(stmts_label
!= NULL
);
4030 if (*stmts_label
== NULL
)
4031 *stmts_label
= new Unnamed_label(Linemap::unknown_location());
4032 dest
= *stmts_label
;
4034 Block
* then_block
= new Block(b
, loc
);
4035 Statement
* s
= Statement::make_goto_unnamed_statement(dest
, loc
);
4036 then_block
->add_statement(s
);
4037 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
4038 b
->add_statement(s
);
4041 if (this->statements_
!= NULL
4042 || (!this->is_fallthrough_
4043 && stmts_label
!= NULL
4044 && *stmts_label
!= NULL
))
4046 go_assert(!this->is_fallthrough_
);
4047 if (stmts_label
!= NULL
&& *stmts_label
!= NULL
)
4049 go_assert(!this->is_default_
);
4050 if (this->statements_
!= NULL
)
4051 (*stmts_label
)->set_location(this->statements_
->start_location());
4052 Statement
* s
= Statement::make_unnamed_label_statement(*stmts_label
);
4053 b
->add_statement(s
);
4054 *stmts_label
= NULL
;
4056 if (this->statements_
!= NULL
)
4057 b
->add_statement(Statement::make_block_statement(this->statements_
,
4061 if (this->is_fallthrough_
)
4062 go_assert(next_case_label
== NULL
);
4065 Location gloc
= (this->statements_
== NULL
4067 : this->statements_
->end_location());
4068 b
->add_statement(Statement::make_goto_unnamed_statement(break_label
,
4070 if (next_case_label
!= NULL
)
4073 Statement::make_unnamed_label_statement(next_case_label
);
4074 b
->add_statement(s
);
4079 // Return true if this type clause may fall through to the statements
4080 // following the switch.
4083 Type_case_clauses::Type_case_clause::may_fall_through() const
4085 if (this->is_fallthrough_
)
4087 // This case means that we automatically fall through to the
4088 // next case (it's used for T1 in case T1, T2:). It does not
4089 // mean that we fall through to the end of the type switch as a
4090 // whole. There is sure to be a next case and that next case
4091 // will determine whether we fall through to the statements
4092 // after the type switch.
4095 if (this->statements_
== NULL
)
4097 return this->statements_
->may_fall_through();
4100 // Dump the AST representation for a type case clause
4103 Type_case_clauses::Type_case_clause::dump_clause(
4104 Ast_dump_context
* ast_dump_context
) const
4106 ast_dump_context
->print_indent();
4107 if (this->is_default_
)
4109 ast_dump_context
->ostream() << "default:";
4113 ast_dump_context
->ostream() << "case ";
4114 ast_dump_context
->dump_type(this->type_
);
4115 ast_dump_context
->ostream() << ":" ;
4117 ast_dump_context
->dump_block(this->statements_
);
4118 if (this->is_fallthrough_
)
4120 ast_dump_context
->print_indent();
4121 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
4125 // Class Type_case_clauses.
4130 Type_case_clauses::traverse(Traverse
* traverse
)
4132 for (Type_clauses::iterator p
= this->clauses_
.begin();
4133 p
!= this->clauses_
.end();
4136 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4137 return TRAVERSE_EXIT
;
4139 return TRAVERSE_CONTINUE
;
4142 // Check for duplicate types.
4145 Type_case_clauses::check_duplicates() const
4147 typedef Unordered_set_hash(const Type
*, Type_hash_identical
,
4148 Type_identical
) Types_seen
;
4149 Types_seen types_seen
;
4150 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4151 p
!= this->clauses_
.end();
4154 Type
* t
= p
->type();
4157 if (t
->is_nil_constant_as_type())
4158 t
= Type::make_nil_type();
4159 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
.insert(t
);
4161 error_at(p
->location(), "duplicate type in switch");
4165 // Lower the clauses in a type switch. Add statements to the block B.
4166 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4167 // BREAK_LABEL is the label at the end of the type switch.
4170 Type_case_clauses::lower(Type
* switch_val_type
, Block
* b
,
4171 Temporary_statement
* descriptor_temp
,
4172 Unnamed_label
* break_label
) const
4174 const Type_case_clause
* default_case
= NULL
;
4176 Unnamed_label
* stmts_label
= NULL
;
4177 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4178 p
!= this->clauses_
.end();
4181 if (!p
->is_default())
4182 p
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4186 // We are generating a series of tests, which means that we
4187 // need to move the default case to the end.
4191 go_assert(stmts_label
== NULL
);
4193 if (default_case
!= NULL
)
4194 default_case
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4198 // Return true if these clauses may fall through to the statements
4199 // following the switch statement.
4202 Type_case_clauses::may_fall_through() const
4204 bool found_default
= false;
4205 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4206 p
!= this->clauses_
.end();
4209 if (p
->may_fall_through())
4211 if (p
->is_default())
4212 found_default
= true;
4214 return !found_default
;
4217 // Dump the AST representation for case clauses (from a switch statement)
4220 Type_case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4222 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4223 p
!= this->clauses_
.end();
4225 p
->dump_clause(ast_dump_context
);
4228 // Class Type_switch_statement.
4233 Type_switch_statement::do_traverse(Traverse
* traverse
)
4235 if (this->var_
== NULL
)
4237 if (this->traverse_expression(traverse
, &this->expr_
) == TRAVERSE_EXIT
)
4238 return TRAVERSE_EXIT
;
4240 if (this->clauses_
!= NULL
)
4241 return this->clauses_
->traverse(traverse
);
4242 return TRAVERSE_CONTINUE
;
4245 // Lower a type switch statement to a series of if statements. The gc
4246 // compiler is able to generate a table in some cases. However, that
4247 // does not work for us because we may have type descriptors in
4248 // different shared libraries, so we can't compare them with simple
4249 // equality testing.
4252 Type_switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
4253 Statement_inserter
*)
4255 const Location loc
= this->location();
4257 if (this->clauses_
!= NULL
)
4258 this->clauses_
->check_duplicates();
4260 Block
* b
= new Block(enclosing
, loc
);
4262 Type
* val_type
= (this->var_
!= NULL
4263 ? this->var_
->var_value()->type()
4264 : this->expr_
->type());
4266 if (val_type
->interface_type() == NULL
)
4268 if (!val_type
->is_error())
4269 this->report_error(_("cannot type switch on non-interface value"));
4270 return Statement::make_error_statement(loc
);
4273 // var descriptor_temp DESCRIPTOR_TYPE
4274 Type
* descriptor_type
= Type::make_type_descriptor_ptr_type();
4275 Temporary_statement
* descriptor_temp
=
4276 Statement::make_temporary(descriptor_type
, NULL
, loc
);
4277 b
->add_statement(descriptor_temp
);
4279 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4281 bool is_empty
= val_type
->interface_type()->is_empty();
4283 if (this->var_
== NULL
)
4286 ref
= Expression::make_var_reference(this->var_
, loc
);
4287 Expression
* call
= Runtime::make_call((is_empty
4288 ? Runtime::EFACETYPE
4289 : Runtime::IFACETYPE
),
4291 Temporary_reference_expression
* lhs
=
4292 Expression::make_temporary_reference(descriptor_temp
, loc
);
4293 lhs
->set_is_lvalue();
4294 Statement
* s
= Statement::make_assignment(lhs
, call
, loc
);
4295 b
->add_statement(s
);
4297 if (this->clauses_
!= NULL
)
4298 this->clauses_
->lower(val_type
, b
, descriptor_temp
, this->break_label());
4300 s
= Statement::make_unnamed_label_statement(this->break_label_
);
4301 b
->add_statement(s
);
4303 return Statement::make_block_statement(b
, loc
);
4306 // Return whether this switch may fall through.
4309 Type_switch_statement::do_may_fall_through() const
4311 if (this->clauses_
== NULL
)
4314 // If we have a break label, then some case needed it. That implies
4315 // that the switch statement as a whole can fall through.
4316 if (this->break_label_
!= NULL
)
4319 return this->clauses_
->may_fall_through();
4322 // Return the break label for this type switch statement, creating it
4326 Type_switch_statement::break_label()
4328 if (this->break_label_
== NULL
)
4329 this->break_label_
= new Unnamed_label(this->location());
4330 return this->break_label_
;
4333 // Dump the AST representation for a type switch statement
4336 Type_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
4339 ast_dump_context
->print_indent();
4340 ast_dump_context
->ostream() << "switch " << this->var_
->name() << " = ";
4341 ast_dump_context
->dump_expression(this->expr_
);
4342 ast_dump_context
->ostream() << " .(type)";
4343 if (ast_dump_context
->dump_subblocks())
4345 ast_dump_context
->ostream() << " {" << std::endl
;
4346 this->clauses_
->dump_clauses(ast_dump_context
);
4347 ast_dump_context
->ostream() << "}";
4349 ast_dump_context
->ostream() << std::endl
;
4352 // Make a type switch statement.
4354 Type_switch_statement
*
4355 Statement::make_type_switch_statement(Named_object
* var
, Expression
* expr
,
4358 return new Type_switch_statement(var
, expr
, location
);
4361 // Class Send_statement.
4366 Send_statement::do_traverse(Traverse
* traverse
)
4368 if (this->traverse_expression(traverse
, &this->channel_
) == TRAVERSE_EXIT
)
4369 return TRAVERSE_EXIT
;
4370 return this->traverse_expression(traverse
, &this->val_
);
4376 Send_statement::do_determine_types()
4378 this->channel_
->determine_type_no_context();
4379 Type
* type
= this->channel_
->type();
4380 Type_context context
;
4381 if (type
->channel_type() != NULL
)
4382 context
.type
= type
->channel_type()->element_type();
4383 this->val_
->determine_type(&context
);
4389 Send_statement::do_check_types(Gogo
*)
4391 Type
* type
= this->channel_
->type();
4392 if (type
->is_error())
4394 this->set_is_error();
4397 Channel_type
* channel_type
= type
->channel_type();
4398 if (channel_type
== NULL
)
4400 error_at(this->location(), "left operand of %<<-%> must be channel");
4401 this->set_is_error();
4404 Type
* element_type
= channel_type
->element_type();
4405 if (!Type::are_assignable(element_type
, this->val_
->type(), NULL
))
4407 this->report_error(_("incompatible types in send"));
4410 if (!channel_type
->may_send())
4412 this->report_error(_("invalid send on receive-only channel"));
4417 // Convert a send statement to the backend representation.
4420 Send_statement::do_get_backend(Translate_context
* context
)
4422 Location loc
= this->location();
4424 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
4425 Type
* element_type
= channel_type
->element_type();
4426 Expression
* val
= Expression::make_cast(element_type
, this->val_
, loc
);
4429 bool can_take_address
;
4430 switch (element_type
->base()->classification())
4432 case Type::TYPE_BOOLEAN
:
4433 case Type::TYPE_INTEGER
:
4434 case Type::TYPE_FUNCTION
:
4435 case Type::TYPE_POINTER
:
4436 case Type::TYPE_MAP
:
4437 case Type::TYPE_CHANNEL
:
4439 can_take_address
= false;
4442 case Type::TYPE_FLOAT
:
4443 case Type::TYPE_COMPLEX
:
4444 case Type::TYPE_STRING
:
4445 case Type::TYPE_INTERFACE
:
4447 can_take_address
= false;
4450 case Type::TYPE_STRUCT
:
4452 can_take_address
= true;
4455 case Type::TYPE_ARRAY
:
4457 can_take_address
= !element_type
->is_slice_type();
4461 case Type::TYPE_ERROR
:
4462 case Type::TYPE_VOID
:
4463 case Type::TYPE_SINK
:
4464 case Type::TYPE_NIL
:
4465 case Type::TYPE_NAMED
:
4466 case Type::TYPE_FORWARD
:
4467 go_assert(saw_errors());
4468 return context
->backend()->error_statement();
4471 // Only try to take the address of a variable. We have already
4472 // moved variables to the heap, so this should not cause that to
4473 // happen unnecessarily.
4474 if (can_take_address
4475 && val
->var_expression() == NULL
4476 && val
->temporary_reference_expression() == NULL
)
4477 can_take_address
= false;
4479 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
4482 Runtime::Function code
;
4483 Bstatement
* btemp
= NULL
;
4486 // Type is small enough to handle as uint64.
4487 code
= Runtime::SEND_SMALL
;
4488 val
= Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
4491 else if (can_take_address
)
4493 // Must pass address of value. The function doesn't change the
4494 // value, so just take its address directly.
4495 code
= Runtime::SEND_BIG
;
4496 val
= Expression::make_unary(OPERATOR_AND
, val
, loc
);
4500 // Must pass address of value, but the value is small enough
4501 // that it might be in registers. Copy value into temporary
4502 // variable to take address.
4503 code
= Runtime::SEND_BIG
;
4504 Temporary_statement
* temp
= Statement::make_temporary(element_type
,
4506 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
4507 val
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
4508 btemp
= temp
->get_backend(context
);
4511 Expression
* call
= Runtime::make_call(code
, loc
, 3, td
, this->channel_
, val
);
4513 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4514 Bexpression
* bcall
= call
->get_backend(context
);
4515 Bstatement
* s
= context
->backend()->expression_statement(bcall
);
4520 return context
->backend()->compound_statement(btemp
, s
);
4523 // Dump the AST representation for a send statement
4526 Send_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4528 ast_dump_context
->print_indent();
4529 ast_dump_context
->dump_expression(this->channel_
);
4530 ast_dump_context
->ostream() << " <- ";
4531 ast_dump_context
->dump_expression(this->val_
);
4532 ast_dump_context
->ostream() << std::endl
;
4535 // Make a send statement.
4538 Statement::make_send_statement(Expression
* channel
, Expression
* val
,
4541 return new Send_statement(channel
, val
, location
);
4544 // Class Select_clauses::Select_clause.
4549 Select_clauses::Select_clause::traverse(Traverse
* traverse
)
4551 if (!this->is_lowered_
4552 && (traverse
->traverse_mask()
4553 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
4555 if (this->channel_
!= NULL
)
4557 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
4558 return TRAVERSE_EXIT
;
4560 if (this->val_
!= NULL
)
4562 if (Expression::traverse(&this->val_
, traverse
) == TRAVERSE_EXIT
)
4563 return TRAVERSE_EXIT
;
4565 if (this->closed_
!= NULL
)
4567 if (Expression::traverse(&this->closed_
, traverse
) == TRAVERSE_EXIT
)
4568 return TRAVERSE_EXIT
;
4571 if (this->statements_
!= NULL
)
4573 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
4574 return TRAVERSE_EXIT
;
4576 return TRAVERSE_CONTINUE
;
4579 // Lowering. We call a function to register this clause, and arrange
4580 // to set any variables in any receive clause.
4583 Select_clauses::Select_clause::lower(Gogo
* gogo
, Named_object
* function
,
4584 Block
* b
, Temporary_statement
* sel
)
4586 Location loc
= this->location_
;
4588 Expression
* selref
= Expression::make_temporary_reference(sel
, loc
);
4591 mpz_init_set_ui(ival
, this->index_
);
4592 Expression
* index_expr
= Expression::make_integer(&ival
, NULL
, loc
);
4595 if (this->is_default_
)
4597 go_assert(this->channel_
== NULL
&& this->val_
== NULL
);
4598 this->lower_default(b
, selref
, index_expr
);
4599 this->is_lowered_
= true;
4603 // Evaluate the channel before the select statement.
4604 Temporary_statement
* channel_temp
= Statement::make_temporary(NULL
,
4607 b
->add_statement(channel_temp
);
4608 Expression
* chanref
= Expression::make_temporary_reference(channel_temp
,
4612 this->lower_send(b
, selref
, chanref
, index_expr
);
4614 this->lower_recv(gogo
, function
, b
, selref
, chanref
, index_expr
);
4616 // Now all references should be handled through the statements, not
4618 this->is_lowered_
= true;
4623 // Lower a default clause in a select statement.
4626 Select_clauses::Select_clause::lower_default(Block
* b
, Expression
* selref
,
4627 Expression
* index_expr
)
4629 Location loc
= this->location_
;
4630 Expression
* call
= Runtime::make_call(Runtime::SELECTDEFAULT
, loc
, 2, selref
,
4632 b
->add_statement(Statement::make_statement(call
, true));
4635 // Lower a send clause in a select statement.
4638 Select_clauses::Select_clause::lower_send(Block
* b
, Expression
* selref
,
4639 Expression
* chanref
,
4640 Expression
* index_expr
)
4642 Location loc
= this->location_
;
4644 Channel_type
* ct
= this->channel_
->type()->channel_type();
4648 Type
* valtype
= ct
->element_type();
4650 // Note that copying the value to a temporary here means that we
4651 // evaluate the send values in the required order.
4652 Temporary_statement
* val
= Statement::make_temporary(valtype
, this->val_
,
4654 b
->add_statement(val
);
4656 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4657 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4659 Expression
* call
= Runtime::make_call(Runtime::SELECTSEND
, loc
, 4, selref
,
4660 chanref
, valaddr
, index_expr
);
4661 b
->add_statement(Statement::make_statement(call
, true));
4664 // Lower a receive clause in a select statement.
4667 Select_clauses::Select_clause::lower_recv(Gogo
* gogo
, Named_object
* function
,
4668 Block
* b
, Expression
* selref
,
4669 Expression
* chanref
,
4670 Expression
* index_expr
)
4672 Location loc
= this->location_
;
4674 Channel_type
* ct
= this->channel_
->type()->channel_type();
4678 Type
* valtype
= ct
->element_type();
4679 Temporary_statement
* val
= Statement::make_temporary(valtype
, NULL
, loc
);
4680 b
->add_statement(val
);
4682 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4683 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4685 Temporary_statement
* closed_temp
= NULL
;
4688 if (this->closed_
== NULL
&& this->closedvar_
== NULL
)
4689 call
= Runtime::make_call(Runtime::SELECTRECV
, loc
, 4, selref
, chanref
,
4690 valaddr
, index_expr
);
4693 closed_temp
= Statement::make_temporary(Type::lookup_bool_type(), NULL
,
4695 b
->add_statement(closed_temp
);
4696 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4698 Expression
* caddr
= Expression::make_unary(OPERATOR_AND
, cref
, loc
);
4699 call
= Runtime::make_call(Runtime::SELECTRECV2
, loc
, 5, selref
, chanref
,
4700 valaddr
, caddr
, index_expr
);
4703 b
->add_statement(Statement::make_statement(call
, true));
4705 // If the block of statements is executed, arrange for the received
4706 // value to move from VAL to the place where the statements expect
4711 if (this->var_
!= NULL
)
4713 go_assert(this->val_
== NULL
);
4714 valref
= Expression::make_temporary_reference(val
, loc
);
4715 this->var_
->var_value()->set_init(valref
);
4716 this->var_
->var_value()->clear_type_from_chan_element();
4718 else if (this->val_
!= NULL
&& !this->val_
->is_sink_expression())
4720 init
= new Block(b
, loc
);
4721 valref
= Expression::make_temporary_reference(val
, loc
);
4722 init
->add_statement(Statement::make_assignment(this->val_
, valref
, loc
));
4725 if (this->closedvar_
!= NULL
)
4727 go_assert(this->closed_
== NULL
);
4728 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4730 this->closedvar_
->var_value()->set_init(cref
);
4732 else if (this->closed_
!= NULL
&& !this->closed_
->is_sink_expression())
4735 init
= new Block(b
, loc
);
4736 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4738 init
->add_statement(Statement::make_assignment(this->closed_
, cref
,
4744 gogo
->lower_block(function
, init
);
4746 if (this->statements_
!= NULL
)
4747 init
->add_statement(Statement::make_block_statement(this->statements_
,
4749 this->statements_
= init
;
4756 Select_clauses::Select_clause::determine_types()
4758 go_assert(this->is_lowered_
);
4759 if (this->statements_
!= NULL
)
4760 this->statements_
->determine_types();
4766 Select_clauses::Select_clause::check_types()
4768 if (this->is_default_
)
4771 Channel_type
* ct
= this->channel_
->type()->channel_type();
4774 error_at(this->channel_
->location(), "expected channel");
4778 if (this->is_send_
&& !ct
->may_send())
4779 error_at(this->location(), "invalid send on receive-only channel");
4780 else if (!this->is_send_
&& !ct
->may_receive())
4781 error_at(this->location(), "invalid receive on send-only channel");
4784 // Whether this clause may fall through to the statement which follows
4785 // the overall select statement.
4788 Select_clauses::Select_clause::may_fall_through() const
4790 if (this->statements_
== NULL
)
4792 return this->statements_
->may_fall_through();
4795 // Return the backend representation for the statements to execute.
4798 Select_clauses::Select_clause::get_statements_backend(
4799 Translate_context
* context
)
4801 if (this->statements_
== NULL
)
4803 Bblock
* bblock
= this->statements_
->get_backend(context
);
4804 return context
->backend()->block_statement(bblock
);
4807 // Dump the AST representation for a select case clause
4810 Select_clauses::Select_clause::dump_clause(
4811 Ast_dump_context
* ast_dump_context
) const
4813 ast_dump_context
->print_indent();
4814 if (this->is_default_
)
4816 ast_dump_context
->ostream() << "default:";
4820 ast_dump_context
->ostream() << "case " ;
4823 ast_dump_context
->dump_expression(this->channel_
);
4824 ast_dump_context
->ostream() << " <- " ;
4825 if (this->val_
!= NULL
)
4826 ast_dump_context
->dump_expression(this->val_
);
4830 if (this->val_
!= NULL
)
4831 ast_dump_context
->dump_expression(this->val_
);
4832 if (this->closed_
!= NULL
)
4834 // FIXME: can val_ == NULL and closed_ ! = NULL?
4835 ast_dump_context
->ostream() << " , " ;
4836 ast_dump_context
->dump_expression(this->closed_
);
4838 if (this->closedvar_
!= NULL
|| this->var_
!= NULL
)
4839 ast_dump_context
->ostream() << " := " ;
4841 ast_dump_context
->ostream() << " <- " ;
4842 ast_dump_context
->dump_expression(this->channel_
);
4844 ast_dump_context
->ostream() << ":" ;
4846 ast_dump_context
->dump_block(this->statements_
);
4849 // Class Select_clauses.
4854 Select_clauses::traverse(Traverse
* traverse
)
4856 for (Clauses::iterator p
= this->clauses_
.begin();
4857 p
!= this->clauses_
.end();
4860 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4861 return TRAVERSE_EXIT
;
4863 return TRAVERSE_CONTINUE
;
4866 // Lowering. Here we pull out the channel and the send values, to
4867 // enforce the order of evaluation. We also add explicit send and
4868 // receive statements to the clauses.
4871 Select_clauses::lower(Gogo
* gogo
, Named_object
* function
, Block
* b
,
4872 Temporary_statement
* sel
)
4874 for (Clauses::iterator p
= this->clauses_
.begin();
4875 p
!= this->clauses_
.end();
4877 p
->lower(gogo
, function
, b
, sel
);
4883 Select_clauses::determine_types()
4885 for (Clauses::iterator p
= this->clauses_
.begin();
4886 p
!= this->clauses_
.end();
4888 p
->determine_types();
4894 Select_clauses::check_types()
4896 for (Clauses::iterator p
= this->clauses_
.begin();
4897 p
!= this->clauses_
.end();
4902 // Return whether these select clauses fall through to the statement
4903 // following the overall select statement.
4906 Select_clauses::may_fall_through() const
4908 for (Clauses::const_iterator p
= this->clauses_
.begin();
4909 p
!= this->clauses_
.end();
4911 if (p
->may_fall_through())
4916 // Convert to the backend representation. We have already accumulated
4917 // all the select information. Now we call selectgo, which will
4918 // return the index of the clause to execute.
4921 Select_clauses::get_backend(Translate_context
* context
,
4922 Temporary_statement
* sel
,
4923 Unnamed_label
*break_label
,
4926 size_t count
= this->clauses_
.size();
4927 std::vector
<std::vector
<Bexpression
*> > cases(count
);
4928 std::vector
<Bstatement
*> clauses(count
);
4930 Type
* int32_type
= Type::lookup_integer_type("int32");
4933 for (Clauses::iterator p
= this->clauses_
.begin();
4934 p
!= this->clauses_
.end();
4937 int index
= p
->index();
4939 mpz_init_set_ui(ival
, index
);
4940 Expression
* index_expr
= Expression::make_integer(&ival
, int32_type
,
4943 cases
[i
].push_back(index_expr
->get_backend(context
));
4945 Bstatement
* s
= p
->get_statements_backend(context
);
4946 Location gloc
= (p
->statements() == NULL
4948 : p
->statements()->end_location());
4949 Bstatement
* g
= break_label
->get_goto(context
, gloc
);
4954 clauses
[i
] = context
->backend()->compound_statement(s
, g
);
4957 Expression
* selref
= Expression::make_temporary_reference(sel
, location
);
4958 Expression
* call
= Runtime::make_call(Runtime::SELECTGO
, location
, 1,
4960 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4961 Bexpression
* bcall
= call
->get_backend(context
);
4964 return context
->backend()->expression_statement(bcall
);
4966 std::vector
<Bstatement
*> statements
;
4967 statements
.reserve(2);
4969 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
4970 Bstatement
* switch_stmt
= context
->backend()->switch_statement(bfunction
,
4975 statements
.push_back(switch_stmt
);
4977 Bstatement
* ldef
= break_label
->get_definition(context
);
4978 statements
.push_back(ldef
);
4980 return context
->backend()->statement_list(statements
);
4982 // Dump the AST representation for select clauses.
4985 Select_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4987 for (Clauses::const_iterator p
= this->clauses_
.begin();
4988 p
!= this->clauses_
.end();
4990 p
->dump_clause(ast_dump_context
);
4993 // Class Select_statement.
4995 // Return the break label for this switch statement, creating it if
4999 Select_statement::break_label()
5001 if (this->break_label_
== NULL
)
5002 this->break_label_
= new Unnamed_label(this->location());
5003 return this->break_label_
;
5006 // Lower a select statement. This will still return a select
5007 // statement, but it will be modified to implement the order of
5008 // evaluation rules, and to include the send and receive statements as
5009 // explicit statements in the clauses.
5012 Select_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
5013 Block
* enclosing
, Statement_inserter
*)
5015 if (this->is_lowered_
)
5018 Location loc
= this->location();
5020 Block
* b
= new Block(enclosing
, loc
);
5022 go_assert(this->sel_
== NULL
);
5025 mpz_init_set_ui(ival
, this->clauses_
->size());
5026 Expression
* size_expr
= Expression::make_integer(&ival
, NULL
, loc
);
5029 Expression
* call
= Runtime::make_call(Runtime::NEWSELECT
, loc
, 1, size_expr
);
5031 this->sel_
= Statement::make_temporary(NULL
, call
, loc
);
5032 b
->add_statement(this->sel_
);
5034 this->clauses_
->lower(gogo
, function
, b
, this->sel_
);
5035 this->is_lowered_
= true;
5036 b
->add_statement(this);
5038 return Statement::make_block_statement(b
, loc
);
5041 // Whether the select statement itself may fall through to the following
5045 Select_statement::do_may_fall_through() const
5047 // A select statement is terminating if no break statement
5048 // refers to it and all of its clauses are terminating.
5049 if (this->break_label_
!= NULL
)
5051 return this->clauses_
->may_fall_through();
5054 // Return the backend representation for a select statement.
5057 Select_statement::do_get_backend(Translate_context
* context
)
5059 return this->clauses_
->get_backend(context
, this->sel_
, this->break_label(),
5063 // Dump the AST representation for a select statement.
5066 Select_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5068 ast_dump_context
->print_indent();
5069 ast_dump_context
->ostream() << "select";
5070 if (ast_dump_context
->dump_subblocks())
5072 ast_dump_context
->ostream() << " {" << std::endl
;
5073 this->clauses_
->dump_clauses(ast_dump_context
);
5074 ast_dump_context
->ostream() << "}";
5076 ast_dump_context
->ostream() << std::endl
;
5079 // Make a select statement.
5082 Statement::make_select_statement(Location location
)
5084 return new Select_statement(location
);
5087 // Class For_statement.
5092 For_statement::do_traverse(Traverse
* traverse
)
5094 if (this->init_
!= NULL
)
5096 if (this->init_
->traverse(traverse
) == TRAVERSE_EXIT
)
5097 return TRAVERSE_EXIT
;
5099 if (this->cond_
!= NULL
)
5101 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
)
5102 return TRAVERSE_EXIT
;
5104 if (this->post_
!= NULL
)
5106 if (this->post_
->traverse(traverse
) == TRAVERSE_EXIT
)
5107 return TRAVERSE_EXIT
;
5109 return this->statements_
->traverse(traverse
);
5112 // Lower a For_statement into if statements and gotos. Getting rid of
5113 // complex statements make it easier to handle garbage collection.
5116 For_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
5117 Statement_inserter
*)
5120 Location loc
= this->location();
5122 Block
* b
= new Block(enclosing
, this->location());
5123 if (this->init_
!= NULL
)
5125 s
= Statement::make_block_statement(this->init_
,
5126 this->init_
->start_location());
5127 b
->add_statement(s
);
5130 Unnamed_label
* entry
= NULL
;
5131 if (this->cond_
!= NULL
)
5133 entry
= new Unnamed_label(this->location());
5134 b
->add_statement(Statement::make_goto_unnamed_statement(entry
, loc
));
5137 Unnamed_label
* top
= new Unnamed_label(this->location());
5138 b
->add_statement(Statement::make_unnamed_label_statement(top
));
5140 s
= Statement::make_block_statement(this->statements_
,
5141 this->statements_
->start_location());
5142 b
->add_statement(s
);
5144 Location end_loc
= this->statements_
->end_location();
5146 Unnamed_label
* cont
= this->continue_label_
;
5148 b
->add_statement(Statement::make_unnamed_label_statement(cont
));
5150 if (this->post_
!= NULL
)
5152 s
= Statement::make_block_statement(this->post_
,
5153 this->post_
->start_location());
5154 b
->add_statement(s
);
5155 end_loc
= this->post_
->end_location();
5158 if (this->cond_
== NULL
)
5159 b
->add_statement(Statement::make_goto_unnamed_statement(top
, end_loc
));
5162 b
->add_statement(Statement::make_unnamed_label_statement(entry
));
5164 Location cond_loc
= this->cond_
->location();
5165 Block
* then_block
= new Block(b
, cond_loc
);
5166 s
= Statement::make_goto_unnamed_statement(top
, cond_loc
);
5167 then_block
->add_statement(s
);
5169 s
= Statement::make_if_statement(this->cond_
, then_block
, NULL
, cond_loc
);
5170 b
->add_statement(s
);
5173 Unnamed_label
* brk
= this->break_label_
;
5175 b
->add_statement(Statement::make_unnamed_label_statement(brk
));
5177 b
->set_end_location(end_loc
);
5179 return Statement::make_block_statement(b
, loc
);
5182 // Return the break label, creating it if necessary.
5185 For_statement::break_label()
5187 if (this->break_label_
== NULL
)
5188 this->break_label_
= new Unnamed_label(this->location());
5189 return this->break_label_
;
5192 // Return the continue LABEL_EXPR.
5195 For_statement::continue_label()
5197 if (this->continue_label_
== NULL
)
5198 this->continue_label_
= new Unnamed_label(this->location());
5199 return this->continue_label_
;
5202 // Set the break and continue labels a for statement. This is used
5203 // when lowering a for range statement.
5206 For_statement::set_break_continue_labels(Unnamed_label
* break_label
,
5207 Unnamed_label
* continue_label
)
5209 go_assert(this->break_label_
== NULL
&& this->continue_label_
== NULL
);
5210 this->break_label_
= break_label
;
5211 this->continue_label_
= continue_label
;
5214 // Whether the overall statement may fall through.
5217 For_statement::do_may_fall_through() const
5219 // A for loop is terminating if it has no condition and
5220 // no break statement.
5221 if(this->cond_
!= NULL
)
5223 if(this->break_label_
!= NULL
)
5228 // Dump the AST representation for a for statement.
5231 For_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5233 if (this->init_
!= NULL
&& ast_dump_context
->dump_subblocks())
5235 ast_dump_context
->print_indent();
5236 ast_dump_context
->indent();
5237 ast_dump_context
->ostream() << "// INIT " << std::endl
;
5238 ast_dump_context
->dump_block(this->init_
);
5239 ast_dump_context
->unindent();
5241 ast_dump_context
->print_indent();
5242 ast_dump_context
->ostream() << "for ";
5243 if (this->cond_
!= NULL
)
5244 ast_dump_context
->dump_expression(this->cond_
);
5246 if (ast_dump_context
->dump_subblocks())
5248 ast_dump_context
->ostream() << " {" << std::endl
;
5249 ast_dump_context
->dump_block(this->statements_
);
5250 if (this->init_
!= NULL
)
5252 ast_dump_context
->print_indent();
5253 ast_dump_context
->ostream() << "// POST " << std::endl
;
5254 ast_dump_context
->dump_block(this->post_
);
5256 ast_dump_context
->unindent();
5258 ast_dump_context
->print_indent();
5259 ast_dump_context
->ostream() << "}";
5262 ast_dump_context
->ostream() << std::endl
;
5265 // Make a for statement.
5268 Statement::make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
5271 return new For_statement(init
, cond
, post
, location
);
5274 // Class For_range_statement.
5279 For_range_statement::do_traverse(Traverse
* traverse
)
5281 if (this->traverse_expression(traverse
, &this->index_var_
) == TRAVERSE_EXIT
)
5282 return TRAVERSE_EXIT
;
5283 if (this->value_var_
!= NULL
)
5285 if (this->traverse_expression(traverse
, &this->value_var_
)
5287 return TRAVERSE_EXIT
;
5289 if (this->traverse_expression(traverse
, &this->range_
) == TRAVERSE_EXIT
)
5290 return TRAVERSE_EXIT
;
5291 return this->statements_
->traverse(traverse
);
5294 // Lower a for range statement. For simplicity we lower this into a
5295 // for statement, which will then be lowered in turn to goto
5299 For_range_statement::do_lower(Gogo
* gogo
, Named_object
*, Block
* enclosing
,
5300 Statement_inserter
*)
5302 Type
* range_type
= this->range_
->type();
5303 if (range_type
->points_to() != NULL
5304 && range_type
->points_to()->array_type() != NULL
5305 && !range_type
->points_to()->is_slice_type())
5306 range_type
= range_type
->points_to();
5309 Type
* value_type
= NULL
;
5310 if (range_type
->array_type() != NULL
)
5312 index_type
= Type::lookup_integer_type("int");
5313 value_type
= range_type
->array_type()->element_type();
5315 else if (range_type
->is_string_type())
5317 index_type
= Type::lookup_integer_type("int");
5318 value_type
= Type::lookup_integer_type("int32");
5320 else if (range_type
->map_type() != NULL
)
5322 index_type
= range_type
->map_type()->key_type();
5323 value_type
= range_type
->map_type()->val_type();
5325 else if (range_type
->channel_type() != NULL
)
5327 index_type
= range_type
->channel_type()->element_type();
5328 if (this->value_var_
!= NULL
)
5330 if (!this->value_var_
->type()->is_error())
5331 this->report_error(_("too many variables for range clause "
5333 return Statement::make_error_statement(this->location());
5338 this->report_error(_("range clause must have "
5339 "array, slice, string, map, or channel type"));
5340 return Statement::make_error_statement(this->location());
5343 Location loc
= this->location();
5344 Block
* temp_block
= new Block(enclosing
, loc
);
5346 Named_object
* range_object
= NULL
;
5347 Temporary_statement
* range_temp
= NULL
;
5348 Var_expression
* ve
= this->range_
->var_expression();
5350 range_object
= ve
->named_object();
5353 range_temp
= Statement::make_temporary(NULL
, this->range_
, loc
);
5354 temp_block
->add_statement(range_temp
);
5355 this->range_
= NULL
;
5358 Temporary_statement
* index_temp
= Statement::make_temporary(index_type
,
5360 temp_block
->add_statement(index_temp
);
5362 Temporary_statement
* value_temp
= NULL
;
5363 if (this->value_var_
!= NULL
)
5365 value_temp
= Statement::make_temporary(value_type
, NULL
, loc
);
5366 temp_block
->add_statement(value_temp
);
5369 Block
* body
= new Block(temp_block
, loc
);
5376 // Arrange to do a loop appropriate for the type. We will produce
5377 // for INIT ; COND ; POST {
5379 // INDEX = INDEX_TEMP
5380 // VALUE = VALUE_TEMP // If there is a value
5381 // original statements
5384 if (range_type
->is_slice_type())
5385 this->lower_range_slice(gogo
, temp_block
, body
, range_object
, range_temp
,
5386 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5388 else if (range_type
->array_type() != NULL
)
5389 this->lower_range_array(gogo
, temp_block
, body
, range_object
, range_temp
,
5390 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5392 else if (range_type
->is_string_type())
5393 this->lower_range_string(gogo
, temp_block
, body
, range_object
, range_temp
,
5394 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5396 else if (range_type
->map_type() != NULL
)
5397 this->lower_range_map(gogo
, temp_block
, body
, range_object
, range_temp
,
5398 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5400 else if (range_type
->channel_type() != NULL
)
5401 this->lower_range_channel(gogo
, temp_block
, body
, range_object
, range_temp
,
5402 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5407 if (iter_init
!= NULL
)
5408 body
->add_statement(Statement::make_block_statement(iter_init
, loc
));
5411 Expression
* index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5412 if (this->value_var_
== NULL
)
5414 assign
= Statement::make_assignment(this->index_var_
, index_ref
, loc
);
5418 Expression_list
* lhs
= new Expression_list();
5419 lhs
->push_back(this->index_var_
);
5420 lhs
->push_back(this->value_var_
);
5422 Expression_list
* rhs
= new Expression_list();
5423 rhs
->push_back(index_ref
);
5424 rhs
->push_back(Expression::make_temporary_reference(value_temp
, loc
));
5426 assign
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5428 body
->add_statement(assign
);
5430 body
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
5432 body
->set_end_location(this->statements_
->end_location());
5434 For_statement
* loop
= Statement::make_for_statement(init
, cond
, post
,
5436 loop
->add_statements(body
);
5437 loop
->set_break_continue_labels(this->break_label_
, this->continue_label_
);
5439 temp_block
->add_statement(loop
);
5441 return Statement::make_block_statement(temp_block
, loc
);
5444 // Return a reference to the range, which may be in RANGE_OBJECT or in
5448 For_range_statement::make_range_ref(Named_object
* range_object
,
5449 Temporary_statement
* range_temp
,
5452 if (range_object
!= NULL
)
5453 return Expression::make_var_reference(range_object
, loc
);
5455 return Expression::make_temporary_reference(range_temp
, loc
);
5458 // Return a call to the predeclared function FUNCNAME passing a
5459 // reference to the temporary variable ARG.
5462 For_range_statement::call_builtin(Gogo
* gogo
, const char* funcname
,
5466 Named_object
* no
= gogo
->lookup_global(funcname
);
5467 go_assert(no
!= NULL
&& no
->is_function_declaration());
5468 Expression
* func
= Expression::make_func_reference(no
, NULL
, loc
);
5469 Expression_list
* params
= new Expression_list();
5470 params
->push_back(arg
);
5471 return Expression::make_call(func
, params
, false, loc
);
5474 // Lower a for range over an array.
5477 For_range_statement::lower_range_array(Gogo
* gogo
,
5480 Named_object
* range_object
,
5481 Temporary_statement
* range_temp
,
5482 Temporary_statement
* index_temp
,
5483 Temporary_statement
* value_temp
,
5489 Location loc
= this->location();
5491 // The loop we generate:
5492 // len_temp := len(range)
5493 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5494 // value_temp = range[index_temp]
5495 // index = index_temp
5496 // value = value_temp
5502 // len_temp = len(range)
5505 Block
* init
= new Block(enclosing
, loc
);
5507 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5508 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5509 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5511 init
->add_statement(len_temp
);
5514 mpz_init_set_ui(zval
, 0UL);
5515 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5518 Temporary_reference_expression
* tref
=
5519 Expression::make_temporary_reference(index_temp
, loc
);
5520 tref
->set_is_lvalue();
5521 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5522 init
->add_statement(s
);
5527 // index_temp < len_temp
5529 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5530 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5531 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5535 // Set *PITER_INIT to
5536 // value_temp = range[index_temp]
5538 Block
* iter_init
= NULL
;
5539 if (value_temp
!= NULL
)
5541 iter_init
= new Block(body_block
, loc
);
5543 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5544 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5545 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, NULL
, loc
);
5547 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5548 tref
->set_is_lvalue();
5549 s
= Statement::make_assignment(tref
, index
, loc
);
5551 iter_init
->add_statement(s
);
5553 *piter_init
= iter_init
;
5558 Block
* post
= new Block(enclosing
, loc
);
5559 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5560 tref
->set_is_lvalue();
5561 s
= Statement::make_inc_statement(tref
);
5562 post
->add_statement(s
);
5566 // Lower a for range over a slice.
5569 For_range_statement::lower_range_slice(Gogo
* gogo
,
5572 Named_object
* range_object
,
5573 Temporary_statement
* range_temp
,
5574 Temporary_statement
* index_temp
,
5575 Temporary_statement
* value_temp
,
5581 Location loc
= this->location();
5583 // The loop we generate:
5584 // for_temp := range
5585 // len_temp := len(for_temp)
5586 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5587 // value_temp = for_temp[index_temp]
5588 // index = index_temp
5589 // value = value_temp
5593 // Using for_temp means that we don't need to check bounds when
5594 // fetching range_temp[index_temp].
5597 // range_temp := range
5599 // len_temp = len(range_temp)
5602 Block
* init
= new Block(enclosing
, loc
);
5604 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5605 Temporary_statement
* for_temp
= Statement::make_temporary(NULL
, ref
, loc
);
5606 init
->add_statement(for_temp
);
5608 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5609 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5610 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5612 init
->add_statement(len_temp
);
5615 mpz_init_set_ui(zval
, 0UL);
5616 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5619 Temporary_reference_expression
* tref
=
5620 Expression::make_temporary_reference(index_temp
, loc
);
5621 tref
->set_is_lvalue();
5622 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5623 init
->add_statement(s
);
5628 // index_temp < len_temp
5630 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5631 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5632 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5636 // Set *PITER_INIT to
5637 // value_temp = range[index_temp]
5639 Block
* iter_init
= NULL
;
5640 if (value_temp
!= NULL
)
5642 iter_init
= new Block(body_block
, loc
);
5644 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5645 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5646 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, NULL
, loc
);
5648 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5649 tref
->set_is_lvalue();
5650 s
= Statement::make_assignment(tref
, index
, loc
);
5652 iter_init
->add_statement(s
);
5654 *piter_init
= iter_init
;
5659 Block
* post
= new Block(enclosing
, loc
);
5660 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5661 tref
->set_is_lvalue();
5662 s
= Statement::make_inc_statement(tref
);
5663 post
->add_statement(s
);
5667 // Lower a for range over a string.
5670 For_range_statement::lower_range_string(Gogo
*,
5673 Named_object
* range_object
,
5674 Temporary_statement
* range_temp
,
5675 Temporary_statement
* index_temp
,
5676 Temporary_statement
* value_temp
,
5682 Location loc
= this->location();
5684 // The loop we generate:
5685 // var next_index_temp int
5686 // for index_temp = 0; ; index_temp = next_index_temp {
5687 // next_index_temp, value_temp = stringiter2(range, index_temp)
5688 // if next_index_temp == 0 {
5691 // index = index_temp
5692 // value = value_temp
5697 // var next_index_temp int
5700 Block
* init
= new Block(enclosing
, loc
);
5702 Temporary_statement
* next_index_temp
=
5703 Statement::make_temporary(index_temp
->type(), NULL
, loc
);
5704 init
->add_statement(next_index_temp
);
5707 mpz_init_set_ui(zval
, 0UL);
5708 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5710 Temporary_reference_expression
* ref
=
5711 Expression::make_temporary_reference(index_temp
, loc
);
5712 ref
->set_is_lvalue();
5713 Statement
* s
= Statement::make_assignment(ref
, zexpr
, loc
);
5715 init
->add_statement(s
);
5718 // The loop has no condition.
5722 // Set *PITER_INIT to
5723 // next_index_temp = runtime.stringiter(range, index_temp)
5725 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
5727 // if next_index_temp == 0 {
5731 Block
* iter_init
= new Block(body_block
, loc
);
5733 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5734 Expression
* p2
= Expression::make_temporary_reference(index_temp
, loc
);
5735 Call_expression
* call
= Runtime::make_call((value_temp
== NULL
5736 ? Runtime::STRINGITER
5737 : Runtime::STRINGITER2
),
5740 if (value_temp
== NULL
)
5742 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5743 ref
->set_is_lvalue();
5744 s
= Statement::make_assignment(ref
, call
, loc
);
5748 Expression_list
* lhs
= new Expression_list();
5750 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5751 ref
->set_is_lvalue();
5752 lhs
->push_back(ref
);
5754 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5755 ref
->set_is_lvalue();
5756 lhs
->push_back(ref
);
5758 Expression_list
* rhs
= new Expression_list();
5759 rhs
->push_back(Expression::make_call_result(call
, 0));
5760 rhs
->push_back(Expression::make_call_result(call
, 1));
5762 s
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5764 iter_init
->add_statement(s
);
5766 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5767 zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5769 Expression
* equals
= Expression::make_binary(OPERATOR_EQEQ
, ref
, zexpr
, loc
);
5771 Block
* then_block
= new Block(iter_init
, loc
);
5772 s
= Statement::make_break_statement(this->break_label(), loc
);
5773 then_block
->add_statement(s
);
5775 s
= Statement::make_if_statement(equals
, then_block
, NULL
, loc
);
5776 iter_init
->add_statement(s
);
5778 *piter_init
= iter_init
;
5781 // index_temp = next_index_temp
5783 Block
* post
= new Block(enclosing
, loc
);
5785 Temporary_reference_expression
* lhs
=
5786 Expression::make_temporary_reference(index_temp
, loc
);
5787 lhs
->set_is_lvalue();
5788 Expression
* rhs
= Expression::make_temporary_reference(next_index_temp
, loc
);
5789 s
= Statement::make_assignment(lhs
, rhs
, loc
);
5791 post
->add_statement(s
);
5795 // Lower a for range over a map.
5798 For_range_statement::lower_range_map(Gogo
*,
5801 Named_object
* range_object
,
5802 Temporary_statement
* range_temp
,
5803 Temporary_statement
* index_temp
,
5804 Temporary_statement
* value_temp
,
5810 Location loc
= this->location();
5812 // The runtime uses a struct to handle ranges over a map. The
5813 // struct is four pointers long. The first pointer is NULL when we
5814 // have completed the iteration.
5816 // The loop we generate:
5817 // var hiter map_iteration_struct
5818 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
5819 // mapiter2(hiter, &index_temp, &value_temp)
5820 // index = index_temp
5821 // value = value_temp
5826 // var hiter map_iteration_struct
5827 // runtime.mapiterinit(range, &hiter)
5829 Block
* init
= new Block(enclosing
, loc
);
5831 Type
* map_iteration_type
= Runtime::map_iteration_type();
5832 Temporary_statement
* hiter
= Statement::make_temporary(map_iteration_type
,
5834 init
->add_statement(hiter
);
5836 Expression
* p1
= this->make_range_ref(range_object
, range_temp
, loc
);
5837 Expression
* ref
= Expression::make_temporary_reference(hiter
, loc
);
5838 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5839 Expression
* call
= Runtime::make_call(Runtime::MAPITERINIT
, loc
, 2, p1
, p2
);
5840 init
->add_statement(Statement::make_statement(call
, true));
5847 ref
= Expression::make_temporary_reference(hiter
, loc
);
5850 mpz_init_set_ui(zval
, 0UL);
5851 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, loc
);
5854 Expression
* index
= Expression::make_index(ref
, zexpr
, NULL
, NULL
, loc
);
5856 Expression
* ne
= Expression::make_binary(OPERATOR_NOTEQ
, index
,
5857 Expression::make_nil(loc
),
5862 // Set *PITER_INIT to
5863 // mapiter1(hiter, &index_temp)
5865 // mapiter2(hiter, &index_temp, &value_temp)
5867 Block
* iter_init
= new Block(body_block
, loc
);
5869 ref
= Expression::make_temporary_reference(hiter
, loc
);
5870 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5871 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5872 p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5873 if (value_temp
== NULL
)
5874 call
= Runtime::make_call(Runtime::MAPITER1
, loc
, 2, p1
, p2
);
5877 ref
= Expression::make_temporary_reference(value_temp
, loc
);
5878 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5879 call
= Runtime::make_call(Runtime::MAPITER2
, loc
, 3, p1
, p2
, p3
);
5881 iter_init
->add_statement(Statement::make_statement(call
, true));
5883 *piter_init
= iter_init
;
5886 // mapiternext(&hiter)
5888 Block
* post
= new Block(enclosing
, loc
);
5890 ref
= Expression::make_temporary_reference(hiter
, loc
);
5891 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5892 call
= Runtime::make_call(Runtime::MAPITERNEXT
, loc
, 1, p1
);
5893 post
->add_statement(Statement::make_statement(call
, true));
5898 // Lower a for range over a channel.
5901 For_range_statement::lower_range_channel(Gogo
*,
5904 Named_object
* range_object
,
5905 Temporary_statement
* range_temp
,
5906 Temporary_statement
* index_temp
,
5907 Temporary_statement
* value_temp
,
5913 go_assert(value_temp
== NULL
);
5915 Location loc
= this->location();
5917 // The loop we generate:
5919 // index_temp, ok_temp = <-range
5923 // index = index_temp
5927 // We have no initialization code, no condition, and no post code.
5933 // Set *PITER_INIT to
5934 // index_temp, ok_temp = <-range
5939 Block
* iter_init
= new Block(body_block
, loc
);
5941 Temporary_statement
* ok_temp
=
5942 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
5943 iter_init
->add_statement(ok_temp
);
5945 Expression
* cref
= this->make_range_ref(range_object
, range_temp
, loc
);
5946 Temporary_reference_expression
* iref
=
5947 Expression::make_temporary_reference(index_temp
, loc
);
5948 iref
->set_is_lvalue();
5949 Temporary_reference_expression
* oref
=
5950 Expression::make_temporary_reference(ok_temp
, loc
);
5951 oref
->set_is_lvalue();
5952 Statement
* s
= Statement::make_tuple_receive_assignment(iref
, oref
, cref
,
5954 iter_init
->add_statement(s
);
5956 Block
* then_block
= new Block(iter_init
, loc
);
5957 s
= Statement::make_break_statement(this->break_label(), loc
);
5958 then_block
->add_statement(s
);
5960 oref
= Expression::make_temporary_reference(ok_temp
, loc
);
5961 Expression
* cond
= Expression::make_unary(OPERATOR_NOT
, oref
, loc
);
5962 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
5963 iter_init
->add_statement(s
);
5965 *piter_init
= iter_init
;
5968 // Return the break LABEL_EXPR.
5971 For_range_statement::break_label()
5973 if (this->break_label_
== NULL
)
5974 this->break_label_
= new Unnamed_label(this->location());
5975 return this->break_label_
;
5978 // Return the continue LABEL_EXPR.
5981 For_range_statement::continue_label()
5983 if (this->continue_label_
== NULL
)
5984 this->continue_label_
= new Unnamed_label(this->location());
5985 return this->continue_label_
;
5988 // Dump the AST representation for a for range statement.
5991 For_range_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5994 ast_dump_context
->print_indent();
5995 ast_dump_context
->ostream() << "for ";
5996 ast_dump_context
->dump_expression(this->index_var_
);
5997 if (this->value_var_
!= NULL
)
5999 ast_dump_context
->ostream() << ", ";
6000 ast_dump_context
->dump_expression(this->value_var_
);
6003 ast_dump_context
->ostream() << " = range ";
6004 ast_dump_context
->dump_expression(this->range_
);
6005 if (ast_dump_context
->dump_subblocks())
6007 ast_dump_context
->ostream() << " {" << std::endl
;
6009 ast_dump_context
->indent();
6011 ast_dump_context
->dump_block(this->statements_
);
6013 ast_dump_context
->unindent();
6014 ast_dump_context
->print_indent();
6015 ast_dump_context
->ostream() << "}";
6017 ast_dump_context
->ostream() << std::endl
;
6020 // Make a for statement with a range clause.
6022 For_range_statement
*
6023 Statement::make_for_range_statement(Expression
* index_var
,
6024 Expression
* value_var
,
6028 return new For_range_statement(index_var
, value_var
, range
, location
);