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.
11 #ifndef ENABLE_BUILD_WITH_CXX
20 #include "tree-iterator.h"
21 #include "tree-flow.h"
24 #ifndef ENABLE_BUILD_WITH_CXX
30 #include "expressions.h"
34 #include "statements.h"
38 Statement::Statement(Statement_classification classification,
39 source_location location)
40 : classification_(classification), location_(location)
44 Statement::~Statement()
48 // Traverse the tree. The work of walking the components is handled
52 Statement::traverse(Block* block, size_t* pindex, Traverse* traverse)
54 if (this->classification_ == STATEMENT_ERROR)
55 return TRAVERSE_CONTINUE;
57 unsigned int traverse_mask = traverse->traverse_mask();
59 if ((traverse_mask & Traverse::traverse_statements) != 0)
61 int t = traverse->statement(block, pindex, this);
62 if (t == TRAVERSE_EXIT)
64 else if (t == TRAVERSE_SKIP_COMPONENTS)
65 return TRAVERSE_CONTINUE;
68 // No point in checking traverse_mask here--a statement may contain
69 // other blocks or statements, and if we got here we always want to
71 return this->do_traverse(traverse);
74 // Traverse the contents of a statement.
77 Statement::traverse_contents(Traverse* traverse)
79 return this->do_traverse(traverse);
82 // Traverse assignments.
85 Statement::traverse_assignments(Traverse_assignments* tassign)
87 if (this->classification_ == STATEMENT_ERROR)
89 return this->do_traverse_assignments(tassign);
92 // Traverse an expression in a statement. This is a helper function
96 Statement::traverse_expression(Traverse* traverse, Expression** expr)
98 if ((traverse->traverse_mask()
99 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
100 return TRAVERSE_CONTINUE;
101 return Expression::traverse(expr, traverse);
104 // Traverse an expression list in a statement. This is a helper
105 // function for child classes.
108 Statement::traverse_expression_list(Traverse* traverse,
109 Expression_list* expr_list)
111 if (expr_list == NULL)
112 return TRAVERSE_CONTINUE;
113 if ((traverse->traverse_mask()
114 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
115 return TRAVERSE_CONTINUE;
116 return expr_list->traverse(traverse);
119 // Traverse a type in a statement. This is a helper function for
123 Statement::traverse_type(Traverse* traverse, Type* type)
125 if ((traverse->traverse_mask()
126 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
127 return TRAVERSE_CONTINUE;
128 return Type::traverse(type, traverse);
131 // Set type information for unnamed constants. This is really done by
135 Statement::determine_types()
137 this->do_determine_types();
140 // If this is a thunk statement, return it.
143 Statement::thunk_statement()
145 Thunk_statement* ret = this->convert<Thunk_statement, STATEMENT_GO>();
147 ret = this->convert<Thunk_statement, STATEMENT_DEFER>();
151 // Get a tree for a Statement. This is really done by the child
155 Statement::get_tree(Translate_context* context)
157 if (this->classification_ == STATEMENT_ERROR)
158 return error_mark_node;
160 return this->do_get_tree(context);
163 // Build tree nodes and set locations.
166 Statement::build_stmt_1(int tree_code_value, tree node)
168 tree ret = build1(static_cast<tree_code>(tree_code_value),
169 void_type_node, node);
170 SET_EXPR_LOCATION(ret, this->location_);
174 // Note that this statement is erroneous. This is called by children
175 // when they discover an error.
178 Statement::set_is_error()
180 this->classification_ = STATEMENT_ERROR;
183 // For children to call to report an error conveniently.
186 Statement::report_error(const char* msg)
188 error_at(this->location_, "%s", msg);
189 this->set_is_error();
192 // An error statement, used to avoid crashing after we report an
195 class Error_statement : public Statement
198 Error_statement(source_location location)
199 : Statement(STATEMENT_ERROR, location)
204 do_traverse(Traverse*)
205 { return TRAVERSE_CONTINUE; }
208 do_get_tree(Translate_context*)
209 { gcc_unreachable(); }
212 // Make an error statement.
215 Statement::make_error_statement(source_location location)
217 return new Error_statement(location);
220 // Class Variable_declaration_statement.
222 Variable_declaration_statement::Variable_declaration_statement(
224 : Statement(STATEMENT_VARIABLE_DECLARATION, var->var_value()->location()),
229 // We don't actually traverse the variable here; it was traversed
230 // while traversing the Block.
233 Variable_declaration_statement::do_traverse(Traverse*)
235 return TRAVERSE_CONTINUE;
238 // Traverse the assignments in a variable declaration. Note that this
239 // traversal is different from the usual traversal.
242 Variable_declaration_statement::do_traverse_assignments(
243 Traverse_assignments* tassign)
245 tassign->initialize_variable(this->var_);
249 // Return the tree for a variable declaration.
252 Variable_declaration_statement::do_get_tree(Translate_context* context)
254 Variable* var = this->var_->var_value();
255 Bvariable* bvar = this->var_->get_backend_variable(context->gogo(),
256 context->function());
257 tree init = var->get_init_tree(context->gogo(), context->function());
258 Bexpression* binit = init == NULL_TREE ? NULL : tree_to_expr(init);
260 if (!var->is_in_heap())
262 gcc_assert(binit != NULL);
263 ret = context->backend()->init_statement(bvar, binit);
267 // Something takes the address of this variable, so the value is
268 // stored in the heap. Initialize it to newly allocated memory
269 // space, and assign the initial value to the new space.
270 source_location loc = this->location();
271 tree decl = var_to_tree(bvar);
272 tree decl_type = TREE_TYPE(decl);
273 gcc_assert(POINTER_TYPE_P(decl_type));
274 tree size = TYPE_SIZE_UNIT(TREE_TYPE(decl_type));
275 tree space = context->gogo()->allocate_memory(var->type(), size, loc);
277 space = save_expr(space);
278 space = fold_convert_loc(loc, decl_type, space);
279 Bstatement* s1 = context->backend()->init_statement(bvar,
280 tree_to_expr(space));
285 tree indir = build_fold_indirect_ref_loc(loc, space);
286 Bexpression* bindir = tree_to_expr(indir);
287 Bstatement* s2 = context->backend()->assignment_statement(bindir,
290 ret = context->backend()->compound_statement(s1, s2);
293 return stat_to_tree(ret);
296 // Make a variable declaration.
299 Statement::make_variable_declaration(Named_object* var)
301 return new Variable_declaration_statement(var);
304 // Class Temporary_statement.
306 // Return the type of the temporary variable.
309 Temporary_statement::type() const
311 return this->type_ != NULL ? this->type_ : this->init_->type();
317 Temporary_statement::do_traverse(Traverse* traverse)
319 if (this->type_ != NULL
320 && this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
321 return TRAVERSE_EXIT;
322 if (this->init_ == NULL)
323 return TRAVERSE_CONTINUE;
325 return this->traverse_expression(traverse, &this->init_);
328 // Traverse assignments.
331 Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
333 if (this->init_ == NULL)
335 tassign->value(&this->init_, true, true);
342 Temporary_statement::do_determine_types()
344 if (this->type_ != NULL && this->type_->is_abstract())
345 this->type_ = this->type_->make_non_abstract_type();
347 if (this->init_ != NULL)
349 if (this->type_ == NULL)
350 this->init_->determine_type_no_context();
353 Type_context context(this->type_, false);
354 this->init_->determine_type(&context);
358 if (this->type_ == NULL)
360 this->type_ = this->init_->type();
361 gcc_assert(!this->type_->is_abstract());
368 Temporary_statement::do_check_types(Gogo*)
370 if (this->type_ != NULL && this->init_ != NULL)
373 if (!Type::are_assignable(this->type_, this->init_->type(), &reason))
376 error_at(this->location(), "incompatible types in assignment");
378 error_at(this->location(), "incompatible types in assignment (%s)",
380 this->set_is_error();
388 Temporary_statement::do_get_tree(Translate_context* context)
390 gcc_assert(this->bvariable_ == NULL);
392 // FIXME: Permitting FUNCTION to be NULL here is a temporary measure
393 // until we have a better representation of the init function.
394 Named_object* function = context->function();
395 Bfunction* bfunction;
396 if (function == NULL)
399 bfunction = tree_to_function(function->func_value()->get_decl());
401 Btype* btype = tree_to_type(this->type()->get_tree(context->gogo()));
404 if (this->init_ == NULL)
406 else if (this->type_ == NULL)
407 binit = tree_to_expr(this->init_->get_tree(context));
410 Expression* init = Expression::make_cast(this->type_, this->init_,
412 context->gogo()->lower_expression(context->function(), &init);
413 binit = tree_to_expr(init->get_tree(context));
416 Bstatement* statement;
418 context->backend()->temporary_variable(bfunction, context->bblock(),
420 this->is_address_taken_,
421 this->location(), &statement);
422 return stat_to_tree(statement);
425 // Return the backend variable.
428 Temporary_statement::get_backend_variable(Translate_context* context) const
430 if (this->bvariable_ == NULL)
432 gcc_assert(saw_errors());
433 return context->backend()->error_variable();
435 return this->bvariable_;
438 // Make and initialize a temporary variable in BLOCK.
441 Statement::make_temporary(Type* type, Expression* init,
442 source_location location)
444 return new Temporary_statement(type, init, location);
447 // An assignment statement.
449 class Assignment_statement : public Statement
452 Assignment_statement(Expression* lhs, Expression* rhs,
453 source_location location)
454 : Statement(STATEMENT_ASSIGNMENT, location),
460 do_traverse(Traverse* traverse);
463 do_traverse_assignments(Traverse_assignments*);
466 do_determine_types();
469 do_check_types(Gogo*);
472 do_get_tree(Translate_context*);
475 // Left hand side--the lvalue.
477 // Right hand side--the rvalue.
484 Assignment_statement::do_traverse(Traverse* traverse)
486 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
487 return TRAVERSE_EXIT;
488 return this->traverse_expression(traverse, &this->rhs_);
492 Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
494 tassign->assignment(&this->lhs_, &this->rhs_);
498 // Set types for the assignment.
501 Assignment_statement::do_determine_types()
503 this->lhs_->determine_type_no_context();
504 Type_context context(this->lhs_->type(), false);
505 this->rhs_->determine_type(&context);
508 // Check types for an assignment.
511 Assignment_statement::do_check_types(Gogo*)
513 // The left hand side must be either addressable, a map index
514 // expression, or the blank identifier.
515 if (!this->lhs_->is_addressable()
516 && this->lhs_->map_index_expression() == NULL
517 && !this->lhs_->is_sink_expression())
519 if (!this->lhs_->type()->is_error())
520 this->report_error(_("invalid left hand side of assignment"));
524 Type* lhs_type = this->lhs_->type();
525 Type* rhs_type = this->rhs_->type();
527 if (!Type::are_assignable(lhs_type, rhs_type, &reason))
530 error_at(this->location(), "incompatible types in assignment");
532 error_at(this->location(), "incompatible types in assignment (%s)",
534 this->set_is_error();
537 if (lhs_type->is_error() || rhs_type->is_error())
538 this->set_is_error();
541 // Build a tree for an assignment statement.
544 Assignment_statement::do_get_tree(Translate_context* context)
546 tree rhs_tree = this->rhs_->get_tree(context);
548 if (this->lhs_->is_sink_expression())
551 tree lhs_tree = this->lhs_->get_tree(context);
553 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
554 return error_mark_node;
556 rhs_tree = Expression::convert_for_assignment(context, this->lhs_->type(),
557 this->rhs_->type(), rhs_tree,
559 if (rhs_tree == error_mark_node)
560 return error_mark_node;
563 ret = context->backend()->assignment_statement(tree_to_expr(lhs_tree),
564 tree_to_expr(rhs_tree),
566 return stat_to_tree(ret);
569 // Make an assignment statement.
572 Statement::make_assignment(Expression* lhs, Expression* rhs,
573 source_location location)
575 return new Assignment_statement(lhs, rhs, location);
578 // The Move_ordered_evals class is used to find any subexpressions of
579 // an expression that have an evaluation order dependency. It creates
580 // temporary variables to hold them.
582 class Move_ordered_evals : public Traverse
585 Move_ordered_evals(Block* block)
586 : Traverse(traverse_expressions),
592 expression(Expression**);
595 // The block where new temporary variables should be added.
600 Move_ordered_evals::expression(Expression** pexpr)
602 // We have to look at subexpressions first.
603 if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
604 return TRAVERSE_EXIT;
605 if ((*pexpr)->must_eval_in_order())
607 source_location loc = (*pexpr)->location();
608 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
609 this->block_->add_statement(temp);
610 *pexpr = Expression::make_temporary_reference(temp, loc);
612 return TRAVERSE_SKIP_COMPONENTS;
615 // An assignment operation statement.
617 class Assignment_operation_statement : public Statement
620 Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
621 source_location location)
622 : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
623 op_(op), lhs_(lhs), rhs_(rhs)
628 do_traverse(Traverse*);
631 do_traverse_assignments(Traverse_assignments*)
632 { gcc_unreachable(); }
635 do_lower(Gogo*, Named_object*, Block*);
638 do_get_tree(Translate_context*)
639 { gcc_unreachable(); }
642 // The operator (OPERATOR_PLUSEQ, etc.).
653 Assignment_operation_statement::do_traverse(Traverse* traverse)
655 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
656 return TRAVERSE_EXIT;
657 return this->traverse_expression(traverse, &this->rhs_);
660 // Lower an assignment operation statement to a regular assignment
664 Assignment_operation_statement::do_lower(Gogo*, Named_object*,
667 source_location loc = this->location();
669 // We have to evaluate the left hand side expression only once. We
670 // do this by moving out any expression with side effects.
671 Block* b = new Block(enclosing, loc);
672 Move_ordered_evals moe(b);
673 this->lhs_->traverse_subexpressions(&moe);
675 Expression* lval = this->lhs_->copy();
680 case OPERATOR_PLUSEQ:
683 case OPERATOR_MINUSEQ:
692 case OPERATOR_MULTEQ:
701 case OPERATOR_LSHIFTEQ:
702 op = OPERATOR_LSHIFT;
704 case OPERATOR_RSHIFTEQ:
705 op = OPERATOR_RSHIFT;
710 case OPERATOR_BITCLEAREQ:
711 op = OPERATOR_BITCLEAR;
717 Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
718 Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
719 if (b->statements()->empty())
727 return Statement::make_block_statement(b, loc);
731 // Make an assignment operation statement.
734 Statement::make_assignment_operation(Operator op, Expression* lhs,
735 Expression* rhs, source_location location)
737 return new Assignment_operation_statement(op, lhs, rhs, location);
740 // A tuple assignment statement. This differs from an assignment
741 // statement in that the right-hand-side expressions are evaluated in
744 class Tuple_assignment_statement : public Statement
747 Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
748 source_location location)
749 : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
755 do_traverse(Traverse* traverse);
758 do_traverse_assignments(Traverse_assignments*)
759 { gcc_unreachable(); }
762 do_lower(Gogo*, Named_object*, Block*);
765 do_get_tree(Translate_context*)
766 { gcc_unreachable(); }
769 // Left hand side--a list of lvalues.
770 Expression_list* lhs_;
771 // Right hand side--a list of rvalues.
772 Expression_list* rhs_;
778 Tuple_assignment_statement::do_traverse(Traverse* traverse)
780 if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
781 return TRAVERSE_EXIT;
782 return this->traverse_expression_list(traverse, this->rhs_);
785 // Lower a tuple assignment. We use temporary variables to split it
786 // up into a set of single assignments.
789 Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
791 source_location loc = this->location();
793 Block* b = new Block(enclosing, loc);
795 // First move out any subexpressions on the left hand side. The
796 // right hand side will be evaluated in the required order anyhow.
797 Move_ordered_evals moe(b);
798 for (Expression_list::const_iterator plhs = this->lhs_->begin();
799 plhs != this->lhs_->end();
801 (*plhs)->traverse_subexpressions(&moe);
803 std::vector<Temporary_statement*> temps;
804 temps.reserve(this->lhs_->size());
806 Expression_list::const_iterator prhs = this->rhs_->begin();
807 for (Expression_list::const_iterator plhs = this->lhs_->begin();
808 plhs != this->lhs_->end();
811 gcc_assert(prhs != this->rhs_->end());
813 if ((*plhs)->is_error_expression()
814 || (*plhs)->type()->is_error()
815 || (*prhs)->is_error_expression()
816 || (*prhs)->type()->is_error())
819 if ((*plhs)->is_sink_expression())
821 b->add_statement(Statement::make_statement(*prhs));
825 Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
827 b->add_statement(temp);
828 temps.push_back(temp);
831 gcc_assert(prhs == this->rhs_->end());
833 prhs = this->rhs_->begin();
834 std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
835 for (Expression_list::const_iterator plhs = this->lhs_->begin();
836 plhs != this->lhs_->end();
839 if ((*plhs)->is_error_expression()
840 || (*plhs)->type()->is_error()
841 || (*prhs)->is_error_expression()
842 || (*prhs)->type()->is_error())
845 if ((*plhs)->is_sink_expression())
848 Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
849 Statement* s = Statement::make_assignment(*plhs, ref, loc);
853 gcc_assert(ptemp == temps.end());
855 return Statement::make_block_statement(b, loc);
858 // Make a tuple assignment statement.
861 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
862 source_location location)
864 return new Tuple_assignment_statement(lhs, rhs, location);
867 // A tuple assignment from a map index expression.
870 class Tuple_map_assignment_statement : public Statement
873 Tuple_map_assignment_statement(Expression* val, Expression* present,
874 Expression* map_index,
875 source_location location)
876 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
877 val_(val), present_(present), map_index_(map_index)
882 do_traverse(Traverse* traverse);
885 do_traverse_assignments(Traverse_assignments*)
886 { gcc_unreachable(); }
889 do_lower(Gogo*, Named_object*, Block*);
892 do_get_tree(Translate_context*)
893 { gcc_unreachable(); }
896 // Lvalue which receives the value from the map.
898 // Lvalue which receives whether the key value was present.
899 Expression* present_;
900 // The map index expression.
901 Expression* map_index_;
907 Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
909 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
910 || this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
911 return TRAVERSE_EXIT;
912 return this->traverse_expression(traverse, &this->map_index_);
915 // Lower a tuple map assignment.
918 Tuple_map_assignment_statement::do_lower(Gogo*, Named_object*,
921 source_location loc = this->location();
923 Map_index_expression* map_index = this->map_index_->map_index_expression();
924 if (map_index == NULL)
926 this->report_error(_("expected map index on right hand side"));
927 return Statement::make_error_statement(loc);
929 Map_type* map_type = map_index->get_map_type();
930 if (map_type == NULL)
931 return Statement::make_error_statement(loc);
933 Block* b = new Block(enclosing, loc);
935 // Move out any subexpressions to make sure that functions are
936 // called in the required order.
937 Move_ordered_evals moe(b);
938 this->val_->traverse_subexpressions(&moe);
939 this->present_->traverse_subexpressions(&moe);
941 // Copy the key value into a temporary so that we can take its
942 // address without pushing the value onto the heap.
944 // var key_temp KEY_TYPE = MAP_INDEX
945 Temporary_statement* key_temp =
946 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
947 b->add_statement(key_temp);
949 // var val_temp VAL_TYPE
950 Temporary_statement* val_temp =
951 Statement::make_temporary(map_type->val_type(), NULL, loc);
952 b->add_statement(val_temp);
954 // var present_temp bool
955 Temporary_statement* present_temp =
956 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
957 b->add_statement(present_temp);
959 // present_temp = mapaccess2(MAP, &key_temp, &val_temp)
960 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
961 Expression* a1 = Expression::make_unary(OPERATOR_AND, ref, loc);
962 ref = Expression::make_temporary_reference(val_temp, loc);
963 Expression* a2 = Expression::make_unary(OPERATOR_AND, ref, loc);
964 Expression* call = Runtime::make_call(Runtime::MAPACCESS2, loc, 3,
965 map_index->map(), a1, a2);
967 ref = Expression::make_temporary_reference(present_temp, loc);
968 Statement* s = Statement::make_assignment(ref, call, loc);
972 ref = Expression::make_temporary_reference(val_temp, loc);
973 s = Statement::make_assignment(this->val_, ref, loc);
976 // present = present_temp
977 ref = Expression::make_temporary_reference(present_temp, loc);
978 s = Statement::make_assignment(this->present_, ref, loc);
981 return Statement::make_block_statement(b, loc);
984 // Make a map assignment statement which returns a pair of values.
987 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
988 Expression* map_index,
989 source_location location)
991 return new Tuple_map_assignment_statement(val, present, map_index, location);
994 // Assign a pair of entries to a map.
997 class Map_assignment_statement : public Statement
1000 Map_assignment_statement(Expression* map_index,
1001 Expression* val, Expression* should_set,
1002 source_location location)
1003 : Statement(STATEMENT_MAP_ASSIGNMENT, location),
1004 map_index_(map_index), val_(val), should_set_(should_set)
1009 do_traverse(Traverse* traverse);
1012 do_traverse_assignments(Traverse_assignments*)
1013 { gcc_unreachable(); }
1016 do_lower(Gogo*, Named_object*, Block*);
1019 do_get_tree(Translate_context*)
1020 { gcc_unreachable(); }
1023 // A reference to the map index which should be set or deleted.
1024 Expression* map_index_;
1025 // The value to add to the map.
1027 // Whether or not to add the value.
1028 Expression* should_set_;
1031 // Traverse a map assignment.
1034 Map_assignment_statement::do_traverse(Traverse* traverse)
1036 if (this->traverse_expression(traverse, &this->map_index_) == TRAVERSE_EXIT
1037 || this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
1038 return TRAVERSE_EXIT;
1039 return this->traverse_expression(traverse, &this->should_set_);
1042 // Lower a map assignment to a function call.
1045 Map_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
1047 source_location loc = this->location();
1049 Map_index_expression* map_index = this->map_index_->map_index_expression();
1050 if (map_index == NULL)
1052 this->report_error(_("expected map index on left hand side"));
1053 return Statement::make_error_statement(loc);
1055 Map_type* map_type = map_index->get_map_type();
1056 if (map_type == NULL)
1057 return Statement::make_error_statement(loc);
1059 Block* b = new Block(enclosing, loc);
1061 // Evaluate the map first to get order of evaluation right.
1062 // map_temp := m // we are evaluating m[k] = v, p
1063 Temporary_statement* map_temp = Statement::make_temporary(map_type,
1066 b->add_statement(map_temp);
1068 // var key_temp MAP_KEY_TYPE = k
1069 Temporary_statement* key_temp =
1070 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1071 b->add_statement(key_temp);
1073 // var val_temp MAP_VAL_TYPE = v
1074 Temporary_statement* val_temp =
1075 Statement::make_temporary(map_type->val_type(), this->val_, loc);
1076 b->add_statement(val_temp);
1078 // var insert_temp bool = p
1079 Temporary_statement* insert_temp =
1080 Statement::make_temporary(Type::lookup_bool_type(), this->should_set_,
1082 b->add_statement(insert_temp);
1084 // mapassign2(map_temp, &key_temp, &val_temp, p)
1085 Expression* p1 = Expression::make_temporary_reference(map_temp, loc);
1086 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
1087 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1088 ref = Expression::make_temporary_reference(val_temp, loc);
1089 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1090 Expression* p4 = Expression::make_temporary_reference(insert_temp, loc);
1091 Expression* call = Runtime::make_call(Runtime::MAPASSIGN2, loc, 4,
1093 Statement* s = Statement::make_statement(call);
1094 b->add_statement(s);
1096 return Statement::make_block_statement(b, loc);
1099 // Make a statement which assigns a pair of entries to a map.
1102 Statement::make_map_assignment(Expression* map_index,
1103 Expression* val, Expression* should_set,
1104 source_location location)
1106 return new Map_assignment_statement(map_index, val, should_set, location);
1109 // A tuple assignment from a receive statement.
1111 class Tuple_receive_assignment_statement : public Statement
1114 Tuple_receive_assignment_statement(Expression* val, Expression* closed,
1115 Expression* channel, bool for_select,
1116 source_location location)
1117 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
1118 val_(val), closed_(closed), channel_(channel), for_select_(for_select)
1123 do_traverse(Traverse* traverse);
1126 do_traverse_assignments(Traverse_assignments*)
1127 { gcc_unreachable(); }
1130 do_lower(Gogo*, Named_object*, Block*);
1133 do_get_tree(Translate_context*)
1134 { gcc_unreachable(); }
1137 // Lvalue which receives the value from the channel.
1139 // Lvalue which receives whether the channel is closed.
1140 Expression* closed_;
1141 // The channel on which we receive the value.
1142 Expression* channel_;
1143 // Whether this is for a select statement.
1150 Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
1152 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1153 || this->traverse_expression(traverse, &this->closed_) == TRAVERSE_EXIT)
1154 return TRAVERSE_EXIT;
1155 return this->traverse_expression(traverse, &this->channel_);
1158 // Lower to a function call.
1161 Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
1164 source_location loc = this->location();
1166 Channel_type* channel_type = this->channel_->type()->channel_type();
1167 if (channel_type == NULL)
1169 this->report_error(_("expected channel"));
1170 return Statement::make_error_statement(loc);
1172 if (!channel_type->may_receive())
1174 this->report_error(_("invalid receive on send-only channel"));
1175 return Statement::make_error_statement(loc);
1178 Block* b = new Block(enclosing, loc);
1180 // Make sure that any subexpressions on the left hand side are
1181 // evaluated in the right order.
1182 Move_ordered_evals moe(b);
1183 this->val_->traverse_subexpressions(&moe);
1184 this->closed_->traverse_subexpressions(&moe);
1186 // var val_temp ELEMENT_TYPE
1187 Temporary_statement* val_temp =
1188 Statement::make_temporary(channel_type->element_type(), NULL, loc);
1189 b->add_statement(val_temp);
1191 // var closed_temp bool
1192 Temporary_statement* closed_temp =
1193 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
1194 b->add_statement(closed_temp);
1196 // closed_temp = chanrecv[23](channel, &val_temp)
1197 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1198 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1199 Expression* call = Runtime::make_call((this->for_select_
1200 ? Runtime::CHANRECV3
1201 : Runtime::CHANRECV2),
1202 loc, 2, this->channel_, p2);
1203 ref = Expression::make_temporary_reference(closed_temp, loc);
1204 Statement* s = Statement::make_assignment(ref, call, loc);
1205 b->add_statement(s);
1208 ref = Expression::make_temporary_reference(val_temp, loc);
1209 s = Statement::make_assignment(this->val_, ref, loc);
1210 b->add_statement(s);
1212 // closed = closed_temp
1213 ref = Expression::make_temporary_reference(closed_temp, loc);
1214 s = Statement::make_assignment(this->closed_, ref, loc);
1215 b->add_statement(s);
1217 return Statement::make_block_statement(b, loc);
1220 // Make a nonblocking receive statement.
1223 Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
1224 Expression* channel,
1226 source_location location)
1228 return new Tuple_receive_assignment_statement(val, closed, channel,
1229 for_select, location);
1232 // An assignment to a pair of values from a type guard. This is a
1233 // conditional type guard. v, ok = i.(type).
1235 class Tuple_type_guard_assignment_statement : public Statement
1238 Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
1239 Expression* expr, Type* type,
1240 source_location location)
1241 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
1242 val_(val), ok_(ok), expr_(expr), type_(type)
1247 do_traverse(Traverse*);
1250 do_traverse_assignments(Traverse_assignments*)
1251 { gcc_unreachable(); }
1254 do_lower(Gogo*, Named_object*, Block*);
1257 do_get_tree(Translate_context*)
1258 { gcc_unreachable(); }
1262 lower_to_type(Runtime::Function);
1265 lower_to_object_type(Block*, Runtime::Function);
1267 // The variable which recieves the converted value.
1269 // The variable which receives the indication of success.
1271 // The expression being converted.
1273 // The type to which the expression is being converted.
1277 // Traverse a type guard tuple assignment.
1280 Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
1282 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1283 || this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
1284 || this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
1285 return TRAVERSE_EXIT;
1286 return this->traverse_expression(traverse, &this->expr_);
1289 // Lower to a function call.
1292 Tuple_type_guard_assignment_statement::do_lower(Gogo*, Named_object*,
1295 source_location loc = this->location();
1297 Type* expr_type = this->expr_->type();
1298 if (expr_type->interface_type() == NULL)
1300 if (!expr_type->is_error() && !this->type_->is_error())
1301 this->report_error(_("type assertion only valid for interface types"));
1302 return Statement::make_error_statement(loc);
1305 Block* b = new Block(enclosing, loc);
1307 // Make sure that any subexpressions on the left hand side are
1308 // evaluated in the right order.
1309 Move_ordered_evals moe(b);
1310 this->val_->traverse_subexpressions(&moe);
1311 this->ok_->traverse_subexpressions(&moe);
1313 bool expr_is_empty = expr_type->interface_type()->is_empty();
1314 Call_expression* call;
1315 if (this->type_->interface_type() != NULL)
1317 if (this->type_->interface_type()->is_empty())
1318 call = Runtime::make_call((expr_is_empty
1319 ? Runtime::IFACEE2E2
1320 : Runtime::IFACEI2E2),
1321 loc, 1, this->expr_);
1323 call = this->lower_to_type(expr_is_empty
1324 ? Runtime::IFACEE2I2
1325 : Runtime::IFACEI2I2);
1327 else if (this->type_->points_to() != NULL)
1328 call = this->lower_to_type(expr_is_empty
1329 ? Runtime::IFACEE2T2P
1330 : Runtime::IFACEI2T2P);
1333 this->lower_to_object_type(b,
1335 ? Runtime::IFACEE2T2
1336 : Runtime::IFACEI2T2));
1342 Expression* res = Expression::make_call_result(call, 0);
1343 res = Expression::make_unsafe_cast(this->type_, res, loc);
1344 Statement* s = Statement::make_assignment(this->val_, res, loc);
1345 b->add_statement(s);
1347 res = Expression::make_call_result(call, 1);
1348 s = Statement::make_assignment(this->ok_, res, loc);
1349 b->add_statement(s);
1352 return Statement::make_block_statement(b, loc);
1355 // Lower a conversion to a non-empty interface type or a pointer type.
1358 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code)
1360 source_location loc = this->location();
1361 return Runtime::make_call(code, loc, 2,
1362 Expression::make_type_descriptor(this->type_, loc),
1366 // Lower a conversion to a non-interface non-pointer type.
1369 Tuple_type_guard_assignment_statement::lower_to_object_type(
1371 Runtime::Function code)
1373 source_location loc = this->location();
1375 // var val_temp TYPE
1376 Temporary_statement* val_temp = Statement::make_temporary(this->type_,
1378 b->add_statement(val_temp);
1380 // ok = CODE(type_descriptor, expr, &val_temp)
1381 Expression* p1 = Expression::make_type_descriptor(this->type_, loc);
1382 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1383 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1384 Expression* call = Runtime::make_call(code, loc, 3, p1, this->expr_, p3);
1385 Statement* s = Statement::make_assignment(this->ok_, call, loc);
1386 b->add_statement(s);
1389 ref = Expression::make_temporary_reference(val_temp, loc);
1390 s = Statement::make_assignment(this->val_, ref, loc);
1391 b->add_statement(s);
1394 // Make an assignment from a type guard to a pair of variables.
1397 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
1398 Expression* expr, Type* type,
1399 source_location location)
1401 return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
1405 // An expression statement.
1407 class Expression_statement : public Statement
1410 Expression_statement(Expression* expr)
1411 : Statement(STATEMENT_EXPRESSION, expr->location()),
1417 do_traverse(Traverse* traverse)
1418 { return this->traverse_expression(traverse, &this->expr_); }
1421 do_determine_types()
1422 { this->expr_->determine_type_no_context(); }
1425 do_may_fall_through() const;
1428 do_get_tree(Translate_context* context);
1434 // An expression statement may fall through unless it is a call to a
1435 // function which does not return.
1438 Expression_statement::do_may_fall_through() const
1440 const Call_expression* call = this->expr_->call_expression();
1443 const Expression* fn = call->fn();
1444 const Func_expression* fe = fn->func_expression();
1447 const Named_object* no = fe->named_object();
1449 Function_type* fntype;
1450 if (no->is_function())
1451 fntype = no->func_value()->type();
1452 else if (no->is_function_declaration())
1453 fntype = no->func_declaration_value()->type();
1457 // The builtin function panic does not return.
1458 if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
1465 // Convert to backend representation.
1468 Expression_statement::do_get_tree(Translate_context* context)
1470 tree expr_tree = this->expr_->get_tree(context);
1471 Bexpression* bexpr = tree_to_expr(expr_tree);
1472 Bstatement* ret = context->backend()->expression_statement(bexpr);
1473 return stat_to_tree(ret);
1476 // Make an expression statement from an Expression.
1479 Statement::make_statement(Expression* expr)
1481 return new Expression_statement(expr);
1484 // A block statement--a list of statements which may include variable
1487 class Block_statement : public Statement
1490 Block_statement(Block* block, source_location location)
1491 : Statement(STATEMENT_BLOCK, location),
1497 do_traverse(Traverse* traverse)
1498 { return this->block_->traverse(traverse); }
1501 do_determine_types()
1502 { this->block_->determine_types(); }
1505 do_may_fall_through() const
1506 { return this->block_->may_fall_through(); }
1509 do_get_tree(Translate_context* context);
1515 // Convert a block to the backend representation of a statement.
1518 Block_statement::do_get_tree(Translate_context* context)
1520 Bblock* bblock = this->block_->get_backend(context);
1521 Bstatement* ret = context->backend()->block_statement(bblock);
1522 return stat_to_tree(ret);
1525 // Make a block statement.
1528 Statement::make_block_statement(Block* block, source_location location)
1530 return new Block_statement(block, location);
1533 // An increment or decrement statement.
1535 class Inc_dec_statement : public Statement
1538 Inc_dec_statement(bool is_inc, Expression* expr)
1539 : Statement(STATEMENT_INCDEC, expr->location()),
1540 expr_(expr), is_inc_(is_inc)
1545 do_traverse(Traverse* traverse)
1546 { return this->traverse_expression(traverse, &this->expr_); }
1549 do_traverse_assignments(Traverse_assignments*)
1550 { gcc_unreachable(); }
1553 do_lower(Gogo*, Named_object*, Block*);
1556 do_get_tree(Translate_context*)
1557 { gcc_unreachable(); }
1560 // The l-value to increment or decrement.
1562 // Whether to increment or decrement.
1566 // Lower to += or -=.
1569 Inc_dec_statement::do_lower(Gogo*, Named_object*, Block*)
1571 source_location loc = this->location();
1574 mpz_init_set_ui(oval, 1UL);
1575 Expression* oexpr = Expression::make_integer(&oval, NULL, loc);
1578 Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
1579 return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
1582 // Make an increment statement.
1585 Statement::make_inc_statement(Expression* expr)
1587 return new Inc_dec_statement(true, expr);
1590 // Make a decrement statement.
1593 Statement::make_dec_statement(Expression* expr)
1595 return new Inc_dec_statement(false, expr);
1598 // Class Thunk_statement. This is the base class for go and defer
1601 const char* const Thunk_statement::thunk_field_fn = "fn";
1603 const char* const Thunk_statement::thunk_field_receiver = "receiver";
1607 Thunk_statement::Thunk_statement(Statement_classification classification,
1608 Call_expression* call,
1609 source_location location)
1610 : Statement(classification, location),
1611 call_(call), struct_type_(NULL)
1615 // Return whether this is a simple statement which does not require a
1619 Thunk_statement::is_simple(Function_type* fntype) const
1621 // We need a thunk to call a method, or to pass a variable number of
1623 if (fntype->is_method() || fntype->is_varargs())
1626 // A defer statement requires a thunk to set up for whether the
1627 // function can call recover.
1628 if (this->classification() == STATEMENT_DEFER)
1631 // We can only permit a single parameter of pointer type.
1632 const Typed_identifier_list* parameters = fntype->parameters();
1633 if (parameters != NULL
1634 && (parameters->size() > 1
1635 || (parameters->size() == 1
1636 && parameters->begin()->type()->points_to() == NULL)))
1639 // If the function returns multiple values, or returns a type other
1640 // than integer, floating point, or pointer, then it may get a
1641 // hidden first parameter, in which case we need the more
1642 // complicated approach. This is true even though we are going to
1643 // ignore the return value.
1644 const Typed_identifier_list* results = fntype->results();
1646 && (results->size() > 1
1647 || (results->size() == 1
1648 && !results->begin()->type()->is_basic_type()
1649 && results->begin()->type()->points_to() == NULL)))
1652 // If this calls something which is not a simple function, then we
1654 Expression* fn = this->call_->call_expression()->fn();
1655 if (fn->bound_method_expression() != NULL
1656 || fn->interface_field_reference_expression() != NULL)
1662 // Traverse a thunk statement.
1665 Thunk_statement::do_traverse(Traverse* traverse)
1667 return this->traverse_expression(traverse, &this->call_);
1670 // We implement traverse_assignment for a thunk statement because it
1671 // effectively copies the function call.
1674 Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
1676 Expression* fn = this->call_->call_expression()->fn();
1677 Expression* fn2 = fn;
1678 tassign->value(&fn2, true, false);
1682 // Determine types in a thunk statement.
1685 Thunk_statement::do_determine_types()
1687 this->call_->determine_type_no_context();
1689 // Now that we know the types of the call, build the struct used to
1691 Call_expression* ce = this->call_->call_expression();
1694 Function_type* fntype = ce->get_function_type();
1695 if (fntype != NULL && !this->is_simple(fntype))
1696 this->struct_type_ = this->build_struct(fntype);
1699 // Check types in a thunk statement.
1702 Thunk_statement::do_check_types(Gogo*)
1704 Call_expression* ce = this->call_->call_expression();
1707 if (!this->call_->is_error_expression())
1708 this->report_error("expected call expression");
1711 Function_type* fntype = ce->get_function_type();
1712 if (fntype != NULL && fntype->is_method())
1714 Expression* fn = ce->fn();
1715 if (fn->bound_method_expression() == NULL
1716 && fn->interface_field_reference_expression() == NULL)
1717 this->report_error(_("no object for method call"));
1721 // The Traverse class used to find and simplify thunk statements.
1723 class Simplify_thunk_traverse : public Traverse
1726 Simplify_thunk_traverse(Gogo* gogo)
1727 : Traverse(traverse_functions | traverse_blocks),
1728 gogo_(gogo), function_(NULL)
1732 function(Named_object*);
1740 // The function we are traversing.
1741 Named_object* function_;
1744 // Keep track of the current function while looking for thunks.
1747 Simplify_thunk_traverse::function(Named_object* no)
1749 gcc_assert(this->function_ == NULL);
1750 this->function_ = no;
1751 int t = no->func_value()->traverse(this);
1752 this->function_ = NULL;
1753 if (t == TRAVERSE_EXIT)
1755 return TRAVERSE_SKIP_COMPONENTS;
1758 // Look for thunks in a block.
1761 Simplify_thunk_traverse::block(Block* b)
1763 // The parser ensures that thunk statements always appear at the end
1765 if (b->statements()->size() < 1)
1766 return TRAVERSE_CONTINUE;
1767 Thunk_statement* stat = b->statements()->back()->thunk_statement();
1769 return TRAVERSE_CONTINUE;
1770 if (stat->simplify_statement(this->gogo_, this->function_, b))
1771 return TRAVERSE_SKIP_COMPONENTS;
1772 return TRAVERSE_CONTINUE;
1775 // Simplify all thunk statements.
1778 Gogo::simplify_thunk_statements()
1780 Simplify_thunk_traverse thunk_traverse(this);
1781 this->traverse(&thunk_traverse);
1784 // Simplify complex thunk statements into simple ones. A complicated
1785 // thunk statement is one which takes anything other than zero
1786 // parameters or a single pointer parameter. We rewrite it into code
1787 // which allocates a struct, stores the parameter values into the
1788 // struct, and does a simple go or defer statement which passes the
1789 // struct to a thunk. The thunk does the real call.
1792 Thunk_statement::simplify_statement(Gogo* gogo, Named_object* function,
1795 if (this->classification() == STATEMENT_ERROR)
1797 if (this->call_->is_error_expression())
1800 if (this->classification() == STATEMENT_DEFER)
1802 // Make sure that the defer stack exists for the function. We
1803 // will use when converting this statement to the backend
1804 // representation, but we want it to exist when we start
1805 // converting the function.
1806 function->func_value()->defer_stack(this->location());
1809 Call_expression* ce = this->call_->call_expression();
1810 Function_type* fntype = ce->get_function_type();
1813 gcc_assert(saw_errors());
1814 this->set_is_error();
1817 if (this->is_simple(fntype))
1820 Expression* fn = ce->fn();
1821 Bound_method_expression* bound_method = fn->bound_method_expression();
1822 Interface_field_reference_expression* interface_method =
1823 fn->interface_field_reference_expression();
1824 const bool is_method = bound_method != NULL || interface_method != NULL;
1826 source_location location = this->location();
1828 std::string thunk_name = Gogo::thunk_name();
1831 this->build_thunk(gogo, thunk_name, fntype);
1833 // Generate code to call the thunk.
1835 // Get the values to store into the struct which is the single
1836 // argument to the thunk.
1838 Expression_list* vals = new Expression_list();
1839 if (fntype->is_builtin())
1841 else if (!is_method)
1842 vals->push_back(fn);
1843 else if (interface_method != NULL)
1844 vals->push_back(interface_method->expr());
1845 else if (bound_method != NULL)
1847 vals->push_back(bound_method->method());
1848 Expression* first_arg = bound_method->first_argument();
1850 // We always pass a pointer when calling a method.
1851 if (first_arg->type()->points_to() == NULL)
1852 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, location);
1854 // If we are calling a method which was inherited from an
1855 // embedded struct, and the method did not get a stub, then the
1856 // first type may be wrong.
1857 Type* fatype = bound_method->first_argument_type();
1860 if (fatype->points_to() == NULL)
1861 fatype = Type::make_pointer_type(fatype);
1862 Type* unsafe = Type::make_pointer_type(Type::make_void_type());
1863 first_arg = Expression::make_cast(unsafe, first_arg, location);
1864 first_arg = Expression::make_cast(fatype, first_arg, location);
1867 vals->push_back(first_arg);
1872 if (ce->args() != NULL)
1874 for (Expression_list::const_iterator p = ce->args()->begin();
1875 p != ce->args()->end();
1877 vals->push_back(*p);
1880 // Build the struct.
1881 Expression* constructor =
1882 Expression::make_struct_composite_literal(this->struct_type_, vals,
1885 // Allocate the initialized struct on the heap.
1886 constructor = Expression::make_heap_composite(constructor, location);
1888 // Look up the thunk.
1889 Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
1890 gcc_assert(named_thunk != NULL && named_thunk->is_function());
1893 Expression* func = Expression::make_func_reference(named_thunk, NULL,
1895 Expression_list* params = new Expression_list();
1896 params->push_back(constructor);
1897 Call_expression* call = Expression::make_call(func, params, false, location);
1899 // Build the simple go or defer statement.
1901 if (this->classification() == STATEMENT_GO)
1902 s = Statement::make_go_statement(call, location);
1903 else if (this->classification() == STATEMENT_DEFER)
1904 s = Statement::make_defer_statement(call, location);
1908 // The current block should end with the go statement.
1909 gcc_assert(block->statements()->size() >= 1);
1910 gcc_assert(block->statements()->back() == this);
1911 block->replace_statement(block->statements()->size() - 1, s);
1913 // We already ran the determine_types pass, so we need to run it now
1914 // for the new statement.
1915 s->determine_types();
1918 gogo->check_types_in_block(block);
1920 // Return true to tell the block not to keep looking at statements.
1924 // Set the name to use for thunk parameter N.
1927 Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
1929 snprintf(buf, buflen, "a%d", n);
1932 // Build a new struct type to hold the parameters for a complicated
1933 // thunk statement. FNTYPE is the type of the function call.
1936 Thunk_statement::build_struct(Function_type* fntype)
1938 source_location location = this->location();
1940 Struct_field_list* fields = new Struct_field_list();
1942 Call_expression* ce = this->call_->call_expression();
1943 Expression* fn = ce->fn();
1945 Interface_field_reference_expression* interface_method =
1946 fn->interface_field_reference_expression();
1947 if (interface_method != NULL)
1949 // If this thunk statement calls a method on an interface, we
1950 // pass the interface object to the thunk.
1951 Typed_identifier tid(Thunk_statement::thunk_field_fn,
1952 interface_method->expr()->type(),
1954 fields->push_back(Struct_field(tid));
1956 else if (!fntype->is_builtin())
1958 // The function to call.
1959 Typed_identifier tid(Go_statement::thunk_field_fn, fntype, location);
1960 fields->push_back(Struct_field(tid));
1962 else if (ce->is_recover_call())
1964 // The predeclared recover function has no argument. However,
1965 // we add an argument when building recover thunks. Handle that
1967 fields->push_back(Struct_field(Typed_identifier("can_recover",
1968 Type::lookup_bool_type(),
1972 if (fn->bound_method_expression() != NULL)
1974 gcc_assert(fntype->is_method());
1975 Type* rtype = fntype->receiver()->type();
1976 // We always pass the receiver as a pointer.
1977 if (rtype->points_to() == NULL)
1978 rtype = Type::make_pointer_type(rtype);
1979 Typed_identifier tid(Thunk_statement::thunk_field_receiver, rtype,
1981 fields->push_back(Struct_field(tid));
1984 const Expression_list* args = ce->args();
1988 for (Expression_list::const_iterator p = args->begin();
1993 this->thunk_field_param(i, buf, sizeof buf);
1994 fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
1999 return Type::make_struct_type(fields, location);
2002 // Build the thunk we are going to call. This is a brand new, albeit
2003 // artificial, function.
2006 Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
2007 Function_type* fntype)
2009 source_location location = this->location();
2011 Call_expression* ce = this->call_->call_expression();
2013 bool may_call_recover = false;
2014 if (this->classification() == STATEMENT_DEFER)
2016 Func_expression* fn = ce->fn()->func_expression();
2018 may_call_recover = true;
2021 const Named_object* no = fn->named_object();
2022 if (!no->is_function())
2023 may_call_recover = true;
2025 may_call_recover = no->func_value()->calls_recover();
2029 // Build the type of the thunk. The thunk takes a single parameter,
2030 // which is a pointer to the special structure we build.
2031 const char* const parameter_name = "__go_thunk_parameter";
2032 Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
2033 Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
2034 thunk_parameters->push_back(Typed_identifier(parameter_name,
2035 pointer_to_struct_type,
2038 Typed_identifier_list* thunk_results = NULL;
2039 if (may_call_recover)
2041 // When deferring a function which may call recover, add a
2042 // return value, to disable tail call optimizations which will
2043 // break the way we check whether recover is permitted.
2044 thunk_results = new Typed_identifier_list();
2045 thunk_results->push_back(Typed_identifier("", Type::lookup_bool_type(),
2049 Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
2053 // Start building the thunk.
2054 Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
2057 // For a defer statement, start with a call to
2058 // __go_set_defer_retaddr. */
2059 Label* retaddr_label = NULL;
2060 if (may_call_recover)
2062 retaddr_label = gogo->add_label_reference("retaddr");
2063 Expression* arg = Expression::make_label_addr(retaddr_label, location);
2064 Expression* call = Runtime::make_call(Runtime::SET_DEFER_RETADDR,
2067 // This is a hack to prevent the middle-end from deleting the
2069 gogo->start_block(location);
2070 gogo->add_statement(Statement::make_goto_statement(retaddr_label,
2072 Block* then_block = gogo->finish_block(location);
2073 then_block->determine_types();
2075 Statement* s = Statement::make_if_statement(call, then_block, NULL,
2077 s->determine_types();
2078 gogo->add_statement(s);
2081 // Get a reference to the parameter.
2082 Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
2083 gcc_assert(named_parameter != NULL && named_parameter->is_variable());
2085 // Build the call. Note that the field names are the same as the
2086 // ones used in build_struct.
2087 Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
2089 thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
2092 Bound_method_expression* bound_method = ce->fn()->bound_method_expression();
2093 Interface_field_reference_expression* interface_method =
2094 ce->fn()->interface_field_reference_expression();
2096 Expression* func_to_call;
2097 unsigned int next_index;
2098 if (!fntype->is_builtin())
2100 func_to_call = Expression::make_field_reference(thunk_parameter,
2106 gcc_assert(bound_method == NULL && interface_method == NULL);
2107 func_to_call = ce->fn();
2111 if (bound_method != NULL)
2113 Expression* r = Expression::make_field_reference(thunk_parameter, 1,
2115 // The main program passes in a function pointer from the
2116 // interface expression, so here we can make a bound method in
2118 func_to_call = Expression::make_bound_method(r, func_to_call,
2122 else if (interface_method != NULL)
2124 // The main program passes the interface object.
2125 const std::string& name(interface_method->name());
2126 func_to_call = Expression::make_interface_field_reference(func_to_call,
2131 Expression_list* call_params = new Expression_list();
2132 const Struct_field_list* fields = this->struct_type_->fields();
2133 Struct_field_list::const_iterator p = fields->begin();
2134 for (unsigned int i = 0; i < next_index; ++i)
2136 bool is_recover_call = ce->is_recover_call();
2137 Expression* recover_arg = NULL;
2138 for (; p != fields->end(); ++p, ++next_index)
2140 Expression* thunk_param = Expression::make_var_reference(named_parameter,
2142 thunk_param = Expression::make_unary(OPERATOR_MULT, thunk_param,
2144 Expression* param = Expression::make_field_reference(thunk_param,
2147 if (!is_recover_call)
2148 call_params->push_back(param);
2151 gcc_assert(call_params->empty());
2152 recover_arg = param;
2156 if (call_params->empty())
2162 Expression* call = Expression::make_call(func_to_call, call_params, false,
2164 // We need to lower in case this is a builtin function.
2165 call = call->lower(gogo, function, -1);
2166 Call_expression* call_ce = call->call_expression();
2167 if (call_ce != NULL && may_call_recover)
2168 call_ce->set_is_deferred();
2170 Statement* call_statement = Statement::make_statement(call);
2172 // We already ran the determine_types pass, so we need to run it
2173 // just for this statement now.
2174 call_statement->determine_types();
2177 call->check_types(gogo);
2179 if (call_ce != NULL && recover_arg != NULL)
2180 call_ce->set_recover_arg(recover_arg);
2182 gogo->add_statement(call_statement);
2184 // If this is a defer statement, the label comes immediately after
2186 if (may_call_recover)
2188 gogo->add_label_definition("retaddr", location);
2190 Expression_list* vals = new Expression_list();
2191 vals->push_back(Expression::make_boolean(false, location));
2192 gogo->add_statement(Statement::make_return_statement(vals, location));
2195 // That is all the thunk has to do.
2196 gogo->finish_function(location);
2199 // Get the function and argument trees.
2202 Thunk_statement::get_fn_and_arg(Expression** pfn, Expression** parg)
2204 if (this->call_->is_error_expression())
2207 Call_expression* ce = this->call_->call_expression();
2211 const Expression_list* args = ce->args();
2212 if (args == NULL || args->empty())
2213 *parg = Expression::make_nil(this->location());
2216 gcc_assert(args->size() == 1);
2217 *parg = args->front();
2223 // Class Go_statement.
2226 Go_statement::do_get_tree(Translate_context* context)
2230 if (!this->get_fn_and_arg(&fn, &arg))
2231 return error_mark_node;
2233 Expression* call = Runtime::make_call(Runtime::GO, this->location(), 2,
2235 tree call_tree = call->get_tree(context);
2236 Bexpression* call_bexpr = tree_to_expr(call_tree);
2237 Bstatement* ret = context->backend()->expression_statement(call_bexpr);
2238 return stat_to_tree(ret);
2241 // Make a go statement.
2244 Statement::make_go_statement(Call_expression* call, source_location location)
2246 return new Go_statement(call, location);
2249 // Class Defer_statement.
2252 Defer_statement::do_get_tree(Translate_context* context)
2256 if (!this->get_fn_and_arg(&fn, &arg))
2257 return error_mark_node;
2259 source_location loc = this->location();
2260 Expression* ds = context->function()->func_value()->defer_stack(loc);
2262 Expression* call = Runtime::make_call(Runtime::DEFER, loc, 3,
2264 tree call_tree = call->get_tree(context);
2265 Bexpression* call_bexpr = tree_to_expr(call_tree);
2266 Bstatement* ret = context->backend()->expression_statement(call_bexpr);
2267 return stat_to_tree(ret);
2270 // Make a defer statement.
2273 Statement::make_defer_statement(Call_expression* call,
2274 source_location location)
2276 return new Defer_statement(call, location);
2279 // Class Return_statement.
2281 // Traverse assignments. We treat each return value as a top level
2282 // RHS in an expression.
2285 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2287 Expression_list* vals = this->vals_;
2290 for (Expression_list::iterator p = vals->begin();
2293 tassign->value(&*p, true, true);
2298 // Lower a return statement. If we are returning a function call
2299 // which returns multiple values which match the current function,
2300 // split up the call's results. If the function has named result
2301 // variables, and the return statement lists explicit values, then
2302 // implement it by assigning the values to the result variables and
2303 // changing the statement to not list any values. This lets
2304 // panic/recover work correctly.
2307 Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing)
2309 if (this->is_lowered_)
2312 Expression_list* vals = this->vals_;
2314 this->is_lowered_ = true;
2316 source_location loc = this->location();
2318 size_t vals_count = vals == NULL ? 0 : vals->size();
2319 Function::Results* results = function->func_value()->result_variables();
2320 size_t results_count = results == NULL ? 0 : results->size();
2322 if (vals_count == 0)
2324 if (results_count > 0 && !function->func_value()->results_are_named())
2326 this->report_error(_("not enough arguments to return"));
2332 if (results_count == 0)
2334 this->report_error(_("return with value in function "
2335 "with no return type"));
2339 // If the current function has multiple return values, and we are
2340 // returning a single call expression, split up the call expression.
2341 if (results_count > 1
2342 && vals->size() == 1
2343 && vals->front()->call_expression() != NULL)
2345 Call_expression* call = vals->front()->call_expression();
2347 vals = new Expression_list;
2348 for (size_t i = 0; i < results_count; ++i)
2349 vals->push_back(Expression::make_call_result(call, i));
2350 vals_count = results_count;
2353 if (vals_count < results_count)
2355 this->report_error(_("not enough arguments to return"));
2359 if (vals_count > results_count)
2361 this->report_error(_("too many values in return statement"));
2365 Block* b = new Block(enclosing, loc);
2367 Expression_list* lhs = new Expression_list();
2368 Expression_list* rhs = new Expression_list();
2370 Expression_list::const_iterator pe = vals->begin();
2372 for (Function::Results::const_iterator pr = results->begin();
2373 pr != results->end();
2376 Named_object* rv = *pr;
2377 Expression* e = *pe;
2379 // Check types now so that we give a good error message. The
2380 // result type is known. We determine the expression type
2383 Type *rvtype = rv->result_var_value()->type();
2384 Type_context type_context(rvtype, false);
2385 e->determine_type(&type_context);
2388 if (Type::are_assignable(rvtype, e->type(), &reason))
2390 Expression* ve = Expression::make_var_reference(rv, e->location());
2397 error_at(e->location(), "incompatible type for return value %d", i);
2399 error_at(e->location(),
2400 "incompatible type for return value %d (%s)",
2404 gcc_assert(lhs->size() == rhs->size());
2408 else if (lhs->size() == 1)
2410 b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
2416 b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
2418 b->add_statement(this);
2422 return Statement::make_block_statement(b, loc);
2425 // Convert a return statement to the backend representation.
2428 Return_statement::do_get_tree(Translate_context* context)
2430 source_location loc = this->location();
2432 Function* function = context->function()->func_value();
2433 tree fndecl = function->get_decl();
2435 Function::Results* results = function->result_variables();
2436 std::vector<Bexpression*> retvals;
2437 if (results != NULL && !results->empty())
2439 retvals.reserve(results->size());
2440 for (Function::Results::const_iterator p = results->begin();
2441 p != results->end();
2444 Expression* vr = Expression::make_var_reference(*p, loc);
2445 retvals.push_back(tree_to_expr(vr->get_tree(context)));
2450 ret = context->backend()->return_statement(tree_to_function(fndecl),
2452 return stat_to_tree(ret);
2455 // Make a return statement.
2458 Statement::make_return_statement(Expression_list* vals,
2459 source_location location)
2461 return new Return_statement(vals, location);
2464 // A break or continue statement.
2466 class Bc_statement : public Statement
2469 Bc_statement(bool is_break, Unnamed_label* label, source_location location)
2470 : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
2471 label_(label), is_break_(is_break)
2476 { return this->is_break_; }
2480 do_traverse(Traverse*)
2481 { return TRAVERSE_CONTINUE; }
2484 do_may_fall_through() const
2488 do_get_tree(Translate_context* context)
2490 return stat_to_tree(this->label_->get_goto(context, this->location()));
2494 // The label that this branches to.
2495 Unnamed_label* label_;
2496 // True if this is "break", false if it is "continue".
2500 // Make a break statement.
2503 Statement::make_break_statement(Unnamed_label* label, source_location location)
2505 return new Bc_statement(true, label, location);
2508 // Make a continue statement.
2511 Statement::make_continue_statement(Unnamed_label* label,
2512 source_location location)
2514 return new Bc_statement(false, label, location);
2517 // A goto statement.
2519 class Goto_statement : public Statement
2522 Goto_statement(Label* label, source_location location)
2523 : Statement(STATEMENT_GOTO, location),
2529 do_traverse(Traverse*)
2530 { return TRAVERSE_CONTINUE; }
2533 do_check_types(Gogo*);
2536 do_may_fall_through() const
2540 do_get_tree(Translate_context*);
2546 // Check types for a label. There aren't any types per se, but we use
2547 // this to give an error if the label was never defined.
2550 Goto_statement::do_check_types(Gogo*)
2552 if (!this->label_->is_defined())
2554 error_at(this->location(), "reference to undefined label %qs",
2555 Gogo::message_name(this->label_->name()).c_str());
2556 this->set_is_error();
2560 // Return the tree for the goto statement.
2563 Goto_statement::do_get_tree(Translate_context* context)
2565 Blabel* blabel = this->label_->get_backend_label(context);
2566 Bstatement* statement = context->backend()->goto_statement(blabel,
2568 return stat_to_tree(statement);
2571 // Make a goto statement.
2574 Statement::make_goto_statement(Label* label, source_location location)
2576 return new Goto_statement(label, location);
2579 // A goto statement to an unnamed label.
2581 class Goto_unnamed_statement : public Statement
2584 Goto_unnamed_statement(Unnamed_label* label, source_location location)
2585 : Statement(STATEMENT_GOTO_UNNAMED, location),
2591 do_traverse(Traverse*)
2592 { return TRAVERSE_CONTINUE; }
2595 do_may_fall_through() const
2599 do_get_tree(Translate_context* context)
2601 return stat_to_tree(this->label_->get_goto(context, this->location()));
2605 Unnamed_label* label_;
2608 // Make a goto statement to an unnamed label.
2611 Statement::make_goto_unnamed_statement(Unnamed_label* label,
2612 source_location location)
2614 return new Goto_unnamed_statement(label, location);
2617 // Class Label_statement.
2622 Label_statement::do_traverse(Traverse*)
2624 return TRAVERSE_CONTINUE;
2627 // Return a tree defining this label.
2630 Label_statement::do_get_tree(Translate_context* context)
2632 Blabel* blabel = this->label_->get_backend_label(context);
2633 Bstatement* statement;
2634 statement = context->backend()->label_definition_statement(blabel);
2635 return stat_to_tree(statement);
2638 // Make a label statement.
2641 Statement::make_label_statement(Label* label, source_location location)
2643 return new Label_statement(label, location);
2646 // An unnamed label statement.
2648 class Unnamed_label_statement : public Statement
2651 Unnamed_label_statement(Unnamed_label* label)
2652 : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
2658 do_traverse(Traverse*)
2659 { return TRAVERSE_CONTINUE; }
2662 do_get_tree(Translate_context* context)
2663 { return stat_to_tree(this->label_->get_definition(context)); }
2667 Unnamed_label* label_;
2670 // Make an unnamed label statement.
2673 Statement::make_unnamed_label_statement(Unnamed_label* label)
2675 return new Unnamed_label_statement(label);
2680 class If_statement : public Statement
2683 If_statement(Expression* cond, Block* then_block, Block* else_block,
2684 source_location location)
2685 : Statement(STATEMENT_IF, location),
2686 cond_(cond), then_block_(then_block), else_block_(else_block)
2691 do_traverse(Traverse*);
2694 do_determine_types();
2697 do_check_types(Gogo*);
2700 do_may_fall_through() const;
2703 do_get_tree(Translate_context*);
2714 If_statement::do_traverse(Traverse* traverse)
2716 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT
2717 || this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
2718 return TRAVERSE_EXIT;
2719 if (this->else_block_ != NULL)
2721 if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
2722 return TRAVERSE_EXIT;
2724 return TRAVERSE_CONTINUE;
2728 If_statement::do_determine_types()
2730 Type_context context(Type::lookup_bool_type(), false);
2731 this->cond_->determine_type(&context);
2732 this->then_block_->determine_types();
2733 if (this->else_block_ != NULL)
2734 this->else_block_->determine_types();
2740 If_statement::do_check_types(Gogo*)
2742 Type* type = this->cond_->type();
2743 if (type->is_error())
2744 this->set_is_error();
2745 else if (!type->is_boolean_type())
2746 this->report_error(_("expected boolean expression"));
2749 // Whether the overall statement may fall through.
2752 If_statement::do_may_fall_through() const
2754 return (this->else_block_ == NULL
2755 || this->then_block_->may_fall_through()
2756 || this->else_block_->may_fall_through());
2762 If_statement::do_get_tree(Translate_context* context)
2764 gcc_assert(this->cond_->type()->is_boolean_type()
2765 || this->cond_->type()->is_error());
2766 tree cond_tree = this->cond_->get_tree(context);
2767 Bblock* then_block = this->then_block_->get_backend(context);
2768 Bblock* else_block = (this->else_block_ == NULL
2770 : this->else_block_->get_backend(context));
2771 Bexpression* cond_expr = tree_to_expr(cond_tree);
2773 Bstatement* ret = context->backend()->if_statement(cond_expr, then_block,
2776 return stat_to_tree(ret);
2779 // Make an if statement.
2782 Statement::make_if_statement(Expression* cond, Block* then_block,
2783 Block* else_block, source_location location)
2785 return new If_statement(cond, then_block, else_block, location);
2788 // Class Case_clauses::Hash_integer_value.
2790 class Case_clauses::Hash_integer_value
2794 operator()(Expression*) const;
2798 Case_clauses::Hash_integer_value::operator()(Expression* pe) const
2803 if (!pe->integer_constant_value(true, ival, &itype))
2805 size_t ret = mpz_get_ui(ival);
2810 // Class Case_clauses::Eq_integer_value.
2812 class Case_clauses::Eq_integer_value
2816 operator()(Expression*, Expression*) const;
2820 Case_clauses::Eq_integer_value::operator()(Expression* a, Expression* b) const
2828 if (!a->integer_constant_value(true, aval, &atype)
2829 || !b->integer_constant_value(true, bval, &btype))
2831 bool ret = mpz_cmp(aval, bval) == 0;
2837 // Class Case_clauses::Case_clause.
2842 Case_clauses::Case_clause::traverse(Traverse* traverse)
2844 if (this->cases_ != NULL
2845 && (traverse->traverse_mask()
2846 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
2848 if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
2849 return TRAVERSE_EXIT;
2851 if (this->statements_ != NULL)
2853 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
2854 return TRAVERSE_EXIT;
2856 return TRAVERSE_CONTINUE;
2859 // Check whether all the case expressions are integer constants.
2862 Case_clauses::Case_clause::is_constant() const
2864 if (this->cases_ != NULL)
2866 for (Expression_list::const_iterator p = this->cases_->begin();
2867 p != this->cases_->end();
2869 if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
2875 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
2876 // value we are switching on; it may be NULL. If START_LABEL is not
2877 // NULL, it goes at the start of the statements, after the condition
2878 // test. We branch to FINISH_LABEL at the end of the statements.
2881 Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
2882 Unnamed_label* start_label,
2883 Unnamed_label* finish_label) const
2885 source_location loc = this->location_;
2886 Unnamed_label* next_case_label;
2887 if (this->cases_ == NULL || this->cases_->empty())
2889 gcc_assert(this->is_default_);
2890 next_case_label = NULL;
2894 Expression* cond = NULL;
2896 for (Expression_list::const_iterator p = this->cases_->begin();
2897 p != this->cases_->end();
2900 Expression* this_cond;
2901 if (val_temp == NULL)
2905 Expression* ref = Expression::make_temporary_reference(val_temp,
2907 this_cond = Expression::make_binary(OPERATOR_EQEQ, ref, *p, loc);
2913 cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
2916 Block* then_block = new Block(b, loc);
2917 next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
2918 Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
2920 then_block->add_statement(s);
2922 // if !COND { goto NEXT_CASE_LABEL }
2923 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
2924 s = Statement::make_if_statement(cond, then_block, NULL, loc);
2925 b->add_statement(s);
2928 if (start_label != NULL)
2929 b->add_statement(Statement::make_unnamed_label_statement(start_label));
2931 if (this->statements_ != NULL)
2932 b->add_statement(Statement::make_block_statement(this->statements_, loc));
2934 Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
2935 b->add_statement(s);
2937 if (next_case_label != NULL)
2938 b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
2944 Case_clauses::Case_clause::determine_types(Type* type)
2946 if (this->cases_ != NULL)
2948 Type_context case_context(type, false);
2949 for (Expression_list::iterator p = this->cases_->begin();
2950 p != this->cases_->end();
2952 (*p)->determine_type(&case_context);
2954 if (this->statements_ != NULL)
2955 this->statements_->determine_types();
2958 // Check types. Returns false if there was an error.
2961 Case_clauses::Case_clause::check_types(Type* type)
2963 if (this->cases_ != NULL)
2965 for (Expression_list::iterator p = this->cases_->begin();
2966 p != this->cases_->end();
2969 if (!Type::are_assignable(type, (*p)->type(), NULL)
2970 && !Type::are_assignable((*p)->type(), type, NULL))
2972 error_at((*p)->location(),
2973 "type mismatch between switch value and case clause");
2981 // Return true if this clause may fall through to the following
2982 // statements. Note that this is not the same as whether the case
2983 // uses the "fallthrough" keyword.
2986 Case_clauses::Case_clause::may_fall_through() const
2988 if (this->statements_ == NULL)
2990 return this->statements_->may_fall_through();
2993 // Convert the case values and statements to the backend
2994 // representation. BREAK_LABEL is the label which break statements
2995 // should branch to. CASE_CONSTANTS is used to detect duplicate
2996 // constants. *CASES should be passed as an empty vector; the values
2997 // for this case will be added to it. If this is the default case,
2998 // *CASES will remain empty. This returns the statement to execute if
2999 // one of these cases is selected.
3002 Case_clauses::Case_clause::get_backend(Translate_context* context,
3003 Unnamed_label* break_label,
3004 Case_constants* case_constants,
3005 std::vector<Bexpression*>* cases) const
3007 if (this->cases_ != NULL)
3009 gcc_assert(!this->is_default_);
3010 for (Expression_list::const_iterator p = this->cases_->begin();
3011 p != this->cases_->end();
3015 if (e->classification() != Expression::EXPRESSION_INTEGER)
3020 if (!(*p)->integer_constant_value(true, ival, &itype))
3022 // Something went wrong. This can happen with a
3023 // negative constant and an unsigned switch value.
3024 gcc_assert(saw_errors());
3027 gcc_assert(itype != NULL);
3028 e = Expression::make_integer(&ival, itype, e->location());
3032 std::pair<Case_constants::iterator, bool> ins =
3033 case_constants->insert(e);
3036 // Value was already present.
3037 error_at(this->location_, "duplicate case in switch");
3041 tree case_tree = e->get_tree(context);
3042 Bexpression* case_expr = tree_to_expr(case_tree);
3043 cases->push_back(case_expr);
3047 Bstatement* statements;
3048 if (this->statements_ == NULL)
3052 Bblock* bblock = this->statements_->get_backend(context);
3053 statements = context->backend()->block_statement(bblock);
3056 Bstatement* break_stat;
3057 if (this->is_fallthrough_)
3060 break_stat = break_label->get_goto(context, this->location_);
3062 if (statements == NULL)
3064 else if (break_stat == NULL)
3067 return context->backend()->compound_statement(statements, break_stat);
3070 // Class Case_clauses.
3075 Case_clauses::traverse(Traverse* traverse)
3077 for (Clauses::iterator p = this->clauses_.begin();
3078 p != this->clauses_.end();
3081 if (p->traverse(traverse) == TRAVERSE_EXIT)
3082 return TRAVERSE_EXIT;
3084 return TRAVERSE_CONTINUE;
3087 // Check whether all the case expressions are constant.
3090 Case_clauses::is_constant() const
3092 for (Clauses::const_iterator p = this->clauses_.begin();
3093 p != this->clauses_.end();
3095 if (!p->is_constant())
3100 // Lower case clauses for a nonconstant switch.
3103 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3104 Unnamed_label* break_label) const
3106 // The default case.
3107 const Case_clause* default_case = NULL;
3109 // The label for the fallthrough of the previous case.
3110 Unnamed_label* last_fallthrough_label = NULL;
3112 // The label for the start of the default case. This is used if the
3113 // case before the default case falls through.
3114 Unnamed_label* default_start_label = NULL;
3116 // The label for the end of the default case. This normally winds
3117 // up as BREAK_LABEL, but it will be different if the default case
3119 Unnamed_label* default_finish_label = NULL;
3121 for (Clauses::const_iterator p = this->clauses_.begin();
3122 p != this->clauses_.end();
3125 // The label to use for the start of the statements for this
3126 // case. This is NULL unless the previous case falls through.
3127 Unnamed_label* start_label = last_fallthrough_label;
3129 // The label to jump to after the end of the statements for this
3131 Unnamed_label* finish_label = break_label;
3133 last_fallthrough_label = NULL;
3134 if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3136 finish_label = new Unnamed_label(p->location());
3137 last_fallthrough_label = finish_label;
3140 if (!p->is_default())
3141 p->lower(b, val_temp, start_label, finish_label);
3144 // We have to move the default case to the end, so that we
3145 // only use it if all the other tests fail.
3147 default_start_label = start_label;
3148 default_finish_label = finish_label;
3152 if (default_case != NULL)
3153 default_case->lower(b, val_temp, default_start_label,
3154 default_finish_label);
3161 Case_clauses::determine_types(Type* type)
3163 for (Clauses::iterator p = this->clauses_.begin();
3164 p != this->clauses_.end();
3166 p->determine_types(type);
3169 // Check types. Returns false if there was an error.
3172 Case_clauses::check_types(Type* type)
3175 for (Clauses::iterator p = this->clauses_.begin();
3176 p != this->clauses_.end();
3179 if (!p->check_types(type))
3185 // Return true if these clauses may fall through to the statements
3186 // following the switch statement.
3189 Case_clauses::may_fall_through() const
3191 bool found_default = false;
3192 for (Clauses::const_iterator p = this->clauses_.begin();
3193 p != this->clauses_.end();
3196 if (p->may_fall_through() && !p->is_fallthrough())
3198 if (p->is_default())
3199 found_default = true;
3201 return !found_default;
3204 // Convert the cases to the backend representation. This sets
3205 // *ALL_CASES and *ALL_STATEMENTS.
3208 Case_clauses::get_backend(Translate_context* context,
3209 Unnamed_label* break_label,
3210 std::vector<std::vector<Bexpression*> >* all_cases,
3211 std::vector<Bstatement*>* all_statements) const
3213 Case_constants case_constants;
3215 size_t c = this->clauses_.size();
3216 all_cases->resize(c);
3217 all_statements->resize(c);
3220 for (Clauses::const_iterator p = this->clauses_.begin();
3221 p != this->clauses_.end();
3224 std::vector<Bexpression*> cases;
3225 Bstatement* stat = p->get_backend(context, break_label, &case_constants,
3227 (*all_cases)[i].swap(cases);
3228 (*all_statements)[i] = stat;
3232 // A constant switch statement. A Switch_statement is lowered to this
3233 // when all the cases are constants.
3235 class Constant_switch_statement : public Statement
3238 Constant_switch_statement(Expression* val, Case_clauses* clauses,
3239 Unnamed_label* break_label,
3240 source_location location)
3241 : Statement(STATEMENT_CONSTANT_SWITCH, location),
3242 val_(val), clauses_(clauses), break_label_(break_label)
3247 do_traverse(Traverse*);
3250 do_determine_types();
3253 do_check_types(Gogo*);
3256 do_may_fall_through() const;
3259 do_get_tree(Translate_context*);
3262 // The value to switch on.
3264 // The case clauses.
3265 Case_clauses* clauses_;
3266 // The break label, if needed.
3267 Unnamed_label* break_label_;
3273 Constant_switch_statement::do_traverse(Traverse* traverse)
3275 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3276 return TRAVERSE_EXIT;
3277 return this->clauses_->traverse(traverse);
3283 Constant_switch_statement::do_determine_types()
3285 this->val_->determine_type_no_context();
3286 this->clauses_->determine_types(this->val_->type());
3292 Constant_switch_statement::do_check_types(Gogo*)
3294 if (!this->clauses_->check_types(this->val_->type()))
3295 this->set_is_error();
3298 // Return whether this switch may fall through.
3301 Constant_switch_statement::do_may_fall_through() const
3303 if (this->clauses_ == NULL)
3306 // If we have a break label, then some case needed it. That implies
3307 // that the switch statement as a whole can fall through.
3308 if (this->break_label_ != NULL)
3311 return this->clauses_->may_fall_through();
3314 // Convert to GENERIC.
3317 Constant_switch_statement::do_get_tree(Translate_context* context)
3319 tree switch_val_tree = this->val_->get_tree(context);
3320 Bexpression* switch_val_expr = tree_to_expr(switch_val_tree);
3322 Unnamed_label* break_label = this->break_label_;
3323 if (break_label == NULL)
3324 break_label = new Unnamed_label(this->location());
3326 std::vector<std::vector<Bexpression*> > all_cases;
3327 std::vector<Bstatement*> all_statements;
3328 this->clauses_->get_backend(context, break_label, &all_cases,
3331 Bstatement* switch_statement;
3332 switch_statement = context->backend()->switch_statement(switch_val_expr,
3336 Bstatement* ldef = break_label->get_definition(context);
3337 Bstatement* ret = context->backend()->compound_statement(switch_statement,
3339 return stat_to_tree(ret);
3342 // Class Switch_statement.
3347 Switch_statement::do_traverse(Traverse* traverse)
3349 if (this->val_ != NULL)
3351 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3352 return TRAVERSE_EXIT;
3354 return this->clauses_->traverse(traverse);
3357 // Lower a Switch_statement to a Constant_switch_statement or a series
3358 // of if statements.
3361 Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
3363 source_location loc = this->location();
3365 if (this->val_ != NULL
3366 && (this->val_->is_error_expression()
3367 || this->val_->type()->is_error()))
3368 return Statement::make_error_statement(loc);
3370 if (this->val_ != NULL
3371 && this->val_->type()->integer_type() != NULL
3372 && !this->clauses_->empty()
3373 && this->clauses_->is_constant())
3374 return new Constant_switch_statement(this->val_, this->clauses_,
3375 this->break_label_, loc);
3377 Block* b = new Block(enclosing, loc);
3379 if (this->clauses_->empty())
3381 Expression* val = this->val_;
3383 val = Expression::make_boolean(true, loc);
3384 return Statement::make_statement(val);
3387 Temporary_statement* val_temp;
3388 if (this->val_ == NULL)
3392 // var val_temp VAL_TYPE = VAL
3393 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3394 b->add_statement(val_temp);
3397 this->clauses_->lower(b, val_temp, this->break_label());
3399 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3400 b->add_statement(s);
3402 return Statement::make_block_statement(b, loc);
3405 // Return the break label for this switch statement, creating it if
3409 Switch_statement::break_label()
3411 if (this->break_label_ == NULL)
3412 this->break_label_ = new Unnamed_label(this->location());
3413 return this->break_label_;
3416 // Make a switch statement.
3419 Statement::make_switch_statement(Expression* val, source_location location)
3421 return new Switch_statement(val, location);
3424 // Class Type_case_clauses::Type_case_clause.
3429 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3431 if (!this->is_default_
3432 && ((traverse->traverse_mask()
3433 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3434 && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3435 return TRAVERSE_EXIT;
3436 if (this->statements_ != NULL)
3437 return this->statements_->traverse(traverse);
3438 return TRAVERSE_CONTINUE;
3441 // Lower one clause in a type switch. Add statements to the block B.
3442 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3443 // BREAK_LABEL is the label at the end of the type switch.
3444 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3448 Type_case_clauses::Type_case_clause::lower(Block* b,
3449 Temporary_statement* descriptor_temp,
3450 Unnamed_label* break_label,
3451 Unnamed_label** stmts_label) const
3453 source_location loc = this->location_;
3455 Unnamed_label* next_case_label = NULL;
3456 if (!this->is_default_)
3458 Type* type = this->type_;
3460 Expression* ref = Expression::make_temporary_reference(descriptor_temp,
3464 // The language permits case nil, which is of course a constant
3465 // rather than a type. It will appear here as an invalid
3467 if (type->is_nil_constant_as_type())
3468 cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3469 Expression::make_nil(loc),
3472 cond = Runtime::make_call((type->interface_type() == NULL
3473 ? Runtime::IFACETYPEEQ
3474 : Runtime::IFACEI2TP),
3476 Expression::make_type_descriptor(type, loc),
3479 Unnamed_label* dest;
3480 if (!this->is_fallthrough_)
3482 // if !COND { goto NEXT_CASE_LABEL }
3483 next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
3484 dest = next_case_label;
3485 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3489 // if COND { goto STMTS_LABEL }
3490 gcc_assert(stmts_label != NULL);
3491 if (*stmts_label == NULL)
3492 *stmts_label = new Unnamed_label(UNKNOWN_LOCATION);
3493 dest = *stmts_label;
3495 Block* then_block = new Block(b, loc);
3496 Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
3497 then_block->add_statement(s);
3498 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3499 b->add_statement(s);
3502 if (this->statements_ != NULL
3503 || (!this->is_fallthrough_
3504 && stmts_label != NULL
3505 && *stmts_label != NULL))
3507 gcc_assert(!this->is_fallthrough_);
3508 if (stmts_label != NULL && *stmts_label != NULL)
3510 gcc_assert(!this->is_default_);
3511 if (this->statements_ != NULL)
3512 (*stmts_label)->set_location(this->statements_->start_location());
3513 Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
3514 b->add_statement(s);
3515 *stmts_label = NULL;
3517 if (this->statements_ != NULL)
3518 b->add_statement(Statement::make_block_statement(this->statements_,
3522 if (this->is_fallthrough_)
3523 gcc_assert(next_case_label == NULL);
3526 source_location gloc = (this->statements_ == NULL
3528 : this->statements_->end_location());
3529 b->add_statement(Statement::make_goto_unnamed_statement(break_label,
3531 if (next_case_label != NULL)
3534 Statement::make_unnamed_label_statement(next_case_label);
3535 b->add_statement(s);
3540 // Class Type_case_clauses.
3545 Type_case_clauses::traverse(Traverse* traverse)
3547 for (Type_clauses::iterator p = this->clauses_.begin();
3548 p != this->clauses_.end();
3551 if (p->traverse(traverse) == TRAVERSE_EXIT)
3552 return TRAVERSE_EXIT;
3554 return TRAVERSE_CONTINUE;
3557 // Check for duplicate types.
3560 Type_case_clauses::check_duplicates() const
3562 typedef Unordered_set_hash(const Type*, Type_hash_identical,
3563 Type_identical) Types_seen;
3564 Types_seen types_seen;
3565 for (Type_clauses::const_iterator p = this->clauses_.begin();
3566 p != this->clauses_.end();
3569 Type* t = p->type();
3572 if (t->is_nil_constant_as_type())
3573 t = Type::make_nil_type();
3574 std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
3576 error_at(p->location(), "duplicate type in switch");
3580 // Lower the clauses in a type switch. Add statements to the block B.
3581 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3582 // BREAK_LABEL is the label at the end of the type switch.
3585 Type_case_clauses::lower(Block* b, Temporary_statement* descriptor_temp,
3586 Unnamed_label* break_label) const
3588 const Type_case_clause* default_case = NULL;
3590 Unnamed_label* stmts_label = NULL;
3591 for (Type_clauses::const_iterator p = this->clauses_.begin();
3592 p != this->clauses_.end();
3595 if (!p->is_default())
3596 p->lower(b, descriptor_temp, break_label, &stmts_label);
3599 // We are generating a series of tests, which means that we
3600 // need to move the default case to the end.
3604 gcc_assert(stmts_label == NULL);
3606 if (default_case != NULL)
3607 default_case->lower(b, descriptor_temp, break_label, NULL);
3610 // Class Type_switch_statement.
3615 Type_switch_statement::do_traverse(Traverse* traverse)
3617 if (this->var_ == NULL)
3619 if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
3620 return TRAVERSE_EXIT;
3622 if (this->clauses_ != NULL)
3623 return this->clauses_->traverse(traverse);
3624 return TRAVERSE_CONTINUE;
3627 // Lower a type switch statement to a series of if statements. The gc
3628 // compiler is able to generate a table in some cases. However, that
3629 // does not work for us because we may have type descriptors in
3630 // different shared libraries, so we can't compare them with simple
3631 // equality testing.
3634 Type_switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
3636 const source_location loc = this->location();
3638 if (this->clauses_ != NULL)
3639 this->clauses_->check_duplicates();
3641 Block* b = new Block(enclosing, loc);
3643 Type* val_type = (this->var_ != NULL
3644 ? this->var_->var_value()->type()
3645 : this->expr_->type());
3647 // var descriptor_temp DESCRIPTOR_TYPE
3648 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3649 Temporary_statement* descriptor_temp =
3650 Statement::make_temporary(descriptor_type, NULL, loc);
3651 b->add_statement(descriptor_temp);
3653 if (val_type->interface_type() == NULL)
3655 // Doing a type switch on a non-interface type. Should we issue
3656 // a warning for this case?
3657 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3660 if (val_type->is_nil_type())
3661 rhs = Expression::make_nil(loc);
3664 if (val_type->is_abstract())
3665 val_type = val_type->make_non_abstract_type();
3666 rhs = Expression::make_type_descriptor(val_type, loc);
3668 Statement* s = Statement::make_assignment(lhs, rhs, loc);
3669 b->add_statement(s);
3673 // descriptor_temp = ifacetype(val_temp)
3674 // FIXME: This should be inlined.
3675 bool is_empty = val_type->interface_type()->is_empty();
3677 if (this->var_ == NULL)
3680 ref = Expression::make_var_reference(this->var_, loc);
3681 Expression* call = Runtime::make_call((is_empty
3682 ? Runtime::EFACETYPE
3683 : Runtime::IFACETYPE),
3685 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3687 Statement* s = Statement::make_assignment(lhs, call, loc);
3688 b->add_statement(s);
3691 if (this->clauses_ != NULL)
3692 this->clauses_->lower(b, descriptor_temp, this->break_label());
3694 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3695 b->add_statement(s);
3697 return Statement::make_block_statement(b, loc);
3700 // Return the break label for this type switch statement, creating it
3704 Type_switch_statement::break_label()
3706 if (this->break_label_ == NULL)
3707 this->break_label_ = new Unnamed_label(this->location());
3708 return this->break_label_;
3711 // Make a type switch statement.
3713 Type_switch_statement*
3714 Statement::make_type_switch_statement(Named_object* var, Expression* expr,
3715 source_location location)
3717 return new Type_switch_statement(var, expr, location);
3720 // Class Send_statement.
3725 Send_statement::do_traverse(Traverse* traverse)
3727 if (this->traverse_expression(traverse, &this->channel_) == TRAVERSE_EXIT)
3728 return TRAVERSE_EXIT;
3729 return this->traverse_expression(traverse, &this->val_);
3735 Send_statement::do_determine_types()
3737 this->channel_->determine_type_no_context();
3738 Type* type = this->channel_->type();
3739 Type_context context;
3740 if (type->channel_type() != NULL)
3741 context.type = type->channel_type()->element_type();
3742 this->val_->determine_type(&context);
3748 Send_statement::do_check_types(Gogo*)
3750 Type* type = this->channel_->type();
3751 if (type->is_error())
3753 this->set_is_error();
3756 Channel_type* channel_type = type->channel_type();
3757 if (channel_type == NULL)
3759 error_at(this->location(), "left operand of %<<-%> must be channel");
3760 this->set_is_error();
3763 Type* element_type = channel_type->element_type();
3764 if (!Type::are_assignable(element_type, this->val_->type(), NULL))
3766 this->report_error(_("incompatible types in send"));
3769 if (!channel_type->may_send())
3771 this->report_error(_("invalid send on receive-only channel"));
3776 // Get a tree for a send statement.
3779 Send_statement::do_get_tree(Translate_context* context)
3781 source_location loc = this->location();
3783 Channel_type* channel_type = this->channel_->type()->channel_type();
3784 Type* element_type = channel_type->element_type();
3785 Expression* val = Expression::make_cast(element_type, this->val_, loc);
3788 bool can_take_address;
3789 switch (element_type->base()->classification())
3791 case Type::TYPE_BOOLEAN:
3792 case Type::TYPE_INTEGER:
3793 case Type::TYPE_FUNCTION:
3794 case Type::TYPE_POINTER:
3795 case Type::TYPE_MAP:
3796 case Type::TYPE_CHANNEL:
3798 can_take_address = false;
3801 case Type::TYPE_FLOAT:
3802 case Type::TYPE_COMPLEX:
3803 case Type::TYPE_STRING:
3804 case Type::TYPE_INTERFACE:
3806 can_take_address = false;
3809 case Type::TYPE_STRUCT:
3811 can_take_address = true;
3814 case Type::TYPE_ARRAY:
3816 can_take_address = !element_type->is_open_array_type();
3820 case Type::TYPE_ERROR:
3821 case Type::TYPE_VOID:
3822 case Type::TYPE_SINK:
3823 case Type::TYPE_NIL:
3824 case Type::TYPE_NAMED:
3825 case Type::TYPE_FORWARD:
3826 gcc_assert(saw_errors());
3827 return error_mark_node;
3830 // Only try to take the address of a variable. We have already
3831 // moved variables to the heap, so this should not cause that to
3832 // happen unnecessarily.
3833 if (can_take_address
3834 && val->var_expression() == NULL
3835 && val->temporary_reference_expression() == NULL)
3836 can_take_address = false;
3838 Runtime::Function code;
3839 Bstatement* btemp = NULL;
3843 // Type is small enough to handle as uint64.
3844 code = Runtime::SEND_SMALL;
3845 val = Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
3848 else if (can_take_address)
3850 // Must pass address of value. The function doesn't change the
3851 // value, so just take its address directly.
3852 code = Runtime::SEND_BIG;
3853 val = Expression::make_unary(OPERATOR_AND, val, loc);
3857 // Must pass address of value, but the value is small enough
3858 // that it might be in registers. Copy value into temporary
3859 // variable to take address.
3860 code = Runtime::SEND_BIG;
3861 Temporary_statement* temp = Statement::make_temporary(element_type,
3863 Expression* ref = Expression::make_temporary_reference(temp, loc);
3864 val = Expression::make_unary(OPERATOR_AND, ref, loc);
3865 btemp = tree_to_stat(temp->get_tree(context));
3868 call = Runtime::make_call(code, loc, 3, this->channel_, val,
3869 Expression::make_boolean(this->for_select_, loc));
3871 context->gogo()->lower_expression(context->function(), &call);
3872 Bexpression* bcall = tree_to_expr(call->get_tree(context));
3873 Bstatement* s = context->backend()->expression_statement(bcall);
3876 return stat_to_tree(s);
3878 return stat_to_tree(context->backend()->compound_statement(btemp, s));
3881 // Make a send statement.
3884 Statement::make_send_statement(Expression* channel, Expression* val,
3885 source_location location)
3887 return new Send_statement(channel, val, location);
3890 // Class Select_clauses::Select_clause.
3895 Select_clauses::Select_clause::traverse(Traverse* traverse)
3897 if (!this->is_lowered_
3898 && (traverse->traverse_mask()
3899 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3901 if (this->channel_ != NULL)
3903 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
3904 return TRAVERSE_EXIT;
3906 if (this->val_ != NULL)
3908 if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
3909 return TRAVERSE_EXIT;
3911 if (this->closed_ != NULL)
3913 if (Expression::traverse(&this->closed_, traverse) == TRAVERSE_EXIT)
3914 return TRAVERSE_EXIT;
3917 if (this->statements_ != NULL)
3919 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3920 return TRAVERSE_EXIT;
3922 return TRAVERSE_CONTINUE;
3925 // Lowering. Here we pull out the channel and the send values, to
3926 // enforce the order of evaluation. We also add explicit send and
3927 // receive statements to the clauses.
3930 Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
3933 if (this->is_default_)
3935 gcc_assert(this->channel_ == NULL && this->val_ == NULL);
3936 this->is_lowered_ = true;
3940 source_location loc = this->location_;
3942 // Evaluate the channel before the select statement.
3943 Temporary_statement* channel_temp = Statement::make_temporary(NULL,
3946 b->add_statement(channel_temp);
3947 this->channel_ = Expression::make_temporary_reference(channel_temp, loc);
3949 // If this is a send clause, evaluate the value to send before the
3950 // select statement.
3951 Temporary_statement* val_temp = NULL;
3952 if (this->is_send_ && !this->val_->is_constant())
3954 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3955 b->add_statement(val_temp);
3958 // Add the send or receive before the rest of the statements if any.
3959 Block *init = new Block(b, loc);
3960 Expression* ref = Expression::make_temporary_reference(channel_temp, loc);
3964 if (val_temp == NULL)
3967 ref2 = Expression::make_temporary_reference(val_temp, loc);
3968 Send_statement* send = Statement::make_send_statement(ref, ref2, loc);
3969 send->set_for_select();
3970 init->add_statement(send);
3972 else if (this->closed_ != NULL && !this->closed_->is_sink_expression())
3974 gcc_assert(this->var_ == NULL && this->closedvar_ == NULL);
3975 if (this->val_ == NULL)
3976 this->val_ = Expression::make_sink(loc);
3977 Statement* s = Statement::make_tuple_receive_assignment(this->val_,
3980 init->add_statement(s);
3982 else if (this->closedvar_ != NULL)
3984 gcc_assert(this->val_ == NULL);
3986 if (this->var_ == NULL)
3987 val = Expression::make_sink(loc);
3989 val = Expression::make_var_reference(this->var_, loc);
3990 Expression* closed = Expression::make_var_reference(this->closedvar_,
3992 Statement* s = Statement::make_tuple_receive_assignment(val, closed, ref,
3994 // We have to put S in STATEMENTS_, because that is where the
3995 // variables are declared.
3996 gcc_assert(this->statements_ != NULL);
3997 this->statements_->add_statement_at_front(s);
3998 // We have to lower STATEMENTS_ again, to lower the tuple
3999 // receive assignment we just added.
4000 gogo->lower_block(function, this->statements_);
4004 Receive_expression* recv = Expression::make_receive(ref, loc);
4005 recv->set_for_select();
4006 if (this->val_ != NULL)
4008 gcc_assert(this->var_ == NULL);
4009 init->add_statement(Statement::make_assignment(this->val_, recv,
4012 else if (this->var_ != NULL)
4014 this->var_->var_value()->set_init(recv);
4015 this->var_->var_value()->clear_type_from_chan_element();
4019 init->add_statement(Statement::make_statement(recv));
4023 // Lower any statements we just created.
4024 gogo->lower_block(function, init);
4026 if (this->statements_ != NULL)
4027 init->add_statement(Statement::make_block_statement(this->statements_,
4030 this->statements_ = init;
4032 // Now all references should be handled through the statements, not
4034 this->is_lowered_ = true;
4042 Select_clauses::Select_clause::determine_types()
4044 gcc_assert(this->is_lowered_);
4045 if (this->statements_ != NULL)
4046 this->statements_->determine_types();
4049 // Whether this clause may fall through to the statement which follows
4050 // the overall select statement.
4053 Select_clauses::Select_clause::may_fall_through() const
4055 if (this->statements_ == NULL)
4057 return this->statements_->may_fall_through();
4060 // Return a tree for the statements to execute.
4063 Select_clauses::Select_clause::get_statements_backend(
4064 Translate_context* context)
4066 if (this->statements_ == NULL)
4068 Bblock* bblock = this->statements_->get_backend(context);
4069 return context->backend()->block_statement(bblock);
4072 // Class Select_clauses.
4077 Select_clauses::traverse(Traverse* traverse)
4079 for (Clauses::iterator p = this->clauses_.begin();
4080 p != this->clauses_.end();
4083 if (p->traverse(traverse) == TRAVERSE_EXIT)
4084 return TRAVERSE_EXIT;
4086 return TRAVERSE_CONTINUE;
4089 // Lowering. Here we pull out the channel and the send values, to
4090 // enforce the order of evaluation. We also add explicit send and
4091 // receive statements to the clauses.
4094 Select_clauses::lower(Gogo* gogo, Named_object* function, Block* b)
4096 for (Clauses::iterator p = this->clauses_.begin();
4097 p != this->clauses_.end();
4099 p->lower(gogo, function, b);
4105 Select_clauses::determine_types()
4107 for (Clauses::iterator p = this->clauses_.begin();
4108 p != this->clauses_.end();
4110 p->determine_types();
4113 // Return whether these select clauses fall through to the statement
4114 // following the overall select statement.
4117 Select_clauses::may_fall_through() const
4119 for (Clauses::const_iterator p = this->clauses_.begin();
4120 p != this->clauses_.end();
4122 if (p->may_fall_through())
4127 // Convert to the backend representation. We build a call to
4128 // size_t __go_select(size_t count, _Bool has_default,
4129 // channel* channels, _Bool* is_send)
4131 // There are COUNT entries in the CHANNELS and IS_SEND arrays. The
4132 // value in the IS_SEND array is true for send, false for receive.
4133 // __go_select returns an integer from 0 to COUNT, inclusive. A
4134 // return of 0 means that the default case should be run; this only
4135 // happens if HAS_DEFAULT is non-zero. Otherwise the number indicates
4138 // FIXME: This doesn't handle channels which send interface types
4139 // where the receiver has a static type which matches that interface.
4142 Select_clauses::get_backend(Translate_context* context,
4143 Unnamed_label *break_label,
4144 source_location location)
4146 size_t count = this->clauses_.size();
4148 Expression_list* chan_init = new Expression_list();
4149 chan_init->reserve(count);
4151 Expression_list* is_send_init = new Expression_list();
4152 is_send_init->reserve(count);
4154 Select_clause *default_clause = NULL;
4156 Type* runtime_chanptr_type = Runtime::chanptr_type();
4157 Type* runtime_chan_type = runtime_chanptr_type->points_to();
4159 for (Clauses::iterator p = this->clauses_.begin();
4160 p != this->clauses_.end();
4163 if (p->is_default())
4165 default_clause = &*p;
4170 if (p->channel()->type()->channel_type() == NULL)
4172 // We should have given an error in the send or receive
4173 // statement we created via lowering.
4174 gcc_assert(saw_errors());
4175 return context->backend()->error_statement();
4178 Expression* c = p->channel();
4179 c = Expression::make_unsafe_cast(runtime_chan_type, c, p->location());
4180 chan_init->push_back(c);
4182 is_send_init->push_back(Expression::make_boolean(p->is_send(),
4186 if (chan_init->empty())
4188 gcc_assert(count == 0);
4190 Bstatement* ldef = break_label->get_definition(context);
4191 if (default_clause != NULL)
4193 // There is a default clause and no cases. Just execute the
4195 s = default_clause->get_statements_backend(context);
4199 // There isn't even a default clause. In this case select
4200 // pauses forever. Call the runtime function with nils.
4202 mpz_init_set_ui(zval, 0);
4203 Expression* zero = Expression::make_integer(&zval, NULL, location);
4205 Expression* default_arg = Expression::make_boolean(false, location);
4206 Expression* nil1 = Expression::make_nil(location);
4207 Expression* nil2 = nil1->copy();
4208 Expression* call = Runtime::make_call(Runtime::SELECT, location, 4,
4209 zero, default_arg, nil1, nil2);
4210 context->gogo()->lower_expression(context->function(), &call);
4211 Bexpression* bcall = tree_to_expr(call->get_tree(context));
4212 s = context->backend()->expression_statement(bcall);
4216 return context->backend()->compound_statement(s, ldef);
4218 gcc_assert(count > 0);
4220 std::vector<Bstatement*> statements;
4223 mpz_init_set_ui(ival, count);
4224 Expression* ecount = Expression::make_integer(&ival, NULL, location);
4227 Type* chan_array_type = Type::make_array_type(runtime_chan_type, ecount);
4228 Expression* chans = Expression::make_composite_literal(chan_array_type, 0,
4231 context->gogo()->lower_expression(context->function(), &chans);
4232 Temporary_statement* chan_temp = Statement::make_temporary(chan_array_type,
4235 statements.push_back(tree_to_stat(chan_temp->get_tree(context)));
4237 Type* is_send_array_type = Type::make_array_type(Type::lookup_bool_type(),
4239 Expression* is_sends = Expression::make_composite_literal(is_send_array_type,
4243 context->gogo()->lower_expression(context->function(), &is_sends);
4244 Temporary_statement* is_send_temp =
4245 Statement::make_temporary(is_send_array_type, is_sends, location);
4246 statements.push_back(tree_to_stat(is_send_temp->get_tree(context)));
4248 mpz_init_set_ui(ival, 0);
4249 Expression* zero = Expression::make_integer(&ival, NULL, location);
4252 Expression* ref = Expression::make_temporary_reference(chan_temp, location);
4253 Expression* chan_arg = Expression::make_array_index(ref, zero, NULL,
4255 chan_arg = Expression::make_unary(OPERATOR_AND, chan_arg, location);
4256 chan_arg = Expression::make_unsafe_cast(runtime_chanptr_type, chan_arg,
4259 ref = Expression::make_temporary_reference(is_send_temp, location);
4260 Expression* is_send_arg = Expression::make_array_index(ref, zero->copy(),
4262 is_send_arg = Expression::make_unary(OPERATOR_AND, is_send_arg, location);
4264 Expression* default_arg = Expression::make_boolean(default_clause != NULL,
4266 Expression* call = Runtime::make_call(Runtime::SELECT, location, 4,
4267 ecount->copy(), default_arg,
4268 chan_arg, is_send_arg);
4269 context->gogo()->lower_expression(context->function(), &call);
4270 Bexpression* bcall = tree_to_expr(call->get_tree(context));
4272 std::vector<std::vector<Bexpression*> > cases;
4273 std::vector<Bstatement*> clauses;
4275 cases.resize(count + (default_clause != NULL ? 1 : 0));
4276 clauses.resize(count + (default_clause != NULL ? 1 : 0));
4280 if (default_clause != NULL)
4282 this->add_clause_backend(context, location, index, 0, default_clause,
4283 break_label, &cases, &clauses);
4288 for (Clauses::iterator p = this->clauses_.begin();
4289 p != this->clauses_.end();
4292 if (!p->is_default())
4294 this->add_clause_backend(context, location, index, i, &*p,
4295 break_label, &cases, &clauses);
4301 Bstatement* switch_stmt = context->backend()->switch_statement(bcall,
4305 statements.push_back(switch_stmt);
4307 Bstatement* ldef = break_label->get_definition(context);
4308 statements.push_back(ldef);
4310 return context->backend()->statement_list(statements);
4313 // Add the tree for CLAUSE to STMT_LIST.
4316 Select_clauses::add_clause_backend(
4317 Translate_context* context,
4318 source_location location,
4321 Select_clause* clause,
4322 Unnamed_label* bottom_label,
4323 std::vector<std::vector<Bexpression*> > *cases,
4324 std::vector<Bstatement*>* clauses)
4327 mpz_init_set_ui(ival, case_value);
4328 Expression* e = Expression::make_integer(&ival, NULL, location);
4330 (*cases)[index].push_back(tree_to_expr(e->get_tree(context)));
4332 Bstatement* s = clause->get_statements_backend(context);
4334 source_location gloc = (clause->statements() == NULL
4335 ? clause->location()
4336 : clause->statements()->end_location());
4337 Bstatement* g = bottom_label->get_goto(context, gloc);
4340 (*clauses)[index] = g;
4342 (*clauses)[index] = context->backend()->compound_statement(s, g);
4345 // Class Select_statement.
4347 // Return the break label for this switch statement, creating it if
4351 Select_statement::break_label()
4353 if (this->break_label_ == NULL)
4354 this->break_label_ = new Unnamed_label(this->location());
4355 return this->break_label_;
4358 // Lower a select statement. This will still return a select
4359 // statement, but it will be modified to implement the order of
4360 // evaluation rules, and to include the send and receive statements as
4361 // explicit statements in the clauses.
4364 Select_statement::do_lower(Gogo* gogo, Named_object* function,
4367 if (this->is_lowered_)
4369 Block* b = new Block(enclosing, this->location());
4370 this->clauses_->lower(gogo, function, b);
4371 this->is_lowered_ = true;
4372 b->add_statement(this);
4373 return Statement::make_block_statement(b, this->location());
4376 // Return the tree for a select statement.
4379 Select_statement::do_get_tree(Translate_context* context)
4381 Bstatement* ret = this->clauses_->get_backend(context, this->break_label(),
4383 return stat_to_tree(ret);
4386 // Make a select statement.
4389 Statement::make_select_statement(source_location location)
4391 return new Select_statement(location);
4394 // Class For_statement.
4399 For_statement::do_traverse(Traverse* traverse)
4401 if (this->init_ != NULL)
4403 if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
4404 return TRAVERSE_EXIT;
4406 if (this->cond_ != NULL)
4408 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
4409 return TRAVERSE_EXIT;
4411 if (this->post_ != NULL)
4413 if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
4414 return TRAVERSE_EXIT;
4416 return this->statements_->traverse(traverse);
4419 // Lower a For_statement into if statements and gotos. Getting rid of
4420 // complex statements make it easier to handle garbage collection.
4423 For_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
4426 source_location loc = this->location();
4428 Block* b = new Block(enclosing, this->location());
4429 if (this->init_ != NULL)
4431 s = Statement::make_block_statement(this->init_,
4432 this->init_->start_location());
4433 b->add_statement(s);
4436 Unnamed_label* entry = NULL;
4437 if (this->cond_ != NULL)
4439 entry = new Unnamed_label(this->location());
4440 b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
4443 Unnamed_label* top = new Unnamed_label(this->location());
4444 b->add_statement(Statement::make_unnamed_label_statement(top));
4446 s = Statement::make_block_statement(this->statements_,
4447 this->statements_->start_location());
4448 b->add_statement(s);
4450 source_location end_loc = this->statements_->end_location();
4452 Unnamed_label* cont = this->continue_label_;
4454 b->add_statement(Statement::make_unnamed_label_statement(cont));
4456 if (this->post_ != NULL)
4458 s = Statement::make_block_statement(this->post_,
4459 this->post_->start_location());
4460 b->add_statement(s);
4461 end_loc = this->post_->end_location();
4464 if (this->cond_ == NULL)
4465 b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
4468 b->add_statement(Statement::make_unnamed_label_statement(entry));
4470 source_location cond_loc = this->cond_->location();
4471 Block* then_block = new Block(b, cond_loc);
4472 s = Statement::make_goto_unnamed_statement(top, cond_loc);
4473 then_block->add_statement(s);
4475 s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
4476 b->add_statement(s);
4479 Unnamed_label* brk = this->break_label_;
4481 b->add_statement(Statement::make_unnamed_label_statement(brk));
4483 b->set_end_location(end_loc);
4485 return Statement::make_block_statement(b, loc);
4488 // Return the break label, creating it if necessary.
4491 For_statement::break_label()
4493 if (this->break_label_ == NULL)
4494 this->break_label_ = new Unnamed_label(this->location());
4495 return this->break_label_;
4498 // Return the continue LABEL_EXPR.
4501 For_statement::continue_label()
4503 if (this->continue_label_ == NULL)
4504 this->continue_label_ = new Unnamed_label(this->location());
4505 return this->continue_label_;
4508 // Set the break and continue labels a for statement. This is used
4509 // when lowering a for range statement.
4512 For_statement::set_break_continue_labels(Unnamed_label* break_label,
4513 Unnamed_label* continue_label)
4515 gcc_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
4516 this->break_label_ = break_label;
4517 this->continue_label_ = continue_label;
4520 // Make a for statement.
4523 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
4524 source_location location)
4526 return new For_statement(init, cond, post, location);
4529 // Class For_range_statement.
4534 For_range_statement::do_traverse(Traverse* traverse)
4536 if (this->traverse_expression(traverse, &this->index_var_) == TRAVERSE_EXIT)
4537 return TRAVERSE_EXIT;
4538 if (this->value_var_ != NULL)
4540 if (this->traverse_expression(traverse, &this->value_var_)
4542 return TRAVERSE_EXIT;
4544 if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
4545 return TRAVERSE_EXIT;
4546 return this->statements_->traverse(traverse);
4549 // Lower a for range statement. For simplicity we lower this into a
4550 // for statement, which will then be lowered in turn to goto
4554 For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing)
4556 Type* range_type = this->range_->type();
4557 if (range_type->points_to() != NULL
4558 && range_type->points_to()->array_type() != NULL
4559 && !range_type->points_to()->is_open_array_type())
4560 range_type = range_type->points_to();
4563 Type* value_type = NULL;
4564 if (range_type->array_type() != NULL)
4566 index_type = Type::lookup_integer_type("int");
4567 value_type = range_type->array_type()->element_type();
4569 else if (range_type->is_string_type())
4571 index_type = Type::lookup_integer_type("int");
4572 value_type = index_type;
4574 else if (range_type->map_type() != NULL)
4576 index_type = range_type->map_type()->key_type();
4577 value_type = range_type->map_type()->val_type();
4579 else if (range_type->channel_type() != NULL)
4581 index_type = range_type->channel_type()->element_type();
4582 if (this->value_var_ != NULL)
4584 if (!this->value_var_->type()->is_error())
4585 this->report_error(_("too many variables for range clause "
4587 return Statement::make_error_statement(this->location());
4592 this->report_error(_("range clause must have "
4593 "array, slice, setring, map, or channel type"));
4594 return Statement::make_error_statement(this->location());
4597 source_location loc = this->location();
4598 Block* temp_block = new Block(enclosing, loc);
4600 Named_object* range_object = NULL;
4601 Temporary_statement* range_temp = NULL;
4602 Var_expression* ve = this->range_->var_expression();
4604 range_object = ve->named_object();
4607 range_temp = Statement::make_temporary(NULL, this->range_, loc);
4608 temp_block->add_statement(range_temp);
4611 Temporary_statement* index_temp = Statement::make_temporary(index_type,
4613 temp_block->add_statement(index_temp);
4615 Temporary_statement* value_temp = NULL;
4616 if (this->value_var_ != NULL)
4618 value_temp = Statement::make_temporary(value_type, NULL, loc);
4619 temp_block->add_statement(value_temp);
4622 Block* body = new Block(temp_block, loc);
4629 // Arrange to do a loop appropriate for the type. We will produce
4630 // for INIT ; COND ; POST {
4632 // INDEX = INDEX_TEMP
4633 // VALUE = VALUE_TEMP // If there is a value
4634 // original statements
4637 if (range_type->array_type() != NULL)
4638 this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
4639 index_temp, value_temp, &init, &cond, &iter_init,
4641 else if (range_type->is_string_type())
4642 this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
4643 index_temp, value_temp, &init, &cond, &iter_init,
4645 else if (range_type->map_type() != NULL)
4646 this->lower_range_map(gogo, temp_block, body, range_object, range_temp,
4647 index_temp, value_temp, &init, &cond, &iter_init,
4649 else if (range_type->channel_type() != NULL)
4650 this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
4651 index_temp, value_temp, &init, &cond, &iter_init,
4656 if (iter_init != NULL)
4657 body->add_statement(Statement::make_block_statement(iter_init, loc));
4660 Expression* index_ref = Expression::make_temporary_reference(index_temp, loc);
4661 if (this->value_var_ == NULL)
4663 assign = Statement::make_assignment(this->index_var_, index_ref, loc);
4667 Expression_list* lhs = new Expression_list();
4668 lhs->push_back(this->index_var_);
4669 lhs->push_back(this->value_var_);
4671 Expression_list* rhs = new Expression_list();
4672 rhs->push_back(index_ref);
4673 rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4675 assign = Statement::make_tuple_assignment(lhs, rhs, loc);
4677 body->add_statement(assign);
4679 body->add_statement(Statement::make_block_statement(this->statements_, loc));
4681 body->set_end_location(this->statements_->end_location());
4683 For_statement* loop = Statement::make_for_statement(init, cond, post,
4685 loop->add_statements(body);
4686 loop->set_break_continue_labels(this->break_label_, this->continue_label_);
4688 temp_block->add_statement(loop);
4690 return Statement::make_block_statement(temp_block, loc);
4693 // Return a reference to the range, which may be in RANGE_OBJECT or in
4697 For_range_statement::make_range_ref(Named_object* range_object,
4698 Temporary_statement* range_temp,
4699 source_location loc)
4701 if (range_object != NULL)
4702 return Expression::make_var_reference(range_object, loc);
4704 return Expression::make_temporary_reference(range_temp, loc);
4707 // Return a call to the predeclared function FUNCNAME passing a
4708 // reference to the temporary variable ARG.
4711 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
4713 source_location loc)
4715 Named_object* no = gogo->lookup_global(funcname);
4716 gcc_assert(no != NULL && no->is_function_declaration());
4717 Expression* func = Expression::make_func_reference(no, NULL, loc);
4718 Expression_list* params = new Expression_list();
4719 params->push_back(arg);
4720 return Expression::make_call(func, params, false, loc);
4723 // Lower a for range over an array or slice.
4726 For_range_statement::lower_range_array(Gogo* gogo,
4729 Named_object* range_object,
4730 Temporary_statement* range_temp,
4731 Temporary_statement* index_temp,
4732 Temporary_statement* value_temp,
4738 source_location loc = this->location();
4740 // The loop we generate:
4741 // len_temp := len(range)
4742 // for index_temp = 0; index_temp < len_temp; index_temp++ {
4743 // value_temp = range[index_temp]
4744 // index = index_temp
4745 // value = value_temp
4751 // len_temp = len(range)
4754 Block* init = new Block(enclosing, loc);
4756 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
4757 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
4758 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
4760 init->add_statement(len_temp);
4763 mpz_init_set_ui(zval, 0UL);
4764 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4767 ref = Expression::make_temporary_reference(index_temp, loc);
4768 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4769 init->add_statement(s);
4774 // index_temp < len_temp
4776 ref = Expression::make_temporary_reference(index_temp, loc);
4777 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
4778 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
4782 // Set *PITER_INIT to
4783 // value_temp = range[index_temp]
4785 Block* iter_init = NULL;
4786 if (value_temp != NULL)
4788 iter_init = new Block(body_block, loc);
4790 ref = this->make_range_ref(range_object, range_temp, loc);
4791 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
4792 Expression* index = Expression::make_index(ref, ref2, NULL, loc);
4794 ref = Expression::make_temporary_reference(value_temp, loc);
4795 s = Statement::make_assignment(ref, index, loc);
4797 iter_init->add_statement(s);
4799 *piter_init = iter_init;
4804 Block* post = new Block(enclosing, loc);
4805 ref = Expression::make_temporary_reference(index_temp, loc);
4806 s = Statement::make_inc_statement(ref);
4807 post->add_statement(s);
4811 // Lower a for range over a string.
4814 For_range_statement::lower_range_string(Gogo*,
4817 Named_object* range_object,
4818 Temporary_statement* range_temp,
4819 Temporary_statement* index_temp,
4820 Temporary_statement* value_temp,
4826 source_location loc = this->location();
4828 // The loop we generate:
4829 // var next_index_temp int
4830 // for index_temp = 0; ; index_temp = next_index_temp {
4831 // next_index_temp, value_temp = stringiter2(range, index_temp)
4832 // if next_index_temp == 0 {
4835 // index = index_temp
4836 // value = value_temp
4841 // var next_index_temp int
4844 Block* init = new Block(enclosing, loc);
4846 Temporary_statement* next_index_temp =
4847 Statement::make_temporary(index_temp->type(), NULL, loc);
4848 init->add_statement(next_index_temp);
4851 mpz_init_set_ui(zval, 0UL);
4852 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4854 Expression* ref = Expression::make_temporary_reference(index_temp, loc);
4855 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4857 init->add_statement(s);
4860 // The loop has no condition.
4864 // Set *PITER_INIT to
4865 // next_index_temp = runtime.stringiter(range, index_temp)
4867 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
4869 // if next_index_temp == 0 {
4873 Block* iter_init = new Block(body_block, loc);
4875 Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
4876 Expression* p2 = Expression::make_temporary_reference(index_temp, loc);
4877 Call_expression* call = Runtime::make_call((value_temp == NULL
4878 ? Runtime::STRINGITER
4879 : Runtime::STRINGITER2),
4882 if (value_temp == NULL)
4884 ref = Expression::make_temporary_reference(next_index_temp, loc);
4885 s = Statement::make_assignment(ref, call, loc);
4889 Expression_list* lhs = new Expression_list();
4890 lhs->push_back(Expression::make_temporary_reference(next_index_temp,
4892 lhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4894 Expression_list* rhs = new Expression_list();
4895 rhs->push_back(Expression::make_call_result(call, 0));
4896 rhs->push_back(Expression::make_call_result(call, 1));
4898 s = Statement::make_tuple_assignment(lhs, rhs, loc);
4900 iter_init->add_statement(s);
4902 ref = Expression::make_temporary_reference(next_index_temp, loc);
4903 zexpr = Expression::make_integer(&zval, NULL, loc);
4905 Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
4907 Block* then_block = new Block(iter_init, loc);
4908 s = Statement::make_break_statement(this->break_label(), loc);
4909 then_block->add_statement(s);
4911 s = Statement::make_if_statement(equals, then_block, NULL, loc);
4912 iter_init->add_statement(s);
4914 *piter_init = iter_init;
4917 // index_temp = next_index_temp
4919 Block* post = new Block(enclosing, loc);
4921 Expression* lhs = Expression::make_temporary_reference(index_temp, loc);
4922 Expression* rhs = Expression::make_temporary_reference(next_index_temp, loc);
4923 s = Statement::make_assignment(lhs, rhs, loc);
4925 post->add_statement(s);
4929 // Lower a for range over a map.
4932 For_range_statement::lower_range_map(Gogo*,
4935 Named_object* range_object,
4936 Temporary_statement* range_temp,
4937 Temporary_statement* index_temp,
4938 Temporary_statement* value_temp,
4944 source_location loc = this->location();
4946 // The runtime uses a struct to handle ranges over a map. The
4947 // struct is four pointers long. The first pointer is NULL when we
4948 // have completed the iteration.
4950 // The loop we generate:
4951 // var hiter map_iteration_struct
4952 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
4953 // mapiter2(hiter, &index_temp, &value_temp)
4954 // index = index_temp
4955 // value = value_temp
4960 // var hiter map_iteration_struct
4961 // runtime.mapiterinit(range, &hiter)
4963 Block* init = new Block(enclosing, loc);
4965 Type* map_iteration_type = Runtime::map_iteration_type();
4966 Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
4968 init->add_statement(hiter);
4970 Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
4971 Expression* ref = Expression::make_temporary_reference(hiter, loc);
4972 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
4973 Expression* call = Runtime::make_call(Runtime::MAPITERINIT, loc, 2, p1, p2);
4974 init->add_statement(Statement::make_statement(call));
4981 ref = Expression::make_temporary_reference(hiter, loc);
4984 mpz_init_set_ui(zval, 0UL);
4985 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4988 Expression* index = Expression::make_index(ref, zexpr, NULL, loc);
4990 Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
4991 Expression::make_nil(loc),
4996 // Set *PITER_INIT to
4997 // mapiter1(hiter, &index_temp)
4999 // mapiter2(hiter, &index_temp, &value_temp)
5001 Block* iter_init = new Block(body_block, loc);
5003 ref = Expression::make_temporary_reference(hiter, loc);
5004 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
5005 ref = Expression::make_temporary_reference(index_temp, loc);
5006 p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
5007 if (value_temp == NULL)
5008 call = Runtime::make_call(Runtime::MAPITER1, loc, 2, p1, p2);
5011 ref = Expression::make_temporary_reference(value_temp, loc);
5012 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
5013 call = Runtime::make_call(Runtime::MAPITER2, loc, 3, p1, p2, p3);
5015 iter_init->add_statement(Statement::make_statement(call));
5017 *piter_init = iter_init;
5020 // mapiternext(&hiter)
5022 Block* post = new Block(enclosing, loc);
5024 ref = Expression::make_temporary_reference(hiter, loc);
5025 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
5026 call = Runtime::make_call(Runtime::MAPITERNEXT, loc, 1, p1);
5027 post->add_statement(Statement::make_statement(call));
5032 // Lower a for range over a channel.
5035 For_range_statement::lower_range_channel(Gogo*,
5038 Named_object* range_object,
5039 Temporary_statement* range_temp,
5040 Temporary_statement* index_temp,
5041 Temporary_statement* value_temp,
5047 gcc_assert(value_temp == NULL);
5049 source_location loc = this->location();
5051 // The loop we generate:
5053 // index_temp, ok_temp = <-range
5057 // index = index_temp
5061 // We have no initialization code, no condition, and no post code.
5067 // Set *PITER_INIT to
5068 // index_temp, ok_temp = <-range
5073 Block* iter_init = new Block(body_block, loc);
5075 Temporary_statement* ok_temp =
5076 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
5077 iter_init->add_statement(ok_temp);
5079 Expression* cref = this->make_range_ref(range_object, range_temp, loc);
5080 Expression* iref = Expression::make_temporary_reference(index_temp, loc);
5081 Expression* oref = Expression::make_temporary_reference(ok_temp, loc);
5082 Statement* s = Statement::make_tuple_receive_assignment(iref, oref, cref,
5084 iter_init->add_statement(s);
5086 Block* then_block = new Block(iter_init, loc);
5087 s = Statement::make_break_statement(this->break_label(), loc);
5088 then_block->add_statement(s);
5090 oref = Expression::make_temporary_reference(ok_temp, loc);
5091 Expression* cond = Expression::make_unary(OPERATOR_NOT, oref, loc);
5092 s = Statement::make_if_statement(cond, then_block, NULL, loc);
5093 iter_init->add_statement(s);
5095 *piter_init = iter_init;
5098 // Return the break LABEL_EXPR.
5101 For_range_statement::break_label()
5103 if (this->break_label_ == NULL)
5104 this->break_label_ = new Unnamed_label(this->location());
5105 return this->break_label_;
5108 // Return the continue LABEL_EXPR.
5111 For_range_statement::continue_label()
5113 if (this->continue_label_ == NULL)
5114 this->continue_label_ = new Unnamed_label(this->location());
5115 return this->continue_label_;
5118 // Make a for statement with a range clause.
5120 For_range_statement*
5121 Statement::make_for_range_statement(Expression* index_var,
5122 Expression* value_var,
5124 source_location location)
5126 return new For_range_statement(index_var, value_var, range, location);