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_dereference(e
, Expression::NIL_CHECK_NOT_NEEDED
,
320 Bexpression
* be
= e
->get_backend(context
);
321 set
= context
->backend()->assignment_statement(bfunction
, be
, binit
, loc
);
324 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
325 Bexpression
* bref
= ref
->get_backend(context
);
326 Bstatement
* sinit
= context
->backend()->init_statement(bfunction
, bvar
, bref
);
328 std::vector
<Bstatement
*> stats
;
330 stats
.push_back(btemp
);
332 stats
.push_back(set
);
333 stats
.push_back(sinit
);
334 return context
->backend()->statement_list(stats
);
337 // Dump the AST representation for a variable declaration.
340 Variable_declaration_statement::do_dump_statement(
341 Ast_dump_context
* ast_dump_context
) const
343 ast_dump_context
->print_indent();
345 go_assert(var_
->is_variable());
346 ast_dump_context
->ostream() << "var " << this->var_
->name() << " ";
347 Variable
* var
= this->var_
->var_value();
350 ast_dump_context
->dump_type(var
->type());
351 ast_dump_context
->ostream() << " ";
353 if (var
->init() != NULL
)
355 ast_dump_context
->ostream() << "= ";
356 ast_dump_context
->dump_expression(var
->init());
358 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
361 // Make a variable declaration.
364 Statement::make_variable_declaration(Named_object
* var
)
366 return new Variable_declaration_statement(var
);
369 // Class Temporary_statement.
371 // Return the type of the temporary variable.
374 Temporary_statement::type() const
376 Type
* type
= this->type_
!= NULL
? this->type_
: this->init_
->type();
378 // Temporary variables cannot have a void type.
379 if (type
->is_void_type())
381 go_assert(saw_errors());
382 return Type::make_error_type();
390 Temporary_statement::do_traverse(Traverse
* traverse
)
392 if (this->type_
!= NULL
393 && this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
394 return TRAVERSE_EXIT
;
395 if (this->init_
== NULL
)
396 return TRAVERSE_CONTINUE
;
398 return this->traverse_expression(traverse
, &this->init_
);
401 // Traverse assignments.
404 Temporary_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
406 if (this->init_
== NULL
)
408 tassign
->value(&this->init_
, true, true);
415 Temporary_statement::do_determine_types()
417 if (this->type_
!= NULL
&& this->type_
->is_abstract())
418 this->type_
= this->type_
->make_non_abstract_type();
420 if (this->init_
!= NULL
)
422 if (this->type_
== NULL
)
423 this->init_
->determine_type_no_context();
426 Type_context
context(this->type_
, false);
427 this->init_
->determine_type(&context
);
431 if (this->type_
== NULL
)
433 this->type_
= this->init_
->type();
434 go_assert(!this->type_
->is_abstract());
441 Temporary_statement::do_check_types(Gogo
*)
443 if (this->type_
!= NULL
&& this->init_
!= NULL
)
446 if (!Type::are_assignable(this->type_
, this->init_
->type(), &reason
))
449 go_error_at(this->location(), "incompatible types in assignment");
451 go_error_at(this->location(), "incompatible types in assignment (%s)",
453 this->set_is_error();
458 // Flatten a temporary statement: add another temporary when it might
459 // be needed for interface conversion.
462 Temporary_statement::do_flatten(Gogo
*, Named_object
*, Block
*,
463 Statement_inserter
* inserter
)
465 if (this->type()->is_error_type()
466 || (this->init_
!= NULL
467 && this->init_
->is_error_expression()))
469 go_assert(saw_errors());
470 return Statement::make_error_statement(this->location());
473 if (this->type_
!= NULL
474 && this->init_
!= NULL
475 && !Type::are_identical(this->type_
, this->init_
->type(), false, NULL
)
476 && this->init_
->type()->interface_type() != NULL
477 && !this->init_
->is_variable())
479 Temporary_statement
*temp
=
480 Statement::make_temporary(NULL
, this->init_
, this->location());
481 inserter
->insert(temp
);
482 this->init_
= Expression::make_temporary_reference(temp
,
488 // Convert to backend representation.
491 Temporary_statement::do_get_backend(Translate_context
* context
)
493 go_assert(this->bvariable_
== NULL
);
495 Named_object
* function
= context
->function();
496 go_assert(function
!= NULL
);
497 Bfunction
* bfunction
= function
->func_value()->get_decl();
498 Btype
* btype
= this->type()->get_backend(context
->gogo());
501 if (this->init_
== NULL
)
503 else if (this->type_
== NULL
)
504 binit
= this->init_
->get_backend(context
);
507 Expression
* init
= Expression::convert_for_assignment(context
->gogo(),
511 binit
= init
->get_backend(context
);
515 binit
= context
->backend()->convert_expression(btype
, binit
,
518 Bstatement
* statement
;
520 context
->backend()->temporary_variable(bfunction
, context
->bblock(),
522 this->is_address_taken_
,
523 this->location(), &statement
);
527 // Return the backend variable.
530 Temporary_statement::get_backend_variable(Translate_context
* context
) const
532 if (this->bvariable_
== NULL
)
534 go_assert(saw_errors());
535 return context
->backend()->error_variable();
537 return this->bvariable_
;
540 // Dump the AST represemtation for a temporary statement
543 Temporary_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
545 ast_dump_context
->print_indent();
546 ast_dump_context
->dump_temp_variable_name(this);
547 if (this->type_
!= NULL
)
549 ast_dump_context
->ostream() << " ";
550 ast_dump_context
->dump_type(this->type_
);
552 if (this->init_
!= NULL
)
554 ast_dump_context
->ostream() << " = ";
555 ast_dump_context
->dump_expression(this->init_
);
557 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
560 // Make and initialize a temporary variable in BLOCK.
563 Statement::make_temporary(Type
* type
, Expression
* init
,
566 return new Temporary_statement(type
, init
, location
);
569 // The Move_subexpressions class is used to move all top-level
570 // subexpressions of an expression. This is used for things like
571 // index expressions in which we must evaluate the index value before
572 // it can be changed by a multiple assignment.
574 class Move_subexpressions
: public Traverse
577 Move_subexpressions(int skip
, Block
* block
)
578 : Traverse(traverse_expressions
),
579 skip_(skip
), block_(block
)
584 expression(Expression
**);
587 // The number of subexpressions to skip moving. This is used to
588 // avoid moving the array itself, as we only need to move the index.
590 // The block where new temporary variables should be added.
595 Move_subexpressions::expression(Expression
** pexpr
)
599 else if ((*pexpr
)->temporary_reference_expression() == NULL
600 && !(*pexpr
)->is_nil_expression()
601 && !(*pexpr
)->is_constant())
603 Location loc
= (*pexpr
)->location();
604 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
605 this->block_
->add_statement(temp
);
606 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
608 // We only need to move top-level subexpressions.
609 return TRAVERSE_SKIP_COMPONENTS
;
612 // The Move_ordered_evals class is used to find any subexpressions of
613 // an expression that have an evaluation order dependency. It creates
614 // temporary variables to hold them.
616 class Move_ordered_evals
: public Traverse
619 Move_ordered_evals(Block
* block
)
620 : Traverse(traverse_expressions
),
626 expression(Expression
**);
629 // The block where new temporary variables should be added.
634 Move_ordered_evals::expression(Expression
** pexpr
)
636 // We have to look at subexpressions first.
637 if ((*pexpr
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
638 return TRAVERSE_EXIT
;
641 if ((*pexpr
)->must_eval_subexpressions_in_order(&i
))
643 Move_subexpressions
ms(i
, this->block_
);
644 if ((*pexpr
)->traverse_subexpressions(&ms
) == TRAVERSE_EXIT
)
645 return TRAVERSE_EXIT
;
648 if ((*pexpr
)->must_eval_in_order())
650 Call_expression
* call
= (*pexpr
)->call_expression();
651 if (call
!= NULL
&& call
->is_multi_value_arg())
653 // A call expression which returns multiple results as an argument
654 // to another call must be handled specially. We can't create a
655 // temporary because there is no type to give it. Instead, group
656 // the caller and this multi-valued call argument and use a temporary
657 // variable to hold them.
658 return TRAVERSE_SKIP_COMPONENTS
;
661 Location loc
= (*pexpr
)->location();
662 Temporary_statement
* temp
= Statement::make_temporary(NULL
, *pexpr
, loc
);
663 this->block_
->add_statement(temp
);
664 *pexpr
= Expression::make_temporary_reference(temp
, loc
);
666 return TRAVERSE_SKIP_COMPONENTS
;
669 // Class Assignment_statement.
674 Assignment_statement::do_traverse(Traverse
* traverse
)
676 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
677 return TRAVERSE_EXIT
;
678 return this->traverse_expression(traverse
, &this->rhs_
);
682 Assignment_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
684 tassign
->assignment(&this->lhs_
, &this->rhs_
);
688 // Lower an assignment to a map index expression to a runtime function
692 Assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
695 Map_index_expression
* mie
= this->lhs_
->map_index_expression();
698 Location loc
= this->location();
700 Expression
* map
= mie
->map();
701 Map_type
* mt
= map
->type()->map_type();
704 go_assert(saw_errors());
705 return Statement::make_error_statement(loc
);
708 Block
* b
= new Block(enclosing
, loc
);
710 // Move out any subexpressions on the left hand side to make
711 // sure that functions are called in the required order.
712 Move_ordered_evals
moe(b
);
713 mie
->traverse_subexpressions(&moe
);
715 // Copy the key into a temporary so that we can take its address
716 // without pushing the value onto the heap.
718 // var key_temp KEY_TYPE = MAP_INDEX
719 Temporary_statement
* key_temp
= Statement::make_temporary(mt
->key_type(),
722 b
->add_statement(key_temp
);
724 // Copy the value into a temporary to ensure that it is
725 // evaluated before we add the key to the map. This may matter
726 // if the value is itself a reference to the map.
728 // var val_temp VAL_TYPE = RHS
729 Temporary_statement
* val_temp
= Statement::make_temporary(mt
->val_type(),
732 b
->add_statement(val_temp
);
734 // *mapassign(TYPE, MAP, &key_temp) = RHS
735 Expression
* a1
= Expression::make_type_descriptor(mt
, loc
);
736 Expression
* a2
= mie
->map();
737 Temporary_reference_expression
* ref
=
738 Expression::make_temporary_reference(key_temp
, loc
);
739 Expression
* a3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
740 Expression
* call
= Runtime::make_call(Runtime::MAPASSIGN
, loc
, 3,
742 Type
* ptrval_type
= Type::make_pointer_type(mt
->val_type());
743 call
= Expression::make_cast(ptrval_type
, call
, loc
);
745 Expression::make_dereference(call
, Expression::NIL_CHECK_NOT_NEEDED
,
747 ref
= Expression::make_temporary_reference(val_temp
, loc
);
748 b
->add_statement(Statement::make_assignment(indir
, ref
, loc
));
750 return Statement::make_block_statement(b
, loc
);
756 // Set types for the assignment.
759 Assignment_statement::do_determine_types()
761 this->lhs_
->determine_type_no_context();
762 Type
* rhs_context_type
= this->lhs_
->type();
763 if (rhs_context_type
->is_sink_type())
764 rhs_context_type
= NULL
;
765 Type_context
context(rhs_context_type
, false);
766 this->rhs_
->determine_type(&context
);
769 // Check types for an assignment.
772 Assignment_statement::do_check_types(Gogo
*)
774 // The left hand side must be either addressable, a map index
775 // expression, or the blank identifier.
776 if (!this->lhs_
->is_addressable()
777 && this->lhs_
->map_index_expression() == NULL
778 && !this->lhs_
->is_sink_expression())
780 if (!this->lhs_
->type()->is_error())
781 this->report_error(_("invalid left hand side of assignment"));
785 Type
* lhs_type
= this->lhs_
->type();
786 Type
* rhs_type
= this->rhs_
->type();
788 // Invalid assignment of nil to the blank identifier.
789 if (lhs_type
->is_sink_type()
790 && rhs_type
->is_nil_type())
792 this->report_error(_("use of untyped nil"));
797 if (!Type::are_assignable(lhs_type
, rhs_type
, &reason
))
800 go_error_at(this->location(), "incompatible types in assignment");
802 go_error_at(this->location(), "incompatible types in assignment (%s)",
804 this->set_is_error();
807 if (lhs_type
->is_error() || rhs_type
->is_error())
808 this->set_is_error();
811 // Flatten an assignment statement. We may need a temporary for
812 // interface conversion.
815 Assignment_statement::do_flatten(Gogo
*, Named_object
*, Block
*,
816 Statement_inserter
* inserter
)
818 if (this->lhs_
->is_error_expression()
819 || this->lhs_
->type()->is_error_type()
820 || this->rhs_
->is_error_expression()
821 || this->rhs_
->type()->is_error_type())
823 go_assert(saw_errors());
824 return Statement::make_error_statement(this->location());
827 if (!this->lhs_
->is_sink_expression()
828 && !Type::are_identical(this->lhs_
->type(), this->rhs_
->type(),
830 && this->rhs_
->type()->interface_type() != NULL
831 && !this->rhs_
->is_variable())
833 Temporary_statement
* temp
=
834 Statement::make_temporary(NULL
, this->rhs_
, this->location());
835 inserter
->insert(temp
);
836 this->rhs_
= Expression::make_temporary_reference(temp
,
842 // Convert an assignment statement to the backend representation.
845 Assignment_statement::do_get_backend(Translate_context
* context
)
847 if (this->lhs_
->is_sink_expression())
849 Bexpression
* rhs
= this->rhs_
->get_backend(context
);
850 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
851 return context
->backend()->expression_statement(bfunction
, rhs
);
854 Bexpression
* lhs
= this->lhs_
->get_backend(context
);
856 Expression::convert_for_assignment(context
->gogo(), this->lhs_
->type(),
857 this->rhs_
, this->location());
858 Bexpression
* rhs
= conv
->get_backend(context
);
859 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
860 return context
->backend()->assignment_statement(bfunction
, lhs
, rhs
,
864 // Dump the AST representation for an assignment statement.
867 Assignment_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
870 ast_dump_context
->print_indent();
871 ast_dump_context
->dump_expression(this->lhs_
);
872 ast_dump_context
->ostream() << " = " ;
873 ast_dump_context
->dump_expression(this->rhs_
);
874 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
877 // Make an assignment statement.
880 Statement::make_assignment(Expression
* lhs
, Expression
* rhs
,
883 return new Assignment_statement(lhs
, rhs
, location
);
886 // An assignment operation statement.
888 class Assignment_operation_statement
: public Statement
891 Assignment_operation_statement(Operator op
, Expression
* lhs
, Expression
* rhs
,
893 : Statement(STATEMENT_ASSIGNMENT_OPERATION
, location
),
894 op_(op
), lhs_(lhs
), rhs_(rhs
)
899 do_traverse(Traverse
*);
902 do_traverse_assignments(Traverse_assignments
*)
903 { go_unreachable(); }
906 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
909 do_get_backend(Translate_context
*)
910 { go_unreachable(); }
913 do_dump_statement(Ast_dump_context
*) const;
916 // The operator (OPERATOR_PLUSEQ, etc.).
927 Assignment_operation_statement::do_traverse(Traverse
* traverse
)
929 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
930 return TRAVERSE_EXIT
;
931 return this->traverse_expression(traverse
, &this->rhs_
);
934 // Lower an assignment operation statement to a regular assignment
938 Assignment_operation_statement::do_lower(Gogo
*, Named_object
*,
939 Block
* enclosing
, Statement_inserter
*)
941 Location loc
= this->location();
943 // We have to evaluate the left hand side expression only once. We
944 // do this by moving out any expression with side effects.
945 Block
* b
= new Block(enclosing
, loc
);
946 Move_ordered_evals
moe(b
);
947 this->lhs_
->traverse_subexpressions(&moe
);
949 Expression
* lval
= this->lhs_
->copy();
954 case OPERATOR_PLUSEQ
:
957 case OPERATOR_MINUSEQ
:
966 case OPERATOR_MULTEQ
:
975 case OPERATOR_LSHIFTEQ
:
976 op
= OPERATOR_LSHIFT
;
978 case OPERATOR_RSHIFTEQ
:
979 op
= OPERATOR_RSHIFT
;
984 case OPERATOR_BITCLEAREQ
:
985 op
= OPERATOR_BITCLEAR
;
991 Expression
* binop
= Expression::make_binary(op
, lval
, this->rhs_
, loc
);
992 Statement
* s
= Statement::make_assignment(this->lhs_
, binop
, loc
);
993 if (b
->statements()->empty())
1000 b
->add_statement(s
);
1001 return Statement::make_block_statement(b
, loc
);
1005 // Dump the AST representation for an assignment operation statement
1008 Assignment_operation_statement::do_dump_statement(
1009 Ast_dump_context
* ast_dump_context
) const
1011 ast_dump_context
->print_indent();
1012 ast_dump_context
->dump_expression(this->lhs_
);
1013 ast_dump_context
->dump_operator(this->op_
);
1014 ast_dump_context
->dump_expression(this->rhs_
);
1015 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1018 // Make an assignment operation statement.
1021 Statement::make_assignment_operation(Operator op
, Expression
* lhs
,
1022 Expression
* rhs
, Location location
)
1024 return new Assignment_operation_statement(op
, lhs
, rhs
, location
);
1027 // A tuple assignment statement. This differs from an assignment
1028 // statement in that the right-hand-side expressions are evaluated in
1031 class Tuple_assignment_statement
: public Statement
1034 Tuple_assignment_statement(Expression_list
* lhs
, Expression_list
* rhs
,
1036 : Statement(STATEMENT_TUPLE_ASSIGNMENT
, location
),
1037 lhs_(lhs
), rhs_(rhs
)
1042 do_traverse(Traverse
* traverse
);
1045 do_traverse_assignments(Traverse_assignments
*)
1046 { go_unreachable(); }
1049 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1052 do_get_backend(Translate_context
*)
1053 { go_unreachable(); }
1056 do_dump_statement(Ast_dump_context
*) const;
1059 // Left hand side--a list of lvalues.
1060 Expression_list
* lhs_
;
1061 // Right hand side--a list of rvalues.
1062 Expression_list
* rhs_
;
1068 Tuple_assignment_statement::do_traverse(Traverse
* traverse
)
1070 if (this->traverse_expression_list(traverse
, this->lhs_
) == TRAVERSE_EXIT
)
1071 return TRAVERSE_EXIT
;
1072 return this->traverse_expression_list(traverse
, this->rhs_
);
1075 // Lower a tuple assignment. We use temporary variables to split it
1076 // up into a set of single assignments.
1079 Tuple_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
1080 Statement_inserter
*)
1082 Location loc
= this->location();
1084 Block
* b
= new Block(enclosing
, loc
);
1086 // First move out any subexpressions on the left hand side. The
1087 // right hand side will be evaluated in the required order anyhow.
1088 Move_ordered_evals
moe(b
);
1089 for (Expression_list::iterator plhs
= this->lhs_
->begin();
1090 plhs
!= this->lhs_
->end();
1092 Expression::traverse(&*plhs
, &moe
);
1094 std::vector
<Temporary_statement
*> temps
;
1095 temps
.reserve(this->lhs_
->size());
1097 Expression_list::const_iterator prhs
= this->rhs_
->begin();
1098 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
1099 plhs
!= this->lhs_
->end();
1102 go_assert(prhs
!= this->rhs_
->end());
1104 if ((*plhs
)->is_error_expression()
1105 || (*plhs
)->type()->is_error()
1106 || (*prhs
)->is_error_expression()
1107 || (*prhs
)->type()->is_error())
1110 if ((*plhs
)->is_sink_expression())
1112 if ((*prhs
)->type()->is_nil_type())
1113 this->report_error(_("use of untyped nil"));
1115 b
->add_statement(Statement::make_statement(*prhs
, true));
1119 Temporary_statement
* temp
= Statement::make_temporary((*plhs
)->type(),
1121 b
->add_statement(temp
);
1122 temps
.push_back(temp
);
1125 go_assert(prhs
== this->rhs_
->end());
1127 prhs
= this->rhs_
->begin();
1128 std::vector
<Temporary_statement
*>::const_iterator ptemp
= temps
.begin();
1129 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
1130 plhs
!= this->lhs_
->end();
1133 if ((*plhs
)->is_error_expression()
1134 || (*plhs
)->type()->is_error()
1135 || (*prhs
)->is_error_expression()
1136 || (*prhs
)->type()->is_error())
1139 if ((*plhs
)->is_sink_expression())
1142 Expression
* ref
= Expression::make_temporary_reference(*ptemp
, loc
);
1143 b
->add_statement(Statement::make_assignment(*plhs
, ref
, loc
));
1146 go_assert(ptemp
== temps
.end() || saw_errors());
1148 return Statement::make_block_statement(b
, loc
);
1151 // Dump the AST representation for a tuple assignment statement.
1154 Tuple_assignment_statement::do_dump_statement(
1155 Ast_dump_context
* ast_dump_context
) const
1157 ast_dump_context
->print_indent();
1158 ast_dump_context
->dump_expression_list(this->lhs_
);
1159 ast_dump_context
->ostream() << " = ";
1160 ast_dump_context
->dump_expression_list(this->rhs_
);
1161 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1164 // Make a tuple assignment statement.
1167 Statement::make_tuple_assignment(Expression_list
* lhs
, Expression_list
* rhs
,
1170 return new Tuple_assignment_statement(lhs
, rhs
, location
);
1173 // A tuple assignment from a map index expression.
1176 class Tuple_map_assignment_statement
: public Statement
1179 Tuple_map_assignment_statement(Expression
* val
, Expression
* present
,
1180 Expression
* map_index
,
1182 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT
, location
),
1183 val_(val
), present_(present
), map_index_(map_index
)
1188 do_traverse(Traverse
* traverse
);
1191 do_traverse_assignments(Traverse_assignments
*)
1192 { go_unreachable(); }
1195 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1198 do_get_backend(Translate_context
*)
1199 { go_unreachable(); }
1202 do_dump_statement(Ast_dump_context
*) const;
1205 // Lvalue which receives the value from the map.
1207 // Lvalue which receives whether the key value was present.
1208 Expression
* present_
;
1209 // The map index expression.
1210 Expression
* map_index_
;
1216 Tuple_map_assignment_statement::do_traverse(Traverse
* traverse
)
1218 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1219 || this->traverse_expression(traverse
, &this->present_
) == TRAVERSE_EXIT
)
1220 return TRAVERSE_EXIT
;
1221 return this->traverse_expression(traverse
, &this->map_index_
);
1224 // Lower a tuple map assignment.
1227 Tuple_map_assignment_statement::do_lower(Gogo
* gogo
, Named_object
*,
1228 Block
* enclosing
, Statement_inserter
*)
1230 Location loc
= this->location();
1232 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1233 if (map_index
== NULL
)
1235 this->report_error(_("expected map index on right hand side"));
1236 return Statement::make_error_statement(loc
);
1238 Map_type
* map_type
= map_index
->get_map_type();
1239 if (map_type
== NULL
)
1240 return Statement::make_error_statement(loc
);
1242 Block
* b
= new Block(enclosing
, loc
);
1244 // Move out any subexpressions to make sure that functions are
1245 // called in the required order.
1246 Move_ordered_evals
moe(b
);
1247 this->val_
->traverse_subexpressions(&moe
);
1248 this->present_
->traverse_subexpressions(&moe
);
1250 // Copy the key value into a temporary so that we can take its
1251 // address without pushing the value onto the heap.
1253 // var key_temp KEY_TYPE = MAP_INDEX
1254 Temporary_statement
* key_temp
=
1255 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1256 b
->add_statement(key_temp
);
1258 // var val_ptr_temp *VAL_TYPE
1259 Type
* val_ptr_type
= Type::make_pointer_type(map_type
->val_type());
1260 Temporary_statement
* val_ptr_temp
= Statement::make_temporary(val_ptr_type
,
1262 b
->add_statement(val_ptr_temp
);
1264 // var present_temp bool
1265 Temporary_statement
* present_temp
=
1266 Statement::make_temporary((this->present_
->type()->is_sink_type())
1267 ? Type::make_boolean_type()
1268 : this->present_
->type(),
1270 b
->add_statement(present_temp
);
1272 // val_ptr_temp, present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp)
1273 Expression
* a1
= Expression::make_type_descriptor(map_type
, loc
);
1274 Expression
* a2
= map_index
->map();
1275 Temporary_reference_expression
* ref
=
1276 Expression::make_temporary_reference(key_temp
, loc
);
1277 Expression
* a3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1278 Expression
* a4
= map_type
->fat_zero_value(gogo
);
1279 Call_expression
* call
;
1281 call
= Runtime::make_call(Runtime::MAPACCESS2
, loc
, 3, a1
, a2
, a3
);
1283 call
= Runtime::make_call(Runtime::MAPACCESS2_FAT
, loc
, 4, a1
, a2
, a3
, a4
);
1284 ref
= Expression::make_temporary_reference(val_ptr_temp
, loc
);
1285 ref
->set_is_lvalue();
1286 Expression
* res
= Expression::make_call_result(call
, 0);
1287 res
= Expression::make_unsafe_cast(val_ptr_type
, res
, loc
);
1288 Statement
* s
= Statement::make_assignment(ref
, res
, loc
);
1289 b
->add_statement(s
);
1290 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1291 ref
->set_is_lvalue();
1292 res
= Expression::make_call_result(call
, 1);
1293 s
= Statement::make_assignment(ref
, res
, loc
);
1294 b
->add_statement(s
);
1296 // val = *val__ptr_temp
1297 ref
= Expression::make_temporary_reference(val_ptr_temp
, loc
);
1299 Expression::make_dereference(ref
, Expression::NIL_CHECK_NOT_NEEDED
, loc
);
1300 s
= Statement::make_assignment(this->val_
, ind
, loc
);
1301 b
->add_statement(s
);
1303 // present = present_temp
1304 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1305 s
= Statement::make_assignment(this->present_
, ref
, loc
);
1306 b
->add_statement(s
);
1308 return Statement::make_block_statement(b
, loc
);
1311 // Dump the AST representation for a tuple map assignment statement.
1314 Tuple_map_assignment_statement::do_dump_statement(
1315 Ast_dump_context
* ast_dump_context
) const
1317 ast_dump_context
->print_indent();
1318 ast_dump_context
->dump_expression(this->val_
);
1319 ast_dump_context
->ostream() << ", ";
1320 ast_dump_context
->dump_expression(this->present_
);
1321 ast_dump_context
->ostream() << " = ";
1322 ast_dump_context
->dump_expression(this->map_index_
);
1323 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1326 // Make a map assignment statement which returns a pair of values.
1329 Statement::make_tuple_map_assignment(Expression
* val
, Expression
* present
,
1330 Expression
* map_index
,
1333 return new Tuple_map_assignment_statement(val
, present
, map_index
, location
);
1336 // A tuple assignment from a receive statement.
1338 class Tuple_receive_assignment_statement
: public Statement
1341 Tuple_receive_assignment_statement(Expression
* val
, Expression
* closed
,
1342 Expression
* channel
, Location location
)
1343 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
, location
),
1344 val_(val
), closed_(closed
), channel_(channel
)
1349 do_traverse(Traverse
* traverse
);
1352 do_traverse_assignments(Traverse_assignments
*)
1353 { go_unreachable(); }
1356 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1359 do_get_backend(Translate_context
*)
1360 { go_unreachable(); }
1363 do_dump_statement(Ast_dump_context
*) const;
1366 // Lvalue which receives the value from the channel.
1368 // Lvalue which receives whether the channel is closed.
1369 Expression
* closed_
;
1370 // The channel on which we receive the value.
1371 Expression
* channel_
;
1377 Tuple_receive_assignment_statement::do_traverse(Traverse
* traverse
)
1379 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1380 || this->traverse_expression(traverse
, &this->closed_
) == TRAVERSE_EXIT
)
1381 return TRAVERSE_EXIT
;
1382 return this->traverse_expression(traverse
, &this->channel_
);
1385 // Lower to a function call.
1388 Tuple_receive_assignment_statement::do_lower(Gogo
*, Named_object
*,
1390 Statement_inserter
*)
1392 Location loc
= this->location();
1394 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
1395 if (channel_type
== NULL
)
1397 this->report_error(_("expected channel"));
1398 return Statement::make_error_statement(loc
);
1400 if (!channel_type
->may_receive())
1402 this->report_error(_("invalid receive on send-only channel"));
1403 return Statement::make_error_statement(loc
);
1406 Block
* b
= new Block(enclosing
, loc
);
1408 // Make sure that any subexpressions on the left hand side are
1409 // evaluated in the right order.
1410 Move_ordered_evals
moe(b
);
1411 this->val_
->traverse_subexpressions(&moe
);
1412 this->closed_
->traverse_subexpressions(&moe
);
1414 // var val_temp ELEMENT_TYPE
1415 Temporary_statement
* val_temp
=
1416 Statement::make_temporary(channel_type
->element_type(), NULL
, loc
);
1417 b
->add_statement(val_temp
);
1419 // var closed_temp bool
1420 Temporary_statement
* closed_temp
=
1421 Statement::make_temporary((this->closed_
->type()->is_sink_type())
1422 ? Type::make_boolean_type()
1423 : this->closed_
->type(),
1425 b
->add_statement(closed_temp
);
1427 // closed_temp = chanrecv2(channel, &val_temp)
1428 Temporary_reference_expression
* ref
=
1429 Expression::make_temporary_reference(val_temp
, loc
);
1430 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1431 Expression
* call
= Runtime::make_call(Runtime::CHANRECV2
,
1432 loc
, 2, this->channel_
, p2
);
1433 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1434 ref
->set_is_lvalue();
1435 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1436 b
->add_statement(s
);
1439 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1440 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1441 b
->add_statement(s
);
1443 // closed = closed_temp
1444 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1445 s
= Statement::make_assignment(this->closed_
, ref
, loc
);
1446 b
->add_statement(s
);
1448 return Statement::make_block_statement(b
, loc
);
1451 // Dump the AST representation for a tuple receive statement.
1454 Tuple_receive_assignment_statement::do_dump_statement(
1455 Ast_dump_context
* ast_dump_context
) const
1457 ast_dump_context
->print_indent();
1458 ast_dump_context
->dump_expression(this->val_
);
1459 ast_dump_context
->ostream() << ", ";
1460 ast_dump_context
->dump_expression(this->closed_
);
1461 ast_dump_context
->ostream() << " <- ";
1462 ast_dump_context
->dump_expression(this->channel_
);
1463 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1466 // Make a nonblocking receive statement.
1469 Statement::make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
1470 Expression
* channel
,
1473 return new Tuple_receive_assignment_statement(val
, closed
, channel
,
1477 // An assignment to a pair of values from a type guard. This is a
1478 // conditional type guard. v, ok = i.(type).
1480 class Tuple_type_guard_assignment_statement
: public Statement
1483 Tuple_type_guard_assignment_statement(Expression
* val
, Expression
* ok
,
1484 Expression
* expr
, Type
* type
,
1486 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
, location
),
1487 val_(val
), ok_(ok
), expr_(expr
), type_(type
)
1492 do_traverse(Traverse
*);
1495 do_traverse_assignments(Traverse_assignments
*)
1496 { go_unreachable(); }
1499 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1502 do_get_backend(Translate_context
*)
1503 { go_unreachable(); }
1506 do_dump_statement(Ast_dump_context
*) const;
1510 lower_to_type(Runtime::Function
);
1513 lower_to_object_type(Block
*, Runtime::Function
);
1515 // The variable which recieves the converted value.
1517 // The variable which receives the indication of success.
1519 // The expression being converted.
1521 // The type to which the expression is being converted.
1525 // Traverse a type guard tuple assignment.
1528 Tuple_type_guard_assignment_statement::do_traverse(Traverse
* traverse
)
1530 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1531 || this->traverse_expression(traverse
, &this->ok_
) == TRAVERSE_EXIT
1532 || this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
1533 return TRAVERSE_EXIT
;
1534 return this->traverse_expression(traverse
, &this->expr_
);
1537 // Lower to a function call.
1540 Tuple_type_guard_assignment_statement::do_lower(Gogo
*, Named_object
*,
1542 Statement_inserter
*)
1544 Location loc
= this->location();
1546 Type
* expr_type
= this->expr_
->type();
1547 if (expr_type
->interface_type() == NULL
)
1549 if (!expr_type
->is_error() && !this->type_
->is_error())
1550 this->report_error(_("type assertion only valid for interface types"));
1551 return Statement::make_error_statement(loc
);
1554 Block
* b
= new Block(enclosing
, loc
);
1556 // Make sure that any subexpressions on the left hand side are
1557 // evaluated in the right order.
1558 Move_ordered_evals
moe(b
);
1559 this->val_
->traverse_subexpressions(&moe
);
1560 this->ok_
->traverse_subexpressions(&moe
);
1562 bool expr_is_empty
= expr_type
->interface_type()->is_empty();
1563 Call_expression
* call
;
1564 if (this->type_
->interface_type() != NULL
)
1566 if (this->type_
->interface_type()->is_empty())
1567 call
= Runtime::make_call((expr_is_empty
1568 ? Runtime::IFACEE2E2
1569 : Runtime::IFACEI2E2
),
1570 loc
, 1, this->expr_
);
1572 call
= this->lower_to_type(expr_is_empty
1573 ? Runtime::IFACEE2I2
1574 : Runtime::IFACEI2I2
);
1576 else if (this->type_
->points_to() != NULL
)
1577 call
= this->lower_to_type(expr_is_empty
1578 ? Runtime::IFACEE2T2P
1579 : Runtime::IFACEI2T2P
);
1582 this->lower_to_object_type(b
,
1584 ? Runtime::IFACEE2T2
1585 : Runtime::IFACEI2T2
));
1591 Expression
* res
= Expression::make_call_result(call
, 0);
1592 res
= Expression::make_unsafe_cast(this->type_
, res
, loc
);
1593 Statement
* s
= Statement::make_assignment(this->val_
, res
, loc
);
1594 b
->add_statement(s
);
1596 res
= Expression::make_call_result(call
, 1);
1597 s
= Statement::make_assignment(this->ok_
, res
, loc
);
1598 b
->add_statement(s
);
1601 return Statement::make_block_statement(b
, loc
);
1604 // Lower a conversion to a non-empty interface type or a pointer type.
1607 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code
)
1609 Location loc
= this->location();
1610 return Runtime::make_call(code
, loc
, 2,
1611 Expression::make_type_descriptor(this->type_
, loc
),
1615 // Lower a conversion to a non-interface non-pointer type.
1618 Tuple_type_guard_assignment_statement::lower_to_object_type(
1620 Runtime::Function code
)
1622 Location loc
= this->location();
1624 // var val_temp TYPE
1625 Temporary_statement
* val_temp
= Statement::make_temporary(this->type_
,
1627 b
->add_statement(val_temp
);
1629 // ok = CODE(type_descriptor, expr, &val_temp)
1630 Expression
* p1
= Expression::make_type_descriptor(this->type_
, loc
);
1631 Expression
* ref
= Expression::make_temporary_reference(val_temp
, loc
);
1632 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1633 Expression
* call
= Runtime::make_call(code
, loc
, 3, p1
, this->expr_
, p3
);
1634 Statement
* s
= Statement::make_assignment(this->ok_
, call
, loc
);
1635 b
->add_statement(s
);
1638 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1639 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1640 b
->add_statement(s
);
1643 // Dump the AST representation for a tuple type guard statement.
1646 Tuple_type_guard_assignment_statement::do_dump_statement(
1647 Ast_dump_context
* ast_dump_context
) const
1649 ast_dump_context
->print_indent();
1650 ast_dump_context
->dump_expression(this->val_
);
1651 ast_dump_context
->ostream() << ", ";
1652 ast_dump_context
->dump_expression(this->ok_
);
1653 ast_dump_context
->ostream() << " = ";
1654 ast_dump_context
->dump_expression(this->expr_
);
1655 ast_dump_context
->ostream() << " . ";
1656 ast_dump_context
->dump_type(this->type_
);
1657 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1660 // Make an assignment from a type guard to a pair of variables.
1663 Statement::make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
1664 Expression
* expr
, Type
* type
,
1667 return new Tuple_type_guard_assignment_statement(val
, ok
, expr
, type
,
1671 // Class Expression_statement.
1675 Expression_statement::Expression_statement(Expression
* expr
, bool is_ignored
)
1676 : Statement(STATEMENT_EXPRESSION
, expr
->location()),
1677 expr_(expr
), is_ignored_(is_ignored
)
1684 Expression_statement::do_determine_types()
1686 this->expr_
->determine_type_no_context();
1689 // Check the types of an expression statement. The only check we do
1690 // is to possibly give an error about discarding the value of the
1694 Expression_statement::do_check_types(Gogo
*)
1696 if (!this->is_ignored_
)
1697 this->expr_
->discarding_value();
1700 // An expression statement is only a terminating statement if it is
1704 Expression_statement::do_may_fall_through() const
1706 const Call_expression
* call
= this->expr_
->call_expression();
1709 const Expression
* fn
= call
->fn();
1710 // panic is still an unknown named object.
1711 const Unknown_expression
* ue
= fn
->unknown_expression();
1714 Named_object
* no
= ue
->named_object();
1716 if (no
->is_unknown())
1717 no
= no
->unknown_value()->real_named_object();
1720 Function_type
* fntype
;
1721 if (no
->is_function())
1722 fntype
= no
->func_value()->type();
1723 else if (no
->is_function_declaration())
1724 fntype
= no
->func_declaration_value()->type();
1728 // The builtin function panic does not return.
1729 if (fntype
!= NULL
&& fntype
->is_builtin() && no
->name() == "panic")
1737 // Convert to backend representation.
1740 Expression_statement::do_get_backend(Translate_context
* context
)
1742 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
1743 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
1744 return context
->backend()->expression_statement(bfunction
, bexpr
);
1747 // Dump the AST representation for an expression statement
1750 Expression_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
1753 ast_dump_context
->print_indent();
1754 ast_dump_context
->dump_expression(expr_
);
1755 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1758 // Make an expression statement from an Expression.
1761 Statement::make_statement(Expression
* expr
, bool is_ignored
)
1763 return new Expression_statement(expr
, is_ignored
);
1766 // Convert a block to the backend representation of a statement.
1769 Block_statement::do_get_backend(Translate_context
* context
)
1771 Bblock
* bblock
= this->block_
->get_backend(context
);
1772 return context
->backend()->block_statement(bblock
);
1775 // Dump the AST for a block statement
1778 Block_statement::do_dump_statement(Ast_dump_context
*) const
1780 // block statement braces are dumped when traversing.
1783 // Make a block statement.
1786 Statement::make_block_statement(Block
* block
, Location location
)
1788 return new Block_statement(block
, location
);
1791 // An increment or decrement statement.
1793 class Inc_dec_statement
: public Statement
1796 Inc_dec_statement(bool is_inc
, Expression
* expr
)
1797 : Statement(STATEMENT_INCDEC
, expr
->location()),
1798 expr_(expr
), is_inc_(is_inc
)
1803 do_traverse(Traverse
* traverse
)
1804 { return this->traverse_expression(traverse
, &this->expr_
); }
1807 do_traverse_assignments(Traverse_assignments
*)
1808 { go_unreachable(); }
1811 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1814 do_get_backend(Translate_context
*)
1815 { go_unreachable(); }
1818 do_dump_statement(Ast_dump_context
*) const;
1821 // The l-value to increment or decrement.
1823 // Whether to increment or decrement.
1827 // Lower to += or -=.
1830 Inc_dec_statement::do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
1832 Location loc
= this->location();
1833 if (!this->expr_
->type()->is_numeric_type())
1835 this->report_error("increment or decrement of non-numeric type");
1836 return Statement::make_error_statement(loc
);
1838 Expression
* oexpr
= Expression::make_integer_ul(1, this->expr_
->type(), loc
);
1839 Operator op
= this->is_inc_
? OPERATOR_PLUSEQ
: OPERATOR_MINUSEQ
;
1840 return Statement::make_assignment_operation(op
, this->expr_
, oexpr
, loc
);
1843 // Dump the AST representation for a inc/dec statement.
1846 Inc_dec_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
1848 ast_dump_context
->print_indent();
1849 ast_dump_context
->dump_expression(expr_
);
1850 ast_dump_context
->ostream() << (is_inc_
? "++": "--") << dsuffix(location()) << std::endl
;
1853 // Make an increment statement.
1856 Statement::make_inc_statement(Expression
* expr
)
1858 return new Inc_dec_statement(true, expr
);
1861 // Make a decrement statement.
1864 Statement::make_dec_statement(Expression
* expr
)
1866 return new Inc_dec_statement(false, expr
);
1869 // Class Thunk_statement. This is the base class for go and defer
1874 Thunk_statement::Thunk_statement(Statement_classification classification
,
1875 Call_expression
* call
,
1877 : Statement(classification
, location
),
1878 call_(call
), struct_type_(NULL
)
1882 // Return whether this is a simple statement which does not require a
1886 Thunk_statement::is_simple(Function_type
* fntype
) const
1888 // We need a thunk to call a method, or to pass a variable number of
1890 if (fntype
->is_method() || fntype
->is_varargs())
1893 // A defer statement requires a thunk to set up for whether the
1894 // function can call recover.
1895 if (this->classification() == STATEMENT_DEFER
)
1898 // We can only permit a single parameter of pointer type.
1899 const Typed_identifier_list
* parameters
= fntype
->parameters();
1900 if (parameters
!= NULL
1901 && (parameters
->size() > 1
1902 || (parameters
->size() == 1
1903 && parameters
->begin()->type()->points_to() == NULL
)))
1906 // If the function returns multiple values, or returns a type other
1907 // than integer, floating point, or pointer, then it may get a
1908 // hidden first parameter, in which case we need the more
1909 // complicated approach. This is true even though we are going to
1910 // ignore the return value.
1911 const Typed_identifier_list
* results
= fntype
->results();
1913 && (results
->size() > 1
1914 || (results
->size() == 1
1915 && !results
->begin()->type()->is_basic_type()
1916 && results
->begin()->type()->points_to() == NULL
)))
1919 // If this calls something that is not a simple function, then we
1921 Expression
* fn
= this->call_
->call_expression()->fn();
1922 if (fn
->func_expression() == NULL
)
1925 // If the function uses a closure, then we need a thunk. FIXME: We
1926 // could accept a zero argument function with a closure.
1927 if (fn
->func_expression()->closure() != NULL
)
1933 // Traverse a thunk statement.
1936 Thunk_statement::do_traverse(Traverse
* traverse
)
1938 return this->traverse_expression(traverse
, &this->call_
);
1941 // We implement traverse_assignment for a thunk statement because it
1942 // effectively copies the function call.
1945 Thunk_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
1947 Expression
* fn
= this->call_
->call_expression()->fn();
1948 Expression
* fn2
= fn
;
1949 tassign
->value(&fn2
, true, false);
1953 // Determine types in a thunk statement.
1956 Thunk_statement::do_determine_types()
1958 this->call_
->determine_type_no_context();
1960 // Now that we know the types of the call, build the struct used to
1962 Call_expression
* ce
= this->call_
->call_expression();
1965 Function_type
* fntype
= ce
->get_function_type();
1966 if (fntype
!= NULL
&& !this->is_simple(fntype
))
1967 this->struct_type_
= this->build_struct(fntype
);
1970 // Check types in a thunk statement.
1973 Thunk_statement::do_check_types(Gogo
*)
1975 if (!this->call_
->discarding_value())
1977 Call_expression
* ce
= this->call_
->call_expression();
1980 if (!this->call_
->is_error_expression())
1981 this->report_error("expected call expression");
1986 // The Traverse class used to find and simplify thunk statements.
1988 class Simplify_thunk_traverse
: public Traverse
1991 Simplify_thunk_traverse(Gogo
* gogo
)
1992 : Traverse(traverse_functions
| traverse_blocks
),
1993 gogo_(gogo
), function_(NULL
)
1997 function(Named_object
*);
2005 // The function we are traversing.
2006 Named_object
* function_
;
2009 // Keep track of the current function while looking for thunks.
2012 Simplify_thunk_traverse::function(Named_object
* no
)
2014 go_assert(this->function_
== NULL
);
2015 this->function_
= no
;
2016 int t
= no
->func_value()->traverse(this);
2017 this->function_
= NULL
;
2018 if (t
== TRAVERSE_EXIT
)
2020 return TRAVERSE_SKIP_COMPONENTS
;
2023 // Look for thunks in a block.
2026 Simplify_thunk_traverse::block(Block
* b
)
2028 // The parser ensures that thunk statements always appear at the end
2030 if (b
->statements()->size() < 1)
2031 return TRAVERSE_CONTINUE
;
2032 Thunk_statement
* stat
= b
->statements()->back()->thunk_statement();
2034 return TRAVERSE_CONTINUE
;
2035 if (stat
->simplify_statement(this->gogo_
, this->function_
, b
))
2036 return TRAVERSE_SKIP_COMPONENTS
;
2037 return TRAVERSE_CONTINUE
;
2040 // Simplify all thunk statements.
2043 Gogo::simplify_thunk_statements()
2045 Simplify_thunk_traverse
thunk_traverse(this);
2046 this->traverse(&thunk_traverse
);
2049 // Return true if the thunk function is a constant, which means that
2050 // it does not need to be passed to the thunk routine.
2053 Thunk_statement::is_constant_function() const
2055 Call_expression
* ce
= this->call_
->call_expression();
2056 Function_type
* fntype
= ce
->get_function_type();
2059 go_assert(saw_errors());
2062 if (fntype
->is_builtin())
2064 Expression
* fn
= ce
->fn();
2065 if (fn
->func_expression() != NULL
)
2066 return fn
->func_expression()->closure() == NULL
;
2067 if (fn
->interface_field_reference_expression() != NULL
)
2072 // Simplify complex thunk statements into simple ones. A complicated
2073 // thunk statement is one which takes anything other than zero
2074 // parameters or a single pointer parameter. We rewrite it into code
2075 // which allocates a struct, stores the parameter values into the
2076 // struct, and does a simple go or defer statement which passes the
2077 // struct to a thunk. The thunk does the real call.
2080 Thunk_statement::simplify_statement(Gogo
* gogo
, Named_object
* function
,
2083 if (this->classification() == STATEMENT_ERROR
)
2085 if (this->call_
->is_error_expression())
2088 if (this->classification() == STATEMENT_DEFER
)
2090 // Make sure that the defer stack exists for the function. We
2091 // will use when converting this statement to the backend
2092 // representation, but we want it to exist when we start
2093 // converting the function.
2094 function
->func_value()->defer_stack(this->location());
2097 Call_expression
* ce
= this->call_
->call_expression();
2098 Function_type
* fntype
= ce
->get_function_type();
2101 go_assert(saw_errors());
2102 this->set_is_error();
2105 if (this->is_simple(fntype
))
2108 Expression
* fn
= ce
->fn();
2109 Interface_field_reference_expression
* interface_method
=
2110 fn
->interface_field_reference_expression();
2112 Location location
= this->location();
2114 bool is_constant_function
= this->is_constant_function();
2115 Temporary_statement
* fn_temp
= NULL
;
2116 if (!is_constant_function
)
2118 fn_temp
= Statement::make_temporary(NULL
, fn
, location
);
2119 block
->insert_statement_before(block
->statements()->size() - 1, fn_temp
);
2120 fn
= Expression::make_temporary_reference(fn_temp
, location
);
2123 std::string thunk_name
= gogo
->thunk_name();
2126 this->build_thunk(gogo
, thunk_name
);
2128 // Generate code to call the thunk.
2130 // Get the values to store into the struct which is the single
2131 // argument to the thunk.
2133 Expression_list
* vals
= new Expression_list();
2134 if (!is_constant_function
)
2135 vals
->push_back(fn
);
2137 if (interface_method
!= NULL
)
2138 vals
->push_back(interface_method
->expr());
2140 if (ce
->args() != NULL
)
2142 for (Expression_list::const_iterator p
= ce
->args()->begin();
2143 p
!= ce
->args()->end();
2146 if ((*p
)->is_constant())
2148 vals
->push_back(*p
);
2152 // Build the struct.
2153 Expression
* constructor
=
2154 Expression::make_struct_composite_literal(this->struct_type_
, vals
,
2157 // Allocate the initialized struct on the heap.
2158 constructor
= Expression::make_heap_expression(constructor
, location
);
2159 if ((Node::make_node(this)->encoding() & ESCAPE_MASK
) == Node::ESCAPE_NONE
)
2160 constructor
->heap_expression()->set_allocate_on_stack();
2162 // Throw an error if the function is nil. This is so that for `go
2163 // nil` we get a backtrace from the go statement, rather than a
2164 // useless backtrace from the brand new goroutine.
2165 Expression
* param
= constructor
;
2166 if (!is_constant_function
)
2168 fn
= Expression::make_temporary_reference(fn_temp
, location
);
2169 Expression
* nil
= Expression::make_nil(location
);
2170 Expression
* isnil
= Expression::make_binary(OPERATOR_EQEQ
, fn
, nil
,
2172 Expression
* crash
= gogo
->runtime_error(RUNTIME_ERROR_GO_NIL
, location
);
2173 crash
= Expression::make_conditional(isnil
, crash
,
2174 Expression::make_nil(location
),
2176 param
= Expression::make_compound(crash
, constructor
, location
);
2179 // Look up the thunk.
2180 Named_object
* named_thunk
= gogo
->lookup(thunk_name
, NULL
);
2181 go_assert(named_thunk
!= NULL
&& named_thunk
->is_function());
2184 Expression
* func
= Expression::make_func_reference(named_thunk
, NULL
,
2186 Expression_list
* params
= new Expression_list();
2187 params
->push_back(param
);
2188 Call_expression
* call
= Expression::make_call(func
, params
, false, location
);
2190 // Build the simple go or defer statement.
2192 if (this->classification() == STATEMENT_GO
)
2193 s
= Statement::make_go_statement(call
, location
);
2194 else if (this->classification() == STATEMENT_DEFER
)
2195 s
= Statement::make_defer_statement(call
, location
);
2199 // The current block should end with the go statement.
2200 go_assert(block
->statements()->size() >= 1);
2201 go_assert(block
->statements()->back() == this);
2202 block
->replace_statement(block
->statements()->size() - 1, s
);
2204 // We already ran the determine_types pass, so we need to run it now
2205 // for the new statement.
2206 s
->determine_types();
2209 gogo
->check_types_in_block(block
);
2211 // Return true to tell the block not to keep looking at statements.
2215 // Set the name to use for thunk parameter N.
2218 Thunk_statement::thunk_field_param(int n
, char* buf
, size_t buflen
)
2220 snprintf(buf
, buflen
, "a%d", n
);
2223 // Build a new struct type to hold the parameters for a complicated
2224 // thunk statement. FNTYPE is the type of the function call.
2227 Thunk_statement::build_struct(Function_type
* fntype
)
2229 Location location
= this->location();
2231 Struct_field_list
* fields
= new Struct_field_list();
2233 Call_expression
* ce
= this->call_
->call_expression();
2234 Expression
* fn
= ce
->fn();
2236 if (!this->is_constant_function())
2238 // The function to call.
2239 fields
->push_back(Struct_field(Typed_identifier("fn", fntype
,
2243 // If this thunk statement calls a method on an interface, we pass
2244 // the interface object to the thunk.
2245 Interface_field_reference_expression
* interface_method
=
2246 fn
->interface_field_reference_expression();
2247 if (interface_method
!= NULL
)
2249 Typed_identifier
tid("object", interface_method
->expr()->type(),
2251 fields
->push_back(Struct_field(tid
));
2254 // The predeclared recover function has no argument. However, we
2255 // add an argument when building recover thunks. Handle that here.
2256 if (ce
->is_recover_call())
2258 fields
->push_back(Struct_field(Typed_identifier("can_recover",
2259 Type::lookup_bool_type(),
2263 const Expression_list
* args
= ce
->args();
2267 for (Expression_list::const_iterator p
= args
->begin();
2271 if ((*p
)->is_constant())
2275 this->thunk_field_param(i
, buf
, sizeof buf
);
2276 fields
->push_back(Struct_field(Typed_identifier(buf
, (*p
)->type(),
2281 Struct_type
*st
= Type::make_struct_type(fields
, location
);
2282 st
->set_is_struct_incomparable();
2286 // Build the thunk we are going to call. This is a brand new, albeit
2287 // artificial, function.
2290 Thunk_statement::build_thunk(Gogo
* gogo
, const std::string
& thunk_name
)
2292 Location location
= this->location();
2294 Call_expression
* ce
= this->call_
->call_expression();
2296 bool may_call_recover
= false;
2297 if (this->classification() == STATEMENT_DEFER
)
2299 Func_expression
* fn
= ce
->fn()->func_expression();
2301 may_call_recover
= true;
2304 const Named_object
* no
= fn
->named_object();
2305 if (!no
->is_function())
2306 may_call_recover
= true;
2308 may_call_recover
= no
->func_value()->calls_recover();
2312 // Build the type of the thunk. The thunk takes a single parameter,
2313 // which is a pointer to the special structure we build.
2314 const char* const parameter_name
= "__go_thunk_parameter";
2315 Typed_identifier_list
* thunk_parameters
= new Typed_identifier_list();
2316 Type
* pointer_to_struct_type
= Type::make_pointer_type(this->struct_type_
);
2317 thunk_parameters
->push_back(Typed_identifier(parameter_name
,
2318 pointer_to_struct_type
,
2321 Typed_identifier_list
* thunk_results
= NULL
;
2322 if (may_call_recover
)
2324 // When deferring a function which may call recover, add a
2325 // return value, to disable tail call optimizations which will
2326 // break the way we check whether recover is permitted.
2327 thunk_results
= new Typed_identifier_list();
2328 thunk_results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
2332 Function_type
* thunk_type
= Type::make_function_type(NULL
, thunk_parameters
,
2336 // Start building the thunk.
2337 Named_object
* function
= gogo
->start_function(thunk_name
, thunk_type
, true,
2340 gogo
->start_block(location
);
2342 // For a defer statement, start with a call to
2343 // __go_set_defer_retaddr. */
2344 Label
* retaddr_label
= NULL
;
2345 if (may_call_recover
)
2347 retaddr_label
= gogo
->add_label_reference("retaddr", location
, false);
2348 Expression
* arg
= Expression::make_label_addr(retaddr_label
, location
);
2349 Expression
* call
= Runtime::make_call(Runtime::SETDEFERRETADDR
,
2352 // This is a hack to prevent the middle-end from deleting the
2354 gogo
->start_block(location
);
2355 gogo
->add_statement(Statement::make_goto_statement(retaddr_label
,
2357 Block
* then_block
= gogo
->finish_block(location
);
2358 then_block
->determine_types();
2360 Statement
* s
= Statement::make_if_statement(call
, then_block
, NULL
,
2362 s
->determine_types();
2363 gogo
->add_statement(s
);
2365 function
->func_value()->set_calls_defer_retaddr();
2368 // Get a reference to the parameter.
2369 Named_object
* named_parameter
= gogo
->lookup(parameter_name
, NULL
);
2370 go_assert(named_parameter
!= NULL
&& named_parameter
->is_variable());
2372 // Build the call. Note that the field names are the same as the
2373 // ones used in build_struct.
2374 Expression
* thunk_parameter
= Expression::make_var_reference(named_parameter
,
2377 Expression::make_dereference(thunk_parameter
,
2378 Expression::NIL_CHECK_NOT_NEEDED
,
2381 Interface_field_reference_expression
* interface_method
=
2382 ce
->fn()->interface_field_reference_expression();
2384 Expression
* func_to_call
;
2385 unsigned int next_index
;
2386 if (this->is_constant_function())
2388 func_to_call
= ce
->fn();
2393 func_to_call
= Expression::make_field_reference(thunk_parameter
,
2398 if (interface_method
!= NULL
)
2400 // The main program passes the interface object.
2401 go_assert(next_index
== 0);
2402 Expression
* r
= Expression::make_field_reference(thunk_parameter
, 0,
2404 const std::string
& name(interface_method
->name());
2405 func_to_call
= Expression::make_interface_field_reference(r
, name
,
2410 Expression_list
* call_params
= new Expression_list();
2411 const Struct_field_list
* fields
= this->struct_type_
->fields();
2412 Struct_field_list::const_iterator p
= fields
->begin();
2413 for (unsigned int i
= 0; i
< next_index
; ++i
)
2415 bool is_recover_call
= ce
->is_recover_call();
2416 Expression
* recover_arg
= NULL
;
2418 const Expression_list
* args
= ce
->args();
2421 for (Expression_list::const_iterator arg
= args
->begin();
2426 if ((*arg
)->is_constant())
2430 Expression
* thunk_param
=
2431 Expression::make_var_reference(named_parameter
, location
);
2433 Expression::make_dereference(thunk_param
,
2434 Expression::NIL_CHECK_NOT_NEEDED
,
2436 param
= Expression::make_field_reference(thunk_param
,
2442 if (!is_recover_call
)
2443 call_params
->push_back(param
);
2446 go_assert(call_params
->empty());
2447 recover_arg
= param
;
2452 if (call_params
->empty())
2458 Call_expression
* call
= Expression::make_call(func_to_call
, call_params
,
2461 // This call expression was already lowered before entering the
2462 // thunk statement. Don't try to lower varargs again, as that will
2463 // cause confusion for, e.g., method calls which already have a
2464 // receiver parameter.
2465 call
->set_varargs_are_lowered();
2467 Statement
* call_statement
= Statement::make_statement(call
, true);
2469 gogo
->add_statement(call_statement
);
2471 // If this is a defer statement, the label comes immediately after
2473 if (may_call_recover
)
2475 gogo
->add_label_definition("retaddr", location
);
2477 Expression_list
* vals
= new Expression_list();
2478 vals
->push_back(Expression::make_boolean(false, location
));
2479 gogo
->add_statement(Statement::make_return_statement(vals
, location
));
2482 Block
* b
= gogo
->finish_block(location
);
2484 gogo
->add_block(b
, location
);
2486 gogo
->lower_block(function
, b
);
2488 // We already ran the determine_types pass, so we need to run it
2489 // just for the call statement now. The other types are known.
2490 call_statement
->determine_types();
2492 gogo
->flatten_block(function
, b
);
2494 if (may_call_recover
2495 || recover_arg
!= NULL
2496 || this->classification() == STATEMENT_GO
)
2498 // Dig up the call expression, which may have been changed
2500 go_assert(call_statement
->classification() == STATEMENT_EXPRESSION
);
2501 Expression_statement
* es
=
2502 static_cast<Expression_statement
*>(call_statement
);
2503 Call_expression
* ce
= es
->expr()->call_expression();
2505 go_assert(saw_errors());
2508 if (may_call_recover
)
2509 ce
->set_is_deferred();
2510 if (this->classification() == STATEMENT_GO
)
2511 ce
->set_is_concurrent();
2512 if (recover_arg
!= NULL
)
2513 ce
->set_recover_arg(recover_arg
);
2517 // That is all the thunk has to do.
2518 gogo
->finish_function(location
);
2521 // Get the function and argument expressions.
2524 Thunk_statement::get_fn_and_arg(Expression
** pfn
, Expression
** parg
)
2526 if (this->call_
->is_error_expression())
2529 Call_expression
* ce
= this->call_
->call_expression();
2531 Expression
* fn
= ce
->fn();
2532 Func_expression
* fe
= fn
->func_expression();
2533 go_assert(fe
!= NULL
);
2534 *pfn
= Expression::make_func_code_reference(fe
->named_object(),
2537 const Expression_list
* args
= ce
->args();
2538 if (args
== NULL
|| args
->empty())
2539 *parg
= Expression::make_nil(this->location());
2542 go_assert(args
->size() == 1);
2543 *parg
= args
->front();
2549 // Class Go_statement.
2552 Go_statement::do_get_backend(Translate_context
* context
)
2556 if (!this->get_fn_and_arg(&fn
, &arg
))
2557 return context
->backend()->error_statement();
2559 Expression
* call
= Runtime::make_call(Runtime::GO
, this->location(), 2,
2561 Bexpression
* bcall
= call
->get_backend(context
);
2562 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
2563 return context
->backend()->expression_statement(bfunction
, bcall
);
2566 // Dump the AST representation for go statement.
2569 Go_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2571 ast_dump_context
->print_indent();
2572 ast_dump_context
->ostream() << "go ";
2573 ast_dump_context
->dump_expression(this->call());
2574 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2577 // Make a go statement.
2580 Statement::make_go_statement(Call_expression
* call
, Location location
)
2582 return new Go_statement(call
, location
);
2585 // Class Defer_statement.
2588 Defer_statement::do_get_backend(Translate_context
* context
)
2592 if (!this->get_fn_and_arg(&fn
, &arg
))
2593 return context
->backend()->error_statement();
2595 Location loc
= this->location();
2596 Expression
* ds
= context
->function()->func_value()->defer_stack(loc
);
2598 Expression
* call
= Runtime::make_call(Runtime::DEFERPROC
, loc
, 3,
2600 Bexpression
* bcall
= call
->get_backend(context
);
2601 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
2602 return context
->backend()->expression_statement(bfunction
, bcall
);
2605 // Dump the AST representation for defer statement.
2608 Defer_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2610 ast_dump_context
->print_indent();
2611 ast_dump_context
->ostream() << "defer ";
2612 ast_dump_context
->dump_expression(this->call());
2613 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2616 // Make a defer statement.
2619 Statement::make_defer_statement(Call_expression
* call
,
2622 return new Defer_statement(call
, location
);
2625 // Class Return_statement.
2627 // Traverse assignments. We treat each return value as a top level
2628 // RHS in an expression.
2631 Return_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
2633 Expression_list
* vals
= this->vals_
;
2636 for (Expression_list::iterator p
= vals
->begin();
2639 tassign
->value(&*p
, true, true);
2644 // Lower a return statement. If we are returning a function call
2645 // which returns multiple values which match the current function,
2646 // split up the call's results. If the return statement lists
2647 // explicit values, implement this statement by assigning the values
2648 // to the result variables and change this statement to a naked
2649 // return. This lets panic/recover work correctly.
2652 Return_statement::do_lower(Gogo
*, Named_object
* function
, Block
* enclosing
,
2653 Statement_inserter
*)
2655 if (this->is_lowered_
)
2658 Expression_list
* vals
= this->vals_
;
2660 this->is_lowered_
= true;
2662 Location loc
= this->location();
2664 size_t vals_count
= vals
== NULL
? 0 : vals
->size();
2665 Function::Results
* results
= function
->func_value()->result_variables();
2666 size_t results_count
= results
== NULL
? 0 : results
->size();
2668 if (vals_count
== 0)
2670 if (results_count
> 0 && !function
->func_value()->results_are_named())
2672 this->report_error(_("not enough arguments to return"));
2678 if (results_count
== 0)
2680 this->report_error(_("return with value in function "
2681 "with no return type"));
2685 // If the current function has multiple return values, and we are
2686 // returning a single call expression, split up the call expression.
2687 if (results_count
> 1
2688 && vals
->size() == 1
2689 && vals
->front()->call_expression() != NULL
)
2691 Call_expression
* call
= vals
->front()->call_expression();
2692 call
->set_expected_result_count(results_count
);
2694 vals
= new Expression_list
;
2695 for (size_t i
= 0; i
< results_count
; ++i
)
2696 vals
->push_back(Expression::make_call_result(call
, i
));
2697 vals_count
= results_count
;
2700 if (vals_count
< results_count
)
2702 this->report_error(_("not enough arguments to return"));
2706 if (vals_count
> results_count
)
2708 this->report_error(_("too many values in return statement"));
2712 Block
* b
= new Block(enclosing
, loc
);
2714 Expression_list
* lhs
= new Expression_list();
2715 Expression_list
* rhs
= new Expression_list();
2717 Expression_list::const_iterator pe
= vals
->begin();
2719 for (Function::Results::const_iterator pr
= results
->begin();
2720 pr
!= results
->end();
2723 Named_object
* rv
= *pr
;
2724 Expression
* e
= *pe
;
2726 // Check types now so that we give a good error message. The
2727 // result type is known. We determine the expression type
2730 Type
*rvtype
= rv
->result_var_value()->type();
2731 Type_context
type_context(rvtype
, false);
2732 e
->determine_type(&type_context
);
2735 if (Type::are_assignable(rvtype
, e
->type(), &reason
))
2737 Expression
* ve
= Expression::make_var_reference(rv
, e
->location());
2744 go_error_at(e
->location(),
2745 "incompatible type for return value %d", i
);
2747 go_error_at(e
->location(),
2748 "incompatible type for return value %d (%s)",
2752 go_assert(lhs
->size() == rhs
->size());
2756 else if (lhs
->size() == 1)
2758 b
->add_statement(Statement::make_assignment(lhs
->front(), rhs
->front(),
2764 b
->add_statement(Statement::make_tuple_assignment(lhs
, rhs
, loc
));
2766 b
->add_statement(this);
2770 return Statement::make_block_statement(b
, loc
);
2773 // Convert a return statement to the backend representation.
2776 Return_statement::do_get_backend(Translate_context
* context
)
2778 Location loc
= this->location();
2780 Function
* function
= context
->function()->func_value();
2781 Function::Results
* results
= function
->result_variables();
2782 std::vector
<Bexpression
*> retvals
;
2783 if (results
!= NULL
&& !results
->empty())
2785 retvals
.reserve(results
->size());
2786 for (Function::Results::const_iterator p
= results
->begin();
2787 p
!= results
->end();
2790 Expression
* vr
= Expression::make_var_reference(*p
, loc
);
2791 retvals
.push_back(vr
->get_backend(context
));
2795 return context
->backend()->return_statement(function
->get_decl(),
2799 // Dump the AST representation for a return statement.
2802 Return_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2804 ast_dump_context
->print_indent();
2805 ast_dump_context
->ostream() << "return " ;
2806 ast_dump_context
->dump_expression_list(this->vals_
);
2807 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2810 // Make a return statement.
2813 Statement::make_return_statement(Expression_list
* vals
,
2816 return new Return_statement(vals
, location
);
2819 // Make a statement that returns the result of a call expression.
2822 Statement::make_return_from_call(Call_expression
* call
, Location location
)
2824 size_t rc
= call
->result_count();
2826 return Statement::make_statement(call
, true);
2829 Expression_list
* vals
= new Expression_list();
2831 vals
->push_back(call
);
2834 for (size_t i
= 0; i
< rc
; ++i
)
2835 vals
->push_back(Expression::make_call_result(call
, i
));
2837 return Statement::make_return_statement(vals
, location
);
2841 // A break or continue statement.
2843 class Bc_statement
: public Statement
2846 Bc_statement(bool is_break
, Unnamed_label
* label
, Location location
)
2847 : Statement(STATEMENT_BREAK_OR_CONTINUE
, location
),
2848 label_(label
), is_break_(is_break
)
2853 { return this->is_break_
; }
2857 do_traverse(Traverse
*)
2858 { return TRAVERSE_CONTINUE
; }
2861 do_may_fall_through() const
2865 do_get_backend(Translate_context
* context
)
2866 { return this->label_
->get_goto(context
, this->location()); }
2869 do_dump_statement(Ast_dump_context
*) const;
2872 // The label that this branches to.
2873 Unnamed_label
* label_
;
2874 // True if this is "break", false if it is "continue".
2878 // Dump the AST representation for a break/continue statement
2881 Bc_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2883 ast_dump_context
->print_indent();
2884 ast_dump_context
->ostream() << (this->is_break_
? "break" : "continue");
2885 if (this->label_
!= NULL
)
2887 ast_dump_context
->ostream() << " ";
2888 ast_dump_context
->dump_label_name(this->label_
);
2890 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2893 // Make a break statement.
2896 Statement::make_break_statement(Unnamed_label
* label
, Location location
)
2898 return new Bc_statement(true, label
, location
);
2901 // Make a continue statement.
2904 Statement::make_continue_statement(Unnamed_label
* label
,
2907 return new Bc_statement(false, label
, location
);
2910 // Class Goto_statement.
2913 Goto_statement::do_traverse(Traverse
*)
2915 return TRAVERSE_CONTINUE
;
2918 // Check types for a label. There aren't any types per se, but we use
2919 // this to give an error if the label was never defined.
2922 Goto_statement::do_check_types(Gogo
*)
2924 if (!this->label_
->is_defined())
2926 go_error_at(this->location(), "reference to undefined label %qs",
2927 Gogo::message_name(this->label_
->name()).c_str());
2928 this->set_is_error();
2932 // Convert the goto statement to the backend representation.
2935 Goto_statement::do_get_backend(Translate_context
* context
)
2937 Blabel
* blabel
= this->label_
->get_backend_label(context
);
2938 return context
->backend()->goto_statement(blabel
, this->location());
2941 // Dump the AST representation for a goto statement.
2944 Goto_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2946 ast_dump_context
->print_indent();
2947 ast_dump_context
->ostream() << "goto " << this->label_
->name() << dsuffix(location()) << std::endl
;
2950 // Make a goto statement.
2953 Statement::make_goto_statement(Label
* label
, Location location
)
2955 return new Goto_statement(label
, location
);
2958 // Class Goto_unnamed_statement.
2961 Goto_unnamed_statement::do_traverse(Traverse
*)
2963 return TRAVERSE_CONTINUE
;
2966 // Convert the goto unnamed statement to the backend representation.
2969 Goto_unnamed_statement::do_get_backend(Translate_context
* context
)
2971 return this->label_
->get_goto(context
, this->location());
2974 // Dump the AST representation for an unnamed goto statement
2977 Goto_unnamed_statement::do_dump_statement(
2978 Ast_dump_context
* ast_dump_context
) const
2980 ast_dump_context
->print_indent();
2981 ast_dump_context
->ostream() << "goto ";
2982 ast_dump_context
->dump_label_name(this->label_
);
2983 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2986 // Make a goto statement to an unnamed label.
2989 Statement::make_goto_unnamed_statement(Unnamed_label
* label
,
2992 return new Goto_unnamed_statement(label
, location
);
2995 // Class Label_statement.
3000 Label_statement::do_traverse(Traverse
*)
3002 return TRAVERSE_CONTINUE
;
3005 // Return the backend representation of the statement defining this
3009 Label_statement::do_get_backend(Translate_context
* context
)
3011 if (this->label_
->is_dummy_label())
3013 Bexpression
* bce
= context
->backend()->boolean_constant_expression(false);
3014 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
3015 return context
->backend()->expression_statement(bfunction
, bce
);
3017 Blabel
* blabel
= this->label_
->get_backend_label(context
);
3018 return context
->backend()->label_definition_statement(blabel
);
3021 // Dump the AST for a label definition statement.
3024 Label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3026 ast_dump_context
->print_indent();
3027 ast_dump_context
->ostream() << this->label_
->name() << ":" << dsuffix(location()) << std::endl
;
3030 // Make a label statement.
3033 Statement::make_label_statement(Label
* label
, Location location
)
3035 return new Label_statement(label
, location
);
3038 // Class Unnamed_label_statement.
3040 Unnamed_label_statement::Unnamed_label_statement(Unnamed_label
* label
)
3041 : Statement(STATEMENT_UNNAMED_LABEL
, label
->location()),
3046 Unnamed_label_statement::do_traverse(Traverse
*)
3048 return TRAVERSE_CONTINUE
;
3051 // Get the backend definition for this unnamed label statement.
3054 Unnamed_label_statement::do_get_backend(Translate_context
* context
)
3056 return this->label_
->get_definition(context
);
3059 // Dump the AST representation for an unnamed label definition statement.
3062 Unnamed_label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3065 ast_dump_context
->print_indent();
3066 ast_dump_context
->dump_label_name(this->label_
);
3067 ast_dump_context
->ostream() << ":" << dsuffix(location()) << std::endl
;
3070 // Make an unnamed label statement.
3073 Statement::make_unnamed_label_statement(Unnamed_label
* label
)
3075 return new Unnamed_label_statement(label
);
3078 // Class If_statement.
3083 If_statement::do_traverse(Traverse
* traverse
)
3085 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
3086 || this->then_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3087 return TRAVERSE_EXIT
;
3088 if (this->else_block_
!= NULL
)
3090 if (this->else_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3091 return TRAVERSE_EXIT
;
3093 return TRAVERSE_CONTINUE
;
3097 If_statement::do_determine_types()
3099 Type_context
context(Type::lookup_bool_type(), false);
3100 this->cond_
->determine_type(&context
);
3101 this->then_block_
->determine_types();
3102 if (this->else_block_
!= NULL
)
3103 this->else_block_
->determine_types();
3109 If_statement::do_check_types(Gogo
*)
3111 Type
* type
= this->cond_
->type();
3112 if (type
->is_error())
3113 this->set_is_error();
3114 else if (!type
->is_boolean_type())
3115 this->report_error(_("expected boolean expression"));
3118 // Whether the overall statement may fall through.
3121 If_statement::do_may_fall_through() const
3123 return (this->else_block_
== NULL
3124 || this->then_block_
->may_fall_through()
3125 || this->else_block_
->may_fall_through());
3128 // Get the backend representation.
3131 If_statement::do_get_backend(Translate_context
* context
)
3133 go_assert(this->cond_
->type()->is_boolean_type()
3134 || this->cond_
->type()->is_error());
3135 Bexpression
* cond
= this->cond_
->get_backend(context
);
3136 Bblock
* then_block
= this->then_block_
->get_backend(context
);
3137 Bblock
* else_block
= (this->else_block_
== NULL
3139 : this->else_block_
->get_backend(context
));
3140 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
3141 return context
->backend()->if_statement(bfunction
,
3142 cond
, then_block
, else_block
,
3146 // Dump the AST representation for an if statement
3149 If_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3151 ast_dump_context
->print_indent();
3152 ast_dump_context
->ostream() << "if ";
3153 ast_dump_context
->dump_expression(this->cond_
);
3154 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
3155 if (ast_dump_context
->dump_subblocks())
3157 ast_dump_context
->dump_block(this->then_block_
);
3158 if (this->else_block_
!= NULL
)
3160 ast_dump_context
->print_indent();
3161 ast_dump_context
->ostream() << "else" << std::endl
;
3162 ast_dump_context
->dump_block(this->else_block_
);
3167 // Make an if statement.
3170 Statement::make_if_statement(Expression
* cond
, Block
* then_block
,
3171 Block
* else_block
, Location location
)
3173 return new If_statement(cond
, then_block
, else_block
, location
);
3176 // Class Case_clauses::Hash_integer_value.
3178 class Case_clauses::Hash_integer_value
3182 operator()(Expression
*) const;
3186 Case_clauses::Hash_integer_value::operator()(Expression
* pe
) const
3188 Numeric_constant nc
;
3190 if (!pe
->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3192 size_t ret
= mpz_get_ui(ival
);
3197 // Class Case_clauses::Eq_integer_value.
3199 class Case_clauses::Eq_integer_value
3203 operator()(Expression
*, Expression
*) const;
3207 Case_clauses::Eq_integer_value::operator()(Expression
* a
, Expression
* b
) const
3209 Numeric_constant anc
;
3211 Numeric_constant bnc
;
3213 if (!a
->numeric_constant_value(&anc
)
3214 || !anc
.to_int(&aval
)
3215 || !b
->numeric_constant_value(&bnc
)
3216 || !bnc
.to_int(&bval
))
3218 bool ret
= mpz_cmp(aval
, bval
) == 0;
3224 // Class Case_clauses::Case_clause.
3229 Case_clauses::Case_clause::traverse(Traverse
* traverse
)
3231 if (this->cases_
!= NULL
3232 && (traverse
->traverse_mask()
3233 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3235 if (this->cases_
->traverse(traverse
) == TRAVERSE_EXIT
)
3236 return TRAVERSE_EXIT
;
3238 if (this->statements_
!= NULL
)
3240 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
3241 return TRAVERSE_EXIT
;
3243 return TRAVERSE_CONTINUE
;
3246 // Check whether all the case expressions are integer constants.
3249 Case_clauses::Case_clause::is_constant() const
3251 if (this->cases_
!= NULL
)
3253 for (Expression_list::const_iterator p
= this->cases_
->begin();
3254 p
!= this->cases_
->end();
3256 if (!(*p
)->is_constant() || (*p
)->type()->integer_type() == NULL
)
3262 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3263 // value we are switching on; it may be NULL. If START_LABEL is not
3264 // NULL, it goes at the start of the statements, after the condition
3265 // test. We branch to FINISH_LABEL at the end of the statements.
3268 Case_clauses::Case_clause::lower(Block
* b
, Temporary_statement
* val_temp
,
3269 Unnamed_label
* start_label
,
3270 Unnamed_label
* finish_label
) const
3272 Location loc
= this->location_
;
3273 Unnamed_label
* next_case_label
;
3274 if (this->cases_
== NULL
|| this->cases_
->empty())
3276 go_assert(this->is_default_
);
3277 next_case_label
= NULL
;
3281 Expression
* cond
= NULL
;
3283 for (Expression_list::const_iterator p
= this->cases_
->begin();
3284 p
!= this->cases_
->end();
3287 Expression
* ref
= Expression::make_temporary_reference(val_temp
,
3289 Expression
* this_cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3294 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, this_cond
, loc
);
3297 Block
* then_block
= new Block(b
, loc
);
3298 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3299 Statement
* s
= Statement::make_goto_unnamed_statement(next_case_label
,
3301 then_block
->add_statement(s
);
3303 // if !COND { goto NEXT_CASE_LABEL }
3304 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3305 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
3306 b
->add_statement(s
);
3309 if (start_label
!= NULL
)
3310 b
->add_statement(Statement::make_unnamed_label_statement(start_label
));
3312 if (this->statements_
!= NULL
)
3313 b
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
3315 Statement
* s
= Statement::make_goto_unnamed_statement(finish_label
, loc
);
3316 b
->add_statement(s
);
3318 if (next_case_label
!= NULL
)
3319 b
->add_statement(Statement::make_unnamed_label_statement(next_case_label
));
3325 Case_clauses::Case_clause::determine_types(Type
* type
)
3327 if (this->cases_
!= NULL
)
3329 Type_context
case_context(type
, false);
3330 for (Expression_list::iterator p
= this->cases_
->begin();
3331 p
!= this->cases_
->end();
3333 (*p
)->determine_type(&case_context
);
3335 if (this->statements_
!= NULL
)
3336 this->statements_
->determine_types();
3339 // Check types. Returns false if there was an error.
3342 Case_clauses::Case_clause::check_types(Type
* type
)
3344 if (this->cases_
!= NULL
)
3346 for (Expression_list::iterator p
= this->cases_
->begin();
3347 p
!= this->cases_
->end();
3350 if (!Type::are_assignable(type
, (*p
)->type(), NULL
)
3351 && !Type::are_assignable((*p
)->type(), type
, NULL
))
3353 go_error_at((*p
)->location(),
3354 "type mismatch between switch value and case clause");
3362 // Return true if this clause may fall through to the following
3363 // statements. Note that this is not the same as whether the case
3364 // uses the "fallthrough" keyword.
3367 Case_clauses::Case_clause::may_fall_through() const
3369 if (this->statements_
== NULL
)
3371 return this->statements_
->may_fall_through();
3374 // Convert the case values and statements to the backend
3375 // representation. BREAK_LABEL is the label which break statements
3376 // should branch to. CASE_CONSTANTS is used to detect duplicate
3377 // constants. *CASES should be passed as an empty vector; the values
3378 // for this case will be added to it. If this is the default case,
3379 // *CASES will remain empty. This returns the statement to execute if
3380 // one of these cases is selected.
3383 Case_clauses::Case_clause::get_backend(Translate_context
* context
,
3384 Unnamed_label
* break_label
,
3385 Case_constants
* case_constants
,
3386 std::vector
<Bexpression
*>* cases
) const
3388 if (this->cases_
!= NULL
)
3390 go_assert(!this->is_default_
);
3391 for (Expression_list::const_iterator p
= this->cases_
->begin();
3392 p
!= this->cases_
->end();
3396 if (e
->classification() != Expression::EXPRESSION_INTEGER
)
3398 Numeric_constant nc
;
3400 if (!(*p
)->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3402 // Something went wrong. This can happen with a
3403 // negative constant and an unsigned switch value.
3404 go_assert(saw_errors());
3407 go_assert(nc
.type() != NULL
);
3408 e
= Expression::make_integer_z(&ival
, nc
.type(), e
->location());
3412 std::pair
<Case_constants::iterator
, bool> ins
=
3413 case_constants
->insert(e
);
3416 // Value was already present.
3417 go_error_at(this->location_
, "duplicate case in switch");
3418 e
= Expression::make_error(this->location_
);
3420 cases
->push_back(e
->get_backend(context
));
3424 Bstatement
* statements
;
3425 if (this->statements_
== NULL
)
3429 Bblock
* bblock
= this->statements_
->get_backend(context
);
3430 statements
= context
->backend()->block_statement(bblock
);
3433 Bstatement
* break_stat
;
3434 if (this->is_fallthrough_
)
3437 break_stat
= break_label
->get_goto(context
, this->location_
);
3439 if (statements
== NULL
)
3441 else if (break_stat
== NULL
)
3444 return context
->backend()->compound_statement(statements
, break_stat
);
3447 // Dump the AST representation for a case clause
3450 Case_clauses::Case_clause::dump_clause(Ast_dump_context
* ast_dump_context
)
3453 ast_dump_context
->print_indent();
3454 if (this->is_default_
)
3456 ast_dump_context
->ostream() << "default:";
3460 ast_dump_context
->ostream() << "case ";
3461 ast_dump_context
->dump_expression_list(this->cases_
);
3462 ast_dump_context
->ostream() << ":" ;
3464 ast_dump_context
->dump_block(this->statements_
);
3465 if (this->is_fallthrough_
)
3467 ast_dump_context
->print_indent();
3468 ast_dump_context
->ostream() << " (fallthrough)" << dsuffix(location()) << std::endl
;
3472 // Class Case_clauses.
3477 Case_clauses::traverse(Traverse
* traverse
)
3479 for (Clauses::iterator p
= this->clauses_
.begin();
3480 p
!= this->clauses_
.end();
3483 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
3484 return TRAVERSE_EXIT
;
3486 return TRAVERSE_CONTINUE
;
3489 // Check whether all the case expressions are constant.
3492 Case_clauses::is_constant() const
3494 for (Clauses::const_iterator p
= this->clauses_
.begin();
3495 p
!= this->clauses_
.end();
3497 if (!p
->is_constant())
3502 // Lower case clauses for a nonconstant switch.
3505 Case_clauses::lower(Block
* b
, Temporary_statement
* val_temp
,
3506 Unnamed_label
* break_label
) const
3508 // The default case.
3509 const Case_clause
* default_case
= NULL
;
3511 // The label for the fallthrough of the previous case.
3512 Unnamed_label
* last_fallthrough_label
= NULL
;
3514 // The label for the start of the default case. This is used if the
3515 // case before the default case falls through.
3516 Unnamed_label
* default_start_label
= NULL
;
3518 // The label for the end of the default case. This normally winds
3519 // up as BREAK_LABEL, but it will be different if the default case
3521 Unnamed_label
* default_finish_label
= NULL
;
3523 for (Clauses::const_iterator p
= this->clauses_
.begin();
3524 p
!= this->clauses_
.end();
3527 // The label to use for the start of the statements for this
3528 // case. This is NULL unless the previous case falls through.
3529 Unnamed_label
* start_label
= last_fallthrough_label
;
3531 // The label to jump to after the end of the statements for this
3533 Unnamed_label
* finish_label
= break_label
;
3535 last_fallthrough_label
= NULL
;
3536 if (p
->is_fallthrough() && p
+ 1 != this->clauses_
.end())
3538 finish_label
= new Unnamed_label(p
->location());
3539 last_fallthrough_label
= finish_label
;
3542 if (!p
->is_default())
3543 p
->lower(b
, val_temp
, start_label
, finish_label
);
3546 // We have to move the default case to the end, so that we
3547 // only use it if all the other tests fail.
3549 default_start_label
= start_label
;
3550 default_finish_label
= finish_label
;
3554 if (default_case
!= NULL
)
3555 default_case
->lower(b
, val_temp
, default_start_label
,
3556 default_finish_label
);
3562 Case_clauses::determine_types(Type
* type
)
3564 for (Clauses::iterator p
= this->clauses_
.begin();
3565 p
!= this->clauses_
.end();
3567 p
->determine_types(type
);
3570 // Check types. Returns false if there was an error.
3573 Case_clauses::check_types(Type
* type
)
3576 for (Clauses::iterator p
= this->clauses_
.begin();
3577 p
!= this->clauses_
.end();
3580 if (!p
->check_types(type
))
3586 // Return true if these clauses may fall through to the statements
3587 // following the switch statement.
3590 Case_clauses::may_fall_through() const
3592 bool found_default
= false;
3593 for (Clauses::const_iterator p
= this->clauses_
.begin();
3594 p
!= this->clauses_
.end();
3597 if (p
->may_fall_through() && !p
->is_fallthrough())
3599 if (p
->is_default())
3600 found_default
= true;
3602 return !found_default
;
3605 // Convert the cases to the backend representation. This sets
3606 // *ALL_CASES and *ALL_STATEMENTS.
3609 Case_clauses::get_backend(Translate_context
* context
,
3610 Unnamed_label
* break_label
,
3611 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
3612 std::vector
<Bstatement
*>* all_statements
) const
3614 Case_constants case_constants
;
3616 size_t c
= this->clauses_
.size();
3617 all_cases
->resize(c
);
3618 all_statements
->resize(c
);
3621 for (Clauses::const_iterator p
= this->clauses_
.begin();
3622 p
!= this->clauses_
.end();
3625 std::vector
<Bexpression
*> cases
;
3626 Bstatement
* stat
= p
->get_backend(context
, break_label
, &case_constants
,
3628 // The final clause can't fall through.
3629 if (i
== c
- 1 && p
->is_fallthrough())
3631 go_assert(saw_errors());
3632 stat
= context
->backend()->error_statement();
3634 (*all_cases
)[i
].swap(cases
);
3635 (*all_statements
)[i
] = stat
;
3639 // Dump the AST representation for case clauses (from a switch statement)
3642 Case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
3644 for (Clauses::const_iterator p
= this->clauses_
.begin();
3645 p
!= this->clauses_
.end();
3647 p
->dump_clause(ast_dump_context
);
3650 // A constant switch statement. A Switch_statement is lowered to this
3651 // when all the cases are constants.
3653 class Constant_switch_statement
: public Statement
3656 Constant_switch_statement(Expression
* val
, Case_clauses
* clauses
,
3657 Unnamed_label
* break_label
,
3659 : Statement(STATEMENT_CONSTANT_SWITCH
, location
),
3660 val_(val
), clauses_(clauses
), break_label_(break_label
)
3665 do_traverse(Traverse
*);
3668 do_determine_types();
3671 do_check_types(Gogo
*);
3674 do_get_backend(Translate_context
*);
3677 do_dump_statement(Ast_dump_context
*) const;
3680 // The value to switch on.
3682 // The case clauses.
3683 Case_clauses
* clauses_
;
3684 // The break label, if needed.
3685 Unnamed_label
* break_label_
;
3691 Constant_switch_statement::do_traverse(Traverse
* traverse
)
3693 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3694 return TRAVERSE_EXIT
;
3695 return this->clauses_
->traverse(traverse
);
3701 Constant_switch_statement::do_determine_types()
3703 this->val_
->determine_type_no_context();
3704 this->clauses_
->determine_types(this->val_
->type());
3710 Constant_switch_statement::do_check_types(Gogo
*)
3712 if (!this->clauses_
->check_types(this->val_
->type()))
3713 this->set_is_error();
3716 // Convert to GENERIC.
3719 Constant_switch_statement::do_get_backend(Translate_context
* context
)
3721 Bexpression
* switch_val_expr
= this->val_
->get_backend(context
);
3723 Unnamed_label
* break_label
= this->break_label_
;
3724 if (break_label
== NULL
)
3725 break_label
= new Unnamed_label(this->location());
3727 std::vector
<std::vector
<Bexpression
*> > all_cases
;
3728 std::vector
<Bstatement
*> all_statements
;
3729 this->clauses_
->get_backend(context
, break_label
, &all_cases
,
3732 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
3733 Bstatement
* switch_statement
;
3734 switch_statement
= context
->backend()->switch_statement(bfunction
,
3739 Bstatement
* ldef
= break_label
->get_definition(context
);
3740 return context
->backend()->compound_statement(switch_statement
, ldef
);
3743 // Dump the AST representation for a constant switch statement.
3746 Constant_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3749 ast_dump_context
->print_indent();
3750 ast_dump_context
->ostream() << "switch ";
3751 ast_dump_context
->dump_expression(this->val_
);
3753 if (ast_dump_context
->dump_subblocks())
3755 ast_dump_context
->ostream() << " {" << std::endl
;
3756 this->clauses_
->dump_clauses(ast_dump_context
);
3757 ast_dump_context
->ostream() << "}";
3760 ast_dump_context
->ostream() << std::endl
;
3763 // Class Switch_statement.
3768 Switch_statement::do_traverse(Traverse
* traverse
)
3770 if (this->val_
!= NULL
)
3772 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3773 return TRAVERSE_EXIT
;
3775 return this->clauses_
->traverse(traverse
);
3778 // Lower a Switch_statement to a Constant_switch_statement or a series
3779 // of if statements.
3782 Switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
3783 Statement_inserter
*)
3785 Location loc
= this->location();
3787 if (this->val_
!= NULL
3788 && (this->val_
->is_error_expression()
3789 || this->val_
->type()->is_error()))
3791 go_assert(saw_errors());
3792 return Statement::make_error_statement(loc
);
3795 if (this->val_
!= NULL
3796 && this->val_
->type()->integer_type() != NULL
3797 && !this->clauses_
->empty()
3798 && this->clauses_
->is_constant())
3799 return new Constant_switch_statement(this->val_
, this->clauses_
,
3800 this->break_label_
, loc
);
3802 if (this->val_
!= NULL
3803 && !this->val_
->type()->is_comparable()
3804 && !Type::are_compatible_for_comparison(true, this->val_
->type(),
3805 Type::make_nil_type(), NULL
))
3807 go_error_at(this->val_
->location(),
3808 "cannot switch on value whose type that may not be compared");
3809 return Statement::make_error_statement(loc
);
3812 Block
* b
= new Block(enclosing
, loc
);
3814 if (this->clauses_
->empty())
3816 Expression
* val
= this->val_
;
3818 val
= Expression::make_boolean(true, loc
);
3819 return Statement::make_statement(val
, true);
3822 // var val_temp VAL_TYPE = VAL
3823 Expression
* val
= this->val_
;
3825 val
= Expression::make_boolean(true, loc
);
3827 Type
* type
= val
->type();
3828 if (type
->is_abstract())
3829 type
= type
->make_non_abstract_type();
3830 Temporary_statement
* val_temp
= Statement::make_temporary(type
, val
, loc
);
3831 b
->add_statement(val_temp
);
3833 this->clauses_
->lower(b
, val_temp
, this->break_label());
3835 Statement
* s
= Statement::make_unnamed_label_statement(this->break_label_
);
3836 b
->add_statement(s
);
3838 return Statement::make_block_statement(b
, loc
);
3841 // Return the break label for this switch statement, creating it if
3845 Switch_statement::break_label()
3847 if (this->break_label_
== NULL
)
3848 this->break_label_
= new Unnamed_label(this->location());
3849 return this->break_label_
;
3852 // Dump the AST representation for a switch statement.
3855 Switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3857 ast_dump_context
->print_indent();
3858 ast_dump_context
->ostream() << "switch ";
3859 if (this->val_
!= NULL
)
3861 ast_dump_context
->dump_expression(this->val_
);
3863 if (ast_dump_context
->dump_subblocks())
3865 ast_dump_context
->ostream() << " {" << dsuffix(location()) << std::endl
;
3866 this->clauses_
->dump_clauses(ast_dump_context
);
3867 ast_dump_context
->print_indent();
3868 ast_dump_context
->ostream() << "}";
3870 ast_dump_context
->ostream() << std::endl
;
3873 // Return whether this switch may fall through.
3876 Switch_statement::do_may_fall_through() const
3878 if (this->clauses_
== NULL
)
3881 // If we have a break label, then some case needed it. That implies
3882 // that the switch statement as a whole can fall through.
3883 if (this->break_label_
!= NULL
)
3886 return this->clauses_
->may_fall_through();
3889 // Make a switch statement.
3892 Statement::make_switch_statement(Expression
* val
, Location location
)
3894 return new Switch_statement(val
, location
);
3897 // Class Type_case_clauses::Type_case_clause.
3902 Type_case_clauses::Type_case_clause::traverse(Traverse
* traverse
)
3904 if (!this->is_default_
3905 && ((traverse
->traverse_mask()
3906 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3907 && Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3908 return TRAVERSE_EXIT
;
3909 if (this->statements_
!= NULL
)
3910 return this->statements_
->traverse(traverse
);
3911 return TRAVERSE_CONTINUE
;
3914 // Lower one clause in a type switch. Add statements to the block B.
3915 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3916 // BREAK_LABEL is the label at the end of the type switch.
3917 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3921 Type_case_clauses::Type_case_clause::lower(Type
* switch_val_type
,
3923 Temporary_statement
* descriptor_temp
,
3924 Unnamed_label
* break_label
,
3925 Unnamed_label
** stmts_label
) const
3927 Location loc
= this->location_
;
3929 Unnamed_label
* next_case_label
= NULL
;
3930 if (!this->is_default_
)
3932 Type
* type
= this->type_
;
3935 if (switch_val_type
->interface_type() != NULL
3936 && !type
->is_nil_constant_as_type()
3937 && type
->interface_type() == NULL
3938 && !switch_val_type
->interface_type()->implements_interface(type
,
3942 go_error_at(this->location_
, "impossible type switch case");
3944 go_error_at(this->location_
, "impossible type switch case (%s)",
3948 Expression
* ref
= Expression::make_temporary_reference(descriptor_temp
,
3952 // The language permits case nil, which is of course a constant
3953 // rather than a type. It will appear here as an invalid
3955 if (type
->is_nil_constant_as_type())
3956 cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3957 Expression::make_nil(loc
),
3960 cond
= Runtime::make_call((type
->interface_type() == NULL
3961 ? Runtime::IFACETYPEEQ
3962 : Runtime::IFACET2IP
),
3964 Expression::make_type_descriptor(type
, loc
),
3967 Unnamed_label
* dest
;
3968 if (!this->is_fallthrough_
)
3970 // if !COND { goto NEXT_CASE_LABEL }
3971 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3972 dest
= next_case_label
;
3973 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3977 // if COND { goto STMTS_LABEL }
3978 go_assert(stmts_label
!= NULL
);
3979 if (*stmts_label
== NULL
)
3980 *stmts_label
= new Unnamed_label(Linemap::unknown_location());
3981 dest
= *stmts_label
;
3983 Block
* then_block
= new Block(b
, loc
);
3984 Statement
* s
= Statement::make_goto_unnamed_statement(dest
, loc
);
3985 then_block
->add_statement(s
);
3986 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
3987 b
->add_statement(s
);
3990 if (this->statements_
!= NULL
3991 || (!this->is_fallthrough_
3992 && stmts_label
!= NULL
3993 && *stmts_label
!= NULL
))
3995 go_assert(!this->is_fallthrough_
);
3996 if (stmts_label
!= NULL
&& *stmts_label
!= NULL
)
3998 go_assert(!this->is_default_
);
3999 if (this->statements_
!= NULL
)
4000 (*stmts_label
)->set_location(this->statements_
->start_location());
4001 Statement
* s
= Statement::make_unnamed_label_statement(*stmts_label
);
4002 b
->add_statement(s
);
4003 *stmts_label
= NULL
;
4005 if (this->statements_
!= NULL
)
4006 b
->add_statement(Statement::make_block_statement(this->statements_
,
4010 if (this->is_fallthrough_
)
4011 go_assert(next_case_label
== NULL
);
4014 Location gloc
= (this->statements_
== NULL
4016 : this->statements_
->end_location());
4017 b
->add_statement(Statement::make_goto_unnamed_statement(break_label
,
4019 if (next_case_label
!= NULL
)
4022 Statement::make_unnamed_label_statement(next_case_label
);
4023 b
->add_statement(s
);
4028 // Return true if this type clause may fall through to the statements
4029 // following the switch.
4032 Type_case_clauses::Type_case_clause::may_fall_through() const
4034 if (this->is_fallthrough_
)
4036 // This case means that we automatically fall through to the
4037 // next case (it's used for T1 in case T1, T2:). It does not
4038 // mean that we fall through to the end of the type switch as a
4039 // whole. There is sure to be a next case and that next case
4040 // will determine whether we fall through to the statements
4041 // after the type switch.
4044 if (this->statements_
== NULL
)
4046 return this->statements_
->may_fall_through();
4049 // Dump the AST representation for a type case clause
4052 Type_case_clauses::Type_case_clause::dump_clause(
4053 Ast_dump_context
* ast_dump_context
) const
4055 ast_dump_context
->print_indent();
4056 if (this->is_default_
)
4058 ast_dump_context
->ostream() << "default:";
4062 ast_dump_context
->ostream() << "case ";
4063 ast_dump_context
->dump_type(this->type_
);
4064 ast_dump_context
->ostream() << ":" ;
4066 ast_dump_context
->dump_block(this->statements_
);
4067 if (this->is_fallthrough_
)
4069 ast_dump_context
->print_indent();
4070 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
4074 // Class Type_case_clauses.
4079 Type_case_clauses::traverse(Traverse
* traverse
)
4081 for (Type_clauses::iterator p
= this->clauses_
.begin();
4082 p
!= this->clauses_
.end();
4085 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4086 return TRAVERSE_EXIT
;
4088 return TRAVERSE_CONTINUE
;
4091 // Check for duplicate types.
4094 Type_case_clauses::check_duplicates() const
4096 typedef Unordered_set_hash(const Type
*, Type_hash_identical
,
4097 Type_identical
) Types_seen
;
4098 Types_seen types_seen
;
4099 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4100 p
!= this->clauses_
.end();
4103 Type
* t
= p
->type();
4106 if (t
->is_nil_constant_as_type())
4107 t
= Type::make_nil_type();
4108 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
.insert(t
);
4110 go_error_at(p
->location(), "duplicate type in switch");
4114 // Lower the clauses in a type switch. Add statements to the block B.
4115 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4116 // BREAK_LABEL is the label at the end of the type switch.
4119 Type_case_clauses::lower(Type
* switch_val_type
, Block
* b
,
4120 Temporary_statement
* descriptor_temp
,
4121 Unnamed_label
* break_label
) const
4123 const Type_case_clause
* default_case
= NULL
;
4125 Unnamed_label
* stmts_label
= NULL
;
4126 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4127 p
!= this->clauses_
.end();
4130 if (!p
->is_default())
4131 p
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4135 // We are generating a series of tests, which means that we
4136 // need to move the default case to the end.
4140 go_assert(stmts_label
== NULL
);
4142 if (default_case
!= NULL
)
4143 default_case
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4147 // Return true if these clauses may fall through to the statements
4148 // following the switch statement.
4151 Type_case_clauses::may_fall_through() const
4153 bool found_default
= false;
4154 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4155 p
!= this->clauses_
.end();
4158 if (p
->may_fall_through())
4160 if (p
->is_default())
4161 found_default
= true;
4163 return !found_default
;
4166 // Dump the AST representation for case clauses (from a switch statement)
4169 Type_case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4171 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4172 p
!= this->clauses_
.end();
4174 p
->dump_clause(ast_dump_context
);
4177 // Class Type_switch_statement.
4182 Type_switch_statement::do_traverse(Traverse
* traverse
)
4184 if (this->traverse_expression(traverse
, &this->expr_
) == TRAVERSE_EXIT
)
4185 return TRAVERSE_EXIT
;
4186 if (this->clauses_
!= NULL
)
4187 return this->clauses_
->traverse(traverse
);
4188 return TRAVERSE_CONTINUE
;
4191 // Lower a type switch statement to a series of if statements. The gc
4192 // compiler is able to generate a table in some cases. However, that
4193 // does not work for us because we may have type descriptors in
4194 // different shared libraries, so we can't compare them with simple
4195 // equality testing.
4198 Type_switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
4199 Statement_inserter
*)
4201 const Location loc
= this->location();
4203 if (this->clauses_
!= NULL
)
4204 this->clauses_
->check_duplicates();
4206 Block
* b
= new Block(enclosing
, loc
);
4208 Type
* val_type
= this->expr_
->type();
4209 if (val_type
->interface_type() == NULL
)
4211 if (!val_type
->is_error())
4212 this->report_error(_("cannot type switch on non-interface value"));
4213 return Statement::make_error_statement(loc
);
4216 // var descriptor_temp DESCRIPTOR_TYPE
4217 Type
* descriptor_type
= Type::make_type_descriptor_ptr_type();
4218 Temporary_statement
* descriptor_temp
=
4219 Statement::make_temporary(descriptor_type
, NULL
, loc
);
4220 b
->add_statement(descriptor_temp
);
4222 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4224 bool is_empty
= val_type
->interface_type()->is_empty();
4225 Expression
* call
= Runtime::make_call((is_empty
4226 ? Runtime::EFACETYPE
4227 : Runtime::IFACETYPE
),
4228 loc
, 1, this->expr_
);
4229 Temporary_reference_expression
* lhs
=
4230 Expression::make_temporary_reference(descriptor_temp
, loc
);
4231 lhs
->set_is_lvalue();
4232 Statement
* s
= Statement::make_assignment(lhs
, call
, loc
);
4233 b
->add_statement(s
);
4235 if (this->clauses_
!= NULL
)
4236 this->clauses_
->lower(val_type
, b
, descriptor_temp
, this->break_label());
4238 s
= Statement::make_unnamed_label_statement(this->break_label_
);
4239 b
->add_statement(s
);
4241 return Statement::make_block_statement(b
, loc
);
4244 // Return whether this switch may fall through.
4247 Type_switch_statement::do_may_fall_through() const
4249 if (this->clauses_
== NULL
)
4252 // If we have a break label, then some case needed it. That implies
4253 // that the switch statement as a whole can fall through.
4254 if (this->break_label_
!= NULL
)
4257 return this->clauses_
->may_fall_through();
4260 // Return the break label for this type switch statement, creating it
4264 Type_switch_statement::break_label()
4266 if (this->break_label_
== NULL
)
4267 this->break_label_
= new Unnamed_label(this->location());
4268 return this->break_label_
;
4271 // Dump the AST representation for a type switch statement
4274 Type_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
4277 ast_dump_context
->print_indent();
4278 ast_dump_context
->ostream() << "switch ";
4279 if (!this->name_
.empty())
4280 ast_dump_context
->ostream() << this->name_
<< " = ";
4281 ast_dump_context
->dump_expression(this->expr_
);
4282 ast_dump_context
->ostream() << " .(type)";
4283 if (ast_dump_context
->dump_subblocks())
4285 ast_dump_context
->ostream() << " {" << dsuffix(location()) << std::endl
;
4286 this->clauses_
->dump_clauses(ast_dump_context
);
4287 ast_dump_context
->ostream() << "}";
4289 ast_dump_context
->ostream() << std::endl
;
4292 // Make a type switch statement.
4294 Type_switch_statement
*
4295 Statement::make_type_switch_statement(const std::string
& name
, Expression
* expr
,
4298 return new Type_switch_statement(name
, expr
, location
);
4301 // Class Send_statement.
4306 Send_statement::do_traverse(Traverse
* traverse
)
4308 if (this->traverse_expression(traverse
, &this->channel_
) == TRAVERSE_EXIT
)
4309 return TRAVERSE_EXIT
;
4310 return this->traverse_expression(traverse
, &this->val_
);
4316 Send_statement::do_determine_types()
4318 this->channel_
->determine_type_no_context();
4319 Type
* type
= this->channel_
->type();
4320 Type_context context
;
4321 if (type
->channel_type() != NULL
)
4322 context
.type
= type
->channel_type()->element_type();
4323 this->val_
->determine_type(&context
);
4329 Send_statement::do_check_types(Gogo
*)
4331 Type
* type
= this->channel_
->type();
4332 if (type
->is_error())
4334 this->set_is_error();
4337 Channel_type
* channel_type
= type
->channel_type();
4338 if (channel_type
== NULL
)
4340 go_error_at(this->location(), "left operand of %<<-%> must be channel");
4341 this->set_is_error();
4344 Type
* element_type
= channel_type
->element_type();
4345 if (!Type::are_assignable(element_type
, this->val_
->type(), NULL
))
4347 this->report_error(_("incompatible types in send"));
4350 if (!channel_type
->may_send())
4352 this->report_error(_("invalid send on receive-only channel"));
4357 // Flatten a send statement. We may need a temporary for interface
4361 Send_statement::do_flatten(Gogo
*, Named_object
*, Block
*,
4362 Statement_inserter
* inserter
)
4364 if (this->channel_
->is_error_expression()
4365 || this->channel_
->type()->is_error_type())
4367 go_assert(saw_errors());
4368 return Statement::make_error_statement(this->location());
4371 Type
* element_type
= this->channel_
->type()->channel_type()->element_type();
4372 if (!Type::are_identical(element_type
, this->val_
->type(), false, NULL
)
4373 && this->val_
->type()->interface_type() != NULL
4374 && !this->val_
->is_variable())
4376 Temporary_statement
* temp
=
4377 Statement::make_temporary(NULL
, this->val_
, this->location());
4378 inserter
->insert(temp
);
4379 this->val_
= Expression::make_temporary_reference(temp
,
4385 // Convert a send statement to the backend representation.
4388 Send_statement::do_get_backend(Translate_context
* context
)
4390 Location loc
= this->location();
4392 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
4393 Type
* element_type
= channel_type
->element_type();
4394 Expression
* val
= Expression::convert_for_assignment(context
->gogo(),
4398 bool can_take_address
;
4399 switch (element_type
->base()->classification())
4401 case Type::TYPE_BOOLEAN
:
4402 case Type::TYPE_INTEGER
:
4403 case Type::TYPE_FUNCTION
:
4404 case Type::TYPE_POINTER
:
4405 case Type::TYPE_MAP
:
4406 case Type::TYPE_CHANNEL
:
4407 case Type::TYPE_FLOAT
:
4408 case Type::TYPE_COMPLEX
:
4409 case Type::TYPE_STRING
:
4410 case Type::TYPE_INTERFACE
:
4411 can_take_address
= false;
4414 case Type::TYPE_STRUCT
:
4415 can_take_address
= true;
4418 case Type::TYPE_ARRAY
:
4419 can_take_address
= !element_type
->is_slice_type();
4423 case Type::TYPE_ERROR
:
4424 case Type::TYPE_VOID
:
4425 case Type::TYPE_SINK
:
4426 case Type::TYPE_NIL
:
4427 case Type::TYPE_NAMED
:
4428 case Type::TYPE_FORWARD
:
4429 go_assert(saw_errors());
4430 return context
->backend()->error_statement();
4433 // Only try to take the address of a variable. We have already
4434 // moved variables to the heap, so this should not cause that to
4435 // happen unnecessarily.
4436 if (can_take_address
4437 && val
->var_expression() == NULL
4438 && val
->temporary_reference_expression() == NULL
)
4439 can_take_address
= false;
4441 Bstatement
* btemp
= NULL
;
4442 if (can_take_address
)
4444 // The function doesn't change the value, so just take its
4445 // address directly.
4446 val
= Expression::make_unary(OPERATOR_AND
, val
, loc
);
4450 // The value is not in a variable, or is small enough that it
4451 // might be in a register, and taking the address would push it
4452 // on the stack. Copy it into a temporary variable to take the
4454 Temporary_statement
* temp
= Statement::make_temporary(element_type
,
4456 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
4457 val
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
4458 btemp
= temp
->get_backend(context
);
4461 Expression
* call
= Runtime::make_call(Runtime::CHANSEND
, loc
, 2,
4462 this->channel_
, val
);
4464 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4465 Bexpression
* bcall
= call
->get_backend(context
);
4466 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
4467 Bstatement
* s
= context
->backend()->expression_statement(bfunction
, bcall
);
4472 return context
->backend()->compound_statement(btemp
, s
);
4475 // Dump the AST representation for a send statement
4478 Send_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4480 ast_dump_context
->print_indent();
4481 ast_dump_context
->dump_expression(this->channel_
);
4482 ast_dump_context
->ostream() << " <- ";
4483 ast_dump_context
->dump_expression(this->val_
);
4484 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
4487 // Make a send statement.
4490 Statement::make_send_statement(Expression
* channel
, Expression
* val
,
4493 return new Send_statement(channel
, val
, location
);
4496 // Class Select_clauses::Select_clause.
4501 Select_clauses::Select_clause::traverse(Traverse
* traverse
)
4503 if (!this->is_lowered_
4504 && (traverse
->traverse_mask()
4505 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
4507 if (this->channel_
!= NULL
)
4509 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
4510 return TRAVERSE_EXIT
;
4512 if (this->val_
!= NULL
)
4514 if (Expression::traverse(&this->val_
, traverse
) == TRAVERSE_EXIT
)
4515 return TRAVERSE_EXIT
;
4517 if (this->closed_
!= NULL
)
4519 if (Expression::traverse(&this->closed_
, traverse
) == TRAVERSE_EXIT
)
4520 return TRAVERSE_EXIT
;
4523 if (this->statements_
!= NULL
)
4525 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
4526 return TRAVERSE_EXIT
;
4528 return TRAVERSE_CONTINUE
;
4531 // Lowering. We call a function to register this clause, and arrange
4532 // to set any variables in any receive clause.
4535 Select_clauses::Select_clause::lower(Gogo
* gogo
, Named_object
* function
,
4536 Block
* b
, Temporary_statement
* sel
)
4538 Location loc
= this->location_
;
4540 Expression
* selref
= Expression::make_temporary_reference(sel
, loc
);
4541 selref
= Expression::make_unary(OPERATOR_AND
, selref
, loc
);
4543 if (this->is_default_
)
4545 go_assert(this->channel_
== NULL
&& this->val_
== NULL
);
4546 this->lower_default(b
, selref
);
4547 this->is_lowered_
= true;
4551 // Evaluate the channel before the select statement.
4552 Temporary_statement
* channel_temp
= Statement::make_temporary(NULL
,
4555 b
->add_statement(channel_temp
);
4556 Expression
* chanref
= Expression::make_temporary_reference(channel_temp
,
4560 this->lower_send(b
, selref
, chanref
);
4562 this->lower_recv(gogo
, function
, b
, selref
, chanref
);
4564 // Now all references should be handled through the statements, not
4566 this->is_lowered_
= true;
4570 // Lower a default clause in a select statement.
4573 Select_clauses::Select_clause::lower_default(Block
* b
, Expression
* selref
)
4575 Location loc
= this->location_
;
4576 Expression
* call
= Runtime::make_call(Runtime::SELECTDEFAULT
, loc
, 1,
4578 b
->add_statement(Statement::make_statement(call
, true));
4581 // Lower a send clause in a select statement.
4584 Select_clauses::Select_clause::lower_send(Block
* b
, Expression
* selref
,
4585 Expression
* chanref
)
4587 Location loc
= this->location_
;
4589 Channel_type
* ct
= this->channel_
->type()->channel_type();
4593 Type
* valtype
= ct
->element_type();
4595 // Note that copying the value to a temporary here means that we
4596 // evaluate the send values in the required order.
4597 Temporary_statement
* val
= Statement::make_temporary(valtype
, this->val_
,
4599 b
->add_statement(val
);
4601 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4602 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4604 Expression
* call
= Runtime::make_call(Runtime::SELECTSEND
, loc
, 3, selref
,
4606 b
->add_statement(Statement::make_statement(call
, true));
4609 // Lower a receive clause in a select statement.
4612 Select_clauses::Select_clause::lower_recv(Gogo
* gogo
, Named_object
* function
,
4613 Block
* b
, Expression
* selref
,
4614 Expression
* chanref
)
4616 Location loc
= this->location_
;
4618 Channel_type
* ct
= this->channel_
->type()->channel_type();
4622 Type
* valtype
= ct
->element_type();
4623 Temporary_statement
* val
= Statement::make_temporary(valtype
, NULL
, loc
);
4624 b
->add_statement(val
);
4626 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4627 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4629 Temporary_statement
* closed_temp
= NULL
;
4632 if (this->closed_
== NULL
&& this->closedvar_
== NULL
)
4633 caddr
= Expression::make_nil(loc
);
4636 closed_temp
= Statement::make_temporary(Type::lookup_bool_type(), NULL
,
4638 b
->add_statement(closed_temp
);
4639 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4641 caddr
= Expression::make_unary(OPERATOR_AND
, cref
, loc
);
4644 Expression
* call
= Runtime::make_call(Runtime::SELECTRECV
, loc
, 4, selref
,
4645 chanref
, valaddr
, caddr
);
4647 b
->add_statement(Statement::make_statement(call
, true));
4649 // If the block of statements is executed, arrange for the received
4650 // value to move from VAL to the place where the statements expect
4655 if (this->var_
!= NULL
)
4657 go_assert(this->val_
== NULL
);
4658 valref
= Expression::make_temporary_reference(val
, loc
);
4659 this->var_
->var_value()->set_init(valref
);
4660 this->var_
->var_value()->clear_type_from_chan_element();
4662 else if (this->val_
!= NULL
&& !this->val_
->is_sink_expression())
4664 init
= new Block(b
, loc
);
4665 valref
= Expression::make_temporary_reference(val
, loc
);
4666 init
->add_statement(Statement::make_assignment(this->val_
, valref
, loc
));
4669 if (this->closedvar_
!= NULL
)
4671 go_assert(this->closed_
== NULL
);
4672 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4674 this->closedvar_
->var_value()->set_init(cref
);
4676 else if (this->closed_
!= NULL
&& !this->closed_
->is_sink_expression())
4679 init
= new Block(b
, loc
);
4680 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4682 init
->add_statement(Statement::make_assignment(this->closed_
, cref
,
4688 gogo
->lower_block(function
, init
);
4690 if (this->statements_
!= NULL
)
4691 init
->add_statement(Statement::make_block_statement(this->statements_
,
4693 this->statements_
= init
;
4700 Select_clauses::Select_clause::determine_types()
4702 go_assert(this->is_lowered_
);
4703 if (this->statements_
!= NULL
)
4704 this->statements_
->determine_types();
4710 Select_clauses::Select_clause::check_types()
4712 if (this->is_default_
)
4715 Channel_type
* ct
= this->channel_
->type()->channel_type();
4718 go_error_at(this->channel_
->location(), "expected channel");
4722 if (this->is_send_
&& !ct
->may_send())
4723 go_error_at(this->location(), "invalid send on receive-only channel");
4724 else if (!this->is_send_
&& !ct
->may_receive())
4725 go_error_at(this->location(), "invalid receive on send-only channel");
4728 // Whether this clause may fall through to the statement which follows
4729 // the overall select statement.
4732 Select_clauses::Select_clause::may_fall_through() const
4734 if (this->statements_
== NULL
)
4736 return this->statements_
->may_fall_through();
4739 // Return the backend representation for the statements to execute.
4742 Select_clauses::Select_clause::get_statements_backend(
4743 Translate_context
* context
)
4745 if (this->statements_
== NULL
)
4747 Bblock
* bblock
= this->statements_
->get_backend(context
);
4748 return context
->backend()->block_statement(bblock
);
4751 // Dump the AST representation for a select case clause
4754 Select_clauses::Select_clause::dump_clause(
4755 Ast_dump_context
* ast_dump_context
) const
4757 ast_dump_context
->print_indent();
4758 if (this->is_default_
)
4760 ast_dump_context
->ostream() << "default:";
4764 ast_dump_context
->ostream() << "case " ;
4767 ast_dump_context
->dump_expression(this->channel_
);
4768 ast_dump_context
->ostream() << " <- " ;
4769 if (this->val_
!= NULL
)
4770 ast_dump_context
->dump_expression(this->val_
);
4774 if (this->val_
!= NULL
)
4775 ast_dump_context
->dump_expression(this->val_
);
4776 if (this->closed_
!= NULL
)
4778 // FIXME: can val_ == NULL and closed_ ! = NULL?
4779 ast_dump_context
->ostream() << " , " ;
4780 ast_dump_context
->dump_expression(this->closed_
);
4782 if (this->closedvar_
!= NULL
|| this->var_
!= NULL
)
4783 ast_dump_context
->ostream() << " := " ;
4785 ast_dump_context
->ostream() << " <- " ;
4786 ast_dump_context
->dump_expression(this->channel_
);
4788 ast_dump_context
->ostream() << ":" ;
4790 ast_dump_context
->dump_block(this->statements_
);
4793 // Class Select_clauses.
4798 Select_clauses::traverse(Traverse
* traverse
)
4800 for (Clauses::iterator p
= this->clauses_
.begin();
4801 p
!= this->clauses_
.end();
4804 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4805 return TRAVERSE_EXIT
;
4807 return TRAVERSE_CONTINUE
;
4810 // Lowering. Here we pull out the channel and the send values, to
4811 // enforce the order of evaluation. We also add explicit send and
4812 // receive statements to the clauses.
4815 Select_clauses::lower(Gogo
* gogo
, Named_object
* function
, Block
* b
,
4816 Temporary_statement
* sel
)
4818 for (Clauses::iterator p
= this->clauses_
.begin();
4819 p
!= this->clauses_
.end();
4821 p
->lower(gogo
, function
, b
, sel
);
4827 Select_clauses::determine_types()
4829 for (Clauses::iterator p
= this->clauses_
.begin();
4830 p
!= this->clauses_
.end();
4832 p
->determine_types();
4838 Select_clauses::check_types()
4840 for (Clauses::iterator p
= this->clauses_
.begin();
4841 p
!= this->clauses_
.end();
4846 // Return whether these select clauses fall through to the statement
4847 // following the overall select statement.
4850 Select_clauses::may_fall_through() const
4852 for (Clauses::const_iterator p
= this->clauses_
.begin();
4853 p
!= this->clauses_
.end();
4855 if (p
->may_fall_through())
4860 // Convert to the backend representation. We have already accumulated
4861 // all the select information. Now we call selectgo, which will
4862 // return the index of the clause to execute.
4865 Select_clauses::get_backend(Translate_context
* context
,
4866 Temporary_statement
* sel
,
4867 Unnamed_label
*break_label
,
4870 size_t count
= this->clauses_
.size();
4871 std::vector
<std::vector
<Bexpression
*> > cases(count
+ 1);
4872 std::vector
<Bstatement
*> clauses(count
+ 1);
4874 Type
* int_type
= Type::lookup_integer_type("int");
4877 for (Clauses::iterator p
= this->clauses_
.begin();
4878 p
!= this->clauses_
.end();
4881 Expression
* index_expr
= Expression::make_integer_ul(i
, int_type
,
4883 cases
[i
].push_back(index_expr
->get_backend(context
));
4885 Bstatement
* s
= p
->get_statements_backend(context
);
4886 Location gloc
= (p
->statements() == NULL
4888 : p
->statements()->end_location());
4889 Bstatement
* g
= break_label
->get_goto(context
, gloc
);
4894 clauses
[i
] = context
->backend()->compound_statement(s
, g
);
4897 Expression
* selref
= Expression::make_temporary_reference(sel
, location
);
4898 selref
= Expression::make_unary(OPERATOR_AND
, selref
, location
);
4899 Expression
* call
= Runtime::make_call(Runtime::SELECTGO
, location
, 1,
4901 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4902 Bexpression
* bcall
= call
->get_backend(context
);
4906 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
4907 return context
->backend()->expression_statement(bfunction
, bcall
);
4910 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
4912 Expression
* crash
= Runtime::make_call(Runtime::UNREACHABLE
, location
, 0);
4913 Bexpression
* bcrash
= crash
->get_backend(context
);
4914 clauses
[count
] = context
->backend()->expression_statement(bfunction
, bcrash
);
4916 std::vector
<Bstatement
*> statements
;
4917 statements
.reserve(2);
4919 Bstatement
* switch_stmt
= context
->backend()->switch_statement(bfunction
,
4924 statements
.push_back(switch_stmt
);
4926 Bstatement
* ldef
= break_label
->get_definition(context
);
4927 statements
.push_back(ldef
);
4929 return context
->backend()->statement_list(statements
);
4931 // Dump the AST representation for select clauses.
4934 Select_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4936 for (Clauses::const_iterator p
= this->clauses_
.begin();
4937 p
!= this->clauses_
.end();
4939 p
->dump_clause(ast_dump_context
);
4942 // Class Select_statement.
4944 // Return the break label for this switch statement, creating it if
4948 Select_statement::break_label()
4950 if (this->break_label_
== NULL
)
4951 this->break_label_
= new Unnamed_label(this->location());
4952 return this->break_label_
;
4955 // Lower a select statement. This will still return a select
4956 // statement, but it will be modified to implement the order of
4957 // evaluation rules, and to include the send and receive statements as
4958 // explicit statements in the clauses.
4961 Select_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
4962 Block
* enclosing
, Statement_inserter
*)
4964 if (this->is_lowered_
)
4967 Location loc
= this->location();
4969 Block
* b
= new Block(enclosing
, loc
);
4971 go_assert(this->sel_
== NULL
);
4973 int ncases
= this->clauses_
->size();
4974 Type
* selstruct_type
= Channel_type::select_type(ncases
);
4975 this->sel_
= Statement::make_temporary(selstruct_type
, NULL
, loc
);
4976 b
->add_statement(this->sel_
);
4978 int64_t selstruct_size
;
4979 if (!selstruct_type
->backend_type_size(gogo
, &selstruct_size
))
4981 go_assert(saw_errors());
4982 return Statement::make_error_statement(loc
);
4985 Expression
* ref
= Expression::make_temporary_reference(this->sel_
, loc
);
4986 ref
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
4987 Expression
* selstruct_size_expr
=
4988 Expression::make_integer_int64(selstruct_size
, NULL
, loc
);
4989 Expression
* size_expr
= Expression::make_integer_ul(ncases
, NULL
, loc
);
4990 Expression
* call
= Runtime::make_call(Runtime::NEWSELECT
, loc
, 3,
4991 ref
, selstruct_size_expr
, size_expr
);
4992 b
->add_statement(Statement::make_statement(call
, true));
4994 this->clauses_
->lower(gogo
, function
, b
, this->sel_
);
4995 this->is_lowered_
= true;
4996 b
->add_statement(this);
4998 return Statement::make_block_statement(b
, loc
);
5001 // Whether the select statement itself may fall through to the following
5005 Select_statement::do_may_fall_through() const
5007 // A select statement is terminating if no break statement
5008 // refers to it and all of its clauses are terminating.
5009 if (this->break_label_
!= NULL
)
5011 return this->clauses_
->may_fall_through();
5014 // Return the backend representation for a select statement.
5017 Select_statement::do_get_backend(Translate_context
* context
)
5019 return this->clauses_
->get_backend(context
, this->sel_
, this->break_label(),
5023 // Dump the AST representation for a select statement.
5026 Select_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5028 ast_dump_context
->print_indent();
5029 ast_dump_context
->ostream() << "select";
5030 if (ast_dump_context
->dump_subblocks())
5032 ast_dump_context
->ostream() << " {" << dsuffix(location()) << std::endl
;
5033 this->clauses_
->dump_clauses(ast_dump_context
);
5034 ast_dump_context
->ostream() << "}";
5036 ast_dump_context
->ostream() << std::endl
;
5039 // Make a select statement.
5042 Statement::make_select_statement(Location location
)
5044 return new Select_statement(location
);
5047 // Class For_statement.
5052 For_statement::do_traverse(Traverse
* traverse
)
5054 if (this->init_
!= NULL
)
5056 if (this->init_
->traverse(traverse
) == TRAVERSE_EXIT
)
5057 return TRAVERSE_EXIT
;
5059 if (this->cond_
!= NULL
)
5061 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
)
5062 return TRAVERSE_EXIT
;
5064 if (this->post_
!= NULL
)
5066 if (this->post_
->traverse(traverse
) == TRAVERSE_EXIT
)
5067 return TRAVERSE_EXIT
;
5069 return this->statements_
->traverse(traverse
);
5072 // Lower a For_statement into if statements and gotos. Getting rid of
5073 // complex statements make it easier to handle garbage collection.
5076 For_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
5077 Statement_inserter
*)
5080 Location loc
= this->location();
5082 Block
* b
= new Block(enclosing
, this->location());
5083 if (this->init_
!= NULL
)
5085 s
= Statement::make_block_statement(this->init_
,
5086 this->init_
->start_location());
5087 b
->add_statement(s
);
5090 Unnamed_label
* entry
= NULL
;
5091 if (this->cond_
!= NULL
)
5093 entry
= new Unnamed_label(this->location());
5094 b
->add_statement(Statement::make_goto_unnamed_statement(entry
, loc
));
5097 Unnamed_label
* top
= new Unnamed_label(this->location());
5098 top
->set_derived_from(this);
5099 b
->add_statement(Statement::make_unnamed_label_statement(top
));
5101 s
= Statement::make_block_statement(this->statements_
,
5102 this->statements_
->start_location());
5103 b
->add_statement(s
);
5105 Location end_loc
= this->statements_
->end_location();
5107 Unnamed_label
* cont
= this->continue_label_
;
5109 b
->add_statement(Statement::make_unnamed_label_statement(cont
));
5111 if (this->post_
!= NULL
)
5113 s
= Statement::make_block_statement(this->post_
,
5114 this->post_
->start_location());
5115 b
->add_statement(s
);
5116 end_loc
= this->post_
->end_location();
5119 if (this->cond_
== NULL
)
5120 b
->add_statement(Statement::make_goto_unnamed_statement(top
, end_loc
));
5123 b
->add_statement(Statement::make_unnamed_label_statement(entry
));
5125 Location cond_loc
= this->cond_
->location();
5126 Block
* then_block
= new Block(b
, cond_loc
);
5127 s
= Statement::make_goto_unnamed_statement(top
, cond_loc
);
5128 then_block
->add_statement(s
);
5130 s
= Statement::make_if_statement(this->cond_
, then_block
, NULL
, cond_loc
);
5131 b
->add_statement(s
);
5134 Unnamed_label
* brk
= this->break_label_
;
5136 b
->add_statement(Statement::make_unnamed_label_statement(brk
));
5138 b
->set_end_location(end_loc
);
5140 Statement
* bs
= Statement::make_block_statement(b
, loc
);
5141 bs
->block_statement()->set_is_lowered_for_statement();
5145 // Return the break label, creating it if necessary.
5148 For_statement::break_label()
5150 if (this->break_label_
== NULL
)
5151 this->break_label_
= new Unnamed_label(this->location());
5152 return this->break_label_
;
5155 // Return the continue LABEL_EXPR.
5158 For_statement::continue_label()
5160 if (this->continue_label_
== NULL
)
5161 this->continue_label_
= new Unnamed_label(this->location());
5162 return this->continue_label_
;
5165 // Set the break and continue labels a for statement. This is used
5166 // when lowering a for range statement.
5169 For_statement::set_break_continue_labels(Unnamed_label
* break_label
,
5170 Unnamed_label
* continue_label
)
5172 go_assert(this->break_label_
== NULL
&& this->continue_label_
== NULL
);
5173 this->break_label_
= break_label
;
5174 this->continue_label_
= continue_label
;
5177 // Whether the overall statement may fall through.
5180 For_statement::do_may_fall_through() const
5182 // A for loop is terminating if it has no condition and
5183 // no break statement.
5184 if(this->cond_
!= NULL
)
5186 if(this->break_label_
!= NULL
)
5191 // Dump the AST representation for a for statement.
5194 For_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5196 if (this->init_
!= NULL
&& ast_dump_context
->dump_subblocks())
5198 ast_dump_context
->print_indent();
5199 ast_dump_context
->indent();
5200 ast_dump_context
->ostream() << "// INIT " << std::endl
;
5201 ast_dump_context
->dump_block(this->init_
);
5202 ast_dump_context
->unindent();
5204 ast_dump_context
->print_indent();
5205 ast_dump_context
->ostream() << "for ";
5206 if (this->cond_
!= NULL
)
5207 ast_dump_context
->dump_expression(this->cond_
);
5209 if (ast_dump_context
->dump_subblocks())
5211 ast_dump_context
->ostream() << " {" << std::endl
;
5212 ast_dump_context
->dump_block(this->statements_
);
5213 if (this->init_
!= NULL
)
5215 ast_dump_context
->print_indent();
5216 ast_dump_context
->ostream() << "// POST " << std::endl
;
5217 ast_dump_context
->dump_block(this->post_
);
5219 ast_dump_context
->unindent();
5221 ast_dump_context
->print_indent();
5222 ast_dump_context
->ostream() << "}";
5225 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
5228 // Make a for statement.
5231 Statement::make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
5234 return new For_statement(init
, cond
, post
, location
);
5237 // Class For_range_statement.
5242 For_range_statement::do_traverse(Traverse
* traverse
)
5244 if (this->index_var_
!= NULL
)
5246 if (this->traverse_expression(traverse
, &this->index_var_
)
5248 return TRAVERSE_EXIT
;
5250 if (this->value_var_
!= NULL
)
5252 if (this->traverse_expression(traverse
, &this->value_var_
)
5254 return TRAVERSE_EXIT
;
5256 if (this->traverse_expression(traverse
, &this->range_
) == TRAVERSE_EXIT
)
5257 return TRAVERSE_EXIT
;
5258 return this->statements_
->traverse(traverse
);
5261 // Lower a for range statement. For simplicity we lower this into a
5262 // for statement, which will then be lowered in turn to goto
5266 For_range_statement::do_lower(Gogo
* gogo
, Named_object
*, Block
* enclosing
,
5267 Statement_inserter
*)
5269 Type
* range_type
= this->range_
->type();
5270 if (range_type
->points_to() != NULL
5271 && range_type
->points_to()->array_type() != NULL
5272 && !range_type
->points_to()->is_slice_type())
5273 range_type
= range_type
->points_to();
5276 Type
* value_type
= NULL
;
5277 if (range_type
->array_type() != NULL
)
5279 index_type
= Type::lookup_integer_type("int");
5280 value_type
= range_type
->array_type()->element_type();
5282 else if (range_type
->is_string_type())
5284 index_type
= Type::lookup_integer_type("int");
5285 value_type
= gogo
->lookup_global("rune")->type_value();
5287 else if (range_type
->map_type() != NULL
)
5289 index_type
= range_type
->map_type()->key_type();
5290 value_type
= range_type
->map_type()->val_type();
5292 else if (range_type
->channel_type() != NULL
)
5294 index_type
= range_type
->channel_type()->element_type();
5295 if (this->value_var_
!= NULL
)
5297 if (!this->value_var_
->type()->is_error())
5298 this->report_error(_("too many variables for range clause "
5300 return Statement::make_error_statement(this->location());
5305 this->report_error(_("range clause must have "
5306 "array, slice, string, map, or channel type"));
5307 return Statement::make_error_statement(this->location());
5310 // If there is only one iteration variable, and len(this->range_) is
5311 // constant, then we do not evaluate the range variable. len(x) is
5312 // a contant if x is a string constant or if x is an array. If x is
5313 // a constant then evaluating it won't make any difference, so the
5314 // only case to consider is when x is an array whose length is constant.
5316 if ((this->value_var_
== NULL
|| this->value_var_
->is_sink_expression())
5317 && range_type
->array_type() != NULL
5318 && !range_type
->is_slice_type()
5319 && Builtin_call_expression::array_len_is_constant(this->range_
))
5322 Location loc
= this->location();
5323 Block
* temp_block
= new Block(enclosing
, loc
);
5325 Named_object
* range_object
= NULL
;
5326 Temporary_statement
* range_temp
= NULL
;
5329 Var_expression
* ve
= this->range_
->var_expression();
5331 range_object
= ve
->named_object();
5334 range_temp
= Statement::make_temporary(NULL
, this->range_
, loc
);
5335 temp_block
->add_statement(range_temp
);
5336 this->range_
= NULL
;
5340 Temporary_statement
* index_temp
= Statement::make_temporary(index_type
,
5342 temp_block
->add_statement(index_temp
);
5344 Temporary_statement
* value_temp
= NULL
;
5345 if (this->value_var_
!= NULL
&& !this->value_var_
->is_sink_expression())
5347 value_temp
= Statement::make_temporary(value_type
, NULL
, loc
);
5348 temp_block
->add_statement(value_temp
);
5351 Block
* body
= new Block(temp_block
, loc
);
5358 // Arrange to do a loop appropriate for the type. We will produce
5359 // for INIT ; COND ; POST {
5361 // INDEX = INDEX_TEMP
5362 // VALUE = VALUE_TEMP // If there is a value
5363 // original statements
5366 if (range_type
->is_slice_type())
5367 this->lower_range_slice(gogo
, temp_block
, body
, range_object
, range_temp
,
5368 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5370 else if (range_type
->array_type() != NULL
)
5371 this->lower_range_array(gogo
, temp_block
, body
, range_object
, range_temp
,
5372 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5374 else if (range_type
->is_string_type())
5375 this->lower_range_string(gogo
, temp_block
, body
, range_object
, range_temp
,
5376 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5378 else if (range_type
->map_type() != NULL
)
5379 this->lower_range_map(gogo
, range_type
->map_type(), temp_block
, body
,
5380 range_object
, range_temp
, index_temp
, value_temp
,
5381 &init
, &cond
, &iter_init
, &post
);
5382 else if (range_type
->channel_type() != NULL
)
5383 this->lower_range_channel(gogo
, temp_block
, body
, range_object
, range_temp
,
5384 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5389 if (iter_init
!= NULL
)
5390 body
->add_statement(Statement::make_block_statement(iter_init
, loc
));
5392 if (this->index_var_
!= NULL
)
5395 Expression
* index_ref
=
5396 Expression::make_temporary_reference(index_temp
, loc
);
5397 if (this->value_var_
== NULL
|| this->value_var_
->is_sink_expression())
5398 assign
= Statement::make_assignment(this->index_var_
, index_ref
, loc
);
5401 Expression_list
* lhs
= new Expression_list();
5402 lhs
->push_back(this->index_var_
);
5403 lhs
->push_back(this->value_var_
);
5405 Expression_list
* rhs
= new Expression_list();
5406 rhs
->push_back(index_ref
);
5407 rhs
->push_back(Expression::make_temporary_reference(value_temp
, loc
));
5409 assign
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5411 body
->add_statement(assign
);
5414 body
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
5416 body
->set_end_location(this->statements_
->end_location());
5418 For_statement
* loop
= Statement::make_for_statement(init
, cond
, post
,
5420 loop
->add_statements(body
);
5421 loop
->set_break_continue_labels(this->break_label_
, this->continue_label_
);
5423 temp_block
->add_statement(loop
);
5425 return Statement::make_block_statement(temp_block
, loc
);
5428 // Return a reference to the range, which may be in RANGE_OBJECT or in
5432 For_range_statement::make_range_ref(Named_object
* range_object
,
5433 Temporary_statement
* range_temp
,
5436 if (range_object
!= NULL
)
5437 return Expression::make_var_reference(range_object
, loc
);
5439 return Expression::make_temporary_reference(range_temp
, loc
);
5442 // Return a call to the predeclared function FUNCNAME passing a
5443 // reference to the temporary variable ARG.
5446 For_range_statement::call_builtin(Gogo
* gogo
, const char* funcname
,
5450 Named_object
* no
= gogo
->lookup_global(funcname
);
5451 go_assert(no
!= NULL
&& no
->is_function_declaration());
5452 Expression
* func
= Expression::make_func_reference(no
, NULL
, loc
);
5453 Expression_list
* params
= new Expression_list();
5454 params
->push_back(arg
);
5455 return Expression::make_call(func
, params
, false, loc
);
5458 // Lower a for range over an array.
5461 For_range_statement::lower_range_array(Gogo
* gogo
,
5464 Named_object
* range_object
,
5465 Temporary_statement
* range_temp
,
5466 Temporary_statement
* index_temp
,
5467 Temporary_statement
* value_temp
,
5473 Location loc
= this->location();
5475 // The loop we generate:
5476 // len_temp := len(range)
5477 // range_temp := range
5478 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5479 // value_temp = range_temp[index_temp]
5480 // index = index_temp
5481 // value = value_temp
5487 // len_temp = len(range)
5490 Block
* init
= new Block(enclosing
, loc
);
5492 Expression
* len_arg
;
5493 if (range_object
== NULL
&& range_temp
== NULL
)
5495 // Don't evaluate this->range_, just get its length.
5496 len_arg
= this->range_
;
5500 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5501 range_temp
= Statement::make_temporary(NULL
, ref
, loc
);
5502 init
->add_statement(range_temp
);
5505 Expression
* len_call
= this->call_builtin(gogo
, "len", len_arg
, loc
);
5506 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5508 init
->add_statement(len_temp
);
5510 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, loc
);
5512 Temporary_reference_expression
* tref
=
5513 Expression::make_temporary_reference(index_temp
, loc
);
5514 tref
->set_is_lvalue();
5515 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5516 init
->add_statement(s
);
5521 // index_temp < len_temp
5523 Expression
* ref
= Expression::make_temporary_reference(index_temp
, loc
);
5524 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5525 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5529 // Set *PITER_INIT to
5530 // value_temp = range[index_temp]
5532 Block
* iter_init
= NULL
;
5533 if (value_temp
!= NULL
)
5535 iter_init
= new Block(body_block
, loc
);
5537 ref
= Expression::make_temporary_reference(range_temp
, loc
);
5538 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5539 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, NULL
, loc
);
5541 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5542 tref
->set_is_lvalue();
5543 s
= Statement::make_assignment(tref
, index
, loc
);
5545 iter_init
->add_statement(s
);
5547 *piter_init
= iter_init
;
5552 Block
* post
= new Block(enclosing
, loc
);
5553 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5554 tref
->set_is_lvalue();
5555 s
= Statement::make_inc_statement(tref
);
5556 post
->add_statement(s
);
5560 // Lower a for range over a slice.
5563 For_range_statement::lower_range_slice(Gogo
* gogo
,
5566 Named_object
* range_object
,
5567 Temporary_statement
* range_temp
,
5568 Temporary_statement
* index_temp
,
5569 Temporary_statement
* value_temp
,
5575 Location loc
= this->location();
5577 // The loop we generate:
5578 // for_temp := range
5579 // len_temp := len(for_temp)
5580 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5581 // value_temp = for_temp[index_temp]
5582 // index = index_temp
5583 // value = value_temp
5587 // Using for_temp means that we don't need to check bounds when
5588 // fetching range_temp[index_temp].
5591 // range_temp := range
5593 // len_temp = len(range_temp)
5596 Block
* init
= new Block(enclosing
, loc
);
5598 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5599 Temporary_statement
* for_temp
= Statement::make_temporary(NULL
, ref
, loc
);
5600 init
->add_statement(for_temp
);
5602 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5603 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5604 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5606 init
->add_statement(len_temp
);
5608 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, loc
);
5610 Temporary_reference_expression
* tref
=
5611 Expression::make_temporary_reference(index_temp
, loc
);
5612 tref
->set_is_lvalue();
5613 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5614 init
->add_statement(s
);
5619 // index_temp < len_temp
5621 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5622 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5623 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5627 // Set *PITER_INIT to
5628 // value_temp = range[index_temp]
5630 Block
* iter_init
= NULL
;
5631 if (value_temp
!= NULL
)
5633 iter_init
= new Block(body_block
, loc
);
5635 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5636 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5637 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, NULL
, loc
);
5639 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5640 tref
->set_is_lvalue();
5641 s
= Statement::make_assignment(tref
, index
, loc
);
5643 iter_init
->add_statement(s
);
5645 *piter_init
= iter_init
;
5650 Block
* post
= new Block(enclosing
, loc
);
5651 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5652 tref
->set_is_lvalue();
5653 s
= Statement::make_inc_statement(tref
);
5654 post
->add_statement(s
);
5658 // Lower a for range over a string.
5661 For_range_statement::lower_range_string(Gogo
* gogo
,
5664 Named_object
* range_object
,
5665 Temporary_statement
* range_temp
,
5666 Temporary_statement
* index_temp
,
5667 Temporary_statement
* value_temp
,
5673 Location loc
= this->location();
5675 // The loop we generate:
5676 // len_temp := len(range)
5677 // var next_index_temp int
5678 // for index_temp = 0; index_temp < len_temp; index_temp = next_index_temp {
5679 // value_temp = rune(range[index_temp])
5680 // if value_temp < utf8.RuneSelf {
5681 // next_index_temp = index_temp + 1
5683 // value_temp, next_index_temp = decoderune(range, index_temp)
5685 // index = index_temp
5686 // value = value_temp
5691 // len_temp := len(range)
5692 // var next_index_temp int
5694 // var value_temp rune // if value_temp not passed in
5696 Block
* init
= new Block(enclosing
, loc
);
5698 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5699 Call_expression
* call
= this->call_builtin(gogo
, "len", ref
, loc
);
5700 Temporary_statement
* len_temp
=
5701 Statement::make_temporary(index_temp
->type(), call
, loc
);
5702 init
->add_statement(len_temp
);
5704 Temporary_statement
* next_index_temp
=
5705 Statement::make_temporary(index_temp
->type(), NULL
, loc
);
5706 init
->add_statement(next_index_temp
);
5708 Temporary_reference_expression
* index_ref
=
5709 Expression::make_temporary_reference(index_temp
, loc
);
5710 index_ref
->set_is_lvalue();
5711 Expression
* zexpr
= Expression::make_integer_ul(0, index_temp
->type(), loc
);
5712 Statement
* s
= Statement::make_assignment(index_ref
, zexpr
, loc
);
5713 init
->add_statement(s
);
5716 if (value_temp
!= NULL
)
5717 rune_type
= value_temp
->type();
5720 rune_type
= gogo
->lookup_global("rune")->type_value();
5721 value_temp
= Statement::make_temporary(rune_type
, NULL
, loc
);
5722 init
->add_statement(value_temp
);
5728 // index_temp < len_temp
5730 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5731 Expression
* len_ref
=
5732 Expression::make_temporary_reference(len_temp
, loc
);
5733 *pcond
= Expression::make_binary(OPERATOR_LT
, index_ref
, len_ref
, loc
);
5735 // Set *PITER_INIT to
5736 // value_temp = rune(range[index_temp])
5737 // if value_temp < utf8.RuneSelf {
5738 // next_index_temp = index_temp + 1
5740 // value_temp, next_index_temp = decoderune(range, index_temp)
5743 Block
* iter_init
= new Block(body_block
, loc
);
5745 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5746 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5747 ref
= Expression::make_string_index(ref
, index_ref
, NULL
, loc
);
5748 ref
= Expression::make_cast(rune_type
, ref
, loc
);
5749 Temporary_reference_expression
* value_ref
=
5750 Expression::make_temporary_reference(value_temp
, loc
);
5751 value_ref
->set_is_lvalue();
5752 s
= Statement::make_assignment(value_ref
, ref
, loc
);
5753 iter_init
->add_statement(s
);
5755 value_ref
= Expression::make_temporary_reference(value_temp
, loc
);
5756 Expression
* rune_self
= Expression::make_integer_ul(0x80, rune_type
, loc
);
5757 Expression
* cond
= Expression::make_binary(OPERATOR_LT
, value_ref
, rune_self
,
5760 Block
* then_block
= new Block(iter_init
, loc
);
5762 Temporary_reference_expression
* lhs
=
5763 Expression::make_temporary_reference(next_index_temp
, loc
);
5764 lhs
->set_is_lvalue();
5765 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5766 Expression
* one
= Expression::make_integer_ul(1, index_temp
->type(), loc
);
5767 Expression
* sum
= Expression::make_binary(OPERATOR_PLUS
, index_ref
, one
,
5769 s
= Statement::make_assignment(lhs
, sum
, loc
);
5770 then_block
->add_statement(s
);
5772 Block
* else_block
= new Block(iter_init
, loc
);
5774 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5775 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5776 call
= Runtime::make_call(Runtime::DECODERUNE
, loc
, 2, ref
, index_ref
);
5778 value_ref
= Expression::make_temporary_reference(value_temp
, loc
);
5779 value_ref
->set_is_lvalue();
5780 Expression
* res
= Expression::make_call_result(call
, 0);
5781 s
= Statement::make_assignment(value_ref
, res
, loc
);
5782 else_block
->add_statement(s
);
5784 lhs
= Expression::make_temporary_reference(next_index_temp
, loc
);
5785 lhs
->set_is_lvalue();
5786 res
= Expression::make_call_result(call
, 1);
5787 s
= Statement::make_assignment(lhs
, res
, loc
);
5788 else_block
->add_statement(s
);
5790 s
= Statement::make_if_statement(cond
, then_block
, else_block
, loc
);
5791 iter_init
->add_statement(s
);
5793 *piter_init
= iter_init
;
5796 // index_temp = next_index_temp
5798 Block
* post
= new Block(enclosing
, loc
);
5800 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5801 index_ref
->set_is_lvalue();
5802 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5803 s
= Statement::make_assignment(index_ref
, ref
, loc
);
5805 post
->add_statement(s
);
5809 // Lower a for range over a map.
5812 For_range_statement::lower_range_map(Gogo
* gogo
,
5816 Named_object
* range_object
,
5817 Temporary_statement
* range_temp
,
5818 Temporary_statement
* index_temp
,
5819 Temporary_statement
* value_temp
,
5825 Location loc
= this->location();
5827 // The runtime uses a struct to handle ranges over a map. The
5828 // struct is built by Map_type::hiter_type for a specific map type.
5830 // The loop we generate:
5831 // var hiter map_iteration_struct
5832 // for mapiterinit(type, range, &hiter); hiter.key != nil; mapiternext(&hiter) {
5833 // index_temp = *hiter.key
5834 // value_temp = *hiter.val
5835 // index = index_temp
5836 // value = value_temp
5841 // var hiter map_iteration_struct
5842 // runtime.mapiterinit(type, range, &hiter)
5844 Block
* init
= new Block(enclosing
, loc
);
5846 Type
* map_iteration_type
= map_type
->hiter_type(gogo
);
5847 Temporary_statement
* hiter
= Statement::make_temporary(map_iteration_type
,
5849 init
->add_statement(hiter
);
5851 Expression
* p1
= Expression::make_type_descriptor(map_type
, loc
);
5852 Expression
* p2
= this->make_range_ref(range_object
, range_temp
, loc
);
5853 Expression
* ref
= Expression::make_temporary_reference(hiter
, loc
);
5854 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5855 Expression
* call
= Runtime::make_call(Runtime::MAPITERINIT
, loc
, 3,
5857 init
->add_statement(Statement::make_statement(call
, true));
5864 ref
= Expression::make_temporary_reference(hiter
, loc
);
5865 ref
= Expression::make_field_reference(ref
, 0, loc
);
5866 Expression
* ne
= Expression::make_binary(OPERATOR_NOTEQ
, ref
,
5867 Expression::make_nil(loc
),
5871 // Set *PITER_INIT to
5872 // index_temp = *hiter.key
5873 // value_temp = *hiter.val
5875 Block
* iter_init
= new Block(body_block
, loc
);
5877 Expression
* lhs
= Expression::make_temporary_reference(index_temp
, loc
);
5878 Expression
* rhs
= Expression::make_temporary_reference(hiter
, loc
);
5879 rhs
= Expression::make_field_reference(ref
, 0, loc
);
5880 rhs
= Expression::make_dereference(ref
, Expression::NIL_CHECK_NOT_NEEDED
,
5882 Statement
* set
= Statement::make_assignment(lhs
, rhs
, loc
);
5883 iter_init
->add_statement(set
);
5885 if (value_temp
!= NULL
)
5887 lhs
= Expression::make_temporary_reference(value_temp
, loc
);
5888 rhs
= Expression::make_temporary_reference(hiter
, loc
);
5889 rhs
= Expression::make_field_reference(rhs
, 1, loc
);
5890 rhs
= Expression::make_dereference(rhs
, Expression::NIL_CHECK_NOT_NEEDED
,
5892 set
= Statement::make_assignment(lhs
, rhs
, loc
);
5893 iter_init
->add_statement(set
);
5896 *piter_init
= iter_init
;
5899 // mapiternext(&hiter)
5901 Block
* post
= new Block(enclosing
, loc
);
5903 ref
= Expression::make_temporary_reference(hiter
, loc
);
5904 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5905 call
= Runtime::make_call(Runtime::MAPITERNEXT
, loc
, 1, p1
);
5906 post
->add_statement(Statement::make_statement(call
, true));
5911 // Lower a for range over a channel.
5914 For_range_statement::lower_range_channel(Gogo
*,
5917 Named_object
* range_object
,
5918 Temporary_statement
* range_temp
,
5919 Temporary_statement
* index_temp
,
5920 Temporary_statement
* value_temp
,
5926 go_assert(value_temp
== NULL
);
5928 Location loc
= this->location();
5930 // The loop we generate:
5932 // index_temp, ok_temp = <-range
5936 // index = index_temp
5940 // We have no initialization code, no condition, and no post code.
5946 // Set *PITER_INIT to
5947 // index_temp, ok_temp = <-range
5952 Block
* iter_init
= new Block(body_block
, loc
);
5954 Temporary_statement
* ok_temp
=
5955 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
5956 iter_init
->add_statement(ok_temp
);
5958 Expression
* cref
= this->make_range_ref(range_object
, range_temp
, loc
);
5959 Temporary_reference_expression
* iref
=
5960 Expression::make_temporary_reference(index_temp
, loc
);
5961 iref
->set_is_lvalue();
5962 Temporary_reference_expression
* oref
=
5963 Expression::make_temporary_reference(ok_temp
, loc
);
5964 oref
->set_is_lvalue();
5965 Statement
* s
= Statement::make_tuple_receive_assignment(iref
, oref
, cref
,
5967 iter_init
->add_statement(s
);
5969 Block
* then_block
= new Block(iter_init
, loc
);
5970 s
= Statement::make_break_statement(this->break_label(), loc
);
5971 then_block
->add_statement(s
);
5973 oref
= Expression::make_temporary_reference(ok_temp
, loc
);
5974 Expression
* cond
= Expression::make_unary(OPERATOR_NOT
, oref
, loc
);
5975 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
5976 iter_init
->add_statement(s
);
5978 *piter_init
= iter_init
;
5981 // Return the break LABEL_EXPR.
5984 For_range_statement::break_label()
5986 if (this->break_label_
== NULL
)
5987 this->break_label_
= new Unnamed_label(this->location());
5988 return this->break_label_
;
5991 // Return the continue LABEL_EXPR.
5994 For_range_statement::continue_label()
5996 if (this->continue_label_
== NULL
)
5997 this->continue_label_
= new Unnamed_label(this->location());
5998 return this->continue_label_
;
6001 // Dump the AST representation for a for range statement.
6004 For_range_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
6007 ast_dump_context
->print_indent();
6008 ast_dump_context
->ostream() << "for ";
6009 ast_dump_context
->dump_expression(this->index_var_
);
6010 if (this->value_var_
!= NULL
)
6012 ast_dump_context
->ostream() << ", ";
6013 ast_dump_context
->dump_expression(this->value_var_
);
6016 ast_dump_context
->ostream() << " = range ";
6017 ast_dump_context
->dump_expression(this->range_
);
6018 if (ast_dump_context
->dump_subblocks())
6020 ast_dump_context
->ostream() << " {" << std::endl
;
6022 ast_dump_context
->indent();
6024 ast_dump_context
->dump_block(this->statements_
);
6026 ast_dump_context
->unindent();
6027 ast_dump_context
->print_indent();
6028 ast_dump_context
->ostream() << "}";
6030 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
6033 // Make a for statement with a range clause.
6035 For_range_statement
*
6036 Statement::make_for_range_statement(Expression
* index_var
,
6037 Expression
* value_var
,
6041 return new For_range_statement(index_var
, value_var
, range
, location
);