compiler: Remove obsolete hidden_fields_are_ok code.
[official-gcc.git] / gcc / go / gofrontend / statements.cc
blob183aaf4f8ab516f5c05594751cc0d23cffc59517
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.
7 #include "go-system.h"
9 #include "go-c.h"
10 #include "types.h"
11 #include "expressions.h"
12 #include "gogo.h"
13 #include "runtime.h"
14 #include "backend.h"
15 #include "statements.h"
16 #include "ast-dump.h"
18 // Class Statement.
20 Statement::Statement(Statement_classification classification,
21 Location location)
22 : classification_(classification), location_(location)
26 Statement::~Statement()
30 // Traverse the tree. The work of walking the components is handled
31 // by the subclasses.
33 int
34 Statement::traverse(Block* block, size_t* pindex, Traverse* traverse)
36 if (this->classification_ == STATEMENT_ERROR)
37 return TRAVERSE_CONTINUE;
39 unsigned int traverse_mask = traverse->traverse_mask();
41 if ((traverse_mask & Traverse::traverse_statements) != 0)
43 int t = traverse->statement(block, pindex, this);
44 if (t == TRAVERSE_EXIT)
45 return TRAVERSE_EXIT;
46 else if (t == TRAVERSE_SKIP_COMPONENTS)
47 return TRAVERSE_CONTINUE;
50 // No point in checking traverse_mask here--a statement may contain
51 // other blocks or statements, and if we got here we always want to
52 // walk them.
53 return this->do_traverse(traverse);
56 // Traverse the contents of a statement.
58 int
59 Statement::traverse_contents(Traverse* traverse)
61 return this->do_traverse(traverse);
64 // Traverse assignments.
66 bool
67 Statement::traverse_assignments(Traverse_assignments* tassign)
69 if (this->classification_ == STATEMENT_ERROR)
70 return false;
71 return this->do_traverse_assignments(tassign);
74 // Traverse an expression in a statement. This is a helper function
75 // for child classes.
77 int
78 Statement::traverse_expression(Traverse* traverse, Expression** expr)
80 if ((traverse->traverse_mask()
81 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
82 return TRAVERSE_CONTINUE;
83 return Expression::traverse(expr, traverse);
86 // Traverse an expression list in a statement. This is a helper
87 // function for child classes.
89 int
90 Statement::traverse_expression_list(Traverse* traverse,
91 Expression_list* expr_list)
93 if (expr_list == NULL)
94 return TRAVERSE_CONTINUE;
95 if ((traverse->traverse_mask()
96 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
97 return TRAVERSE_CONTINUE;
98 return expr_list->traverse(traverse);
101 // Traverse a type in a statement. This is a helper function for
102 // child classes.
105 Statement::traverse_type(Traverse* traverse, Type* type)
107 if ((traverse->traverse_mask()
108 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
109 return TRAVERSE_CONTINUE;
110 return Type::traverse(type, traverse);
113 // Set type information for unnamed constants. This is really done by
114 // the child class.
116 void
117 Statement::determine_types()
119 this->do_determine_types();
122 // If this is a thunk statement, return it.
124 Thunk_statement*
125 Statement::thunk_statement()
127 Thunk_statement* ret = this->convert<Thunk_statement, STATEMENT_GO>();
128 if (ret == NULL)
129 ret = this->convert<Thunk_statement, STATEMENT_DEFER>();
130 return ret;
133 // Convert a Statement to the backend representation. This is really
134 // done by the child class.
136 Bstatement*
137 Statement::get_backend(Translate_context* context)
139 if (this->classification_ == STATEMENT_ERROR)
140 return context->backend()->error_statement();
141 return this->do_get_backend(context);
144 // Dump AST representation for a statement to a dump context.
146 void
147 Statement::dump_statement(Ast_dump_context* ast_dump_context) const
149 this->do_dump_statement(ast_dump_context);
152 // Note that this statement is erroneous. This is called by children
153 // when they discover an error.
155 void
156 Statement::set_is_error()
158 this->classification_ = STATEMENT_ERROR;
161 // For children to call to report an error conveniently.
163 void
164 Statement::report_error(const char* msg)
166 error_at(this->location_, "%s", msg);
167 this->set_is_error();
170 // An error statement, used to avoid crashing after we report an
171 // error.
173 class Error_statement : public Statement
175 public:
176 Error_statement(Location location)
177 : Statement(STATEMENT_ERROR, location)
180 protected:
182 do_traverse(Traverse*)
183 { return TRAVERSE_CONTINUE; }
185 Bstatement*
186 do_get_backend(Translate_context*)
187 { go_unreachable(); }
189 void
190 do_dump_statement(Ast_dump_context*) const;
193 // Dump the AST representation for an error statement.
195 void
196 Error_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
198 ast_dump_context->print_indent();
199 ast_dump_context->ostream() << "Error statement" << std::endl;
202 // Make an error statement.
204 Statement*
205 Statement::make_error_statement(Location location)
207 return new Error_statement(location);
210 // Class Variable_declaration_statement.
212 Variable_declaration_statement::Variable_declaration_statement(
213 Named_object* var)
214 : Statement(STATEMENT_VARIABLE_DECLARATION, var->var_value()->location()),
215 var_(var)
219 // We don't actually traverse the variable here; it was traversed
220 // while traversing the Block.
223 Variable_declaration_statement::do_traverse(Traverse*)
225 return TRAVERSE_CONTINUE;
228 // Traverse the assignments in a variable declaration. Note that this
229 // traversal is different from the usual traversal.
231 bool
232 Variable_declaration_statement::do_traverse_assignments(
233 Traverse_assignments* tassign)
235 tassign->initialize_variable(this->var_);
236 return true;
239 // Lower the variable's initialization expression.
241 Statement*
242 Variable_declaration_statement::do_lower(Gogo* gogo, Named_object* function,
243 Block*, Statement_inserter* inserter)
245 this->var_->var_value()->lower_init_expression(gogo, function, inserter);
246 return this;
249 // Flatten the variable's initialization expression.
251 Statement*
252 Variable_declaration_statement::do_flatten(Gogo* gogo, Named_object* function,
253 Block*, Statement_inserter* inserter)
255 this->var_->var_value()->flatten_init_expression(gogo, function, inserter);
256 return this;
259 // Convert a variable declaration to the backend representation.
261 Bstatement*
262 Variable_declaration_statement::do_get_backend(Translate_context* context)
264 Variable* var = this->var_->var_value();
265 Bvariable* bvar = this->var_->get_backend_variable(context->gogo(),
266 context->function());
267 Bexpression* binit = var->get_init(context->gogo(), context->function());
269 if (!var->is_in_heap())
271 go_assert(binit != NULL);
272 return context->backend()->init_statement(bvar, binit);
275 // Something takes the address of this variable, so the value is
276 // stored in the heap. Initialize it to newly allocated memory
277 // space, and assign the initial value to the new space.
278 Location loc = this->location();
279 Named_object* newfn = context->gogo()->lookup_global("new");
280 go_assert(newfn != NULL && newfn->is_function_declaration());
281 Expression* func = Expression::make_func_reference(newfn, NULL, loc);
282 Expression_list* params = new Expression_list();
283 params->push_back(Expression::make_type(var->type(), loc));
284 Expression* call = Expression::make_call(func, params, false, loc);
285 context->gogo()->lower_expression(context->function(), NULL, &call);
286 Temporary_statement* temp = Statement::make_temporary(NULL, call, loc);
287 Bstatement* btemp = temp->get_backend(context);
289 Bstatement* set = NULL;
290 if (binit != NULL)
292 Expression* e = Expression::make_temporary_reference(temp, loc);
293 e = Expression::make_unary(OPERATOR_MULT, e, loc);
294 Bexpression* be = e->get_backend(context);
295 set = context->backend()->assignment_statement(be, binit, loc);
298 Expression* ref = Expression::make_temporary_reference(temp, loc);
299 Bexpression* bref = ref->get_backend(context);
300 Bstatement* sinit = context->backend()->init_statement(bvar, bref);
302 std::vector<Bstatement*> stats;
303 stats.reserve(3);
304 stats.push_back(btemp);
305 if (set != NULL)
306 stats.push_back(set);
307 stats.push_back(sinit);
308 return context->backend()->statement_list(stats);
311 // Dump the AST representation for a variable declaration.
313 void
314 Variable_declaration_statement::do_dump_statement(
315 Ast_dump_context* ast_dump_context) const
317 ast_dump_context->print_indent();
319 go_assert(var_->is_variable());
320 ast_dump_context->ostream() << "var " << this->var_->name() << " ";
321 Variable* var = this->var_->var_value();
322 if (var->has_type())
324 ast_dump_context->dump_type(var->type());
325 ast_dump_context->ostream() << " ";
327 if (var->init() != NULL)
329 ast_dump_context->ostream() << "= ";
330 ast_dump_context->dump_expression(var->init());
332 ast_dump_context->ostream() << std::endl;
335 // Make a variable declaration.
337 Statement*
338 Statement::make_variable_declaration(Named_object* var)
340 return new Variable_declaration_statement(var);
343 // Class Temporary_statement.
345 // Return the type of the temporary variable.
347 Type*
348 Temporary_statement::type() const
350 return this->type_ != NULL ? this->type_ : this->init_->type();
353 // Traversal.
356 Temporary_statement::do_traverse(Traverse* traverse)
358 if (this->type_ != NULL
359 && this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
360 return TRAVERSE_EXIT;
361 if (this->init_ == NULL)
362 return TRAVERSE_CONTINUE;
363 else
364 return this->traverse_expression(traverse, &this->init_);
367 // Traverse assignments.
369 bool
370 Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
372 if (this->init_ == NULL)
373 return false;
374 tassign->value(&this->init_, true, true);
375 return true;
378 // Determine types.
380 void
381 Temporary_statement::do_determine_types()
383 if (this->type_ != NULL && this->type_->is_abstract())
384 this->type_ = this->type_->make_non_abstract_type();
386 if (this->init_ != NULL)
388 if (this->type_ == NULL)
389 this->init_->determine_type_no_context();
390 else
392 Type_context context(this->type_, false);
393 this->init_->determine_type(&context);
397 if (this->type_ == NULL)
399 this->type_ = this->init_->type();
400 go_assert(!this->type_->is_abstract());
404 // Check types.
406 void
407 Temporary_statement::do_check_types(Gogo*)
409 if (this->type_ != NULL && this->init_ != NULL)
411 std::string reason;
412 if (!Type::are_assignable(this->type_, this->init_->type(), &reason))
414 if (reason.empty())
415 error_at(this->location(), "incompatible types in assignment");
416 else
417 error_at(this->location(), "incompatible types in assignment (%s)",
418 reason.c_str());
419 this->set_is_error();
424 // Convert to backend representation.
426 Bstatement*
427 Temporary_statement::do_get_backend(Translate_context* context)
429 go_assert(this->bvariable_ == NULL);
431 Named_object* function = context->function();
432 go_assert(function != NULL);
433 Bfunction* bfunction = function->func_value()->get_decl();
434 Btype* btype = this->type()->get_backend(context->gogo());
436 Bexpression* binit;
437 if (this->init_ == NULL)
438 binit = NULL;
439 else if (this->type_ == NULL)
440 binit = this->init_->get_backend(context);
441 else
443 Expression* init = Expression::make_cast(this->type_, this->init_,
444 this->location());
445 context->gogo()->lower_expression(context->function(), NULL, &init);
446 binit = init->get_backend(context);
449 Bstatement* statement;
450 this->bvariable_ =
451 context->backend()->temporary_variable(bfunction, context->bblock(),
452 btype, binit,
453 this->is_address_taken_,
454 this->location(), &statement);
455 return statement;
458 // Return the backend variable.
460 Bvariable*
461 Temporary_statement::get_backend_variable(Translate_context* context) const
463 if (this->bvariable_ == NULL)
465 go_assert(saw_errors());
466 return context->backend()->error_variable();
468 return this->bvariable_;
471 // Dump the AST represemtation for a temporary statement
473 void
474 Temporary_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
476 ast_dump_context->print_indent();
477 ast_dump_context->dump_temp_variable_name(this);
478 if (this->type_ != NULL)
480 ast_dump_context->ostream() << " ";
481 ast_dump_context->dump_type(this->type_);
483 if (this->init_ != NULL)
485 ast_dump_context->ostream() << " = ";
486 ast_dump_context->dump_expression(this->init_);
488 ast_dump_context->ostream() << std::endl;
491 // Make and initialize a temporary variable in BLOCK.
493 Temporary_statement*
494 Statement::make_temporary(Type* type, Expression* init,
495 Location location)
497 return new Temporary_statement(type, init, location);
500 // An assignment statement.
502 class Assignment_statement : public Statement
504 public:
505 Assignment_statement(Expression* lhs, Expression* rhs,
506 Location location)
507 : Statement(STATEMENT_ASSIGNMENT, location),
508 lhs_(lhs), rhs_(rhs)
511 protected:
513 do_traverse(Traverse* traverse);
515 bool
516 do_traverse_assignments(Traverse_assignments*);
518 void
519 do_determine_types();
521 void
522 do_check_types(Gogo*);
524 Bstatement*
525 do_get_backend(Translate_context*);
527 void
528 do_dump_statement(Ast_dump_context*) const;
530 private:
531 // Left hand side--the lvalue.
532 Expression* lhs_;
533 // Right hand side--the rvalue.
534 Expression* rhs_;
537 // Traversal.
540 Assignment_statement::do_traverse(Traverse* traverse)
542 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
543 return TRAVERSE_EXIT;
544 return this->traverse_expression(traverse, &this->rhs_);
547 bool
548 Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
550 tassign->assignment(&this->lhs_, &this->rhs_);
551 return true;
554 // Set types for the assignment.
556 void
557 Assignment_statement::do_determine_types()
559 this->lhs_->determine_type_no_context();
560 Type* rhs_context_type = this->lhs_->type();
561 if (rhs_context_type->is_sink_type())
562 rhs_context_type = NULL;
563 Type_context context(rhs_context_type, false);
564 this->rhs_->determine_type(&context);
567 // Check types for an assignment.
569 void
570 Assignment_statement::do_check_types(Gogo*)
572 // The left hand side must be either addressable, a map index
573 // expression, or the blank identifier.
574 if (!this->lhs_->is_addressable()
575 && this->lhs_->map_index_expression() == NULL
576 && !this->lhs_->is_sink_expression())
578 if (!this->lhs_->type()->is_error())
579 this->report_error(_("invalid left hand side of assignment"));
580 return;
583 Type* lhs_type = this->lhs_->type();
584 Type* rhs_type = this->rhs_->type();
586 // Invalid assignment of nil to the blank identifier.
587 if (lhs_type->is_sink_type()
588 && rhs_type->is_nil_type())
590 this->report_error(_("use of untyped nil"));
591 return;
594 std::string reason;
595 if (!Type::are_assignable(lhs_type, rhs_type, &reason))
597 if (reason.empty())
598 error_at(this->location(), "incompatible types in assignment");
599 else
600 error_at(this->location(), "incompatible types in assignment (%s)",
601 reason.c_str());
602 this->set_is_error();
605 if (lhs_type->is_error() || rhs_type->is_error())
606 this->set_is_error();
609 // Convert an assignment statement to the backend representation.
611 Bstatement*
612 Assignment_statement::do_get_backend(Translate_context* context)
614 if (this->lhs_->is_sink_expression())
616 Bexpression* rhs = this->rhs_->get_backend(context);
617 return context->backend()->expression_statement(rhs);
620 Bexpression* lhs = this->lhs_->get_backend(context);
621 Expression* conv =
622 Expression::convert_for_assignment(context->gogo(), this->lhs_->type(),
623 this->rhs_, this->location());
624 Bexpression* rhs = conv->get_backend(context);
625 return context->backend()->assignment_statement(lhs, rhs, this->location());
628 // Dump the AST representation for an assignment statement.
630 void
631 Assignment_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
632 const
634 ast_dump_context->print_indent();
635 ast_dump_context->dump_expression(this->lhs_);
636 ast_dump_context->ostream() << " = " ;
637 ast_dump_context->dump_expression(this->rhs_);
638 ast_dump_context->ostream() << std::endl;
641 // Make an assignment statement.
643 Statement*
644 Statement::make_assignment(Expression* lhs, Expression* rhs,
645 Location location)
647 return new Assignment_statement(lhs, rhs, location);
650 // The Move_subexpressions class is used to move all top-level
651 // subexpressions of an expression. This is used for things like
652 // index expressions in which we must evaluate the index value before
653 // it can be changed by a multiple assignment.
655 class Move_subexpressions : public Traverse
657 public:
658 Move_subexpressions(int skip, Block* block)
659 : Traverse(traverse_expressions),
660 skip_(skip), block_(block)
663 protected:
665 expression(Expression**);
667 private:
668 // The number of subexpressions to skip moving. This is used to
669 // avoid moving the array itself, as we only need to move the index.
670 int skip_;
671 // The block where new temporary variables should be added.
672 Block* block_;
676 Move_subexpressions::expression(Expression** pexpr)
678 if (this->skip_ > 0)
679 --this->skip_;
680 else if ((*pexpr)->temporary_reference_expression() == NULL)
682 Location loc = (*pexpr)->location();
683 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
684 this->block_->add_statement(temp);
685 *pexpr = Expression::make_temporary_reference(temp, loc);
687 // We only need to move top-level subexpressions.
688 return TRAVERSE_SKIP_COMPONENTS;
691 // The Move_ordered_evals class is used to find any subexpressions of
692 // an expression that have an evaluation order dependency. It creates
693 // temporary variables to hold them.
695 class Move_ordered_evals : public Traverse
697 public:
698 Move_ordered_evals(Block* block)
699 : Traverse(traverse_expressions),
700 block_(block)
703 protected:
705 expression(Expression**);
707 private:
708 // The block where new temporary variables should be added.
709 Block* block_;
713 Move_ordered_evals::expression(Expression** pexpr)
715 // We have to look at subexpressions first.
716 if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
717 return TRAVERSE_EXIT;
719 int i;
720 if ((*pexpr)->must_eval_subexpressions_in_order(&i))
722 Move_subexpressions ms(i, this->block_);
723 if ((*pexpr)->traverse_subexpressions(&ms) == TRAVERSE_EXIT)
724 return TRAVERSE_EXIT;
727 if ((*pexpr)->must_eval_in_order())
729 Location loc = (*pexpr)->location();
730 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
731 this->block_->add_statement(temp);
732 *pexpr = Expression::make_temporary_reference(temp, loc);
734 return TRAVERSE_SKIP_COMPONENTS;
737 // An assignment operation statement.
739 class Assignment_operation_statement : public Statement
741 public:
742 Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
743 Location location)
744 : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
745 op_(op), lhs_(lhs), rhs_(rhs)
748 protected:
750 do_traverse(Traverse*);
752 bool
753 do_traverse_assignments(Traverse_assignments*)
754 { go_unreachable(); }
756 Statement*
757 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
759 Bstatement*
760 do_get_backend(Translate_context*)
761 { go_unreachable(); }
763 void
764 do_dump_statement(Ast_dump_context*) const;
766 private:
767 // The operator (OPERATOR_PLUSEQ, etc.).
768 Operator op_;
769 // Left hand side.
770 Expression* lhs_;
771 // Right hand side.
772 Expression* rhs_;
775 // Traversal.
778 Assignment_operation_statement::do_traverse(Traverse* traverse)
780 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
781 return TRAVERSE_EXIT;
782 return this->traverse_expression(traverse, &this->rhs_);
785 // Lower an assignment operation statement to a regular assignment
786 // statement.
788 Statement*
789 Assignment_operation_statement::do_lower(Gogo*, Named_object*,
790 Block* enclosing, Statement_inserter*)
792 Location loc = this->location();
794 // We have to evaluate the left hand side expression only once. We
795 // do this by moving out any expression with side effects.
796 Block* b = new Block(enclosing, loc);
797 Move_ordered_evals moe(b);
798 this->lhs_->traverse_subexpressions(&moe);
800 Expression* lval = this->lhs_->copy();
802 Operator op;
803 switch (this->op_)
805 case OPERATOR_PLUSEQ:
806 op = OPERATOR_PLUS;
807 break;
808 case OPERATOR_MINUSEQ:
809 op = OPERATOR_MINUS;
810 break;
811 case OPERATOR_OREQ:
812 op = OPERATOR_OR;
813 break;
814 case OPERATOR_XOREQ:
815 op = OPERATOR_XOR;
816 break;
817 case OPERATOR_MULTEQ:
818 op = OPERATOR_MULT;
819 break;
820 case OPERATOR_DIVEQ:
821 op = OPERATOR_DIV;
822 break;
823 case OPERATOR_MODEQ:
824 op = OPERATOR_MOD;
825 break;
826 case OPERATOR_LSHIFTEQ:
827 op = OPERATOR_LSHIFT;
828 break;
829 case OPERATOR_RSHIFTEQ:
830 op = OPERATOR_RSHIFT;
831 break;
832 case OPERATOR_ANDEQ:
833 op = OPERATOR_AND;
834 break;
835 case OPERATOR_BITCLEAREQ:
836 op = OPERATOR_BITCLEAR;
837 break;
838 default:
839 go_unreachable();
842 Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
843 Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
844 if (b->statements()->empty())
846 delete b;
847 return s;
849 else
851 b->add_statement(s);
852 return Statement::make_block_statement(b, loc);
856 // Dump the AST representation for an assignment operation statement
858 void
859 Assignment_operation_statement::do_dump_statement(
860 Ast_dump_context* ast_dump_context) const
862 ast_dump_context->print_indent();
863 ast_dump_context->dump_expression(this->lhs_);
864 ast_dump_context->dump_operator(this->op_);
865 ast_dump_context->dump_expression(this->rhs_);
866 ast_dump_context->ostream() << std::endl;
869 // Make an assignment operation statement.
871 Statement*
872 Statement::make_assignment_operation(Operator op, Expression* lhs,
873 Expression* rhs, Location location)
875 return new Assignment_operation_statement(op, lhs, rhs, location);
878 // A tuple assignment statement. This differs from an assignment
879 // statement in that the right-hand-side expressions are evaluated in
880 // parallel.
882 class Tuple_assignment_statement : public Statement
884 public:
885 Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
886 Location location)
887 : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
888 lhs_(lhs), rhs_(rhs)
891 protected:
893 do_traverse(Traverse* traverse);
895 bool
896 do_traverse_assignments(Traverse_assignments*)
897 { go_unreachable(); }
899 Statement*
900 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
902 Bstatement*
903 do_get_backend(Translate_context*)
904 { go_unreachable(); }
906 void
907 do_dump_statement(Ast_dump_context*) const;
909 private:
910 // Left hand side--a list of lvalues.
911 Expression_list* lhs_;
912 // Right hand side--a list of rvalues.
913 Expression_list* rhs_;
916 // Traversal.
919 Tuple_assignment_statement::do_traverse(Traverse* traverse)
921 if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
922 return TRAVERSE_EXIT;
923 return this->traverse_expression_list(traverse, this->rhs_);
926 // Lower a tuple assignment. We use temporary variables to split it
927 // up into a set of single assignments.
929 Statement*
930 Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
931 Statement_inserter*)
933 Location loc = this->location();
935 Block* b = new Block(enclosing, loc);
937 // First move out any subexpressions on the left hand side. The
938 // right hand side will be evaluated in the required order anyhow.
939 Move_ordered_evals moe(b);
940 for (Expression_list::iterator plhs = this->lhs_->begin();
941 plhs != this->lhs_->end();
942 ++plhs)
943 Expression::traverse(&*plhs, &moe);
945 std::vector<Temporary_statement*> temps;
946 temps.reserve(this->lhs_->size());
948 Expression_list::const_iterator prhs = this->rhs_->begin();
949 for (Expression_list::const_iterator plhs = this->lhs_->begin();
950 plhs != this->lhs_->end();
951 ++plhs, ++prhs)
953 go_assert(prhs != this->rhs_->end());
955 if ((*plhs)->is_error_expression()
956 || (*plhs)->type()->is_error()
957 || (*prhs)->is_error_expression()
958 || (*prhs)->type()->is_error())
959 continue;
961 if ((*plhs)->is_sink_expression())
963 if ((*prhs)->type()->is_nil_type())
964 this->report_error(_("use of untyped nil"));
965 else
966 b->add_statement(Statement::make_statement(*prhs, true));
967 continue;
970 Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
971 *prhs, loc);
972 b->add_statement(temp);
973 temps.push_back(temp);
976 go_assert(prhs == this->rhs_->end());
978 prhs = this->rhs_->begin();
979 std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
980 for (Expression_list::const_iterator plhs = this->lhs_->begin();
981 plhs != this->lhs_->end();
982 ++plhs, ++prhs)
984 if ((*plhs)->is_error_expression()
985 || (*plhs)->type()->is_error()
986 || (*prhs)->is_error_expression()
987 || (*prhs)->type()->is_error())
988 continue;
990 if ((*plhs)->is_sink_expression())
991 continue;
993 Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
994 b->add_statement(Statement::make_assignment(*plhs, ref, loc));
995 ++ptemp;
997 go_assert(ptemp == temps.end() || saw_errors());
999 return Statement::make_block_statement(b, loc);
1002 // Dump the AST representation for a tuple assignment statement.
1004 void
1005 Tuple_assignment_statement::do_dump_statement(
1006 Ast_dump_context* ast_dump_context) const
1008 ast_dump_context->print_indent();
1009 ast_dump_context->dump_expression_list(this->lhs_);
1010 ast_dump_context->ostream() << " = ";
1011 ast_dump_context->dump_expression_list(this->rhs_);
1012 ast_dump_context->ostream() << std::endl;
1015 // Make a tuple assignment statement.
1017 Statement*
1018 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
1019 Location location)
1021 return new Tuple_assignment_statement(lhs, rhs, location);
1024 // A tuple assignment from a map index expression.
1025 // v, ok = m[k]
1027 class Tuple_map_assignment_statement : public Statement
1029 public:
1030 Tuple_map_assignment_statement(Expression* val, Expression* present,
1031 Expression* map_index,
1032 Location location)
1033 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
1034 val_(val), present_(present), map_index_(map_index)
1037 protected:
1039 do_traverse(Traverse* traverse);
1041 bool
1042 do_traverse_assignments(Traverse_assignments*)
1043 { go_unreachable(); }
1045 Statement*
1046 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1048 Bstatement*
1049 do_get_backend(Translate_context*)
1050 { go_unreachable(); }
1052 void
1053 do_dump_statement(Ast_dump_context*) const;
1055 private:
1056 // Lvalue which receives the value from the map.
1057 Expression* val_;
1058 // Lvalue which receives whether the key value was present.
1059 Expression* present_;
1060 // The map index expression.
1061 Expression* map_index_;
1064 // Traversal.
1067 Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
1069 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1070 || this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
1071 return TRAVERSE_EXIT;
1072 return this->traverse_expression(traverse, &this->map_index_);
1075 // Lower a tuple map assignment.
1077 Statement*
1078 Tuple_map_assignment_statement::do_lower(Gogo*, Named_object*,
1079 Block* enclosing, Statement_inserter*)
1081 Location loc = this->location();
1083 Map_index_expression* map_index = this->map_index_->map_index_expression();
1084 if (map_index == NULL)
1086 this->report_error(_("expected map index on right hand side"));
1087 return Statement::make_error_statement(loc);
1089 Map_type* map_type = map_index->get_map_type();
1090 if (map_type == NULL)
1091 return Statement::make_error_statement(loc);
1093 Block* b = new Block(enclosing, loc);
1095 // Move out any subexpressions to make sure that functions are
1096 // called in the required order.
1097 Move_ordered_evals moe(b);
1098 this->val_->traverse_subexpressions(&moe);
1099 this->present_->traverse_subexpressions(&moe);
1101 // Copy the key value into a temporary so that we can take its
1102 // address without pushing the value onto the heap.
1104 // var key_temp KEY_TYPE = MAP_INDEX
1105 Temporary_statement* key_temp =
1106 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1107 b->add_statement(key_temp);
1109 // var val_temp VAL_TYPE
1110 Temporary_statement* val_temp =
1111 Statement::make_temporary(map_type->val_type(), NULL, loc);
1112 b->add_statement(val_temp);
1114 // var present_temp bool
1115 Temporary_statement* present_temp =
1116 Statement::make_temporary((this->present_->type()->is_sink_type())
1117 ? Type::make_boolean_type()
1118 : this->present_->type(),
1119 NULL, loc);
1120 b->add_statement(present_temp);
1122 // present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp, &val_temp)
1123 Expression* a1 = Expression::make_type_descriptor(map_type, loc);
1124 Expression* a2 = map_index->map();
1125 Temporary_reference_expression* ref =
1126 Expression::make_temporary_reference(key_temp, loc);
1127 Expression* a3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1128 ref = Expression::make_temporary_reference(val_temp, loc);
1129 Expression* a4 = Expression::make_unary(OPERATOR_AND, ref, loc);
1130 Expression* call = Runtime::make_call(Runtime::MAPACCESS2, loc, 4,
1131 a1, a2, a3, a4);
1132 ref = Expression::make_temporary_reference(present_temp, loc);
1133 ref->set_is_lvalue();
1134 Statement* s = Statement::make_assignment(ref, call, loc);
1135 b->add_statement(s);
1137 // val = val_temp
1138 ref = Expression::make_temporary_reference(val_temp, loc);
1139 s = Statement::make_assignment(this->val_, ref, loc);
1140 b->add_statement(s);
1142 // present = present_temp
1143 ref = Expression::make_temporary_reference(present_temp, loc);
1144 s = Statement::make_assignment(this->present_, ref, loc);
1145 b->add_statement(s);
1147 return Statement::make_block_statement(b, loc);
1150 // Dump the AST representation for a tuple map assignment statement.
1152 void
1153 Tuple_map_assignment_statement::do_dump_statement(
1154 Ast_dump_context* ast_dump_context) const
1156 ast_dump_context->print_indent();
1157 ast_dump_context->dump_expression(this->val_);
1158 ast_dump_context->ostream() << ", ";
1159 ast_dump_context->dump_expression(this->present_);
1160 ast_dump_context->ostream() << " = ";
1161 ast_dump_context->dump_expression(this->map_index_);
1162 ast_dump_context->ostream() << std::endl;
1165 // Make a map assignment statement which returns a pair of values.
1167 Statement*
1168 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
1169 Expression* map_index,
1170 Location location)
1172 return new Tuple_map_assignment_statement(val, present, map_index, location);
1175 // Assign a pair of entries to a map.
1176 // m[k] = v, p
1178 class Map_assignment_statement : public Statement
1180 public:
1181 Map_assignment_statement(Expression* map_index,
1182 Expression* val, Expression* should_set,
1183 Location location)
1184 : Statement(STATEMENT_MAP_ASSIGNMENT, location),
1185 map_index_(map_index), val_(val), should_set_(should_set)
1188 protected:
1190 do_traverse(Traverse* traverse);
1192 bool
1193 do_traverse_assignments(Traverse_assignments*)
1194 { go_unreachable(); }
1196 Statement*
1197 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1199 Bstatement*
1200 do_get_backend(Translate_context*)
1201 { go_unreachable(); }
1203 void
1204 do_dump_statement(Ast_dump_context*) const;
1206 private:
1207 // A reference to the map index which should be set or deleted.
1208 Expression* map_index_;
1209 // The value to add to the map.
1210 Expression* val_;
1211 // Whether or not to add the value.
1212 Expression* should_set_;
1215 // Traverse a map assignment.
1218 Map_assignment_statement::do_traverse(Traverse* traverse)
1220 if (this->traverse_expression(traverse, &this->map_index_) == TRAVERSE_EXIT
1221 || this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
1222 return TRAVERSE_EXIT;
1223 return this->traverse_expression(traverse, &this->should_set_);
1226 // Lower a map assignment to a function call.
1228 Statement*
1229 Map_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
1230 Statement_inserter*)
1232 Location loc = this->location();
1234 Map_index_expression* map_index = this->map_index_->map_index_expression();
1235 if (map_index == NULL)
1237 this->report_error(_("expected map index on left hand side"));
1238 return Statement::make_error_statement(loc);
1240 Map_type* map_type = map_index->get_map_type();
1241 if (map_type == NULL)
1242 return Statement::make_error_statement(loc);
1244 Block* b = new Block(enclosing, loc);
1246 // Evaluate the map first to get order of evaluation right.
1247 // map_temp := m // we are evaluating m[k] = v, p
1248 Temporary_statement* map_temp = Statement::make_temporary(map_type,
1249 map_index->map(),
1250 loc);
1251 b->add_statement(map_temp);
1253 // var key_temp MAP_KEY_TYPE = k
1254 Temporary_statement* key_temp =
1255 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1256 b->add_statement(key_temp);
1258 // var val_temp MAP_VAL_TYPE = v
1259 Temporary_statement* val_temp =
1260 Statement::make_temporary(map_type->val_type(), this->val_, loc);
1261 b->add_statement(val_temp);
1263 // var insert_temp bool = p
1264 Temporary_statement* insert_temp =
1265 Statement::make_temporary(Type::lookup_bool_type(), this->should_set_,
1266 loc);
1267 b->add_statement(insert_temp);
1269 // mapassign2(map_temp, &key_temp, &val_temp, p)
1270 Expression* p1 = Expression::make_temporary_reference(map_temp, loc);
1271 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
1272 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1273 ref = Expression::make_temporary_reference(val_temp, loc);
1274 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1275 Expression* p4 = Expression::make_temporary_reference(insert_temp, loc);
1276 Expression* call = Runtime::make_call(Runtime::MAPASSIGN2, loc, 4,
1277 p1, p2, p3, p4);
1278 Statement* s = Statement::make_statement(call, true);
1279 b->add_statement(s);
1281 return Statement::make_block_statement(b, loc);
1284 // Dump the AST representation for a map assignment statement.
1286 void
1287 Map_assignment_statement::do_dump_statement(
1288 Ast_dump_context* ast_dump_context) const
1290 ast_dump_context->print_indent();
1291 ast_dump_context->dump_expression(this->map_index_);
1292 ast_dump_context->ostream() << " = ";
1293 ast_dump_context->dump_expression(this->val_);
1294 ast_dump_context->ostream() << ", ";
1295 ast_dump_context->dump_expression(this->should_set_);
1296 ast_dump_context->ostream() << std::endl;
1299 // Make a statement which assigns a pair of entries to a map.
1301 Statement*
1302 Statement::make_map_assignment(Expression* map_index,
1303 Expression* val, Expression* should_set,
1304 Location location)
1306 return new Map_assignment_statement(map_index, val, should_set, location);
1309 // A tuple assignment from a receive statement.
1311 class Tuple_receive_assignment_statement : public Statement
1313 public:
1314 Tuple_receive_assignment_statement(Expression* val, Expression* closed,
1315 Expression* channel, Location location)
1316 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
1317 val_(val), closed_(closed), channel_(channel)
1320 protected:
1322 do_traverse(Traverse* traverse);
1324 bool
1325 do_traverse_assignments(Traverse_assignments*)
1326 { go_unreachable(); }
1328 Statement*
1329 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1331 Bstatement*
1332 do_get_backend(Translate_context*)
1333 { go_unreachable(); }
1335 void
1336 do_dump_statement(Ast_dump_context*) const;
1338 private:
1339 // Lvalue which receives the value from the channel.
1340 Expression* val_;
1341 // Lvalue which receives whether the channel is closed.
1342 Expression* closed_;
1343 // The channel on which we receive the value.
1344 Expression* channel_;
1347 // Traversal.
1350 Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
1352 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1353 || this->traverse_expression(traverse, &this->closed_) == TRAVERSE_EXIT)
1354 return TRAVERSE_EXIT;
1355 return this->traverse_expression(traverse, &this->channel_);
1358 // Lower to a function call.
1360 Statement*
1361 Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
1362 Block* enclosing,
1363 Statement_inserter*)
1365 Location loc = this->location();
1367 Channel_type* channel_type = this->channel_->type()->channel_type();
1368 if (channel_type == NULL)
1370 this->report_error(_("expected channel"));
1371 return Statement::make_error_statement(loc);
1373 if (!channel_type->may_receive())
1375 this->report_error(_("invalid receive on send-only channel"));
1376 return Statement::make_error_statement(loc);
1379 Block* b = new Block(enclosing, loc);
1381 // Make sure that any subexpressions on the left hand side are
1382 // evaluated in the right order.
1383 Move_ordered_evals moe(b);
1384 this->val_->traverse_subexpressions(&moe);
1385 this->closed_->traverse_subexpressions(&moe);
1387 // var val_temp ELEMENT_TYPE
1388 Temporary_statement* val_temp =
1389 Statement::make_temporary(channel_type->element_type(), NULL, loc);
1390 b->add_statement(val_temp);
1392 // var closed_temp bool
1393 Temporary_statement* closed_temp =
1394 Statement::make_temporary((this->closed_->type()->is_sink_type())
1395 ? Type::make_boolean_type()
1396 : this->closed_->type(),
1397 NULL, loc);
1398 b->add_statement(closed_temp);
1400 // closed_temp = chanrecv2(type, channel, &val_temp)
1401 Expression* td = Expression::make_type_descriptor(this->channel_->type(),
1402 loc);
1403 Temporary_reference_expression* ref =
1404 Expression::make_temporary_reference(val_temp, loc);
1405 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1406 Expression* call = Runtime::make_call(Runtime::CHANRECV2,
1407 loc, 3, td, this->channel_, p2);
1408 ref = Expression::make_temporary_reference(closed_temp, loc);
1409 ref->set_is_lvalue();
1410 Statement* s = Statement::make_assignment(ref, call, loc);
1411 b->add_statement(s);
1413 // val = val_temp
1414 ref = Expression::make_temporary_reference(val_temp, loc);
1415 s = Statement::make_assignment(this->val_, ref, loc);
1416 b->add_statement(s);
1418 // closed = closed_temp
1419 ref = Expression::make_temporary_reference(closed_temp, loc);
1420 s = Statement::make_assignment(this->closed_, ref, loc);
1421 b->add_statement(s);
1423 return Statement::make_block_statement(b, loc);
1426 // Dump the AST representation for a tuple receive statement.
1428 void
1429 Tuple_receive_assignment_statement::do_dump_statement(
1430 Ast_dump_context* ast_dump_context) const
1432 ast_dump_context->print_indent();
1433 ast_dump_context->dump_expression(this->val_);
1434 ast_dump_context->ostream() << ", ";
1435 ast_dump_context->dump_expression(this->closed_);
1436 ast_dump_context->ostream() << " <- ";
1437 ast_dump_context->dump_expression(this->channel_);
1438 ast_dump_context->ostream() << std::endl;
1441 // Make a nonblocking receive statement.
1443 Statement*
1444 Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
1445 Expression* channel,
1446 Location location)
1448 return new Tuple_receive_assignment_statement(val, closed, channel,
1449 location);
1452 // An assignment to a pair of values from a type guard. This is a
1453 // conditional type guard. v, ok = i.(type).
1455 class Tuple_type_guard_assignment_statement : public Statement
1457 public:
1458 Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
1459 Expression* expr, Type* type,
1460 Location location)
1461 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
1462 val_(val), ok_(ok), expr_(expr), type_(type)
1465 protected:
1467 do_traverse(Traverse*);
1469 bool
1470 do_traverse_assignments(Traverse_assignments*)
1471 { go_unreachable(); }
1473 Statement*
1474 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1476 Bstatement*
1477 do_get_backend(Translate_context*)
1478 { go_unreachable(); }
1480 void
1481 do_dump_statement(Ast_dump_context*) const;
1483 private:
1484 Call_expression*
1485 lower_to_type(Runtime::Function);
1487 void
1488 lower_to_object_type(Block*, Runtime::Function);
1490 // The variable which recieves the converted value.
1491 Expression* val_;
1492 // The variable which receives the indication of success.
1493 Expression* ok_;
1494 // The expression being converted.
1495 Expression* expr_;
1496 // The type to which the expression is being converted.
1497 Type* type_;
1500 // Traverse a type guard tuple assignment.
1503 Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
1505 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1506 || this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
1507 || this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
1508 return TRAVERSE_EXIT;
1509 return this->traverse_expression(traverse, &this->expr_);
1512 // Lower to a function call.
1514 Statement*
1515 Tuple_type_guard_assignment_statement::do_lower(Gogo*, Named_object*,
1516 Block* enclosing,
1517 Statement_inserter*)
1519 Location loc = this->location();
1521 Type* expr_type = this->expr_->type();
1522 if (expr_type->interface_type() == NULL)
1524 if (!expr_type->is_error() && !this->type_->is_error())
1525 this->report_error(_("type assertion only valid for interface types"));
1526 return Statement::make_error_statement(loc);
1529 Block* b = new Block(enclosing, loc);
1531 // Make sure that any subexpressions on the left hand side are
1532 // evaluated in the right order.
1533 Move_ordered_evals moe(b);
1534 this->val_->traverse_subexpressions(&moe);
1535 this->ok_->traverse_subexpressions(&moe);
1537 bool expr_is_empty = expr_type->interface_type()->is_empty();
1538 Call_expression* call;
1539 if (this->type_->interface_type() != NULL)
1541 if (this->type_->interface_type()->is_empty())
1542 call = Runtime::make_call((expr_is_empty
1543 ? Runtime::IFACEE2E2
1544 : Runtime::IFACEI2E2),
1545 loc, 1, this->expr_);
1546 else
1547 call = this->lower_to_type(expr_is_empty
1548 ? Runtime::IFACEE2I2
1549 : Runtime::IFACEI2I2);
1551 else if (this->type_->points_to() != NULL)
1552 call = this->lower_to_type(expr_is_empty
1553 ? Runtime::IFACEE2T2P
1554 : Runtime::IFACEI2T2P);
1555 else
1557 this->lower_to_object_type(b,
1558 (expr_is_empty
1559 ? Runtime::IFACEE2T2
1560 : Runtime::IFACEI2T2));
1561 call = NULL;
1564 if (call != NULL)
1566 Expression* res = Expression::make_call_result(call, 0);
1567 res = Expression::make_unsafe_cast(this->type_, res, loc);
1568 Statement* s = Statement::make_assignment(this->val_, res, loc);
1569 b->add_statement(s);
1571 res = Expression::make_call_result(call, 1);
1572 s = Statement::make_assignment(this->ok_, res, loc);
1573 b->add_statement(s);
1576 return Statement::make_block_statement(b, loc);
1579 // Lower a conversion to a non-empty interface type or a pointer type.
1581 Call_expression*
1582 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code)
1584 Location loc = this->location();
1585 return Runtime::make_call(code, loc, 2,
1586 Expression::make_type_descriptor(this->type_, loc),
1587 this->expr_);
1590 // Lower a conversion to a non-interface non-pointer type.
1592 void
1593 Tuple_type_guard_assignment_statement::lower_to_object_type(
1594 Block* b,
1595 Runtime::Function code)
1597 Location loc = this->location();
1599 // var val_temp TYPE
1600 Temporary_statement* val_temp = Statement::make_temporary(this->type_,
1601 NULL, loc);
1602 b->add_statement(val_temp);
1604 // ok = CODE(type_descriptor, expr, &val_temp)
1605 Expression* p1 = Expression::make_type_descriptor(this->type_, loc);
1606 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1607 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1608 Expression* call = Runtime::make_call(code, loc, 3, p1, this->expr_, p3);
1609 Statement* s = Statement::make_assignment(this->ok_, call, loc);
1610 b->add_statement(s);
1612 // val = val_temp
1613 ref = Expression::make_temporary_reference(val_temp, loc);
1614 s = Statement::make_assignment(this->val_, ref, loc);
1615 b->add_statement(s);
1618 // Dump the AST representation for a tuple type guard statement.
1620 void
1621 Tuple_type_guard_assignment_statement::do_dump_statement(
1622 Ast_dump_context* ast_dump_context) const
1624 ast_dump_context->print_indent();
1625 ast_dump_context->dump_expression(this->val_);
1626 ast_dump_context->ostream() << ", ";
1627 ast_dump_context->dump_expression(this->ok_);
1628 ast_dump_context->ostream() << " = ";
1629 ast_dump_context->dump_expression(this->expr_);
1630 ast_dump_context->ostream() << " . ";
1631 ast_dump_context->dump_type(this->type_);
1632 ast_dump_context->ostream() << std::endl;
1635 // Make an assignment from a type guard to a pair of variables.
1637 Statement*
1638 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
1639 Expression* expr, Type* type,
1640 Location location)
1642 return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
1643 location);
1646 // Class Expression_statement.
1648 // Constructor.
1650 Expression_statement::Expression_statement(Expression* expr, bool is_ignored)
1651 : Statement(STATEMENT_EXPRESSION, expr->location()),
1652 expr_(expr), is_ignored_(is_ignored)
1656 // Determine types.
1658 void
1659 Expression_statement::do_determine_types()
1661 this->expr_->determine_type_no_context();
1664 // Check the types of an expression statement. The only check we do
1665 // is to possibly give an error about discarding the value of the
1666 // expression.
1668 void
1669 Expression_statement::do_check_types(Gogo*)
1671 if (!this->is_ignored_)
1672 this->expr_->discarding_value();
1675 // An expression statement is only a terminating statement if it is
1676 // a call to panic.
1678 bool
1679 Expression_statement::do_may_fall_through() const
1681 const Call_expression* call = this->expr_->call_expression();
1682 if (call != NULL)
1684 const Expression* fn = call->fn();
1685 // panic is still an unknown named object.
1686 const Unknown_expression* ue = fn->unknown_expression();
1687 if (ue != NULL)
1689 Named_object* no = ue->named_object();
1691 if (no->is_unknown())
1692 no = no->unknown_value()->real_named_object();
1693 if (no != NULL)
1695 Function_type* fntype;
1696 if (no->is_function())
1697 fntype = no->func_value()->type();
1698 else if (no->is_function_declaration())
1699 fntype = no->func_declaration_value()->type();
1700 else
1701 fntype = NULL;
1703 // The builtin function panic does not return.
1704 if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
1705 return false;
1709 return true;
1712 // Convert to backend representation.
1714 Bstatement*
1715 Expression_statement::do_get_backend(Translate_context* context)
1717 Bexpression* bexpr = this->expr_->get_backend(context);
1718 return context->backend()->expression_statement(bexpr);
1721 // Dump the AST representation for an expression statement
1723 void
1724 Expression_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
1725 const
1727 ast_dump_context->print_indent();
1728 ast_dump_context->dump_expression(expr_);
1729 ast_dump_context->ostream() << std::endl;
1732 // Make an expression statement from an Expression.
1734 Statement*
1735 Statement::make_statement(Expression* expr, bool is_ignored)
1737 return new Expression_statement(expr, is_ignored);
1740 // A block statement--a list of statements which may include variable
1741 // definitions.
1743 class Block_statement : public Statement
1745 public:
1746 Block_statement(Block* block, Location location)
1747 : Statement(STATEMENT_BLOCK, location),
1748 block_(block)
1751 protected:
1753 do_traverse(Traverse* traverse)
1754 { return this->block_->traverse(traverse); }
1756 void
1757 do_determine_types()
1758 { this->block_->determine_types(); }
1760 bool
1761 do_may_fall_through() const
1762 { return this->block_->may_fall_through(); }
1764 Bstatement*
1765 do_get_backend(Translate_context* context);
1767 void
1768 do_dump_statement(Ast_dump_context*) const;
1770 private:
1771 Block* block_;
1774 // Convert a block to the backend representation of a statement.
1776 Bstatement*
1777 Block_statement::do_get_backend(Translate_context* context)
1779 Bblock* bblock = this->block_->get_backend(context);
1780 return context->backend()->block_statement(bblock);
1783 // Dump the AST for a block statement
1785 void
1786 Block_statement::do_dump_statement(Ast_dump_context*) const
1788 // block statement braces are dumped when traversing.
1791 // Make a block statement.
1793 Statement*
1794 Statement::make_block_statement(Block* block, Location location)
1796 return new Block_statement(block, location);
1799 // An increment or decrement statement.
1801 class Inc_dec_statement : public Statement
1803 public:
1804 Inc_dec_statement(bool is_inc, Expression* expr)
1805 : Statement(STATEMENT_INCDEC, expr->location()),
1806 expr_(expr), is_inc_(is_inc)
1809 protected:
1811 do_traverse(Traverse* traverse)
1812 { return this->traverse_expression(traverse, &this->expr_); }
1814 bool
1815 do_traverse_assignments(Traverse_assignments*)
1816 { go_unreachable(); }
1818 Statement*
1819 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1821 Bstatement*
1822 do_get_backend(Translate_context*)
1823 { go_unreachable(); }
1825 void
1826 do_dump_statement(Ast_dump_context*) const;
1828 private:
1829 // The l-value to increment or decrement.
1830 Expression* expr_;
1831 // Whether to increment or decrement.
1832 bool is_inc_;
1835 // Lower to += or -=.
1837 Statement*
1838 Inc_dec_statement::do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
1840 Location loc = this->location();
1842 mpz_t oval;
1843 mpz_init_set_ui(oval, 1UL);
1844 Expression* oexpr = Expression::make_integer(&oval, this->expr_->type(), loc);
1845 mpz_clear(oval);
1847 Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
1848 return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
1851 // Dump the AST representation for a inc/dec statement.
1853 void
1854 Inc_dec_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
1856 ast_dump_context->print_indent();
1857 ast_dump_context->dump_expression(expr_);
1858 ast_dump_context->ostream() << (is_inc_? "++": "--") << std::endl;
1861 // Make an increment statement.
1863 Statement*
1864 Statement::make_inc_statement(Expression* expr)
1866 return new Inc_dec_statement(true, expr);
1869 // Make a decrement statement.
1871 Statement*
1872 Statement::make_dec_statement(Expression* expr)
1874 return new Inc_dec_statement(false, expr);
1877 // Class Thunk_statement. This is the base class for go and defer
1878 // statements.
1880 // Constructor.
1882 Thunk_statement::Thunk_statement(Statement_classification classification,
1883 Call_expression* call,
1884 Location location)
1885 : Statement(classification, location),
1886 call_(call), struct_type_(NULL)
1890 // Return whether this is a simple statement which does not require a
1891 // thunk.
1893 bool
1894 Thunk_statement::is_simple(Function_type* fntype) const
1896 // We need a thunk to call a method, or to pass a variable number of
1897 // arguments.
1898 if (fntype->is_method() || fntype->is_varargs())
1899 return false;
1901 // A defer statement requires a thunk to set up for whether the
1902 // function can call recover.
1903 if (this->classification() == STATEMENT_DEFER)
1904 return false;
1906 // We can only permit a single parameter of pointer type.
1907 const Typed_identifier_list* parameters = fntype->parameters();
1908 if (parameters != NULL
1909 && (parameters->size() > 1
1910 || (parameters->size() == 1
1911 && parameters->begin()->type()->points_to() == NULL)))
1912 return false;
1914 // If the function returns multiple values, or returns a type other
1915 // than integer, floating point, or pointer, then it may get a
1916 // hidden first parameter, in which case we need the more
1917 // complicated approach. This is true even though we are going to
1918 // ignore the return value.
1919 const Typed_identifier_list* results = fntype->results();
1920 if (results != NULL
1921 && (results->size() > 1
1922 || (results->size() == 1
1923 && !results->begin()->type()->is_basic_type()
1924 && results->begin()->type()->points_to() == NULL)))
1925 return false;
1927 // If this calls something that is not a simple function, then we
1928 // need a thunk.
1929 Expression* fn = this->call_->call_expression()->fn();
1930 if (fn->func_expression() == NULL)
1931 return false;
1933 // If the function uses a closure, then we need a thunk. FIXME: We
1934 // could accept a zero argument function with a closure.
1935 if (fn->func_expression()->closure() != NULL)
1936 return false;
1938 return true;
1941 // Traverse a thunk statement.
1944 Thunk_statement::do_traverse(Traverse* traverse)
1946 return this->traverse_expression(traverse, &this->call_);
1949 // We implement traverse_assignment for a thunk statement because it
1950 // effectively copies the function call.
1952 bool
1953 Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
1955 Expression* fn = this->call_->call_expression()->fn();
1956 Expression* fn2 = fn;
1957 tassign->value(&fn2, true, false);
1958 return true;
1961 // Determine types in a thunk statement.
1963 void
1964 Thunk_statement::do_determine_types()
1966 this->call_->determine_type_no_context();
1968 // Now that we know the types of the call, build the struct used to
1969 // pass parameters.
1970 Call_expression* ce = this->call_->call_expression();
1971 if (ce == NULL)
1972 return;
1973 Function_type* fntype = ce->get_function_type();
1974 if (fntype != NULL && !this->is_simple(fntype))
1975 this->struct_type_ = this->build_struct(fntype);
1978 // Check types in a thunk statement.
1980 void
1981 Thunk_statement::do_check_types(Gogo*)
1983 if (!this->call_->discarding_value())
1984 return;
1985 Call_expression* ce = this->call_->call_expression();
1986 if (ce == NULL)
1988 if (!this->call_->is_error_expression())
1989 this->report_error("expected call expression");
1990 return;
1994 // The Traverse class used to find and simplify thunk statements.
1996 class Simplify_thunk_traverse : public Traverse
1998 public:
1999 Simplify_thunk_traverse(Gogo* gogo)
2000 : Traverse(traverse_functions | traverse_blocks),
2001 gogo_(gogo), function_(NULL)
2005 function(Named_object*);
2008 block(Block*);
2010 private:
2011 // General IR.
2012 Gogo* gogo_;
2013 // The function we are traversing.
2014 Named_object* function_;
2017 // Keep track of the current function while looking for thunks.
2020 Simplify_thunk_traverse::function(Named_object* no)
2022 go_assert(this->function_ == NULL);
2023 this->function_ = no;
2024 int t = no->func_value()->traverse(this);
2025 this->function_ = NULL;
2026 if (t == TRAVERSE_EXIT)
2027 return t;
2028 return TRAVERSE_SKIP_COMPONENTS;
2031 // Look for thunks in a block.
2034 Simplify_thunk_traverse::block(Block* b)
2036 // The parser ensures that thunk statements always appear at the end
2037 // of a block.
2038 if (b->statements()->size() < 1)
2039 return TRAVERSE_CONTINUE;
2040 Thunk_statement* stat = b->statements()->back()->thunk_statement();
2041 if (stat == NULL)
2042 return TRAVERSE_CONTINUE;
2043 if (stat->simplify_statement(this->gogo_, this->function_, b))
2044 return TRAVERSE_SKIP_COMPONENTS;
2045 return TRAVERSE_CONTINUE;
2048 // Simplify all thunk statements.
2050 void
2051 Gogo::simplify_thunk_statements()
2053 Simplify_thunk_traverse thunk_traverse(this);
2054 this->traverse(&thunk_traverse);
2057 // Return true if the thunk function is a constant, which means that
2058 // it does not need to be passed to the thunk routine.
2060 bool
2061 Thunk_statement::is_constant_function() const
2063 Call_expression* ce = this->call_->call_expression();
2064 Function_type* fntype = ce->get_function_type();
2065 if (fntype == NULL)
2067 go_assert(saw_errors());
2068 return false;
2070 if (fntype->is_builtin())
2071 return true;
2072 Expression* fn = ce->fn();
2073 if (fn->func_expression() != NULL)
2074 return fn->func_expression()->closure() == NULL;
2075 if (fn->interface_field_reference_expression() != NULL)
2076 return true;
2077 return false;
2080 // Simplify complex thunk statements into simple ones. A complicated
2081 // thunk statement is one which takes anything other than zero
2082 // parameters or a single pointer parameter. We rewrite it into code
2083 // which allocates a struct, stores the parameter values into the
2084 // struct, and does a simple go or defer statement which passes the
2085 // struct to a thunk. The thunk does the real call.
2087 bool
2088 Thunk_statement::simplify_statement(Gogo* gogo, Named_object* function,
2089 Block* block)
2091 if (this->classification() == STATEMENT_ERROR)
2092 return false;
2093 if (this->call_->is_error_expression())
2094 return false;
2096 if (this->classification() == STATEMENT_DEFER)
2098 // Make sure that the defer stack exists for the function. We
2099 // will use when converting this statement to the backend
2100 // representation, but we want it to exist when we start
2101 // converting the function.
2102 function->func_value()->defer_stack(this->location());
2105 Call_expression* ce = this->call_->call_expression();
2106 Function_type* fntype = ce->get_function_type();
2107 if (fntype == NULL)
2109 go_assert(saw_errors());
2110 this->set_is_error();
2111 return false;
2113 if (this->is_simple(fntype))
2114 return false;
2116 Expression* fn = ce->fn();
2117 Interface_field_reference_expression* interface_method =
2118 fn->interface_field_reference_expression();
2120 Location location = this->location();
2122 std::string thunk_name = Gogo::thunk_name();
2124 // Build the thunk.
2125 this->build_thunk(gogo, thunk_name);
2127 // Generate code to call the thunk.
2129 // Get the values to store into the struct which is the single
2130 // argument to the thunk.
2132 Expression_list* vals = new Expression_list();
2133 if (!this->is_constant_function())
2134 vals->push_back(fn);
2136 if (interface_method != NULL)
2137 vals->push_back(interface_method->expr());
2139 if (ce->args() != NULL)
2141 for (Expression_list::const_iterator p = ce->args()->begin();
2142 p != ce->args()->end();
2143 ++p)
2145 if ((*p)->is_constant())
2146 continue;
2147 vals->push_back(*p);
2151 // Build the struct.
2152 Expression* constructor =
2153 Expression::make_struct_composite_literal(this->struct_type_, vals,
2154 location);
2156 // Allocate the initialized struct on the heap.
2157 constructor = Expression::make_heap_expression(constructor, location);
2159 // Look up the thunk.
2160 Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
2161 go_assert(named_thunk != NULL && named_thunk->is_function());
2163 // Build the call.
2164 Expression* func = Expression::make_func_reference(named_thunk, NULL,
2165 location);
2166 Expression_list* params = new Expression_list();
2167 params->push_back(constructor);
2168 Call_expression* call = Expression::make_call(func, params, false, location);
2170 // Build the simple go or defer statement.
2171 Statement* s;
2172 if (this->classification() == STATEMENT_GO)
2173 s = Statement::make_go_statement(call, location);
2174 else if (this->classification() == STATEMENT_DEFER)
2175 s = Statement::make_defer_statement(call, location);
2176 else
2177 go_unreachable();
2179 // The current block should end with the go statement.
2180 go_assert(block->statements()->size() >= 1);
2181 go_assert(block->statements()->back() == this);
2182 block->replace_statement(block->statements()->size() - 1, s);
2184 // We already ran the determine_types pass, so we need to run it now
2185 // for the new statement.
2186 s->determine_types();
2188 // Sanity check.
2189 gogo->check_types_in_block(block);
2191 // Return true to tell the block not to keep looking at statements.
2192 return true;
2195 // Set the name to use for thunk parameter N.
2197 void
2198 Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
2200 snprintf(buf, buflen, "a%d", n);
2203 // Build a new struct type to hold the parameters for a complicated
2204 // thunk statement. FNTYPE is the type of the function call.
2206 Struct_type*
2207 Thunk_statement::build_struct(Function_type* fntype)
2209 Location location = this->location();
2211 Struct_field_list* fields = new Struct_field_list();
2213 Call_expression* ce = this->call_->call_expression();
2214 Expression* fn = ce->fn();
2216 if (!this->is_constant_function())
2218 // The function to call.
2219 fields->push_back(Struct_field(Typed_identifier("fn", fntype,
2220 location)));
2223 // If this thunk statement calls a method on an interface, we pass
2224 // the interface object to the thunk.
2225 Interface_field_reference_expression* interface_method =
2226 fn->interface_field_reference_expression();
2227 if (interface_method != NULL)
2229 Typed_identifier tid("object", interface_method->expr()->type(),
2230 location);
2231 fields->push_back(Struct_field(tid));
2234 // The predeclared recover function has no argument. However, we
2235 // add an argument when building recover thunks. Handle that here.
2236 if (ce->is_recover_call())
2238 fields->push_back(Struct_field(Typed_identifier("can_recover",
2239 Type::lookup_bool_type(),
2240 location)));
2243 const Expression_list* args = ce->args();
2244 if (args != NULL)
2246 int i = 0;
2247 for (Expression_list::const_iterator p = args->begin();
2248 p != args->end();
2249 ++p, ++i)
2251 if ((*p)->is_constant())
2252 continue;
2254 char buf[50];
2255 this->thunk_field_param(i, buf, sizeof buf);
2256 fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
2257 location)));
2261 return Type::make_struct_type(fields, location);
2264 // Build the thunk we are going to call. This is a brand new, albeit
2265 // artificial, function.
2267 void
2268 Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name)
2270 Location location = this->location();
2272 Call_expression* ce = this->call_->call_expression();
2274 bool may_call_recover = false;
2275 if (this->classification() == STATEMENT_DEFER)
2277 Func_expression* fn = ce->fn()->func_expression();
2278 if (fn == NULL)
2279 may_call_recover = true;
2280 else
2282 const Named_object* no = fn->named_object();
2283 if (!no->is_function())
2284 may_call_recover = true;
2285 else
2286 may_call_recover = no->func_value()->calls_recover();
2290 // Build the type of the thunk. The thunk takes a single parameter,
2291 // which is a pointer to the special structure we build.
2292 const char* const parameter_name = "__go_thunk_parameter";
2293 Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
2294 Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
2295 thunk_parameters->push_back(Typed_identifier(parameter_name,
2296 pointer_to_struct_type,
2297 location));
2299 Typed_identifier_list* thunk_results = NULL;
2300 if (may_call_recover)
2302 // When deferring a function which may call recover, add a
2303 // return value, to disable tail call optimizations which will
2304 // break the way we check whether recover is permitted.
2305 thunk_results = new Typed_identifier_list();
2306 thunk_results->push_back(Typed_identifier("", Type::lookup_bool_type(),
2307 location));
2310 Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
2311 thunk_results,
2312 location);
2314 // Start building the thunk.
2315 Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
2316 location);
2318 gogo->start_block(location);
2320 // For a defer statement, start with a call to
2321 // __go_set_defer_retaddr. */
2322 Label* retaddr_label = NULL;
2323 if (may_call_recover)
2325 retaddr_label = gogo->add_label_reference("retaddr", location, false);
2326 Expression* arg = Expression::make_label_addr(retaddr_label, location);
2327 Expression* call = Runtime::make_call(Runtime::SET_DEFER_RETADDR,
2328 location, 1, arg);
2330 // This is a hack to prevent the middle-end from deleting the
2331 // label.
2332 gogo->start_block(location);
2333 gogo->add_statement(Statement::make_goto_statement(retaddr_label,
2334 location));
2335 Block* then_block = gogo->finish_block(location);
2336 then_block->determine_types();
2338 Statement* s = Statement::make_if_statement(call, then_block, NULL,
2339 location);
2340 s->determine_types();
2341 gogo->add_statement(s);
2343 function->func_value()->set_calls_defer_retaddr();
2346 // Get a reference to the parameter.
2347 Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
2348 go_assert(named_parameter != NULL && named_parameter->is_variable());
2350 // Build the call. Note that the field names are the same as the
2351 // ones used in build_struct.
2352 Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
2353 location);
2354 thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
2355 location);
2357 Interface_field_reference_expression* interface_method =
2358 ce->fn()->interface_field_reference_expression();
2360 Expression* func_to_call;
2361 unsigned int next_index;
2362 if (this->is_constant_function())
2364 func_to_call = ce->fn();
2365 next_index = 0;
2367 else
2369 func_to_call = Expression::make_field_reference(thunk_parameter,
2370 0, location);
2371 next_index = 1;
2374 if (interface_method != NULL)
2376 // The main program passes the interface object.
2377 go_assert(next_index == 0);
2378 Expression* r = Expression::make_field_reference(thunk_parameter, 0,
2379 location);
2380 const std::string& name(interface_method->name());
2381 func_to_call = Expression::make_interface_field_reference(r, name,
2382 location);
2383 next_index = 1;
2386 Expression_list* call_params = new Expression_list();
2387 const Struct_field_list* fields = this->struct_type_->fields();
2388 Struct_field_list::const_iterator p = fields->begin();
2389 for (unsigned int i = 0; i < next_index; ++i)
2390 ++p;
2391 bool is_recover_call = ce->is_recover_call();
2392 Expression* recover_arg = NULL;
2394 const Expression_list* args = ce->args();
2395 if (args != NULL)
2397 for (Expression_list::const_iterator arg = args->begin();
2398 arg != args->end();
2399 ++arg)
2401 Expression* param;
2402 if ((*arg)->is_constant())
2403 param = *arg;
2404 else
2406 Expression* thunk_param =
2407 Expression::make_var_reference(named_parameter, location);
2408 thunk_param =
2409 Expression::make_unary(OPERATOR_MULT, thunk_param, location);
2410 param = Expression::make_field_reference(thunk_param,
2411 next_index,
2412 location);
2413 ++next_index;
2416 if (!is_recover_call)
2417 call_params->push_back(param);
2418 else
2420 go_assert(call_params->empty());
2421 recover_arg = param;
2426 if (call_params->empty())
2428 delete call_params;
2429 call_params = NULL;
2432 Call_expression* call = Expression::make_call(func_to_call, call_params,
2433 false, location);
2435 // This call expression was already lowered before entering the
2436 // thunk statement. Don't try to lower varargs again, as that will
2437 // cause confusion for, e.g., method calls which already have a
2438 // receiver parameter.
2439 call->set_varargs_are_lowered();
2441 Statement* call_statement = Statement::make_statement(call, true);
2443 gogo->add_statement(call_statement);
2445 // If this is a defer statement, the label comes immediately after
2446 // the call.
2447 if (may_call_recover)
2449 gogo->add_label_definition("retaddr", location);
2451 Expression_list* vals = new Expression_list();
2452 vals->push_back(Expression::make_boolean(false, location));
2453 gogo->add_statement(Statement::make_return_statement(vals, location));
2456 Block* b = gogo->finish_block(location);
2458 gogo->add_block(b, location);
2460 gogo->lower_block(function, b);
2461 gogo->flatten_block(function, b);
2463 // We already ran the determine_types pass, so we need to run it
2464 // just for the call statement now. The other types are known.
2465 call_statement->determine_types();
2467 if (may_call_recover || recover_arg != NULL)
2469 // Dig up the call expression, which may have been changed
2470 // during lowering.
2471 go_assert(call_statement->classification() == STATEMENT_EXPRESSION);
2472 Expression_statement* es =
2473 static_cast<Expression_statement*>(call_statement);
2474 Call_expression* ce = es->expr()->call_expression();
2475 if (ce == NULL)
2476 go_assert(saw_errors());
2477 else
2479 if (may_call_recover)
2480 ce->set_is_deferred();
2481 if (recover_arg != NULL)
2482 ce->set_recover_arg(recover_arg);
2486 // That is all the thunk has to do.
2487 gogo->finish_function(location);
2490 // Get the function and argument expressions.
2492 bool
2493 Thunk_statement::get_fn_and_arg(Expression** pfn, Expression** parg)
2495 if (this->call_->is_error_expression())
2496 return false;
2498 Call_expression* ce = this->call_->call_expression();
2500 Expression* fn = ce->fn();
2501 Func_expression* fe = fn->func_expression();
2502 go_assert(fe != NULL);
2503 *pfn = Expression::make_func_code_reference(fe->named_object(),
2504 fe->location());
2506 const Expression_list* args = ce->args();
2507 if (args == NULL || args->empty())
2508 *parg = Expression::make_nil(this->location());
2509 else
2511 go_assert(args->size() == 1);
2512 *parg = args->front();
2515 return true;
2518 // Class Go_statement.
2520 Bstatement*
2521 Go_statement::do_get_backend(Translate_context* context)
2523 Expression* fn;
2524 Expression* arg;
2525 if (!this->get_fn_and_arg(&fn, &arg))
2526 return context->backend()->error_statement();
2528 Expression* call = Runtime::make_call(Runtime::GO, this->location(), 2,
2529 fn, arg);
2530 Bexpression* bcall = call->get_backend(context);
2531 return context->backend()->expression_statement(bcall);
2534 // Dump the AST representation for go statement.
2536 void
2537 Go_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2539 ast_dump_context->print_indent();
2540 ast_dump_context->ostream() << "go ";
2541 ast_dump_context->dump_expression(this->call());
2542 ast_dump_context->ostream() << std::endl;
2545 // Make a go statement.
2547 Statement*
2548 Statement::make_go_statement(Call_expression* call, Location location)
2550 return new Go_statement(call, location);
2553 // Class Defer_statement.
2555 Bstatement*
2556 Defer_statement::do_get_backend(Translate_context* context)
2558 Expression* fn;
2559 Expression* arg;
2560 if (!this->get_fn_and_arg(&fn, &arg))
2561 return context->backend()->error_statement();
2563 Location loc = this->location();
2564 Expression* ds = context->function()->func_value()->defer_stack(loc);
2566 Expression* call = Runtime::make_call(Runtime::DEFER, loc, 3,
2567 ds, fn, arg);
2568 Bexpression* bcall = call->get_backend(context);
2569 return context->backend()->expression_statement(bcall);
2572 // Dump the AST representation for defer statement.
2574 void
2575 Defer_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2577 ast_dump_context->print_indent();
2578 ast_dump_context->ostream() << "defer ";
2579 ast_dump_context->dump_expression(this->call());
2580 ast_dump_context->ostream() << std::endl;
2583 // Make a defer statement.
2585 Statement*
2586 Statement::make_defer_statement(Call_expression* call,
2587 Location location)
2589 return new Defer_statement(call, location);
2592 // Class Return_statement.
2594 // Traverse assignments. We treat each return value as a top level
2595 // RHS in an expression.
2597 bool
2598 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2600 Expression_list* vals = this->vals_;
2601 if (vals != NULL)
2603 for (Expression_list::iterator p = vals->begin();
2604 p != vals->end();
2605 ++p)
2606 tassign->value(&*p, true, true);
2608 return true;
2611 // Lower a return statement. If we are returning a function call
2612 // which returns multiple values which match the current function,
2613 // split up the call's results. If the return statement lists
2614 // explicit values, implement this statement by assigning the values
2615 // to the result variables and change this statement to a naked
2616 // return. This lets panic/recover work correctly.
2618 Statement*
2619 Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
2620 Statement_inserter*)
2622 if (this->is_lowered_)
2623 return this;
2625 Expression_list* vals = this->vals_;
2626 this->vals_ = NULL;
2627 this->is_lowered_ = true;
2629 Location loc = this->location();
2631 size_t vals_count = vals == NULL ? 0 : vals->size();
2632 Function::Results* results = function->func_value()->result_variables();
2633 size_t results_count = results == NULL ? 0 : results->size();
2635 if (vals_count == 0)
2637 if (results_count > 0 && !function->func_value()->results_are_named())
2639 this->report_error(_("not enough arguments to return"));
2640 return this;
2642 return this;
2645 if (results_count == 0)
2647 this->report_error(_("return with value in function "
2648 "with no return type"));
2649 return this;
2652 // If the current function has multiple return values, and we are
2653 // returning a single call expression, split up the call expression.
2654 if (results_count > 1
2655 && vals->size() == 1
2656 && vals->front()->call_expression() != NULL)
2658 Call_expression* call = vals->front()->call_expression();
2659 call->set_expected_result_count(results_count);
2660 delete vals;
2661 vals = new Expression_list;
2662 for (size_t i = 0; i < results_count; ++i)
2663 vals->push_back(Expression::make_call_result(call, i));
2664 vals_count = results_count;
2667 if (vals_count < results_count)
2669 this->report_error(_("not enough arguments to return"));
2670 return this;
2673 if (vals_count > results_count)
2675 this->report_error(_("too many values in return statement"));
2676 return this;
2679 Block* b = new Block(enclosing, loc);
2681 Expression_list* lhs = new Expression_list();
2682 Expression_list* rhs = new Expression_list();
2684 Expression_list::const_iterator pe = vals->begin();
2685 int i = 1;
2686 for (Function::Results::const_iterator pr = results->begin();
2687 pr != results->end();
2688 ++pr, ++pe, ++i)
2690 Named_object* rv = *pr;
2691 Expression* e = *pe;
2693 // Check types now so that we give a good error message. The
2694 // result type is known. We determine the expression type
2695 // early.
2697 Type *rvtype = rv->result_var_value()->type();
2698 Type_context type_context(rvtype, false);
2699 e->determine_type(&type_context);
2701 std::string reason;
2702 if (Type::are_assignable(rvtype, e->type(), &reason))
2704 Expression* ve = Expression::make_var_reference(rv, e->location());
2705 lhs->push_back(ve);
2706 rhs->push_back(e);
2708 else
2710 if (reason.empty())
2711 error_at(e->location(), "incompatible type for return value %d", i);
2712 else
2713 error_at(e->location(),
2714 "incompatible type for return value %d (%s)",
2715 i, reason.c_str());
2718 go_assert(lhs->size() == rhs->size());
2720 if (lhs->empty())
2722 else if (lhs->size() == 1)
2724 b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
2725 loc));
2726 delete lhs;
2727 delete rhs;
2729 else
2730 b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
2732 b->add_statement(this);
2734 delete vals;
2736 return Statement::make_block_statement(b, loc);
2739 // Convert a return statement to the backend representation.
2741 Bstatement*
2742 Return_statement::do_get_backend(Translate_context* context)
2744 Location loc = this->location();
2746 Function* function = context->function()->func_value();
2747 Function::Results* results = function->result_variables();
2748 std::vector<Bexpression*> retvals;
2749 if (results != NULL && !results->empty())
2751 retvals.reserve(results->size());
2752 for (Function::Results::const_iterator p = results->begin();
2753 p != results->end();
2754 p++)
2756 Expression* vr = Expression::make_var_reference(*p, loc);
2757 retvals.push_back(vr->get_backend(context));
2761 return context->backend()->return_statement(function->get_decl(),
2762 retvals, loc);
2765 // Dump the AST representation for a return statement.
2767 void
2768 Return_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2770 ast_dump_context->print_indent();
2771 ast_dump_context->ostream() << "return " ;
2772 ast_dump_context->dump_expression_list(this->vals_);
2773 ast_dump_context->ostream() << std::endl;
2776 // Make a return statement.
2778 Return_statement*
2779 Statement::make_return_statement(Expression_list* vals,
2780 Location location)
2782 return new Return_statement(vals, location);
2785 // Make a statement that returns the result of a call expression.
2787 Statement*
2788 Statement::make_return_from_call(Call_expression* call, Location location)
2790 size_t rc = call->result_count();
2791 if (rc == 0)
2792 return Statement::make_statement(call, true);
2793 else
2795 Expression_list* vals = new Expression_list();
2796 if (rc == 1)
2797 vals->push_back(call);
2798 else
2800 for (size_t i = 0; i < rc; ++i)
2801 vals->push_back(Expression::make_call_result(call, i));
2803 return Statement::make_return_statement(vals, location);
2807 // A break or continue statement.
2809 class Bc_statement : public Statement
2811 public:
2812 Bc_statement(bool is_break, Unnamed_label* label, Location location)
2813 : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
2814 label_(label), is_break_(is_break)
2817 bool
2818 is_break() const
2819 { return this->is_break_; }
2821 protected:
2823 do_traverse(Traverse*)
2824 { return TRAVERSE_CONTINUE; }
2826 bool
2827 do_may_fall_through() const
2828 { return false; }
2830 Bstatement*
2831 do_get_backend(Translate_context* context)
2832 { return this->label_->get_goto(context, this->location()); }
2834 void
2835 do_dump_statement(Ast_dump_context*) const;
2837 private:
2838 // The label that this branches to.
2839 Unnamed_label* label_;
2840 // True if this is "break", false if it is "continue".
2841 bool is_break_;
2844 // Dump the AST representation for a break/continue statement
2846 void
2847 Bc_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2849 ast_dump_context->print_indent();
2850 ast_dump_context->ostream() << (this->is_break_ ? "break" : "continue");
2851 if (this->label_ != NULL)
2853 ast_dump_context->ostream() << " ";
2854 ast_dump_context->dump_label_name(this->label_);
2856 ast_dump_context->ostream() << std::endl;
2859 // Make a break statement.
2861 Statement*
2862 Statement::make_break_statement(Unnamed_label* label, Location location)
2864 return new Bc_statement(true, label, location);
2867 // Make a continue statement.
2869 Statement*
2870 Statement::make_continue_statement(Unnamed_label* label,
2871 Location location)
2873 return new Bc_statement(false, label, location);
2876 // A goto statement.
2878 class Goto_statement : public Statement
2880 public:
2881 Goto_statement(Label* label, Location location)
2882 : Statement(STATEMENT_GOTO, location),
2883 label_(label)
2886 protected:
2888 do_traverse(Traverse*)
2889 { return TRAVERSE_CONTINUE; }
2891 void
2892 do_check_types(Gogo*);
2894 bool
2895 do_may_fall_through() const
2896 { return false; }
2898 Bstatement*
2899 do_get_backend(Translate_context*);
2901 void
2902 do_dump_statement(Ast_dump_context*) const;
2904 private:
2905 Label* label_;
2908 // Check types for a label. There aren't any types per se, but we use
2909 // this to give an error if the label was never defined.
2911 void
2912 Goto_statement::do_check_types(Gogo*)
2914 if (!this->label_->is_defined())
2916 error_at(this->location(), "reference to undefined label %qs",
2917 Gogo::message_name(this->label_->name()).c_str());
2918 this->set_is_error();
2922 // Convert the goto statement to the backend representation.
2924 Bstatement*
2925 Goto_statement::do_get_backend(Translate_context* context)
2927 Blabel* blabel = this->label_->get_backend_label(context);
2928 return context->backend()->goto_statement(blabel, this->location());
2931 // Dump the AST representation for a goto statement.
2933 void
2934 Goto_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2936 ast_dump_context->print_indent();
2937 ast_dump_context->ostream() << "goto " << this->label_->name() << std::endl;
2940 // Make a goto statement.
2942 Statement*
2943 Statement::make_goto_statement(Label* label, Location location)
2945 return new Goto_statement(label, location);
2948 // A goto statement to an unnamed label.
2950 class Goto_unnamed_statement : public Statement
2952 public:
2953 Goto_unnamed_statement(Unnamed_label* label, Location location)
2954 : Statement(STATEMENT_GOTO_UNNAMED, location),
2955 label_(label)
2958 protected:
2960 do_traverse(Traverse*)
2961 { return TRAVERSE_CONTINUE; }
2963 bool
2964 do_may_fall_through() const
2965 { return false; }
2967 Bstatement*
2968 do_get_backend(Translate_context* context)
2969 { return this->label_->get_goto(context, this->location()); }
2971 void
2972 do_dump_statement(Ast_dump_context*) const;
2974 private:
2975 Unnamed_label* label_;
2978 // Dump the AST representation for an unnamed goto statement
2980 void
2981 Goto_unnamed_statement::do_dump_statement(
2982 Ast_dump_context* ast_dump_context) const
2984 ast_dump_context->print_indent();
2985 ast_dump_context->ostream() << "goto ";
2986 ast_dump_context->dump_label_name(this->label_);
2987 ast_dump_context->ostream() << std::endl;
2990 // Make a goto statement to an unnamed label.
2992 Statement*
2993 Statement::make_goto_unnamed_statement(Unnamed_label* label,
2994 Location location)
2996 return new Goto_unnamed_statement(label, location);
2999 // Class Label_statement.
3001 // Traversal.
3004 Label_statement::do_traverse(Traverse*)
3006 return TRAVERSE_CONTINUE;
3009 // Return the backend representation of the statement defining this
3010 // label.
3012 Bstatement*
3013 Label_statement::do_get_backend(Translate_context* context)
3015 Blabel* blabel = this->label_->get_backend_label(context);
3016 return context->backend()->label_definition_statement(blabel);
3019 // Dump the AST for a label definition statement.
3021 void
3022 Label_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3024 ast_dump_context->print_indent();
3025 ast_dump_context->ostream() << this->label_->name() << ":" << std::endl;
3028 // Make a label statement.
3030 Statement*
3031 Statement::make_label_statement(Label* label, Location location)
3033 return new Label_statement(label, location);
3036 // An unnamed label statement.
3038 class Unnamed_label_statement : public Statement
3040 public:
3041 Unnamed_label_statement(Unnamed_label* label)
3042 : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
3043 label_(label)
3046 protected:
3048 do_traverse(Traverse*)
3049 { return TRAVERSE_CONTINUE; }
3051 Bstatement*
3052 do_get_backend(Translate_context* context)
3053 { return this->label_->get_definition(context); }
3055 void
3056 do_dump_statement(Ast_dump_context*) const;
3058 private:
3059 // The label.
3060 Unnamed_label* label_;
3063 // Dump the AST representation for an unnamed label definition statement.
3065 void
3066 Unnamed_label_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
3067 const
3069 ast_dump_context->print_indent();
3070 ast_dump_context->dump_label_name(this->label_);
3071 ast_dump_context->ostream() << ":" << std::endl;
3074 // Make an unnamed label statement.
3076 Statement*
3077 Statement::make_unnamed_label_statement(Unnamed_label* label)
3079 return new Unnamed_label_statement(label);
3082 // An if statement.
3084 class If_statement : public Statement
3086 public:
3087 If_statement(Expression* cond, Block* then_block, Block* else_block,
3088 Location location)
3089 : Statement(STATEMENT_IF, location),
3090 cond_(cond), then_block_(then_block), else_block_(else_block)
3093 protected:
3095 do_traverse(Traverse*);
3097 void
3098 do_determine_types();
3100 void
3101 do_check_types(Gogo*);
3103 bool
3104 do_may_fall_through() const;
3106 Bstatement*
3107 do_get_backend(Translate_context*);
3109 void
3110 do_dump_statement(Ast_dump_context*) const;
3112 private:
3113 Expression* cond_;
3114 Block* then_block_;
3115 Block* else_block_;
3118 // Traversal.
3121 If_statement::do_traverse(Traverse* traverse)
3123 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT
3124 || this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
3125 return TRAVERSE_EXIT;
3126 if (this->else_block_ != NULL)
3128 if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
3129 return TRAVERSE_EXIT;
3131 return TRAVERSE_CONTINUE;
3134 void
3135 If_statement::do_determine_types()
3137 Type_context context(Type::lookup_bool_type(), false);
3138 this->cond_->determine_type(&context);
3139 this->then_block_->determine_types();
3140 if (this->else_block_ != NULL)
3141 this->else_block_->determine_types();
3144 // Check types.
3146 void
3147 If_statement::do_check_types(Gogo*)
3149 Type* type = this->cond_->type();
3150 if (type->is_error())
3151 this->set_is_error();
3152 else if (!type->is_boolean_type())
3153 this->report_error(_("expected boolean expression"));
3156 // Whether the overall statement may fall through.
3158 bool
3159 If_statement::do_may_fall_through() const
3161 return (this->else_block_ == NULL
3162 || this->then_block_->may_fall_through()
3163 || this->else_block_->may_fall_through());
3166 // Get the backend representation.
3168 Bstatement*
3169 If_statement::do_get_backend(Translate_context* context)
3171 go_assert(this->cond_->type()->is_boolean_type()
3172 || this->cond_->type()->is_error());
3173 Bexpression* cond = this->cond_->get_backend(context);
3174 Bblock* then_block = this->then_block_->get_backend(context);
3175 Bblock* else_block = (this->else_block_ == NULL
3176 ? NULL
3177 : this->else_block_->get_backend(context));
3178 return context->backend()->if_statement(cond, then_block, else_block,
3179 this->location());
3182 // Dump the AST representation for an if statement
3184 void
3185 If_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3187 ast_dump_context->print_indent();
3188 ast_dump_context->ostream() << "if ";
3189 ast_dump_context->dump_expression(this->cond_);
3190 ast_dump_context->ostream() << std::endl;
3191 if (ast_dump_context->dump_subblocks())
3193 ast_dump_context->dump_block(this->then_block_);
3194 if (this->else_block_ != NULL)
3196 ast_dump_context->print_indent();
3197 ast_dump_context->ostream() << "else" << std::endl;
3198 ast_dump_context->dump_block(this->else_block_);
3203 // Make an if statement.
3205 Statement*
3206 Statement::make_if_statement(Expression* cond, Block* then_block,
3207 Block* else_block, Location location)
3209 return new If_statement(cond, then_block, else_block, location);
3212 // Class Case_clauses::Hash_integer_value.
3214 class Case_clauses::Hash_integer_value
3216 public:
3217 size_t
3218 operator()(Expression*) const;
3221 size_t
3222 Case_clauses::Hash_integer_value::operator()(Expression* pe) const
3224 Numeric_constant nc;
3225 mpz_t ival;
3226 if (!pe->numeric_constant_value(&nc) || !nc.to_int(&ival))
3227 go_unreachable();
3228 size_t ret = mpz_get_ui(ival);
3229 mpz_clear(ival);
3230 return ret;
3233 // Class Case_clauses::Eq_integer_value.
3235 class Case_clauses::Eq_integer_value
3237 public:
3238 bool
3239 operator()(Expression*, Expression*) const;
3242 bool
3243 Case_clauses::Eq_integer_value::operator()(Expression* a, Expression* b) const
3245 Numeric_constant anc;
3246 mpz_t aval;
3247 Numeric_constant bnc;
3248 mpz_t bval;
3249 if (!a->numeric_constant_value(&anc)
3250 || !anc.to_int(&aval)
3251 || !b->numeric_constant_value(&bnc)
3252 || !bnc.to_int(&bval))
3253 go_unreachable();
3254 bool ret = mpz_cmp(aval, bval) == 0;
3255 mpz_clear(aval);
3256 mpz_clear(bval);
3257 return ret;
3260 // Class Case_clauses::Case_clause.
3262 // Traversal.
3265 Case_clauses::Case_clause::traverse(Traverse* traverse)
3267 if (this->cases_ != NULL
3268 && (traverse->traverse_mask()
3269 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3271 if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
3272 return TRAVERSE_EXIT;
3274 if (this->statements_ != NULL)
3276 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3277 return TRAVERSE_EXIT;
3279 return TRAVERSE_CONTINUE;
3282 // Check whether all the case expressions are integer constants.
3284 bool
3285 Case_clauses::Case_clause::is_constant() const
3287 if (this->cases_ != NULL)
3289 for (Expression_list::const_iterator p = this->cases_->begin();
3290 p != this->cases_->end();
3291 ++p)
3292 if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
3293 return false;
3295 return true;
3298 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3299 // value we are switching on; it may be NULL. If START_LABEL is not
3300 // NULL, it goes at the start of the statements, after the condition
3301 // test. We branch to FINISH_LABEL at the end of the statements.
3303 void
3304 Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
3305 Unnamed_label* start_label,
3306 Unnamed_label* finish_label) const
3308 Location loc = this->location_;
3309 Unnamed_label* next_case_label;
3310 if (this->cases_ == NULL || this->cases_->empty())
3312 go_assert(this->is_default_);
3313 next_case_label = NULL;
3315 else
3317 Expression* cond = NULL;
3319 for (Expression_list::const_iterator p = this->cases_->begin();
3320 p != this->cases_->end();
3321 ++p)
3323 Expression* ref = Expression::make_temporary_reference(val_temp,
3324 loc);
3325 Expression* this_cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3326 *p, loc);
3327 if (cond == NULL)
3328 cond = this_cond;
3329 else
3330 cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
3333 Block* then_block = new Block(b, loc);
3334 next_case_label = new Unnamed_label(Linemap::unknown_location());
3335 Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
3336 loc);
3337 then_block->add_statement(s);
3339 // if !COND { goto NEXT_CASE_LABEL }
3340 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3341 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3342 b->add_statement(s);
3345 if (start_label != NULL)
3346 b->add_statement(Statement::make_unnamed_label_statement(start_label));
3348 if (this->statements_ != NULL)
3349 b->add_statement(Statement::make_block_statement(this->statements_, loc));
3351 Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
3352 b->add_statement(s);
3354 if (next_case_label != NULL)
3355 b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
3358 // Determine types.
3360 void
3361 Case_clauses::Case_clause::determine_types(Type* type)
3363 if (this->cases_ != NULL)
3365 Type_context case_context(type, false);
3366 for (Expression_list::iterator p = this->cases_->begin();
3367 p != this->cases_->end();
3368 ++p)
3369 (*p)->determine_type(&case_context);
3371 if (this->statements_ != NULL)
3372 this->statements_->determine_types();
3375 // Check types. Returns false if there was an error.
3377 bool
3378 Case_clauses::Case_clause::check_types(Type* type)
3380 if (this->cases_ != NULL)
3382 for (Expression_list::iterator p = this->cases_->begin();
3383 p != this->cases_->end();
3384 ++p)
3386 if (!Type::are_assignable(type, (*p)->type(), NULL)
3387 && !Type::are_assignable((*p)->type(), type, NULL))
3389 error_at((*p)->location(),
3390 "type mismatch between switch value and case clause");
3391 return false;
3395 return true;
3398 // Return true if this clause may fall through to the following
3399 // statements. Note that this is not the same as whether the case
3400 // uses the "fallthrough" keyword.
3402 bool
3403 Case_clauses::Case_clause::may_fall_through() const
3405 if (this->statements_ == NULL)
3406 return true;
3407 return this->statements_->may_fall_through();
3410 // Convert the case values and statements to the backend
3411 // representation. BREAK_LABEL is the label which break statements
3412 // should branch to. CASE_CONSTANTS is used to detect duplicate
3413 // constants. *CASES should be passed as an empty vector; the values
3414 // for this case will be added to it. If this is the default case,
3415 // *CASES will remain empty. This returns the statement to execute if
3416 // one of these cases is selected.
3418 Bstatement*
3419 Case_clauses::Case_clause::get_backend(Translate_context* context,
3420 Unnamed_label* break_label,
3421 Case_constants* case_constants,
3422 std::vector<Bexpression*>* cases) const
3424 if (this->cases_ != NULL)
3426 go_assert(!this->is_default_);
3427 for (Expression_list::const_iterator p = this->cases_->begin();
3428 p != this->cases_->end();
3429 ++p)
3431 Expression* e = *p;
3432 if (e->classification() != Expression::EXPRESSION_INTEGER)
3434 Numeric_constant nc;
3435 mpz_t ival;
3436 if (!(*p)->numeric_constant_value(&nc) || !nc.to_int(&ival))
3438 // Something went wrong. This can happen with a
3439 // negative constant and an unsigned switch value.
3440 go_assert(saw_errors());
3441 continue;
3443 go_assert(nc.type() != NULL);
3444 e = Expression::make_integer(&ival, nc.type(), e->location());
3445 mpz_clear(ival);
3448 std::pair<Case_constants::iterator, bool> ins =
3449 case_constants->insert(e);
3450 if (!ins.second)
3452 // Value was already present.
3453 error_at(this->location_, "duplicate case in switch");
3454 e = Expression::make_error(this->location_);
3456 cases->push_back(e->get_backend(context));
3460 Bstatement* statements;
3461 if (this->statements_ == NULL)
3462 statements = NULL;
3463 else
3465 Bblock* bblock = this->statements_->get_backend(context);
3466 statements = context->backend()->block_statement(bblock);
3469 Bstatement* break_stat;
3470 if (this->is_fallthrough_)
3471 break_stat = NULL;
3472 else
3473 break_stat = break_label->get_goto(context, this->location_);
3475 if (statements == NULL)
3476 return break_stat;
3477 else if (break_stat == NULL)
3478 return statements;
3479 else
3480 return context->backend()->compound_statement(statements, break_stat);
3483 // Dump the AST representation for a case clause
3485 void
3486 Case_clauses::Case_clause::dump_clause(Ast_dump_context* ast_dump_context)
3487 const
3489 ast_dump_context->print_indent();
3490 if (this->is_default_)
3492 ast_dump_context->ostream() << "default:";
3494 else
3496 ast_dump_context->ostream() << "case ";
3497 ast_dump_context->dump_expression_list(this->cases_);
3498 ast_dump_context->ostream() << ":" ;
3500 ast_dump_context->dump_block(this->statements_);
3501 if (this->is_fallthrough_)
3503 ast_dump_context->print_indent();
3504 ast_dump_context->ostream() << " (fallthrough)" << std::endl;
3508 // Class Case_clauses.
3510 // Traversal.
3513 Case_clauses::traverse(Traverse* traverse)
3515 for (Clauses::iterator p = this->clauses_.begin();
3516 p != this->clauses_.end();
3517 ++p)
3519 if (p->traverse(traverse) == TRAVERSE_EXIT)
3520 return TRAVERSE_EXIT;
3522 return TRAVERSE_CONTINUE;
3525 // Check whether all the case expressions are constant.
3527 bool
3528 Case_clauses::is_constant() const
3530 for (Clauses::const_iterator p = this->clauses_.begin();
3531 p != this->clauses_.end();
3532 ++p)
3533 if (!p->is_constant())
3534 return false;
3535 return true;
3538 // Lower case clauses for a nonconstant switch.
3540 void
3541 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3542 Unnamed_label* break_label) const
3544 // The default case.
3545 const Case_clause* default_case = NULL;
3547 // The label for the fallthrough of the previous case.
3548 Unnamed_label* last_fallthrough_label = NULL;
3550 // The label for the start of the default case. This is used if the
3551 // case before the default case falls through.
3552 Unnamed_label* default_start_label = NULL;
3554 // The label for the end of the default case. This normally winds
3555 // up as BREAK_LABEL, but it will be different if the default case
3556 // falls through.
3557 Unnamed_label* default_finish_label = NULL;
3559 for (Clauses::const_iterator p = this->clauses_.begin();
3560 p != this->clauses_.end();
3561 ++p)
3563 // The label to use for the start of the statements for this
3564 // case. This is NULL unless the previous case falls through.
3565 Unnamed_label* start_label = last_fallthrough_label;
3567 // The label to jump to after the end of the statements for this
3568 // case.
3569 Unnamed_label* finish_label = break_label;
3571 last_fallthrough_label = NULL;
3572 if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3574 finish_label = new Unnamed_label(p->location());
3575 last_fallthrough_label = finish_label;
3578 if (!p->is_default())
3579 p->lower(b, val_temp, start_label, finish_label);
3580 else
3582 // We have to move the default case to the end, so that we
3583 // only use it if all the other tests fail.
3584 default_case = &*p;
3585 default_start_label = start_label;
3586 default_finish_label = finish_label;
3590 if (default_case != NULL)
3591 default_case->lower(b, val_temp, default_start_label,
3592 default_finish_label);
3595 // Determine types.
3597 void
3598 Case_clauses::determine_types(Type* type)
3600 for (Clauses::iterator p = this->clauses_.begin();
3601 p != this->clauses_.end();
3602 ++p)
3603 p->determine_types(type);
3606 // Check types. Returns false if there was an error.
3608 bool
3609 Case_clauses::check_types(Type* type)
3611 bool ret = true;
3612 for (Clauses::iterator p = this->clauses_.begin();
3613 p != this->clauses_.end();
3614 ++p)
3616 if (!p->check_types(type))
3617 ret = false;
3619 return ret;
3622 // Return true if these clauses may fall through to the statements
3623 // following the switch statement.
3625 bool
3626 Case_clauses::may_fall_through() const
3628 bool found_default = false;
3629 for (Clauses::const_iterator p = this->clauses_.begin();
3630 p != this->clauses_.end();
3631 ++p)
3633 if (p->may_fall_through() && !p->is_fallthrough())
3634 return true;
3635 if (p->is_default())
3636 found_default = true;
3638 return !found_default;
3641 // Convert the cases to the backend representation. This sets
3642 // *ALL_CASES and *ALL_STATEMENTS.
3644 void
3645 Case_clauses::get_backend(Translate_context* context,
3646 Unnamed_label* break_label,
3647 std::vector<std::vector<Bexpression*> >* all_cases,
3648 std::vector<Bstatement*>* all_statements) const
3650 Case_constants case_constants;
3652 size_t c = this->clauses_.size();
3653 all_cases->resize(c);
3654 all_statements->resize(c);
3656 size_t i = 0;
3657 for (Clauses::const_iterator p = this->clauses_.begin();
3658 p != this->clauses_.end();
3659 ++p, ++i)
3661 std::vector<Bexpression*> cases;
3662 Bstatement* stat = p->get_backend(context, break_label, &case_constants,
3663 &cases);
3664 (*all_cases)[i].swap(cases);
3665 (*all_statements)[i] = stat;
3669 // Dump the AST representation for case clauses (from a switch statement)
3671 void
3672 Case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
3674 for (Clauses::const_iterator p = this->clauses_.begin();
3675 p != this->clauses_.end();
3676 ++p)
3677 p->dump_clause(ast_dump_context);
3680 // A constant switch statement. A Switch_statement is lowered to this
3681 // when all the cases are constants.
3683 class Constant_switch_statement : public Statement
3685 public:
3686 Constant_switch_statement(Expression* val, Case_clauses* clauses,
3687 Unnamed_label* break_label,
3688 Location location)
3689 : Statement(STATEMENT_CONSTANT_SWITCH, location),
3690 val_(val), clauses_(clauses), break_label_(break_label)
3693 protected:
3695 do_traverse(Traverse*);
3697 void
3698 do_determine_types();
3700 void
3701 do_check_types(Gogo*);
3703 Bstatement*
3704 do_get_backend(Translate_context*);
3706 void
3707 do_dump_statement(Ast_dump_context*) const;
3709 private:
3710 // The value to switch on.
3711 Expression* val_;
3712 // The case clauses.
3713 Case_clauses* clauses_;
3714 // The break label, if needed.
3715 Unnamed_label* break_label_;
3718 // Traversal.
3721 Constant_switch_statement::do_traverse(Traverse* traverse)
3723 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3724 return TRAVERSE_EXIT;
3725 return this->clauses_->traverse(traverse);
3728 // Determine types.
3730 void
3731 Constant_switch_statement::do_determine_types()
3733 this->val_->determine_type_no_context();
3734 this->clauses_->determine_types(this->val_->type());
3737 // Check types.
3739 void
3740 Constant_switch_statement::do_check_types(Gogo*)
3742 if (!this->clauses_->check_types(this->val_->type()))
3743 this->set_is_error();
3746 // Convert to GENERIC.
3748 Bstatement*
3749 Constant_switch_statement::do_get_backend(Translate_context* context)
3751 Bexpression* switch_val_expr = this->val_->get_backend(context);
3753 Unnamed_label* break_label = this->break_label_;
3754 if (break_label == NULL)
3755 break_label = new Unnamed_label(this->location());
3757 std::vector<std::vector<Bexpression*> > all_cases;
3758 std::vector<Bstatement*> all_statements;
3759 this->clauses_->get_backend(context, break_label, &all_cases,
3760 &all_statements);
3762 Bfunction* bfunction = context->function()->func_value()->get_decl();
3763 Bstatement* switch_statement;
3764 switch_statement = context->backend()->switch_statement(bfunction,
3765 switch_val_expr,
3766 all_cases,
3767 all_statements,
3768 this->location());
3769 Bstatement* ldef = break_label->get_definition(context);
3770 return context->backend()->compound_statement(switch_statement, ldef);
3773 // Dump the AST representation for a constant switch statement.
3775 void
3776 Constant_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
3777 const
3779 ast_dump_context->print_indent();
3780 ast_dump_context->ostream() << "switch ";
3781 ast_dump_context->dump_expression(this->val_);
3783 if (ast_dump_context->dump_subblocks())
3785 ast_dump_context->ostream() << " {" << std::endl;
3786 this->clauses_->dump_clauses(ast_dump_context);
3787 ast_dump_context->ostream() << "}";
3790 ast_dump_context->ostream() << std::endl;
3793 // Class Switch_statement.
3795 // Traversal.
3798 Switch_statement::do_traverse(Traverse* traverse)
3800 if (this->val_ != NULL)
3802 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3803 return TRAVERSE_EXIT;
3805 return this->clauses_->traverse(traverse);
3808 // Lower a Switch_statement to a Constant_switch_statement or a series
3809 // of if statements.
3811 Statement*
3812 Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
3813 Statement_inserter*)
3815 Location loc = this->location();
3817 if (this->val_ != NULL
3818 && (this->val_->is_error_expression()
3819 || this->val_->type()->is_error()))
3820 return Statement::make_error_statement(loc);
3822 if (this->val_ != NULL
3823 && this->val_->type()->integer_type() != NULL
3824 && !this->clauses_->empty()
3825 && this->clauses_->is_constant())
3826 return new Constant_switch_statement(this->val_, this->clauses_,
3827 this->break_label_, loc);
3829 if (this->val_ != NULL
3830 && !this->val_->type()->is_comparable()
3831 && !Type::are_compatible_for_comparison(true, this->val_->type(),
3832 Type::make_nil_type(), NULL))
3834 error_at(this->val_->location(),
3835 "cannot switch on value whose type that may not be compared");
3836 return Statement::make_error_statement(loc);
3839 Block* b = new Block(enclosing, loc);
3841 if (this->clauses_->empty())
3843 Expression* val = this->val_;
3844 if (val == NULL)
3845 val = Expression::make_boolean(true, loc);
3846 return Statement::make_statement(val, true);
3849 // var val_temp VAL_TYPE = VAL
3850 Expression* val = this->val_;
3851 if (val == NULL)
3852 val = Expression::make_boolean(true, loc);
3853 Temporary_statement* val_temp = Statement::make_temporary(NULL, val, loc);
3854 b->add_statement(val_temp);
3856 this->clauses_->lower(b, val_temp, this->break_label());
3858 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3859 b->add_statement(s);
3861 return Statement::make_block_statement(b, loc);
3864 // Return the break label for this switch statement, creating it if
3865 // necessary.
3867 Unnamed_label*
3868 Switch_statement::break_label()
3870 if (this->break_label_ == NULL)
3871 this->break_label_ = new Unnamed_label(this->location());
3872 return this->break_label_;
3875 // Dump the AST representation for a switch statement.
3877 void
3878 Switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3880 ast_dump_context->print_indent();
3881 ast_dump_context->ostream() << "switch ";
3882 if (this->val_ != NULL)
3884 ast_dump_context->dump_expression(this->val_);
3886 if (ast_dump_context->dump_subblocks())
3888 ast_dump_context->ostream() << " {" << std::endl;
3889 this->clauses_->dump_clauses(ast_dump_context);
3890 ast_dump_context->print_indent();
3891 ast_dump_context->ostream() << "}";
3893 ast_dump_context->ostream() << std::endl;
3896 // Return whether this switch may fall through.
3898 bool
3899 Switch_statement::do_may_fall_through() const
3901 if (this->clauses_ == NULL)
3902 return true;
3904 // If we have a break label, then some case needed it. That implies
3905 // that the switch statement as a whole can fall through.
3906 if (this->break_label_ != NULL)
3907 return true;
3909 return this->clauses_->may_fall_through();
3912 // Make a switch statement.
3914 Switch_statement*
3915 Statement::make_switch_statement(Expression* val, Location location)
3917 return new Switch_statement(val, location);
3920 // Class Type_case_clauses::Type_case_clause.
3922 // Traversal.
3925 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3927 if (!this->is_default_
3928 && ((traverse->traverse_mask()
3929 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3930 && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3931 return TRAVERSE_EXIT;
3932 if (this->statements_ != NULL)
3933 return this->statements_->traverse(traverse);
3934 return TRAVERSE_CONTINUE;
3937 // Lower one clause in a type switch. Add statements to the block B.
3938 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3939 // BREAK_LABEL is the label at the end of the type switch.
3940 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3941 // statements.
3943 void
3944 Type_case_clauses::Type_case_clause::lower(Type* switch_val_type,
3945 Block* b,
3946 Temporary_statement* descriptor_temp,
3947 Unnamed_label* break_label,
3948 Unnamed_label** stmts_label) const
3950 Location loc = this->location_;
3952 Unnamed_label* next_case_label = NULL;
3953 if (!this->is_default_)
3955 Type* type = this->type_;
3957 std::string reason;
3958 if (switch_val_type->interface_type() != NULL
3959 && !type->is_nil_constant_as_type()
3960 && type->interface_type() == NULL
3961 && !switch_val_type->interface_type()->implements_interface(type,
3962 &reason))
3964 if (reason.empty())
3965 error_at(this->location_, "impossible type switch case");
3966 else
3967 error_at(this->location_, "impossible type switch case (%s)",
3968 reason.c_str());
3971 Expression* ref = Expression::make_temporary_reference(descriptor_temp,
3972 loc);
3974 Expression* cond;
3975 // The language permits case nil, which is of course a constant
3976 // rather than a type. It will appear here as an invalid
3977 // forwarding type.
3978 if (type->is_nil_constant_as_type())
3979 cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3980 Expression::make_nil(loc),
3981 loc);
3982 else
3983 cond = Runtime::make_call((type->interface_type() == NULL
3984 ? Runtime::IFACETYPEEQ
3985 : Runtime::IFACEI2TP),
3986 loc, 2,
3987 Expression::make_type_descriptor(type, loc),
3988 ref);
3990 Unnamed_label* dest;
3991 if (!this->is_fallthrough_)
3993 // if !COND { goto NEXT_CASE_LABEL }
3994 next_case_label = new Unnamed_label(Linemap::unknown_location());
3995 dest = next_case_label;
3996 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3998 else
4000 // if COND { goto STMTS_LABEL }
4001 go_assert(stmts_label != NULL);
4002 if (*stmts_label == NULL)
4003 *stmts_label = new Unnamed_label(Linemap::unknown_location());
4004 dest = *stmts_label;
4006 Block* then_block = new Block(b, loc);
4007 Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
4008 then_block->add_statement(s);
4009 s = Statement::make_if_statement(cond, then_block, NULL, loc);
4010 b->add_statement(s);
4013 if (this->statements_ != NULL
4014 || (!this->is_fallthrough_
4015 && stmts_label != NULL
4016 && *stmts_label != NULL))
4018 go_assert(!this->is_fallthrough_);
4019 if (stmts_label != NULL && *stmts_label != NULL)
4021 go_assert(!this->is_default_);
4022 if (this->statements_ != NULL)
4023 (*stmts_label)->set_location(this->statements_->start_location());
4024 Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
4025 b->add_statement(s);
4026 *stmts_label = NULL;
4028 if (this->statements_ != NULL)
4029 b->add_statement(Statement::make_block_statement(this->statements_,
4030 loc));
4033 if (this->is_fallthrough_)
4034 go_assert(next_case_label == NULL);
4035 else
4037 Location gloc = (this->statements_ == NULL
4038 ? loc
4039 : this->statements_->end_location());
4040 b->add_statement(Statement::make_goto_unnamed_statement(break_label,
4041 gloc));
4042 if (next_case_label != NULL)
4044 Statement* s =
4045 Statement::make_unnamed_label_statement(next_case_label);
4046 b->add_statement(s);
4051 // Return true if this type clause may fall through to the statements
4052 // following the switch.
4054 bool
4055 Type_case_clauses::Type_case_clause::may_fall_through() const
4057 if (this->is_fallthrough_)
4059 // This case means that we automatically fall through to the
4060 // next case (it's used for T1 in case T1, T2:). It does not
4061 // mean that we fall through to the end of the type switch as a
4062 // whole. There is sure to be a next case and that next case
4063 // will determine whether we fall through to the statements
4064 // after the type switch.
4065 return false;
4067 if (this->statements_ == NULL)
4068 return true;
4069 return this->statements_->may_fall_through();
4072 // Dump the AST representation for a type case clause
4074 void
4075 Type_case_clauses::Type_case_clause::dump_clause(
4076 Ast_dump_context* ast_dump_context) const
4078 ast_dump_context->print_indent();
4079 if (this->is_default_)
4081 ast_dump_context->ostream() << "default:";
4083 else
4085 ast_dump_context->ostream() << "case ";
4086 ast_dump_context->dump_type(this->type_);
4087 ast_dump_context->ostream() << ":" ;
4089 ast_dump_context->dump_block(this->statements_);
4090 if (this->is_fallthrough_)
4092 ast_dump_context->print_indent();
4093 ast_dump_context->ostream() << " (fallthrough)" << std::endl;
4097 // Class Type_case_clauses.
4099 // Traversal.
4102 Type_case_clauses::traverse(Traverse* traverse)
4104 for (Type_clauses::iterator p = this->clauses_.begin();
4105 p != this->clauses_.end();
4106 ++p)
4108 if (p->traverse(traverse) == TRAVERSE_EXIT)
4109 return TRAVERSE_EXIT;
4111 return TRAVERSE_CONTINUE;
4114 // Check for duplicate types.
4116 void
4117 Type_case_clauses::check_duplicates() const
4119 typedef Unordered_set_hash(const Type*, Type_hash_identical,
4120 Type_identical) Types_seen;
4121 Types_seen types_seen;
4122 for (Type_clauses::const_iterator p = this->clauses_.begin();
4123 p != this->clauses_.end();
4124 ++p)
4126 Type* t = p->type();
4127 if (t == NULL)
4128 continue;
4129 if (t->is_nil_constant_as_type())
4130 t = Type::make_nil_type();
4131 std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
4132 if (!ins.second)
4133 error_at(p->location(), "duplicate type in switch");
4137 // Lower the clauses in a type switch. Add statements to the block B.
4138 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4139 // BREAK_LABEL is the label at the end of the type switch.
4141 void
4142 Type_case_clauses::lower(Type* switch_val_type, Block* b,
4143 Temporary_statement* descriptor_temp,
4144 Unnamed_label* break_label) const
4146 const Type_case_clause* default_case = NULL;
4148 Unnamed_label* stmts_label = NULL;
4149 for (Type_clauses::const_iterator p = this->clauses_.begin();
4150 p != this->clauses_.end();
4151 ++p)
4153 if (!p->is_default())
4154 p->lower(switch_val_type, b, descriptor_temp, break_label,
4155 &stmts_label);
4156 else
4158 // We are generating a series of tests, which means that we
4159 // need to move the default case to the end.
4160 default_case = &*p;
4163 go_assert(stmts_label == NULL);
4165 if (default_case != NULL)
4166 default_case->lower(switch_val_type, b, descriptor_temp, break_label,
4167 NULL);
4170 // Return true if these clauses may fall through to the statements
4171 // following the switch statement.
4173 bool
4174 Type_case_clauses::may_fall_through() const
4176 bool found_default = false;
4177 for (Type_clauses::const_iterator p = this->clauses_.begin();
4178 p != this->clauses_.end();
4179 ++p)
4181 if (p->may_fall_through())
4182 return true;
4183 if (p->is_default())
4184 found_default = true;
4186 return !found_default;
4189 // Dump the AST representation for case clauses (from a switch statement)
4191 void
4192 Type_case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
4194 for (Type_clauses::const_iterator p = this->clauses_.begin();
4195 p != this->clauses_.end();
4196 ++p)
4197 p->dump_clause(ast_dump_context);
4200 // Class Type_switch_statement.
4202 // Traversal.
4205 Type_switch_statement::do_traverse(Traverse* traverse)
4207 if (this->var_ == NULL)
4209 if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
4210 return TRAVERSE_EXIT;
4212 if (this->clauses_ != NULL)
4213 return this->clauses_->traverse(traverse);
4214 return TRAVERSE_CONTINUE;
4217 // Lower a type switch statement to a series of if statements. The gc
4218 // compiler is able to generate a table in some cases. However, that
4219 // does not work for us because we may have type descriptors in
4220 // different shared libraries, so we can't compare them with simple
4221 // equality testing.
4223 Statement*
4224 Type_switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
4225 Statement_inserter*)
4227 const Location loc = this->location();
4229 if (this->clauses_ != NULL)
4230 this->clauses_->check_duplicates();
4232 Block* b = new Block(enclosing, loc);
4234 Type* val_type = (this->var_ != NULL
4235 ? this->var_->var_value()->type()
4236 : this->expr_->type());
4238 if (val_type->interface_type() == NULL)
4240 if (!val_type->is_error())
4241 this->report_error(_("cannot type switch on non-interface value"));
4242 return Statement::make_error_statement(loc);
4245 // var descriptor_temp DESCRIPTOR_TYPE
4246 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
4247 Temporary_statement* descriptor_temp =
4248 Statement::make_temporary(descriptor_type, NULL, loc);
4249 b->add_statement(descriptor_temp);
4251 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4252 // inlined.
4253 bool is_empty = val_type->interface_type()->is_empty();
4254 Expression* ref;
4255 if (this->var_ == NULL)
4256 ref = this->expr_;
4257 else
4258 ref = Expression::make_var_reference(this->var_, loc);
4259 Expression* call = Runtime::make_call((is_empty
4260 ? Runtime::EFACETYPE
4261 : Runtime::IFACETYPE),
4262 loc, 1, ref);
4263 Temporary_reference_expression* lhs =
4264 Expression::make_temporary_reference(descriptor_temp, loc);
4265 lhs->set_is_lvalue();
4266 Statement* s = Statement::make_assignment(lhs, call, loc);
4267 b->add_statement(s);
4269 if (this->clauses_ != NULL)
4270 this->clauses_->lower(val_type, b, descriptor_temp, this->break_label());
4272 s = Statement::make_unnamed_label_statement(this->break_label_);
4273 b->add_statement(s);
4275 return Statement::make_block_statement(b, loc);
4278 // Return whether this switch may fall through.
4280 bool
4281 Type_switch_statement::do_may_fall_through() const
4283 if (this->clauses_ == NULL)
4284 return true;
4286 // If we have a break label, then some case needed it. That implies
4287 // that the switch statement as a whole can fall through.
4288 if (this->break_label_ != NULL)
4289 return true;
4291 return this->clauses_->may_fall_through();
4294 // Return the break label for this type switch statement, creating it
4295 // if necessary.
4297 Unnamed_label*
4298 Type_switch_statement::break_label()
4300 if (this->break_label_ == NULL)
4301 this->break_label_ = new Unnamed_label(this->location());
4302 return this->break_label_;
4305 // Dump the AST representation for a type switch statement
4307 void
4308 Type_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
4309 const
4311 ast_dump_context->print_indent();
4312 ast_dump_context->ostream() << "switch " << this->var_->name() << " = ";
4313 ast_dump_context->dump_expression(this->expr_);
4314 ast_dump_context->ostream() << " .(type)";
4315 if (ast_dump_context->dump_subblocks())
4317 ast_dump_context->ostream() << " {" << std::endl;
4318 this->clauses_->dump_clauses(ast_dump_context);
4319 ast_dump_context->ostream() << "}";
4321 ast_dump_context->ostream() << std::endl;
4324 // Make a type switch statement.
4326 Type_switch_statement*
4327 Statement::make_type_switch_statement(Named_object* var, Expression* expr,
4328 Location location)
4330 return new Type_switch_statement(var, expr, location);
4333 // Class Send_statement.
4335 // Traversal.
4338 Send_statement::do_traverse(Traverse* traverse)
4340 if (this->traverse_expression(traverse, &this->channel_) == TRAVERSE_EXIT)
4341 return TRAVERSE_EXIT;
4342 return this->traverse_expression(traverse, &this->val_);
4345 // Determine types.
4347 void
4348 Send_statement::do_determine_types()
4350 this->channel_->determine_type_no_context();
4351 Type* type = this->channel_->type();
4352 Type_context context;
4353 if (type->channel_type() != NULL)
4354 context.type = type->channel_type()->element_type();
4355 this->val_->determine_type(&context);
4358 // Check types.
4360 void
4361 Send_statement::do_check_types(Gogo*)
4363 Type* type = this->channel_->type();
4364 if (type->is_error())
4366 this->set_is_error();
4367 return;
4369 Channel_type* channel_type = type->channel_type();
4370 if (channel_type == NULL)
4372 error_at(this->location(), "left operand of %<<-%> must be channel");
4373 this->set_is_error();
4374 return;
4376 Type* element_type = channel_type->element_type();
4377 if (!Type::are_assignable(element_type, this->val_->type(), NULL))
4379 this->report_error(_("incompatible types in send"));
4380 return;
4382 if (!channel_type->may_send())
4384 this->report_error(_("invalid send on receive-only channel"));
4385 return;
4389 // Convert a send statement to the backend representation.
4391 Bstatement*
4392 Send_statement::do_get_backend(Translate_context* context)
4394 Location loc = this->location();
4396 Channel_type* channel_type = this->channel_->type()->channel_type();
4397 Type* element_type = channel_type->element_type();
4398 Expression* val = Expression::make_cast(element_type, this->val_, loc);
4400 bool is_small;
4401 bool can_take_address;
4402 switch (element_type->base()->classification())
4404 case Type::TYPE_BOOLEAN:
4405 case Type::TYPE_INTEGER:
4406 case Type::TYPE_FUNCTION:
4407 case Type::TYPE_POINTER:
4408 case Type::TYPE_MAP:
4409 case Type::TYPE_CHANNEL:
4410 is_small = true;
4411 can_take_address = false;
4412 break;
4414 case Type::TYPE_FLOAT:
4415 case Type::TYPE_COMPLEX:
4416 case Type::TYPE_STRING:
4417 case Type::TYPE_INTERFACE:
4418 is_small = false;
4419 can_take_address = false;
4420 break;
4422 case Type::TYPE_STRUCT:
4423 is_small = false;
4424 can_take_address = true;
4425 break;
4427 case Type::TYPE_ARRAY:
4428 is_small = false;
4429 can_take_address = !element_type->is_slice_type();
4430 break;
4432 default:
4433 case Type::TYPE_ERROR:
4434 case Type::TYPE_VOID:
4435 case Type::TYPE_SINK:
4436 case Type::TYPE_NIL:
4437 case Type::TYPE_NAMED:
4438 case Type::TYPE_FORWARD:
4439 go_assert(saw_errors());
4440 return context->backend()->error_statement();
4443 // Only try to take the address of a variable. We have already
4444 // moved variables to the heap, so this should not cause that to
4445 // happen unnecessarily.
4446 if (can_take_address
4447 && val->var_expression() == NULL
4448 && val->temporary_reference_expression() == NULL)
4449 can_take_address = false;
4451 Expression* td = Expression::make_type_descriptor(this->channel_->type(),
4452 loc);
4454 Runtime::Function code;
4455 Bstatement* btemp = NULL;
4456 if (is_small)
4458 // Type is small enough to handle as uint64.
4459 code = Runtime::SEND_SMALL;
4460 val = Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
4461 val, loc);
4463 else if (can_take_address)
4465 // Must pass address of value. The function doesn't change the
4466 // value, so just take its address directly.
4467 code = Runtime::SEND_BIG;
4468 val = Expression::make_unary(OPERATOR_AND, val, loc);
4470 else
4472 // Must pass address of value, but the value is small enough
4473 // that it might be in registers. Copy value into temporary
4474 // variable to take address.
4475 code = Runtime::SEND_BIG;
4476 Temporary_statement* temp = Statement::make_temporary(element_type,
4477 val, loc);
4478 Expression* ref = Expression::make_temporary_reference(temp, loc);
4479 val = Expression::make_unary(OPERATOR_AND, ref, loc);
4480 btemp = temp->get_backend(context);
4483 Expression* call = Runtime::make_call(code, loc, 3, td, this->channel_, val);
4485 context->gogo()->lower_expression(context->function(), NULL, &call);
4486 Bexpression* bcall = call->get_backend(context);
4487 Bstatement* s = context->backend()->expression_statement(bcall);
4489 if (btemp == NULL)
4490 return s;
4491 else
4492 return context->backend()->compound_statement(btemp, s);
4495 // Dump the AST representation for a send statement
4497 void
4498 Send_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
4500 ast_dump_context->print_indent();
4501 ast_dump_context->dump_expression(this->channel_);
4502 ast_dump_context->ostream() << " <- ";
4503 ast_dump_context->dump_expression(this->val_);
4504 ast_dump_context->ostream() << std::endl;
4507 // Make a send statement.
4509 Send_statement*
4510 Statement::make_send_statement(Expression* channel, Expression* val,
4511 Location location)
4513 return new Send_statement(channel, val, location);
4516 // Class Select_clauses::Select_clause.
4518 // Traversal.
4521 Select_clauses::Select_clause::traverse(Traverse* traverse)
4523 if (!this->is_lowered_
4524 && (traverse->traverse_mask()
4525 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
4527 if (this->channel_ != NULL)
4529 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
4530 return TRAVERSE_EXIT;
4532 if (this->val_ != NULL)
4534 if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
4535 return TRAVERSE_EXIT;
4537 if (this->closed_ != NULL)
4539 if (Expression::traverse(&this->closed_, traverse) == TRAVERSE_EXIT)
4540 return TRAVERSE_EXIT;
4543 if (this->statements_ != NULL)
4545 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
4546 return TRAVERSE_EXIT;
4548 return TRAVERSE_CONTINUE;
4551 // Lowering. We call a function to register this clause, and arrange
4552 // to set any variables in any receive clause.
4554 void
4555 Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
4556 Block* b, Temporary_statement* sel)
4558 Location loc = this->location_;
4560 Expression* selref = Expression::make_temporary_reference(sel, loc);
4562 mpz_t ival;
4563 mpz_init_set_ui(ival, this->index_);
4564 Expression* index_expr = Expression::make_integer(&ival, NULL, loc);
4565 mpz_clear(ival);
4567 if (this->is_default_)
4569 go_assert(this->channel_ == NULL && this->val_ == NULL);
4570 this->lower_default(b, selref, index_expr);
4571 this->is_lowered_ = true;
4572 return;
4575 // Evaluate the channel before the select statement.
4576 Temporary_statement* channel_temp = Statement::make_temporary(NULL,
4577 this->channel_,
4578 loc);
4579 b->add_statement(channel_temp);
4580 Expression* chanref = Expression::make_temporary_reference(channel_temp,
4581 loc);
4583 if (this->is_send_)
4584 this->lower_send(b, selref, chanref, index_expr);
4585 else
4586 this->lower_recv(gogo, function, b, selref, chanref, index_expr);
4588 // Now all references should be handled through the statements, not
4589 // through here.
4590 this->is_lowered_ = true;
4591 this->val_ = NULL;
4592 this->var_ = NULL;
4595 // Lower a default clause in a select statement.
4597 void
4598 Select_clauses::Select_clause::lower_default(Block* b, Expression* selref,
4599 Expression* index_expr)
4601 Location loc = this->location_;
4602 Expression* call = Runtime::make_call(Runtime::SELECTDEFAULT, loc, 2, selref,
4603 index_expr);
4604 b->add_statement(Statement::make_statement(call, true));
4607 // Lower a send clause in a select statement.
4609 void
4610 Select_clauses::Select_clause::lower_send(Block* b, Expression* selref,
4611 Expression* chanref,
4612 Expression* index_expr)
4614 Location loc = this->location_;
4616 Channel_type* ct = this->channel_->type()->channel_type();
4617 if (ct == NULL)
4618 return;
4620 Type* valtype = ct->element_type();
4622 // Note that copying the value to a temporary here means that we
4623 // evaluate the send values in the required order.
4624 Temporary_statement* val = Statement::make_temporary(valtype, this->val_,
4625 loc);
4626 b->add_statement(val);
4628 Expression* valref = Expression::make_temporary_reference(val, loc);
4629 Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
4631 Expression* call = Runtime::make_call(Runtime::SELECTSEND, loc, 4, selref,
4632 chanref, valaddr, index_expr);
4633 b->add_statement(Statement::make_statement(call, true));
4636 // Lower a receive clause in a select statement.
4638 void
4639 Select_clauses::Select_clause::lower_recv(Gogo* gogo, Named_object* function,
4640 Block* b, Expression* selref,
4641 Expression* chanref,
4642 Expression* index_expr)
4644 Location loc = this->location_;
4646 Channel_type* ct = this->channel_->type()->channel_type();
4647 if (ct == NULL)
4648 return;
4650 Type* valtype = ct->element_type();
4651 Temporary_statement* val = Statement::make_temporary(valtype, NULL, loc);
4652 b->add_statement(val);
4654 Expression* valref = Expression::make_temporary_reference(val, loc);
4655 Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
4657 Temporary_statement* closed_temp = NULL;
4659 Expression* call;
4660 if (this->closed_ == NULL && this->closedvar_ == NULL)
4661 call = Runtime::make_call(Runtime::SELECTRECV, loc, 4, selref, chanref,
4662 valaddr, index_expr);
4663 else
4665 closed_temp = Statement::make_temporary(Type::lookup_bool_type(), NULL,
4666 loc);
4667 b->add_statement(closed_temp);
4668 Expression* cref = Expression::make_temporary_reference(closed_temp,
4669 loc);
4670 Expression* caddr = Expression::make_unary(OPERATOR_AND, cref, loc);
4671 call = Runtime::make_call(Runtime::SELECTRECV2, loc, 5, selref, chanref,
4672 valaddr, caddr, index_expr);
4675 b->add_statement(Statement::make_statement(call, true));
4677 // If the block of statements is executed, arrange for the received
4678 // value to move from VAL to the place where the statements expect
4679 // it.
4681 Block* init = NULL;
4683 if (this->var_ != NULL)
4685 go_assert(this->val_ == NULL);
4686 valref = Expression::make_temporary_reference(val, loc);
4687 this->var_->var_value()->set_init(valref);
4688 this->var_->var_value()->clear_type_from_chan_element();
4690 else if (this->val_ != NULL && !this->val_->is_sink_expression())
4692 init = new Block(b, loc);
4693 valref = Expression::make_temporary_reference(val, loc);
4694 init->add_statement(Statement::make_assignment(this->val_, valref, loc));
4697 if (this->closedvar_ != NULL)
4699 go_assert(this->closed_ == NULL);
4700 Expression* cref = Expression::make_temporary_reference(closed_temp,
4701 loc);
4702 this->closedvar_->var_value()->set_init(cref);
4704 else if (this->closed_ != NULL && !this->closed_->is_sink_expression())
4706 if (init == NULL)
4707 init = new Block(b, loc);
4708 Expression* cref = Expression::make_temporary_reference(closed_temp,
4709 loc);
4710 init->add_statement(Statement::make_assignment(this->closed_, cref,
4711 loc));
4714 if (init != NULL)
4716 gogo->lower_block(function, init);
4718 if (this->statements_ != NULL)
4719 init->add_statement(Statement::make_block_statement(this->statements_,
4720 loc));
4721 this->statements_ = init;
4725 // Determine types.
4727 void
4728 Select_clauses::Select_clause::determine_types()
4730 go_assert(this->is_lowered_);
4731 if (this->statements_ != NULL)
4732 this->statements_->determine_types();
4735 // Check types.
4737 void
4738 Select_clauses::Select_clause::check_types()
4740 if (this->is_default_)
4741 return;
4743 Channel_type* ct = this->channel_->type()->channel_type();
4744 if (ct == NULL)
4746 error_at(this->channel_->location(), "expected channel");
4747 return;
4750 if (this->is_send_ && !ct->may_send())
4751 error_at(this->location(), "invalid send on receive-only channel");
4752 else if (!this->is_send_ && !ct->may_receive())
4753 error_at(this->location(), "invalid receive on send-only channel");
4756 // Whether this clause may fall through to the statement which follows
4757 // the overall select statement.
4759 bool
4760 Select_clauses::Select_clause::may_fall_through() const
4762 if (this->statements_ == NULL)
4763 return true;
4764 return this->statements_->may_fall_through();
4767 // Return the backend representation for the statements to execute.
4769 Bstatement*
4770 Select_clauses::Select_clause::get_statements_backend(
4771 Translate_context* context)
4773 if (this->statements_ == NULL)
4774 return NULL;
4775 Bblock* bblock = this->statements_->get_backend(context);
4776 return context->backend()->block_statement(bblock);
4779 // Dump the AST representation for a select case clause
4781 void
4782 Select_clauses::Select_clause::dump_clause(
4783 Ast_dump_context* ast_dump_context) const
4785 ast_dump_context->print_indent();
4786 if (this->is_default_)
4788 ast_dump_context->ostream() << "default:";
4790 else
4792 ast_dump_context->ostream() << "case " ;
4793 if (this->is_send_)
4795 ast_dump_context->dump_expression(this->channel_);
4796 ast_dump_context->ostream() << " <- " ;
4797 if (this->val_ != NULL)
4798 ast_dump_context->dump_expression(this->val_);
4800 else
4802 if (this->val_ != NULL)
4803 ast_dump_context->dump_expression(this->val_);
4804 if (this->closed_ != NULL)
4806 // FIXME: can val_ == NULL and closed_ ! = NULL?
4807 ast_dump_context->ostream() << " , " ;
4808 ast_dump_context->dump_expression(this->closed_);
4810 if (this->closedvar_ != NULL || this->var_ != NULL)
4811 ast_dump_context->ostream() << " := " ;
4813 ast_dump_context->ostream() << " <- " ;
4814 ast_dump_context->dump_expression(this->channel_);
4816 ast_dump_context->ostream() << ":" ;
4818 ast_dump_context->dump_block(this->statements_);
4821 // Class Select_clauses.
4823 // Traversal.
4826 Select_clauses::traverse(Traverse* traverse)
4828 for (Clauses::iterator p = this->clauses_.begin();
4829 p != this->clauses_.end();
4830 ++p)
4832 if (p->traverse(traverse) == TRAVERSE_EXIT)
4833 return TRAVERSE_EXIT;
4835 return TRAVERSE_CONTINUE;
4838 // Lowering. Here we pull out the channel and the send values, to
4839 // enforce the order of evaluation. We also add explicit send and
4840 // receive statements to the clauses.
4842 void
4843 Select_clauses::lower(Gogo* gogo, Named_object* function, Block* b,
4844 Temporary_statement* sel)
4846 for (Clauses::iterator p = this->clauses_.begin();
4847 p != this->clauses_.end();
4848 ++p)
4849 p->lower(gogo, function, b, sel);
4852 // Determine types.
4854 void
4855 Select_clauses::determine_types()
4857 for (Clauses::iterator p = this->clauses_.begin();
4858 p != this->clauses_.end();
4859 ++p)
4860 p->determine_types();
4863 // Check types.
4865 void
4866 Select_clauses::check_types()
4868 for (Clauses::iterator p = this->clauses_.begin();
4869 p != this->clauses_.end();
4870 ++p)
4871 p->check_types();
4874 // Return whether these select clauses fall through to the statement
4875 // following the overall select statement.
4877 bool
4878 Select_clauses::may_fall_through() const
4880 for (Clauses::const_iterator p = this->clauses_.begin();
4881 p != this->clauses_.end();
4882 ++p)
4883 if (p->may_fall_through())
4884 return true;
4885 return false;
4888 // Convert to the backend representation. We have already accumulated
4889 // all the select information. Now we call selectgo, which will
4890 // return the index of the clause to execute.
4892 Bstatement*
4893 Select_clauses::get_backend(Translate_context* context,
4894 Temporary_statement* sel,
4895 Unnamed_label *break_label,
4896 Location location)
4898 size_t count = this->clauses_.size();
4899 std::vector<std::vector<Bexpression*> > cases(count);
4900 std::vector<Bstatement*> clauses(count);
4902 Type* int32_type = Type::lookup_integer_type("int32");
4904 int i = 0;
4905 for (Clauses::iterator p = this->clauses_.begin();
4906 p != this->clauses_.end();
4907 ++p, ++i)
4909 int index = p->index();
4910 mpz_t ival;
4911 mpz_init_set_ui(ival, index);
4912 Expression* index_expr = Expression::make_integer(&ival, int32_type,
4913 location);
4914 mpz_clear(ival);
4915 cases[i].push_back(index_expr->get_backend(context));
4917 Bstatement* s = p->get_statements_backend(context);
4918 Location gloc = (p->statements() == NULL
4919 ? p->location()
4920 : p->statements()->end_location());
4921 Bstatement* g = break_label->get_goto(context, gloc);
4923 if (s == NULL)
4924 clauses[i] = g;
4925 else
4926 clauses[i] = context->backend()->compound_statement(s, g);
4929 Expression* selref = Expression::make_temporary_reference(sel, location);
4930 Expression* call = Runtime::make_call(Runtime::SELECTGO, location, 1,
4931 selref);
4932 context->gogo()->lower_expression(context->function(), NULL, &call);
4933 Bexpression* bcall = call->get_backend(context);
4935 if (count == 0)
4936 return context->backend()->expression_statement(bcall);
4938 std::vector<Bstatement*> statements;
4939 statements.reserve(2);
4941 Bfunction* bfunction = context->function()->func_value()->get_decl();
4942 Bstatement* switch_stmt = context->backend()->switch_statement(bfunction,
4943 bcall,
4944 cases,
4945 clauses,
4946 location);
4947 statements.push_back(switch_stmt);
4949 Bstatement* ldef = break_label->get_definition(context);
4950 statements.push_back(ldef);
4952 return context->backend()->statement_list(statements);
4954 // Dump the AST representation for select clauses.
4956 void
4957 Select_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
4959 for (Clauses::const_iterator p = this->clauses_.begin();
4960 p != this->clauses_.end();
4961 ++p)
4962 p->dump_clause(ast_dump_context);
4965 // Class Select_statement.
4967 // Return the break label for this switch statement, creating it if
4968 // necessary.
4970 Unnamed_label*
4971 Select_statement::break_label()
4973 if (this->break_label_ == NULL)
4974 this->break_label_ = new Unnamed_label(this->location());
4975 return this->break_label_;
4978 // Lower a select statement. This will still return a select
4979 // statement, but it will be modified to implement the order of
4980 // evaluation rules, and to include the send and receive statements as
4981 // explicit statements in the clauses.
4983 Statement*
4984 Select_statement::do_lower(Gogo* gogo, Named_object* function,
4985 Block* enclosing, Statement_inserter*)
4987 if (this->is_lowered_)
4988 return this;
4990 Location loc = this->location();
4992 Block* b = new Block(enclosing, loc);
4994 go_assert(this->sel_ == NULL);
4996 mpz_t ival;
4997 mpz_init_set_ui(ival, this->clauses_->size());
4998 Expression* size_expr = Expression::make_integer(&ival, NULL, loc);
4999 mpz_clear(ival);
5001 Expression* call = Runtime::make_call(Runtime::NEWSELECT, loc, 1, size_expr);
5003 this->sel_ = Statement::make_temporary(NULL, call, loc);
5004 b->add_statement(this->sel_);
5006 this->clauses_->lower(gogo, function, b, this->sel_);
5007 this->is_lowered_ = true;
5008 b->add_statement(this);
5010 return Statement::make_block_statement(b, loc);
5013 // Whether the select statement itself may fall through to the following
5014 // statement.
5016 bool
5017 Select_statement::do_may_fall_through() const
5019 // A select statement is terminating if no break statement
5020 // refers to it and all of its clauses are terminating.
5021 if (this->break_label_ != NULL)
5022 return true;
5023 return this->clauses_->may_fall_through();
5026 // Return the backend representation for a select statement.
5028 Bstatement*
5029 Select_statement::do_get_backend(Translate_context* context)
5031 return this->clauses_->get_backend(context, this->sel_, this->break_label(),
5032 this->location());
5035 // Dump the AST representation for a select statement.
5037 void
5038 Select_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
5040 ast_dump_context->print_indent();
5041 ast_dump_context->ostream() << "select";
5042 if (ast_dump_context->dump_subblocks())
5044 ast_dump_context->ostream() << " {" << std::endl;
5045 this->clauses_->dump_clauses(ast_dump_context);
5046 ast_dump_context->ostream() << "}";
5048 ast_dump_context->ostream() << std::endl;
5051 // Make a select statement.
5053 Select_statement*
5054 Statement::make_select_statement(Location location)
5056 return new Select_statement(location);
5059 // Class For_statement.
5061 // Traversal.
5064 For_statement::do_traverse(Traverse* traverse)
5066 if (this->init_ != NULL)
5068 if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
5069 return TRAVERSE_EXIT;
5071 if (this->cond_ != NULL)
5073 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
5074 return TRAVERSE_EXIT;
5076 if (this->post_ != NULL)
5078 if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
5079 return TRAVERSE_EXIT;
5081 return this->statements_->traverse(traverse);
5084 // Lower a For_statement into if statements and gotos. Getting rid of
5085 // complex statements make it easier to handle garbage collection.
5087 Statement*
5088 For_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
5089 Statement_inserter*)
5091 Statement* s;
5092 Location loc = this->location();
5094 Block* b = new Block(enclosing, this->location());
5095 if (this->init_ != NULL)
5097 s = Statement::make_block_statement(this->init_,
5098 this->init_->start_location());
5099 b->add_statement(s);
5102 Unnamed_label* entry = NULL;
5103 if (this->cond_ != NULL)
5105 entry = new Unnamed_label(this->location());
5106 b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
5109 Unnamed_label* top = new Unnamed_label(this->location());
5110 b->add_statement(Statement::make_unnamed_label_statement(top));
5112 s = Statement::make_block_statement(this->statements_,
5113 this->statements_->start_location());
5114 b->add_statement(s);
5116 Location end_loc = this->statements_->end_location();
5118 Unnamed_label* cont = this->continue_label_;
5119 if (cont != NULL)
5120 b->add_statement(Statement::make_unnamed_label_statement(cont));
5122 if (this->post_ != NULL)
5124 s = Statement::make_block_statement(this->post_,
5125 this->post_->start_location());
5126 b->add_statement(s);
5127 end_loc = this->post_->end_location();
5130 if (this->cond_ == NULL)
5131 b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
5132 else
5134 b->add_statement(Statement::make_unnamed_label_statement(entry));
5136 Location cond_loc = this->cond_->location();
5137 Block* then_block = new Block(b, cond_loc);
5138 s = Statement::make_goto_unnamed_statement(top, cond_loc);
5139 then_block->add_statement(s);
5141 s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
5142 b->add_statement(s);
5145 Unnamed_label* brk = this->break_label_;
5146 if (brk != NULL)
5147 b->add_statement(Statement::make_unnamed_label_statement(brk));
5149 b->set_end_location(end_loc);
5151 return Statement::make_block_statement(b, loc);
5154 // Return the break label, creating it if necessary.
5156 Unnamed_label*
5157 For_statement::break_label()
5159 if (this->break_label_ == NULL)
5160 this->break_label_ = new Unnamed_label(this->location());
5161 return this->break_label_;
5164 // Return the continue LABEL_EXPR.
5166 Unnamed_label*
5167 For_statement::continue_label()
5169 if (this->continue_label_ == NULL)
5170 this->continue_label_ = new Unnamed_label(this->location());
5171 return this->continue_label_;
5174 // Set the break and continue labels a for statement. This is used
5175 // when lowering a for range statement.
5177 void
5178 For_statement::set_break_continue_labels(Unnamed_label* break_label,
5179 Unnamed_label* continue_label)
5181 go_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
5182 this->break_label_ = break_label;
5183 this->continue_label_ = continue_label;
5186 // Whether the overall statement may fall through.
5188 bool
5189 For_statement::do_may_fall_through() const
5191 // A for loop is terminating if it has no condition and
5192 // no break statement.
5193 if(this->cond_ != NULL)
5194 return true;
5195 if(this->break_label_ != NULL)
5196 return true;
5197 return false;
5200 // Dump the AST representation for a for statement.
5202 void
5203 For_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
5205 if (this->init_ != NULL && ast_dump_context->dump_subblocks())
5207 ast_dump_context->print_indent();
5208 ast_dump_context->indent();
5209 ast_dump_context->ostream() << "// INIT " << std::endl;
5210 ast_dump_context->dump_block(this->init_);
5211 ast_dump_context->unindent();
5213 ast_dump_context->print_indent();
5214 ast_dump_context->ostream() << "for ";
5215 if (this->cond_ != NULL)
5216 ast_dump_context->dump_expression(this->cond_);
5218 if (ast_dump_context->dump_subblocks())
5220 ast_dump_context->ostream() << " {" << std::endl;
5221 ast_dump_context->dump_block(this->statements_);
5222 if (this->init_ != NULL)
5224 ast_dump_context->print_indent();
5225 ast_dump_context->ostream() << "// POST " << std::endl;
5226 ast_dump_context->dump_block(this->post_);
5228 ast_dump_context->unindent();
5230 ast_dump_context->print_indent();
5231 ast_dump_context->ostream() << "}";
5234 ast_dump_context->ostream() << std::endl;
5237 // Make a for statement.
5239 For_statement*
5240 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
5241 Location location)
5243 return new For_statement(init, cond, post, location);
5246 // Class For_range_statement.
5248 // Traversal.
5251 For_range_statement::do_traverse(Traverse* traverse)
5253 if (this->index_var_ != NULL)
5255 if (this->traverse_expression(traverse, &this->index_var_)
5256 == TRAVERSE_EXIT)
5257 return TRAVERSE_EXIT;
5259 if (this->value_var_ != NULL)
5261 if (this->traverse_expression(traverse, &this->value_var_)
5262 == TRAVERSE_EXIT)
5263 return TRAVERSE_EXIT;
5265 if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
5266 return TRAVERSE_EXIT;
5267 return this->statements_->traverse(traverse);
5270 // Lower a for range statement. For simplicity we lower this into a
5271 // for statement, which will then be lowered in turn to goto
5272 // statements.
5274 Statement*
5275 For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing,
5276 Statement_inserter*)
5278 Type* range_type = this->range_->type();
5279 if (range_type->points_to() != NULL
5280 && range_type->points_to()->array_type() != NULL
5281 && !range_type->points_to()->is_slice_type())
5282 range_type = range_type->points_to();
5284 Type* index_type;
5285 Type* value_type = NULL;
5286 if (range_type->array_type() != NULL)
5288 index_type = Type::lookup_integer_type("int");
5289 value_type = range_type->array_type()->element_type();
5291 else if (range_type->is_string_type())
5293 index_type = Type::lookup_integer_type("int");
5294 value_type = Type::lookup_integer_type("int32");
5296 else if (range_type->map_type() != NULL)
5298 index_type = range_type->map_type()->key_type();
5299 value_type = range_type->map_type()->val_type();
5301 else if (range_type->channel_type() != NULL)
5303 index_type = range_type->channel_type()->element_type();
5304 if (this->value_var_ != NULL)
5306 if (!this->value_var_->type()->is_error())
5307 this->report_error(_("too many variables for range clause "
5308 "with channel"));
5309 return Statement::make_error_statement(this->location());
5312 else
5314 this->report_error(_("range clause must have "
5315 "array, slice, string, map, or channel type"));
5316 return Statement::make_error_statement(this->location());
5319 Location loc = this->location();
5320 Block* temp_block = new Block(enclosing, loc);
5322 Named_object* range_object = NULL;
5323 Temporary_statement* range_temp = NULL;
5324 Var_expression* ve = this->range_->var_expression();
5325 if (ve != NULL)
5326 range_object = ve->named_object();
5327 else
5329 range_temp = Statement::make_temporary(NULL, this->range_, loc);
5330 temp_block->add_statement(range_temp);
5331 this->range_ = NULL;
5334 Temporary_statement* index_temp = Statement::make_temporary(index_type,
5335 NULL, loc);
5336 temp_block->add_statement(index_temp);
5338 Temporary_statement* value_temp = NULL;
5339 if (this->value_var_ != NULL)
5341 value_temp = Statement::make_temporary(value_type, NULL, loc);
5342 temp_block->add_statement(value_temp);
5345 Block* body = new Block(temp_block, loc);
5347 Block* init;
5348 Expression* cond;
5349 Block* iter_init;
5350 Block* post;
5352 // Arrange to do a loop appropriate for the type. We will produce
5353 // for INIT ; COND ; POST {
5354 // ITER_INIT
5355 // INDEX = INDEX_TEMP
5356 // VALUE = VALUE_TEMP // If there is a value
5357 // original statements
5358 // }
5360 if (range_type->is_slice_type())
5361 this->lower_range_slice(gogo, temp_block, body, range_object, range_temp,
5362 index_temp, value_temp, &init, &cond, &iter_init,
5363 &post);
5364 else if (range_type->array_type() != NULL)
5365 this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
5366 index_temp, value_temp, &init, &cond, &iter_init,
5367 &post);
5368 else if (range_type->is_string_type())
5369 this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
5370 index_temp, value_temp, &init, &cond, &iter_init,
5371 &post);
5372 else if (range_type->map_type() != NULL)
5373 this->lower_range_map(gogo, temp_block, body, range_object, range_temp,
5374 index_temp, value_temp, &init, &cond, &iter_init,
5375 &post);
5376 else if (range_type->channel_type() != NULL)
5377 this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
5378 index_temp, value_temp, &init, &cond, &iter_init,
5379 &post);
5380 else
5381 go_unreachable();
5383 if (iter_init != NULL)
5384 body->add_statement(Statement::make_block_statement(iter_init, loc));
5386 if (this->index_var_ != NULL)
5388 Statement* assign;
5389 Expression* index_ref =
5390 Expression::make_temporary_reference(index_temp, loc);
5391 if (this->value_var_ == NULL)
5392 assign = Statement::make_assignment(this->index_var_, index_ref, loc);
5393 else
5395 Expression_list* lhs = new Expression_list();
5396 lhs->push_back(this->index_var_);
5397 lhs->push_back(this->value_var_);
5399 Expression_list* rhs = new Expression_list();
5400 rhs->push_back(index_ref);
5401 rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
5403 assign = Statement::make_tuple_assignment(lhs, rhs, loc);
5405 body->add_statement(assign);
5408 body->add_statement(Statement::make_block_statement(this->statements_, loc));
5410 body->set_end_location(this->statements_->end_location());
5412 For_statement* loop = Statement::make_for_statement(init, cond, post,
5413 this->location());
5414 loop->add_statements(body);
5415 loop->set_break_continue_labels(this->break_label_, this->continue_label_);
5417 temp_block->add_statement(loop);
5419 return Statement::make_block_statement(temp_block, loc);
5422 // Return a reference to the range, which may be in RANGE_OBJECT or in
5423 // RANGE_TEMP.
5425 Expression*
5426 For_range_statement::make_range_ref(Named_object* range_object,
5427 Temporary_statement* range_temp,
5428 Location loc)
5430 if (range_object != NULL)
5431 return Expression::make_var_reference(range_object, loc);
5432 else
5433 return Expression::make_temporary_reference(range_temp, loc);
5436 // Return a call to the predeclared function FUNCNAME passing a
5437 // reference to the temporary variable ARG.
5439 Expression*
5440 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
5441 Expression* arg,
5442 Location loc)
5444 Named_object* no = gogo->lookup_global(funcname);
5445 go_assert(no != NULL && no->is_function_declaration());
5446 Expression* func = Expression::make_func_reference(no, NULL, loc);
5447 Expression_list* params = new Expression_list();
5448 params->push_back(arg);
5449 return Expression::make_call(func, params, false, loc);
5452 // Lower a for range over an array.
5454 void
5455 For_range_statement::lower_range_array(Gogo* gogo,
5456 Block* enclosing,
5457 Block* body_block,
5458 Named_object* range_object,
5459 Temporary_statement* range_temp,
5460 Temporary_statement* index_temp,
5461 Temporary_statement* value_temp,
5462 Block** pinit,
5463 Expression** pcond,
5464 Block** piter_init,
5465 Block** ppost)
5467 Location loc = this->location();
5469 // The loop we generate:
5470 // len_temp := len(range)
5471 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5472 // value_temp = range[index_temp]
5473 // index = index_temp
5474 // value = value_temp
5475 // original body
5476 // }
5478 // Set *PINIT to
5479 // var len_temp int
5480 // len_temp = len(range)
5481 // index_temp = 0
5483 Block* init = new Block(enclosing, loc);
5485 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5486 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
5487 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
5488 len_call, loc);
5489 init->add_statement(len_temp);
5491 mpz_t zval;
5492 mpz_init_set_ui(zval, 0UL);
5493 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
5494 mpz_clear(zval);
5496 Temporary_reference_expression* tref =
5497 Expression::make_temporary_reference(index_temp, loc);
5498 tref->set_is_lvalue();
5499 Statement* s = Statement::make_assignment(tref, zexpr, loc);
5500 init->add_statement(s);
5502 *pinit = init;
5504 // Set *PCOND to
5505 // index_temp < len_temp
5507 ref = Expression::make_temporary_reference(index_temp, loc);
5508 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
5509 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
5511 *pcond = lt;
5513 // Set *PITER_INIT to
5514 // value_temp = range[index_temp]
5516 Block* iter_init = NULL;
5517 if (value_temp != NULL)
5519 iter_init = new Block(body_block, loc);
5521 ref = this->make_range_ref(range_object, range_temp, loc);
5522 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
5523 Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
5525 tref = Expression::make_temporary_reference(value_temp, loc);
5526 tref->set_is_lvalue();
5527 s = Statement::make_assignment(tref, index, loc);
5529 iter_init->add_statement(s);
5531 *piter_init = iter_init;
5533 // Set *PPOST to
5534 // index_temp++
5536 Block* post = new Block(enclosing, loc);
5537 tref = Expression::make_temporary_reference(index_temp, loc);
5538 tref->set_is_lvalue();
5539 s = Statement::make_inc_statement(tref);
5540 post->add_statement(s);
5541 *ppost = post;
5544 // Lower a for range over a slice.
5546 void
5547 For_range_statement::lower_range_slice(Gogo* gogo,
5548 Block* enclosing,
5549 Block* body_block,
5550 Named_object* range_object,
5551 Temporary_statement* range_temp,
5552 Temporary_statement* index_temp,
5553 Temporary_statement* value_temp,
5554 Block** pinit,
5555 Expression** pcond,
5556 Block** piter_init,
5557 Block** ppost)
5559 Location loc = this->location();
5561 // The loop we generate:
5562 // for_temp := range
5563 // len_temp := len(for_temp)
5564 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5565 // value_temp = for_temp[index_temp]
5566 // index = index_temp
5567 // value = value_temp
5568 // original body
5569 // }
5571 // Using for_temp means that we don't need to check bounds when
5572 // fetching range_temp[index_temp].
5574 // Set *PINIT to
5575 // range_temp := range
5576 // var len_temp int
5577 // len_temp = len(range_temp)
5578 // index_temp = 0
5580 Block* init = new Block(enclosing, loc);
5582 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5583 Temporary_statement* for_temp = Statement::make_temporary(NULL, ref, loc);
5584 init->add_statement(for_temp);
5586 ref = Expression::make_temporary_reference(for_temp, loc);
5587 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
5588 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
5589 len_call, loc);
5590 init->add_statement(len_temp);
5592 mpz_t zval;
5593 mpz_init_set_ui(zval, 0UL);
5594 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
5595 mpz_clear(zval);
5597 Temporary_reference_expression* tref =
5598 Expression::make_temporary_reference(index_temp, loc);
5599 tref->set_is_lvalue();
5600 Statement* s = Statement::make_assignment(tref, zexpr, loc);
5601 init->add_statement(s);
5603 *pinit = init;
5605 // Set *PCOND to
5606 // index_temp < len_temp
5608 ref = Expression::make_temporary_reference(index_temp, loc);
5609 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
5610 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
5612 *pcond = lt;
5614 // Set *PITER_INIT to
5615 // value_temp = range[index_temp]
5617 Block* iter_init = NULL;
5618 if (value_temp != NULL)
5620 iter_init = new Block(body_block, loc);
5622 ref = Expression::make_temporary_reference(for_temp, loc);
5623 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
5624 Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
5626 tref = Expression::make_temporary_reference(value_temp, loc);
5627 tref->set_is_lvalue();
5628 s = Statement::make_assignment(tref, index, loc);
5630 iter_init->add_statement(s);
5632 *piter_init = iter_init;
5634 // Set *PPOST to
5635 // index_temp++
5637 Block* post = new Block(enclosing, loc);
5638 tref = Expression::make_temporary_reference(index_temp, loc);
5639 tref->set_is_lvalue();
5640 s = Statement::make_inc_statement(tref);
5641 post->add_statement(s);
5642 *ppost = post;
5645 // Lower a for range over a string.
5647 void
5648 For_range_statement::lower_range_string(Gogo*,
5649 Block* enclosing,
5650 Block* body_block,
5651 Named_object* range_object,
5652 Temporary_statement* range_temp,
5653 Temporary_statement* index_temp,
5654 Temporary_statement* value_temp,
5655 Block** pinit,
5656 Expression** pcond,
5657 Block** piter_init,
5658 Block** ppost)
5660 Location loc = this->location();
5662 // The loop we generate:
5663 // var next_index_temp int
5664 // for index_temp = 0; ; index_temp = next_index_temp {
5665 // next_index_temp, value_temp = stringiter2(range, index_temp)
5666 // if next_index_temp == 0 {
5667 // break
5668 // }
5669 // index = index_temp
5670 // value = value_temp
5671 // original body
5672 // }
5674 // Set *PINIT to
5675 // var next_index_temp int
5676 // index_temp = 0
5678 Block* init = new Block(enclosing, loc);
5680 Temporary_statement* next_index_temp =
5681 Statement::make_temporary(index_temp->type(), NULL, loc);
5682 init->add_statement(next_index_temp);
5684 mpz_t zval;
5685 mpz_init_set_ui(zval, 0UL);
5686 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
5688 Temporary_reference_expression* ref =
5689 Expression::make_temporary_reference(index_temp, loc);
5690 ref->set_is_lvalue();
5691 Statement* s = Statement::make_assignment(ref, zexpr, loc);
5693 init->add_statement(s);
5694 *pinit = init;
5696 // The loop has no condition.
5698 *pcond = NULL;
5700 // Set *PITER_INIT to
5701 // next_index_temp = runtime.stringiter(range, index_temp)
5702 // or
5703 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
5704 // followed by
5705 // if next_index_temp == 0 {
5706 // break
5707 // }
5709 Block* iter_init = new Block(body_block, loc);
5711 Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
5712 Expression* p2 = Expression::make_temporary_reference(index_temp, loc);
5713 Call_expression* call = Runtime::make_call((value_temp == NULL
5714 ? Runtime::STRINGITER
5715 : Runtime::STRINGITER2),
5716 loc, 2, p1, p2);
5718 if (value_temp == NULL)
5720 ref = Expression::make_temporary_reference(next_index_temp, loc);
5721 ref->set_is_lvalue();
5722 s = Statement::make_assignment(ref, call, loc);
5724 else
5726 Expression_list* lhs = new Expression_list();
5728 ref = Expression::make_temporary_reference(next_index_temp, loc);
5729 ref->set_is_lvalue();
5730 lhs->push_back(ref);
5732 ref = Expression::make_temporary_reference(value_temp, loc);
5733 ref->set_is_lvalue();
5734 lhs->push_back(ref);
5736 Expression_list* rhs = new Expression_list();
5737 rhs->push_back(Expression::make_call_result(call, 0));
5738 rhs->push_back(Expression::make_call_result(call, 1));
5740 s = Statement::make_tuple_assignment(lhs, rhs, loc);
5742 iter_init->add_statement(s);
5744 ref = Expression::make_temporary_reference(next_index_temp, loc);
5745 zexpr = Expression::make_integer(&zval, NULL, loc);
5746 mpz_clear(zval);
5747 Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
5749 Block* then_block = new Block(iter_init, loc);
5750 s = Statement::make_break_statement(this->break_label(), loc);
5751 then_block->add_statement(s);
5753 s = Statement::make_if_statement(equals, then_block, NULL, loc);
5754 iter_init->add_statement(s);
5756 *piter_init = iter_init;
5758 // Set *PPOST to
5759 // index_temp = next_index_temp
5761 Block* post = new Block(enclosing, loc);
5763 Temporary_reference_expression* lhs =
5764 Expression::make_temporary_reference(index_temp, loc);
5765 lhs->set_is_lvalue();
5766 Expression* rhs = Expression::make_temporary_reference(next_index_temp, loc);
5767 s = Statement::make_assignment(lhs, rhs, loc);
5769 post->add_statement(s);
5770 *ppost = post;
5773 // Lower a for range over a map.
5775 void
5776 For_range_statement::lower_range_map(Gogo*,
5777 Block* enclosing,
5778 Block* body_block,
5779 Named_object* range_object,
5780 Temporary_statement* range_temp,
5781 Temporary_statement* index_temp,
5782 Temporary_statement* value_temp,
5783 Block** pinit,
5784 Expression** pcond,
5785 Block** piter_init,
5786 Block** ppost)
5788 Location loc = this->location();
5790 // The runtime uses a struct to handle ranges over a map. The
5791 // struct is four pointers long. The first pointer is NULL when we
5792 // have completed the iteration.
5794 // The loop we generate:
5795 // var hiter map_iteration_struct
5796 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
5797 // mapiter2(hiter, &index_temp, &value_temp)
5798 // index = index_temp
5799 // value = value_temp
5800 // original body
5801 // }
5803 // Set *PINIT to
5804 // var hiter map_iteration_struct
5805 // runtime.mapiterinit(range, &hiter)
5807 Block* init = new Block(enclosing, loc);
5809 Type* map_iteration_type = Runtime::map_iteration_type();
5810 Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
5811 NULL, loc);
5812 init->add_statement(hiter);
5814 Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
5815 Expression* ref = Expression::make_temporary_reference(hiter, loc);
5816 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
5817 Expression* call = Runtime::make_call(Runtime::MAPITERINIT, loc, 2, p1, p2);
5818 init->add_statement(Statement::make_statement(call, true));
5820 *pinit = init;
5822 // Set *PCOND to
5823 // hiter[0] != nil
5825 ref = Expression::make_temporary_reference(hiter, loc);
5827 mpz_t zval;
5828 mpz_init_set_ui(zval, 0UL);
5829 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
5830 mpz_clear(zval);
5832 Expression* index = Expression::make_index(ref, zexpr, NULL, NULL, loc);
5834 Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
5835 Expression::make_nil(loc),
5836 loc);
5838 *pcond = ne;
5840 // Set *PITER_INIT to
5841 // mapiter1(hiter, &index_temp)
5842 // or
5843 // mapiter2(hiter, &index_temp, &value_temp)
5845 Block* iter_init = new Block(body_block, loc);
5847 ref = Expression::make_temporary_reference(hiter, loc);
5848 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
5849 ref = Expression::make_temporary_reference(index_temp, loc);
5850 p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
5851 if (value_temp == NULL)
5852 call = Runtime::make_call(Runtime::MAPITER1, loc, 2, p1, p2);
5853 else
5855 ref = Expression::make_temporary_reference(value_temp, loc);
5856 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
5857 call = Runtime::make_call(Runtime::MAPITER2, loc, 3, p1, p2, p3);
5859 iter_init->add_statement(Statement::make_statement(call, true));
5861 *piter_init = iter_init;
5863 // Set *PPOST to
5864 // mapiternext(&hiter)
5866 Block* post = new Block(enclosing, loc);
5868 ref = Expression::make_temporary_reference(hiter, loc);
5869 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
5870 call = Runtime::make_call(Runtime::MAPITERNEXT, loc, 1, p1);
5871 post->add_statement(Statement::make_statement(call, true));
5873 *ppost = post;
5876 // Lower a for range over a channel.
5878 void
5879 For_range_statement::lower_range_channel(Gogo*,
5880 Block*,
5881 Block* body_block,
5882 Named_object* range_object,
5883 Temporary_statement* range_temp,
5884 Temporary_statement* index_temp,
5885 Temporary_statement* value_temp,
5886 Block** pinit,
5887 Expression** pcond,
5888 Block** piter_init,
5889 Block** ppost)
5891 go_assert(value_temp == NULL);
5893 Location loc = this->location();
5895 // The loop we generate:
5896 // for {
5897 // index_temp, ok_temp = <-range
5898 // if !ok_temp {
5899 // break
5900 // }
5901 // index = index_temp
5902 // original body
5903 // }
5905 // We have no initialization code, no condition, and no post code.
5907 *pinit = NULL;
5908 *pcond = NULL;
5909 *ppost = NULL;
5911 // Set *PITER_INIT to
5912 // index_temp, ok_temp = <-range
5913 // if !ok_temp {
5914 // break
5915 // }
5917 Block* iter_init = new Block(body_block, loc);
5919 Temporary_statement* ok_temp =
5920 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
5921 iter_init->add_statement(ok_temp);
5923 Expression* cref = this->make_range_ref(range_object, range_temp, loc);
5924 Temporary_reference_expression* iref =
5925 Expression::make_temporary_reference(index_temp, loc);
5926 iref->set_is_lvalue();
5927 Temporary_reference_expression* oref =
5928 Expression::make_temporary_reference(ok_temp, loc);
5929 oref->set_is_lvalue();
5930 Statement* s = Statement::make_tuple_receive_assignment(iref, oref, cref,
5931 loc);
5932 iter_init->add_statement(s);
5934 Block* then_block = new Block(iter_init, loc);
5935 s = Statement::make_break_statement(this->break_label(), loc);
5936 then_block->add_statement(s);
5938 oref = Expression::make_temporary_reference(ok_temp, loc);
5939 Expression* cond = Expression::make_unary(OPERATOR_NOT, oref, loc);
5940 s = Statement::make_if_statement(cond, then_block, NULL, loc);
5941 iter_init->add_statement(s);
5943 *piter_init = iter_init;
5946 // Return the break LABEL_EXPR.
5948 Unnamed_label*
5949 For_range_statement::break_label()
5951 if (this->break_label_ == NULL)
5952 this->break_label_ = new Unnamed_label(this->location());
5953 return this->break_label_;
5956 // Return the continue LABEL_EXPR.
5958 Unnamed_label*
5959 For_range_statement::continue_label()
5961 if (this->continue_label_ == NULL)
5962 this->continue_label_ = new Unnamed_label(this->location());
5963 return this->continue_label_;
5966 // Dump the AST representation for a for range statement.
5968 void
5969 For_range_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
5972 ast_dump_context->print_indent();
5973 ast_dump_context->ostream() << "for ";
5974 ast_dump_context->dump_expression(this->index_var_);
5975 if (this->value_var_ != NULL)
5977 ast_dump_context->ostream() << ", ";
5978 ast_dump_context->dump_expression(this->value_var_);
5981 ast_dump_context->ostream() << " = range ";
5982 ast_dump_context->dump_expression(this->range_);
5983 if (ast_dump_context->dump_subblocks())
5985 ast_dump_context->ostream() << " {" << std::endl;
5987 ast_dump_context->indent();
5989 ast_dump_context->dump_block(this->statements_);
5991 ast_dump_context->unindent();
5992 ast_dump_context->print_indent();
5993 ast_dump_context->ostream() << "}";
5995 ast_dump_context->ostream() << std::endl;
5998 // Make a for statement with a range clause.
6000 For_range_statement*
6001 Statement::make_for_range_statement(Expression* index_var,
6002 Expression* value_var,
6003 Expression* range,
6004 Location location)
6006 return new For_range_statement(index_var, value_var, range, location);