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 tree val = this->var_->get_tree(context->gogo(), context->function());
255 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
256 return error_mark_node;
257 Variable* variable = this->var_->var_value();
259 tree init = variable->get_init_tree(context->gogo(), context->function());
260 if (init == error_mark_node)
261 return error_mark_node;
263 // If this variable lives on the heap, we need to allocate it now.
264 if (!variable->is_in_heap())
266 DECL_INITIAL(val) = init;
267 return this->build_stmt_1(DECL_EXPR, val);
271 gcc_assert(TREE_CODE(val) == INDIRECT_REF);
272 tree decl = TREE_OPERAND(val, 0);
273 gcc_assert(TREE_CODE(decl) == VAR_DECL);
274 tree type = TREE_TYPE(decl);
275 gcc_assert(POINTER_TYPE_P(type));
276 tree size = TYPE_SIZE_UNIT(TREE_TYPE(type));
277 tree space = context->gogo()->allocate_memory(variable->type(), size,
279 space = fold_convert(TREE_TYPE(decl), space);
280 DECL_INITIAL(decl) = space;
281 return build2(COMPOUND_EXPR, void_type_node,
282 this->build_stmt_1(DECL_EXPR, decl),
283 build2(MODIFY_EXPR, void_type_node, val, init));
287 // Make a variable declaration.
290 Statement::make_variable_declaration(Named_object* var)
292 return new Variable_declaration_statement(var);
295 // Class Temporary_statement.
297 // Return the type of the temporary variable.
300 Temporary_statement::type() const
302 return this->type_ != NULL ? this->type_ : this->init_->type();
305 // Return the tree for the temporary variable.
308 Temporary_statement::get_decl() const
310 if (this->decl_ == NULL)
312 gcc_assert(saw_errors());
313 return error_mark_node;
321 Temporary_statement::do_traverse(Traverse* traverse)
323 if (this->type_ != NULL
324 && this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
325 return TRAVERSE_EXIT;
326 if (this->init_ == NULL)
327 return TRAVERSE_CONTINUE;
329 return this->traverse_expression(traverse, &this->init_);
332 // Traverse assignments.
335 Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
337 if (this->init_ == NULL)
339 tassign->value(&this->init_, true, true);
346 Temporary_statement::do_determine_types()
348 if (this->type_ != NULL && this->type_->is_abstract())
349 this->type_ = this->type_->make_non_abstract_type();
351 if (this->init_ != NULL)
353 if (this->type_ == NULL)
354 this->init_->determine_type_no_context();
357 Type_context context(this->type_, false);
358 this->init_->determine_type(&context);
362 if (this->type_ == NULL)
364 this->type_ = this->init_->type();
365 gcc_assert(!this->type_->is_abstract());
372 Temporary_statement::do_check_types(Gogo*)
374 if (this->type_ != NULL && this->init_ != NULL)
377 if (!Type::are_assignable(this->type_, this->init_->type(), &reason))
380 error_at(this->location(), "incompatible types in assignment");
382 error_at(this->location(), "incompatible types in assignment (%s)",
384 this->set_is_error();
392 Temporary_statement::do_get_tree(Translate_context* context)
394 gcc_assert(this->decl_ == NULL_TREE);
395 tree type_tree = this->type()->get_tree(context->gogo());
396 tree init_tree = (this->init_ == NULL
398 : this->init_->get_tree(context));
399 if (type_tree == error_mark_node || init_tree == error_mark_node)
401 this->decl_ = error_mark_node;
402 return error_mark_node;
404 // We can only use create_tmp_var if the type is not addressable.
405 if (!TREE_ADDRESSABLE(type_tree))
407 this->decl_ = create_tmp_var(type_tree, "GOTMP");
408 DECL_SOURCE_LOCATION(this->decl_) = this->location();
412 gcc_assert(context->function() != NULL && context->block() != NULL);
413 tree decl = build_decl(this->location(), VAR_DECL,
414 create_tmp_var_name("GOTMP"),
416 DECL_ARTIFICIAL(decl) = 1;
417 DECL_IGNORED_P(decl) = 1;
419 gcc_assert(current_function_decl != NULL_TREE);
420 DECL_CONTEXT(decl) = current_function_decl;
422 // We have to add this variable to the block so that it winds up
424 tree block_tree = context->block_tree();
425 gcc_assert(block_tree != NULL_TREE);
426 DECL_CHAIN(decl) = BLOCK_VARS(block_tree);
427 BLOCK_VARS(block_tree) = decl;
431 if (init_tree != NULL_TREE)
432 DECL_INITIAL(this->decl_) =
433 Expression::convert_for_assignment(context, this->type(),
434 this->init_->type(), init_tree,
436 if (this->is_address_taken_)
437 TREE_ADDRESSABLE(this->decl_) = 1;
438 return this->build_stmt_1(DECL_EXPR, this->decl_);
441 // Make and initialize a temporary variable in BLOCK.
444 Statement::make_temporary(Type* type, Expression* init,
445 source_location location)
447 return new Temporary_statement(type, init, location);
450 // An assignment statement.
452 class Assignment_statement : public Statement
455 Assignment_statement(Expression* lhs, Expression* rhs,
456 source_location location)
457 : Statement(STATEMENT_ASSIGNMENT, location),
463 do_traverse(Traverse* traverse);
466 do_traverse_assignments(Traverse_assignments*);
469 do_determine_types();
472 do_check_types(Gogo*);
475 do_get_tree(Translate_context*);
478 // Left hand side--the lvalue.
480 // Right hand side--the rvalue.
487 Assignment_statement::do_traverse(Traverse* traverse)
489 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
490 return TRAVERSE_EXIT;
491 return this->traverse_expression(traverse, &this->rhs_);
495 Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
497 tassign->assignment(&this->lhs_, &this->rhs_);
501 // Set types for the assignment.
504 Assignment_statement::do_determine_types()
506 this->lhs_->determine_type_no_context();
507 Type_context context(this->lhs_->type(), false);
508 this->rhs_->determine_type(&context);
511 // Check types for an assignment.
514 Assignment_statement::do_check_types(Gogo*)
516 // The left hand side must be either addressable, a map index
517 // expression, or the blank identifier.
518 if (!this->lhs_->is_addressable()
519 && this->lhs_->map_index_expression() == NULL
520 && !this->lhs_->is_sink_expression())
522 if (!this->lhs_->type()->is_error())
523 this->report_error(_("invalid left hand side of assignment"));
527 Type* lhs_type = this->lhs_->type();
528 Type* rhs_type = this->rhs_->type();
530 if (!Type::are_assignable(lhs_type, rhs_type, &reason))
533 error_at(this->location(), "incompatible types in assignment");
535 error_at(this->location(), "incompatible types in assignment (%s)",
537 this->set_is_error();
540 if (lhs_type->is_error() || rhs_type->is_error())
541 this->set_is_error();
544 // Build a tree for an assignment statement.
547 Assignment_statement::do_get_tree(Translate_context* context)
549 tree rhs_tree = this->rhs_->get_tree(context);
551 if (this->lhs_->is_sink_expression())
554 tree lhs_tree = this->lhs_->get_tree(context);
556 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
557 return error_mark_node;
559 rhs_tree = Expression::convert_for_assignment(context, this->lhs_->type(),
560 this->rhs_->type(), rhs_tree,
562 if (rhs_tree == error_mark_node)
563 return error_mark_node;
566 ret = context->backend()->assignment_statement(tree_to_expr(lhs_tree),
567 tree_to_expr(rhs_tree),
569 return stat_to_tree(ret);
572 // Make an assignment statement.
575 Statement::make_assignment(Expression* lhs, Expression* rhs,
576 source_location location)
578 return new Assignment_statement(lhs, rhs, location);
581 // The Move_ordered_evals class is used to find any subexpressions of
582 // an expression that have an evaluation order dependency. It creates
583 // temporary variables to hold them.
585 class Move_ordered_evals : public Traverse
588 Move_ordered_evals(Block* block)
589 : Traverse(traverse_expressions),
595 expression(Expression**);
598 // The block where new temporary variables should be added.
603 Move_ordered_evals::expression(Expression** pexpr)
605 // We have to look at subexpressions first.
606 if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
607 return TRAVERSE_EXIT;
608 if ((*pexpr)->must_eval_in_order())
610 source_location loc = (*pexpr)->location();
611 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
612 this->block_->add_statement(temp);
613 *pexpr = Expression::make_temporary_reference(temp, loc);
615 return TRAVERSE_SKIP_COMPONENTS;
618 // An assignment operation statement.
620 class Assignment_operation_statement : public Statement
623 Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
624 source_location location)
625 : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
626 op_(op), lhs_(lhs), rhs_(rhs)
631 do_traverse(Traverse*);
634 do_traverse_assignments(Traverse_assignments*)
635 { gcc_unreachable(); }
638 do_lower(Gogo*, Named_object*, Block*);
641 do_get_tree(Translate_context*)
642 { gcc_unreachable(); }
645 // The operator (OPERATOR_PLUSEQ, etc.).
656 Assignment_operation_statement::do_traverse(Traverse* traverse)
658 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
659 return TRAVERSE_EXIT;
660 return this->traverse_expression(traverse, &this->rhs_);
663 // Lower an assignment operation statement to a regular assignment
667 Assignment_operation_statement::do_lower(Gogo*, Named_object*,
670 source_location loc = this->location();
672 // We have to evaluate the left hand side expression only once. We
673 // do this by moving out any expression with side effects.
674 Block* b = new Block(enclosing, loc);
675 Move_ordered_evals moe(b);
676 this->lhs_->traverse_subexpressions(&moe);
678 Expression* lval = this->lhs_->copy();
683 case OPERATOR_PLUSEQ:
686 case OPERATOR_MINUSEQ:
695 case OPERATOR_MULTEQ:
704 case OPERATOR_LSHIFTEQ:
705 op = OPERATOR_LSHIFT;
707 case OPERATOR_RSHIFTEQ:
708 op = OPERATOR_RSHIFT;
713 case OPERATOR_BITCLEAREQ:
714 op = OPERATOR_BITCLEAR;
720 Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
721 Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
722 if (b->statements()->empty())
730 return Statement::make_block_statement(b, loc);
734 // Make an assignment operation statement.
737 Statement::make_assignment_operation(Operator op, Expression* lhs,
738 Expression* rhs, source_location location)
740 return new Assignment_operation_statement(op, lhs, rhs, location);
743 // A tuple assignment statement. This differs from an assignment
744 // statement in that the right-hand-side expressions are evaluated in
747 class Tuple_assignment_statement : public Statement
750 Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
751 source_location location)
752 : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
758 do_traverse(Traverse* traverse);
761 do_traverse_assignments(Traverse_assignments*)
762 { gcc_unreachable(); }
765 do_lower(Gogo*, Named_object*, Block*);
768 do_get_tree(Translate_context*)
769 { gcc_unreachable(); }
772 // Left hand side--a list of lvalues.
773 Expression_list* lhs_;
774 // Right hand side--a list of rvalues.
775 Expression_list* rhs_;
781 Tuple_assignment_statement::do_traverse(Traverse* traverse)
783 if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
784 return TRAVERSE_EXIT;
785 return this->traverse_expression_list(traverse, this->rhs_);
788 // Lower a tuple assignment. We use temporary variables to split it
789 // up into a set of single assignments.
792 Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
794 source_location loc = this->location();
796 Block* b = new Block(enclosing, loc);
798 // First move out any subexpressions on the left hand side. The
799 // right hand side will be evaluated in the required order anyhow.
800 Move_ordered_evals moe(b);
801 for (Expression_list::const_iterator plhs = this->lhs_->begin();
802 plhs != this->lhs_->end();
804 (*plhs)->traverse_subexpressions(&moe);
806 std::vector<Temporary_statement*> temps;
807 temps.reserve(this->lhs_->size());
809 Expression_list::const_iterator prhs = this->rhs_->begin();
810 for (Expression_list::const_iterator plhs = this->lhs_->begin();
811 plhs != this->lhs_->end();
814 gcc_assert(prhs != this->rhs_->end());
816 if ((*plhs)->is_error_expression()
817 || (*plhs)->type()->is_error()
818 || (*prhs)->is_error_expression()
819 || (*prhs)->type()->is_error())
822 if ((*plhs)->is_sink_expression())
824 b->add_statement(Statement::make_statement(*prhs));
828 Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
830 b->add_statement(temp);
831 temps.push_back(temp);
834 gcc_assert(prhs == this->rhs_->end());
836 prhs = this->rhs_->begin();
837 std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
838 for (Expression_list::const_iterator plhs = this->lhs_->begin();
839 plhs != this->lhs_->end();
842 if ((*plhs)->is_error_expression()
843 || (*plhs)->type()->is_error()
844 || (*prhs)->is_error_expression()
845 || (*prhs)->type()->is_error())
848 if ((*plhs)->is_sink_expression())
851 Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
852 Statement* s = Statement::make_assignment(*plhs, ref, loc);
856 gcc_assert(ptemp == temps.end());
858 return Statement::make_block_statement(b, loc);
861 // Make a tuple assignment statement.
864 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
865 source_location location)
867 return new Tuple_assignment_statement(lhs, rhs, location);
870 // A tuple assignment from a map index expression.
873 class Tuple_map_assignment_statement : public Statement
876 Tuple_map_assignment_statement(Expression* val, Expression* present,
877 Expression* map_index,
878 source_location location)
879 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
880 val_(val), present_(present), map_index_(map_index)
885 do_traverse(Traverse* traverse);
888 do_traverse_assignments(Traverse_assignments*)
889 { gcc_unreachable(); }
892 do_lower(Gogo*, Named_object*, Block*);
895 do_get_tree(Translate_context*)
896 { gcc_unreachable(); }
899 // Lvalue which receives the value from the map.
901 // Lvalue which receives whether the key value was present.
902 Expression* present_;
903 // The map index expression.
904 Expression* map_index_;
910 Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
912 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
913 || this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
914 return TRAVERSE_EXIT;
915 return this->traverse_expression(traverse, &this->map_index_);
918 // Lower a tuple map assignment.
921 Tuple_map_assignment_statement::do_lower(Gogo*, Named_object*,
924 source_location loc = this->location();
926 Map_index_expression* map_index = this->map_index_->map_index_expression();
927 if (map_index == NULL)
929 this->report_error(_("expected map index on right hand side"));
930 return Statement::make_error_statement(loc);
932 Map_type* map_type = map_index->get_map_type();
933 if (map_type == NULL)
934 return Statement::make_error_statement(loc);
936 Block* b = new Block(enclosing, loc);
938 // Move out any subexpressions to make sure that functions are
939 // called in the required order.
940 Move_ordered_evals moe(b);
941 this->val_->traverse_subexpressions(&moe);
942 this->present_->traverse_subexpressions(&moe);
944 // Copy the key value into a temporary so that we can take its
945 // address without pushing the value onto the heap.
947 // var key_temp KEY_TYPE = MAP_INDEX
948 Temporary_statement* key_temp =
949 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
950 b->add_statement(key_temp);
952 // var val_temp VAL_TYPE
953 Temporary_statement* val_temp =
954 Statement::make_temporary(map_type->val_type(), NULL, loc);
955 b->add_statement(val_temp);
957 // var present_temp bool
958 Temporary_statement* present_temp =
959 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
960 b->add_statement(present_temp);
962 // present_temp = mapaccess2(MAP, &key_temp, &val_temp)
963 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
964 Expression* a1 = Expression::make_unary(OPERATOR_AND, ref, loc);
965 ref = Expression::make_temporary_reference(val_temp, loc);
966 Expression* a2 = Expression::make_unary(OPERATOR_AND, ref, loc);
967 Expression* call = Runtime::make_call(Runtime::MAPACCESS2, loc, 3,
968 map_index->map(), a1, a2);
970 ref = Expression::make_temporary_reference(present_temp, loc);
971 Statement* s = Statement::make_assignment(ref, call, loc);
975 ref = Expression::make_temporary_reference(val_temp, loc);
976 s = Statement::make_assignment(this->val_, ref, loc);
979 // present = present_temp
980 ref = Expression::make_temporary_reference(present_temp, loc);
981 s = Statement::make_assignment(this->present_, ref, loc);
984 return Statement::make_block_statement(b, loc);
987 // Make a map assignment statement which returns a pair of values.
990 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
991 Expression* map_index,
992 source_location location)
994 return new Tuple_map_assignment_statement(val, present, map_index, location);
997 // Assign a pair of entries to a map.
1000 class Map_assignment_statement : public Statement
1003 Map_assignment_statement(Expression* map_index,
1004 Expression* val, Expression* should_set,
1005 source_location location)
1006 : Statement(STATEMENT_MAP_ASSIGNMENT, location),
1007 map_index_(map_index), val_(val), should_set_(should_set)
1012 do_traverse(Traverse* traverse);
1015 do_traverse_assignments(Traverse_assignments*)
1016 { gcc_unreachable(); }
1019 do_lower(Gogo*, Named_object*, Block*);
1022 do_get_tree(Translate_context*)
1023 { gcc_unreachable(); }
1026 // A reference to the map index which should be set or deleted.
1027 Expression* map_index_;
1028 // The value to add to the map.
1030 // Whether or not to add the value.
1031 Expression* should_set_;
1034 // Traverse a map assignment.
1037 Map_assignment_statement::do_traverse(Traverse* traverse)
1039 if (this->traverse_expression(traverse, &this->map_index_) == TRAVERSE_EXIT
1040 || this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
1041 return TRAVERSE_EXIT;
1042 return this->traverse_expression(traverse, &this->should_set_);
1045 // Lower a map assignment to a function call.
1048 Map_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
1050 source_location loc = this->location();
1052 Map_index_expression* map_index = this->map_index_->map_index_expression();
1053 if (map_index == NULL)
1055 this->report_error(_("expected map index on left hand side"));
1056 return Statement::make_error_statement(loc);
1058 Map_type* map_type = map_index->get_map_type();
1059 if (map_type == NULL)
1060 return Statement::make_error_statement(loc);
1062 Block* b = new Block(enclosing, loc);
1064 // Evaluate the map first to get order of evaluation right.
1065 // map_temp := m // we are evaluating m[k] = v, p
1066 Temporary_statement* map_temp = Statement::make_temporary(map_type,
1069 b->add_statement(map_temp);
1071 // var key_temp MAP_KEY_TYPE = k
1072 Temporary_statement* key_temp =
1073 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1074 b->add_statement(key_temp);
1076 // var val_temp MAP_VAL_TYPE = v
1077 Temporary_statement* val_temp =
1078 Statement::make_temporary(map_type->val_type(), this->val_, loc);
1079 b->add_statement(val_temp);
1081 // var insert_temp bool = p
1082 Temporary_statement* insert_temp =
1083 Statement::make_temporary(Type::lookup_bool_type(), this->should_set_,
1085 b->add_statement(insert_temp);
1087 // mapassign2(map_temp, &key_temp, &val_temp, p)
1088 Expression* p1 = Expression::make_temporary_reference(map_temp, loc);
1089 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
1090 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1091 ref = Expression::make_temporary_reference(val_temp, loc);
1092 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1093 Expression* p4 = Expression::make_temporary_reference(insert_temp, loc);
1094 Expression* call = Runtime::make_call(Runtime::MAPASSIGN2, loc, 4,
1096 Statement* s = Statement::make_statement(call);
1097 b->add_statement(s);
1099 return Statement::make_block_statement(b, loc);
1102 // Make a statement which assigns a pair of entries to a map.
1105 Statement::make_map_assignment(Expression* map_index,
1106 Expression* val, Expression* should_set,
1107 source_location location)
1109 return new Map_assignment_statement(map_index, val, should_set, location);
1112 // A tuple assignment from a receive statement.
1114 class Tuple_receive_assignment_statement : public Statement
1117 Tuple_receive_assignment_statement(Expression* val, Expression* closed,
1118 Expression* channel, bool for_select,
1119 source_location location)
1120 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
1121 val_(val), closed_(closed), channel_(channel), for_select_(for_select)
1126 do_traverse(Traverse* traverse);
1129 do_traverse_assignments(Traverse_assignments*)
1130 { gcc_unreachable(); }
1133 do_lower(Gogo*, Named_object*, Block*);
1136 do_get_tree(Translate_context*)
1137 { gcc_unreachable(); }
1140 // Lvalue which receives the value from the channel.
1142 // Lvalue which receives whether the channel is closed.
1143 Expression* closed_;
1144 // The channel on which we receive the value.
1145 Expression* channel_;
1146 // Whether this is for a select statement.
1153 Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
1155 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1156 || this->traverse_expression(traverse, &this->closed_) == TRAVERSE_EXIT)
1157 return TRAVERSE_EXIT;
1158 return this->traverse_expression(traverse, &this->channel_);
1161 // Lower to a function call.
1164 Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
1167 source_location loc = this->location();
1169 Channel_type* channel_type = this->channel_->type()->channel_type();
1170 if (channel_type == NULL)
1172 this->report_error(_("expected channel"));
1173 return Statement::make_error_statement(loc);
1175 if (!channel_type->may_receive())
1177 this->report_error(_("invalid receive on send-only channel"));
1178 return Statement::make_error_statement(loc);
1181 Block* b = new Block(enclosing, loc);
1183 // Make sure that any subexpressions on the left hand side are
1184 // evaluated in the right order.
1185 Move_ordered_evals moe(b);
1186 this->val_->traverse_subexpressions(&moe);
1187 this->closed_->traverse_subexpressions(&moe);
1189 // var val_temp ELEMENT_TYPE
1190 Temporary_statement* val_temp =
1191 Statement::make_temporary(channel_type->element_type(), NULL, loc);
1192 b->add_statement(val_temp);
1194 // var closed_temp bool
1195 Temporary_statement* closed_temp =
1196 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
1197 b->add_statement(closed_temp);
1199 // closed_temp = chanrecv[23](channel, &val_temp)
1200 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1201 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1202 Expression* call = Runtime::make_call((this->for_select_
1203 ? Runtime::CHANRECV3
1204 : Runtime::CHANRECV2),
1205 loc, 2, this->channel_, p2);
1206 ref = Expression::make_temporary_reference(closed_temp, loc);
1207 Statement* s = Statement::make_assignment(ref, call, loc);
1208 b->add_statement(s);
1211 ref = Expression::make_temporary_reference(val_temp, loc);
1212 s = Statement::make_assignment(this->val_, ref, loc);
1213 b->add_statement(s);
1215 // closed = closed_temp
1216 ref = Expression::make_temporary_reference(closed_temp, loc);
1217 s = Statement::make_assignment(this->closed_, ref, loc);
1218 b->add_statement(s);
1220 return Statement::make_block_statement(b, loc);
1223 // Make a nonblocking receive statement.
1226 Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
1227 Expression* channel,
1229 source_location location)
1231 return new Tuple_receive_assignment_statement(val, closed, channel,
1232 for_select, location);
1235 // An assignment to a pair of values from a type guard. This is a
1236 // conditional type guard. v, ok = i.(type).
1238 class Tuple_type_guard_assignment_statement : public Statement
1241 Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
1242 Expression* expr, Type* type,
1243 source_location location)
1244 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
1245 val_(val), ok_(ok), expr_(expr), type_(type)
1250 do_traverse(Traverse*);
1253 do_traverse_assignments(Traverse_assignments*)
1254 { gcc_unreachable(); }
1257 do_lower(Gogo*, Named_object*, Block*);
1260 do_get_tree(Translate_context*)
1261 { gcc_unreachable(); }
1265 lower_to_type(Runtime::Function);
1268 lower_to_object_type(Block*, Runtime::Function);
1270 // The variable which recieves the converted value.
1272 // The variable which receives the indication of success.
1274 // The expression being converted.
1276 // The type to which the expression is being converted.
1280 // Traverse a type guard tuple assignment.
1283 Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
1285 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1286 || this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
1287 || this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
1288 return TRAVERSE_EXIT;
1289 return this->traverse_expression(traverse, &this->expr_);
1292 // Lower to a function call.
1295 Tuple_type_guard_assignment_statement::do_lower(Gogo*, Named_object*,
1298 source_location loc = this->location();
1300 Type* expr_type = this->expr_->type();
1301 if (expr_type->interface_type() == NULL)
1303 if (!expr_type->is_error() && !this->type_->is_error())
1304 this->report_error(_("type assertion only valid for interface types"));
1305 return Statement::make_error_statement(loc);
1308 Block* b = new Block(enclosing, loc);
1310 // Make sure that any subexpressions on the left hand side are
1311 // evaluated in the right order.
1312 Move_ordered_evals moe(b);
1313 this->val_->traverse_subexpressions(&moe);
1314 this->ok_->traverse_subexpressions(&moe);
1316 bool expr_is_empty = expr_type->interface_type()->is_empty();
1317 Call_expression* call;
1318 if (this->type_->interface_type() != NULL)
1320 if (this->type_->interface_type()->is_empty())
1321 call = Runtime::make_call((expr_is_empty
1322 ? Runtime::IFACEE2E2
1323 : Runtime::IFACEI2E2),
1324 loc, 1, this->expr_);
1326 call = this->lower_to_type(expr_is_empty
1327 ? Runtime::IFACEE2I2
1328 : Runtime::IFACEI2I2);
1330 else if (this->type_->points_to() != NULL)
1331 call = this->lower_to_type(expr_is_empty
1332 ? Runtime::IFACEE2T2P
1333 : Runtime::IFACEI2T2P);
1336 this->lower_to_object_type(b,
1338 ? Runtime::IFACEE2T2
1339 : Runtime::IFACEI2T2));
1345 Expression* res = Expression::make_call_result(call, 0);
1346 res = Expression::make_unsafe_cast(this->type_, res, loc);
1347 Statement* s = Statement::make_assignment(this->val_, res, loc);
1348 b->add_statement(s);
1350 res = Expression::make_call_result(call, 1);
1351 s = Statement::make_assignment(this->ok_, res, loc);
1352 b->add_statement(s);
1355 return Statement::make_block_statement(b, loc);
1358 // Lower a conversion to a non-empty interface type or a pointer type.
1361 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code)
1363 source_location loc = this->location();
1364 return Runtime::make_call(code, loc, 2,
1365 Expression::make_type_descriptor(this->type_, loc),
1369 // Lower a conversion to a non-interface non-pointer type.
1372 Tuple_type_guard_assignment_statement::lower_to_object_type(
1374 Runtime::Function code)
1376 source_location loc = this->location();
1378 // var val_temp TYPE
1379 Temporary_statement* val_temp = Statement::make_temporary(this->type_,
1381 b->add_statement(val_temp);
1383 // ok = CODE(type_descriptor, expr, &val_temp)
1384 Expression* p1 = Expression::make_type_descriptor(this->type_, loc);
1385 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1386 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1387 Expression* call = Runtime::make_call(code, loc, 3, p1, this->expr_, p3);
1388 Statement* s = Statement::make_assignment(this->ok_, call, loc);
1389 b->add_statement(s);
1392 ref = Expression::make_temporary_reference(val_temp, loc);
1393 s = Statement::make_assignment(this->val_, ref, loc);
1394 b->add_statement(s);
1397 // Make an assignment from a type guard to a pair of variables.
1400 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
1401 Expression* expr, Type* type,
1402 source_location location)
1404 return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
1408 // An expression statement.
1410 class Expression_statement : public Statement
1413 Expression_statement(Expression* expr)
1414 : Statement(STATEMENT_EXPRESSION, expr->location()),
1420 do_traverse(Traverse* traverse)
1421 { return this->traverse_expression(traverse, &this->expr_); }
1424 do_determine_types()
1425 { this->expr_->determine_type_no_context(); }
1428 do_may_fall_through() const;
1431 do_get_tree(Translate_context* context);
1437 // An expression statement may fall through unless it is a call to a
1438 // function which does not return.
1441 Expression_statement::do_may_fall_through() const
1443 const Call_expression* call = this->expr_->call_expression();
1446 const Expression* fn = call->fn();
1447 const Func_expression* fe = fn->func_expression();
1450 const Named_object* no = fe->named_object();
1452 Function_type* fntype;
1453 if (no->is_function())
1454 fntype = no->func_value()->type();
1455 else if (no->is_function_declaration())
1456 fntype = no->func_declaration_value()->type();
1460 // The builtin function panic does not return.
1461 if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
1468 // Convert to backend representation.
1471 Expression_statement::do_get_tree(Translate_context* context)
1473 tree expr_tree = this->expr_->get_tree(context);
1474 Bexpression* bexpr = tree_to_expr(expr_tree);
1475 Bstatement* ret = context->backend()->expression_statement(bexpr);
1476 return stat_to_tree(ret);
1479 // Make an expression statement from an Expression.
1482 Statement::make_statement(Expression* expr)
1484 return new Expression_statement(expr);
1487 // A block statement--a list of statements which may include variable
1490 class Block_statement : public Statement
1493 Block_statement(Block* block, source_location location)
1494 : Statement(STATEMENT_BLOCK, location),
1500 do_traverse(Traverse* traverse)
1501 { return this->block_->traverse(traverse); }
1504 do_determine_types()
1505 { this->block_->determine_types(); }
1508 do_may_fall_through() const
1509 { return this->block_->may_fall_through(); }
1512 do_get_tree(Translate_context* context)
1513 { return this->block_->get_tree(context); }
1519 // Make a block statement.
1522 Statement::make_block_statement(Block* block, source_location location)
1524 return new Block_statement(block, location);
1527 // An increment or decrement statement.
1529 class Inc_dec_statement : public Statement
1532 Inc_dec_statement(bool is_inc, Expression* expr)
1533 : Statement(STATEMENT_INCDEC, expr->location()),
1534 expr_(expr), is_inc_(is_inc)
1539 do_traverse(Traverse* traverse)
1540 { return this->traverse_expression(traverse, &this->expr_); }
1543 do_traverse_assignments(Traverse_assignments*)
1544 { gcc_unreachable(); }
1547 do_lower(Gogo*, Named_object*, Block*);
1550 do_get_tree(Translate_context*)
1551 { gcc_unreachable(); }
1554 // The l-value to increment or decrement.
1556 // Whether to increment or decrement.
1560 // Lower to += or -=.
1563 Inc_dec_statement::do_lower(Gogo*, Named_object*, Block*)
1565 source_location loc = this->location();
1568 mpz_init_set_ui(oval, 1UL);
1569 Expression* oexpr = Expression::make_integer(&oval, NULL, loc);
1572 Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
1573 return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
1576 // Make an increment statement.
1579 Statement::make_inc_statement(Expression* expr)
1581 return new Inc_dec_statement(true, expr);
1584 // Make a decrement statement.
1587 Statement::make_dec_statement(Expression* expr)
1589 return new Inc_dec_statement(false, expr);
1592 // Class Thunk_statement. This is the base class for go and defer
1595 const char* const Thunk_statement::thunk_field_fn = "fn";
1597 const char* const Thunk_statement::thunk_field_receiver = "receiver";
1601 Thunk_statement::Thunk_statement(Statement_classification classification,
1602 Call_expression* call,
1603 source_location location)
1604 : Statement(classification, location),
1605 call_(call), struct_type_(NULL)
1609 // Return whether this is a simple statement which does not require a
1613 Thunk_statement::is_simple(Function_type* fntype) const
1615 // We need a thunk to call a method, or to pass a variable number of
1617 if (fntype->is_method() || fntype->is_varargs())
1620 // A defer statement requires a thunk to set up for whether the
1621 // function can call recover.
1622 if (this->classification() == STATEMENT_DEFER)
1625 // We can only permit a single parameter of pointer type.
1626 const Typed_identifier_list* parameters = fntype->parameters();
1627 if (parameters != NULL
1628 && (parameters->size() > 1
1629 || (parameters->size() == 1
1630 && parameters->begin()->type()->points_to() == NULL)))
1633 // If the function returns multiple values, or returns a type other
1634 // than integer, floating point, or pointer, then it may get a
1635 // hidden first parameter, in which case we need the more
1636 // complicated approach. This is true even though we are going to
1637 // ignore the return value.
1638 const Typed_identifier_list* results = fntype->results();
1640 && (results->size() > 1
1641 || (results->size() == 1
1642 && !results->begin()->type()->is_basic_type()
1643 && results->begin()->type()->points_to() == NULL)))
1646 // If this calls something which is not a simple function, then we
1648 Expression* fn = this->call_->call_expression()->fn();
1649 if (fn->bound_method_expression() != NULL
1650 || fn->interface_field_reference_expression() != NULL)
1656 // Traverse a thunk statement.
1659 Thunk_statement::do_traverse(Traverse* traverse)
1661 return this->traverse_expression(traverse, &this->call_);
1664 // We implement traverse_assignment for a thunk statement because it
1665 // effectively copies the function call.
1668 Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
1670 Expression* fn = this->call_->call_expression()->fn();
1671 Expression* fn2 = fn;
1672 tassign->value(&fn2, true, false);
1676 // Determine types in a thunk statement.
1679 Thunk_statement::do_determine_types()
1681 this->call_->determine_type_no_context();
1683 // Now that we know the types of the call, build the struct used to
1685 Call_expression* ce = this->call_->call_expression();
1688 Function_type* fntype = ce->get_function_type();
1689 if (fntype != NULL && !this->is_simple(fntype))
1690 this->struct_type_ = this->build_struct(fntype);
1693 // Check types in a thunk statement.
1696 Thunk_statement::do_check_types(Gogo*)
1698 Call_expression* ce = this->call_->call_expression();
1701 if (!this->call_->is_error_expression())
1702 this->report_error("expected call expression");
1705 Function_type* fntype = ce->get_function_type();
1706 if (fntype != NULL && fntype->is_method())
1708 Expression* fn = ce->fn();
1709 if (fn->bound_method_expression() == NULL
1710 && fn->interface_field_reference_expression() == NULL)
1711 this->report_error(_("no object for method call"));
1715 // The Traverse class used to find and simplify thunk statements.
1717 class Simplify_thunk_traverse : public Traverse
1720 Simplify_thunk_traverse(Gogo* gogo)
1721 : Traverse(traverse_blocks),
1733 Simplify_thunk_traverse::block(Block* b)
1735 // The parser ensures that thunk statements always appear at the end
1737 if (b->statements()->size() < 1)
1738 return TRAVERSE_CONTINUE;
1739 Thunk_statement* stat = b->statements()->back()->thunk_statement();
1741 return TRAVERSE_CONTINUE;
1742 if (stat->simplify_statement(this->gogo_, b))
1743 return TRAVERSE_SKIP_COMPONENTS;
1744 return TRAVERSE_CONTINUE;
1747 // Simplify all thunk statements.
1750 Gogo::simplify_thunk_statements()
1752 Simplify_thunk_traverse thunk_traverse(this);
1753 this->traverse(&thunk_traverse);
1756 // Simplify complex thunk statements into simple ones. A complicated
1757 // thunk statement is one which takes anything other than zero
1758 // parameters or a single pointer parameter. We rewrite it into code
1759 // which allocates a struct, stores the parameter values into the
1760 // struct, and does a simple go or defer statement which passes the
1761 // struct to a thunk. The thunk does the real call.
1764 Thunk_statement::simplify_statement(Gogo* gogo, Block* block)
1766 if (this->classification() == STATEMENT_ERROR)
1768 if (this->call_->is_error_expression())
1771 Call_expression* ce = this->call_->call_expression();
1772 Function_type* fntype = ce->get_function_type();
1775 gcc_assert(saw_errors());
1776 this->set_is_error();
1779 if (this->is_simple(fntype))
1782 Expression* fn = ce->fn();
1783 Bound_method_expression* bound_method = fn->bound_method_expression();
1784 Interface_field_reference_expression* interface_method =
1785 fn->interface_field_reference_expression();
1786 const bool is_method = bound_method != NULL || interface_method != NULL;
1788 source_location location = this->location();
1790 std::string thunk_name = Gogo::thunk_name();
1793 this->build_thunk(gogo, thunk_name, fntype);
1795 // Generate code to call the thunk.
1797 // Get the values to store into the struct which is the single
1798 // argument to the thunk.
1800 Expression_list* vals = new Expression_list();
1801 if (fntype->is_builtin())
1803 else if (!is_method)
1804 vals->push_back(fn);
1805 else if (interface_method != NULL)
1806 vals->push_back(interface_method->expr());
1807 else if (bound_method != NULL)
1809 vals->push_back(bound_method->method());
1810 Expression* first_arg = bound_method->first_argument();
1812 // We always pass a pointer when calling a method.
1813 if (first_arg->type()->points_to() == NULL)
1814 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, location);
1816 // If we are calling a method which was inherited from an
1817 // embedded struct, and the method did not get a stub, then the
1818 // first type may be wrong.
1819 Type* fatype = bound_method->first_argument_type();
1822 if (fatype->points_to() == NULL)
1823 fatype = Type::make_pointer_type(fatype);
1824 Type* unsafe = Type::make_pointer_type(Type::make_void_type());
1825 first_arg = Expression::make_cast(unsafe, first_arg, location);
1826 first_arg = Expression::make_cast(fatype, first_arg, location);
1829 vals->push_back(first_arg);
1834 if (ce->args() != NULL)
1836 for (Expression_list::const_iterator p = ce->args()->begin();
1837 p != ce->args()->end();
1839 vals->push_back(*p);
1842 // Build the struct.
1843 Expression* constructor =
1844 Expression::make_struct_composite_literal(this->struct_type_, vals,
1847 // Allocate the initialized struct on the heap.
1848 constructor = Expression::make_heap_composite(constructor, location);
1850 // Look up the thunk.
1851 Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
1852 gcc_assert(named_thunk != NULL && named_thunk->is_function());
1855 Expression* func = Expression::make_func_reference(named_thunk, NULL,
1857 Expression_list* params = new Expression_list();
1858 params->push_back(constructor);
1859 Call_expression* call = Expression::make_call(func, params, false, location);
1861 // Build the simple go or defer statement.
1863 if (this->classification() == STATEMENT_GO)
1864 s = Statement::make_go_statement(call, location);
1865 else if (this->classification() == STATEMENT_DEFER)
1866 s = Statement::make_defer_statement(call, location);
1870 // The current block should end with the go statement.
1871 gcc_assert(block->statements()->size() >= 1);
1872 gcc_assert(block->statements()->back() == this);
1873 block->replace_statement(block->statements()->size() - 1, s);
1875 // We already ran the determine_types pass, so we need to run it now
1876 // for the new statement.
1877 s->determine_types();
1880 gogo->check_types_in_block(block);
1882 // Return true to tell the block not to keep looking at statements.
1886 // Set the name to use for thunk parameter N.
1889 Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
1891 snprintf(buf, buflen, "a%d", n);
1894 // Build a new struct type to hold the parameters for a complicated
1895 // thunk statement. FNTYPE is the type of the function call.
1898 Thunk_statement::build_struct(Function_type* fntype)
1900 source_location location = this->location();
1902 Struct_field_list* fields = new Struct_field_list();
1904 Call_expression* ce = this->call_->call_expression();
1905 Expression* fn = ce->fn();
1907 Interface_field_reference_expression* interface_method =
1908 fn->interface_field_reference_expression();
1909 if (interface_method != NULL)
1911 // If this thunk statement calls a method on an interface, we
1912 // pass the interface object to the thunk.
1913 Typed_identifier tid(Thunk_statement::thunk_field_fn,
1914 interface_method->expr()->type(),
1916 fields->push_back(Struct_field(tid));
1918 else if (!fntype->is_builtin())
1920 // The function to call.
1921 Typed_identifier tid(Go_statement::thunk_field_fn, fntype, location);
1922 fields->push_back(Struct_field(tid));
1924 else if (ce->is_recover_call())
1926 // The predeclared recover function has no argument. However,
1927 // we add an argument when building recover thunks. Handle that
1929 fields->push_back(Struct_field(Typed_identifier("can_recover",
1930 Type::lookup_bool_type(),
1934 if (fn->bound_method_expression() != NULL)
1936 gcc_assert(fntype->is_method());
1937 Type* rtype = fntype->receiver()->type();
1938 // We always pass the receiver as a pointer.
1939 if (rtype->points_to() == NULL)
1940 rtype = Type::make_pointer_type(rtype);
1941 Typed_identifier tid(Thunk_statement::thunk_field_receiver, rtype,
1943 fields->push_back(Struct_field(tid));
1946 const Expression_list* args = ce->args();
1950 for (Expression_list::const_iterator p = args->begin();
1955 this->thunk_field_param(i, buf, sizeof buf);
1956 fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
1961 return Type::make_struct_type(fields, location);
1964 // Build the thunk we are going to call. This is a brand new, albeit
1965 // artificial, function.
1968 Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
1969 Function_type* fntype)
1971 source_location location = this->location();
1973 Call_expression* ce = this->call_->call_expression();
1975 bool may_call_recover = false;
1976 if (this->classification() == STATEMENT_DEFER)
1978 Func_expression* fn = ce->fn()->func_expression();
1980 may_call_recover = true;
1983 const Named_object* no = fn->named_object();
1984 if (!no->is_function())
1985 may_call_recover = true;
1987 may_call_recover = no->func_value()->calls_recover();
1991 // Build the type of the thunk. The thunk takes a single parameter,
1992 // which is a pointer to the special structure we build.
1993 const char* const parameter_name = "__go_thunk_parameter";
1994 Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
1995 Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
1996 thunk_parameters->push_back(Typed_identifier(parameter_name,
1997 pointer_to_struct_type,
2000 Typed_identifier_list* thunk_results = NULL;
2001 if (may_call_recover)
2003 // When deferring a function which may call recover, add a
2004 // return value, to disable tail call optimizations which will
2005 // break the way we check whether recover is permitted.
2006 thunk_results = new Typed_identifier_list();
2007 thunk_results->push_back(Typed_identifier("", Type::lookup_bool_type(),
2011 Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
2015 // Start building the thunk.
2016 Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
2019 // For a defer statement, start with a call to
2020 // __go_set_defer_retaddr. */
2021 Label* retaddr_label = NULL;
2022 if (may_call_recover)
2024 retaddr_label = gogo->add_label_reference("retaddr");
2025 Expression* arg = Expression::make_label_addr(retaddr_label, location);
2026 Expression* call = Runtime::make_call(Runtime::SET_DEFER_RETADDR,
2029 // This is a hack to prevent the middle-end from deleting the
2031 gogo->start_block(location);
2032 gogo->add_statement(Statement::make_goto_statement(retaddr_label,
2034 Block* then_block = gogo->finish_block(location);
2035 then_block->determine_types();
2037 Statement* s = Statement::make_if_statement(call, then_block, NULL,
2039 s->determine_types();
2040 gogo->add_statement(s);
2043 // Get a reference to the parameter.
2044 Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
2045 gcc_assert(named_parameter != NULL && named_parameter->is_variable());
2047 // Build the call. Note that the field names are the same as the
2048 // ones used in build_struct.
2049 Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
2051 thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
2054 Bound_method_expression* bound_method = ce->fn()->bound_method_expression();
2055 Interface_field_reference_expression* interface_method =
2056 ce->fn()->interface_field_reference_expression();
2058 Expression* func_to_call;
2059 unsigned int next_index;
2060 if (!fntype->is_builtin())
2062 func_to_call = Expression::make_field_reference(thunk_parameter,
2068 gcc_assert(bound_method == NULL && interface_method == NULL);
2069 func_to_call = ce->fn();
2073 if (bound_method != NULL)
2075 Expression* r = Expression::make_field_reference(thunk_parameter, 1,
2077 // The main program passes in a function pointer from the
2078 // interface expression, so here we can make a bound method in
2080 func_to_call = Expression::make_bound_method(r, func_to_call,
2084 else if (interface_method != NULL)
2086 // The main program passes the interface object.
2087 const std::string& name(interface_method->name());
2088 func_to_call = Expression::make_interface_field_reference(func_to_call,
2093 Expression_list* call_params = new Expression_list();
2094 const Struct_field_list* fields = this->struct_type_->fields();
2095 Struct_field_list::const_iterator p = fields->begin();
2096 for (unsigned int i = 0; i < next_index; ++i)
2098 bool is_recover_call = ce->is_recover_call();
2099 Expression* recover_arg = NULL;
2100 for (; p != fields->end(); ++p, ++next_index)
2102 Expression* thunk_param = Expression::make_var_reference(named_parameter,
2104 thunk_param = Expression::make_unary(OPERATOR_MULT, thunk_param,
2106 Expression* param = Expression::make_field_reference(thunk_param,
2109 if (!is_recover_call)
2110 call_params->push_back(param);
2113 gcc_assert(call_params->empty());
2114 recover_arg = param;
2118 if (call_params->empty())
2124 Expression* call = Expression::make_call(func_to_call, call_params, false,
2126 // We need to lower in case this is a builtin function.
2127 call = call->lower(gogo, function, -1);
2128 Call_expression* call_ce = call->call_expression();
2129 if (call_ce != NULL && may_call_recover)
2130 call_ce->set_is_deferred();
2132 Statement* call_statement = Statement::make_statement(call);
2134 // We already ran the determine_types pass, so we need to run it
2135 // just for this statement now.
2136 call_statement->determine_types();
2139 call->check_types(gogo);
2141 if (call_ce != NULL && recover_arg != NULL)
2142 call_ce->set_recover_arg(recover_arg);
2144 gogo->add_statement(call_statement);
2146 // If this is a defer statement, the label comes immediately after
2148 if (may_call_recover)
2150 gogo->add_label_definition("retaddr", location);
2152 Expression_list* vals = new Expression_list();
2153 vals->push_back(Expression::make_boolean(false, location));
2154 gogo->add_statement(Statement::make_return_statement(vals, location));
2157 // That is all the thunk has to do.
2158 gogo->finish_function(location);
2161 // Get the function and argument trees.
2164 Thunk_statement::get_fn_and_arg(Translate_context* context, tree* pfn,
2167 if (this->call_->is_error_expression())
2169 *pfn = error_mark_node;
2170 *parg = error_mark_node;
2174 Call_expression* ce = this->call_->call_expression();
2176 Expression* fn = ce->fn();
2177 *pfn = fn->get_tree(context);
2179 const Expression_list* args = ce->args();
2180 if (args == NULL || args->empty())
2181 *parg = null_pointer_node;
2184 gcc_assert(args->size() == 1);
2185 *parg = args->front()->get_tree(context);
2189 // Class Go_statement.
2192 Go_statement::do_get_tree(Translate_context* context)
2196 this->get_fn_and_arg(context, &fn_tree, &arg_tree);
2198 static tree go_fndecl;
2200 tree fn_arg_type = NULL_TREE;
2201 if (go_fndecl == NULL_TREE)
2203 // Only build FN_ARG_TYPE if we need it.
2204 tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
2205 tree subfntype = build_function_type(ptr_type_node, subargtypes);
2206 fn_arg_type = build_pointer_type(subfntype);
2209 return Gogo::call_builtin(&go_fndecl,
2220 // Make a go statement.
2223 Statement::make_go_statement(Call_expression* call, source_location location)
2225 return new Go_statement(call, location);
2228 // Class Defer_statement.
2231 Defer_statement::do_get_tree(Translate_context* context)
2233 source_location loc = this->location();
2237 this->get_fn_and_arg(context, &fn_tree, &arg_tree);
2238 if (fn_tree == error_mark_node || arg_tree == error_mark_node)
2239 return error_mark_node;
2241 static tree defer_fndecl;
2243 tree fn_arg_type = NULL_TREE;
2244 if (defer_fndecl == NULL_TREE)
2246 // Only build FN_ARG_TYPE if we need it.
2247 tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
2248 tree subfntype = build_function_type(ptr_type_node, subargtypes);
2249 fn_arg_type = build_pointer_type(subfntype);
2252 tree defer_stack = context->function()->func_value()->defer_stack(loc);
2254 return Gogo::call_builtin(&defer_fndecl,
2267 // Make a defer statement.
2270 Statement::make_defer_statement(Call_expression* call,
2271 source_location location)
2273 return new Defer_statement(call, location);
2276 // Class Return_statement.
2278 // Traverse assignments. We treat each return value as a top level
2279 // RHS in an expression.
2282 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2284 Expression_list* vals = this->vals_;
2287 for (Expression_list::iterator p = vals->begin();
2290 tassign->value(&*p, true, true);
2295 // Lower a return statement. If we are returning a function call
2296 // which returns multiple values which match the current function,
2297 // split up the call's results. If the function has named result
2298 // variables, and the return statement lists explicit values, then
2299 // implement it by assigning the values to the result variables and
2300 // changing the statement to not list any values. This lets
2301 // panic/recover work correctly.
2304 Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing)
2306 if (this->is_lowered_)
2309 Expression_list* vals = this->vals_;
2311 this->is_lowered_ = true;
2313 source_location loc = this->location();
2315 size_t vals_count = vals == NULL ? 0 : vals->size();
2316 Function::Results* results = function->func_value()->result_variables();
2317 size_t results_count = results == NULL ? 0 : results->size();
2319 if (vals_count == 0)
2321 if (results_count > 0 && !function->func_value()->results_are_named())
2323 this->report_error(_("not enough arguments to return"));
2329 if (results_count == 0)
2331 this->report_error(_("return with value in function "
2332 "with no return type"));
2336 // If the current function has multiple return values, and we are
2337 // returning a single call expression, split up the call expression.
2338 if (results_count > 1
2339 && vals->size() == 1
2340 && vals->front()->call_expression() != NULL)
2342 Call_expression* call = vals->front()->call_expression();
2344 vals = new Expression_list;
2345 for (size_t i = 0; i < results_count; ++i)
2346 vals->push_back(Expression::make_call_result(call, i));
2347 vals_count = results_count;
2350 if (vals_count < results_count)
2352 this->report_error(_("not enough arguments to return"));
2356 if (vals_count > results_count)
2358 this->report_error(_("too many values in return statement"));
2362 Block* b = new Block(enclosing, loc);
2364 Expression_list* lhs = new Expression_list();
2365 Expression_list* rhs = new Expression_list();
2367 Expression_list::const_iterator pe = vals->begin();
2369 for (Function::Results::const_iterator pr = results->begin();
2370 pr != results->end();
2373 Named_object* rv = *pr;
2374 Expression* e = *pe;
2376 // Check types now so that we give a good error message. The
2377 // result type is known. We determine the expression type
2380 Type *rvtype = rv->result_var_value()->type();
2381 Type_context type_context(rvtype, false);
2382 e->determine_type(&type_context);
2385 if (Type::are_assignable(rvtype, e->type(), &reason))
2387 Expression* ve = Expression::make_var_reference(rv, e->location());
2394 error_at(e->location(), "incompatible type for return value %d", i);
2396 error_at(e->location(),
2397 "incompatible type for return value %d (%s)",
2401 gcc_assert(lhs->size() == rhs->size());
2405 else if (lhs->size() == 1)
2407 b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
2413 b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
2415 b->add_statement(this);
2419 return Statement::make_block_statement(b, loc);
2422 // Convert a return statement to the backend representation.
2425 Return_statement::do_get_tree(Translate_context* context)
2427 Function* function = context->function()->func_value();
2428 tree fndecl = function->get_decl();
2430 Function::Results* results = function->result_variables();
2431 std::vector<Bexpression*> retvals;
2432 if (results != NULL && !results->empty())
2434 retvals.reserve(results->size());
2435 for (Function::Results::const_iterator p = results->begin();
2436 p != results->end();
2439 tree rv = (*p)->get_tree(context->gogo(), context->function());
2440 retvals.push_back(tree_to_expr(rv));
2445 ret = context->backend()->return_statement(tree_to_function(fndecl),
2446 retvals, this->location());
2447 return stat_to_tree(ret);
2450 // Make a return statement.
2453 Statement::make_return_statement(Expression_list* vals,
2454 source_location location)
2456 return new Return_statement(vals, location);
2459 // A break or continue statement.
2461 class Bc_statement : public Statement
2464 Bc_statement(bool is_break, Unnamed_label* label, source_location location)
2465 : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
2466 label_(label), is_break_(is_break)
2471 { return this->is_break_; }
2475 do_traverse(Traverse*)
2476 { return TRAVERSE_CONTINUE; }
2479 do_may_fall_through() const
2483 do_get_tree(Translate_context* context)
2485 return stat_to_tree(this->label_->get_goto(context, this->location()));
2489 // The label that this branches to.
2490 Unnamed_label* label_;
2491 // True if this is "break", false if it is "continue".
2495 // Make a break statement.
2498 Statement::make_break_statement(Unnamed_label* label, source_location location)
2500 return new Bc_statement(true, label, location);
2503 // Make a continue statement.
2506 Statement::make_continue_statement(Unnamed_label* label,
2507 source_location location)
2509 return new Bc_statement(false, label, location);
2512 // A goto statement.
2514 class Goto_statement : public Statement
2517 Goto_statement(Label* label, source_location location)
2518 : Statement(STATEMENT_GOTO, location),
2524 do_traverse(Traverse*)
2525 { return TRAVERSE_CONTINUE; }
2528 do_check_types(Gogo*);
2531 do_may_fall_through() const
2535 do_get_tree(Translate_context*);
2541 // Check types for a label. There aren't any types per se, but we use
2542 // this to give an error if the label was never defined.
2545 Goto_statement::do_check_types(Gogo*)
2547 if (!this->label_->is_defined())
2549 error_at(this->location(), "reference to undefined label %qs",
2550 Gogo::message_name(this->label_->name()).c_str());
2551 this->set_is_error();
2555 // Return the tree for the goto statement.
2558 Goto_statement::do_get_tree(Translate_context* context)
2560 Blabel* blabel = this->label_->get_backend_label(context);
2561 Bstatement* statement = context->backend()->goto_statement(blabel,
2563 return stat_to_tree(statement);
2566 // Make a goto statement.
2569 Statement::make_goto_statement(Label* label, source_location location)
2571 return new Goto_statement(label, location);
2574 // A goto statement to an unnamed label.
2576 class Goto_unnamed_statement : public Statement
2579 Goto_unnamed_statement(Unnamed_label* label, source_location location)
2580 : Statement(STATEMENT_GOTO_UNNAMED, location),
2586 do_traverse(Traverse*)
2587 { return TRAVERSE_CONTINUE; }
2590 do_may_fall_through() const
2594 do_get_tree(Translate_context* context)
2596 return stat_to_tree(this->label_->get_goto(context, this->location()));
2600 Unnamed_label* label_;
2603 // Make a goto statement to an unnamed label.
2606 Statement::make_goto_unnamed_statement(Unnamed_label* label,
2607 source_location location)
2609 return new Goto_unnamed_statement(label, location);
2612 // Class Label_statement.
2617 Label_statement::do_traverse(Traverse*)
2619 return TRAVERSE_CONTINUE;
2622 // Return a tree defining this label.
2625 Label_statement::do_get_tree(Translate_context* context)
2627 Blabel* blabel = this->label_->get_backend_label(context);
2628 Bstatement* statement;
2629 statement = context->backend()->label_definition_statement(blabel);
2630 return stat_to_tree(statement);
2633 // Make a label statement.
2636 Statement::make_label_statement(Label* label, source_location location)
2638 return new Label_statement(label, location);
2641 // An unnamed label statement.
2643 class Unnamed_label_statement : public Statement
2646 Unnamed_label_statement(Unnamed_label* label)
2647 : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
2653 do_traverse(Traverse*)
2654 { return TRAVERSE_CONTINUE; }
2657 do_get_tree(Translate_context* context)
2658 { return stat_to_tree(this->label_->get_definition(context)); }
2662 Unnamed_label* label_;
2665 // Make an unnamed label statement.
2668 Statement::make_unnamed_label_statement(Unnamed_label* label)
2670 return new Unnamed_label_statement(label);
2675 class If_statement : public Statement
2678 If_statement(Expression* cond, Block* then_block, Block* else_block,
2679 source_location location)
2680 : Statement(STATEMENT_IF, location),
2681 cond_(cond), then_block_(then_block), else_block_(else_block)
2686 do_traverse(Traverse*);
2689 do_determine_types();
2692 do_check_types(Gogo*);
2695 do_may_fall_through() const;
2698 do_get_tree(Translate_context*);
2709 If_statement::do_traverse(Traverse* traverse)
2711 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT
2712 || this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
2713 return TRAVERSE_EXIT;
2714 if (this->else_block_ != NULL)
2716 if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
2717 return TRAVERSE_EXIT;
2719 return TRAVERSE_CONTINUE;
2723 If_statement::do_determine_types()
2725 Type_context context(Type::lookup_bool_type(), false);
2726 this->cond_->determine_type(&context);
2727 this->then_block_->determine_types();
2728 if (this->else_block_ != NULL)
2729 this->else_block_->determine_types();
2735 If_statement::do_check_types(Gogo*)
2737 Type* type = this->cond_->type();
2738 if (type->is_error())
2739 this->set_is_error();
2740 else if (!type->is_boolean_type())
2741 this->report_error(_("expected boolean expression"));
2744 // Whether the overall statement may fall through.
2747 If_statement::do_may_fall_through() const
2749 return (this->else_block_ == NULL
2750 || this->then_block_->may_fall_through()
2751 || this->else_block_->may_fall_through());
2757 If_statement::do_get_tree(Translate_context* context)
2759 gcc_assert(this->cond_->type()->is_boolean_type()
2760 || this->cond_->type()->is_error());
2761 tree cond_tree = this->cond_->get_tree(context);
2762 tree then_tree = this->then_block_->get_tree(context);
2763 tree else_tree = (this->else_block_ == NULL
2765 : this->else_block_->get_tree(context));
2767 Bexpression* cond_expr = tree_to_expr(cond_tree);
2768 Bstatement* then_stat = tree_to_stat(then_tree);
2769 Bstatement* else_stat = (else_tree == NULL_TREE
2771 : tree_to_stat(else_tree));
2773 Bstatement* ret = context->backend()->if_statement(cond_expr, then_stat,
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)
3051 statements = tree_to_stat(this->statements_->get_tree(context));
3053 Bstatement* break_stat;
3054 if (this->is_fallthrough_)
3057 break_stat = break_label->get_goto(context, this->location_);
3059 if (statements == NULL)
3061 else if (break_stat == NULL)
3065 std::vector<Bstatement*> list(2);
3066 list[0] = statements;
3067 list[1] = break_stat;
3068 return context->backend()->statement_list(list);
3072 // Class Case_clauses.
3077 Case_clauses::traverse(Traverse* traverse)
3079 for (Clauses::iterator p = this->clauses_.begin();
3080 p != this->clauses_.end();
3083 if (p->traverse(traverse) == TRAVERSE_EXIT)
3084 return TRAVERSE_EXIT;
3086 return TRAVERSE_CONTINUE;
3089 // Check whether all the case expressions are constant.
3092 Case_clauses::is_constant() const
3094 for (Clauses::const_iterator p = this->clauses_.begin();
3095 p != this->clauses_.end();
3097 if (!p->is_constant())
3102 // Lower case clauses for a nonconstant switch.
3105 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3106 Unnamed_label* break_label) const
3108 // The default case.
3109 const Case_clause* default_case = NULL;
3111 // The label for the fallthrough of the previous case.
3112 Unnamed_label* last_fallthrough_label = NULL;
3114 // The label for the start of the default case. This is used if the
3115 // case before the default case falls through.
3116 Unnamed_label* default_start_label = NULL;
3118 // The label for the end of the default case. This normally winds
3119 // up as BREAK_LABEL, but it will be different if the default case
3121 Unnamed_label* default_finish_label = NULL;
3123 for (Clauses::const_iterator p = this->clauses_.begin();
3124 p != this->clauses_.end();
3127 // The label to use for the start of the statements for this
3128 // case. This is NULL unless the previous case falls through.
3129 Unnamed_label* start_label = last_fallthrough_label;
3131 // The label to jump to after the end of the statements for this
3133 Unnamed_label* finish_label = break_label;
3135 last_fallthrough_label = NULL;
3136 if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3138 finish_label = new Unnamed_label(p->location());
3139 last_fallthrough_label = finish_label;
3142 if (!p->is_default())
3143 p->lower(b, val_temp, start_label, finish_label);
3146 // We have to move the default case to the end, so that we
3147 // only use it if all the other tests fail.
3149 default_start_label = start_label;
3150 default_finish_label = finish_label;
3154 if (default_case != NULL)
3155 default_case->lower(b, val_temp, default_start_label,
3156 default_finish_label);
3163 Case_clauses::determine_types(Type* type)
3165 for (Clauses::iterator p = this->clauses_.begin();
3166 p != this->clauses_.end();
3168 p->determine_types(type);
3171 // Check types. Returns false if there was an error.
3174 Case_clauses::check_types(Type* type)
3177 for (Clauses::iterator p = this->clauses_.begin();
3178 p != this->clauses_.end();
3181 if (!p->check_types(type))
3187 // Return true if these clauses may fall through to the statements
3188 // following the switch statement.
3191 Case_clauses::may_fall_through() const
3193 bool found_default = false;
3194 for (Clauses::const_iterator p = this->clauses_.begin();
3195 p != this->clauses_.end();
3198 if (p->may_fall_through() && !p->is_fallthrough())
3200 if (p->is_default())
3201 found_default = true;
3203 return !found_default;
3206 // Convert the cases to the backend representation. This sets
3207 // *ALL_CASES and *ALL_STATEMENTS.
3210 Case_clauses::get_backend(Translate_context* context,
3211 Unnamed_label* break_label,
3212 std::vector<std::vector<Bexpression*> >* all_cases,
3213 std::vector<Bstatement*>* all_statements) const
3215 Case_constants case_constants;
3217 size_t c = this->clauses_.size();
3218 all_cases->resize(c);
3219 all_statements->resize(c);
3222 for (Clauses::const_iterator p = this->clauses_.begin();
3223 p != this->clauses_.end();
3226 std::vector<Bexpression*> cases;
3227 Bstatement* stat = p->get_backend(context, break_label, &case_constants,
3229 (*all_cases)[i].swap(cases);
3230 (*all_statements)[i] = stat;
3234 // A constant switch statement. A Switch_statement is lowered to this
3235 // when all the cases are constants.
3237 class Constant_switch_statement : public Statement
3240 Constant_switch_statement(Expression* val, Case_clauses* clauses,
3241 Unnamed_label* break_label,
3242 source_location location)
3243 : Statement(STATEMENT_CONSTANT_SWITCH, location),
3244 val_(val), clauses_(clauses), break_label_(break_label)
3249 do_traverse(Traverse*);
3252 do_determine_types();
3255 do_check_types(Gogo*);
3258 do_may_fall_through() const;
3261 do_get_tree(Translate_context*);
3264 // The value to switch on.
3266 // The case clauses.
3267 Case_clauses* clauses_;
3268 // The break label, if needed.
3269 Unnamed_label* break_label_;
3275 Constant_switch_statement::do_traverse(Traverse* traverse)
3277 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3278 return TRAVERSE_EXIT;
3279 return this->clauses_->traverse(traverse);
3285 Constant_switch_statement::do_determine_types()
3287 this->val_->determine_type_no_context();
3288 this->clauses_->determine_types(this->val_->type());
3294 Constant_switch_statement::do_check_types(Gogo*)
3296 if (!this->clauses_->check_types(this->val_->type()))
3297 this->set_is_error();
3300 // Return whether this switch may fall through.
3303 Constant_switch_statement::do_may_fall_through() const
3305 if (this->clauses_ == NULL)
3308 // If we have a break label, then some case needed it. That implies
3309 // that the switch statement as a whole can fall through.
3310 if (this->break_label_ != NULL)
3313 return this->clauses_->may_fall_through();
3316 // Convert to GENERIC.
3319 Constant_switch_statement::do_get_tree(Translate_context* context)
3321 tree switch_val_tree = this->val_->get_tree(context);
3322 Bexpression* switch_val_expr = tree_to_expr(switch_val_tree);
3324 Unnamed_label* break_label = this->break_label_;
3325 if (break_label == NULL)
3326 break_label = new Unnamed_label(this->location());
3328 std::vector<std::vector<Bexpression*> > all_cases;
3329 std::vector<Bstatement*> all_statements;
3330 this->clauses_->get_backend(context, break_label, &all_cases,
3333 Bstatement* switch_statement;
3334 switch_statement = context->backend()->switch_statement(switch_val_expr,
3339 std::vector<Bstatement*> stats(2);
3340 stats[0] = switch_statement;
3341 stats[1] = break_label->get_definition(context);
3342 Bstatement* ret = context->backend()->statement_list(stats);
3343 return stat_to_tree(ret);
3346 // Class Switch_statement.
3351 Switch_statement::do_traverse(Traverse* traverse)
3353 if (this->val_ != NULL)
3355 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3356 return TRAVERSE_EXIT;
3358 return this->clauses_->traverse(traverse);
3361 // Lower a Switch_statement to a Constant_switch_statement or a series
3362 // of if statements.
3365 Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
3367 source_location loc = this->location();
3369 if (this->val_ != NULL
3370 && (this->val_->is_error_expression()
3371 || this->val_->type()->is_error()))
3372 return Statement::make_error_statement(loc);
3374 if (this->val_ != NULL
3375 && this->val_->type()->integer_type() != NULL
3376 && !this->clauses_->empty()
3377 && this->clauses_->is_constant())
3378 return new Constant_switch_statement(this->val_, this->clauses_,
3379 this->break_label_, loc);
3381 Block* b = new Block(enclosing, loc);
3383 if (this->clauses_->empty())
3385 Expression* val = this->val_;
3387 val = Expression::make_boolean(true, loc);
3388 return Statement::make_statement(val);
3391 Temporary_statement* val_temp;
3392 if (this->val_ == NULL)
3396 // var val_temp VAL_TYPE = VAL
3397 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3398 b->add_statement(val_temp);
3401 this->clauses_->lower(b, val_temp, this->break_label());
3403 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3404 b->add_statement(s);
3406 return Statement::make_block_statement(b, loc);
3409 // Return the break label for this switch statement, creating it if
3413 Switch_statement::break_label()
3415 if (this->break_label_ == NULL)
3416 this->break_label_ = new Unnamed_label(this->location());
3417 return this->break_label_;
3420 // Make a switch statement.
3423 Statement::make_switch_statement(Expression* val, source_location location)
3425 return new Switch_statement(val, location);
3428 // Class Type_case_clauses::Type_case_clause.
3433 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3435 if (!this->is_default_
3436 && ((traverse->traverse_mask()
3437 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3438 && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3439 return TRAVERSE_EXIT;
3440 if (this->statements_ != NULL)
3441 return this->statements_->traverse(traverse);
3442 return TRAVERSE_CONTINUE;
3445 // Lower one clause in a type switch. Add statements to the block B.
3446 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3447 // BREAK_LABEL is the label at the end of the type switch.
3448 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3452 Type_case_clauses::Type_case_clause::lower(Block* b,
3453 Temporary_statement* descriptor_temp,
3454 Unnamed_label* break_label,
3455 Unnamed_label** stmts_label) const
3457 source_location loc = this->location_;
3459 Unnamed_label* next_case_label = NULL;
3460 if (!this->is_default_)
3462 Type* type = this->type_;
3464 Expression* ref = Expression::make_temporary_reference(descriptor_temp,
3468 // The language permits case nil, which is of course a constant
3469 // rather than a type. It will appear here as an invalid
3471 if (type->is_nil_constant_as_type())
3472 cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3473 Expression::make_nil(loc),
3476 cond = Runtime::make_call((type->interface_type() == NULL
3477 ? Runtime::IFACETYPEEQ
3478 : Runtime::IFACEI2TP),
3480 Expression::make_type_descriptor(type, loc),
3483 Unnamed_label* dest;
3484 if (!this->is_fallthrough_)
3486 // if !COND { goto NEXT_CASE_LABEL }
3487 next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
3488 dest = next_case_label;
3489 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3493 // if COND { goto STMTS_LABEL }
3494 gcc_assert(stmts_label != NULL);
3495 if (*stmts_label == NULL)
3496 *stmts_label = new Unnamed_label(UNKNOWN_LOCATION);
3497 dest = *stmts_label;
3499 Block* then_block = new Block(b, loc);
3500 Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
3501 then_block->add_statement(s);
3502 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3503 b->add_statement(s);
3506 if (this->statements_ != NULL
3507 || (!this->is_fallthrough_
3508 && stmts_label != NULL
3509 && *stmts_label != NULL))
3511 gcc_assert(!this->is_fallthrough_);
3512 if (stmts_label != NULL && *stmts_label != NULL)
3514 gcc_assert(!this->is_default_);
3515 if (this->statements_ != NULL)
3516 (*stmts_label)->set_location(this->statements_->start_location());
3517 Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
3518 b->add_statement(s);
3519 *stmts_label = NULL;
3521 if (this->statements_ != NULL)
3522 b->add_statement(Statement::make_block_statement(this->statements_,
3526 if (this->is_fallthrough_)
3527 gcc_assert(next_case_label == NULL);
3530 source_location gloc = (this->statements_ == NULL
3532 : this->statements_->end_location());
3533 b->add_statement(Statement::make_goto_unnamed_statement(break_label,
3535 if (next_case_label != NULL)
3538 Statement::make_unnamed_label_statement(next_case_label);
3539 b->add_statement(s);
3544 // Class Type_case_clauses.
3549 Type_case_clauses::traverse(Traverse* traverse)
3551 for (Type_clauses::iterator p = this->clauses_.begin();
3552 p != this->clauses_.end();
3555 if (p->traverse(traverse) == TRAVERSE_EXIT)
3556 return TRAVERSE_EXIT;
3558 return TRAVERSE_CONTINUE;
3561 // Check for duplicate types.
3564 Type_case_clauses::check_duplicates() const
3566 typedef Unordered_set_hash(const Type*, Type_hash_identical,
3567 Type_identical) Types_seen;
3568 Types_seen types_seen;
3569 for (Type_clauses::const_iterator p = this->clauses_.begin();
3570 p != this->clauses_.end();
3573 Type* t = p->type();
3576 if (t->is_nil_constant_as_type())
3577 t = Type::make_nil_type();
3578 std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
3580 error_at(p->location(), "duplicate type in switch");
3584 // Lower the clauses in a type switch. Add statements to the block B.
3585 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3586 // BREAK_LABEL is the label at the end of the type switch.
3589 Type_case_clauses::lower(Block* b, Temporary_statement* descriptor_temp,
3590 Unnamed_label* break_label) const
3592 const Type_case_clause* default_case = NULL;
3594 Unnamed_label* stmts_label = NULL;
3595 for (Type_clauses::const_iterator p = this->clauses_.begin();
3596 p != this->clauses_.end();
3599 if (!p->is_default())
3600 p->lower(b, descriptor_temp, break_label, &stmts_label);
3603 // We are generating a series of tests, which means that we
3604 // need to move the default case to the end.
3608 gcc_assert(stmts_label == NULL);
3610 if (default_case != NULL)
3611 default_case->lower(b, descriptor_temp, break_label, NULL);
3614 // Class Type_switch_statement.
3619 Type_switch_statement::do_traverse(Traverse* traverse)
3621 if (this->var_ == NULL)
3623 if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
3624 return TRAVERSE_EXIT;
3626 if (this->clauses_ != NULL)
3627 return this->clauses_->traverse(traverse);
3628 return TRAVERSE_CONTINUE;
3631 // Lower a type switch statement to a series of if statements. The gc
3632 // compiler is able to generate a table in some cases. However, that
3633 // does not work for us because we may have type descriptors in
3634 // different shared libraries, so we can't compare them with simple
3635 // equality testing.
3638 Type_switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
3640 const source_location loc = this->location();
3642 if (this->clauses_ != NULL)
3643 this->clauses_->check_duplicates();
3645 Block* b = new Block(enclosing, loc);
3647 Type* val_type = (this->var_ != NULL
3648 ? this->var_->var_value()->type()
3649 : this->expr_->type());
3651 // var descriptor_temp DESCRIPTOR_TYPE
3652 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3653 Temporary_statement* descriptor_temp =
3654 Statement::make_temporary(descriptor_type, NULL, loc);
3655 b->add_statement(descriptor_temp);
3657 if (val_type->interface_type() == NULL)
3659 // Doing a type switch on a non-interface type. Should we issue
3660 // a warning for this case?
3661 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3664 if (val_type->is_nil_type())
3665 rhs = Expression::make_nil(loc);
3668 if (val_type->is_abstract())
3669 val_type = val_type->make_non_abstract_type();
3670 rhs = Expression::make_type_descriptor(val_type, loc);
3672 Statement* s = Statement::make_assignment(lhs, rhs, loc);
3673 b->add_statement(s);
3677 // descriptor_temp = ifacetype(val_temp)
3678 // FIXME: This should be inlined.
3679 bool is_empty = val_type->interface_type()->is_empty();
3681 if (this->var_ == NULL)
3684 ref = Expression::make_var_reference(this->var_, loc);
3685 Expression* call = Runtime::make_call((is_empty
3686 ? Runtime::EFACETYPE
3687 : Runtime::IFACETYPE),
3689 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3691 Statement* s = Statement::make_assignment(lhs, call, loc);
3692 b->add_statement(s);
3695 if (this->clauses_ != NULL)
3696 this->clauses_->lower(b, descriptor_temp, this->break_label());
3698 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3699 b->add_statement(s);
3701 return Statement::make_block_statement(b, loc);
3704 // Return the break label for this type switch statement, creating it
3708 Type_switch_statement::break_label()
3710 if (this->break_label_ == NULL)
3711 this->break_label_ = new Unnamed_label(this->location());
3712 return this->break_label_;
3715 // Make a type switch statement.
3717 Type_switch_statement*
3718 Statement::make_type_switch_statement(Named_object* var, Expression* expr,
3719 source_location location)
3721 return new Type_switch_statement(var, expr, location);
3724 // Class Send_statement.
3729 Send_statement::do_traverse(Traverse* traverse)
3731 if (this->traverse_expression(traverse, &this->channel_) == TRAVERSE_EXIT)
3732 return TRAVERSE_EXIT;
3733 return this->traverse_expression(traverse, &this->val_);
3739 Send_statement::do_determine_types()
3741 this->channel_->determine_type_no_context();
3742 Type* type = this->channel_->type();
3743 Type_context context;
3744 if (type->channel_type() != NULL)
3745 context.type = type->channel_type()->element_type();
3746 this->val_->determine_type(&context);
3752 Send_statement::do_check_types(Gogo*)
3754 Type* type = this->channel_->type();
3755 if (type->is_error())
3757 this->set_is_error();
3760 Channel_type* channel_type = type->channel_type();
3761 if (channel_type == NULL)
3763 error_at(this->location(), "left operand of %<<-%> must be channel");
3764 this->set_is_error();
3767 Type* element_type = channel_type->element_type();
3768 if (!Type::are_assignable(element_type, this->val_->type(), NULL))
3770 this->report_error(_("incompatible types in send"));
3773 if (!channel_type->may_send())
3775 this->report_error(_("invalid send on receive-only channel"));
3780 // Get a tree for a send statement.
3783 Send_statement::do_get_tree(Translate_context* context)
3785 tree channel = this->channel_->get_tree(context);
3786 tree val = this->val_->get_tree(context);
3787 if (channel == error_mark_node || val == error_mark_node)
3788 return error_mark_node;
3789 Channel_type* channel_type = this->channel_->type()->channel_type();
3790 val = Expression::convert_for_assignment(context,
3791 channel_type->element_type(),
3795 return Gogo::send_on_channel(channel, val, true, this->for_select_,
3799 // Make a send statement.
3802 Statement::make_send_statement(Expression* channel, Expression* val,
3803 source_location location)
3805 return new Send_statement(channel, val, location);
3808 // Class Select_clauses::Select_clause.
3813 Select_clauses::Select_clause::traverse(Traverse* traverse)
3815 if (!this->is_lowered_
3816 && (traverse->traverse_mask()
3817 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3819 if (this->channel_ != NULL)
3821 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
3822 return TRAVERSE_EXIT;
3824 if (this->val_ != NULL)
3826 if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
3827 return TRAVERSE_EXIT;
3829 if (this->closed_ != NULL)
3831 if (Expression::traverse(&this->closed_, traverse) == TRAVERSE_EXIT)
3832 return TRAVERSE_EXIT;
3835 if (this->statements_ != NULL)
3837 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3838 return TRAVERSE_EXIT;
3840 return TRAVERSE_CONTINUE;
3843 // Lowering. Here we pull out the channel and the send values, to
3844 // enforce the order of evaluation. We also add explicit send and
3845 // receive statements to the clauses.
3848 Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
3851 if (this->is_default_)
3853 gcc_assert(this->channel_ == NULL && this->val_ == NULL);
3854 this->is_lowered_ = true;
3858 source_location loc = this->location_;
3860 // Evaluate the channel before the select statement.
3861 Temporary_statement* channel_temp = Statement::make_temporary(NULL,
3864 b->add_statement(channel_temp);
3865 this->channel_ = Expression::make_temporary_reference(channel_temp, loc);
3867 // If this is a send clause, evaluate the value to send before the
3868 // select statement.
3869 Temporary_statement* val_temp = NULL;
3870 if (this->is_send_ && !this->val_->is_constant())
3872 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3873 b->add_statement(val_temp);
3876 // Add the send or receive before the rest of the statements if any.
3877 Block *init = new Block(b, loc);
3878 Expression* ref = Expression::make_temporary_reference(channel_temp, loc);
3882 if (val_temp == NULL)
3885 ref2 = Expression::make_temporary_reference(val_temp, loc);
3886 Send_statement* send = Statement::make_send_statement(ref, ref2, loc);
3887 send->set_for_select();
3888 init->add_statement(send);
3890 else if (this->closed_ != NULL && !this->closed_->is_sink_expression())
3892 gcc_assert(this->var_ == NULL && this->closedvar_ == NULL);
3893 if (this->val_ == NULL)
3894 this->val_ = Expression::make_sink(loc);
3895 Statement* s = Statement::make_tuple_receive_assignment(this->val_,
3898 init->add_statement(s);
3900 else if (this->closedvar_ != NULL)
3902 gcc_assert(this->val_ == NULL);
3904 if (this->var_ == NULL)
3905 val = Expression::make_sink(loc);
3907 val = Expression::make_var_reference(this->var_, loc);
3908 Expression* closed = Expression::make_var_reference(this->closedvar_,
3910 Statement* s = Statement::make_tuple_receive_assignment(val, closed, ref,
3912 // We have to put S in STATEMENTS_, because that is where the
3913 // variables are declared.
3914 gcc_assert(this->statements_ != NULL);
3915 this->statements_->add_statement_at_front(s);
3916 // We have to lower STATEMENTS_ again, to lower the tuple
3917 // receive assignment we just added.
3918 gogo->lower_block(function, this->statements_);
3922 Receive_expression* recv = Expression::make_receive(ref, loc);
3923 recv->set_for_select();
3924 if (this->val_ != NULL)
3926 gcc_assert(this->var_ == NULL);
3927 init->add_statement(Statement::make_assignment(this->val_, recv,
3930 else if (this->var_ != NULL)
3932 this->var_->var_value()->set_init(recv);
3933 this->var_->var_value()->clear_type_from_chan_element();
3937 init->add_statement(Statement::make_statement(recv));
3941 // Lower any statements we just created.
3942 gogo->lower_block(function, init);
3944 if (this->statements_ != NULL)
3945 init->add_statement(Statement::make_block_statement(this->statements_,
3948 this->statements_ = init;
3950 // Now all references should be handled through the statements, not
3952 this->is_lowered_ = true;
3960 Select_clauses::Select_clause::determine_types()
3962 gcc_assert(this->is_lowered_);
3963 if (this->statements_ != NULL)
3964 this->statements_->determine_types();
3967 // Whether this clause may fall through to the statement which follows
3968 // the overall select statement.
3971 Select_clauses::Select_clause::may_fall_through() const
3973 if (this->statements_ == NULL)
3975 return this->statements_->may_fall_through();
3978 // Return a tree for the statements to execute.
3981 Select_clauses::Select_clause::get_statements_tree(Translate_context* context)
3983 if (this->statements_ == NULL)
3985 return this->statements_->get_tree(context);
3988 // Class Select_clauses.
3993 Select_clauses::traverse(Traverse* traverse)
3995 for (Clauses::iterator p = this->clauses_.begin();
3996 p != this->clauses_.end();
3999 if (p->traverse(traverse) == TRAVERSE_EXIT)
4000 return TRAVERSE_EXIT;
4002 return TRAVERSE_CONTINUE;
4005 // Lowering. Here we pull out the channel and the send values, to
4006 // enforce the order of evaluation. We also add explicit send and
4007 // receive statements to the clauses.
4010 Select_clauses::lower(Gogo* gogo, Named_object* function, Block* b)
4012 for (Clauses::iterator p = this->clauses_.begin();
4013 p != this->clauses_.end();
4015 p->lower(gogo, function, b);
4021 Select_clauses::determine_types()
4023 for (Clauses::iterator p = this->clauses_.begin();
4024 p != this->clauses_.end();
4026 p->determine_types();
4029 // Return whether these select clauses fall through to the statement
4030 // following the overall select statement.
4033 Select_clauses::may_fall_through() const
4035 for (Clauses::const_iterator p = this->clauses_.begin();
4036 p != this->clauses_.end();
4038 if (p->may_fall_through())
4043 // Return a tree. We build a call to
4044 // size_t __go_select(size_t count, _Bool has_default,
4045 // channel* channels, _Bool* is_send)
4047 // There are COUNT entries in the CHANNELS and IS_SEND arrays. The
4048 // value in the IS_SEND array is true for send, false for receive.
4049 // __go_select returns an integer from 0 to COUNT, inclusive. A
4050 // return of 0 means that the default case should be run; this only
4051 // happens if HAS_DEFAULT is non-zero. Otherwise the number indicates
4054 // FIXME: This doesn't handle channels which send interface types
4055 // where the receiver has a static type which matches that interface.
4058 Select_clauses::get_tree(Translate_context* context,
4059 Unnamed_label *break_label,
4060 source_location location)
4062 size_t count = this->clauses_.size();
4063 VEC(constructor_elt, gc)* chan_init = VEC_alloc(constructor_elt, gc, count);
4064 VEC(constructor_elt, gc)* is_send_init = VEC_alloc(constructor_elt, gc,
4066 Select_clause* default_clause = NULL;
4067 tree final_stmt_list = NULL_TREE;
4068 tree channel_type_tree = NULL_TREE;
4071 for (Clauses::iterator p = this->clauses_.begin();
4072 p != this->clauses_.end();
4075 if (p->is_default())
4077 default_clause = &*p;
4082 if (p->channel()->type()->channel_type() == NULL)
4084 // We should have given an error in the send or receive
4085 // statement we created via lowering.
4086 gcc_assert(saw_errors());
4087 return error_mark_node;
4090 tree channel_tree = p->channel()->get_tree(context);
4091 if (channel_tree == error_mark_node)
4092 return error_mark_node;
4093 channel_type_tree = TREE_TYPE(channel_tree);
4095 constructor_elt* elt = VEC_quick_push(constructor_elt, chan_init, NULL);
4096 elt->index = build_int_cstu(sizetype, i);
4097 elt->value = channel_tree;
4099 elt = VEC_quick_push(constructor_elt, is_send_init, NULL);
4100 elt->index = build_int_cstu(sizetype, i);
4101 elt->value = p->is_send() ? boolean_true_node : boolean_false_node;
4105 gcc_assert(i == count);
4107 if (i == 0 && default_clause != NULL)
4109 // There is only a default clause.
4110 gcc_assert(final_stmt_list == NULL_TREE);
4111 tree stmt_list = NULL_TREE;
4112 append_to_statement_list(default_clause->get_statements_tree(context),
4114 Bstatement* ldef = break_label->get_definition(context);
4115 append_to_statement_list(stat_to_tree(ldef), &stmt_list);
4119 tree pointer_chan_type_tree = (channel_type_tree == NULL_TREE
4121 : build_pointer_type(channel_type_tree));
4123 tree pointer_boolean_type_tree = build_pointer_type(boolean_type_node);
4128 chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4130 is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4135 tree index_type_tree = build_index_type(size_int(count - 1));
4136 tree chan_array_type_tree = build_array_type(channel_type_tree,
4138 tree chan_constructor = build_constructor(chan_array_type_tree,
4140 tree chan_var = create_tmp_var(chan_array_type_tree, "CHAN");
4141 DECL_IGNORED_P(chan_var) = 0;
4142 DECL_INITIAL(chan_var) = chan_constructor;
4143 DECL_SOURCE_LOCATION(chan_var) = location;
4144 TREE_ADDRESSABLE(chan_var) = 1;
4145 tree decl_expr = build1(DECL_EXPR, void_type_node, chan_var);
4146 SET_EXPR_LOCATION(decl_expr, location);
4147 append_to_statement_list(decl_expr, &final_stmt_list);
4149 tree is_send_array_type_tree = build_array_type(boolean_type_node,
4151 tree is_send_constructor = build_constructor(is_send_array_type_tree,
4153 tree is_send_var = create_tmp_var(is_send_array_type_tree, "ISSEND");
4154 DECL_IGNORED_P(is_send_var) = 0;
4155 DECL_INITIAL(is_send_var) = is_send_constructor;
4156 DECL_SOURCE_LOCATION(is_send_var) = location;
4157 TREE_ADDRESSABLE(is_send_var) = 1;
4158 decl_expr = build1(DECL_EXPR, void_type_node, is_send_var);
4159 SET_EXPR_LOCATION(decl_expr, location);
4160 append_to_statement_list(decl_expr, &final_stmt_list);
4162 chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4163 build_fold_addr_expr_loc(location,
4165 is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4166 build_fold_addr_expr_loc(location,
4170 static tree select_fndecl;
4171 tree call = Gogo::call_builtin(&select_fndecl,
4179 (default_clause == NULL
4180 ? boolean_false_node
4181 : boolean_true_node),
4182 pointer_chan_type_tree,
4184 pointer_boolean_type_tree,
4186 if (call == error_mark_node)
4187 return error_mark_node;
4189 tree stmt_list = NULL_TREE;
4191 if (default_clause != NULL)
4192 this->add_clause_tree(context, 0, default_clause, break_label, &stmt_list);
4195 for (Clauses::iterator p = this->clauses_.begin();
4196 p != this->clauses_.end();
4199 if (!p->is_default())
4201 this->add_clause_tree(context, i, &*p, break_label, &stmt_list);
4206 Bstatement* ldef = break_label->get_definition(context);
4207 append_to_statement_list(stat_to_tree(ldef), &stmt_list);
4209 tree switch_stmt = build3(SWITCH_EXPR, sizetype, call, stmt_list, NULL_TREE);
4210 SET_EXPR_LOCATION(switch_stmt, location);
4211 append_to_statement_list(switch_stmt, &final_stmt_list);
4213 return final_stmt_list;
4216 // Add the tree for CLAUSE to STMT_LIST.
4219 Select_clauses::add_clause_tree(Translate_context* context, int case_index,
4220 Select_clause* clause,
4221 Unnamed_label* bottom_label, tree* stmt_list)
4223 tree label = create_artificial_label(clause->location());
4224 append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
4225 build_int_cst(sizetype, case_index),
4228 append_to_statement_list(clause->get_statements_tree(context), stmt_list);
4229 source_location gloc = (clause->statements() == NULL
4230 ? clause->location()
4231 : clause->statements()->end_location());
4232 Bstatement* g = bottom_label->get_goto(context, gloc);
4233 append_to_statement_list(stat_to_tree(g), stmt_list);
4236 // Class Select_statement.
4238 // Return the break label for this switch statement, creating it if
4242 Select_statement::break_label()
4244 if (this->break_label_ == NULL)
4245 this->break_label_ = new Unnamed_label(this->location());
4246 return this->break_label_;
4249 // Lower a select statement. This will still return a select
4250 // statement, but it will be modified to implement the order of
4251 // evaluation rules, and to include the send and receive statements as
4252 // explicit statements in the clauses.
4255 Select_statement::do_lower(Gogo* gogo, Named_object* function,
4258 if (this->is_lowered_)
4260 Block* b = new Block(enclosing, this->location());
4261 this->clauses_->lower(gogo, function, b);
4262 this->is_lowered_ = true;
4263 b->add_statement(this);
4264 return Statement::make_block_statement(b, this->location());
4267 // Return the tree for a select statement.
4270 Select_statement::do_get_tree(Translate_context* context)
4272 return this->clauses_->get_tree(context, this->break_label(),
4276 // Make a select statement.
4279 Statement::make_select_statement(source_location location)
4281 return new Select_statement(location);
4284 // Class For_statement.
4289 For_statement::do_traverse(Traverse* traverse)
4291 if (this->init_ != NULL)
4293 if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
4294 return TRAVERSE_EXIT;
4296 if (this->cond_ != NULL)
4298 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
4299 return TRAVERSE_EXIT;
4301 if (this->post_ != NULL)
4303 if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
4304 return TRAVERSE_EXIT;
4306 return this->statements_->traverse(traverse);
4309 // Lower a For_statement into if statements and gotos. Getting rid of
4310 // complex statements make it easier to handle garbage collection.
4313 For_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
4316 source_location loc = this->location();
4318 Block* b = new Block(enclosing, this->location());
4319 if (this->init_ != NULL)
4321 s = Statement::make_block_statement(this->init_,
4322 this->init_->start_location());
4323 b->add_statement(s);
4326 Unnamed_label* entry = NULL;
4327 if (this->cond_ != NULL)
4329 entry = new Unnamed_label(this->location());
4330 b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
4333 Unnamed_label* top = new Unnamed_label(this->location());
4334 b->add_statement(Statement::make_unnamed_label_statement(top));
4336 s = Statement::make_block_statement(this->statements_,
4337 this->statements_->start_location());
4338 b->add_statement(s);
4340 source_location end_loc = this->statements_->end_location();
4342 Unnamed_label* cont = this->continue_label_;
4344 b->add_statement(Statement::make_unnamed_label_statement(cont));
4346 if (this->post_ != NULL)
4348 s = Statement::make_block_statement(this->post_,
4349 this->post_->start_location());
4350 b->add_statement(s);
4351 end_loc = this->post_->end_location();
4354 if (this->cond_ == NULL)
4355 b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
4358 b->add_statement(Statement::make_unnamed_label_statement(entry));
4360 source_location cond_loc = this->cond_->location();
4361 Block* then_block = new Block(b, cond_loc);
4362 s = Statement::make_goto_unnamed_statement(top, cond_loc);
4363 then_block->add_statement(s);
4365 s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
4366 b->add_statement(s);
4369 Unnamed_label* brk = this->break_label_;
4371 b->add_statement(Statement::make_unnamed_label_statement(brk));
4373 b->set_end_location(end_loc);
4375 return Statement::make_block_statement(b, loc);
4378 // Return the break label, creating it if necessary.
4381 For_statement::break_label()
4383 if (this->break_label_ == NULL)
4384 this->break_label_ = new Unnamed_label(this->location());
4385 return this->break_label_;
4388 // Return the continue LABEL_EXPR.
4391 For_statement::continue_label()
4393 if (this->continue_label_ == NULL)
4394 this->continue_label_ = new Unnamed_label(this->location());
4395 return this->continue_label_;
4398 // Set the break and continue labels a for statement. This is used
4399 // when lowering a for range statement.
4402 For_statement::set_break_continue_labels(Unnamed_label* break_label,
4403 Unnamed_label* continue_label)
4405 gcc_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
4406 this->break_label_ = break_label;
4407 this->continue_label_ = continue_label;
4410 // Make a for statement.
4413 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
4414 source_location location)
4416 return new For_statement(init, cond, post, location);
4419 // Class For_range_statement.
4424 For_range_statement::do_traverse(Traverse* traverse)
4426 if (this->traverse_expression(traverse, &this->index_var_) == TRAVERSE_EXIT)
4427 return TRAVERSE_EXIT;
4428 if (this->value_var_ != NULL)
4430 if (this->traverse_expression(traverse, &this->value_var_)
4432 return TRAVERSE_EXIT;
4434 if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
4435 return TRAVERSE_EXIT;
4436 return this->statements_->traverse(traverse);
4439 // Lower a for range statement. For simplicity we lower this into a
4440 // for statement, which will then be lowered in turn to goto
4444 For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing)
4446 Type* range_type = this->range_->type();
4447 if (range_type->points_to() != NULL
4448 && range_type->points_to()->array_type() != NULL
4449 && !range_type->points_to()->is_open_array_type())
4450 range_type = range_type->points_to();
4453 Type* value_type = NULL;
4454 if (range_type->array_type() != NULL)
4456 index_type = Type::lookup_integer_type("int");
4457 value_type = range_type->array_type()->element_type();
4459 else if (range_type->is_string_type())
4461 index_type = Type::lookup_integer_type("int");
4462 value_type = index_type;
4464 else if (range_type->map_type() != NULL)
4466 index_type = range_type->map_type()->key_type();
4467 value_type = range_type->map_type()->val_type();
4469 else if (range_type->channel_type() != NULL)
4471 index_type = range_type->channel_type()->element_type();
4472 if (this->value_var_ != NULL)
4474 if (!this->value_var_->type()->is_error())
4475 this->report_error(_("too many variables for range clause "
4477 return Statement::make_error_statement(this->location());
4482 this->report_error(_("range clause must have "
4483 "array, slice, setring, map, or channel type"));
4484 return Statement::make_error_statement(this->location());
4487 source_location loc = this->location();
4488 Block* temp_block = new Block(enclosing, loc);
4490 Named_object* range_object = NULL;
4491 Temporary_statement* range_temp = NULL;
4492 Var_expression* ve = this->range_->var_expression();
4494 range_object = ve->named_object();
4497 range_temp = Statement::make_temporary(NULL, this->range_, loc);
4498 temp_block->add_statement(range_temp);
4501 Temporary_statement* index_temp = Statement::make_temporary(index_type,
4503 temp_block->add_statement(index_temp);
4505 Temporary_statement* value_temp = NULL;
4506 if (this->value_var_ != NULL)
4508 value_temp = Statement::make_temporary(value_type, NULL, loc);
4509 temp_block->add_statement(value_temp);
4512 Block* body = new Block(temp_block, loc);
4519 // Arrange to do a loop appropriate for the type. We will produce
4520 // for INIT ; COND ; POST {
4522 // INDEX = INDEX_TEMP
4523 // VALUE = VALUE_TEMP // If there is a value
4524 // original statements
4527 if (range_type->array_type() != NULL)
4528 this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
4529 index_temp, value_temp, &init, &cond, &iter_init,
4531 else if (range_type->is_string_type())
4532 this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
4533 index_temp, value_temp, &init, &cond, &iter_init,
4535 else if (range_type->map_type() != NULL)
4536 this->lower_range_map(gogo, temp_block, body, range_object, range_temp,
4537 index_temp, value_temp, &init, &cond, &iter_init,
4539 else if (range_type->channel_type() != NULL)
4540 this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
4541 index_temp, value_temp, &init, &cond, &iter_init,
4546 if (iter_init != NULL)
4547 body->add_statement(Statement::make_block_statement(iter_init, loc));
4550 Expression* index_ref = Expression::make_temporary_reference(index_temp, loc);
4551 if (this->value_var_ == NULL)
4553 assign = Statement::make_assignment(this->index_var_, index_ref, loc);
4557 Expression_list* lhs = new Expression_list();
4558 lhs->push_back(this->index_var_);
4559 lhs->push_back(this->value_var_);
4561 Expression_list* rhs = new Expression_list();
4562 rhs->push_back(index_ref);
4563 rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4565 assign = Statement::make_tuple_assignment(lhs, rhs, loc);
4567 body->add_statement(assign);
4569 body->add_statement(Statement::make_block_statement(this->statements_, loc));
4571 body->set_end_location(this->statements_->end_location());
4573 For_statement* loop = Statement::make_for_statement(init, cond, post,
4575 loop->add_statements(body);
4576 loop->set_break_continue_labels(this->break_label_, this->continue_label_);
4578 temp_block->add_statement(loop);
4580 return Statement::make_block_statement(temp_block, loc);
4583 // Return a reference to the range, which may be in RANGE_OBJECT or in
4587 For_range_statement::make_range_ref(Named_object* range_object,
4588 Temporary_statement* range_temp,
4589 source_location loc)
4591 if (range_object != NULL)
4592 return Expression::make_var_reference(range_object, loc);
4594 return Expression::make_temporary_reference(range_temp, loc);
4597 // Return a call to the predeclared function FUNCNAME passing a
4598 // reference to the temporary variable ARG.
4601 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
4603 source_location loc)
4605 Named_object* no = gogo->lookup_global(funcname);
4606 gcc_assert(no != NULL && no->is_function_declaration());
4607 Expression* func = Expression::make_func_reference(no, NULL, loc);
4608 Expression_list* params = new Expression_list();
4609 params->push_back(arg);
4610 return Expression::make_call(func, params, false, loc);
4613 // Lower a for range over an array or slice.
4616 For_range_statement::lower_range_array(Gogo* gogo,
4619 Named_object* range_object,
4620 Temporary_statement* range_temp,
4621 Temporary_statement* index_temp,
4622 Temporary_statement* value_temp,
4628 source_location loc = this->location();
4630 // The loop we generate:
4631 // len_temp := len(range)
4632 // for index_temp = 0; index_temp < len_temp; index_temp++ {
4633 // value_temp = range[index_temp]
4634 // index = index_temp
4635 // value = value_temp
4641 // len_temp = len(range)
4644 Block* init = new Block(enclosing, loc);
4646 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
4647 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
4648 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
4650 init->add_statement(len_temp);
4653 mpz_init_set_ui(zval, 0UL);
4654 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4657 ref = Expression::make_temporary_reference(index_temp, loc);
4658 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4659 init->add_statement(s);
4664 // index_temp < len_temp
4666 ref = Expression::make_temporary_reference(index_temp, loc);
4667 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
4668 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
4672 // Set *PITER_INIT to
4673 // value_temp = range[index_temp]
4675 Block* iter_init = NULL;
4676 if (value_temp != NULL)
4678 iter_init = new Block(body_block, loc);
4680 ref = this->make_range_ref(range_object, range_temp, loc);
4681 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
4682 Expression* index = Expression::make_index(ref, ref2, NULL, loc);
4684 ref = Expression::make_temporary_reference(value_temp, loc);
4685 s = Statement::make_assignment(ref, index, loc);
4687 iter_init->add_statement(s);
4689 *piter_init = iter_init;
4694 Block* post = new Block(enclosing, loc);
4695 ref = Expression::make_temporary_reference(index_temp, loc);
4696 s = Statement::make_inc_statement(ref);
4697 post->add_statement(s);
4701 // Lower a for range over a string.
4704 For_range_statement::lower_range_string(Gogo*,
4707 Named_object* range_object,
4708 Temporary_statement* range_temp,
4709 Temporary_statement* index_temp,
4710 Temporary_statement* value_temp,
4716 source_location loc = this->location();
4718 // The loop we generate:
4719 // var next_index_temp int
4720 // for index_temp = 0; ; index_temp = next_index_temp {
4721 // next_index_temp, value_temp = stringiter2(range, index_temp)
4722 // if next_index_temp == 0 {
4725 // index = index_temp
4726 // value = value_temp
4731 // var next_index_temp int
4734 Block* init = new Block(enclosing, loc);
4736 Temporary_statement* next_index_temp =
4737 Statement::make_temporary(index_temp->type(), NULL, loc);
4738 init->add_statement(next_index_temp);
4741 mpz_init_set_ui(zval, 0UL);
4742 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4744 Expression* ref = Expression::make_temporary_reference(index_temp, loc);
4745 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4747 init->add_statement(s);
4750 // The loop has no condition.
4754 // Set *PITER_INIT to
4755 // next_index_temp = runtime.stringiter(range, index_temp)
4757 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
4759 // if next_index_temp == 0 {
4763 Block* iter_init = new Block(body_block, loc);
4765 Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
4766 Expression* p2 = Expression::make_temporary_reference(index_temp, loc);
4767 Call_expression* call = Runtime::make_call((value_temp == NULL
4768 ? Runtime::STRINGITER
4769 : Runtime::STRINGITER2),
4772 if (value_temp == NULL)
4774 ref = Expression::make_temporary_reference(next_index_temp, loc);
4775 s = Statement::make_assignment(ref, call, loc);
4779 Expression_list* lhs = new Expression_list();
4780 lhs->push_back(Expression::make_temporary_reference(next_index_temp,
4782 lhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4784 Expression_list* rhs = new Expression_list();
4785 rhs->push_back(Expression::make_call_result(call, 0));
4786 rhs->push_back(Expression::make_call_result(call, 1));
4788 s = Statement::make_tuple_assignment(lhs, rhs, loc);
4790 iter_init->add_statement(s);
4792 ref = Expression::make_temporary_reference(next_index_temp, loc);
4793 zexpr = Expression::make_integer(&zval, NULL, loc);
4795 Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
4797 Block* then_block = new Block(iter_init, loc);
4798 s = Statement::make_break_statement(this->break_label(), loc);
4799 then_block->add_statement(s);
4801 s = Statement::make_if_statement(equals, then_block, NULL, loc);
4802 iter_init->add_statement(s);
4804 *piter_init = iter_init;
4807 // index_temp = next_index_temp
4809 Block* post = new Block(enclosing, loc);
4811 Expression* lhs = Expression::make_temporary_reference(index_temp, loc);
4812 Expression* rhs = Expression::make_temporary_reference(next_index_temp, loc);
4813 s = Statement::make_assignment(lhs, rhs, loc);
4815 post->add_statement(s);
4819 // Lower a for range over a map.
4822 For_range_statement::lower_range_map(Gogo*,
4825 Named_object* range_object,
4826 Temporary_statement* range_temp,
4827 Temporary_statement* index_temp,
4828 Temporary_statement* value_temp,
4834 source_location loc = this->location();
4836 // The runtime uses a struct to handle ranges over a map. The
4837 // struct is four pointers long. The first pointer is NULL when we
4838 // have completed the iteration.
4840 // The loop we generate:
4841 // var hiter map_iteration_struct
4842 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
4843 // mapiter2(hiter, &index_temp, &value_temp)
4844 // index = index_temp
4845 // value = value_temp
4850 // var hiter map_iteration_struct
4851 // runtime.mapiterinit(range, &hiter)
4853 Block* init = new Block(enclosing, loc);
4855 Type* map_iteration_type = Runtime::map_iteration_type();
4856 Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
4858 init->add_statement(hiter);
4860 Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
4861 Expression* ref = Expression::make_temporary_reference(hiter, loc);
4862 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
4863 Expression* call = Runtime::make_call(Runtime::MAPITERINIT, loc, 2, p1, p2);
4864 init->add_statement(Statement::make_statement(call));
4871 ref = Expression::make_temporary_reference(hiter, loc);
4874 mpz_init_set_ui(zval, 0UL);
4875 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4878 Expression* index = Expression::make_index(ref, zexpr, NULL, loc);
4880 Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
4881 Expression::make_nil(loc),
4886 // Set *PITER_INIT to
4887 // mapiter1(hiter, &index_temp)
4889 // mapiter2(hiter, &index_temp, &value_temp)
4891 Block* iter_init = new Block(body_block, loc);
4893 ref = Expression::make_temporary_reference(hiter, loc);
4894 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
4895 ref = Expression::make_temporary_reference(index_temp, loc);
4896 p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
4897 if (value_temp == NULL)
4898 call = Runtime::make_call(Runtime::MAPITER1, loc, 2, p1, p2);
4901 ref = Expression::make_temporary_reference(value_temp, loc);
4902 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
4903 call = Runtime::make_call(Runtime::MAPITER2, loc, 3, p1, p2, p3);
4905 iter_init->add_statement(Statement::make_statement(call));
4907 *piter_init = iter_init;
4910 // mapiternext(&hiter)
4912 Block* post = new Block(enclosing, loc);
4914 ref = Expression::make_temporary_reference(hiter, loc);
4915 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
4916 call = Runtime::make_call(Runtime::MAPITERNEXT, loc, 1, p1);
4917 post->add_statement(Statement::make_statement(call));
4922 // Lower a for range over a channel.
4925 For_range_statement::lower_range_channel(Gogo*,
4928 Named_object* range_object,
4929 Temporary_statement* range_temp,
4930 Temporary_statement* index_temp,
4931 Temporary_statement* value_temp,
4937 gcc_assert(value_temp == NULL);
4939 source_location loc = this->location();
4941 // The loop we generate:
4943 // index_temp, ok_temp = <-range
4947 // index = index_temp
4951 // We have no initialization code, no condition, and no post code.
4957 // Set *PITER_INIT to
4958 // index_temp, ok_temp = <-range
4963 Block* iter_init = new Block(body_block, loc);
4965 Temporary_statement* ok_temp =
4966 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
4967 iter_init->add_statement(ok_temp);
4969 Expression* cref = this->make_range_ref(range_object, range_temp, loc);
4970 Expression* iref = Expression::make_temporary_reference(index_temp, loc);
4971 Expression* oref = Expression::make_temporary_reference(ok_temp, loc);
4972 Statement* s = Statement::make_tuple_receive_assignment(iref, oref, cref,
4974 iter_init->add_statement(s);
4976 Block* then_block = new Block(iter_init, loc);
4977 s = Statement::make_break_statement(this->break_label(), loc);
4978 then_block->add_statement(s);
4980 oref = Expression::make_temporary_reference(ok_temp, loc);
4981 Expression* cond = Expression::make_unary(OPERATOR_NOT, oref, loc);
4982 s = Statement::make_if_statement(cond, then_block, NULL, loc);
4983 iter_init->add_statement(s);
4985 *piter_init = iter_init;
4988 // Return the break LABEL_EXPR.
4991 For_range_statement::break_label()
4993 if (this->break_label_ == NULL)
4994 this->break_label_ = new Unnamed_label(this->location());
4995 return this->break_label_;
4998 // Return the continue LABEL_EXPR.
5001 For_range_statement::continue_label()
5003 if (this->continue_label_ == NULL)
5004 this->continue_label_ = new Unnamed_label(this->location());
5005 return this->continue_label_;
5008 // Make a for statement with a range clause.
5010 For_range_statement*
5011 Statement::make_for_range_statement(Expression* index_var,
5012 Expression* value_var,
5014 source_location location)
5016 return new For_range_statement(index_var, value_var, range, location);