compiler: fix test for mismatch between function results and uses
[official-gcc.git] / gcc / go / gofrontend / statements.cc
blobc407591170a3f95254e89df304ca82c62478e2c8
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 bool ok;
413 if (this->are_hidden_fields_ok_)
414 ok = Type::are_assignable_hidden_ok(this->type_, this->init_->type(),
415 &reason);
416 else
417 ok = Type::are_assignable(this->type_, this->init_->type(), &reason);
418 if (!ok)
420 if (reason.empty())
421 error_at(this->location(), "incompatible types in assignment");
422 else
423 error_at(this->location(), "incompatible types in assignment (%s)",
424 reason.c_str());
425 this->set_is_error();
430 // Convert to backend representation.
432 Bstatement*
433 Temporary_statement::do_get_backend(Translate_context* context)
435 go_assert(this->bvariable_ == NULL);
437 Named_object* function = context->function();
438 go_assert(function != NULL);
439 Bfunction* bfunction = function->func_value()->get_decl();
440 Btype* btype = this->type()->get_backend(context->gogo());
442 Bexpression* binit;
443 if (this->init_ == NULL)
444 binit = NULL;
445 else if (this->type_ == NULL)
446 binit = this->init_->get_backend(context);
447 else
449 Expression* init = Expression::make_cast(this->type_, this->init_,
450 this->location());
451 context->gogo()->lower_expression(context->function(), NULL, &init);
452 binit = init->get_backend(context);
455 Bstatement* statement;
456 this->bvariable_ =
457 context->backend()->temporary_variable(bfunction, context->bblock(),
458 btype, binit,
459 this->is_address_taken_,
460 this->location(), &statement);
461 return statement;
464 // Return the backend variable.
466 Bvariable*
467 Temporary_statement::get_backend_variable(Translate_context* context) const
469 if (this->bvariable_ == NULL)
471 go_assert(saw_errors());
472 return context->backend()->error_variable();
474 return this->bvariable_;
477 // Dump the AST represemtation for a temporary statement
479 void
480 Temporary_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
482 ast_dump_context->print_indent();
483 ast_dump_context->dump_temp_variable_name(this);
484 if (this->type_ != NULL)
486 ast_dump_context->ostream() << " ";
487 ast_dump_context->dump_type(this->type_);
489 if (this->init_ != NULL)
491 ast_dump_context->ostream() << " = ";
492 ast_dump_context->dump_expression(this->init_);
494 ast_dump_context->ostream() << std::endl;
497 // Make and initialize a temporary variable in BLOCK.
499 Temporary_statement*
500 Statement::make_temporary(Type* type, Expression* init,
501 Location location)
503 return new Temporary_statement(type, init, location);
506 // An assignment statement.
508 class Assignment_statement : public Statement
510 public:
511 Assignment_statement(Expression* lhs, Expression* rhs,
512 Location location)
513 : Statement(STATEMENT_ASSIGNMENT, location),
514 lhs_(lhs), rhs_(rhs), are_hidden_fields_ok_(false)
517 // Note that it is OK for this assignment statement to set hidden
518 // fields.
519 void
520 set_hidden_fields_are_ok()
521 { this->are_hidden_fields_ok_ = true; }
523 protected:
525 do_traverse(Traverse* traverse);
527 bool
528 do_traverse_assignments(Traverse_assignments*);
530 void
531 do_determine_types();
533 void
534 do_check_types(Gogo*);
536 Bstatement*
537 do_get_backend(Translate_context*);
539 void
540 do_dump_statement(Ast_dump_context*) const;
542 private:
543 // Left hand side--the lvalue.
544 Expression* lhs_;
545 // Right hand side--the rvalue.
546 Expression* rhs_;
547 // True if this statement may set hidden fields in the assignment
548 // statement. This is used for generated method stubs.
549 bool are_hidden_fields_ok_;
552 // Traversal.
555 Assignment_statement::do_traverse(Traverse* traverse)
557 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
558 return TRAVERSE_EXIT;
559 return this->traverse_expression(traverse, &this->rhs_);
562 bool
563 Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
565 tassign->assignment(&this->lhs_, &this->rhs_);
566 return true;
569 // Set types for the assignment.
571 void
572 Assignment_statement::do_determine_types()
574 this->lhs_->determine_type_no_context();
575 Type* rhs_context_type = this->lhs_->type();
576 if (rhs_context_type->is_sink_type())
577 rhs_context_type = NULL;
578 Type_context context(rhs_context_type, false);
579 this->rhs_->determine_type(&context);
582 // Check types for an assignment.
584 void
585 Assignment_statement::do_check_types(Gogo*)
587 // The left hand side must be either addressable, a map index
588 // expression, or the blank identifier.
589 if (!this->lhs_->is_addressable()
590 && this->lhs_->map_index_expression() == NULL
591 && !this->lhs_->is_sink_expression())
593 if (!this->lhs_->type()->is_error())
594 this->report_error(_("invalid left hand side of assignment"));
595 return;
598 Type* lhs_type = this->lhs_->type();
599 Type* rhs_type = this->rhs_->type();
601 // Invalid assignment of nil to the blank identifier.
602 if (lhs_type->is_sink_type()
603 && rhs_type->is_nil_type())
605 this->report_error(_("use of untyped nil"));
606 return;
609 std::string reason;
610 bool ok;
611 if (this->are_hidden_fields_ok_)
612 ok = Type::are_assignable_hidden_ok(lhs_type, rhs_type, &reason);
613 else
614 ok = Type::are_assignable(lhs_type, rhs_type, &reason);
615 if (!ok)
617 if (reason.empty())
618 error_at(this->location(), "incompatible types in assignment");
619 else
620 error_at(this->location(), "incompatible types in assignment (%s)",
621 reason.c_str());
622 this->set_is_error();
625 if (lhs_type->is_error() || rhs_type->is_error())
626 this->set_is_error();
629 // Convert an assignment statement to the backend representation.
631 Bstatement*
632 Assignment_statement::do_get_backend(Translate_context* context)
634 if (this->lhs_->is_sink_expression())
636 Bexpression* rhs = this->rhs_->get_backend(context);
637 return context->backend()->expression_statement(rhs);
640 Bexpression* lhs = this->lhs_->get_backend(context);
641 Expression* conv =
642 Expression::convert_for_assignment(context->gogo(), this->lhs_->type(),
643 this->rhs_, this->location());
644 Bexpression* rhs = conv->get_backend(context);
645 return context->backend()->assignment_statement(lhs, rhs, this->location());
648 // Dump the AST representation for an assignment statement.
650 void
651 Assignment_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
652 const
654 ast_dump_context->print_indent();
655 ast_dump_context->dump_expression(this->lhs_);
656 ast_dump_context->ostream() << " = " ;
657 ast_dump_context->dump_expression(this->rhs_);
658 ast_dump_context->ostream() << std::endl;
661 // Make an assignment statement.
663 Statement*
664 Statement::make_assignment(Expression* lhs, Expression* rhs,
665 Location location)
667 return new Assignment_statement(lhs, rhs, location);
670 // The Move_subexpressions class is used to move all top-level
671 // subexpressions of an expression. This is used for things like
672 // index expressions in which we must evaluate the index value before
673 // it can be changed by a multiple assignment.
675 class Move_subexpressions : public Traverse
677 public:
678 Move_subexpressions(int skip, Block* block)
679 : Traverse(traverse_expressions),
680 skip_(skip), block_(block)
683 protected:
685 expression(Expression**);
687 private:
688 // The number of subexpressions to skip moving. This is used to
689 // avoid moving the array itself, as we only need to move the index.
690 int skip_;
691 // The block where new temporary variables should be added.
692 Block* block_;
696 Move_subexpressions::expression(Expression** pexpr)
698 if (this->skip_ > 0)
699 --this->skip_;
700 else if ((*pexpr)->temporary_reference_expression() == NULL)
702 Location loc = (*pexpr)->location();
703 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
704 this->block_->add_statement(temp);
705 *pexpr = Expression::make_temporary_reference(temp, loc);
707 // We only need to move top-level subexpressions.
708 return TRAVERSE_SKIP_COMPONENTS;
711 // The Move_ordered_evals class is used to find any subexpressions of
712 // an expression that have an evaluation order dependency. It creates
713 // temporary variables to hold them.
715 class Move_ordered_evals : public Traverse
717 public:
718 Move_ordered_evals(Block* block)
719 : Traverse(traverse_expressions),
720 block_(block)
723 protected:
725 expression(Expression**);
727 private:
728 // The block where new temporary variables should be added.
729 Block* block_;
733 Move_ordered_evals::expression(Expression** pexpr)
735 // We have to look at subexpressions first.
736 if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
737 return TRAVERSE_EXIT;
739 int i;
740 if ((*pexpr)->must_eval_subexpressions_in_order(&i))
742 Move_subexpressions ms(i, this->block_);
743 if ((*pexpr)->traverse_subexpressions(&ms) == TRAVERSE_EXIT)
744 return TRAVERSE_EXIT;
747 if ((*pexpr)->must_eval_in_order())
749 Location loc = (*pexpr)->location();
750 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
751 this->block_->add_statement(temp);
752 *pexpr = Expression::make_temporary_reference(temp, loc);
754 return TRAVERSE_SKIP_COMPONENTS;
757 // An assignment operation statement.
759 class Assignment_operation_statement : public Statement
761 public:
762 Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
763 Location location)
764 : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
765 op_(op), lhs_(lhs), rhs_(rhs)
768 protected:
770 do_traverse(Traverse*);
772 bool
773 do_traverse_assignments(Traverse_assignments*)
774 { go_unreachable(); }
776 Statement*
777 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
779 Bstatement*
780 do_get_backend(Translate_context*)
781 { go_unreachable(); }
783 void
784 do_dump_statement(Ast_dump_context*) const;
786 private:
787 // The operator (OPERATOR_PLUSEQ, etc.).
788 Operator op_;
789 // Left hand side.
790 Expression* lhs_;
791 // Right hand side.
792 Expression* rhs_;
795 // Traversal.
798 Assignment_operation_statement::do_traverse(Traverse* traverse)
800 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
801 return TRAVERSE_EXIT;
802 return this->traverse_expression(traverse, &this->rhs_);
805 // Lower an assignment operation statement to a regular assignment
806 // statement.
808 Statement*
809 Assignment_operation_statement::do_lower(Gogo*, Named_object*,
810 Block* enclosing, Statement_inserter*)
812 Location loc = this->location();
814 // We have to evaluate the left hand side expression only once. We
815 // do this by moving out any expression with side effects.
816 Block* b = new Block(enclosing, loc);
817 Move_ordered_evals moe(b);
818 this->lhs_->traverse_subexpressions(&moe);
820 Expression* lval = this->lhs_->copy();
822 Operator op;
823 switch (this->op_)
825 case OPERATOR_PLUSEQ:
826 op = OPERATOR_PLUS;
827 break;
828 case OPERATOR_MINUSEQ:
829 op = OPERATOR_MINUS;
830 break;
831 case OPERATOR_OREQ:
832 op = OPERATOR_OR;
833 break;
834 case OPERATOR_XOREQ:
835 op = OPERATOR_XOR;
836 break;
837 case OPERATOR_MULTEQ:
838 op = OPERATOR_MULT;
839 break;
840 case OPERATOR_DIVEQ:
841 op = OPERATOR_DIV;
842 break;
843 case OPERATOR_MODEQ:
844 op = OPERATOR_MOD;
845 break;
846 case OPERATOR_LSHIFTEQ:
847 op = OPERATOR_LSHIFT;
848 break;
849 case OPERATOR_RSHIFTEQ:
850 op = OPERATOR_RSHIFT;
851 break;
852 case OPERATOR_ANDEQ:
853 op = OPERATOR_AND;
854 break;
855 case OPERATOR_BITCLEAREQ:
856 op = OPERATOR_BITCLEAR;
857 break;
858 default:
859 go_unreachable();
862 Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
863 Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
864 if (b->statements()->empty())
866 delete b;
867 return s;
869 else
871 b->add_statement(s);
872 return Statement::make_block_statement(b, loc);
876 // Dump the AST representation for an assignment operation statement
878 void
879 Assignment_operation_statement::do_dump_statement(
880 Ast_dump_context* ast_dump_context) const
882 ast_dump_context->print_indent();
883 ast_dump_context->dump_expression(this->lhs_);
884 ast_dump_context->dump_operator(this->op_);
885 ast_dump_context->dump_expression(this->rhs_);
886 ast_dump_context->ostream() << std::endl;
889 // Make an assignment operation statement.
891 Statement*
892 Statement::make_assignment_operation(Operator op, Expression* lhs,
893 Expression* rhs, Location location)
895 return new Assignment_operation_statement(op, lhs, rhs, location);
898 // A tuple assignment statement. This differs from an assignment
899 // statement in that the right-hand-side expressions are evaluated in
900 // parallel.
902 class Tuple_assignment_statement : public Statement
904 public:
905 Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
906 Location location)
907 : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
908 lhs_(lhs), rhs_(rhs), are_hidden_fields_ok_(false)
911 // Note that it is OK for this assignment statement to set hidden
912 // fields.
913 void
914 set_hidden_fields_are_ok()
915 { this->are_hidden_fields_ok_ = true; }
917 protected:
919 do_traverse(Traverse* traverse);
921 bool
922 do_traverse_assignments(Traverse_assignments*)
923 { go_unreachable(); }
925 Statement*
926 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
928 Bstatement*
929 do_get_backend(Translate_context*)
930 { go_unreachable(); }
932 void
933 do_dump_statement(Ast_dump_context*) const;
935 private:
936 // Left hand side--a list of lvalues.
937 Expression_list* lhs_;
938 // Right hand side--a list of rvalues.
939 Expression_list* rhs_;
940 // True if this statement may set hidden fields in the assignment
941 // statement. This is used for generated method stubs.
942 bool are_hidden_fields_ok_;
945 // Traversal.
948 Tuple_assignment_statement::do_traverse(Traverse* traverse)
950 if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
951 return TRAVERSE_EXIT;
952 return this->traverse_expression_list(traverse, this->rhs_);
955 // Lower a tuple assignment. We use temporary variables to split it
956 // up into a set of single assignments.
958 Statement*
959 Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
960 Statement_inserter*)
962 Location loc = this->location();
964 Block* b = new Block(enclosing, loc);
966 // First move out any subexpressions on the left hand side. The
967 // right hand side will be evaluated in the required order anyhow.
968 Move_ordered_evals moe(b);
969 for (Expression_list::iterator plhs = this->lhs_->begin();
970 plhs != this->lhs_->end();
971 ++plhs)
972 Expression::traverse(&*plhs, &moe);
974 std::vector<Temporary_statement*> temps;
975 temps.reserve(this->lhs_->size());
977 Expression_list::const_iterator prhs = this->rhs_->begin();
978 for (Expression_list::const_iterator plhs = this->lhs_->begin();
979 plhs != this->lhs_->end();
980 ++plhs, ++prhs)
982 go_assert(prhs != this->rhs_->end());
984 if ((*plhs)->is_error_expression()
985 || (*plhs)->type()->is_error()
986 || (*prhs)->is_error_expression()
987 || (*prhs)->type()->is_error())
988 continue;
990 if ((*plhs)->is_sink_expression())
992 if ((*prhs)->type()->is_nil_type())
993 this->report_error(_("use of untyped nil"));
994 else
995 b->add_statement(Statement::make_statement(*prhs, true));
996 continue;
999 Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
1000 *prhs, loc);
1001 if (this->are_hidden_fields_ok_)
1002 temp->set_hidden_fields_are_ok();
1003 b->add_statement(temp);
1004 temps.push_back(temp);
1007 go_assert(prhs == this->rhs_->end());
1009 prhs = this->rhs_->begin();
1010 std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
1011 for (Expression_list::const_iterator plhs = this->lhs_->begin();
1012 plhs != this->lhs_->end();
1013 ++plhs, ++prhs)
1015 if ((*plhs)->is_error_expression()
1016 || (*plhs)->type()->is_error()
1017 || (*prhs)->is_error_expression()
1018 || (*prhs)->type()->is_error())
1019 continue;
1021 if ((*plhs)->is_sink_expression())
1022 continue;
1024 Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
1025 Statement* s = Statement::make_assignment(*plhs, ref, loc);
1026 if (this->are_hidden_fields_ok_)
1028 Assignment_statement* as = static_cast<Assignment_statement*>(s);
1029 as->set_hidden_fields_are_ok();
1031 b->add_statement(s);
1032 ++ptemp;
1034 go_assert(ptemp == temps.end() || saw_errors());
1036 return Statement::make_block_statement(b, loc);
1039 // Dump the AST representation for a tuple assignment statement.
1041 void
1042 Tuple_assignment_statement::do_dump_statement(
1043 Ast_dump_context* ast_dump_context) const
1045 ast_dump_context->print_indent();
1046 ast_dump_context->dump_expression_list(this->lhs_);
1047 ast_dump_context->ostream() << " = ";
1048 ast_dump_context->dump_expression_list(this->rhs_);
1049 ast_dump_context->ostream() << std::endl;
1052 // Make a tuple assignment statement.
1054 Statement*
1055 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
1056 Location location)
1058 return new Tuple_assignment_statement(lhs, rhs, location);
1061 // A tuple assignment from a map index expression.
1062 // v, ok = m[k]
1064 class Tuple_map_assignment_statement : public Statement
1066 public:
1067 Tuple_map_assignment_statement(Expression* val, Expression* present,
1068 Expression* map_index,
1069 Location location)
1070 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
1071 val_(val), present_(present), map_index_(map_index)
1074 protected:
1076 do_traverse(Traverse* traverse);
1078 bool
1079 do_traverse_assignments(Traverse_assignments*)
1080 { go_unreachable(); }
1082 Statement*
1083 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1085 Bstatement*
1086 do_get_backend(Translate_context*)
1087 { go_unreachable(); }
1089 void
1090 do_dump_statement(Ast_dump_context*) const;
1092 private:
1093 // Lvalue which receives the value from the map.
1094 Expression* val_;
1095 // Lvalue which receives whether the key value was present.
1096 Expression* present_;
1097 // The map index expression.
1098 Expression* map_index_;
1101 // Traversal.
1104 Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
1106 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1107 || this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
1108 return TRAVERSE_EXIT;
1109 return this->traverse_expression(traverse, &this->map_index_);
1112 // Lower a tuple map assignment.
1114 Statement*
1115 Tuple_map_assignment_statement::do_lower(Gogo*, Named_object*,
1116 Block* enclosing, Statement_inserter*)
1118 Location loc = this->location();
1120 Map_index_expression* map_index = this->map_index_->map_index_expression();
1121 if (map_index == NULL)
1123 this->report_error(_("expected map index on right hand side"));
1124 return Statement::make_error_statement(loc);
1126 Map_type* map_type = map_index->get_map_type();
1127 if (map_type == NULL)
1128 return Statement::make_error_statement(loc);
1130 Block* b = new Block(enclosing, loc);
1132 // Move out any subexpressions to make sure that functions are
1133 // called in the required order.
1134 Move_ordered_evals moe(b);
1135 this->val_->traverse_subexpressions(&moe);
1136 this->present_->traverse_subexpressions(&moe);
1138 // Copy the key value into a temporary so that we can take its
1139 // address without pushing the value onto the heap.
1141 // var key_temp KEY_TYPE = MAP_INDEX
1142 Temporary_statement* key_temp =
1143 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1144 b->add_statement(key_temp);
1146 // var val_temp VAL_TYPE
1147 Temporary_statement* val_temp =
1148 Statement::make_temporary(map_type->val_type(), NULL, loc);
1149 b->add_statement(val_temp);
1151 // var present_temp bool
1152 Temporary_statement* present_temp =
1153 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
1154 b->add_statement(present_temp);
1156 // present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp, &val_temp)
1157 Expression* a1 = Expression::make_type_descriptor(map_type, loc);
1158 Expression* a2 = map_index->map();
1159 Temporary_reference_expression* ref =
1160 Expression::make_temporary_reference(key_temp, loc);
1161 Expression* a3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1162 ref = Expression::make_temporary_reference(val_temp, loc);
1163 Expression* a4 = Expression::make_unary(OPERATOR_AND, ref, loc);
1164 Expression* call = Runtime::make_call(Runtime::MAPACCESS2, loc, 4,
1165 a1, a2, a3, a4);
1167 ref = Expression::make_temporary_reference(present_temp, loc);
1168 ref->set_is_lvalue();
1169 Statement* s = Statement::make_assignment(ref, call, loc);
1170 b->add_statement(s);
1172 // val = val_temp
1173 ref = Expression::make_temporary_reference(val_temp, loc);
1174 s = Statement::make_assignment(this->val_, ref, loc);
1175 b->add_statement(s);
1177 // present = present_temp
1178 ref = Expression::make_temporary_reference(present_temp, loc);
1179 s = Statement::make_assignment(this->present_, ref, loc);
1180 b->add_statement(s);
1182 return Statement::make_block_statement(b, loc);
1185 // Dump the AST representation for a tuple map assignment statement.
1187 void
1188 Tuple_map_assignment_statement::do_dump_statement(
1189 Ast_dump_context* ast_dump_context) const
1191 ast_dump_context->print_indent();
1192 ast_dump_context->dump_expression(this->val_);
1193 ast_dump_context->ostream() << ", ";
1194 ast_dump_context->dump_expression(this->present_);
1195 ast_dump_context->ostream() << " = ";
1196 ast_dump_context->dump_expression(this->map_index_);
1197 ast_dump_context->ostream() << std::endl;
1200 // Make a map assignment statement which returns a pair of values.
1202 Statement*
1203 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
1204 Expression* map_index,
1205 Location location)
1207 return new Tuple_map_assignment_statement(val, present, map_index, location);
1210 // Assign a pair of entries to a map.
1211 // m[k] = v, p
1213 class Map_assignment_statement : public Statement
1215 public:
1216 Map_assignment_statement(Expression* map_index,
1217 Expression* val, Expression* should_set,
1218 Location location)
1219 : Statement(STATEMENT_MAP_ASSIGNMENT, location),
1220 map_index_(map_index), val_(val), should_set_(should_set)
1223 protected:
1225 do_traverse(Traverse* traverse);
1227 bool
1228 do_traverse_assignments(Traverse_assignments*)
1229 { go_unreachable(); }
1231 Statement*
1232 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1234 Bstatement*
1235 do_get_backend(Translate_context*)
1236 { go_unreachable(); }
1238 void
1239 do_dump_statement(Ast_dump_context*) const;
1241 private:
1242 // A reference to the map index which should be set or deleted.
1243 Expression* map_index_;
1244 // The value to add to the map.
1245 Expression* val_;
1246 // Whether or not to add the value.
1247 Expression* should_set_;
1250 // Traverse a map assignment.
1253 Map_assignment_statement::do_traverse(Traverse* traverse)
1255 if (this->traverse_expression(traverse, &this->map_index_) == TRAVERSE_EXIT
1256 || this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
1257 return TRAVERSE_EXIT;
1258 return this->traverse_expression(traverse, &this->should_set_);
1261 // Lower a map assignment to a function call.
1263 Statement*
1264 Map_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
1265 Statement_inserter*)
1267 Location loc = this->location();
1269 Map_index_expression* map_index = this->map_index_->map_index_expression();
1270 if (map_index == NULL)
1272 this->report_error(_("expected map index on left hand side"));
1273 return Statement::make_error_statement(loc);
1275 Map_type* map_type = map_index->get_map_type();
1276 if (map_type == NULL)
1277 return Statement::make_error_statement(loc);
1279 Block* b = new Block(enclosing, loc);
1281 // Evaluate the map first to get order of evaluation right.
1282 // map_temp := m // we are evaluating m[k] = v, p
1283 Temporary_statement* map_temp = Statement::make_temporary(map_type,
1284 map_index->map(),
1285 loc);
1286 b->add_statement(map_temp);
1288 // var key_temp MAP_KEY_TYPE = k
1289 Temporary_statement* key_temp =
1290 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1291 b->add_statement(key_temp);
1293 // var val_temp MAP_VAL_TYPE = v
1294 Temporary_statement* val_temp =
1295 Statement::make_temporary(map_type->val_type(), this->val_, loc);
1296 b->add_statement(val_temp);
1298 // var insert_temp bool = p
1299 Temporary_statement* insert_temp =
1300 Statement::make_temporary(Type::lookup_bool_type(), this->should_set_,
1301 loc);
1302 b->add_statement(insert_temp);
1304 // mapassign2(map_temp, &key_temp, &val_temp, p)
1305 Expression* p1 = Expression::make_temporary_reference(map_temp, loc);
1306 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
1307 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1308 ref = Expression::make_temporary_reference(val_temp, loc);
1309 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1310 Expression* p4 = Expression::make_temporary_reference(insert_temp, loc);
1311 Expression* call = Runtime::make_call(Runtime::MAPASSIGN2, loc, 4,
1312 p1, p2, p3, p4);
1313 Statement* s = Statement::make_statement(call, true);
1314 b->add_statement(s);
1316 return Statement::make_block_statement(b, loc);
1319 // Dump the AST representation for a map assignment statement.
1321 void
1322 Map_assignment_statement::do_dump_statement(
1323 Ast_dump_context* ast_dump_context) const
1325 ast_dump_context->print_indent();
1326 ast_dump_context->dump_expression(this->map_index_);
1327 ast_dump_context->ostream() << " = ";
1328 ast_dump_context->dump_expression(this->val_);
1329 ast_dump_context->ostream() << ", ";
1330 ast_dump_context->dump_expression(this->should_set_);
1331 ast_dump_context->ostream() << std::endl;
1334 // Make a statement which assigns a pair of entries to a map.
1336 Statement*
1337 Statement::make_map_assignment(Expression* map_index,
1338 Expression* val, Expression* should_set,
1339 Location location)
1341 return new Map_assignment_statement(map_index, val, should_set, location);
1344 // A tuple assignment from a receive statement.
1346 class Tuple_receive_assignment_statement : public Statement
1348 public:
1349 Tuple_receive_assignment_statement(Expression* val, Expression* closed,
1350 Expression* channel, Location location)
1351 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
1352 val_(val), closed_(closed), channel_(channel)
1355 protected:
1357 do_traverse(Traverse* traverse);
1359 bool
1360 do_traverse_assignments(Traverse_assignments*)
1361 { go_unreachable(); }
1363 Statement*
1364 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1366 Bstatement*
1367 do_get_backend(Translate_context*)
1368 { go_unreachable(); }
1370 void
1371 do_dump_statement(Ast_dump_context*) const;
1373 private:
1374 // Lvalue which receives the value from the channel.
1375 Expression* val_;
1376 // Lvalue which receives whether the channel is closed.
1377 Expression* closed_;
1378 // The channel on which we receive the value.
1379 Expression* channel_;
1382 // Traversal.
1385 Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
1387 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1388 || this->traverse_expression(traverse, &this->closed_) == TRAVERSE_EXIT)
1389 return TRAVERSE_EXIT;
1390 return this->traverse_expression(traverse, &this->channel_);
1393 // Lower to a function call.
1395 Statement*
1396 Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
1397 Block* enclosing,
1398 Statement_inserter*)
1400 Location loc = this->location();
1402 Channel_type* channel_type = this->channel_->type()->channel_type();
1403 if (channel_type == NULL)
1405 this->report_error(_("expected channel"));
1406 return Statement::make_error_statement(loc);
1408 if (!channel_type->may_receive())
1410 this->report_error(_("invalid receive on send-only channel"));
1411 return Statement::make_error_statement(loc);
1414 Block* b = new Block(enclosing, loc);
1416 // Make sure that any subexpressions on the left hand side are
1417 // evaluated in the right order.
1418 Move_ordered_evals moe(b);
1419 this->val_->traverse_subexpressions(&moe);
1420 this->closed_->traverse_subexpressions(&moe);
1422 // var val_temp ELEMENT_TYPE
1423 Temporary_statement* val_temp =
1424 Statement::make_temporary(channel_type->element_type(), NULL, loc);
1425 b->add_statement(val_temp);
1427 // var closed_temp bool
1428 Temporary_statement* closed_temp =
1429 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
1430 b->add_statement(closed_temp);
1432 // closed_temp = chanrecv2(type, channel, &val_temp)
1433 Expression* td = Expression::make_type_descriptor(this->channel_->type(),
1434 loc);
1435 Temporary_reference_expression* ref =
1436 Expression::make_temporary_reference(val_temp, loc);
1437 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1438 Expression* call = Runtime::make_call(Runtime::CHANRECV2,
1439 loc, 3, td, this->channel_, p2);
1440 ref = Expression::make_temporary_reference(closed_temp, loc);
1441 ref->set_is_lvalue();
1442 Statement* s = Statement::make_assignment(ref, call, loc);
1443 b->add_statement(s);
1445 // val = val_temp
1446 ref = Expression::make_temporary_reference(val_temp, loc);
1447 s = Statement::make_assignment(this->val_, ref, loc);
1448 b->add_statement(s);
1450 // closed = closed_temp
1451 ref = Expression::make_temporary_reference(closed_temp, loc);
1452 s = Statement::make_assignment(this->closed_, ref, loc);
1453 b->add_statement(s);
1455 return Statement::make_block_statement(b, loc);
1458 // Dump the AST representation for a tuple receive statement.
1460 void
1461 Tuple_receive_assignment_statement::do_dump_statement(
1462 Ast_dump_context* ast_dump_context) const
1464 ast_dump_context->print_indent();
1465 ast_dump_context->dump_expression(this->val_);
1466 ast_dump_context->ostream() << ", ";
1467 ast_dump_context->dump_expression(this->closed_);
1468 ast_dump_context->ostream() << " <- ";
1469 ast_dump_context->dump_expression(this->channel_);
1470 ast_dump_context->ostream() << std::endl;
1473 // Make a nonblocking receive statement.
1475 Statement*
1476 Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
1477 Expression* channel,
1478 Location location)
1480 return new Tuple_receive_assignment_statement(val, closed, channel,
1481 location);
1484 // An assignment to a pair of values from a type guard. This is a
1485 // conditional type guard. v, ok = i.(type).
1487 class Tuple_type_guard_assignment_statement : public Statement
1489 public:
1490 Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
1491 Expression* expr, Type* type,
1492 Location location)
1493 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
1494 val_(val), ok_(ok), expr_(expr), type_(type)
1497 protected:
1499 do_traverse(Traverse*);
1501 bool
1502 do_traverse_assignments(Traverse_assignments*)
1503 { go_unreachable(); }
1505 Statement*
1506 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1508 Bstatement*
1509 do_get_backend(Translate_context*)
1510 { go_unreachable(); }
1512 void
1513 do_dump_statement(Ast_dump_context*) const;
1515 private:
1516 Call_expression*
1517 lower_to_type(Runtime::Function);
1519 void
1520 lower_to_object_type(Block*, Runtime::Function);
1522 // The variable which recieves the converted value.
1523 Expression* val_;
1524 // The variable which receives the indication of success.
1525 Expression* ok_;
1526 // The expression being converted.
1527 Expression* expr_;
1528 // The type to which the expression is being converted.
1529 Type* type_;
1532 // Traverse a type guard tuple assignment.
1535 Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
1537 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1538 || this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
1539 || this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
1540 return TRAVERSE_EXIT;
1541 return this->traverse_expression(traverse, &this->expr_);
1544 // Lower to a function call.
1546 Statement*
1547 Tuple_type_guard_assignment_statement::do_lower(Gogo*, Named_object*,
1548 Block* enclosing,
1549 Statement_inserter*)
1551 Location loc = this->location();
1553 Type* expr_type = this->expr_->type();
1554 if (expr_type->interface_type() == NULL)
1556 if (!expr_type->is_error() && !this->type_->is_error())
1557 this->report_error(_("type assertion only valid for interface types"));
1558 return Statement::make_error_statement(loc);
1561 Block* b = new Block(enclosing, loc);
1563 // Make sure that any subexpressions on the left hand side are
1564 // evaluated in the right order.
1565 Move_ordered_evals moe(b);
1566 this->val_->traverse_subexpressions(&moe);
1567 this->ok_->traverse_subexpressions(&moe);
1569 bool expr_is_empty = expr_type->interface_type()->is_empty();
1570 Call_expression* call;
1571 if (this->type_->interface_type() != NULL)
1573 if (this->type_->interface_type()->is_empty())
1574 call = Runtime::make_call((expr_is_empty
1575 ? Runtime::IFACEE2E2
1576 : Runtime::IFACEI2E2),
1577 loc, 1, this->expr_);
1578 else
1579 call = this->lower_to_type(expr_is_empty
1580 ? Runtime::IFACEE2I2
1581 : Runtime::IFACEI2I2);
1583 else if (this->type_->points_to() != NULL)
1584 call = this->lower_to_type(expr_is_empty
1585 ? Runtime::IFACEE2T2P
1586 : Runtime::IFACEI2T2P);
1587 else
1589 this->lower_to_object_type(b,
1590 (expr_is_empty
1591 ? Runtime::IFACEE2T2
1592 : Runtime::IFACEI2T2));
1593 call = NULL;
1596 if (call != NULL)
1598 Expression* res = Expression::make_call_result(call, 0);
1599 res = Expression::make_unsafe_cast(this->type_, res, loc);
1600 Statement* s = Statement::make_assignment(this->val_, res, loc);
1601 b->add_statement(s);
1603 res = Expression::make_call_result(call, 1);
1604 s = Statement::make_assignment(this->ok_, res, loc);
1605 b->add_statement(s);
1608 return Statement::make_block_statement(b, loc);
1611 // Lower a conversion to a non-empty interface type or a pointer type.
1613 Call_expression*
1614 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code)
1616 Location loc = this->location();
1617 return Runtime::make_call(code, loc, 2,
1618 Expression::make_type_descriptor(this->type_, loc),
1619 this->expr_);
1622 // Lower a conversion to a non-interface non-pointer type.
1624 void
1625 Tuple_type_guard_assignment_statement::lower_to_object_type(
1626 Block* b,
1627 Runtime::Function code)
1629 Location loc = this->location();
1631 // var val_temp TYPE
1632 Temporary_statement* val_temp = Statement::make_temporary(this->type_,
1633 NULL, loc);
1634 b->add_statement(val_temp);
1636 // ok = CODE(type_descriptor, expr, &val_temp)
1637 Expression* p1 = Expression::make_type_descriptor(this->type_, loc);
1638 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1639 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1640 Expression* call = Runtime::make_call(code, loc, 3, p1, this->expr_, p3);
1641 Statement* s = Statement::make_assignment(this->ok_, call, loc);
1642 b->add_statement(s);
1644 // val = val_temp
1645 ref = Expression::make_temporary_reference(val_temp, loc);
1646 s = Statement::make_assignment(this->val_, ref, loc);
1647 b->add_statement(s);
1650 // Dump the AST representation for a tuple type guard statement.
1652 void
1653 Tuple_type_guard_assignment_statement::do_dump_statement(
1654 Ast_dump_context* ast_dump_context) const
1656 ast_dump_context->print_indent();
1657 ast_dump_context->dump_expression(this->val_);
1658 ast_dump_context->ostream() << ", ";
1659 ast_dump_context->dump_expression(this->ok_);
1660 ast_dump_context->ostream() << " = ";
1661 ast_dump_context->dump_expression(this->expr_);
1662 ast_dump_context->ostream() << " . ";
1663 ast_dump_context->dump_type(this->type_);
1664 ast_dump_context->ostream() << std::endl;
1667 // Make an assignment from a type guard to a pair of variables.
1669 Statement*
1670 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
1671 Expression* expr, Type* type,
1672 Location location)
1674 return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
1675 location);
1678 // Class Expression_statement.
1680 // Constructor.
1682 Expression_statement::Expression_statement(Expression* expr, bool is_ignored)
1683 : Statement(STATEMENT_EXPRESSION, expr->location()),
1684 expr_(expr), is_ignored_(is_ignored)
1688 // Determine types.
1690 void
1691 Expression_statement::do_determine_types()
1693 this->expr_->determine_type_no_context();
1696 // Check the types of an expression statement. The only check we do
1697 // is to possibly give an error about discarding the value of the
1698 // expression.
1700 void
1701 Expression_statement::do_check_types(Gogo*)
1703 if (!this->is_ignored_)
1704 this->expr_->discarding_value();
1707 // An expression statement is only a terminating statement if it is
1708 // a call to panic.
1710 bool
1711 Expression_statement::do_may_fall_through() const
1713 const Call_expression* call = this->expr_->call_expression();
1714 if (call != NULL)
1716 const Expression* fn = call->fn();
1717 // panic is still an unknown named object.
1718 const Unknown_expression* ue = fn->unknown_expression();
1719 if (ue != NULL)
1721 Named_object* no = ue->named_object();
1723 if (no->is_unknown())
1724 no = no->unknown_value()->real_named_object();
1725 if (no != NULL)
1727 Function_type* fntype;
1728 if (no->is_function())
1729 fntype = no->func_value()->type();
1730 else if (no->is_function_declaration())
1731 fntype = no->func_declaration_value()->type();
1732 else
1733 fntype = NULL;
1735 // The builtin function panic does not return.
1736 if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
1737 return false;
1741 return true;
1744 // Convert to backend representation.
1746 Bstatement*
1747 Expression_statement::do_get_backend(Translate_context* context)
1749 Bexpression* bexpr = this->expr_->get_backend(context);
1750 return context->backend()->expression_statement(bexpr);
1753 // Dump the AST representation for an expression statement
1755 void
1756 Expression_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
1757 const
1759 ast_dump_context->print_indent();
1760 ast_dump_context->dump_expression(expr_);
1761 ast_dump_context->ostream() << std::endl;
1764 // Make an expression statement from an Expression.
1766 Statement*
1767 Statement::make_statement(Expression* expr, bool is_ignored)
1769 return new Expression_statement(expr, is_ignored);
1772 // A block statement--a list of statements which may include variable
1773 // definitions.
1775 class Block_statement : public Statement
1777 public:
1778 Block_statement(Block* block, Location location)
1779 : Statement(STATEMENT_BLOCK, location),
1780 block_(block)
1783 protected:
1785 do_traverse(Traverse* traverse)
1786 { return this->block_->traverse(traverse); }
1788 void
1789 do_determine_types()
1790 { this->block_->determine_types(); }
1792 bool
1793 do_may_fall_through() const
1794 { return this->block_->may_fall_through(); }
1796 Bstatement*
1797 do_get_backend(Translate_context* context);
1799 void
1800 do_dump_statement(Ast_dump_context*) const;
1802 private:
1803 Block* block_;
1806 // Convert a block to the backend representation of a statement.
1808 Bstatement*
1809 Block_statement::do_get_backend(Translate_context* context)
1811 Bblock* bblock = this->block_->get_backend(context);
1812 return context->backend()->block_statement(bblock);
1815 // Dump the AST for a block statement
1817 void
1818 Block_statement::do_dump_statement(Ast_dump_context*) const
1820 // block statement braces are dumped when traversing.
1823 // Make a block statement.
1825 Statement*
1826 Statement::make_block_statement(Block* block, Location location)
1828 return new Block_statement(block, location);
1831 // An increment or decrement statement.
1833 class Inc_dec_statement : public Statement
1835 public:
1836 Inc_dec_statement(bool is_inc, Expression* expr)
1837 : Statement(STATEMENT_INCDEC, expr->location()),
1838 expr_(expr), is_inc_(is_inc)
1841 protected:
1843 do_traverse(Traverse* traverse)
1844 { return this->traverse_expression(traverse, &this->expr_); }
1846 bool
1847 do_traverse_assignments(Traverse_assignments*)
1848 { go_unreachable(); }
1850 Statement*
1851 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1853 Bstatement*
1854 do_get_backend(Translate_context*)
1855 { go_unreachable(); }
1857 void
1858 do_dump_statement(Ast_dump_context*) const;
1860 private:
1861 // The l-value to increment or decrement.
1862 Expression* expr_;
1863 // Whether to increment or decrement.
1864 bool is_inc_;
1867 // Lower to += or -=.
1869 Statement*
1870 Inc_dec_statement::do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
1872 Location loc = this->location();
1874 mpz_t oval;
1875 mpz_init_set_ui(oval, 1UL);
1876 Expression* oexpr = Expression::make_integer(&oval, NULL, loc);
1877 mpz_clear(oval);
1879 Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
1880 return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
1883 // Dump the AST representation for a inc/dec statement.
1885 void
1886 Inc_dec_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
1888 ast_dump_context->print_indent();
1889 ast_dump_context->dump_expression(expr_);
1890 ast_dump_context->ostream() << (is_inc_? "++": "--") << std::endl;
1893 // Make an increment statement.
1895 Statement*
1896 Statement::make_inc_statement(Expression* expr)
1898 return new Inc_dec_statement(true, expr);
1901 // Make a decrement statement.
1903 Statement*
1904 Statement::make_dec_statement(Expression* expr)
1906 return new Inc_dec_statement(false, expr);
1909 // Class Thunk_statement. This is the base class for go and defer
1910 // statements.
1912 // Constructor.
1914 Thunk_statement::Thunk_statement(Statement_classification classification,
1915 Call_expression* call,
1916 Location location)
1917 : Statement(classification, location),
1918 call_(call), struct_type_(NULL)
1922 // Return whether this is a simple statement which does not require a
1923 // thunk.
1925 bool
1926 Thunk_statement::is_simple(Function_type* fntype) const
1928 // We need a thunk to call a method, or to pass a variable number of
1929 // arguments.
1930 if (fntype->is_method() || fntype->is_varargs())
1931 return false;
1933 // A defer statement requires a thunk to set up for whether the
1934 // function can call recover.
1935 if (this->classification() == STATEMENT_DEFER)
1936 return false;
1938 // We can only permit a single parameter of pointer type.
1939 const Typed_identifier_list* parameters = fntype->parameters();
1940 if (parameters != NULL
1941 && (parameters->size() > 1
1942 || (parameters->size() == 1
1943 && parameters->begin()->type()->points_to() == NULL)))
1944 return false;
1946 // If the function returns multiple values, or returns a type other
1947 // than integer, floating point, or pointer, then it may get a
1948 // hidden first parameter, in which case we need the more
1949 // complicated approach. This is true even though we are going to
1950 // ignore the return value.
1951 const Typed_identifier_list* results = fntype->results();
1952 if (results != NULL
1953 && (results->size() > 1
1954 || (results->size() == 1
1955 && !results->begin()->type()->is_basic_type()
1956 && results->begin()->type()->points_to() == NULL)))
1957 return false;
1959 // If this calls something that is not a simple function, then we
1960 // need a thunk.
1961 Expression* fn = this->call_->call_expression()->fn();
1962 if (fn->func_expression() == NULL)
1963 return false;
1965 // If the function uses a closure, then we need a thunk. FIXME: We
1966 // could accept a zero argument function with a closure.
1967 if (fn->func_expression()->closure() != NULL)
1968 return false;
1970 return true;
1973 // Traverse a thunk statement.
1976 Thunk_statement::do_traverse(Traverse* traverse)
1978 return this->traverse_expression(traverse, &this->call_);
1981 // We implement traverse_assignment for a thunk statement because it
1982 // effectively copies the function call.
1984 bool
1985 Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
1987 Expression* fn = this->call_->call_expression()->fn();
1988 Expression* fn2 = fn;
1989 tassign->value(&fn2, true, false);
1990 return true;
1993 // Determine types in a thunk statement.
1995 void
1996 Thunk_statement::do_determine_types()
1998 this->call_->determine_type_no_context();
2000 // Now that we know the types of the call, build the struct used to
2001 // pass parameters.
2002 Call_expression* ce = this->call_->call_expression();
2003 if (ce == NULL)
2004 return;
2005 Function_type* fntype = ce->get_function_type();
2006 if (fntype != NULL && !this->is_simple(fntype))
2007 this->struct_type_ = this->build_struct(fntype);
2010 // Check types in a thunk statement.
2012 void
2013 Thunk_statement::do_check_types(Gogo*)
2015 if (!this->call_->discarding_value())
2016 return;
2017 Call_expression* ce = this->call_->call_expression();
2018 if (ce == NULL)
2020 if (!this->call_->is_error_expression())
2021 this->report_error("expected call expression");
2022 return;
2026 // The Traverse class used to find and simplify thunk statements.
2028 class Simplify_thunk_traverse : public Traverse
2030 public:
2031 Simplify_thunk_traverse(Gogo* gogo)
2032 : Traverse(traverse_functions | traverse_blocks),
2033 gogo_(gogo), function_(NULL)
2037 function(Named_object*);
2040 block(Block*);
2042 private:
2043 // General IR.
2044 Gogo* gogo_;
2045 // The function we are traversing.
2046 Named_object* function_;
2049 // Keep track of the current function while looking for thunks.
2052 Simplify_thunk_traverse::function(Named_object* no)
2054 go_assert(this->function_ == NULL);
2055 this->function_ = no;
2056 int t = no->func_value()->traverse(this);
2057 this->function_ = NULL;
2058 if (t == TRAVERSE_EXIT)
2059 return t;
2060 return TRAVERSE_SKIP_COMPONENTS;
2063 // Look for thunks in a block.
2066 Simplify_thunk_traverse::block(Block* b)
2068 // The parser ensures that thunk statements always appear at the end
2069 // of a block.
2070 if (b->statements()->size() < 1)
2071 return TRAVERSE_CONTINUE;
2072 Thunk_statement* stat = b->statements()->back()->thunk_statement();
2073 if (stat == NULL)
2074 return TRAVERSE_CONTINUE;
2075 if (stat->simplify_statement(this->gogo_, this->function_, b))
2076 return TRAVERSE_SKIP_COMPONENTS;
2077 return TRAVERSE_CONTINUE;
2080 // Simplify all thunk statements.
2082 void
2083 Gogo::simplify_thunk_statements()
2085 Simplify_thunk_traverse thunk_traverse(this);
2086 this->traverse(&thunk_traverse);
2089 // Return true if the thunk function is a constant, which means that
2090 // it does not need to be passed to the thunk routine.
2092 bool
2093 Thunk_statement::is_constant_function() const
2095 Call_expression* ce = this->call_->call_expression();
2096 Function_type* fntype = ce->get_function_type();
2097 if (fntype == NULL)
2099 go_assert(saw_errors());
2100 return false;
2102 if (fntype->is_builtin())
2103 return true;
2104 Expression* fn = ce->fn();
2105 if (fn->func_expression() != NULL)
2106 return fn->func_expression()->closure() == NULL;
2107 if (fn->interface_field_reference_expression() != NULL)
2108 return true;
2109 return false;
2112 // Simplify complex thunk statements into simple ones. A complicated
2113 // thunk statement is one which takes anything other than zero
2114 // parameters or a single pointer parameter. We rewrite it into code
2115 // which allocates a struct, stores the parameter values into the
2116 // struct, and does a simple go or defer statement which passes the
2117 // struct to a thunk. The thunk does the real call.
2119 bool
2120 Thunk_statement::simplify_statement(Gogo* gogo, Named_object* function,
2121 Block* block)
2123 if (this->classification() == STATEMENT_ERROR)
2124 return false;
2125 if (this->call_->is_error_expression())
2126 return false;
2128 if (this->classification() == STATEMENT_DEFER)
2130 // Make sure that the defer stack exists for the function. We
2131 // will use when converting this statement to the backend
2132 // representation, but we want it to exist when we start
2133 // converting the function.
2134 function->func_value()->defer_stack(this->location());
2137 Call_expression* ce = this->call_->call_expression();
2138 Function_type* fntype = ce->get_function_type();
2139 if (fntype == NULL)
2141 go_assert(saw_errors());
2142 this->set_is_error();
2143 return false;
2145 if (this->is_simple(fntype))
2146 return false;
2148 Expression* fn = ce->fn();
2149 Interface_field_reference_expression* interface_method =
2150 fn->interface_field_reference_expression();
2152 Location location = this->location();
2154 std::string thunk_name = Gogo::thunk_name();
2156 // Build the thunk.
2157 this->build_thunk(gogo, thunk_name);
2159 // Generate code to call the thunk.
2161 // Get the values to store into the struct which is the single
2162 // argument to the thunk.
2164 Expression_list* vals = new Expression_list();
2165 if (!this->is_constant_function())
2166 vals->push_back(fn);
2168 if (interface_method != NULL)
2169 vals->push_back(interface_method->expr());
2171 if (ce->args() != NULL)
2173 for (Expression_list::const_iterator p = ce->args()->begin();
2174 p != ce->args()->end();
2175 ++p)
2176 vals->push_back(*p);
2179 // Build the struct.
2180 Expression* constructor =
2181 Expression::make_struct_composite_literal(this->struct_type_, vals,
2182 location);
2184 // Allocate the initialized struct on the heap.
2185 constructor = Expression::make_heap_expression(constructor, location);
2187 // Look up the thunk.
2188 Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
2189 go_assert(named_thunk != NULL && named_thunk->is_function());
2191 // Build the call.
2192 Expression* func = Expression::make_func_reference(named_thunk, NULL,
2193 location);
2194 Expression_list* params = new Expression_list();
2195 params->push_back(constructor);
2196 Call_expression* call = Expression::make_call(func, params, false, location);
2198 // Build the simple go or defer statement.
2199 Statement* s;
2200 if (this->classification() == STATEMENT_GO)
2201 s = Statement::make_go_statement(call, location);
2202 else if (this->classification() == STATEMENT_DEFER)
2203 s = Statement::make_defer_statement(call, location);
2204 else
2205 go_unreachable();
2207 // The current block should end with the go statement.
2208 go_assert(block->statements()->size() >= 1);
2209 go_assert(block->statements()->back() == this);
2210 block->replace_statement(block->statements()->size() - 1, s);
2212 // We already ran the determine_types pass, so we need to run it now
2213 // for the new statement.
2214 s->determine_types();
2216 // Sanity check.
2217 gogo->check_types_in_block(block);
2219 // Return true to tell the block not to keep looking at statements.
2220 return true;
2223 // Set the name to use for thunk parameter N.
2225 void
2226 Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
2228 snprintf(buf, buflen, "a%d", n);
2231 // Build a new struct type to hold the parameters for a complicated
2232 // thunk statement. FNTYPE is the type of the function call.
2234 Struct_type*
2235 Thunk_statement::build_struct(Function_type* fntype)
2237 Location location = this->location();
2239 Struct_field_list* fields = new Struct_field_list();
2241 Call_expression* ce = this->call_->call_expression();
2242 Expression* fn = ce->fn();
2244 if (!this->is_constant_function())
2246 // The function to call.
2247 fields->push_back(Struct_field(Typed_identifier("fn", fntype,
2248 location)));
2251 // If this thunk statement calls a method on an interface, we pass
2252 // the interface object to the thunk.
2253 Interface_field_reference_expression* interface_method =
2254 fn->interface_field_reference_expression();
2255 if (interface_method != NULL)
2257 Typed_identifier tid("object", interface_method->expr()->type(),
2258 location);
2259 fields->push_back(Struct_field(tid));
2262 // The predeclared recover function has no argument. However, we
2263 // add an argument when building recover thunks. Handle that here.
2264 if (ce->is_recover_call())
2266 fields->push_back(Struct_field(Typed_identifier("can_recover",
2267 Type::lookup_bool_type(),
2268 location)));
2271 const Expression_list* args = ce->args();
2272 if (args != NULL)
2274 int i = 0;
2275 for (Expression_list::const_iterator p = args->begin();
2276 p != args->end();
2277 ++p, ++i)
2279 char buf[50];
2280 this->thunk_field_param(i, buf, sizeof buf);
2281 fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
2282 location)));
2286 return Type::make_struct_type(fields, location);
2289 // Build the thunk we are going to call. This is a brand new, albeit
2290 // artificial, function.
2292 void
2293 Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name)
2295 Location location = this->location();
2297 Call_expression* ce = this->call_->call_expression();
2299 bool may_call_recover = false;
2300 if (this->classification() == STATEMENT_DEFER)
2302 Func_expression* fn = ce->fn()->func_expression();
2303 if (fn == NULL)
2304 may_call_recover = true;
2305 else
2307 const Named_object* no = fn->named_object();
2308 if (!no->is_function())
2309 may_call_recover = true;
2310 else
2311 may_call_recover = no->func_value()->calls_recover();
2315 // Build the type of the thunk. The thunk takes a single parameter,
2316 // which is a pointer to the special structure we build.
2317 const char* const parameter_name = "__go_thunk_parameter";
2318 Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
2319 Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
2320 thunk_parameters->push_back(Typed_identifier(parameter_name,
2321 pointer_to_struct_type,
2322 location));
2324 Typed_identifier_list* thunk_results = NULL;
2325 if (may_call_recover)
2327 // When deferring a function which may call recover, add a
2328 // return value, to disable tail call optimizations which will
2329 // break the way we check whether recover is permitted.
2330 thunk_results = new Typed_identifier_list();
2331 thunk_results->push_back(Typed_identifier("", Type::lookup_bool_type(),
2332 location));
2335 Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
2336 thunk_results,
2337 location);
2339 // Start building the thunk.
2340 Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
2341 location);
2343 gogo->start_block(location);
2345 // For a defer statement, start with a call to
2346 // __go_set_defer_retaddr. */
2347 Label* retaddr_label = NULL;
2348 if (may_call_recover)
2350 retaddr_label = gogo->add_label_reference("retaddr", location, false);
2351 Expression* arg = Expression::make_label_addr(retaddr_label, location);
2352 Expression* call = Runtime::make_call(Runtime::SET_DEFER_RETADDR,
2353 location, 1, arg);
2355 // This is a hack to prevent the middle-end from deleting the
2356 // label.
2357 gogo->start_block(location);
2358 gogo->add_statement(Statement::make_goto_statement(retaddr_label,
2359 location));
2360 Block* then_block = gogo->finish_block(location);
2361 then_block->determine_types();
2363 Statement* s = Statement::make_if_statement(call, then_block, NULL,
2364 location);
2365 s->determine_types();
2366 gogo->add_statement(s);
2369 // Get a reference to the parameter.
2370 Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
2371 go_assert(named_parameter != NULL && named_parameter->is_variable());
2373 // Build the call. Note that the field names are the same as the
2374 // ones used in build_struct.
2375 Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
2376 location);
2377 thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
2378 location);
2380 Interface_field_reference_expression* interface_method =
2381 ce->fn()->interface_field_reference_expression();
2383 Expression* func_to_call;
2384 unsigned int next_index;
2385 if (this->is_constant_function())
2387 func_to_call = ce->fn();
2388 next_index = 0;
2390 else
2392 func_to_call = Expression::make_field_reference(thunk_parameter,
2393 0, location);
2394 next_index = 1;
2397 if (interface_method != NULL)
2399 // The main program passes the interface object.
2400 go_assert(next_index == 0);
2401 Expression* r = Expression::make_field_reference(thunk_parameter, 0,
2402 location);
2403 const std::string& name(interface_method->name());
2404 func_to_call = Expression::make_interface_field_reference(r, name,
2405 location);
2406 next_index = 1;
2409 Expression_list* call_params = new Expression_list();
2410 const Struct_field_list* fields = this->struct_type_->fields();
2411 Struct_field_list::const_iterator p = fields->begin();
2412 for (unsigned int i = 0; i < next_index; ++i)
2413 ++p;
2414 bool is_recover_call = ce->is_recover_call();
2415 Expression* recover_arg = NULL;
2416 for (; p != fields->end(); ++p, ++next_index)
2418 Expression* thunk_param = Expression::make_var_reference(named_parameter,
2419 location);
2420 thunk_param = Expression::make_unary(OPERATOR_MULT, thunk_param,
2421 location);
2422 Expression* param = Expression::make_field_reference(thunk_param,
2423 next_index,
2424 location);
2425 if (!is_recover_call)
2426 call_params->push_back(param);
2427 else
2429 go_assert(call_params->empty());
2430 recover_arg = param;
2434 if (call_params->empty())
2436 delete call_params;
2437 call_params = NULL;
2440 Call_expression* call = Expression::make_call(func_to_call, call_params,
2441 false, location);
2443 // This call expression was already lowered before entering the
2444 // thunk statement. Don't try to lower varargs again, as that will
2445 // cause confusion for, e.g., method calls which already have a
2446 // receiver parameter.
2447 call->set_varargs_are_lowered();
2449 Statement* call_statement = Statement::make_statement(call, true);
2451 gogo->add_statement(call_statement);
2453 // If this is a defer statement, the label comes immediately after
2454 // the call.
2455 if (may_call_recover)
2457 gogo->add_label_definition("retaddr", location);
2459 Expression_list* vals = new Expression_list();
2460 vals->push_back(Expression::make_boolean(false, location));
2461 gogo->add_statement(Statement::make_return_statement(vals, location));
2464 Block* b = gogo->finish_block(location);
2466 gogo->add_block(b, location);
2468 gogo->lower_block(function, b);
2469 gogo->flatten_block(function, b);
2471 // We already ran the determine_types pass, so we need to run it
2472 // just for the call statement now. The other types are known.
2473 call_statement->determine_types();
2475 if (may_call_recover || recover_arg != NULL)
2477 // Dig up the call expression, which may have been changed
2478 // during lowering.
2479 go_assert(call_statement->classification() == STATEMENT_EXPRESSION);
2480 Expression_statement* es =
2481 static_cast<Expression_statement*>(call_statement);
2482 Call_expression* ce = es->expr()->call_expression();
2483 if (ce == NULL)
2484 go_assert(saw_errors());
2485 else
2487 if (may_call_recover)
2488 ce->set_is_deferred();
2489 if (recover_arg != NULL)
2490 ce->set_recover_arg(recover_arg);
2494 // That is all the thunk has to do.
2495 gogo->finish_function(location);
2498 // Get the function and argument expressions.
2500 bool
2501 Thunk_statement::get_fn_and_arg(Expression** pfn, Expression** parg)
2503 if (this->call_->is_error_expression())
2504 return false;
2506 Call_expression* ce = this->call_->call_expression();
2508 Expression* fn = ce->fn();
2509 Func_expression* fe = fn->func_expression();
2510 go_assert(fe != NULL);
2511 *pfn = Expression::make_func_code_reference(fe->named_object(),
2512 fe->location());
2514 const Expression_list* args = ce->args();
2515 if (args == NULL || args->empty())
2516 *parg = Expression::make_nil(this->location());
2517 else
2519 go_assert(args->size() == 1);
2520 *parg = args->front();
2523 return true;
2526 // Class Go_statement.
2528 Bstatement*
2529 Go_statement::do_get_backend(Translate_context* context)
2531 Expression* fn;
2532 Expression* arg;
2533 if (!this->get_fn_and_arg(&fn, &arg))
2534 return context->backend()->error_statement();
2536 Expression* call = Runtime::make_call(Runtime::GO, this->location(), 2,
2537 fn, arg);
2538 Bexpression* bcall = call->get_backend(context);
2539 return context->backend()->expression_statement(bcall);
2542 // Dump the AST representation for go statement.
2544 void
2545 Go_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2547 ast_dump_context->print_indent();
2548 ast_dump_context->ostream() << "go ";
2549 ast_dump_context->dump_expression(this->call());
2550 ast_dump_context->ostream() << std::endl;
2553 // Make a go statement.
2555 Statement*
2556 Statement::make_go_statement(Call_expression* call, Location location)
2558 return new Go_statement(call, location);
2561 // Class Defer_statement.
2563 Bstatement*
2564 Defer_statement::do_get_backend(Translate_context* context)
2566 Expression* fn;
2567 Expression* arg;
2568 if (!this->get_fn_and_arg(&fn, &arg))
2569 return context->backend()->error_statement();
2571 Location loc = this->location();
2572 Expression* ds = context->function()->func_value()->defer_stack(loc);
2574 Expression* call = Runtime::make_call(Runtime::DEFER, loc, 3,
2575 ds, fn, arg);
2576 Bexpression* bcall = call->get_backend(context);
2577 return context->backend()->expression_statement(bcall);
2580 // Dump the AST representation for defer statement.
2582 void
2583 Defer_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2585 ast_dump_context->print_indent();
2586 ast_dump_context->ostream() << "defer ";
2587 ast_dump_context->dump_expression(this->call());
2588 ast_dump_context->ostream() << std::endl;
2591 // Make a defer statement.
2593 Statement*
2594 Statement::make_defer_statement(Call_expression* call,
2595 Location location)
2597 return new Defer_statement(call, location);
2600 // Class Return_statement.
2602 // Traverse assignments. We treat each return value as a top level
2603 // RHS in an expression.
2605 bool
2606 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2608 Expression_list* vals = this->vals_;
2609 if (vals != NULL)
2611 for (Expression_list::iterator p = vals->begin();
2612 p != vals->end();
2613 ++p)
2614 tassign->value(&*p, true, true);
2616 return true;
2619 // Lower a return statement. If we are returning a function call
2620 // which returns multiple values which match the current function,
2621 // split up the call's results. If the return statement lists
2622 // explicit values, implement this statement by assigning the values
2623 // to the result variables and change this statement to a naked
2624 // return. This lets panic/recover work correctly.
2626 Statement*
2627 Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
2628 Statement_inserter*)
2630 if (this->is_lowered_)
2631 return this;
2633 Expression_list* vals = this->vals_;
2634 this->vals_ = NULL;
2635 this->is_lowered_ = true;
2637 Location loc = this->location();
2639 size_t vals_count = vals == NULL ? 0 : vals->size();
2640 Function::Results* results = function->func_value()->result_variables();
2641 size_t results_count = results == NULL ? 0 : results->size();
2643 if (vals_count == 0)
2645 if (results_count > 0 && !function->func_value()->results_are_named())
2647 this->report_error(_("not enough arguments to return"));
2648 return this;
2650 return this;
2653 if (results_count == 0)
2655 this->report_error(_("return with value in function "
2656 "with no return type"));
2657 return this;
2660 // If the current function has multiple return values, and we are
2661 // returning a single call expression, split up the call expression.
2662 if (results_count > 1
2663 && vals->size() == 1
2664 && vals->front()->call_expression() != NULL)
2666 Call_expression* call = vals->front()->call_expression();
2667 call->set_expected_result_count(results_count);
2668 delete vals;
2669 vals = new Expression_list;
2670 for (size_t i = 0; i < results_count; ++i)
2671 vals->push_back(Expression::make_call_result(call, i));
2672 vals_count = results_count;
2675 if (vals_count < results_count)
2677 this->report_error(_("not enough arguments to return"));
2678 return this;
2681 if (vals_count > results_count)
2683 this->report_error(_("too many values in return statement"));
2684 return this;
2687 Block* b = new Block(enclosing, loc);
2689 Expression_list* lhs = new Expression_list();
2690 Expression_list* rhs = new Expression_list();
2692 Expression_list::const_iterator pe = vals->begin();
2693 int i = 1;
2694 for (Function::Results::const_iterator pr = results->begin();
2695 pr != results->end();
2696 ++pr, ++pe, ++i)
2698 Named_object* rv = *pr;
2699 Expression* e = *pe;
2701 // Check types now so that we give a good error message. The
2702 // result type is known. We determine the expression type
2703 // early.
2705 Type *rvtype = rv->result_var_value()->type();
2706 Type_context type_context(rvtype, false);
2707 e->determine_type(&type_context);
2709 std::string reason;
2710 bool ok;
2711 if (this->are_hidden_fields_ok_)
2712 ok = Type::are_assignable_hidden_ok(rvtype, e->type(), &reason);
2713 else
2714 ok = Type::are_assignable(rvtype, e->type(), &reason);
2715 if (ok)
2717 Expression* ve = Expression::make_var_reference(rv, e->location());
2718 lhs->push_back(ve);
2719 rhs->push_back(e);
2721 else
2723 if (reason.empty())
2724 error_at(e->location(), "incompatible type for return value %d", i);
2725 else
2726 error_at(e->location(),
2727 "incompatible type for return value %d (%s)",
2728 i, reason.c_str());
2731 go_assert(lhs->size() == rhs->size());
2733 if (lhs->empty())
2735 else if (lhs->size() == 1)
2737 Statement* s = Statement::make_assignment(lhs->front(), rhs->front(),
2738 loc);
2739 if (this->are_hidden_fields_ok_)
2741 Assignment_statement* as = static_cast<Assignment_statement*>(s);
2742 as->set_hidden_fields_are_ok();
2744 b->add_statement(s);
2745 delete lhs;
2746 delete rhs;
2748 else
2750 Statement* s = Statement::make_tuple_assignment(lhs, rhs, loc);
2751 if (this->are_hidden_fields_ok_)
2753 Tuple_assignment_statement* tas =
2754 static_cast<Tuple_assignment_statement*>(s);
2755 tas->set_hidden_fields_are_ok();
2757 b->add_statement(s);
2760 b->add_statement(this);
2762 delete vals;
2764 return Statement::make_block_statement(b, loc);
2767 // Convert a return statement to the backend representation.
2769 Bstatement*
2770 Return_statement::do_get_backend(Translate_context* context)
2772 Location loc = this->location();
2774 Function* function = context->function()->func_value();
2775 Function::Results* results = function->result_variables();
2776 std::vector<Bexpression*> retvals;
2777 if (results != NULL && !results->empty())
2779 retvals.reserve(results->size());
2780 for (Function::Results::const_iterator p = results->begin();
2781 p != results->end();
2782 p++)
2784 Expression* vr = Expression::make_var_reference(*p, loc);
2785 retvals.push_back(vr->get_backend(context));
2789 return context->backend()->return_statement(function->get_decl(),
2790 retvals, loc);
2793 // Dump the AST representation for a return statement.
2795 void
2796 Return_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2798 ast_dump_context->print_indent();
2799 ast_dump_context->ostream() << "return " ;
2800 ast_dump_context->dump_expression_list(this->vals_);
2801 ast_dump_context->ostream() << std::endl;
2804 // Make a return statement.
2806 Return_statement*
2807 Statement::make_return_statement(Expression_list* vals,
2808 Location location)
2810 return new Return_statement(vals, location);
2813 // Make a statement that returns the result of a call expression.
2815 Statement*
2816 Statement::make_return_from_call(Call_expression* call, Location location)
2818 size_t rc = call->result_count();
2819 if (rc == 0)
2820 return Statement::make_statement(call, true);
2821 else
2823 Expression_list* vals = new Expression_list();
2824 if (rc == 1)
2825 vals->push_back(call);
2826 else
2828 for (size_t i = 0; i < rc; ++i)
2829 vals->push_back(Expression::make_call_result(call, i));
2831 return Statement::make_return_statement(vals, location);
2835 // A break or continue statement.
2837 class Bc_statement : public Statement
2839 public:
2840 Bc_statement(bool is_break, Unnamed_label* label, Location location)
2841 : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
2842 label_(label), is_break_(is_break)
2845 bool
2846 is_break() const
2847 { return this->is_break_; }
2849 protected:
2851 do_traverse(Traverse*)
2852 { return TRAVERSE_CONTINUE; }
2854 bool
2855 do_may_fall_through() const
2856 { return false; }
2858 Bstatement*
2859 do_get_backend(Translate_context* context)
2860 { return this->label_->get_goto(context, this->location()); }
2862 void
2863 do_dump_statement(Ast_dump_context*) const;
2865 private:
2866 // The label that this branches to.
2867 Unnamed_label* label_;
2868 // True if this is "break", false if it is "continue".
2869 bool is_break_;
2872 // Dump the AST representation for a break/continue statement
2874 void
2875 Bc_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2877 ast_dump_context->print_indent();
2878 ast_dump_context->ostream() << (this->is_break_ ? "break" : "continue");
2879 if (this->label_ != NULL)
2881 ast_dump_context->ostream() << " ";
2882 ast_dump_context->dump_label_name(this->label_);
2884 ast_dump_context->ostream() << std::endl;
2887 // Make a break statement.
2889 Statement*
2890 Statement::make_break_statement(Unnamed_label* label, Location location)
2892 return new Bc_statement(true, label, location);
2895 // Make a continue statement.
2897 Statement*
2898 Statement::make_continue_statement(Unnamed_label* label,
2899 Location location)
2901 return new Bc_statement(false, label, location);
2904 // A goto statement.
2906 class Goto_statement : public Statement
2908 public:
2909 Goto_statement(Label* label, Location location)
2910 : Statement(STATEMENT_GOTO, location),
2911 label_(label)
2914 protected:
2916 do_traverse(Traverse*)
2917 { return TRAVERSE_CONTINUE; }
2919 void
2920 do_check_types(Gogo*);
2922 bool
2923 do_may_fall_through() const
2924 { return false; }
2926 Bstatement*
2927 do_get_backend(Translate_context*);
2929 void
2930 do_dump_statement(Ast_dump_context*) const;
2932 private:
2933 Label* label_;
2936 // Check types for a label. There aren't any types per se, but we use
2937 // this to give an error if the label was never defined.
2939 void
2940 Goto_statement::do_check_types(Gogo*)
2942 if (!this->label_->is_defined())
2944 error_at(this->location(), "reference to undefined label %qs",
2945 Gogo::message_name(this->label_->name()).c_str());
2946 this->set_is_error();
2950 // Convert the goto statement to the backend representation.
2952 Bstatement*
2953 Goto_statement::do_get_backend(Translate_context* context)
2955 Blabel* blabel = this->label_->get_backend_label(context);
2956 return context->backend()->goto_statement(blabel, this->location());
2959 // Dump the AST representation for a goto statement.
2961 void
2962 Goto_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2964 ast_dump_context->print_indent();
2965 ast_dump_context->ostream() << "goto " << this->label_->name() << std::endl;
2968 // Make a goto statement.
2970 Statement*
2971 Statement::make_goto_statement(Label* label, Location location)
2973 return new Goto_statement(label, location);
2976 // A goto statement to an unnamed label.
2978 class Goto_unnamed_statement : public Statement
2980 public:
2981 Goto_unnamed_statement(Unnamed_label* label, Location location)
2982 : Statement(STATEMENT_GOTO_UNNAMED, location),
2983 label_(label)
2986 protected:
2988 do_traverse(Traverse*)
2989 { return TRAVERSE_CONTINUE; }
2991 bool
2992 do_may_fall_through() const
2993 { return false; }
2995 Bstatement*
2996 do_get_backend(Translate_context* context)
2997 { return this->label_->get_goto(context, this->location()); }
2999 void
3000 do_dump_statement(Ast_dump_context*) const;
3002 private:
3003 Unnamed_label* label_;
3006 // Dump the AST representation for an unnamed goto statement
3008 void
3009 Goto_unnamed_statement::do_dump_statement(
3010 Ast_dump_context* ast_dump_context) const
3012 ast_dump_context->print_indent();
3013 ast_dump_context->ostream() << "goto ";
3014 ast_dump_context->dump_label_name(this->label_);
3015 ast_dump_context->ostream() << std::endl;
3018 // Make a goto statement to an unnamed label.
3020 Statement*
3021 Statement::make_goto_unnamed_statement(Unnamed_label* label,
3022 Location location)
3024 return new Goto_unnamed_statement(label, location);
3027 // Class Label_statement.
3029 // Traversal.
3032 Label_statement::do_traverse(Traverse*)
3034 return TRAVERSE_CONTINUE;
3037 // Return the backend representation of the statement defining this
3038 // label.
3040 Bstatement*
3041 Label_statement::do_get_backend(Translate_context* context)
3043 Blabel* blabel = this->label_->get_backend_label(context);
3044 return context->backend()->label_definition_statement(blabel);
3047 // Dump the AST for a label definition statement.
3049 void
3050 Label_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3052 ast_dump_context->print_indent();
3053 ast_dump_context->ostream() << this->label_->name() << ":" << std::endl;
3056 // Make a label statement.
3058 Statement*
3059 Statement::make_label_statement(Label* label, Location location)
3061 return new Label_statement(label, location);
3064 // An unnamed label statement.
3066 class Unnamed_label_statement : public Statement
3068 public:
3069 Unnamed_label_statement(Unnamed_label* label)
3070 : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
3071 label_(label)
3074 protected:
3076 do_traverse(Traverse*)
3077 { return TRAVERSE_CONTINUE; }
3079 Bstatement*
3080 do_get_backend(Translate_context* context)
3081 { return this->label_->get_definition(context); }
3083 void
3084 do_dump_statement(Ast_dump_context*) const;
3086 private:
3087 // The label.
3088 Unnamed_label* label_;
3091 // Dump the AST representation for an unnamed label definition statement.
3093 void
3094 Unnamed_label_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
3095 const
3097 ast_dump_context->print_indent();
3098 ast_dump_context->dump_label_name(this->label_);
3099 ast_dump_context->ostream() << ":" << std::endl;
3102 // Make an unnamed label statement.
3104 Statement*
3105 Statement::make_unnamed_label_statement(Unnamed_label* label)
3107 return new Unnamed_label_statement(label);
3110 // An if statement.
3112 class If_statement : public Statement
3114 public:
3115 If_statement(Expression* cond, Block* then_block, Block* else_block,
3116 Location location)
3117 : Statement(STATEMENT_IF, location),
3118 cond_(cond), then_block_(then_block), else_block_(else_block)
3121 protected:
3123 do_traverse(Traverse*);
3125 void
3126 do_determine_types();
3128 void
3129 do_check_types(Gogo*);
3131 bool
3132 do_may_fall_through() const;
3134 Bstatement*
3135 do_get_backend(Translate_context*);
3137 void
3138 do_dump_statement(Ast_dump_context*) const;
3140 private:
3141 Expression* cond_;
3142 Block* then_block_;
3143 Block* else_block_;
3146 // Traversal.
3149 If_statement::do_traverse(Traverse* traverse)
3151 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT
3152 || this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
3153 return TRAVERSE_EXIT;
3154 if (this->else_block_ != NULL)
3156 if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
3157 return TRAVERSE_EXIT;
3159 return TRAVERSE_CONTINUE;
3162 void
3163 If_statement::do_determine_types()
3165 Type_context context(Type::lookup_bool_type(), false);
3166 this->cond_->determine_type(&context);
3167 this->then_block_->determine_types();
3168 if (this->else_block_ != NULL)
3169 this->else_block_->determine_types();
3172 // Check types.
3174 void
3175 If_statement::do_check_types(Gogo*)
3177 Type* type = this->cond_->type();
3178 if (type->is_error())
3179 this->set_is_error();
3180 else if (!type->is_boolean_type())
3181 this->report_error(_("expected boolean expression"));
3184 // Whether the overall statement may fall through.
3186 bool
3187 If_statement::do_may_fall_through() const
3189 return (this->else_block_ == NULL
3190 || this->then_block_->may_fall_through()
3191 || this->else_block_->may_fall_through());
3194 // Get the backend representation.
3196 Bstatement*
3197 If_statement::do_get_backend(Translate_context* context)
3199 go_assert(this->cond_->type()->is_boolean_type()
3200 || this->cond_->type()->is_error());
3201 Bexpression* cond = this->cond_->get_backend(context);
3202 Bblock* then_block = this->then_block_->get_backend(context);
3203 Bblock* else_block = (this->else_block_ == NULL
3204 ? NULL
3205 : this->else_block_->get_backend(context));
3206 return context->backend()->if_statement(cond, then_block, else_block,
3207 this->location());
3210 // Dump the AST representation for an if statement
3212 void
3213 If_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3215 ast_dump_context->print_indent();
3216 ast_dump_context->ostream() << "if ";
3217 ast_dump_context->dump_expression(this->cond_);
3218 ast_dump_context->ostream() << std::endl;
3219 if (ast_dump_context->dump_subblocks())
3221 ast_dump_context->dump_block(this->then_block_);
3222 if (this->else_block_ != NULL)
3224 ast_dump_context->print_indent();
3225 ast_dump_context->ostream() << "else" << std::endl;
3226 ast_dump_context->dump_block(this->else_block_);
3231 // Make an if statement.
3233 Statement*
3234 Statement::make_if_statement(Expression* cond, Block* then_block,
3235 Block* else_block, Location location)
3237 return new If_statement(cond, then_block, else_block, location);
3240 // Class Case_clauses::Hash_integer_value.
3242 class Case_clauses::Hash_integer_value
3244 public:
3245 size_t
3246 operator()(Expression*) const;
3249 size_t
3250 Case_clauses::Hash_integer_value::operator()(Expression* pe) const
3252 Numeric_constant nc;
3253 mpz_t ival;
3254 if (!pe->numeric_constant_value(&nc) || !nc.to_int(&ival))
3255 go_unreachable();
3256 size_t ret = mpz_get_ui(ival);
3257 mpz_clear(ival);
3258 return ret;
3261 // Class Case_clauses::Eq_integer_value.
3263 class Case_clauses::Eq_integer_value
3265 public:
3266 bool
3267 operator()(Expression*, Expression*) const;
3270 bool
3271 Case_clauses::Eq_integer_value::operator()(Expression* a, Expression* b) const
3273 Numeric_constant anc;
3274 mpz_t aval;
3275 Numeric_constant bnc;
3276 mpz_t bval;
3277 if (!a->numeric_constant_value(&anc)
3278 || !anc.to_int(&aval)
3279 || !b->numeric_constant_value(&bnc)
3280 || !bnc.to_int(&bval))
3281 go_unreachable();
3282 bool ret = mpz_cmp(aval, bval) == 0;
3283 mpz_clear(aval);
3284 mpz_clear(bval);
3285 return ret;
3288 // Class Case_clauses::Case_clause.
3290 // Traversal.
3293 Case_clauses::Case_clause::traverse(Traverse* traverse)
3295 if (this->cases_ != NULL
3296 && (traverse->traverse_mask()
3297 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3299 if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
3300 return TRAVERSE_EXIT;
3302 if (this->statements_ != NULL)
3304 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3305 return TRAVERSE_EXIT;
3307 return TRAVERSE_CONTINUE;
3310 // Check whether all the case expressions are integer constants.
3312 bool
3313 Case_clauses::Case_clause::is_constant() const
3315 if (this->cases_ != NULL)
3317 for (Expression_list::const_iterator p = this->cases_->begin();
3318 p != this->cases_->end();
3319 ++p)
3320 if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
3321 return false;
3323 return true;
3326 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3327 // value we are switching on; it may be NULL. If START_LABEL is not
3328 // NULL, it goes at the start of the statements, after the condition
3329 // test. We branch to FINISH_LABEL at the end of the statements.
3331 void
3332 Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
3333 Unnamed_label* start_label,
3334 Unnamed_label* finish_label) const
3336 Location loc = this->location_;
3337 Unnamed_label* next_case_label;
3338 if (this->cases_ == NULL || this->cases_->empty())
3340 go_assert(this->is_default_);
3341 next_case_label = NULL;
3343 else
3345 Expression* cond = NULL;
3347 for (Expression_list::const_iterator p = this->cases_->begin();
3348 p != this->cases_->end();
3349 ++p)
3351 Expression* ref = Expression::make_temporary_reference(val_temp,
3352 loc);
3353 Expression* this_cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3354 *p, loc);
3355 if (cond == NULL)
3356 cond = this_cond;
3357 else
3358 cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
3361 Block* then_block = new Block(b, loc);
3362 next_case_label = new Unnamed_label(Linemap::unknown_location());
3363 Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
3364 loc);
3365 then_block->add_statement(s);
3367 // if !COND { goto NEXT_CASE_LABEL }
3368 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3369 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3370 b->add_statement(s);
3373 if (start_label != NULL)
3374 b->add_statement(Statement::make_unnamed_label_statement(start_label));
3376 if (this->statements_ != NULL)
3377 b->add_statement(Statement::make_block_statement(this->statements_, loc));
3379 Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
3380 b->add_statement(s);
3382 if (next_case_label != NULL)
3383 b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
3386 // Determine types.
3388 void
3389 Case_clauses::Case_clause::determine_types(Type* type)
3391 if (this->cases_ != NULL)
3393 Type_context case_context(type, false);
3394 for (Expression_list::iterator p = this->cases_->begin();
3395 p != this->cases_->end();
3396 ++p)
3397 (*p)->determine_type(&case_context);
3399 if (this->statements_ != NULL)
3400 this->statements_->determine_types();
3403 // Check types. Returns false if there was an error.
3405 bool
3406 Case_clauses::Case_clause::check_types(Type* type)
3408 if (this->cases_ != NULL)
3410 for (Expression_list::iterator p = this->cases_->begin();
3411 p != this->cases_->end();
3412 ++p)
3414 if (!Type::are_assignable(type, (*p)->type(), NULL)
3415 && !Type::are_assignable((*p)->type(), type, NULL))
3417 error_at((*p)->location(),
3418 "type mismatch between switch value and case clause");
3419 return false;
3423 return true;
3426 // Return true if this clause may fall through to the following
3427 // statements. Note that this is not the same as whether the case
3428 // uses the "fallthrough" keyword.
3430 bool
3431 Case_clauses::Case_clause::may_fall_through() const
3433 if (this->statements_ == NULL)
3434 return true;
3435 return this->statements_->may_fall_through();
3438 // Convert the case values and statements to the backend
3439 // representation. BREAK_LABEL is the label which break statements
3440 // should branch to. CASE_CONSTANTS is used to detect duplicate
3441 // constants. *CASES should be passed as an empty vector; the values
3442 // for this case will be added to it. If this is the default case,
3443 // *CASES will remain empty. This returns the statement to execute if
3444 // one of these cases is selected.
3446 Bstatement*
3447 Case_clauses::Case_clause::get_backend(Translate_context* context,
3448 Unnamed_label* break_label,
3449 Case_constants* case_constants,
3450 std::vector<Bexpression*>* cases) const
3452 if (this->cases_ != NULL)
3454 go_assert(!this->is_default_);
3455 for (Expression_list::const_iterator p = this->cases_->begin();
3456 p != this->cases_->end();
3457 ++p)
3459 Expression* e = *p;
3460 if (e->classification() != Expression::EXPRESSION_INTEGER)
3462 Numeric_constant nc;
3463 mpz_t ival;
3464 if (!(*p)->numeric_constant_value(&nc) || !nc.to_int(&ival))
3466 // Something went wrong. This can happen with a
3467 // negative constant and an unsigned switch value.
3468 go_assert(saw_errors());
3469 continue;
3471 go_assert(nc.type() != NULL);
3472 e = Expression::make_integer(&ival, nc.type(), e->location());
3473 mpz_clear(ival);
3476 std::pair<Case_constants::iterator, bool> ins =
3477 case_constants->insert(e);
3478 if (!ins.second)
3480 // Value was already present.
3481 error_at(this->location_, "duplicate case in switch");
3482 e = Expression::make_error(this->location_);
3484 cases->push_back(e->get_backend(context));
3488 Bstatement* statements;
3489 if (this->statements_ == NULL)
3490 statements = NULL;
3491 else
3493 Bblock* bblock = this->statements_->get_backend(context);
3494 statements = context->backend()->block_statement(bblock);
3497 Bstatement* break_stat;
3498 if (this->is_fallthrough_)
3499 break_stat = NULL;
3500 else
3501 break_stat = break_label->get_goto(context, this->location_);
3503 if (statements == NULL)
3504 return break_stat;
3505 else if (break_stat == NULL)
3506 return statements;
3507 else
3508 return context->backend()->compound_statement(statements, break_stat);
3511 // Dump the AST representation for a case clause
3513 void
3514 Case_clauses::Case_clause::dump_clause(Ast_dump_context* ast_dump_context)
3515 const
3517 ast_dump_context->print_indent();
3518 if (this->is_default_)
3520 ast_dump_context->ostream() << "default:";
3522 else
3524 ast_dump_context->ostream() << "case ";
3525 ast_dump_context->dump_expression_list(this->cases_);
3526 ast_dump_context->ostream() << ":" ;
3528 ast_dump_context->dump_block(this->statements_);
3529 if (this->is_fallthrough_)
3531 ast_dump_context->print_indent();
3532 ast_dump_context->ostream() << " (fallthrough)" << std::endl;
3536 // Class Case_clauses.
3538 // Traversal.
3541 Case_clauses::traverse(Traverse* traverse)
3543 for (Clauses::iterator p = this->clauses_.begin();
3544 p != this->clauses_.end();
3545 ++p)
3547 if (p->traverse(traverse) == TRAVERSE_EXIT)
3548 return TRAVERSE_EXIT;
3550 return TRAVERSE_CONTINUE;
3553 // Check whether all the case expressions are constant.
3555 bool
3556 Case_clauses::is_constant() const
3558 for (Clauses::const_iterator p = this->clauses_.begin();
3559 p != this->clauses_.end();
3560 ++p)
3561 if (!p->is_constant())
3562 return false;
3563 return true;
3566 // Lower case clauses for a nonconstant switch.
3568 void
3569 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3570 Unnamed_label* break_label) const
3572 // The default case.
3573 const Case_clause* default_case = NULL;
3575 // The label for the fallthrough of the previous case.
3576 Unnamed_label* last_fallthrough_label = NULL;
3578 // The label for the start of the default case. This is used if the
3579 // case before the default case falls through.
3580 Unnamed_label* default_start_label = NULL;
3582 // The label for the end of the default case. This normally winds
3583 // up as BREAK_LABEL, but it will be different if the default case
3584 // falls through.
3585 Unnamed_label* default_finish_label = NULL;
3587 for (Clauses::const_iterator p = this->clauses_.begin();
3588 p != this->clauses_.end();
3589 ++p)
3591 // The label to use for the start of the statements for this
3592 // case. This is NULL unless the previous case falls through.
3593 Unnamed_label* start_label = last_fallthrough_label;
3595 // The label to jump to after the end of the statements for this
3596 // case.
3597 Unnamed_label* finish_label = break_label;
3599 last_fallthrough_label = NULL;
3600 if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3602 finish_label = new Unnamed_label(p->location());
3603 last_fallthrough_label = finish_label;
3606 if (!p->is_default())
3607 p->lower(b, val_temp, start_label, finish_label);
3608 else
3610 // We have to move the default case to the end, so that we
3611 // only use it if all the other tests fail.
3612 default_case = &*p;
3613 default_start_label = start_label;
3614 default_finish_label = finish_label;
3618 if (default_case != NULL)
3619 default_case->lower(b, val_temp, default_start_label,
3620 default_finish_label);
3623 // Determine types.
3625 void
3626 Case_clauses::determine_types(Type* type)
3628 for (Clauses::iterator p = this->clauses_.begin();
3629 p != this->clauses_.end();
3630 ++p)
3631 p->determine_types(type);
3634 // Check types. Returns false if there was an error.
3636 bool
3637 Case_clauses::check_types(Type* type)
3639 bool ret = true;
3640 for (Clauses::iterator p = this->clauses_.begin();
3641 p != this->clauses_.end();
3642 ++p)
3644 if (!p->check_types(type))
3645 ret = false;
3647 return ret;
3650 // Return true if these clauses may fall through to the statements
3651 // following the switch statement.
3653 bool
3654 Case_clauses::may_fall_through() const
3656 bool found_default = false;
3657 for (Clauses::const_iterator p = this->clauses_.begin();
3658 p != this->clauses_.end();
3659 ++p)
3661 if (p->may_fall_through() && !p->is_fallthrough())
3662 return true;
3663 if (p->is_default())
3664 found_default = true;
3666 return !found_default;
3669 // Convert the cases to the backend representation. This sets
3670 // *ALL_CASES and *ALL_STATEMENTS.
3672 void
3673 Case_clauses::get_backend(Translate_context* context,
3674 Unnamed_label* break_label,
3675 std::vector<std::vector<Bexpression*> >* all_cases,
3676 std::vector<Bstatement*>* all_statements) const
3678 Case_constants case_constants;
3680 size_t c = this->clauses_.size();
3681 all_cases->resize(c);
3682 all_statements->resize(c);
3684 size_t i = 0;
3685 for (Clauses::const_iterator p = this->clauses_.begin();
3686 p != this->clauses_.end();
3687 ++p, ++i)
3689 std::vector<Bexpression*> cases;
3690 Bstatement* stat = p->get_backend(context, break_label, &case_constants,
3691 &cases);
3692 (*all_cases)[i].swap(cases);
3693 (*all_statements)[i] = stat;
3697 // Dump the AST representation for case clauses (from a switch statement)
3699 void
3700 Case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
3702 for (Clauses::const_iterator p = this->clauses_.begin();
3703 p != this->clauses_.end();
3704 ++p)
3705 p->dump_clause(ast_dump_context);
3708 // A constant switch statement. A Switch_statement is lowered to this
3709 // when all the cases are constants.
3711 class Constant_switch_statement : public Statement
3713 public:
3714 Constant_switch_statement(Expression* val, Case_clauses* clauses,
3715 Unnamed_label* break_label,
3716 Location location)
3717 : Statement(STATEMENT_CONSTANT_SWITCH, location),
3718 val_(val), clauses_(clauses), break_label_(break_label)
3721 protected:
3723 do_traverse(Traverse*);
3725 void
3726 do_determine_types();
3728 void
3729 do_check_types(Gogo*);
3731 Bstatement*
3732 do_get_backend(Translate_context*);
3734 void
3735 do_dump_statement(Ast_dump_context*) const;
3737 private:
3738 // The value to switch on.
3739 Expression* val_;
3740 // The case clauses.
3741 Case_clauses* clauses_;
3742 // The break label, if needed.
3743 Unnamed_label* break_label_;
3746 // Traversal.
3749 Constant_switch_statement::do_traverse(Traverse* traverse)
3751 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3752 return TRAVERSE_EXIT;
3753 return this->clauses_->traverse(traverse);
3756 // Determine types.
3758 void
3759 Constant_switch_statement::do_determine_types()
3761 this->val_->determine_type_no_context();
3762 this->clauses_->determine_types(this->val_->type());
3765 // Check types.
3767 void
3768 Constant_switch_statement::do_check_types(Gogo*)
3770 if (!this->clauses_->check_types(this->val_->type()))
3771 this->set_is_error();
3774 // Convert to GENERIC.
3776 Bstatement*
3777 Constant_switch_statement::do_get_backend(Translate_context* context)
3779 Bexpression* switch_val_expr = this->val_->get_backend(context);
3781 Unnamed_label* break_label = this->break_label_;
3782 if (break_label == NULL)
3783 break_label = new Unnamed_label(this->location());
3785 std::vector<std::vector<Bexpression*> > all_cases;
3786 std::vector<Bstatement*> all_statements;
3787 this->clauses_->get_backend(context, break_label, &all_cases,
3788 &all_statements);
3790 Bfunction* bfunction = context->function()->func_value()->get_decl();
3791 Bstatement* switch_statement;
3792 switch_statement = context->backend()->switch_statement(bfunction,
3793 switch_val_expr,
3794 all_cases,
3795 all_statements,
3796 this->location());
3797 Bstatement* ldef = break_label->get_definition(context);
3798 return context->backend()->compound_statement(switch_statement, ldef);
3801 // Dump the AST representation for a constant switch statement.
3803 void
3804 Constant_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
3805 const
3807 ast_dump_context->print_indent();
3808 ast_dump_context->ostream() << "switch ";
3809 ast_dump_context->dump_expression(this->val_);
3811 if (ast_dump_context->dump_subblocks())
3813 ast_dump_context->ostream() << " {" << std::endl;
3814 this->clauses_->dump_clauses(ast_dump_context);
3815 ast_dump_context->ostream() << "}";
3818 ast_dump_context->ostream() << std::endl;
3821 // Class Switch_statement.
3823 // Traversal.
3826 Switch_statement::do_traverse(Traverse* traverse)
3828 if (this->val_ != NULL)
3830 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3831 return TRAVERSE_EXIT;
3833 return this->clauses_->traverse(traverse);
3836 // Lower a Switch_statement to a Constant_switch_statement or a series
3837 // of if statements.
3839 Statement*
3840 Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
3841 Statement_inserter*)
3843 Location loc = this->location();
3845 if (this->val_ != NULL
3846 && (this->val_->is_error_expression()
3847 || this->val_->type()->is_error()))
3848 return Statement::make_error_statement(loc);
3850 if (this->val_ != NULL
3851 && this->val_->type()->integer_type() != NULL
3852 && !this->clauses_->empty()
3853 && this->clauses_->is_constant())
3854 return new Constant_switch_statement(this->val_, this->clauses_,
3855 this->break_label_, loc);
3857 if (this->val_ != NULL
3858 && !this->val_->type()->is_comparable()
3859 && !Type::are_compatible_for_comparison(true, this->val_->type(),
3860 Type::make_nil_type(), NULL))
3862 error_at(this->val_->location(),
3863 "cannot switch on value whose type that may not be compared");
3864 return Statement::make_error_statement(loc);
3867 Block* b = new Block(enclosing, loc);
3869 if (this->clauses_->empty())
3871 Expression* val = this->val_;
3872 if (val == NULL)
3873 val = Expression::make_boolean(true, loc);
3874 return Statement::make_statement(val, true);
3877 // var val_temp VAL_TYPE = VAL
3878 Expression* val = this->val_;
3879 if (val == NULL)
3880 val = Expression::make_boolean(true, loc);
3881 Temporary_statement* val_temp = Statement::make_temporary(NULL, val, loc);
3882 b->add_statement(val_temp);
3884 this->clauses_->lower(b, val_temp, this->break_label());
3886 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3887 b->add_statement(s);
3889 return Statement::make_block_statement(b, loc);
3892 // Return the break label for this switch statement, creating it if
3893 // necessary.
3895 Unnamed_label*
3896 Switch_statement::break_label()
3898 if (this->break_label_ == NULL)
3899 this->break_label_ = new Unnamed_label(this->location());
3900 return this->break_label_;
3903 // Dump the AST representation for a switch statement.
3905 void
3906 Switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3908 ast_dump_context->print_indent();
3909 ast_dump_context->ostream() << "switch ";
3910 if (this->val_ != NULL)
3912 ast_dump_context->dump_expression(this->val_);
3914 if (ast_dump_context->dump_subblocks())
3916 ast_dump_context->ostream() << " {" << std::endl;
3917 this->clauses_->dump_clauses(ast_dump_context);
3918 ast_dump_context->print_indent();
3919 ast_dump_context->ostream() << "}";
3921 ast_dump_context->ostream() << std::endl;
3924 // Return whether this switch may fall through.
3926 bool
3927 Switch_statement::do_may_fall_through() const
3929 if (this->clauses_ == NULL)
3930 return true;
3932 // If we have a break label, then some case needed it. That implies
3933 // that the switch statement as a whole can fall through.
3934 if (this->break_label_ != NULL)
3935 return true;
3937 return this->clauses_->may_fall_through();
3940 // Make a switch statement.
3942 Switch_statement*
3943 Statement::make_switch_statement(Expression* val, Location location)
3945 return new Switch_statement(val, location);
3948 // Class Type_case_clauses::Type_case_clause.
3950 // Traversal.
3953 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3955 if (!this->is_default_
3956 && ((traverse->traverse_mask()
3957 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3958 && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3959 return TRAVERSE_EXIT;
3960 if (this->statements_ != NULL)
3961 return this->statements_->traverse(traverse);
3962 return TRAVERSE_CONTINUE;
3965 // Lower one clause in a type switch. Add statements to the block B.
3966 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3967 // BREAK_LABEL is the label at the end of the type switch.
3968 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3969 // statements.
3971 void
3972 Type_case_clauses::Type_case_clause::lower(Type* switch_val_type,
3973 Block* b,
3974 Temporary_statement* descriptor_temp,
3975 Unnamed_label* break_label,
3976 Unnamed_label** stmts_label) const
3978 Location loc = this->location_;
3980 Unnamed_label* next_case_label = NULL;
3981 if (!this->is_default_)
3983 Type* type = this->type_;
3985 std::string reason;
3986 if (switch_val_type->interface_type() != NULL
3987 && !type->is_nil_constant_as_type()
3988 && type->interface_type() == NULL
3989 && !switch_val_type->interface_type()->implements_interface(type,
3990 &reason))
3992 if (reason.empty())
3993 error_at(this->location_, "impossible type switch case");
3994 else
3995 error_at(this->location_, "impossible type switch case (%s)",
3996 reason.c_str());
3999 Expression* ref = Expression::make_temporary_reference(descriptor_temp,
4000 loc);
4002 Expression* cond;
4003 // The language permits case nil, which is of course a constant
4004 // rather than a type. It will appear here as an invalid
4005 // forwarding type.
4006 if (type->is_nil_constant_as_type())
4007 cond = Expression::make_binary(OPERATOR_EQEQ, ref,
4008 Expression::make_nil(loc),
4009 loc);
4010 else
4011 cond = Runtime::make_call((type->interface_type() == NULL
4012 ? Runtime::IFACETYPEEQ
4013 : Runtime::IFACEI2TP),
4014 loc, 2,
4015 Expression::make_type_descriptor(type, loc),
4016 ref);
4018 Unnamed_label* dest;
4019 if (!this->is_fallthrough_)
4021 // if !COND { goto NEXT_CASE_LABEL }
4022 next_case_label = new Unnamed_label(Linemap::unknown_location());
4023 dest = next_case_label;
4024 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
4026 else
4028 // if COND { goto STMTS_LABEL }
4029 go_assert(stmts_label != NULL);
4030 if (*stmts_label == NULL)
4031 *stmts_label = new Unnamed_label(Linemap::unknown_location());
4032 dest = *stmts_label;
4034 Block* then_block = new Block(b, loc);
4035 Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
4036 then_block->add_statement(s);
4037 s = Statement::make_if_statement(cond, then_block, NULL, loc);
4038 b->add_statement(s);
4041 if (this->statements_ != NULL
4042 || (!this->is_fallthrough_
4043 && stmts_label != NULL
4044 && *stmts_label != NULL))
4046 go_assert(!this->is_fallthrough_);
4047 if (stmts_label != NULL && *stmts_label != NULL)
4049 go_assert(!this->is_default_);
4050 if (this->statements_ != NULL)
4051 (*stmts_label)->set_location(this->statements_->start_location());
4052 Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
4053 b->add_statement(s);
4054 *stmts_label = NULL;
4056 if (this->statements_ != NULL)
4057 b->add_statement(Statement::make_block_statement(this->statements_,
4058 loc));
4061 if (this->is_fallthrough_)
4062 go_assert(next_case_label == NULL);
4063 else
4065 Location gloc = (this->statements_ == NULL
4066 ? loc
4067 : this->statements_->end_location());
4068 b->add_statement(Statement::make_goto_unnamed_statement(break_label,
4069 gloc));
4070 if (next_case_label != NULL)
4072 Statement* s =
4073 Statement::make_unnamed_label_statement(next_case_label);
4074 b->add_statement(s);
4079 // Return true if this type clause may fall through to the statements
4080 // following the switch.
4082 bool
4083 Type_case_clauses::Type_case_clause::may_fall_through() const
4085 if (this->is_fallthrough_)
4087 // This case means that we automatically fall through to the
4088 // next case (it's used for T1 in case T1, T2:). It does not
4089 // mean that we fall through to the end of the type switch as a
4090 // whole. There is sure to be a next case and that next case
4091 // will determine whether we fall through to the statements
4092 // after the type switch.
4093 return false;
4095 if (this->statements_ == NULL)
4096 return true;
4097 return this->statements_->may_fall_through();
4100 // Dump the AST representation for a type case clause
4102 void
4103 Type_case_clauses::Type_case_clause::dump_clause(
4104 Ast_dump_context* ast_dump_context) const
4106 ast_dump_context->print_indent();
4107 if (this->is_default_)
4109 ast_dump_context->ostream() << "default:";
4111 else
4113 ast_dump_context->ostream() << "case ";
4114 ast_dump_context->dump_type(this->type_);
4115 ast_dump_context->ostream() << ":" ;
4117 ast_dump_context->dump_block(this->statements_);
4118 if (this->is_fallthrough_)
4120 ast_dump_context->print_indent();
4121 ast_dump_context->ostream() << " (fallthrough)" << std::endl;
4125 // Class Type_case_clauses.
4127 // Traversal.
4130 Type_case_clauses::traverse(Traverse* traverse)
4132 for (Type_clauses::iterator p = this->clauses_.begin();
4133 p != this->clauses_.end();
4134 ++p)
4136 if (p->traverse(traverse) == TRAVERSE_EXIT)
4137 return TRAVERSE_EXIT;
4139 return TRAVERSE_CONTINUE;
4142 // Check for duplicate types.
4144 void
4145 Type_case_clauses::check_duplicates() const
4147 typedef Unordered_set_hash(const Type*, Type_hash_identical,
4148 Type_identical) Types_seen;
4149 Types_seen types_seen;
4150 for (Type_clauses::const_iterator p = this->clauses_.begin();
4151 p != this->clauses_.end();
4152 ++p)
4154 Type* t = p->type();
4155 if (t == NULL)
4156 continue;
4157 if (t->is_nil_constant_as_type())
4158 t = Type::make_nil_type();
4159 std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
4160 if (!ins.second)
4161 error_at(p->location(), "duplicate type in switch");
4165 // Lower the clauses in a type switch. Add statements to the block B.
4166 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4167 // BREAK_LABEL is the label at the end of the type switch.
4169 void
4170 Type_case_clauses::lower(Type* switch_val_type, Block* b,
4171 Temporary_statement* descriptor_temp,
4172 Unnamed_label* break_label) const
4174 const Type_case_clause* default_case = NULL;
4176 Unnamed_label* stmts_label = NULL;
4177 for (Type_clauses::const_iterator p = this->clauses_.begin();
4178 p != this->clauses_.end();
4179 ++p)
4181 if (!p->is_default())
4182 p->lower(switch_val_type, b, descriptor_temp, break_label,
4183 &stmts_label);
4184 else
4186 // We are generating a series of tests, which means that we
4187 // need to move the default case to the end.
4188 default_case = &*p;
4191 go_assert(stmts_label == NULL);
4193 if (default_case != NULL)
4194 default_case->lower(switch_val_type, b, descriptor_temp, break_label,
4195 NULL);
4198 // Return true if these clauses may fall through to the statements
4199 // following the switch statement.
4201 bool
4202 Type_case_clauses::may_fall_through() const
4204 bool found_default = false;
4205 for (Type_clauses::const_iterator p = this->clauses_.begin();
4206 p != this->clauses_.end();
4207 ++p)
4209 if (p->may_fall_through())
4210 return true;
4211 if (p->is_default())
4212 found_default = true;
4214 return !found_default;
4217 // Dump the AST representation for case clauses (from a switch statement)
4219 void
4220 Type_case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
4222 for (Type_clauses::const_iterator p = this->clauses_.begin();
4223 p != this->clauses_.end();
4224 ++p)
4225 p->dump_clause(ast_dump_context);
4228 // Class Type_switch_statement.
4230 // Traversal.
4233 Type_switch_statement::do_traverse(Traverse* traverse)
4235 if (this->var_ == NULL)
4237 if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
4238 return TRAVERSE_EXIT;
4240 if (this->clauses_ != NULL)
4241 return this->clauses_->traverse(traverse);
4242 return TRAVERSE_CONTINUE;
4245 // Lower a type switch statement to a series of if statements. The gc
4246 // compiler is able to generate a table in some cases. However, that
4247 // does not work for us because we may have type descriptors in
4248 // different shared libraries, so we can't compare them with simple
4249 // equality testing.
4251 Statement*
4252 Type_switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
4253 Statement_inserter*)
4255 const Location loc = this->location();
4257 if (this->clauses_ != NULL)
4258 this->clauses_->check_duplicates();
4260 Block* b = new Block(enclosing, loc);
4262 Type* val_type = (this->var_ != NULL
4263 ? this->var_->var_value()->type()
4264 : this->expr_->type());
4266 if (val_type->interface_type() == NULL)
4268 if (!val_type->is_error())
4269 this->report_error(_("cannot type switch on non-interface value"));
4270 return Statement::make_error_statement(loc);
4273 // var descriptor_temp DESCRIPTOR_TYPE
4274 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
4275 Temporary_statement* descriptor_temp =
4276 Statement::make_temporary(descriptor_type, NULL, loc);
4277 b->add_statement(descriptor_temp);
4279 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4280 // inlined.
4281 bool is_empty = val_type->interface_type()->is_empty();
4282 Expression* ref;
4283 if (this->var_ == NULL)
4284 ref = this->expr_;
4285 else
4286 ref = Expression::make_var_reference(this->var_, loc);
4287 Expression* call = Runtime::make_call((is_empty
4288 ? Runtime::EFACETYPE
4289 : Runtime::IFACETYPE),
4290 loc, 1, ref);
4291 Temporary_reference_expression* lhs =
4292 Expression::make_temporary_reference(descriptor_temp, loc);
4293 lhs->set_is_lvalue();
4294 Statement* s = Statement::make_assignment(lhs, call, loc);
4295 b->add_statement(s);
4297 if (this->clauses_ != NULL)
4298 this->clauses_->lower(val_type, b, descriptor_temp, this->break_label());
4300 s = Statement::make_unnamed_label_statement(this->break_label_);
4301 b->add_statement(s);
4303 return Statement::make_block_statement(b, loc);
4306 // Return whether this switch may fall through.
4308 bool
4309 Type_switch_statement::do_may_fall_through() const
4311 if (this->clauses_ == NULL)
4312 return true;
4314 // If we have a break label, then some case needed it. That implies
4315 // that the switch statement as a whole can fall through.
4316 if (this->break_label_ != NULL)
4317 return true;
4319 return this->clauses_->may_fall_through();
4322 // Return the break label for this type switch statement, creating it
4323 // if necessary.
4325 Unnamed_label*
4326 Type_switch_statement::break_label()
4328 if (this->break_label_ == NULL)
4329 this->break_label_ = new Unnamed_label(this->location());
4330 return this->break_label_;
4333 // Dump the AST representation for a type switch statement
4335 void
4336 Type_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
4337 const
4339 ast_dump_context->print_indent();
4340 ast_dump_context->ostream() << "switch " << this->var_->name() << " = ";
4341 ast_dump_context->dump_expression(this->expr_);
4342 ast_dump_context->ostream() << " .(type)";
4343 if (ast_dump_context->dump_subblocks())
4345 ast_dump_context->ostream() << " {" << std::endl;
4346 this->clauses_->dump_clauses(ast_dump_context);
4347 ast_dump_context->ostream() << "}";
4349 ast_dump_context->ostream() << std::endl;
4352 // Make a type switch statement.
4354 Type_switch_statement*
4355 Statement::make_type_switch_statement(Named_object* var, Expression* expr,
4356 Location location)
4358 return new Type_switch_statement(var, expr, location);
4361 // Class Send_statement.
4363 // Traversal.
4366 Send_statement::do_traverse(Traverse* traverse)
4368 if (this->traverse_expression(traverse, &this->channel_) == TRAVERSE_EXIT)
4369 return TRAVERSE_EXIT;
4370 return this->traverse_expression(traverse, &this->val_);
4373 // Determine types.
4375 void
4376 Send_statement::do_determine_types()
4378 this->channel_->determine_type_no_context();
4379 Type* type = this->channel_->type();
4380 Type_context context;
4381 if (type->channel_type() != NULL)
4382 context.type = type->channel_type()->element_type();
4383 this->val_->determine_type(&context);
4386 // Check types.
4388 void
4389 Send_statement::do_check_types(Gogo*)
4391 Type* type = this->channel_->type();
4392 if (type->is_error())
4394 this->set_is_error();
4395 return;
4397 Channel_type* channel_type = type->channel_type();
4398 if (channel_type == NULL)
4400 error_at(this->location(), "left operand of %<<-%> must be channel");
4401 this->set_is_error();
4402 return;
4404 Type* element_type = channel_type->element_type();
4405 if (!Type::are_assignable(element_type, this->val_->type(), NULL))
4407 this->report_error(_("incompatible types in send"));
4408 return;
4410 if (!channel_type->may_send())
4412 this->report_error(_("invalid send on receive-only channel"));
4413 return;
4417 // Convert a send statement to the backend representation.
4419 Bstatement*
4420 Send_statement::do_get_backend(Translate_context* context)
4422 Location loc = this->location();
4424 Channel_type* channel_type = this->channel_->type()->channel_type();
4425 Type* element_type = channel_type->element_type();
4426 Expression* val = Expression::make_cast(element_type, this->val_, loc);
4428 bool is_small;
4429 bool can_take_address;
4430 switch (element_type->base()->classification())
4432 case Type::TYPE_BOOLEAN:
4433 case Type::TYPE_INTEGER:
4434 case Type::TYPE_FUNCTION:
4435 case Type::TYPE_POINTER:
4436 case Type::TYPE_MAP:
4437 case Type::TYPE_CHANNEL:
4438 is_small = true;
4439 can_take_address = false;
4440 break;
4442 case Type::TYPE_FLOAT:
4443 case Type::TYPE_COMPLEX:
4444 case Type::TYPE_STRING:
4445 case Type::TYPE_INTERFACE:
4446 is_small = false;
4447 can_take_address = false;
4448 break;
4450 case Type::TYPE_STRUCT:
4451 is_small = false;
4452 can_take_address = true;
4453 break;
4455 case Type::TYPE_ARRAY:
4456 is_small = false;
4457 can_take_address = !element_type->is_slice_type();
4458 break;
4460 default:
4461 case Type::TYPE_ERROR:
4462 case Type::TYPE_VOID:
4463 case Type::TYPE_SINK:
4464 case Type::TYPE_NIL:
4465 case Type::TYPE_NAMED:
4466 case Type::TYPE_FORWARD:
4467 go_assert(saw_errors());
4468 return context->backend()->error_statement();
4471 // Only try to take the address of a variable. We have already
4472 // moved variables to the heap, so this should not cause that to
4473 // happen unnecessarily.
4474 if (can_take_address
4475 && val->var_expression() == NULL
4476 && val->temporary_reference_expression() == NULL)
4477 can_take_address = false;
4479 Expression* td = Expression::make_type_descriptor(this->channel_->type(),
4480 loc);
4482 Runtime::Function code;
4483 Bstatement* btemp = NULL;
4484 if (is_small)
4486 // Type is small enough to handle as uint64.
4487 code = Runtime::SEND_SMALL;
4488 val = Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
4489 val, loc);
4491 else if (can_take_address)
4493 // Must pass address of value. The function doesn't change the
4494 // value, so just take its address directly.
4495 code = Runtime::SEND_BIG;
4496 val = Expression::make_unary(OPERATOR_AND, val, loc);
4498 else
4500 // Must pass address of value, but the value is small enough
4501 // that it might be in registers. Copy value into temporary
4502 // variable to take address.
4503 code = Runtime::SEND_BIG;
4504 Temporary_statement* temp = Statement::make_temporary(element_type,
4505 val, loc);
4506 Expression* ref = Expression::make_temporary_reference(temp, loc);
4507 val = Expression::make_unary(OPERATOR_AND, ref, loc);
4508 btemp = temp->get_backend(context);
4511 Expression* call = Runtime::make_call(code, loc, 3, td, this->channel_, val);
4513 context->gogo()->lower_expression(context->function(), NULL, &call);
4514 Bexpression* bcall = call->get_backend(context);
4515 Bstatement* s = context->backend()->expression_statement(bcall);
4517 if (btemp == NULL)
4518 return s;
4519 else
4520 return context->backend()->compound_statement(btemp, s);
4523 // Dump the AST representation for a send statement
4525 void
4526 Send_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
4528 ast_dump_context->print_indent();
4529 ast_dump_context->dump_expression(this->channel_);
4530 ast_dump_context->ostream() << " <- ";
4531 ast_dump_context->dump_expression(this->val_);
4532 ast_dump_context->ostream() << std::endl;
4535 // Make a send statement.
4537 Send_statement*
4538 Statement::make_send_statement(Expression* channel, Expression* val,
4539 Location location)
4541 return new Send_statement(channel, val, location);
4544 // Class Select_clauses::Select_clause.
4546 // Traversal.
4549 Select_clauses::Select_clause::traverse(Traverse* traverse)
4551 if (!this->is_lowered_
4552 && (traverse->traverse_mask()
4553 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
4555 if (this->channel_ != NULL)
4557 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
4558 return TRAVERSE_EXIT;
4560 if (this->val_ != NULL)
4562 if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
4563 return TRAVERSE_EXIT;
4565 if (this->closed_ != NULL)
4567 if (Expression::traverse(&this->closed_, traverse) == TRAVERSE_EXIT)
4568 return TRAVERSE_EXIT;
4571 if (this->statements_ != NULL)
4573 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
4574 return TRAVERSE_EXIT;
4576 return TRAVERSE_CONTINUE;
4579 // Lowering. We call a function to register this clause, and arrange
4580 // to set any variables in any receive clause.
4582 void
4583 Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
4584 Block* b, Temporary_statement* sel)
4586 Location loc = this->location_;
4588 Expression* selref = Expression::make_temporary_reference(sel, loc);
4590 mpz_t ival;
4591 mpz_init_set_ui(ival, this->index_);
4592 Expression* index_expr = Expression::make_integer(&ival, NULL, loc);
4593 mpz_clear(ival);
4595 if (this->is_default_)
4597 go_assert(this->channel_ == NULL && this->val_ == NULL);
4598 this->lower_default(b, selref, index_expr);
4599 this->is_lowered_ = true;
4600 return;
4603 // Evaluate the channel before the select statement.
4604 Temporary_statement* channel_temp = Statement::make_temporary(NULL,
4605 this->channel_,
4606 loc);
4607 b->add_statement(channel_temp);
4608 Expression* chanref = Expression::make_temporary_reference(channel_temp,
4609 loc);
4611 if (this->is_send_)
4612 this->lower_send(b, selref, chanref, index_expr);
4613 else
4614 this->lower_recv(gogo, function, b, selref, chanref, index_expr);
4616 // Now all references should be handled through the statements, not
4617 // through here.
4618 this->is_lowered_ = true;
4619 this->val_ = NULL;
4620 this->var_ = NULL;
4623 // Lower a default clause in a select statement.
4625 void
4626 Select_clauses::Select_clause::lower_default(Block* b, Expression* selref,
4627 Expression* index_expr)
4629 Location loc = this->location_;
4630 Expression* call = Runtime::make_call(Runtime::SELECTDEFAULT, loc, 2, selref,
4631 index_expr);
4632 b->add_statement(Statement::make_statement(call, true));
4635 // Lower a send clause in a select statement.
4637 void
4638 Select_clauses::Select_clause::lower_send(Block* b, Expression* selref,
4639 Expression* chanref,
4640 Expression* index_expr)
4642 Location loc = this->location_;
4644 Channel_type* ct = this->channel_->type()->channel_type();
4645 if (ct == NULL)
4646 return;
4648 Type* valtype = ct->element_type();
4650 // Note that copying the value to a temporary here means that we
4651 // evaluate the send values in the required order.
4652 Temporary_statement* val = Statement::make_temporary(valtype, this->val_,
4653 loc);
4654 b->add_statement(val);
4656 Expression* valref = Expression::make_temporary_reference(val, loc);
4657 Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
4659 Expression* call = Runtime::make_call(Runtime::SELECTSEND, loc, 4, selref,
4660 chanref, valaddr, index_expr);
4661 b->add_statement(Statement::make_statement(call, true));
4664 // Lower a receive clause in a select statement.
4666 void
4667 Select_clauses::Select_clause::lower_recv(Gogo* gogo, Named_object* function,
4668 Block* b, Expression* selref,
4669 Expression* chanref,
4670 Expression* index_expr)
4672 Location loc = this->location_;
4674 Channel_type* ct = this->channel_->type()->channel_type();
4675 if (ct == NULL)
4676 return;
4678 Type* valtype = ct->element_type();
4679 Temporary_statement* val = Statement::make_temporary(valtype, NULL, loc);
4680 b->add_statement(val);
4682 Expression* valref = Expression::make_temporary_reference(val, loc);
4683 Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
4685 Temporary_statement* closed_temp = NULL;
4687 Expression* call;
4688 if (this->closed_ == NULL && this->closedvar_ == NULL)
4689 call = Runtime::make_call(Runtime::SELECTRECV, loc, 4, selref, chanref,
4690 valaddr, index_expr);
4691 else
4693 closed_temp = Statement::make_temporary(Type::lookup_bool_type(), NULL,
4694 loc);
4695 b->add_statement(closed_temp);
4696 Expression* cref = Expression::make_temporary_reference(closed_temp,
4697 loc);
4698 Expression* caddr = Expression::make_unary(OPERATOR_AND, cref, loc);
4699 call = Runtime::make_call(Runtime::SELECTRECV2, loc, 5, selref, chanref,
4700 valaddr, caddr, index_expr);
4703 b->add_statement(Statement::make_statement(call, true));
4705 // If the block of statements is executed, arrange for the received
4706 // value to move from VAL to the place where the statements expect
4707 // it.
4709 Block* init = NULL;
4711 if (this->var_ != NULL)
4713 go_assert(this->val_ == NULL);
4714 valref = Expression::make_temporary_reference(val, loc);
4715 this->var_->var_value()->set_init(valref);
4716 this->var_->var_value()->clear_type_from_chan_element();
4718 else if (this->val_ != NULL && !this->val_->is_sink_expression())
4720 init = new Block(b, loc);
4721 valref = Expression::make_temporary_reference(val, loc);
4722 init->add_statement(Statement::make_assignment(this->val_, valref, loc));
4725 if (this->closedvar_ != NULL)
4727 go_assert(this->closed_ == NULL);
4728 Expression* cref = Expression::make_temporary_reference(closed_temp,
4729 loc);
4730 this->closedvar_->var_value()->set_init(cref);
4732 else if (this->closed_ != NULL && !this->closed_->is_sink_expression())
4734 if (init == NULL)
4735 init = new Block(b, loc);
4736 Expression* cref = Expression::make_temporary_reference(closed_temp,
4737 loc);
4738 init->add_statement(Statement::make_assignment(this->closed_, cref,
4739 loc));
4742 if (init != NULL)
4744 gogo->lower_block(function, init);
4746 if (this->statements_ != NULL)
4747 init->add_statement(Statement::make_block_statement(this->statements_,
4748 loc));
4749 this->statements_ = init;
4753 // Determine types.
4755 void
4756 Select_clauses::Select_clause::determine_types()
4758 go_assert(this->is_lowered_);
4759 if (this->statements_ != NULL)
4760 this->statements_->determine_types();
4763 // Check types.
4765 void
4766 Select_clauses::Select_clause::check_types()
4768 if (this->is_default_)
4769 return;
4771 Channel_type* ct = this->channel_->type()->channel_type();
4772 if (ct == NULL)
4774 error_at(this->channel_->location(), "expected channel");
4775 return;
4778 if (this->is_send_ && !ct->may_send())
4779 error_at(this->location(), "invalid send on receive-only channel");
4780 else if (!this->is_send_ && !ct->may_receive())
4781 error_at(this->location(), "invalid receive on send-only channel");
4784 // Whether this clause may fall through to the statement which follows
4785 // the overall select statement.
4787 bool
4788 Select_clauses::Select_clause::may_fall_through() const
4790 if (this->statements_ == NULL)
4791 return true;
4792 return this->statements_->may_fall_through();
4795 // Return the backend representation for the statements to execute.
4797 Bstatement*
4798 Select_clauses::Select_clause::get_statements_backend(
4799 Translate_context* context)
4801 if (this->statements_ == NULL)
4802 return NULL;
4803 Bblock* bblock = this->statements_->get_backend(context);
4804 return context->backend()->block_statement(bblock);
4807 // Dump the AST representation for a select case clause
4809 void
4810 Select_clauses::Select_clause::dump_clause(
4811 Ast_dump_context* ast_dump_context) const
4813 ast_dump_context->print_indent();
4814 if (this->is_default_)
4816 ast_dump_context->ostream() << "default:";
4818 else
4820 ast_dump_context->ostream() << "case " ;
4821 if (this->is_send_)
4823 ast_dump_context->dump_expression(this->channel_);
4824 ast_dump_context->ostream() << " <- " ;
4825 if (this->val_ != NULL)
4826 ast_dump_context->dump_expression(this->val_);
4828 else
4830 if (this->val_ != NULL)
4831 ast_dump_context->dump_expression(this->val_);
4832 if (this->closed_ != NULL)
4834 // FIXME: can val_ == NULL and closed_ ! = NULL?
4835 ast_dump_context->ostream() << " , " ;
4836 ast_dump_context->dump_expression(this->closed_);
4838 if (this->closedvar_ != NULL || this->var_ != NULL)
4839 ast_dump_context->ostream() << " := " ;
4841 ast_dump_context->ostream() << " <- " ;
4842 ast_dump_context->dump_expression(this->channel_);
4844 ast_dump_context->ostream() << ":" ;
4846 ast_dump_context->dump_block(this->statements_);
4849 // Class Select_clauses.
4851 // Traversal.
4854 Select_clauses::traverse(Traverse* traverse)
4856 for (Clauses::iterator p = this->clauses_.begin();
4857 p != this->clauses_.end();
4858 ++p)
4860 if (p->traverse(traverse) == TRAVERSE_EXIT)
4861 return TRAVERSE_EXIT;
4863 return TRAVERSE_CONTINUE;
4866 // Lowering. Here we pull out the channel and the send values, to
4867 // enforce the order of evaluation. We also add explicit send and
4868 // receive statements to the clauses.
4870 void
4871 Select_clauses::lower(Gogo* gogo, Named_object* function, Block* b,
4872 Temporary_statement* sel)
4874 for (Clauses::iterator p = this->clauses_.begin();
4875 p != this->clauses_.end();
4876 ++p)
4877 p->lower(gogo, function, b, sel);
4880 // Determine types.
4882 void
4883 Select_clauses::determine_types()
4885 for (Clauses::iterator p = this->clauses_.begin();
4886 p != this->clauses_.end();
4887 ++p)
4888 p->determine_types();
4891 // Check types.
4893 void
4894 Select_clauses::check_types()
4896 for (Clauses::iterator p = this->clauses_.begin();
4897 p != this->clauses_.end();
4898 ++p)
4899 p->check_types();
4902 // Return whether these select clauses fall through to the statement
4903 // following the overall select statement.
4905 bool
4906 Select_clauses::may_fall_through() const
4908 for (Clauses::const_iterator p = this->clauses_.begin();
4909 p != this->clauses_.end();
4910 ++p)
4911 if (p->may_fall_through())
4912 return true;
4913 return false;
4916 // Convert to the backend representation. We have already accumulated
4917 // all the select information. Now we call selectgo, which will
4918 // return the index of the clause to execute.
4920 Bstatement*
4921 Select_clauses::get_backend(Translate_context* context,
4922 Temporary_statement* sel,
4923 Unnamed_label *break_label,
4924 Location location)
4926 size_t count = this->clauses_.size();
4927 std::vector<std::vector<Bexpression*> > cases(count);
4928 std::vector<Bstatement*> clauses(count);
4930 Type* int32_type = Type::lookup_integer_type("int32");
4932 int i = 0;
4933 for (Clauses::iterator p = this->clauses_.begin();
4934 p != this->clauses_.end();
4935 ++p, ++i)
4937 int index = p->index();
4938 mpz_t ival;
4939 mpz_init_set_ui(ival, index);
4940 Expression* index_expr = Expression::make_integer(&ival, int32_type,
4941 location);
4942 mpz_clear(ival);
4943 cases[i].push_back(index_expr->get_backend(context));
4945 Bstatement* s = p->get_statements_backend(context);
4946 Location gloc = (p->statements() == NULL
4947 ? p->location()
4948 : p->statements()->end_location());
4949 Bstatement* g = break_label->get_goto(context, gloc);
4951 if (s == NULL)
4952 clauses[i] = g;
4953 else
4954 clauses[i] = context->backend()->compound_statement(s, g);
4957 Expression* selref = Expression::make_temporary_reference(sel, location);
4958 Expression* call = Runtime::make_call(Runtime::SELECTGO, location, 1,
4959 selref);
4960 context->gogo()->lower_expression(context->function(), NULL, &call);
4961 Bexpression* bcall = call->get_backend(context);
4963 if (count == 0)
4964 return context->backend()->expression_statement(bcall);
4966 std::vector<Bstatement*> statements;
4967 statements.reserve(2);
4969 Bfunction* bfunction = context->function()->func_value()->get_decl();
4970 Bstatement* switch_stmt = context->backend()->switch_statement(bfunction,
4971 bcall,
4972 cases,
4973 clauses,
4974 location);
4975 statements.push_back(switch_stmt);
4977 Bstatement* ldef = break_label->get_definition(context);
4978 statements.push_back(ldef);
4980 return context->backend()->statement_list(statements);
4982 // Dump the AST representation for select clauses.
4984 void
4985 Select_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
4987 for (Clauses::const_iterator p = this->clauses_.begin();
4988 p != this->clauses_.end();
4989 ++p)
4990 p->dump_clause(ast_dump_context);
4993 // Class Select_statement.
4995 // Return the break label for this switch statement, creating it if
4996 // necessary.
4998 Unnamed_label*
4999 Select_statement::break_label()
5001 if (this->break_label_ == NULL)
5002 this->break_label_ = new Unnamed_label(this->location());
5003 return this->break_label_;
5006 // Lower a select statement. This will still return a select
5007 // statement, but it will be modified to implement the order of
5008 // evaluation rules, and to include the send and receive statements as
5009 // explicit statements in the clauses.
5011 Statement*
5012 Select_statement::do_lower(Gogo* gogo, Named_object* function,
5013 Block* enclosing, Statement_inserter*)
5015 if (this->is_lowered_)
5016 return this;
5018 Location loc = this->location();
5020 Block* b = new Block(enclosing, loc);
5022 go_assert(this->sel_ == NULL);
5024 mpz_t ival;
5025 mpz_init_set_ui(ival, this->clauses_->size());
5026 Expression* size_expr = Expression::make_integer(&ival, NULL, loc);
5027 mpz_clear(ival);
5029 Expression* call = Runtime::make_call(Runtime::NEWSELECT, loc, 1, size_expr);
5031 this->sel_ = Statement::make_temporary(NULL, call, loc);
5032 b->add_statement(this->sel_);
5034 this->clauses_->lower(gogo, function, b, this->sel_);
5035 this->is_lowered_ = true;
5036 b->add_statement(this);
5038 return Statement::make_block_statement(b, loc);
5041 // Whether the select statement itself may fall through to the following
5042 // statement.
5044 bool
5045 Select_statement::do_may_fall_through() const
5047 // A select statement is terminating if no break statement
5048 // refers to it and all of its clauses are terminating.
5049 if (this->break_label_ != NULL)
5050 return true;
5051 return this->clauses_->may_fall_through();
5054 // Return the backend representation for a select statement.
5056 Bstatement*
5057 Select_statement::do_get_backend(Translate_context* context)
5059 return this->clauses_->get_backend(context, this->sel_, this->break_label(),
5060 this->location());
5063 // Dump the AST representation for a select statement.
5065 void
5066 Select_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
5068 ast_dump_context->print_indent();
5069 ast_dump_context->ostream() << "select";
5070 if (ast_dump_context->dump_subblocks())
5072 ast_dump_context->ostream() << " {" << std::endl;
5073 this->clauses_->dump_clauses(ast_dump_context);
5074 ast_dump_context->ostream() << "}";
5076 ast_dump_context->ostream() << std::endl;
5079 // Make a select statement.
5081 Select_statement*
5082 Statement::make_select_statement(Location location)
5084 return new Select_statement(location);
5087 // Class For_statement.
5089 // Traversal.
5092 For_statement::do_traverse(Traverse* traverse)
5094 if (this->init_ != NULL)
5096 if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
5097 return TRAVERSE_EXIT;
5099 if (this->cond_ != NULL)
5101 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
5102 return TRAVERSE_EXIT;
5104 if (this->post_ != NULL)
5106 if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
5107 return TRAVERSE_EXIT;
5109 return this->statements_->traverse(traverse);
5112 // Lower a For_statement into if statements and gotos. Getting rid of
5113 // complex statements make it easier to handle garbage collection.
5115 Statement*
5116 For_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
5117 Statement_inserter*)
5119 Statement* s;
5120 Location loc = this->location();
5122 Block* b = new Block(enclosing, this->location());
5123 if (this->init_ != NULL)
5125 s = Statement::make_block_statement(this->init_,
5126 this->init_->start_location());
5127 b->add_statement(s);
5130 Unnamed_label* entry = NULL;
5131 if (this->cond_ != NULL)
5133 entry = new Unnamed_label(this->location());
5134 b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
5137 Unnamed_label* top = new Unnamed_label(this->location());
5138 b->add_statement(Statement::make_unnamed_label_statement(top));
5140 s = Statement::make_block_statement(this->statements_,
5141 this->statements_->start_location());
5142 b->add_statement(s);
5144 Location end_loc = this->statements_->end_location();
5146 Unnamed_label* cont = this->continue_label_;
5147 if (cont != NULL)
5148 b->add_statement(Statement::make_unnamed_label_statement(cont));
5150 if (this->post_ != NULL)
5152 s = Statement::make_block_statement(this->post_,
5153 this->post_->start_location());
5154 b->add_statement(s);
5155 end_loc = this->post_->end_location();
5158 if (this->cond_ == NULL)
5159 b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
5160 else
5162 b->add_statement(Statement::make_unnamed_label_statement(entry));
5164 Location cond_loc = this->cond_->location();
5165 Block* then_block = new Block(b, cond_loc);
5166 s = Statement::make_goto_unnamed_statement(top, cond_loc);
5167 then_block->add_statement(s);
5169 s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
5170 b->add_statement(s);
5173 Unnamed_label* brk = this->break_label_;
5174 if (brk != NULL)
5175 b->add_statement(Statement::make_unnamed_label_statement(brk));
5177 b->set_end_location(end_loc);
5179 return Statement::make_block_statement(b, loc);
5182 // Return the break label, creating it if necessary.
5184 Unnamed_label*
5185 For_statement::break_label()
5187 if (this->break_label_ == NULL)
5188 this->break_label_ = new Unnamed_label(this->location());
5189 return this->break_label_;
5192 // Return the continue LABEL_EXPR.
5194 Unnamed_label*
5195 For_statement::continue_label()
5197 if (this->continue_label_ == NULL)
5198 this->continue_label_ = new Unnamed_label(this->location());
5199 return this->continue_label_;
5202 // Set the break and continue labels a for statement. This is used
5203 // when lowering a for range statement.
5205 void
5206 For_statement::set_break_continue_labels(Unnamed_label* break_label,
5207 Unnamed_label* continue_label)
5209 go_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
5210 this->break_label_ = break_label;
5211 this->continue_label_ = continue_label;
5214 // Whether the overall statement may fall through.
5216 bool
5217 For_statement::do_may_fall_through() const
5219 // A for loop is terminating if it has no condition and
5220 // no break statement.
5221 if(this->cond_ != NULL)
5222 return true;
5223 if(this->break_label_ != NULL)
5224 return true;
5225 return false;
5228 // Dump the AST representation for a for statement.
5230 void
5231 For_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
5233 if (this->init_ != NULL && ast_dump_context->dump_subblocks())
5235 ast_dump_context->print_indent();
5236 ast_dump_context->indent();
5237 ast_dump_context->ostream() << "// INIT " << std::endl;
5238 ast_dump_context->dump_block(this->init_);
5239 ast_dump_context->unindent();
5241 ast_dump_context->print_indent();
5242 ast_dump_context->ostream() << "for ";
5243 if (this->cond_ != NULL)
5244 ast_dump_context->dump_expression(this->cond_);
5246 if (ast_dump_context->dump_subblocks())
5248 ast_dump_context->ostream() << " {" << std::endl;
5249 ast_dump_context->dump_block(this->statements_);
5250 if (this->init_ != NULL)
5252 ast_dump_context->print_indent();
5253 ast_dump_context->ostream() << "// POST " << std::endl;
5254 ast_dump_context->dump_block(this->post_);
5256 ast_dump_context->unindent();
5258 ast_dump_context->print_indent();
5259 ast_dump_context->ostream() << "}";
5262 ast_dump_context->ostream() << std::endl;
5265 // Make a for statement.
5267 For_statement*
5268 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
5269 Location location)
5271 return new For_statement(init, cond, post, location);
5274 // Class For_range_statement.
5276 // Traversal.
5279 For_range_statement::do_traverse(Traverse* traverse)
5281 if (this->traverse_expression(traverse, &this->index_var_) == TRAVERSE_EXIT)
5282 return TRAVERSE_EXIT;
5283 if (this->value_var_ != NULL)
5285 if (this->traverse_expression(traverse, &this->value_var_)
5286 == TRAVERSE_EXIT)
5287 return TRAVERSE_EXIT;
5289 if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
5290 return TRAVERSE_EXIT;
5291 return this->statements_->traverse(traverse);
5294 // Lower a for range statement. For simplicity we lower this into a
5295 // for statement, which will then be lowered in turn to goto
5296 // statements.
5298 Statement*
5299 For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing,
5300 Statement_inserter*)
5302 Type* range_type = this->range_->type();
5303 if (range_type->points_to() != NULL
5304 && range_type->points_to()->array_type() != NULL
5305 && !range_type->points_to()->is_slice_type())
5306 range_type = range_type->points_to();
5308 Type* index_type;
5309 Type* value_type = NULL;
5310 if (range_type->array_type() != NULL)
5312 index_type = Type::lookup_integer_type("int");
5313 value_type = range_type->array_type()->element_type();
5315 else if (range_type->is_string_type())
5317 index_type = Type::lookup_integer_type("int");
5318 value_type = Type::lookup_integer_type("int32");
5320 else if (range_type->map_type() != NULL)
5322 index_type = range_type->map_type()->key_type();
5323 value_type = range_type->map_type()->val_type();
5325 else if (range_type->channel_type() != NULL)
5327 index_type = range_type->channel_type()->element_type();
5328 if (this->value_var_ != NULL)
5330 if (!this->value_var_->type()->is_error())
5331 this->report_error(_("too many variables for range clause "
5332 "with channel"));
5333 return Statement::make_error_statement(this->location());
5336 else
5338 this->report_error(_("range clause must have "
5339 "array, slice, string, map, or channel type"));
5340 return Statement::make_error_statement(this->location());
5343 Location loc = this->location();
5344 Block* temp_block = new Block(enclosing, loc);
5346 Named_object* range_object = NULL;
5347 Temporary_statement* range_temp = NULL;
5348 Var_expression* ve = this->range_->var_expression();
5349 if (ve != NULL)
5350 range_object = ve->named_object();
5351 else
5353 range_temp = Statement::make_temporary(NULL, this->range_, loc);
5354 temp_block->add_statement(range_temp);
5355 this->range_ = NULL;
5358 Temporary_statement* index_temp = Statement::make_temporary(index_type,
5359 NULL, loc);
5360 temp_block->add_statement(index_temp);
5362 Temporary_statement* value_temp = NULL;
5363 if (this->value_var_ != NULL)
5365 value_temp = Statement::make_temporary(value_type, NULL, loc);
5366 temp_block->add_statement(value_temp);
5369 Block* body = new Block(temp_block, loc);
5371 Block* init;
5372 Expression* cond;
5373 Block* iter_init;
5374 Block* post;
5376 // Arrange to do a loop appropriate for the type. We will produce
5377 // for INIT ; COND ; POST {
5378 // ITER_INIT
5379 // INDEX = INDEX_TEMP
5380 // VALUE = VALUE_TEMP // If there is a value
5381 // original statements
5382 // }
5384 if (range_type->is_slice_type())
5385 this->lower_range_slice(gogo, temp_block, body, range_object, range_temp,
5386 index_temp, value_temp, &init, &cond, &iter_init,
5387 &post);
5388 else if (range_type->array_type() != NULL)
5389 this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
5390 index_temp, value_temp, &init, &cond, &iter_init,
5391 &post);
5392 else if (range_type->is_string_type())
5393 this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
5394 index_temp, value_temp, &init, &cond, &iter_init,
5395 &post);
5396 else if (range_type->map_type() != NULL)
5397 this->lower_range_map(gogo, temp_block, body, range_object, range_temp,
5398 index_temp, value_temp, &init, &cond, &iter_init,
5399 &post);
5400 else if (range_type->channel_type() != NULL)
5401 this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
5402 index_temp, value_temp, &init, &cond, &iter_init,
5403 &post);
5404 else
5405 go_unreachable();
5407 if (iter_init != NULL)
5408 body->add_statement(Statement::make_block_statement(iter_init, loc));
5410 Statement* assign;
5411 Expression* index_ref = Expression::make_temporary_reference(index_temp, loc);
5412 if (this->value_var_ == NULL)
5414 assign = Statement::make_assignment(this->index_var_, index_ref, loc);
5416 else
5418 Expression_list* lhs = new Expression_list();
5419 lhs->push_back(this->index_var_);
5420 lhs->push_back(this->value_var_);
5422 Expression_list* rhs = new Expression_list();
5423 rhs->push_back(index_ref);
5424 rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
5426 assign = Statement::make_tuple_assignment(lhs, rhs, loc);
5428 body->add_statement(assign);
5430 body->add_statement(Statement::make_block_statement(this->statements_, loc));
5432 body->set_end_location(this->statements_->end_location());
5434 For_statement* loop = Statement::make_for_statement(init, cond, post,
5435 this->location());
5436 loop->add_statements(body);
5437 loop->set_break_continue_labels(this->break_label_, this->continue_label_);
5439 temp_block->add_statement(loop);
5441 return Statement::make_block_statement(temp_block, loc);
5444 // Return a reference to the range, which may be in RANGE_OBJECT or in
5445 // RANGE_TEMP.
5447 Expression*
5448 For_range_statement::make_range_ref(Named_object* range_object,
5449 Temporary_statement* range_temp,
5450 Location loc)
5452 if (range_object != NULL)
5453 return Expression::make_var_reference(range_object, loc);
5454 else
5455 return Expression::make_temporary_reference(range_temp, loc);
5458 // Return a call to the predeclared function FUNCNAME passing a
5459 // reference to the temporary variable ARG.
5461 Expression*
5462 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
5463 Expression* arg,
5464 Location loc)
5466 Named_object* no = gogo->lookup_global(funcname);
5467 go_assert(no != NULL && no->is_function_declaration());
5468 Expression* func = Expression::make_func_reference(no, NULL, loc);
5469 Expression_list* params = new Expression_list();
5470 params->push_back(arg);
5471 return Expression::make_call(func, params, false, loc);
5474 // Lower a for range over an array.
5476 void
5477 For_range_statement::lower_range_array(Gogo* gogo,
5478 Block* enclosing,
5479 Block* body_block,
5480 Named_object* range_object,
5481 Temporary_statement* range_temp,
5482 Temporary_statement* index_temp,
5483 Temporary_statement* value_temp,
5484 Block** pinit,
5485 Expression** pcond,
5486 Block** piter_init,
5487 Block** ppost)
5489 Location loc = this->location();
5491 // The loop we generate:
5492 // len_temp := len(range)
5493 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5494 // value_temp = range[index_temp]
5495 // index = index_temp
5496 // value = value_temp
5497 // original body
5498 // }
5500 // Set *PINIT to
5501 // var len_temp int
5502 // len_temp = len(range)
5503 // index_temp = 0
5505 Block* init = new Block(enclosing, loc);
5507 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5508 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
5509 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
5510 len_call, loc);
5511 init->add_statement(len_temp);
5513 mpz_t zval;
5514 mpz_init_set_ui(zval, 0UL);
5515 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
5516 mpz_clear(zval);
5518 Temporary_reference_expression* tref =
5519 Expression::make_temporary_reference(index_temp, loc);
5520 tref->set_is_lvalue();
5521 Statement* s = Statement::make_assignment(tref, zexpr, loc);
5522 init->add_statement(s);
5524 *pinit = init;
5526 // Set *PCOND to
5527 // index_temp < len_temp
5529 ref = Expression::make_temporary_reference(index_temp, loc);
5530 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
5531 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
5533 *pcond = lt;
5535 // Set *PITER_INIT to
5536 // value_temp = range[index_temp]
5538 Block* iter_init = NULL;
5539 if (value_temp != NULL)
5541 iter_init = new Block(body_block, loc);
5543 ref = this->make_range_ref(range_object, range_temp, loc);
5544 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
5545 Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
5547 tref = Expression::make_temporary_reference(value_temp, loc);
5548 tref->set_is_lvalue();
5549 s = Statement::make_assignment(tref, index, loc);
5551 iter_init->add_statement(s);
5553 *piter_init = iter_init;
5555 // Set *PPOST to
5556 // index_temp++
5558 Block* post = new Block(enclosing, loc);
5559 tref = Expression::make_temporary_reference(index_temp, loc);
5560 tref->set_is_lvalue();
5561 s = Statement::make_inc_statement(tref);
5562 post->add_statement(s);
5563 *ppost = post;
5566 // Lower a for range over a slice.
5568 void
5569 For_range_statement::lower_range_slice(Gogo* gogo,
5570 Block* enclosing,
5571 Block* body_block,
5572 Named_object* range_object,
5573 Temporary_statement* range_temp,
5574 Temporary_statement* index_temp,
5575 Temporary_statement* value_temp,
5576 Block** pinit,
5577 Expression** pcond,
5578 Block** piter_init,
5579 Block** ppost)
5581 Location loc = this->location();
5583 // The loop we generate:
5584 // for_temp := range
5585 // len_temp := len(for_temp)
5586 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5587 // value_temp = for_temp[index_temp]
5588 // index = index_temp
5589 // value = value_temp
5590 // original body
5591 // }
5593 // Using for_temp means that we don't need to check bounds when
5594 // fetching range_temp[index_temp].
5596 // Set *PINIT to
5597 // range_temp := range
5598 // var len_temp int
5599 // len_temp = len(range_temp)
5600 // index_temp = 0
5602 Block* init = new Block(enclosing, loc);
5604 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5605 Temporary_statement* for_temp = Statement::make_temporary(NULL, ref, loc);
5606 init->add_statement(for_temp);
5608 ref = Expression::make_temporary_reference(for_temp, loc);
5609 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
5610 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
5611 len_call, loc);
5612 init->add_statement(len_temp);
5614 mpz_t zval;
5615 mpz_init_set_ui(zval, 0UL);
5616 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
5617 mpz_clear(zval);
5619 Temporary_reference_expression* tref =
5620 Expression::make_temporary_reference(index_temp, loc);
5621 tref->set_is_lvalue();
5622 Statement* s = Statement::make_assignment(tref, zexpr, loc);
5623 init->add_statement(s);
5625 *pinit = init;
5627 // Set *PCOND to
5628 // index_temp < len_temp
5630 ref = Expression::make_temporary_reference(index_temp, loc);
5631 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
5632 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
5634 *pcond = lt;
5636 // Set *PITER_INIT to
5637 // value_temp = range[index_temp]
5639 Block* iter_init = NULL;
5640 if (value_temp != NULL)
5642 iter_init = new Block(body_block, loc);
5644 ref = Expression::make_temporary_reference(for_temp, loc);
5645 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
5646 Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
5648 tref = Expression::make_temporary_reference(value_temp, loc);
5649 tref->set_is_lvalue();
5650 s = Statement::make_assignment(tref, index, loc);
5652 iter_init->add_statement(s);
5654 *piter_init = iter_init;
5656 // Set *PPOST to
5657 // index_temp++
5659 Block* post = new Block(enclosing, loc);
5660 tref = Expression::make_temporary_reference(index_temp, loc);
5661 tref->set_is_lvalue();
5662 s = Statement::make_inc_statement(tref);
5663 post->add_statement(s);
5664 *ppost = post;
5667 // Lower a for range over a string.
5669 void
5670 For_range_statement::lower_range_string(Gogo*,
5671 Block* enclosing,
5672 Block* body_block,
5673 Named_object* range_object,
5674 Temporary_statement* range_temp,
5675 Temporary_statement* index_temp,
5676 Temporary_statement* value_temp,
5677 Block** pinit,
5678 Expression** pcond,
5679 Block** piter_init,
5680 Block** ppost)
5682 Location loc = this->location();
5684 // The loop we generate:
5685 // var next_index_temp int
5686 // for index_temp = 0; ; index_temp = next_index_temp {
5687 // next_index_temp, value_temp = stringiter2(range, index_temp)
5688 // if next_index_temp == 0 {
5689 // break
5690 // }
5691 // index = index_temp
5692 // value = value_temp
5693 // original body
5694 // }
5696 // Set *PINIT to
5697 // var next_index_temp int
5698 // index_temp = 0
5700 Block* init = new Block(enclosing, loc);
5702 Temporary_statement* next_index_temp =
5703 Statement::make_temporary(index_temp->type(), NULL, loc);
5704 init->add_statement(next_index_temp);
5706 mpz_t zval;
5707 mpz_init_set_ui(zval, 0UL);
5708 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
5710 Temporary_reference_expression* ref =
5711 Expression::make_temporary_reference(index_temp, loc);
5712 ref->set_is_lvalue();
5713 Statement* s = Statement::make_assignment(ref, zexpr, loc);
5715 init->add_statement(s);
5716 *pinit = init;
5718 // The loop has no condition.
5720 *pcond = NULL;
5722 // Set *PITER_INIT to
5723 // next_index_temp = runtime.stringiter(range, index_temp)
5724 // or
5725 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
5726 // followed by
5727 // if next_index_temp == 0 {
5728 // break
5729 // }
5731 Block* iter_init = new Block(body_block, loc);
5733 Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
5734 Expression* p2 = Expression::make_temporary_reference(index_temp, loc);
5735 Call_expression* call = Runtime::make_call((value_temp == NULL
5736 ? Runtime::STRINGITER
5737 : Runtime::STRINGITER2),
5738 loc, 2, p1, p2);
5740 if (value_temp == NULL)
5742 ref = Expression::make_temporary_reference(next_index_temp, loc);
5743 ref->set_is_lvalue();
5744 s = Statement::make_assignment(ref, call, loc);
5746 else
5748 Expression_list* lhs = new Expression_list();
5750 ref = Expression::make_temporary_reference(next_index_temp, loc);
5751 ref->set_is_lvalue();
5752 lhs->push_back(ref);
5754 ref = Expression::make_temporary_reference(value_temp, loc);
5755 ref->set_is_lvalue();
5756 lhs->push_back(ref);
5758 Expression_list* rhs = new Expression_list();
5759 rhs->push_back(Expression::make_call_result(call, 0));
5760 rhs->push_back(Expression::make_call_result(call, 1));
5762 s = Statement::make_tuple_assignment(lhs, rhs, loc);
5764 iter_init->add_statement(s);
5766 ref = Expression::make_temporary_reference(next_index_temp, loc);
5767 zexpr = Expression::make_integer(&zval, NULL, loc);
5768 mpz_clear(zval);
5769 Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
5771 Block* then_block = new Block(iter_init, loc);
5772 s = Statement::make_break_statement(this->break_label(), loc);
5773 then_block->add_statement(s);
5775 s = Statement::make_if_statement(equals, then_block, NULL, loc);
5776 iter_init->add_statement(s);
5778 *piter_init = iter_init;
5780 // Set *PPOST to
5781 // index_temp = next_index_temp
5783 Block* post = new Block(enclosing, loc);
5785 Temporary_reference_expression* lhs =
5786 Expression::make_temporary_reference(index_temp, loc);
5787 lhs->set_is_lvalue();
5788 Expression* rhs = Expression::make_temporary_reference(next_index_temp, loc);
5789 s = Statement::make_assignment(lhs, rhs, loc);
5791 post->add_statement(s);
5792 *ppost = post;
5795 // Lower a for range over a map.
5797 void
5798 For_range_statement::lower_range_map(Gogo*,
5799 Block* enclosing,
5800 Block* body_block,
5801 Named_object* range_object,
5802 Temporary_statement* range_temp,
5803 Temporary_statement* index_temp,
5804 Temporary_statement* value_temp,
5805 Block** pinit,
5806 Expression** pcond,
5807 Block** piter_init,
5808 Block** ppost)
5810 Location loc = this->location();
5812 // The runtime uses a struct to handle ranges over a map. The
5813 // struct is four pointers long. The first pointer is NULL when we
5814 // have completed the iteration.
5816 // The loop we generate:
5817 // var hiter map_iteration_struct
5818 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
5819 // mapiter2(hiter, &index_temp, &value_temp)
5820 // index = index_temp
5821 // value = value_temp
5822 // original body
5823 // }
5825 // Set *PINIT to
5826 // var hiter map_iteration_struct
5827 // runtime.mapiterinit(range, &hiter)
5829 Block* init = new Block(enclosing, loc);
5831 Type* map_iteration_type = Runtime::map_iteration_type();
5832 Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
5833 NULL, loc);
5834 init->add_statement(hiter);
5836 Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
5837 Expression* ref = Expression::make_temporary_reference(hiter, loc);
5838 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
5839 Expression* call = Runtime::make_call(Runtime::MAPITERINIT, loc, 2, p1, p2);
5840 init->add_statement(Statement::make_statement(call, true));
5842 *pinit = init;
5844 // Set *PCOND to
5845 // hiter[0] != nil
5847 ref = Expression::make_temporary_reference(hiter, loc);
5849 mpz_t zval;
5850 mpz_init_set_ui(zval, 0UL);
5851 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
5852 mpz_clear(zval);
5854 Expression* index = Expression::make_index(ref, zexpr, NULL, NULL, loc);
5856 Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
5857 Expression::make_nil(loc),
5858 loc);
5860 *pcond = ne;
5862 // Set *PITER_INIT to
5863 // mapiter1(hiter, &index_temp)
5864 // or
5865 // mapiter2(hiter, &index_temp, &value_temp)
5867 Block* iter_init = new Block(body_block, loc);
5869 ref = Expression::make_temporary_reference(hiter, loc);
5870 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
5871 ref = Expression::make_temporary_reference(index_temp, loc);
5872 p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
5873 if (value_temp == NULL)
5874 call = Runtime::make_call(Runtime::MAPITER1, loc, 2, p1, p2);
5875 else
5877 ref = Expression::make_temporary_reference(value_temp, loc);
5878 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
5879 call = Runtime::make_call(Runtime::MAPITER2, loc, 3, p1, p2, p3);
5881 iter_init->add_statement(Statement::make_statement(call, true));
5883 *piter_init = iter_init;
5885 // Set *PPOST to
5886 // mapiternext(&hiter)
5888 Block* post = new Block(enclosing, loc);
5890 ref = Expression::make_temporary_reference(hiter, loc);
5891 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
5892 call = Runtime::make_call(Runtime::MAPITERNEXT, loc, 1, p1);
5893 post->add_statement(Statement::make_statement(call, true));
5895 *ppost = post;
5898 // Lower a for range over a channel.
5900 void
5901 For_range_statement::lower_range_channel(Gogo*,
5902 Block*,
5903 Block* body_block,
5904 Named_object* range_object,
5905 Temporary_statement* range_temp,
5906 Temporary_statement* index_temp,
5907 Temporary_statement* value_temp,
5908 Block** pinit,
5909 Expression** pcond,
5910 Block** piter_init,
5911 Block** ppost)
5913 go_assert(value_temp == NULL);
5915 Location loc = this->location();
5917 // The loop we generate:
5918 // for {
5919 // index_temp, ok_temp = <-range
5920 // if !ok_temp {
5921 // break
5922 // }
5923 // index = index_temp
5924 // original body
5925 // }
5927 // We have no initialization code, no condition, and no post code.
5929 *pinit = NULL;
5930 *pcond = NULL;
5931 *ppost = NULL;
5933 // Set *PITER_INIT to
5934 // index_temp, ok_temp = <-range
5935 // if !ok_temp {
5936 // break
5937 // }
5939 Block* iter_init = new Block(body_block, loc);
5941 Temporary_statement* ok_temp =
5942 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
5943 iter_init->add_statement(ok_temp);
5945 Expression* cref = this->make_range_ref(range_object, range_temp, loc);
5946 Temporary_reference_expression* iref =
5947 Expression::make_temporary_reference(index_temp, loc);
5948 iref->set_is_lvalue();
5949 Temporary_reference_expression* oref =
5950 Expression::make_temporary_reference(ok_temp, loc);
5951 oref->set_is_lvalue();
5952 Statement* s = Statement::make_tuple_receive_assignment(iref, oref, cref,
5953 loc);
5954 iter_init->add_statement(s);
5956 Block* then_block = new Block(iter_init, loc);
5957 s = Statement::make_break_statement(this->break_label(), loc);
5958 then_block->add_statement(s);
5960 oref = Expression::make_temporary_reference(ok_temp, loc);
5961 Expression* cond = Expression::make_unary(OPERATOR_NOT, oref, loc);
5962 s = Statement::make_if_statement(cond, then_block, NULL, loc);
5963 iter_init->add_statement(s);
5965 *piter_init = iter_init;
5968 // Return the break LABEL_EXPR.
5970 Unnamed_label*
5971 For_range_statement::break_label()
5973 if (this->break_label_ == NULL)
5974 this->break_label_ = new Unnamed_label(this->location());
5975 return this->break_label_;
5978 // Return the continue LABEL_EXPR.
5980 Unnamed_label*
5981 For_range_statement::continue_label()
5983 if (this->continue_label_ == NULL)
5984 this->continue_label_ = new Unnamed_label(this->location());
5985 return this->continue_label_;
5988 // Dump the AST representation for a for range statement.
5990 void
5991 For_range_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
5994 ast_dump_context->print_indent();
5995 ast_dump_context->ostream() << "for ";
5996 ast_dump_context->dump_expression(this->index_var_);
5997 if (this->value_var_ != NULL)
5999 ast_dump_context->ostream() << ", ";
6000 ast_dump_context->dump_expression(this->value_var_);
6003 ast_dump_context->ostream() << " = range ";
6004 ast_dump_context->dump_expression(this->range_);
6005 if (ast_dump_context->dump_subblocks())
6007 ast_dump_context->ostream() << " {" << std::endl;
6009 ast_dump_context->indent();
6011 ast_dump_context->dump_block(this->statements_);
6013 ast_dump_context->unindent();
6014 ast_dump_context->print_indent();
6015 ast_dump_context->ostream() << "}";
6017 ast_dump_context->ostream() << std::endl;
6020 // Make a for statement with a range clause.
6022 For_range_statement*
6023 Statement::make_for_range_statement(Expression* index_var,
6024 Expression* value_var,
6025 Expression* range,
6026 Location location)
6028 return new For_range_statement(index_var, value_var, range, location);