compiler: introduce size threshold for nil checks
[official-gcc.git] / gcc / go / gofrontend / statements.cc
blobd3878a6ba0561c4e41ae7bdcb85b5786b5e65d5e
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 "go-diagnostics.h"
11 #include "types.h"
12 #include "expressions.h"
13 #include "gogo.h"
14 #include "runtime.h"
15 #include "backend.h"
16 #include "statements.h"
17 #include "ast-dump.h"
19 // Class Statement.
21 Statement::Statement(Statement_classification classification,
22 Location location)
23 : classification_(classification), location_(location)
27 Statement::~Statement()
31 // Traverse the tree. The work of walking the components is handled
32 // by the subclasses.
34 int
35 Statement::traverse(Block* block, size_t* pindex, Traverse* traverse)
37 if (this->classification_ == STATEMENT_ERROR)
38 return TRAVERSE_CONTINUE;
40 unsigned int traverse_mask = traverse->traverse_mask();
42 if ((traverse_mask & Traverse::traverse_statements) != 0)
44 int t = traverse->statement(block, pindex, this);
45 if (t == TRAVERSE_EXIT)
46 return TRAVERSE_EXIT;
47 else if (t == TRAVERSE_SKIP_COMPONENTS)
48 return TRAVERSE_CONTINUE;
51 // No point in checking traverse_mask here--a statement may contain
52 // other blocks or statements, and if we got here we always want to
53 // walk them.
54 return this->do_traverse(traverse);
57 // Traverse the contents of a statement.
59 int
60 Statement::traverse_contents(Traverse* traverse)
62 return this->do_traverse(traverse);
65 // Traverse assignments.
67 bool
68 Statement::traverse_assignments(Traverse_assignments* tassign)
70 if (this->classification_ == STATEMENT_ERROR)
71 return false;
72 return this->do_traverse_assignments(tassign);
75 // Traverse an expression in a statement. This is a helper function
76 // for child classes.
78 int
79 Statement::traverse_expression(Traverse* traverse, Expression** expr)
81 if ((traverse->traverse_mask()
82 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
83 return TRAVERSE_CONTINUE;
84 return Expression::traverse(expr, traverse);
87 // Traverse an expression list in a statement. This is a helper
88 // function for child classes.
90 int
91 Statement::traverse_expression_list(Traverse* traverse,
92 Expression_list* expr_list)
94 if (expr_list == NULL)
95 return TRAVERSE_CONTINUE;
96 if ((traverse->traverse_mask()
97 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
98 return TRAVERSE_CONTINUE;
99 return expr_list->traverse(traverse);
102 // Traverse a type in a statement. This is a helper function for
103 // child classes.
106 Statement::traverse_type(Traverse* traverse, Type* type)
108 if ((traverse->traverse_mask()
109 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
110 return TRAVERSE_CONTINUE;
111 return Type::traverse(type, traverse);
114 // Set type information for unnamed constants. This is really done by
115 // the child class.
117 void
118 Statement::determine_types()
120 this->do_determine_types();
123 // If this is a thunk statement, return it.
125 Thunk_statement*
126 Statement::thunk_statement()
128 Thunk_statement* ret = this->convert<Thunk_statement, STATEMENT_GO>();
129 if (ret == NULL)
130 ret = this->convert<Thunk_statement, STATEMENT_DEFER>();
131 return ret;
134 // Convert a Statement to the backend representation. This is really
135 // done by the child class.
137 Bstatement*
138 Statement::get_backend(Translate_context* context)
140 if (this->classification_ == STATEMENT_ERROR)
141 return context->backend()->error_statement();
142 return this->do_get_backend(context);
145 // Dump AST representation for a statement to a dump context.
147 void
148 Statement::dump_statement(Ast_dump_context* ast_dump_context) const
150 this->do_dump_statement(ast_dump_context);
153 // Note that this statement is erroneous. This is called by children
154 // when they discover an error.
156 void
157 Statement::set_is_error()
159 this->classification_ = STATEMENT_ERROR;
162 // For children to call to report an error conveniently.
164 void
165 Statement::report_error(const char* msg)
167 go_error_at(this->location_, "%s", msg);
168 this->set_is_error();
171 // An error statement, used to avoid crashing after we report an
172 // error.
174 class Error_statement : public Statement
176 public:
177 Error_statement(Location location)
178 : Statement(STATEMENT_ERROR, location)
181 protected:
183 do_traverse(Traverse*)
184 { return TRAVERSE_CONTINUE; }
186 Bstatement*
187 do_get_backend(Translate_context*)
188 { go_unreachable(); }
190 void
191 do_dump_statement(Ast_dump_context*) const;
195 // Helper to tack on available source position information
196 // at the end of a statement.
198 static std::string
199 dsuffix(Location location)
201 std::string lstr = Linemap::location_to_string(location);
202 if (lstr == "")
203 return lstr;
204 std::string rval(" // ");
205 rval += lstr;
206 return rval;
209 // Dump the AST representation for an error statement.
211 void
212 Error_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
214 ast_dump_context->print_indent();
215 ast_dump_context->ostream() << "Error statement" << std::endl;
218 // Make an error statement.
220 Statement*
221 Statement::make_error_statement(Location location)
223 return new Error_statement(location);
226 // Class Variable_declaration_statement.
228 Variable_declaration_statement::Variable_declaration_statement(
229 Named_object* var)
230 : Statement(STATEMENT_VARIABLE_DECLARATION, var->var_value()->location()),
231 var_(var)
235 // We don't actually traverse the variable here; it was traversed
236 // while traversing the Block.
239 Variable_declaration_statement::do_traverse(Traverse*)
241 return TRAVERSE_CONTINUE;
244 // Traverse the assignments in a variable declaration. Note that this
245 // traversal is different from the usual traversal.
247 bool
248 Variable_declaration_statement::do_traverse_assignments(
249 Traverse_assignments* tassign)
251 tassign->initialize_variable(this->var_);
252 return true;
255 // Lower the variable's initialization expression.
257 Statement*
258 Variable_declaration_statement::do_lower(Gogo* gogo, Named_object* function,
259 Block*, Statement_inserter* inserter)
261 this->var_->var_value()->lower_init_expression(gogo, function, inserter);
262 return this;
265 // Flatten the variable's initialization expression.
267 Statement*
268 Variable_declaration_statement::do_flatten(Gogo* gogo, Named_object* function,
269 Block*, Statement_inserter* inserter)
271 Variable* var = this->var_->var_value();
272 if (var->type()->is_error_type()
273 || (var->init() != NULL
274 && var->init()->is_error_expression()))
276 go_assert(saw_errors());
277 return Statement::make_error_statement(this->location());
279 this->var_->var_value()->flatten_init_expression(gogo, function, inserter);
280 return this;
283 // Convert a variable declaration to the backend representation.
285 Bstatement*
286 Variable_declaration_statement::do_get_backend(Translate_context* context)
288 Bfunction* bfunction = context->function()->func_value()->get_decl();
289 Variable* var = this->var_->var_value();
290 Bvariable* bvar = this->var_->get_backend_variable(context->gogo(),
291 context->function());
292 Bexpression* binit = var->get_init(context->gogo(), context->function());
294 if (!var->is_in_heap())
296 go_assert(binit != NULL);
297 return context->backend()->init_statement(bfunction, bvar, binit);
300 // Something takes the address of this variable, so the value is
301 // stored in the heap. Initialize it to newly allocated memory
302 // space, and assign the initial value to the new space.
303 Location loc = this->location();
304 Named_object* newfn = context->gogo()->lookup_global("new");
305 go_assert(newfn != NULL && newfn->is_function_declaration());
306 Expression* func = Expression::make_func_reference(newfn, NULL, loc);
307 Expression_list* params = new Expression_list();
308 params->push_back(Expression::make_type(var->type(), loc));
309 Expression* call = Expression::make_call(func, params, false, loc);
310 context->gogo()->lower_expression(context->function(), NULL, &call);
311 Temporary_statement* temp = Statement::make_temporary(NULL, call, loc);
312 Bstatement* btemp = temp->get_backend(context);
314 Bstatement* set = NULL;
315 if (binit != NULL)
317 Expression* e = Expression::make_temporary_reference(temp, loc);
318 e = Expression::make_dereference(e, Expression::NIL_CHECK_NOT_NEEDED,
319 loc);
320 Bexpression* be = e->get_backend(context);
321 set = context->backend()->assignment_statement(bfunction, be, binit, loc);
324 Expression* ref = Expression::make_temporary_reference(temp, loc);
325 Bexpression* bref = ref->get_backend(context);
326 Bstatement* sinit = context->backend()->init_statement(bfunction, bvar, bref);
328 std::vector<Bstatement*> stats;
329 stats.reserve(3);
330 stats.push_back(btemp);
331 if (set != NULL)
332 stats.push_back(set);
333 stats.push_back(sinit);
334 return context->backend()->statement_list(stats);
337 // Dump the AST representation for a variable declaration.
339 void
340 Variable_declaration_statement::do_dump_statement(
341 Ast_dump_context* ast_dump_context) const
343 ast_dump_context->print_indent();
345 go_assert(var_->is_variable());
346 ast_dump_context->ostream() << "var " << this->var_->name() << " ";
347 Variable* var = this->var_->var_value();
348 if (var->has_type())
350 ast_dump_context->dump_type(var->type());
351 ast_dump_context->ostream() << " ";
353 if (var->init() != NULL)
355 ast_dump_context->ostream() << "= ";
356 ast_dump_context->dump_expression(var->init());
358 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
361 // Make a variable declaration.
363 Statement*
364 Statement::make_variable_declaration(Named_object* var)
366 return new Variable_declaration_statement(var);
369 // Class Temporary_statement.
371 // Return the type of the temporary variable.
373 Type*
374 Temporary_statement::type() const
376 Type* type = this->type_ != NULL ? this->type_ : this->init_->type();
378 // Temporary variables cannot have a void type.
379 if (type->is_void_type())
381 go_assert(saw_errors());
382 return Type::make_error_type();
384 return type;
387 // Traversal.
390 Temporary_statement::do_traverse(Traverse* traverse)
392 if (this->type_ != NULL
393 && this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
394 return TRAVERSE_EXIT;
395 if (this->init_ == NULL)
396 return TRAVERSE_CONTINUE;
397 else
398 return this->traverse_expression(traverse, &this->init_);
401 // Traverse assignments.
403 bool
404 Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
406 if (this->init_ == NULL)
407 return false;
408 tassign->value(&this->init_, true, true);
409 return true;
412 // Determine types.
414 void
415 Temporary_statement::do_determine_types()
417 if (this->type_ != NULL && this->type_->is_abstract())
418 this->type_ = this->type_->make_non_abstract_type();
420 if (this->init_ != NULL)
422 if (this->type_ == NULL)
423 this->init_->determine_type_no_context();
424 else
426 Type_context context(this->type_, false);
427 this->init_->determine_type(&context);
431 if (this->type_ == NULL)
433 this->type_ = this->init_->type();
434 go_assert(!this->type_->is_abstract());
438 // Check types.
440 void
441 Temporary_statement::do_check_types(Gogo*)
443 if (this->type_ != NULL && this->init_ != NULL)
445 std::string reason;
446 if (!Type::are_assignable(this->type_, this->init_->type(), &reason))
448 if (reason.empty())
449 go_error_at(this->location(), "incompatible types in assignment");
450 else
451 go_error_at(this->location(), "incompatible types in assignment (%s)",
452 reason.c_str());
453 this->set_is_error();
458 // Flatten a temporary statement: add another temporary when it might
459 // be needed for interface conversion.
461 Statement*
462 Temporary_statement::do_flatten(Gogo*, Named_object*, Block*,
463 Statement_inserter* inserter)
465 if (this->type()->is_error_type()
466 || (this->init_ != NULL
467 && this->init_->is_error_expression()))
469 go_assert(saw_errors());
470 return Statement::make_error_statement(this->location());
473 if (this->type_ != NULL
474 && this->init_ != NULL
475 && !Type::are_identical(this->type_, this->init_->type(), false, NULL)
476 && this->init_->type()->interface_type() != NULL
477 && !this->init_->is_variable())
479 Temporary_statement *temp =
480 Statement::make_temporary(NULL, this->init_, this->location());
481 inserter->insert(temp);
482 this->init_ = Expression::make_temporary_reference(temp,
483 this->location());
485 return this;
488 // Convert to backend representation.
490 Bstatement*
491 Temporary_statement::do_get_backend(Translate_context* context)
493 go_assert(this->bvariable_ == NULL);
495 Named_object* function = context->function();
496 go_assert(function != NULL);
497 Bfunction* bfunction = function->func_value()->get_decl();
498 Btype* btype = this->type()->get_backend(context->gogo());
500 Bexpression* binit;
501 if (this->init_ == NULL)
502 binit = NULL;
503 else if (this->type_ == NULL)
504 binit = this->init_->get_backend(context);
505 else
507 Expression* init = Expression::convert_for_assignment(context->gogo(),
508 this->type_,
509 this->init_,
510 this->location());
511 binit = init->get_backend(context);
514 if (binit != NULL)
515 binit = context->backend()->convert_expression(btype, binit,
516 this->location());
518 Bstatement* statement;
519 this->bvariable_ =
520 context->backend()->temporary_variable(bfunction, context->bblock(),
521 btype, binit,
522 this->is_address_taken_,
523 this->location(), &statement);
524 return statement;
527 // Return the backend variable.
529 Bvariable*
530 Temporary_statement::get_backend_variable(Translate_context* context) const
532 if (this->bvariable_ == NULL)
534 go_assert(saw_errors());
535 return context->backend()->error_variable();
537 return this->bvariable_;
540 // Dump the AST represemtation for a temporary statement
542 void
543 Temporary_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
545 ast_dump_context->print_indent();
546 ast_dump_context->dump_temp_variable_name(this);
547 if (this->type_ != NULL)
549 ast_dump_context->ostream() << " ";
550 ast_dump_context->dump_type(this->type_);
552 if (this->init_ != NULL)
554 ast_dump_context->ostream() << " = ";
555 ast_dump_context->dump_expression(this->init_);
557 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
560 // Make and initialize a temporary variable in BLOCK.
562 Temporary_statement*
563 Statement::make_temporary(Type* type, Expression* init,
564 Location location)
566 return new Temporary_statement(type, init, location);
569 // The Move_subexpressions class is used to move all top-level
570 // subexpressions of an expression. This is used for things like
571 // index expressions in which we must evaluate the index value before
572 // it can be changed by a multiple assignment.
574 class Move_subexpressions : public Traverse
576 public:
577 Move_subexpressions(int skip, Block* block)
578 : Traverse(traverse_expressions),
579 skip_(skip), block_(block)
582 protected:
584 expression(Expression**);
586 private:
587 // The number of subexpressions to skip moving. This is used to
588 // avoid moving the array itself, as we only need to move the index.
589 int skip_;
590 // The block where new temporary variables should be added.
591 Block* block_;
595 Move_subexpressions::expression(Expression** pexpr)
597 if (this->skip_ > 0)
598 --this->skip_;
599 else if ((*pexpr)->temporary_reference_expression() == NULL
600 && !(*pexpr)->is_nil_expression()
601 && !(*pexpr)->is_constant())
603 Location loc = (*pexpr)->location();
604 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
605 this->block_->add_statement(temp);
606 *pexpr = Expression::make_temporary_reference(temp, loc);
608 // We only need to move top-level subexpressions.
609 return TRAVERSE_SKIP_COMPONENTS;
612 // The Move_ordered_evals class is used to find any subexpressions of
613 // an expression that have an evaluation order dependency. It creates
614 // temporary variables to hold them.
616 class Move_ordered_evals : public Traverse
618 public:
619 Move_ordered_evals(Block* block)
620 : Traverse(traverse_expressions),
621 block_(block)
624 protected:
626 expression(Expression**);
628 private:
629 // The block where new temporary variables should be added.
630 Block* block_;
634 Move_ordered_evals::expression(Expression** pexpr)
636 // We have to look at subexpressions first.
637 if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
638 return TRAVERSE_EXIT;
640 int i;
641 if ((*pexpr)->must_eval_subexpressions_in_order(&i))
643 Move_subexpressions ms(i, this->block_);
644 if ((*pexpr)->traverse_subexpressions(&ms) == TRAVERSE_EXIT)
645 return TRAVERSE_EXIT;
648 if ((*pexpr)->must_eval_in_order())
650 Call_expression* call = (*pexpr)->call_expression();
651 if (call != NULL && call->is_multi_value_arg())
653 // A call expression which returns multiple results as an argument
654 // to another call must be handled specially. We can't create a
655 // temporary because there is no type to give it. Instead, group
656 // the caller and this multi-valued call argument and use a temporary
657 // variable to hold them.
658 return TRAVERSE_SKIP_COMPONENTS;
661 Location loc = (*pexpr)->location();
662 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
663 this->block_->add_statement(temp);
664 *pexpr = Expression::make_temporary_reference(temp, loc);
666 return TRAVERSE_SKIP_COMPONENTS;
669 // Class Assignment_statement.
671 // Traversal.
674 Assignment_statement::do_traverse(Traverse* traverse)
676 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
677 return TRAVERSE_EXIT;
678 return this->traverse_expression(traverse, &this->rhs_);
681 bool
682 Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
684 tassign->assignment(&this->lhs_, &this->rhs_);
685 return true;
688 // Lower an assignment to a map index expression to a runtime function
689 // call.
691 Statement*
692 Assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
693 Statement_inserter*)
695 Map_index_expression* mie = this->lhs_->map_index_expression();
696 if (mie != NULL)
698 Location loc = this->location();
700 Expression* map = mie->map();
701 Map_type* mt = map->type()->map_type();
702 if (mt == NULL)
704 go_assert(saw_errors());
705 return Statement::make_error_statement(loc);
708 Block* b = new Block(enclosing, loc);
710 // Move out any subexpressions on the left hand side to make
711 // sure that functions are called in the required order.
712 Move_ordered_evals moe(b);
713 mie->traverse_subexpressions(&moe);
715 // Copy the key into a temporary so that we can take its address
716 // without pushing the value onto the heap.
718 // var key_temp KEY_TYPE = MAP_INDEX
719 Temporary_statement* key_temp = Statement::make_temporary(mt->key_type(),
720 mie->index(),
721 loc);
722 b->add_statement(key_temp);
724 // Copy the value into a temporary to ensure that it is
725 // evaluated before we add the key to the map. This may matter
726 // if the value is itself a reference to the map.
728 // var val_temp VAL_TYPE = RHS
729 Temporary_statement* val_temp = Statement::make_temporary(mt->val_type(),
730 this->rhs_,
731 loc);
732 b->add_statement(val_temp);
734 // *mapassign(TYPE, MAP, &key_temp) = RHS
735 Expression* a1 = Expression::make_type_descriptor(mt, loc);
736 Expression* a2 = mie->map();
737 Temporary_reference_expression* ref =
738 Expression::make_temporary_reference(key_temp, loc);
739 Expression* a3 = Expression::make_unary(OPERATOR_AND, ref, loc);
740 Expression* call = Runtime::make_call(Runtime::MAPASSIGN, loc, 3,
741 a1, a2, a3);
742 Type* ptrval_type = Type::make_pointer_type(mt->val_type());
743 call = Expression::make_cast(ptrval_type, call, loc);
744 Expression* indir =
745 Expression::make_dereference(call, Expression::NIL_CHECK_NOT_NEEDED,
746 loc);
747 ref = Expression::make_temporary_reference(val_temp, loc);
748 b->add_statement(Statement::make_assignment(indir, ref, loc));
750 return Statement::make_block_statement(b, loc);
753 return this;
756 // Set types for the assignment.
758 void
759 Assignment_statement::do_determine_types()
761 this->lhs_->determine_type_no_context();
762 Type* rhs_context_type = this->lhs_->type();
763 if (rhs_context_type->is_sink_type())
764 rhs_context_type = NULL;
765 Type_context context(rhs_context_type, false);
766 this->rhs_->determine_type(&context);
769 // Check types for an assignment.
771 void
772 Assignment_statement::do_check_types(Gogo*)
774 // The left hand side must be either addressable, a map index
775 // expression, or the blank identifier.
776 if (!this->lhs_->is_addressable()
777 && this->lhs_->map_index_expression() == NULL
778 && !this->lhs_->is_sink_expression())
780 if (!this->lhs_->type()->is_error())
781 this->report_error(_("invalid left hand side of assignment"));
782 return;
785 Type* lhs_type = this->lhs_->type();
786 Type* rhs_type = this->rhs_->type();
788 // Invalid assignment of nil to the blank identifier.
789 if (lhs_type->is_sink_type()
790 && rhs_type->is_nil_type())
792 this->report_error(_("use of untyped nil"));
793 return;
796 std::string reason;
797 if (!Type::are_assignable(lhs_type, rhs_type, &reason))
799 if (reason.empty())
800 go_error_at(this->location(), "incompatible types in assignment");
801 else
802 go_error_at(this->location(), "incompatible types in assignment (%s)",
803 reason.c_str());
804 this->set_is_error();
807 if (lhs_type->is_error() || rhs_type->is_error())
808 this->set_is_error();
811 // Flatten an assignment statement. We may need a temporary for
812 // interface conversion.
814 Statement*
815 Assignment_statement::do_flatten(Gogo*, Named_object*, Block*,
816 Statement_inserter* inserter)
818 if (this->lhs_->is_error_expression()
819 || this->lhs_->type()->is_error_type()
820 || this->rhs_->is_error_expression()
821 || this->rhs_->type()->is_error_type())
823 go_assert(saw_errors());
824 return Statement::make_error_statement(this->location());
827 if (!this->lhs_->is_sink_expression()
828 && !Type::are_identical(this->lhs_->type(), this->rhs_->type(),
829 false, NULL)
830 && this->rhs_->type()->interface_type() != NULL
831 && !this->rhs_->is_variable())
833 Temporary_statement* temp =
834 Statement::make_temporary(NULL, this->rhs_, this->location());
835 inserter->insert(temp);
836 this->rhs_ = Expression::make_temporary_reference(temp,
837 this->location());
839 return this;
842 // Convert an assignment statement to the backend representation.
844 Bstatement*
845 Assignment_statement::do_get_backend(Translate_context* context)
847 if (this->lhs_->is_sink_expression())
849 Bexpression* rhs = this->rhs_->get_backend(context);
850 Bfunction* bfunction = context->function()->func_value()->get_decl();
851 return context->backend()->expression_statement(bfunction, rhs);
854 Bexpression* lhs = this->lhs_->get_backend(context);
855 Expression* conv =
856 Expression::convert_for_assignment(context->gogo(), this->lhs_->type(),
857 this->rhs_, this->location());
858 Bexpression* rhs = conv->get_backend(context);
859 Bfunction* bfunction = context->function()->func_value()->get_decl();
860 return context->backend()->assignment_statement(bfunction, lhs, rhs,
861 this->location());
864 // Dump the AST representation for an assignment statement.
866 void
867 Assignment_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
868 const
870 ast_dump_context->print_indent();
871 ast_dump_context->dump_expression(this->lhs_);
872 ast_dump_context->ostream() << " = " ;
873 ast_dump_context->dump_expression(this->rhs_);
874 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
877 // Make an assignment statement.
879 Statement*
880 Statement::make_assignment(Expression* lhs, Expression* rhs,
881 Location location)
883 return new Assignment_statement(lhs, rhs, location);
886 // An assignment operation statement.
888 class Assignment_operation_statement : public Statement
890 public:
891 Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
892 Location location)
893 : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
894 op_(op), lhs_(lhs), rhs_(rhs)
897 protected:
899 do_traverse(Traverse*);
901 bool
902 do_traverse_assignments(Traverse_assignments*)
903 { go_unreachable(); }
905 Statement*
906 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
908 Bstatement*
909 do_get_backend(Translate_context*)
910 { go_unreachable(); }
912 void
913 do_dump_statement(Ast_dump_context*) const;
915 private:
916 // The operator (OPERATOR_PLUSEQ, etc.).
917 Operator op_;
918 // Left hand side.
919 Expression* lhs_;
920 // Right hand side.
921 Expression* rhs_;
924 // Traversal.
927 Assignment_operation_statement::do_traverse(Traverse* traverse)
929 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
930 return TRAVERSE_EXIT;
931 return this->traverse_expression(traverse, &this->rhs_);
934 // Lower an assignment operation statement to a regular assignment
935 // statement.
937 Statement*
938 Assignment_operation_statement::do_lower(Gogo*, Named_object*,
939 Block* enclosing, Statement_inserter*)
941 Location loc = this->location();
943 // We have to evaluate the left hand side expression only once. We
944 // do this by moving out any expression with side effects.
945 Block* b = new Block(enclosing, loc);
946 Move_ordered_evals moe(b);
947 this->lhs_->traverse_subexpressions(&moe);
949 Expression* lval = this->lhs_->copy();
951 Operator op;
952 switch (this->op_)
954 case OPERATOR_PLUSEQ:
955 op = OPERATOR_PLUS;
956 break;
957 case OPERATOR_MINUSEQ:
958 op = OPERATOR_MINUS;
959 break;
960 case OPERATOR_OREQ:
961 op = OPERATOR_OR;
962 break;
963 case OPERATOR_XOREQ:
964 op = OPERATOR_XOR;
965 break;
966 case OPERATOR_MULTEQ:
967 op = OPERATOR_MULT;
968 break;
969 case OPERATOR_DIVEQ:
970 op = OPERATOR_DIV;
971 break;
972 case OPERATOR_MODEQ:
973 op = OPERATOR_MOD;
974 break;
975 case OPERATOR_LSHIFTEQ:
976 op = OPERATOR_LSHIFT;
977 break;
978 case OPERATOR_RSHIFTEQ:
979 op = OPERATOR_RSHIFT;
980 break;
981 case OPERATOR_ANDEQ:
982 op = OPERATOR_AND;
983 break;
984 case OPERATOR_BITCLEAREQ:
985 op = OPERATOR_BITCLEAR;
986 break;
987 default:
988 go_unreachable();
991 Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
992 Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
993 if (b->statements()->empty())
995 delete b;
996 return s;
998 else
1000 b->add_statement(s);
1001 return Statement::make_block_statement(b, loc);
1005 // Dump the AST representation for an assignment operation statement
1007 void
1008 Assignment_operation_statement::do_dump_statement(
1009 Ast_dump_context* ast_dump_context) const
1011 ast_dump_context->print_indent();
1012 ast_dump_context->dump_expression(this->lhs_);
1013 ast_dump_context->dump_operator(this->op_);
1014 ast_dump_context->dump_expression(this->rhs_);
1015 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1018 // Make an assignment operation statement.
1020 Statement*
1021 Statement::make_assignment_operation(Operator op, Expression* lhs,
1022 Expression* rhs, Location location)
1024 return new Assignment_operation_statement(op, lhs, rhs, location);
1027 // A tuple assignment statement. This differs from an assignment
1028 // statement in that the right-hand-side expressions are evaluated in
1029 // parallel.
1031 class Tuple_assignment_statement : public Statement
1033 public:
1034 Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
1035 Location location)
1036 : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
1037 lhs_(lhs), rhs_(rhs)
1040 protected:
1042 do_traverse(Traverse* traverse);
1044 bool
1045 do_traverse_assignments(Traverse_assignments*)
1046 { go_unreachable(); }
1048 Statement*
1049 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1051 Bstatement*
1052 do_get_backend(Translate_context*)
1053 { go_unreachable(); }
1055 void
1056 do_dump_statement(Ast_dump_context*) const;
1058 private:
1059 // Left hand side--a list of lvalues.
1060 Expression_list* lhs_;
1061 // Right hand side--a list of rvalues.
1062 Expression_list* rhs_;
1065 // Traversal.
1068 Tuple_assignment_statement::do_traverse(Traverse* traverse)
1070 if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
1071 return TRAVERSE_EXIT;
1072 return this->traverse_expression_list(traverse, this->rhs_);
1075 // Lower a tuple assignment. We use temporary variables to split it
1076 // up into a set of single assignments.
1078 Statement*
1079 Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
1080 Statement_inserter*)
1082 Location loc = this->location();
1084 Block* b = new Block(enclosing, loc);
1086 // First move out any subexpressions on the left hand side. The
1087 // right hand side will be evaluated in the required order anyhow.
1088 Move_ordered_evals moe(b);
1089 for (Expression_list::iterator plhs = this->lhs_->begin();
1090 plhs != this->lhs_->end();
1091 ++plhs)
1092 Expression::traverse(&*plhs, &moe);
1094 std::vector<Temporary_statement*> temps;
1095 temps.reserve(this->lhs_->size());
1097 Expression_list::const_iterator prhs = this->rhs_->begin();
1098 for (Expression_list::const_iterator plhs = this->lhs_->begin();
1099 plhs != this->lhs_->end();
1100 ++plhs, ++prhs)
1102 go_assert(prhs != this->rhs_->end());
1104 if ((*plhs)->is_error_expression()
1105 || (*plhs)->type()->is_error()
1106 || (*prhs)->is_error_expression()
1107 || (*prhs)->type()->is_error())
1108 continue;
1110 if ((*plhs)->is_sink_expression())
1112 if ((*prhs)->type()->is_nil_type())
1113 this->report_error(_("use of untyped nil"));
1114 else
1115 b->add_statement(Statement::make_statement(*prhs, true));
1116 continue;
1119 Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
1120 *prhs, loc);
1121 b->add_statement(temp);
1122 temps.push_back(temp);
1125 go_assert(prhs == this->rhs_->end());
1127 prhs = this->rhs_->begin();
1128 std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
1129 for (Expression_list::const_iterator plhs = this->lhs_->begin();
1130 plhs != this->lhs_->end();
1131 ++plhs, ++prhs)
1133 if ((*plhs)->is_error_expression()
1134 || (*plhs)->type()->is_error()
1135 || (*prhs)->is_error_expression()
1136 || (*prhs)->type()->is_error())
1137 continue;
1139 if ((*plhs)->is_sink_expression())
1140 continue;
1142 Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
1143 b->add_statement(Statement::make_assignment(*plhs, ref, loc));
1144 ++ptemp;
1146 go_assert(ptemp == temps.end() || saw_errors());
1148 return Statement::make_block_statement(b, loc);
1151 // Dump the AST representation for a tuple assignment statement.
1153 void
1154 Tuple_assignment_statement::do_dump_statement(
1155 Ast_dump_context* ast_dump_context) const
1157 ast_dump_context->print_indent();
1158 ast_dump_context->dump_expression_list(this->lhs_);
1159 ast_dump_context->ostream() << " = ";
1160 ast_dump_context->dump_expression_list(this->rhs_);
1161 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1164 // Make a tuple assignment statement.
1166 Statement*
1167 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
1168 Location location)
1170 return new Tuple_assignment_statement(lhs, rhs, location);
1173 // A tuple assignment from a map index expression.
1174 // v, ok = m[k]
1176 class Tuple_map_assignment_statement : public Statement
1178 public:
1179 Tuple_map_assignment_statement(Expression* val, Expression* present,
1180 Expression* map_index,
1181 Location location)
1182 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
1183 val_(val), present_(present), map_index_(map_index)
1186 protected:
1188 do_traverse(Traverse* traverse);
1190 bool
1191 do_traverse_assignments(Traverse_assignments*)
1192 { go_unreachable(); }
1194 Statement*
1195 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1197 Bstatement*
1198 do_get_backend(Translate_context*)
1199 { go_unreachable(); }
1201 void
1202 do_dump_statement(Ast_dump_context*) const;
1204 private:
1205 // Lvalue which receives the value from the map.
1206 Expression* val_;
1207 // Lvalue which receives whether the key value was present.
1208 Expression* present_;
1209 // The map index expression.
1210 Expression* map_index_;
1213 // Traversal.
1216 Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
1218 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1219 || this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
1220 return TRAVERSE_EXIT;
1221 return this->traverse_expression(traverse, &this->map_index_);
1224 // Lower a tuple map assignment.
1226 Statement*
1227 Tuple_map_assignment_statement::do_lower(Gogo* gogo, Named_object*,
1228 Block* enclosing, Statement_inserter*)
1230 Location loc = this->location();
1232 Map_index_expression* map_index = this->map_index_->map_index_expression();
1233 if (map_index == NULL)
1235 this->report_error(_("expected map index on right hand side"));
1236 return Statement::make_error_statement(loc);
1238 Map_type* map_type = map_index->get_map_type();
1239 if (map_type == NULL)
1240 return Statement::make_error_statement(loc);
1242 Block* b = new Block(enclosing, loc);
1244 // Move out any subexpressions to make sure that functions are
1245 // called in the required order.
1246 Move_ordered_evals moe(b);
1247 this->val_->traverse_subexpressions(&moe);
1248 this->present_->traverse_subexpressions(&moe);
1250 // Copy the key value into a temporary so that we can take its
1251 // address without pushing the value onto the heap.
1253 // var key_temp KEY_TYPE = MAP_INDEX
1254 Temporary_statement* key_temp =
1255 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1256 b->add_statement(key_temp);
1258 // var val_ptr_temp *VAL_TYPE
1259 Type* val_ptr_type = Type::make_pointer_type(map_type->val_type());
1260 Temporary_statement* val_ptr_temp = Statement::make_temporary(val_ptr_type,
1261 NULL, loc);
1262 b->add_statement(val_ptr_temp);
1264 // var present_temp bool
1265 Temporary_statement* present_temp =
1266 Statement::make_temporary((this->present_->type()->is_sink_type())
1267 ? Type::make_boolean_type()
1268 : this->present_->type(),
1269 NULL, loc);
1270 b->add_statement(present_temp);
1272 // val_ptr_temp, present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp)
1273 Expression* a1 = Expression::make_type_descriptor(map_type, loc);
1274 Expression* a2 = map_index->map();
1275 Temporary_reference_expression* ref =
1276 Expression::make_temporary_reference(key_temp, loc);
1277 Expression* a3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1278 Expression* a4 = map_type->fat_zero_value(gogo);
1279 Call_expression* call;
1280 if (a4 == NULL)
1281 call = Runtime::make_call(Runtime::MAPACCESS2, loc, 3, a1, a2, a3);
1282 else
1283 call = Runtime::make_call(Runtime::MAPACCESS2_FAT, loc, 4, a1, a2, a3, a4);
1284 ref = Expression::make_temporary_reference(val_ptr_temp, loc);
1285 ref->set_is_lvalue();
1286 Expression* res = Expression::make_call_result(call, 0);
1287 res = Expression::make_unsafe_cast(val_ptr_type, res, loc);
1288 Statement* s = Statement::make_assignment(ref, res, loc);
1289 b->add_statement(s);
1290 ref = Expression::make_temporary_reference(present_temp, loc);
1291 ref->set_is_lvalue();
1292 res = Expression::make_call_result(call, 1);
1293 s = Statement::make_assignment(ref, res, loc);
1294 b->add_statement(s);
1296 // val = *val__ptr_temp
1297 ref = Expression::make_temporary_reference(val_ptr_temp, loc);
1298 Expression* ind =
1299 Expression::make_dereference(ref, Expression::NIL_CHECK_NOT_NEEDED, loc);
1300 s = Statement::make_assignment(this->val_, ind, loc);
1301 b->add_statement(s);
1303 // present = present_temp
1304 ref = Expression::make_temporary_reference(present_temp, loc);
1305 s = Statement::make_assignment(this->present_, ref, loc);
1306 b->add_statement(s);
1308 return Statement::make_block_statement(b, loc);
1311 // Dump the AST representation for a tuple map assignment statement.
1313 void
1314 Tuple_map_assignment_statement::do_dump_statement(
1315 Ast_dump_context* ast_dump_context) const
1317 ast_dump_context->print_indent();
1318 ast_dump_context->dump_expression(this->val_);
1319 ast_dump_context->ostream() << ", ";
1320 ast_dump_context->dump_expression(this->present_);
1321 ast_dump_context->ostream() << " = ";
1322 ast_dump_context->dump_expression(this->map_index_);
1323 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1326 // Make a map assignment statement which returns a pair of values.
1328 Statement*
1329 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
1330 Expression* map_index,
1331 Location location)
1333 return new Tuple_map_assignment_statement(val, present, map_index, location);
1336 // A tuple assignment from a receive statement.
1338 class Tuple_receive_assignment_statement : public Statement
1340 public:
1341 Tuple_receive_assignment_statement(Expression* val, Expression* closed,
1342 Expression* channel, Location location)
1343 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
1344 val_(val), closed_(closed), channel_(channel)
1347 protected:
1349 do_traverse(Traverse* traverse);
1351 bool
1352 do_traverse_assignments(Traverse_assignments*)
1353 { go_unreachable(); }
1355 Statement*
1356 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1358 Bstatement*
1359 do_get_backend(Translate_context*)
1360 { go_unreachable(); }
1362 void
1363 do_dump_statement(Ast_dump_context*) const;
1365 private:
1366 // Lvalue which receives the value from the channel.
1367 Expression* val_;
1368 // Lvalue which receives whether the channel is closed.
1369 Expression* closed_;
1370 // The channel on which we receive the value.
1371 Expression* channel_;
1374 // Traversal.
1377 Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
1379 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1380 || this->traverse_expression(traverse, &this->closed_) == TRAVERSE_EXIT)
1381 return TRAVERSE_EXIT;
1382 return this->traverse_expression(traverse, &this->channel_);
1385 // Lower to a function call.
1387 Statement*
1388 Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
1389 Block* enclosing,
1390 Statement_inserter*)
1392 Location loc = this->location();
1394 Channel_type* channel_type = this->channel_->type()->channel_type();
1395 if (channel_type == NULL)
1397 this->report_error(_("expected channel"));
1398 return Statement::make_error_statement(loc);
1400 if (!channel_type->may_receive())
1402 this->report_error(_("invalid receive on send-only channel"));
1403 return Statement::make_error_statement(loc);
1406 Block* b = new Block(enclosing, loc);
1408 // Make sure that any subexpressions on the left hand side are
1409 // evaluated in the right order.
1410 Move_ordered_evals moe(b);
1411 this->val_->traverse_subexpressions(&moe);
1412 this->closed_->traverse_subexpressions(&moe);
1414 // var val_temp ELEMENT_TYPE
1415 Temporary_statement* val_temp =
1416 Statement::make_temporary(channel_type->element_type(), NULL, loc);
1417 b->add_statement(val_temp);
1419 // var closed_temp bool
1420 Temporary_statement* closed_temp =
1421 Statement::make_temporary((this->closed_->type()->is_sink_type())
1422 ? Type::make_boolean_type()
1423 : this->closed_->type(),
1424 NULL, loc);
1425 b->add_statement(closed_temp);
1427 // closed_temp = chanrecv2(channel, &val_temp)
1428 Temporary_reference_expression* ref =
1429 Expression::make_temporary_reference(val_temp, loc);
1430 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1431 Expression* call = Runtime::make_call(Runtime::CHANRECV2,
1432 loc, 2, this->channel_, p2);
1433 ref = Expression::make_temporary_reference(closed_temp, loc);
1434 ref->set_is_lvalue();
1435 Statement* s = Statement::make_assignment(ref, call, loc);
1436 b->add_statement(s);
1438 // val = val_temp
1439 ref = Expression::make_temporary_reference(val_temp, loc);
1440 s = Statement::make_assignment(this->val_, ref, loc);
1441 b->add_statement(s);
1443 // closed = closed_temp
1444 ref = Expression::make_temporary_reference(closed_temp, loc);
1445 s = Statement::make_assignment(this->closed_, ref, loc);
1446 b->add_statement(s);
1448 return Statement::make_block_statement(b, loc);
1451 // Dump the AST representation for a tuple receive statement.
1453 void
1454 Tuple_receive_assignment_statement::do_dump_statement(
1455 Ast_dump_context* ast_dump_context) const
1457 ast_dump_context->print_indent();
1458 ast_dump_context->dump_expression(this->val_);
1459 ast_dump_context->ostream() << ", ";
1460 ast_dump_context->dump_expression(this->closed_);
1461 ast_dump_context->ostream() << " <- ";
1462 ast_dump_context->dump_expression(this->channel_);
1463 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1466 // Make a nonblocking receive statement.
1468 Statement*
1469 Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
1470 Expression* channel,
1471 Location location)
1473 return new Tuple_receive_assignment_statement(val, closed, channel,
1474 location);
1477 // An assignment to a pair of values from a type guard. This is a
1478 // conditional type guard. v, ok = i.(type).
1480 class Tuple_type_guard_assignment_statement : public Statement
1482 public:
1483 Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
1484 Expression* expr, Type* type,
1485 Location location)
1486 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
1487 val_(val), ok_(ok), expr_(expr), type_(type)
1490 protected:
1492 do_traverse(Traverse*);
1494 bool
1495 do_traverse_assignments(Traverse_assignments*)
1496 { go_unreachable(); }
1498 Statement*
1499 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1501 Bstatement*
1502 do_get_backend(Translate_context*)
1503 { go_unreachable(); }
1505 void
1506 do_dump_statement(Ast_dump_context*) const;
1508 private:
1509 Call_expression*
1510 lower_to_type(Runtime::Function);
1512 void
1513 lower_to_object_type(Block*, Runtime::Function);
1515 // The variable which recieves the converted value.
1516 Expression* val_;
1517 // The variable which receives the indication of success.
1518 Expression* ok_;
1519 // The expression being converted.
1520 Expression* expr_;
1521 // The type to which the expression is being converted.
1522 Type* type_;
1525 // Traverse a type guard tuple assignment.
1528 Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
1530 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1531 || this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
1532 || this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
1533 return TRAVERSE_EXIT;
1534 return this->traverse_expression(traverse, &this->expr_);
1537 // Lower to a function call.
1539 Statement*
1540 Tuple_type_guard_assignment_statement::do_lower(Gogo*, Named_object*,
1541 Block* enclosing,
1542 Statement_inserter*)
1544 Location loc = this->location();
1546 Type* expr_type = this->expr_->type();
1547 if (expr_type->interface_type() == NULL)
1549 if (!expr_type->is_error() && !this->type_->is_error())
1550 this->report_error(_("type assertion only valid for interface types"));
1551 return Statement::make_error_statement(loc);
1554 Block* b = new Block(enclosing, loc);
1556 // Make sure that any subexpressions on the left hand side are
1557 // evaluated in the right order.
1558 Move_ordered_evals moe(b);
1559 this->val_->traverse_subexpressions(&moe);
1560 this->ok_->traverse_subexpressions(&moe);
1562 bool expr_is_empty = expr_type->interface_type()->is_empty();
1563 Call_expression* call;
1564 if (this->type_->interface_type() != NULL)
1566 if (this->type_->interface_type()->is_empty())
1567 call = Runtime::make_call((expr_is_empty
1568 ? Runtime::IFACEE2E2
1569 : Runtime::IFACEI2E2),
1570 loc, 1, this->expr_);
1571 else
1572 call = this->lower_to_type(expr_is_empty
1573 ? Runtime::IFACEE2I2
1574 : Runtime::IFACEI2I2);
1576 else if (this->type_->points_to() != NULL)
1577 call = this->lower_to_type(expr_is_empty
1578 ? Runtime::IFACEE2T2P
1579 : Runtime::IFACEI2T2P);
1580 else
1582 this->lower_to_object_type(b,
1583 (expr_is_empty
1584 ? Runtime::IFACEE2T2
1585 : Runtime::IFACEI2T2));
1586 call = NULL;
1589 if (call != NULL)
1591 Expression* res = Expression::make_call_result(call, 0);
1592 res = Expression::make_unsafe_cast(this->type_, res, loc);
1593 Statement* s = Statement::make_assignment(this->val_, res, loc);
1594 b->add_statement(s);
1596 res = Expression::make_call_result(call, 1);
1597 s = Statement::make_assignment(this->ok_, res, loc);
1598 b->add_statement(s);
1601 return Statement::make_block_statement(b, loc);
1604 // Lower a conversion to a non-empty interface type or a pointer type.
1606 Call_expression*
1607 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code)
1609 Location loc = this->location();
1610 return Runtime::make_call(code, loc, 2,
1611 Expression::make_type_descriptor(this->type_, loc),
1612 this->expr_);
1615 // Lower a conversion to a non-interface non-pointer type.
1617 void
1618 Tuple_type_guard_assignment_statement::lower_to_object_type(
1619 Block* b,
1620 Runtime::Function code)
1622 Location loc = this->location();
1624 // var val_temp TYPE
1625 Temporary_statement* val_temp = Statement::make_temporary(this->type_,
1626 NULL, loc);
1627 b->add_statement(val_temp);
1629 // ok = CODE(type_descriptor, expr, &val_temp)
1630 Expression* p1 = Expression::make_type_descriptor(this->type_, loc);
1631 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1632 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1633 Expression* call = Runtime::make_call(code, loc, 3, p1, this->expr_, p3);
1634 Statement* s = Statement::make_assignment(this->ok_, call, loc);
1635 b->add_statement(s);
1637 // val = val_temp
1638 ref = Expression::make_temporary_reference(val_temp, loc);
1639 s = Statement::make_assignment(this->val_, ref, loc);
1640 b->add_statement(s);
1643 // Dump the AST representation for a tuple type guard statement.
1645 void
1646 Tuple_type_guard_assignment_statement::do_dump_statement(
1647 Ast_dump_context* ast_dump_context) const
1649 ast_dump_context->print_indent();
1650 ast_dump_context->dump_expression(this->val_);
1651 ast_dump_context->ostream() << ", ";
1652 ast_dump_context->dump_expression(this->ok_);
1653 ast_dump_context->ostream() << " = ";
1654 ast_dump_context->dump_expression(this->expr_);
1655 ast_dump_context->ostream() << " . ";
1656 ast_dump_context->dump_type(this->type_);
1657 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1660 // Make an assignment from a type guard to a pair of variables.
1662 Statement*
1663 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
1664 Expression* expr, Type* type,
1665 Location location)
1667 return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
1668 location);
1671 // Class Expression_statement.
1673 // Constructor.
1675 Expression_statement::Expression_statement(Expression* expr, bool is_ignored)
1676 : Statement(STATEMENT_EXPRESSION, expr->location()),
1677 expr_(expr), is_ignored_(is_ignored)
1681 // Determine types.
1683 void
1684 Expression_statement::do_determine_types()
1686 this->expr_->determine_type_no_context();
1689 // Check the types of an expression statement. The only check we do
1690 // is to possibly give an error about discarding the value of the
1691 // expression.
1693 void
1694 Expression_statement::do_check_types(Gogo*)
1696 if (!this->is_ignored_)
1697 this->expr_->discarding_value();
1700 // An expression statement is only a terminating statement if it is
1701 // a call to panic.
1703 bool
1704 Expression_statement::do_may_fall_through() const
1706 const Call_expression* call = this->expr_->call_expression();
1707 if (call != NULL)
1709 const Expression* fn = call->fn();
1710 // panic is still an unknown named object.
1711 const Unknown_expression* ue = fn->unknown_expression();
1712 if (ue != NULL)
1714 Named_object* no = ue->named_object();
1716 if (no->is_unknown())
1717 no = no->unknown_value()->real_named_object();
1718 if (no != NULL)
1720 Function_type* fntype;
1721 if (no->is_function())
1722 fntype = no->func_value()->type();
1723 else if (no->is_function_declaration())
1724 fntype = no->func_declaration_value()->type();
1725 else
1726 fntype = NULL;
1728 // The builtin function panic does not return.
1729 if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
1730 return false;
1734 return true;
1737 // Convert to backend representation.
1739 Bstatement*
1740 Expression_statement::do_get_backend(Translate_context* context)
1742 Bexpression* bexpr = this->expr_->get_backend(context);
1743 Bfunction* bfunction = context->function()->func_value()->get_decl();
1744 return context->backend()->expression_statement(bfunction, bexpr);
1747 // Dump the AST representation for an expression statement
1749 void
1750 Expression_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
1751 const
1753 ast_dump_context->print_indent();
1754 ast_dump_context->dump_expression(expr_);
1755 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1758 // Make an expression statement from an Expression.
1760 Statement*
1761 Statement::make_statement(Expression* expr, bool is_ignored)
1763 return new Expression_statement(expr, is_ignored);
1766 // Convert a block to the backend representation of a statement.
1768 Bstatement*
1769 Block_statement::do_get_backend(Translate_context* context)
1771 Bblock* bblock = this->block_->get_backend(context);
1772 return context->backend()->block_statement(bblock);
1775 // Dump the AST for a block statement
1777 void
1778 Block_statement::do_dump_statement(Ast_dump_context*) const
1780 // block statement braces are dumped when traversing.
1783 // Make a block statement.
1785 Statement*
1786 Statement::make_block_statement(Block* block, Location location)
1788 return new Block_statement(block, location);
1791 // An increment or decrement statement.
1793 class Inc_dec_statement : public Statement
1795 public:
1796 Inc_dec_statement(bool is_inc, Expression* expr)
1797 : Statement(STATEMENT_INCDEC, expr->location()),
1798 expr_(expr), is_inc_(is_inc)
1801 protected:
1803 do_traverse(Traverse* traverse)
1804 { return this->traverse_expression(traverse, &this->expr_); }
1806 bool
1807 do_traverse_assignments(Traverse_assignments*)
1808 { go_unreachable(); }
1810 Statement*
1811 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1813 Bstatement*
1814 do_get_backend(Translate_context*)
1815 { go_unreachable(); }
1817 void
1818 do_dump_statement(Ast_dump_context*) const;
1820 private:
1821 // The l-value to increment or decrement.
1822 Expression* expr_;
1823 // Whether to increment or decrement.
1824 bool is_inc_;
1827 // Lower to += or -=.
1829 Statement*
1830 Inc_dec_statement::do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
1832 Location loc = this->location();
1833 if (!this->expr_->type()->is_numeric_type())
1835 this->report_error("increment or decrement of non-numeric type");
1836 return Statement::make_error_statement(loc);
1838 Expression* oexpr = Expression::make_integer_ul(1, this->expr_->type(), loc);
1839 Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
1840 return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
1843 // Dump the AST representation for a inc/dec statement.
1845 void
1846 Inc_dec_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
1848 ast_dump_context->print_indent();
1849 ast_dump_context->dump_expression(expr_);
1850 ast_dump_context->ostream() << (is_inc_? "++": "--") << dsuffix(location()) << std::endl;
1853 // Make an increment statement.
1855 Statement*
1856 Statement::make_inc_statement(Expression* expr)
1858 return new Inc_dec_statement(true, expr);
1861 // Make a decrement statement.
1863 Statement*
1864 Statement::make_dec_statement(Expression* expr)
1866 return new Inc_dec_statement(false, expr);
1869 // Class Thunk_statement. This is the base class for go and defer
1870 // statements.
1872 // Constructor.
1874 Thunk_statement::Thunk_statement(Statement_classification classification,
1875 Call_expression* call,
1876 Location location)
1877 : Statement(classification, location),
1878 call_(call), struct_type_(NULL)
1882 // Return whether this is a simple statement which does not require a
1883 // thunk.
1885 bool
1886 Thunk_statement::is_simple(Function_type* fntype) const
1888 // We need a thunk to call a method, or to pass a variable number of
1889 // arguments.
1890 if (fntype->is_method() || fntype->is_varargs())
1891 return false;
1893 // A defer statement requires a thunk to set up for whether the
1894 // function can call recover.
1895 if (this->classification() == STATEMENT_DEFER)
1896 return false;
1898 // We can only permit a single parameter of pointer type.
1899 const Typed_identifier_list* parameters = fntype->parameters();
1900 if (parameters != NULL
1901 && (parameters->size() > 1
1902 || (parameters->size() == 1
1903 && parameters->begin()->type()->points_to() == NULL)))
1904 return false;
1906 // If the function returns multiple values, or returns a type other
1907 // than integer, floating point, or pointer, then it may get a
1908 // hidden first parameter, in which case we need the more
1909 // complicated approach. This is true even though we are going to
1910 // ignore the return value.
1911 const Typed_identifier_list* results = fntype->results();
1912 if (results != NULL
1913 && (results->size() > 1
1914 || (results->size() == 1
1915 && !results->begin()->type()->is_basic_type()
1916 && results->begin()->type()->points_to() == NULL)))
1917 return false;
1919 // If this calls something that is not a simple function, then we
1920 // need a thunk.
1921 Expression* fn = this->call_->call_expression()->fn();
1922 if (fn->func_expression() == NULL)
1923 return false;
1925 // If the function uses a closure, then we need a thunk. FIXME: We
1926 // could accept a zero argument function with a closure.
1927 if (fn->func_expression()->closure() != NULL)
1928 return false;
1930 return true;
1933 // Traverse a thunk statement.
1936 Thunk_statement::do_traverse(Traverse* traverse)
1938 return this->traverse_expression(traverse, &this->call_);
1941 // We implement traverse_assignment for a thunk statement because it
1942 // effectively copies the function call.
1944 bool
1945 Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
1947 Expression* fn = this->call_->call_expression()->fn();
1948 Expression* fn2 = fn;
1949 tassign->value(&fn2, true, false);
1950 return true;
1953 // Determine types in a thunk statement.
1955 void
1956 Thunk_statement::do_determine_types()
1958 this->call_->determine_type_no_context();
1960 // Now that we know the types of the call, build the struct used to
1961 // pass parameters.
1962 Call_expression* ce = this->call_->call_expression();
1963 if (ce == NULL)
1964 return;
1965 Function_type* fntype = ce->get_function_type();
1966 if (fntype != NULL && !this->is_simple(fntype))
1967 this->struct_type_ = this->build_struct(fntype);
1970 // Check types in a thunk statement.
1972 void
1973 Thunk_statement::do_check_types(Gogo*)
1975 if (!this->call_->discarding_value())
1976 return;
1977 Call_expression* ce = this->call_->call_expression();
1978 if (ce == NULL)
1980 if (!this->call_->is_error_expression())
1981 this->report_error("expected call expression");
1982 return;
1986 // The Traverse class used to find and simplify thunk statements.
1988 class Simplify_thunk_traverse : public Traverse
1990 public:
1991 Simplify_thunk_traverse(Gogo* gogo)
1992 : Traverse(traverse_functions | traverse_blocks),
1993 gogo_(gogo), function_(NULL)
1997 function(Named_object*);
2000 block(Block*);
2002 private:
2003 // General IR.
2004 Gogo* gogo_;
2005 // The function we are traversing.
2006 Named_object* function_;
2009 // Keep track of the current function while looking for thunks.
2012 Simplify_thunk_traverse::function(Named_object* no)
2014 go_assert(this->function_ == NULL);
2015 this->function_ = no;
2016 int t = no->func_value()->traverse(this);
2017 this->function_ = NULL;
2018 if (t == TRAVERSE_EXIT)
2019 return t;
2020 return TRAVERSE_SKIP_COMPONENTS;
2023 // Look for thunks in a block.
2026 Simplify_thunk_traverse::block(Block* b)
2028 // The parser ensures that thunk statements always appear at the end
2029 // of a block.
2030 if (b->statements()->size() < 1)
2031 return TRAVERSE_CONTINUE;
2032 Thunk_statement* stat = b->statements()->back()->thunk_statement();
2033 if (stat == NULL)
2034 return TRAVERSE_CONTINUE;
2035 if (stat->simplify_statement(this->gogo_, this->function_, b))
2036 return TRAVERSE_SKIP_COMPONENTS;
2037 return TRAVERSE_CONTINUE;
2040 // Simplify all thunk statements.
2042 void
2043 Gogo::simplify_thunk_statements()
2045 Simplify_thunk_traverse thunk_traverse(this);
2046 this->traverse(&thunk_traverse);
2049 // Return true if the thunk function is a constant, which means that
2050 // it does not need to be passed to the thunk routine.
2052 bool
2053 Thunk_statement::is_constant_function() const
2055 Call_expression* ce = this->call_->call_expression();
2056 Function_type* fntype = ce->get_function_type();
2057 if (fntype == NULL)
2059 go_assert(saw_errors());
2060 return false;
2062 if (fntype->is_builtin())
2063 return true;
2064 Expression* fn = ce->fn();
2065 if (fn->func_expression() != NULL)
2066 return fn->func_expression()->closure() == NULL;
2067 if (fn->interface_field_reference_expression() != NULL)
2068 return true;
2069 return false;
2072 // Simplify complex thunk statements into simple ones. A complicated
2073 // thunk statement is one which takes anything other than zero
2074 // parameters or a single pointer parameter. We rewrite it into code
2075 // which allocates a struct, stores the parameter values into the
2076 // struct, and does a simple go or defer statement which passes the
2077 // struct to a thunk. The thunk does the real call.
2079 bool
2080 Thunk_statement::simplify_statement(Gogo* gogo, Named_object* function,
2081 Block* block)
2083 if (this->classification() == STATEMENT_ERROR)
2084 return false;
2085 if (this->call_->is_error_expression())
2086 return false;
2088 if (this->classification() == STATEMENT_DEFER)
2090 // Make sure that the defer stack exists for the function. We
2091 // will use when converting this statement to the backend
2092 // representation, but we want it to exist when we start
2093 // converting the function.
2094 function->func_value()->defer_stack(this->location());
2097 Call_expression* ce = this->call_->call_expression();
2098 Function_type* fntype = ce->get_function_type();
2099 if (fntype == NULL)
2101 go_assert(saw_errors());
2102 this->set_is_error();
2103 return false;
2105 if (this->is_simple(fntype))
2106 return false;
2108 Expression* fn = ce->fn();
2109 Interface_field_reference_expression* interface_method =
2110 fn->interface_field_reference_expression();
2112 Location location = this->location();
2114 bool is_constant_function = this->is_constant_function();
2115 Temporary_statement* fn_temp = NULL;
2116 if (!is_constant_function)
2118 fn_temp = Statement::make_temporary(NULL, fn, location);
2119 block->insert_statement_before(block->statements()->size() - 1, fn_temp);
2120 fn = Expression::make_temporary_reference(fn_temp, location);
2123 std::string thunk_name = Gogo::thunk_name();
2125 // Build the thunk.
2126 this->build_thunk(gogo, thunk_name);
2128 // Generate code to call the thunk.
2130 // Get the values to store into the struct which is the single
2131 // argument to the thunk.
2133 Expression_list* vals = new Expression_list();
2134 if (!is_constant_function)
2135 vals->push_back(fn);
2137 if (interface_method != NULL)
2138 vals->push_back(interface_method->expr());
2140 if (ce->args() != NULL)
2142 for (Expression_list::const_iterator p = ce->args()->begin();
2143 p != ce->args()->end();
2144 ++p)
2146 if ((*p)->is_constant())
2147 continue;
2148 vals->push_back(*p);
2152 // Build the struct.
2153 Expression* constructor =
2154 Expression::make_struct_composite_literal(this->struct_type_, vals,
2155 location);
2157 // Allocate the initialized struct on the heap.
2158 constructor = Expression::make_heap_expression(constructor, location);
2160 // Throw an error if the function is nil. This is so that for `go
2161 // nil` we get a backtrace from the go statement, rather than a
2162 // useless backtrace from the brand new goroutine.
2163 Expression* param = constructor;
2164 if (!is_constant_function)
2166 fn = Expression::make_temporary_reference(fn_temp, location);
2167 Expression* nil = Expression::make_nil(location);
2168 Expression* isnil = Expression::make_binary(OPERATOR_EQEQ, fn, nil,
2169 location);
2170 Expression* crash = gogo->runtime_error(RUNTIME_ERROR_GO_NIL, location);
2171 crash = Expression::make_conditional(isnil, crash,
2172 Expression::make_nil(location),
2173 location);
2174 param = Expression::make_compound(crash, constructor, location);
2177 // Look up the thunk.
2178 Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
2179 go_assert(named_thunk != NULL && named_thunk->is_function());
2181 // Build the call.
2182 Expression* func = Expression::make_func_reference(named_thunk, NULL,
2183 location);
2184 Expression_list* params = new Expression_list();
2185 params->push_back(param);
2186 Call_expression* call = Expression::make_call(func, params, false, location);
2188 // Build the simple go or defer statement.
2189 Statement* s;
2190 if (this->classification() == STATEMENT_GO)
2191 s = Statement::make_go_statement(call, location);
2192 else if (this->classification() == STATEMENT_DEFER)
2193 s = Statement::make_defer_statement(call, location);
2194 else
2195 go_unreachable();
2197 // The current block should end with the go statement.
2198 go_assert(block->statements()->size() >= 1);
2199 go_assert(block->statements()->back() == this);
2200 block->replace_statement(block->statements()->size() - 1, s);
2202 // We already ran the determine_types pass, so we need to run it now
2203 // for the new statement.
2204 s->determine_types();
2206 // Sanity check.
2207 gogo->check_types_in_block(block);
2209 // Return true to tell the block not to keep looking at statements.
2210 return true;
2213 // Set the name to use for thunk parameter N.
2215 void
2216 Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
2218 snprintf(buf, buflen, "a%d", n);
2221 // Build a new struct type to hold the parameters for a complicated
2222 // thunk statement. FNTYPE is the type of the function call.
2224 Struct_type*
2225 Thunk_statement::build_struct(Function_type* fntype)
2227 Location location = this->location();
2229 Struct_field_list* fields = new Struct_field_list();
2231 Call_expression* ce = this->call_->call_expression();
2232 Expression* fn = ce->fn();
2234 if (!this->is_constant_function())
2236 // The function to call.
2237 fields->push_back(Struct_field(Typed_identifier("fn", fntype,
2238 location)));
2241 // If this thunk statement calls a method on an interface, we pass
2242 // the interface object to the thunk.
2243 Interface_field_reference_expression* interface_method =
2244 fn->interface_field_reference_expression();
2245 if (interface_method != NULL)
2247 Typed_identifier tid("object", interface_method->expr()->type(),
2248 location);
2249 fields->push_back(Struct_field(tid));
2252 // The predeclared recover function has no argument. However, we
2253 // add an argument when building recover thunks. Handle that here.
2254 if (ce->is_recover_call())
2256 fields->push_back(Struct_field(Typed_identifier("can_recover",
2257 Type::lookup_bool_type(),
2258 location)));
2261 const Expression_list* args = ce->args();
2262 if (args != NULL)
2264 int i = 0;
2265 for (Expression_list::const_iterator p = args->begin();
2266 p != args->end();
2267 ++p, ++i)
2269 if ((*p)->is_constant())
2270 continue;
2272 char buf[50];
2273 this->thunk_field_param(i, buf, sizeof buf);
2274 fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
2275 location)));
2279 Struct_type *st = Type::make_struct_type(fields, location);
2280 st->set_is_struct_incomparable();
2281 return st;
2284 // Build the thunk we are going to call. This is a brand new, albeit
2285 // artificial, function.
2287 void
2288 Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name)
2290 Location location = this->location();
2292 Call_expression* ce = this->call_->call_expression();
2294 bool may_call_recover = false;
2295 if (this->classification() == STATEMENT_DEFER)
2297 Func_expression* fn = ce->fn()->func_expression();
2298 if (fn == NULL)
2299 may_call_recover = true;
2300 else
2302 const Named_object* no = fn->named_object();
2303 if (!no->is_function())
2304 may_call_recover = true;
2305 else
2306 may_call_recover = no->func_value()->calls_recover();
2310 // Build the type of the thunk. The thunk takes a single parameter,
2311 // which is a pointer to the special structure we build.
2312 const char* const parameter_name = "__go_thunk_parameter";
2313 Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
2314 Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
2315 thunk_parameters->push_back(Typed_identifier(parameter_name,
2316 pointer_to_struct_type,
2317 location));
2319 Typed_identifier_list* thunk_results = NULL;
2320 if (may_call_recover)
2322 // When deferring a function which may call recover, add a
2323 // return value, to disable tail call optimizations which will
2324 // break the way we check whether recover is permitted.
2325 thunk_results = new Typed_identifier_list();
2326 thunk_results->push_back(Typed_identifier("", Type::lookup_bool_type(),
2327 location));
2330 Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
2331 thunk_results,
2332 location);
2334 // Start building the thunk.
2335 Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
2336 location);
2338 gogo->start_block(location);
2340 // For a defer statement, start with a call to
2341 // __go_set_defer_retaddr. */
2342 Label* retaddr_label = NULL;
2343 if (may_call_recover)
2345 retaddr_label = gogo->add_label_reference("retaddr", location, false);
2346 Expression* arg = Expression::make_label_addr(retaddr_label, location);
2347 Expression* call = Runtime::make_call(Runtime::SETDEFERRETADDR,
2348 location, 1, arg);
2350 // This is a hack to prevent the middle-end from deleting the
2351 // label.
2352 gogo->start_block(location);
2353 gogo->add_statement(Statement::make_goto_statement(retaddr_label,
2354 location));
2355 Block* then_block = gogo->finish_block(location);
2356 then_block->determine_types();
2358 Statement* s = Statement::make_if_statement(call, then_block, NULL,
2359 location);
2360 s->determine_types();
2361 gogo->add_statement(s);
2363 function->func_value()->set_calls_defer_retaddr();
2366 // Get a reference to the parameter.
2367 Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
2368 go_assert(named_parameter != NULL && named_parameter->is_variable());
2370 // Build the call. Note that the field names are the same as the
2371 // ones used in build_struct.
2372 Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
2373 location);
2374 thunk_parameter =
2375 Expression::make_dereference(thunk_parameter,
2376 Expression::NIL_CHECK_NOT_NEEDED,
2377 location);
2379 Interface_field_reference_expression* interface_method =
2380 ce->fn()->interface_field_reference_expression();
2382 Expression* func_to_call;
2383 unsigned int next_index;
2384 if (this->is_constant_function())
2386 func_to_call = ce->fn();
2387 next_index = 0;
2389 else
2391 func_to_call = Expression::make_field_reference(thunk_parameter,
2392 0, location);
2393 next_index = 1;
2396 if (interface_method != NULL)
2398 // The main program passes the interface object.
2399 go_assert(next_index == 0);
2400 Expression* r = Expression::make_field_reference(thunk_parameter, 0,
2401 location);
2402 const std::string& name(interface_method->name());
2403 func_to_call = Expression::make_interface_field_reference(r, name,
2404 location);
2405 next_index = 1;
2408 Expression_list* call_params = new Expression_list();
2409 const Struct_field_list* fields = this->struct_type_->fields();
2410 Struct_field_list::const_iterator p = fields->begin();
2411 for (unsigned int i = 0; i < next_index; ++i)
2412 ++p;
2413 bool is_recover_call = ce->is_recover_call();
2414 Expression* recover_arg = NULL;
2416 const Expression_list* args = ce->args();
2417 if (args != NULL)
2419 for (Expression_list::const_iterator arg = args->begin();
2420 arg != args->end();
2421 ++arg)
2423 Expression* param;
2424 if ((*arg)->is_constant())
2425 param = *arg;
2426 else
2428 Expression* thunk_param =
2429 Expression::make_var_reference(named_parameter, location);
2430 thunk_param =
2431 Expression::make_dereference(thunk_param,
2432 Expression::NIL_CHECK_NOT_NEEDED,
2433 location);
2434 param = Expression::make_field_reference(thunk_param,
2435 next_index,
2436 location);
2437 ++next_index;
2440 if (!is_recover_call)
2441 call_params->push_back(param);
2442 else
2444 go_assert(call_params->empty());
2445 recover_arg = param;
2450 if (call_params->empty())
2452 delete call_params;
2453 call_params = NULL;
2456 Call_expression* call = Expression::make_call(func_to_call, call_params,
2457 false, location);
2459 // This call expression was already lowered before entering the
2460 // thunk statement. Don't try to lower varargs again, as that will
2461 // cause confusion for, e.g., method calls which already have a
2462 // receiver parameter.
2463 call->set_varargs_are_lowered();
2465 Statement* call_statement = Statement::make_statement(call, true);
2467 gogo->add_statement(call_statement);
2469 // If this is a defer statement, the label comes immediately after
2470 // the call.
2471 if (may_call_recover)
2473 gogo->add_label_definition("retaddr", location);
2475 Expression_list* vals = new Expression_list();
2476 vals->push_back(Expression::make_boolean(false, location));
2477 gogo->add_statement(Statement::make_return_statement(vals, location));
2480 Block* b = gogo->finish_block(location);
2482 gogo->add_block(b, location);
2484 gogo->lower_block(function, b);
2486 // We already ran the determine_types pass, so we need to run it
2487 // just for the call statement now. The other types are known.
2488 call_statement->determine_types();
2490 gogo->flatten_block(function, b);
2492 if (may_call_recover
2493 || recover_arg != NULL
2494 || this->classification() == STATEMENT_GO)
2496 // Dig up the call expression, which may have been changed
2497 // during lowering.
2498 go_assert(call_statement->classification() == STATEMENT_EXPRESSION);
2499 Expression_statement* es =
2500 static_cast<Expression_statement*>(call_statement);
2501 Call_expression* ce = es->expr()->call_expression();
2502 if (ce == NULL)
2503 go_assert(saw_errors());
2504 else
2506 if (may_call_recover)
2507 ce->set_is_deferred();
2508 if (this->classification() == STATEMENT_GO)
2509 ce->set_is_concurrent();
2510 if (recover_arg != NULL)
2511 ce->set_recover_arg(recover_arg);
2515 // That is all the thunk has to do.
2516 gogo->finish_function(location);
2519 // Get the function and argument expressions.
2521 bool
2522 Thunk_statement::get_fn_and_arg(Expression** pfn, Expression** parg)
2524 if (this->call_->is_error_expression())
2525 return false;
2527 Call_expression* ce = this->call_->call_expression();
2529 Expression* fn = ce->fn();
2530 Func_expression* fe = fn->func_expression();
2531 go_assert(fe != NULL);
2532 *pfn = Expression::make_func_code_reference(fe->named_object(),
2533 fe->location());
2535 const Expression_list* args = ce->args();
2536 if (args == NULL || args->empty())
2537 *parg = Expression::make_nil(this->location());
2538 else
2540 go_assert(args->size() == 1);
2541 *parg = args->front();
2544 return true;
2547 // Class Go_statement.
2549 Bstatement*
2550 Go_statement::do_get_backend(Translate_context* context)
2552 Expression* fn;
2553 Expression* arg;
2554 if (!this->get_fn_and_arg(&fn, &arg))
2555 return context->backend()->error_statement();
2557 Expression* call = Runtime::make_call(Runtime::GO, this->location(), 2,
2558 fn, arg);
2559 Bexpression* bcall = call->get_backend(context);
2560 Bfunction* bfunction = context->function()->func_value()->get_decl();
2561 return context->backend()->expression_statement(bfunction, bcall);
2564 // Dump the AST representation for go statement.
2566 void
2567 Go_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2569 ast_dump_context->print_indent();
2570 ast_dump_context->ostream() << "go ";
2571 ast_dump_context->dump_expression(this->call());
2572 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
2575 // Make a go statement.
2577 Statement*
2578 Statement::make_go_statement(Call_expression* call, Location location)
2580 return new Go_statement(call, location);
2583 // Class Defer_statement.
2585 Bstatement*
2586 Defer_statement::do_get_backend(Translate_context* context)
2588 Expression* fn;
2589 Expression* arg;
2590 if (!this->get_fn_and_arg(&fn, &arg))
2591 return context->backend()->error_statement();
2593 Location loc = this->location();
2594 Expression* ds = context->function()->func_value()->defer_stack(loc);
2596 Expression* call = Runtime::make_call(Runtime::DEFERPROC, loc, 3,
2597 ds, fn, arg);
2598 Bexpression* bcall = call->get_backend(context);
2599 Bfunction* bfunction = context->function()->func_value()->get_decl();
2600 return context->backend()->expression_statement(bfunction, bcall);
2603 // Dump the AST representation for defer statement.
2605 void
2606 Defer_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2608 ast_dump_context->print_indent();
2609 ast_dump_context->ostream() << "defer ";
2610 ast_dump_context->dump_expression(this->call());
2611 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
2614 // Make a defer statement.
2616 Statement*
2617 Statement::make_defer_statement(Call_expression* call,
2618 Location location)
2620 return new Defer_statement(call, location);
2623 // Class Return_statement.
2625 // Traverse assignments. We treat each return value as a top level
2626 // RHS in an expression.
2628 bool
2629 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2631 Expression_list* vals = this->vals_;
2632 if (vals != NULL)
2634 for (Expression_list::iterator p = vals->begin();
2635 p != vals->end();
2636 ++p)
2637 tassign->value(&*p, true, true);
2639 return true;
2642 // Lower a return statement. If we are returning a function call
2643 // which returns multiple values which match the current function,
2644 // split up the call's results. If the return statement lists
2645 // explicit values, implement this statement by assigning the values
2646 // to the result variables and change this statement to a naked
2647 // return. This lets panic/recover work correctly.
2649 Statement*
2650 Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
2651 Statement_inserter*)
2653 if (this->is_lowered_)
2654 return this;
2656 Expression_list* vals = this->vals_;
2657 this->vals_ = NULL;
2658 this->is_lowered_ = true;
2660 Location loc = this->location();
2662 size_t vals_count = vals == NULL ? 0 : vals->size();
2663 Function::Results* results = function->func_value()->result_variables();
2664 size_t results_count = results == NULL ? 0 : results->size();
2666 if (vals_count == 0)
2668 if (results_count > 0 && !function->func_value()->results_are_named())
2670 this->report_error(_("not enough arguments to return"));
2671 return this;
2673 return this;
2676 if (results_count == 0)
2678 this->report_error(_("return with value in function "
2679 "with no return type"));
2680 return this;
2683 // If the current function has multiple return values, and we are
2684 // returning a single call expression, split up the call expression.
2685 if (results_count > 1
2686 && vals->size() == 1
2687 && vals->front()->call_expression() != NULL)
2689 Call_expression* call = vals->front()->call_expression();
2690 call->set_expected_result_count(results_count);
2691 delete vals;
2692 vals = new Expression_list;
2693 for (size_t i = 0; i < results_count; ++i)
2694 vals->push_back(Expression::make_call_result(call, i));
2695 vals_count = results_count;
2698 if (vals_count < results_count)
2700 this->report_error(_("not enough arguments to return"));
2701 return this;
2704 if (vals_count > results_count)
2706 this->report_error(_("too many values in return statement"));
2707 return this;
2710 Block* b = new Block(enclosing, loc);
2712 Expression_list* lhs = new Expression_list();
2713 Expression_list* rhs = new Expression_list();
2715 Expression_list::const_iterator pe = vals->begin();
2716 int i = 1;
2717 for (Function::Results::const_iterator pr = results->begin();
2718 pr != results->end();
2719 ++pr, ++pe, ++i)
2721 Named_object* rv = *pr;
2722 Expression* e = *pe;
2724 // Check types now so that we give a good error message. The
2725 // result type is known. We determine the expression type
2726 // early.
2728 Type *rvtype = rv->result_var_value()->type();
2729 Type_context type_context(rvtype, false);
2730 e->determine_type(&type_context);
2732 std::string reason;
2733 if (Type::are_assignable(rvtype, e->type(), &reason))
2735 Expression* ve = Expression::make_var_reference(rv, e->location());
2736 lhs->push_back(ve);
2737 rhs->push_back(e);
2739 else
2741 if (reason.empty())
2742 go_error_at(e->location(),
2743 "incompatible type for return value %d", i);
2744 else
2745 go_error_at(e->location(),
2746 "incompatible type for return value %d (%s)",
2747 i, reason.c_str());
2750 go_assert(lhs->size() == rhs->size());
2752 if (lhs->empty())
2754 else if (lhs->size() == 1)
2756 b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
2757 loc));
2758 delete lhs;
2759 delete rhs;
2761 else
2762 b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
2764 b->add_statement(this);
2766 delete vals;
2768 return Statement::make_block_statement(b, loc);
2771 // Convert a return statement to the backend representation.
2773 Bstatement*
2774 Return_statement::do_get_backend(Translate_context* context)
2776 Location loc = this->location();
2778 Function* function = context->function()->func_value();
2779 Function::Results* results = function->result_variables();
2780 std::vector<Bexpression*> retvals;
2781 if (results != NULL && !results->empty())
2783 retvals.reserve(results->size());
2784 for (Function::Results::const_iterator p = results->begin();
2785 p != results->end();
2786 p++)
2788 Expression* vr = Expression::make_var_reference(*p, loc);
2789 retvals.push_back(vr->get_backend(context));
2793 return context->backend()->return_statement(function->get_decl(),
2794 retvals, loc);
2797 // Dump the AST representation for a return statement.
2799 void
2800 Return_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2802 ast_dump_context->print_indent();
2803 ast_dump_context->ostream() << "return " ;
2804 ast_dump_context->dump_expression_list(this->vals_);
2805 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
2808 // Make a return statement.
2810 Return_statement*
2811 Statement::make_return_statement(Expression_list* vals,
2812 Location location)
2814 return new Return_statement(vals, location);
2817 // Make a statement that returns the result of a call expression.
2819 Statement*
2820 Statement::make_return_from_call(Call_expression* call, Location location)
2822 size_t rc = call->result_count();
2823 if (rc == 0)
2824 return Statement::make_statement(call, true);
2825 else
2827 Expression_list* vals = new Expression_list();
2828 if (rc == 1)
2829 vals->push_back(call);
2830 else
2832 for (size_t i = 0; i < rc; ++i)
2833 vals->push_back(Expression::make_call_result(call, i));
2835 return Statement::make_return_statement(vals, location);
2839 // A break or continue statement.
2841 class Bc_statement : public Statement
2843 public:
2844 Bc_statement(bool is_break, Unnamed_label* label, Location location)
2845 : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
2846 label_(label), is_break_(is_break)
2849 bool
2850 is_break() const
2851 { return this->is_break_; }
2853 protected:
2855 do_traverse(Traverse*)
2856 { return TRAVERSE_CONTINUE; }
2858 bool
2859 do_may_fall_through() const
2860 { return false; }
2862 Bstatement*
2863 do_get_backend(Translate_context* context)
2864 { return this->label_->get_goto(context, this->location()); }
2866 void
2867 do_dump_statement(Ast_dump_context*) const;
2869 private:
2870 // The label that this branches to.
2871 Unnamed_label* label_;
2872 // True if this is "break", false if it is "continue".
2873 bool is_break_;
2876 // Dump the AST representation for a break/continue statement
2878 void
2879 Bc_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2881 ast_dump_context->print_indent();
2882 ast_dump_context->ostream() << (this->is_break_ ? "break" : "continue");
2883 if (this->label_ != NULL)
2885 ast_dump_context->ostream() << " ";
2886 ast_dump_context->dump_label_name(this->label_);
2888 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
2891 // Make a break statement.
2893 Statement*
2894 Statement::make_break_statement(Unnamed_label* label, Location location)
2896 return new Bc_statement(true, label, location);
2899 // Make a continue statement.
2901 Statement*
2902 Statement::make_continue_statement(Unnamed_label* label,
2903 Location location)
2905 return new Bc_statement(false, label, location);
2908 // Class Goto_statement.
2911 Goto_statement::do_traverse(Traverse*)
2913 return TRAVERSE_CONTINUE;
2916 // Check types for a label. There aren't any types per se, but we use
2917 // this to give an error if the label was never defined.
2919 void
2920 Goto_statement::do_check_types(Gogo*)
2922 if (!this->label_->is_defined())
2924 go_error_at(this->location(), "reference to undefined label %qs",
2925 Gogo::message_name(this->label_->name()).c_str());
2926 this->set_is_error();
2930 // Convert the goto statement to the backend representation.
2932 Bstatement*
2933 Goto_statement::do_get_backend(Translate_context* context)
2935 Blabel* blabel = this->label_->get_backend_label(context);
2936 return context->backend()->goto_statement(blabel, this->location());
2939 // Dump the AST representation for a goto statement.
2941 void
2942 Goto_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2944 ast_dump_context->print_indent();
2945 ast_dump_context->ostream() << "goto " << this->label_->name() << dsuffix(location()) << std::endl;
2948 // Make a goto statement.
2950 Statement*
2951 Statement::make_goto_statement(Label* label, Location location)
2953 return new Goto_statement(label, location);
2956 // Class Goto_unnamed_statement.
2959 Goto_unnamed_statement::do_traverse(Traverse*)
2961 return TRAVERSE_CONTINUE;
2964 // Convert the goto unnamed statement to the backend representation.
2966 Bstatement*
2967 Goto_unnamed_statement::do_get_backend(Translate_context* context)
2969 return this->label_->get_goto(context, this->location());
2972 // Dump the AST representation for an unnamed goto statement
2974 void
2975 Goto_unnamed_statement::do_dump_statement(
2976 Ast_dump_context* ast_dump_context) const
2978 ast_dump_context->print_indent();
2979 ast_dump_context->ostream() << "goto ";
2980 ast_dump_context->dump_label_name(this->label_);
2981 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
2984 // Make a goto statement to an unnamed label.
2986 Statement*
2987 Statement::make_goto_unnamed_statement(Unnamed_label* label,
2988 Location location)
2990 return new Goto_unnamed_statement(label, location);
2993 // Class Label_statement.
2995 // Traversal.
2998 Label_statement::do_traverse(Traverse*)
3000 return TRAVERSE_CONTINUE;
3003 // Return the backend representation of the statement defining this
3004 // label.
3006 Bstatement*
3007 Label_statement::do_get_backend(Translate_context* context)
3009 if (this->label_->is_dummy_label())
3011 Bexpression* bce = context->backend()->boolean_constant_expression(false);
3012 Bfunction* bfunction = context->function()->func_value()->get_decl();
3013 return context->backend()->expression_statement(bfunction, bce);
3015 Blabel* blabel = this->label_->get_backend_label(context);
3016 return context->backend()->label_definition_statement(blabel);
3019 // Dump the AST for a label definition statement.
3021 void
3022 Label_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3024 ast_dump_context->print_indent();
3025 ast_dump_context->ostream() << this->label_->name() << ":" << dsuffix(location()) << std::endl;
3028 // Make a label statement.
3030 Statement*
3031 Statement::make_label_statement(Label* label, Location location)
3033 return new Label_statement(label, location);
3036 // Class Unnamed_label_statement.
3038 Unnamed_label_statement::Unnamed_label_statement(Unnamed_label* label)
3039 : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
3040 label_(label)
3044 Unnamed_label_statement::do_traverse(Traverse*)
3046 return TRAVERSE_CONTINUE;
3049 // Get the backend definition for this unnamed label statement.
3051 Bstatement*
3052 Unnamed_label_statement::do_get_backend(Translate_context* context)
3054 return this->label_->get_definition(context);
3057 // Dump the AST representation for an unnamed label definition statement.
3059 void
3060 Unnamed_label_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
3061 const
3063 ast_dump_context->print_indent();
3064 ast_dump_context->dump_label_name(this->label_);
3065 ast_dump_context->ostream() << ":" << dsuffix(location()) << std::endl;
3068 // Make an unnamed label statement.
3070 Statement*
3071 Statement::make_unnamed_label_statement(Unnamed_label* label)
3073 return new Unnamed_label_statement(label);
3076 // Class If_statement.
3078 // Traversal.
3081 If_statement::do_traverse(Traverse* traverse)
3083 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT
3084 || this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
3085 return TRAVERSE_EXIT;
3086 if (this->else_block_ != NULL)
3088 if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
3089 return TRAVERSE_EXIT;
3091 return TRAVERSE_CONTINUE;
3094 void
3095 If_statement::do_determine_types()
3097 Type_context context(Type::lookup_bool_type(), false);
3098 this->cond_->determine_type(&context);
3099 this->then_block_->determine_types();
3100 if (this->else_block_ != NULL)
3101 this->else_block_->determine_types();
3104 // Check types.
3106 void
3107 If_statement::do_check_types(Gogo*)
3109 Type* type = this->cond_->type();
3110 if (type->is_error())
3111 this->set_is_error();
3112 else if (!type->is_boolean_type())
3113 this->report_error(_("expected boolean expression"));
3116 // Whether the overall statement may fall through.
3118 bool
3119 If_statement::do_may_fall_through() const
3121 return (this->else_block_ == NULL
3122 || this->then_block_->may_fall_through()
3123 || this->else_block_->may_fall_through());
3126 // Get the backend representation.
3128 Bstatement*
3129 If_statement::do_get_backend(Translate_context* context)
3131 go_assert(this->cond_->type()->is_boolean_type()
3132 || this->cond_->type()->is_error());
3133 Bexpression* cond = this->cond_->get_backend(context);
3134 Bblock* then_block = this->then_block_->get_backend(context);
3135 Bblock* else_block = (this->else_block_ == NULL
3136 ? NULL
3137 : this->else_block_->get_backend(context));
3138 Bfunction* bfunction = context->function()->func_value()->get_decl();
3139 return context->backend()->if_statement(bfunction,
3140 cond, then_block, else_block,
3141 this->location());
3144 // Dump the AST representation for an if statement
3146 void
3147 If_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3149 ast_dump_context->print_indent();
3150 ast_dump_context->ostream() << "if ";
3151 ast_dump_context->dump_expression(this->cond_);
3152 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
3153 if (ast_dump_context->dump_subblocks())
3155 ast_dump_context->dump_block(this->then_block_);
3156 if (this->else_block_ != NULL)
3158 ast_dump_context->print_indent();
3159 ast_dump_context->ostream() << "else" << std::endl;
3160 ast_dump_context->dump_block(this->else_block_);
3165 // Make an if statement.
3167 Statement*
3168 Statement::make_if_statement(Expression* cond, Block* then_block,
3169 Block* else_block, Location location)
3171 return new If_statement(cond, then_block, else_block, location);
3174 // Class Case_clauses::Hash_integer_value.
3176 class Case_clauses::Hash_integer_value
3178 public:
3179 size_t
3180 operator()(Expression*) const;
3183 size_t
3184 Case_clauses::Hash_integer_value::operator()(Expression* pe) const
3186 Numeric_constant nc;
3187 mpz_t ival;
3188 if (!pe->numeric_constant_value(&nc) || !nc.to_int(&ival))
3189 go_unreachable();
3190 size_t ret = mpz_get_ui(ival);
3191 mpz_clear(ival);
3192 return ret;
3195 // Class Case_clauses::Eq_integer_value.
3197 class Case_clauses::Eq_integer_value
3199 public:
3200 bool
3201 operator()(Expression*, Expression*) const;
3204 bool
3205 Case_clauses::Eq_integer_value::operator()(Expression* a, Expression* b) const
3207 Numeric_constant anc;
3208 mpz_t aval;
3209 Numeric_constant bnc;
3210 mpz_t bval;
3211 if (!a->numeric_constant_value(&anc)
3212 || !anc.to_int(&aval)
3213 || !b->numeric_constant_value(&bnc)
3214 || !bnc.to_int(&bval))
3215 go_unreachable();
3216 bool ret = mpz_cmp(aval, bval) == 0;
3217 mpz_clear(aval);
3218 mpz_clear(bval);
3219 return ret;
3222 // Class Case_clauses::Case_clause.
3224 // Traversal.
3227 Case_clauses::Case_clause::traverse(Traverse* traverse)
3229 if (this->cases_ != NULL
3230 && (traverse->traverse_mask()
3231 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3233 if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
3234 return TRAVERSE_EXIT;
3236 if (this->statements_ != NULL)
3238 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3239 return TRAVERSE_EXIT;
3241 return TRAVERSE_CONTINUE;
3244 // Check whether all the case expressions are integer constants.
3246 bool
3247 Case_clauses::Case_clause::is_constant() const
3249 if (this->cases_ != NULL)
3251 for (Expression_list::const_iterator p = this->cases_->begin();
3252 p != this->cases_->end();
3253 ++p)
3254 if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
3255 return false;
3257 return true;
3260 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3261 // value we are switching on; it may be NULL. If START_LABEL is not
3262 // NULL, it goes at the start of the statements, after the condition
3263 // test. We branch to FINISH_LABEL at the end of the statements.
3265 void
3266 Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
3267 Unnamed_label* start_label,
3268 Unnamed_label* finish_label) const
3270 Location loc = this->location_;
3271 Unnamed_label* next_case_label;
3272 if (this->cases_ == NULL || this->cases_->empty())
3274 go_assert(this->is_default_);
3275 next_case_label = NULL;
3277 else
3279 Expression* cond = NULL;
3281 for (Expression_list::const_iterator p = this->cases_->begin();
3282 p != this->cases_->end();
3283 ++p)
3285 Expression* ref = Expression::make_temporary_reference(val_temp,
3286 loc);
3287 Expression* this_cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3288 *p, loc);
3289 if (cond == NULL)
3290 cond = this_cond;
3291 else
3292 cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
3295 Block* then_block = new Block(b, loc);
3296 next_case_label = new Unnamed_label(Linemap::unknown_location());
3297 Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
3298 loc);
3299 then_block->add_statement(s);
3301 // if !COND { goto NEXT_CASE_LABEL }
3302 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3303 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3304 b->add_statement(s);
3307 if (start_label != NULL)
3308 b->add_statement(Statement::make_unnamed_label_statement(start_label));
3310 if (this->statements_ != NULL)
3311 b->add_statement(Statement::make_block_statement(this->statements_, loc));
3313 Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
3314 b->add_statement(s);
3316 if (next_case_label != NULL)
3317 b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
3320 // Determine types.
3322 void
3323 Case_clauses::Case_clause::determine_types(Type* type)
3325 if (this->cases_ != NULL)
3327 Type_context case_context(type, false);
3328 for (Expression_list::iterator p = this->cases_->begin();
3329 p != this->cases_->end();
3330 ++p)
3331 (*p)->determine_type(&case_context);
3333 if (this->statements_ != NULL)
3334 this->statements_->determine_types();
3337 // Check types. Returns false if there was an error.
3339 bool
3340 Case_clauses::Case_clause::check_types(Type* type)
3342 if (this->cases_ != NULL)
3344 for (Expression_list::iterator p = this->cases_->begin();
3345 p != this->cases_->end();
3346 ++p)
3348 if (!Type::are_assignable(type, (*p)->type(), NULL)
3349 && !Type::are_assignable((*p)->type(), type, NULL))
3351 go_error_at((*p)->location(),
3352 "type mismatch between switch value and case clause");
3353 return false;
3357 return true;
3360 // Return true if this clause may fall through to the following
3361 // statements. Note that this is not the same as whether the case
3362 // uses the "fallthrough" keyword.
3364 bool
3365 Case_clauses::Case_clause::may_fall_through() const
3367 if (this->statements_ == NULL)
3368 return true;
3369 return this->statements_->may_fall_through();
3372 // Convert the case values and statements to the backend
3373 // representation. BREAK_LABEL is the label which break statements
3374 // should branch to. CASE_CONSTANTS is used to detect duplicate
3375 // constants. *CASES should be passed as an empty vector; the values
3376 // for this case will be added to it. If this is the default case,
3377 // *CASES will remain empty. This returns the statement to execute if
3378 // one of these cases is selected.
3380 Bstatement*
3381 Case_clauses::Case_clause::get_backend(Translate_context* context,
3382 Unnamed_label* break_label,
3383 Case_constants* case_constants,
3384 std::vector<Bexpression*>* cases) const
3386 if (this->cases_ != NULL)
3388 go_assert(!this->is_default_);
3389 for (Expression_list::const_iterator p = this->cases_->begin();
3390 p != this->cases_->end();
3391 ++p)
3393 Expression* e = *p;
3394 if (e->classification() != Expression::EXPRESSION_INTEGER)
3396 Numeric_constant nc;
3397 mpz_t ival;
3398 if (!(*p)->numeric_constant_value(&nc) || !nc.to_int(&ival))
3400 // Something went wrong. This can happen with a
3401 // negative constant and an unsigned switch value.
3402 go_assert(saw_errors());
3403 continue;
3405 go_assert(nc.type() != NULL);
3406 e = Expression::make_integer_z(&ival, nc.type(), e->location());
3407 mpz_clear(ival);
3410 std::pair<Case_constants::iterator, bool> ins =
3411 case_constants->insert(e);
3412 if (!ins.second)
3414 // Value was already present.
3415 go_error_at(this->location_, "duplicate case in switch");
3416 e = Expression::make_error(this->location_);
3418 cases->push_back(e->get_backend(context));
3422 Bstatement* statements;
3423 if (this->statements_ == NULL)
3424 statements = NULL;
3425 else
3427 Bblock* bblock = this->statements_->get_backend(context);
3428 statements = context->backend()->block_statement(bblock);
3431 Bstatement* break_stat;
3432 if (this->is_fallthrough_)
3433 break_stat = NULL;
3434 else
3435 break_stat = break_label->get_goto(context, this->location_);
3437 if (statements == NULL)
3438 return break_stat;
3439 else if (break_stat == NULL)
3440 return statements;
3441 else
3442 return context->backend()->compound_statement(statements, break_stat);
3445 // Dump the AST representation for a case clause
3447 void
3448 Case_clauses::Case_clause::dump_clause(Ast_dump_context* ast_dump_context)
3449 const
3451 ast_dump_context->print_indent();
3452 if (this->is_default_)
3454 ast_dump_context->ostream() << "default:";
3456 else
3458 ast_dump_context->ostream() << "case ";
3459 ast_dump_context->dump_expression_list(this->cases_);
3460 ast_dump_context->ostream() << ":" ;
3462 ast_dump_context->dump_block(this->statements_);
3463 if (this->is_fallthrough_)
3465 ast_dump_context->print_indent();
3466 ast_dump_context->ostream() << " (fallthrough)" << dsuffix(location()) << std::endl;
3470 // Class Case_clauses.
3472 // Traversal.
3475 Case_clauses::traverse(Traverse* traverse)
3477 for (Clauses::iterator p = this->clauses_.begin();
3478 p != this->clauses_.end();
3479 ++p)
3481 if (p->traverse(traverse) == TRAVERSE_EXIT)
3482 return TRAVERSE_EXIT;
3484 return TRAVERSE_CONTINUE;
3487 // Check whether all the case expressions are constant.
3489 bool
3490 Case_clauses::is_constant() const
3492 for (Clauses::const_iterator p = this->clauses_.begin();
3493 p != this->clauses_.end();
3494 ++p)
3495 if (!p->is_constant())
3496 return false;
3497 return true;
3500 // Lower case clauses for a nonconstant switch.
3502 void
3503 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3504 Unnamed_label* break_label) const
3506 // The default case.
3507 const Case_clause* default_case = NULL;
3509 // The label for the fallthrough of the previous case.
3510 Unnamed_label* last_fallthrough_label = NULL;
3512 // The label for the start of the default case. This is used if the
3513 // case before the default case falls through.
3514 Unnamed_label* default_start_label = NULL;
3516 // The label for the end of the default case. This normally winds
3517 // up as BREAK_LABEL, but it will be different if the default case
3518 // falls through.
3519 Unnamed_label* default_finish_label = NULL;
3521 for (Clauses::const_iterator p = this->clauses_.begin();
3522 p != this->clauses_.end();
3523 ++p)
3525 // The label to use for the start of the statements for this
3526 // case. This is NULL unless the previous case falls through.
3527 Unnamed_label* start_label = last_fallthrough_label;
3529 // The label to jump to after the end of the statements for this
3530 // case.
3531 Unnamed_label* finish_label = break_label;
3533 last_fallthrough_label = NULL;
3534 if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3536 finish_label = new Unnamed_label(p->location());
3537 last_fallthrough_label = finish_label;
3540 if (!p->is_default())
3541 p->lower(b, val_temp, start_label, finish_label);
3542 else
3544 // We have to move the default case to the end, so that we
3545 // only use it if all the other tests fail.
3546 default_case = &*p;
3547 default_start_label = start_label;
3548 default_finish_label = finish_label;
3552 if (default_case != NULL)
3553 default_case->lower(b, val_temp, default_start_label,
3554 default_finish_label);
3557 // Determine types.
3559 void
3560 Case_clauses::determine_types(Type* type)
3562 for (Clauses::iterator p = this->clauses_.begin();
3563 p != this->clauses_.end();
3564 ++p)
3565 p->determine_types(type);
3568 // Check types. Returns false if there was an error.
3570 bool
3571 Case_clauses::check_types(Type* type)
3573 bool ret = true;
3574 for (Clauses::iterator p = this->clauses_.begin();
3575 p != this->clauses_.end();
3576 ++p)
3578 if (!p->check_types(type))
3579 ret = false;
3581 return ret;
3584 // Return true if these clauses may fall through to the statements
3585 // following the switch statement.
3587 bool
3588 Case_clauses::may_fall_through() const
3590 bool found_default = false;
3591 for (Clauses::const_iterator p = this->clauses_.begin();
3592 p != this->clauses_.end();
3593 ++p)
3595 if (p->may_fall_through() && !p->is_fallthrough())
3596 return true;
3597 if (p->is_default())
3598 found_default = true;
3600 return !found_default;
3603 // Convert the cases to the backend representation. This sets
3604 // *ALL_CASES and *ALL_STATEMENTS.
3606 void
3607 Case_clauses::get_backend(Translate_context* context,
3608 Unnamed_label* break_label,
3609 std::vector<std::vector<Bexpression*> >* all_cases,
3610 std::vector<Bstatement*>* all_statements) const
3612 Case_constants case_constants;
3614 size_t c = this->clauses_.size();
3615 all_cases->resize(c);
3616 all_statements->resize(c);
3618 size_t i = 0;
3619 for (Clauses::const_iterator p = this->clauses_.begin();
3620 p != this->clauses_.end();
3621 ++p, ++i)
3623 std::vector<Bexpression*> cases;
3624 Bstatement* stat = p->get_backend(context, break_label, &case_constants,
3625 &cases);
3626 // The final clause can't fall through.
3627 if (i == c - 1 && p->is_fallthrough())
3629 go_assert(saw_errors());
3630 stat = context->backend()->error_statement();
3632 (*all_cases)[i].swap(cases);
3633 (*all_statements)[i] = stat;
3637 // Dump the AST representation for case clauses (from a switch statement)
3639 void
3640 Case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
3642 for (Clauses::const_iterator p = this->clauses_.begin();
3643 p != this->clauses_.end();
3644 ++p)
3645 p->dump_clause(ast_dump_context);
3648 // A constant switch statement. A Switch_statement is lowered to this
3649 // when all the cases are constants.
3651 class Constant_switch_statement : public Statement
3653 public:
3654 Constant_switch_statement(Expression* val, Case_clauses* clauses,
3655 Unnamed_label* break_label,
3656 Location location)
3657 : Statement(STATEMENT_CONSTANT_SWITCH, location),
3658 val_(val), clauses_(clauses), break_label_(break_label)
3661 protected:
3663 do_traverse(Traverse*);
3665 void
3666 do_determine_types();
3668 void
3669 do_check_types(Gogo*);
3671 Bstatement*
3672 do_get_backend(Translate_context*);
3674 void
3675 do_dump_statement(Ast_dump_context*) const;
3677 private:
3678 // The value to switch on.
3679 Expression* val_;
3680 // The case clauses.
3681 Case_clauses* clauses_;
3682 // The break label, if needed.
3683 Unnamed_label* break_label_;
3686 // Traversal.
3689 Constant_switch_statement::do_traverse(Traverse* traverse)
3691 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3692 return TRAVERSE_EXIT;
3693 return this->clauses_->traverse(traverse);
3696 // Determine types.
3698 void
3699 Constant_switch_statement::do_determine_types()
3701 this->val_->determine_type_no_context();
3702 this->clauses_->determine_types(this->val_->type());
3705 // Check types.
3707 void
3708 Constant_switch_statement::do_check_types(Gogo*)
3710 if (!this->clauses_->check_types(this->val_->type()))
3711 this->set_is_error();
3714 // Convert to GENERIC.
3716 Bstatement*
3717 Constant_switch_statement::do_get_backend(Translate_context* context)
3719 Bexpression* switch_val_expr = this->val_->get_backend(context);
3721 Unnamed_label* break_label = this->break_label_;
3722 if (break_label == NULL)
3723 break_label = new Unnamed_label(this->location());
3725 std::vector<std::vector<Bexpression*> > all_cases;
3726 std::vector<Bstatement*> all_statements;
3727 this->clauses_->get_backend(context, break_label, &all_cases,
3728 &all_statements);
3730 Bfunction* bfunction = context->function()->func_value()->get_decl();
3731 Bstatement* switch_statement;
3732 switch_statement = context->backend()->switch_statement(bfunction,
3733 switch_val_expr,
3734 all_cases,
3735 all_statements,
3736 this->location());
3737 Bstatement* ldef = break_label->get_definition(context);
3738 return context->backend()->compound_statement(switch_statement, ldef);
3741 // Dump the AST representation for a constant switch statement.
3743 void
3744 Constant_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
3745 const
3747 ast_dump_context->print_indent();
3748 ast_dump_context->ostream() << "switch ";
3749 ast_dump_context->dump_expression(this->val_);
3751 if (ast_dump_context->dump_subblocks())
3753 ast_dump_context->ostream() << " {" << std::endl;
3754 this->clauses_->dump_clauses(ast_dump_context);
3755 ast_dump_context->ostream() << "}";
3758 ast_dump_context->ostream() << std::endl;
3761 // Class Switch_statement.
3763 // Traversal.
3766 Switch_statement::do_traverse(Traverse* traverse)
3768 if (this->val_ != NULL)
3770 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3771 return TRAVERSE_EXIT;
3773 return this->clauses_->traverse(traverse);
3776 // Lower a Switch_statement to a Constant_switch_statement or a series
3777 // of if statements.
3779 Statement*
3780 Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
3781 Statement_inserter*)
3783 Location loc = this->location();
3785 if (this->val_ != NULL
3786 && (this->val_->is_error_expression()
3787 || this->val_->type()->is_error()))
3789 go_assert(saw_errors());
3790 return Statement::make_error_statement(loc);
3793 if (this->val_ != NULL
3794 && this->val_->type()->integer_type() != NULL
3795 && !this->clauses_->empty()
3796 && this->clauses_->is_constant())
3797 return new Constant_switch_statement(this->val_, this->clauses_,
3798 this->break_label_, loc);
3800 if (this->val_ != NULL
3801 && !this->val_->type()->is_comparable()
3802 && !Type::are_compatible_for_comparison(true, this->val_->type(),
3803 Type::make_nil_type(), NULL))
3805 go_error_at(this->val_->location(),
3806 "cannot switch on value whose type that may not be compared");
3807 return Statement::make_error_statement(loc);
3810 Block* b = new Block(enclosing, loc);
3812 if (this->clauses_->empty())
3814 Expression* val = this->val_;
3815 if (val == NULL)
3816 val = Expression::make_boolean(true, loc);
3817 return Statement::make_statement(val, true);
3820 // var val_temp VAL_TYPE = VAL
3821 Expression* val = this->val_;
3822 if (val == NULL)
3823 val = Expression::make_boolean(true, loc);
3825 Type* type = val->type();
3826 if (type->is_abstract())
3827 type = type->make_non_abstract_type();
3828 Temporary_statement* val_temp = Statement::make_temporary(type, val, loc);
3829 b->add_statement(val_temp);
3831 this->clauses_->lower(b, val_temp, this->break_label());
3833 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3834 b->add_statement(s);
3836 return Statement::make_block_statement(b, loc);
3839 // Return the break label for this switch statement, creating it if
3840 // necessary.
3842 Unnamed_label*
3843 Switch_statement::break_label()
3845 if (this->break_label_ == NULL)
3846 this->break_label_ = new Unnamed_label(this->location());
3847 return this->break_label_;
3850 // Dump the AST representation for a switch statement.
3852 void
3853 Switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3855 ast_dump_context->print_indent();
3856 ast_dump_context->ostream() << "switch ";
3857 if (this->val_ != NULL)
3859 ast_dump_context->dump_expression(this->val_);
3861 if (ast_dump_context->dump_subblocks())
3863 ast_dump_context->ostream() << " {" << dsuffix(location()) << std::endl;
3864 this->clauses_->dump_clauses(ast_dump_context);
3865 ast_dump_context->print_indent();
3866 ast_dump_context->ostream() << "}";
3868 ast_dump_context->ostream() << std::endl;
3871 // Return whether this switch may fall through.
3873 bool
3874 Switch_statement::do_may_fall_through() const
3876 if (this->clauses_ == NULL)
3877 return true;
3879 // If we have a break label, then some case needed it. That implies
3880 // that the switch statement as a whole can fall through.
3881 if (this->break_label_ != NULL)
3882 return true;
3884 return this->clauses_->may_fall_through();
3887 // Make a switch statement.
3889 Switch_statement*
3890 Statement::make_switch_statement(Expression* val, Location location)
3892 return new Switch_statement(val, location);
3895 // Class Type_case_clauses::Type_case_clause.
3897 // Traversal.
3900 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3902 if (!this->is_default_
3903 && ((traverse->traverse_mask()
3904 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3905 && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3906 return TRAVERSE_EXIT;
3907 if (this->statements_ != NULL)
3908 return this->statements_->traverse(traverse);
3909 return TRAVERSE_CONTINUE;
3912 // Lower one clause in a type switch. Add statements to the block B.
3913 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3914 // BREAK_LABEL is the label at the end of the type switch.
3915 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3916 // statements.
3918 void
3919 Type_case_clauses::Type_case_clause::lower(Type* switch_val_type,
3920 Block* b,
3921 Temporary_statement* descriptor_temp,
3922 Unnamed_label* break_label,
3923 Unnamed_label** stmts_label) const
3925 Location loc = this->location_;
3927 Unnamed_label* next_case_label = NULL;
3928 if (!this->is_default_)
3930 Type* type = this->type_;
3932 std::string reason;
3933 if (switch_val_type->interface_type() != NULL
3934 && !type->is_nil_constant_as_type()
3935 && type->interface_type() == NULL
3936 && !switch_val_type->interface_type()->implements_interface(type,
3937 &reason))
3939 if (reason.empty())
3940 go_error_at(this->location_, "impossible type switch case");
3941 else
3942 go_error_at(this->location_, "impossible type switch case (%s)",
3943 reason.c_str());
3946 Expression* ref = Expression::make_temporary_reference(descriptor_temp,
3947 loc);
3949 Expression* cond;
3950 // The language permits case nil, which is of course a constant
3951 // rather than a type. It will appear here as an invalid
3952 // forwarding type.
3953 if (type->is_nil_constant_as_type())
3954 cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3955 Expression::make_nil(loc),
3956 loc);
3957 else
3958 cond = Runtime::make_call((type->interface_type() == NULL
3959 ? Runtime::IFACETYPEEQ
3960 : Runtime::IFACET2IP),
3961 loc, 2,
3962 Expression::make_type_descriptor(type, loc),
3963 ref);
3965 Unnamed_label* dest;
3966 if (!this->is_fallthrough_)
3968 // if !COND { goto NEXT_CASE_LABEL }
3969 next_case_label = new Unnamed_label(Linemap::unknown_location());
3970 dest = next_case_label;
3971 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3973 else
3975 // if COND { goto STMTS_LABEL }
3976 go_assert(stmts_label != NULL);
3977 if (*stmts_label == NULL)
3978 *stmts_label = new Unnamed_label(Linemap::unknown_location());
3979 dest = *stmts_label;
3981 Block* then_block = new Block(b, loc);
3982 Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
3983 then_block->add_statement(s);
3984 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3985 b->add_statement(s);
3988 if (this->statements_ != NULL
3989 || (!this->is_fallthrough_
3990 && stmts_label != NULL
3991 && *stmts_label != NULL))
3993 go_assert(!this->is_fallthrough_);
3994 if (stmts_label != NULL && *stmts_label != NULL)
3996 go_assert(!this->is_default_);
3997 if (this->statements_ != NULL)
3998 (*stmts_label)->set_location(this->statements_->start_location());
3999 Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
4000 b->add_statement(s);
4001 *stmts_label = NULL;
4003 if (this->statements_ != NULL)
4004 b->add_statement(Statement::make_block_statement(this->statements_,
4005 loc));
4008 if (this->is_fallthrough_)
4009 go_assert(next_case_label == NULL);
4010 else
4012 Location gloc = (this->statements_ == NULL
4013 ? loc
4014 : this->statements_->end_location());
4015 b->add_statement(Statement::make_goto_unnamed_statement(break_label,
4016 gloc));
4017 if (next_case_label != NULL)
4019 Statement* s =
4020 Statement::make_unnamed_label_statement(next_case_label);
4021 b->add_statement(s);
4026 // Return true if this type clause may fall through to the statements
4027 // following the switch.
4029 bool
4030 Type_case_clauses::Type_case_clause::may_fall_through() const
4032 if (this->is_fallthrough_)
4034 // This case means that we automatically fall through to the
4035 // next case (it's used for T1 in case T1, T2:). It does not
4036 // mean that we fall through to the end of the type switch as a
4037 // whole. There is sure to be a next case and that next case
4038 // will determine whether we fall through to the statements
4039 // after the type switch.
4040 return false;
4042 if (this->statements_ == NULL)
4043 return true;
4044 return this->statements_->may_fall_through();
4047 // Dump the AST representation for a type case clause
4049 void
4050 Type_case_clauses::Type_case_clause::dump_clause(
4051 Ast_dump_context* ast_dump_context) const
4053 ast_dump_context->print_indent();
4054 if (this->is_default_)
4056 ast_dump_context->ostream() << "default:";
4058 else
4060 ast_dump_context->ostream() << "case ";
4061 ast_dump_context->dump_type(this->type_);
4062 ast_dump_context->ostream() << ":" ;
4064 ast_dump_context->dump_block(this->statements_);
4065 if (this->is_fallthrough_)
4067 ast_dump_context->print_indent();
4068 ast_dump_context->ostream() << " (fallthrough)" << std::endl;
4072 // Class Type_case_clauses.
4074 // Traversal.
4077 Type_case_clauses::traverse(Traverse* traverse)
4079 for (Type_clauses::iterator p = this->clauses_.begin();
4080 p != this->clauses_.end();
4081 ++p)
4083 if (p->traverse(traverse) == TRAVERSE_EXIT)
4084 return TRAVERSE_EXIT;
4086 return TRAVERSE_CONTINUE;
4089 // Check for duplicate types.
4091 void
4092 Type_case_clauses::check_duplicates() const
4094 typedef Unordered_set_hash(const Type*, Type_hash_identical,
4095 Type_identical) Types_seen;
4096 Types_seen types_seen;
4097 for (Type_clauses::const_iterator p = this->clauses_.begin();
4098 p != this->clauses_.end();
4099 ++p)
4101 Type* t = p->type();
4102 if (t == NULL)
4103 continue;
4104 if (t->is_nil_constant_as_type())
4105 t = Type::make_nil_type();
4106 std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
4107 if (!ins.second)
4108 go_error_at(p->location(), "duplicate type in switch");
4112 // Lower the clauses in a type switch. Add statements to the block B.
4113 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4114 // BREAK_LABEL is the label at the end of the type switch.
4116 void
4117 Type_case_clauses::lower(Type* switch_val_type, Block* b,
4118 Temporary_statement* descriptor_temp,
4119 Unnamed_label* break_label) const
4121 const Type_case_clause* default_case = NULL;
4123 Unnamed_label* stmts_label = NULL;
4124 for (Type_clauses::const_iterator p = this->clauses_.begin();
4125 p != this->clauses_.end();
4126 ++p)
4128 if (!p->is_default())
4129 p->lower(switch_val_type, b, descriptor_temp, break_label,
4130 &stmts_label);
4131 else
4133 // We are generating a series of tests, which means that we
4134 // need to move the default case to the end.
4135 default_case = &*p;
4138 go_assert(stmts_label == NULL);
4140 if (default_case != NULL)
4141 default_case->lower(switch_val_type, b, descriptor_temp, break_label,
4142 NULL);
4145 // Return true if these clauses may fall through to the statements
4146 // following the switch statement.
4148 bool
4149 Type_case_clauses::may_fall_through() const
4151 bool found_default = false;
4152 for (Type_clauses::const_iterator p = this->clauses_.begin();
4153 p != this->clauses_.end();
4154 ++p)
4156 if (p->may_fall_through())
4157 return true;
4158 if (p->is_default())
4159 found_default = true;
4161 return !found_default;
4164 // Dump the AST representation for case clauses (from a switch statement)
4166 void
4167 Type_case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
4169 for (Type_clauses::const_iterator p = this->clauses_.begin();
4170 p != this->clauses_.end();
4171 ++p)
4172 p->dump_clause(ast_dump_context);
4175 // Class Type_switch_statement.
4177 // Traversal.
4180 Type_switch_statement::do_traverse(Traverse* traverse)
4182 if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
4183 return TRAVERSE_EXIT;
4184 if (this->clauses_ != NULL)
4185 return this->clauses_->traverse(traverse);
4186 return TRAVERSE_CONTINUE;
4189 // Lower a type switch statement to a series of if statements. The gc
4190 // compiler is able to generate a table in some cases. However, that
4191 // does not work for us because we may have type descriptors in
4192 // different shared libraries, so we can't compare them with simple
4193 // equality testing.
4195 Statement*
4196 Type_switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
4197 Statement_inserter*)
4199 const Location loc = this->location();
4201 if (this->clauses_ != NULL)
4202 this->clauses_->check_duplicates();
4204 Block* b = new Block(enclosing, loc);
4206 Type* val_type = this->expr_->type();
4207 if (val_type->interface_type() == NULL)
4209 if (!val_type->is_error())
4210 this->report_error(_("cannot type switch on non-interface value"));
4211 return Statement::make_error_statement(loc);
4214 // var descriptor_temp DESCRIPTOR_TYPE
4215 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
4216 Temporary_statement* descriptor_temp =
4217 Statement::make_temporary(descriptor_type, NULL, loc);
4218 b->add_statement(descriptor_temp);
4220 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4221 // inlined.
4222 bool is_empty = val_type->interface_type()->is_empty();
4223 Expression* call = Runtime::make_call((is_empty
4224 ? Runtime::EFACETYPE
4225 : Runtime::IFACETYPE),
4226 loc, 1, this->expr_);
4227 Temporary_reference_expression* lhs =
4228 Expression::make_temporary_reference(descriptor_temp, loc);
4229 lhs->set_is_lvalue();
4230 Statement* s = Statement::make_assignment(lhs, call, loc);
4231 b->add_statement(s);
4233 if (this->clauses_ != NULL)
4234 this->clauses_->lower(val_type, b, descriptor_temp, this->break_label());
4236 s = Statement::make_unnamed_label_statement(this->break_label_);
4237 b->add_statement(s);
4239 return Statement::make_block_statement(b, loc);
4242 // Return whether this switch may fall through.
4244 bool
4245 Type_switch_statement::do_may_fall_through() const
4247 if (this->clauses_ == NULL)
4248 return true;
4250 // If we have a break label, then some case needed it. That implies
4251 // that the switch statement as a whole can fall through.
4252 if (this->break_label_ != NULL)
4253 return true;
4255 return this->clauses_->may_fall_through();
4258 // Return the break label for this type switch statement, creating it
4259 // if necessary.
4261 Unnamed_label*
4262 Type_switch_statement::break_label()
4264 if (this->break_label_ == NULL)
4265 this->break_label_ = new Unnamed_label(this->location());
4266 return this->break_label_;
4269 // Dump the AST representation for a type switch statement
4271 void
4272 Type_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
4273 const
4275 ast_dump_context->print_indent();
4276 ast_dump_context->ostream() << "switch ";
4277 if (!this->name_.empty())
4278 ast_dump_context->ostream() << this->name_ << " = ";
4279 ast_dump_context->dump_expression(this->expr_);
4280 ast_dump_context->ostream() << " .(type)";
4281 if (ast_dump_context->dump_subblocks())
4283 ast_dump_context->ostream() << " {" << dsuffix(location()) << std::endl;
4284 this->clauses_->dump_clauses(ast_dump_context);
4285 ast_dump_context->ostream() << "}";
4287 ast_dump_context->ostream() << std::endl;
4290 // Make a type switch statement.
4292 Type_switch_statement*
4293 Statement::make_type_switch_statement(const std::string& name, Expression* expr,
4294 Location location)
4296 return new Type_switch_statement(name, expr, location);
4299 // Class Send_statement.
4301 // Traversal.
4304 Send_statement::do_traverse(Traverse* traverse)
4306 if (this->traverse_expression(traverse, &this->channel_) == TRAVERSE_EXIT)
4307 return TRAVERSE_EXIT;
4308 return this->traverse_expression(traverse, &this->val_);
4311 // Determine types.
4313 void
4314 Send_statement::do_determine_types()
4316 this->channel_->determine_type_no_context();
4317 Type* type = this->channel_->type();
4318 Type_context context;
4319 if (type->channel_type() != NULL)
4320 context.type = type->channel_type()->element_type();
4321 this->val_->determine_type(&context);
4324 // Check types.
4326 void
4327 Send_statement::do_check_types(Gogo*)
4329 Type* type = this->channel_->type();
4330 if (type->is_error())
4332 this->set_is_error();
4333 return;
4335 Channel_type* channel_type = type->channel_type();
4336 if (channel_type == NULL)
4338 go_error_at(this->location(), "left operand of %<<-%> must be channel");
4339 this->set_is_error();
4340 return;
4342 Type* element_type = channel_type->element_type();
4343 if (!Type::are_assignable(element_type, this->val_->type(), NULL))
4345 this->report_error(_("incompatible types in send"));
4346 return;
4348 if (!channel_type->may_send())
4350 this->report_error(_("invalid send on receive-only channel"));
4351 return;
4355 // Flatten a send statement. We may need a temporary for interface
4356 // conversion.
4358 Statement*
4359 Send_statement::do_flatten(Gogo*, Named_object*, Block*,
4360 Statement_inserter* inserter)
4362 if (this->channel_->is_error_expression()
4363 || this->channel_->type()->is_error_type())
4365 go_assert(saw_errors());
4366 return Statement::make_error_statement(this->location());
4369 Type* element_type = this->channel_->type()->channel_type()->element_type();
4370 if (!Type::are_identical(element_type, this->val_->type(), false, NULL)
4371 && this->val_->type()->interface_type() != NULL
4372 && !this->val_->is_variable())
4374 Temporary_statement* temp =
4375 Statement::make_temporary(NULL, this->val_, this->location());
4376 inserter->insert(temp);
4377 this->val_ = Expression::make_temporary_reference(temp,
4378 this->location());
4380 return this;
4383 // Convert a send statement to the backend representation.
4385 Bstatement*
4386 Send_statement::do_get_backend(Translate_context* context)
4388 Location loc = this->location();
4390 Channel_type* channel_type = this->channel_->type()->channel_type();
4391 Type* element_type = channel_type->element_type();
4392 Expression* val = Expression::convert_for_assignment(context->gogo(),
4393 element_type,
4394 this->val_, loc);
4396 bool can_take_address;
4397 switch (element_type->base()->classification())
4399 case Type::TYPE_BOOLEAN:
4400 case Type::TYPE_INTEGER:
4401 case Type::TYPE_FUNCTION:
4402 case Type::TYPE_POINTER:
4403 case Type::TYPE_MAP:
4404 case Type::TYPE_CHANNEL:
4405 case Type::TYPE_FLOAT:
4406 case Type::TYPE_COMPLEX:
4407 case Type::TYPE_STRING:
4408 case Type::TYPE_INTERFACE:
4409 can_take_address = false;
4410 break;
4412 case Type::TYPE_STRUCT:
4413 can_take_address = true;
4414 break;
4416 case Type::TYPE_ARRAY:
4417 can_take_address = !element_type->is_slice_type();
4418 break;
4420 default:
4421 case Type::TYPE_ERROR:
4422 case Type::TYPE_VOID:
4423 case Type::TYPE_SINK:
4424 case Type::TYPE_NIL:
4425 case Type::TYPE_NAMED:
4426 case Type::TYPE_FORWARD:
4427 go_assert(saw_errors());
4428 return context->backend()->error_statement();
4431 // Only try to take the address of a variable. We have already
4432 // moved variables to the heap, so this should not cause that to
4433 // happen unnecessarily.
4434 if (can_take_address
4435 && val->var_expression() == NULL
4436 && val->temporary_reference_expression() == NULL)
4437 can_take_address = false;
4439 Bstatement* btemp = NULL;
4440 if (can_take_address)
4442 // The function doesn't change the value, so just take its
4443 // address directly.
4444 val = Expression::make_unary(OPERATOR_AND, val, loc);
4446 else
4448 // The value is not in a variable, or is small enough that it
4449 // might be in a register, and taking the address would push it
4450 // on the stack. Copy it into a temporary variable to take the
4451 // address.
4452 Temporary_statement* temp = Statement::make_temporary(element_type,
4453 val, loc);
4454 Expression* ref = Expression::make_temporary_reference(temp, loc);
4455 val = Expression::make_unary(OPERATOR_AND, ref, loc);
4456 btemp = temp->get_backend(context);
4459 Expression* call = Runtime::make_call(Runtime::CHANSEND, loc, 2,
4460 this->channel_, val);
4462 context->gogo()->lower_expression(context->function(), NULL, &call);
4463 Bexpression* bcall = call->get_backend(context);
4464 Bfunction* bfunction = context->function()->func_value()->get_decl();
4465 Bstatement* s = context->backend()->expression_statement(bfunction, bcall);
4467 if (btemp == NULL)
4468 return s;
4469 else
4470 return context->backend()->compound_statement(btemp, s);
4473 // Dump the AST representation for a send statement
4475 void
4476 Send_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
4478 ast_dump_context->print_indent();
4479 ast_dump_context->dump_expression(this->channel_);
4480 ast_dump_context->ostream() << " <- ";
4481 ast_dump_context->dump_expression(this->val_);
4482 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
4485 // Make a send statement.
4487 Send_statement*
4488 Statement::make_send_statement(Expression* channel, Expression* val,
4489 Location location)
4491 return new Send_statement(channel, val, location);
4494 // Class Select_clauses::Select_clause.
4496 // Traversal.
4499 Select_clauses::Select_clause::traverse(Traverse* traverse)
4501 if (!this->is_lowered_
4502 && (traverse->traverse_mask()
4503 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
4505 if (this->channel_ != NULL)
4507 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
4508 return TRAVERSE_EXIT;
4510 if (this->val_ != NULL)
4512 if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
4513 return TRAVERSE_EXIT;
4515 if (this->closed_ != NULL)
4517 if (Expression::traverse(&this->closed_, traverse) == TRAVERSE_EXIT)
4518 return TRAVERSE_EXIT;
4521 if (this->statements_ != NULL)
4523 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
4524 return TRAVERSE_EXIT;
4526 return TRAVERSE_CONTINUE;
4529 // Lowering. We call a function to register this clause, and arrange
4530 // to set any variables in any receive clause.
4532 void
4533 Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
4534 Block* b, Temporary_statement* sel)
4536 Location loc = this->location_;
4538 Expression* selref = Expression::make_temporary_reference(sel, loc);
4539 selref = Expression::make_unary(OPERATOR_AND, selref, loc);
4541 if (this->is_default_)
4543 go_assert(this->channel_ == NULL && this->val_ == NULL);
4544 this->lower_default(b, selref);
4545 this->is_lowered_ = true;
4546 return;
4549 // Evaluate the channel before the select statement.
4550 Temporary_statement* channel_temp = Statement::make_temporary(NULL,
4551 this->channel_,
4552 loc);
4553 b->add_statement(channel_temp);
4554 Expression* chanref = Expression::make_temporary_reference(channel_temp,
4555 loc);
4557 if (this->is_send_)
4558 this->lower_send(b, selref, chanref);
4559 else
4560 this->lower_recv(gogo, function, b, selref, chanref);
4562 // Now all references should be handled through the statements, not
4563 // through here.
4564 this->is_lowered_ = true;
4565 this->val_ = NULL;
4568 // Lower a default clause in a select statement.
4570 void
4571 Select_clauses::Select_clause::lower_default(Block* b, Expression* selref)
4573 Location loc = this->location_;
4574 Expression* call = Runtime::make_call(Runtime::SELECTDEFAULT, loc, 1,
4575 selref);
4576 b->add_statement(Statement::make_statement(call, true));
4579 // Lower a send clause in a select statement.
4581 void
4582 Select_clauses::Select_clause::lower_send(Block* b, Expression* selref,
4583 Expression* chanref)
4585 Location loc = this->location_;
4587 Channel_type* ct = this->channel_->type()->channel_type();
4588 if (ct == NULL)
4589 return;
4591 Type* valtype = ct->element_type();
4593 // Note that copying the value to a temporary here means that we
4594 // evaluate the send values in the required order.
4595 Temporary_statement* val = Statement::make_temporary(valtype, this->val_,
4596 loc);
4597 b->add_statement(val);
4599 Expression* valref = Expression::make_temporary_reference(val, loc);
4600 Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
4602 Expression* call = Runtime::make_call(Runtime::SELECTSEND, loc, 3, selref,
4603 chanref, valaddr);
4604 b->add_statement(Statement::make_statement(call, true));
4607 // Lower a receive clause in a select statement.
4609 void
4610 Select_clauses::Select_clause::lower_recv(Gogo* gogo, Named_object* function,
4611 Block* b, Expression* selref,
4612 Expression* chanref)
4614 Location loc = this->location_;
4616 Channel_type* ct = this->channel_->type()->channel_type();
4617 if (ct == NULL)
4618 return;
4620 Type* valtype = ct->element_type();
4621 Temporary_statement* val = Statement::make_temporary(valtype, NULL, loc);
4622 b->add_statement(val);
4624 Expression* valref = Expression::make_temporary_reference(val, loc);
4625 Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
4627 Temporary_statement* closed_temp = NULL;
4629 Expression* caddr;
4630 if (this->closed_ == NULL && this->closedvar_ == NULL)
4631 caddr = Expression::make_nil(loc);
4632 else
4634 closed_temp = Statement::make_temporary(Type::lookup_bool_type(), NULL,
4635 loc);
4636 b->add_statement(closed_temp);
4637 Expression* cref = Expression::make_temporary_reference(closed_temp,
4638 loc);
4639 caddr = Expression::make_unary(OPERATOR_AND, cref, loc);
4642 Expression* call = Runtime::make_call(Runtime::SELECTRECV, loc, 4, selref,
4643 chanref, valaddr, caddr);
4645 b->add_statement(Statement::make_statement(call, true));
4647 // If the block of statements is executed, arrange for the received
4648 // value to move from VAL to the place where the statements expect
4649 // it.
4651 Block* init = NULL;
4653 if (this->var_ != NULL)
4655 go_assert(this->val_ == NULL);
4656 valref = Expression::make_temporary_reference(val, loc);
4657 this->var_->var_value()->set_init(valref);
4658 this->var_->var_value()->clear_type_from_chan_element();
4660 else if (this->val_ != NULL && !this->val_->is_sink_expression())
4662 init = new Block(b, loc);
4663 valref = Expression::make_temporary_reference(val, loc);
4664 init->add_statement(Statement::make_assignment(this->val_, valref, loc));
4667 if (this->closedvar_ != NULL)
4669 go_assert(this->closed_ == NULL);
4670 Expression* cref = Expression::make_temporary_reference(closed_temp,
4671 loc);
4672 this->closedvar_->var_value()->set_init(cref);
4674 else if (this->closed_ != NULL && !this->closed_->is_sink_expression())
4676 if (init == NULL)
4677 init = new Block(b, loc);
4678 Expression* cref = Expression::make_temporary_reference(closed_temp,
4679 loc);
4680 init->add_statement(Statement::make_assignment(this->closed_, cref,
4681 loc));
4684 if (init != NULL)
4686 gogo->lower_block(function, init);
4688 if (this->statements_ != NULL)
4689 init->add_statement(Statement::make_block_statement(this->statements_,
4690 loc));
4691 this->statements_ = init;
4695 // Determine types.
4697 void
4698 Select_clauses::Select_clause::determine_types()
4700 go_assert(this->is_lowered_);
4701 if (this->statements_ != NULL)
4702 this->statements_->determine_types();
4705 // Check types.
4707 void
4708 Select_clauses::Select_clause::check_types()
4710 if (this->is_default_)
4711 return;
4713 Channel_type* ct = this->channel_->type()->channel_type();
4714 if (ct == NULL)
4716 go_error_at(this->channel_->location(), "expected channel");
4717 return;
4720 if (this->is_send_ && !ct->may_send())
4721 go_error_at(this->location(), "invalid send on receive-only channel");
4722 else if (!this->is_send_ && !ct->may_receive())
4723 go_error_at(this->location(), "invalid receive on send-only channel");
4726 // Whether this clause may fall through to the statement which follows
4727 // the overall select statement.
4729 bool
4730 Select_clauses::Select_clause::may_fall_through() const
4732 if (this->statements_ == NULL)
4733 return true;
4734 return this->statements_->may_fall_through();
4737 // Return the backend representation for the statements to execute.
4739 Bstatement*
4740 Select_clauses::Select_clause::get_statements_backend(
4741 Translate_context* context)
4743 if (this->statements_ == NULL)
4744 return NULL;
4745 Bblock* bblock = this->statements_->get_backend(context);
4746 return context->backend()->block_statement(bblock);
4749 // Dump the AST representation for a select case clause
4751 void
4752 Select_clauses::Select_clause::dump_clause(
4753 Ast_dump_context* ast_dump_context) const
4755 ast_dump_context->print_indent();
4756 if (this->is_default_)
4758 ast_dump_context->ostream() << "default:";
4760 else
4762 ast_dump_context->ostream() << "case " ;
4763 if (this->is_send_)
4765 ast_dump_context->dump_expression(this->channel_);
4766 ast_dump_context->ostream() << " <- " ;
4767 if (this->val_ != NULL)
4768 ast_dump_context->dump_expression(this->val_);
4770 else
4772 if (this->val_ != NULL)
4773 ast_dump_context->dump_expression(this->val_);
4774 if (this->closed_ != NULL)
4776 // FIXME: can val_ == NULL and closed_ ! = NULL?
4777 ast_dump_context->ostream() << " , " ;
4778 ast_dump_context->dump_expression(this->closed_);
4780 if (this->closedvar_ != NULL || this->var_ != NULL)
4781 ast_dump_context->ostream() << " := " ;
4783 ast_dump_context->ostream() << " <- " ;
4784 ast_dump_context->dump_expression(this->channel_);
4786 ast_dump_context->ostream() << ":" ;
4788 ast_dump_context->dump_block(this->statements_);
4791 // Class Select_clauses.
4793 // Traversal.
4796 Select_clauses::traverse(Traverse* traverse)
4798 for (Clauses::iterator p = this->clauses_.begin();
4799 p != this->clauses_.end();
4800 ++p)
4802 if (p->traverse(traverse) == TRAVERSE_EXIT)
4803 return TRAVERSE_EXIT;
4805 return TRAVERSE_CONTINUE;
4808 // Lowering. Here we pull out the channel and the send values, to
4809 // enforce the order of evaluation. We also add explicit send and
4810 // receive statements to the clauses.
4812 void
4813 Select_clauses::lower(Gogo* gogo, Named_object* function, Block* b,
4814 Temporary_statement* sel)
4816 for (Clauses::iterator p = this->clauses_.begin();
4817 p != this->clauses_.end();
4818 ++p)
4819 p->lower(gogo, function, b, sel);
4822 // Determine types.
4824 void
4825 Select_clauses::determine_types()
4827 for (Clauses::iterator p = this->clauses_.begin();
4828 p != this->clauses_.end();
4829 ++p)
4830 p->determine_types();
4833 // Check types.
4835 void
4836 Select_clauses::check_types()
4838 for (Clauses::iterator p = this->clauses_.begin();
4839 p != this->clauses_.end();
4840 ++p)
4841 p->check_types();
4844 // Return whether these select clauses fall through to the statement
4845 // following the overall select statement.
4847 bool
4848 Select_clauses::may_fall_through() const
4850 for (Clauses::const_iterator p = this->clauses_.begin();
4851 p != this->clauses_.end();
4852 ++p)
4853 if (p->may_fall_through())
4854 return true;
4855 return false;
4858 // Convert to the backend representation. We have already accumulated
4859 // all the select information. Now we call selectgo, which will
4860 // return the index of the clause to execute.
4862 Bstatement*
4863 Select_clauses::get_backend(Translate_context* context,
4864 Temporary_statement* sel,
4865 Unnamed_label *break_label,
4866 Location location)
4868 size_t count = this->clauses_.size();
4869 std::vector<std::vector<Bexpression*> > cases(count);
4870 std::vector<Bstatement*> clauses(count);
4872 Type* int_type = Type::lookup_integer_type("int");
4874 int i = 0;
4875 for (Clauses::iterator p = this->clauses_.begin();
4876 p != this->clauses_.end();
4877 ++p, ++i)
4879 Expression* index_expr = Expression::make_integer_ul(i, int_type,
4880 location);
4881 cases[i].push_back(index_expr->get_backend(context));
4883 Bstatement* s = p->get_statements_backend(context);
4884 Location gloc = (p->statements() == NULL
4885 ? p->location()
4886 : p->statements()->end_location());
4887 Bstatement* g = break_label->get_goto(context, gloc);
4889 if (s == NULL)
4890 clauses[i] = g;
4891 else
4892 clauses[i] = context->backend()->compound_statement(s, g);
4895 Expression* selref = Expression::make_temporary_reference(sel, location);
4896 selref = Expression::make_unary(OPERATOR_AND, selref, location);
4897 Expression* call = Runtime::make_call(Runtime::SELECTGO, location, 1,
4898 selref);
4899 context->gogo()->lower_expression(context->function(), NULL, &call);
4900 Bexpression* bcall = call->get_backend(context);
4902 if (count == 0)
4904 Bfunction* bfunction = context->function()->func_value()->get_decl();
4905 return context->backend()->expression_statement(bfunction, bcall);
4908 std::vector<Bstatement*> statements;
4909 statements.reserve(2);
4911 Bfunction* bfunction = context->function()->func_value()->get_decl();
4912 Bstatement* switch_stmt = context->backend()->switch_statement(bfunction,
4913 bcall,
4914 cases,
4915 clauses,
4916 location);
4917 statements.push_back(switch_stmt);
4919 Bstatement* ldef = break_label->get_definition(context);
4920 statements.push_back(ldef);
4922 return context->backend()->statement_list(statements);
4924 // Dump the AST representation for select clauses.
4926 void
4927 Select_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
4929 for (Clauses::const_iterator p = this->clauses_.begin();
4930 p != this->clauses_.end();
4931 ++p)
4932 p->dump_clause(ast_dump_context);
4935 // Class Select_statement.
4937 // Return the break label for this switch statement, creating it if
4938 // necessary.
4940 Unnamed_label*
4941 Select_statement::break_label()
4943 if (this->break_label_ == NULL)
4944 this->break_label_ = new Unnamed_label(this->location());
4945 return this->break_label_;
4948 // Lower a select statement. This will still return a select
4949 // statement, but it will be modified to implement the order of
4950 // evaluation rules, and to include the send and receive statements as
4951 // explicit statements in the clauses.
4953 Statement*
4954 Select_statement::do_lower(Gogo* gogo, Named_object* function,
4955 Block* enclosing, Statement_inserter*)
4957 if (this->is_lowered_)
4958 return this;
4960 Location loc = this->location();
4962 Block* b = new Block(enclosing, loc);
4964 go_assert(this->sel_ == NULL);
4966 int ncases = this->clauses_->size();
4967 Type* selstruct_type = Channel_type::select_type(ncases);
4968 this->sel_ = Statement::make_temporary(selstruct_type, NULL, loc);
4969 b->add_statement(this->sel_);
4971 int64_t selstruct_size;
4972 if (!selstruct_type->backend_type_size(gogo, &selstruct_size))
4974 go_assert(saw_errors());
4975 return Statement::make_error_statement(loc);
4978 Expression* ref = Expression::make_temporary_reference(this->sel_, loc);
4979 ref = Expression::make_unary(OPERATOR_AND, ref, loc);
4980 Expression* selstruct_size_expr =
4981 Expression::make_integer_int64(selstruct_size, NULL, loc);
4982 Expression* size_expr = Expression::make_integer_ul(ncases, NULL, loc);
4983 Expression* call = Runtime::make_call(Runtime::NEWSELECT, loc, 3,
4984 ref, selstruct_size_expr, size_expr);
4985 b->add_statement(Statement::make_statement(call, true));
4987 this->clauses_->lower(gogo, function, b, this->sel_);
4988 this->is_lowered_ = true;
4989 b->add_statement(this);
4991 return Statement::make_block_statement(b, loc);
4994 // Whether the select statement itself may fall through to the following
4995 // statement.
4997 bool
4998 Select_statement::do_may_fall_through() const
5000 // A select statement is terminating if no break statement
5001 // refers to it and all of its clauses are terminating.
5002 if (this->break_label_ != NULL)
5003 return true;
5004 return this->clauses_->may_fall_through();
5007 // Return the backend representation for a select statement.
5009 Bstatement*
5010 Select_statement::do_get_backend(Translate_context* context)
5012 return this->clauses_->get_backend(context, this->sel_, this->break_label(),
5013 this->location());
5016 // Dump the AST representation for a select statement.
5018 void
5019 Select_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
5021 ast_dump_context->print_indent();
5022 ast_dump_context->ostream() << "select";
5023 if (ast_dump_context->dump_subblocks())
5025 ast_dump_context->ostream() << " {" << dsuffix(location()) << std::endl;
5026 this->clauses_->dump_clauses(ast_dump_context);
5027 ast_dump_context->ostream() << "}";
5029 ast_dump_context->ostream() << std::endl;
5032 // Make a select statement.
5034 Select_statement*
5035 Statement::make_select_statement(Location location)
5037 return new Select_statement(location);
5040 // Class For_statement.
5042 // Traversal.
5045 For_statement::do_traverse(Traverse* traverse)
5047 if (this->init_ != NULL)
5049 if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
5050 return TRAVERSE_EXIT;
5052 if (this->cond_ != NULL)
5054 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
5055 return TRAVERSE_EXIT;
5057 if (this->post_ != NULL)
5059 if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
5060 return TRAVERSE_EXIT;
5062 return this->statements_->traverse(traverse);
5065 // Lower a For_statement into if statements and gotos. Getting rid of
5066 // complex statements make it easier to handle garbage collection.
5068 Statement*
5069 For_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
5070 Statement_inserter*)
5072 Statement* s;
5073 Location loc = this->location();
5075 Block* b = new Block(enclosing, this->location());
5076 if (this->init_ != NULL)
5078 s = Statement::make_block_statement(this->init_,
5079 this->init_->start_location());
5080 b->add_statement(s);
5083 Unnamed_label* entry = NULL;
5084 if (this->cond_ != NULL)
5086 entry = new Unnamed_label(this->location());
5087 b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
5090 Unnamed_label* top = new Unnamed_label(this->location());
5091 top->set_derived_from(this);
5092 b->add_statement(Statement::make_unnamed_label_statement(top));
5094 s = Statement::make_block_statement(this->statements_,
5095 this->statements_->start_location());
5096 b->add_statement(s);
5098 Location end_loc = this->statements_->end_location();
5100 Unnamed_label* cont = this->continue_label_;
5101 if (cont != NULL)
5102 b->add_statement(Statement::make_unnamed_label_statement(cont));
5104 if (this->post_ != NULL)
5106 s = Statement::make_block_statement(this->post_,
5107 this->post_->start_location());
5108 b->add_statement(s);
5109 end_loc = this->post_->end_location();
5112 if (this->cond_ == NULL)
5113 b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
5114 else
5116 b->add_statement(Statement::make_unnamed_label_statement(entry));
5118 Location cond_loc = this->cond_->location();
5119 Block* then_block = new Block(b, cond_loc);
5120 s = Statement::make_goto_unnamed_statement(top, cond_loc);
5121 then_block->add_statement(s);
5123 s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
5124 b->add_statement(s);
5127 Unnamed_label* brk = this->break_label_;
5128 if (brk != NULL)
5129 b->add_statement(Statement::make_unnamed_label_statement(brk));
5131 b->set_end_location(end_loc);
5133 Statement* bs = Statement::make_block_statement(b, loc);
5134 bs->block_statement()->set_is_lowered_for_statement();
5135 return bs;
5138 // Return the break label, creating it if necessary.
5140 Unnamed_label*
5141 For_statement::break_label()
5143 if (this->break_label_ == NULL)
5144 this->break_label_ = new Unnamed_label(this->location());
5145 return this->break_label_;
5148 // Return the continue LABEL_EXPR.
5150 Unnamed_label*
5151 For_statement::continue_label()
5153 if (this->continue_label_ == NULL)
5154 this->continue_label_ = new Unnamed_label(this->location());
5155 return this->continue_label_;
5158 // Set the break and continue labels a for statement. This is used
5159 // when lowering a for range statement.
5161 void
5162 For_statement::set_break_continue_labels(Unnamed_label* break_label,
5163 Unnamed_label* continue_label)
5165 go_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
5166 this->break_label_ = break_label;
5167 this->continue_label_ = continue_label;
5170 // Whether the overall statement may fall through.
5172 bool
5173 For_statement::do_may_fall_through() const
5175 // A for loop is terminating if it has no condition and
5176 // no break statement.
5177 if(this->cond_ != NULL)
5178 return true;
5179 if(this->break_label_ != NULL)
5180 return true;
5181 return false;
5184 // Dump the AST representation for a for statement.
5186 void
5187 For_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
5189 if (this->init_ != NULL && ast_dump_context->dump_subblocks())
5191 ast_dump_context->print_indent();
5192 ast_dump_context->indent();
5193 ast_dump_context->ostream() << "// INIT " << std::endl;
5194 ast_dump_context->dump_block(this->init_);
5195 ast_dump_context->unindent();
5197 ast_dump_context->print_indent();
5198 ast_dump_context->ostream() << "for ";
5199 if (this->cond_ != NULL)
5200 ast_dump_context->dump_expression(this->cond_);
5202 if (ast_dump_context->dump_subblocks())
5204 ast_dump_context->ostream() << " {" << std::endl;
5205 ast_dump_context->dump_block(this->statements_);
5206 if (this->init_ != NULL)
5208 ast_dump_context->print_indent();
5209 ast_dump_context->ostream() << "// POST " << std::endl;
5210 ast_dump_context->dump_block(this->post_);
5212 ast_dump_context->unindent();
5214 ast_dump_context->print_indent();
5215 ast_dump_context->ostream() << "}";
5218 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
5221 // Make a for statement.
5223 For_statement*
5224 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
5225 Location location)
5227 return new For_statement(init, cond, post, location);
5230 // Class For_range_statement.
5232 // Traversal.
5235 For_range_statement::do_traverse(Traverse* traverse)
5237 if (this->index_var_ != NULL)
5239 if (this->traverse_expression(traverse, &this->index_var_)
5240 == TRAVERSE_EXIT)
5241 return TRAVERSE_EXIT;
5243 if (this->value_var_ != NULL)
5245 if (this->traverse_expression(traverse, &this->value_var_)
5246 == TRAVERSE_EXIT)
5247 return TRAVERSE_EXIT;
5249 if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
5250 return TRAVERSE_EXIT;
5251 return this->statements_->traverse(traverse);
5254 // Lower a for range statement. For simplicity we lower this into a
5255 // for statement, which will then be lowered in turn to goto
5256 // statements.
5258 Statement*
5259 For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing,
5260 Statement_inserter*)
5262 Type* range_type = this->range_->type();
5263 if (range_type->points_to() != NULL
5264 && range_type->points_to()->array_type() != NULL
5265 && !range_type->points_to()->is_slice_type())
5266 range_type = range_type->points_to();
5268 Type* index_type;
5269 Type* value_type = NULL;
5270 if (range_type->array_type() != NULL)
5272 index_type = Type::lookup_integer_type("int");
5273 value_type = range_type->array_type()->element_type();
5275 else if (range_type->is_string_type())
5277 index_type = Type::lookup_integer_type("int");
5278 value_type = gogo->lookup_global("rune")->type_value();
5280 else if (range_type->map_type() != NULL)
5282 index_type = range_type->map_type()->key_type();
5283 value_type = range_type->map_type()->val_type();
5285 else if (range_type->channel_type() != NULL)
5287 index_type = range_type->channel_type()->element_type();
5288 if (this->value_var_ != NULL)
5290 if (!this->value_var_->type()->is_error())
5291 this->report_error(_("too many variables for range clause "
5292 "with channel"));
5293 return Statement::make_error_statement(this->location());
5296 else
5298 this->report_error(_("range clause must have "
5299 "array, slice, string, map, or channel type"));
5300 return Statement::make_error_statement(this->location());
5303 Location loc = this->location();
5304 Block* temp_block = new Block(enclosing, loc);
5306 Named_object* range_object = NULL;
5307 Temporary_statement* range_temp = NULL;
5308 Var_expression* ve = this->range_->var_expression();
5309 if (ve != NULL)
5310 range_object = ve->named_object();
5311 else
5313 range_temp = Statement::make_temporary(NULL, this->range_, loc);
5314 temp_block->add_statement(range_temp);
5315 this->range_ = NULL;
5318 Temporary_statement* index_temp = Statement::make_temporary(index_type,
5319 NULL, loc);
5320 temp_block->add_statement(index_temp);
5322 Temporary_statement* value_temp = NULL;
5323 if (this->value_var_ != NULL)
5325 value_temp = Statement::make_temporary(value_type, NULL, loc);
5326 temp_block->add_statement(value_temp);
5329 Block* body = new Block(temp_block, loc);
5331 Block* init;
5332 Expression* cond;
5333 Block* iter_init;
5334 Block* post;
5336 // Arrange to do a loop appropriate for the type. We will produce
5337 // for INIT ; COND ; POST {
5338 // ITER_INIT
5339 // INDEX = INDEX_TEMP
5340 // VALUE = VALUE_TEMP // If there is a value
5341 // original statements
5342 // }
5344 if (range_type->is_slice_type())
5345 this->lower_range_slice(gogo, temp_block, body, range_object, range_temp,
5346 index_temp, value_temp, &init, &cond, &iter_init,
5347 &post);
5348 else if (range_type->array_type() != NULL)
5349 this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
5350 index_temp, value_temp, &init, &cond, &iter_init,
5351 &post);
5352 else if (range_type->is_string_type())
5353 this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
5354 index_temp, value_temp, &init, &cond, &iter_init,
5355 &post);
5356 else if (range_type->map_type() != NULL)
5357 this->lower_range_map(gogo, range_type->map_type(), temp_block, body,
5358 range_object, range_temp, index_temp, value_temp,
5359 &init, &cond, &iter_init, &post);
5360 else if (range_type->channel_type() != NULL)
5361 this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
5362 index_temp, value_temp, &init, &cond, &iter_init,
5363 &post);
5364 else
5365 go_unreachable();
5367 if (iter_init != NULL)
5368 body->add_statement(Statement::make_block_statement(iter_init, loc));
5370 if (this->index_var_ != NULL)
5372 Statement* assign;
5373 Expression* index_ref =
5374 Expression::make_temporary_reference(index_temp, loc);
5375 if (this->value_var_ == NULL)
5376 assign = Statement::make_assignment(this->index_var_, index_ref, loc);
5377 else
5379 Expression_list* lhs = new Expression_list();
5380 lhs->push_back(this->index_var_);
5381 lhs->push_back(this->value_var_);
5383 Expression_list* rhs = new Expression_list();
5384 rhs->push_back(index_ref);
5385 rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
5387 assign = Statement::make_tuple_assignment(lhs, rhs, loc);
5389 body->add_statement(assign);
5392 body->add_statement(Statement::make_block_statement(this->statements_, loc));
5394 body->set_end_location(this->statements_->end_location());
5396 For_statement* loop = Statement::make_for_statement(init, cond, post,
5397 this->location());
5398 loop->add_statements(body);
5399 loop->set_break_continue_labels(this->break_label_, this->continue_label_);
5401 temp_block->add_statement(loop);
5403 return Statement::make_block_statement(temp_block, loc);
5406 // Return a reference to the range, which may be in RANGE_OBJECT or in
5407 // RANGE_TEMP.
5409 Expression*
5410 For_range_statement::make_range_ref(Named_object* range_object,
5411 Temporary_statement* range_temp,
5412 Location loc)
5414 if (range_object != NULL)
5415 return Expression::make_var_reference(range_object, loc);
5416 else
5417 return Expression::make_temporary_reference(range_temp, loc);
5420 // Return a call to the predeclared function FUNCNAME passing a
5421 // reference to the temporary variable ARG.
5423 Call_expression*
5424 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
5425 Expression* arg,
5426 Location loc)
5428 Named_object* no = gogo->lookup_global(funcname);
5429 go_assert(no != NULL && no->is_function_declaration());
5430 Expression* func = Expression::make_func_reference(no, NULL, loc);
5431 Expression_list* params = new Expression_list();
5432 params->push_back(arg);
5433 return Expression::make_call(func, params, false, loc);
5436 // Lower a for range over an array.
5438 void
5439 For_range_statement::lower_range_array(Gogo* gogo,
5440 Block* enclosing,
5441 Block* body_block,
5442 Named_object* range_object,
5443 Temporary_statement* range_temp,
5444 Temporary_statement* index_temp,
5445 Temporary_statement* value_temp,
5446 Block** pinit,
5447 Expression** pcond,
5448 Block** piter_init,
5449 Block** ppost)
5451 Location loc = this->location();
5453 // The loop we generate:
5454 // len_temp := len(range)
5455 // range_temp := range
5456 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5457 // value_temp = range_temp[index_temp]
5458 // index = index_temp
5459 // value = value_temp
5460 // original body
5461 // }
5463 // Set *PINIT to
5464 // var len_temp int
5465 // len_temp = len(range)
5466 // index_temp = 0
5468 Block* init = new Block(enclosing, loc);
5470 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5471 range_temp = Statement::make_temporary(NULL, ref, loc);
5472 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
5473 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
5474 len_call, loc);
5475 init->add_statement(range_temp);
5476 init->add_statement(len_temp);
5478 Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
5480 Temporary_reference_expression* tref =
5481 Expression::make_temporary_reference(index_temp, loc);
5482 tref->set_is_lvalue();
5483 Statement* s = Statement::make_assignment(tref, zexpr, loc);
5484 init->add_statement(s);
5486 *pinit = init;
5488 // Set *PCOND to
5489 // index_temp < len_temp
5491 ref = Expression::make_temporary_reference(index_temp, loc);
5492 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
5493 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
5495 *pcond = lt;
5497 // Set *PITER_INIT to
5498 // value_temp = range[index_temp]
5500 Block* iter_init = NULL;
5501 if (value_temp != NULL)
5503 iter_init = new Block(body_block, loc);
5505 ref = Expression::make_temporary_reference(range_temp, loc);
5506 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
5507 Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
5509 tref = Expression::make_temporary_reference(value_temp, loc);
5510 tref->set_is_lvalue();
5511 s = Statement::make_assignment(tref, index, loc);
5513 iter_init->add_statement(s);
5515 *piter_init = iter_init;
5517 // Set *PPOST to
5518 // index_temp++
5520 Block* post = new Block(enclosing, loc);
5521 tref = Expression::make_temporary_reference(index_temp, loc);
5522 tref->set_is_lvalue();
5523 s = Statement::make_inc_statement(tref);
5524 post->add_statement(s);
5525 *ppost = post;
5528 // Lower a for range over a slice.
5530 void
5531 For_range_statement::lower_range_slice(Gogo* gogo,
5532 Block* enclosing,
5533 Block* body_block,
5534 Named_object* range_object,
5535 Temporary_statement* range_temp,
5536 Temporary_statement* index_temp,
5537 Temporary_statement* value_temp,
5538 Block** pinit,
5539 Expression** pcond,
5540 Block** piter_init,
5541 Block** ppost)
5543 Location loc = this->location();
5545 // The loop we generate:
5546 // for_temp := range
5547 // len_temp := len(for_temp)
5548 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5549 // value_temp = for_temp[index_temp]
5550 // index = index_temp
5551 // value = value_temp
5552 // original body
5553 // }
5555 // Using for_temp means that we don't need to check bounds when
5556 // fetching range_temp[index_temp].
5558 // Set *PINIT to
5559 // range_temp := range
5560 // var len_temp int
5561 // len_temp = len(range_temp)
5562 // index_temp = 0
5564 Block* init = new Block(enclosing, loc);
5566 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5567 Temporary_statement* for_temp = Statement::make_temporary(NULL, ref, loc);
5568 init->add_statement(for_temp);
5570 ref = Expression::make_temporary_reference(for_temp, loc);
5571 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
5572 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
5573 len_call, loc);
5574 init->add_statement(len_temp);
5576 Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
5578 Temporary_reference_expression* tref =
5579 Expression::make_temporary_reference(index_temp, loc);
5580 tref->set_is_lvalue();
5581 Statement* s = Statement::make_assignment(tref, zexpr, loc);
5582 init->add_statement(s);
5584 *pinit = init;
5586 // Set *PCOND to
5587 // index_temp < len_temp
5589 ref = Expression::make_temporary_reference(index_temp, loc);
5590 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
5591 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
5593 *pcond = lt;
5595 // Set *PITER_INIT to
5596 // value_temp = range[index_temp]
5598 Block* iter_init = NULL;
5599 if (value_temp != NULL)
5601 iter_init = new Block(body_block, loc);
5603 ref = Expression::make_temporary_reference(for_temp, loc);
5604 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
5605 Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
5607 tref = Expression::make_temporary_reference(value_temp, loc);
5608 tref->set_is_lvalue();
5609 s = Statement::make_assignment(tref, index, loc);
5611 iter_init->add_statement(s);
5613 *piter_init = iter_init;
5615 // Set *PPOST to
5616 // index_temp++
5618 Block* post = new Block(enclosing, loc);
5619 tref = Expression::make_temporary_reference(index_temp, loc);
5620 tref->set_is_lvalue();
5621 s = Statement::make_inc_statement(tref);
5622 post->add_statement(s);
5623 *ppost = post;
5626 // Lower a for range over a string.
5628 void
5629 For_range_statement::lower_range_string(Gogo* gogo,
5630 Block* enclosing,
5631 Block* body_block,
5632 Named_object* range_object,
5633 Temporary_statement* range_temp,
5634 Temporary_statement* index_temp,
5635 Temporary_statement* value_temp,
5636 Block** pinit,
5637 Expression** pcond,
5638 Block** piter_init,
5639 Block** ppost)
5641 Location loc = this->location();
5643 // The loop we generate:
5644 // len_temp := len(range)
5645 // var next_index_temp int
5646 // for index_temp = 0; index_temp < len_temp; index_temp = next_index_temp {
5647 // value_temp = rune(range[index_temp])
5648 // if value_temp < utf8.RuneSelf {
5649 // next_index_temp = index_temp + 1
5650 // } else {
5651 // value_temp, next_index_temp = decoderune(range, index_temp)
5652 // }
5653 // index = index_temp
5654 // value = value_temp
5655 // // original body
5656 // }
5658 // Set *PINIT to
5659 // len_temp := len(range)
5660 // var next_index_temp int
5661 // index_temp = 0
5662 // var value_temp rune // if value_temp not passed in
5664 Block* init = new Block(enclosing, loc);
5666 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5667 Call_expression* call = this->call_builtin(gogo, "len", ref, loc);
5668 Temporary_statement* len_temp =
5669 Statement::make_temporary(index_temp->type(), call, loc);
5670 init->add_statement(len_temp);
5672 Temporary_statement* next_index_temp =
5673 Statement::make_temporary(index_temp->type(), NULL, loc);
5674 init->add_statement(next_index_temp);
5676 Temporary_reference_expression* index_ref =
5677 Expression::make_temporary_reference(index_temp, loc);
5678 index_ref->set_is_lvalue();
5679 Expression* zexpr = Expression::make_integer_ul(0, index_temp->type(), loc);
5680 Statement* s = Statement::make_assignment(index_ref, zexpr, loc);
5681 init->add_statement(s);
5683 Type* rune_type;
5684 if (value_temp != NULL)
5685 rune_type = value_temp->type();
5686 else
5688 rune_type = gogo->lookup_global("rune")->type_value();
5689 value_temp = Statement::make_temporary(rune_type, NULL, loc);
5690 init->add_statement(value_temp);
5693 *pinit = init;
5695 // Set *PCOND to
5696 // index_temp < len_temp
5698 index_ref = Expression::make_temporary_reference(index_temp, loc);
5699 Expression* len_ref =
5700 Expression::make_temporary_reference(len_temp, loc);
5701 *pcond = Expression::make_binary(OPERATOR_LT, index_ref, len_ref, loc);
5703 // Set *PITER_INIT to
5704 // value_temp = rune(range[index_temp])
5705 // if value_temp < utf8.RuneSelf {
5706 // next_index_temp = index_temp + 1
5707 // } else {
5708 // value_temp, next_index_temp = decoderune(range, index_temp)
5709 // }
5711 Block* iter_init = new Block(body_block, loc);
5713 ref = this->make_range_ref(range_object, range_temp, loc);
5714 index_ref = Expression::make_temporary_reference(index_temp, loc);
5715 ref = Expression::make_string_index(ref, index_ref, NULL, loc);
5716 ref = Expression::make_cast(rune_type, ref, loc);
5717 Temporary_reference_expression* value_ref =
5718 Expression::make_temporary_reference(value_temp, loc);
5719 value_ref->set_is_lvalue();
5720 s = Statement::make_assignment(value_ref, ref, loc);
5721 iter_init->add_statement(s);
5723 value_ref = Expression::make_temporary_reference(value_temp, loc);
5724 Expression* rune_self = Expression::make_integer_ul(0x80, rune_type, loc);
5725 Expression* cond = Expression::make_binary(OPERATOR_LT, value_ref, rune_self,
5726 loc);
5728 Block* then_block = new Block(iter_init, loc);
5730 Temporary_reference_expression* lhs =
5731 Expression::make_temporary_reference(next_index_temp, loc);
5732 lhs->set_is_lvalue();
5733 index_ref = Expression::make_temporary_reference(index_temp, loc);
5734 Expression* one = Expression::make_integer_ul(1, index_temp->type(), loc);
5735 Expression* sum = Expression::make_binary(OPERATOR_PLUS, index_ref, one,
5736 loc);
5737 s = Statement::make_assignment(lhs, sum, loc);
5738 then_block->add_statement(s);
5740 Block* else_block = new Block(iter_init, loc);
5742 ref = this->make_range_ref(range_object, range_temp, loc);
5743 index_ref = Expression::make_temporary_reference(index_temp, loc);
5744 call = Runtime::make_call(Runtime::DECODERUNE, loc, 2, ref, index_ref);
5746 value_ref = Expression::make_temporary_reference(value_temp, loc);
5747 value_ref->set_is_lvalue();
5748 Expression* res = Expression::make_call_result(call, 0);
5749 s = Statement::make_assignment(value_ref, res, loc);
5750 else_block->add_statement(s);
5752 lhs = Expression::make_temporary_reference(next_index_temp, loc);
5753 lhs->set_is_lvalue();
5754 res = Expression::make_call_result(call, 1);
5755 s = Statement::make_assignment(lhs, res, loc);
5756 else_block->add_statement(s);
5758 s = Statement::make_if_statement(cond, then_block, else_block, loc);
5759 iter_init->add_statement(s);
5761 *piter_init = iter_init;
5763 // Set *PPOST to
5764 // index_temp = next_index_temp
5766 Block* post = new Block(enclosing, loc);
5768 index_ref = Expression::make_temporary_reference(index_temp, loc);
5769 index_ref->set_is_lvalue();
5770 ref = Expression::make_temporary_reference(next_index_temp, loc);
5771 s = Statement::make_assignment(index_ref, ref, loc);
5773 post->add_statement(s);
5774 *ppost = post;
5777 // Lower a for range over a map.
5779 void
5780 For_range_statement::lower_range_map(Gogo* gogo,
5781 Map_type* map_type,
5782 Block* enclosing,
5783 Block* body_block,
5784 Named_object* range_object,
5785 Temporary_statement* range_temp,
5786 Temporary_statement* index_temp,
5787 Temporary_statement* value_temp,
5788 Block** pinit,
5789 Expression** pcond,
5790 Block** piter_init,
5791 Block** ppost)
5793 Location loc = this->location();
5795 // The runtime uses a struct to handle ranges over a map. The
5796 // struct is built by Map_type::hiter_type for a specific map type.
5798 // The loop we generate:
5799 // var hiter map_iteration_struct
5800 // for mapiterinit(type, range, &hiter); hiter.key != nil; mapiternext(&hiter) {
5801 // index_temp = *hiter.key
5802 // value_temp = *hiter.val
5803 // index = index_temp
5804 // value = value_temp
5805 // original body
5806 // }
5808 // Set *PINIT to
5809 // var hiter map_iteration_struct
5810 // runtime.mapiterinit(type, range, &hiter)
5812 Block* init = new Block(enclosing, loc);
5814 Type* map_iteration_type = map_type->hiter_type(gogo);
5815 Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
5816 NULL, loc);
5817 init->add_statement(hiter);
5819 Expression* p1 = Expression::make_type_descriptor(map_type, loc);
5820 Expression* p2 = this->make_range_ref(range_object, range_temp, loc);
5821 Expression* ref = Expression::make_temporary_reference(hiter, loc);
5822 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
5823 Expression* call = Runtime::make_call(Runtime::MAPITERINIT, loc, 3,
5824 p1, p2, p3);
5825 init->add_statement(Statement::make_statement(call, true));
5827 *pinit = init;
5829 // Set *PCOND to
5830 // hiter.key != nil
5832 ref = Expression::make_temporary_reference(hiter, loc);
5833 ref = Expression::make_field_reference(ref, 0, loc);
5834 Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, ref,
5835 Expression::make_nil(loc),
5836 loc);
5837 *pcond = ne;
5839 // Set *PITER_INIT to
5840 // index_temp = *hiter.key
5841 // value_temp = *hiter.val
5843 Block* iter_init = new Block(body_block, loc);
5845 Expression* lhs = Expression::make_temporary_reference(index_temp, loc);
5846 Expression* rhs = Expression::make_temporary_reference(hiter, loc);
5847 rhs = Expression::make_field_reference(ref, 0, loc);
5848 rhs = Expression::make_dereference(ref, Expression::NIL_CHECK_NOT_NEEDED,
5849 loc);
5850 Statement* set = Statement::make_assignment(lhs, rhs, loc);
5851 iter_init->add_statement(set);
5853 if (value_temp != NULL)
5855 lhs = Expression::make_temporary_reference(value_temp, loc);
5856 rhs = Expression::make_temporary_reference(hiter, loc);
5857 rhs = Expression::make_field_reference(rhs, 1, loc);
5858 rhs = Expression::make_dereference(rhs, Expression::NIL_CHECK_NOT_NEEDED,
5859 loc);
5860 set = Statement::make_assignment(lhs, rhs, loc);
5861 iter_init->add_statement(set);
5864 *piter_init = iter_init;
5866 // Set *PPOST to
5867 // mapiternext(&hiter)
5869 Block* post = new Block(enclosing, loc);
5871 ref = Expression::make_temporary_reference(hiter, loc);
5872 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
5873 call = Runtime::make_call(Runtime::MAPITERNEXT, loc, 1, p1);
5874 post->add_statement(Statement::make_statement(call, true));
5876 *ppost = post;
5879 // Lower a for range over a channel.
5881 void
5882 For_range_statement::lower_range_channel(Gogo*,
5883 Block*,
5884 Block* body_block,
5885 Named_object* range_object,
5886 Temporary_statement* range_temp,
5887 Temporary_statement* index_temp,
5888 Temporary_statement* value_temp,
5889 Block** pinit,
5890 Expression** pcond,
5891 Block** piter_init,
5892 Block** ppost)
5894 go_assert(value_temp == NULL);
5896 Location loc = this->location();
5898 // The loop we generate:
5899 // for {
5900 // index_temp, ok_temp = <-range
5901 // if !ok_temp {
5902 // break
5903 // }
5904 // index = index_temp
5905 // original body
5906 // }
5908 // We have no initialization code, no condition, and no post code.
5910 *pinit = NULL;
5911 *pcond = NULL;
5912 *ppost = NULL;
5914 // Set *PITER_INIT to
5915 // index_temp, ok_temp = <-range
5916 // if !ok_temp {
5917 // break
5918 // }
5920 Block* iter_init = new Block(body_block, loc);
5922 Temporary_statement* ok_temp =
5923 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
5924 iter_init->add_statement(ok_temp);
5926 Expression* cref = this->make_range_ref(range_object, range_temp, loc);
5927 Temporary_reference_expression* iref =
5928 Expression::make_temporary_reference(index_temp, loc);
5929 iref->set_is_lvalue();
5930 Temporary_reference_expression* oref =
5931 Expression::make_temporary_reference(ok_temp, loc);
5932 oref->set_is_lvalue();
5933 Statement* s = Statement::make_tuple_receive_assignment(iref, oref, cref,
5934 loc);
5935 iter_init->add_statement(s);
5937 Block* then_block = new Block(iter_init, loc);
5938 s = Statement::make_break_statement(this->break_label(), loc);
5939 then_block->add_statement(s);
5941 oref = Expression::make_temporary_reference(ok_temp, loc);
5942 Expression* cond = Expression::make_unary(OPERATOR_NOT, oref, loc);
5943 s = Statement::make_if_statement(cond, then_block, NULL, loc);
5944 iter_init->add_statement(s);
5946 *piter_init = iter_init;
5949 // Return the break LABEL_EXPR.
5951 Unnamed_label*
5952 For_range_statement::break_label()
5954 if (this->break_label_ == NULL)
5955 this->break_label_ = new Unnamed_label(this->location());
5956 return this->break_label_;
5959 // Return the continue LABEL_EXPR.
5961 Unnamed_label*
5962 For_range_statement::continue_label()
5964 if (this->continue_label_ == NULL)
5965 this->continue_label_ = new Unnamed_label(this->location());
5966 return this->continue_label_;
5969 // Dump the AST representation for a for range statement.
5971 void
5972 For_range_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
5975 ast_dump_context->print_indent();
5976 ast_dump_context->ostream() << "for ";
5977 ast_dump_context->dump_expression(this->index_var_);
5978 if (this->value_var_ != NULL)
5980 ast_dump_context->ostream() << ", ";
5981 ast_dump_context->dump_expression(this->value_var_);
5984 ast_dump_context->ostream() << " = range ";
5985 ast_dump_context->dump_expression(this->range_);
5986 if (ast_dump_context->dump_subblocks())
5988 ast_dump_context->ostream() << " {" << std::endl;
5990 ast_dump_context->indent();
5992 ast_dump_context->dump_block(this->statements_);
5994 ast_dump_context->unindent();
5995 ast_dump_context->print_indent();
5996 ast_dump_context->ostream() << "}";
5998 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
6001 // Make a for statement with a range clause.
6003 For_range_statement*
6004 Statement::make_for_range_statement(Expression* index_var,
6005 Expression* value_var,
6006 Expression* range,
6007 Location location)
6009 return new For_range_statement(index_var, value_var, range, location);