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.
10 #include "go-diagnostics.h"
12 #include "expressions.h"
16 #include "statements.h"
21 Statement::Statement(Statement_classification classification
,
23 : classification_(classification
), location_(location
)
27 Statement::~Statement()
31 // Traverse the tree. The work of walking the components is handled
35 Statement::traverse(Block
* block
, size_t* pindex
, Traverse
* traverse
)
37 if (this->classification_
== STATEMENT_ERROR
)
38 return TRAVERSE_CONTINUE
;
40 unsigned int traverse_mask
= traverse
->traverse_mask();
42 if ((traverse_mask
& Traverse::traverse_statements
) != 0)
44 int t
= traverse
->statement(block
, pindex
, this);
45 if (t
== TRAVERSE_EXIT
)
47 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
48 return TRAVERSE_CONTINUE
;
51 // No point in checking traverse_mask here--a statement may contain
52 // other blocks or statements, and if we got here we always want to
54 return this->do_traverse(traverse
);
57 // Traverse the contents of a statement.
60 Statement::traverse_contents(Traverse
* traverse
)
62 return this->do_traverse(traverse
);
65 // Traverse assignments.
68 Statement::traverse_assignments(Traverse_assignments
* tassign
)
70 if (this->classification_
== STATEMENT_ERROR
)
72 return this->do_traverse_assignments(tassign
);
75 // Traverse an expression in a statement. This is a helper function
79 Statement::traverse_expression(Traverse
* traverse
, Expression
** expr
)
81 if ((traverse
->traverse_mask()
82 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
83 return TRAVERSE_CONTINUE
;
84 return Expression::traverse(expr
, traverse
);
87 // Traverse an expression list in a statement. This is a helper
88 // function for child classes.
91 Statement::traverse_expression_list(Traverse
* traverse
,
92 Expression_list
* expr_list
)
94 if (expr_list
== NULL
)
95 return TRAVERSE_CONTINUE
;
96 if ((traverse
->traverse_mask()
97 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
98 return TRAVERSE_CONTINUE
;
99 return expr_list
->traverse(traverse
);
102 // Traverse a type in a statement. This is a helper function for
106 Statement::traverse_type(Traverse
* traverse
, Type
* type
)
108 if ((traverse
->traverse_mask()
109 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) == 0)
110 return TRAVERSE_CONTINUE
;
111 return Type::traverse(type
, traverse
);
114 // Set type information for unnamed constants. This is really done by
118 Statement::determine_types()
120 this->do_determine_types();
123 // If this is a thunk statement, return it.
126 Statement::thunk_statement()
128 Thunk_statement
* ret
= this->convert
<Thunk_statement
, STATEMENT_GO
>();
130 ret
= this->convert
<Thunk_statement
, STATEMENT_DEFER
>();
134 // Convert a Statement to the backend representation. This is really
135 // done by the child class.
138 Statement::get_backend(Translate_context
* context
)
140 if (this->classification_
== STATEMENT_ERROR
)
141 return context
->backend()->error_statement();
142 return this->do_get_backend(context
);
145 // Dump AST representation for a statement to a dump context.
148 Statement::dump_statement(Ast_dump_context
* ast_dump_context
) const
150 this->do_dump_statement(ast_dump_context
);
153 // Note that this statement is erroneous. This is called by children
154 // when they discover an error.
157 Statement::set_is_error()
159 this->classification_
= STATEMENT_ERROR
;
162 // For children to call to report an error conveniently.
165 Statement::report_error(const char* msg
)
167 go_error_at(this->location_
, "%s", msg
);
168 this->set_is_error();
171 // An error statement, used to avoid crashing after we report an
174 class Error_statement
: public Statement
177 Error_statement(Location location
)
178 : Statement(STATEMENT_ERROR
, location
)
183 do_traverse(Traverse
*)
184 { return TRAVERSE_CONTINUE
; }
187 do_get_backend(Translate_context
*)
188 { go_unreachable(); }
191 do_dump_statement(Ast_dump_context
*) const;
195 // Helper to tack on available source position information
196 // at the end of a statement.
199 dsuffix(Location location
)
201 std::string lstr
= Linemap::location_to_string(location
);
204 std::string
rval(" // ");
209 // Dump the AST representation for an error statement.
212 Error_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
214 ast_dump_context
->print_indent();
215 ast_dump_context
->ostream() << "Error statement" << std::endl
;
218 // Make an error statement.
221 Statement::make_error_statement(Location location
)
223 return new Error_statement(location
);
226 // Class Variable_declaration_statement.
228 Variable_declaration_statement::Variable_declaration_statement(
230 : Statement(STATEMENT_VARIABLE_DECLARATION
, var
->var_value()->location()),
235 // We don't actually traverse the variable here; it was traversed
236 // while traversing the Block.
239 Variable_declaration_statement::do_traverse(Traverse
*)
241 return TRAVERSE_CONTINUE
;
244 // Traverse the assignments in a variable declaration. Note that this
245 // traversal is different from the usual traversal.
248 Variable_declaration_statement::do_traverse_assignments(
249 Traverse_assignments
* tassign
)
251 tassign
->initialize_variable(this->var_
);
255 // Lower the variable's initialization expression.
258 Variable_declaration_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
259 Block
*, Statement_inserter
* inserter
)
261 this->var_
->var_value()->lower_init_expression(gogo
, function
, inserter
);
265 // Flatten the variable's initialization expression.
268 Variable_declaration_statement::do_flatten(Gogo
* gogo
, Named_object
* function
,
269 Block
*, Statement_inserter
* inserter
)
271 Variable
* var
= this->var_
->var_value();
272 if (var
->type()->is_error_type()
273 || (var
->init() != NULL
274 && var
->init()->is_error_expression()))
276 go_assert(saw_errors());
277 return Statement::make_error_statement(this->location());
279 this->var_
->var_value()->flatten_init_expression(gogo
, function
, inserter
);
283 // Convert a variable declaration to the backend representation.
286 Variable_declaration_statement::do_get_backend(Translate_context
* context
)
288 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
289 Variable
* var
= this->var_
->var_value();
290 Bvariable
* bvar
= this->var_
->get_backend_variable(context
->gogo(),
291 context
->function());
292 Bexpression
* binit
= var
->get_init(context
->gogo(), context
->function());
294 if (!var
->is_in_heap())
296 go_assert(binit
!= NULL
);
297 return context
->backend()->init_statement(bfunction
, bvar
, binit
);
300 // Something takes the address of this variable, so the value is
301 // stored in the heap. Initialize it to newly allocated memory
302 // space, and assign the initial value to the new space.
303 Location loc
= this->location();
304 Named_object
* newfn
= context
->gogo()->lookup_global("new");
305 go_assert(newfn
!= NULL
&& newfn
->is_function_declaration());
306 Expression
* func
= Expression::make_func_reference(newfn
, NULL
, loc
);
307 Expression_list
* params
= new Expression_list();
308 params
->push_back(Expression::make_type(var
->type(), loc
));
309 Expression
* call
= Expression::make_call(func
, params
, false, loc
);
310 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
311 Temporary_statement
* temp
= Statement::make_temporary(NULL
, call
, loc
);
312 Bstatement
* btemp
= temp
->get_backend(context
);
314 Bstatement
* set
= NULL
;
317 Expression
* e
= Expression::make_temporary_reference(temp
, loc
);
318 e
= Expression::make_unary(OPERATOR_MULT
, e
, loc
);
319 Bexpression
* be
= e
->get_backend(context
);
320 set
= context
->backend()->assignment_statement(bfunction
, be
, binit
, loc
);
323 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
324 Bexpression
* bref
= ref
->get_backend(context
);
325 Bstatement
* sinit
= context
->backend()->init_statement(bfunction
, bvar
, bref
);
327 std::vector
<Bstatement
*> stats
;
329 stats
.push_back(btemp
);
331 stats
.push_back(set
);
332 stats
.push_back(sinit
);
333 return context
->backend()->statement_list(stats
);
336 // Dump the AST representation for a variable declaration.
339 Variable_declaration_statement::do_dump_statement(
340 Ast_dump_context
* ast_dump_context
) const
342 ast_dump_context
->print_indent();
344 go_assert(var_
->is_variable());
345 ast_dump_context
->ostream() << "var " << this->var_
->name() << " ";
346 Variable
* var
= this->var_
->var_value();
349 ast_dump_context
->dump_type(var
->type());
350 ast_dump_context
->ostream() << " ";
352 if (var
->init() != NULL
)
354 ast_dump_context
->ostream() << "= ";
355 ast_dump_context
->dump_expression(var
->init());
357 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
360 // Make a variable declaration.
363 Statement::make_variable_declaration(Named_object
* var
)
365 return new Variable_declaration_statement(var
);
368 // Class Temporary_statement.
370 // Return the type of the temporary variable.
373 Temporary_statement::type() const
375 Type
* type
= this->type_
!= NULL
? this->type_
: this->init_
->type();
377 // Temporary variables cannot have a void type.
378 if (type
->is_void_type())
380 go_assert(saw_errors());
381 return Type::make_error_type();
389 Temporary_statement::do_traverse(Traverse
* traverse
)
391 if (this->type_
!= NULL
392 && this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
393 return TRAVERSE_EXIT
;
394 if (this->init_
== NULL
)
395 return TRAVERSE_CONTINUE
;
397 return this->traverse_expression(traverse
, &this->init_
);
400 // Traverse assignments.
403 Temporary_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
405 if (this->init_
== NULL
)
407 tassign
->value(&this->init_
, true, true);
414 Temporary_statement::do_determine_types()
416 if (this->type_
!= NULL
&& this->type_
->is_abstract())
417 this->type_
= this->type_
->make_non_abstract_type();
419 if (this->init_
!= NULL
)
421 if (this->type_
== NULL
)
422 this->init_
->determine_type_no_context();
425 Type_context
context(this->type_
, false);
426 this->init_
->determine_type(&context
);
430 if (this->type_
== NULL
)
432 this->type_
= this->init_
->type();
433 go_assert(!this->type_
->is_abstract());
440 Temporary_statement::do_check_types(Gogo
*)
442 if (this->type_
!= NULL
&& this->init_
!= NULL
)
445 if (!Type::are_assignable(this->type_
, this->init_
->type(), &reason
))
448 go_error_at(this->location(), "incompatible types in assignment");
450 go_error_at(this->location(), "incompatible types in assignment (%s)",
452 this->set_is_error();
457 // Flatten a temporary statement: add another temporary when it might
458 // be needed for interface conversion.
461 Temporary_statement::do_flatten(Gogo
*, Named_object
*, Block
*,
462 Statement_inserter
* inserter
)
464 if (this->type()->is_error_type()
465 || (this->init_
!= NULL
466 && this->init_
->is_error_expression()))
468 go_assert(saw_errors());
469 return Statement::make_error_statement(this->location());
472 if (this->type_
!= NULL
473 && this->init_
!= NULL
474 && !Type::are_identical(this->type_
, this->init_
->type(), false, NULL
)
475 && this->init_
->type()->interface_type() != NULL
476 && !this->init_
->is_variable())
478 Temporary_statement
*temp
=
479 Statement::make_temporary(NULL
, this->init_
, this->location());
480 inserter
->insert(temp
);
481 this->init_
= Expression::make_temporary_reference(temp
,
487 // Convert to backend representation.
490 Temporary_statement::do_get_backend(Translate_context
* context
)
492 go_assert(this->bvariable_
== NULL
);
494 Named_object
* function
= context
->function();
495 go_assert(function
!= NULL
);
496 Bfunction
* bfunction
= function
->func_value()->get_decl();
497 Btype
* btype
= this->type()->get_backend(context
->gogo());
500 if (this->init_
== NULL
)
502 else if (this->type_
== NULL
)
503 binit
= this->init_
->get_backend(context
);
506 Expression
* init
= Expression::convert_for_assignment(context
->gogo(),
510 binit
= init
->get_backend(context
);
514 binit
= context
->backend()->convert_expression(btype
, binit
,
517 Bstatement
* statement
;
519 context
->backend()->temporary_variable(bfunction
, context
->bblock(),
521 this->is_address_taken_
,
522 this->location(), &statement
);
526 // Return the backend variable.
529 Temporary_statement::get_backend_variable(Translate_context
* context
) const
531 if (this->bvariable_
== NULL
)
533 go_assert(saw_errors());
534 return context
->backend()->error_variable();
536 return this->bvariable_
;
539 // Dump the AST represemtation for a temporary statement
542 Temporary_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
544 ast_dump_context
->print_indent();
545 ast_dump_context
->dump_temp_variable_name(this);
546 if (this->type_
!= NULL
)
548 ast_dump_context
->ostream() << " ";
549 ast_dump_context
->dump_type(this->type_
);
551 if (this->init_
!= NULL
)
553 ast_dump_context
->ostream() << " = ";
554 ast_dump_context
->dump_expression(this->init_
);
556 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
559 // Make and initialize a temporary variable in BLOCK.
562 Statement::make_temporary(Type
* type
, Expression
* init
,
565 return new Temporary_statement(type
, init
, location
);
568 // The Move_subexpressions class is used to move all top-level
569 // subexpressions of an expression. This is used for things like
570 // index expressions in which we must evaluate the index value before
571 // it can be changed by a multiple assignment.
573 class Move_subexpressions
: public Traverse
576 Move_subexpressions(int skip
, Block
* block
)
577 : Traverse(traverse_expressions
),
578 skip_(skip
), block_(block
)
583 expression(Expression
**);
586 // The number of subexpressions to skip moving. This is used to
587 // avoid moving the array itself, as we only need to move the index.
589 // The block where new temporary variables should be added.
594 Move_subexpressions::expression(Expression
** pexpr
)
598 else if ((*pexpr
)->temporary_reference_expression() == NULL
599 && !(*pexpr
)->is_nil_expression()
600 && !(*pexpr
)->is_constant())
602 Location loc
= (*pexpr
)->location();
603 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
604 this->block_
->add_statement(temp
);
605 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
607 // We only need to move top-level subexpressions.
608 return TRAVERSE_SKIP_COMPONENTS
;
611 // The Move_ordered_evals class is used to find any subexpressions of
612 // an expression that have an evaluation order dependency. It creates
613 // temporary variables to hold them.
615 class Move_ordered_evals
: public Traverse
618 Move_ordered_evals(Block
* block
)
619 : Traverse(traverse_expressions
),
625 expression(Expression
**);
628 // The block where new temporary variables should be added.
633 Move_ordered_evals::expression(Expression
** pexpr
)
635 // We have to look at subexpressions first.
636 if ((*pexpr
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
637 return TRAVERSE_EXIT
;
640 if ((*pexpr
)->must_eval_subexpressions_in_order(&i
))
642 Move_subexpressions
ms(i
, this->block_
);
643 if ((*pexpr
)->traverse_subexpressions(&ms
) == TRAVERSE_EXIT
)
644 return TRAVERSE_EXIT
;
647 if ((*pexpr
)->must_eval_in_order())
649 Call_expression
* call
= (*pexpr
)->call_expression();
650 if (call
!= NULL
&& call
->is_multi_value_arg())
652 // A call expression which returns multiple results as an argument
653 // to another call must be handled specially. We can't create a
654 // temporary because there is no type to give it. Instead, group
655 // the caller and this multi-valued call argument and use a temporary
656 // variable to hold them.
657 return TRAVERSE_SKIP_COMPONENTS
;
660 Location loc
= (*pexpr
)->location();
661 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
662 this->block_
->add_statement(temp
);
663 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
665 return TRAVERSE_SKIP_COMPONENTS
;
668 // Class Assignment_statement.
673 Assignment_statement::do_traverse(Traverse
* traverse
)
675 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
676 return TRAVERSE_EXIT
;
677 return this->traverse_expression(traverse
, &this->rhs_
);
681 Assignment_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
683 tassign
->assignment(&this->lhs_
, &this->rhs_
);
687 // Lower an assignment to a map index expression to a runtime function
691 Assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
694 Map_index_expression
* mie
= this->lhs_
->map_index_expression();
697 Location loc
= this->location();
699 Expression
* map
= mie
->map();
700 Map_type
* mt
= map
->type()->map_type();
703 go_assert(saw_errors());
704 return Statement::make_error_statement(loc
);
707 Block
* b
= new Block(enclosing
, loc
);
709 // Move out any subexpressions on the left hand side to make
710 // sure that functions are called in the required order.
711 Move_ordered_evals
moe(b
);
712 mie
->traverse_subexpressions(&moe
);
714 // Copy the key into a temporary so that we can take its address
715 // without pushing the value onto the heap.
717 // var key_temp KEY_TYPE = MAP_INDEX
718 Temporary_statement
* key_temp
= Statement::make_temporary(mt
->key_type(),
721 b
->add_statement(key_temp
);
723 // Copy the value into a temporary to ensure that it is
724 // evaluated before we add the key to the map. This may matter
725 // if the value is itself a reference to the map.
727 // var val_temp VAL_TYPE = RHS
728 Temporary_statement
* val_temp
= Statement::make_temporary(mt
->val_type(),
731 b
->add_statement(val_temp
);
733 // *mapassign(TYPE, MAP, &key_temp) = RHS
734 Expression
* a1
= Expression::make_type_descriptor(mt
, loc
);
735 Expression
* a2
= mie
->map();
736 Temporary_reference_expression
* ref
=
737 Expression::make_temporary_reference(key_temp
, loc
);
738 Expression
* a3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
739 Expression
* call
= Runtime::make_call(Runtime::MAPASSIGN
, loc
, 3,
741 Type
* ptrval_type
= Type::make_pointer_type(mt
->val_type());
742 call
= Expression::make_cast(ptrval_type
, call
, loc
);
743 Expression
* indir
= Expression::make_unary(OPERATOR_MULT
, call
, loc
);
744 ref
= Expression::make_temporary_reference(val_temp
, loc
);
745 b
->add_statement(Statement::make_assignment(indir
, ref
, loc
));
747 return Statement::make_block_statement(b
, loc
);
753 // Set types for the assignment.
756 Assignment_statement::do_determine_types()
758 this->lhs_
->determine_type_no_context();
759 Type
* rhs_context_type
= this->lhs_
->type();
760 if (rhs_context_type
->is_sink_type())
761 rhs_context_type
= NULL
;
762 Type_context
context(rhs_context_type
, false);
763 this->rhs_
->determine_type(&context
);
766 // Check types for an assignment.
769 Assignment_statement::do_check_types(Gogo
*)
771 // The left hand side must be either addressable, a map index
772 // expression, or the blank identifier.
773 if (!this->lhs_
->is_addressable()
774 && this->lhs_
->map_index_expression() == NULL
775 && !this->lhs_
->is_sink_expression())
777 if (!this->lhs_
->type()->is_error())
778 this->report_error(_("invalid left hand side of assignment"));
782 Type
* lhs_type
= this->lhs_
->type();
783 Type
* rhs_type
= this->rhs_
->type();
785 // Invalid assignment of nil to the blank identifier.
786 if (lhs_type
->is_sink_type()
787 && rhs_type
->is_nil_type())
789 this->report_error(_("use of untyped nil"));
794 if (!Type::are_assignable(lhs_type
, rhs_type
, &reason
))
797 go_error_at(this->location(), "incompatible types in assignment");
799 go_error_at(this->location(), "incompatible types in assignment (%s)",
801 this->set_is_error();
804 if (lhs_type
->is_error() || rhs_type
->is_error())
805 this->set_is_error();
808 // Flatten an assignment statement. We may need a temporary for
809 // interface conversion.
812 Assignment_statement::do_flatten(Gogo
*, Named_object
*, Block
*,
813 Statement_inserter
* inserter
)
815 if (this->lhs_
->is_error_expression()
816 || this->lhs_
->type()->is_error_type()
817 || this->rhs_
->is_error_expression()
818 || this->rhs_
->type()->is_error_type())
820 go_assert(saw_errors());
821 return Statement::make_error_statement(this->location());
824 if (!this->lhs_
->is_sink_expression()
825 && !Type::are_identical(this->lhs_
->type(), this->rhs_
->type(),
827 && this->rhs_
->type()->interface_type() != NULL
828 && !this->rhs_
->is_variable())
830 Temporary_statement
* temp
=
831 Statement::make_temporary(NULL
, this->rhs_
, this->location());
832 inserter
->insert(temp
);
833 this->rhs_
= Expression::make_temporary_reference(temp
,
840 // Helper class to locate a root Var_expression within an expression
841 // tree and mark it as being in an "lvalue" or assignment
842 // context. Examples:
846 // x.z.w[blah(v + u)], y.another = 2, 3
848 // In the code above, vars "x" and "y" appear in lvalue / assignment
849 // context, whereas the other vars "v", "u", etc are in rvalue context.
851 // Note: at the moment the Var_expression version of "do_copy()"
852 // defaults to returning the original object, not a new object,
853 // meaning that a given Var_expression can be referenced from more
854 // than one place in the tree. This means that when we want to mark a
855 // Var_expression as having lvalue semantics, we need to make a copy
858 // mystruct.myfield += 42
860 // When this is lowered to eliminate the += operator, we get a tree
862 // mystruct.myfield = mystruct.field + 42
864 // in which the "mystruct" same Var_expression is referenced on both
865 // LHS and RHS subtrees. This in turn means that if we try to mark the
866 // LHS Var_expression the RHS Var_expression will also be marked. To
867 // address this issue, the code below clones any var_expression before
868 // applying an lvalue marking.
871 class Mark_lvalue_varexprs
: public Traverse
874 Mark_lvalue_varexprs()
875 : Traverse(traverse_expressions
)
880 expression(Expression
**);
885 int Mark_lvalue_varexprs::expression(Expression
** ppexpr
)
887 Expression
* e
= *ppexpr
;
889 Var_expression
* ve
= e
->var_expression();
892 ve
= new Var_expression(ve
->named_object(), ve
->location());
893 ve
->set_in_lvalue_pos();
895 return TRAVERSE_EXIT
;
898 Field_reference_expression
* fre
= e
->field_reference_expression();
900 return TRAVERSE_CONTINUE
;
902 Array_index_expression
* aie
= e
->array_index_expression();
905 Mark_lvalue_varexprs mlve
;
906 aie
->array()->traverse_subexpressions(&mlve
);
907 return TRAVERSE_EXIT
;
910 Unary_expression
* ue
= e
->unary_expression();
911 if (ue
&& ue
->op() == OPERATOR_MULT
)
912 return TRAVERSE_CONTINUE
;
914 return TRAVERSE_EXIT
;
917 // Convert an assignment statement to the backend representation.
920 Assignment_statement::do_get_backend(Translate_context
* context
)
922 if (this->lhs_
->is_sink_expression())
924 Bexpression
* rhs
= this->rhs_
->get_backend(context
);
925 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
926 return context
->backend()->expression_statement(bfunction
, rhs
);
929 Mark_lvalue_varexprs mlve
;
930 Expression::traverse(&this->lhs_
, &mlve
);
932 Bexpression
* lhs
= this->lhs_
->get_backend(context
);
934 Expression::convert_for_assignment(context
->gogo(), this->lhs_
->type(),
935 this->rhs_
, this->location());
936 Bexpression
* rhs
= conv
->get_backend(context
);
937 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
938 return context
->backend()->assignment_statement(bfunction
, lhs
, rhs
,
942 // Dump the AST representation for an assignment statement.
945 Assignment_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
948 ast_dump_context
->print_indent();
949 ast_dump_context
->dump_expression(this->lhs_
);
950 ast_dump_context
->ostream() << " = " ;
951 ast_dump_context
->dump_expression(this->rhs_
);
952 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
955 // Make an assignment statement.
958 Statement::make_assignment(Expression
* lhs
, Expression
* rhs
,
961 return new Assignment_statement(lhs
, rhs
, location
);
964 // An assignment operation statement.
966 class Assignment_operation_statement
: public Statement
969 Assignment_operation_statement(Operator op
, Expression
* lhs
, Expression
* rhs
,
971 : Statement(STATEMENT_ASSIGNMENT_OPERATION
, location
),
972 op_(op
), lhs_(lhs
), rhs_(rhs
)
977 do_traverse(Traverse
*);
980 do_traverse_assignments(Traverse_assignments
*)
981 { go_unreachable(); }
984 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
987 do_get_backend(Translate_context
*)
988 { go_unreachable(); }
991 do_dump_statement(Ast_dump_context
*) const;
994 // The operator (OPERATOR_PLUSEQ, etc.).
1005 Assignment_operation_statement::do_traverse(Traverse
* traverse
)
1007 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
1008 return TRAVERSE_EXIT
;
1009 return this->traverse_expression(traverse
, &this->rhs_
);
1012 // Lower an assignment operation statement to a regular assignment
1016 Assignment_operation_statement::do_lower(Gogo
*, Named_object
*,
1017 Block
* enclosing
, Statement_inserter
*)
1019 Location loc
= this->location();
1021 // We have to evaluate the left hand side expression only once. We
1022 // do this by moving out any expression with side effects.
1023 Block
* b
= new Block(enclosing
, loc
);
1024 Move_ordered_evals
moe(b
);
1025 this->lhs_
->traverse_subexpressions(&moe
);
1027 Expression
* lval
= this->lhs_
->copy();
1032 case OPERATOR_PLUSEQ
:
1035 case OPERATOR_MINUSEQ
:
1036 op
= OPERATOR_MINUS
;
1041 case OPERATOR_XOREQ
:
1044 case OPERATOR_MULTEQ
:
1047 case OPERATOR_DIVEQ
:
1050 case OPERATOR_MODEQ
:
1053 case OPERATOR_LSHIFTEQ
:
1054 op
= OPERATOR_LSHIFT
;
1056 case OPERATOR_RSHIFTEQ
:
1057 op
= OPERATOR_RSHIFT
;
1059 case OPERATOR_ANDEQ
:
1062 case OPERATOR_BITCLEAREQ
:
1063 op
= OPERATOR_BITCLEAR
;
1069 Expression
* binop
= Expression::make_binary(op
, lval
, this->rhs_
, loc
);
1070 Statement
* s
= Statement::make_assignment(this->lhs_
, binop
, loc
);
1071 if (b
->statements()->empty())
1078 b
->add_statement(s
);
1079 return Statement::make_block_statement(b
, loc
);
1083 // Dump the AST representation for an assignment operation statement
1086 Assignment_operation_statement::do_dump_statement(
1087 Ast_dump_context
* ast_dump_context
) const
1089 ast_dump_context
->print_indent();
1090 ast_dump_context
->dump_expression(this->lhs_
);
1091 ast_dump_context
->dump_operator(this->op_
);
1092 ast_dump_context
->dump_expression(this->rhs_
);
1093 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1096 // Make an assignment operation statement.
1099 Statement::make_assignment_operation(Operator op
, Expression
* lhs
,
1100 Expression
* rhs
, Location location
)
1102 return new Assignment_operation_statement(op
, lhs
, rhs
, location
);
1105 // A tuple assignment statement. This differs from an assignment
1106 // statement in that the right-hand-side expressions are evaluated in
1109 class Tuple_assignment_statement
: public Statement
1112 Tuple_assignment_statement(Expression_list
* lhs
, Expression_list
* rhs
,
1114 : Statement(STATEMENT_TUPLE_ASSIGNMENT
, location
),
1115 lhs_(lhs
), rhs_(rhs
)
1120 do_traverse(Traverse
* traverse
);
1123 do_traverse_assignments(Traverse_assignments
*)
1124 { go_unreachable(); }
1127 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1130 do_get_backend(Translate_context
*)
1131 { go_unreachable(); }
1134 do_dump_statement(Ast_dump_context
*) const;
1137 // Left hand side--a list of lvalues.
1138 Expression_list
* lhs_
;
1139 // Right hand side--a list of rvalues.
1140 Expression_list
* rhs_
;
1146 Tuple_assignment_statement::do_traverse(Traverse
* traverse
)
1148 if (this->traverse_expression_list(traverse
, this->lhs_
) == TRAVERSE_EXIT
)
1149 return TRAVERSE_EXIT
;
1150 return this->traverse_expression_list(traverse
, this->rhs_
);
1153 // Lower a tuple assignment. We use temporary variables to split it
1154 // up into a set of single assignments.
1157 Tuple_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
1158 Statement_inserter
*)
1160 Location loc
= this->location();
1162 Block
* b
= new Block(enclosing
, loc
);
1164 // First move out any subexpressions on the left hand side. The
1165 // right hand side will be evaluated in the required order anyhow.
1166 Move_ordered_evals
moe(b
);
1167 for (Expression_list::iterator plhs
= this->lhs_
->begin();
1168 plhs
!= this->lhs_
->end();
1170 Expression::traverse(&*plhs
, &moe
);
1172 std::vector
<Temporary_statement
*> temps
;
1173 temps
.reserve(this->lhs_
->size());
1175 Expression_list::const_iterator prhs
= this->rhs_
->begin();
1176 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
1177 plhs
!= this->lhs_
->end();
1180 go_assert(prhs
!= this->rhs_
->end());
1182 if ((*plhs
)->is_error_expression()
1183 || (*plhs
)->type()->is_error()
1184 || (*prhs
)->is_error_expression()
1185 || (*prhs
)->type()->is_error())
1188 if ((*plhs
)->is_sink_expression())
1190 if ((*prhs
)->type()->is_nil_type())
1191 this->report_error(_("use of untyped nil"));
1193 b
->add_statement(Statement::make_statement(*prhs
, true));
1197 Temporary_statement
* temp
= Statement::make_temporary((*plhs
)->type(),
1199 b
->add_statement(temp
);
1200 temps
.push_back(temp
);
1203 go_assert(prhs
== this->rhs_
->end());
1205 prhs
= this->rhs_
->begin();
1206 std::vector
<Temporary_statement
*>::const_iterator ptemp
= temps
.begin();
1207 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
1208 plhs
!= this->lhs_
->end();
1211 if ((*plhs
)->is_error_expression()
1212 || (*plhs
)->type()->is_error()
1213 || (*prhs
)->is_error_expression()
1214 || (*prhs
)->type()->is_error())
1217 if ((*plhs
)->is_sink_expression())
1220 Expression
* ref
= Expression::make_temporary_reference(*ptemp
, loc
);
1221 b
->add_statement(Statement::make_assignment(*plhs
, ref
, loc
));
1224 go_assert(ptemp
== temps
.end() || saw_errors());
1226 return Statement::make_block_statement(b
, loc
);
1229 // Dump the AST representation for a tuple assignment statement.
1232 Tuple_assignment_statement::do_dump_statement(
1233 Ast_dump_context
* ast_dump_context
) const
1235 ast_dump_context
->print_indent();
1236 ast_dump_context
->dump_expression_list(this->lhs_
);
1237 ast_dump_context
->ostream() << " = ";
1238 ast_dump_context
->dump_expression_list(this->rhs_
);
1239 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1242 // Make a tuple assignment statement.
1245 Statement::make_tuple_assignment(Expression_list
* lhs
, Expression_list
* rhs
,
1248 return new Tuple_assignment_statement(lhs
, rhs
, location
);
1251 // A tuple assignment from a map index expression.
1254 class Tuple_map_assignment_statement
: public Statement
1257 Tuple_map_assignment_statement(Expression
* val
, Expression
* present
,
1258 Expression
* map_index
,
1260 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT
, location
),
1261 val_(val
), present_(present
), map_index_(map_index
)
1266 do_traverse(Traverse
* traverse
);
1269 do_traverse_assignments(Traverse_assignments
*)
1270 { go_unreachable(); }
1273 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1276 do_get_backend(Translate_context
*)
1277 { go_unreachable(); }
1280 do_dump_statement(Ast_dump_context
*) const;
1283 // Lvalue which receives the value from the map.
1285 // Lvalue which receives whether the key value was present.
1286 Expression
* present_
;
1287 // The map index expression.
1288 Expression
* map_index_
;
1294 Tuple_map_assignment_statement::do_traverse(Traverse
* traverse
)
1296 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1297 || this->traverse_expression(traverse
, &this->present_
) == TRAVERSE_EXIT
)
1298 return TRAVERSE_EXIT
;
1299 return this->traverse_expression(traverse
, &this->map_index_
);
1302 // Lower a tuple map assignment.
1305 Tuple_map_assignment_statement::do_lower(Gogo
* gogo
, Named_object
*,
1306 Block
* enclosing
, Statement_inserter
*)
1308 Location loc
= this->location();
1310 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1311 if (map_index
== NULL
)
1313 this->report_error(_("expected map index on right hand side"));
1314 return Statement::make_error_statement(loc
);
1316 Map_type
* map_type
= map_index
->get_map_type();
1317 if (map_type
== NULL
)
1318 return Statement::make_error_statement(loc
);
1320 Block
* b
= new Block(enclosing
, loc
);
1322 // Move out any subexpressions to make sure that functions are
1323 // called in the required order.
1324 Move_ordered_evals
moe(b
);
1325 this->val_
->traverse_subexpressions(&moe
);
1326 this->present_
->traverse_subexpressions(&moe
);
1328 // Copy the key value into a temporary so that we can take its
1329 // address without pushing the value onto the heap.
1331 // var key_temp KEY_TYPE = MAP_INDEX
1332 Temporary_statement
* key_temp
=
1333 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1334 b
->add_statement(key_temp
);
1336 // var val_ptr_temp *VAL_TYPE
1337 Type
* val_ptr_type
= Type::make_pointer_type(map_type
->val_type());
1338 Temporary_statement
* val_ptr_temp
= Statement::make_temporary(val_ptr_type
,
1340 b
->add_statement(val_ptr_temp
);
1342 // var present_temp bool
1343 Temporary_statement
* present_temp
=
1344 Statement::make_temporary((this->present_
->type()->is_sink_type())
1345 ? Type::make_boolean_type()
1346 : this->present_
->type(),
1348 b
->add_statement(present_temp
);
1350 // val_ptr_temp, present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp)
1351 Expression
* a1
= Expression::make_type_descriptor(map_type
, loc
);
1352 Expression
* a2
= map_index
->map();
1353 Temporary_reference_expression
* ref
=
1354 Expression::make_temporary_reference(key_temp
, loc
);
1355 Expression
* a3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1356 Expression
* a4
= map_type
->fat_zero_value(gogo
);
1357 Call_expression
* call
;
1359 call
= Runtime::make_call(Runtime::MAPACCESS2
, loc
, 3, a1
, a2
, a3
);
1361 call
= Runtime::make_call(Runtime::MAPACCESS2_FAT
, loc
, 4, a1
, a2
, a3
, a4
);
1362 ref
= Expression::make_temporary_reference(val_ptr_temp
, loc
);
1363 ref
->set_is_lvalue();
1364 Expression
* res
= Expression::make_call_result(call
, 0);
1365 res
= Expression::make_unsafe_cast(val_ptr_type
, res
, loc
);
1366 Statement
* s
= Statement::make_assignment(ref
, res
, loc
);
1367 b
->add_statement(s
);
1368 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1369 ref
->set_is_lvalue();
1370 res
= Expression::make_call_result(call
, 1);
1371 s
= Statement::make_assignment(ref
, res
, loc
);
1372 b
->add_statement(s
);
1374 // val = *val__ptr_temp
1375 ref
= Expression::make_temporary_reference(val_ptr_temp
, loc
);
1376 Expression
* ind
= Expression::make_unary(OPERATOR_MULT
, ref
, loc
);
1377 s
= Statement::make_assignment(this->val_
, ind
, loc
);
1378 b
->add_statement(s
);
1380 // present = present_temp
1381 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1382 s
= Statement::make_assignment(this->present_
, ref
, loc
);
1383 b
->add_statement(s
);
1385 return Statement::make_block_statement(b
, loc
);
1388 // Dump the AST representation for a tuple map assignment statement.
1391 Tuple_map_assignment_statement::do_dump_statement(
1392 Ast_dump_context
* ast_dump_context
) const
1394 ast_dump_context
->print_indent();
1395 ast_dump_context
->dump_expression(this->val_
);
1396 ast_dump_context
->ostream() << ", ";
1397 ast_dump_context
->dump_expression(this->present_
);
1398 ast_dump_context
->ostream() << " = ";
1399 ast_dump_context
->dump_expression(this->map_index_
);
1400 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1403 // Make a map assignment statement which returns a pair of values.
1406 Statement::make_tuple_map_assignment(Expression
* val
, Expression
* present
,
1407 Expression
* map_index
,
1410 return new Tuple_map_assignment_statement(val
, present
, map_index
, location
);
1413 // A tuple assignment from a receive statement.
1415 class Tuple_receive_assignment_statement
: public Statement
1418 Tuple_receive_assignment_statement(Expression
* val
, Expression
* closed
,
1419 Expression
* channel
, Location location
)
1420 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
, location
),
1421 val_(val
), closed_(closed
), channel_(channel
)
1426 do_traverse(Traverse
* traverse
);
1429 do_traverse_assignments(Traverse_assignments
*)
1430 { go_unreachable(); }
1433 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1436 do_get_backend(Translate_context
*)
1437 { go_unreachable(); }
1440 do_dump_statement(Ast_dump_context
*) const;
1443 // Lvalue which receives the value from the channel.
1445 // Lvalue which receives whether the channel is closed.
1446 Expression
* closed_
;
1447 // The channel on which we receive the value.
1448 Expression
* channel_
;
1454 Tuple_receive_assignment_statement::do_traverse(Traverse
* traverse
)
1456 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1457 || this->traverse_expression(traverse
, &this->closed_
) == TRAVERSE_EXIT
)
1458 return TRAVERSE_EXIT
;
1459 return this->traverse_expression(traverse
, &this->channel_
);
1462 // Lower to a function call.
1465 Tuple_receive_assignment_statement::do_lower(Gogo
*, Named_object
*,
1467 Statement_inserter
*)
1469 Location loc
= this->location();
1471 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
1472 if (channel_type
== NULL
)
1474 this->report_error(_("expected channel"));
1475 return Statement::make_error_statement(loc
);
1477 if (!channel_type
->may_receive())
1479 this->report_error(_("invalid receive on send-only channel"));
1480 return Statement::make_error_statement(loc
);
1483 Block
* b
= new Block(enclosing
, loc
);
1485 // Make sure that any subexpressions on the left hand side are
1486 // evaluated in the right order.
1487 Move_ordered_evals
moe(b
);
1488 this->val_
->traverse_subexpressions(&moe
);
1489 this->closed_
->traverse_subexpressions(&moe
);
1491 // var val_temp ELEMENT_TYPE
1492 Temporary_statement
* val_temp
=
1493 Statement::make_temporary(channel_type
->element_type(), NULL
, loc
);
1494 b
->add_statement(val_temp
);
1496 // var closed_temp bool
1497 Temporary_statement
* closed_temp
=
1498 Statement::make_temporary((this->closed_
->type()->is_sink_type())
1499 ? Type::make_boolean_type()
1500 : this->closed_
->type(),
1502 b
->add_statement(closed_temp
);
1504 // closed_temp = chanrecv2(type, channel, &val_temp)
1505 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
1507 Temporary_reference_expression
* ref
=
1508 Expression::make_temporary_reference(val_temp
, loc
);
1509 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1510 Expression
* call
= Runtime::make_call(Runtime::CHANRECV2
,
1511 loc
, 3, td
, this->channel_
, p2
);
1512 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1513 ref
->set_is_lvalue();
1514 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1515 b
->add_statement(s
);
1518 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1519 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1520 b
->add_statement(s
);
1522 // closed = closed_temp
1523 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1524 s
= Statement::make_assignment(this->closed_
, ref
, loc
);
1525 b
->add_statement(s
);
1527 return Statement::make_block_statement(b
, loc
);
1530 // Dump the AST representation for a tuple receive statement.
1533 Tuple_receive_assignment_statement::do_dump_statement(
1534 Ast_dump_context
* ast_dump_context
) const
1536 ast_dump_context
->print_indent();
1537 ast_dump_context
->dump_expression(this->val_
);
1538 ast_dump_context
->ostream() << ", ";
1539 ast_dump_context
->dump_expression(this->closed_
);
1540 ast_dump_context
->ostream() << " <- ";
1541 ast_dump_context
->dump_expression(this->channel_
);
1542 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1545 // Make a nonblocking receive statement.
1548 Statement::make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
1549 Expression
* channel
,
1552 return new Tuple_receive_assignment_statement(val
, closed
, channel
,
1556 // An assignment to a pair of values from a type guard. This is a
1557 // conditional type guard. v, ok = i.(type).
1559 class Tuple_type_guard_assignment_statement
: public Statement
1562 Tuple_type_guard_assignment_statement(Expression
* val
, Expression
* ok
,
1563 Expression
* expr
, Type
* type
,
1565 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
, location
),
1566 val_(val
), ok_(ok
), expr_(expr
), type_(type
)
1571 do_traverse(Traverse
*);
1574 do_traverse_assignments(Traverse_assignments
*)
1575 { go_unreachable(); }
1578 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1581 do_get_backend(Translate_context
*)
1582 { go_unreachable(); }
1585 do_dump_statement(Ast_dump_context
*) const;
1589 lower_to_type(Runtime::Function
);
1592 lower_to_object_type(Block
*, Runtime::Function
);
1594 // The variable which recieves the converted value.
1596 // The variable which receives the indication of success.
1598 // The expression being converted.
1600 // The type to which the expression is being converted.
1604 // Traverse a type guard tuple assignment.
1607 Tuple_type_guard_assignment_statement::do_traverse(Traverse
* traverse
)
1609 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1610 || this->traverse_expression(traverse
, &this->ok_
) == TRAVERSE_EXIT
1611 || this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
1612 return TRAVERSE_EXIT
;
1613 return this->traverse_expression(traverse
, &this->expr_
);
1616 // Lower to a function call.
1619 Tuple_type_guard_assignment_statement::do_lower(Gogo
*, Named_object
*,
1621 Statement_inserter
*)
1623 Location loc
= this->location();
1625 Type
* expr_type
= this->expr_
->type();
1626 if (expr_type
->interface_type() == NULL
)
1628 if (!expr_type
->is_error() && !this->type_
->is_error())
1629 this->report_error(_("type assertion only valid for interface types"));
1630 return Statement::make_error_statement(loc
);
1633 Block
* b
= new Block(enclosing
, loc
);
1635 // Make sure that any subexpressions on the left hand side are
1636 // evaluated in the right order.
1637 Move_ordered_evals
moe(b
);
1638 this->val_
->traverse_subexpressions(&moe
);
1639 this->ok_
->traverse_subexpressions(&moe
);
1641 bool expr_is_empty
= expr_type
->interface_type()->is_empty();
1642 Call_expression
* call
;
1643 if (this->type_
->interface_type() != NULL
)
1645 if (this->type_
->interface_type()->is_empty())
1646 call
= Runtime::make_call((expr_is_empty
1647 ? Runtime::IFACEE2E2
1648 : Runtime::IFACEI2E2
),
1649 loc
, 1, this->expr_
);
1651 call
= this->lower_to_type(expr_is_empty
1652 ? Runtime::IFACEE2I2
1653 : Runtime::IFACEI2I2
);
1655 else if (this->type_
->points_to() != NULL
)
1656 call
= this->lower_to_type(expr_is_empty
1657 ? Runtime::IFACEE2T2P
1658 : Runtime::IFACEI2T2P
);
1661 this->lower_to_object_type(b
,
1663 ? Runtime::IFACEE2T2
1664 : Runtime::IFACEI2T2
));
1670 Expression
* res
= Expression::make_call_result(call
, 0);
1671 res
= Expression::make_unsafe_cast(this->type_
, res
, loc
);
1672 Statement
* s
= Statement::make_assignment(this->val_
, res
, loc
);
1673 b
->add_statement(s
);
1675 res
= Expression::make_call_result(call
, 1);
1676 s
= Statement::make_assignment(this->ok_
, res
, loc
);
1677 b
->add_statement(s
);
1680 return Statement::make_block_statement(b
, loc
);
1683 // Lower a conversion to a non-empty interface type or a pointer type.
1686 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code
)
1688 Location loc
= this->location();
1689 return Runtime::make_call(code
, loc
, 2,
1690 Expression::make_type_descriptor(this->type_
, loc
),
1694 // Lower a conversion to a non-interface non-pointer type.
1697 Tuple_type_guard_assignment_statement::lower_to_object_type(
1699 Runtime::Function code
)
1701 Location loc
= this->location();
1703 // var val_temp TYPE
1704 Temporary_statement
* val_temp
= Statement::make_temporary(this->type_
,
1706 b
->add_statement(val_temp
);
1708 // ok = CODE(type_descriptor, expr, &val_temp)
1709 Expression
* p1
= Expression::make_type_descriptor(this->type_
, loc
);
1710 Expression
* ref
= Expression::make_temporary_reference(val_temp
, loc
);
1711 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1712 Expression
* call
= Runtime::make_call(code
, loc
, 3, p1
, this->expr_
, p3
);
1713 Statement
* s
= Statement::make_assignment(this->ok_
, call
, loc
);
1714 b
->add_statement(s
);
1717 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1718 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1719 b
->add_statement(s
);
1722 // Dump the AST representation for a tuple type guard statement.
1725 Tuple_type_guard_assignment_statement::do_dump_statement(
1726 Ast_dump_context
* ast_dump_context
) const
1728 ast_dump_context
->print_indent();
1729 ast_dump_context
->dump_expression(this->val_
);
1730 ast_dump_context
->ostream() << ", ";
1731 ast_dump_context
->dump_expression(this->ok_
);
1732 ast_dump_context
->ostream() << " = ";
1733 ast_dump_context
->dump_expression(this->expr_
);
1734 ast_dump_context
->ostream() << " . ";
1735 ast_dump_context
->dump_type(this->type_
);
1736 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1739 // Make an assignment from a type guard to a pair of variables.
1742 Statement::make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
1743 Expression
* expr
, Type
* type
,
1746 return new Tuple_type_guard_assignment_statement(val
, ok
, expr
, type
,
1750 // Class Expression_statement.
1754 Expression_statement::Expression_statement(Expression
* expr
, bool is_ignored
)
1755 : Statement(STATEMENT_EXPRESSION
, expr
->location()),
1756 expr_(expr
), is_ignored_(is_ignored
)
1763 Expression_statement::do_determine_types()
1765 this->expr_
->determine_type_no_context();
1768 // Check the types of an expression statement. The only check we do
1769 // is to possibly give an error about discarding the value of the
1773 Expression_statement::do_check_types(Gogo
*)
1775 if (!this->is_ignored_
)
1776 this->expr_
->discarding_value();
1779 // An expression statement is only a terminating statement if it is
1783 Expression_statement::do_may_fall_through() const
1785 const Call_expression
* call
= this->expr_
->call_expression();
1788 const Expression
* fn
= call
->fn();
1789 // panic is still an unknown named object.
1790 const Unknown_expression
* ue
= fn
->unknown_expression();
1793 Named_object
* no
= ue
->named_object();
1795 if (no
->is_unknown())
1796 no
= no
->unknown_value()->real_named_object();
1799 Function_type
* fntype
;
1800 if (no
->is_function())
1801 fntype
= no
->func_value()->type();
1802 else if (no
->is_function_declaration())
1803 fntype
= no
->func_declaration_value()->type();
1807 // The builtin function panic does not return.
1808 if (fntype
!= NULL
&& fntype
->is_builtin() && no
->name() == "panic")
1816 // Convert to backend representation.
1819 Expression_statement::do_get_backend(Translate_context
* context
)
1821 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
1822 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
1823 return context
->backend()->expression_statement(bfunction
, bexpr
);
1826 // Dump the AST representation for an expression statement
1829 Expression_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
1832 ast_dump_context
->print_indent();
1833 ast_dump_context
->dump_expression(expr_
);
1834 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1837 // Make an expression statement from an Expression.
1840 Statement::make_statement(Expression
* expr
, bool is_ignored
)
1842 return new Expression_statement(expr
, is_ignored
);
1845 // Convert a block to the backend representation of a statement.
1848 Block_statement::do_get_backend(Translate_context
* context
)
1850 Bblock
* bblock
= this->block_
->get_backend(context
);
1851 return context
->backend()->block_statement(bblock
);
1854 // Dump the AST for a block statement
1857 Block_statement::do_dump_statement(Ast_dump_context
*) const
1859 // block statement braces are dumped when traversing.
1862 // Make a block statement.
1865 Statement::make_block_statement(Block
* block
, Location location
)
1867 return new Block_statement(block
, location
);
1870 // An increment or decrement statement.
1872 class Inc_dec_statement
: public Statement
1875 Inc_dec_statement(bool is_inc
, Expression
* expr
)
1876 : Statement(STATEMENT_INCDEC
, expr
->location()),
1877 expr_(expr
), is_inc_(is_inc
)
1882 do_traverse(Traverse
* traverse
)
1883 { return this->traverse_expression(traverse
, &this->expr_
); }
1886 do_traverse_assignments(Traverse_assignments
*)
1887 { go_unreachable(); }
1890 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1893 do_get_backend(Translate_context
*)
1894 { go_unreachable(); }
1897 do_dump_statement(Ast_dump_context
*) const;
1900 // The l-value to increment or decrement.
1902 // Whether to increment or decrement.
1906 // Lower to += or -=.
1909 Inc_dec_statement::do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
1911 Location loc
= this->location();
1912 Expression
* oexpr
= Expression::make_integer_ul(1, this->expr_
->type(), loc
);
1913 Operator op
= this->is_inc_
? OPERATOR_PLUSEQ
: OPERATOR_MINUSEQ
;
1914 return Statement::make_assignment_operation(op
, this->expr_
, oexpr
, loc
);
1917 // Dump the AST representation for a inc/dec statement.
1920 Inc_dec_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
1922 ast_dump_context
->print_indent();
1923 ast_dump_context
->dump_expression(expr_
);
1924 ast_dump_context
->ostream() << (is_inc_
? "++": "--") << dsuffix(location()) << std::endl
;
1927 // Make an increment statement.
1930 Statement::make_inc_statement(Expression
* expr
)
1932 return new Inc_dec_statement(true, expr
);
1935 // Make a decrement statement.
1938 Statement::make_dec_statement(Expression
* expr
)
1940 return new Inc_dec_statement(false, expr
);
1943 // Class Thunk_statement. This is the base class for go and defer
1948 Thunk_statement::Thunk_statement(Statement_classification classification
,
1949 Call_expression
* call
,
1951 : Statement(classification
, location
),
1952 call_(call
), struct_type_(NULL
)
1956 // Return whether this is a simple statement which does not require a
1960 Thunk_statement::is_simple(Function_type
* fntype
) const
1962 // We need a thunk to call a method, or to pass a variable number of
1964 if (fntype
->is_method() || fntype
->is_varargs())
1967 // A defer statement requires a thunk to set up for whether the
1968 // function can call recover.
1969 if (this->classification() == STATEMENT_DEFER
)
1972 // We can only permit a single parameter of pointer type.
1973 const Typed_identifier_list
* parameters
= fntype
->parameters();
1974 if (parameters
!= NULL
1975 && (parameters
->size() > 1
1976 || (parameters
->size() == 1
1977 && parameters
->begin()->type()->points_to() == NULL
)))
1980 // If the function returns multiple values, or returns a type other
1981 // than integer, floating point, or pointer, then it may get a
1982 // hidden first parameter, in which case we need the more
1983 // complicated approach. This is true even though we are going to
1984 // ignore the return value.
1985 const Typed_identifier_list
* results
= fntype
->results();
1987 && (results
->size() > 1
1988 || (results
->size() == 1
1989 && !results
->begin()->type()->is_basic_type()
1990 && results
->begin()->type()->points_to() == NULL
)))
1993 // If this calls something that is not a simple function, then we
1995 Expression
* fn
= this->call_
->call_expression()->fn();
1996 if (fn
->func_expression() == NULL
)
1999 // If the function uses a closure, then we need a thunk. FIXME: We
2000 // could accept a zero argument function with a closure.
2001 if (fn
->func_expression()->closure() != NULL
)
2007 // Traverse a thunk statement.
2010 Thunk_statement::do_traverse(Traverse
* traverse
)
2012 return this->traverse_expression(traverse
, &this->call_
);
2015 // We implement traverse_assignment for a thunk statement because it
2016 // effectively copies the function call.
2019 Thunk_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
2021 Expression
* fn
= this->call_
->call_expression()->fn();
2022 Expression
* fn2
= fn
;
2023 tassign
->value(&fn2
, true, false);
2027 // Determine types in a thunk statement.
2030 Thunk_statement::do_determine_types()
2032 this->call_
->determine_type_no_context();
2034 // Now that we know the types of the call, build the struct used to
2036 Call_expression
* ce
= this->call_
->call_expression();
2039 Function_type
* fntype
= ce
->get_function_type();
2040 if (fntype
!= NULL
&& !this->is_simple(fntype
))
2041 this->struct_type_
= this->build_struct(fntype
);
2044 // Check types in a thunk statement.
2047 Thunk_statement::do_check_types(Gogo
*)
2049 if (!this->call_
->discarding_value())
2051 Call_expression
* ce
= this->call_
->call_expression();
2054 if (!this->call_
->is_error_expression())
2055 this->report_error("expected call expression");
2060 // The Traverse class used to find and simplify thunk statements.
2062 class Simplify_thunk_traverse
: public Traverse
2065 Simplify_thunk_traverse(Gogo
* gogo
)
2066 : Traverse(traverse_functions
| traverse_blocks
),
2067 gogo_(gogo
), function_(NULL
)
2071 function(Named_object
*);
2079 // The function we are traversing.
2080 Named_object
* function_
;
2083 // Keep track of the current function while looking for thunks.
2086 Simplify_thunk_traverse::function(Named_object
* no
)
2088 go_assert(this->function_
== NULL
);
2089 this->function_
= no
;
2090 int t
= no
->func_value()->traverse(this);
2091 this->function_
= NULL
;
2092 if (t
== TRAVERSE_EXIT
)
2094 return TRAVERSE_SKIP_COMPONENTS
;
2097 // Look for thunks in a block.
2100 Simplify_thunk_traverse::block(Block
* b
)
2102 // The parser ensures that thunk statements always appear at the end
2104 if (b
->statements()->size() < 1)
2105 return TRAVERSE_CONTINUE
;
2106 Thunk_statement
* stat
= b
->statements()->back()->thunk_statement();
2108 return TRAVERSE_CONTINUE
;
2109 if (stat
->simplify_statement(this->gogo_
, this->function_
, b
))
2110 return TRAVERSE_SKIP_COMPONENTS
;
2111 return TRAVERSE_CONTINUE
;
2114 // Simplify all thunk statements.
2117 Gogo::simplify_thunk_statements()
2119 Simplify_thunk_traverse
thunk_traverse(this);
2120 this->traverse(&thunk_traverse
);
2123 // Return true if the thunk function is a constant, which means that
2124 // it does not need to be passed to the thunk routine.
2127 Thunk_statement::is_constant_function() const
2129 Call_expression
* ce
= this->call_
->call_expression();
2130 Function_type
* fntype
= ce
->get_function_type();
2133 go_assert(saw_errors());
2136 if (fntype
->is_builtin())
2138 Expression
* fn
= ce
->fn();
2139 if (fn
->func_expression() != NULL
)
2140 return fn
->func_expression()->closure() == NULL
;
2141 if (fn
->interface_field_reference_expression() != NULL
)
2146 // Simplify complex thunk statements into simple ones. A complicated
2147 // thunk statement is one which takes anything other than zero
2148 // parameters or a single pointer parameter. We rewrite it into code
2149 // which allocates a struct, stores the parameter values into the
2150 // struct, and does a simple go or defer statement which passes the
2151 // struct to a thunk. The thunk does the real call.
2154 Thunk_statement::simplify_statement(Gogo
* gogo
, Named_object
* function
,
2157 if (this->classification() == STATEMENT_ERROR
)
2159 if (this->call_
->is_error_expression())
2162 if (this->classification() == STATEMENT_DEFER
)
2164 // Make sure that the defer stack exists for the function. We
2165 // will use when converting this statement to the backend
2166 // representation, but we want it to exist when we start
2167 // converting the function.
2168 function
->func_value()->defer_stack(this->location());
2171 Call_expression
* ce
= this->call_
->call_expression();
2172 Function_type
* fntype
= ce
->get_function_type();
2175 go_assert(saw_errors());
2176 this->set_is_error();
2179 if (this->is_simple(fntype
))
2182 Expression
* fn
= ce
->fn();
2183 Interface_field_reference_expression
* interface_method
=
2184 fn
->interface_field_reference_expression();
2186 Location location
= this->location();
2188 std::string thunk_name
= Gogo::thunk_name();
2191 this->build_thunk(gogo
, thunk_name
);
2193 // Generate code to call the thunk.
2195 // Get the values to store into the struct which is the single
2196 // argument to the thunk.
2198 Expression_list
* vals
= new Expression_list();
2199 if (!this->is_constant_function())
2200 vals
->push_back(fn
);
2202 if (interface_method
!= NULL
)
2203 vals
->push_back(interface_method
->expr());
2205 if (ce
->args() != NULL
)
2207 for (Expression_list::const_iterator p
= ce
->args()->begin();
2208 p
!= ce
->args()->end();
2211 if ((*p
)->is_constant())
2213 vals
->push_back(*p
);
2217 // Build the struct.
2218 Expression
* constructor
=
2219 Expression::make_struct_composite_literal(this->struct_type_
, vals
,
2222 // Allocate the initialized struct on the heap.
2223 constructor
= Expression::make_heap_expression(constructor
, location
);
2225 // Look up the thunk.
2226 Named_object
* named_thunk
= gogo
->lookup(thunk_name
, NULL
);
2227 go_assert(named_thunk
!= NULL
&& named_thunk
->is_function());
2230 Expression
* func
= Expression::make_func_reference(named_thunk
, NULL
,
2232 Expression_list
* params
= new Expression_list();
2233 params
->push_back(constructor
);
2234 Call_expression
* call
= Expression::make_call(func
, params
, false, location
);
2236 // Build the simple go or defer statement.
2238 if (this->classification() == STATEMENT_GO
)
2239 s
= Statement::make_go_statement(call
, location
);
2240 else if (this->classification() == STATEMENT_DEFER
)
2241 s
= Statement::make_defer_statement(call
, location
);
2245 // The current block should end with the go statement.
2246 go_assert(block
->statements()->size() >= 1);
2247 go_assert(block
->statements()->back() == this);
2248 block
->replace_statement(block
->statements()->size() - 1, s
);
2250 // We already ran the determine_types pass, so we need to run it now
2251 // for the new statement.
2252 s
->determine_types();
2255 gogo
->check_types_in_block(block
);
2257 // Return true to tell the block not to keep looking at statements.
2261 // Set the name to use for thunk parameter N.
2264 Thunk_statement::thunk_field_param(int n
, char* buf
, size_t buflen
)
2266 snprintf(buf
, buflen
, "a%d", n
);
2269 // Build a new struct type to hold the parameters for a complicated
2270 // thunk statement. FNTYPE is the type of the function call.
2273 Thunk_statement::build_struct(Function_type
* fntype
)
2275 Location location
= this->location();
2277 Struct_field_list
* fields
= new Struct_field_list();
2279 Call_expression
* ce
= this->call_
->call_expression();
2280 Expression
* fn
= ce
->fn();
2282 if (!this->is_constant_function())
2284 // The function to call.
2285 fields
->push_back(Struct_field(Typed_identifier("fn", fntype
,
2289 // If this thunk statement calls a method on an interface, we pass
2290 // the interface object to the thunk.
2291 Interface_field_reference_expression
* interface_method
=
2292 fn
->interface_field_reference_expression();
2293 if (interface_method
!= NULL
)
2295 Typed_identifier
tid("object", interface_method
->expr()->type(),
2297 fields
->push_back(Struct_field(tid
));
2300 // The predeclared recover function has no argument. However, we
2301 // add an argument when building recover thunks. Handle that here.
2302 if (ce
->is_recover_call())
2304 fields
->push_back(Struct_field(Typed_identifier("can_recover",
2305 Type::lookup_bool_type(),
2309 const Expression_list
* args
= ce
->args();
2313 for (Expression_list::const_iterator p
= args
->begin();
2317 if ((*p
)->is_constant())
2321 this->thunk_field_param(i
, buf
, sizeof buf
);
2322 fields
->push_back(Struct_field(Typed_identifier(buf
, (*p
)->type(),
2327 Struct_type
*st
= Type::make_struct_type(fields
, location
);
2328 st
->set_is_struct_incomparable();
2332 // Build the thunk we are going to call. This is a brand new, albeit
2333 // artificial, function.
2336 Thunk_statement::build_thunk(Gogo
* gogo
, const std::string
& thunk_name
)
2338 Location location
= this->location();
2340 Call_expression
* ce
= this->call_
->call_expression();
2342 bool may_call_recover
= false;
2343 if (this->classification() == STATEMENT_DEFER
)
2345 Func_expression
* fn
= ce
->fn()->func_expression();
2347 may_call_recover
= true;
2350 const Named_object
* no
= fn
->named_object();
2351 if (!no
->is_function())
2352 may_call_recover
= true;
2354 may_call_recover
= no
->func_value()->calls_recover();
2358 // Build the type of the thunk. The thunk takes a single parameter,
2359 // which is a pointer to the special structure we build.
2360 const char* const parameter_name
= "__go_thunk_parameter";
2361 Typed_identifier_list
* thunk_parameters
= new Typed_identifier_list();
2362 Type
* pointer_to_struct_type
= Type::make_pointer_type(this->struct_type_
);
2363 thunk_parameters
->push_back(Typed_identifier(parameter_name
,
2364 pointer_to_struct_type
,
2367 Typed_identifier_list
* thunk_results
= NULL
;
2368 if (may_call_recover
)
2370 // When deferring a function which may call recover, add a
2371 // return value, to disable tail call optimizations which will
2372 // break the way we check whether recover is permitted.
2373 thunk_results
= new Typed_identifier_list();
2374 thunk_results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
2378 Function_type
* thunk_type
= Type::make_function_type(NULL
, thunk_parameters
,
2382 // Start building the thunk.
2383 Named_object
* function
= gogo
->start_function(thunk_name
, thunk_type
, true,
2386 gogo
->start_block(location
);
2388 // For a defer statement, start with a call to
2389 // __go_set_defer_retaddr. */
2390 Label
* retaddr_label
= NULL
;
2391 if (may_call_recover
)
2393 retaddr_label
= gogo
->add_label_reference("retaddr", location
, false);
2394 Expression
* arg
= Expression::make_label_addr(retaddr_label
, location
);
2395 Expression
* call
= Runtime::make_call(Runtime::SETDEFERRETADDR
,
2398 // This is a hack to prevent the middle-end from deleting the
2400 gogo
->start_block(location
);
2401 gogo
->add_statement(Statement::make_goto_statement(retaddr_label
,
2403 Block
* then_block
= gogo
->finish_block(location
);
2404 then_block
->determine_types();
2406 Statement
* s
= Statement::make_if_statement(call
, then_block
, NULL
,
2408 s
->determine_types();
2409 gogo
->add_statement(s
);
2411 function
->func_value()->set_calls_defer_retaddr();
2414 // Get a reference to the parameter.
2415 Named_object
* named_parameter
= gogo
->lookup(parameter_name
, NULL
);
2416 go_assert(named_parameter
!= NULL
&& named_parameter
->is_variable());
2418 // Build the call. Note that the field names are the same as the
2419 // ones used in build_struct.
2420 Expression
* thunk_parameter
= Expression::make_var_reference(named_parameter
,
2422 thunk_parameter
= Expression::make_unary(OPERATOR_MULT
, thunk_parameter
,
2425 Interface_field_reference_expression
* interface_method
=
2426 ce
->fn()->interface_field_reference_expression();
2428 Expression
* func_to_call
;
2429 unsigned int next_index
;
2430 if (this->is_constant_function())
2432 func_to_call
= ce
->fn();
2437 func_to_call
= Expression::make_field_reference(thunk_parameter
,
2442 if (interface_method
!= NULL
)
2444 // The main program passes the interface object.
2445 go_assert(next_index
== 0);
2446 Expression
* r
= Expression::make_field_reference(thunk_parameter
, 0,
2448 const std::string
& name(interface_method
->name());
2449 func_to_call
= Expression::make_interface_field_reference(r
, name
,
2454 Expression_list
* call_params
= new Expression_list();
2455 const Struct_field_list
* fields
= this->struct_type_
->fields();
2456 Struct_field_list::const_iterator p
= fields
->begin();
2457 for (unsigned int i
= 0; i
< next_index
; ++i
)
2459 bool is_recover_call
= ce
->is_recover_call();
2460 Expression
* recover_arg
= NULL
;
2462 const Expression_list
* args
= ce
->args();
2465 for (Expression_list::const_iterator arg
= args
->begin();
2470 if ((*arg
)->is_constant())
2474 Expression
* thunk_param
=
2475 Expression::make_var_reference(named_parameter
, location
);
2477 Expression::make_unary(OPERATOR_MULT
, thunk_param
, location
);
2478 param
= Expression::make_field_reference(thunk_param
,
2484 if (!is_recover_call
)
2485 call_params
->push_back(param
);
2488 go_assert(call_params
->empty());
2489 recover_arg
= param
;
2494 if (call_params
->empty())
2500 Call_expression
* call
= Expression::make_call(func_to_call
, call_params
,
2503 // This call expression was already lowered before entering the
2504 // thunk statement. Don't try to lower varargs again, as that will
2505 // cause confusion for, e.g., method calls which already have a
2506 // receiver parameter.
2507 call
->set_varargs_are_lowered();
2509 Statement
* call_statement
= Statement::make_statement(call
, true);
2511 gogo
->add_statement(call_statement
);
2513 // If this is a defer statement, the label comes immediately after
2515 if (may_call_recover
)
2517 gogo
->add_label_definition("retaddr", location
);
2519 Expression_list
* vals
= new Expression_list();
2520 vals
->push_back(Expression::make_boolean(false, location
));
2521 gogo
->add_statement(Statement::make_return_statement(vals
, location
));
2524 Block
* b
= gogo
->finish_block(location
);
2526 gogo
->add_block(b
, location
);
2528 gogo
->lower_block(function
, b
);
2530 // We already ran the determine_types pass, so we need to run it
2531 // just for the call statement now. The other types are known.
2532 call_statement
->determine_types();
2534 gogo
->flatten_block(function
, b
);
2536 if (may_call_recover
2537 || recover_arg
!= NULL
2538 || this->classification() == STATEMENT_GO
)
2540 // Dig up the call expression, which may have been changed
2542 go_assert(call_statement
->classification() == STATEMENT_EXPRESSION
);
2543 Expression_statement
* es
=
2544 static_cast<Expression_statement
*>(call_statement
);
2545 Call_expression
* ce
= es
->expr()->call_expression();
2547 go_assert(saw_errors());
2550 if (may_call_recover
)
2551 ce
->set_is_deferred();
2552 if (this->classification() == STATEMENT_GO
)
2553 ce
->set_is_concurrent();
2554 if (recover_arg
!= NULL
)
2555 ce
->set_recover_arg(recover_arg
);
2559 // That is all the thunk has to do.
2560 gogo
->finish_function(location
);
2563 // Get the function and argument expressions.
2566 Thunk_statement::get_fn_and_arg(Expression
** pfn
, Expression
** parg
)
2568 if (this->call_
->is_error_expression())
2571 Call_expression
* ce
= this->call_
->call_expression();
2573 Expression
* fn
= ce
->fn();
2574 Func_expression
* fe
= fn
->func_expression();
2575 go_assert(fe
!= NULL
);
2576 *pfn
= Expression::make_func_code_reference(fe
->named_object(),
2579 const Expression_list
* args
= ce
->args();
2580 if (args
== NULL
|| args
->empty())
2581 *parg
= Expression::make_nil(this->location());
2584 go_assert(args
->size() == 1);
2585 *parg
= args
->front();
2591 // Class Go_statement.
2594 Go_statement::do_get_backend(Translate_context
* context
)
2598 if (!this->get_fn_and_arg(&fn
, &arg
))
2599 return context
->backend()->error_statement();
2601 Expression
* call
= Runtime::make_call(Runtime::GO
, this->location(), 2,
2603 Bexpression
* bcall
= call
->get_backend(context
);
2604 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
2605 return context
->backend()->expression_statement(bfunction
, bcall
);
2608 // Dump the AST representation for go statement.
2611 Go_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2613 ast_dump_context
->print_indent();
2614 ast_dump_context
->ostream() << "go ";
2615 ast_dump_context
->dump_expression(this->call());
2616 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2619 // Make a go statement.
2622 Statement::make_go_statement(Call_expression
* call
, Location location
)
2624 return new Go_statement(call
, location
);
2627 // Class Defer_statement.
2630 Defer_statement::do_get_backend(Translate_context
* context
)
2634 if (!this->get_fn_and_arg(&fn
, &arg
))
2635 return context
->backend()->error_statement();
2637 Location loc
= this->location();
2638 Expression
* ds
= context
->function()->func_value()->defer_stack(loc
);
2640 Expression
* call
= Runtime::make_call(Runtime::DEFERPROC
, loc
, 3,
2642 Bexpression
* bcall
= call
->get_backend(context
);
2643 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
2644 return context
->backend()->expression_statement(bfunction
, bcall
);
2647 // Dump the AST representation for defer statement.
2650 Defer_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2652 ast_dump_context
->print_indent();
2653 ast_dump_context
->ostream() << "defer ";
2654 ast_dump_context
->dump_expression(this->call());
2655 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2658 // Make a defer statement.
2661 Statement::make_defer_statement(Call_expression
* call
,
2664 return new Defer_statement(call
, location
);
2667 // Class Return_statement.
2669 // Traverse assignments. We treat each return value as a top level
2670 // RHS in an expression.
2673 Return_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
2675 Expression_list
* vals
= this->vals_
;
2678 for (Expression_list::iterator p
= vals
->begin();
2681 tassign
->value(&*p
, true, true);
2686 // Lower a return statement. If we are returning a function call
2687 // which returns multiple values which match the current function,
2688 // split up the call's results. If the return statement lists
2689 // explicit values, implement this statement by assigning the values
2690 // to the result variables and change this statement to a naked
2691 // return. This lets panic/recover work correctly.
2694 Return_statement::do_lower(Gogo
*, Named_object
* function
, Block
* enclosing
,
2695 Statement_inserter
*)
2697 if (this->is_lowered_
)
2700 Expression_list
* vals
= this->vals_
;
2702 this->is_lowered_
= true;
2704 Location loc
= this->location();
2706 size_t vals_count
= vals
== NULL
? 0 : vals
->size();
2707 Function::Results
* results
= function
->func_value()->result_variables();
2708 size_t results_count
= results
== NULL
? 0 : results
->size();
2710 if (vals_count
== 0)
2712 if (results_count
> 0 && !function
->func_value()->results_are_named())
2714 this->report_error(_("not enough arguments to return"));
2720 if (results_count
== 0)
2722 this->report_error(_("return with value in function "
2723 "with no return type"));
2727 // If the current function has multiple return values, and we are
2728 // returning a single call expression, split up the call expression.
2729 if (results_count
> 1
2730 && vals
->size() == 1
2731 && vals
->front()->call_expression() != NULL
)
2733 Call_expression
* call
= vals
->front()->call_expression();
2734 call
->set_expected_result_count(results_count
);
2736 vals
= new Expression_list
;
2737 for (size_t i
= 0; i
< results_count
; ++i
)
2738 vals
->push_back(Expression::make_call_result(call
, i
));
2739 vals_count
= results_count
;
2742 if (vals_count
< results_count
)
2744 this->report_error(_("not enough arguments to return"));
2748 if (vals_count
> results_count
)
2750 this->report_error(_("too many values in return statement"));
2754 Block
* b
= new Block(enclosing
, loc
);
2756 Expression_list
* lhs
= new Expression_list();
2757 Expression_list
* rhs
= new Expression_list();
2759 Expression_list::const_iterator pe
= vals
->begin();
2761 for (Function::Results::const_iterator pr
= results
->begin();
2762 pr
!= results
->end();
2765 Named_object
* rv
= *pr
;
2766 Expression
* e
= *pe
;
2768 // Check types now so that we give a good error message. The
2769 // result type is known. We determine the expression type
2772 Type
*rvtype
= rv
->result_var_value()->type();
2773 Type_context
type_context(rvtype
, false);
2774 e
->determine_type(&type_context
);
2777 if (Type::are_assignable(rvtype
, e
->type(), &reason
))
2779 Expression
* ve
= Expression::make_var_reference(rv
, e
->location());
2786 go_error_at(e
->location(),
2787 "incompatible type for return value %d", i
);
2789 go_error_at(e
->location(),
2790 "incompatible type for return value %d (%s)",
2794 go_assert(lhs
->size() == rhs
->size());
2798 else if (lhs
->size() == 1)
2800 b
->add_statement(Statement::make_assignment(lhs
->front(), rhs
->front(),
2806 b
->add_statement(Statement::make_tuple_assignment(lhs
, rhs
, loc
));
2808 b
->add_statement(this);
2812 return Statement::make_block_statement(b
, loc
);
2815 // Convert a return statement to the backend representation.
2818 Return_statement::do_get_backend(Translate_context
* context
)
2820 Location loc
= this->location();
2822 Function
* function
= context
->function()->func_value();
2823 Function::Results
* results
= function
->result_variables();
2824 std::vector
<Bexpression
*> retvals
;
2825 if (results
!= NULL
&& !results
->empty())
2827 retvals
.reserve(results
->size());
2828 for (Function::Results::const_iterator p
= results
->begin();
2829 p
!= results
->end();
2832 Expression
* vr
= Expression::make_var_reference(*p
, loc
);
2833 retvals
.push_back(vr
->get_backend(context
));
2837 return context
->backend()->return_statement(function
->get_decl(),
2841 // Dump the AST representation for a return statement.
2844 Return_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2846 ast_dump_context
->print_indent();
2847 ast_dump_context
->ostream() << "return " ;
2848 ast_dump_context
->dump_expression_list(this->vals_
);
2849 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2852 // Make a return statement.
2855 Statement::make_return_statement(Expression_list
* vals
,
2858 return new Return_statement(vals
, location
);
2861 // Make a statement that returns the result of a call expression.
2864 Statement::make_return_from_call(Call_expression
* call
, Location location
)
2866 size_t rc
= call
->result_count();
2868 return Statement::make_statement(call
, true);
2871 Expression_list
* vals
= new Expression_list();
2873 vals
->push_back(call
);
2876 for (size_t i
= 0; i
< rc
; ++i
)
2877 vals
->push_back(Expression::make_call_result(call
, i
));
2879 return Statement::make_return_statement(vals
, location
);
2883 // A break or continue statement.
2885 class Bc_statement
: public Statement
2888 Bc_statement(bool is_break
, Unnamed_label
* label
, Location location
)
2889 : Statement(STATEMENT_BREAK_OR_CONTINUE
, location
),
2890 label_(label
), is_break_(is_break
)
2895 { return this->is_break_
; }
2899 do_traverse(Traverse
*)
2900 { return TRAVERSE_CONTINUE
; }
2903 do_may_fall_through() const
2907 do_get_backend(Translate_context
* context
)
2908 { return this->label_
->get_goto(context
, this->location()); }
2911 do_dump_statement(Ast_dump_context
*) const;
2914 // The label that this branches to.
2915 Unnamed_label
* label_
;
2916 // True if this is "break", false if it is "continue".
2920 // Dump the AST representation for a break/continue statement
2923 Bc_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2925 ast_dump_context
->print_indent();
2926 ast_dump_context
->ostream() << (this->is_break_
? "break" : "continue");
2927 if (this->label_
!= NULL
)
2929 ast_dump_context
->ostream() << " ";
2930 ast_dump_context
->dump_label_name(this->label_
);
2932 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2935 // Make a break statement.
2938 Statement::make_break_statement(Unnamed_label
* label
, Location location
)
2940 return new Bc_statement(true, label
, location
);
2943 // Make a continue statement.
2946 Statement::make_continue_statement(Unnamed_label
* label
,
2949 return new Bc_statement(false, label
, location
);
2952 // Class Goto_statement.
2955 Goto_statement::do_traverse(Traverse
*)
2957 return TRAVERSE_CONTINUE
;
2960 // Check types for a label. There aren't any types per se, but we use
2961 // this to give an error if the label was never defined.
2964 Goto_statement::do_check_types(Gogo
*)
2966 if (!this->label_
->is_defined())
2968 go_error_at(this->location(), "reference to undefined label %qs",
2969 Gogo::message_name(this->label_
->name()).c_str());
2970 this->set_is_error();
2974 // Convert the goto statement to the backend representation.
2977 Goto_statement::do_get_backend(Translate_context
* context
)
2979 Blabel
* blabel
= this->label_
->get_backend_label(context
);
2980 return context
->backend()->goto_statement(blabel
, this->location());
2983 // Dump the AST representation for a goto statement.
2986 Goto_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2988 ast_dump_context
->print_indent();
2989 ast_dump_context
->ostream() << "goto " << this->label_
->name() << dsuffix(location()) << std::endl
;
2992 // Make a goto statement.
2995 Statement::make_goto_statement(Label
* label
, Location location
)
2997 return new Goto_statement(label
, location
);
3000 // Class Goto_unnamed_statement.
3003 Goto_unnamed_statement::do_traverse(Traverse
*)
3005 return TRAVERSE_CONTINUE
;
3008 // Convert the goto unnamed statement to the backend representation.
3011 Goto_unnamed_statement::do_get_backend(Translate_context
* context
)
3013 return this->label_
->get_goto(context
, this->location());
3016 // Dump the AST representation for an unnamed goto statement
3019 Goto_unnamed_statement::do_dump_statement(
3020 Ast_dump_context
* ast_dump_context
) const
3022 ast_dump_context
->print_indent();
3023 ast_dump_context
->ostream() << "goto ";
3024 ast_dump_context
->dump_label_name(this->label_
);
3025 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
3028 // Make a goto statement to an unnamed label.
3031 Statement::make_goto_unnamed_statement(Unnamed_label
* label
,
3034 return new Goto_unnamed_statement(label
, location
);
3037 // Class Label_statement.
3042 Label_statement::do_traverse(Traverse
*)
3044 return TRAVERSE_CONTINUE
;
3047 // Return the backend representation of the statement defining this
3051 Label_statement::do_get_backend(Translate_context
* context
)
3053 if (this->label_
->is_dummy_label())
3055 Bexpression
* bce
= context
->backend()->boolean_constant_expression(false);
3056 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
3057 return context
->backend()->expression_statement(bfunction
, bce
);
3059 Blabel
* blabel
= this->label_
->get_backend_label(context
);
3060 return context
->backend()->label_definition_statement(blabel
);
3063 // Dump the AST for a label definition statement.
3066 Label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3068 ast_dump_context
->print_indent();
3069 ast_dump_context
->ostream() << this->label_
->name() << ":" << dsuffix(location()) << std::endl
;
3072 // Make a label statement.
3075 Statement::make_label_statement(Label
* label
, Location location
)
3077 return new Label_statement(label
, location
);
3080 // Class Unnamed_label_statement.
3082 Unnamed_label_statement::Unnamed_label_statement(Unnamed_label
* label
)
3083 : Statement(STATEMENT_UNNAMED_LABEL
, label
->location()),
3088 Unnamed_label_statement::do_traverse(Traverse
*)
3090 return TRAVERSE_CONTINUE
;
3093 // Get the backend definition for this unnamed label statement.
3096 Unnamed_label_statement::do_get_backend(Translate_context
* context
)
3098 return this->label_
->get_definition(context
);
3101 // Dump the AST representation for an unnamed label definition statement.
3104 Unnamed_label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3107 ast_dump_context
->print_indent();
3108 ast_dump_context
->dump_label_name(this->label_
);
3109 ast_dump_context
->ostream() << ":" << dsuffix(location()) << std::endl
;
3112 // Make an unnamed label statement.
3115 Statement::make_unnamed_label_statement(Unnamed_label
* label
)
3117 return new Unnamed_label_statement(label
);
3120 // Class If_statement.
3125 If_statement::do_traverse(Traverse
* traverse
)
3127 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
3128 || this->then_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3129 return TRAVERSE_EXIT
;
3130 if (this->else_block_
!= NULL
)
3132 if (this->else_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3133 return TRAVERSE_EXIT
;
3135 return TRAVERSE_CONTINUE
;
3139 If_statement::do_determine_types()
3141 Type_context
context(Type::lookup_bool_type(), false);
3142 this->cond_
->determine_type(&context
);
3143 this->then_block_
->determine_types();
3144 if (this->else_block_
!= NULL
)
3145 this->else_block_
->determine_types();
3151 If_statement::do_check_types(Gogo
*)
3153 Type
* type
= this->cond_
->type();
3154 if (type
->is_error())
3155 this->set_is_error();
3156 else if (!type
->is_boolean_type())
3157 this->report_error(_("expected boolean expression"));
3160 // Whether the overall statement may fall through.
3163 If_statement::do_may_fall_through() const
3165 return (this->else_block_
== NULL
3166 || this->then_block_
->may_fall_through()
3167 || this->else_block_
->may_fall_through());
3170 // Get the backend representation.
3173 If_statement::do_get_backend(Translate_context
* context
)
3175 go_assert(this->cond_
->type()->is_boolean_type()
3176 || this->cond_
->type()->is_error());
3177 Bexpression
* cond
= this->cond_
->get_backend(context
);
3178 Bblock
* then_block
= this->then_block_
->get_backend(context
);
3179 Bblock
* else_block
= (this->else_block_
== NULL
3181 : this->else_block_
->get_backend(context
));
3182 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
3183 return context
->backend()->if_statement(bfunction
,
3184 cond
, then_block
, else_block
,
3188 // Dump the AST representation for an if statement
3191 If_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3193 ast_dump_context
->print_indent();
3194 ast_dump_context
->ostream() << "if ";
3195 ast_dump_context
->dump_expression(this->cond_
);
3196 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
3197 if (ast_dump_context
->dump_subblocks())
3199 ast_dump_context
->dump_block(this->then_block_
);
3200 if (this->else_block_
!= NULL
)
3202 ast_dump_context
->print_indent();
3203 ast_dump_context
->ostream() << "else" << std::endl
;
3204 ast_dump_context
->dump_block(this->else_block_
);
3209 // Make an if statement.
3212 Statement::make_if_statement(Expression
* cond
, Block
* then_block
,
3213 Block
* else_block
, Location location
)
3215 return new If_statement(cond
, then_block
, else_block
, location
);
3218 // Class Case_clauses::Hash_integer_value.
3220 class Case_clauses::Hash_integer_value
3224 operator()(Expression
*) const;
3228 Case_clauses::Hash_integer_value::operator()(Expression
* pe
) const
3230 Numeric_constant nc
;
3232 if (!pe
->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3234 size_t ret
= mpz_get_ui(ival
);
3239 // Class Case_clauses::Eq_integer_value.
3241 class Case_clauses::Eq_integer_value
3245 operator()(Expression
*, Expression
*) const;
3249 Case_clauses::Eq_integer_value::operator()(Expression
* a
, Expression
* b
) const
3251 Numeric_constant anc
;
3253 Numeric_constant bnc
;
3255 if (!a
->numeric_constant_value(&anc
)
3256 || !anc
.to_int(&aval
)
3257 || !b
->numeric_constant_value(&bnc
)
3258 || !bnc
.to_int(&bval
))
3260 bool ret
= mpz_cmp(aval
, bval
) == 0;
3266 // Class Case_clauses::Case_clause.
3271 Case_clauses::Case_clause::traverse(Traverse
* traverse
)
3273 if (this->cases_
!= NULL
3274 && (traverse
->traverse_mask()
3275 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3277 if (this->cases_
->traverse(traverse
) == TRAVERSE_EXIT
)
3278 return TRAVERSE_EXIT
;
3280 if (this->statements_
!= NULL
)
3282 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
3283 return TRAVERSE_EXIT
;
3285 return TRAVERSE_CONTINUE
;
3288 // Check whether all the case expressions are integer constants.
3291 Case_clauses::Case_clause::is_constant() const
3293 if (this->cases_
!= NULL
)
3295 for (Expression_list::const_iterator p
= this->cases_
->begin();
3296 p
!= this->cases_
->end();
3298 if (!(*p
)->is_constant() || (*p
)->type()->integer_type() == NULL
)
3304 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3305 // value we are switching on; it may be NULL. If START_LABEL is not
3306 // NULL, it goes at the start of the statements, after the condition
3307 // test. We branch to FINISH_LABEL at the end of the statements.
3310 Case_clauses::Case_clause::lower(Block
* b
, Temporary_statement
* val_temp
,
3311 Unnamed_label
* start_label
,
3312 Unnamed_label
* finish_label
) const
3314 Location loc
= this->location_
;
3315 Unnamed_label
* next_case_label
;
3316 if (this->cases_
== NULL
|| this->cases_
->empty())
3318 go_assert(this->is_default_
);
3319 next_case_label
= NULL
;
3323 Expression
* cond
= NULL
;
3325 for (Expression_list::const_iterator p
= this->cases_
->begin();
3326 p
!= this->cases_
->end();
3329 Expression
* ref
= Expression::make_temporary_reference(val_temp
,
3331 Expression
* this_cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3336 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, this_cond
, loc
);
3339 Block
* then_block
= new Block(b
, loc
);
3340 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3341 Statement
* s
= Statement::make_goto_unnamed_statement(next_case_label
,
3343 then_block
->add_statement(s
);
3345 // if !COND { goto NEXT_CASE_LABEL }
3346 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3347 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
3348 b
->add_statement(s
);
3351 if (start_label
!= NULL
)
3352 b
->add_statement(Statement::make_unnamed_label_statement(start_label
));
3354 if (this->statements_
!= NULL
)
3355 b
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
3357 Statement
* s
= Statement::make_goto_unnamed_statement(finish_label
, loc
);
3358 b
->add_statement(s
);
3360 if (next_case_label
!= NULL
)
3361 b
->add_statement(Statement::make_unnamed_label_statement(next_case_label
));
3367 Case_clauses::Case_clause::determine_types(Type
* type
)
3369 if (this->cases_
!= NULL
)
3371 Type_context
case_context(type
, false);
3372 for (Expression_list::iterator p
= this->cases_
->begin();
3373 p
!= this->cases_
->end();
3375 (*p
)->determine_type(&case_context
);
3377 if (this->statements_
!= NULL
)
3378 this->statements_
->determine_types();
3381 // Check types. Returns false if there was an error.
3384 Case_clauses::Case_clause::check_types(Type
* type
)
3386 if (this->cases_
!= NULL
)
3388 for (Expression_list::iterator p
= this->cases_
->begin();
3389 p
!= this->cases_
->end();
3392 if (!Type::are_assignable(type
, (*p
)->type(), NULL
)
3393 && !Type::are_assignable((*p
)->type(), type
, NULL
))
3395 go_error_at((*p
)->location(),
3396 "type mismatch between switch value and case clause");
3404 // Return true if this clause may fall through to the following
3405 // statements. Note that this is not the same as whether the case
3406 // uses the "fallthrough" keyword.
3409 Case_clauses::Case_clause::may_fall_through() const
3411 if (this->statements_
== NULL
)
3413 return this->statements_
->may_fall_through();
3416 // Convert the case values and statements to the backend
3417 // representation. BREAK_LABEL is the label which break statements
3418 // should branch to. CASE_CONSTANTS is used to detect duplicate
3419 // constants. *CASES should be passed as an empty vector; the values
3420 // for this case will be added to it. If this is the default case,
3421 // *CASES will remain empty. This returns the statement to execute if
3422 // one of these cases is selected.
3425 Case_clauses::Case_clause::get_backend(Translate_context
* context
,
3426 Unnamed_label
* break_label
,
3427 Case_constants
* case_constants
,
3428 std::vector
<Bexpression
*>* cases
) const
3430 if (this->cases_
!= NULL
)
3432 go_assert(!this->is_default_
);
3433 for (Expression_list::const_iterator p
= this->cases_
->begin();
3434 p
!= this->cases_
->end();
3438 if (e
->classification() != Expression::EXPRESSION_INTEGER
)
3440 Numeric_constant nc
;
3442 if (!(*p
)->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3444 // Something went wrong. This can happen with a
3445 // negative constant and an unsigned switch value.
3446 go_assert(saw_errors());
3449 go_assert(nc
.type() != NULL
);
3450 e
= Expression::make_integer_z(&ival
, nc
.type(), e
->location());
3454 std::pair
<Case_constants::iterator
, bool> ins
=
3455 case_constants
->insert(e
);
3458 // Value was already present.
3459 go_error_at(this->location_
, "duplicate case in switch");
3460 e
= Expression::make_error(this->location_
);
3462 cases
->push_back(e
->get_backend(context
));
3466 Bstatement
* statements
;
3467 if (this->statements_
== NULL
)
3471 Bblock
* bblock
= this->statements_
->get_backend(context
);
3472 statements
= context
->backend()->block_statement(bblock
);
3475 Bstatement
* break_stat
;
3476 if (this->is_fallthrough_
)
3479 break_stat
= break_label
->get_goto(context
, this->location_
);
3481 if (statements
== NULL
)
3483 else if (break_stat
== NULL
)
3486 return context
->backend()->compound_statement(statements
, break_stat
);
3489 // Dump the AST representation for a case clause
3492 Case_clauses::Case_clause::dump_clause(Ast_dump_context
* ast_dump_context
)
3495 ast_dump_context
->print_indent();
3496 if (this->is_default_
)
3498 ast_dump_context
->ostream() << "default:";
3502 ast_dump_context
->ostream() << "case ";
3503 ast_dump_context
->dump_expression_list(this->cases_
);
3504 ast_dump_context
->ostream() << ":" ;
3506 ast_dump_context
->dump_block(this->statements_
);
3507 if (this->is_fallthrough_
)
3509 ast_dump_context
->print_indent();
3510 ast_dump_context
->ostream() << " (fallthrough)" << dsuffix(location()) << std::endl
;
3514 // Class Case_clauses.
3519 Case_clauses::traverse(Traverse
* traverse
)
3521 for (Clauses::iterator p
= this->clauses_
.begin();
3522 p
!= this->clauses_
.end();
3525 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
3526 return TRAVERSE_EXIT
;
3528 return TRAVERSE_CONTINUE
;
3531 // Check whether all the case expressions are constant.
3534 Case_clauses::is_constant() const
3536 for (Clauses::const_iterator p
= this->clauses_
.begin();
3537 p
!= this->clauses_
.end();
3539 if (!p
->is_constant())
3544 // Lower case clauses for a nonconstant switch.
3547 Case_clauses::lower(Block
* b
, Temporary_statement
* val_temp
,
3548 Unnamed_label
* break_label
) const
3550 // The default case.
3551 const Case_clause
* default_case
= NULL
;
3553 // The label for the fallthrough of the previous case.
3554 Unnamed_label
* last_fallthrough_label
= NULL
;
3556 // The label for the start of the default case. This is used if the
3557 // case before the default case falls through.
3558 Unnamed_label
* default_start_label
= NULL
;
3560 // The label for the end of the default case. This normally winds
3561 // up as BREAK_LABEL, but it will be different if the default case
3563 Unnamed_label
* default_finish_label
= NULL
;
3565 for (Clauses::const_iterator p
= this->clauses_
.begin();
3566 p
!= this->clauses_
.end();
3569 // The label to use for the start of the statements for this
3570 // case. This is NULL unless the previous case falls through.
3571 Unnamed_label
* start_label
= last_fallthrough_label
;
3573 // The label to jump to after the end of the statements for this
3575 Unnamed_label
* finish_label
= break_label
;
3577 last_fallthrough_label
= NULL
;
3578 if (p
->is_fallthrough() && p
+ 1 != this->clauses_
.end())
3580 finish_label
= new Unnamed_label(p
->location());
3581 last_fallthrough_label
= finish_label
;
3584 if (!p
->is_default())
3585 p
->lower(b
, val_temp
, start_label
, finish_label
);
3588 // We have to move the default case to the end, so that we
3589 // only use it if all the other tests fail.
3591 default_start_label
= start_label
;
3592 default_finish_label
= finish_label
;
3596 if (default_case
!= NULL
)
3597 default_case
->lower(b
, val_temp
, default_start_label
,
3598 default_finish_label
);
3604 Case_clauses::determine_types(Type
* type
)
3606 for (Clauses::iterator p
= this->clauses_
.begin();
3607 p
!= this->clauses_
.end();
3609 p
->determine_types(type
);
3612 // Check types. Returns false if there was an error.
3615 Case_clauses::check_types(Type
* type
)
3618 for (Clauses::iterator p
= this->clauses_
.begin();
3619 p
!= this->clauses_
.end();
3622 if (!p
->check_types(type
))
3628 // Return true if these clauses may fall through to the statements
3629 // following the switch statement.
3632 Case_clauses::may_fall_through() const
3634 bool found_default
= false;
3635 for (Clauses::const_iterator p
= this->clauses_
.begin();
3636 p
!= this->clauses_
.end();
3639 if (p
->may_fall_through() && !p
->is_fallthrough())
3641 if (p
->is_default())
3642 found_default
= true;
3644 return !found_default
;
3647 // Convert the cases to the backend representation. This sets
3648 // *ALL_CASES and *ALL_STATEMENTS.
3651 Case_clauses::get_backend(Translate_context
* context
,
3652 Unnamed_label
* break_label
,
3653 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
3654 std::vector
<Bstatement
*>* all_statements
) const
3656 Case_constants case_constants
;
3658 size_t c
= this->clauses_
.size();
3659 all_cases
->resize(c
);
3660 all_statements
->resize(c
);
3663 for (Clauses::const_iterator p
= this->clauses_
.begin();
3664 p
!= this->clauses_
.end();
3667 std::vector
<Bexpression
*> cases
;
3668 Bstatement
* stat
= p
->get_backend(context
, break_label
, &case_constants
,
3670 (*all_cases
)[i
].swap(cases
);
3671 (*all_statements
)[i
] = stat
;
3675 // Dump the AST representation for case clauses (from a switch statement)
3678 Case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
3680 for (Clauses::const_iterator p
= this->clauses_
.begin();
3681 p
!= this->clauses_
.end();
3683 p
->dump_clause(ast_dump_context
);
3686 // A constant switch statement. A Switch_statement is lowered to this
3687 // when all the cases are constants.
3689 class Constant_switch_statement
: public Statement
3692 Constant_switch_statement(Expression
* val
, Case_clauses
* clauses
,
3693 Unnamed_label
* break_label
,
3695 : Statement(STATEMENT_CONSTANT_SWITCH
, location
),
3696 val_(val
), clauses_(clauses
), break_label_(break_label
)
3701 do_traverse(Traverse
*);
3704 do_determine_types();
3707 do_check_types(Gogo
*);
3710 do_get_backend(Translate_context
*);
3713 do_dump_statement(Ast_dump_context
*) const;
3716 // The value to switch on.
3718 // The case clauses.
3719 Case_clauses
* clauses_
;
3720 // The break label, if needed.
3721 Unnamed_label
* break_label_
;
3727 Constant_switch_statement::do_traverse(Traverse
* traverse
)
3729 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3730 return TRAVERSE_EXIT
;
3731 return this->clauses_
->traverse(traverse
);
3737 Constant_switch_statement::do_determine_types()
3739 this->val_
->determine_type_no_context();
3740 this->clauses_
->determine_types(this->val_
->type());
3746 Constant_switch_statement::do_check_types(Gogo
*)
3748 if (!this->clauses_
->check_types(this->val_
->type()))
3749 this->set_is_error();
3752 // Convert to GENERIC.
3755 Constant_switch_statement::do_get_backend(Translate_context
* context
)
3757 Bexpression
* switch_val_expr
= this->val_
->get_backend(context
);
3759 Unnamed_label
* break_label
= this->break_label_
;
3760 if (break_label
== NULL
)
3761 break_label
= new Unnamed_label(this->location());
3763 std::vector
<std::vector
<Bexpression
*> > all_cases
;
3764 std::vector
<Bstatement
*> all_statements
;
3765 this->clauses_
->get_backend(context
, break_label
, &all_cases
,
3768 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
3769 Bstatement
* switch_statement
;
3770 switch_statement
= context
->backend()->switch_statement(bfunction
,
3775 Bstatement
* ldef
= break_label
->get_definition(context
);
3776 return context
->backend()->compound_statement(switch_statement
, ldef
);
3779 // Dump the AST representation for a constant switch statement.
3782 Constant_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3785 ast_dump_context
->print_indent();
3786 ast_dump_context
->ostream() << "switch ";
3787 ast_dump_context
->dump_expression(this->val_
);
3789 if (ast_dump_context
->dump_subblocks())
3791 ast_dump_context
->ostream() << " {" << std::endl
;
3792 this->clauses_
->dump_clauses(ast_dump_context
);
3793 ast_dump_context
->ostream() << "}";
3796 ast_dump_context
->ostream() << std::endl
;
3799 // Class Switch_statement.
3804 Switch_statement::do_traverse(Traverse
* traverse
)
3806 if (this->val_
!= NULL
)
3808 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3809 return TRAVERSE_EXIT
;
3811 return this->clauses_
->traverse(traverse
);
3814 // Lower a Switch_statement to a Constant_switch_statement or a series
3815 // of if statements.
3818 Switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
3819 Statement_inserter
*)
3821 Location loc
= this->location();
3823 if (this->val_
!= NULL
3824 && (this->val_
->is_error_expression()
3825 || this->val_
->type()->is_error()))
3827 go_assert(saw_errors());
3828 return Statement::make_error_statement(loc
);
3831 if (this->val_
!= NULL
3832 && this->val_
->type()->integer_type() != NULL
3833 && !this->clauses_
->empty()
3834 && this->clauses_
->is_constant())
3835 return new Constant_switch_statement(this->val_
, this->clauses_
,
3836 this->break_label_
, loc
);
3838 if (this->val_
!= NULL
3839 && !this->val_
->type()->is_comparable()
3840 && !Type::are_compatible_for_comparison(true, this->val_
->type(),
3841 Type::make_nil_type(), NULL
))
3843 go_error_at(this->val_
->location(),
3844 "cannot switch on value whose type that may not be compared");
3845 return Statement::make_error_statement(loc
);
3848 Block
* b
= new Block(enclosing
, loc
);
3850 if (this->clauses_
->empty())
3852 Expression
* val
= this->val_
;
3854 val
= Expression::make_boolean(true, loc
);
3855 return Statement::make_statement(val
, true);
3858 // var val_temp VAL_TYPE = VAL
3859 Expression
* val
= this->val_
;
3861 val
= Expression::make_boolean(true, loc
);
3863 Type
* type
= val
->type();
3864 if (type
->is_abstract())
3865 type
= type
->make_non_abstract_type();
3866 Temporary_statement
* val_temp
= Statement::make_temporary(type
, val
, loc
);
3867 b
->add_statement(val_temp
);
3869 this->clauses_
->lower(b
, val_temp
, this->break_label());
3871 Statement
* s
= Statement::make_unnamed_label_statement(this->break_label_
);
3872 b
->add_statement(s
);
3874 return Statement::make_block_statement(b
, loc
);
3877 // Return the break label for this switch statement, creating it if
3881 Switch_statement::break_label()
3883 if (this->break_label_
== NULL
)
3884 this->break_label_
= new Unnamed_label(this->location());
3885 return this->break_label_
;
3888 // Dump the AST representation for a switch statement.
3891 Switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3893 ast_dump_context
->print_indent();
3894 ast_dump_context
->ostream() << "switch ";
3895 if (this->val_
!= NULL
)
3897 ast_dump_context
->dump_expression(this->val_
);
3899 if (ast_dump_context
->dump_subblocks())
3901 ast_dump_context
->ostream() << " {" << dsuffix(location()) << std::endl
;
3902 this->clauses_
->dump_clauses(ast_dump_context
);
3903 ast_dump_context
->print_indent();
3904 ast_dump_context
->ostream() << "}";
3906 ast_dump_context
->ostream() << std::endl
;
3909 // Return whether this switch may fall through.
3912 Switch_statement::do_may_fall_through() const
3914 if (this->clauses_
== NULL
)
3917 // If we have a break label, then some case needed it. That implies
3918 // that the switch statement as a whole can fall through.
3919 if (this->break_label_
!= NULL
)
3922 return this->clauses_
->may_fall_through();
3925 // Make a switch statement.
3928 Statement::make_switch_statement(Expression
* val
, Location location
)
3930 return new Switch_statement(val
, location
);
3933 // Class Type_case_clauses::Type_case_clause.
3938 Type_case_clauses::Type_case_clause::traverse(Traverse
* traverse
)
3940 if (!this->is_default_
3941 && ((traverse
->traverse_mask()
3942 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3943 && Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3944 return TRAVERSE_EXIT
;
3945 if (this->statements_
!= NULL
)
3946 return this->statements_
->traverse(traverse
);
3947 return TRAVERSE_CONTINUE
;
3950 // Lower one clause in a type switch. Add statements to the block B.
3951 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3952 // BREAK_LABEL is the label at the end of the type switch.
3953 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3957 Type_case_clauses::Type_case_clause::lower(Type
* switch_val_type
,
3959 Temporary_statement
* descriptor_temp
,
3960 Unnamed_label
* break_label
,
3961 Unnamed_label
** stmts_label
) const
3963 Location loc
= this->location_
;
3965 Unnamed_label
* next_case_label
= NULL
;
3966 if (!this->is_default_
)
3968 Type
* type
= this->type_
;
3971 if (switch_val_type
->interface_type() != NULL
3972 && !type
->is_nil_constant_as_type()
3973 && type
->interface_type() == NULL
3974 && !switch_val_type
->interface_type()->implements_interface(type
,
3978 go_error_at(this->location_
, "impossible type switch case");
3980 go_error_at(this->location_
, "impossible type switch case (%s)",
3984 Expression
* ref
= Expression::make_temporary_reference(descriptor_temp
,
3988 // The language permits case nil, which is of course a constant
3989 // rather than a type. It will appear here as an invalid
3991 if (type
->is_nil_constant_as_type())
3992 cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3993 Expression::make_nil(loc
),
3996 cond
= Runtime::make_call((type
->interface_type() == NULL
3997 ? Runtime::IFACETYPEEQ
3998 : Runtime::IFACET2IP
),
4000 Expression::make_type_descriptor(type
, loc
),
4003 Unnamed_label
* dest
;
4004 if (!this->is_fallthrough_
)
4006 // if !COND { goto NEXT_CASE_LABEL }
4007 next_case_label
= new Unnamed_label(Linemap::unknown_location());
4008 dest
= next_case_label
;
4009 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
4013 // if COND { goto STMTS_LABEL }
4014 go_assert(stmts_label
!= NULL
);
4015 if (*stmts_label
== NULL
)
4016 *stmts_label
= new Unnamed_label(Linemap::unknown_location());
4017 dest
= *stmts_label
;
4019 Block
* then_block
= new Block(b
, loc
);
4020 Statement
* s
= Statement::make_goto_unnamed_statement(dest
, loc
);
4021 then_block
->add_statement(s
);
4022 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
4023 b
->add_statement(s
);
4026 if (this->statements_
!= NULL
4027 || (!this->is_fallthrough_
4028 && stmts_label
!= NULL
4029 && *stmts_label
!= NULL
))
4031 go_assert(!this->is_fallthrough_
);
4032 if (stmts_label
!= NULL
&& *stmts_label
!= NULL
)
4034 go_assert(!this->is_default_
);
4035 if (this->statements_
!= NULL
)
4036 (*stmts_label
)->set_location(this->statements_
->start_location());
4037 Statement
* s
= Statement::make_unnamed_label_statement(*stmts_label
);
4038 b
->add_statement(s
);
4039 *stmts_label
= NULL
;
4041 if (this->statements_
!= NULL
)
4042 b
->add_statement(Statement::make_block_statement(this->statements_
,
4046 if (this->is_fallthrough_
)
4047 go_assert(next_case_label
== NULL
);
4050 Location gloc
= (this->statements_
== NULL
4052 : this->statements_
->end_location());
4053 b
->add_statement(Statement::make_goto_unnamed_statement(break_label
,
4055 if (next_case_label
!= NULL
)
4058 Statement::make_unnamed_label_statement(next_case_label
);
4059 b
->add_statement(s
);
4064 // Return true if this type clause may fall through to the statements
4065 // following the switch.
4068 Type_case_clauses::Type_case_clause::may_fall_through() const
4070 if (this->is_fallthrough_
)
4072 // This case means that we automatically fall through to the
4073 // next case (it's used for T1 in case T1, T2:). It does not
4074 // mean that we fall through to the end of the type switch as a
4075 // whole. There is sure to be a next case and that next case
4076 // will determine whether we fall through to the statements
4077 // after the type switch.
4080 if (this->statements_
== NULL
)
4082 return this->statements_
->may_fall_through();
4085 // Dump the AST representation for a type case clause
4088 Type_case_clauses::Type_case_clause::dump_clause(
4089 Ast_dump_context
* ast_dump_context
) const
4091 ast_dump_context
->print_indent();
4092 if (this->is_default_
)
4094 ast_dump_context
->ostream() << "default:";
4098 ast_dump_context
->ostream() << "case ";
4099 ast_dump_context
->dump_type(this->type_
);
4100 ast_dump_context
->ostream() << ":" ;
4102 ast_dump_context
->dump_block(this->statements_
);
4103 if (this->is_fallthrough_
)
4105 ast_dump_context
->print_indent();
4106 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
4110 // Class Type_case_clauses.
4115 Type_case_clauses::traverse(Traverse
* traverse
)
4117 for (Type_clauses::iterator p
= this->clauses_
.begin();
4118 p
!= this->clauses_
.end();
4121 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4122 return TRAVERSE_EXIT
;
4124 return TRAVERSE_CONTINUE
;
4127 // Check for duplicate types.
4130 Type_case_clauses::check_duplicates() const
4132 typedef Unordered_set_hash(const Type
*, Type_hash_identical
,
4133 Type_identical
) Types_seen
;
4134 Types_seen types_seen
;
4135 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4136 p
!= this->clauses_
.end();
4139 Type
* t
= p
->type();
4142 if (t
->is_nil_constant_as_type())
4143 t
= Type::make_nil_type();
4144 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
.insert(t
);
4146 go_error_at(p
->location(), "duplicate type in switch");
4150 // Lower the clauses in a type switch. Add statements to the block B.
4151 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4152 // BREAK_LABEL is the label at the end of the type switch.
4155 Type_case_clauses::lower(Type
* switch_val_type
, Block
* b
,
4156 Temporary_statement
* descriptor_temp
,
4157 Unnamed_label
* break_label
) const
4159 const Type_case_clause
* default_case
= NULL
;
4161 Unnamed_label
* stmts_label
= NULL
;
4162 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4163 p
!= this->clauses_
.end();
4166 if (!p
->is_default())
4167 p
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4171 // We are generating a series of tests, which means that we
4172 // need to move the default case to the end.
4176 go_assert(stmts_label
== NULL
);
4178 if (default_case
!= NULL
)
4179 default_case
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4183 // Return true if these clauses may fall through to the statements
4184 // following the switch statement.
4187 Type_case_clauses::may_fall_through() const
4189 bool found_default
= false;
4190 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4191 p
!= this->clauses_
.end();
4194 if (p
->may_fall_through())
4196 if (p
->is_default())
4197 found_default
= true;
4199 return !found_default
;
4202 // Dump the AST representation for case clauses (from a switch statement)
4205 Type_case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4207 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4208 p
!= this->clauses_
.end();
4210 p
->dump_clause(ast_dump_context
);
4213 // Class Type_switch_statement.
4218 Type_switch_statement::do_traverse(Traverse
* traverse
)
4220 if (this->traverse_expression(traverse
, &this->expr_
) == TRAVERSE_EXIT
)
4221 return TRAVERSE_EXIT
;
4222 if (this->clauses_
!= NULL
)
4223 return this->clauses_
->traverse(traverse
);
4224 return TRAVERSE_CONTINUE
;
4227 // Lower a type switch statement to a series of if statements. The gc
4228 // compiler is able to generate a table in some cases. However, that
4229 // does not work for us because we may have type descriptors in
4230 // different shared libraries, so we can't compare them with simple
4231 // equality testing.
4234 Type_switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
4235 Statement_inserter
*)
4237 const Location loc
= this->location();
4239 if (this->clauses_
!= NULL
)
4240 this->clauses_
->check_duplicates();
4242 Block
* b
= new Block(enclosing
, loc
);
4244 Type
* val_type
= this->expr_
->type();
4245 if (val_type
->interface_type() == NULL
)
4247 if (!val_type
->is_error())
4248 this->report_error(_("cannot type switch on non-interface value"));
4249 return Statement::make_error_statement(loc
);
4252 // var descriptor_temp DESCRIPTOR_TYPE
4253 Type
* descriptor_type
= Type::make_type_descriptor_ptr_type();
4254 Temporary_statement
* descriptor_temp
=
4255 Statement::make_temporary(descriptor_type
, NULL
, loc
);
4256 b
->add_statement(descriptor_temp
);
4258 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4260 bool is_empty
= val_type
->interface_type()->is_empty();
4261 Expression
* call
= Runtime::make_call((is_empty
4262 ? Runtime::EFACETYPE
4263 : Runtime::IFACETYPE
),
4264 loc
, 1, this->expr_
);
4265 Temporary_reference_expression
* lhs
=
4266 Expression::make_temporary_reference(descriptor_temp
, loc
);
4267 lhs
->set_is_lvalue();
4268 Statement
* s
= Statement::make_assignment(lhs
, call
, loc
);
4269 b
->add_statement(s
);
4271 if (this->clauses_
!= NULL
)
4272 this->clauses_
->lower(val_type
, b
, descriptor_temp
, this->break_label());
4274 s
= Statement::make_unnamed_label_statement(this->break_label_
);
4275 b
->add_statement(s
);
4277 return Statement::make_block_statement(b
, loc
);
4280 // Return whether this switch may fall through.
4283 Type_switch_statement::do_may_fall_through() const
4285 if (this->clauses_
== NULL
)
4288 // If we have a break label, then some case needed it. That implies
4289 // that the switch statement as a whole can fall through.
4290 if (this->break_label_
!= NULL
)
4293 return this->clauses_
->may_fall_through();
4296 // Return the break label for this type switch statement, creating it
4300 Type_switch_statement::break_label()
4302 if (this->break_label_
== NULL
)
4303 this->break_label_
= new Unnamed_label(this->location());
4304 return this->break_label_
;
4307 // Dump the AST representation for a type switch statement
4310 Type_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
4313 ast_dump_context
->print_indent();
4314 ast_dump_context
->ostream() << "switch ";
4315 if (!this->name_
.empty())
4316 ast_dump_context
->ostream() << this->name_
<< " = ";
4317 ast_dump_context
->dump_expression(this->expr_
);
4318 ast_dump_context
->ostream() << " .(type)";
4319 if (ast_dump_context
->dump_subblocks())
4321 ast_dump_context
->ostream() << " {" << dsuffix(location()) << std::endl
;
4322 this->clauses_
->dump_clauses(ast_dump_context
);
4323 ast_dump_context
->ostream() << "}";
4325 ast_dump_context
->ostream() << std::endl
;
4328 // Make a type switch statement.
4330 Type_switch_statement
*
4331 Statement::make_type_switch_statement(const std::string
& name
, Expression
* expr
,
4334 return new Type_switch_statement(name
, expr
, location
);
4337 // Class Send_statement.
4342 Send_statement::do_traverse(Traverse
* traverse
)
4344 if (this->traverse_expression(traverse
, &this->channel_
) == TRAVERSE_EXIT
)
4345 return TRAVERSE_EXIT
;
4346 return this->traverse_expression(traverse
, &this->val_
);
4352 Send_statement::do_determine_types()
4354 this->channel_
->determine_type_no_context();
4355 Type
* type
= this->channel_
->type();
4356 Type_context context
;
4357 if (type
->channel_type() != NULL
)
4358 context
.type
= type
->channel_type()->element_type();
4359 this->val_
->determine_type(&context
);
4365 Send_statement::do_check_types(Gogo
*)
4367 Type
* type
= this->channel_
->type();
4368 if (type
->is_error())
4370 this->set_is_error();
4373 Channel_type
* channel_type
= type
->channel_type();
4374 if (channel_type
== NULL
)
4376 go_error_at(this->location(), "left operand of %<<-%> must be channel");
4377 this->set_is_error();
4380 Type
* element_type
= channel_type
->element_type();
4381 if (!Type::are_assignable(element_type
, this->val_
->type(), NULL
))
4383 this->report_error(_("incompatible types in send"));
4386 if (!channel_type
->may_send())
4388 this->report_error(_("invalid send on receive-only channel"));
4393 // Flatten a send statement. We may need a temporary for interface
4397 Send_statement::do_flatten(Gogo
*, Named_object
*, Block
*,
4398 Statement_inserter
* inserter
)
4400 if (this->channel_
->is_error_expression()
4401 || this->channel_
->type()->is_error_type())
4403 go_assert(saw_errors());
4404 return Statement::make_error_statement(this->location());
4407 Type
* element_type
= this->channel_
->type()->channel_type()->element_type();
4408 if (!Type::are_identical(element_type
, this->val_
->type(), false, NULL
)
4409 && this->val_
->type()->interface_type() != NULL
4410 && !this->val_
->is_variable())
4412 Temporary_statement
* temp
=
4413 Statement::make_temporary(NULL
, this->val_
, this->location());
4414 inserter
->insert(temp
);
4415 this->val_
= Expression::make_temporary_reference(temp
,
4421 // Convert a send statement to the backend representation.
4424 Send_statement::do_get_backend(Translate_context
* context
)
4426 Location loc
= this->location();
4428 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
4429 Type
* element_type
= channel_type
->element_type();
4430 Expression
* val
= Expression::convert_for_assignment(context
->gogo(),
4434 bool can_take_address
;
4435 switch (element_type
->base()->classification())
4437 case Type::TYPE_BOOLEAN
:
4438 case Type::TYPE_INTEGER
:
4439 case Type::TYPE_FUNCTION
:
4440 case Type::TYPE_POINTER
:
4441 case Type::TYPE_MAP
:
4442 case Type::TYPE_CHANNEL
:
4443 case Type::TYPE_FLOAT
:
4444 case Type::TYPE_COMPLEX
:
4445 case Type::TYPE_STRING
:
4446 case Type::TYPE_INTERFACE
:
4447 can_take_address
= false;
4450 case Type::TYPE_STRUCT
:
4451 can_take_address
= true;
4454 case Type::TYPE_ARRAY
:
4455 can_take_address
= !element_type
->is_slice_type();
4459 case Type::TYPE_ERROR
:
4460 case Type::TYPE_VOID
:
4461 case Type::TYPE_SINK
:
4462 case Type::TYPE_NIL
:
4463 case Type::TYPE_NAMED
:
4464 case Type::TYPE_FORWARD
:
4465 go_assert(saw_errors());
4466 return context
->backend()->error_statement();
4469 // Only try to take the address of a variable. We have already
4470 // moved variables to the heap, so this should not cause that to
4471 // happen unnecessarily.
4472 if (can_take_address
4473 && val
->var_expression() == NULL
4474 && val
->temporary_reference_expression() == NULL
)
4475 can_take_address
= false;
4477 Expression
* td
= Expression::make_type_descriptor(this->channel_
->type(),
4480 Bstatement
* btemp
= NULL
;
4481 if (can_take_address
)
4483 // The function doesn't change the value, so just take its
4484 // address directly.
4485 val
= Expression::make_unary(OPERATOR_AND
, val
, loc
);
4489 // The value is not in a variable, or is small enough that it
4490 // might be in a register, and taking the address would push it
4491 // on the stack. Copy it into a temporary variable to take the
4493 Temporary_statement
* temp
= Statement::make_temporary(element_type
,
4495 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
4496 val
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
4497 btemp
= temp
->get_backend(context
);
4500 Expression
* call
= Runtime::make_call(Runtime::CHANSEND
, loc
, 3, td
,
4501 this->channel_
, val
);
4503 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4504 Bexpression
* bcall
= call
->get_backend(context
);
4505 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
4506 Bstatement
* s
= context
->backend()->expression_statement(bfunction
, bcall
);
4511 return context
->backend()->compound_statement(btemp
, s
);
4514 // Dump the AST representation for a send statement
4517 Send_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4519 ast_dump_context
->print_indent();
4520 ast_dump_context
->dump_expression(this->channel_
);
4521 ast_dump_context
->ostream() << " <- ";
4522 ast_dump_context
->dump_expression(this->val_
);
4523 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
4526 // Make a send statement.
4529 Statement::make_send_statement(Expression
* channel
, Expression
* val
,
4532 return new Send_statement(channel
, val
, location
);
4535 // Class Select_clauses::Select_clause.
4540 Select_clauses::Select_clause::traverse(Traverse
* traverse
)
4542 if (!this->is_lowered_
4543 && (traverse
->traverse_mask()
4544 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
4546 if (this->channel_
!= NULL
)
4548 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
4549 return TRAVERSE_EXIT
;
4551 if (this->val_
!= NULL
)
4553 if (Expression::traverse(&this->val_
, traverse
) == TRAVERSE_EXIT
)
4554 return TRAVERSE_EXIT
;
4556 if (this->closed_
!= NULL
)
4558 if (Expression::traverse(&this->closed_
, traverse
) == TRAVERSE_EXIT
)
4559 return TRAVERSE_EXIT
;
4562 if (this->statements_
!= NULL
)
4564 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
4565 return TRAVERSE_EXIT
;
4567 return TRAVERSE_CONTINUE
;
4570 // Lowering. We call a function to register this clause, and arrange
4571 // to set any variables in any receive clause.
4574 Select_clauses::Select_clause::lower(Gogo
* gogo
, Named_object
* function
,
4575 Block
* b
, Temporary_statement
* sel
)
4577 Location loc
= this->location_
;
4579 Expression
* selref
= Expression::make_temporary_reference(sel
, loc
);
4580 selref
= Expression::make_unary(OPERATOR_AND
, selref
, loc
);
4582 Expression
* index_expr
= Expression::make_integer_ul(this->index_
, NULL
,
4585 if (this->is_default_
)
4587 go_assert(this->channel_
== NULL
&& this->val_
== NULL
);
4588 this->lower_default(b
, selref
, index_expr
);
4589 this->is_lowered_
= true;
4593 // Evaluate the channel before the select statement.
4594 Temporary_statement
* channel_temp
= Statement::make_temporary(NULL
,
4597 b
->add_statement(channel_temp
);
4598 Expression
* chanref
= Expression::make_temporary_reference(channel_temp
,
4602 this->lower_send(b
, selref
, chanref
, index_expr
);
4604 this->lower_recv(gogo
, function
, b
, selref
, chanref
, index_expr
);
4606 // Now all references should be handled through the statements, not
4608 this->is_lowered_
= true;
4612 // Lower a default clause in a select statement.
4615 Select_clauses::Select_clause::lower_default(Block
* b
, Expression
* selref
,
4616 Expression
* index_expr
)
4618 Location loc
= this->location_
;
4619 Expression
* call
= Runtime::make_call(Runtime::SELECTDEFAULT
, loc
, 2, selref
,
4621 b
->add_statement(Statement::make_statement(call
, true));
4624 // Lower a send clause in a select statement.
4627 Select_clauses::Select_clause::lower_send(Block
* b
, Expression
* selref
,
4628 Expression
* chanref
,
4629 Expression
* index_expr
)
4631 Location loc
= this->location_
;
4633 Channel_type
* ct
= this->channel_
->type()->channel_type();
4637 Type
* valtype
= ct
->element_type();
4639 // Note that copying the value to a temporary here means that we
4640 // evaluate the send values in the required order.
4641 Temporary_statement
* val
= Statement::make_temporary(valtype
, this->val_
,
4643 b
->add_statement(val
);
4645 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4646 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4648 Expression
* call
= Runtime::make_call(Runtime::SELECTSEND
, loc
, 4, selref
,
4649 chanref
, valaddr
, index_expr
);
4650 b
->add_statement(Statement::make_statement(call
, true));
4653 // Lower a receive clause in a select statement.
4656 Select_clauses::Select_clause::lower_recv(Gogo
* gogo
, Named_object
* function
,
4657 Block
* b
, Expression
* selref
,
4658 Expression
* chanref
,
4659 Expression
* index_expr
)
4661 Location loc
= this->location_
;
4663 Channel_type
* ct
= this->channel_
->type()->channel_type();
4667 Type
* valtype
= ct
->element_type();
4668 Temporary_statement
* val
= Statement::make_temporary(valtype
, NULL
, loc
);
4669 b
->add_statement(val
);
4671 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4672 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4674 Temporary_statement
* closed_temp
= NULL
;
4677 if (this->closed_
== NULL
&& this->closedvar_
== NULL
)
4678 call
= Runtime::make_call(Runtime::SELECTRECV
, loc
, 4, selref
, chanref
,
4679 valaddr
, index_expr
);
4682 closed_temp
= Statement::make_temporary(Type::lookup_bool_type(), NULL
,
4684 b
->add_statement(closed_temp
);
4685 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4687 Expression
* caddr
= Expression::make_unary(OPERATOR_AND
, cref
, loc
);
4688 call
= Runtime::make_call(Runtime::SELECTRECV2
, loc
, 5, selref
, chanref
,
4689 valaddr
, caddr
, index_expr
);
4692 b
->add_statement(Statement::make_statement(call
, true));
4694 // If the block of statements is executed, arrange for the received
4695 // value to move from VAL to the place where the statements expect
4700 if (this->var_
!= NULL
)
4702 go_assert(this->val_
== NULL
);
4703 valref
= Expression::make_temporary_reference(val
, loc
);
4704 this->var_
->var_value()->set_init(valref
);
4705 this->var_
->var_value()->clear_type_from_chan_element();
4707 else if (this->val_
!= NULL
&& !this->val_
->is_sink_expression())
4709 init
= new Block(b
, loc
);
4710 valref
= Expression::make_temporary_reference(val
, loc
);
4711 init
->add_statement(Statement::make_assignment(this->val_
, valref
, loc
));
4714 if (this->closedvar_
!= NULL
)
4716 go_assert(this->closed_
== NULL
);
4717 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4719 this->closedvar_
->var_value()->set_init(cref
);
4721 else if (this->closed_
!= NULL
&& !this->closed_
->is_sink_expression())
4724 init
= new Block(b
, loc
);
4725 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4727 init
->add_statement(Statement::make_assignment(this->closed_
, cref
,
4733 gogo
->lower_block(function
, init
);
4735 if (this->statements_
!= NULL
)
4736 init
->add_statement(Statement::make_block_statement(this->statements_
,
4738 this->statements_
= init
;
4745 Select_clauses::Select_clause::determine_types()
4747 go_assert(this->is_lowered_
);
4748 if (this->statements_
!= NULL
)
4749 this->statements_
->determine_types();
4755 Select_clauses::Select_clause::check_types()
4757 if (this->is_default_
)
4760 Channel_type
* ct
= this->channel_
->type()->channel_type();
4763 go_error_at(this->channel_
->location(), "expected channel");
4767 if (this->is_send_
&& !ct
->may_send())
4768 go_error_at(this->location(), "invalid send on receive-only channel");
4769 else if (!this->is_send_
&& !ct
->may_receive())
4770 go_error_at(this->location(), "invalid receive on send-only channel");
4773 // Whether this clause may fall through to the statement which follows
4774 // the overall select statement.
4777 Select_clauses::Select_clause::may_fall_through() const
4779 if (this->statements_
== NULL
)
4781 return this->statements_
->may_fall_through();
4784 // Return the backend representation for the statements to execute.
4787 Select_clauses::Select_clause::get_statements_backend(
4788 Translate_context
* context
)
4790 if (this->statements_
== NULL
)
4792 Bblock
* bblock
= this->statements_
->get_backend(context
);
4793 return context
->backend()->block_statement(bblock
);
4796 // Dump the AST representation for a select case clause
4799 Select_clauses::Select_clause::dump_clause(
4800 Ast_dump_context
* ast_dump_context
) const
4802 ast_dump_context
->print_indent();
4803 if (this->is_default_
)
4805 ast_dump_context
->ostream() << "default:";
4809 ast_dump_context
->ostream() << "case " ;
4812 ast_dump_context
->dump_expression(this->channel_
);
4813 ast_dump_context
->ostream() << " <- " ;
4814 if (this->val_
!= NULL
)
4815 ast_dump_context
->dump_expression(this->val_
);
4819 if (this->val_
!= NULL
)
4820 ast_dump_context
->dump_expression(this->val_
);
4821 if (this->closed_
!= NULL
)
4823 // FIXME: can val_ == NULL and closed_ ! = NULL?
4824 ast_dump_context
->ostream() << " , " ;
4825 ast_dump_context
->dump_expression(this->closed_
);
4827 if (this->closedvar_
!= NULL
|| this->var_
!= NULL
)
4828 ast_dump_context
->ostream() << " := " ;
4830 ast_dump_context
->ostream() << " <- " ;
4831 ast_dump_context
->dump_expression(this->channel_
);
4833 ast_dump_context
->ostream() << ":" ;
4835 ast_dump_context
->dump_block(this->statements_
);
4838 // Class Select_clauses.
4843 Select_clauses::traverse(Traverse
* traverse
)
4845 for (Clauses::iterator p
= this->clauses_
.begin();
4846 p
!= this->clauses_
.end();
4849 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4850 return TRAVERSE_EXIT
;
4852 return TRAVERSE_CONTINUE
;
4855 // Lowering. Here we pull out the channel and the send values, to
4856 // enforce the order of evaluation. We also add explicit send and
4857 // receive statements to the clauses.
4860 Select_clauses::lower(Gogo
* gogo
, Named_object
* function
, Block
* b
,
4861 Temporary_statement
* sel
)
4863 for (Clauses::iterator p
= this->clauses_
.begin();
4864 p
!= this->clauses_
.end();
4866 p
->lower(gogo
, function
, b
, sel
);
4872 Select_clauses::determine_types()
4874 for (Clauses::iterator p
= this->clauses_
.begin();
4875 p
!= this->clauses_
.end();
4877 p
->determine_types();
4883 Select_clauses::check_types()
4885 for (Clauses::iterator p
= this->clauses_
.begin();
4886 p
!= this->clauses_
.end();
4891 // Return whether these select clauses fall through to the statement
4892 // following the overall select statement.
4895 Select_clauses::may_fall_through() const
4897 for (Clauses::const_iterator p
= this->clauses_
.begin();
4898 p
!= this->clauses_
.end();
4900 if (p
->may_fall_through())
4905 // Convert to the backend representation. We have already accumulated
4906 // all the select information. Now we call selectgo, which will
4907 // return the index of the clause to execute.
4910 Select_clauses::get_backend(Translate_context
* context
,
4911 Temporary_statement
* sel
,
4912 Unnamed_label
*break_label
,
4915 size_t count
= this->clauses_
.size();
4916 std::vector
<std::vector
<Bexpression
*> > cases(count
);
4917 std::vector
<Bstatement
*> clauses(count
);
4919 Type
* int32_type
= Type::lookup_integer_type("int32");
4922 for (Clauses::iterator p
= this->clauses_
.begin();
4923 p
!= this->clauses_
.end();
4926 int index
= p
->index();
4927 Expression
* index_expr
= Expression::make_integer_ul(index
, int32_type
,
4929 cases
[i
].push_back(index_expr
->get_backend(context
));
4931 Bstatement
* s
= p
->get_statements_backend(context
);
4932 Location gloc
= (p
->statements() == NULL
4934 : p
->statements()->end_location());
4935 Bstatement
* g
= break_label
->get_goto(context
, gloc
);
4940 clauses
[i
] = context
->backend()->compound_statement(s
, g
);
4943 Expression
* selref
= Expression::make_temporary_reference(sel
, location
);
4944 selref
= Expression::make_unary(OPERATOR_AND
, selref
, location
);
4945 Expression
* call
= Runtime::make_call(Runtime::SELECTGO
, location
, 1,
4947 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4948 Bexpression
* bcall
= call
->get_backend(context
);
4952 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
4953 return context
->backend()->expression_statement(bfunction
, bcall
);
4956 std::vector
<Bstatement
*> statements
;
4957 statements
.reserve(2);
4959 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
4960 Bstatement
* switch_stmt
= context
->backend()->switch_statement(bfunction
,
4965 statements
.push_back(switch_stmt
);
4967 Bstatement
* ldef
= break_label
->get_definition(context
);
4968 statements
.push_back(ldef
);
4970 return context
->backend()->statement_list(statements
);
4972 // Dump the AST representation for select clauses.
4975 Select_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4977 for (Clauses::const_iterator p
= this->clauses_
.begin();
4978 p
!= this->clauses_
.end();
4980 p
->dump_clause(ast_dump_context
);
4983 // Class Select_statement.
4985 // Return the break label for this switch statement, creating it if
4989 Select_statement::break_label()
4991 if (this->break_label_
== NULL
)
4992 this->break_label_
= new Unnamed_label(this->location());
4993 return this->break_label_
;
4996 // Lower a select statement. This will still return a select
4997 // statement, but it will be modified to implement the order of
4998 // evaluation rules, and to include the send and receive statements as
4999 // explicit statements in the clauses.
5002 Select_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
5003 Block
* enclosing
, Statement_inserter
*)
5005 if (this->is_lowered_
)
5008 Location loc
= this->location();
5010 Block
* b
= new Block(enclosing
, loc
);
5012 go_assert(this->sel_
== NULL
);
5014 int ncases
= this->clauses_
->size();
5015 Type
* selstruct_type
= Channel_type::select_type(ncases
);
5016 this->sel_
= Statement::make_temporary(selstruct_type
, NULL
, loc
);
5017 b
->add_statement(this->sel_
);
5019 int64_t selstruct_size
;
5020 if (!selstruct_type
->backend_type_size(gogo
, &selstruct_size
))
5022 go_assert(saw_errors());
5023 return Statement::make_error_statement(loc
);
5026 Expression
* ref
= Expression::make_temporary_reference(this->sel_
, loc
);
5027 ref
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5028 Expression
* selstruct_size_expr
=
5029 Expression::make_integer_int64(selstruct_size
, NULL
, loc
);
5030 Expression
* size_expr
= Expression::make_integer_ul(ncases
, NULL
, loc
);
5031 Expression
* call
= Runtime::make_call(Runtime::NEWSELECT
, loc
, 3,
5032 ref
, selstruct_size_expr
, size_expr
);
5033 b
->add_statement(Statement::make_statement(call
, true));
5035 this->clauses_
->lower(gogo
, function
, b
, this->sel_
);
5036 this->is_lowered_
= true;
5037 b
->add_statement(this);
5039 return Statement::make_block_statement(b
, loc
);
5042 // Whether the select statement itself may fall through to the following
5046 Select_statement::do_may_fall_through() const
5048 // A select statement is terminating if no break statement
5049 // refers to it and all of its clauses are terminating.
5050 if (this->break_label_
!= NULL
)
5052 return this->clauses_
->may_fall_through();
5055 // Return the backend representation for a select statement.
5058 Select_statement::do_get_backend(Translate_context
* context
)
5060 return this->clauses_
->get_backend(context
, this->sel_
, this->break_label(),
5064 // Dump the AST representation for a select statement.
5067 Select_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5069 ast_dump_context
->print_indent();
5070 ast_dump_context
->ostream() << "select";
5071 if (ast_dump_context
->dump_subblocks())
5073 ast_dump_context
->ostream() << " {" << dsuffix(location()) << std::endl
;
5074 this->clauses_
->dump_clauses(ast_dump_context
);
5075 ast_dump_context
->ostream() << "}";
5077 ast_dump_context
->ostream() << std::endl
;
5080 // Make a select statement.
5083 Statement::make_select_statement(Location location
)
5085 return new Select_statement(location
);
5088 // Class For_statement.
5093 For_statement::do_traverse(Traverse
* traverse
)
5095 if (this->init_
!= NULL
)
5097 if (this->init_
->traverse(traverse
) == TRAVERSE_EXIT
)
5098 return TRAVERSE_EXIT
;
5100 if (this->cond_
!= NULL
)
5102 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
)
5103 return TRAVERSE_EXIT
;
5105 if (this->post_
!= NULL
)
5107 if (this->post_
->traverse(traverse
) == TRAVERSE_EXIT
)
5108 return TRAVERSE_EXIT
;
5110 return this->statements_
->traverse(traverse
);
5113 // Lower a For_statement into if statements and gotos. Getting rid of
5114 // complex statements make it easier to handle garbage collection.
5117 For_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
5118 Statement_inserter
*)
5121 Location loc
= this->location();
5123 Block
* b
= new Block(enclosing
, this->location());
5124 if (this->init_
!= NULL
)
5126 s
= Statement::make_block_statement(this->init_
,
5127 this->init_
->start_location());
5128 b
->add_statement(s
);
5131 Unnamed_label
* entry
= NULL
;
5132 if (this->cond_
!= NULL
)
5134 entry
= new Unnamed_label(this->location());
5135 b
->add_statement(Statement::make_goto_unnamed_statement(entry
, loc
));
5138 Unnamed_label
* top
= new Unnamed_label(this->location());
5139 top
->set_derived_from(this);
5140 b
->add_statement(Statement::make_unnamed_label_statement(top
));
5142 s
= Statement::make_block_statement(this->statements_
,
5143 this->statements_
->start_location());
5144 b
->add_statement(s
);
5146 Location end_loc
= this->statements_
->end_location();
5148 Unnamed_label
* cont
= this->continue_label_
;
5150 b
->add_statement(Statement::make_unnamed_label_statement(cont
));
5152 if (this->post_
!= NULL
)
5154 s
= Statement::make_block_statement(this->post_
,
5155 this->post_
->start_location());
5156 b
->add_statement(s
);
5157 end_loc
= this->post_
->end_location();
5160 if (this->cond_
== NULL
)
5161 b
->add_statement(Statement::make_goto_unnamed_statement(top
, end_loc
));
5164 b
->add_statement(Statement::make_unnamed_label_statement(entry
));
5166 Location cond_loc
= this->cond_
->location();
5167 Block
* then_block
= new Block(b
, cond_loc
);
5168 s
= Statement::make_goto_unnamed_statement(top
, cond_loc
);
5169 then_block
->add_statement(s
);
5171 s
= Statement::make_if_statement(this->cond_
, then_block
, NULL
, cond_loc
);
5172 b
->add_statement(s
);
5175 Unnamed_label
* brk
= this->break_label_
;
5177 b
->add_statement(Statement::make_unnamed_label_statement(brk
));
5179 b
->set_end_location(end_loc
);
5181 Statement
* bs
= Statement::make_block_statement(b
, loc
);
5182 bs
->block_statement()->set_is_lowered_for_statement();
5186 // Return the break label, creating it if necessary.
5189 For_statement::break_label()
5191 if (this->break_label_
== NULL
)
5192 this->break_label_
= new Unnamed_label(this->location());
5193 return this->break_label_
;
5196 // Return the continue LABEL_EXPR.
5199 For_statement::continue_label()
5201 if (this->continue_label_
== NULL
)
5202 this->continue_label_
= new Unnamed_label(this->location());
5203 return this->continue_label_
;
5206 // Set the break and continue labels a for statement. This is used
5207 // when lowering a for range statement.
5210 For_statement::set_break_continue_labels(Unnamed_label
* break_label
,
5211 Unnamed_label
* continue_label
)
5213 go_assert(this->break_label_
== NULL
&& this->continue_label_
== NULL
);
5214 this->break_label_
= break_label
;
5215 this->continue_label_
= continue_label
;
5218 // Whether the overall statement may fall through.
5221 For_statement::do_may_fall_through() const
5223 // A for loop is terminating if it has no condition and
5224 // no break statement.
5225 if(this->cond_
!= NULL
)
5227 if(this->break_label_
!= NULL
)
5232 // Dump the AST representation for a for statement.
5235 For_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5237 if (this->init_
!= NULL
&& ast_dump_context
->dump_subblocks())
5239 ast_dump_context
->print_indent();
5240 ast_dump_context
->indent();
5241 ast_dump_context
->ostream() << "// INIT " << std::endl
;
5242 ast_dump_context
->dump_block(this->init_
);
5243 ast_dump_context
->unindent();
5245 ast_dump_context
->print_indent();
5246 ast_dump_context
->ostream() << "for ";
5247 if (this->cond_
!= NULL
)
5248 ast_dump_context
->dump_expression(this->cond_
);
5250 if (ast_dump_context
->dump_subblocks())
5252 ast_dump_context
->ostream() << " {" << std::endl
;
5253 ast_dump_context
->dump_block(this->statements_
);
5254 if (this->init_
!= NULL
)
5256 ast_dump_context
->print_indent();
5257 ast_dump_context
->ostream() << "// POST " << std::endl
;
5258 ast_dump_context
->dump_block(this->post_
);
5260 ast_dump_context
->unindent();
5262 ast_dump_context
->print_indent();
5263 ast_dump_context
->ostream() << "}";
5266 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
5269 // Make a for statement.
5272 Statement::make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
5275 return new For_statement(init
, cond
, post
, location
);
5278 // Class For_range_statement.
5283 For_range_statement::do_traverse(Traverse
* traverse
)
5285 if (this->index_var_
!= NULL
)
5287 if (this->traverse_expression(traverse
, &this->index_var_
)
5289 return TRAVERSE_EXIT
;
5291 if (this->value_var_
!= NULL
)
5293 if (this->traverse_expression(traverse
, &this->value_var_
)
5295 return TRAVERSE_EXIT
;
5297 if (this->traverse_expression(traverse
, &this->range_
) == TRAVERSE_EXIT
)
5298 return TRAVERSE_EXIT
;
5299 return this->statements_
->traverse(traverse
);
5302 // Lower a for range statement. For simplicity we lower this into a
5303 // for statement, which will then be lowered in turn to goto
5307 For_range_statement::do_lower(Gogo
* gogo
, Named_object
*, Block
* enclosing
,
5308 Statement_inserter
*)
5310 Type
* range_type
= this->range_
->type();
5311 if (range_type
->points_to() != NULL
5312 && range_type
->points_to()->array_type() != NULL
5313 && !range_type
->points_to()->is_slice_type())
5314 range_type
= range_type
->points_to();
5317 Type
* value_type
= NULL
;
5318 if (range_type
->array_type() != NULL
)
5320 index_type
= Type::lookup_integer_type("int");
5321 value_type
= range_type
->array_type()->element_type();
5323 else if (range_type
->is_string_type())
5325 index_type
= Type::lookup_integer_type("int");
5326 value_type
= gogo
->lookup_global("rune")->type_value();
5328 else if (range_type
->map_type() != NULL
)
5330 index_type
= range_type
->map_type()->key_type();
5331 value_type
= range_type
->map_type()->val_type();
5333 else if (range_type
->channel_type() != NULL
)
5335 index_type
= range_type
->channel_type()->element_type();
5336 if (this->value_var_
!= NULL
)
5338 if (!this->value_var_
->type()->is_error())
5339 this->report_error(_("too many variables for range clause "
5341 return Statement::make_error_statement(this->location());
5346 this->report_error(_("range clause must have "
5347 "array, slice, string, map, or channel type"));
5348 return Statement::make_error_statement(this->location());
5351 Location loc
= this->location();
5352 Block
* temp_block
= new Block(enclosing
, loc
);
5354 Named_object
* range_object
= NULL
;
5355 Temporary_statement
* range_temp
= NULL
;
5356 Var_expression
* ve
= this->range_
->var_expression();
5358 range_object
= ve
->named_object();
5361 range_temp
= Statement::make_temporary(NULL
, this->range_
, loc
);
5362 temp_block
->add_statement(range_temp
);
5363 this->range_
= NULL
;
5366 Temporary_statement
* index_temp
= Statement::make_temporary(index_type
,
5368 temp_block
->add_statement(index_temp
);
5370 Temporary_statement
* value_temp
= NULL
;
5371 if (this->value_var_
!= NULL
)
5373 value_temp
= Statement::make_temporary(value_type
, NULL
, loc
);
5374 temp_block
->add_statement(value_temp
);
5377 Block
* body
= new Block(temp_block
, loc
);
5384 // Arrange to do a loop appropriate for the type. We will produce
5385 // for INIT ; COND ; POST {
5387 // INDEX = INDEX_TEMP
5388 // VALUE = VALUE_TEMP // If there is a value
5389 // original statements
5392 if (range_type
->is_slice_type())
5393 this->lower_range_slice(gogo
, temp_block
, body
, range_object
, range_temp
,
5394 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5396 else if (range_type
->array_type() != NULL
)
5397 this->lower_range_array(gogo
, temp_block
, body
, range_object
, range_temp
,
5398 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5400 else if (range_type
->is_string_type())
5401 this->lower_range_string(gogo
, temp_block
, body
, range_object
, range_temp
,
5402 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5404 else if (range_type
->map_type() != NULL
)
5405 this->lower_range_map(gogo
, range_type
->map_type(), temp_block
, body
,
5406 range_object
, range_temp
, index_temp
, value_temp
,
5407 &init
, &cond
, &iter_init
, &post
);
5408 else if (range_type
->channel_type() != NULL
)
5409 this->lower_range_channel(gogo
, temp_block
, body
, range_object
, range_temp
,
5410 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5415 if (iter_init
!= NULL
)
5416 body
->add_statement(Statement::make_block_statement(iter_init
, loc
));
5418 if (this->index_var_
!= NULL
)
5421 Expression
* index_ref
=
5422 Expression::make_temporary_reference(index_temp
, loc
);
5423 if (this->value_var_
== NULL
)
5424 assign
= Statement::make_assignment(this->index_var_
, index_ref
, loc
);
5427 Expression_list
* lhs
= new Expression_list();
5428 lhs
->push_back(this->index_var_
);
5429 lhs
->push_back(this->value_var_
);
5431 Expression_list
* rhs
= new Expression_list();
5432 rhs
->push_back(index_ref
);
5433 rhs
->push_back(Expression::make_temporary_reference(value_temp
, loc
));
5435 assign
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5437 body
->add_statement(assign
);
5440 body
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
5442 body
->set_end_location(this->statements_
->end_location());
5444 For_statement
* loop
= Statement::make_for_statement(init
, cond
, post
,
5446 loop
->add_statements(body
);
5447 loop
->set_break_continue_labels(this->break_label_
, this->continue_label_
);
5449 temp_block
->add_statement(loop
);
5451 return Statement::make_block_statement(temp_block
, loc
);
5454 // Return a reference to the range, which may be in RANGE_OBJECT or in
5458 For_range_statement::make_range_ref(Named_object
* range_object
,
5459 Temporary_statement
* range_temp
,
5462 if (range_object
!= NULL
)
5463 return Expression::make_var_reference(range_object
, loc
);
5465 return Expression::make_temporary_reference(range_temp
, loc
);
5468 // Return a call to the predeclared function FUNCNAME passing a
5469 // reference to the temporary variable ARG.
5472 For_range_statement::call_builtin(Gogo
* gogo
, const char* funcname
,
5476 Named_object
* no
= gogo
->lookup_global(funcname
);
5477 go_assert(no
!= NULL
&& no
->is_function_declaration());
5478 Expression
* func
= Expression::make_func_reference(no
, NULL
, loc
);
5479 Expression_list
* params
= new Expression_list();
5480 params
->push_back(arg
);
5481 return Expression::make_call(func
, params
, false, loc
);
5484 // Lower a for range over an array.
5487 For_range_statement::lower_range_array(Gogo
* gogo
,
5490 Named_object
* range_object
,
5491 Temporary_statement
* range_temp
,
5492 Temporary_statement
* index_temp
,
5493 Temporary_statement
* value_temp
,
5499 Location loc
= this->location();
5501 // The loop we generate:
5502 // len_temp := len(range)
5503 // range_temp := range
5504 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5505 // value_temp = range_temp[index_temp]
5506 // index = index_temp
5507 // value = value_temp
5513 // len_temp = len(range)
5516 Block
* init
= new Block(enclosing
, loc
);
5518 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5519 range_temp
= Statement::make_temporary(NULL
, ref
, loc
);
5520 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5521 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5523 init
->add_statement(range_temp
);
5524 init
->add_statement(len_temp
);
5526 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, loc
);
5528 Temporary_reference_expression
* tref
=
5529 Expression::make_temporary_reference(index_temp
, loc
);
5530 tref
->set_is_lvalue();
5531 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5532 init
->add_statement(s
);
5537 // index_temp < len_temp
5539 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5540 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5541 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5545 // Set *PITER_INIT to
5546 // value_temp = range[index_temp]
5548 Block
* iter_init
= NULL
;
5549 if (value_temp
!= NULL
)
5551 iter_init
= new Block(body_block
, loc
);
5553 ref
= Expression::make_temporary_reference(range_temp
, loc
);
5554 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5555 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, NULL
, loc
);
5557 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5558 tref
->set_is_lvalue();
5559 s
= Statement::make_assignment(tref
, index
, loc
);
5561 iter_init
->add_statement(s
);
5563 *piter_init
= iter_init
;
5568 Block
* post
= new Block(enclosing
, loc
);
5569 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5570 tref
->set_is_lvalue();
5571 s
= Statement::make_inc_statement(tref
);
5572 post
->add_statement(s
);
5576 // Lower a for range over a slice.
5579 For_range_statement::lower_range_slice(Gogo
* gogo
,
5582 Named_object
* range_object
,
5583 Temporary_statement
* range_temp
,
5584 Temporary_statement
* index_temp
,
5585 Temporary_statement
* value_temp
,
5591 Location loc
= this->location();
5593 // The loop we generate:
5594 // for_temp := range
5595 // len_temp := len(for_temp)
5596 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5597 // value_temp = for_temp[index_temp]
5598 // index = index_temp
5599 // value = value_temp
5603 // Using for_temp means that we don't need to check bounds when
5604 // fetching range_temp[index_temp].
5607 // range_temp := range
5609 // len_temp = len(range_temp)
5612 Block
* init
= new Block(enclosing
, loc
);
5614 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5615 Temporary_statement
* for_temp
= Statement::make_temporary(NULL
, ref
, loc
);
5616 init
->add_statement(for_temp
);
5618 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5619 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5620 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5622 init
->add_statement(len_temp
);
5624 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, loc
);
5626 Temporary_reference_expression
* tref
=
5627 Expression::make_temporary_reference(index_temp
, loc
);
5628 tref
->set_is_lvalue();
5629 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5630 init
->add_statement(s
);
5635 // index_temp < len_temp
5637 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5638 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5639 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5643 // Set *PITER_INIT to
5644 // value_temp = range[index_temp]
5646 Block
* iter_init
= NULL
;
5647 if (value_temp
!= NULL
)
5649 iter_init
= new Block(body_block
, loc
);
5651 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5652 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5653 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, NULL
, loc
);
5655 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5656 tref
->set_is_lvalue();
5657 s
= Statement::make_assignment(tref
, index
, loc
);
5659 iter_init
->add_statement(s
);
5661 *piter_init
= iter_init
;
5666 Block
* post
= new Block(enclosing
, loc
);
5667 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5668 tref
->set_is_lvalue();
5669 s
= Statement::make_inc_statement(tref
);
5670 post
->add_statement(s
);
5674 // Lower a for range over a string.
5677 For_range_statement::lower_range_string(Gogo
* gogo
,
5680 Named_object
* range_object
,
5681 Temporary_statement
* range_temp
,
5682 Temporary_statement
* index_temp
,
5683 Temporary_statement
* value_temp
,
5689 Location loc
= this->location();
5691 // The loop we generate:
5692 // len_temp := len(range)
5693 // var next_index_temp int
5694 // for index_temp = 0; index_temp < len_temp; index_temp = next_index_temp {
5695 // value_temp = rune(range[index_temp])
5696 // if value_temp < utf8.RuneSelf {
5697 // next_index_temp = index_temp + 1
5699 // value_temp, next_index_temp = decoderune(range, index_temp)
5701 // index = index_temp
5702 // value = value_temp
5707 // len_temp := len(range)
5708 // var next_index_temp int
5710 // var value_temp rune // if value_temp not passed in
5712 Block
* init
= new Block(enclosing
, loc
);
5714 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5715 Call_expression
* call
= this->call_builtin(gogo
, "len", ref
, loc
);
5716 Temporary_statement
* len_temp
=
5717 Statement::make_temporary(index_temp
->type(), call
, loc
);
5718 init
->add_statement(len_temp
);
5720 Temporary_statement
* next_index_temp
=
5721 Statement::make_temporary(index_temp
->type(), NULL
, loc
);
5722 init
->add_statement(next_index_temp
);
5724 Temporary_reference_expression
* index_ref
=
5725 Expression::make_temporary_reference(index_temp
, loc
);
5726 index_ref
->set_is_lvalue();
5727 Expression
* zexpr
= Expression::make_integer_ul(0, index_temp
->type(), loc
);
5728 Statement
* s
= Statement::make_assignment(index_ref
, zexpr
, loc
);
5729 init
->add_statement(s
);
5732 if (value_temp
!= NULL
)
5733 rune_type
= value_temp
->type();
5736 rune_type
= gogo
->lookup_global("rune")->type_value();
5737 value_temp
= Statement::make_temporary(rune_type
, NULL
, loc
);
5738 init
->add_statement(value_temp
);
5744 // index_temp < len_temp
5746 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5747 Expression
* len_ref
=
5748 Expression::make_temporary_reference(len_temp
, loc
);
5749 *pcond
= Expression::make_binary(OPERATOR_LT
, index_ref
, len_ref
, loc
);
5751 // Set *PITER_INIT to
5752 // value_temp = rune(range[index_temp])
5753 // if value_temp < utf8.RuneSelf {
5754 // next_index_temp = index_temp + 1
5756 // value_temp, next_index_temp = decoderune(range, index_temp)
5759 Block
* iter_init
= new Block(body_block
, loc
);
5761 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5762 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5763 ref
= Expression::make_string_index(ref
, index_ref
, NULL
, loc
);
5764 ref
= Expression::make_cast(rune_type
, ref
, loc
);
5765 Temporary_reference_expression
* value_ref
=
5766 Expression::make_temporary_reference(value_temp
, loc
);
5767 value_ref
->set_is_lvalue();
5768 s
= Statement::make_assignment(value_ref
, ref
, loc
);
5769 iter_init
->add_statement(s
);
5771 value_ref
= Expression::make_temporary_reference(value_temp
, loc
);
5772 Expression
* rune_self
= Expression::make_integer_ul(0x80, rune_type
, loc
);
5773 Expression
* cond
= Expression::make_binary(OPERATOR_LT
, value_ref
, rune_self
,
5776 Block
* then_block
= new Block(iter_init
, loc
);
5778 Temporary_reference_expression
* lhs
=
5779 Expression::make_temporary_reference(next_index_temp
, loc
);
5780 lhs
->set_is_lvalue();
5781 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5782 Expression
* one
= Expression::make_integer_ul(1, index_temp
->type(), loc
);
5783 Expression
* sum
= Expression::make_binary(OPERATOR_PLUS
, index_ref
, one
,
5785 s
= Statement::make_assignment(lhs
, sum
, loc
);
5786 then_block
->add_statement(s
);
5788 Block
* else_block
= new Block(iter_init
, loc
);
5790 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5791 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5792 call
= Runtime::make_call(Runtime::DECODERUNE
, loc
, 2, ref
, index_ref
);
5794 value_ref
= Expression::make_temporary_reference(value_temp
, loc
);
5795 value_ref
->set_is_lvalue();
5796 Expression
* res
= Expression::make_call_result(call
, 0);
5797 s
= Statement::make_assignment(value_ref
, res
, loc
);
5798 else_block
->add_statement(s
);
5800 lhs
= Expression::make_temporary_reference(next_index_temp
, loc
);
5801 lhs
->set_is_lvalue();
5802 res
= Expression::make_call_result(call
, 1);
5803 s
= Statement::make_assignment(lhs
, res
, loc
);
5804 else_block
->add_statement(s
);
5806 s
= Statement::make_if_statement(cond
, then_block
, else_block
, loc
);
5807 iter_init
->add_statement(s
);
5809 *piter_init
= iter_init
;
5812 // index_temp = next_index_temp
5814 Block
* post
= new Block(enclosing
, loc
);
5816 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5817 index_ref
->set_is_lvalue();
5818 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5819 s
= Statement::make_assignment(index_ref
, ref
, loc
);
5821 post
->add_statement(s
);
5825 // Lower a for range over a map.
5828 For_range_statement::lower_range_map(Gogo
* gogo
,
5832 Named_object
* range_object
,
5833 Temporary_statement
* range_temp
,
5834 Temporary_statement
* index_temp
,
5835 Temporary_statement
* value_temp
,
5841 Location loc
= this->location();
5843 // The runtime uses a struct to handle ranges over a map. The
5844 // struct is built by Map_type::hiter_type for a specific map type.
5846 // The loop we generate:
5847 // var hiter map_iteration_struct
5848 // for mapiterinit(type, range, &hiter); hiter.key != nil; mapiternext(&hiter) {
5849 // index_temp = *hiter.key
5850 // value_temp = *hiter.val
5851 // index = index_temp
5852 // value = value_temp
5857 // var hiter map_iteration_struct
5858 // runtime.mapiterinit(type, range, &hiter)
5860 Block
* init
= new Block(enclosing
, loc
);
5862 Type
* map_iteration_type
= map_type
->hiter_type(gogo
);
5863 Temporary_statement
* hiter
= Statement::make_temporary(map_iteration_type
,
5865 init
->add_statement(hiter
);
5867 Expression
* p1
= Expression::make_type_descriptor(map_type
, loc
);
5868 Expression
* p2
= this->make_range_ref(range_object
, range_temp
, loc
);
5869 Expression
* ref
= Expression::make_temporary_reference(hiter
, loc
);
5870 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5871 Expression
* call
= Runtime::make_call(Runtime::MAPITERINIT
, loc
, 3,
5873 init
->add_statement(Statement::make_statement(call
, true));
5880 ref
= Expression::make_temporary_reference(hiter
, loc
);
5881 ref
= Expression::make_field_reference(ref
, 0, loc
);
5882 Expression
* ne
= Expression::make_binary(OPERATOR_NOTEQ
, ref
,
5883 Expression::make_nil(loc
),
5887 // Set *PITER_INIT to
5888 // index_temp = *hiter.key
5889 // value_temp = *hiter.val
5891 Block
* iter_init
= new Block(body_block
, loc
);
5893 Expression
* lhs
= Expression::make_temporary_reference(index_temp
, loc
);
5894 Expression
* rhs
= Expression::make_temporary_reference(hiter
, loc
);
5895 rhs
= Expression::make_field_reference(ref
, 0, loc
);
5896 rhs
= Expression::make_unary(OPERATOR_MULT
, ref
, loc
);
5897 Statement
* set
= Statement::make_assignment(lhs
, rhs
, loc
);
5898 iter_init
->add_statement(set
);
5900 if (value_temp
!= NULL
)
5902 lhs
= Expression::make_temporary_reference(value_temp
, loc
);
5903 rhs
= Expression::make_temporary_reference(hiter
, loc
);
5904 rhs
= Expression::make_field_reference(rhs
, 1, loc
);
5905 rhs
= Expression::make_unary(OPERATOR_MULT
, rhs
, loc
);
5906 set
= Statement::make_assignment(lhs
, rhs
, loc
);
5907 iter_init
->add_statement(set
);
5910 *piter_init
= iter_init
;
5913 // mapiternext(&hiter)
5915 Block
* post
= new Block(enclosing
, loc
);
5917 ref
= Expression::make_temporary_reference(hiter
, loc
);
5918 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5919 call
= Runtime::make_call(Runtime::MAPITERNEXT
, loc
, 1, p1
);
5920 post
->add_statement(Statement::make_statement(call
, true));
5925 // Lower a for range over a channel.
5928 For_range_statement::lower_range_channel(Gogo
*,
5931 Named_object
* range_object
,
5932 Temporary_statement
* range_temp
,
5933 Temporary_statement
* index_temp
,
5934 Temporary_statement
* value_temp
,
5940 go_assert(value_temp
== NULL
);
5942 Location loc
= this->location();
5944 // The loop we generate:
5946 // index_temp, ok_temp = <-range
5950 // index = index_temp
5954 // We have no initialization code, no condition, and no post code.
5960 // Set *PITER_INIT to
5961 // index_temp, ok_temp = <-range
5966 Block
* iter_init
= new Block(body_block
, loc
);
5968 Temporary_statement
* ok_temp
=
5969 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
5970 iter_init
->add_statement(ok_temp
);
5972 Expression
* cref
= this->make_range_ref(range_object
, range_temp
, loc
);
5973 Temporary_reference_expression
* iref
=
5974 Expression::make_temporary_reference(index_temp
, loc
);
5975 iref
->set_is_lvalue();
5976 Temporary_reference_expression
* oref
=
5977 Expression::make_temporary_reference(ok_temp
, loc
);
5978 oref
->set_is_lvalue();
5979 Statement
* s
= Statement::make_tuple_receive_assignment(iref
, oref
, cref
,
5981 iter_init
->add_statement(s
);
5983 Block
* then_block
= new Block(iter_init
, loc
);
5984 s
= Statement::make_break_statement(this->break_label(), loc
);
5985 then_block
->add_statement(s
);
5987 oref
= Expression::make_temporary_reference(ok_temp
, loc
);
5988 Expression
* cond
= Expression::make_unary(OPERATOR_NOT
, oref
, loc
);
5989 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
5990 iter_init
->add_statement(s
);
5992 *piter_init
= iter_init
;
5995 // Return the break LABEL_EXPR.
5998 For_range_statement::break_label()
6000 if (this->break_label_
== NULL
)
6001 this->break_label_
= new Unnamed_label(this->location());
6002 return this->break_label_
;
6005 // Return the continue LABEL_EXPR.
6008 For_range_statement::continue_label()
6010 if (this->continue_label_
== NULL
)
6011 this->continue_label_
= new Unnamed_label(this->location());
6012 return this->continue_label_
;
6015 // Dump the AST representation for a for range statement.
6018 For_range_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
6021 ast_dump_context
->print_indent();
6022 ast_dump_context
->ostream() << "for ";
6023 ast_dump_context
->dump_expression(this->index_var_
);
6024 if (this->value_var_
!= NULL
)
6026 ast_dump_context
->ostream() << ", ";
6027 ast_dump_context
->dump_expression(this->value_var_
);
6030 ast_dump_context
->ostream() << " = range ";
6031 ast_dump_context
->dump_expression(this->range_
);
6032 if (ast_dump_context
->dump_subblocks())
6034 ast_dump_context
->ostream() << " {" << std::endl
;
6036 ast_dump_context
->indent();
6038 ast_dump_context
->dump_block(this->statements_
);
6040 ast_dump_context
->unindent();
6041 ast_dump_context
->print_indent();
6042 ast_dump_context
->ostream() << "}";
6044 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
6047 // Make a for statement with a range clause.
6049 For_range_statement
*
6050 Statement::make_for_range_statement(Expression
* index_var
,
6051 Expression
* value_var
,
6055 return new For_range_statement(index_var
, value_var
, range
, location
);