Big merge of changes to gofrontend repo that were postponed due to the
[official-gcc.git] / gcc / go / gofrontend / statements.cc
blob00367ef0553973395b00eed89195ef5eadc06dd1
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_unary(OPERATOR_MULT, e, loc);
319 Bexpression* be = e->get_backend(context);
320 set = context->backend()->assignment_statement(bfunction, be, binit, loc);
323 Expression* ref = Expression::make_temporary_reference(temp, loc);
324 Bexpression* bref = ref->get_backend(context);
325 Bstatement* sinit = context->backend()->init_statement(bfunction, bvar, bref);
327 std::vector<Bstatement*> stats;
328 stats.reserve(3);
329 stats.push_back(btemp);
330 if (set != NULL)
331 stats.push_back(set);
332 stats.push_back(sinit);
333 return context->backend()->statement_list(stats);
336 // Dump the AST representation for a variable declaration.
338 void
339 Variable_declaration_statement::do_dump_statement(
340 Ast_dump_context* ast_dump_context) const
342 ast_dump_context->print_indent();
344 go_assert(var_->is_variable());
345 ast_dump_context->ostream() << "var " << this->var_->name() << " ";
346 Variable* var = this->var_->var_value();
347 if (var->has_type())
349 ast_dump_context->dump_type(var->type());
350 ast_dump_context->ostream() << " ";
352 if (var->init() != NULL)
354 ast_dump_context->ostream() << "= ";
355 ast_dump_context->dump_expression(var->init());
357 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
360 // Make a variable declaration.
362 Statement*
363 Statement::make_variable_declaration(Named_object* var)
365 return new Variable_declaration_statement(var);
368 // Class Temporary_statement.
370 // Return the type of the temporary variable.
372 Type*
373 Temporary_statement::type() const
375 Type* type = this->type_ != NULL ? this->type_ : this->init_->type();
377 // Temporary variables cannot have a void type.
378 if (type->is_void_type())
380 go_assert(saw_errors());
381 return Type::make_error_type();
383 return type;
386 // Traversal.
389 Temporary_statement::do_traverse(Traverse* traverse)
391 if (this->type_ != NULL
392 && this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
393 return TRAVERSE_EXIT;
394 if (this->init_ == NULL)
395 return TRAVERSE_CONTINUE;
396 else
397 return this->traverse_expression(traverse, &this->init_);
400 // Traverse assignments.
402 bool
403 Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
405 if (this->init_ == NULL)
406 return false;
407 tassign->value(&this->init_, true, true);
408 return true;
411 // Determine types.
413 void
414 Temporary_statement::do_determine_types()
416 if (this->type_ != NULL && this->type_->is_abstract())
417 this->type_ = this->type_->make_non_abstract_type();
419 if (this->init_ != NULL)
421 if (this->type_ == NULL)
422 this->init_->determine_type_no_context();
423 else
425 Type_context context(this->type_, false);
426 this->init_->determine_type(&context);
430 if (this->type_ == NULL)
432 this->type_ = this->init_->type();
433 go_assert(!this->type_->is_abstract());
437 // Check types.
439 void
440 Temporary_statement::do_check_types(Gogo*)
442 if (this->type_ != NULL && this->init_ != NULL)
444 std::string reason;
445 if (!Type::are_assignable(this->type_, this->init_->type(), &reason))
447 if (reason.empty())
448 go_error_at(this->location(), "incompatible types in assignment");
449 else
450 go_error_at(this->location(), "incompatible types in assignment (%s)",
451 reason.c_str());
452 this->set_is_error();
457 // Flatten a temporary statement: add another temporary when it might
458 // be needed for interface conversion.
460 Statement*
461 Temporary_statement::do_flatten(Gogo*, Named_object*, Block*,
462 Statement_inserter* inserter)
464 if (this->type()->is_error_type()
465 || (this->init_ != NULL
466 && this->init_->is_error_expression()))
468 go_assert(saw_errors());
469 return Statement::make_error_statement(this->location());
472 if (this->type_ != NULL
473 && this->init_ != NULL
474 && !Type::are_identical(this->type_, this->init_->type(), false, NULL)
475 && this->init_->type()->interface_type() != NULL
476 && !this->init_->is_variable())
478 Temporary_statement *temp =
479 Statement::make_temporary(NULL, this->init_, this->location());
480 inserter->insert(temp);
481 this->init_ = Expression::make_temporary_reference(temp,
482 this->location());
484 return this;
487 // Convert to backend representation.
489 Bstatement*
490 Temporary_statement::do_get_backend(Translate_context* context)
492 go_assert(this->bvariable_ == NULL);
494 Named_object* function = context->function();
495 go_assert(function != NULL);
496 Bfunction* bfunction = function->func_value()->get_decl();
497 Btype* btype = this->type()->get_backend(context->gogo());
499 Bexpression* binit;
500 if (this->init_ == NULL)
501 binit = NULL;
502 else if (this->type_ == NULL)
503 binit = this->init_->get_backend(context);
504 else
506 Expression* init = Expression::convert_for_assignment(context->gogo(),
507 this->type_,
508 this->init_,
509 this->location());
510 binit = init->get_backend(context);
513 if (binit != NULL)
514 binit = context->backend()->convert_expression(btype, binit,
515 this->location());
517 Bstatement* statement;
518 this->bvariable_ =
519 context->backend()->temporary_variable(bfunction, context->bblock(),
520 btype, binit,
521 this->is_address_taken_,
522 this->location(), &statement);
523 return statement;
526 // Return the backend variable.
528 Bvariable*
529 Temporary_statement::get_backend_variable(Translate_context* context) const
531 if (this->bvariable_ == NULL)
533 go_assert(saw_errors());
534 return context->backend()->error_variable();
536 return this->bvariable_;
539 // Dump the AST represemtation for a temporary statement
541 void
542 Temporary_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
544 ast_dump_context->print_indent();
545 ast_dump_context->dump_temp_variable_name(this);
546 if (this->type_ != NULL)
548 ast_dump_context->ostream() << " ";
549 ast_dump_context->dump_type(this->type_);
551 if (this->init_ != NULL)
553 ast_dump_context->ostream() << " = ";
554 ast_dump_context->dump_expression(this->init_);
556 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
559 // Make and initialize a temporary variable in BLOCK.
561 Temporary_statement*
562 Statement::make_temporary(Type* type, Expression* init,
563 Location location)
565 return new Temporary_statement(type, init, location);
568 // The Move_subexpressions class is used to move all top-level
569 // subexpressions of an expression. This is used for things like
570 // index expressions in which we must evaluate the index value before
571 // it can be changed by a multiple assignment.
573 class Move_subexpressions : public Traverse
575 public:
576 Move_subexpressions(int skip, Block* block)
577 : Traverse(traverse_expressions),
578 skip_(skip), block_(block)
581 protected:
583 expression(Expression**);
585 private:
586 // The number of subexpressions to skip moving. This is used to
587 // avoid moving the array itself, as we only need to move the index.
588 int skip_;
589 // The block where new temporary variables should be added.
590 Block* block_;
594 Move_subexpressions::expression(Expression** pexpr)
596 if (this->skip_ > 0)
597 --this->skip_;
598 else if ((*pexpr)->temporary_reference_expression() == NULL
599 && !(*pexpr)->is_nil_expression()
600 && !(*pexpr)->is_constant())
602 Location loc = (*pexpr)->location();
603 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
604 this->block_->add_statement(temp);
605 *pexpr = Expression::make_temporary_reference(temp, loc);
607 // We only need to move top-level subexpressions.
608 return TRAVERSE_SKIP_COMPONENTS;
611 // The Move_ordered_evals class is used to find any subexpressions of
612 // an expression that have an evaluation order dependency. It creates
613 // temporary variables to hold them.
615 class Move_ordered_evals : public Traverse
617 public:
618 Move_ordered_evals(Block* block)
619 : Traverse(traverse_expressions),
620 block_(block)
623 protected:
625 expression(Expression**);
627 private:
628 // The block where new temporary variables should be added.
629 Block* block_;
633 Move_ordered_evals::expression(Expression** pexpr)
635 // We have to look at subexpressions first.
636 if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
637 return TRAVERSE_EXIT;
639 int i;
640 if ((*pexpr)->must_eval_subexpressions_in_order(&i))
642 Move_subexpressions ms(i, this->block_);
643 if ((*pexpr)->traverse_subexpressions(&ms) == TRAVERSE_EXIT)
644 return TRAVERSE_EXIT;
647 if ((*pexpr)->must_eval_in_order())
649 Call_expression* call = (*pexpr)->call_expression();
650 if (call != NULL && call->is_multi_value_arg())
652 // A call expression which returns multiple results as an argument
653 // to another call must be handled specially. We can't create a
654 // temporary because there is no type to give it. Instead, group
655 // the caller and this multi-valued call argument and use a temporary
656 // variable to hold them.
657 return TRAVERSE_SKIP_COMPONENTS;
660 Location loc = (*pexpr)->location();
661 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
662 this->block_->add_statement(temp);
663 *pexpr = Expression::make_temporary_reference(temp, loc);
665 return TRAVERSE_SKIP_COMPONENTS;
668 // Class Assignment_statement.
670 // Traversal.
673 Assignment_statement::do_traverse(Traverse* traverse)
675 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
676 return TRAVERSE_EXIT;
677 return this->traverse_expression(traverse, &this->rhs_);
680 bool
681 Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
683 tassign->assignment(&this->lhs_, &this->rhs_);
684 return true;
687 // Lower an assignment to a map index expression to a runtime function
688 // call.
690 Statement*
691 Assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
692 Statement_inserter*)
694 Map_index_expression* mie = this->lhs_->map_index_expression();
695 if (mie != NULL)
697 Location loc = this->location();
699 Expression* map = mie->map();
700 Map_type* mt = map->type()->map_type();
701 if (mt == NULL)
703 go_assert(saw_errors());
704 return Statement::make_error_statement(loc);
707 Block* b = new Block(enclosing, loc);
709 // Move out any subexpressions on the left hand side to make
710 // sure that functions are called in the required order.
711 Move_ordered_evals moe(b);
712 mie->traverse_subexpressions(&moe);
714 // Copy the key into a temporary so that we can take its address
715 // without pushing the value onto the heap.
717 // var key_temp KEY_TYPE = MAP_INDEX
718 Temporary_statement* key_temp = Statement::make_temporary(mt->key_type(),
719 mie->index(),
720 loc);
721 b->add_statement(key_temp);
723 // Copy the value into a temporary to ensure that it is
724 // evaluated before we add the key to the map. This may matter
725 // if the value is itself a reference to the map.
727 // var val_temp VAL_TYPE = RHS
728 Temporary_statement* val_temp = Statement::make_temporary(mt->val_type(),
729 this->rhs_,
730 loc);
731 b->add_statement(val_temp);
733 // *mapassign(TYPE, MAP, &key_temp) = RHS
734 Expression* a1 = Expression::make_type_descriptor(mt, loc);
735 Expression* a2 = mie->map();
736 Temporary_reference_expression* ref =
737 Expression::make_temporary_reference(key_temp, loc);
738 Expression* a3 = Expression::make_unary(OPERATOR_AND, ref, loc);
739 Expression* call = Runtime::make_call(Runtime::MAPASSIGN, loc, 3,
740 a1, a2, a3);
741 Type* ptrval_type = Type::make_pointer_type(mt->val_type());
742 call = Expression::make_cast(ptrval_type, call, loc);
743 Expression* indir = Expression::make_unary(OPERATOR_MULT, call, loc);
744 ref = Expression::make_temporary_reference(val_temp, loc);
745 b->add_statement(Statement::make_assignment(indir, ref, loc));
747 return Statement::make_block_statement(b, loc);
750 return this;
753 // Set types for the assignment.
755 void
756 Assignment_statement::do_determine_types()
758 this->lhs_->determine_type_no_context();
759 Type* rhs_context_type = this->lhs_->type();
760 if (rhs_context_type->is_sink_type())
761 rhs_context_type = NULL;
762 Type_context context(rhs_context_type, false);
763 this->rhs_->determine_type(&context);
766 // Check types for an assignment.
768 void
769 Assignment_statement::do_check_types(Gogo*)
771 // The left hand side must be either addressable, a map index
772 // expression, or the blank identifier.
773 if (!this->lhs_->is_addressable()
774 && this->lhs_->map_index_expression() == NULL
775 && !this->lhs_->is_sink_expression())
777 if (!this->lhs_->type()->is_error())
778 this->report_error(_("invalid left hand side of assignment"));
779 return;
782 Type* lhs_type = this->lhs_->type();
783 Type* rhs_type = this->rhs_->type();
785 // Invalid assignment of nil to the blank identifier.
786 if (lhs_type->is_sink_type()
787 && rhs_type->is_nil_type())
789 this->report_error(_("use of untyped nil"));
790 return;
793 std::string reason;
794 if (!Type::are_assignable(lhs_type, rhs_type, &reason))
796 if (reason.empty())
797 go_error_at(this->location(), "incompatible types in assignment");
798 else
799 go_error_at(this->location(), "incompatible types in assignment (%s)",
800 reason.c_str());
801 this->set_is_error();
804 if (lhs_type->is_error() || rhs_type->is_error())
805 this->set_is_error();
808 // Flatten an assignment statement. We may need a temporary for
809 // interface conversion.
811 Statement*
812 Assignment_statement::do_flatten(Gogo*, Named_object*, Block*,
813 Statement_inserter* inserter)
815 if (this->lhs_->is_error_expression()
816 || this->lhs_->type()->is_error_type()
817 || this->rhs_->is_error_expression()
818 || this->rhs_->type()->is_error_type())
820 go_assert(saw_errors());
821 return Statement::make_error_statement(this->location());
824 if (!this->lhs_->is_sink_expression()
825 && !Type::are_identical(this->lhs_->type(), this->rhs_->type(),
826 false, NULL)
827 && this->rhs_->type()->interface_type() != NULL
828 && !this->rhs_->is_variable())
830 Temporary_statement* temp =
831 Statement::make_temporary(NULL, this->rhs_, this->location());
832 inserter->insert(temp);
833 this->rhs_ = Expression::make_temporary_reference(temp,
834 this->location());
836 return this;
840 // Helper class to locate a root Var_expression within an expression
841 // tree and mark it as being in an "lvalue" or assignment
842 // context. Examples:
844 // x, y = 40, foo(w)
845 // x[2] = bar(v)
846 // x.z.w[blah(v + u)], y.another = 2, 3
848 // In the code above, vars "x" and "y" appear in lvalue / assignment
849 // context, whereas the other vars "v", "u", etc are in rvalue context.
851 // Note: at the moment the Var_expression version of "do_copy()"
852 // defaults to returning the original object, not a new object,
853 // meaning that a given Var_expression can be referenced from more
854 // than one place in the tree. This means that when we want to mark a
855 // Var_expression as having lvalue semantics, we need to make a copy
856 // of it. Example:
858 // mystruct.myfield += 42
860 // When this is lowered to eliminate the += operator, we get a tree
862 // mystruct.myfield = mystruct.field + 42
864 // in which the "mystruct" same Var_expression is referenced on both
865 // LHS and RHS subtrees. This in turn means that if we try to mark the
866 // LHS Var_expression the RHS Var_expression will also be marked. To
867 // address this issue, the code below clones any var_expression before
868 // applying an lvalue marking.
871 class Mark_lvalue_varexprs : public Traverse
873 public:
874 Mark_lvalue_varexprs()
875 : Traverse(traverse_expressions)
878 protected:
880 expression(Expression**);
882 private:
885 int Mark_lvalue_varexprs::expression(Expression** ppexpr)
887 Expression* e = *ppexpr;
889 Var_expression* ve = e->var_expression();
890 if (ve)
892 ve = new Var_expression(ve->named_object(), ve->location());
893 ve->set_in_lvalue_pos();
894 *ppexpr = ve;
895 return TRAVERSE_EXIT;
898 Field_reference_expression* fre = e->field_reference_expression();
899 if (fre != NULL)
900 return TRAVERSE_CONTINUE;
902 Array_index_expression* aie = e->array_index_expression();
903 if (aie != NULL)
905 Mark_lvalue_varexprs mlve;
906 aie->array()->traverse_subexpressions(&mlve);
907 return TRAVERSE_EXIT;
910 Unary_expression* ue = e->unary_expression();
911 if (ue && ue->op() == OPERATOR_MULT)
912 return TRAVERSE_CONTINUE;
914 return TRAVERSE_EXIT;
917 // Convert an assignment statement to the backend representation.
919 Bstatement*
920 Assignment_statement::do_get_backend(Translate_context* context)
922 if (this->lhs_->is_sink_expression())
924 Bexpression* rhs = this->rhs_->get_backend(context);
925 Bfunction* bfunction = context->function()->func_value()->get_decl();
926 return context->backend()->expression_statement(bfunction, rhs);
929 Mark_lvalue_varexprs mlve;
930 Expression::traverse(&this->lhs_, &mlve);
932 Bexpression* lhs = this->lhs_->get_backend(context);
933 Expression* conv =
934 Expression::convert_for_assignment(context->gogo(), this->lhs_->type(),
935 this->rhs_, this->location());
936 Bexpression* rhs = conv->get_backend(context);
937 Bfunction* bfunction = context->function()->func_value()->get_decl();
938 return context->backend()->assignment_statement(bfunction, lhs, rhs,
939 this->location());
942 // Dump the AST representation for an assignment statement.
944 void
945 Assignment_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
946 const
948 ast_dump_context->print_indent();
949 ast_dump_context->dump_expression(this->lhs_);
950 ast_dump_context->ostream() << " = " ;
951 ast_dump_context->dump_expression(this->rhs_);
952 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
955 // Make an assignment statement.
957 Statement*
958 Statement::make_assignment(Expression* lhs, Expression* rhs,
959 Location location)
961 return new Assignment_statement(lhs, rhs, location);
964 // An assignment operation statement.
966 class Assignment_operation_statement : public Statement
968 public:
969 Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
970 Location location)
971 : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
972 op_(op), lhs_(lhs), rhs_(rhs)
975 protected:
977 do_traverse(Traverse*);
979 bool
980 do_traverse_assignments(Traverse_assignments*)
981 { go_unreachable(); }
983 Statement*
984 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
986 Bstatement*
987 do_get_backend(Translate_context*)
988 { go_unreachable(); }
990 void
991 do_dump_statement(Ast_dump_context*) const;
993 private:
994 // The operator (OPERATOR_PLUSEQ, etc.).
995 Operator op_;
996 // Left hand side.
997 Expression* lhs_;
998 // Right hand side.
999 Expression* rhs_;
1002 // Traversal.
1005 Assignment_operation_statement::do_traverse(Traverse* traverse)
1007 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
1008 return TRAVERSE_EXIT;
1009 return this->traverse_expression(traverse, &this->rhs_);
1012 // Lower an assignment operation statement to a regular assignment
1013 // statement.
1015 Statement*
1016 Assignment_operation_statement::do_lower(Gogo*, Named_object*,
1017 Block* enclosing, Statement_inserter*)
1019 Location loc = this->location();
1021 // We have to evaluate the left hand side expression only once. We
1022 // do this by moving out any expression with side effects.
1023 Block* b = new Block(enclosing, loc);
1024 Move_ordered_evals moe(b);
1025 this->lhs_->traverse_subexpressions(&moe);
1027 Expression* lval = this->lhs_->copy();
1029 Operator op;
1030 switch (this->op_)
1032 case OPERATOR_PLUSEQ:
1033 op = OPERATOR_PLUS;
1034 break;
1035 case OPERATOR_MINUSEQ:
1036 op = OPERATOR_MINUS;
1037 break;
1038 case OPERATOR_OREQ:
1039 op = OPERATOR_OR;
1040 break;
1041 case OPERATOR_XOREQ:
1042 op = OPERATOR_XOR;
1043 break;
1044 case OPERATOR_MULTEQ:
1045 op = OPERATOR_MULT;
1046 break;
1047 case OPERATOR_DIVEQ:
1048 op = OPERATOR_DIV;
1049 break;
1050 case OPERATOR_MODEQ:
1051 op = OPERATOR_MOD;
1052 break;
1053 case OPERATOR_LSHIFTEQ:
1054 op = OPERATOR_LSHIFT;
1055 break;
1056 case OPERATOR_RSHIFTEQ:
1057 op = OPERATOR_RSHIFT;
1058 break;
1059 case OPERATOR_ANDEQ:
1060 op = OPERATOR_AND;
1061 break;
1062 case OPERATOR_BITCLEAREQ:
1063 op = OPERATOR_BITCLEAR;
1064 break;
1065 default:
1066 go_unreachable();
1069 Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
1070 Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
1071 if (b->statements()->empty())
1073 delete b;
1074 return s;
1076 else
1078 b->add_statement(s);
1079 return Statement::make_block_statement(b, loc);
1083 // Dump the AST representation for an assignment operation statement
1085 void
1086 Assignment_operation_statement::do_dump_statement(
1087 Ast_dump_context* ast_dump_context) const
1089 ast_dump_context->print_indent();
1090 ast_dump_context->dump_expression(this->lhs_);
1091 ast_dump_context->dump_operator(this->op_);
1092 ast_dump_context->dump_expression(this->rhs_);
1093 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1096 // Make an assignment operation statement.
1098 Statement*
1099 Statement::make_assignment_operation(Operator op, Expression* lhs,
1100 Expression* rhs, Location location)
1102 return new Assignment_operation_statement(op, lhs, rhs, location);
1105 // A tuple assignment statement. This differs from an assignment
1106 // statement in that the right-hand-side expressions are evaluated in
1107 // parallel.
1109 class Tuple_assignment_statement : public Statement
1111 public:
1112 Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
1113 Location location)
1114 : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
1115 lhs_(lhs), rhs_(rhs)
1118 protected:
1120 do_traverse(Traverse* traverse);
1122 bool
1123 do_traverse_assignments(Traverse_assignments*)
1124 { go_unreachable(); }
1126 Statement*
1127 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1129 Bstatement*
1130 do_get_backend(Translate_context*)
1131 { go_unreachable(); }
1133 void
1134 do_dump_statement(Ast_dump_context*) const;
1136 private:
1137 // Left hand side--a list of lvalues.
1138 Expression_list* lhs_;
1139 // Right hand side--a list of rvalues.
1140 Expression_list* rhs_;
1143 // Traversal.
1146 Tuple_assignment_statement::do_traverse(Traverse* traverse)
1148 if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
1149 return TRAVERSE_EXIT;
1150 return this->traverse_expression_list(traverse, this->rhs_);
1153 // Lower a tuple assignment. We use temporary variables to split it
1154 // up into a set of single assignments.
1156 Statement*
1157 Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
1158 Statement_inserter*)
1160 Location loc = this->location();
1162 Block* b = new Block(enclosing, loc);
1164 // First move out any subexpressions on the left hand side. The
1165 // right hand side will be evaluated in the required order anyhow.
1166 Move_ordered_evals moe(b);
1167 for (Expression_list::iterator plhs = this->lhs_->begin();
1168 plhs != this->lhs_->end();
1169 ++plhs)
1170 Expression::traverse(&*plhs, &moe);
1172 std::vector<Temporary_statement*> temps;
1173 temps.reserve(this->lhs_->size());
1175 Expression_list::const_iterator prhs = this->rhs_->begin();
1176 for (Expression_list::const_iterator plhs = this->lhs_->begin();
1177 plhs != this->lhs_->end();
1178 ++plhs, ++prhs)
1180 go_assert(prhs != this->rhs_->end());
1182 if ((*plhs)->is_error_expression()
1183 || (*plhs)->type()->is_error()
1184 || (*prhs)->is_error_expression()
1185 || (*prhs)->type()->is_error())
1186 continue;
1188 if ((*plhs)->is_sink_expression())
1190 if ((*prhs)->type()->is_nil_type())
1191 this->report_error(_("use of untyped nil"));
1192 else
1193 b->add_statement(Statement::make_statement(*prhs, true));
1194 continue;
1197 Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
1198 *prhs, loc);
1199 b->add_statement(temp);
1200 temps.push_back(temp);
1203 go_assert(prhs == this->rhs_->end());
1205 prhs = this->rhs_->begin();
1206 std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
1207 for (Expression_list::const_iterator plhs = this->lhs_->begin();
1208 plhs != this->lhs_->end();
1209 ++plhs, ++prhs)
1211 if ((*plhs)->is_error_expression()
1212 || (*plhs)->type()->is_error()
1213 || (*prhs)->is_error_expression()
1214 || (*prhs)->type()->is_error())
1215 continue;
1217 if ((*plhs)->is_sink_expression())
1218 continue;
1220 Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
1221 b->add_statement(Statement::make_assignment(*plhs, ref, loc));
1222 ++ptemp;
1224 go_assert(ptemp == temps.end() || saw_errors());
1226 return Statement::make_block_statement(b, loc);
1229 // Dump the AST representation for a tuple assignment statement.
1231 void
1232 Tuple_assignment_statement::do_dump_statement(
1233 Ast_dump_context* ast_dump_context) const
1235 ast_dump_context->print_indent();
1236 ast_dump_context->dump_expression_list(this->lhs_);
1237 ast_dump_context->ostream() << " = ";
1238 ast_dump_context->dump_expression_list(this->rhs_);
1239 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1242 // Make a tuple assignment statement.
1244 Statement*
1245 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
1246 Location location)
1248 return new Tuple_assignment_statement(lhs, rhs, location);
1251 // A tuple assignment from a map index expression.
1252 // v, ok = m[k]
1254 class Tuple_map_assignment_statement : public Statement
1256 public:
1257 Tuple_map_assignment_statement(Expression* val, Expression* present,
1258 Expression* map_index,
1259 Location location)
1260 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
1261 val_(val), present_(present), map_index_(map_index)
1264 protected:
1266 do_traverse(Traverse* traverse);
1268 bool
1269 do_traverse_assignments(Traverse_assignments*)
1270 { go_unreachable(); }
1272 Statement*
1273 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1275 Bstatement*
1276 do_get_backend(Translate_context*)
1277 { go_unreachable(); }
1279 void
1280 do_dump_statement(Ast_dump_context*) const;
1282 private:
1283 // Lvalue which receives the value from the map.
1284 Expression* val_;
1285 // Lvalue which receives whether the key value was present.
1286 Expression* present_;
1287 // The map index expression.
1288 Expression* map_index_;
1291 // Traversal.
1294 Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
1296 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1297 || this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
1298 return TRAVERSE_EXIT;
1299 return this->traverse_expression(traverse, &this->map_index_);
1302 // Lower a tuple map assignment.
1304 Statement*
1305 Tuple_map_assignment_statement::do_lower(Gogo* gogo, Named_object*,
1306 Block* enclosing, Statement_inserter*)
1308 Location loc = this->location();
1310 Map_index_expression* map_index = this->map_index_->map_index_expression();
1311 if (map_index == NULL)
1313 this->report_error(_("expected map index on right hand side"));
1314 return Statement::make_error_statement(loc);
1316 Map_type* map_type = map_index->get_map_type();
1317 if (map_type == NULL)
1318 return Statement::make_error_statement(loc);
1320 Block* b = new Block(enclosing, loc);
1322 // Move out any subexpressions to make sure that functions are
1323 // called in the required order.
1324 Move_ordered_evals moe(b);
1325 this->val_->traverse_subexpressions(&moe);
1326 this->present_->traverse_subexpressions(&moe);
1328 // Copy the key value into a temporary so that we can take its
1329 // address without pushing the value onto the heap.
1331 // var key_temp KEY_TYPE = MAP_INDEX
1332 Temporary_statement* key_temp =
1333 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1334 b->add_statement(key_temp);
1336 // var val_ptr_temp *VAL_TYPE
1337 Type* val_ptr_type = Type::make_pointer_type(map_type->val_type());
1338 Temporary_statement* val_ptr_temp = Statement::make_temporary(val_ptr_type,
1339 NULL, loc);
1340 b->add_statement(val_ptr_temp);
1342 // var present_temp bool
1343 Temporary_statement* present_temp =
1344 Statement::make_temporary((this->present_->type()->is_sink_type())
1345 ? Type::make_boolean_type()
1346 : this->present_->type(),
1347 NULL, loc);
1348 b->add_statement(present_temp);
1350 // val_ptr_temp, present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp)
1351 Expression* a1 = Expression::make_type_descriptor(map_type, loc);
1352 Expression* a2 = map_index->map();
1353 Temporary_reference_expression* ref =
1354 Expression::make_temporary_reference(key_temp, loc);
1355 Expression* a3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1356 Expression* a4 = map_type->fat_zero_value(gogo);
1357 Call_expression* call;
1358 if (a4 == NULL)
1359 call = Runtime::make_call(Runtime::MAPACCESS2, loc, 3, a1, a2, a3);
1360 else
1361 call = Runtime::make_call(Runtime::MAPACCESS2_FAT, loc, 4, a1, a2, a3, a4);
1362 ref = Expression::make_temporary_reference(val_ptr_temp, loc);
1363 ref->set_is_lvalue();
1364 Expression* res = Expression::make_call_result(call, 0);
1365 res = Expression::make_unsafe_cast(val_ptr_type, res, loc);
1366 Statement* s = Statement::make_assignment(ref, res, loc);
1367 b->add_statement(s);
1368 ref = Expression::make_temporary_reference(present_temp, loc);
1369 ref->set_is_lvalue();
1370 res = Expression::make_call_result(call, 1);
1371 s = Statement::make_assignment(ref, res, loc);
1372 b->add_statement(s);
1374 // val = *val__ptr_temp
1375 ref = Expression::make_temporary_reference(val_ptr_temp, loc);
1376 Expression* ind = Expression::make_unary(OPERATOR_MULT, ref, loc);
1377 s = Statement::make_assignment(this->val_, ind, loc);
1378 b->add_statement(s);
1380 // present = present_temp
1381 ref = Expression::make_temporary_reference(present_temp, loc);
1382 s = Statement::make_assignment(this->present_, ref, loc);
1383 b->add_statement(s);
1385 return Statement::make_block_statement(b, loc);
1388 // Dump the AST representation for a tuple map assignment statement.
1390 void
1391 Tuple_map_assignment_statement::do_dump_statement(
1392 Ast_dump_context* ast_dump_context) const
1394 ast_dump_context->print_indent();
1395 ast_dump_context->dump_expression(this->val_);
1396 ast_dump_context->ostream() << ", ";
1397 ast_dump_context->dump_expression(this->present_);
1398 ast_dump_context->ostream() << " = ";
1399 ast_dump_context->dump_expression(this->map_index_);
1400 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1403 // Make a map assignment statement which returns a pair of values.
1405 Statement*
1406 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
1407 Expression* map_index,
1408 Location location)
1410 return new Tuple_map_assignment_statement(val, present, map_index, location);
1413 // A tuple assignment from a receive statement.
1415 class Tuple_receive_assignment_statement : public Statement
1417 public:
1418 Tuple_receive_assignment_statement(Expression* val, Expression* closed,
1419 Expression* channel, Location location)
1420 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
1421 val_(val), closed_(closed), channel_(channel)
1424 protected:
1426 do_traverse(Traverse* traverse);
1428 bool
1429 do_traverse_assignments(Traverse_assignments*)
1430 { go_unreachable(); }
1432 Statement*
1433 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1435 Bstatement*
1436 do_get_backend(Translate_context*)
1437 { go_unreachable(); }
1439 void
1440 do_dump_statement(Ast_dump_context*) const;
1442 private:
1443 // Lvalue which receives the value from the channel.
1444 Expression* val_;
1445 // Lvalue which receives whether the channel is closed.
1446 Expression* closed_;
1447 // The channel on which we receive the value.
1448 Expression* channel_;
1451 // Traversal.
1454 Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
1456 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1457 || this->traverse_expression(traverse, &this->closed_) == TRAVERSE_EXIT)
1458 return TRAVERSE_EXIT;
1459 return this->traverse_expression(traverse, &this->channel_);
1462 // Lower to a function call.
1464 Statement*
1465 Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
1466 Block* enclosing,
1467 Statement_inserter*)
1469 Location loc = this->location();
1471 Channel_type* channel_type = this->channel_->type()->channel_type();
1472 if (channel_type == NULL)
1474 this->report_error(_("expected channel"));
1475 return Statement::make_error_statement(loc);
1477 if (!channel_type->may_receive())
1479 this->report_error(_("invalid receive on send-only channel"));
1480 return Statement::make_error_statement(loc);
1483 Block* b = new Block(enclosing, loc);
1485 // Make sure that any subexpressions on the left hand side are
1486 // evaluated in the right order.
1487 Move_ordered_evals moe(b);
1488 this->val_->traverse_subexpressions(&moe);
1489 this->closed_->traverse_subexpressions(&moe);
1491 // var val_temp ELEMENT_TYPE
1492 Temporary_statement* val_temp =
1493 Statement::make_temporary(channel_type->element_type(), NULL, loc);
1494 b->add_statement(val_temp);
1496 // var closed_temp bool
1497 Temporary_statement* closed_temp =
1498 Statement::make_temporary((this->closed_->type()->is_sink_type())
1499 ? Type::make_boolean_type()
1500 : this->closed_->type(),
1501 NULL, loc);
1502 b->add_statement(closed_temp);
1504 // closed_temp = chanrecv2(type, channel, &val_temp)
1505 Expression* td = Expression::make_type_descriptor(this->channel_->type(),
1506 loc);
1507 Temporary_reference_expression* ref =
1508 Expression::make_temporary_reference(val_temp, loc);
1509 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1510 Expression* call = Runtime::make_call(Runtime::CHANRECV2,
1511 loc, 3, td, this->channel_, p2);
1512 ref = Expression::make_temporary_reference(closed_temp, loc);
1513 ref->set_is_lvalue();
1514 Statement* s = Statement::make_assignment(ref, call, loc);
1515 b->add_statement(s);
1517 // val = val_temp
1518 ref = Expression::make_temporary_reference(val_temp, loc);
1519 s = Statement::make_assignment(this->val_, ref, loc);
1520 b->add_statement(s);
1522 // closed = closed_temp
1523 ref = Expression::make_temporary_reference(closed_temp, loc);
1524 s = Statement::make_assignment(this->closed_, ref, loc);
1525 b->add_statement(s);
1527 return Statement::make_block_statement(b, loc);
1530 // Dump the AST representation for a tuple receive statement.
1532 void
1533 Tuple_receive_assignment_statement::do_dump_statement(
1534 Ast_dump_context* ast_dump_context) const
1536 ast_dump_context->print_indent();
1537 ast_dump_context->dump_expression(this->val_);
1538 ast_dump_context->ostream() << ", ";
1539 ast_dump_context->dump_expression(this->closed_);
1540 ast_dump_context->ostream() << " <- ";
1541 ast_dump_context->dump_expression(this->channel_);
1542 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1545 // Make a nonblocking receive statement.
1547 Statement*
1548 Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
1549 Expression* channel,
1550 Location location)
1552 return new Tuple_receive_assignment_statement(val, closed, channel,
1553 location);
1556 // An assignment to a pair of values from a type guard. This is a
1557 // conditional type guard. v, ok = i.(type).
1559 class Tuple_type_guard_assignment_statement : public Statement
1561 public:
1562 Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
1563 Expression* expr, Type* type,
1564 Location location)
1565 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
1566 val_(val), ok_(ok), expr_(expr), type_(type)
1569 protected:
1571 do_traverse(Traverse*);
1573 bool
1574 do_traverse_assignments(Traverse_assignments*)
1575 { go_unreachable(); }
1577 Statement*
1578 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1580 Bstatement*
1581 do_get_backend(Translate_context*)
1582 { go_unreachable(); }
1584 void
1585 do_dump_statement(Ast_dump_context*) const;
1587 private:
1588 Call_expression*
1589 lower_to_type(Runtime::Function);
1591 void
1592 lower_to_object_type(Block*, Runtime::Function);
1594 // The variable which recieves the converted value.
1595 Expression* val_;
1596 // The variable which receives the indication of success.
1597 Expression* ok_;
1598 // The expression being converted.
1599 Expression* expr_;
1600 // The type to which the expression is being converted.
1601 Type* type_;
1604 // Traverse a type guard tuple assignment.
1607 Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
1609 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1610 || this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
1611 || this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
1612 return TRAVERSE_EXIT;
1613 return this->traverse_expression(traverse, &this->expr_);
1616 // Lower to a function call.
1618 Statement*
1619 Tuple_type_guard_assignment_statement::do_lower(Gogo*, Named_object*,
1620 Block* enclosing,
1621 Statement_inserter*)
1623 Location loc = this->location();
1625 Type* expr_type = this->expr_->type();
1626 if (expr_type->interface_type() == NULL)
1628 if (!expr_type->is_error() && !this->type_->is_error())
1629 this->report_error(_("type assertion only valid for interface types"));
1630 return Statement::make_error_statement(loc);
1633 Block* b = new Block(enclosing, loc);
1635 // Make sure that any subexpressions on the left hand side are
1636 // evaluated in the right order.
1637 Move_ordered_evals moe(b);
1638 this->val_->traverse_subexpressions(&moe);
1639 this->ok_->traverse_subexpressions(&moe);
1641 bool expr_is_empty = expr_type->interface_type()->is_empty();
1642 Call_expression* call;
1643 if (this->type_->interface_type() != NULL)
1645 if (this->type_->interface_type()->is_empty())
1646 call = Runtime::make_call((expr_is_empty
1647 ? Runtime::IFACEE2E2
1648 : Runtime::IFACEI2E2),
1649 loc, 1, this->expr_);
1650 else
1651 call = this->lower_to_type(expr_is_empty
1652 ? Runtime::IFACEE2I2
1653 : Runtime::IFACEI2I2);
1655 else if (this->type_->points_to() != NULL)
1656 call = this->lower_to_type(expr_is_empty
1657 ? Runtime::IFACEE2T2P
1658 : Runtime::IFACEI2T2P);
1659 else
1661 this->lower_to_object_type(b,
1662 (expr_is_empty
1663 ? Runtime::IFACEE2T2
1664 : Runtime::IFACEI2T2));
1665 call = NULL;
1668 if (call != NULL)
1670 Expression* res = Expression::make_call_result(call, 0);
1671 res = Expression::make_unsafe_cast(this->type_, res, loc);
1672 Statement* s = Statement::make_assignment(this->val_, res, loc);
1673 b->add_statement(s);
1675 res = Expression::make_call_result(call, 1);
1676 s = Statement::make_assignment(this->ok_, res, loc);
1677 b->add_statement(s);
1680 return Statement::make_block_statement(b, loc);
1683 // Lower a conversion to a non-empty interface type or a pointer type.
1685 Call_expression*
1686 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code)
1688 Location loc = this->location();
1689 return Runtime::make_call(code, loc, 2,
1690 Expression::make_type_descriptor(this->type_, loc),
1691 this->expr_);
1694 // Lower a conversion to a non-interface non-pointer type.
1696 void
1697 Tuple_type_guard_assignment_statement::lower_to_object_type(
1698 Block* b,
1699 Runtime::Function code)
1701 Location loc = this->location();
1703 // var val_temp TYPE
1704 Temporary_statement* val_temp = Statement::make_temporary(this->type_,
1705 NULL, loc);
1706 b->add_statement(val_temp);
1708 // ok = CODE(type_descriptor, expr, &val_temp)
1709 Expression* p1 = Expression::make_type_descriptor(this->type_, loc);
1710 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1711 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1712 Expression* call = Runtime::make_call(code, loc, 3, p1, this->expr_, p3);
1713 Statement* s = Statement::make_assignment(this->ok_, call, loc);
1714 b->add_statement(s);
1716 // val = val_temp
1717 ref = Expression::make_temporary_reference(val_temp, loc);
1718 s = Statement::make_assignment(this->val_, ref, loc);
1719 b->add_statement(s);
1722 // Dump the AST representation for a tuple type guard statement.
1724 void
1725 Tuple_type_guard_assignment_statement::do_dump_statement(
1726 Ast_dump_context* ast_dump_context) const
1728 ast_dump_context->print_indent();
1729 ast_dump_context->dump_expression(this->val_);
1730 ast_dump_context->ostream() << ", ";
1731 ast_dump_context->dump_expression(this->ok_);
1732 ast_dump_context->ostream() << " = ";
1733 ast_dump_context->dump_expression(this->expr_);
1734 ast_dump_context->ostream() << " . ";
1735 ast_dump_context->dump_type(this->type_);
1736 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1739 // Make an assignment from a type guard to a pair of variables.
1741 Statement*
1742 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
1743 Expression* expr, Type* type,
1744 Location location)
1746 return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
1747 location);
1750 // Class Expression_statement.
1752 // Constructor.
1754 Expression_statement::Expression_statement(Expression* expr, bool is_ignored)
1755 : Statement(STATEMENT_EXPRESSION, expr->location()),
1756 expr_(expr), is_ignored_(is_ignored)
1760 // Determine types.
1762 void
1763 Expression_statement::do_determine_types()
1765 this->expr_->determine_type_no_context();
1768 // Check the types of an expression statement. The only check we do
1769 // is to possibly give an error about discarding the value of the
1770 // expression.
1772 void
1773 Expression_statement::do_check_types(Gogo*)
1775 if (!this->is_ignored_)
1776 this->expr_->discarding_value();
1779 // An expression statement is only a terminating statement if it is
1780 // a call to panic.
1782 bool
1783 Expression_statement::do_may_fall_through() const
1785 const Call_expression* call = this->expr_->call_expression();
1786 if (call != NULL)
1788 const Expression* fn = call->fn();
1789 // panic is still an unknown named object.
1790 const Unknown_expression* ue = fn->unknown_expression();
1791 if (ue != NULL)
1793 Named_object* no = ue->named_object();
1795 if (no->is_unknown())
1796 no = no->unknown_value()->real_named_object();
1797 if (no != NULL)
1799 Function_type* fntype;
1800 if (no->is_function())
1801 fntype = no->func_value()->type();
1802 else if (no->is_function_declaration())
1803 fntype = no->func_declaration_value()->type();
1804 else
1805 fntype = NULL;
1807 // The builtin function panic does not return.
1808 if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
1809 return false;
1813 return true;
1816 // Convert to backend representation.
1818 Bstatement*
1819 Expression_statement::do_get_backend(Translate_context* context)
1821 Bexpression* bexpr = this->expr_->get_backend(context);
1822 Bfunction* bfunction = context->function()->func_value()->get_decl();
1823 return context->backend()->expression_statement(bfunction, bexpr);
1826 // Dump the AST representation for an expression statement
1828 void
1829 Expression_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
1830 const
1832 ast_dump_context->print_indent();
1833 ast_dump_context->dump_expression(expr_);
1834 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
1837 // Make an expression statement from an Expression.
1839 Statement*
1840 Statement::make_statement(Expression* expr, bool is_ignored)
1842 return new Expression_statement(expr, is_ignored);
1845 // Convert a block to the backend representation of a statement.
1847 Bstatement*
1848 Block_statement::do_get_backend(Translate_context* context)
1850 Bblock* bblock = this->block_->get_backend(context);
1851 return context->backend()->block_statement(bblock);
1854 // Dump the AST for a block statement
1856 void
1857 Block_statement::do_dump_statement(Ast_dump_context*) const
1859 // block statement braces are dumped when traversing.
1862 // Make a block statement.
1864 Statement*
1865 Statement::make_block_statement(Block* block, Location location)
1867 return new Block_statement(block, location);
1870 // An increment or decrement statement.
1872 class Inc_dec_statement : public Statement
1874 public:
1875 Inc_dec_statement(bool is_inc, Expression* expr)
1876 : Statement(STATEMENT_INCDEC, expr->location()),
1877 expr_(expr), is_inc_(is_inc)
1880 protected:
1882 do_traverse(Traverse* traverse)
1883 { return this->traverse_expression(traverse, &this->expr_); }
1885 bool
1886 do_traverse_assignments(Traverse_assignments*)
1887 { go_unreachable(); }
1889 Statement*
1890 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1892 Bstatement*
1893 do_get_backend(Translate_context*)
1894 { go_unreachable(); }
1896 void
1897 do_dump_statement(Ast_dump_context*) const;
1899 private:
1900 // The l-value to increment or decrement.
1901 Expression* expr_;
1902 // Whether to increment or decrement.
1903 bool is_inc_;
1906 // Lower to += or -=.
1908 Statement*
1909 Inc_dec_statement::do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
1911 Location loc = this->location();
1912 Expression* oexpr = Expression::make_integer_ul(1, this->expr_->type(), loc);
1913 Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
1914 return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
1917 // Dump the AST representation for a inc/dec statement.
1919 void
1920 Inc_dec_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
1922 ast_dump_context->print_indent();
1923 ast_dump_context->dump_expression(expr_);
1924 ast_dump_context->ostream() << (is_inc_? "++": "--") << dsuffix(location()) << std::endl;
1927 // Make an increment statement.
1929 Statement*
1930 Statement::make_inc_statement(Expression* expr)
1932 return new Inc_dec_statement(true, expr);
1935 // Make a decrement statement.
1937 Statement*
1938 Statement::make_dec_statement(Expression* expr)
1940 return new Inc_dec_statement(false, expr);
1943 // Class Thunk_statement. This is the base class for go and defer
1944 // statements.
1946 // Constructor.
1948 Thunk_statement::Thunk_statement(Statement_classification classification,
1949 Call_expression* call,
1950 Location location)
1951 : Statement(classification, location),
1952 call_(call), struct_type_(NULL)
1956 // Return whether this is a simple statement which does not require a
1957 // thunk.
1959 bool
1960 Thunk_statement::is_simple(Function_type* fntype) const
1962 // We need a thunk to call a method, or to pass a variable number of
1963 // arguments.
1964 if (fntype->is_method() || fntype->is_varargs())
1965 return false;
1967 // A defer statement requires a thunk to set up for whether the
1968 // function can call recover.
1969 if (this->classification() == STATEMENT_DEFER)
1970 return false;
1972 // We can only permit a single parameter of pointer type.
1973 const Typed_identifier_list* parameters = fntype->parameters();
1974 if (parameters != NULL
1975 && (parameters->size() > 1
1976 || (parameters->size() == 1
1977 && parameters->begin()->type()->points_to() == NULL)))
1978 return false;
1980 // If the function returns multiple values, or returns a type other
1981 // than integer, floating point, or pointer, then it may get a
1982 // hidden first parameter, in which case we need the more
1983 // complicated approach. This is true even though we are going to
1984 // ignore the return value.
1985 const Typed_identifier_list* results = fntype->results();
1986 if (results != NULL
1987 && (results->size() > 1
1988 || (results->size() == 1
1989 && !results->begin()->type()->is_basic_type()
1990 && results->begin()->type()->points_to() == NULL)))
1991 return false;
1993 // If this calls something that is not a simple function, then we
1994 // need a thunk.
1995 Expression* fn = this->call_->call_expression()->fn();
1996 if (fn->func_expression() == NULL)
1997 return false;
1999 // If the function uses a closure, then we need a thunk. FIXME: We
2000 // could accept a zero argument function with a closure.
2001 if (fn->func_expression()->closure() != NULL)
2002 return false;
2004 return true;
2007 // Traverse a thunk statement.
2010 Thunk_statement::do_traverse(Traverse* traverse)
2012 return this->traverse_expression(traverse, &this->call_);
2015 // We implement traverse_assignment for a thunk statement because it
2016 // effectively copies the function call.
2018 bool
2019 Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
2021 Expression* fn = this->call_->call_expression()->fn();
2022 Expression* fn2 = fn;
2023 tassign->value(&fn2, true, false);
2024 return true;
2027 // Determine types in a thunk statement.
2029 void
2030 Thunk_statement::do_determine_types()
2032 this->call_->determine_type_no_context();
2034 // Now that we know the types of the call, build the struct used to
2035 // pass parameters.
2036 Call_expression* ce = this->call_->call_expression();
2037 if (ce == NULL)
2038 return;
2039 Function_type* fntype = ce->get_function_type();
2040 if (fntype != NULL && !this->is_simple(fntype))
2041 this->struct_type_ = this->build_struct(fntype);
2044 // Check types in a thunk statement.
2046 void
2047 Thunk_statement::do_check_types(Gogo*)
2049 if (!this->call_->discarding_value())
2050 return;
2051 Call_expression* ce = this->call_->call_expression();
2052 if (ce == NULL)
2054 if (!this->call_->is_error_expression())
2055 this->report_error("expected call expression");
2056 return;
2060 // The Traverse class used to find and simplify thunk statements.
2062 class Simplify_thunk_traverse : public Traverse
2064 public:
2065 Simplify_thunk_traverse(Gogo* gogo)
2066 : Traverse(traverse_functions | traverse_blocks),
2067 gogo_(gogo), function_(NULL)
2071 function(Named_object*);
2074 block(Block*);
2076 private:
2077 // General IR.
2078 Gogo* gogo_;
2079 // The function we are traversing.
2080 Named_object* function_;
2083 // Keep track of the current function while looking for thunks.
2086 Simplify_thunk_traverse::function(Named_object* no)
2088 go_assert(this->function_ == NULL);
2089 this->function_ = no;
2090 int t = no->func_value()->traverse(this);
2091 this->function_ = NULL;
2092 if (t == TRAVERSE_EXIT)
2093 return t;
2094 return TRAVERSE_SKIP_COMPONENTS;
2097 // Look for thunks in a block.
2100 Simplify_thunk_traverse::block(Block* b)
2102 // The parser ensures that thunk statements always appear at the end
2103 // of a block.
2104 if (b->statements()->size() < 1)
2105 return TRAVERSE_CONTINUE;
2106 Thunk_statement* stat = b->statements()->back()->thunk_statement();
2107 if (stat == NULL)
2108 return TRAVERSE_CONTINUE;
2109 if (stat->simplify_statement(this->gogo_, this->function_, b))
2110 return TRAVERSE_SKIP_COMPONENTS;
2111 return TRAVERSE_CONTINUE;
2114 // Simplify all thunk statements.
2116 void
2117 Gogo::simplify_thunk_statements()
2119 Simplify_thunk_traverse thunk_traverse(this);
2120 this->traverse(&thunk_traverse);
2123 // Return true if the thunk function is a constant, which means that
2124 // it does not need to be passed to the thunk routine.
2126 bool
2127 Thunk_statement::is_constant_function() const
2129 Call_expression* ce = this->call_->call_expression();
2130 Function_type* fntype = ce->get_function_type();
2131 if (fntype == NULL)
2133 go_assert(saw_errors());
2134 return false;
2136 if (fntype->is_builtin())
2137 return true;
2138 Expression* fn = ce->fn();
2139 if (fn->func_expression() != NULL)
2140 return fn->func_expression()->closure() == NULL;
2141 if (fn->interface_field_reference_expression() != NULL)
2142 return true;
2143 return false;
2146 // Simplify complex thunk statements into simple ones. A complicated
2147 // thunk statement is one which takes anything other than zero
2148 // parameters or a single pointer parameter. We rewrite it into code
2149 // which allocates a struct, stores the parameter values into the
2150 // struct, and does a simple go or defer statement which passes the
2151 // struct to a thunk. The thunk does the real call.
2153 bool
2154 Thunk_statement::simplify_statement(Gogo* gogo, Named_object* function,
2155 Block* block)
2157 if (this->classification() == STATEMENT_ERROR)
2158 return false;
2159 if (this->call_->is_error_expression())
2160 return false;
2162 if (this->classification() == STATEMENT_DEFER)
2164 // Make sure that the defer stack exists for the function. We
2165 // will use when converting this statement to the backend
2166 // representation, but we want it to exist when we start
2167 // converting the function.
2168 function->func_value()->defer_stack(this->location());
2171 Call_expression* ce = this->call_->call_expression();
2172 Function_type* fntype = ce->get_function_type();
2173 if (fntype == NULL)
2175 go_assert(saw_errors());
2176 this->set_is_error();
2177 return false;
2179 if (this->is_simple(fntype))
2180 return false;
2182 Expression* fn = ce->fn();
2183 Interface_field_reference_expression* interface_method =
2184 fn->interface_field_reference_expression();
2186 Location location = this->location();
2188 std::string thunk_name = Gogo::thunk_name();
2190 // Build the thunk.
2191 this->build_thunk(gogo, thunk_name);
2193 // Generate code to call the thunk.
2195 // Get the values to store into the struct which is the single
2196 // argument to the thunk.
2198 Expression_list* vals = new Expression_list();
2199 if (!this->is_constant_function())
2200 vals->push_back(fn);
2202 if (interface_method != NULL)
2203 vals->push_back(interface_method->expr());
2205 if (ce->args() != NULL)
2207 for (Expression_list::const_iterator p = ce->args()->begin();
2208 p != ce->args()->end();
2209 ++p)
2211 if ((*p)->is_constant())
2212 continue;
2213 vals->push_back(*p);
2217 // Build the struct.
2218 Expression* constructor =
2219 Expression::make_struct_composite_literal(this->struct_type_, vals,
2220 location);
2222 // Allocate the initialized struct on the heap.
2223 constructor = Expression::make_heap_expression(constructor, location);
2225 // Look up the thunk.
2226 Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
2227 go_assert(named_thunk != NULL && named_thunk->is_function());
2229 // Build the call.
2230 Expression* func = Expression::make_func_reference(named_thunk, NULL,
2231 location);
2232 Expression_list* params = new Expression_list();
2233 params->push_back(constructor);
2234 Call_expression* call = Expression::make_call(func, params, false, location);
2236 // Build the simple go or defer statement.
2237 Statement* s;
2238 if (this->classification() == STATEMENT_GO)
2239 s = Statement::make_go_statement(call, location);
2240 else if (this->classification() == STATEMENT_DEFER)
2241 s = Statement::make_defer_statement(call, location);
2242 else
2243 go_unreachable();
2245 // The current block should end with the go statement.
2246 go_assert(block->statements()->size() >= 1);
2247 go_assert(block->statements()->back() == this);
2248 block->replace_statement(block->statements()->size() - 1, s);
2250 // We already ran the determine_types pass, so we need to run it now
2251 // for the new statement.
2252 s->determine_types();
2254 // Sanity check.
2255 gogo->check_types_in_block(block);
2257 // Return true to tell the block not to keep looking at statements.
2258 return true;
2261 // Set the name to use for thunk parameter N.
2263 void
2264 Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
2266 snprintf(buf, buflen, "a%d", n);
2269 // Build a new struct type to hold the parameters for a complicated
2270 // thunk statement. FNTYPE is the type of the function call.
2272 Struct_type*
2273 Thunk_statement::build_struct(Function_type* fntype)
2275 Location location = this->location();
2277 Struct_field_list* fields = new Struct_field_list();
2279 Call_expression* ce = this->call_->call_expression();
2280 Expression* fn = ce->fn();
2282 if (!this->is_constant_function())
2284 // The function to call.
2285 fields->push_back(Struct_field(Typed_identifier("fn", fntype,
2286 location)));
2289 // If this thunk statement calls a method on an interface, we pass
2290 // the interface object to the thunk.
2291 Interface_field_reference_expression* interface_method =
2292 fn->interface_field_reference_expression();
2293 if (interface_method != NULL)
2295 Typed_identifier tid("object", interface_method->expr()->type(),
2296 location);
2297 fields->push_back(Struct_field(tid));
2300 // The predeclared recover function has no argument. However, we
2301 // add an argument when building recover thunks. Handle that here.
2302 if (ce->is_recover_call())
2304 fields->push_back(Struct_field(Typed_identifier("can_recover",
2305 Type::lookup_bool_type(),
2306 location)));
2309 const Expression_list* args = ce->args();
2310 if (args != NULL)
2312 int i = 0;
2313 for (Expression_list::const_iterator p = args->begin();
2314 p != args->end();
2315 ++p, ++i)
2317 if ((*p)->is_constant())
2318 continue;
2320 char buf[50];
2321 this->thunk_field_param(i, buf, sizeof buf);
2322 fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
2323 location)));
2327 Struct_type *st = Type::make_struct_type(fields, location);
2328 st->set_is_struct_incomparable();
2329 return st;
2332 // Build the thunk we are going to call. This is a brand new, albeit
2333 // artificial, function.
2335 void
2336 Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name)
2338 Location location = this->location();
2340 Call_expression* ce = this->call_->call_expression();
2342 bool may_call_recover = false;
2343 if (this->classification() == STATEMENT_DEFER)
2345 Func_expression* fn = ce->fn()->func_expression();
2346 if (fn == NULL)
2347 may_call_recover = true;
2348 else
2350 const Named_object* no = fn->named_object();
2351 if (!no->is_function())
2352 may_call_recover = true;
2353 else
2354 may_call_recover = no->func_value()->calls_recover();
2358 // Build the type of the thunk. The thunk takes a single parameter,
2359 // which is a pointer to the special structure we build.
2360 const char* const parameter_name = "__go_thunk_parameter";
2361 Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
2362 Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
2363 thunk_parameters->push_back(Typed_identifier(parameter_name,
2364 pointer_to_struct_type,
2365 location));
2367 Typed_identifier_list* thunk_results = NULL;
2368 if (may_call_recover)
2370 // When deferring a function which may call recover, add a
2371 // return value, to disable tail call optimizations which will
2372 // break the way we check whether recover is permitted.
2373 thunk_results = new Typed_identifier_list();
2374 thunk_results->push_back(Typed_identifier("", Type::lookup_bool_type(),
2375 location));
2378 Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
2379 thunk_results,
2380 location);
2382 // Start building the thunk.
2383 Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
2384 location);
2386 gogo->start_block(location);
2388 // For a defer statement, start with a call to
2389 // __go_set_defer_retaddr. */
2390 Label* retaddr_label = NULL;
2391 if (may_call_recover)
2393 retaddr_label = gogo->add_label_reference("retaddr", location, false);
2394 Expression* arg = Expression::make_label_addr(retaddr_label, location);
2395 Expression* call = Runtime::make_call(Runtime::SETDEFERRETADDR,
2396 location, 1, arg);
2398 // This is a hack to prevent the middle-end from deleting the
2399 // label.
2400 gogo->start_block(location);
2401 gogo->add_statement(Statement::make_goto_statement(retaddr_label,
2402 location));
2403 Block* then_block = gogo->finish_block(location);
2404 then_block->determine_types();
2406 Statement* s = Statement::make_if_statement(call, then_block, NULL,
2407 location);
2408 s->determine_types();
2409 gogo->add_statement(s);
2411 function->func_value()->set_calls_defer_retaddr();
2414 // Get a reference to the parameter.
2415 Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
2416 go_assert(named_parameter != NULL && named_parameter->is_variable());
2418 // Build the call. Note that the field names are the same as the
2419 // ones used in build_struct.
2420 Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
2421 location);
2422 thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
2423 location);
2425 Interface_field_reference_expression* interface_method =
2426 ce->fn()->interface_field_reference_expression();
2428 Expression* func_to_call;
2429 unsigned int next_index;
2430 if (this->is_constant_function())
2432 func_to_call = ce->fn();
2433 next_index = 0;
2435 else
2437 func_to_call = Expression::make_field_reference(thunk_parameter,
2438 0, location);
2439 next_index = 1;
2442 if (interface_method != NULL)
2444 // The main program passes the interface object.
2445 go_assert(next_index == 0);
2446 Expression* r = Expression::make_field_reference(thunk_parameter, 0,
2447 location);
2448 const std::string& name(interface_method->name());
2449 func_to_call = Expression::make_interface_field_reference(r, name,
2450 location);
2451 next_index = 1;
2454 Expression_list* call_params = new Expression_list();
2455 const Struct_field_list* fields = this->struct_type_->fields();
2456 Struct_field_list::const_iterator p = fields->begin();
2457 for (unsigned int i = 0; i < next_index; ++i)
2458 ++p;
2459 bool is_recover_call = ce->is_recover_call();
2460 Expression* recover_arg = NULL;
2462 const Expression_list* args = ce->args();
2463 if (args != NULL)
2465 for (Expression_list::const_iterator arg = args->begin();
2466 arg != args->end();
2467 ++arg)
2469 Expression* param;
2470 if ((*arg)->is_constant())
2471 param = *arg;
2472 else
2474 Expression* thunk_param =
2475 Expression::make_var_reference(named_parameter, location);
2476 thunk_param =
2477 Expression::make_unary(OPERATOR_MULT, thunk_param, location);
2478 param = Expression::make_field_reference(thunk_param,
2479 next_index,
2480 location);
2481 ++next_index;
2484 if (!is_recover_call)
2485 call_params->push_back(param);
2486 else
2488 go_assert(call_params->empty());
2489 recover_arg = param;
2494 if (call_params->empty())
2496 delete call_params;
2497 call_params = NULL;
2500 Call_expression* call = Expression::make_call(func_to_call, call_params,
2501 false, location);
2503 // This call expression was already lowered before entering the
2504 // thunk statement. Don't try to lower varargs again, as that will
2505 // cause confusion for, e.g., method calls which already have a
2506 // receiver parameter.
2507 call->set_varargs_are_lowered();
2509 Statement* call_statement = Statement::make_statement(call, true);
2511 gogo->add_statement(call_statement);
2513 // If this is a defer statement, the label comes immediately after
2514 // the call.
2515 if (may_call_recover)
2517 gogo->add_label_definition("retaddr", location);
2519 Expression_list* vals = new Expression_list();
2520 vals->push_back(Expression::make_boolean(false, location));
2521 gogo->add_statement(Statement::make_return_statement(vals, location));
2524 Block* b = gogo->finish_block(location);
2526 gogo->add_block(b, location);
2528 gogo->lower_block(function, b);
2530 // We already ran the determine_types pass, so we need to run it
2531 // just for the call statement now. The other types are known.
2532 call_statement->determine_types();
2534 gogo->flatten_block(function, b);
2536 if (may_call_recover
2537 || recover_arg != NULL
2538 || this->classification() == STATEMENT_GO)
2540 // Dig up the call expression, which may have been changed
2541 // during lowering.
2542 go_assert(call_statement->classification() == STATEMENT_EXPRESSION);
2543 Expression_statement* es =
2544 static_cast<Expression_statement*>(call_statement);
2545 Call_expression* ce = es->expr()->call_expression();
2546 if (ce == NULL)
2547 go_assert(saw_errors());
2548 else
2550 if (may_call_recover)
2551 ce->set_is_deferred();
2552 if (this->classification() == STATEMENT_GO)
2553 ce->set_is_concurrent();
2554 if (recover_arg != NULL)
2555 ce->set_recover_arg(recover_arg);
2559 // That is all the thunk has to do.
2560 gogo->finish_function(location);
2563 // Get the function and argument expressions.
2565 bool
2566 Thunk_statement::get_fn_and_arg(Expression** pfn, Expression** parg)
2568 if (this->call_->is_error_expression())
2569 return false;
2571 Call_expression* ce = this->call_->call_expression();
2573 Expression* fn = ce->fn();
2574 Func_expression* fe = fn->func_expression();
2575 go_assert(fe != NULL);
2576 *pfn = Expression::make_func_code_reference(fe->named_object(),
2577 fe->location());
2579 const Expression_list* args = ce->args();
2580 if (args == NULL || args->empty())
2581 *parg = Expression::make_nil(this->location());
2582 else
2584 go_assert(args->size() == 1);
2585 *parg = args->front();
2588 return true;
2591 // Class Go_statement.
2593 Bstatement*
2594 Go_statement::do_get_backend(Translate_context* context)
2596 Expression* fn;
2597 Expression* arg;
2598 if (!this->get_fn_and_arg(&fn, &arg))
2599 return context->backend()->error_statement();
2601 Expression* call = Runtime::make_call(Runtime::GO, this->location(), 2,
2602 fn, arg);
2603 Bexpression* bcall = call->get_backend(context);
2604 Bfunction* bfunction = context->function()->func_value()->get_decl();
2605 return context->backend()->expression_statement(bfunction, bcall);
2608 // Dump the AST representation for go statement.
2610 void
2611 Go_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2613 ast_dump_context->print_indent();
2614 ast_dump_context->ostream() << "go ";
2615 ast_dump_context->dump_expression(this->call());
2616 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
2619 // Make a go statement.
2621 Statement*
2622 Statement::make_go_statement(Call_expression* call, Location location)
2624 return new Go_statement(call, location);
2627 // Class Defer_statement.
2629 Bstatement*
2630 Defer_statement::do_get_backend(Translate_context* context)
2632 Expression* fn;
2633 Expression* arg;
2634 if (!this->get_fn_and_arg(&fn, &arg))
2635 return context->backend()->error_statement();
2637 Location loc = this->location();
2638 Expression* ds = context->function()->func_value()->defer_stack(loc);
2640 Expression* call = Runtime::make_call(Runtime::DEFERPROC, loc, 3,
2641 ds, fn, arg);
2642 Bexpression* bcall = call->get_backend(context);
2643 Bfunction* bfunction = context->function()->func_value()->get_decl();
2644 return context->backend()->expression_statement(bfunction, bcall);
2647 // Dump the AST representation for defer statement.
2649 void
2650 Defer_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2652 ast_dump_context->print_indent();
2653 ast_dump_context->ostream() << "defer ";
2654 ast_dump_context->dump_expression(this->call());
2655 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
2658 // Make a defer statement.
2660 Statement*
2661 Statement::make_defer_statement(Call_expression* call,
2662 Location location)
2664 return new Defer_statement(call, location);
2667 // Class Return_statement.
2669 // Traverse assignments. We treat each return value as a top level
2670 // RHS in an expression.
2672 bool
2673 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2675 Expression_list* vals = this->vals_;
2676 if (vals != NULL)
2678 for (Expression_list::iterator p = vals->begin();
2679 p != vals->end();
2680 ++p)
2681 tassign->value(&*p, true, true);
2683 return true;
2686 // Lower a return statement. If we are returning a function call
2687 // which returns multiple values which match the current function,
2688 // split up the call's results. If the return statement lists
2689 // explicit values, implement this statement by assigning the values
2690 // to the result variables and change this statement to a naked
2691 // return. This lets panic/recover work correctly.
2693 Statement*
2694 Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
2695 Statement_inserter*)
2697 if (this->is_lowered_)
2698 return this;
2700 Expression_list* vals = this->vals_;
2701 this->vals_ = NULL;
2702 this->is_lowered_ = true;
2704 Location loc = this->location();
2706 size_t vals_count = vals == NULL ? 0 : vals->size();
2707 Function::Results* results = function->func_value()->result_variables();
2708 size_t results_count = results == NULL ? 0 : results->size();
2710 if (vals_count == 0)
2712 if (results_count > 0 && !function->func_value()->results_are_named())
2714 this->report_error(_("not enough arguments to return"));
2715 return this;
2717 return this;
2720 if (results_count == 0)
2722 this->report_error(_("return with value in function "
2723 "with no return type"));
2724 return this;
2727 // If the current function has multiple return values, and we are
2728 // returning a single call expression, split up the call expression.
2729 if (results_count > 1
2730 && vals->size() == 1
2731 && vals->front()->call_expression() != NULL)
2733 Call_expression* call = vals->front()->call_expression();
2734 call->set_expected_result_count(results_count);
2735 delete vals;
2736 vals = new Expression_list;
2737 for (size_t i = 0; i < results_count; ++i)
2738 vals->push_back(Expression::make_call_result(call, i));
2739 vals_count = results_count;
2742 if (vals_count < results_count)
2744 this->report_error(_("not enough arguments to return"));
2745 return this;
2748 if (vals_count > results_count)
2750 this->report_error(_("too many values in return statement"));
2751 return this;
2754 Block* b = new Block(enclosing, loc);
2756 Expression_list* lhs = new Expression_list();
2757 Expression_list* rhs = new Expression_list();
2759 Expression_list::const_iterator pe = vals->begin();
2760 int i = 1;
2761 for (Function::Results::const_iterator pr = results->begin();
2762 pr != results->end();
2763 ++pr, ++pe, ++i)
2765 Named_object* rv = *pr;
2766 Expression* e = *pe;
2768 // Check types now so that we give a good error message. The
2769 // result type is known. We determine the expression type
2770 // early.
2772 Type *rvtype = rv->result_var_value()->type();
2773 Type_context type_context(rvtype, false);
2774 e->determine_type(&type_context);
2776 std::string reason;
2777 if (Type::are_assignable(rvtype, e->type(), &reason))
2779 Expression* ve = Expression::make_var_reference(rv, e->location());
2780 lhs->push_back(ve);
2781 rhs->push_back(e);
2783 else
2785 if (reason.empty())
2786 go_error_at(e->location(),
2787 "incompatible type for return value %d", i);
2788 else
2789 go_error_at(e->location(),
2790 "incompatible type for return value %d (%s)",
2791 i, reason.c_str());
2794 go_assert(lhs->size() == rhs->size());
2796 if (lhs->empty())
2798 else if (lhs->size() == 1)
2800 b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
2801 loc));
2802 delete lhs;
2803 delete rhs;
2805 else
2806 b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
2808 b->add_statement(this);
2810 delete vals;
2812 return Statement::make_block_statement(b, loc);
2815 // Convert a return statement to the backend representation.
2817 Bstatement*
2818 Return_statement::do_get_backend(Translate_context* context)
2820 Location loc = this->location();
2822 Function* function = context->function()->func_value();
2823 Function::Results* results = function->result_variables();
2824 std::vector<Bexpression*> retvals;
2825 if (results != NULL && !results->empty())
2827 retvals.reserve(results->size());
2828 for (Function::Results::const_iterator p = results->begin();
2829 p != results->end();
2830 p++)
2832 Expression* vr = Expression::make_var_reference(*p, loc);
2833 retvals.push_back(vr->get_backend(context));
2837 return context->backend()->return_statement(function->get_decl(),
2838 retvals, loc);
2841 // Dump the AST representation for a return statement.
2843 void
2844 Return_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2846 ast_dump_context->print_indent();
2847 ast_dump_context->ostream() << "return " ;
2848 ast_dump_context->dump_expression_list(this->vals_);
2849 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
2852 // Make a return statement.
2854 Return_statement*
2855 Statement::make_return_statement(Expression_list* vals,
2856 Location location)
2858 return new Return_statement(vals, location);
2861 // Make a statement that returns the result of a call expression.
2863 Statement*
2864 Statement::make_return_from_call(Call_expression* call, Location location)
2866 size_t rc = call->result_count();
2867 if (rc == 0)
2868 return Statement::make_statement(call, true);
2869 else
2871 Expression_list* vals = new Expression_list();
2872 if (rc == 1)
2873 vals->push_back(call);
2874 else
2876 for (size_t i = 0; i < rc; ++i)
2877 vals->push_back(Expression::make_call_result(call, i));
2879 return Statement::make_return_statement(vals, location);
2883 // A break or continue statement.
2885 class Bc_statement : public Statement
2887 public:
2888 Bc_statement(bool is_break, Unnamed_label* label, Location location)
2889 : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
2890 label_(label), is_break_(is_break)
2893 bool
2894 is_break() const
2895 { return this->is_break_; }
2897 protected:
2899 do_traverse(Traverse*)
2900 { return TRAVERSE_CONTINUE; }
2902 bool
2903 do_may_fall_through() const
2904 { return false; }
2906 Bstatement*
2907 do_get_backend(Translate_context* context)
2908 { return this->label_->get_goto(context, this->location()); }
2910 void
2911 do_dump_statement(Ast_dump_context*) const;
2913 private:
2914 // The label that this branches to.
2915 Unnamed_label* label_;
2916 // True if this is "break", false if it is "continue".
2917 bool is_break_;
2920 // Dump the AST representation for a break/continue statement
2922 void
2923 Bc_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2925 ast_dump_context->print_indent();
2926 ast_dump_context->ostream() << (this->is_break_ ? "break" : "continue");
2927 if (this->label_ != NULL)
2929 ast_dump_context->ostream() << " ";
2930 ast_dump_context->dump_label_name(this->label_);
2932 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
2935 // Make a break statement.
2937 Statement*
2938 Statement::make_break_statement(Unnamed_label* label, Location location)
2940 return new Bc_statement(true, label, location);
2943 // Make a continue statement.
2945 Statement*
2946 Statement::make_continue_statement(Unnamed_label* label,
2947 Location location)
2949 return new Bc_statement(false, label, location);
2952 // Class Goto_statement.
2955 Goto_statement::do_traverse(Traverse*)
2957 return TRAVERSE_CONTINUE;
2960 // Check types for a label. There aren't any types per se, but we use
2961 // this to give an error if the label was never defined.
2963 void
2964 Goto_statement::do_check_types(Gogo*)
2966 if (!this->label_->is_defined())
2968 go_error_at(this->location(), "reference to undefined label %qs",
2969 Gogo::message_name(this->label_->name()).c_str());
2970 this->set_is_error();
2974 // Convert the goto statement to the backend representation.
2976 Bstatement*
2977 Goto_statement::do_get_backend(Translate_context* context)
2979 Blabel* blabel = this->label_->get_backend_label(context);
2980 return context->backend()->goto_statement(blabel, this->location());
2983 // Dump the AST representation for a goto statement.
2985 void
2986 Goto_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
2988 ast_dump_context->print_indent();
2989 ast_dump_context->ostream() << "goto " << this->label_->name() << dsuffix(location()) << std::endl;
2992 // Make a goto statement.
2994 Statement*
2995 Statement::make_goto_statement(Label* label, Location location)
2997 return new Goto_statement(label, location);
3000 // Class Goto_unnamed_statement.
3003 Goto_unnamed_statement::do_traverse(Traverse*)
3005 return TRAVERSE_CONTINUE;
3008 // Convert the goto unnamed statement to the backend representation.
3010 Bstatement*
3011 Goto_unnamed_statement::do_get_backend(Translate_context* context)
3013 return this->label_->get_goto(context, this->location());
3016 // Dump the AST representation for an unnamed goto statement
3018 void
3019 Goto_unnamed_statement::do_dump_statement(
3020 Ast_dump_context* ast_dump_context) const
3022 ast_dump_context->print_indent();
3023 ast_dump_context->ostream() << "goto ";
3024 ast_dump_context->dump_label_name(this->label_);
3025 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
3028 // Make a goto statement to an unnamed label.
3030 Statement*
3031 Statement::make_goto_unnamed_statement(Unnamed_label* label,
3032 Location location)
3034 return new Goto_unnamed_statement(label, location);
3037 // Class Label_statement.
3039 // Traversal.
3042 Label_statement::do_traverse(Traverse*)
3044 return TRAVERSE_CONTINUE;
3047 // Return the backend representation of the statement defining this
3048 // label.
3050 Bstatement*
3051 Label_statement::do_get_backend(Translate_context* context)
3053 if (this->label_->is_dummy_label())
3055 Bexpression* bce = context->backend()->boolean_constant_expression(false);
3056 Bfunction* bfunction = context->function()->func_value()->get_decl();
3057 return context->backend()->expression_statement(bfunction, bce);
3059 Blabel* blabel = this->label_->get_backend_label(context);
3060 return context->backend()->label_definition_statement(blabel);
3063 // Dump the AST for a label definition statement.
3065 void
3066 Label_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3068 ast_dump_context->print_indent();
3069 ast_dump_context->ostream() << this->label_->name() << ":" << dsuffix(location()) << std::endl;
3072 // Make a label statement.
3074 Statement*
3075 Statement::make_label_statement(Label* label, Location location)
3077 return new Label_statement(label, location);
3080 // Class Unnamed_label_statement.
3082 Unnamed_label_statement::Unnamed_label_statement(Unnamed_label* label)
3083 : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
3084 label_(label)
3088 Unnamed_label_statement::do_traverse(Traverse*)
3090 return TRAVERSE_CONTINUE;
3093 // Get the backend definition for this unnamed label statement.
3095 Bstatement*
3096 Unnamed_label_statement::do_get_backend(Translate_context* context)
3098 return this->label_->get_definition(context);
3101 // Dump the AST representation for an unnamed label definition statement.
3103 void
3104 Unnamed_label_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
3105 const
3107 ast_dump_context->print_indent();
3108 ast_dump_context->dump_label_name(this->label_);
3109 ast_dump_context->ostream() << ":" << dsuffix(location()) << std::endl;
3112 // Make an unnamed label statement.
3114 Statement*
3115 Statement::make_unnamed_label_statement(Unnamed_label* label)
3117 return new Unnamed_label_statement(label);
3120 // Class If_statement.
3122 // Traversal.
3125 If_statement::do_traverse(Traverse* traverse)
3127 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT
3128 || this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
3129 return TRAVERSE_EXIT;
3130 if (this->else_block_ != NULL)
3132 if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
3133 return TRAVERSE_EXIT;
3135 return TRAVERSE_CONTINUE;
3138 void
3139 If_statement::do_determine_types()
3141 Type_context context(Type::lookup_bool_type(), false);
3142 this->cond_->determine_type(&context);
3143 this->then_block_->determine_types();
3144 if (this->else_block_ != NULL)
3145 this->else_block_->determine_types();
3148 // Check types.
3150 void
3151 If_statement::do_check_types(Gogo*)
3153 Type* type = this->cond_->type();
3154 if (type->is_error())
3155 this->set_is_error();
3156 else if (!type->is_boolean_type())
3157 this->report_error(_("expected boolean expression"));
3160 // Whether the overall statement may fall through.
3162 bool
3163 If_statement::do_may_fall_through() const
3165 return (this->else_block_ == NULL
3166 || this->then_block_->may_fall_through()
3167 || this->else_block_->may_fall_through());
3170 // Get the backend representation.
3172 Bstatement*
3173 If_statement::do_get_backend(Translate_context* context)
3175 go_assert(this->cond_->type()->is_boolean_type()
3176 || this->cond_->type()->is_error());
3177 Bexpression* cond = this->cond_->get_backend(context);
3178 Bblock* then_block = this->then_block_->get_backend(context);
3179 Bblock* else_block = (this->else_block_ == NULL
3180 ? NULL
3181 : this->else_block_->get_backend(context));
3182 Bfunction* bfunction = context->function()->func_value()->get_decl();
3183 return context->backend()->if_statement(bfunction,
3184 cond, then_block, else_block,
3185 this->location());
3188 // Dump the AST representation for an if statement
3190 void
3191 If_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3193 ast_dump_context->print_indent();
3194 ast_dump_context->ostream() << "if ";
3195 ast_dump_context->dump_expression(this->cond_);
3196 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
3197 if (ast_dump_context->dump_subblocks())
3199 ast_dump_context->dump_block(this->then_block_);
3200 if (this->else_block_ != NULL)
3202 ast_dump_context->print_indent();
3203 ast_dump_context->ostream() << "else" << std::endl;
3204 ast_dump_context->dump_block(this->else_block_);
3209 // Make an if statement.
3211 Statement*
3212 Statement::make_if_statement(Expression* cond, Block* then_block,
3213 Block* else_block, Location location)
3215 return new If_statement(cond, then_block, else_block, location);
3218 // Class Case_clauses::Hash_integer_value.
3220 class Case_clauses::Hash_integer_value
3222 public:
3223 size_t
3224 operator()(Expression*) const;
3227 size_t
3228 Case_clauses::Hash_integer_value::operator()(Expression* pe) const
3230 Numeric_constant nc;
3231 mpz_t ival;
3232 if (!pe->numeric_constant_value(&nc) || !nc.to_int(&ival))
3233 go_unreachable();
3234 size_t ret = mpz_get_ui(ival);
3235 mpz_clear(ival);
3236 return ret;
3239 // Class Case_clauses::Eq_integer_value.
3241 class Case_clauses::Eq_integer_value
3243 public:
3244 bool
3245 operator()(Expression*, Expression*) const;
3248 bool
3249 Case_clauses::Eq_integer_value::operator()(Expression* a, Expression* b) const
3251 Numeric_constant anc;
3252 mpz_t aval;
3253 Numeric_constant bnc;
3254 mpz_t bval;
3255 if (!a->numeric_constant_value(&anc)
3256 || !anc.to_int(&aval)
3257 || !b->numeric_constant_value(&bnc)
3258 || !bnc.to_int(&bval))
3259 go_unreachable();
3260 bool ret = mpz_cmp(aval, bval) == 0;
3261 mpz_clear(aval);
3262 mpz_clear(bval);
3263 return ret;
3266 // Class Case_clauses::Case_clause.
3268 // Traversal.
3271 Case_clauses::Case_clause::traverse(Traverse* traverse)
3273 if (this->cases_ != NULL
3274 && (traverse->traverse_mask()
3275 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3277 if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
3278 return TRAVERSE_EXIT;
3280 if (this->statements_ != NULL)
3282 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3283 return TRAVERSE_EXIT;
3285 return TRAVERSE_CONTINUE;
3288 // Check whether all the case expressions are integer constants.
3290 bool
3291 Case_clauses::Case_clause::is_constant() const
3293 if (this->cases_ != NULL)
3295 for (Expression_list::const_iterator p = this->cases_->begin();
3296 p != this->cases_->end();
3297 ++p)
3298 if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
3299 return false;
3301 return true;
3304 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3305 // value we are switching on; it may be NULL. If START_LABEL is not
3306 // NULL, it goes at the start of the statements, after the condition
3307 // test. We branch to FINISH_LABEL at the end of the statements.
3309 void
3310 Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
3311 Unnamed_label* start_label,
3312 Unnamed_label* finish_label) const
3314 Location loc = this->location_;
3315 Unnamed_label* next_case_label;
3316 if (this->cases_ == NULL || this->cases_->empty())
3318 go_assert(this->is_default_);
3319 next_case_label = NULL;
3321 else
3323 Expression* cond = NULL;
3325 for (Expression_list::const_iterator p = this->cases_->begin();
3326 p != this->cases_->end();
3327 ++p)
3329 Expression* ref = Expression::make_temporary_reference(val_temp,
3330 loc);
3331 Expression* this_cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3332 *p, loc);
3333 if (cond == NULL)
3334 cond = this_cond;
3335 else
3336 cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
3339 Block* then_block = new Block(b, loc);
3340 next_case_label = new Unnamed_label(Linemap::unknown_location());
3341 Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
3342 loc);
3343 then_block->add_statement(s);
3345 // if !COND { goto NEXT_CASE_LABEL }
3346 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3347 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3348 b->add_statement(s);
3351 if (start_label != NULL)
3352 b->add_statement(Statement::make_unnamed_label_statement(start_label));
3354 if (this->statements_ != NULL)
3355 b->add_statement(Statement::make_block_statement(this->statements_, loc));
3357 Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
3358 b->add_statement(s);
3360 if (next_case_label != NULL)
3361 b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
3364 // Determine types.
3366 void
3367 Case_clauses::Case_clause::determine_types(Type* type)
3369 if (this->cases_ != NULL)
3371 Type_context case_context(type, false);
3372 for (Expression_list::iterator p = this->cases_->begin();
3373 p != this->cases_->end();
3374 ++p)
3375 (*p)->determine_type(&case_context);
3377 if (this->statements_ != NULL)
3378 this->statements_->determine_types();
3381 // Check types. Returns false if there was an error.
3383 bool
3384 Case_clauses::Case_clause::check_types(Type* type)
3386 if (this->cases_ != NULL)
3388 for (Expression_list::iterator p = this->cases_->begin();
3389 p != this->cases_->end();
3390 ++p)
3392 if (!Type::are_assignable(type, (*p)->type(), NULL)
3393 && !Type::are_assignable((*p)->type(), type, NULL))
3395 go_error_at((*p)->location(),
3396 "type mismatch between switch value and case clause");
3397 return false;
3401 return true;
3404 // Return true if this clause may fall through to the following
3405 // statements. Note that this is not the same as whether the case
3406 // uses the "fallthrough" keyword.
3408 bool
3409 Case_clauses::Case_clause::may_fall_through() const
3411 if (this->statements_ == NULL)
3412 return true;
3413 return this->statements_->may_fall_through();
3416 // Convert the case values and statements to the backend
3417 // representation. BREAK_LABEL is the label which break statements
3418 // should branch to. CASE_CONSTANTS is used to detect duplicate
3419 // constants. *CASES should be passed as an empty vector; the values
3420 // for this case will be added to it. If this is the default case,
3421 // *CASES will remain empty. This returns the statement to execute if
3422 // one of these cases is selected.
3424 Bstatement*
3425 Case_clauses::Case_clause::get_backend(Translate_context* context,
3426 Unnamed_label* break_label,
3427 Case_constants* case_constants,
3428 std::vector<Bexpression*>* cases) const
3430 if (this->cases_ != NULL)
3432 go_assert(!this->is_default_);
3433 for (Expression_list::const_iterator p = this->cases_->begin();
3434 p != this->cases_->end();
3435 ++p)
3437 Expression* e = *p;
3438 if (e->classification() != Expression::EXPRESSION_INTEGER)
3440 Numeric_constant nc;
3441 mpz_t ival;
3442 if (!(*p)->numeric_constant_value(&nc) || !nc.to_int(&ival))
3444 // Something went wrong. This can happen with a
3445 // negative constant and an unsigned switch value.
3446 go_assert(saw_errors());
3447 continue;
3449 go_assert(nc.type() != NULL);
3450 e = Expression::make_integer_z(&ival, nc.type(), e->location());
3451 mpz_clear(ival);
3454 std::pair<Case_constants::iterator, bool> ins =
3455 case_constants->insert(e);
3456 if (!ins.second)
3458 // Value was already present.
3459 go_error_at(this->location_, "duplicate case in switch");
3460 e = Expression::make_error(this->location_);
3462 cases->push_back(e->get_backend(context));
3466 Bstatement* statements;
3467 if (this->statements_ == NULL)
3468 statements = NULL;
3469 else
3471 Bblock* bblock = this->statements_->get_backend(context);
3472 statements = context->backend()->block_statement(bblock);
3475 Bstatement* break_stat;
3476 if (this->is_fallthrough_)
3477 break_stat = NULL;
3478 else
3479 break_stat = break_label->get_goto(context, this->location_);
3481 if (statements == NULL)
3482 return break_stat;
3483 else if (break_stat == NULL)
3484 return statements;
3485 else
3486 return context->backend()->compound_statement(statements, break_stat);
3489 // Dump the AST representation for a case clause
3491 void
3492 Case_clauses::Case_clause::dump_clause(Ast_dump_context* ast_dump_context)
3493 const
3495 ast_dump_context->print_indent();
3496 if (this->is_default_)
3498 ast_dump_context->ostream() << "default:";
3500 else
3502 ast_dump_context->ostream() << "case ";
3503 ast_dump_context->dump_expression_list(this->cases_);
3504 ast_dump_context->ostream() << ":" ;
3506 ast_dump_context->dump_block(this->statements_);
3507 if (this->is_fallthrough_)
3509 ast_dump_context->print_indent();
3510 ast_dump_context->ostream() << " (fallthrough)" << dsuffix(location()) << std::endl;
3514 // Class Case_clauses.
3516 // Traversal.
3519 Case_clauses::traverse(Traverse* traverse)
3521 for (Clauses::iterator p = this->clauses_.begin();
3522 p != this->clauses_.end();
3523 ++p)
3525 if (p->traverse(traverse) == TRAVERSE_EXIT)
3526 return TRAVERSE_EXIT;
3528 return TRAVERSE_CONTINUE;
3531 // Check whether all the case expressions are constant.
3533 bool
3534 Case_clauses::is_constant() const
3536 for (Clauses::const_iterator p = this->clauses_.begin();
3537 p != this->clauses_.end();
3538 ++p)
3539 if (!p->is_constant())
3540 return false;
3541 return true;
3544 // Lower case clauses for a nonconstant switch.
3546 void
3547 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3548 Unnamed_label* break_label) const
3550 // The default case.
3551 const Case_clause* default_case = NULL;
3553 // The label for the fallthrough of the previous case.
3554 Unnamed_label* last_fallthrough_label = NULL;
3556 // The label for the start of the default case. This is used if the
3557 // case before the default case falls through.
3558 Unnamed_label* default_start_label = NULL;
3560 // The label for the end of the default case. This normally winds
3561 // up as BREAK_LABEL, but it will be different if the default case
3562 // falls through.
3563 Unnamed_label* default_finish_label = NULL;
3565 for (Clauses::const_iterator p = this->clauses_.begin();
3566 p != this->clauses_.end();
3567 ++p)
3569 // The label to use for the start of the statements for this
3570 // case. This is NULL unless the previous case falls through.
3571 Unnamed_label* start_label = last_fallthrough_label;
3573 // The label to jump to after the end of the statements for this
3574 // case.
3575 Unnamed_label* finish_label = break_label;
3577 last_fallthrough_label = NULL;
3578 if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3580 finish_label = new Unnamed_label(p->location());
3581 last_fallthrough_label = finish_label;
3584 if (!p->is_default())
3585 p->lower(b, val_temp, start_label, finish_label);
3586 else
3588 // We have to move the default case to the end, so that we
3589 // only use it if all the other tests fail.
3590 default_case = &*p;
3591 default_start_label = start_label;
3592 default_finish_label = finish_label;
3596 if (default_case != NULL)
3597 default_case->lower(b, val_temp, default_start_label,
3598 default_finish_label);
3601 // Determine types.
3603 void
3604 Case_clauses::determine_types(Type* type)
3606 for (Clauses::iterator p = this->clauses_.begin();
3607 p != this->clauses_.end();
3608 ++p)
3609 p->determine_types(type);
3612 // Check types. Returns false if there was an error.
3614 bool
3615 Case_clauses::check_types(Type* type)
3617 bool ret = true;
3618 for (Clauses::iterator p = this->clauses_.begin();
3619 p != this->clauses_.end();
3620 ++p)
3622 if (!p->check_types(type))
3623 ret = false;
3625 return ret;
3628 // Return true if these clauses may fall through to the statements
3629 // following the switch statement.
3631 bool
3632 Case_clauses::may_fall_through() const
3634 bool found_default = false;
3635 for (Clauses::const_iterator p = this->clauses_.begin();
3636 p != this->clauses_.end();
3637 ++p)
3639 if (p->may_fall_through() && !p->is_fallthrough())
3640 return true;
3641 if (p->is_default())
3642 found_default = true;
3644 return !found_default;
3647 // Convert the cases to the backend representation. This sets
3648 // *ALL_CASES and *ALL_STATEMENTS.
3650 void
3651 Case_clauses::get_backend(Translate_context* context,
3652 Unnamed_label* break_label,
3653 std::vector<std::vector<Bexpression*> >* all_cases,
3654 std::vector<Bstatement*>* all_statements) const
3656 Case_constants case_constants;
3658 size_t c = this->clauses_.size();
3659 all_cases->resize(c);
3660 all_statements->resize(c);
3662 size_t i = 0;
3663 for (Clauses::const_iterator p = this->clauses_.begin();
3664 p != this->clauses_.end();
3665 ++p, ++i)
3667 std::vector<Bexpression*> cases;
3668 Bstatement* stat = p->get_backend(context, break_label, &case_constants,
3669 &cases);
3670 (*all_cases)[i].swap(cases);
3671 (*all_statements)[i] = stat;
3675 // Dump the AST representation for case clauses (from a switch statement)
3677 void
3678 Case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
3680 for (Clauses::const_iterator p = this->clauses_.begin();
3681 p != this->clauses_.end();
3682 ++p)
3683 p->dump_clause(ast_dump_context);
3686 // A constant switch statement. A Switch_statement is lowered to this
3687 // when all the cases are constants.
3689 class Constant_switch_statement : public Statement
3691 public:
3692 Constant_switch_statement(Expression* val, Case_clauses* clauses,
3693 Unnamed_label* break_label,
3694 Location location)
3695 : Statement(STATEMENT_CONSTANT_SWITCH, location),
3696 val_(val), clauses_(clauses), break_label_(break_label)
3699 protected:
3701 do_traverse(Traverse*);
3703 void
3704 do_determine_types();
3706 void
3707 do_check_types(Gogo*);
3709 Bstatement*
3710 do_get_backend(Translate_context*);
3712 void
3713 do_dump_statement(Ast_dump_context*) const;
3715 private:
3716 // The value to switch on.
3717 Expression* val_;
3718 // The case clauses.
3719 Case_clauses* clauses_;
3720 // The break label, if needed.
3721 Unnamed_label* break_label_;
3724 // Traversal.
3727 Constant_switch_statement::do_traverse(Traverse* traverse)
3729 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3730 return TRAVERSE_EXIT;
3731 return this->clauses_->traverse(traverse);
3734 // Determine types.
3736 void
3737 Constant_switch_statement::do_determine_types()
3739 this->val_->determine_type_no_context();
3740 this->clauses_->determine_types(this->val_->type());
3743 // Check types.
3745 void
3746 Constant_switch_statement::do_check_types(Gogo*)
3748 if (!this->clauses_->check_types(this->val_->type()))
3749 this->set_is_error();
3752 // Convert to GENERIC.
3754 Bstatement*
3755 Constant_switch_statement::do_get_backend(Translate_context* context)
3757 Bexpression* switch_val_expr = this->val_->get_backend(context);
3759 Unnamed_label* break_label = this->break_label_;
3760 if (break_label == NULL)
3761 break_label = new Unnamed_label(this->location());
3763 std::vector<std::vector<Bexpression*> > all_cases;
3764 std::vector<Bstatement*> all_statements;
3765 this->clauses_->get_backend(context, break_label, &all_cases,
3766 &all_statements);
3768 Bfunction* bfunction = context->function()->func_value()->get_decl();
3769 Bstatement* switch_statement;
3770 switch_statement = context->backend()->switch_statement(bfunction,
3771 switch_val_expr,
3772 all_cases,
3773 all_statements,
3774 this->location());
3775 Bstatement* ldef = break_label->get_definition(context);
3776 return context->backend()->compound_statement(switch_statement, ldef);
3779 // Dump the AST representation for a constant switch statement.
3781 void
3782 Constant_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
3783 const
3785 ast_dump_context->print_indent();
3786 ast_dump_context->ostream() << "switch ";
3787 ast_dump_context->dump_expression(this->val_);
3789 if (ast_dump_context->dump_subblocks())
3791 ast_dump_context->ostream() << " {" << std::endl;
3792 this->clauses_->dump_clauses(ast_dump_context);
3793 ast_dump_context->ostream() << "}";
3796 ast_dump_context->ostream() << std::endl;
3799 // Class Switch_statement.
3801 // Traversal.
3804 Switch_statement::do_traverse(Traverse* traverse)
3806 if (this->val_ != NULL)
3808 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3809 return TRAVERSE_EXIT;
3811 return this->clauses_->traverse(traverse);
3814 // Lower a Switch_statement to a Constant_switch_statement or a series
3815 // of if statements.
3817 Statement*
3818 Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
3819 Statement_inserter*)
3821 Location loc = this->location();
3823 if (this->val_ != NULL
3824 && (this->val_->is_error_expression()
3825 || this->val_->type()->is_error()))
3827 go_assert(saw_errors());
3828 return Statement::make_error_statement(loc);
3831 if (this->val_ != NULL
3832 && this->val_->type()->integer_type() != NULL
3833 && !this->clauses_->empty()
3834 && this->clauses_->is_constant())
3835 return new Constant_switch_statement(this->val_, this->clauses_,
3836 this->break_label_, loc);
3838 if (this->val_ != NULL
3839 && !this->val_->type()->is_comparable()
3840 && !Type::are_compatible_for_comparison(true, this->val_->type(),
3841 Type::make_nil_type(), NULL))
3843 go_error_at(this->val_->location(),
3844 "cannot switch on value whose type that may not be compared");
3845 return Statement::make_error_statement(loc);
3848 Block* b = new Block(enclosing, loc);
3850 if (this->clauses_->empty())
3852 Expression* val = this->val_;
3853 if (val == NULL)
3854 val = Expression::make_boolean(true, loc);
3855 return Statement::make_statement(val, true);
3858 // var val_temp VAL_TYPE = VAL
3859 Expression* val = this->val_;
3860 if (val == NULL)
3861 val = Expression::make_boolean(true, loc);
3863 Type* type = val->type();
3864 if (type->is_abstract())
3865 type = type->make_non_abstract_type();
3866 Temporary_statement* val_temp = Statement::make_temporary(type, val, loc);
3867 b->add_statement(val_temp);
3869 this->clauses_->lower(b, val_temp, this->break_label());
3871 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3872 b->add_statement(s);
3874 return Statement::make_block_statement(b, loc);
3877 // Return the break label for this switch statement, creating it if
3878 // necessary.
3880 Unnamed_label*
3881 Switch_statement::break_label()
3883 if (this->break_label_ == NULL)
3884 this->break_label_ = new Unnamed_label(this->location());
3885 return this->break_label_;
3888 // Dump the AST representation for a switch statement.
3890 void
3891 Switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
3893 ast_dump_context->print_indent();
3894 ast_dump_context->ostream() << "switch ";
3895 if (this->val_ != NULL)
3897 ast_dump_context->dump_expression(this->val_);
3899 if (ast_dump_context->dump_subblocks())
3901 ast_dump_context->ostream() << " {" << dsuffix(location()) << std::endl;
3902 this->clauses_->dump_clauses(ast_dump_context);
3903 ast_dump_context->print_indent();
3904 ast_dump_context->ostream() << "}";
3906 ast_dump_context->ostream() << std::endl;
3909 // Return whether this switch may fall through.
3911 bool
3912 Switch_statement::do_may_fall_through() const
3914 if (this->clauses_ == NULL)
3915 return true;
3917 // If we have a break label, then some case needed it. That implies
3918 // that the switch statement as a whole can fall through.
3919 if (this->break_label_ != NULL)
3920 return true;
3922 return this->clauses_->may_fall_through();
3925 // Make a switch statement.
3927 Switch_statement*
3928 Statement::make_switch_statement(Expression* val, Location location)
3930 return new Switch_statement(val, location);
3933 // Class Type_case_clauses::Type_case_clause.
3935 // Traversal.
3938 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3940 if (!this->is_default_
3941 && ((traverse->traverse_mask()
3942 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3943 && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3944 return TRAVERSE_EXIT;
3945 if (this->statements_ != NULL)
3946 return this->statements_->traverse(traverse);
3947 return TRAVERSE_CONTINUE;
3950 // Lower one clause in a type switch. Add statements to the block B.
3951 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3952 // BREAK_LABEL is the label at the end of the type switch.
3953 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3954 // statements.
3956 void
3957 Type_case_clauses::Type_case_clause::lower(Type* switch_val_type,
3958 Block* b,
3959 Temporary_statement* descriptor_temp,
3960 Unnamed_label* break_label,
3961 Unnamed_label** stmts_label) const
3963 Location loc = this->location_;
3965 Unnamed_label* next_case_label = NULL;
3966 if (!this->is_default_)
3968 Type* type = this->type_;
3970 std::string reason;
3971 if (switch_val_type->interface_type() != NULL
3972 && !type->is_nil_constant_as_type()
3973 && type->interface_type() == NULL
3974 && !switch_val_type->interface_type()->implements_interface(type,
3975 &reason))
3977 if (reason.empty())
3978 go_error_at(this->location_, "impossible type switch case");
3979 else
3980 go_error_at(this->location_, "impossible type switch case (%s)",
3981 reason.c_str());
3984 Expression* ref = Expression::make_temporary_reference(descriptor_temp,
3985 loc);
3987 Expression* cond;
3988 // The language permits case nil, which is of course a constant
3989 // rather than a type. It will appear here as an invalid
3990 // forwarding type.
3991 if (type->is_nil_constant_as_type())
3992 cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3993 Expression::make_nil(loc),
3994 loc);
3995 else
3996 cond = Runtime::make_call((type->interface_type() == NULL
3997 ? Runtime::IFACETYPEEQ
3998 : Runtime::IFACET2IP),
3999 loc, 2,
4000 Expression::make_type_descriptor(type, loc),
4001 ref);
4003 Unnamed_label* dest;
4004 if (!this->is_fallthrough_)
4006 // if !COND { goto NEXT_CASE_LABEL }
4007 next_case_label = new Unnamed_label(Linemap::unknown_location());
4008 dest = next_case_label;
4009 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
4011 else
4013 // if COND { goto STMTS_LABEL }
4014 go_assert(stmts_label != NULL);
4015 if (*stmts_label == NULL)
4016 *stmts_label = new Unnamed_label(Linemap::unknown_location());
4017 dest = *stmts_label;
4019 Block* then_block = new Block(b, loc);
4020 Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
4021 then_block->add_statement(s);
4022 s = Statement::make_if_statement(cond, then_block, NULL, loc);
4023 b->add_statement(s);
4026 if (this->statements_ != NULL
4027 || (!this->is_fallthrough_
4028 && stmts_label != NULL
4029 && *stmts_label != NULL))
4031 go_assert(!this->is_fallthrough_);
4032 if (stmts_label != NULL && *stmts_label != NULL)
4034 go_assert(!this->is_default_);
4035 if (this->statements_ != NULL)
4036 (*stmts_label)->set_location(this->statements_->start_location());
4037 Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
4038 b->add_statement(s);
4039 *stmts_label = NULL;
4041 if (this->statements_ != NULL)
4042 b->add_statement(Statement::make_block_statement(this->statements_,
4043 loc));
4046 if (this->is_fallthrough_)
4047 go_assert(next_case_label == NULL);
4048 else
4050 Location gloc = (this->statements_ == NULL
4051 ? loc
4052 : this->statements_->end_location());
4053 b->add_statement(Statement::make_goto_unnamed_statement(break_label,
4054 gloc));
4055 if (next_case_label != NULL)
4057 Statement* s =
4058 Statement::make_unnamed_label_statement(next_case_label);
4059 b->add_statement(s);
4064 // Return true if this type clause may fall through to the statements
4065 // following the switch.
4067 bool
4068 Type_case_clauses::Type_case_clause::may_fall_through() const
4070 if (this->is_fallthrough_)
4072 // This case means that we automatically fall through to the
4073 // next case (it's used for T1 in case T1, T2:). It does not
4074 // mean that we fall through to the end of the type switch as a
4075 // whole. There is sure to be a next case and that next case
4076 // will determine whether we fall through to the statements
4077 // after the type switch.
4078 return false;
4080 if (this->statements_ == NULL)
4081 return true;
4082 return this->statements_->may_fall_through();
4085 // Dump the AST representation for a type case clause
4087 void
4088 Type_case_clauses::Type_case_clause::dump_clause(
4089 Ast_dump_context* ast_dump_context) const
4091 ast_dump_context->print_indent();
4092 if (this->is_default_)
4094 ast_dump_context->ostream() << "default:";
4096 else
4098 ast_dump_context->ostream() << "case ";
4099 ast_dump_context->dump_type(this->type_);
4100 ast_dump_context->ostream() << ":" ;
4102 ast_dump_context->dump_block(this->statements_);
4103 if (this->is_fallthrough_)
4105 ast_dump_context->print_indent();
4106 ast_dump_context->ostream() << " (fallthrough)" << std::endl;
4110 // Class Type_case_clauses.
4112 // Traversal.
4115 Type_case_clauses::traverse(Traverse* traverse)
4117 for (Type_clauses::iterator p = this->clauses_.begin();
4118 p != this->clauses_.end();
4119 ++p)
4121 if (p->traverse(traverse) == TRAVERSE_EXIT)
4122 return TRAVERSE_EXIT;
4124 return TRAVERSE_CONTINUE;
4127 // Check for duplicate types.
4129 void
4130 Type_case_clauses::check_duplicates() const
4132 typedef Unordered_set_hash(const Type*, Type_hash_identical,
4133 Type_identical) Types_seen;
4134 Types_seen types_seen;
4135 for (Type_clauses::const_iterator p = this->clauses_.begin();
4136 p != this->clauses_.end();
4137 ++p)
4139 Type* t = p->type();
4140 if (t == NULL)
4141 continue;
4142 if (t->is_nil_constant_as_type())
4143 t = Type::make_nil_type();
4144 std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
4145 if (!ins.second)
4146 go_error_at(p->location(), "duplicate type in switch");
4150 // Lower the clauses in a type switch. Add statements to the block B.
4151 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
4152 // BREAK_LABEL is the label at the end of the type switch.
4154 void
4155 Type_case_clauses::lower(Type* switch_val_type, Block* b,
4156 Temporary_statement* descriptor_temp,
4157 Unnamed_label* break_label) const
4159 const Type_case_clause* default_case = NULL;
4161 Unnamed_label* stmts_label = NULL;
4162 for (Type_clauses::const_iterator p = this->clauses_.begin();
4163 p != this->clauses_.end();
4164 ++p)
4166 if (!p->is_default())
4167 p->lower(switch_val_type, b, descriptor_temp, break_label,
4168 &stmts_label);
4169 else
4171 // We are generating a series of tests, which means that we
4172 // need to move the default case to the end.
4173 default_case = &*p;
4176 go_assert(stmts_label == NULL);
4178 if (default_case != NULL)
4179 default_case->lower(switch_val_type, b, descriptor_temp, break_label,
4180 NULL);
4183 // Return true if these clauses may fall through to the statements
4184 // following the switch statement.
4186 bool
4187 Type_case_clauses::may_fall_through() const
4189 bool found_default = false;
4190 for (Type_clauses::const_iterator p = this->clauses_.begin();
4191 p != this->clauses_.end();
4192 ++p)
4194 if (p->may_fall_through())
4195 return true;
4196 if (p->is_default())
4197 found_default = true;
4199 return !found_default;
4202 // Dump the AST representation for case clauses (from a switch statement)
4204 void
4205 Type_case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
4207 for (Type_clauses::const_iterator p = this->clauses_.begin();
4208 p != this->clauses_.end();
4209 ++p)
4210 p->dump_clause(ast_dump_context);
4213 // Class Type_switch_statement.
4215 // Traversal.
4218 Type_switch_statement::do_traverse(Traverse* traverse)
4220 if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
4221 return TRAVERSE_EXIT;
4222 if (this->clauses_ != NULL)
4223 return this->clauses_->traverse(traverse);
4224 return TRAVERSE_CONTINUE;
4227 // Lower a type switch statement to a series of if statements. The gc
4228 // compiler is able to generate a table in some cases. However, that
4229 // does not work for us because we may have type descriptors in
4230 // different shared libraries, so we can't compare them with simple
4231 // equality testing.
4233 Statement*
4234 Type_switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
4235 Statement_inserter*)
4237 const Location loc = this->location();
4239 if (this->clauses_ != NULL)
4240 this->clauses_->check_duplicates();
4242 Block* b = new Block(enclosing, loc);
4244 Type* val_type = this->expr_->type();
4245 if (val_type->interface_type() == NULL)
4247 if (!val_type->is_error())
4248 this->report_error(_("cannot type switch on non-interface value"));
4249 return Statement::make_error_statement(loc);
4252 // var descriptor_temp DESCRIPTOR_TYPE
4253 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
4254 Temporary_statement* descriptor_temp =
4255 Statement::make_temporary(descriptor_type, NULL, loc);
4256 b->add_statement(descriptor_temp);
4258 // descriptor_temp = ifacetype(val_temp) FIXME: This should be
4259 // inlined.
4260 bool is_empty = val_type->interface_type()->is_empty();
4261 Expression* call = Runtime::make_call((is_empty
4262 ? Runtime::EFACETYPE
4263 : Runtime::IFACETYPE),
4264 loc, 1, this->expr_);
4265 Temporary_reference_expression* lhs =
4266 Expression::make_temporary_reference(descriptor_temp, loc);
4267 lhs->set_is_lvalue();
4268 Statement* s = Statement::make_assignment(lhs, call, loc);
4269 b->add_statement(s);
4271 if (this->clauses_ != NULL)
4272 this->clauses_->lower(val_type, b, descriptor_temp, this->break_label());
4274 s = Statement::make_unnamed_label_statement(this->break_label_);
4275 b->add_statement(s);
4277 return Statement::make_block_statement(b, loc);
4280 // Return whether this switch may fall through.
4282 bool
4283 Type_switch_statement::do_may_fall_through() const
4285 if (this->clauses_ == NULL)
4286 return true;
4288 // If we have a break label, then some case needed it. That implies
4289 // that the switch statement as a whole can fall through.
4290 if (this->break_label_ != NULL)
4291 return true;
4293 return this->clauses_->may_fall_through();
4296 // Return the break label for this type switch statement, creating it
4297 // if necessary.
4299 Unnamed_label*
4300 Type_switch_statement::break_label()
4302 if (this->break_label_ == NULL)
4303 this->break_label_ = new Unnamed_label(this->location());
4304 return this->break_label_;
4307 // Dump the AST representation for a type switch statement
4309 void
4310 Type_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
4311 const
4313 ast_dump_context->print_indent();
4314 ast_dump_context->ostream() << "switch ";
4315 if (!this->name_.empty())
4316 ast_dump_context->ostream() << this->name_ << " = ";
4317 ast_dump_context->dump_expression(this->expr_);
4318 ast_dump_context->ostream() << " .(type)";
4319 if (ast_dump_context->dump_subblocks())
4321 ast_dump_context->ostream() << " {" << dsuffix(location()) << std::endl;
4322 this->clauses_->dump_clauses(ast_dump_context);
4323 ast_dump_context->ostream() << "}";
4325 ast_dump_context->ostream() << std::endl;
4328 // Make a type switch statement.
4330 Type_switch_statement*
4331 Statement::make_type_switch_statement(const std::string& name, Expression* expr,
4332 Location location)
4334 return new Type_switch_statement(name, expr, location);
4337 // Class Send_statement.
4339 // Traversal.
4342 Send_statement::do_traverse(Traverse* traverse)
4344 if (this->traverse_expression(traverse, &this->channel_) == TRAVERSE_EXIT)
4345 return TRAVERSE_EXIT;
4346 return this->traverse_expression(traverse, &this->val_);
4349 // Determine types.
4351 void
4352 Send_statement::do_determine_types()
4354 this->channel_->determine_type_no_context();
4355 Type* type = this->channel_->type();
4356 Type_context context;
4357 if (type->channel_type() != NULL)
4358 context.type = type->channel_type()->element_type();
4359 this->val_->determine_type(&context);
4362 // Check types.
4364 void
4365 Send_statement::do_check_types(Gogo*)
4367 Type* type = this->channel_->type();
4368 if (type->is_error())
4370 this->set_is_error();
4371 return;
4373 Channel_type* channel_type = type->channel_type();
4374 if (channel_type == NULL)
4376 go_error_at(this->location(), "left operand of %<<-%> must be channel");
4377 this->set_is_error();
4378 return;
4380 Type* element_type = channel_type->element_type();
4381 if (!Type::are_assignable(element_type, this->val_->type(), NULL))
4383 this->report_error(_("incompatible types in send"));
4384 return;
4386 if (!channel_type->may_send())
4388 this->report_error(_("invalid send on receive-only channel"));
4389 return;
4393 // Flatten a send statement. We may need a temporary for interface
4394 // conversion.
4396 Statement*
4397 Send_statement::do_flatten(Gogo*, Named_object*, Block*,
4398 Statement_inserter* inserter)
4400 if (this->channel_->is_error_expression()
4401 || this->channel_->type()->is_error_type())
4403 go_assert(saw_errors());
4404 return Statement::make_error_statement(this->location());
4407 Type* element_type = this->channel_->type()->channel_type()->element_type();
4408 if (!Type::are_identical(element_type, this->val_->type(), false, NULL)
4409 && this->val_->type()->interface_type() != NULL
4410 && !this->val_->is_variable())
4412 Temporary_statement* temp =
4413 Statement::make_temporary(NULL, this->val_, this->location());
4414 inserter->insert(temp);
4415 this->val_ = Expression::make_temporary_reference(temp,
4416 this->location());
4418 return this;
4421 // Convert a send statement to the backend representation.
4423 Bstatement*
4424 Send_statement::do_get_backend(Translate_context* context)
4426 Location loc = this->location();
4428 Channel_type* channel_type = this->channel_->type()->channel_type();
4429 Type* element_type = channel_type->element_type();
4430 Expression* val = Expression::convert_for_assignment(context->gogo(),
4431 element_type,
4432 this->val_, loc);
4434 bool can_take_address;
4435 switch (element_type->base()->classification())
4437 case Type::TYPE_BOOLEAN:
4438 case Type::TYPE_INTEGER:
4439 case Type::TYPE_FUNCTION:
4440 case Type::TYPE_POINTER:
4441 case Type::TYPE_MAP:
4442 case Type::TYPE_CHANNEL:
4443 case Type::TYPE_FLOAT:
4444 case Type::TYPE_COMPLEX:
4445 case Type::TYPE_STRING:
4446 case Type::TYPE_INTERFACE:
4447 can_take_address = false;
4448 break;
4450 case Type::TYPE_STRUCT:
4451 can_take_address = true;
4452 break;
4454 case Type::TYPE_ARRAY:
4455 can_take_address = !element_type->is_slice_type();
4456 break;
4458 default:
4459 case Type::TYPE_ERROR:
4460 case Type::TYPE_VOID:
4461 case Type::TYPE_SINK:
4462 case Type::TYPE_NIL:
4463 case Type::TYPE_NAMED:
4464 case Type::TYPE_FORWARD:
4465 go_assert(saw_errors());
4466 return context->backend()->error_statement();
4469 // Only try to take the address of a variable. We have already
4470 // moved variables to the heap, so this should not cause that to
4471 // happen unnecessarily.
4472 if (can_take_address
4473 && val->var_expression() == NULL
4474 && val->temporary_reference_expression() == NULL)
4475 can_take_address = false;
4477 Expression* td = Expression::make_type_descriptor(this->channel_->type(),
4478 loc);
4480 Bstatement* btemp = NULL;
4481 if (can_take_address)
4483 // The function doesn't change the value, so just take its
4484 // address directly.
4485 val = Expression::make_unary(OPERATOR_AND, val, loc);
4487 else
4489 // The value is not in a variable, or is small enough that it
4490 // might be in a register, and taking the address would push it
4491 // on the stack. Copy it into a temporary variable to take the
4492 // address.
4493 Temporary_statement* temp = Statement::make_temporary(element_type,
4494 val, loc);
4495 Expression* ref = Expression::make_temporary_reference(temp, loc);
4496 val = Expression::make_unary(OPERATOR_AND, ref, loc);
4497 btemp = temp->get_backend(context);
4500 Expression* call = Runtime::make_call(Runtime::CHANSEND, loc, 3, td,
4501 this->channel_, val);
4503 context->gogo()->lower_expression(context->function(), NULL, &call);
4504 Bexpression* bcall = call->get_backend(context);
4505 Bfunction* bfunction = context->function()->func_value()->get_decl();
4506 Bstatement* s = context->backend()->expression_statement(bfunction, bcall);
4508 if (btemp == NULL)
4509 return s;
4510 else
4511 return context->backend()->compound_statement(btemp, s);
4514 // Dump the AST representation for a send statement
4516 void
4517 Send_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
4519 ast_dump_context->print_indent();
4520 ast_dump_context->dump_expression(this->channel_);
4521 ast_dump_context->ostream() << " <- ";
4522 ast_dump_context->dump_expression(this->val_);
4523 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
4526 // Make a send statement.
4528 Send_statement*
4529 Statement::make_send_statement(Expression* channel, Expression* val,
4530 Location location)
4532 return new Send_statement(channel, val, location);
4535 // Class Select_clauses::Select_clause.
4537 // Traversal.
4540 Select_clauses::Select_clause::traverse(Traverse* traverse)
4542 if (!this->is_lowered_
4543 && (traverse->traverse_mask()
4544 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
4546 if (this->channel_ != NULL)
4548 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
4549 return TRAVERSE_EXIT;
4551 if (this->val_ != NULL)
4553 if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
4554 return TRAVERSE_EXIT;
4556 if (this->closed_ != NULL)
4558 if (Expression::traverse(&this->closed_, traverse) == TRAVERSE_EXIT)
4559 return TRAVERSE_EXIT;
4562 if (this->statements_ != NULL)
4564 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
4565 return TRAVERSE_EXIT;
4567 return TRAVERSE_CONTINUE;
4570 // Lowering. We call a function to register this clause, and arrange
4571 // to set any variables in any receive clause.
4573 void
4574 Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
4575 Block* b, Temporary_statement* sel)
4577 Location loc = this->location_;
4579 Expression* selref = Expression::make_temporary_reference(sel, loc);
4580 selref = Expression::make_unary(OPERATOR_AND, selref, loc);
4582 Expression* index_expr = Expression::make_integer_ul(this->index_, NULL,
4583 loc);
4585 if (this->is_default_)
4587 go_assert(this->channel_ == NULL && this->val_ == NULL);
4588 this->lower_default(b, selref, index_expr);
4589 this->is_lowered_ = true;
4590 return;
4593 // Evaluate the channel before the select statement.
4594 Temporary_statement* channel_temp = Statement::make_temporary(NULL,
4595 this->channel_,
4596 loc);
4597 b->add_statement(channel_temp);
4598 Expression* chanref = Expression::make_temporary_reference(channel_temp,
4599 loc);
4601 if (this->is_send_)
4602 this->lower_send(b, selref, chanref, index_expr);
4603 else
4604 this->lower_recv(gogo, function, b, selref, chanref, index_expr);
4606 // Now all references should be handled through the statements, not
4607 // through here.
4608 this->is_lowered_ = true;
4609 this->val_ = NULL;
4612 // Lower a default clause in a select statement.
4614 void
4615 Select_clauses::Select_clause::lower_default(Block* b, Expression* selref,
4616 Expression* index_expr)
4618 Location loc = this->location_;
4619 Expression* call = Runtime::make_call(Runtime::SELECTDEFAULT, loc, 2, selref,
4620 index_expr);
4621 b->add_statement(Statement::make_statement(call, true));
4624 // Lower a send clause in a select statement.
4626 void
4627 Select_clauses::Select_clause::lower_send(Block* b, Expression* selref,
4628 Expression* chanref,
4629 Expression* index_expr)
4631 Location loc = this->location_;
4633 Channel_type* ct = this->channel_->type()->channel_type();
4634 if (ct == NULL)
4635 return;
4637 Type* valtype = ct->element_type();
4639 // Note that copying the value to a temporary here means that we
4640 // evaluate the send values in the required order.
4641 Temporary_statement* val = Statement::make_temporary(valtype, this->val_,
4642 loc);
4643 b->add_statement(val);
4645 Expression* valref = Expression::make_temporary_reference(val, loc);
4646 Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
4648 Expression* call = Runtime::make_call(Runtime::SELECTSEND, loc, 4, selref,
4649 chanref, valaddr, index_expr);
4650 b->add_statement(Statement::make_statement(call, true));
4653 // Lower a receive clause in a select statement.
4655 void
4656 Select_clauses::Select_clause::lower_recv(Gogo* gogo, Named_object* function,
4657 Block* b, Expression* selref,
4658 Expression* chanref,
4659 Expression* index_expr)
4661 Location loc = this->location_;
4663 Channel_type* ct = this->channel_->type()->channel_type();
4664 if (ct == NULL)
4665 return;
4667 Type* valtype = ct->element_type();
4668 Temporary_statement* val = Statement::make_temporary(valtype, NULL, loc);
4669 b->add_statement(val);
4671 Expression* valref = Expression::make_temporary_reference(val, loc);
4672 Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
4674 Temporary_statement* closed_temp = NULL;
4676 Expression* call;
4677 if (this->closed_ == NULL && this->closedvar_ == NULL)
4678 call = Runtime::make_call(Runtime::SELECTRECV, loc, 4, selref, chanref,
4679 valaddr, index_expr);
4680 else
4682 closed_temp = Statement::make_temporary(Type::lookup_bool_type(), NULL,
4683 loc);
4684 b->add_statement(closed_temp);
4685 Expression* cref = Expression::make_temporary_reference(closed_temp,
4686 loc);
4687 Expression* caddr = Expression::make_unary(OPERATOR_AND, cref, loc);
4688 call = Runtime::make_call(Runtime::SELECTRECV2, loc, 5, selref, chanref,
4689 valaddr, caddr, index_expr);
4692 b->add_statement(Statement::make_statement(call, true));
4694 // If the block of statements is executed, arrange for the received
4695 // value to move from VAL to the place where the statements expect
4696 // it.
4698 Block* init = NULL;
4700 if (this->var_ != NULL)
4702 go_assert(this->val_ == NULL);
4703 valref = Expression::make_temporary_reference(val, loc);
4704 this->var_->var_value()->set_init(valref);
4705 this->var_->var_value()->clear_type_from_chan_element();
4707 else if (this->val_ != NULL && !this->val_->is_sink_expression())
4709 init = new Block(b, loc);
4710 valref = Expression::make_temporary_reference(val, loc);
4711 init->add_statement(Statement::make_assignment(this->val_, valref, loc));
4714 if (this->closedvar_ != NULL)
4716 go_assert(this->closed_ == NULL);
4717 Expression* cref = Expression::make_temporary_reference(closed_temp,
4718 loc);
4719 this->closedvar_->var_value()->set_init(cref);
4721 else if (this->closed_ != NULL && !this->closed_->is_sink_expression())
4723 if (init == NULL)
4724 init = new Block(b, loc);
4725 Expression* cref = Expression::make_temporary_reference(closed_temp,
4726 loc);
4727 init->add_statement(Statement::make_assignment(this->closed_, cref,
4728 loc));
4731 if (init != NULL)
4733 gogo->lower_block(function, init);
4735 if (this->statements_ != NULL)
4736 init->add_statement(Statement::make_block_statement(this->statements_,
4737 loc));
4738 this->statements_ = init;
4742 // Determine types.
4744 void
4745 Select_clauses::Select_clause::determine_types()
4747 go_assert(this->is_lowered_);
4748 if (this->statements_ != NULL)
4749 this->statements_->determine_types();
4752 // Check types.
4754 void
4755 Select_clauses::Select_clause::check_types()
4757 if (this->is_default_)
4758 return;
4760 Channel_type* ct = this->channel_->type()->channel_type();
4761 if (ct == NULL)
4763 go_error_at(this->channel_->location(), "expected channel");
4764 return;
4767 if (this->is_send_ && !ct->may_send())
4768 go_error_at(this->location(), "invalid send on receive-only channel");
4769 else if (!this->is_send_ && !ct->may_receive())
4770 go_error_at(this->location(), "invalid receive on send-only channel");
4773 // Whether this clause may fall through to the statement which follows
4774 // the overall select statement.
4776 bool
4777 Select_clauses::Select_clause::may_fall_through() const
4779 if (this->statements_ == NULL)
4780 return true;
4781 return this->statements_->may_fall_through();
4784 // Return the backend representation for the statements to execute.
4786 Bstatement*
4787 Select_clauses::Select_clause::get_statements_backend(
4788 Translate_context* context)
4790 if (this->statements_ == NULL)
4791 return NULL;
4792 Bblock* bblock = this->statements_->get_backend(context);
4793 return context->backend()->block_statement(bblock);
4796 // Dump the AST representation for a select case clause
4798 void
4799 Select_clauses::Select_clause::dump_clause(
4800 Ast_dump_context* ast_dump_context) const
4802 ast_dump_context->print_indent();
4803 if (this->is_default_)
4805 ast_dump_context->ostream() << "default:";
4807 else
4809 ast_dump_context->ostream() << "case " ;
4810 if (this->is_send_)
4812 ast_dump_context->dump_expression(this->channel_);
4813 ast_dump_context->ostream() << " <- " ;
4814 if (this->val_ != NULL)
4815 ast_dump_context->dump_expression(this->val_);
4817 else
4819 if (this->val_ != NULL)
4820 ast_dump_context->dump_expression(this->val_);
4821 if (this->closed_ != NULL)
4823 // FIXME: can val_ == NULL and closed_ ! = NULL?
4824 ast_dump_context->ostream() << " , " ;
4825 ast_dump_context->dump_expression(this->closed_);
4827 if (this->closedvar_ != NULL || this->var_ != NULL)
4828 ast_dump_context->ostream() << " := " ;
4830 ast_dump_context->ostream() << " <- " ;
4831 ast_dump_context->dump_expression(this->channel_);
4833 ast_dump_context->ostream() << ":" ;
4835 ast_dump_context->dump_block(this->statements_);
4838 // Class Select_clauses.
4840 // Traversal.
4843 Select_clauses::traverse(Traverse* traverse)
4845 for (Clauses::iterator p = this->clauses_.begin();
4846 p != this->clauses_.end();
4847 ++p)
4849 if (p->traverse(traverse) == TRAVERSE_EXIT)
4850 return TRAVERSE_EXIT;
4852 return TRAVERSE_CONTINUE;
4855 // Lowering. Here we pull out the channel and the send values, to
4856 // enforce the order of evaluation. We also add explicit send and
4857 // receive statements to the clauses.
4859 void
4860 Select_clauses::lower(Gogo* gogo, Named_object* function, Block* b,
4861 Temporary_statement* sel)
4863 for (Clauses::iterator p = this->clauses_.begin();
4864 p != this->clauses_.end();
4865 ++p)
4866 p->lower(gogo, function, b, sel);
4869 // Determine types.
4871 void
4872 Select_clauses::determine_types()
4874 for (Clauses::iterator p = this->clauses_.begin();
4875 p != this->clauses_.end();
4876 ++p)
4877 p->determine_types();
4880 // Check types.
4882 void
4883 Select_clauses::check_types()
4885 for (Clauses::iterator p = this->clauses_.begin();
4886 p != this->clauses_.end();
4887 ++p)
4888 p->check_types();
4891 // Return whether these select clauses fall through to the statement
4892 // following the overall select statement.
4894 bool
4895 Select_clauses::may_fall_through() const
4897 for (Clauses::const_iterator p = this->clauses_.begin();
4898 p != this->clauses_.end();
4899 ++p)
4900 if (p->may_fall_through())
4901 return true;
4902 return false;
4905 // Convert to the backend representation. We have already accumulated
4906 // all the select information. Now we call selectgo, which will
4907 // return the index of the clause to execute.
4909 Bstatement*
4910 Select_clauses::get_backend(Translate_context* context,
4911 Temporary_statement* sel,
4912 Unnamed_label *break_label,
4913 Location location)
4915 size_t count = this->clauses_.size();
4916 std::vector<std::vector<Bexpression*> > cases(count);
4917 std::vector<Bstatement*> clauses(count);
4919 Type* int32_type = Type::lookup_integer_type("int32");
4921 int i = 0;
4922 for (Clauses::iterator p = this->clauses_.begin();
4923 p != this->clauses_.end();
4924 ++p, ++i)
4926 int index = p->index();
4927 Expression* index_expr = Expression::make_integer_ul(index, int32_type,
4928 location);
4929 cases[i].push_back(index_expr->get_backend(context));
4931 Bstatement* s = p->get_statements_backend(context);
4932 Location gloc = (p->statements() == NULL
4933 ? p->location()
4934 : p->statements()->end_location());
4935 Bstatement* g = break_label->get_goto(context, gloc);
4937 if (s == NULL)
4938 clauses[i] = g;
4939 else
4940 clauses[i] = context->backend()->compound_statement(s, g);
4943 Expression* selref = Expression::make_temporary_reference(sel, location);
4944 selref = Expression::make_unary(OPERATOR_AND, selref, location);
4945 Expression* call = Runtime::make_call(Runtime::SELECTGO, location, 1,
4946 selref);
4947 context->gogo()->lower_expression(context->function(), NULL, &call);
4948 Bexpression* bcall = call->get_backend(context);
4950 if (count == 0)
4952 Bfunction* bfunction = context->function()->func_value()->get_decl();
4953 return context->backend()->expression_statement(bfunction, bcall);
4956 std::vector<Bstatement*> statements;
4957 statements.reserve(2);
4959 Bfunction* bfunction = context->function()->func_value()->get_decl();
4960 Bstatement* switch_stmt = context->backend()->switch_statement(bfunction,
4961 bcall,
4962 cases,
4963 clauses,
4964 location);
4965 statements.push_back(switch_stmt);
4967 Bstatement* ldef = break_label->get_definition(context);
4968 statements.push_back(ldef);
4970 return context->backend()->statement_list(statements);
4972 // Dump the AST representation for select clauses.
4974 void
4975 Select_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
4977 for (Clauses::const_iterator p = this->clauses_.begin();
4978 p != this->clauses_.end();
4979 ++p)
4980 p->dump_clause(ast_dump_context);
4983 // Class Select_statement.
4985 // Return the break label for this switch statement, creating it if
4986 // necessary.
4988 Unnamed_label*
4989 Select_statement::break_label()
4991 if (this->break_label_ == NULL)
4992 this->break_label_ = new Unnamed_label(this->location());
4993 return this->break_label_;
4996 // Lower a select statement. This will still return a select
4997 // statement, but it will be modified to implement the order of
4998 // evaluation rules, and to include the send and receive statements as
4999 // explicit statements in the clauses.
5001 Statement*
5002 Select_statement::do_lower(Gogo* gogo, Named_object* function,
5003 Block* enclosing, Statement_inserter*)
5005 if (this->is_lowered_)
5006 return this;
5008 Location loc = this->location();
5010 Block* b = new Block(enclosing, loc);
5012 go_assert(this->sel_ == NULL);
5014 int ncases = this->clauses_->size();
5015 Type* selstruct_type = Channel_type::select_type(ncases);
5016 this->sel_ = Statement::make_temporary(selstruct_type, NULL, loc);
5017 b->add_statement(this->sel_);
5019 int64_t selstruct_size;
5020 if (!selstruct_type->backend_type_size(gogo, &selstruct_size))
5022 go_assert(saw_errors());
5023 return Statement::make_error_statement(loc);
5026 Expression* ref = Expression::make_temporary_reference(this->sel_, loc);
5027 ref = Expression::make_unary(OPERATOR_AND, ref, loc);
5028 Expression* selstruct_size_expr =
5029 Expression::make_integer_int64(selstruct_size, NULL, loc);
5030 Expression* size_expr = Expression::make_integer_ul(ncases, NULL, loc);
5031 Expression* call = Runtime::make_call(Runtime::NEWSELECT, loc, 3,
5032 ref, selstruct_size_expr, size_expr);
5033 b->add_statement(Statement::make_statement(call, true));
5035 this->clauses_->lower(gogo, function, b, this->sel_);
5036 this->is_lowered_ = true;
5037 b->add_statement(this);
5039 return Statement::make_block_statement(b, loc);
5042 // Whether the select statement itself may fall through to the following
5043 // statement.
5045 bool
5046 Select_statement::do_may_fall_through() const
5048 // A select statement is terminating if no break statement
5049 // refers to it and all of its clauses are terminating.
5050 if (this->break_label_ != NULL)
5051 return true;
5052 return this->clauses_->may_fall_through();
5055 // Return the backend representation for a select statement.
5057 Bstatement*
5058 Select_statement::do_get_backend(Translate_context* context)
5060 return this->clauses_->get_backend(context, this->sel_, this->break_label(),
5061 this->location());
5064 // Dump the AST representation for a select statement.
5066 void
5067 Select_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
5069 ast_dump_context->print_indent();
5070 ast_dump_context->ostream() << "select";
5071 if (ast_dump_context->dump_subblocks())
5073 ast_dump_context->ostream() << " {" << dsuffix(location()) << std::endl;
5074 this->clauses_->dump_clauses(ast_dump_context);
5075 ast_dump_context->ostream() << "}";
5077 ast_dump_context->ostream() << std::endl;
5080 // Make a select statement.
5082 Select_statement*
5083 Statement::make_select_statement(Location location)
5085 return new Select_statement(location);
5088 // Class For_statement.
5090 // Traversal.
5093 For_statement::do_traverse(Traverse* traverse)
5095 if (this->init_ != NULL)
5097 if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
5098 return TRAVERSE_EXIT;
5100 if (this->cond_ != NULL)
5102 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
5103 return TRAVERSE_EXIT;
5105 if (this->post_ != NULL)
5107 if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
5108 return TRAVERSE_EXIT;
5110 return this->statements_->traverse(traverse);
5113 // Lower a For_statement into if statements and gotos. Getting rid of
5114 // complex statements make it easier to handle garbage collection.
5116 Statement*
5117 For_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
5118 Statement_inserter*)
5120 Statement* s;
5121 Location loc = this->location();
5123 Block* b = new Block(enclosing, this->location());
5124 if (this->init_ != NULL)
5126 s = Statement::make_block_statement(this->init_,
5127 this->init_->start_location());
5128 b->add_statement(s);
5131 Unnamed_label* entry = NULL;
5132 if (this->cond_ != NULL)
5134 entry = new Unnamed_label(this->location());
5135 b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
5138 Unnamed_label* top = new Unnamed_label(this->location());
5139 top->set_derived_from(this);
5140 b->add_statement(Statement::make_unnamed_label_statement(top));
5142 s = Statement::make_block_statement(this->statements_,
5143 this->statements_->start_location());
5144 b->add_statement(s);
5146 Location end_loc = this->statements_->end_location();
5148 Unnamed_label* cont = this->continue_label_;
5149 if (cont != NULL)
5150 b->add_statement(Statement::make_unnamed_label_statement(cont));
5152 if (this->post_ != NULL)
5154 s = Statement::make_block_statement(this->post_,
5155 this->post_->start_location());
5156 b->add_statement(s);
5157 end_loc = this->post_->end_location();
5160 if (this->cond_ == NULL)
5161 b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
5162 else
5164 b->add_statement(Statement::make_unnamed_label_statement(entry));
5166 Location cond_loc = this->cond_->location();
5167 Block* then_block = new Block(b, cond_loc);
5168 s = Statement::make_goto_unnamed_statement(top, cond_loc);
5169 then_block->add_statement(s);
5171 s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
5172 b->add_statement(s);
5175 Unnamed_label* brk = this->break_label_;
5176 if (brk != NULL)
5177 b->add_statement(Statement::make_unnamed_label_statement(brk));
5179 b->set_end_location(end_loc);
5181 Statement* bs = Statement::make_block_statement(b, loc);
5182 bs->block_statement()->set_is_lowered_for_statement();
5183 return bs;
5186 // Return the break label, creating it if necessary.
5188 Unnamed_label*
5189 For_statement::break_label()
5191 if (this->break_label_ == NULL)
5192 this->break_label_ = new Unnamed_label(this->location());
5193 return this->break_label_;
5196 // Return the continue LABEL_EXPR.
5198 Unnamed_label*
5199 For_statement::continue_label()
5201 if (this->continue_label_ == NULL)
5202 this->continue_label_ = new Unnamed_label(this->location());
5203 return this->continue_label_;
5206 // Set the break and continue labels a for statement. This is used
5207 // when lowering a for range statement.
5209 void
5210 For_statement::set_break_continue_labels(Unnamed_label* break_label,
5211 Unnamed_label* continue_label)
5213 go_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
5214 this->break_label_ = break_label;
5215 this->continue_label_ = continue_label;
5218 // Whether the overall statement may fall through.
5220 bool
5221 For_statement::do_may_fall_through() const
5223 // A for loop is terminating if it has no condition and
5224 // no break statement.
5225 if(this->cond_ != NULL)
5226 return true;
5227 if(this->break_label_ != NULL)
5228 return true;
5229 return false;
5232 // Dump the AST representation for a for statement.
5234 void
5235 For_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
5237 if (this->init_ != NULL && ast_dump_context->dump_subblocks())
5239 ast_dump_context->print_indent();
5240 ast_dump_context->indent();
5241 ast_dump_context->ostream() << "// INIT " << std::endl;
5242 ast_dump_context->dump_block(this->init_);
5243 ast_dump_context->unindent();
5245 ast_dump_context->print_indent();
5246 ast_dump_context->ostream() << "for ";
5247 if (this->cond_ != NULL)
5248 ast_dump_context->dump_expression(this->cond_);
5250 if (ast_dump_context->dump_subblocks())
5252 ast_dump_context->ostream() << " {" << std::endl;
5253 ast_dump_context->dump_block(this->statements_);
5254 if (this->init_ != NULL)
5256 ast_dump_context->print_indent();
5257 ast_dump_context->ostream() << "// POST " << std::endl;
5258 ast_dump_context->dump_block(this->post_);
5260 ast_dump_context->unindent();
5262 ast_dump_context->print_indent();
5263 ast_dump_context->ostream() << "}";
5266 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
5269 // Make a for statement.
5271 For_statement*
5272 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
5273 Location location)
5275 return new For_statement(init, cond, post, location);
5278 // Class For_range_statement.
5280 // Traversal.
5283 For_range_statement::do_traverse(Traverse* traverse)
5285 if (this->index_var_ != NULL)
5287 if (this->traverse_expression(traverse, &this->index_var_)
5288 == TRAVERSE_EXIT)
5289 return TRAVERSE_EXIT;
5291 if (this->value_var_ != NULL)
5293 if (this->traverse_expression(traverse, &this->value_var_)
5294 == TRAVERSE_EXIT)
5295 return TRAVERSE_EXIT;
5297 if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
5298 return TRAVERSE_EXIT;
5299 return this->statements_->traverse(traverse);
5302 // Lower a for range statement. For simplicity we lower this into a
5303 // for statement, which will then be lowered in turn to goto
5304 // statements.
5306 Statement*
5307 For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing,
5308 Statement_inserter*)
5310 Type* range_type = this->range_->type();
5311 if (range_type->points_to() != NULL
5312 && range_type->points_to()->array_type() != NULL
5313 && !range_type->points_to()->is_slice_type())
5314 range_type = range_type->points_to();
5316 Type* index_type;
5317 Type* value_type = NULL;
5318 if (range_type->array_type() != NULL)
5320 index_type = Type::lookup_integer_type("int");
5321 value_type = range_type->array_type()->element_type();
5323 else if (range_type->is_string_type())
5325 index_type = Type::lookup_integer_type("int");
5326 value_type = gogo->lookup_global("rune")->type_value();
5328 else if (range_type->map_type() != NULL)
5330 index_type = range_type->map_type()->key_type();
5331 value_type = range_type->map_type()->val_type();
5333 else if (range_type->channel_type() != NULL)
5335 index_type = range_type->channel_type()->element_type();
5336 if (this->value_var_ != NULL)
5338 if (!this->value_var_->type()->is_error())
5339 this->report_error(_("too many variables for range clause "
5340 "with channel"));
5341 return Statement::make_error_statement(this->location());
5344 else
5346 this->report_error(_("range clause must have "
5347 "array, slice, string, map, or channel type"));
5348 return Statement::make_error_statement(this->location());
5351 Location loc = this->location();
5352 Block* temp_block = new Block(enclosing, loc);
5354 Named_object* range_object = NULL;
5355 Temporary_statement* range_temp = NULL;
5356 Var_expression* ve = this->range_->var_expression();
5357 if (ve != NULL)
5358 range_object = ve->named_object();
5359 else
5361 range_temp = Statement::make_temporary(NULL, this->range_, loc);
5362 temp_block->add_statement(range_temp);
5363 this->range_ = NULL;
5366 Temporary_statement* index_temp = Statement::make_temporary(index_type,
5367 NULL, loc);
5368 temp_block->add_statement(index_temp);
5370 Temporary_statement* value_temp = NULL;
5371 if (this->value_var_ != NULL)
5373 value_temp = Statement::make_temporary(value_type, NULL, loc);
5374 temp_block->add_statement(value_temp);
5377 Block* body = new Block(temp_block, loc);
5379 Block* init;
5380 Expression* cond;
5381 Block* iter_init;
5382 Block* post;
5384 // Arrange to do a loop appropriate for the type. We will produce
5385 // for INIT ; COND ; POST {
5386 // ITER_INIT
5387 // INDEX = INDEX_TEMP
5388 // VALUE = VALUE_TEMP // If there is a value
5389 // original statements
5390 // }
5392 if (range_type->is_slice_type())
5393 this->lower_range_slice(gogo, temp_block, body, range_object, range_temp,
5394 index_temp, value_temp, &init, &cond, &iter_init,
5395 &post);
5396 else if (range_type->array_type() != NULL)
5397 this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
5398 index_temp, value_temp, &init, &cond, &iter_init,
5399 &post);
5400 else if (range_type->is_string_type())
5401 this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
5402 index_temp, value_temp, &init, &cond, &iter_init,
5403 &post);
5404 else if (range_type->map_type() != NULL)
5405 this->lower_range_map(gogo, range_type->map_type(), temp_block, body,
5406 range_object, range_temp, index_temp, value_temp,
5407 &init, &cond, &iter_init, &post);
5408 else if (range_type->channel_type() != NULL)
5409 this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
5410 index_temp, value_temp, &init, &cond, &iter_init,
5411 &post);
5412 else
5413 go_unreachable();
5415 if (iter_init != NULL)
5416 body->add_statement(Statement::make_block_statement(iter_init, loc));
5418 if (this->index_var_ != NULL)
5420 Statement* assign;
5421 Expression* index_ref =
5422 Expression::make_temporary_reference(index_temp, loc);
5423 if (this->value_var_ == NULL)
5424 assign = Statement::make_assignment(this->index_var_, index_ref, loc);
5425 else
5427 Expression_list* lhs = new Expression_list();
5428 lhs->push_back(this->index_var_);
5429 lhs->push_back(this->value_var_);
5431 Expression_list* rhs = new Expression_list();
5432 rhs->push_back(index_ref);
5433 rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
5435 assign = Statement::make_tuple_assignment(lhs, rhs, loc);
5437 body->add_statement(assign);
5440 body->add_statement(Statement::make_block_statement(this->statements_, loc));
5442 body->set_end_location(this->statements_->end_location());
5444 For_statement* loop = Statement::make_for_statement(init, cond, post,
5445 this->location());
5446 loop->add_statements(body);
5447 loop->set_break_continue_labels(this->break_label_, this->continue_label_);
5449 temp_block->add_statement(loop);
5451 return Statement::make_block_statement(temp_block, loc);
5454 // Return a reference to the range, which may be in RANGE_OBJECT or in
5455 // RANGE_TEMP.
5457 Expression*
5458 For_range_statement::make_range_ref(Named_object* range_object,
5459 Temporary_statement* range_temp,
5460 Location loc)
5462 if (range_object != NULL)
5463 return Expression::make_var_reference(range_object, loc);
5464 else
5465 return Expression::make_temporary_reference(range_temp, loc);
5468 // Return a call to the predeclared function FUNCNAME passing a
5469 // reference to the temporary variable ARG.
5471 Call_expression*
5472 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
5473 Expression* arg,
5474 Location loc)
5476 Named_object* no = gogo->lookup_global(funcname);
5477 go_assert(no != NULL && no->is_function_declaration());
5478 Expression* func = Expression::make_func_reference(no, NULL, loc);
5479 Expression_list* params = new Expression_list();
5480 params->push_back(arg);
5481 return Expression::make_call(func, params, false, loc);
5484 // Lower a for range over an array.
5486 void
5487 For_range_statement::lower_range_array(Gogo* gogo,
5488 Block* enclosing,
5489 Block* body_block,
5490 Named_object* range_object,
5491 Temporary_statement* range_temp,
5492 Temporary_statement* index_temp,
5493 Temporary_statement* value_temp,
5494 Block** pinit,
5495 Expression** pcond,
5496 Block** piter_init,
5497 Block** ppost)
5499 Location loc = this->location();
5501 // The loop we generate:
5502 // len_temp := len(range)
5503 // range_temp := range
5504 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5505 // value_temp = range_temp[index_temp]
5506 // index = index_temp
5507 // value = value_temp
5508 // original body
5509 // }
5511 // Set *PINIT to
5512 // var len_temp int
5513 // len_temp = len(range)
5514 // index_temp = 0
5516 Block* init = new Block(enclosing, loc);
5518 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5519 range_temp = Statement::make_temporary(NULL, ref, loc);
5520 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
5521 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
5522 len_call, loc);
5523 init->add_statement(range_temp);
5524 init->add_statement(len_temp);
5526 Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
5528 Temporary_reference_expression* tref =
5529 Expression::make_temporary_reference(index_temp, loc);
5530 tref->set_is_lvalue();
5531 Statement* s = Statement::make_assignment(tref, zexpr, loc);
5532 init->add_statement(s);
5534 *pinit = init;
5536 // Set *PCOND to
5537 // index_temp < len_temp
5539 ref = Expression::make_temporary_reference(index_temp, loc);
5540 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
5541 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
5543 *pcond = lt;
5545 // Set *PITER_INIT to
5546 // value_temp = range[index_temp]
5548 Block* iter_init = NULL;
5549 if (value_temp != NULL)
5551 iter_init = new Block(body_block, loc);
5553 ref = Expression::make_temporary_reference(range_temp, loc);
5554 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
5555 Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
5557 tref = Expression::make_temporary_reference(value_temp, loc);
5558 tref->set_is_lvalue();
5559 s = Statement::make_assignment(tref, index, loc);
5561 iter_init->add_statement(s);
5563 *piter_init = iter_init;
5565 // Set *PPOST to
5566 // index_temp++
5568 Block* post = new Block(enclosing, loc);
5569 tref = Expression::make_temporary_reference(index_temp, loc);
5570 tref->set_is_lvalue();
5571 s = Statement::make_inc_statement(tref);
5572 post->add_statement(s);
5573 *ppost = post;
5576 // Lower a for range over a slice.
5578 void
5579 For_range_statement::lower_range_slice(Gogo* gogo,
5580 Block* enclosing,
5581 Block* body_block,
5582 Named_object* range_object,
5583 Temporary_statement* range_temp,
5584 Temporary_statement* index_temp,
5585 Temporary_statement* value_temp,
5586 Block** pinit,
5587 Expression** pcond,
5588 Block** piter_init,
5589 Block** ppost)
5591 Location loc = this->location();
5593 // The loop we generate:
5594 // for_temp := range
5595 // len_temp := len(for_temp)
5596 // for index_temp = 0; index_temp < len_temp; index_temp++ {
5597 // value_temp = for_temp[index_temp]
5598 // index = index_temp
5599 // value = value_temp
5600 // original body
5601 // }
5603 // Using for_temp means that we don't need to check bounds when
5604 // fetching range_temp[index_temp].
5606 // Set *PINIT to
5607 // range_temp := range
5608 // var len_temp int
5609 // len_temp = len(range_temp)
5610 // index_temp = 0
5612 Block* init = new Block(enclosing, loc);
5614 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5615 Temporary_statement* for_temp = Statement::make_temporary(NULL, ref, loc);
5616 init->add_statement(for_temp);
5618 ref = Expression::make_temporary_reference(for_temp, loc);
5619 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
5620 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
5621 len_call, loc);
5622 init->add_statement(len_temp);
5624 Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
5626 Temporary_reference_expression* tref =
5627 Expression::make_temporary_reference(index_temp, loc);
5628 tref->set_is_lvalue();
5629 Statement* s = Statement::make_assignment(tref, zexpr, loc);
5630 init->add_statement(s);
5632 *pinit = init;
5634 // Set *PCOND to
5635 // index_temp < len_temp
5637 ref = Expression::make_temporary_reference(index_temp, loc);
5638 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
5639 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
5641 *pcond = lt;
5643 // Set *PITER_INIT to
5644 // value_temp = range[index_temp]
5646 Block* iter_init = NULL;
5647 if (value_temp != NULL)
5649 iter_init = new Block(body_block, loc);
5651 ref = Expression::make_temporary_reference(for_temp, loc);
5652 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
5653 Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
5655 tref = Expression::make_temporary_reference(value_temp, loc);
5656 tref->set_is_lvalue();
5657 s = Statement::make_assignment(tref, index, loc);
5659 iter_init->add_statement(s);
5661 *piter_init = iter_init;
5663 // Set *PPOST to
5664 // index_temp++
5666 Block* post = new Block(enclosing, loc);
5667 tref = Expression::make_temporary_reference(index_temp, loc);
5668 tref->set_is_lvalue();
5669 s = Statement::make_inc_statement(tref);
5670 post->add_statement(s);
5671 *ppost = post;
5674 // Lower a for range over a string.
5676 void
5677 For_range_statement::lower_range_string(Gogo* gogo,
5678 Block* enclosing,
5679 Block* body_block,
5680 Named_object* range_object,
5681 Temporary_statement* range_temp,
5682 Temporary_statement* index_temp,
5683 Temporary_statement* value_temp,
5684 Block** pinit,
5685 Expression** pcond,
5686 Block** piter_init,
5687 Block** ppost)
5689 Location loc = this->location();
5691 // The loop we generate:
5692 // len_temp := len(range)
5693 // var next_index_temp int
5694 // for index_temp = 0; index_temp < len_temp; index_temp = next_index_temp {
5695 // value_temp = rune(range[index_temp])
5696 // if value_temp < utf8.RuneSelf {
5697 // next_index_temp = index_temp + 1
5698 // } else {
5699 // value_temp, next_index_temp = decoderune(range, index_temp)
5700 // }
5701 // index = index_temp
5702 // value = value_temp
5703 // // original body
5704 // }
5706 // Set *PINIT to
5707 // len_temp := len(range)
5708 // var next_index_temp int
5709 // index_temp = 0
5710 // var value_temp rune // if value_temp not passed in
5712 Block* init = new Block(enclosing, loc);
5714 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5715 Call_expression* call = this->call_builtin(gogo, "len", ref, loc);
5716 Temporary_statement* len_temp =
5717 Statement::make_temporary(index_temp->type(), call, loc);
5718 init->add_statement(len_temp);
5720 Temporary_statement* next_index_temp =
5721 Statement::make_temporary(index_temp->type(), NULL, loc);
5722 init->add_statement(next_index_temp);
5724 Temporary_reference_expression* index_ref =
5725 Expression::make_temporary_reference(index_temp, loc);
5726 index_ref->set_is_lvalue();
5727 Expression* zexpr = Expression::make_integer_ul(0, index_temp->type(), loc);
5728 Statement* s = Statement::make_assignment(index_ref, zexpr, loc);
5729 init->add_statement(s);
5731 Type* rune_type;
5732 if (value_temp != NULL)
5733 rune_type = value_temp->type();
5734 else
5736 rune_type = gogo->lookup_global("rune")->type_value();
5737 value_temp = Statement::make_temporary(rune_type, NULL, loc);
5738 init->add_statement(value_temp);
5741 *pinit = init;
5743 // Set *PCOND to
5744 // index_temp < len_temp
5746 index_ref = Expression::make_temporary_reference(index_temp, loc);
5747 Expression* len_ref =
5748 Expression::make_temporary_reference(len_temp, loc);
5749 *pcond = Expression::make_binary(OPERATOR_LT, index_ref, len_ref, loc);
5751 // Set *PITER_INIT to
5752 // value_temp = rune(range[index_temp])
5753 // if value_temp < utf8.RuneSelf {
5754 // next_index_temp = index_temp + 1
5755 // } else {
5756 // value_temp, next_index_temp = decoderune(range, index_temp)
5757 // }
5759 Block* iter_init = new Block(body_block, loc);
5761 ref = this->make_range_ref(range_object, range_temp, loc);
5762 index_ref = Expression::make_temporary_reference(index_temp, loc);
5763 ref = Expression::make_string_index(ref, index_ref, NULL, loc);
5764 ref = Expression::make_cast(rune_type, ref, loc);
5765 Temporary_reference_expression* value_ref =
5766 Expression::make_temporary_reference(value_temp, loc);
5767 value_ref->set_is_lvalue();
5768 s = Statement::make_assignment(value_ref, ref, loc);
5769 iter_init->add_statement(s);
5771 value_ref = Expression::make_temporary_reference(value_temp, loc);
5772 Expression* rune_self = Expression::make_integer_ul(0x80, rune_type, loc);
5773 Expression* cond = Expression::make_binary(OPERATOR_LT, value_ref, rune_self,
5774 loc);
5776 Block* then_block = new Block(iter_init, loc);
5778 Temporary_reference_expression* lhs =
5779 Expression::make_temporary_reference(next_index_temp, loc);
5780 lhs->set_is_lvalue();
5781 index_ref = Expression::make_temporary_reference(index_temp, loc);
5782 Expression* one = Expression::make_integer_ul(1, index_temp->type(), loc);
5783 Expression* sum = Expression::make_binary(OPERATOR_PLUS, index_ref, one,
5784 loc);
5785 s = Statement::make_assignment(lhs, sum, loc);
5786 then_block->add_statement(s);
5788 Block* else_block = new Block(iter_init, loc);
5790 ref = this->make_range_ref(range_object, range_temp, loc);
5791 index_ref = Expression::make_temporary_reference(index_temp, loc);
5792 call = Runtime::make_call(Runtime::DECODERUNE, loc, 2, ref, index_ref);
5794 value_ref = Expression::make_temporary_reference(value_temp, loc);
5795 value_ref->set_is_lvalue();
5796 Expression* res = Expression::make_call_result(call, 0);
5797 s = Statement::make_assignment(value_ref, res, loc);
5798 else_block->add_statement(s);
5800 lhs = Expression::make_temporary_reference(next_index_temp, loc);
5801 lhs->set_is_lvalue();
5802 res = Expression::make_call_result(call, 1);
5803 s = Statement::make_assignment(lhs, res, loc);
5804 else_block->add_statement(s);
5806 s = Statement::make_if_statement(cond, then_block, else_block, loc);
5807 iter_init->add_statement(s);
5809 *piter_init = iter_init;
5811 // Set *PPOST to
5812 // index_temp = next_index_temp
5814 Block* post = new Block(enclosing, loc);
5816 index_ref = Expression::make_temporary_reference(index_temp, loc);
5817 index_ref->set_is_lvalue();
5818 ref = Expression::make_temporary_reference(next_index_temp, loc);
5819 s = Statement::make_assignment(index_ref, ref, loc);
5821 post->add_statement(s);
5822 *ppost = post;
5825 // Lower a for range over a map.
5827 void
5828 For_range_statement::lower_range_map(Gogo* gogo,
5829 Map_type* map_type,
5830 Block* enclosing,
5831 Block* body_block,
5832 Named_object* range_object,
5833 Temporary_statement* range_temp,
5834 Temporary_statement* index_temp,
5835 Temporary_statement* value_temp,
5836 Block** pinit,
5837 Expression** pcond,
5838 Block** piter_init,
5839 Block** ppost)
5841 Location loc = this->location();
5843 // The runtime uses a struct to handle ranges over a map. The
5844 // struct is built by Map_type::hiter_type for a specific map type.
5846 // The loop we generate:
5847 // var hiter map_iteration_struct
5848 // for mapiterinit(type, range, &hiter); hiter.key != nil; mapiternext(&hiter) {
5849 // index_temp = *hiter.key
5850 // value_temp = *hiter.val
5851 // index = index_temp
5852 // value = value_temp
5853 // original body
5854 // }
5856 // Set *PINIT to
5857 // var hiter map_iteration_struct
5858 // runtime.mapiterinit(type, range, &hiter)
5860 Block* init = new Block(enclosing, loc);
5862 Type* map_iteration_type = map_type->hiter_type(gogo);
5863 Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
5864 NULL, loc);
5865 init->add_statement(hiter);
5867 Expression* p1 = Expression::make_type_descriptor(map_type, loc);
5868 Expression* p2 = this->make_range_ref(range_object, range_temp, loc);
5869 Expression* ref = Expression::make_temporary_reference(hiter, loc);
5870 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
5871 Expression* call = Runtime::make_call(Runtime::MAPITERINIT, loc, 3,
5872 p1, p2, p3);
5873 init->add_statement(Statement::make_statement(call, true));
5875 *pinit = init;
5877 // Set *PCOND to
5878 // hiter.key != nil
5880 ref = Expression::make_temporary_reference(hiter, loc);
5881 ref = Expression::make_field_reference(ref, 0, loc);
5882 Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, ref,
5883 Expression::make_nil(loc),
5884 loc);
5885 *pcond = ne;
5887 // Set *PITER_INIT to
5888 // index_temp = *hiter.key
5889 // value_temp = *hiter.val
5891 Block* iter_init = new Block(body_block, loc);
5893 Expression* lhs = Expression::make_temporary_reference(index_temp, loc);
5894 Expression* rhs = Expression::make_temporary_reference(hiter, loc);
5895 rhs = Expression::make_field_reference(ref, 0, loc);
5896 rhs = Expression::make_unary(OPERATOR_MULT, ref, loc);
5897 Statement* set = Statement::make_assignment(lhs, rhs, loc);
5898 iter_init->add_statement(set);
5900 if (value_temp != NULL)
5902 lhs = Expression::make_temporary_reference(value_temp, loc);
5903 rhs = Expression::make_temporary_reference(hiter, loc);
5904 rhs = Expression::make_field_reference(rhs, 1, loc);
5905 rhs = Expression::make_unary(OPERATOR_MULT, rhs, loc);
5906 set = Statement::make_assignment(lhs, rhs, loc);
5907 iter_init->add_statement(set);
5910 *piter_init = iter_init;
5912 // Set *PPOST to
5913 // mapiternext(&hiter)
5915 Block* post = new Block(enclosing, loc);
5917 ref = Expression::make_temporary_reference(hiter, loc);
5918 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
5919 call = Runtime::make_call(Runtime::MAPITERNEXT, loc, 1, p1);
5920 post->add_statement(Statement::make_statement(call, true));
5922 *ppost = post;
5925 // Lower a for range over a channel.
5927 void
5928 For_range_statement::lower_range_channel(Gogo*,
5929 Block*,
5930 Block* body_block,
5931 Named_object* range_object,
5932 Temporary_statement* range_temp,
5933 Temporary_statement* index_temp,
5934 Temporary_statement* value_temp,
5935 Block** pinit,
5936 Expression** pcond,
5937 Block** piter_init,
5938 Block** ppost)
5940 go_assert(value_temp == NULL);
5942 Location loc = this->location();
5944 // The loop we generate:
5945 // for {
5946 // index_temp, ok_temp = <-range
5947 // if !ok_temp {
5948 // break
5949 // }
5950 // index = index_temp
5951 // original body
5952 // }
5954 // We have no initialization code, no condition, and no post code.
5956 *pinit = NULL;
5957 *pcond = NULL;
5958 *ppost = NULL;
5960 // Set *PITER_INIT to
5961 // index_temp, ok_temp = <-range
5962 // if !ok_temp {
5963 // break
5964 // }
5966 Block* iter_init = new Block(body_block, loc);
5968 Temporary_statement* ok_temp =
5969 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
5970 iter_init->add_statement(ok_temp);
5972 Expression* cref = this->make_range_ref(range_object, range_temp, loc);
5973 Temporary_reference_expression* iref =
5974 Expression::make_temporary_reference(index_temp, loc);
5975 iref->set_is_lvalue();
5976 Temporary_reference_expression* oref =
5977 Expression::make_temporary_reference(ok_temp, loc);
5978 oref->set_is_lvalue();
5979 Statement* s = Statement::make_tuple_receive_assignment(iref, oref, cref,
5980 loc);
5981 iter_init->add_statement(s);
5983 Block* then_block = new Block(iter_init, loc);
5984 s = Statement::make_break_statement(this->break_label(), loc);
5985 then_block->add_statement(s);
5987 oref = Expression::make_temporary_reference(ok_temp, loc);
5988 Expression* cond = Expression::make_unary(OPERATOR_NOT, oref, loc);
5989 s = Statement::make_if_statement(cond, then_block, NULL, loc);
5990 iter_init->add_statement(s);
5992 *piter_init = iter_init;
5995 // Return the break LABEL_EXPR.
5997 Unnamed_label*
5998 For_range_statement::break_label()
6000 if (this->break_label_ == NULL)
6001 this->break_label_ = new Unnamed_label(this->location());
6002 return this->break_label_;
6005 // Return the continue LABEL_EXPR.
6007 Unnamed_label*
6008 For_range_statement::continue_label()
6010 if (this->continue_label_ == NULL)
6011 this->continue_label_ = new Unnamed_label(this->location());
6012 return this->continue_label_;
6015 // Dump the AST representation for a for range statement.
6017 void
6018 For_range_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
6021 ast_dump_context->print_indent();
6022 ast_dump_context->ostream() << "for ";
6023 ast_dump_context->dump_expression(this->index_var_);
6024 if (this->value_var_ != NULL)
6026 ast_dump_context->ostream() << ", ";
6027 ast_dump_context->dump_expression(this->value_var_);
6030 ast_dump_context->ostream() << " = range ";
6031 ast_dump_context->dump_expression(this->range_);
6032 if (ast_dump_context->dump_subblocks())
6034 ast_dump_context->ostream() << " {" << std::endl;
6036 ast_dump_context->indent();
6038 ast_dump_context->dump_block(this->statements_);
6040 ast_dump_context->unindent();
6041 ast_dump_context->print_indent();
6042 ast_dump_context->ostream() << "}";
6044 ast_dump_context->ostream() << dsuffix(location()) << std::endl;
6047 // Make a for statement with a range clause.
6049 For_range_statement*
6050 Statement::make_for_range_statement(Expression* index_var,
6051 Expression* value_var,
6052 Expression* range,
6053 Location location)
6055 return new For_range_statement(index_var, value_var, range, location);