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
689 // call. Mark some slice assignments as not requiring a write barrier.
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
);
753 // An assignment of the form s = s[:n] does not require a write
754 // barrier, because the pointer value will not change.
755 Array_index_expression
* aie
= this->rhs_
->array_index_expression();
757 && aie
->end() != NULL
758 && Expression::is_same_variable(this->lhs_
, aie
->array()))
762 if (aie
->start()->numeric_constant_value(&nc
)
763 && nc
.to_unsigned_long(&ival
) == Numeric_constant::NC_UL_VALID
765 this->omit_write_barrier_
= true;
771 // Set types for the assignment.
774 Assignment_statement::do_determine_types()
776 this->lhs_
->determine_type_no_context();
777 Type
* rhs_context_type
= this->lhs_
->type();
778 if (rhs_context_type
->is_sink_type())
779 rhs_context_type
= NULL
;
780 Type_context
context(rhs_context_type
, false);
781 this->rhs_
->determine_type(&context
);
784 // Check types for an assignment.
787 Assignment_statement::do_check_types(Gogo
*)
789 // The left hand side must be either addressable, a map index
790 // expression, or the blank identifier.
791 if (!this->lhs_
->is_addressable()
792 && this->lhs_
->map_index_expression() == NULL
793 && !this->lhs_
->is_sink_expression())
795 if (!this->lhs_
->type()->is_error())
796 this->report_error(_("invalid left hand side of assignment"));
800 Type
* lhs_type
= this->lhs_
->type();
801 Type
* rhs_type
= this->rhs_
->type();
803 // Invalid assignment of nil to the blank identifier.
804 if (lhs_type
->is_sink_type()
805 && rhs_type
->is_nil_type())
807 this->report_error(_("use of untyped nil"));
812 if (!Type::are_assignable(lhs_type
, rhs_type
, &reason
))
815 go_error_at(this->location(), "incompatible types in assignment");
817 go_error_at(this->location(), "incompatible types in assignment (%s)",
819 this->set_is_error();
822 if (lhs_type
->is_error() || rhs_type
->is_error())
823 this->set_is_error();
826 // Flatten an assignment statement. We may need a temporary for
827 // interface conversion.
830 Assignment_statement::do_flatten(Gogo
*, Named_object
*, Block
*,
831 Statement_inserter
* inserter
)
833 if (this->lhs_
->is_error_expression()
834 || this->lhs_
->type()->is_error_type()
835 || this->rhs_
->is_error_expression()
836 || this->rhs_
->type()->is_error_type())
838 go_assert(saw_errors());
839 return Statement::make_error_statement(this->location());
842 if (!this->lhs_
->is_sink_expression()
843 && !Type::are_identical(this->lhs_
->type(), this->rhs_
->type(),
845 && this->rhs_
->type()->interface_type() != NULL
846 && !this->rhs_
->is_variable())
848 Temporary_statement
* temp
=
849 Statement::make_temporary(NULL
, this->rhs_
, this->location());
850 inserter
->insert(temp
);
851 this->rhs_
= Expression::make_temporary_reference(temp
,
857 // Convert an assignment statement to the backend representation.
860 Assignment_statement::do_get_backend(Translate_context
* context
)
862 if (this->lhs_
->is_sink_expression())
864 Bexpression
* rhs
= this->rhs_
->get_backend(context
);
865 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
866 return context
->backend()->expression_statement(bfunction
, rhs
);
869 Bexpression
* lhs
= this->lhs_
->get_backend(context
);
871 Expression::convert_for_assignment(context
->gogo(), this->lhs_
->type(),
872 this->rhs_
, this->location());
873 Bexpression
* rhs
= conv
->get_backend(context
);
874 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
875 return context
->backend()->assignment_statement(bfunction
, lhs
, rhs
,
879 // Dump the AST representation for an assignment statement.
882 Assignment_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
885 ast_dump_context
->print_indent();
886 ast_dump_context
->dump_expression(this->lhs_
);
887 ast_dump_context
->ostream() << " = " ;
888 ast_dump_context
->dump_expression(this->rhs_
);
889 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
892 // Make an assignment statement.
894 Assignment_statement
*
895 Statement::make_assignment(Expression
* lhs
, Expression
* rhs
,
898 return new Assignment_statement(lhs
, rhs
, location
);
901 // An assignment operation statement.
903 class Assignment_operation_statement
: public Statement
906 Assignment_operation_statement(Operator op
, Expression
* lhs
, Expression
* rhs
,
908 : Statement(STATEMENT_ASSIGNMENT_OPERATION
, location
),
909 op_(op
), lhs_(lhs
), rhs_(rhs
)
914 do_traverse(Traverse
*);
917 do_traverse_assignments(Traverse_assignments
*)
918 { go_unreachable(); }
921 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
924 do_get_backend(Translate_context
*)
925 { go_unreachable(); }
928 do_dump_statement(Ast_dump_context
*) const;
931 // The operator (OPERATOR_PLUSEQ, etc.).
942 Assignment_operation_statement::do_traverse(Traverse
* traverse
)
944 if (this->traverse_expression(traverse
, &this->lhs_
) == TRAVERSE_EXIT
)
945 return TRAVERSE_EXIT
;
946 return this->traverse_expression(traverse
, &this->rhs_
);
949 // Lower an assignment operation statement to a regular assignment
953 Assignment_operation_statement::do_lower(Gogo
*, Named_object
*,
954 Block
* enclosing
, Statement_inserter
*)
956 Location loc
= this->location();
958 // We have to evaluate the left hand side expression only once. We
959 // do this by moving out any expression with side effects.
960 Block
* b
= new Block(enclosing
, loc
);
961 Move_ordered_evals
moe(b
);
962 this->lhs_
->traverse_subexpressions(&moe
);
964 Expression
* lval
= this->lhs_
->copy();
969 case OPERATOR_PLUSEQ
:
972 case OPERATOR_MINUSEQ
:
981 case OPERATOR_MULTEQ
:
990 case OPERATOR_LSHIFTEQ
:
991 op
= OPERATOR_LSHIFT
;
993 case OPERATOR_RSHIFTEQ
:
994 op
= OPERATOR_RSHIFT
;
999 case OPERATOR_BITCLEAREQ
:
1000 op
= OPERATOR_BITCLEAR
;
1006 Expression
* binop
= Expression::make_binary(op
, lval
, this->rhs_
, loc
);
1007 Statement
* s
= Statement::make_assignment(this->lhs_
, binop
, loc
);
1008 if (b
->statements()->empty())
1015 b
->add_statement(s
);
1016 return Statement::make_block_statement(b
, loc
);
1020 // Dump the AST representation for an assignment operation statement
1023 Assignment_operation_statement::do_dump_statement(
1024 Ast_dump_context
* ast_dump_context
) const
1026 ast_dump_context
->print_indent();
1027 ast_dump_context
->dump_expression(this->lhs_
);
1028 ast_dump_context
->dump_operator(this->op_
);
1029 ast_dump_context
->dump_expression(this->rhs_
);
1030 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1033 // Make an assignment operation statement.
1036 Statement::make_assignment_operation(Operator op
, Expression
* lhs
,
1037 Expression
* rhs
, Location location
)
1039 return new Assignment_operation_statement(op
, lhs
, rhs
, location
);
1042 // A tuple assignment statement. This differs from an assignment
1043 // statement in that the right-hand-side expressions are evaluated in
1046 class Tuple_assignment_statement
: public Statement
1049 Tuple_assignment_statement(Expression_list
* lhs
, Expression_list
* rhs
,
1051 : Statement(STATEMENT_TUPLE_ASSIGNMENT
, location
),
1052 lhs_(lhs
), rhs_(rhs
)
1057 do_traverse(Traverse
* traverse
);
1060 do_traverse_assignments(Traverse_assignments
*)
1061 { go_unreachable(); }
1064 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1067 do_get_backend(Translate_context
*)
1068 { go_unreachable(); }
1071 do_dump_statement(Ast_dump_context
*) const;
1074 // Left hand side--a list of lvalues.
1075 Expression_list
* lhs_
;
1076 // Right hand side--a list of rvalues.
1077 Expression_list
* rhs_
;
1083 Tuple_assignment_statement::do_traverse(Traverse
* traverse
)
1085 if (this->traverse_expression_list(traverse
, this->lhs_
) == TRAVERSE_EXIT
)
1086 return TRAVERSE_EXIT
;
1087 return this->traverse_expression_list(traverse
, this->rhs_
);
1090 // Lower a tuple assignment. We use temporary variables to split it
1091 // up into a set of single assignments.
1094 Tuple_assignment_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
1095 Statement_inserter
*)
1097 Location loc
= this->location();
1099 Block
* b
= new Block(enclosing
, loc
);
1101 // First move out any subexpressions on the left hand side. The
1102 // right hand side will be evaluated in the required order anyhow.
1103 Move_ordered_evals
moe(b
);
1104 for (Expression_list::iterator plhs
= this->lhs_
->begin();
1105 plhs
!= this->lhs_
->end();
1107 Expression::traverse(&*plhs
, &moe
);
1109 std::vector
<Temporary_statement
*> temps
;
1110 temps
.reserve(this->lhs_
->size());
1112 Expression_list::const_iterator prhs
= this->rhs_
->begin();
1113 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
1114 plhs
!= this->lhs_
->end();
1117 go_assert(prhs
!= this->rhs_
->end());
1119 if ((*plhs
)->is_error_expression()
1120 || (*plhs
)->type()->is_error()
1121 || (*prhs
)->is_error_expression()
1122 || (*prhs
)->type()->is_error())
1125 if ((*plhs
)->is_sink_expression())
1127 if ((*prhs
)->type()->is_nil_type())
1128 this->report_error(_("use of untyped nil"));
1130 b
->add_statement(Statement::make_statement(*prhs
, true));
1134 Temporary_statement
* temp
= Statement::make_temporary((*plhs
)->type(),
1136 b
->add_statement(temp
);
1137 temps
.push_back(temp
);
1140 go_assert(prhs
== this->rhs_
->end());
1142 prhs
= this->rhs_
->begin();
1143 std::vector
<Temporary_statement
*>::const_iterator ptemp
= temps
.begin();
1144 for (Expression_list::const_iterator plhs
= this->lhs_
->begin();
1145 plhs
!= this->lhs_
->end();
1148 if ((*plhs
)->is_error_expression()
1149 || (*plhs
)->type()->is_error()
1150 || (*prhs
)->is_error_expression()
1151 || (*prhs
)->type()->is_error())
1154 if ((*plhs
)->is_sink_expression())
1157 Expression
* ref
= Expression::make_temporary_reference(*ptemp
, loc
);
1158 b
->add_statement(Statement::make_assignment(*plhs
, ref
, loc
));
1161 go_assert(ptemp
== temps
.end() || saw_errors());
1163 return Statement::make_block_statement(b
, loc
);
1166 // Dump the AST representation for a tuple assignment statement.
1169 Tuple_assignment_statement::do_dump_statement(
1170 Ast_dump_context
* ast_dump_context
) const
1172 ast_dump_context
->print_indent();
1173 ast_dump_context
->dump_expression_list(this->lhs_
);
1174 ast_dump_context
->ostream() << " = ";
1175 ast_dump_context
->dump_expression_list(this->rhs_
);
1176 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1179 // Make a tuple assignment statement.
1182 Statement::make_tuple_assignment(Expression_list
* lhs
, Expression_list
* rhs
,
1185 return new Tuple_assignment_statement(lhs
, rhs
, location
);
1188 // A tuple assignment from a map index expression.
1191 class Tuple_map_assignment_statement
: public Statement
1194 Tuple_map_assignment_statement(Expression
* val
, Expression
* present
,
1195 Expression
* map_index
,
1197 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT
, location
),
1198 val_(val
), present_(present
), map_index_(map_index
)
1203 do_traverse(Traverse
* traverse
);
1206 do_traverse_assignments(Traverse_assignments
*)
1207 { go_unreachable(); }
1210 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1213 do_get_backend(Translate_context
*)
1214 { go_unreachable(); }
1217 do_dump_statement(Ast_dump_context
*) const;
1220 // Lvalue which receives the value from the map.
1222 // Lvalue which receives whether the key value was present.
1223 Expression
* present_
;
1224 // The map index expression.
1225 Expression
* map_index_
;
1231 Tuple_map_assignment_statement::do_traverse(Traverse
* traverse
)
1233 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1234 || this->traverse_expression(traverse
, &this->present_
) == TRAVERSE_EXIT
)
1235 return TRAVERSE_EXIT
;
1236 return this->traverse_expression(traverse
, &this->map_index_
);
1239 // Lower a tuple map assignment.
1242 Tuple_map_assignment_statement::do_lower(Gogo
* gogo
, Named_object
*,
1243 Block
* enclosing
, Statement_inserter
*)
1245 Location loc
= this->location();
1247 Map_index_expression
* map_index
= this->map_index_
->map_index_expression();
1248 if (map_index
== NULL
)
1250 this->report_error(_("expected map index on right hand side"));
1251 return Statement::make_error_statement(loc
);
1253 Map_type
* map_type
= map_index
->get_map_type();
1254 if (map_type
== NULL
)
1255 return Statement::make_error_statement(loc
);
1257 Block
* b
= new Block(enclosing
, loc
);
1259 // Move out any subexpressions to make sure that functions are
1260 // called in the required order.
1261 Move_ordered_evals
moe(b
);
1262 this->val_
->traverse_subexpressions(&moe
);
1263 this->present_
->traverse_subexpressions(&moe
);
1265 // Copy the key value into a temporary so that we can take its
1266 // address without pushing the value onto the heap.
1268 // var key_temp KEY_TYPE = MAP_INDEX
1269 Temporary_statement
* key_temp
=
1270 Statement::make_temporary(map_type
->key_type(), map_index
->index(), loc
);
1271 b
->add_statement(key_temp
);
1273 // var val_ptr_temp *VAL_TYPE
1274 Type
* val_ptr_type
= Type::make_pointer_type(map_type
->val_type());
1275 Temporary_statement
* val_ptr_temp
= Statement::make_temporary(val_ptr_type
,
1277 b
->add_statement(val_ptr_temp
);
1279 // var present_temp bool
1280 Temporary_statement
* present_temp
=
1281 Statement::make_temporary((this->present_
->type()->is_sink_type())
1282 ? Type::make_boolean_type()
1283 : this->present_
->type(),
1285 b
->add_statement(present_temp
);
1287 // val_ptr_temp, present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp)
1288 Expression
* a1
= Expression::make_type_descriptor(map_type
, loc
);
1289 Expression
* a2
= map_index
->map();
1290 Temporary_reference_expression
* ref
=
1291 Expression::make_temporary_reference(key_temp
, loc
);
1292 Expression
* a3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1293 Expression
* a4
= map_type
->fat_zero_value(gogo
);
1294 Call_expression
* call
;
1296 call
= Runtime::make_call(Runtime::MAPACCESS2
, loc
, 3, a1
, a2
, a3
);
1298 call
= Runtime::make_call(Runtime::MAPACCESS2_FAT
, loc
, 4, a1
, a2
, a3
, a4
);
1299 ref
= Expression::make_temporary_reference(val_ptr_temp
, loc
);
1300 ref
->set_is_lvalue();
1301 Expression
* res
= Expression::make_call_result(call
, 0);
1302 res
= Expression::make_unsafe_cast(val_ptr_type
, res
, loc
);
1303 Statement
* s
= Statement::make_assignment(ref
, res
, loc
);
1304 b
->add_statement(s
);
1305 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1306 ref
->set_is_lvalue();
1307 res
= Expression::make_call_result(call
, 1);
1308 s
= Statement::make_assignment(ref
, res
, loc
);
1309 b
->add_statement(s
);
1311 // val = *val__ptr_temp
1312 ref
= Expression::make_temporary_reference(val_ptr_temp
, loc
);
1314 Expression::make_dereference(ref
, Expression::NIL_CHECK_NOT_NEEDED
, loc
);
1315 s
= Statement::make_assignment(this->val_
, ind
, loc
);
1316 b
->add_statement(s
);
1318 // present = present_temp
1319 ref
= Expression::make_temporary_reference(present_temp
, loc
);
1320 s
= Statement::make_assignment(this->present_
, ref
, loc
);
1321 b
->add_statement(s
);
1323 return Statement::make_block_statement(b
, loc
);
1326 // Dump the AST representation for a tuple map assignment statement.
1329 Tuple_map_assignment_statement::do_dump_statement(
1330 Ast_dump_context
* ast_dump_context
) const
1332 ast_dump_context
->print_indent();
1333 ast_dump_context
->dump_expression(this->val_
);
1334 ast_dump_context
->ostream() << ", ";
1335 ast_dump_context
->dump_expression(this->present_
);
1336 ast_dump_context
->ostream() << " = ";
1337 ast_dump_context
->dump_expression(this->map_index_
);
1338 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1341 // Make a map assignment statement which returns a pair of values.
1344 Statement::make_tuple_map_assignment(Expression
* val
, Expression
* present
,
1345 Expression
* map_index
,
1348 return new Tuple_map_assignment_statement(val
, present
, map_index
, location
);
1351 // A tuple assignment from a receive statement.
1353 class Tuple_receive_assignment_statement
: public Statement
1356 Tuple_receive_assignment_statement(Expression
* val
, Expression
* closed
,
1357 Expression
* channel
, Location location
)
1358 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
, location
),
1359 val_(val
), closed_(closed
), channel_(channel
)
1364 do_traverse(Traverse
* traverse
);
1367 do_traverse_assignments(Traverse_assignments
*)
1368 { go_unreachable(); }
1371 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1374 do_get_backend(Translate_context
*)
1375 { go_unreachable(); }
1378 do_dump_statement(Ast_dump_context
*) const;
1381 // Lvalue which receives the value from the channel.
1383 // Lvalue which receives whether the channel is closed.
1384 Expression
* closed_
;
1385 // The channel on which we receive the value.
1386 Expression
* channel_
;
1392 Tuple_receive_assignment_statement::do_traverse(Traverse
* traverse
)
1394 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1395 || this->traverse_expression(traverse
, &this->closed_
) == TRAVERSE_EXIT
)
1396 return TRAVERSE_EXIT
;
1397 return this->traverse_expression(traverse
, &this->channel_
);
1400 // Lower to a function call.
1403 Tuple_receive_assignment_statement::do_lower(Gogo
*, Named_object
*,
1405 Statement_inserter
*)
1407 Location loc
= this->location();
1409 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
1410 if (channel_type
== NULL
)
1412 this->report_error(_("expected channel"));
1413 return Statement::make_error_statement(loc
);
1415 if (!channel_type
->may_receive())
1417 this->report_error(_("invalid receive on send-only channel"));
1418 return Statement::make_error_statement(loc
);
1421 Block
* b
= new Block(enclosing
, loc
);
1423 // Make sure that any subexpressions on the left hand side are
1424 // evaluated in the right order.
1425 Move_ordered_evals
moe(b
);
1426 this->val_
->traverse_subexpressions(&moe
);
1427 this->closed_
->traverse_subexpressions(&moe
);
1429 // var val_temp ELEMENT_TYPE
1430 Temporary_statement
* val_temp
=
1431 Statement::make_temporary(channel_type
->element_type(), NULL
, loc
);
1432 b
->add_statement(val_temp
);
1434 // var closed_temp bool
1435 Temporary_statement
* closed_temp
=
1436 Statement::make_temporary((this->closed_
->type()->is_sink_type())
1437 ? Type::make_boolean_type()
1438 : this->closed_
->type(),
1440 b
->add_statement(closed_temp
);
1442 // closed_temp = chanrecv2(channel, &val_temp)
1443 Temporary_reference_expression
* ref
=
1444 Expression::make_temporary_reference(val_temp
, loc
);
1445 Expression
* p2
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1446 Expression
* call
= Runtime::make_call(Runtime::CHANRECV2
,
1447 loc
, 2, this->channel_
, p2
);
1448 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1449 ref
->set_is_lvalue();
1450 Statement
* s
= Statement::make_assignment(ref
, call
, loc
);
1451 b
->add_statement(s
);
1454 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1455 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1456 b
->add_statement(s
);
1458 // closed = closed_temp
1459 ref
= Expression::make_temporary_reference(closed_temp
, loc
);
1460 s
= Statement::make_assignment(this->closed_
, ref
, loc
);
1461 b
->add_statement(s
);
1463 return Statement::make_block_statement(b
, loc
);
1466 // Dump the AST representation for a tuple receive statement.
1469 Tuple_receive_assignment_statement::do_dump_statement(
1470 Ast_dump_context
* ast_dump_context
) const
1472 ast_dump_context
->print_indent();
1473 ast_dump_context
->dump_expression(this->val_
);
1474 ast_dump_context
->ostream() << ", ";
1475 ast_dump_context
->dump_expression(this->closed_
);
1476 ast_dump_context
->ostream() << " <- ";
1477 ast_dump_context
->dump_expression(this->channel_
);
1478 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1481 // Make a nonblocking receive statement.
1484 Statement::make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
1485 Expression
* channel
,
1488 return new Tuple_receive_assignment_statement(val
, closed
, channel
,
1492 // An assignment to a pair of values from a type guard. This is a
1493 // conditional type guard. v, ok = i.(type).
1495 class Tuple_type_guard_assignment_statement
: public Statement
1498 Tuple_type_guard_assignment_statement(Expression
* val
, Expression
* ok
,
1499 Expression
* expr
, Type
* type
,
1501 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
, location
),
1502 val_(val
), ok_(ok
), expr_(expr
), type_(type
)
1507 do_traverse(Traverse
*);
1510 do_traverse_assignments(Traverse_assignments
*)
1511 { go_unreachable(); }
1514 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1517 do_get_backend(Translate_context
*)
1518 { go_unreachable(); }
1521 do_dump_statement(Ast_dump_context
*) const;
1525 lower_to_type(Runtime::Function
);
1528 lower_to_object_type(Block
*, Runtime::Function
);
1530 // The variable which recieves the converted value.
1532 // The variable which receives the indication of success.
1534 // The expression being converted.
1536 // The type to which the expression is being converted.
1540 // Traverse a type guard tuple assignment.
1543 Tuple_type_guard_assignment_statement::do_traverse(Traverse
* traverse
)
1545 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
1546 || this->traverse_expression(traverse
, &this->ok_
) == TRAVERSE_EXIT
1547 || this->traverse_type(traverse
, this->type_
) == TRAVERSE_EXIT
)
1548 return TRAVERSE_EXIT
;
1549 return this->traverse_expression(traverse
, &this->expr_
);
1552 // Lower to a function call.
1555 Tuple_type_guard_assignment_statement::do_lower(Gogo
*, Named_object
*,
1557 Statement_inserter
*)
1559 Location loc
= this->location();
1561 Type
* expr_type
= this->expr_
->type();
1562 if (expr_type
->interface_type() == NULL
)
1564 if (!expr_type
->is_error() && !this->type_
->is_error())
1565 this->report_error(_("type assertion only valid for interface types"));
1566 return Statement::make_error_statement(loc
);
1569 Block
* b
= new Block(enclosing
, loc
);
1571 // Make sure that any subexpressions on the left hand side are
1572 // evaluated in the right order.
1573 Move_ordered_evals
moe(b
);
1574 this->val_
->traverse_subexpressions(&moe
);
1575 this->ok_
->traverse_subexpressions(&moe
);
1577 bool expr_is_empty
= expr_type
->interface_type()->is_empty();
1578 Call_expression
* call
;
1579 if (this->type_
->interface_type() != NULL
)
1581 if (this->type_
->interface_type()->is_empty())
1582 call
= Runtime::make_call((expr_is_empty
1583 ? Runtime::IFACEE2E2
1584 : Runtime::IFACEI2E2
),
1585 loc
, 1, this->expr_
);
1587 call
= this->lower_to_type(expr_is_empty
1588 ? Runtime::IFACEE2I2
1589 : Runtime::IFACEI2I2
);
1591 else if (this->type_
->points_to() != NULL
)
1592 call
= this->lower_to_type(expr_is_empty
1593 ? Runtime::IFACEE2T2P
1594 : Runtime::IFACEI2T2P
);
1597 this->lower_to_object_type(b
,
1599 ? Runtime::IFACEE2T2
1600 : Runtime::IFACEI2T2
));
1606 Expression
* res
= Expression::make_call_result(call
, 0);
1607 res
= Expression::make_unsafe_cast(this->type_
, res
, loc
);
1608 Statement
* s
= Statement::make_assignment(this->val_
, res
, loc
);
1609 b
->add_statement(s
);
1611 res
= Expression::make_call_result(call
, 1);
1612 s
= Statement::make_assignment(this->ok_
, res
, loc
);
1613 b
->add_statement(s
);
1616 return Statement::make_block_statement(b
, loc
);
1619 // Lower a conversion to a non-empty interface type or a pointer type.
1622 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code
)
1624 Location loc
= this->location();
1625 return Runtime::make_call(code
, loc
, 2,
1626 Expression::make_type_descriptor(this->type_
, loc
),
1630 // Lower a conversion to a non-interface non-pointer type.
1633 Tuple_type_guard_assignment_statement::lower_to_object_type(
1635 Runtime::Function code
)
1637 Location loc
= this->location();
1639 // var val_temp TYPE
1640 Temporary_statement
* val_temp
= Statement::make_temporary(this->type_
,
1642 b
->add_statement(val_temp
);
1644 // ok = CODE(type_descriptor, expr, &val_temp)
1645 Expression
* p1
= Expression::make_type_descriptor(this->type_
, loc
);
1646 Expression
* ref
= Expression::make_temporary_reference(val_temp
, loc
);
1647 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
1648 Expression
* call
= Runtime::make_call(code
, loc
, 3, p1
, this->expr_
, p3
);
1649 Statement
* s
= Statement::make_assignment(this->ok_
, call
, loc
);
1650 b
->add_statement(s
);
1653 ref
= Expression::make_temporary_reference(val_temp
, loc
);
1654 s
= Statement::make_assignment(this->val_
, ref
, loc
);
1655 b
->add_statement(s
);
1658 // Dump the AST representation for a tuple type guard statement.
1661 Tuple_type_guard_assignment_statement::do_dump_statement(
1662 Ast_dump_context
* ast_dump_context
) const
1664 ast_dump_context
->print_indent();
1665 ast_dump_context
->dump_expression(this->val_
);
1666 ast_dump_context
->ostream() << ", ";
1667 ast_dump_context
->dump_expression(this->ok_
);
1668 ast_dump_context
->ostream() << " = ";
1669 ast_dump_context
->dump_expression(this->expr_
);
1670 ast_dump_context
->ostream() << " . ";
1671 ast_dump_context
->dump_type(this->type_
);
1672 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1675 // Make an assignment from a type guard to a pair of variables.
1678 Statement::make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
1679 Expression
* expr
, Type
* type
,
1682 return new Tuple_type_guard_assignment_statement(val
, ok
, expr
, type
,
1686 // Class Expression_statement.
1690 Expression_statement::Expression_statement(Expression
* expr
, bool is_ignored
)
1691 : Statement(STATEMENT_EXPRESSION
, expr
->location()),
1692 expr_(expr
), is_ignored_(is_ignored
)
1699 Expression_statement::do_determine_types()
1701 this->expr_
->determine_type_no_context();
1704 // Check the types of an expression statement. The only check we do
1705 // is to possibly give an error about discarding the value of the
1709 Expression_statement::do_check_types(Gogo
*)
1711 if (!this->is_ignored_
)
1712 this->expr_
->discarding_value();
1715 // An expression statement is only a terminating statement if it is
1719 Expression_statement::do_may_fall_through() const
1721 const Call_expression
* call
= this->expr_
->call_expression();
1724 const Expression
* fn
= call
->fn();
1725 // panic is still an unknown named object.
1726 const Unknown_expression
* ue
= fn
->unknown_expression();
1729 Named_object
* no
= ue
->named_object();
1731 if (no
->is_unknown())
1732 no
= no
->unknown_value()->real_named_object();
1735 Function_type
* fntype
;
1736 if (no
->is_function())
1737 fntype
= no
->func_value()->type();
1738 else if (no
->is_function_declaration())
1739 fntype
= no
->func_declaration_value()->type();
1743 // The builtin function panic does not return.
1744 if (fntype
!= NULL
&& fntype
->is_builtin() && no
->name() == "panic")
1752 // Convert to backend representation.
1755 Expression_statement::do_get_backend(Translate_context
* context
)
1757 Bexpression
* bexpr
= this->expr_
->get_backend(context
);
1758 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
1759 return context
->backend()->expression_statement(bfunction
, bexpr
);
1762 // Dump the AST representation for an expression statement
1765 Expression_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
1768 ast_dump_context
->print_indent();
1769 ast_dump_context
->dump_expression(expr_
);
1770 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
1773 // Make an expression statement from an Expression.
1776 Statement::make_statement(Expression
* expr
, bool is_ignored
)
1778 return new Expression_statement(expr
, is_ignored
);
1781 // Convert a block to the backend representation of a statement.
1784 Block_statement::do_get_backend(Translate_context
* context
)
1786 Bblock
* bblock
= this->block_
->get_backend(context
);
1787 return context
->backend()->block_statement(bblock
);
1790 // Dump the AST for a block statement
1793 Block_statement::do_dump_statement(Ast_dump_context
*) const
1795 // block statement braces are dumped when traversing.
1798 // Make a block statement.
1801 Statement::make_block_statement(Block
* block
, Location location
)
1803 return new Block_statement(block
, location
);
1806 // An increment or decrement statement.
1808 class Inc_dec_statement
: public Statement
1811 Inc_dec_statement(bool is_inc
, Expression
* expr
)
1812 : Statement(STATEMENT_INCDEC
, expr
->location()),
1813 expr_(expr
), is_inc_(is_inc
)
1818 do_traverse(Traverse
* traverse
)
1819 { return this->traverse_expression(traverse
, &this->expr_
); }
1822 do_traverse_assignments(Traverse_assignments
*)
1823 { go_unreachable(); }
1826 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1829 do_get_backend(Translate_context
*)
1830 { go_unreachable(); }
1833 do_dump_statement(Ast_dump_context
*) const;
1836 // The l-value to increment or decrement.
1838 // Whether to increment or decrement.
1842 // Lower to += or -=.
1845 Inc_dec_statement::do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
1847 Location loc
= this->location();
1848 if (!this->expr_
->type()->is_numeric_type())
1850 this->report_error("increment or decrement of non-numeric type");
1851 return Statement::make_error_statement(loc
);
1853 Expression
* oexpr
= Expression::make_integer_ul(1, this->expr_
->type(), loc
);
1854 Operator op
= this->is_inc_
? OPERATOR_PLUSEQ
: OPERATOR_MINUSEQ
;
1855 return Statement::make_assignment_operation(op
, this->expr_
, oexpr
, loc
);
1858 // Dump the AST representation for a inc/dec statement.
1861 Inc_dec_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
1863 ast_dump_context
->print_indent();
1864 ast_dump_context
->dump_expression(expr_
);
1865 ast_dump_context
->ostream() << (is_inc_
? "++": "--") << dsuffix(location()) << std::endl
;
1868 // Make an increment statement.
1871 Statement::make_inc_statement(Expression
* expr
)
1873 return new Inc_dec_statement(true, expr
);
1876 // Make a decrement statement.
1879 Statement::make_dec_statement(Expression
* expr
)
1881 return new Inc_dec_statement(false, expr
);
1884 // Class Thunk_statement. This is the base class for go and defer
1889 Thunk_statement::Thunk_statement(Statement_classification classification
,
1890 Call_expression
* call
,
1892 : Statement(classification
, location
),
1893 call_(call
), struct_type_(NULL
)
1897 // Return whether this is a simple statement which does not require a
1901 Thunk_statement::is_simple(Function_type
* fntype
) const
1903 // We need a thunk to call a method, or to pass a variable number of
1905 if (fntype
->is_method() || fntype
->is_varargs())
1908 // A defer statement requires a thunk to set up for whether the
1909 // function can call recover.
1910 if (this->classification() == STATEMENT_DEFER
)
1913 // We can only permit a single parameter of pointer type.
1914 const Typed_identifier_list
* parameters
= fntype
->parameters();
1915 if (parameters
!= NULL
1916 && (parameters
->size() > 1
1917 || (parameters
->size() == 1
1918 && parameters
->begin()->type()->points_to() == NULL
)))
1921 // If the function returns multiple values, or returns a type other
1922 // than integer, floating point, or pointer, then it may get a
1923 // hidden first parameter, in which case we need the more
1924 // complicated approach. This is true even though we are going to
1925 // ignore the return value.
1926 const Typed_identifier_list
* results
= fntype
->results();
1928 && (results
->size() > 1
1929 || (results
->size() == 1
1930 && !results
->begin()->type()->is_basic_type()
1931 && results
->begin()->type()->points_to() == NULL
)))
1934 // If this calls something that is not a simple function, then we
1936 Expression
* fn
= this->call_
->call_expression()->fn();
1937 if (fn
->func_expression() == NULL
)
1940 // If the function uses a closure, then we need a thunk. FIXME: We
1941 // could accept a zero argument function with a closure.
1942 if (fn
->func_expression()->closure() != NULL
)
1948 // Traverse a thunk statement.
1951 Thunk_statement::do_traverse(Traverse
* traverse
)
1953 return this->traverse_expression(traverse
, &this->call_
);
1956 // We implement traverse_assignment for a thunk statement because it
1957 // effectively copies the function call.
1960 Thunk_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
1962 Expression
* fn
= this->call_
->call_expression()->fn();
1963 Expression
* fn2
= fn
;
1964 tassign
->value(&fn2
, true, false);
1968 // Determine types in a thunk statement.
1971 Thunk_statement::do_determine_types()
1973 this->call_
->determine_type_no_context();
1975 // Now that we know the types of the call, build the struct used to
1977 Call_expression
* ce
= this->call_
->call_expression();
1980 Function_type
* fntype
= ce
->get_function_type();
1981 if (fntype
!= NULL
&& !this->is_simple(fntype
))
1982 this->struct_type_
= this->build_struct(fntype
);
1985 // Check types in a thunk statement.
1988 Thunk_statement::do_check_types(Gogo
*)
1990 if (!this->call_
->discarding_value())
1992 Call_expression
* ce
= this->call_
->call_expression();
1995 if (!this->call_
->is_error_expression())
1996 this->report_error("expected call expression");
2001 // The Traverse class used to find and simplify thunk statements.
2003 class Simplify_thunk_traverse
: public Traverse
2006 Simplify_thunk_traverse(Gogo
* gogo
)
2007 : Traverse(traverse_functions
| traverse_blocks
),
2008 gogo_(gogo
), function_(NULL
)
2012 function(Named_object
*);
2020 // The function we are traversing.
2021 Named_object
* function_
;
2024 // Keep track of the current function while looking for thunks.
2027 Simplify_thunk_traverse::function(Named_object
* no
)
2029 go_assert(this->function_
== NULL
);
2030 this->function_
= no
;
2031 int t
= no
->func_value()->traverse(this);
2032 this->function_
= NULL
;
2033 if (t
== TRAVERSE_EXIT
)
2035 return TRAVERSE_SKIP_COMPONENTS
;
2038 // Look for thunks in a block.
2041 Simplify_thunk_traverse::block(Block
* b
)
2043 // The parser ensures that thunk statements always appear at the end
2045 if (b
->statements()->size() < 1)
2046 return TRAVERSE_CONTINUE
;
2047 Thunk_statement
* stat
= b
->statements()->back()->thunk_statement();
2049 return TRAVERSE_CONTINUE
;
2050 if (stat
->simplify_statement(this->gogo_
, this->function_
, b
))
2051 return TRAVERSE_SKIP_COMPONENTS
;
2052 return TRAVERSE_CONTINUE
;
2055 // Simplify all thunk statements.
2058 Gogo::simplify_thunk_statements()
2060 Simplify_thunk_traverse
thunk_traverse(this);
2061 this->traverse(&thunk_traverse
);
2064 // Return true if the thunk function is a constant, which means that
2065 // it does not need to be passed to the thunk routine.
2068 Thunk_statement::is_constant_function() const
2070 Call_expression
* ce
= this->call_
->call_expression();
2071 Function_type
* fntype
= ce
->get_function_type();
2074 go_assert(saw_errors());
2077 if (fntype
->is_builtin())
2079 Expression
* fn
= ce
->fn();
2080 if (fn
->func_expression() != NULL
)
2081 return fn
->func_expression()->closure() == NULL
;
2082 if (fn
->interface_field_reference_expression() != NULL
)
2087 // Simplify complex thunk statements into simple ones. A complicated
2088 // thunk statement is one which takes anything other than zero
2089 // parameters or a single pointer parameter. We rewrite it into code
2090 // which allocates a struct, stores the parameter values into the
2091 // struct, and does a simple go or defer statement which passes the
2092 // struct to a thunk. The thunk does the real call.
2095 Thunk_statement::simplify_statement(Gogo
* gogo
, Named_object
* function
,
2098 if (this->classification() == STATEMENT_ERROR
)
2100 if (this->call_
->is_error_expression())
2103 if (this->classification() == STATEMENT_DEFER
)
2105 // Make sure that the defer stack exists for the function. We
2106 // will use when converting this statement to the backend
2107 // representation, but we want it to exist when we start
2108 // converting the function.
2109 function
->func_value()->defer_stack(this->location());
2112 Call_expression
* ce
= this->call_
->call_expression();
2113 Function_type
* fntype
= ce
->get_function_type();
2116 go_assert(saw_errors());
2117 this->set_is_error();
2120 if (this->is_simple(fntype
))
2123 Expression
* fn
= ce
->fn();
2124 Interface_field_reference_expression
* interface_method
=
2125 fn
->interface_field_reference_expression();
2127 Location location
= this->location();
2129 bool is_constant_function
= this->is_constant_function();
2130 Temporary_statement
* fn_temp
= NULL
;
2131 if (!is_constant_function
)
2133 fn_temp
= Statement::make_temporary(NULL
, fn
, location
);
2134 block
->insert_statement_before(block
->statements()->size() - 1, fn_temp
);
2135 fn
= Expression::make_temporary_reference(fn_temp
, location
);
2138 std::string thunk_name
= gogo
->thunk_name();
2141 this->build_thunk(gogo
, thunk_name
);
2143 // Generate code to call the thunk.
2145 // Get the values to store into the struct which is the single
2146 // argument to the thunk.
2148 Expression_list
* vals
= new Expression_list();
2149 if (!is_constant_function
)
2150 vals
->push_back(fn
);
2152 if (interface_method
!= NULL
)
2153 vals
->push_back(interface_method
->expr());
2155 if (ce
->args() != NULL
)
2157 for (Expression_list::const_iterator p
= ce
->args()->begin();
2158 p
!= ce
->args()->end();
2161 if ((*p
)->is_constant())
2163 vals
->push_back(*p
);
2167 // Build the struct.
2168 Expression
* constructor
=
2169 Expression::make_struct_composite_literal(this->struct_type_
, vals
,
2172 // Allocate the initialized struct on the heap.
2173 constructor
= Expression::make_heap_expression(constructor
, location
);
2174 if ((Node::make_node(this)->encoding() & ESCAPE_MASK
) == Node::ESCAPE_NONE
)
2175 constructor
->heap_expression()->set_allocate_on_stack();
2177 // Throw an error if the function is nil. This is so that for `go
2178 // nil` we get a backtrace from the go statement, rather than a
2179 // useless backtrace from the brand new goroutine.
2180 Expression
* param
= constructor
;
2181 if (!is_constant_function
)
2183 fn
= Expression::make_temporary_reference(fn_temp
, location
);
2184 Expression
* nil
= Expression::make_nil(location
);
2185 Expression
* isnil
= Expression::make_binary(OPERATOR_EQEQ
, fn
, nil
,
2187 Expression
* crash
= gogo
->runtime_error(RUNTIME_ERROR_GO_NIL
, location
);
2188 crash
= Expression::make_conditional(isnil
, crash
,
2189 Expression::make_nil(location
),
2191 param
= Expression::make_compound(crash
, constructor
, location
);
2194 // Look up the thunk.
2195 Named_object
* named_thunk
= gogo
->lookup(thunk_name
, NULL
);
2196 go_assert(named_thunk
!= NULL
&& named_thunk
->is_function());
2199 Expression
* func
= Expression::make_func_reference(named_thunk
, NULL
,
2201 Expression_list
* params
= new Expression_list();
2202 params
->push_back(param
);
2203 Call_expression
* call
= Expression::make_call(func
, params
, false, location
);
2205 // Build the simple go or defer statement.
2207 if (this->classification() == STATEMENT_GO
)
2208 s
= Statement::make_go_statement(call
, location
);
2209 else if (this->classification() == STATEMENT_DEFER
)
2210 s
= Statement::make_defer_statement(call
, location
);
2214 // The current block should end with the go statement.
2215 go_assert(block
->statements()->size() >= 1);
2216 go_assert(block
->statements()->back() == this);
2217 block
->replace_statement(block
->statements()->size() - 1, s
);
2219 // We already ran the determine_types pass, so we need to run it now
2220 // for the new statement.
2221 s
->determine_types();
2224 gogo
->check_types_in_block(block
);
2226 // Return true to tell the block not to keep looking at statements.
2230 // Set the name to use for thunk parameter N.
2233 Thunk_statement::thunk_field_param(int n
, char* buf
, size_t buflen
)
2235 snprintf(buf
, buflen
, "a%d", n
);
2238 // Build a new struct type to hold the parameters for a complicated
2239 // thunk statement. FNTYPE is the type of the function call.
2242 Thunk_statement::build_struct(Function_type
* fntype
)
2244 Location location
= this->location();
2246 Struct_field_list
* fields
= new Struct_field_list();
2248 Call_expression
* ce
= this->call_
->call_expression();
2249 Expression
* fn
= ce
->fn();
2251 if (!this->is_constant_function())
2253 // The function to call.
2254 fields
->push_back(Struct_field(Typed_identifier("fn", fntype
,
2258 // If this thunk statement calls a method on an interface, we pass
2259 // the interface object to the thunk.
2260 Interface_field_reference_expression
* interface_method
=
2261 fn
->interface_field_reference_expression();
2262 if (interface_method
!= NULL
)
2264 Typed_identifier
tid("object", interface_method
->expr()->type(),
2266 fields
->push_back(Struct_field(tid
));
2269 // The predeclared recover function has no argument. However, we
2270 // add an argument when building recover thunks. Handle that here.
2271 if (ce
->is_recover_call())
2273 fields
->push_back(Struct_field(Typed_identifier("can_recover",
2274 Type::lookup_bool_type(),
2278 const Expression_list
* args
= ce
->args();
2282 for (Expression_list::const_iterator p
= args
->begin();
2286 if ((*p
)->is_constant())
2290 this->thunk_field_param(i
, buf
, sizeof buf
);
2291 fields
->push_back(Struct_field(Typed_identifier(buf
, (*p
)->type(),
2296 Struct_type
*st
= Type::make_struct_type(fields
, location
);
2297 st
->set_is_struct_incomparable();
2301 // Build the thunk we are going to call. This is a brand new, albeit
2302 // artificial, function.
2305 Thunk_statement::build_thunk(Gogo
* gogo
, const std::string
& thunk_name
)
2307 Location location
= this->location();
2309 Call_expression
* ce
= this->call_
->call_expression();
2311 bool may_call_recover
= false;
2312 if (this->classification() == STATEMENT_DEFER
)
2314 Func_expression
* fn
= ce
->fn()->func_expression();
2316 may_call_recover
= true;
2319 const Named_object
* no
= fn
->named_object();
2320 if (!no
->is_function())
2321 may_call_recover
= true;
2323 may_call_recover
= no
->func_value()->calls_recover();
2327 // Build the type of the thunk. The thunk takes a single parameter,
2328 // which is a pointer to the special structure we build.
2329 const char* const parameter_name
= "__go_thunk_parameter";
2330 Typed_identifier_list
* thunk_parameters
= new Typed_identifier_list();
2331 Type
* pointer_to_struct_type
= Type::make_pointer_type(this->struct_type_
);
2332 thunk_parameters
->push_back(Typed_identifier(parameter_name
,
2333 pointer_to_struct_type
,
2336 Typed_identifier_list
* thunk_results
= NULL
;
2337 if (may_call_recover
)
2339 // When deferring a function which may call recover, add a
2340 // return value, to disable tail call optimizations which will
2341 // break the way we check whether recover is permitted.
2342 thunk_results
= new Typed_identifier_list();
2343 thunk_results
->push_back(Typed_identifier("", Type::lookup_bool_type(),
2347 Function_type
* thunk_type
= Type::make_function_type(NULL
, thunk_parameters
,
2351 // Start building the thunk.
2352 Named_object
* function
= gogo
->start_function(thunk_name
, thunk_type
, true,
2355 gogo
->start_block(location
);
2357 // For a defer statement, start with a call to
2358 // __go_set_defer_retaddr. */
2359 Label
* retaddr_label
= NULL
;
2360 if (may_call_recover
)
2362 retaddr_label
= gogo
->add_label_reference("retaddr", location
, false);
2363 Expression
* arg
= Expression::make_label_addr(retaddr_label
, location
);
2364 Expression
* call
= Runtime::make_call(Runtime::SETDEFERRETADDR
,
2367 // This is a hack to prevent the middle-end from deleting the
2369 gogo
->start_block(location
);
2370 gogo
->add_statement(Statement::make_goto_statement(retaddr_label
,
2372 Block
* then_block
= gogo
->finish_block(location
);
2373 then_block
->determine_types();
2375 Statement
* s
= Statement::make_if_statement(call
, then_block
, NULL
,
2377 s
->determine_types();
2378 gogo
->add_statement(s
);
2380 function
->func_value()->set_calls_defer_retaddr();
2383 // Get a reference to the parameter.
2384 Named_object
* named_parameter
= gogo
->lookup(parameter_name
, NULL
);
2385 go_assert(named_parameter
!= NULL
&& named_parameter
->is_variable());
2387 // Build the call. Note that the field names are the same as the
2388 // ones used in build_struct.
2389 Expression
* thunk_parameter
= Expression::make_var_reference(named_parameter
,
2392 Expression::make_dereference(thunk_parameter
,
2393 Expression::NIL_CHECK_NOT_NEEDED
,
2396 Interface_field_reference_expression
* interface_method
=
2397 ce
->fn()->interface_field_reference_expression();
2399 Expression
* func_to_call
;
2400 unsigned int next_index
;
2401 if (this->is_constant_function())
2403 func_to_call
= ce
->fn();
2408 func_to_call
= Expression::make_field_reference(thunk_parameter
,
2413 if (interface_method
!= NULL
)
2415 // The main program passes the interface object.
2416 go_assert(next_index
== 0);
2417 Expression
* r
= Expression::make_field_reference(thunk_parameter
, 0,
2419 const std::string
& name(interface_method
->name());
2420 func_to_call
= Expression::make_interface_field_reference(r
, name
,
2425 Expression_list
* call_params
= new Expression_list();
2426 const Struct_field_list
* fields
= this->struct_type_
->fields();
2427 Struct_field_list::const_iterator p
= fields
->begin();
2428 for (unsigned int i
= 0; i
< next_index
; ++i
)
2430 bool is_recover_call
= ce
->is_recover_call();
2431 Expression
* recover_arg
= NULL
;
2433 const Expression_list
* args
= ce
->args();
2436 for (Expression_list::const_iterator arg
= args
->begin();
2441 if ((*arg
)->is_constant())
2445 Expression
* thunk_param
=
2446 Expression::make_var_reference(named_parameter
, location
);
2448 Expression::make_dereference(thunk_param
,
2449 Expression::NIL_CHECK_NOT_NEEDED
,
2451 param
= Expression::make_field_reference(thunk_param
,
2457 if (!is_recover_call
)
2458 call_params
->push_back(param
);
2461 go_assert(call_params
->empty());
2462 recover_arg
= param
;
2467 if (call_params
->empty())
2473 Call_expression
* call
= Expression::make_call(func_to_call
, call_params
,
2476 // This call expression was already lowered before entering the
2477 // thunk statement. Don't try to lower varargs again, as that will
2478 // cause confusion for, e.g., method calls which already have a
2479 // receiver parameter.
2480 call
->set_varargs_are_lowered();
2482 Statement
* call_statement
= Statement::make_statement(call
, true);
2484 gogo
->add_statement(call_statement
);
2486 // If this is a defer statement, the label comes immediately after
2488 if (may_call_recover
)
2490 gogo
->add_label_definition("retaddr", location
);
2492 Expression_list
* vals
= new Expression_list();
2493 vals
->push_back(Expression::make_boolean(false, location
));
2494 gogo
->add_statement(Statement::make_return_statement(vals
, location
));
2497 Block
* b
= gogo
->finish_block(location
);
2499 gogo
->add_block(b
, location
);
2501 gogo
->lower_block(function
, b
);
2503 // We already ran the determine_types pass, so we need to run it
2504 // just for the call statement now. The other types are known.
2505 call_statement
->determine_types();
2507 gogo
->flatten_block(function
, b
);
2509 if (may_call_recover
2510 || recover_arg
!= NULL
2511 || this->classification() == STATEMENT_GO
)
2513 // Dig up the call expression, which may have been changed
2515 go_assert(call_statement
->classification() == STATEMENT_EXPRESSION
);
2516 Expression_statement
* es
=
2517 static_cast<Expression_statement
*>(call_statement
);
2518 Call_expression
* ce
= es
->expr()->call_expression();
2520 go_assert(saw_errors());
2523 if (may_call_recover
)
2524 ce
->set_is_deferred();
2525 if (this->classification() == STATEMENT_GO
)
2526 ce
->set_is_concurrent();
2527 if (recover_arg
!= NULL
)
2528 ce
->set_recover_arg(recover_arg
);
2532 // That is all the thunk has to do.
2533 gogo
->finish_function(location
);
2536 // Get the function and argument expressions.
2539 Thunk_statement::get_fn_and_arg(Expression
** pfn
, Expression
** parg
)
2541 if (this->call_
->is_error_expression())
2544 Call_expression
* ce
= this->call_
->call_expression();
2546 Expression
* fn
= ce
->fn();
2547 Func_expression
* fe
= fn
->func_expression();
2548 go_assert(fe
!= NULL
);
2549 *pfn
= Expression::make_func_code_reference(fe
->named_object(),
2552 const Expression_list
* args
= ce
->args();
2553 if (args
== NULL
|| args
->empty())
2554 *parg
= Expression::make_nil(this->location());
2557 go_assert(args
->size() == 1);
2558 *parg
= args
->front();
2564 // Class Go_statement.
2567 Go_statement::do_get_backend(Translate_context
* context
)
2571 if (!this->get_fn_and_arg(&fn
, &arg
))
2572 return context
->backend()->error_statement();
2574 Expression
* call
= Runtime::make_call(Runtime::GO
, this->location(), 2,
2576 Bexpression
* bcall
= call
->get_backend(context
);
2577 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
2578 return context
->backend()->expression_statement(bfunction
, bcall
);
2581 // Dump the AST representation for go statement.
2584 Go_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2586 ast_dump_context
->print_indent();
2587 ast_dump_context
->ostream() << "go ";
2588 ast_dump_context
->dump_expression(this->call());
2589 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2592 // Make a go statement.
2595 Statement::make_go_statement(Call_expression
* call
, Location location
)
2597 return new Go_statement(call
, location
);
2600 // Class Defer_statement.
2603 Defer_statement::do_get_backend(Translate_context
* context
)
2607 if (!this->get_fn_and_arg(&fn
, &arg
))
2608 return context
->backend()->error_statement();
2610 Location loc
= this->location();
2611 Expression
* ds
= context
->function()->func_value()->defer_stack(loc
);
2613 Expression
* call
= Runtime::make_call(Runtime::DEFERPROC
, loc
, 3,
2615 Bexpression
* bcall
= call
->get_backend(context
);
2616 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
2617 return context
->backend()->expression_statement(bfunction
, bcall
);
2620 // Dump the AST representation for defer statement.
2623 Defer_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2625 ast_dump_context
->print_indent();
2626 ast_dump_context
->ostream() << "defer ";
2627 ast_dump_context
->dump_expression(this->call());
2628 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2631 // Make a defer statement.
2634 Statement::make_defer_statement(Call_expression
* call
,
2637 return new Defer_statement(call
, location
);
2640 // Class Return_statement.
2642 // Traverse assignments. We treat each return value as a top level
2643 // RHS in an expression.
2646 Return_statement::do_traverse_assignments(Traverse_assignments
* tassign
)
2648 Expression_list
* vals
= this->vals_
;
2651 for (Expression_list::iterator p
= vals
->begin();
2654 tassign
->value(&*p
, true, true);
2659 // Lower a return statement. If we are returning a function call
2660 // which returns multiple values which match the current function,
2661 // split up the call's results. If the return statement lists
2662 // explicit values, implement this statement by assigning the values
2663 // to the result variables and change this statement to a naked
2664 // return. This lets panic/recover work correctly.
2667 Return_statement::do_lower(Gogo
*, Named_object
* function
, Block
* enclosing
,
2668 Statement_inserter
*)
2670 if (this->is_lowered_
)
2673 Expression_list
* vals
= this->vals_
;
2675 this->is_lowered_
= true;
2677 Location loc
= this->location();
2679 size_t vals_count
= vals
== NULL
? 0 : vals
->size();
2680 Function::Results
* results
= function
->func_value()->result_variables();
2681 size_t results_count
= results
== NULL
? 0 : results
->size();
2683 if (vals_count
== 0)
2685 if (results_count
> 0 && !function
->func_value()->results_are_named())
2687 this->report_error(_("not enough arguments to return"));
2693 if (results_count
== 0)
2695 this->report_error(_("return with value in function "
2696 "with no return type"));
2700 // If the current function has multiple return values, and we are
2701 // returning a single call expression, split up the call expression.
2702 if (results_count
> 1
2703 && vals
->size() == 1
2704 && vals
->front()->call_expression() != NULL
)
2706 Call_expression
* call
= vals
->front()->call_expression();
2707 call
->set_expected_result_count(results_count
);
2709 vals
= new Expression_list
;
2710 for (size_t i
= 0; i
< results_count
; ++i
)
2711 vals
->push_back(Expression::make_call_result(call
, i
));
2712 vals_count
= results_count
;
2715 if (vals_count
< results_count
)
2717 this->report_error(_("not enough arguments to return"));
2721 if (vals_count
> results_count
)
2723 this->report_error(_("too many values in return statement"));
2727 Block
* b
= new Block(enclosing
, loc
);
2729 Expression_list
* lhs
= new Expression_list();
2730 Expression_list
* rhs
= new Expression_list();
2732 Expression_list::const_iterator pe
= vals
->begin();
2734 for (Function::Results::const_iterator pr
= results
->begin();
2735 pr
!= results
->end();
2738 Named_object
* rv
= *pr
;
2739 Expression
* e
= *pe
;
2741 // Check types now so that we give a good error message. The
2742 // result type is known. We determine the expression type
2745 Type
*rvtype
= rv
->result_var_value()->type();
2746 Type_context
type_context(rvtype
, false);
2747 e
->determine_type(&type_context
);
2750 if (Type::are_assignable(rvtype
, e
->type(), &reason
))
2752 Expression
* ve
= Expression::make_var_reference(rv
, e
->location());
2759 go_error_at(e
->location(),
2760 "incompatible type for return value %d", i
);
2762 go_error_at(e
->location(),
2763 "incompatible type for return value %d (%s)",
2767 go_assert(lhs
->size() == rhs
->size());
2771 else if (lhs
->size() == 1)
2773 b
->add_statement(Statement::make_assignment(lhs
->front(), rhs
->front(),
2779 b
->add_statement(Statement::make_tuple_assignment(lhs
, rhs
, loc
));
2781 b
->add_statement(this);
2785 return Statement::make_block_statement(b
, loc
);
2788 // Convert a return statement to the backend representation.
2791 Return_statement::do_get_backend(Translate_context
* context
)
2793 Location loc
= this->location();
2795 Function
* function
= context
->function()->func_value();
2796 Function::Results
* results
= function
->result_variables();
2797 std::vector
<Bexpression
*> retvals
;
2798 if (results
!= NULL
&& !results
->empty())
2800 retvals
.reserve(results
->size());
2801 for (Function::Results::const_iterator p
= results
->begin();
2802 p
!= results
->end();
2805 Expression
* vr
= Expression::make_var_reference(*p
, loc
);
2806 retvals
.push_back(vr
->get_backend(context
));
2810 return context
->backend()->return_statement(function
->get_decl(),
2814 // Dump the AST representation for a return statement.
2817 Return_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2819 ast_dump_context
->print_indent();
2820 ast_dump_context
->ostream() << "return " ;
2821 ast_dump_context
->dump_expression_list(this->vals_
);
2822 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2825 // Make a return statement.
2828 Statement::make_return_statement(Expression_list
* vals
,
2831 return new Return_statement(vals
, location
);
2834 // Make a statement that returns the result of a call expression.
2837 Statement::make_return_from_call(Call_expression
* call
, Location location
)
2839 size_t rc
= call
->result_count();
2841 return Statement::make_statement(call
, true);
2844 Expression_list
* vals
= new Expression_list();
2846 vals
->push_back(call
);
2849 for (size_t i
= 0; i
< rc
; ++i
)
2850 vals
->push_back(Expression::make_call_result(call
, i
));
2852 return Statement::make_return_statement(vals
, location
);
2856 // A break or continue statement.
2858 class Bc_statement
: public Statement
2861 Bc_statement(bool is_break
, Unnamed_label
* label
, Location location
)
2862 : Statement(STATEMENT_BREAK_OR_CONTINUE
, location
),
2863 label_(label
), is_break_(is_break
)
2868 { return this->is_break_
; }
2872 do_traverse(Traverse
*)
2873 { return TRAVERSE_CONTINUE
; }
2876 do_may_fall_through() const
2880 do_get_backend(Translate_context
* context
)
2881 { return this->label_
->get_goto(context
, this->location()); }
2884 do_dump_statement(Ast_dump_context
*) const;
2887 // The label that this branches to.
2888 Unnamed_label
* label_
;
2889 // True if this is "break", false if it is "continue".
2893 // Dump the AST representation for a break/continue statement
2896 Bc_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2898 ast_dump_context
->print_indent();
2899 ast_dump_context
->ostream() << (this->is_break_
? "break" : "continue");
2900 if (this->label_
!= NULL
)
2902 ast_dump_context
->ostream() << " ";
2903 ast_dump_context
->dump_label_name(this->label_
);
2905 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
2908 // Make a break statement.
2911 Statement::make_break_statement(Unnamed_label
* label
, Location location
)
2913 return new Bc_statement(true, label
, location
);
2916 // Make a continue statement.
2919 Statement::make_continue_statement(Unnamed_label
* label
,
2922 return new Bc_statement(false, label
, location
);
2925 // Class Goto_statement.
2928 Goto_statement::do_traverse(Traverse
*)
2930 return TRAVERSE_CONTINUE
;
2933 // Check types for a label. There aren't any types per se, but we use
2934 // this to give an error if the label was never defined.
2937 Goto_statement::do_check_types(Gogo
*)
2939 if (!this->label_
->is_defined())
2941 go_error_at(this->location(), "reference to undefined label %qs",
2942 Gogo::message_name(this->label_
->name()).c_str());
2943 this->set_is_error();
2947 // Convert the goto statement to the backend representation.
2950 Goto_statement::do_get_backend(Translate_context
* context
)
2952 Blabel
* blabel
= this->label_
->get_backend_label(context
);
2953 return context
->backend()->goto_statement(blabel
, this->location());
2956 // Dump the AST representation for a goto statement.
2959 Goto_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
2961 ast_dump_context
->print_indent();
2962 ast_dump_context
->ostream() << "goto " << this->label_
->name() << dsuffix(location()) << std::endl
;
2965 // Make a goto statement.
2968 Statement::make_goto_statement(Label
* label
, Location location
)
2970 return new Goto_statement(label
, location
);
2973 // Class Goto_unnamed_statement.
2976 Goto_unnamed_statement::do_traverse(Traverse
*)
2978 return TRAVERSE_CONTINUE
;
2981 // Convert the goto unnamed statement to the backend representation.
2984 Goto_unnamed_statement::do_get_backend(Translate_context
* context
)
2986 return this->label_
->get_goto(context
, this->location());
2989 // Dump the AST representation for an unnamed goto statement
2992 Goto_unnamed_statement::do_dump_statement(
2993 Ast_dump_context
* ast_dump_context
) const
2995 ast_dump_context
->print_indent();
2996 ast_dump_context
->ostream() << "goto ";
2997 ast_dump_context
->dump_label_name(this->label_
);
2998 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
3001 // Make a goto statement to an unnamed label.
3004 Statement::make_goto_unnamed_statement(Unnamed_label
* label
,
3007 return new Goto_unnamed_statement(label
, location
);
3010 // Class Label_statement.
3015 Label_statement::do_traverse(Traverse
*)
3017 return TRAVERSE_CONTINUE
;
3020 // Return the backend representation of the statement defining this
3024 Label_statement::do_get_backend(Translate_context
* context
)
3026 if (this->label_
->is_dummy_label())
3028 Bexpression
* bce
= context
->backend()->boolean_constant_expression(false);
3029 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
3030 return context
->backend()->expression_statement(bfunction
, bce
);
3032 Blabel
* blabel
= this->label_
->get_backend_label(context
);
3033 return context
->backend()->label_definition_statement(blabel
);
3036 // Dump the AST for a label definition statement.
3039 Label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3041 ast_dump_context
->print_indent();
3042 ast_dump_context
->ostream() << this->label_
->name() << ":" << dsuffix(location()) << std::endl
;
3045 // Make a label statement.
3048 Statement::make_label_statement(Label
* label
, Location location
)
3050 return new Label_statement(label
, location
);
3053 // Class Unnamed_label_statement.
3055 Unnamed_label_statement::Unnamed_label_statement(Unnamed_label
* label
)
3056 : Statement(STATEMENT_UNNAMED_LABEL
, label
->location()),
3061 Unnamed_label_statement::do_traverse(Traverse
*)
3063 return TRAVERSE_CONTINUE
;
3066 // Get the backend definition for this unnamed label statement.
3069 Unnamed_label_statement::do_get_backend(Translate_context
* context
)
3071 return this->label_
->get_definition(context
);
3074 // Dump the AST representation for an unnamed label definition statement.
3077 Unnamed_label_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3080 ast_dump_context
->print_indent();
3081 ast_dump_context
->dump_label_name(this->label_
);
3082 ast_dump_context
->ostream() << ":" << dsuffix(location()) << std::endl
;
3085 // Make an unnamed label statement.
3088 Statement::make_unnamed_label_statement(Unnamed_label
* label
)
3090 return new Unnamed_label_statement(label
);
3093 // Class If_statement.
3098 If_statement::do_traverse(Traverse
* traverse
)
3100 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
3101 || this->then_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3102 return TRAVERSE_EXIT
;
3103 if (this->else_block_
!= NULL
)
3105 if (this->else_block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3106 return TRAVERSE_EXIT
;
3108 return TRAVERSE_CONTINUE
;
3112 If_statement::do_determine_types()
3114 Type_context
context(Type::lookup_bool_type(), false);
3115 this->cond_
->determine_type(&context
);
3116 this->then_block_
->determine_types();
3117 if (this->else_block_
!= NULL
)
3118 this->else_block_
->determine_types();
3124 If_statement::do_check_types(Gogo
*)
3126 Type
* type
= this->cond_
->type();
3127 if (type
->is_error())
3128 this->set_is_error();
3129 else if (!type
->is_boolean_type())
3130 this->report_error(_("expected boolean expression"));
3133 // Whether the overall statement may fall through.
3136 If_statement::do_may_fall_through() const
3138 return (this->else_block_
== NULL
3139 || this->then_block_
->may_fall_through()
3140 || this->else_block_
->may_fall_through());
3143 // Get the backend representation.
3146 If_statement::do_get_backend(Translate_context
* context
)
3148 go_assert(this->cond_
->type()->is_boolean_type()
3149 || this->cond_
->type()->is_error());
3150 Bexpression
* cond
= this->cond_
->get_backend(context
);
3151 Bblock
* then_block
= this->then_block_
->get_backend(context
);
3152 Bblock
* else_block
= (this->else_block_
== NULL
3154 : this->else_block_
->get_backend(context
));
3155 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
3156 return context
->backend()->if_statement(bfunction
,
3157 cond
, then_block
, else_block
,
3161 // Dump the AST representation for an if statement
3164 If_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3166 ast_dump_context
->print_indent();
3167 ast_dump_context
->ostream() << "if ";
3168 ast_dump_context
->dump_expression(this->cond_
);
3169 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
3170 if (ast_dump_context
->dump_subblocks())
3172 ast_dump_context
->dump_block(this->then_block_
);
3173 if (this->else_block_
!= NULL
)
3175 ast_dump_context
->print_indent();
3176 ast_dump_context
->ostream() << "else" << std::endl
;
3177 ast_dump_context
->dump_block(this->else_block_
);
3182 // Make an if statement.
3185 Statement::make_if_statement(Expression
* cond
, Block
* then_block
,
3186 Block
* else_block
, Location location
)
3188 return new If_statement(cond
, then_block
, else_block
, location
);
3191 // Class Case_clauses::Hash_integer_value.
3193 class Case_clauses::Hash_integer_value
3197 operator()(Expression
*) const;
3201 Case_clauses::Hash_integer_value::operator()(Expression
* pe
) const
3203 Numeric_constant nc
;
3205 if (!pe
->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3207 size_t ret
= mpz_get_ui(ival
);
3212 // Class Case_clauses::Eq_integer_value.
3214 class Case_clauses::Eq_integer_value
3218 operator()(Expression
*, Expression
*) const;
3222 Case_clauses::Eq_integer_value::operator()(Expression
* a
, Expression
* b
) const
3224 Numeric_constant anc
;
3226 Numeric_constant bnc
;
3228 if (!a
->numeric_constant_value(&anc
)
3229 || !anc
.to_int(&aval
)
3230 || !b
->numeric_constant_value(&bnc
)
3231 || !bnc
.to_int(&bval
))
3233 bool ret
= mpz_cmp(aval
, bval
) == 0;
3239 // Class Case_clauses::Case_clause.
3244 Case_clauses::Case_clause::traverse(Traverse
* traverse
)
3246 if (this->cases_
!= NULL
3247 && (traverse
->traverse_mask()
3248 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3250 if (this->cases_
->traverse(traverse
) == TRAVERSE_EXIT
)
3251 return TRAVERSE_EXIT
;
3253 if (this->statements_
!= NULL
)
3255 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
3256 return TRAVERSE_EXIT
;
3258 return TRAVERSE_CONTINUE
;
3261 // Check whether all the case expressions are integer constants.
3264 Case_clauses::Case_clause::is_constant() const
3266 if (this->cases_
!= NULL
)
3268 for (Expression_list::const_iterator p
= this->cases_
->begin();
3269 p
!= this->cases_
->end();
3271 if (!(*p
)->is_constant() || (*p
)->type()->integer_type() == NULL
)
3277 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3278 // value we are switching on; it may be NULL. If START_LABEL is not
3279 // NULL, it goes at the start of the statements, after the condition
3280 // test. We branch to FINISH_LABEL at the end of the statements.
3283 Case_clauses::Case_clause::lower(Block
* b
, Temporary_statement
* val_temp
,
3284 Unnamed_label
* start_label
,
3285 Unnamed_label
* finish_label
) const
3287 Location loc
= this->location_
;
3288 Unnamed_label
* next_case_label
;
3289 if (this->cases_
== NULL
|| this->cases_
->empty())
3291 go_assert(this->is_default_
);
3292 next_case_label
= NULL
;
3296 Expression
* cond
= NULL
;
3298 for (Expression_list::const_iterator p
= this->cases_
->begin();
3299 p
!= this->cases_
->end();
3302 Expression
* ref
= Expression::make_temporary_reference(val_temp
,
3304 Expression
* this_cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3309 cond
= Expression::make_binary(OPERATOR_OROR
, cond
, this_cond
, loc
);
3312 Block
* then_block
= new Block(b
, loc
);
3313 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3314 Statement
* s
= Statement::make_goto_unnamed_statement(next_case_label
,
3316 then_block
->add_statement(s
);
3318 // if !COND { goto NEXT_CASE_LABEL }
3319 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3320 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
3321 b
->add_statement(s
);
3324 if (start_label
!= NULL
)
3325 b
->add_statement(Statement::make_unnamed_label_statement(start_label
));
3327 if (this->statements_
!= NULL
)
3328 b
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
3330 Statement
* s
= Statement::make_goto_unnamed_statement(finish_label
, loc
);
3331 b
->add_statement(s
);
3333 if (next_case_label
!= NULL
)
3334 b
->add_statement(Statement::make_unnamed_label_statement(next_case_label
));
3340 Case_clauses::Case_clause::determine_types(Type
* type
)
3342 if (this->cases_
!= NULL
)
3344 Type_context
case_context(type
, false);
3345 for (Expression_list::iterator p
= this->cases_
->begin();
3346 p
!= this->cases_
->end();
3348 (*p
)->determine_type(&case_context
);
3350 if (this->statements_
!= NULL
)
3351 this->statements_
->determine_types();
3354 // Check types. Returns false if there was an error.
3357 Case_clauses::Case_clause::check_types(Type
* type
)
3359 if (this->cases_
!= NULL
)
3361 for (Expression_list::iterator p
= this->cases_
->begin();
3362 p
!= this->cases_
->end();
3365 if (!Type::are_assignable(type
, (*p
)->type(), NULL
)
3366 && !Type::are_assignable((*p
)->type(), type
, NULL
))
3368 go_error_at((*p
)->location(),
3369 "type mismatch between switch value and case clause");
3377 // Return true if this clause may fall through to the following
3378 // statements. Note that this is not the same as whether the case
3379 // uses the "fallthrough" keyword.
3382 Case_clauses::Case_clause::may_fall_through() const
3384 if (this->statements_
== NULL
)
3386 return this->statements_
->may_fall_through();
3389 // Convert the case values and statements to the backend
3390 // representation. BREAK_LABEL is the label which break statements
3391 // should branch to. CASE_CONSTANTS is used to detect duplicate
3392 // constants. *CASES should be passed as an empty vector; the values
3393 // for this case will be added to it. If this is the default case,
3394 // *CASES will remain empty. This returns the statement to execute if
3395 // one of these cases is selected.
3398 Case_clauses::Case_clause::get_backend(Translate_context
* context
,
3399 Unnamed_label
* break_label
,
3400 Case_constants
* case_constants
,
3401 std::vector
<Bexpression
*>* cases
) const
3403 if (this->cases_
!= NULL
)
3405 go_assert(!this->is_default_
);
3406 for (Expression_list::const_iterator p
= this->cases_
->begin();
3407 p
!= this->cases_
->end();
3411 if (e
->classification() != Expression::EXPRESSION_INTEGER
)
3413 Numeric_constant nc
;
3415 if (!(*p
)->numeric_constant_value(&nc
) || !nc
.to_int(&ival
))
3417 // Something went wrong. This can happen with a
3418 // negative constant and an unsigned switch value.
3419 go_assert(saw_errors());
3422 go_assert(nc
.type() != NULL
);
3423 e
= Expression::make_integer_z(&ival
, nc
.type(), e
->location());
3427 std::pair
<Case_constants::iterator
, bool> ins
=
3428 case_constants
->insert(e
);
3431 // Value was already present.
3432 go_error_at(this->location_
, "duplicate case in switch");
3433 e
= Expression::make_error(this->location_
);
3435 cases
->push_back(e
->get_backend(context
));
3439 Bstatement
* statements
;
3440 if (this->statements_
== NULL
)
3444 Bblock
* bblock
= this->statements_
->get_backend(context
);
3445 statements
= context
->backend()->block_statement(bblock
);
3448 Bstatement
* break_stat
;
3449 if (this->is_fallthrough_
)
3452 break_stat
= break_label
->get_goto(context
, this->location_
);
3454 if (statements
== NULL
)
3456 else if (break_stat
== NULL
)
3459 return context
->backend()->compound_statement(statements
, break_stat
);
3462 // Dump the AST representation for a case clause
3465 Case_clauses::Case_clause::dump_clause(Ast_dump_context
* ast_dump_context
)
3468 ast_dump_context
->print_indent();
3469 if (this->is_default_
)
3471 ast_dump_context
->ostream() << "default:";
3475 ast_dump_context
->ostream() << "case ";
3476 ast_dump_context
->dump_expression_list(this->cases_
);
3477 ast_dump_context
->ostream() << ":" ;
3479 ast_dump_context
->dump_block(this->statements_
);
3480 if (this->is_fallthrough_
)
3482 ast_dump_context
->print_indent();
3483 ast_dump_context
->ostream() << " (fallthrough)" << dsuffix(location()) << std::endl
;
3487 // Class Case_clauses.
3492 Case_clauses::traverse(Traverse
* traverse
)
3494 for (Clauses::iterator p
= this->clauses_
.begin();
3495 p
!= this->clauses_
.end();
3498 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
3499 return TRAVERSE_EXIT
;
3501 return TRAVERSE_CONTINUE
;
3504 // Check whether all the case expressions are constant.
3507 Case_clauses::is_constant() const
3509 for (Clauses::const_iterator p
= this->clauses_
.begin();
3510 p
!= this->clauses_
.end();
3512 if (!p
->is_constant())
3517 // Lower case clauses for a nonconstant switch.
3520 Case_clauses::lower(Block
* b
, Temporary_statement
* val_temp
,
3521 Unnamed_label
* break_label
) const
3523 // The default case.
3524 const Case_clause
* default_case
= NULL
;
3526 // The label for the fallthrough of the previous case.
3527 Unnamed_label
* last_fallthrough_label
= NULL
;
3529 // The label for the start of the default case. This is used if the
3530 // case before the default case falls through.
3531 Unnamed_label
* default_start_label
= NULL
;
3533 // The label for the end of the default case. This normally winds
3534 // up as BREAK_LABEL, but it will be different if the default case
3536 Unnamed_label
* default_finish_label
= NULL
;
3538 for (Clauses::const_iterator p
= this->clauses_
.begin();
3539 p
!= this->clauses_
.end();
3542 // The label to use for the start of the statements for this
3543 // case. This is NULL unless the previous case falls through.
3544 Unnamed_label
* start_label
= last_fallthrough_label
;
3546 // The label to jump to after the end of the statements for this
3548 Unnamed_label
* finish_label
= break_label
;
3550 last_fallthrough_label
= NULL
;
3551 if (p
->is_fallthrough() && p
+ 1 != this->clauses_
.end())
3553 finish_label
= new Unnamed_label(p
->location());
3554 last_fallthrough_label
= finish_label
;
3557 if (!p
->is_default())
3558 p
->lower(b
, val_temp
, start_label
, finish_label
);
3561 // We have to move the default case to the end, so that we
3562 // only use it if all the other tests fail.
3564 default_start_label
= start_label
;
3565 default_finish_label
= finish_label
;
3569 if (default_case
!= NULL
)
3570 default_case
->lower(b
, val_temp
, default_start_label
,
3571 default_finish_label
);
3577 Case_clauses::determine_types(Type
* type
)
3579 for (Clauses::iterator p
= this->clauses_
.begin();
3580 p
!= this->clauses_
.end();
3582 p
->determine_types(type
);
3585 // Check types. Returns false if there was an error.
3588 Case_clauses::check_types(Type
* type
)
3591 for (Clauses::iterator p
= this->clauses_
.begin();
3592 p
!= this->clauses_
.end();
3595 if (!p
->check_types(type
))
3601 // Return true if these clauses may fall through to the statements
3602 // following the switch statement.
3605 Case_clauses::may_fall_through() const
3607 bool found_default
= false;
3608 for (Clauses::const_iterator p
= this->clauses_
.begin();
3609 p
!= this->clauses_
.end();
3612 if (p
->may_fall_through() && !p
->is_fallthrough())
3614 if (p
->is_default())
3615 found_default
= true;
3617 return !found_default
;
3620 // Convert the cases to the backend representation. This sets
3621 // *ALL_CASES and *ALL_STATEMENTS.
3624 Case_clauses::get_backend(Translate_context
* context
,
3625 Unnamed_label
* break_label
,
3626 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
3627 std::vector
<Bstatement
*>* all_statements
) const
3629 Case_constants case_constants
;
3631 size_t c
= this->clauses_
.size();
3632 all_cases
->resize(c
);
3633 all_statements
->resize(c
);
3636 for (Clauses::const_iterator p
= this->clauses_
.begin();
3637 p
!= this->clauses_
.end();
3640 std::vector
<Bexpression
*> cases
;
3641 Bstatement
* stat
= p
->get_backend(context
, break_label
, &case_constants
,
3643 // The final clause can't fall through.
3644 if (i
== c
- 1 && p
->is_fallthrough())
3646 go_assert(saw_errors());
3647 stat
= context
->backend()->error_statement();
3649 (*all_cases
)[i
].swap(cases
);
3650 (*all_statements
)[i
] = stat
;
3654 // Dump the AST representation for case clauses (from a switch statement)
3657 Case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
3659 for (Clauses::const_iterator p
= this->clauses_
.begin();
3660 p
!= this->clauses_
.end();
3662 p
->dump_clause(ast_dump_context
);
3665 // A constant switch statement. A Switch_statement is lowered to this
3666 // when all the cases are constants.
3668 class Constant_switch_statement
: public Statement
3671 Constant_switch_statement(Expression
* val
, Case_clauses
* clauses
,
3672 Unnamed_label
* break_label
,
3674 : Statement(STATEMENT_CONSTANT_SWITCH
, location
),
3675 val_(val
), clauses_(clauses
), break_label_(break_label
)
3680 do_traverse(Traverse
*);
3683 do_determine_types();
3686 do_check_types(Gogo
*);
3689 do_get_backend(Translate_context
*);
3692 do_dump_statement(Ast_dump_context
*) const;
3695 // The value to switch on.
3697 // The case clauses.
3698 Case_clauses
* clauses_
;
3699 // The break label, if needed.
3700 Unnamed_label
* break_label_
;
3706 Constant_switch_statement::do_traverse(Traverse
* traverse
)
3708 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3709 return TRAVERSE_EXIT
;
3710 return this->clauses_
->traverse(traverse
);
3716 Constant_switch_statement::do_determine_types()
3718 this->val_
->determine_type_no_context();
3719 this->clauses_
->determine_types(this->val_
->type());
3725 Constant_switch_statement::do_check_types(Gogo
*)
3727 if (!this->clauses_
->check_types(this->val_
->type()))
3728 this->set_is_error();
3731 // Convert to GENERIC.
3734 Constant_switch_statement::do_get_backend(Translate_context
* context
)
3736 Bexpression
* switch_val_expr
= this->val_
->get_backend(context
);
3738 Unnamed_label
* break_label
= this->break_label_
;
3739 if (break_label
== NULL
)
3740 break_label
= new Unnamed_label(this->location());
3742 std::vector
<std::vector
<Bexpression
*> > all_cases
;
3743 std::vector
<Bstatement
*> all_statements
;
3744 this->clauses_
->get_backend(context
, break_label
, &all_cases
,
3747 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
3748 Bstatement
* switch_statement
;
3749 switch_statement
= context
->backend()->switch_statement(bfunction
,
3754 Bstatement
* ldef
= break_label
->get_definition(context
);
3755 return context
->backend()->compound_statement(switch_statement
, ldef
);
3758 // Dump the AST representation for a constant switch statement.
3761 Constant_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
3764 ast_dump_context
->print_indent();
3765 ast_dump_context
->ostream() << "switch ";
3766 ast_dump_context
->dump_expression(this->val_
);
3768 if (ast_dump_context
->dump_subblocks())
3770 ast_dump_context
->ostream() << " {" << std::endl
;
3771 this->clauses_
->dump_clauses(ast_dump_context
);
3772 ast_dump_context
->ostream() << "}";
3775 ast_dump_context
->ostream() << std::endl
;
3778 // Class Switch_statement.
3783 Switch_statement::do_traverse(Traverse
* traverse
)
3785 if (this->val_
!= NULL
)
3787 if (this->traverse_expression(traverse
, &this->val_
) == TRAVERSE_EXIT
)
3788 return TRAVERSE_EXIT
;
3790 return this->clauses_
->traverse(traverse
);
3793 // Lower a Switch_statement to a Constant_switch_statement or a series
3794 // of if statements.
3797 Switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
3798 Statement_inserter
*)
3800 Location loc
= this->location();
3802 if (this->val_
!= NULL
3803 && (this->val_
->is_error_expression()
3804 || this->val_
->type()->is_error()))
3806 go_assert(saw_errors());
3807 return Statement::make_error_statement(loc
);
3810 if (this->val_
!= NULL
3811 && this->val_
->type()->integer_type() != NULL
3812 && !this->clauses_
->empty()
3813 && this->clauses_
->is_constant())
3814 return new Constant_switch_statement(this->val_
, this->clauses_
,
3815 this->break_label_
, loc
);
3817 if (this->val_
!= NULL
3818 && !this->val_
->type()->is_comparable()
3819 && !Type::are_compatible_for_comparison(true, this->val_
->type(),
3820 Type::make_nil_type(), NULL
))
3822 go_error_at(this->val_
->location(),
3823 "cannot switch on value whose type that may not be compared");
3824 return Statement::make_error_statement(loc
);
3827 Block
* b
= new Block(enclosing
, loc
);
3829 if (this->clauses_
->empty())
3831 Expression
* val
= this->val_
;
3833 val
= Expression::make_boolean(true, loc
);
3834 return Statement::make_statement(val
, true);
3837 // var val_temp VAL_TYPE = VAL
3838 Expression
* val
= this->val_
;
3840 val
= Expression::make_boolean(true, loc
);
3842 Type
* type
= val
->type();
3843 if (type
->is_abstract())
3844 type
= type
->make_non_abstract_type();
3845 Temporary_statement
* val_temp
= Statement::make_temporary(type
, val
, loc
);
3846 b
->add_statement(val_temp
);
3848 this->clauses_
->lower(b
, val_temp
, this->break_label());
3850 Statement
* s
= Statement::make_unnamed_label_statement(this->break_label_
);
3851 b
->add_statement(s
);
3853 return Statement::make_block_statement(b
, loc
);
3856 // Return the break label for this switch statement, creating it if
3860 Switch_statement::break_label()
3862 if (this->break_label_
== NULL
)
3863 this->break_label_
= new Unnamed_label(this->location());
3864 return this->break_label_
;
3867 // Dump the AST representation for a switch statement.
3870 Switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
3872 ast_dump_context
->print_indent();
3873 ast_dump_context
->ostream() << "switch ";
3874 if (this->val_
!= NULL
)
3876 ast_dump_context
->dump_expression(this->val_
);
3878 if (ast_dump_context
->dump_subblocks())
3880 ast_dump_context
->ostream() << " {" << dsuffix(location()) << std::endl
;
3881 this->clauses_
->dump_clauses(ast_dump_context
);
3882 ast_dump_context
->print_indent();
3883 ast_dump_context
->ostream() << "}";
3885 ast_dump_context
->ostream() << std::endl
;
3888 // Return whether this switch may fall through.
3891 Switch_statement::do_may_fall_through() const
3893 if (this->clauses_
== NULL
)
3896 // If we have a break label, then some case needed it. That implies
3897 // that the switch statement as a whole can fall through.
3898 if (this->break_label_
!= NULL
)
3901 return this->clauses_
->may_fall_through();
3904 // Make a switch statement.
3907 Statement::make_switch_statement(Expression
* val
, Location location
)
3909 return new Switch_statement(val
, location
);
3912 // Class Type_case_clauses::Type_case_clause.
3917 Type_case_clauses::Type_case_clause::traverse(Traverse
* traverse
)
3919 if (!this->is_default_
3920 && ((traverse
->traverse_mask()
3921 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
3922 && Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3923 return TRAVERSE_EXIT
;
3924 if (this->statements_
!= NULL
)
3925 return this->statements_
->traverse(traverse
);
3926 return TRAVERSE_CONTINUE
;
3929 // Lower one clause in a type switch. Add statements to the block B.
3930 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3931 // BREAK_LABEL is the label at the end of the type switch.
3932 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3936 Type_case_clauses::Type_case_clause::lower(Type
* switch_val_type
,
3938 Temporary_statement
* descriptor_temp
,
3939 Unnamed_label
* break_label
,
3940 Unnamed_label
** stmts_label
) const
3942 Location loc
= this->location_
;
3944 Unnamed_label
* next_case_label
= NULL
;
3945 if (!this->is_default_
)
3947 Type
* type
= this->type_
;
3950 if (switch_val_type
->interface_type() != NULL
3951 && !type
->is_nil_constant_as_type()
3952 && type
->interface_type() == NULL
3953 && !switch_val_type
->interface_type()->implements_interface(type
,
3957 go_error_at(this->location_
, "impossible type switch case");
3959 go_error_at(this->location_
, "impossible type switch case (%s)",
3963 Expression
* ref
= Expression::make_temporary_reference(descriptor_temp
,
3967 // The language permits case nil, which is of course a constant
3968 // rather than a type. It will appear here as an invalid
3970 if (type
->is_nil_constant_as_type())
3971 cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
,
3972 Expression::make_nil(loc
),
3975 cond
= Runtime::make_call((type
->interface_type() == NULL
3976 ? Runtime::IFACETYPEEQ
3977 : Runtime::IFACET2IP
),
3979 Expression::make_type_descriptor(type
, loc
),
3982 Unnamed_label
* dest
;
3983 if (!this->is_fallthrough_
)
3985 // if !COND { goto NEXT_CASE_LABEL }
3986 next_case_label
= new Unnamed_label(Linemap::unknown_location());
3987 dest
= next_case_label
;
3988 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
3992 // if COND { goto STMTS_LABEL }
3993 go_assert(stmts_label
!= NULL
);
3994 if (*stmts_label
== NULL
)
3995 *stmts_label
= new Unnamed_label(Linemap::unknown_location());
3996 dest
= *stmts_label
;
3998 Block
* then_block
= new Block(b
, loc
);
3999 Statement
* s
= Statement::make_goto_unnamed_statement(dest
, loc
);
4000 then_block
->add_statement(s
);
4001 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
4002 b
->add_statement(s
);
4005 if (this->statements_
!= NULL
4006 || (!this->is_fallthrough_
4007 && stmts_label
!= NULL
4008 && *stmts_label
!= NULL
))
4010 go_assert(!this->is_fallthrough_
);
4011 if (stmts_label
!= NULL
&& *stmts_label
!= NULL
)
4013 go_assert(!this->is_default_
);
4014 if (this->statements_
!= NULL
)
4015 (*stmts_label
)->set_location(this->statements_
->start_location());
4016 Statement
* s
= Statement::make_unnamed_label_statement(*stmts_label
);
4017 b
->add_statement(s
);
4018 *stmts_label
= NULL
;
4020 if (this->statements_
!= NULL
)
4021 b
->add_statement(Statement::make_block_statement(this->statements_
,
4025 if (this->is_fallthrough_
)
4026 go_assert(next_case_label
== NULL
);
4029 Location gloc
= (this->statements_
== NULL
4031 : this->statements_
->end_location());
4032 b
->add_statement(Statement::make_goto_unnamed_statement(break_label
,
4034 if (next_case_label
!= NULL
)
4037 Statement::make_unnamed_label_statement(next_case_label
);
4038 b
->add_statement(s
);
4043 // Return true if this type clause may fall through to the statements
4044 // following the switch.
4047 Type_case_clauses::Type_case_clause::may_fall_through() const
4049 if (this->is_fallthrough_
)
4051 // This case means that we automatically fall through to the
4052 // next case (it's used for T1 in case T1, T2:). It does not
4053 // mean that we fall through to the end of the type switch as a
4054 // whole. There is sure to be a next case and that next case
4055 // will determine whether we fall through to the statements
4056 // after the type switch.
4059 if (this->statements_
== NULL
)
4061 return this->statements_
->may_fall_through();
4064 // Dump the AST representation for a type case clause
4067 Type_case_clauses::Type_case_clause::dump_clause(
4068 Ast_dump_context
* ast_dump_context
) const
4070 ast_dump_context
->print_indent();
4071 if (this->is_default_
)
4073 ast_dump_context
->ostream() << "default:";
4077 ast_dump_context
->ostream() << "case ";
4078 ast_dump_context
->dump_type(this->type_
);
4079 ast_dump_context
->ostream() << ":" ;
4081 ast_dump_context
->dump_block(this->statements_
);
4082 if (this->is_fallthrough_
)
4084 ast_dump_context
->print_indent();
4085 ast_dump_context
->ostream() << " (fallthrough)" << std::endl
;
4089 // Class Type_case_clauses.
4094 Type_case_clauses::traverse(Traverse
* traverse
)
4096 for (Type_clauses::iterator p
= this->clauses_
.begin();
4097 p
!= this->clauses_
.end();
4100 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4101 return TRAVERSE_EXIT
;
4103 return TRAVERSE_CONTINUE
;
4106 // Check for duplicate types.
4109 Type_case_clauses::check_duplicates() const
4111 typedef Unordered_set_hash(const Type
*, Type_hash_identical
,
4112 Type_identical
) Types_seen
;
4113 Types_seen types_seen
;
4114 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4115 p
!= this->clauses_
.end();
4118 Type
* t
= p
->type();
4121 if (t
->is_nil_constant_as_type())
4122 t
= Type::make_nil_type();
4123 std::pair
<Types_seen::iterator
, bool> ins
= types_seen
.insert(t
);
4125 go_error_at(p
->location(), "duplicate type in switch");
4129 // Lower the clauses in a type switch. Add statements to the block B.
4130 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4131 // BREAK_LABEL is the label at the end of the type switch.
4134 Type_case_clauses::lower(Type
* switch_val_type
, Block
* b
,
4135 Temporary_statement
* descriptor_temp
,
4136 Unnamed_label
* break_label
) const
4138 const Type_case_clause
* default_case
= NULL
;
4140 Unnamed_label
* stmts_label
= NULL
;
4141 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4142 p
!= this->clauses_
.end();
4145 if (!p
->is_default())
4146 p
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4150 // We are generating a series of tests, which means that we
4151 // need to move the default case to the end.
4155 go_assert(stmts_label
== NULL
);
4157 if (default_case
!= NULL
)
4158 default_case
->lower(switch_val_type
, b
, descriptor_temp
, break_label
,
4162 // Return true if these clauses may fall through to the statements
4163 // following the switch statement.
4166 Type_case_clauses::may_fall_through() const
4168 bool found_default
= false;
4169 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4170 p
!= this->clauses_
.end();
4173 if (p
->may_fall_through())
4175 if (p
->is_default())
4176 found_default
= true;
4178 return !found_default
;
4181 // Dump the AST representation for case clauses (from a switch statement)
4184 Type_case_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4186 for (Type_clauses::const_iterator p
= this->clauses_
.begin();
4187 p
!= this->clauses_
.end();
4189 p
->dump_clause(ast_dump_context
);
4192 // Class Type_switch_statement.
4197 Type_switch_statement::do_traverse(Traverse
* traverse
)
4199 if (this->traverse_expression(traverse
, &this->expr_
) == TRAVERSE_EXIT
)
4200 return TRAVERSE_EXIT
;
4201 if (this->clauses_
!= NULL
)
4202 return this->clauses_
->traverse(traverse
);
4203 return TRAVERSE_CONTINUE
;
4206 // Lower a type switch statement to a series of if statements. The gc
4207 // compiler is able to generate a table in some cases. However, that
4208 // does not work for us because we may have type descriptors in
4209 // different shared libraries, so we can't compare them with simple
4210 // equality testing.
4213 Type_switch_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
4214 Statement_inserter
*)
4216 const Location loc
= this->location();
4218 if (this->clauses_
!= NULL
)
4219 this->clauses_
->check_duplicates();
4221 Block
* b
= new Block(enclosing
, loc
);
4223 Type
* val_type
= this->expr_
->type();
4224 if (val_type
->interface_type() == NULL
)
4226 if (!val_type
->is_error())
4227 this->report_error(_("cannot type switch on non-interface value"));
4228 return Statement::make_error_statement(loc
);
4231 // var descriptor_temp DESCRIPTOR_TYPE
4232 Type
* descriptor_type
= Type::make_type_descriptor_ptr_type();
4233 Temporary_statement
* descriptor_temp
=
4234 Statement::make_temporary(descriptor_type
, NULL
, loc
);
4235 b
->add_statement(descriptor_temp
);
4237 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4239 bool is_empty
= val_type
->interface_type()->is_empty();
4240 Expression
* call
= Runtime::make_call((is_empty
4241 ? Runtime::EFACETYPE
4242 : Runtime::IFACETYPE
),
4243 loc
, 1, this->expr_
);
4244 Temporary_reference_expression
* lhs
=
4245 Expression::make_temporary_reference(descriptor_temp
, loc
);
4246 lhs
->set_is_lvalue();
4247 Statement
* s
= Statement::make_assignment(lhs
, call
, loc
);
4248 b
->add_statement(s
);
4250 if (this->clauses_
!= NULL
)
4251 this->clauses_
->lower(val_type
, b
, descriptor_temp
, this->break_label());
4253 s
= Statement::make_unnamed_label_statement(this->break_label_
);
4254 b
->add_statement(s
);
4256 return Statement::make_block_statement(b
, loc
);
4259 // Return whether this switch may fall through.
4262 Type_switch_statement::do_may_fall_through() const
4264 if (this->clauses_
== NULL
)
4267 // If we have a break label, then some case needed it. That implies
4268 // that the switch statement as a whole can fall through.
4269 if (this->break_label_
!= NULL
)
4272 return this->clauses_
->may_fall_through();
4275 // Return the break label for this type switch statement, creating it
4279 Type_switch_statement::break_label()
4281 if (this->break_label_
== NULL
)
4282 this->break_label_
= new Unnamed_label(this->location());
4283 return this->break_label_
;
4286 // Dump the AST representation for a type switch statement
4289 Type_switch_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
)
4292 ast_dump_context
->print_indent();
4293 ast_dump_context
->ostream() << "switch ";
4294 if (!this->name_
.empty())
4295 ast_dump_context
->ostream() << this->name_
<< " = ";
4296 ast_dump_context
->dump_expression(this->expr_
);
4297 ast_dump_context
->ostream() << " .(type)";
4298 if (ast_dump_context
->dump_subblocks())
4300 ast_dump_context
->ostream() << " {" << dsuffix(location()) << std::endl
;
4301 this->clauses_
->dump_clauses(ast_dump_context
);
4302 ast_dump_context
->ostream() << "}";
4304 ast_dump_context
->ostream() << std::endl
;
4307 // Make a type switch statement.
4309 Type_switch_statement
*
4310 Statement::make_type_switch_statement(const std::string
& name
, Expression
* expr
,
4313 return new Type_switch_statement(name
, expr
, location
);
4316 // Class Send_statement.
4321 Send_statement::do_traverse(Traverse
* traverse
)
4323 if (this->traverse_expression(traverse
, &this->channel_
) == TRAVERSE_EXIT
)
4324 return TRAVERSE_EXIT
;
4325 return this->traverse_expression(traverse
, &this->val_
);
4331 Send_statement::do_determine_types()
4333 this->channel_
->determine_type_no_context();
4334 Type
* type
= this->channel_
->type();
4335 Type_context context
;
4336 if (type
->channel_type() != NULL
)
4337 context
.type
= type
->channel_type()->element_type();
4338 this->val_
->determine_type(&context
);
4344 Send_statement::do_check_types(Gogo
*)
4346 Type
* type
= this->channel_
->type();
4347 if (type
->is_error())
4349 this->set_is_error();
4352 Channel_type
* channel_type
= type
->channel_type();
4353 if (channel_type
== NULL
)
4355 go_error_at(this->location(), "left operand of %<<-%> must be channel");
4356 this->set_is_error();
4359 Type
* element_type
= channel_type
->element_type();
4360 if (!Type::are_assignable(element_type
, this->val_
->type(), NULL
))
4362 this->report_error(_("incompatible types in send"));
4365 if (!channel_type
->may_send())
4367 this->report_error(_("invalid send on receive-only channel"));
4372 // Flatten a send statement. We may need a temporary for interface
4376 Send_statement::do_flatten(Gogo
*, Named_object
*, Block
*,
4377 Statement_inserter
* inserter
)
4379 if (this->channel_
->is_error_expression()
4380 || this->channel_
->type()->is_error_type())
4382 go_assert(saw_errors());
4383 return Statement::make_error_statement(this->location());
4386 Type
* element_type
= this->channel_
->type()->channel_type()->element_type();
4387 if (!Type::are_identical(element_type
, this->val_
->type(), false, NULL
)
4388 && this->val_
->type()->interface_type() != NULL
4389 && !this->val_
->is_variable())
4391 Temporary_statement
* temp
=
4392 Statement::make_temporary(NULL
, this->val_
, this->location());
4393 inserter
->insert(temp
);
4394 this->val_
= Expression::make_temporary_reference(temp
,
4400 // Convert a send statement to the backend representation.
4403 Send_statement::do_get_backend(Translate_context
* context
)
4405 Location loc
= this->location();
4407 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
4408 Type
* element_type
= channel_type
->element_type();
4409 Expression
* val
= Expression::convert_for_assignment(context
->gogo(),
4413 bool can_take_address
;
4414 switch (element_type
->base()->classification())
4416 case Type::TYPE_BOOLEAN
:
4417 case Type::TYPE_INTEGER
:
4418 case Type::TYPE_FUNCTION
:
4419 case Type::TYPE_POINTER
:
4420 case Type::TYPE_MAP
:
4421 case Type::TYPE_CHANNEL
:
4422 case Type::TYPE_FLOAT
:
4423 case Type::TYPE_COMPLEX
:
4424 case Type::TYPE_STRING
:
4425 case Type::TYPE_INTERFACE
:
4426 can_take_address
= false;
4429 case Type::TYPE_STRUCT
:
4430 can_take_address
= true;
4433 case Type::TYPE_ARRAY
:
4434 can_take_address
= !element_type
->is_slice_type();
4438 case Type::TYPE_ERROR
:
4439 case Type::TYPE_VOID
:
4440 case Type::TYPE_SINK
:
4441 case Type::TYPE_NIL
:
4442 case Type::TYPE_NAMED
:
4443 case Type::TYPE_FORWARD
:
4444 go_assert(saw_errors());
4445 return context
->backend()->error_statement();
4448 // Only try to take the address of a variable. We have already
4449 // moved variables to the heap, so this should not cause that to
4450 // happen unnecessarily.
4451 if (can_take_address
4452 && val
->var_expression() == NULL
4453 && val
->temporary_reference_expression() == NULL
)
4454 can_take_address
= false;
4456 Bstatement
* btemp
= NULL
;
4457 if (can_take_address
)
4459 // The function doesn't change the value, so just take its
4460 // address directly.
4461 val
= Expression::make_unary(OPERATOR_AND
, val
, loc
);
4465 // The value is not in a variable, or is small enough that it
4466 // might be in a register, and taking the address would push it
4467 // on the stack. Copy it into a temporary variable to take the
4469 Temporary_statement
* temp
= Statement::make_temporary(element_type
,
4471 Expression
* ref
= Expression::make_temporary_reference(temp
, loc
);
4472 val
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
4473 btemp
= temp
->get_backend(context
);
4476 Expression
* call
= Runtime::make_call(Runtime::CHANSEND
, loc
, 2,
4477 this->channel_
, val
);
4479 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4480 Bexpression
* bcall
= call
->get_backend(context
);
4481 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
4482 Bstatement
* s
= context
->backend()->expression_statement(bfunction
, bcall
);
4487 return context
->backend()->compound_statement(btemp
, s
);
4490 // Dump the AST representation for a send statement
4493 Send_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
4495 ast_dump_context
->print_indent();
4496 ast_dump_context
->dump_expression(this->channel_
);
4497 ast_dump_context
->ostream() << " <- ";
4498 ast_dump_context
->dump_expression(this->val_
);
4499 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
4502 // Make a send statement.
4505 Statement::make_send_statement(Expression
* channel
, Expression
* val
,
4508 return new Send_statement(channel
, val
, location
);
4511 // Class Select_clauses::Select_clause.
4516 Select_clauses::Select_clause::traverse(Traverse
* traverse
)
4518 if (!this->is_lowered_
4519 && (traverse
->traverse_mask()
4520 & (Traverse::traverse_types
| Traverse::traverse_expressions
)) != 0)
4522 if (this->channel_
!= NULL
)
4524 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
4525 return TRAVERSE_EXIT
;
4527 if (this->val_
!= NULL
)
4529 if (Expression::traverse(&this->val_
, traverse
) == TRAVERSE_EXIT
)
4530 return TRAVERSE_EXIT
;
4532 if (this->closed_
!= NULL
)
4534 if (Expression::traverse(&this->closed_
, traverse
) == TRAVERSE_EXIT
)
4535 return TRAVERSE_EXIT
;
4538 if (this->statements_
!= NULL
)
4540 if (this->statements_
->traverse(traverse
) == TRAVERSE_EXIT
)
4541 return TRAVERSE_EXIT
;
4543 return TRAVERSE_CONTINUE
;
4546 // Lowering. We call a function to register this clause, and arrange
4547 // to set any variables in any receive clause.
4550 Select_clauses::Select_clause::lower(Gogo
* gogo
, Named_object
* function
,
4551 Block
* b
, Temporary_statement
* sel
)
4553 Location loc
= this->location_
;
4555 Expression
* selref
= Expression::make_temporary_reference(sel
, loc
);
4556 selref
= Expression::make_unary(OPERATOR_AND
, selref
, loc
);
4558 if (this->is_default_
)
4560 go_assert(this->channel_
== NULL
&& this->val_
== NULL
);
4561 this->lower_default(b
, selref
);
4562 this->is_lowered_
= true;
4566 // Evaluate the channel before the select statement.
4567 Temporary_statement
* channel_temp
= Statement::make_temporary(NULL
,
4570 b
->add_statement(channel_temp
);
4571 Expression
* chanref
= Expression::make_temporary_reference(channel_temp
,
4575 this->lower_send(b
, selref
, chanref
);
4577 this->lower_recv(gogo
, function
, b
, selref
, chanref
);
4579 // Now all references should be handled through the statements, not
4581 this->is_lowered_
= true;
4585 // Lower a default clause in a select statement.
4588 Select_clauses::Select_clause::lower_default(Block
* b
, Expression
* selref
)
4590 Location loc
= this->location_
;
4591 Expression
* call
= Runtime::make_call(Runtime::SELECTDEFAULT
, loc
, 1,
4593 b
->add_statement(Statement::make_statement(call
, true));
4596 // Lower a send clause in a select statement.
4599 Select_clauses::Select_clause::lower_send(Block
* b
, Expression
* selref
,
4600 Expression
* chanref
)
4602 Location loc
= this->location_
;
4604 Channel_type
* ct
= this->channel_
->type()->channel_type();
4608 Type
* valtype
= ct
->element_type();
4610 // Note that copying the value to a temporary here means that we
4611 // evaluate the send values in the required order.
4612 Temporary_statement
* val
= Statement::make_temporary(valtype
, this->val_
,
4614 b
->add_statement(val
);
4616 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4617 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4619 Expression
* call
= Runtime::make_call(Runtime::SELECTSEND
, loc
, 3, selref
,
4621 b
->add_statement(Statement::make_statement(call
, true));
4624 // Lower a receive clause in a select statement.
4627 Select_clauses::Select_clause::lower_recv(Gogo
* gogo
, Named_object
* function
,
4628 Block
* b
, Expression
* selref
,
4629 Expression
* chanref
)
4631 Location loc
= this->location_
;
4633 Channel_type
* ct
= this->channel_
->type()->channel_type();
4637 Type
* valtype
= ct
->element_type();
4638 Temporary_statement
* val
= Statement::make_temporary(valtype
, NULL
, loc
);
4639 b
->add_statement(val
);
4641 Expression
* valref
= Expression::make_temporary_reference(val
, loc
);
4642 Expression
* valaddr
= Expression::make_unary(OPERATOR_AND
, valref
, loc
);
4644 Temporary_statement
* closed_temp
= NULL
;
4647 if (this->closed_
== NULL
&& this->closedvar_
== NULL
)
4648 caddr
= Expression::make_nil(loc
);
4651 closed_temp
= Statement::make_temporary(Type::lookup_bool_type(), NULL
,
4653 b
->add_statement(closed_temp
);
4654 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4656 caddr
= Expression::make_unary(OPERATOR_AND
, cref
, loc
);
4659 Expression
* call
= Runtime::make_call(Runtime::SELECTRECV
, loc
, 4, selref
,
4660 chanref
, valaddr
, caddr
);
4662 b
->add_statement(Statement::make_statement(call
, true));
4664 // If the block of statements is executed, arrange for the received
4665 // value to move from VAL to the place where the statements expect
4670 if (this->var_
!= NULL
)
4672 go_assert(this->val_
== NULL
);
4673 valref
= Expression::make_temporary_reference(val
, loc
);
4674 this->var_
->var_value()->set_init(valref
);
4675 this->var_
->var_value()->clear_type_from_chan_element();
4677 else if (this->val_
!= NULL
&& !this->val_
->is_sink_expression())
4679 init
= new Block(b
, loc
);
4680 valref
= Expression::make_temporary_reference(val
, loc
);
4681 init
->add_statement(Statement::make_assignment(this->val_
, valref
, loc
));
4684 if (this->closedvar_
!= NULL
)
4686 go_assert(this->closed_
== NULL
);
4687 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4689 this->closedvar_
->var_value()->set_init(cref
);
4691 else if (this->closed_
!= NULL
&& !this->closed_
->is_sink_expression())
4694 init
= new Block(b
, loc
);
4695 Expression
* cref
= Expression::make_temporary_reference(closed_temp
,
4697 init
->add_statement(Statement::make_assignment(this->closed_
, cref
,
4703 gogo
->lower_block(function
, init
);
4705 if (this->statements_
!= NULL
)
4706 init
->add_statement(Statement::make_block_statement(this->statements_
,
4708 this->statements_
= init
;
4715 Select_clauses::Select_clause::determine_types()
4717 go_assert(this->is_lowered_
);
4718 if (this->statements_
!= NULL
)
4719 this->statements_
->determine_types();
4725 Select_clauses::Select_clause::check_types()
4727 if (this->is_default_
)
4730 Channel_type
* ct
= this->channel_
->type()->channel_type();
4733 go_error_at(this->channel_
->location(), "expected channel");
4737 if (this->is_send_
&& !ct
->may_send())
4738 go_error_at(this->location(), "invalid send on receive-only channel");
4739 else if (!this->is_send_
&& !ct
->may_receive())
4740 go_error_at(this->location(), "invalid receive on send-only channel");
4743 // Whether this clause may fall through to the statement which follows
4744 // the overall select statement.
4747 Select_clauses::Select_clause::may_fall_through() const
4749 if (this->statements_
== NULL
)
4751 return this->statements_
->may_fall_through();
4754 // Return the backend representation for the statements to execute.
4757 Select_clauses::Select_clause::get_statements_backend(
4758 Translate_context
* context
)
4760 if (this->statements_
== NULL
)
4762 Bblock
* bblock
= this->statements_
->get_backend(context
);
4763 return context
->backend()->block_statement(bblock
);
4766 // Dump the AST representation for a select case clause
4769 Select_clauses::Select_clause::dump_clause(
4770 Ast_dump_context
* ast_dump_context
) const
4772 ast_dump_context
->print_indent();
4773 if (this->is_default_
)
4775 ast_dump_context
->ostream() << "default:";
4779 ast_dump_context
->ostream() << "case " ;
4782 ast_dump_context
->dump_expression(this->channel_
);
4783 ast_dump_context
->ostream() << " <- " ;
4784 if (this->val_
!= NULL
)
4785 ast_dump_context
->dump_expression(this->val_
);
4789 if (this->val_
!= NULL
)
4790 ast_dump_context
->dump_expression(this->val_
);
4791 if (this->closed_
!= NULL
)
4793 // FIXME: can val_ == NULL and closed_ ! = NULL?
4794 ast_dump_context
->ostream() << " , " ;
4795 ast_dump_context
->dump_expression(this->closed_
);
4797 if (this->closedvar_
!= NULL
|| this->var_
!= NULL
)
4798 ast_dump_context
->ostream() << " := " ;
4800 ast_dump_context
->ostream() << " <- " ;
4801 ast_dump_context
->dump_expression(this->channel_
);
4803 ast_dump_context
->ostream() << ":" ;
4805 ast_dump_context
->dump_block(this->statements_
);
4808 // Class Select_clauses.
4813 Select_clauses::traverse(Traverse
* traverse
)
4815 for (Clauses::iterator p
= this->clauses_
.begin();
4816 p
!= this->clauses_
.end();
4819 if (p
->traverse(traverse
) == TRAVERSE_EXIT
)
4820 return TRAVERSE_EXIT
;
4822 return TRAVERSE_CONTINUE
;
4825 // Lowering. Here we pull out the channel and the send values, to
4826 // enforce the order of evaluation. We also add explicit send and
4827 // receive statements to the clauses.
4830 Select_clauses::lower(Gogo
* gogo
, Named_object
* function
, Block
* b
,
4831 Temporary_statement
* sel
)
4833 for (Clauses::iterator p
= this->clauses_
.begin();
4834 p
!= this->clauses_
.end();
4836 p
->lower(gogo
, function
, b
, sel
);
4842 Select_clauses::determine_types()
4844 for (Clauses::iterator p
= this->clauses_
.begin();
4845 p
!= this->clauses_
.end();
4847 p
->determine_types();
4853 Select_clauses::check_types()
4855 for (Clauses::iterator p
= this->clauses_
.begin();
4856 p
!= this->clauses_
.end();
4861 // Return whether these select clauses fall through to the statement
4862 // following the overall select statement.
4865 Select_clauses::may_fall_through() const
4867 for (Clauses::const_iterator p
= this->clauses_
.begin();
4868 p
!= this->clauses_
.end();
4870 if (p
->may_fall_through())
4875 // Convert to the backend representation. We have already accumulated
4876 // all the select information. Now we call selectgo, which will
4877 // return the index of the clause to execute.
4880 Select_clauses::get_backend(Translate_context
* context
,
4881 Temporary_statement
* sel
,
4882 Unnamed_label
*break_label
,
4885 size_t count
= this->clauses_
.size();
4886 std::vector
<std::vector
<Bexpression
*> > cases(count
+ 1);
4887 std::vector
<Bstatement
*> clauses(count
+ 1);
4889 Type
* int_type
= Type::lookup_integer_type("int");
4892 for (Clauses::iterator p
= this->clauses_
.begin();
4893 p
!= this->clauses_
.end();
4896 Expression
* index_expr
= Expression::make_integer_ul(i
, int_type
,
4898 cases
[i
].push_back(index_expr
->get_backend(context
));
4900 Bstatement
* s
= p
->get_statements_backend(context
);
4901 Location gloc
= (p
->statements() == NULL
4903 : p
->statements()->end_location());
4904 Bstatement
* g
= break_label
->get_goto(context
, gloc
);
4909 clauses
[i
] = context
->backend()->compound_statement(s
, g
);
4912 Expression
* selref
= Expression::make_temporary_reference(sel
, location
);
4913 selref
= Expression::make_unary(OPERATOR_AND
, selref
, location
);
4914 Expression
* call
= Runtime::make_call(Runtime::SELECTGO
, location
, 1,
4916 context
->gogo()->lower_expression(context
->function(), NULL
, &call
);
4917 Bexpression
* bcall
= call
->get_backend(context
);
4921 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
4922 return context
->backend()->expression_statement(bfunction
, bcall
);
4925 Bfunction
* bfunction
= context
->function()->func_value()->get_decl();
4927 Expression
* crash
= Runtime::make_call(Runtime::UNREACHABLE
, location
, 0);
4928 Bexpression
* bcrash
= crash
->get_backend(context
);
4929 clauses
[count
] = context
->backend()->expression_statement(bfunction
, bcrash
);
4931 std::vector
<Bstatement
*> statements
;
4932 statements
.reserve(2);
4934 Bstatement
* switch_stmt
= context
->backend()->switch_statement(bfunction
,
4939 statements
.push_back(switch_stmt
);
4941 Bstatement
* ldef
= break_label
->get_definition(context
);
4942 statements
.push_back(ldef
);
4944 return context
->backend()->statement_list(statements
);
4946 // Dump the AST representation for select clauses.
4949 Select_clauses::dump_clauses(Ast_dump_context
* ast_dump_context
) const
4951 for (Clauses::const_iterator p
= this->clauses_
.begin();
4952 p
!= this->clauses_
.end();
4954 p
->dump_clause(ast_dump_context
);
4957 // Class Select_statement.
4959 // Return the break label for this switch statement, creating it if
4963 Select_statement::break_label()
4965 if (this->break_label_
== NULL
)
4966 this->break_label_
= new Unnamed_label(this->location());
4967 return this->break_label_
;
4970 // Lower a select statement. This will still return a select
4971 // statement, but it will be modified to implement the order of
4972 // evaluation rules, and to include the send and receive statements as
4973 // explicit statements in the clauses.
4976 Select_statement::do_lower(Gogo
* gogo
, Named_object
* function
,
4977 Block
* enclosing
, Statement_inserter
*)
4979 if (this->is_lowered_
)
4982 Location loc
= this->location();
4984 Block
* b
= new Block(enclosing
, loc
);
4986 go_assert(this->sel_
== NULL
);
4988 int ncases
= this->clauses_
->size();
4989 Type
* selstruct_type
= Channel_type::select_type(ncases
);
4990 this->sel_
= Statement::make_temporary(selstruct_type
, NULL
, loc
);
4991 b
->add_statement(this->sel_
);
4993 int64_t selstruct_size
;
4994 if (!selstruct_type
->backend_type_size(gogo
, &selstruct_size
))
4996 go_assert(saw_errors());
4997 return Statement::make_error_statement(loc
);
5000 Expression
* ref
= Expression::make_temporary_reference(this->sel_
, loc
);
5001 ref
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5002 Expression
* selstruct_size_expr
=
5003 Expression::make_integer_int64(selstruct_size
, NULL
, loc
);
5004 Expression
* size_expr
= Expression::make_integer_ul(ncases
, NULL
, loc
);
5005 Expression
* call
= Runtime::make_call(Runtime::NEWSELECT
, loc
, 3,
5006 ref
, selstruct_size_expr
, size_expr
);
5007 b
->add_statement(Statement::make_statement(call
, true));
5009 this->clauses_
->lower(gogo
, function
, b
, this->sel_
);
5010 this->is_lowered_
= true;
5011 b
->add_statement(this);
5013 return Statement::make_block_statement(b
, loc
);
5016 // Whether the select statement itself may fall through to the following
5020 Select_statement::do_may_fall_through() const
5022 // A select statement is terminating if no break statement
5023 // refers to it and all of its clauses are terminating.
5024 if (this->break_label_
!= NULL
)
5026 return this->clauses_
->may_fall_through();
5029 // Return the backend representation for a select statement.
5032 Select_statement::do_get_backend(Translate_context
* context
)
5034 return this->clauses_
->get_backend(context
, this->sel_
, this->break_label(),
5038 // Dump the AST representation for a select statement.
5041 Select_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5043 ast_dump_context
->print_indent();
5044 ast_dump_context
->ostream() << "select";
5045 if (ast_dump_context
->dump_subblocks())
5047 ast_dump_context
->ostream() << " {" << dsuffix(location()) << std::endl
;
5048 this->clauses_
->dump_clauses(ast_dump_context
);
5049 ast_dump_context
->ostream() << "}";
5051 ast_dump_context
->ostream() << std::endl
;
5054 // Make a select statement.
5057 Statement::make_select_statement(Location location
)
5059 return new Select_statement(location
);
5062 // Class For_statement.
5067 For_statement::do_traverse(Traverse
* traverse
)
5069 if (this->init_
!= NULL
)
5071 if (this->init_
->traverse(traverse
) == TRAVERSE_EXIT
)
5072 return TRAVERSE_EXIT
;
5074 if (this->cond_
!= NULL
)
5076 if (this->traverse_expression(traverse
, &this->cond_
) == TRAVERSE_EXIT
)
5077 return TRAVERSE_EXIT
;
5079 if (this->post_
!= NULL
)
5081 if (this->post_
->traverse(traverse
) == TRAVERSE_EXIT
)
5082 return TRAVERSE_EXIT
;
5084 return this->statements_
->traverse(traverse
);
5087 // Lower a For_statement into if statements and gotos. Getting rid of
5088 // complex statements make it easier to handle garbage collection.
5091 For_statement::do_lower(Gogo
*, Named_object
*, Block
* enclosing
,
5092 Statement_inserter
*)
5095 Location loc
= this->location();
5097 Block
* b
= new Block(enclosing
, this->location());
5098 if (this->init_
!= NULL
)
5100 s
= Statement::make_block_statement(this->init_
,
5101 this->init_
->start_location());
5102 b
->add_statement(s
);
5105 Unnamed_label
* entry
= NULL
;
5106 if (this->cond_
!= NULL
)
5108 entry
= new Unnamed_label(this->location());
5109 b
->add_statement(Statement::make_goto_unnamed_statement(entry
, loc
));
5112 Unnamed_label
* top
= new Unnamed_label(this->location());
5113 top
->set_derived_from(this);
5114 b
->add_statement(Statement::make_unnamed_label_statement(top
));
5116 s
= Statement::make_block_statement(this->statements_
,
5117 this->statements_
->start_location());
5118 b
->add_statement(s
);
5120 Location end_loc
= this->statements_
->end_location();
5122 Unnamed_label
* cont
= this->continue_label_
;
5124 b
->add_statement(Statement::make_unnamed_label_statement(cont
));
5126 if (this->post_
!= NULL
)
5128 s
= Statement::make_block_statement(this->post_
,
5129 this->post_
->start_location());
5130 b
->add_statement(s
);
5131 end_loc
= this->post_
->end_location();
5134 if (this->cond_
== NULL
)
5135 b
->add_statement(Statement::make_goto_unnamed_statement(top
, end_loc
));
5138 b
->add_statement(Statement::make_unnamed_label_statement(entry
));
5140 Location cond_loc
= this->cond_
->location();
5141 Block
* then_block
= new Block(b
, cond_loc
);
5142 s
= Statement::make_goto_unnamed_statement(top
, cond_loc
);
5143 then_block
->add_statement(s
);
5145 s
= Statement::make_if_statement(this->cond_
, then_block
, NULL
, cond_loc
);
5146 b
->add_statement(s
);
5149 Unnamed_label
* brk
= this->break_label_
;
5151 b
->add_statement(Statement::make_unnamed_label_statement(brk
));
5153 b
->set_end_location(end_loc
);
5155 Statement
* bs
= Statement::make_block_statement(b
, loc
);
5156 bs
->block_statement()->set_is_lowered_for_statement();
5160 // Return the break label, creating it if necessary.
5163 For_statement::break_label()
5165 if (this->break_label_
== NULL
)
5166 this->break_label_
= new Unnamed_label(this->location());
5167 return this->break_label_
;
5170 // Return the continue LABEL_EXPR.
5173 For_statement::continue_label()
5175 if (this->continue_label_
== NULL
)
5176 this->continue_label_
= new Unnamed_label(this->location());
5177 return this->continue_label_
;
5180 // Set the break and continue labels a for statement. This is used
5181 // when lowering a for range statement.
5184 For_statement::set_break_continue_labels(Unnamed_label
* break_label
,
5185 Unnamed_label
* continue_label
)
5187 go_assert(this->break_label_
== NULL
&& this->continue_label_
== NULL
);
5188 this->break_label_
= break_label
;
5189 this->continue_label_
= continue_label
;
5192 // Whether the overall statement may fall through.
5195 For_statement::do_may_fall_through() const
5197 // A for loop is terminating if it has no condition and
5198 // no break statement.
5199 if(this->cond_
!= NULL
)
5201 if(this->break_label_
!= NULL
)
5206 // Dump the AST representation for a for statement.
5209 For_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
5211 if (this->init_
!= NULL
&& ast_dump_context
->dump_subblocks())
5213 ast_dump_context
->print_indent();
5214 ast_dump_context
->indent();
5215 ast_dump_context
->ostream() << "// INIT " << std::endl
;
5216 ast_dump_context
->dump_block(this->init_
);
5217 ast_dump_context
->unindent();
5219 ast_dump_context
->print_indent();
5220 ast_dump_context
->ostream() << "for ";
5221 if (this->cond_
!= NULL
)
5222 ast_dump_context
->dump_expression(this->cond_
);
5224 if (ast_dump_context
->dump_subblocks())
5226 ast_dump_context
->ostream() << " {" << std::endl
;
5227 ast_dump_context
->dump_block(this->statements_
);
5228 if (this->init_
!= NULL
)
5230 ast_dump_context
->print_indent();
5231 ast_dump_context
->ostream() << "// POST " << std::endl
;
5232 ast_dump_context
->dump_block(this->post_
);
5234 ast_dump_context
->unindent();
5236 ast_dump_context
->print_indent();
5237 ast_dump_context
->ostream() << "}";
5240 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
5243 // Make a for statement.
5246 Statement::make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
5249 return new For_statement(init
, cond
, post
, location
);
5252 // Class For_range_statement.
5257 For_range_statement::do_traverse(Traverse
* traverse
)
5259 if (this->index_var_
!= NULL
)
5261 if (this->traverse_expression(traverse
, &this->index_var_
)
5263 return TRAVERSE_EXIT
;
5265 if (this->value_var_
!= NULL
)
5267 if (this->traverse_expression(traverse
, &this->value_var_
)
5269 return TRAVERSE_EXIT
;
5271 if (this->traverse_expression(traverse
, &this->range_
) == TRAVERSE_EXIT
)
5272 return TRAVERSE_EXIT
;
5273 return this->statements_
->traverse(traverse
);
5276 // Lower a for range statement. For simplicity we lower this into a
5277 // for statement, which will then be lowered in turn to goto
5281 For_range_statement::do_lower(Gogo
* gogo
, Named_object
*, Block
* enclosing
,
5282 Statement_inserter
*)
5284 Type
* range_type
= this->range_
->type();
5285 if (range_type
->points_to() != NULL
5286 && range_type
->points_to()->array_type() != NULL
5287 && !range_type
->points_to()->is_slice_type())
5288 range_type
= range_type
->points_to();
5291 Type
* value_type
= NULL
;
5292 if (range_type
->array_type() != NULL
)
5294 index_type
= Type::lookup_integer_type("int");
5295 value_type
= range_type
->array_type()->element_type();
5297 else if (range_type
->is_string_type())
5299 index_type
= Type::lookup_integer_type("int");
5300 value_type
= gogo
->lookup_global("rune")->type_value();
5302 else if (range_type
->map_type() != NULL
)
5304 index_type
= range_type
->map_type()->key_type();
5305 value_type
= range_type
->map_type()->val_type();
5307 else if (range_type
->channel_type() != NULL
)
5309 index_type
= range_type
->channel_type()->element_type();
5310 if (this->value_var_
!= NULL
)
5312 if (!this->value_var_
->type()->is_error())
5313 this->report_error(_("too many variables for range clause "
5315 return Statement::make_error_statement(this->location());
5320 this->report_error(_("range clause must have "
5321 "array, slice, string, map, or channel type"));
5322 return Statement::make_error_statement(this->location());
5325 // If there is only one iteration variable, and len(this->range_) is
5326 // constant, then we do not evaluate the range variable. len(x) is
5327 // a contant if x is a string constant or if x is an array. If x is
5328 // a constant then evaluating it won't make any difference, so the
5329 // only case to consider is when x is an array whose length is constant.
5331 if ((this->value_var_
== NULL
|| this->value_var_
->is_sink_expression())
5332 && range_type
->array_type() != NULL
5333 && !range_type
->is_slice_type()
5334 && Builtin_call_expression::array_len_is_constant(this->range_
))
5337 Location loc
= this->location();
5338 Block
* temp_block
= new Block(enclosing
, loc
);
5340 Named_object
* range_object
= NULL
;
5341 Temporary_statement
* range_temp
= NULL
;
5344 Var_expression
* ve
= this->range_
->var_expression();
5346 range_object
= ve
->named_object();
5349 range_temp
= Statement::make_temporary(NULL
, this->range_
, loc
);
5350 temp_block
->add_statement(range_temp
);
5351 this->range_
= NULL
;
5355 Temporary_statement
* index_temp
= Statement::make_temporary(index_type
,
5357 temp_block
->add_statement(index_temp
);
5359 Temporary_statement
* value_temp
= NULL
;
5360 if (this->value_var_
!= NULL
&& !this->value_var_
->is_sink_expression())
5362 value_temp
= Statement::make_temporary(value_type
, NULL
, loc
);
5363 temp_block
->add_statement(value_temp
);
5366 Block
* body
= new Block(temp_block
, loc
);
5373 // Arrange to do a loop appropriate for the type. We will produce
5374 // for INIT ; COND ; POST {
5376 // INDEX = INDEX_TEMP
5377 // VALUE = VALUE_TEMP // If there is a value
5378 // original statements
5381 if (range_type
->is_slice_type())
5382 this->lower_range_slice(gogo
, temp_block
, body
, range_object
, range_temp
,
5383 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5385 else if (range_type
->array_type() != NULL
)
5386 this->lower_range_array(gogo
, temp_block
, body
, range_object
, range_temp
,
5387 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5389 else if (range_type
->is_string_type())
5390 this->lower_range_string(gogo
, temp_block
, body
, range_object
, range_temp
,
5391 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5393 else if (range_type
->map_type() != NULL
)
5394 this->lower_range_map(gogo
, range_type
->map_type(), temp_block
, body
,
5395 range_object
, range_temp
, index_temp
, value_temp
,
5396 &init
, &cond
, &iter_init
, &post
);
5397 else if (range_type
->channel_type() != NULL
)
5398 this->lower_range_channel(gogo
, temp_block
, body
, range_object
, range_temp
,
5399 index_temp
, value_temp
, &init
, &cond
, &iter_init
,
5404 if (iter_init
!= NULL
)
5405 body
->add_statement(Statement::make_block_statement(iter_init
, loc
));
5407 if (this->index_var_
!= NULL
)
5410 Expression
* index_ref
=
5411 Expression::make_temporary_reference(index_temp
, loc
);
5412 if (this->value_var_
== NULL
|| this->value_var_
->is_sink_expression())
5413 assign
= Statement::make_assignment(this->index_var_
, index_ref
, loc
);
5416 Expression_list
* lhs
= new Expression_list();
5417 lhs
->push_back(this->index_var_
);
5418 lhs
->push_back(this->value_var_
);
5420 Expression_list
* rhs
= new Expression_list();
5421 rhs
->push_back(index_ref
);
5422 rhs
->push_back(Expression::make_temporary_reference(value_temp
, loc
));
5424 assign
= Statement::make_tuple_assignment(lhs
, rhs
, loc
);
5426 body
->add_statement(assign
);
5429 body
->add_statement(Statement::make_block_statement(this->statements_
, loc
));
5431 body
->set_end_location(this->statements_
->end_location());
5433 For_statement
* loop
= Statement::make_for_statement(init
, cond
, post
,
5435 loop
->add_statements(body
);
5436 loop
->set_break_continue_labels(this->break_label_
, this->continue_label_
);
5438 temp_block
->add_statement(loop
);
5440 return Statement::make_block_statement(temp_block
, loc
);
5443 // Return a reference to the range, which may be in RANGE_OBJECT or in
5447 For_range_statement::make_range_ref(Named_object
* range_object
,
5448 Temporary_statement
* range_temp
,
5451 if (range_object
!= NULL
)
5452 return Expression::make_var_reference(range_object
, loc
);
5454 return Expression::make_temporary_reference(range_temp
, loc
);
5457 // Return a call to the predeclared function FUNCNAME passing a
5458 // reference to the temporary variable ARG.
5461 For_range_statement::call_builtin(Gogo
* gogo
, const char* funcname
,
5465 Named_object
* no
= gogo
->lookup_global(funcname
);
5466 go_assert(no
!= NULL
&& no
->is_function_declaration());
5467 Expression
* func
= Expression::make_func_reference(no
, NULL
, loc
);
5468 Expression_list
* params
= new Expression_list();
5469 params
->push_back(arg
);
5470 return Expression::make_call(func
, params
, false, loc
);
5473 // Lower a for range over an array.
5476 For_range_statement::lower_range_array(Gogo
* gogo
,
5479 Named_object
* range_object
,
5480 Temporary_statement
* range_temp
,
5481 Temporary_statement
* index_temp
,
5482 Temporary_statement
* value_temp
,
5488 Location loc
= this->location();
5490 // The loop we generate:
5491 // len_temp := len(range)
5492 // range_temp := range
5493 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5494 // value_temp = range_temp[index_temp]
5495 // index = index_temp
5496 // value = value_temp
5502 // len_temp = len(range)
5505 Block
* init
= new Block(enclosing
, loc
);
5507 Expression
* len_arg
;
5508 if (range_object
== NULL
&& range_temp
== NULL
)
5510 // Don't evaluate this->range_, just get its length.
5511 len_arg
= this->range_
;
5515 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5516 range_temp
= Statement::make_temporary(NULL
, ref
, loc
);
5517 init
->add_statement(range_temp
);
5520 Expression
* len_call
= this->call_builtin(gogo
, "len", len_arg
, loc
);
5521 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5523 init
->add_statement(len_temp
);
5525 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, loc
);
5527 Temporary_reference_expression
* tref
=
5528 Expression::make_temporary_reference(index_temp
, loc
);
5529 tref
->set_is_lvalue();
5530 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5531 init
->add_statement(s
);
5536 // index_temp < len_temp
5538 Expression
* ref
= Expression::make_temporary_reference(index_temp
, loc
);
5539 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5540 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5544 // Set *PITER_INIT to
5545 // value_temp = range[index_temp]
5547 Block
* iter_init
= NULL
;
5548 if (value_temp
!= NULL
)
5550 iter_init
= new Block(body_block
, loc
);
5552 ref
= Expression::make_temporary_reference(range_temp
, loc
);
5553 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5554 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, NULL
, loc
);
5556 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5557 tref
->set_is_lvalue();
5558 s
= Statement::make_assignment(tref
, index
, loc
);
5560 iter_init
->add_statement(s
);
5562 *piter_init
= iter_init
;
5567 Block
* post
= new Block(enclosing
, loc
);
5568 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5569 tref
->set_is_lvalue();
5570 s
= Statement::make_inc_statement(tref
);
5571 post
->add_statement(s
);
5575 // Lower a for range over a slice.
5578 For_range_statement::lower_range_slice(Gogo
* gogo
,
5581 Named_object
* range_object
,
5582 Temporary_statement
* range_temp
,
5583 Temporary_statement
* index_temp
,
5584 Temporary_statement
* value_temp
,
5590 Location loc
= this->location();
5592 // The loop we generate:
5593 // for_temp := range
5594 // len_temp := len(for_temp)
5595 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5596 // value_temp = for_temp[index_temp]
5597 // index = index_temp
5598 // value = value_temp
5602 // Using for_temp means that we don't need to check bounds when
5603 // fetching range_temp[index_temp].
5606 // range_temp := range
5608 // len_temp = len(range_temp)
5611 Block
* init
= new Block(enclosing
, loc
);
5613 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5614 Temporary_statement
* for_temp
= Statement::make_temporary(NULL
, ref
, loc
);
5615 init
->add_statement(for_temp
);
5617 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5618 Expression
* len_call
= this->call_builtin(gogo
, "len", ref
, loc
);
5619 Temporary_statement
* len_temp
= Statement::make_temporary(index_temp
->type(),
5621 init
->add_statement(len_temp
);
5623 Expression
* zexpr
= Expression::make_integer_ul(0, NULL
, loc
);
5625 Temporary_reference_expression
* tref
=
5626 Expression::make_temporary_reference(index_temp
, loc
);
5627 tref
->set_is_lvalue();
5628 Statement
* s
= Statement::make_assignment(tref
, zexpr
, loc
);
5629 init
->add_statement(s
);
5634 // index_temp < len_temp
5636 ref
= Expression::make_temporary_reference(index_temp
, loc
);
5637 Expression
* ref2
= Expression::make_temporary_reference(len_temp
, loc
);
5638 Expression
* lt
= Expression::make_binary(OPERATOR_LT
, ref
, ref2
, loc
);
5642 // Set *PITER_INIT to
5643 // value_temp = range[index_temp]
5645 Block
* iter_init
= NULL
;
5646 if (value_temp
!= NULL
)
5648 iter_init
= new Block(body_block
, loc
);
5650 ref
= Expression::make_temporary_reference(for_temp
, loc
);
5651 Expression
* ref2
= Expression::make_temporary_reference(index_temp
, loc
);
5652 Expression
* index
= Expression::make_index(ref
, ref2
, NULL
, NULL
, loc
);
5654 tref
= Expression::make_temporary_reference(value_temp
, loc
);
5655 tref
->set_is_lvalue();
5656 s
= Statement::make_assignment(tref
, index
, loc
);
5658 iter_init
->add_statement(s
);
5660 *piter_init
= iter_init
;
5665 Block
* post
= new Block(enclosing
, loc
);
5666 tref
= Expression::make_temporary_reference(index_temp
, loc
);
5667 tref
->set_is_lvalue();
5668 s
= Statement::make_inc_statement(tref
);
5669 post
->add_statement(s
);
5673 // Lower a for range over a string.
5676 For_range_statement::lower_range_string(Gogo
* gogo
,
5679 Named_object
* range_object
,
5680 Temporary_statement
* range_temp
,
5681 Temporary_statement
* index_temp
,
5682 Temporary_statement
* value_temp
,
5688 Location loc
= this->location();
5690 // The loop we generate:
5691 // len_temp := len(range)
5692 // var next_index_temp int
5693 // for index_temp = 0; index_temp < len_temp; index_temp = next_index_temp {
5694 // value_temp = rune(range[index_temp])
5695 // if value_temp < utf8.RuneSelf {
5696 // next_index_temp = index_temp + 1
5698 // value_temp, next_index_temp = decoderune(range, index_temp)
5700 // index = index_temp
5701 // value = value_temp
5706 // len_temp := len(range)
5707 // var next_index_temp int
5709 // var value_temp rune // if value_temp not passed in
5711 Block
* init
= new Block(enclosing
, loc
);
5713 Expression
* ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5714 Call_expression
* call
= this->call_builtin(gogo
, "len", ref
, loc
);
5715 Temporary_statement
* len_temp
=
5716 Statement::make_temporary(index_temp
->type(), call
, loc
);
5717 init
->add_statement(len_temp
);
5719 Temporary_statement
* next_index_temp
=
5720 Statement::make_temporary(index_temp
->type(), NULL
, loc
);
5721 init
->add_statement(next_index_temp
);
5723 Temporary_reference_expression
* index_ref
=
5724 Expression::make_temporary_reference(index_temp
, loc
);
5725 index_ref
->set_is_lvalue();
5726 Expression
* zexpr
= Expression::make_integer_ul(0, index_temp
->type(), loc
);
5727 Statement
* s
= Statement::make_assignment(index_ref
, zexpr
, loc
);
5728 init
->add_statement(s
);
5731 if (value_temp
!= NULL
)
5732 rune_type
= value_temp
->type();
5735 rune_type
= gogo
->lookup_global("rune")->type_value();
5736 value_temp
= Statement::make_temporary(rune_type
, NULL
, loc
);
5737 init
->add_statement(value_temp
);
5743 // index_temp < len_temp
5745 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5746 Expression
* len_ref
=
5747 Expression::make_temporary_reference(len_temp
, loc
);
5748 *pcond
= Expression::make_binary(OPERATOR_LT
, index_ref
, len_ref
, loc
);
5750 // Set *PITER_INIT to
5751 // value_temp = rune(range[index_temp])
5752 // if value_temp < utf8.RuneSelf {
5753 // next_index_temp = index_temp + 1
5755 // value_temp, next_index_temp = decoderune(range, index_temp)
5758 Block
* iter_init
= new Block(body_block
, loc
);
5760 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5761 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5762 ref
= Expression::make_string_index(ref
, index_ref
, NULL
, loc
);
5763 ref
= Expression::make_cast(rune_type
, ref
, loc
);
5764 Temporary_reference_expression
* value_ref
=
5765 Expression::make_temporary_reference(value_temp
, loc
);
5766 value_ref
->set_is_lvalue();
5767 s
= Statement::make_assignment(value_ref
, ref
, loc
);
5768 iter_init
->add_statement(s
);
5770 value_ref
= Expression::make_temporary_reference(value_temp
, loc
);
5771 Expression
* rune_self
= Expression::make_integer_ul(0x80, rune_type
, loc
);
5772 Expression
* cond
= Expression::make_binary(OPERATOR_LT
, value_ref
, rune_self
,
5775 Block
* then_block
= new Block(iter_init
, loc
);
5777 Temporary_reference_expression
* lhs
=
5778 Expression::make_temporary_reference(next_index_temp
, loc
);
5779 lhs
->set_is_lvalue();
5780 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5781 Expression
* one
= Expression::make_integer_ul(1, index_temp
->type(), loc
);
5782 Expression
* sum
= Expression::make_binary(OPERATOR_PLUS
, index_ref
, one
,
5784 s
= Statement::make_assignment(lhs
, sum
, loc
);
5785 then_block
->add_statement(s
);
5787 Block
* else_block
= new Block(iter_init
, loc
);
5789 ref
= this->make_range_ref(range_object
, range_temp
, loc
);
5790 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5791 call
= Runtime::make_call(Runtime::DECODERUNE
, loc
, 2, ref
, index_ref
);
5793 value_ref
= Expression::make_temporary_reference(value_temp
, loc
);
5794 value_ref
->set_is_lvalue();
5795 Expression
* res
= Expression::make_call_result(call
, 0);
5796 s
= Statement::make_assignment(value_ref
, res
, loc
);
5797 else_block
->add_statement(s
);
5799 lhs
= Expression::make_temporary_reference(next_index_temp
, loc
);
5800 lhs
->set_is_lvalue();
5801 res
= Expression::make_call_result(call
, 1);
5802 s
= Statement::make_assignment(lhs
, res
, loc
);
5803 else_block
->add_statement(s
);
5805 s
= Statement::make_if_statement(cond
, then_block
, else_block
, loc
);
5806 iter_init
->add_statement(s
);
5808 *piter_init
= iter_init
;
5811 // index_temp = next_index_temp
5813 Block
* post
= new Block(enclosing
, loc
);
5815 index_ref
= Expression::make_temporary_reference(index_temp
, loc
);
5816 index_ref
->set_is_lvalue();
5817 ref
= Expression::make_temporary_reference(next_index_temp
, loc
);
5818 s
= Statement::make_assignment(index_ref
, ref
, loc
);
5820 post
->add_statement(s
);
5824 // Lower a for range over a map.
5827 For_range_statement::lower_range_map(Gogo
* gogo
,
5831 Named_object
* range_object
,
5832 Temporary_statement
* range_temp
,
5833 Temporary_statement
* index_temp
,
5834 Temporary_statement
* value_temp
,
5840 Location loc
= this->location();
5842 // The runtime uses a struct to handle ranges over a map. The
5843 // struct is built by Map_type::hiter_type for a specific map type.
5845 // The loop we generate:
5846 // var hiter map_iteration_struct
5847 // for mapiterinit(type, range, &hiter); hiter.key != nil; mapiternext(&hiter) {
5848 // index_temp = *hiter.key
5849 // value_temp = *hiter.val
5850 // index = index_temp
5851 // value = value_temp
5856 // var hiter map_iteration_struct
5857 // runtime.mapiterinit(type, range, &hiter)
5859 Block
* init
= new Block(enclosing
, loc
);
5861 Type
* map_iteration_type
= map_type
->hiter_type(gogo
);
5862 Temporary_statement
* hiter
= Statement::make_temporary(map_iteration_type
,
5864 init
->add_statement(hiter
);
5866 Expression
* p1
= Expression::make_type_descriptor(map_type
, loc
);
5867 Expression
* p2
= this->make_range_ref(range_object
, range_temp
, loc
);
5868 Expression
* ref
= Expression::make_temporary_reference(hiter
, loc
);
5869 Expression
* p3
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5870 Expression
* call
= Runtime::make_call(Runtime::MAPITERINIT
, loc
, 3,
5872 init
->add_statement(Statement::make_statement(call
, true));
5879 ref
= Expression::make_temporary_reference(hiter
, loc
);
5880 ref
= Expression::make_field_reference(ref
, 0, loc
);
5881 Expression
* ne
= Expression::make_binary(OPERATOR_NOTEQ
, ref
,
5882 Expression::make_nil(loc
),
5886 // Set *PITER_INIT to
5887 // index_temp = *hiter.key
5888 // value_temp = *hiter.val
5890 Block
* iter_init
= new Block(body_block
, loc
);
5892 Expression
* lhs
= Expression::make_temporary_reference(index_temp
, loc
);
5893 Expression
* rhs
= Expression::make_temporary_reference(hiter
, loc
);
5894 rhs
= Expression::make_field_reference(ref
, 0, loc
);
5895 rhs
= Expression::make_dereference(ref
, Expression::NIL_CHECK_NOT_NEEDED
,
5897 Statement
* set
= Statement::make_assignment(lhs
, rhs
, loc
);
5898 iter_init
->add_statement(set
);
5900 if (value_temp
!= NULL
)
5902 lhs
= Expression::make_temporary_reference(value_temp
, loc
);
5903 rhs
= Expression::make_temporary_reference(hiter
, loc
);
5904 rhs
= Expression::make_field_reference(rhs
, 1, loc
);
5905 rhs
= Expression::make_dereference(rhs
, Expression::NIL_CHECK_NOT_NEEDED
,
5907 set
= Statement::make_assignment(lhs
, rhs
, loc
);
5908 iter_init
->add_statement(set
);
5911 *piter_init
= iter_init
;
5914 // mapiternext(&hiter)
5916 Block
* post
= new Block(enclosing
, loc
);
5918 ref
= Expression::make_temporary_reference(hiter
, loc
);
5919 p1
= Expression::make_unary(OPERATOR_AND
, ref
, loc
);
5920 call
= Runtime::make_call(Runtime::MAPITERNEXT
, loc
, 1, p1
);
5921 post
->add_statement(Statement::make_statement(call
, true));
5926 // Lower a for range over a channel.
5929 For_range_statement::lower_range_channel(Gogo
*,
5932 Named_object
* range_object
,
5933 Temporary_statement
* range_temp
,
5934 Temporary_statement
* index_temp
,
5935 Temporary_statement
* value_temp
,
5941 go_assert(value_temp
== NULL
);
5943 Location loc
= this->location();
5945 // The loop we generate:
5947 // index_temp, ok_temp = <-range
5951 // index = index_temp
5955 // We have no initialization code, no condition, and no post code.
5961 // Set *PITER_INIT to
5962 // index_temp, ok_temp = <-range
5967 Block
* iter_init
= new Block(body_block
, loc
);
5969 Temporary_statement
* ok_temp
=
5970 Statement::make_temporary(Type::lookup_bool_type(), NULL
, loc
);
5971 iter_init
->add_statement(ok_temp
);
5973 Expression
* cref
= this->make_range_ref(range_object
, range_temp
, loc
);
5974 Temporary_reference_expression
* iref
=
5975 Expression::make_temporary_reference(index_temp
, loc
);
5976 iref
->set_is_lvalue();
5977 Temporary_reference_expression
* oref
=
5978 Expression::make_temporary_reference(ok_temp
, loc
);
5979 oref
->set_is_lvalue();
5980 Statement
* s
= Statement::make_tuple_receive_assignment(iref
, oref
, cref
,
5982 iter_init
->add_statement(s
);
5984 Block
* then_block
= new Block(iter_init
, loc
);
5985 s
= Statement::make_break_statement(this->break_label(), loc
);
5986 then_block
->add_statement(s
);
5988 oref
= Expression::make_temporary_reference(ok_temp
, loc
);
5989 Expression
* cond
= Expression::make_unary(OPERATOR_NOT
, oref
, loc
);
5990 s
= Statement::make_if_statement(cond
, then_block
, NULL
, loc
);
5991 iter_init
->add_statement(s
);
5993 *piter_init
= iter_init
;
5996 // Return the break LABEL_EXPR.
5999 For_range_statement::break_label()
6001 if (this->break_label_
== NULL
)
6002 this->break_label_
= new Unnamed_label(this->location());
6003 return this->break_label_
;
6006 // Return the continue LABEL_EXPR.
6009 For_range_statement::continue_label()
6011 if (this->continue_label_
== NULL
)
6012 this->continue_label_
= new Unnamed_label(this->location());
6013 return this->continue_label_
;
6016 // Dump the AST representation for a for range statement.
6019 For_range_statement::do_dump_statement(Ast_dump_context
* ast_dump_context
) const
6022 ast_dump_context
->print_indent();
6023 ast_dump_context
->ostream() << "for ";
6024 ast_dump_context
->dump_expression(this->index_var_
);
6025 if (this->value_var_
!= NULL
)
6027 ast_dump_context
->ostream() << ", ";
6028 ast_dump_context
->dump_expression(this->value_var_
);
6031 ast_dump_context
->ostream() << " = range ";
6032 ast_dump_context
->dump_expression(this->range_
);
6033 if (ast_dump_context
->dump_subblocks())
6035 ast_dump_context
->ostream() << " {" << std::endl
;
6037 ast_dump_context
->indent();
6039 ast_dump_context
->dump_block(this->statements_
);
6041 ast_dump_context
->unindent();
6042 ast_dump_context
->print_indent();
6043 ast_dump_context
->ostream() << "}";
6045 ast_dump_context
->ostream() << dsuffix(location()) << std::endl
;
6048 // Make a for statement with a range clause.
6050 For_range_statement
*
6051 Statement::make_for_range_statement(Expression
* index_var
,
6052 Expression
* value_var
,
6056 return new For_range_statement(index_var
, value_var
, range
, location
);