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_backend(Translate_context* context)
157 if (this->classification_ == STATEMENT_ERROR)
158 return context->backend()->error_statement();
159 return this->do_get_backend(context);
162 // Build tree nodes and set locations.
165 Statement::build_stmt_1(int tree_code_value, tree node)
167 tree ret = build1(static_cast<tree_code>(tree_code_value),
168 void_type_node, node);
169 SET_EXPR_LOCATION(ret, this->location_);
173 // Note that this statement is erroneous. This is called by children
174 // when they discover an error.
177 Statement::set_is_error()
179 this->classification_ = STATEMENT_ERROR;
182 // For children to call to report an error conveniently.
185 Statement::report_error(const char* msg)
187 error_at(this->location_, "%s", msg);
188 this->set_is_error();
191 // An error statement, used to avoid crashing after we report an
194 class Error_statement : public Statement
197 Error_statement(source_location location)
198 : Statement(STATEMENT_ERROR, location)
203 do_traverse(Traverse*)
204 { return TRAVERSE_CONTINUE; }
207 do_get_backend(Translate_context*)
208 { gcc_unreachable(); }
211 // Make an error statement.
214 Statement::make_error_statement(source_location location)
216 return new Error_statement(location);
219 // Class Variable_declaration_statement.
221 Variable_declaration_statement::Variable_declaration_statement(
223 : Statement(STATEMENT_VARIABLE_DECLARATION, var->var_value()->location()),
228 // We don't actually traverse the variable here; it was traversed
229 // while traversing the Block.
232 Variable_declaration_statement::do_traverse(Traverse*)
234 return TRAVERSE_CONTINUE;
237 // Traverse the assignments in a variable declaration. Note that this
238 // traversal is different from the usual traversal.
241 Variable_declaration_statement::do_traverse_assignments(
242 Traverse_assignments* tassign)
244 tassign->initialize_variable(this->var_);
248 // Return the tree for a variable declaration.
251 Variable_declaration_statement::do_get_backend(Translate_context* context)
253 Variable* var = this->var_->var_value();
254 Bvariable* bvar = this->var_->get_backend_variable(context->gogo(),
255 context->function());
256 tree init = var->get_init_tree(context->gogo(), context->function());
257 Bexpression* binit = init == NULL_TREE ? NULL : tree_to_expr(init);
258 if (!var->is_in_heap())
260 gcc_assert(binit != NULL);
261 return context->backend()->init_statement(bvar, binit);
265 // Something takes the address of this variable, so the value is
266 // stored in the heap. Initialize it to newly allocated memory
267 // space, and assign the initial value to the new space.
268 source_location loc = this->location();
269 tree decl = var_to_tree(bvar);
270 tree decl_type = TREE_TYPE(decl);
271 gcc_assert(POINTER_TYPE_P(decl_type));
272 tree size = TYPE_SIZE_UNIT(TREE_TYPE(decl_type));
273 tree space = context->gogo()->allocate_memory(var->type(), size, loc);
275 space = save_expr(space);
276 space = fold_convert_loc(loc, decl_type, space);
277 Bstatement* s1 = context->backend()->init_statement(bvar,
278 tree_to_expr(space));
283 tree indir = build_fold_indirect_ref_loc(loc, space);
284 Bexpression* bindir = tree_to_expr(indir);
285 Bstatement* s2 = context->backend()->assignment_statement(bindir,
288 return context->backend()->compound_statement(s1, s2);
293 // Make a variable declaration.
296 Statement::make_variable_declaration(Named_object* var)
298 return new Variable_declaration_statement(var);
301 // Class Temporary_statement.
303 // Return the type of the temporary variable.
306 Temporary_statement::type() const
308 return this->type_ != NULL ? this->type_ : this->init_->type();
314 Temporary_statement::do_traverse(Traverse* traverse)
316 if (this->type_ != NULL
317 && this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
318 return TRAVERSE_EXIT;
319 if (this->init_ == NULL)
320 return TRAVERSE_CONTINUE;
322 return this->traverse_expression(traverse, &this->init_);
325 // Traverse assignments.
328 Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
330 if (this->init_ == NULL)
332 tassign->value(&this->init_, true, true);
339 Temporary_statement::do_determine_types()
341 if (this->type_ != NULL && this->type_->is_abstract())
342 this->type_ = this->type_->make_non_abstract_type();
344 if (this->init_ != NULL)
346 if (this->type_ == NULL)
347 this->init_->determine_type_no_context();
350 Type_context context(this->type_, false);
351 this->init_->determine_type(&context);
355 if (this->type_ == NULL)
357 this->type_ = this->init_->type();
358 gcc_assert(!this->type_->is_abstract());
365 Temporary_statement::do_check_types(Gogo*)
367 if (this->type_ != NULL && this->init_ != NULL)
370 if (!Type::are_assignable(this->type_, this->init_->type(), &reason))
373 error_at(this->location(), "incompatible types in assignment");
375 error_at(this->location(), "incompatible types in assignment (%s)",
377 this->set_is_error();
385 Temporary_statement::do_get_backend(Translate_context* context)
387 gcc_assert(this->bvariable_ == NULL);
389 // FIXME: Permitting FUNCTION to be NULL here is a temporary measure
390 // until we have a better representation of the init function.
391 Named_object* function = context->function();
392 Bfunction* bfunction;
393 if (function == NULL)
396 bfunction = tree_to_function(function->func_value()->get_decl());
398 Btype* btype = tree_to_type(this->type()->get_tree(context->gogo()));
401 if (this->init_ == NULL)
403 else if (this->type_ == NULL)
404 binit = tree_to_expr(this->init_->get_tree(context));
407 Expression* init = Expression::make_cast(this->type_, this->init_,
409 context->gogo()->lower_expression(context->function(), &init);
410 binit = tree_to_expr(init->get_tree(context));
413 Bstatement* statement;
415 context->backend()->temporary_variable(bfunction, context->bblock(),
417 this->is_address_taken_,
418 this->location(), &statement);
422 // Return the backend variable.
425 Temporary_statement::get_backend_variable(Translate_context* context) const
427 if (this->bvariable_ == NULL)
429 gcc_assert(saw_errors());
430 return context->backend()->error_variable();
432 return this->bvariable_;
435 // Make and initialize a temporary variable in BLOCK.
438 Statement::make_temporary(Type* type, Expression* init,
439 source_location location)
441 return new Temporary_statement(type, init, location);
444 // An assignment statement.
446 class Assignment_statement : public Statement
449 Assignment_statement(Expression* lhs, Expression* rhs,
450 source_location location)
451 : Statement(STATEMENT_ASSIGNMENT, location),
457 do_traverse(Traverse* traverse);
460 do_traverse_assignments(Traverse_assignments*);
463 do_determine_types();
466 do_check_types(Gogo*);
469 do_get_backend(Translate_context*);
472 // Left hand side--the lvalue.
474 // Right hand side--the rvalue.
481 Assignment_statement::do_traverse(Traverse* traverse)
483 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
484 return TRAVERSE_EXIT;
485 return this->traverse_expression(traverse, &this->rhs_);
489 Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
491 tassign->assignment(&this->lhs_, &this->rhs_);
495 // Set types for the assignment.
498 Assignment_statement::do_determine_types()
500 this->lhs_->determine_type_no_context();
501 Type_context context(this->lhs_->type(), false);
502 this->rhs_->determine_type(&context);
505 // Check types for an assignment.
508 Assignment_statement::do_check_types(Gogo*)
510 // The left hand side must be either addressable, a map index
511 // expression, or the blank identifier.
512 if (!this->lhs_->is_addressable()
513 && this->lhs_->map_index_expression() == NULL
514 && !this->lhs_->is_sink_expression())
516 if (!this->lhs_->type()->is_error())
517 this->report_error(_("invalid left hand side of assignment"));
521 Type* lhs_type = this->lhs_->type();
522 Type* rhs_type = this->rhs_->type();
524 if (!Type::are_assignable(lhs_type, rhs_type, &reason))
527 error_at(this->location(), "incompatible types in assignment");
529 error_at(this->location(), "incompatible types in assignment (%s)",
531 this->set_is_error();
534 if (lhs_type->is_error() || rhs_type->is_error())
535 this->set_is_error();
538 // Build a tree for an assignment statement.
541 Assignment_statement::do_get_backend(Translate_context* context)
543 tree rhs_tree = this->rhs_->get_tree(context);
544 if (rhs_tree == error_mark_node)
545 return context->backend()->error_statement();
547 if (this->lhs_->is_sink_expression())
548 return context->backend()->expression_statement(tree_to_expr(rhs_tree));
550 tree lhs_tree = this->lhs_->get_tree(context);
552 if (lhs_tree == error_mark_node)
553 return context->backend()->error_statement();
555 rhs_tree = Expression::convert_for_assignment(context, this->lhs_->type(),
556 this->rhs_->type(), rhs_tree,
558 if (rhs_tree == error_mark_node)
559 return context->backend()->error_statement();
561 return context->backend()->assignment_statement(tree_to_expr(lhs_tree),
562 tree_to_expr(rhs_tree),
566 // Make an assignment statement.
569 Statement::make_assignment(Expression* lhs, Expression* rhs,
570 source_location location)
572 return new Assignment_statement(lhs, rhs, location);
575 // The Move_ordered_evals class is used to find any subexpressions of
576 // an expression that have an evaluation order dependency. It creates
577 // temporary variables to hold them.
579 class Move_ordered_evals : public Traverse
582 Move_ordered_evals(Block* block)
583 : Traverse(traverse_expressions),
589 expression(Expression**);
592 // The block where new temporary variables should be added.
597 Move_ordered_evals::expression(Expression** pexpr)
599 // We have to look at subexpressions first.
600 if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
601 return TRAVERSE_EXIT;
602 if ((*pexpr)->must_eval_in_order())
604 source_location loc = (*pexpr)->location();
605 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
606 this->block_->add_statement(temp);
607 *pexpr = Expression::make_temporary_reference(temp, loc);
609 return TRAVERSE_SKIP_COMPONENTS;
612 // An assignment operation statement.
614 class Assignment_operation_statement : public Statement
617 Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
618 source_location location)
619 : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
620 op_(op), lhs_(lhs), rhs_(rhs)
625 do_traverse(Traverse*);
628 do_traverse_assignments(Traverse_assignments*)
629 { gcc_unreachable(); }
632 do_lower(Gogo*, Named_object*, Block*);
635 do_get_backend(Translate_context*)
636 { gcc_unreachable(); }
639 // The operator (OPERATOR_PLUSEQ, etc.).
650 Assignment_operation_statement::do_traverse(Traverse* traverse)
652 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
653 return TRAVERSE_EXIT;
654 return this->traverse_expression(traverse, &this->rhs_);
657 // Lower an assignment operation statement to a regular assignment
661 Assignment_operation_statement::do_lower(Gogo*, Named_object*,
664 source_location loc = this->location();
666 // We have to evaluate the left hand side expression only once. We
667 // do this by moving out any expression with side effects.
668 Block* b = new Block(enclosing, loc);
669 Move_ordered_evals moe(b);
670 this->lhs_->traverse_subexpressions(&moe);
672 Expression* lval = this->lhs_->copy();
677 case OPERATOR_PLUSEQ:
680 case OPERATOR_MINUSEQ:
689 case OPERATOR_MULTEQ:
698 case OPERATOR_LSHIFTEQ:
699 op = OPERATOR_LSHIFT;
701 case OPERATOR_RSHIFTEQ:
702 op = OPERATOR_RSHIFT;
707 case OPERATOR_BITCLEAREQ:
708 op = OPERATOR_BITCLEAR;
714 Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
715 Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
716 if (b->statements()->empty())
724 return Statement::make_block_statement(b, loc);
728 // Make an assignment operation statement.
731 Statement::make_assignment_operation(Operator op, Expression* lhs,
732 Expression* rhs, source_location location)
734 return new Assignment_operation_statement(op, lhs, rhs, location);
737 // A tuple assignment statement. This differs from an assignment
738 // statement in that the right-hand-side expressions are evaluated in
741 class Tuple_assignment_statement : public Statement
744 Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
745 source_location location)
746 : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
752 do_traverse(Traverse* traverse);
755 do_traverse_assignments(Traverse_assignments*)
756 { gcc_unreachable(); }
759 do_lower(Gogo*, Named_object*, Block*);
762 do_get_backend(Translate_context*)
763 { gcc_unreachable(); }
766 // Left hand side--a list of lvalues.
767 Expression_list* lhs_;
768 // Right hand side--a list of rvalues.
769 Expression_list* rhs_;
775 Tuple_assignment_statement::do_traverse(Traverse* traverse)
777 if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
778 return TRAVERSE_EXIT;
779 return this->traverse_expression_list(traverse, this->rhs_);
782 // Lower a tuple assignment. We use temporary variables to split it
783 // up into a set of single assignments.
786 Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
788 source_location loc = this->location();
790 Block* b = new Block(enclosing, loc);
792 // First move out any subexpressions on the left hand side. The
793 // right hand side will be evaluated in the required order anyhow.
794 Move_ordered_evals moe(b);
795 for (Expression_list::const_iterator plhs = this->lhs_->begin();
796 plhs != this->lhs_->end();
798 (*plhs)->traverse_subexpressions(&moe);
800 std::vector<Temporary_statement*> temps;
801 temps.reserve(this->lhs_->size());
803 Expression_list::const_iterator prhs = this->rhs_->begin();
804 for (Expression_list::const_iterator plhs = this->lhs_->begin();
805 plhs != this->lhs_->end();
808 gcc_assert(prhs != this->rhs_->end());
810 if ((*plhs)->is_error_expression()
811 || (*plhs)->type()->is_error()
812 || (*prhs)->is_error_expression()
813 || (*prhs)->type()->is_error())
816 if ((*plhs)->is_sink_expression())
818 b->add_statement(Statement::make_statement(*prhs));
822 Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
824 b->add_statement(temp);
825 temps.push_back(temp);
828 gcc_assert(prhs == this->rhs_->end());
830 prhs = this->rhs_->begin();
831 std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
832 for (Expression_list::const_iterator plhs = this->lhs_->begin();
833 plhs != this->lhs_->end();
836 if ((*plhs)->is_error_expression()
837 || (*plhs)->type()->is_error()
838 || (*prhs)->is_error_expression()
839 || (*prhs)->type()->is_error())
842 if ((*plhs)->is_sink_expression())
845 Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
846 Statement* s = Statement::make_assignment(*plhs, ref, loc);
850 gcc_assert(ptemp == temps.end());
852 return Statement::make_block_statement(b, loc);
855 // Make a tuple assignment statement.
858 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
859 source_location location)
861 return new Tuple_assignment_statement(lhs, rhs, location);
864 // A tuple assignment from a map index expression.
867 class Tuple_map_assignment_statement : public Statement
870 Tuple_map_assignment_statement(Expression* val, Expression* present,
871 Expression* map_index,
872 source_location location)
873 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
874 val_(val), present_(present), map_index_(map_index)
879 do_traverse(Traverse* traverse);
882 do_traverse_assignments(Traverse_assignments*)
883 { gcc_unreachable(); }
886 do_lower(Gogo*, Named_object*, Block*);
889 do_get_backend(Translate_context*)
890 { gcc_unreachable(); }
893 // Lvalue which receives the value from the map.
895 // Lvalue which receives whether the key value was present.
896 Expression* present_;
897 // The map index expression.
898 Expression* map_index_;
904 Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
906 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
907 || this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
908 return TRAVERSE_EXIT;
909 return this->traverse_expression(traverse, &this->map_index_);
912 // Lower a tuple map assignment.
915 Tuple_map_assignment_statement::do_lower(Gogo*, Named_object*,
918 source_location loc = this->location();
920 Map_index_expression* map_index = this->map_index_->map_index_expression();
921 if (map_index == NULL)
923 this->report_error(_("expected map index on right hand side"));
924 return Statement::make_error_statement(loc);
926 Map_type* map_type = map_index->get_map_type();
927 if (map_type == NULL)
928 return Statement::make_error_statement(loc);
930 Block* b = new Block(enclosing, loc);
932 // Move out any subexpressions to make sure that functions are
933 // called in the required order.
934 Move_ordered_evals moe(b);
935 this->val_->traverse_subexpressions(&moe);
936 this->present_->traverse_subexpressions(&moe);
938 // Copy the key value into a temporary so that we can take its
939 // address without pushing the value onto the heap.
941 // var key_temp KEY_TYPE = MAP_INDEX
942 Temporary_statement* key_temp =
943 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
944 b->add_statement(key_temp);
946 // var val_temp VAL_TYPE
947 Temporary_statement* val_temp =
948 Statement::make_temporary(map_type->val_type(), NULL, loc);
949 b->add_statement(val_temp);
951 // var present_temp bool
952 Temporary_statement* present_temp =
953 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
954 b->add_statement(present_temp);
956 // present_temp = mapaccess2(MAP, &key_temp, &val_temp)
957 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
958 Expression* a1 = Expression::make_unary(OPERATOR_AND, ref, loc);
959 ref = Expression::make_temporary_reference(val_temp, loc);
960 Expression* a2 = Expression::make_unary(OPERATOR_AND, ref, loc);
961 Expression* call = Runtime::make_call(Runtime::MAPACCESS2, loc, 3,
962 map_index->map(), a1, a2);
964 ref = Expression::make_temporary_reference(present_temp, loc);
965 Statement* s = Statement::make_assignment(ref, call, loc);
969 ref = Expression::make_temporary_reference(val_temp, loc);
970 s = Statement::make_assignment(this->val_, ref, loc);
973 // present = present_temp
974 ref = Expression::make_temporary_reference(present_temp, loc);
975 s = Statement::make_assignment(this->present_, ref, loc);
978 return Statement::make_block_statement(b, loc);
981 // Make a map assignment statement which returns a pair of values.
984 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
985 Expression* map_index,
986 source_location location)
988 return new Tuple_map_assignment_statement(val, present, map_index, location);
991 // Assign a pair of entries to a map.
994 class Map_assignment_statement : public Statement
997 Map_assignment_statement(Expression* map_index,
998 Expression* val, Expression* should_set,
999 source_location location)
1000 : Statement(STATEMENT_MAP_ASSIGNMENT, location),
1001 map_index_(map_index), val_(val), should_set_(should_set)
1006 do_traverse(Traverse* traverse);
1009 do_traverse_assignments(Traverse_assignments*)
1010 { gcc_unreachable(); }
1013 do_lower(Gogo*, Named_object*, Block*);
1016 do_get_backend(Translate_context*)
1017 { gcc_unreachable(); }
1020 // A reference to the map index which should be set or deleted.
1021 Expression* map_index_;
1022 // The value to add to the map.
1024 // Whether or not to add the value.
1025 Expression* should_set_;
1028 // Traverse a map assignment.
1031 Map_assignment_statement::do_traverse(Traverse* traverse)
1033 if (this->traverse_expression(traverse, &this->map_index_) == TRAVERSE_EXIT
1034 || this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
1035 return TRAVERSE_EXIT;
1036 return this->traverse_expression(traverse, &this->should_set_);
1039 // Lower a map assignment to a function call.
1042 Map_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
1044 source_location loc = this->location();
1046 Map_index_expression* map_index = this->map_index_->map_index_expression();
1047 if (map_index == NULL)
1049 this->report_error(_("expected map index on left hand side"));
1050 return Statement::make_error_statement(loc);
1052 Map_type* map_type = map_index->get_map_type();
1053 if (map_type == NULL)
1054 return Statement::make_error_statement(loc);
1056 Block* b = new Block(enclosing, loc);
1058 // Evaluate the map first to get order of evaluation right.
1059 // map_temp := m // we are evaluating m[k] = v, p
1060 Temporary_statement* map_temp = Statement::make_temporary(map_type,
1063 b->add_statement(map_temp);
1065 // var key_temp MAP_KEY_TYPE = k
1066 Temporary_statement* key_temp =
1067 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1068 b->add_statement(key_temp);
1070 // var val_temp MAP_VAL_TYPE = v
1071 Temporary_statement* val_temp =
1072 Statement::make_temporary(map_type->val_type(), this->val_, loc);
1073 b->add_statement(val_temp);
1075 // var insert_temp bool = p
1076 Temporary_statement* insert_temp =
1077 Statement::make_temporary(Type::lookup_bool_type(), this->should_set_,
1079 b->add_statement(insert_temp);
1081 // mapassign2(map_temp, &key_temp, &val_temp, p)
1082 Expression* p1 = Expression::make_temporary_reference(map_temp, loc);
1083 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
1084 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1085 ref = Expression::make_temporary_reference(val_temp, loc);
1086 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1087 Expression* p4 = Expression::make_temporary_reference(insert_temp, loc);
1088 Expression* call = Runtime::make_call(Runtime::MAPASSIGN2, loc, 4,
1090 Statement* s = Statement::make_statement(call);
1091 b->add_statement(s);
1093 return Statement::make_block_statement(b, loc);
1096 // Make a statement which assigns a pair of entries to a map.
1099 Statement::make_map_assignment(Expression* map_index,
1100 Expression* val, Expression* should_set,
1101 source_location location)
1103 return new Map_assignment_statement(map_index, val, should_set, location);
1106 // A tuple assignment from a receive statement.
1108 class Tuple_receive_assignment_statement : public Statement
1111 Tuple_receive_assignment_statement(Expression* val, Expression* closed,
1112 Expression* channel, bool for_select,
1113 source_location location)
1114 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
1115 val_(val), closed_(closed), channel_(channel), for_select_(for_select)
1120 do_traverse(Traverse* traverse);
1123 do_traverse_assignments(Traverse_assignments*)
1124 { gcc_unreachable(); }
1127 do_lower(Gogo*, Named_object*, Block*);
1130 do_get_backend(Translate_context*)
1131 { gcc_unreachable(); }
1134 // Lvalue which receives the value from the channel.
1136 // Lvalue which receives whether the channel is closed.
1137 Expression* closed_;
1138 // The channel on which we receive the value.
1139 Expression* channel_;
1140 // Whether this is for a select statement.
1147 Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
1149 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1150 || this->traverse_expression(traverse, &this->closed_) == TRAVERSE_EXIT)
1151 return TRAVERSE_EXIT;
1152 return this->traverse_expression(traverse, &this->channel_);
1155 // Lower to a function call.
1158 Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
1161 source_location loc = this->location();
1163 Channel_type* channel_type = this->channel_->type()->channel_type();
1164 if (channel_type == NULL)
1166 this->report_error(_("expected channel"));
1167 return Statement::make_error_statement(loc);
1169 if (!channel_type->may_receive())
1171 this->report_error(_("invalid receive on send-only channel"));
1172 return Statement::make_error_statement(loc);
1175 Block* b = new Block(enclosing, loc);
1177 // Make sure that any subexpressions on the left hand side are
1178 // evaluated in the right order.
1179 Move_ordered_evals moe(b);
1180 this->val_->traverse_subexpressions(&moe);
1181 this->closed_->traverse_subexpressions(&moe);
1183 // var val_temp ELEMENT_TYPE
1184 Temporary_statement* val_temp =
1185 Statement::make_temporary(channel_type->element_type(), NULL, loc);
1186 b->add_statement(val_temp);
1188 // var closed_temp bool
1189 Temporary_statement* closed_temp =
1190 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
1191 b->add_statement(closed_temp);
1193 // closed_temp = chanrecv[23](channel, &val_temp)
1194 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1195 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
1196 Expression* call = Runtime::make_call((this->for_select_
1197 ? Runtime::CHANRECV3
1198 : Runtime::CHANRECV2),
1199 loc, 2, this->channel_, p2);
1200 ref = Expression::make_temporary_reference(closed_temp, loc);
1201 Statement* s = Statement::make_assignment(ref, call, loc);
1202 b->add_statement(s);
1205 ref = Expression::make_temporary_reference(val_temp, loc);
1206 s = Statement::make_assignment(this->val_, ref, loc);
1207 b->add_statement(s);
1209 // closed = closed_temp
1210 ref = Expression::make_temporary_reference(closed_temp, loc);
1211 s = Statement::make_assignment(this->closed_, ref, loc);
1212 b->add_statement(s);
1214 return Statement::make_block_statement(b, loc);
1217 // Make a nonblocking receive statement.
1220 Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
1221 Expression* channel,
1223 source_location location)
1225 return new Tuple_receive_assignment_statement(val, closed, channel,
1226 for_select, location);
1229 // An assignment to a pair of values from a type guard. This is a
1230 // conditional type guard. v, ok = i.(type).
1232 class Tuple_type_guard_assignment_statement : public Statement
1235 Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
1236 Expression* expr, Type* type,
1237 source_location location)
1238 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
1239 val_(val), ok_(ok), expr_(expr), type_(type)
1244 do_traverse(Traverse*);
1247 do_traverse_assignments(Traverse_assignments*)
1248 { gcc_unreachable(); }
1251 do_lower(Gogo*, Named_object*, Block*);
1254 do_get_backend(Translate_context*)
1255 { gcc_unreachable(); }
1259 lower_to_type(Runtime::Function);
1262 lower_to_object_type(Block*, Runtime::Function);
1264 // The variable which recieves the converted value.
1266 // The variable which receives the indication of success.
1268 // The expression being converted.
1270 // The type to which the expression is being converted.
1274 // Traverse a type guard tuple assignment.
1277 Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
1279 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1280 || this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
1281 || this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
1282 return TRAVERSE_EXIT;
1283 return this->traverse_expression(traverse, &this->expr_);
1286 // Lower to a function call.
1289 Tuple_type_guard_assignment_statement::do_lower(Gogo*, Named_object*,
1292 source_location loc = this->location();
1294 Type* expr_type = this->expr_->type();
1295 if (expr_type->interface_type() == NULL)
1297 if (!expr_type->is_error() && !this->type_->is_error())
1298 this->report_error(_("type assertion only valid for interface types"));
1299 return Statement::make_error_statement(loc);
1302 Block* b = new Block(enclosing, loc);
1304 // Make sure that any subexpressions on the left hand side are
1305 // evaluated in the right order.
1306 Move_ordered_evals moe(b);
1307 this->val_->traverse_subexpressions(&moe);
1308 this->ok_->traverse_subexpressions(&moe);
1310 bool expr_is_empty = expr_type->interface_type()->is_empty();
1311 Call_expression* call;
1312 if (this->type_->interface_type() != NULL)
1314 if (this->type_->interface_type()->is_empty())
1315 call = Runtime::make_call((expr_is_empty
1316 ? Runtime::IFACEE2E2
1317 : Runtime::IFACEI2E2),
1318 loc, 1, this->expr_);
1320 call = this->lower_to_type(expr_is_empty
1321 ? Runtime::IFACEE2I2
1322 : Runtime::IFACEI2I2);
1324 else if (this->type_->points_to() != NULL)
1325 call = this->lower_to_type(expr_is_empty
1326 ? Runtime::IFACEE2T2P
1327 : Runtime::IFACEI2T2P);
1330 this->lower_to_object_type(b,
1332 ? Runtime::IFACEE2T2
1333 : Runtime::IFACEI2T2));
1339 Expression* res = Expression::make_call_result(call, 0);
1340 res = Expression::make_unsafe_cast(this->type_, res, loc);
1341 Statement* s = Statement::make_assignment(this->val_, res, loc);
1342 b->add_statement(s);
1344 res = Expression::make_call_result(call, 1);
1345 s = Statement::make_assignment(this->ok_, res, loc);
1346 b->add_statement(s);
1349 return Statement::make_block_statement(b, loc);
1352 // Lower a conversion to a non-empty interface type or a pointer type.
1355 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code)
1357 source_location loc = this->location();
1358 return Runtime::make_call(code, loc, 2,
1359 Expression::make_type_descriptor(this->type_, loc),
1363 // Lower a conversion to a non-interface non-pointer type.
1366 Tuple_type_guard_assignment_statement::lower_to_object_type(
1368 Runtime::Function code)
1370 source_location loc = this->location();
1372 // var val_temp TYPE
1373 Temporary_statement* val_temp = Statement::make_temporary(this->type_,
1375 b->add_statement(val_temp);
1377 // ok = CODE(type_descriptor, expr, &val_temp)
1378 Expression* p1 = Expression::make_type_descriptor(this->type_, loc);
1379 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1380 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
1381 Expression* call = Runtime::make_call(code, loc, 3, p1, this->expr_, p3);
1382 Statement* s = Statement::make_assignment(this->ok_, call, loc);
1383 b->add_statement(s);
1386 ref = Expression::make_temporary_reference(val_temp, loc);
1387 s = Statement::make_assignment(this->val_, ref, loc);
1388 b->add_statement(s);
1391 // Make an assignment from a type guard to a pair of variables.
1394 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
1395 Expression* expr, Type* type,
1396 source_location location)
1398 return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
1402 // An expression statement.
1404 class Expression_statement : public Statement
1407 Expression_statement(Expression* expr)
1408 : Statement(STATEMENT_EXPRESSION, expr->location()),
1414 do_traverse(Traverse* traverse)
1415 { return this->traverse_expression(traverse, &this->expr_); }
1418 do_determine_types()
1419 { this->expr_->determine_type_no_context(); }
1422 do_may_fall_through() const;
1425 do_get_backend(Translate_context* context);
1431 // An expression statement may fall through unless it is a call to a
1432 // function which does not return.
1435 Expression_statement::do_may_fall_through() const
1437 const Call_expression* call = this->expr_->call_expression();
1440 const Expression* fn = call->fn();
1441 const Func_expression* fe = fn->func_expression();
1444 const Named_object* no = fe->named_object();
1446 Function_type* fntype;
1447 if (no->is_function())
1448 fntype = no->func_value()->type();
1449 else if (no->is_function_declaration())
1450 fntype = no->func_declaration_value()->type();
1454 // The builtin function panic does not return.
1455 if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
1462 // Convert to backend representation.
1465 Expression_statement::do_get_backend(Translate_context* context)
1467 tree expr_tree = this->expr_->get_tree(context);
1468 return context->backend()->expression_statement(tree_to_expr(expr_tree));
1471 // Make an expression statement from an Expression.
1474 Statement::make_statement(Expression* expr)
1476 return new Expression_statement(expr);
1479 // A block statement--a list of statements which may include variable
1482 class Block_statement : public Statement
1485 Block_statement(Block* block, source_location location)
1486 : Statement(STATEMENT_BLOCK, location),
1492 do_traverse(Traverse* traverse)
1493 { return this->block_->traverse(traverse); }
1496 do_determine_types()
1497 { this->block_->determine_types(); }
1500 do_may_fall_through() const
1501 { return this->block_->may_fall_through(); }
1504 do_get_backend(Translate_context* context);
1510 // Convert a block to the backend representation of a statement.
1513 Block_statement::do_get_backend(Translate_context* context)
1515 Bblock* bblock = this->block_->get_backend(context);
1516 return context->backend()->block_statement(bblock);
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_backend(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_functions | traverse_blocks),
1722 gogo_(gogo), function_(NULL)
1726 function(Named_object*);
1734 // The function we are traversing.
1735 Named_object* function_;
1738 // Keep track of the current function while looking for thunks.
1741 Simplify_thunk_traverse::function(Named_object* no)
1743 gcc_assert(this->function_ == NULL);
1744 this->function_ = no;
1745 int t = no->func_value()->traverse(this);
1746 this->function_ = NULL;
1747 if (t == TRAVERSE_EXIT)
1749 return TRAVERSE_SKIP_COMPONENTS;
1752 // Look for thunks in a block.
1755 Simplify_thunk_traverse::block(Block* b)
1757 // The parser ensures that thunk statements always appear at the end
1759 if (b->statements()->size() < 1)
1760 return TRAVERSE_CONTINUE;
1761 Thunk_statement* stat = b->statements()->back()->thunk_statement();
1763 return TRAVERSE_CONTINUE;
1764 if (stat->simplify_statement(this->gogo_, this->function_, b))
1765 return TRAVERSE_SKIP_COMPONENTS;
1766 return TRAVERSE_CONTINUE;
1769 // Simplify all thunk statements.
1772 Gogo::simplify_thunk_statements()
1774 Simplify_thunk_traverse thunk_traverse(this);
1775 this->traverse(&thunk_traverse);
1778 // Simplify complex thunk statements into simple ones. A complicated
1779 // thunk statement is one which takes anything other than zero
1780 // parameters or a single pointer parameter. We rewrite it into code
1781 // which allocates a struct, stores the parameter values into the
1782 // struct, and does a simple go or defer statement which passes the
1783 // struct to a thunk. The thunk does the real call.
1786 Thunk_statement::simplify_statement(Gogo* gogo, Named_object* function,
1789 if (this->classification() == STATEMENT_ERROR)
1791 if (this->call_->is_error_expression())
1794 if (this->classification() == STATEMENT_DEFER)
1796 // Make sure that the defer stack exists for the function. We
1797 // will use when converting this statement to the backend
1798 // representation, but we want it to exist when we start
1799 // converting the function.
1800 function->func_value()->defer_stack(this->location());
1803 Call_expression* ce = this->call_->call_expression();
1804 Function_type* fntype = ce->get_function_type();
1807 gcc_assert(saw_errors());
1808 this->set_is_error();
1811 if (this->is_simple(fntype))
1814 Expression* fn = ce->fn();
1815 Bound_method_expression* bound_method = fn->bound_method_expression();
1816 Interface_field_reference_expression* interface_method =
1817 fn->interface_field_reference_expression();
1818 const bool is_method = bound_method != NULL || interface_method != NULL;
1820 source_location location = this->location();
1822 std::string thunk_name = Gogo::thunk_name();
1825 this->build_thunk(gogo, thunk_name, fntype);
1827 // Generate code to call the thunk.
1829 // Get the values to store into the struct which is the single
1830 // argument to the thunk.
1832 Expression_list* vals = new Expression_list();
1833 if (fntype->is_builtin())
1835 else if (!is_method)
1836 vals->push_back(fn);
1837 else if (interface_method != NULL)
1838 vals->push_back(interface_method->expr());
1839 else if (bound_method != NULL)
1841 vals->push_back(bound_method->method());
1842 Expression* first_arg = bound_method->first_argument();
1844 // We always pass a pointer when calling a method.
1845 if (first_arg->type()->points_to() == NULL)
1846 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, location);
1848 // If we are calling a method which was inherited from an
1849 // embedded struct, and the method did not get a stub, then the
1850 // first type may be wrong.
1851 Type* fatype = bound_method->first_argument_type();
1854 if (fatype->points_to() == NULL)
1855 fatype = Type::make_pointer_type(fatype);
1856 Type* unsafe = Type::make_pointer_type(Type::make_void_type());
1857 first_arg = Expression::make_cast(unsafe, first_arg, location);
1858 first_arg = Expression::make_cast(fatype, first_arg, location);
1861 vals->push_back(first_arg);
1866 if (ce->args() != NULL)
1868 for (Expression_list::const_iterator p = ce->args()->begin();
1869 p != ce->args()->end();
1871 vals->push_back(*p);
1874 // Build the struct.
1875 Expression* constructor =
1876 Expression::make_struct_composite_literal(this->struct_type_, vals,
1879 // Allocate the initialized struct on the heap.
1880 constructor = Expression::make_heap_composite(constructor, location);
1882 // Look up the thunk.
1883 Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
1884 gcc_assert(named_thunk != NULL && named_thunk->is_function());
1887 Expression* func = Expression::make_func_reference(named_thunk, NULL,
1889 Expression_list* params = new Expression_list();
1890 params->push_back(constructor);
1891 Call_expression* call = Expression::make_call(func, params, false, location);
1893 // Build the simple go or defer statement.
1895 if (this->classification() == STATEMENT_GO)
1896 s = Statement::make_go_statement(call, location);
1897 else if (this->classification() == STATEMENT_DEFER)
1898 s = Statement::make_defer_statement(call, location);
1902 // The current block should end with the go statement.
1903 gcc_assert(block->statements()->size() >= 1);
1904 gcc_assert(block->statements()->back() == this);
1905 block->replace_statement(block->statements()->size() - 1, s);
1907 // We already ran the determine_types pass, so we need to run it now
1908 // for the new statement.
1909 s->determine_types();
1912 gogo->check_types_in_block(block);
1914 // Return true to tell the block not to keep looking at statements.
1918 // Set the name to use for thunk parameter N.
1921 Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
1923 snprintf(buf, buflen, "a%d", n);
1926 // Build a new struct type to hold the parameters for a complicated
1927 // thunk statement. FNTYPE is the type of the function call.
1930 Thunk_statement::build_struct(Function_type* fntype)
1932 source_location location = this->location();
1934 Struct_field_list* fields = new Struct_field_list();
1936 Call_expression* ce = this->call_->call_expression();
1937 Expression* fn = ce->fn();
1939 Interface_field_reference_expression* interface_method =
1940 fn->interface_field_reference_expression();
1941 if (interface_method != NULL)
1943 // If this thunk statement calls a method on an interface, we
1944 // pass the interface object to the thunk.
1945 Typed_identifier tid(Thunk_statement::thunk_field_fn,
1946 interface_method->expr()->type(),
1948 fields->push_back(Struct_field(tid));
1950 else if (!fntype->is_builtin())
1952 // The function to call.
1953 Typed_identifier tid(Go_statement::thunk_field_fn, fntype, location);
1954 fields->push_back(Struct_field(tid));
1956 else if (ce->is_recover_call())
1958 // The predeclared recover function has no argument. However,
1959 // we add an argument when building recover thunks. Handle that
1961 fields->push_back(Struct_field(Typed_identifier("can_recover",
1962 Type::lookup_bool_type(),
1966 if (fn->bound_method_expression() != NULL)
1968 gcc_assert(fntype->is_method());
1969 Type* rtype = fntype->receiver()->type();
1970 // We always pass the receiver as a pointer.
1971 if (rtype->points_to() == NULL)
1972 rtype = Type::make_pointer_type(rtype);
1973 Typed_identifier tid(Thunk_statement::thunk_field_receiver, rtype,
1975 fields->push_back(Struct_field(tid));
1978 const Expression_list* args = ce->args();
1982 for (Expression_list::const_iterator p = args->begin();
1987 this->thunk_field_param(i, buf, sizeof buf);
1988 fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
1993 return Type::make_struct_type(fields, location);
1996 // Build the thunk we are going to call. This is a brand new, albeit
1997 // artificial, function.
2000 Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
2001 Function_type* fntype)
2003 source_location location = this->location();
2005 Call_expression* ce = this->call_->call_expression();
2007 bool may_call_recover = false;
2008 if (this->classification() == STATEMENT_DEFER)
2010 Func_expression* fn = ce->fn()->func_expression();
2012 may_call_recover = true;
2015 const Named_object* no = fn->named_object();
2016 if (!no->is_function())
2017 may_call_recover = true;
2019 may_call_recover = no->func_value()->calls_recover();
2023 // Build the type of the thunk. The thunk takes a single parameter,
2024 // which is a pointer to the special structure we build.
2025 const char* const parameter_name = "__go_thunk_parameter";
2026 Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
2027 Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
2028 thunk_parameters->push_back(Typed_identifier(parameter_name,
2029 pointer_to_struct_type,
2032 Typed_identifier_list* thunk_results = NULL;
2033 if (may_call_recover)
2035 // When deferring a function which may call recover, add a
2036 // return value, to disable tail call optimizations which will
2037 // break the way we check whether recover is permitted.
2038 thunk_results = new Typed_identifier_list();
2039 thunk_results->push_back(Typed_identifier("", Type::lookup_bool_type(),
2043 Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
2047 // Start building the thunk.
2048 Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
2051 // For a defer statement, start with a call to
2052 // __go_set_defer_retaddr. */
2053 Label* retaddr_label = NULL;
2054 if (may_call_recover)
2056 retaddr_label = gogo->add_label_reference("retaddr");
2057 Expression* arg = Expression::make_label_addr(retaddr_label, location);
2058 Expression* call = Runtime::make_call(Runtime::SET_DEFER_RETADDR,
2061 // This is a hack to prevent the middle-end from deleting the
2063 gogo->start_block(location);
2064 gogo->add_statement(Statement::make_goto_statement(retaddr_label,
2066 Block* then_block = gogo->finish_block(location);
2067 then_block->determine_types();
2069 Statement* s = Statement::make_if_statement(call, then_block, NULL,
2071 s->determine_types();
2072 gogo->add_statement(s);
2075 // Get a reference to the parameter.
2076 Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
2077 gcc_assert(named_parameter != NULL && named_parameter->is_variable());
2079 // Build the call. Note that the field names are the same as the
2080 // ones used in build_struct.
2081 Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
2083 thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
2086 Bound_method_expression* bound_method = ce->fn()->bound_method_expression();
2087 Interface_field_reference_expression* interface_method =
2088 ce->fn()->interface_field_reference_expression();
2090 Expression* func_to_call;
2091 unsigned int next_index;
2092 if (!fntype->is_builtin())
2094 func_to_call = Expression::make_field_reference(thunk_parameter,
2100 gcc_assert(bound_method == NULL && interface_method == NULL);
2101 func_to_call = ce->fn();
2105 if (bound_method != NULL)
2107 Expression* r = Expression::make_field_reference(thunk_parameter, 1,
2109 // The main program passes in a function pointer from the
2110 // interface expression, so here we can make a bound method in
2112 func_to_call = Expression::make_bound_method(r, func_to_call,
2116 else if (interface_method != NULL)
2118 // The main program passes the interface object.
2119 const std::string& name(interface_method->name());
2120 func_to_call = Expression::make_interface_field_reference(func_to_call,
2125 Expression_list* call_params = new Expression_list();
2126 const Struct_field_list* fields = this->struct_type_->fields();
2127 Struct_field_list::const_iterator p = fields->begin();
2128 for (unsigned int i = 0; i < next_index; ++i)
2130 bool is_recover_call = ce->is_recover_call();
2131 Expression* recover_arg = NULL;
2132 for (; p != fields->end(); ++p, ++next_index)
2134 Expression* thunk_param = Expression::make_var_reference(named_parameter,
2136 thunk_param = Expression::make_unary(OPERATOR_MULT, thunk_param,
2138 Expression* param = Expression::make_field_reference(thunk_param,
2141 if (!is_recover_call)
2142 call_params->push_back(param);
2145 gcc_assert(call_params->empty());
2146 recover_arg = param;
2150 if (call_params->empty())
2156 Expression* call = Expression::make_call(func_to_call, call_params, false,
2158 // We need to lower in case this is a builtin function.
2159 call = call->lower(gogo, function, -1);
2160 Call_expression* call_ce = call->call_expression();
2161 if (call_ce != NULL && may_call_recover)
2162 call_ce->set_is_deferred();
2164 Statement* call_statement = Statement::make_statement(call);
2166 // We already ran the determine_types pass, so we need to run it
2167 // just for this statement now.
2168 call_statement->determine_types();
2171 call->check_types(gogo);
2173 if (call_ce != NULL && recover_arg != NULL)
2174 call_ce->set_recover_arg(recover_arg);
2176 gogo->add_statement(call_statement);
2178 // If this is a defer statement, the label comes immediately after
2180 if (may_call_recover)
2182 gogo->add_label_definition("retaddr", location);
2184 Expression_list* vals = new Expression_list();
2185 vals->push_back(Expression::make_boolean(false, location));
2186 gogo->add_statement(Statement::make_return_statement(vals, location));
2189 // That is all the thunk has to do.
2190 gogo->finish_function(location);
2193 // Get the function and argument trees.
2196 Thunk_statement::get_fn_and_arg(Expression** pfn, Expression** parg)
2198 if (this->call_->is_error_expression())
2201 Call_expression* ce = this->call_->call_expression();
2205 const Expression_list* args = ce->args();
2206 if (args == NULL || args->empty())
2207 *parg = Expression::make_nil(this->location());
2210 gcc_assert(args->size() == 1);
2211 *parg = args->front();
2217 // Class Go_statement.
2220 Go_statement::do_get_backend(Translate_context* context)
2224 if (!this->get_fn_and_arg(&fn, &arg))
2225 return context->backend()->error_statement();
2227 Expression* call = Runtime::make_call(Runtime::GO, this->location(), 2,
2229 tree call_tree = call->get_tree(context);
2230 Bexpression* call_bexpr = tree_to_expr(call_tree);
2231 return context->backend()->expression_statement(call_bexpr);
2234 // Make a go statement.
2237 Statement::make_go_statement(Call_expression* call, source_location location)
2239 return new Go_statement(call, location);
2242 // Class Defer_statement.
2245 Defer_statement::do_get_backend(Translate_context* context)
2249 if (!this->get_fn_and_arg(&fn, &arg))
2250 return context->backend()->error_statement();
2252 source_location loc = this->location();
2253 Expression* ds = context->function()->func_value()->defer_stack(loc);
2255 Expression* call = Runtime::make_call(Runtime::DEFER, loc, 3,
2257 tree call_tree = call->get_tree(context);
2258 Bexpression* call_bexpr = tree_to_expr(call_tree);
2259 return context->backend()->expression_statement(call_bexpr);
2262 // Make a defer statement.
2265 Statement::make_defer_statement(Call_expression* call,
2266 source_location location)
2268 return new Defer_statement(call, location);
2271 // Class Return_statement.
2273 // Traverse assignments. We treat each return value as a top level
2274 // RHS in an expression.
2277 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2279 Expression_list* vals = this->vals_;
2282 for (Expression_list::iterator p = vals->begin();
2285 tassign->value(&*p, true, true);
2290 // Lower a return statement. If we are returning a function call
2291 // which returns multiple values which match the current function,
2292 // split up the call's results. If the function has named result
2293 // variables, and the return statement lists explicit values, then
2294 // implement it by assigning the values to the result variables and
2295 // changing the statement to not list any values. This lets
2296 // panic/recover work correctly.
2299 Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing)
2301 if (this->is_lowered_)
2304 Expression_list* vals = this->vals_;
2306 this->is_lowered_ = true;
2308 source_location loc = this->location();
2310 size_t vals_count = vals == NULL ? 0 : vals->size();
2311 Function::Results* results = function->func_value()->result_variables();
2312 size_t results_count = results == NULL ? 0 : results->size();
2314 if (vals_count == 0)
2316 if (results_count > 0 && !function->func_value()->results_are_named())
2318 this->report_error(_("not enough arguments to return"));
2324 if (results_count == 0)
2326 this->report_error(_("return with value in function "
2327 "with no return type"));
2331 // If the current function has multiple return values, and we are
2332 // returning a single call expression, split up the call expression.
2333 if (results_count > 1
2334 && vals->size() == 1
2335 && vals->front()->call_expression() != NULL)
2337 Call_expression* call = vals->front()->call_expression();
2339 vals = new Expression_list;
2340 for (size_t i = 0; i < results_count; ++i)
2341 vals->push_back(Expression::make_call_result(call, i));
2342 vals_count = results_count;
2345 if (vals_count < results_count)
2347 this->report_error(_("not enough arguments to return"));
2351 if (vals_count > results_count)
2353 this->report_error(_("too many values in return statement"));
2357 Block* b = new Block(enclosing, loc);
2359 Expression_list* lhs = new Expression_list();
2360 Expression_list* rhs = new Expression_list();
2362 Expression_list::const_iterator pe = vals->begin();
2364 for (Function::Results::const_iterator pr = results->begin();
2365 pr != results->end();
2368 Named_object* rv = *pr;
2369 Expression* e = *pe;
2371 // Check types now so that we give a good error message. The
2372 // result type is known. We determine the expression type
2375 Type *rvtype = rv->result_var_value()->type();
2376 Type_context type_context(rvtype, false);
2377 e->determine_type(&type_context);
2380 if (Type::are_assignable(rvtype, e->type(), &reason))
2382 Expression* ve = Expression::make_var_reference(rv, e->location());
2389 error_at(e->location(), "incompatible type for return value %d", i);
2391 error_at(e->location(),
2392 "incompatible type for return value %d (%s)",
2396 gcc_assert(lhs->size() == rhs->size());
2400 else if (lhs->size() == 1)
2402 b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
2408 b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
2410 b->add_statement(this);
2414 return Statement::make_block_statement(b, loc);
2417 // Convert a return statement to the backend representation.
2420 Return_statement::do_get_backend(Translate_context* context)
2422 source_location loc = this->location();
2424 Function* function = context->function()->func_value();
2425 tree fndecl = function->get_decl();
2427 Function::Results* results = function->result_variables();
2428 std::vector<Bexpression*> retvals;
2429 if (results != NULL && !results->empty())
2431 retvals.reserve(results->size());
2432 for (Function::Results::const_iterator p = results->begin();
2433 p != results->end();
2436 Expression* vr = Expression::make_var_reference(*p, loc);
2437 retvals.push_back(tree_to_expr(vr->get_tree(context)));
2441 return context->backend()->return_statement(tree_to_function(fndecl),
2445 // Make a return statement.
2448 Statement::make_return_statement(Expression_list* vals,
2449 source_location location)
2451 return new Return_statement(vals, location);
2454 // A break or continue statement.
2456 class Bc_statement : public Statement
2459 Bc_statement(bool is_break, Unnamed_label* label, source_location location)
2460 : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
2461 label_(label), is_break_(is_break)
2466 { return this->is_break_; }
2470 do_traverse(Traverse*)
2471 { return TRAVERSE_CONTINUE; }
2474 do_may_fall_through() const
2478 do_get_backend(Translate_context* context)
2479 { return this->label_->get_goto(context, this->location()); }
2482 // The label that this branches to.
2483 Unnamed_label* label_;
2484 // True if this is "break", false if it is "continue".
2488 // Make a break statement.
2491 Statement::make_break_statement(Unnamed_label* label, source_location location)
2493 return new Bc_statement(true, label, location);
2496 // Make a continue statement.
2499 Statement::make_continue_statement(Unnamed_label* label,
2500 source_location location)
2502 return new Bc_statement(false, label, location);
2505 // A goto statement.
2507 class Goto_statement : public Statement
2510 Goto_statement(Label* label, source_location location)
2511 : Statement(STATEMENT_GOTO, location),
2517 do_traverse(Traverse*)
2518 { return TRAVERSE_CONTINUE; }
2521 do_check_types(Gogo*);
2524 do_may_fall_through() const
2528 do_get_backend(Translate_context*);
2534 // Check types for a label. There aren't any types per se, but we use
2535 // this to give an error if the label was never defined.
2538 Goto_statement::do_check_types(Gogo*)
2540 if (!this->label_->is_defined())
2542 error_at(this->location(), "reference to undefined label %qs",
2543 Gogo::message_name(this->label_->name()).c_str());
2544 this->set_is_error();
2548 // Return the tree for the goto statement.
2551 Goto_statement::do_get_backend(Translate_context* context)
2553 Blabel* blabel = this->label_->get_backend_label(context);
2554 return context->backend()->goto_statement(blabel, this->location());
2557 // Make a goto statement.
2560 Statement::make_goto_statement(Label* label, source_location location)
2562 return new Goto_statement(label, location);
2565 // A goto statement to an unnamed label.
2567 class Goto_unnamed_statement : public Statement
2570 Goto_unnamed_statement(Unnamed_label* label, source_location location)
2571 : Statement(STATEMENT_GOTO_UNNAMED, location),
2577 do_traverse(Traverse*)
2578 { return TRAVERSE_CONTINUE; }
2581 do_may_fall_through() const
2585 do_get_backend(Translate_context* context)
2586 { return this->label_->get_goto(context, this->location()); }
2589 Unnamed_label* label_;
2592 // Make a goto statement to an unnamed label.
2595 Statement::make_goto_unnamed_statement(Unnamed_label* label,
2596 source_location location)
2598 return new Goto_unnamed_statement(label, location);
2601 // Class Label_statement.
2606 Label_statement::do_traverse(Traverse*)
2608 return TRAVERSE_CONTINUE;
2611 // Return a tree defining this label.
2614 Label_statement::do_get_backend(Translate_context* context)
2616 Blabel* blabel = this->label_->get_backend_label(context);
2617 return context->backend()->label_definition_statement(blabel);
2620 // Make a label statement.
2623 Statement::make_label_statement(Label* label, source_location location)
2625 return new Label_statement(label, location);
2628 // An unnamed label statement.
2630 class Unnamed_label_statement : public Statement
2633 Unnamed_label_statement(Unnamed_label* label)
2634 : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
2640 do_traverse(Traverse*)
2641 { return TRAVERSE_CONTINUE; }
2644 do_get_backend(Translate_context* context)
2645 { return this->label_->get_definition(context); }
2649 Unnamed_label* label_;
2652 // Make an unnamed label statement.
2655 Statement::make_unnamed_label_statement(Unnamed_label* label)
2657 return new Unnamed_label_statement(label);
2662 class If_statement : public Statement
2665 If_statement(Expression* cond, Block* then_block, Block* else_block,
2666 source_location location)
2667 : Statement(STATEMENT_IF, location),
2668 cond_(cond), then_block_(then_block), else_block_(else_block)
2673 do_traverse(Traverse*);
2676 do_determine_types();
2679 do_check_types(Gogo*);
2682 do_may_fall_through() const;
2685 do_get_backend(Translate_context*);
2696 If_statement::do_traverse(Traverse* traverse)
2698 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT
2699 || this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
2700 return TRAVERSE_EXIT;
2701 if (this->else_block_ != NULL)
2703 if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
2704 return TRAVERSE_EXIT;
2706 return TRAVERSE_CONTINUE;
2710 If_statement::do_determine_types()
2712 Type_context context(Type::lookup_bool_type(), false);
2713 this->cond_->determine_type(&context);
2714 this->then_block_->determine_types();
2715 if (this->else_block_ != NULL)
2716 this->else_block_->determine_types();
2722 If_statement::do_check_types(Gogo*)
2724 Type* type = this->cond_->type();
2725 if (type->is_error())
2726 this->set_is_error();
2727 else if (!type->is_boolean_type())
2728 this->report_error(_("expected boolean expression"));
2731 // Whether the overall statement may fall through.
2734 If_statement::do_may_fall_through() const
2736 return (this->else_block_ == NULL
2737 || this->then_block_->may_fall_through()
2738 || this->else_block_->may_fall_through());
2744 If_statement::do_get_backend(Translate_context* context)
2746 gcc_assert(this->cond_->type()->is_boolean_type()
2747 || this->cond_->type()->is_error());
2748 tree cond_tree = this->cond_->get_tree(context);
2749 Bexpression* cond_expr = tree_to_expr(cond_tree);
2750 Bblock* then_block = this->then_block_->get_backend(context);
2751 Bblock* else_block = (this->else_block_ == NULL
2753 : this->else_block_->get_backend(context));
2754 return context->backend()->if_statement(cond_expr, then_block,
2755 else_block, this->location());
2758 // Make an if statement.
2761 Statement::make_if_statement(Expression* cond, Block* then_block,
2762 Block* else_block, source_location location)
2764 return new If_statement(cond, then_block, else_block, location);
2767 // Class Case_clauses::Hash_integer_value.
2769 class Case_clauses::Hash_integer_value
2773 operator()(Expression*) const;
2777 Case_clauses::Hash_integer_value::operator()(Expression* pe) const
2782 if (!pe->integer_constant_value(true, ival, &itype))
2784 size_t ret = mpz_get_ui(ival);
2789 // Class Case_clauses::Eq_integer_value.
2791 class Case_clauses::Eq_integer_value
2795 operator()(Expression*, Expression*) const;
2799 Case_clauses::Eq_integer_value::operator()(Expression* a, Expression* b) const
2807 if (!a->integer_constant_value(true, aval, &atype)
2808 || !b->integer_constant_value(true, bval, &btype))
2810 bool ret = mpz_cmp(aval, bval) == 0;
2816 // Class Case_clauses::Case_clause.
2821 Case_clauses::Case_clause::traverse(Traverse* traverse)
2823 if (this->cases_ != NULL
2824 && (traverse->traverse_mask()
2825 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
2827 if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
2828 return TRAVERSE_EXIT;
2830 if (this->statements_ != NULL)
2832 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
2833 return TRAVERSE_EXIT;
2835 return TRAVERSE_CONTINUE;
2838 // Check whether all the case expressions are integer constants.
2841 Case_clauses::Case_clause::is_constant() const
2843 if (this->cases_ != NULL)
2845 for (Expression_list::const_iterator p = this->cases_->begin();
2846 p != this->cases_->end();
2848 if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
2854 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
2855 // value we are switching on; it may be NULL. If START_LABEL is not
2856 // NULL, it goes at the start of the statements, after the condition
2857 // test. We branch to FINISH_LABEL at the end of the statements.
2860 Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
2861 Unnamed_label* start_label,
2862 Unnamed_label* finish_label) const
2864 source_location loc = this->location_;
2865 Unnamed_label* next_case_label;
2866 if (this->cases_ == NULL || this->cases_->empty())
2868 gcc_assert(this->is_default_);
2869 next_case_label = NULL;
2873 Expression* cond = NULL;
2875 for (Expression_list::const_iterator p = this->cases_->begin();
2876 p != this->cases_->end();
2879 Expression* this_cond;
2880 if (val_temp == NULL)
2884 Expression* ref = Expression::make_temporary_reference(val_temp,
2886 this_cond = Expression::make_binary(OPERATOR_EQEQ, ref, *p, loc);
2892 cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
2895 Block* then_block = new Block(b, loc);
2896 next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
2897 Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
2899 then_block->add_statement(s);
2901 // if !COND { goto NEXT_CASE_LABEL }
2902 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
2903 s = Statement::make_if_statement(cond, then_block, NULL, loc);
2904 b->add_statement(s);
2907 if (start_label != NULL)
2908 b->add_statement(Statement::make_unnamed_label_statement(start_label));
2910 if (this->statements_ != NULL)
2911 b->add_statement(Statement::make_block_statement(this->statements_, loc));
2913 Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
2914 b->add_statement(s);
2916 if (next_case_label != NULL)
2917 b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
2923 Case_clauses::Case_clause::determine_types(Type* type)
2925 if (this->cases_ != NULL)
2927 Type_context case_context(type, false);
2928 for (Expression_list::iterator p = this->cases_->begin();
2929 p != this->cases_->end();
2931 (*p)->determine_type(&case_context);
2933 if (this->statements_ != NULL)
2934 this->statements_->determine_types();
2937 // Check types. Returns false if there was an error.
2940 Case_clauses::Case_clause::check_types(Type* type)
2942 if (this->cases_ != NULL)
2944 for (Expression_list::iterator p = this->cases_->begin();
2945 p != this->cases_->end();
2948 if (!Type::are_assignable(type, (*p)->type(), NULL)
2949 && !Type::are_assignable((*p)->type(), type, NULL))
2951 error_at((*p)->location(),
2952 "type mismatch between switch value and case clause");
2960 // Return true if this clause may fall through to the following
2961 // statements. Note that this is not the same as whether the case
2962 // uses the "fallthrough" keyword.
2965 Case_clauses::Case_clause::may_fall_through() const
2967 if (this->statements_ == NULL)
2969 return this->statements_->may_fall_through();
2972 // Convert the case values and statements to the backend
2973 // representation. BREAK_LABEL is the label which break statements
2974 // should branch to. CASE_CONSTANTS is used to detect duplicate
2975 // constants. *CASES should be passed as an empty vector; the values
2976 // for this case will be added to it. If this is the default case,
2977 // *CASES will remain empty. This returns the statement to execute if
2978 // one of these cases is selected.
2981 Case_clauses::Case_clause::get_backend(Translate_context* context,
2982 Unnamed_label* break_label,
2983 Case_constants* case_constants,
2984 std::vector<Bexpression*>* cases) const
2986 if (this->cases_ != NULL)
2988 gcc_assert(!this->is_default_);
2989 for (Expression_list::const_iterator p = this->cases_->begin();
2990 p != this->cases_->end();
2994 if (e->classification() != Expression::EXPRESSION_INTEGER)
2999 if (!(*p)->integer_constant_value(true, ival, &itype))
3001 // Something went wrong. This can happen with a
3002 // negative constant and an unsigned switch value.
3003 gcc_assert(saw_errors());
3006 gcc_assert(itype != NULL);
3007 e = Expression::make_integer(&ival, itype, e->location());
3011 std::pair<Case_constants::iterator, bool> ins =
3012 case_constants->insert(e);
3015 // Value was already present.
3016 error_at(this->location_, "duplicate case in switch");
3020 tree case_tree = e->get_tree(context);
3021 Bexpression* case_expr = tree_to_expr(case_tree);
3022 cases->push_back(case_expr);
3026 Bstatement* statements;
3027 if (this->statements_ == NULL)
3031 Bblock* bblock = this->statements_->get_backend(context);
3032 statements = context->backend()->block_statement(bblock);
3035 Bstatement* break_stat;
3036 if (this->is_fallthrough_)
3039 break_stat = break_label->get_goto(context, this->location_);
3041 if (statements == NULL)
3043 else if (break_stat == NULL)
3046 return context->backend()->compound_statement(statements, break_stat);
3049 // Class Case_clauses.
3054 Case_clauses::traverse(Traverse* traverse)
3056 for (Clauses::iterator p = this->clauses_.begin();
3057 p != this->clauses_.end();
3060 if (p->traverse(traverse) == TRAVERSE_EXIT)
3061 return TRAVERSE_EXIT;
3063 return TRAVERSE_CONTINUE;
3066 // Check whether all the case expressions are constant.
3069 Case_clauses::is_constant() const
3071 for (Clauses::const_iterator p = this->clauses_.begin();
3072 p != this->clauses_.end();
3074 if (!p->is_constant())
3079 // Lower case clauses for a nonconstant switch.
3082 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3083 Unnamed_label* break_label) const
3085 // The default case.
3086 const Case_clause* default_case = NULL;
3088 // The label for the fallthrough of the previous case.
3089 Unnamed_label* last_fallthrough_label = NULL;
3091 // The label for the start of the default case. This is used if the
3092 // case before the default case falls through.
3093 Unnamed_label* default_start_label = NULL;
3095 // The label for the end of the default case. This normally winds
3096 // up as BREAK_LABEL, but it will be different if the default case
3098 Unnamed_label* default_finish_label = NULL;
3100 for (Clauses::const_iterator p = this->clauses_.begin();
3101 p != this->clauses_.end();
3104 // The label to use for the start of the statements for this
3105 // case. This is NULL unless the previous case falls through.
3106 Unnamed_label* start_label = last_fallthrough_label;
3108 // The label to jump to after the end of the statements for this
3110 Unnamed_label* finish_label = break_label;
3112 last_fallthrough_label = NULL;
3113 if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3115 finish_label = new Unnamed_label(p->location());
3116 last_fallthrough_label = finish_label;
3119 if (!p->is_default())
3120 p->lower(b, val_temp, start_label, finish_label);
3123 // We have to move the default case to the end, so that we
3124 // only use it if all the other tests fail.
3126 default_start_label = start_label;
3127 default_finish_label = finish_label;
3131 if (default_case != NULL)
3132 default_case->lower(b, val_temp, default_start_label,
3133 default_finish_label);
3140 Case_clauses::determine_types(Type* type)
3142 for (Clauses::iterator p = this->clauses_.begin();
3143 p != this->clauses_.end();
3145 p->determine_types(type);
3148 // Check types. Returns false if there was an error.
3151 Case_clauses::check_types(Type* type)
3154 for (Clauses::iterator p = this->clauses_.begin();
3155 p != this->clauses_.end();
3158 if (!p->check_types(type))
3164 // Return true if these clauses may fall through to the statements
3165 // following the switch statement.
3168 Case_clauses::may_fall_through() const
3170 bool found_default = false;
3171 for (Clauses::const_iterator p = this->clauses_.begin();
3172 p != this->clauses_.end();
3175 if (p->may_fall_through() && !p->is_fallthrough())
3177 if (p->is_default())
3178 found_default = true;
3180 return !found_default;
3183 // Convert the cases to the backend representation. This sets
3184 // *ALL_CASES and *ALL_STATEMENTS.
3187 Case_clauses::get_backend(Translate_context* context,
3188 Unnamed_label* break_label,
3189 std::vector<std::vector<Bexpression*> >* all_cases,
3190 std::vector<Bstatement*>* all_statements) const
3192 Case_constants case_constants;
3194 size_t c = this->clauses_.size();
3195 all_cases->resize(c);
3196 all_statements->resize(c);
3199 for (Clauses::const_iterator p = this->clauses_.begin();
3200 p != this->clauses_.end();
3203 std::vector<Bexpression*> cases;
3204 Bstatement* stat = p->get_backend(context, break_label, &case_constants,
3206 (*all_cases)[i].swap(cases);
3207 (*all_statements)[i] = stat;
3211 // A constant switch statement. A Switch_statement is lowered to this
3212 // when all the cases are constants.
3214 class Constant_switch_statement : public Statement
3217 Constant_switch_statement(Expression* val, Case_clauses* clauses,
3218 Unnamed_label* break_label,
3219 source_location location)
3220 : Statement(STATEMENT_CONSTANT_SWITCH, location),
3221 val_(val), clauses_(clauses), break_label_(break_label)
3226 do_traverse(Traverse*);
3229 do_determine_types();
3232 do_check_types(Gogo*);
3235 do_may_fall_through() const;
3238 do_get_backend(Translate_context*);
3241 // The value to switch on.
3243 // The case clauses.
3244 Case_clauses* clauses_;
3245 // The break label, if needed.
3246 Unnamed_label* break_label_;
3252 Constant_switch_statement::do_traverse(Traverse* traverse)
3254 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3255 return TRAVERSE_EXIT;
3256 return this->clauses_->traverse(traverse);
3262 Constant_switch_statement::do_determine_types()
3264 this->val_->determine_type_no_context();
3265 this->clauses_->determine_types(this->val_->type());
3271 Constant_switch_statement::do_check_types(Gogo*)
3273 if (!this->clauses_->check_types(this->val_->type()))
3274 this->set_is_error();
3277 // Return whether this switch may fall through.
3280 Constant_switch_statement::do_may_fall_through() const
3282 if (this->clauses_ == NULL)
3285 // If we have a break label, then some case needed it. That implies
3286 // that the switch statement as a whole can fall through.
3287 if (this->break_label_ != NULL)
3290 return this->clauses_->may_fall_through();
3293 // Convert to GENERIC.
3296 Constant_switch_statement::do_get_backend(Translate_context* context)
3298 tree switch_val_tree = this->val_->get_tree(context);
3299 Bexpression* switch_val_expr = tree_to_expr(switch_val_tree);
3301 Unnamed_label* break_label = this->break_label_;
3302 if (break_label == NULL)
3303 break_label = new Unnamed_label(this->location());
3305 std::vector<std::vector<Bexpression*> > all_cases;
3306 std::vector<Bstatement*> all_statements;
3307 this->clauses_->get_backend(context, break_label, &all_cases,
3310 Bstatement* switch_statement;
3311 switch_statement = context->backend()->switch_statement(switch_val_expr,
3315 Bstatement* ldef = break_label->get_definition(context);
3316 return context->backend()->compound_statement(switch_statement, ldef);
3319 // Class Switch_statement.
3324 Switch_statement::do_traverse(Traverse* traverse)
3326 if (this->val_ != NULL)
3328 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3329 return TRAVERSE_EXIT;
3331 return this->clauses_->traverse(traverse);
3334 // Lower a Switch_statement to a Constant_switch_statement or a series
3335 // of if statements.
3338 Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
3340 source_location loc = this->location();
3342 if (this->val_ != NULL
3343 && (this->val_->is_error_expression()
3344 || this->val_->type()->is_error()))
3345 return Statement::make_error_statement(loc);
3347 if (this->val_ != NULL
3348 && this->val_->type()->integer_type() != NULL
3349 && !this->clauses_->empty()
3350 && this->clauses_->is_constant())
3351 return new Constant_switch_statement(this->val_, this->clauses_,
3352 this->break_label_, loc);
3354 Block* b = new Block(enclosing, loc);
3356 if (this->clauses_->empty())
3358 Expression* val = this->val_;
3360 val = Expression::make_boolean(true, loc);
3361 return Statement::make_statement(val);
3364 Temporary_statement* val_temp;
3365 if (this->val_ == NULL)
3369 // var val_temp VAL_TYPE = VAL
3370 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3371 b->add_statement(val_temp);
3374 this->clauses_->lower(b, val_temp, this->break_label());
3376 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3377 b->add_statement(s);
3379 return Statement::make_block_statement(b, loc);
3382 // Return the break label for this switch statement, creating it if
3386 Switch_statement::break_label()
3388 if (this->break_label_ == NULL)
3389 this->break_label_ = new Unnamed_label(this->location());
3390 return this->break_label_;
3393 // Make a switch statement.
3396 Statement::make_switch_statement(Expression* val, source_location location)
3398 return new Switch_statement(val, location);
3401 // Class Type_case_clauses::Type_case_clause.
3406 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3408 if (!this->is_default_
3409 && ((traverse->traverse_mask()
3410 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3411 && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3412 return TRAVERSE_EXIT;
3413 if (this->statements_ != NULL)
3414 return this->statements_->traverse(traverse);
3415 return TRAVERSE_CONTINUE;
3418 // Lower one clause in a type switch. Add statements to the block B.
3419 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3420 // BREAK_LABEL is the label at the end of the type switch.
3421 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3425 Type_case_clauses::Type_case_clause::lower(Block* b,
3426 Temporary_statement* descriptor_temp,
3427 Unnamed_label* break_label,
3428 Unnamed_label** stmts_label) const
3430 source_location loc = this->location_;
3432 Unnamed_label* next_case_label = NULL;
3433 if (!this->is_default_)
3435 Type* type = this->type_;
3437 Expression* ref = Expression::make_temporary_reference(descriptor_temp,
3441 // The language permits case nil, which is of course a constant
3442 // rather than a type. It will appear here as an invalid
3444 if (type->is_nil_constant_as_type())
3445 cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3446 Expression::make_nil(loc),
3449 cond = Runtime::make_call((type->interface_type() == NULL
3450 ? Runtime::IFACETYPEEQ
3451 : Runtime::IFACEI2TP),
3453 Expression::make_type_descriptor(type, loc),
3456 Unnamed_label* dest;
3457 if (!this->is_fallthrough_)
3459 // if !COND { goto NEXT_CASE_LABEL }
3460 next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
3461 dest = next_case_label;
3462 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3466 // if COND { goto STMTS_LABEL }
3467 gcc_assert(stmts_label != NULL);
3468 if (*stmts_label == NULL)
3469 *stmts_label = new Unnamed_label(UNKNOWN_LOCATION);
3470 dest = *stmts_label;
3472 Block* then_block = new Block(b, loc);
3473 Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
3474 then_block->add_statement(s);
3475 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3476 b->add_statement(s);
3479 if (this->statements_ != NULL
3480 || (!this->is_fallthrough_
3481 && stmts_label != NULL
3482 && *stmts_label != NULL))
3484 gcc_assert(!this->is_fallthrough_);
3485 if (stmts_label != NULL && *stmts_label != NULL)
3487 gcc_assert(!this->is_default_);
3488 if (this->statements_ != NULL)
3489 (*stmts_label)->set_location(this->statements_->start_location());
3490 Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
3491 b->add_statement(s);
3492 *stmts_label = NULL;
3494 if (this->statements_ != NULL)
3495 b->add_statement(Statement::make_block_statement(this->statements_,
3499 if (this->is_fallthrough_)
3500 gcc_assert(next_case_label == NULL);
3503 source_location gloc = (this->statements_ == NULL
3505 : this->statements_->end_location());
3506 b->add_statement(Statement::make_goto_unnamed_statement(break_label,
3508 if (next_case_label != NULL)
3511 Statement::make_unnamed_label_statement(next_case_label);
3512 b->add_statement(s);
3517 // Class Type_case_clauses.
3522 Type_case_clauses::traverse(Traverse* traverse)
3524 for (Type_clauses::iterator p = this->clauses_.begin();
3525 p != this->clauses_.end();
3528 if (p->traverse(traverse) == TRAVERSE_EXIT)
3529 return TRAVERSE_EXIT;
3531 return TRAVERSE_CONTINUE;
3534 // Check for duplicate types.
3537 Type_case_clauses::check_duplicates() const
3539 typedef Unordered_set_hash(const Type*, Type_hash_identical,
3540 Type_identical) Types_seen;
3541 Types_seen types_seen;
3542 for (Type_clauses::const_iterator p = this->clauses_.begin();
3543 p != this->clauses_.end();
3546 Type* t = p->type();
3549 if (t->is_nil_constant_as_type())
3550 t = Type::make_nil_type();
3551 std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
3553 error_at(p->location(), "duplicate type in switch");
3557 // Lower the clauses in a type switch. Add statements to the block B.
3558 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3559 // BREAK_LABEL is the label at the end of the type switch.
3562 Type_case_clauses::lower(Block* b, Temporary_statement* descriptor_temp,
3563 Unnamed_label* break_label) const
3565 const Type_case_clause* default_case = NULL;
3567 Unnamed_label* stmts_label = NULL;
3568 for (Type_clauses::const_iterator p = this->clauses_.begin();
3569 p != this->clauses_.end();
3572 if (!p->is_default())
3573 p->lower(b, descriptor_temp, break_label, &stmts_label);
3576 // We are generating a series of tests, which means that we
3577 // need to move the default case to the end.
3581 gcc_assert(stmts_label == NULL);
3583 if (default_case != NULL)
3584 default_case->lower(b, descriptor_temp, break_label, NULL);
3587 // Class Type_switch_statement.
3592 Type_switch_statement::do_traverse(Traverse* traverse)
3594 if (this->var_ == NULL)
3596 if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
3597 return TRAVERSE_EXIT;
3599 if (this->clauses_ != NULL)
3600 return this->clauses_->traverse(traverse);
3601 return TRAVERSE_CONTINUE;
3604 // Lower a type switch statement to a series of if statements. The gc
3605 // compiler is able to generate a table in some cases. However, that
3606 // does not work for us because we may have type descriptors in
3607 // different shared libraries, so we can't compare them with simple
3608 // equality testing.
3611 Type_switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
3613 const source_location loc = this->location();
3615 if (this->clauses_ != NULL)
3616 this->clauses_->check_duplicates();
3618 Block* b = new Block(enclosing, loc);
3620 Type* val_type = (this->var_ != NULL
3621 ? this->var_->var_value()->type()
3622 : this->expr_->type());
3624 // var descriptor_temp DESCRIPTOR_TYPE
3625 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3626 Temporary_statement* descriptor_temp =
3627 Statement::make_temporary(descriptor_type, NULL, loc);
3628 b->add_statement(descriptor_temp);
3630 if (val_type->interface_type() == NULL)
3632 // Doing a type switch on a non-interface type. Should we issue
3633 // a warning for this case?
3634 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3637 if (val_type->is_nil_type())
3638 rhs = Expression::make_nil(loc);
3641 if (val_type->is_abstract())
3642 val_type = val_type->make_non_abstract_type();
3643 rhs = Expression::make_type_descriptor(val_type, loc);
3645 Statement* s = Statement::make_assignment(lhs, rhs, loc);
3646 b->add_statement(s);
3650 // descriptor_temp = ifacetype(val_temp)
3651 // FIXME: This should be inlined.
3652 bool is_empty = val_type->interface_type()->is_empty();
3654 if (this->var_ == NULL)
3657 ref = Expression::make_var_reference(this->var_, loc);
3658 Expression* call = Runtime::make_call((is_empty
3659 ? Runtime::EFACETYPE
3660 : Runtime::IFACETYPE),
3662 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3664 Statement* s = Statement::make_assignment(lhs, call, loc);
3665 b->add_statement(s);
3668 if (this->clauses_ != NULL)
3669 this->clauses_->lower(b, descriptor_temp, this->break_label());
3671 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3672 b->add_statement(s);
3674 return Statement::make_block_statement(b, loc);
3677 // Return the break label for this type switch statement, creating it
3681 Type_switch_statement::break_label()
3683 if (this->break_label_ == NULL)
3684 this->break_label_ = new Unnamed_label(this->location());
3685 return this->break_label_;
3688 // Make a type switch statement.
3690 Type_switch_statement*
3691 Statement::make_type_switch_statement(Named_object* var, Expression* expr,
3692 source_location location)
3694 return new Type_switch_statement(var, expr, location);
3697 // Class Send_statement.
3702 Send_statement::do_traverse(Traverse* traverse)
3704 if (this->traverse_expression(traverse, &this->channel_) == TRAVERSE_EXIT)
3705 return TRAVERSE_EXIT;
3706 return this->traverse_expression(traverse, &this->val_);
3712 Send_statement::do_determine_types()
3714 this->channel_->determine_type_no_context();
3715 Type* type = this->channel_->type();
3716 Type_context context;
3717 if (type->channel_type() != NULL)
3718 context.type = type->channel_type()->element_type();
3719 this->val_->determine_type(&context);
3725 Send_statement::do_check_types(Gogo*)
3727 Type* type = this->channel_->type();
3728 if (type->is_error())
3730 this->set_is_error();
3733 Channel_type* channel_type = type->channel_type();
3734 if (channel_type == NULL)
3736 error_at(this->location(), "left operand of %<<-%> must be channel");
3737 this->set_is_error();
3740 Type* element_type = channel_type->element_type();
3741 if (!Type::are_assignable(element_type, this->val_->type(), NULL))
3743 this->report_error(_("incompatible types in send"));
3746 if (!channel_type->may_send())
3748 this->report_error(_("invalid send on receive-only channel"));
3753 // Get a tree for a send statement.
3756 Send_statement::do_get_backend(Translate_context* context)
3758 source_location loc = this->location();
3760 Channel_type* channel_type = this->channel_->type()->channel_type();
3761 Type* element_type = channel_type->element_type();
3762 Expression* val = Expression::make_cast(element_type, this->val_, loc);
3765 bool can_take_address;
3766 switch (element_type->base()->classification())
3768 case Type::TYPE_BOOLEAN:
3769 case Type::TYPE_INTEGER:
3770 case Type::TYPE_FUNCTION:
3771 case Type::TYPE_POINTER:
3772 case Type::TYPE_MAP:
3773 case Type::TYPE_CHANNEL:
3775 can_take_address = false;
3778 case Type::TYPE_FLOAT:
3779 case Type::TYPE_COMPLEX:
3780 case Type::TYPE_STRING:
3781 case Type::TYPE_INTERFACE:
3783 can_take_address = false;
3786 case Type::TYPE_STRUCT:
3788 can_take_address = true;
3791 case Type::TYPE_ARRAY:
3793 can_take_address = !element_type->is_open_array_type();
3797 case Type::TYPE_ERROR:
3798 case Type::TYPE_VOID:
3799 case Type::TYPE_SINK:
3800 case Type::TYPE_NIL:
3801 case Type::TYPE_NAMED:
3802 case Type::TYPE_FORWARD:
3803 gcc_assert(saw_errors());
3804 return context->backend()->error_statement();
3807 // Only try to take the address of a variable. We have already
3808 // moved variables to the heap, so this should not cause that to
3809 // happen unnecessarily.
3810 if (can_take_address
3811 && val->var_expression() == NULL
3812 && val->temporary_reference_expression() == NULL)
3813 can_take_address = false;
3815 Runtime::Function code;
3816 Bstatement* btemp = NULL;
3820 // Type is small enough to handle as uint64.
3821 code = Runtime::SEND_SMALL;
3822 val = Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
3825 else if (can_take_address)
3827 // Must pass address of value. The function doesn't change the
3828 // value, so just take its address directly.
3829 code = Runtime::SEND_BIG;
3830 val = Expression::make_unary(OPERATOR_AND, val, loc);
3834 // Must pass address of value, but the value is small enough
3835 // that it might be in registers. Copy value into temporary
3836 // variable to take address.
3837 code = Runtime::SEND_BIG;
3838 Temporary_statement* temp = Statement::make_temporary(element_type,
3840 Expression* ref = Expression::make_temporary_reference(temp, loc);
3841 val = Expression::make_unary(OPERATOR_AND, ref, loc);
3842 btemp = temp->get_backend(context);
3845 call = Runtime::make_call(code, loc, 3, this->channel_, val,
3846 Expression::make_boolean(this->for_select_, loc));
3848 context->gogo()->lower_expression(context->function(), &call);
3849 Bexpression* bcall = tree_to_expr(call->get_tree(context));
3850 Bstatement* s = context->backend()->expression_statement(bcall);
3855 return context->backend()->compound_statement(btemp, s);
3858 // Make a send statement.
3861 Statement::make_send_statement(Expression* channel, Expression* val,
3862 source_location location)
3864 return new Send_statement(channel, val, location);
3867 // Class Select_clauses::Select_clause.
3872 Select_clauses::Select_clause::traverse(Traverse* traverse)
3874 if (!this->is_lowered_
3875 && (traverse->traverse_mask()
3876 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3878 if (this->channel_ != NULL)
3880 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
3881 return TRAVERSE_EXIT;
3883 if (this->val_ != NULL)
3885 if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
3886 return TRAVERSE_EXIT;
3888 if (this->closed_ != NULL)
3890 if (Expression::traverse(&this->closed_, traverse) == TRAVERSE_EXIT)
3891 return TRAVERSE_EXIT;
3894 if (this->statements_ != NULL)
3896 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3897 return TRAVERSE_EXIT;
3899 return TRAVERSE_CONTINUE;
3902 // Lowering. Here we pull out the channel and the send values, to
3903 // enforce the order of evaluation. We also add explicit send and
3904 // receive statements to the clauses.
3907 Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
3910 if (this->is_default_)
3912 gcc_assert(this->channel_ == NULL && this->val_ == NULL);
3913 this->is_lowered_ = true;
3917 source_location loc = this->location_;
3919 // Evaluate the channel before the select statement.
3920 Temporary_statement* channel_temp = Statement::make_temporary(NULL,
3923 b->add_statement(channel_temp);
3924 this->channel_ = Expression::make_temporary_reference(channel_temp, loc);
3926 // If this is a send clause, evaluate the value to send before the
3927 // select statement.
3928 Temporary_statement* val_temp = NULL;
3929 if (this->is_send_ && !this->val_->is_constant())
3931 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3932 b->add_statement(val_temp);
3935 // Add the send or receive before the rest of the statements if any.
3936 Block *init = new Block(b, loc);
3937 Expression* ref = Expression::make_temporary_reference(channel_temp, loc);
3941 if (val_temp == NULL)
3944 ref2 = Expression::make_temporary_reference(val_temp, loc);
3945 Send_statement* send = Statement::make_send_statement(ref, ref2, loc);
3946 send->set_for_select();
3947 init->add_statement(send);
3949 else if (this->closed_ != NULL && !this->closed_->is_sink_expression())
3951 gcc_assert(this->var_ == NULL && this->closedvar_ == NULL);
3952 if (this->val_ == NULL)
3953 this->val_ = Expression::make_sink(loc);
3954 Statement* s = Statement::make_tuple_receive_assignment(this->val_,
3957 init->add_statement(s);
3959 else if (this->closedvar_ != NULL)
3961 gcc_assert(this->val_ == NULL);
3963 if (this->var_ == NULL)
3964 val = Expression::make_sink(loc);
3966 val = Expression::make_var_reference(this->var_, loc);
3967 Expression* closed = Expression::make_var_reference(this->closedvar_,
3969 Statement* s = Statement::make_tuple_receive_assignment(val, closed, ref,
3971 // We have to put S in STATEMENTS_, because that is where the
3972 // variables are declared.
3973 gcc_assert(this->statements_ != NULL);
3974 this->statements_->add_statement_at_front(s);
3975 // We have to lower STATEMENTS_ again, to lower the tuple
3976 // receive assignment we just added.
3977 gogo->lower_block(function, this->statements_);
3981 Receive_expression* recv = Expression::make_receive(ref, loc);
3982 recv->set_for_select();
3983 if (this->val_ != NULL)
3985 gcc_assert(this->var_ == NULL);
3986 init->add_statement(Statement::make_assignment(this->val_, recv,
3989 else if (this->var_ != NULL)
3991 this->var_->var_value()->set_init(recv);
3992 this->var_->var_value()->clear_type_from_chan_element();
3996 init->add_statement(Statement::make_statement(recv));
4000 // Lower any statements we just created.
4001 gogo->lower_block(function, init);
4003 if (this->statements_ != NULL)
4004 init->add_statement(Statement::make_block_statement(this->statements_,
4007 this->statements_ = init;
4009 // Now all references should be handled through the statements, not
4011 this->is_lowered_ = true;
4019 Select_clauses::Select_clause::determine_types()
4021 gcc_assert(this->is_lowered_);
4022 if (this->statements_ != NULL)
4023 this->statements_->determine_types();
4026 // Whether this clause may fall through to the statement which follows
4027 // the overall select statement.
4030 Select_clauses::Select_clause::may_fall_through() const
4032 if (this->statements_ == NULL)
4034 return this->statements_->may_fall_through();
4037 // Return a tree for the statements to execute.
4040 Select_clauses::Select_clause::get_statements_backend(
4041 Translate_context* context)
4043 if (this->statements_ == NULL)
4045 Bblock* bblock = this->statements_->get_backend(context);
4046 return context->backend()->block_statement(bblock);
4049 // Class Select_clauses.
4054 Select_clauses::traverse(Traverse* traverse)
4056 for (Clauses::iterator p = this->clauses_.begin();
4057 p != this->clauses_.end();
4060 if (p->traverse(traverse) == TRAVERSE_EXIT)
4061 return TRAVERSE_EXIT;
4063 return TRAVERSE_CONTINUE;
4066 // Lowering. Here we pull out the channel and the send values, to
4067 // enforce the order of evaluation. We also add explicit send and
4068 // receive statements to the clauses.
4071 Select_clauses::lower(Gogo* gogo, Named_object* function, Block* b)
4073 for (Clauses::iterator p = this->clauses_.begin();
4074 p != this->clauses_.end();
4076 p->lower(gogo, function, b);
4082 Select_clauses::determine_types()
4084 for (Clauses::iterator p = this->clauses_.begin();
4085 p != this->clauses_.end();
4087 p->determine_types();
4090 // Return whether these select clauses fall through to the statement
4091 // following the overall select statement.
4094 Select_clauses::may_fall_through() const
4096 for (Clauses::const_iterator p = this->clauses_.begin();
4097 p != this->clauses_.end();
4099 if (p->may_fall_through())
4104 // Convert to the backend representation. We build a call to
4105 // size_t __go_select(size_t count, _Bool has_default,
4106 // channel* channels, _Bool* is_send)
4108 // There are COUNT entries in the CHANNELS and IS_SEND arrays. The
4109 // value in the IS_SEND array is true for send, false for receive.
4110 // __go_select returns an integer from 0 to COUNT, inclusive. A
4111 // return of 0 means that the default case should be run; this only
4112 // happens if HAS_DEFAULT is non-zero. Otherwise the number indicates
4115 // FIXME: This doesn't handle channels which send interface types
4116 // where the receiver has a static type which matches that interface.
4119 Select_clauses::get_backend(Translate_context* context,
4120 Unnamed_label *break_label,
4121 source_location location)
4123 size_t count = this->clauses_.size();
4125 Expression_list* chan_init = new Expression_list();
4126 chan_init->reserve(count);
4128 Expression_list* is_send_init = new Expression_list();
4129 is_send_init->reserve(count);
4131 Select_clause *default_clause = NULL;
4133 Type* runtime_chanptr_type = Runtime::chanptr_type();
4134 Type* runtime_chan_type = runtime_chanptr_type->points_to();
4136 for (Clauses::iterator p = this->clauses_.begin();
4137 p != this->clauses_.end();
4140 if (p->is_default())
4142 default_clause = &*p;
4147 if (p->channel()->type()->channel_type() == NULL)
4149 // We should have given an error in the send or receive
4150 // statement we created via lowering.
4151 gcc_assert(saw_errors());
4152 return context->backend()->error_statement();
4155 Expression* c = p->channel();
4156 c = Expression::make_unsafe_cast(runtime_chan_type, c, p->location());
4157 chan_init->push_back(c);
4159 is_send_init->push_back(Expression::make_boolean(p->is_send(),
4163 if (chan_init->empty())
4165 gcc_assert(count == 0);
4167 Bstatement* ldef = break_label->get_definition(context);
4168 if (default_clause != NULL)
4170 // There is a default clause and no cases. Just execute the
4172 s = default_clause->get_statements_backend(context);
4176 // There isn't even a default clause. In this case select
4177 // pauses forever. Call the runtime function with nils.
4179 mpz_init_set_ui(zval, 0);
4180 Expression* zero = Expression::make_integer(&zval, NULL, location);
4182 Expression* default_arg = Expression::make_boolean(false, location);
4183 Expression* nil1 = Expression::make_nil(location);
4184 Expression* nil2 = nil1->copy();
4185 Expression* call = Runtime::make_call(Runtime::SELECT, location, 4,
4186 zero, default_arg, nil1, nil2);
4187 context->gogo()->lower_expression(context->function(), &call);
4188 Bexpression* bcall = tree_to_expr(call->get_tree(context));
4189 s = context->backend()->expression_statement(bcall);
4193 return context->backend()->compound_statement(s, ldef);
4195 gcc_assert(count > 0);
4197 std::vector<Bstatement*> statements;
4200 mpz_init_set_ui(ival, count);
4201 Expression* ecount = Expression::make_integer(&ival, NULL, location);
4204 Type* chan_array_type = Type::make_array_type(runtime_chan_type, ecount);
4205 Expression* chans = Expression::make_composite_literal(chan_array_type, 0,
4208 context->gogo()->lower_expression(context->function(), &chans);
4209 Temporary_statement* chan_temp = Statement::make_temporary(chan_array_type,
4212 statements.push_back(chan_temp->get_backend(context));
4214 Type* is_send_array_type = Type::make_array_type(Type::lookup_bool_type(),
4216 Expression* is_sends = Expression::make_composite_literal(is_send_array_type,
4220 context->gogo()->lower_expression(context->function(), &is_sends);
4221 Temporary_statement* is_send_temp =
4222 Statement::make_temporary(is_send_array_type, is_sends, location);
4223 statements.push_back(is_send_temp->get_backend(context));
4225 mpz_init_set_ui(ival, 0);
4226 Expression* zero = Expression::make_integer(&ival, NULL, location);
4229 Expression* ref = Expression::make_temporary_reference(chan_temp, location);
4230 Expression* chan_arg = Expression::make_array_index(ref, zero, NULL,
4232 chan_arg = Expression::make_unary(OPERATOR_AND, chan_arg, location);
4233 chan_arg = Expression::make_unsafe_cast(runtime_chanptr_type, chan_arg,
4236 ref = Expression::make_temporary_reference(is_send_temp, location);
4237 Expression* is_send_arg = Expression::make_array_index(ref, zero->copy(),
4239 is_send_arg = Expression::make_unary(OPERATOR_AND, is_send_arg, location);
4241 Expression* default_arg = Expression::make_boolean(default_clause != NULL,
4243 Expression* call = Runtime::make_call(Runtime::SELECT, location, 4,
4244 ecount->copy(), default_arg,
4245 chan_arg, is_send_arg);
4246 context->gogo()->lower_expression(context->function(), &call);
4247 Bexpression* bcall = tree_to_expr(call->get_tree(context));
4249 std::vector<std::vector<Bexpression*> > cases;
4250 std::vector<Bstatement*> clauses;
4252 cases.resize(count + (default_clause != NULL ? 1 : 0));
4253 clauses.resize(count + (default_clause != NULL ? 1 : 0));
4257 if (default_clause != NULL)
4259 this->add_clause_backend(context, location, index, 0, default_clause,
4260 break_label, &cases, &clauses);
4265 for (Clauses::iterator p = this->clauses_.begin();
4266 p != this->clauses_.end();
4269 if (!p->is_default())
4271 this->add_clause_backend(context, location, index, i, &*p,
4272 break_label, &cases, &clauses);
4278 Bstatement* switch_stmt = context->backend()->switch_statement(bcall,
4282 statements.push_back(switch_stmt);
4284 Bstatement* ldef = break_label->get_definition(context);
4285 statements.push_back(ldef);
4287 return context->backend()->statement_list(statements);
4290 // Add the tree for CLAUSE to STMT_LIST.
4293 Select_clauses::add_clause_backend(
4294 Translate_context* context,
4295 source_location location,
4298 Select_clause* clause,
4299 Unnamed_label* bottom_label,
4300 std::vector<std::vector<Bexpression*> > *cases,
4301 std::vector<Bstatement*>* clauses)
4304 mpz_init_set_ui(ival, case_value);
4305 Expression* e = Expression::make_integer(&ival, NULL, location);
4307 (*cases)[index].push_back(tree_to_expr(e->get_tree(context)));
4309 Bstatement* s = clause->get_statements_backend(context);
4311 source_location gloc = (clause->statements() == NULL
4312 ? clause->location()
4313 : clause->statements()->end_location());
4314 Bstatement* g = bottom_label->get_goto(context, gloc);
4317 (*clauses)[index] = g;
4319 (*clauses)[index] = context->backend()->compound_statement(s, g);
4322 // Class Select_statement.
4324 // Return the break label for this switch statement, creating it if
4328 Select_statement::break_label()
4330 if (this->break_label_ == NULL)
4331 this->break_label_ = new Unnamed_label(this->location());
4332 return this->break_label_;
4335 // Lower a select statement. This will still return a select
4336 // statement, but it will be modified to implement the order of
4337 // evaluation rules, and to include the send and receive statements as
4338 // explicit statements in the clauses.
4341 Select_statement::do_lower(Gogo* gogo, Named_object* function,
4344 if (this->is_lowered_)
4346 Block* b = new Block(enclosing, this->location());
4347 this->clauses_->lower(gogo, function, b);
4348 this->is_lowered_ = true;
4349 b->add_statement(this);
4350 return Statement::make_block_statement(b, this->location());
4353 // Return the tree for a select statement.
4356 Select_statement::do_get_backend(Translate_context* context)
4358 return this->clauses_->get_backend(context, this->break_label(),
4362 // Make a select statement.
4365 Statement::make_select_statement(source_location location)
4367 return new Select_statement(location);
4370 // Class For_statement.
4375 For_statement::do_traverse(Traverse* traverse)
4377 if (this->init_ != NULL)
4379 if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
4380 return TRAVERSE_EXIT;
4382 if (this->cond_ != NULL)
4384 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
4385 return TRAVERSE_EXIT;
4387 if (this->post_ != NULL)
4389 if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
4390 return TRAVERSE_EXIT;
4392 return this->statements_->traverse(traverse);
4395 // Lower a For_statement into if statements and gotos. Getting rid of
4396 // complex statements make it easier to handle garbage collection.
4399 For_statement::do_lower(Gogo*, Named_object*, Block* enclosing)
4402 source_location loc = this->location();
4404 Block* b = new Block(enclosing, this->location());
4405 if (this->init_ != NULL)
4407 s = Statement::make_block_statement(this->init_,
4408 this->init_->start_location());
4409 b->add_statement(s);
4412 Unnamed_label* entry = NULL;
4413 if (this->cond_ != NULL)
4415 entry = new Unnamed_label(this->location());
4416 b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
4419 Unnamed_label* top = new Unnamed_label(this->location());
4420 b->add_statement(Statement::make_unnamed_label_statement(top));
4422 s = Statement::make_block_statement(this->statements_,
4423 this->statements_->start_location());
4424 b->add_statement(s);
4426 source_location end_loc = this->statements_->end_location();
4428 Unnamed_label* cont = this->continue_label_;
4430 b->add_statement(Statement::make_unnamed_label_statement(cont));
4432 if (this->post_ != NULL)
4434 s = Statement::make_block_statement(this->post_,
4435 this->post_->start_location());
4436 b->add_statement(s);
4437 end_loc = this->post_->end_location();
4440 if (this->cond_ == NULL)
4441 b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
4444 b->add_statement(Statement::make_unnamed_label_statement(entry));
4446 source_location cond_loc = this->cond_->location();
4447 Block* then_block = new Block(b, cond_loc);
4448 s = Statement::make_goto_unnamed_statement(top, cond_loc);
4449 then_block->add_statement(s);
4451 s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
4452 b->add_statement(s);
4455 Unnamed_label* brk = this->break_label_;
4457 b->add_statement(Statement::make_unnamed_label_statement(brk));
4459 b->set_end_location(end_loc);
4461 return Statement::make_block_statement(b, loc);
4464 // Return the break label, creating it if necessary.
4467 For_statement::break_label()
4469 if (this->break_label_ == NULL)
4470 this->break_label_ = new Unnamed_label(this->location());
4471 return this->break_label_;
4474 // Return the continue LABEL_EXPR.
4477 For_statement::continue_label()
4479 if (this->continue_label_ == NULL)
4480 this->continue_label_ = new Unnamed_label(this->location());
4481 return this->continue_label_;
4484 // Set the break and continue labels a for statement. This is used
4485 // when lowering a for range statement.
4488 For_statement::set_break_continue_labels(Unnamed_label* break_label,
4489 Unnamed_label* continue_label)
4491 gcc_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
4492 this->break_label_ = break_label;
4493 this->continue_label_ = continue_label;
4496 // Make a for statement.
4499 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
4500 source_location location)
4502 return new For_statement(init, cond, post, location);
4505 // Class For_range_statement.
4510 For_range_statement::do_traverse(Traverse* traverse)
4512 if (this->traverse_expression(traverse, &this->index_var_) == TRAVERSE_EXIT)
4513 return TRAVERSE_EXIT;
4514 if (this->value_var_ != NULL)
4516 if (this->traverse_expression(traverse, &this->value_var_)
4518 return TRAVERSE_EXIT;
4520 if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
4521 return TRAVERSE_EXIT;
4522 return this->statements_->traverse(traverse);
4525 // Lower a for range statement. For simplicity we lower this into a
4526 // for statement, which will then be lowered in turn to goto
4530 For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing)
4532 Type* range_type = this->range_->type();
4533 if (range_type->points_to() != NULL
4534 && range_type->points_to()->array_type() != NULL
4535 && !range_type->points_to()->is_open_array_type())
4536 range_type = range_type->points_to();
4539 Type* value_type = NULL;
4540 if (range_type->array_type() != NULL)
4542 index_type = Type::lookup_integer_type("int");
4543 value_type = range_type->array_type()->element_type();
4545 else if (range_type->is_string_type())
4547 index_type = Type::lookup_integer_type("int");
4548 value_type = index_type;
4550 else if (range_type->map_type() != NULL)
4552 index_type = range_type->map_type()->key_type();
4553 value_type = range_type->map_type()->val_type();
4555 else if (range_type->channel_type() != NULL)
4557 index_type = range_type->channel_type()->element_type();
4558 if (this->value_var_ != NULL)
4560 if (!this->value_var_->type()->is_error())
4561 this->report_error(_("too many variables for range clause "
4563 return Statement::make_error_statement(this->location());
4568 this->report_error(_("range clause must have "
4569 "array, slice, setring, map, or channel type"));
4570 return Statement::make_error_statement(this->location());
4573 source_location loc = this->location();
4574 Block* temp_block = new Block(enclosing, loc);
4576 Named_object* range_object = NULL;
4577 Temporary_statement* range_temp = NULL;
4578 Var_expression* ve = this->range_->var_expression();
4580 range_object = ve->named_object();
4583 range_temp = Statement::make_temporary(NULL, this->range_, loc);
4584 temp_block->add_statement(range_temp);
4587 Temporary_statement* index_temp = Statement::make_temporary(index_type,
4589 temp_block->add_statement(index_temp);
4591 Temporary_statement* value_temp = NULL;
4592 if (this->value_var_ != NULL)
4594 value_temp = Statement::make_temporary(value_type, NULL, loc);
4595 temp_block->add_statement(value_temp);
4598 Block* body = new Block(temp_block, loc);
4605 // Arrange to do a loop appropriate for the type. We will produce
4606 // for INIT ; COND ; POST {
4608 // INDEX = INDEX_TEMP
4609 // VALUE = VALUE_TEMP // If there is a value
4610 // original statements
4613 if (range_type->array_type() != NULL)
4614 this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
4615 index_temp, value_temp, &init, &cond, &iter_init,
4617 else if (range_type->is_string_type())
4618 this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
4619 index_temp, value_temp, &init, &cond, &iter_init,
4621 else if (range_type->map_type() != NULL)
4622 this->lower_range_map(gogo, temp_block, body, range_object, range_temp,
4623 index_temp, value_temp, &init, &cond, &iter_init,
4625 else if (range_type->channel_type() != NULL)
4626 this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
4627 index_temp, value_temp, &init, &cond, &iter_init,
4632 if (iter_init != NULL)
4633 body->add_statement(Statement::make_block_statement(iter_init, loc));
4636 Expression* index_ref = Expression::make_temporary_reference(index_temp, loc);
4637 if (this->value_var_ == NULL)
4639 assign = Statement::make_assignment(this->index_var_, index_ref, loc);
4643 Expression_list* lhs = new Expression_list();
4644 lhs->push_back(this->index_var_);
4645 lhs->push_back(this->value_var_);
4647 Expression_list* rhs = new Expression_list();
4648 rhs->push_back(index_ref);
4649 rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4651 assign = Statement::make_tuple_assignment(lhs, rhs, loc);
4653 body->add_statement(assign);
4655 body->add_statement(Statement::make_block_statement(this->statements_, loc));
4657 body->set_end_location(this->statements_->end_location());
4659 For_statement* loop = Statement::make_for_statement(init, cond, post,
4661 loop->add_statements(body);
4662 loop->set_break_continue_labels(this->break_label_, this->continue_label_);
4664 temp_block->add_statement(loop);
4666 return Statement::make_block_statement(temp_block, loc);
4669 // Return a reference to the range, which may be in RANGE_OBJECT or in
4673 For_range_statement::make_range_ref(Named_object* range_object,
4674 Temporary_statement* range_temp,
4675 source_location loc)
4677 if (range_object != NULL)
4678 return Expression::make_var_reference(range_object, loc);
4680 return Expression::make_temporary_reference(range_temp, loc);
4683 // Return a call to the predeclared function FUNCNAME passing a
4684 // reference to the temporary variable ARG.
4687 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
4689 source_location loc)
4691 Named_object* no = gogo->lookup_global(funcname);
4692 gcc_assert(no != NULL && no->is_function_declaration());
4693 Expression* func = Expression::make_func_reference(no, NULL, loc);
4694 Expression_list* params = new Expression_list();
4695 params->push_back(arg);
4696 return Expression::make_call(func, params, false, loc);
4699 // Lower a for range over an array or slice.
4702 For_range_statement::lower_range_array(Gogo* gogo,
4705 Named_object* range_object,
4706 Temporary_statement* range_temp,
4707 Temporary_statement* index_temp,
4708 Temporary_statement* value_temp,
4714 source_location loc = this->location();
4716 // The loop we generate:
4717 // len_temp := len(range)
4718 // for index_temp = 0; index_temp < len_temp; index_temp++ {
4719 // value_temp = range[index_temp]
4720 // index = index_temp
4721 // value = value_temp
4727 // len_temp = len(range)
4730 Block* init = new Block(enclosing, loc);
4732 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
4733 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
4734 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
4736 init->add_statement(len_temp);
4739 mpz_init_set_ui(zval, 0UL);
4740 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4743 ref = Expression::make_temporary_reference(index_temp, loc);
4744 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4745 init->add_statement(s);
4750 // index_temp < len_temp
4752 ref = Expression::make_temporary_reference(index_temp, loc);
4753 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
4754 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
4758 // Set *PITER_INIT to
4759 // value_temp = range[index_temp]
4761 Block* iter_init = NULL;
4762 if (value_temp != NULL)
4764 iter_init = new Block(body_block, loc);
4766 ref = this->make_range_ref(range_object, range_temp, loc);
4767 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
4768 Expression* index = Expression::make_index(ref, ref2, NULL, loc);
4770 ref = Expression::make_temporary_reference(value_temp, loc);
4771 s = Statement::make_assignment(ref, index, loc);
4773 iter_init->add_statement(s);
4775 *piter_init = iter_init;
4780 Block* post = new Block(enclosing, loc);
4781 ref = Expression::make_temporary_reference(index_temp, loc);
4782 s = Statement::make_inc_statement(ref);
4783 post->add_statement(s);
4787 // Lower a for range over a string.
4790 For_range_statement::lower_range_string(Gogo*,
4793 Named_object* range_object,
4794 Temporary_statement* range_temp,
4795 Temporary_statement* index_temp,
4796 Temporary_statement* value_temp,
4802 source_location loc = this->location();
4804 // The loop we generate:
4805 // var next_index_temp int
4806 // for index_temp = 0; ; index_temp = next_index_temp {
4807 // next_index_temp, value_temp = stringiter2(range, index_temp)
4808 // if next_index_temp == 0 {
4811 // index = index_temp
4812 // value = value_temp
4817 // var next_index_temp int
4820 Block* init = new Block(enclosing, loc);
4822 Temporary_statement* next_index_temp =
4823 Statement::make_temporary(index_temp->type(), NULL, loc);
4824 init->add_statement(next_index_temp);
4827 mpz_init_set_ui(zval, 0UL);
4828 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4830 Expression* ref = Expression::make_temporary_reference(index_temp, loc);
4831 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4833 init->add_statement(s);
4836 // The loop has no condition.
4840 // Set *PITER_INIT to
4841 // next_index_temp = runtime.stringiter(range, index_temp)
4843 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
4845 // if next_index_temp == 0 {
4849 Block* iter_init = new Block(body_block, loc);
4851 Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
4852 Expression* p2 = Expression::make_temporary_reference(index_temp, loc);
4853 Call_expression* call = Runtime::make_call((value_temp == NULL
4854 ? Runtime::STRINGITER
4855 : Runtime::STRINGITER2),
4858 if (value_temp == NULL)
4860 ref = Expression::make_temporary_reference(next_index_temp, loc);
4861 s = Statement::make_assignment(ref, call, loc);
4865 Expression_list* lhs = new Expression_list();
4866 lhs->push_back(Expression::make_temporary_reference(next_index_temp,
4868 lhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4870 Expression_list* rhs = new Expression_list();
4871 rhs->push_back(Expression::make_call_result(call, 0));
4872 rhs->push_back(Expression::make_call_result(call, 1));
4874 s = Statement::make_tuple_assignment(lhs, rhs, loc);
4876 iter_init->add_statement(s);
4878 ref = Expression::make_temporary_reference(next_index_temp, loc);
4879 zexpr = Expression::make_integer(&zval, NULL, loc);
4881 Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
4883 Block* then_block = new Block(iter_init, loc);
4884 s = Statement::make_break_statement(this->break_label(), loc);
4885 then_block->add_statement(s);
4887 s = Statement::make_if_statement(equals, then_block, NULL, loc);
4888 iter_init->add_statement(s);
4890 *piter_init = iter_init;
4893 // index_temp = next_index_temp
4895 Block* post = new Block(enclosing, loc);
4897 Expression* lhs = Expression::make_temporary_reference(index_temp, loc);
4898 Expression* rhs = Expression::make_temporary_reference(next_index_temp, loc);
4899 s = Statement::make_assignment(lhs, rhs, loc);
4901 post->add_statement(s);
4905 // Lower a for range over a map.
4908 For_range_statement::lower_range_map(Gogo*,
4911 Named_object* range_object,
4912 Temporary_statement* range_temp,
4913 Temporary_statement* index_temp,
4914 Temporary_statement* value_temp,
4920 source_location loc = this->location();
4922 // The runtime uses a struct to handle ranges over a map. The
4923 // struct is four pointers long. The first pointer is NULL when we
4924 // have completed the iteration.
4926 // The loop we generate:
4927 // var hiter map_iteration_struct
4928 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
4929 // mapiter2(hiter, &index_temp, &value_temp)
4930 // index = index_temp
4931 // value = value_temp
4936 // var hiter map_iteration_struct
4937 // runtime.mapiterinit(range, &hiter)
4939 Block* init = new Block(enclosing, loc);
4941 Type* map_iteration_type = Runtime::map_iteration_type();
4942 Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
4944 init->add_statement(hiter);
4946 Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
4947 Expression* ref = Expression::make_temporary_reference(hiter, loc);
4948 Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
4949 Expression* call = Runtime::make_call(Runtime::MAPITERINIT, loc, 2, p1, p2);
4950 init->add_statement(Statement::make_statement(call));
4957 ref = Expression::make_temporary_reference(hiter, loc);
4960 mpz_init_set_ui(zval, 0UL);
4961 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4964 Expression* index = Expression::make_index(ref, zexpr, NULL, loc);
4966 Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
4967 Expression::make_nil(loc),
4972 // Set *PITER_INIT to
4973 // mapiter1(hiter, &index_temp)
4975 // mapiter2(hiter, &index_temp, &value_temp)
4977 Block* iter_init = new Block(body_block, loc);
4979 ref = Expression::make_temporary_reference(hiter, loc);
4980 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
4981 ref = Expression::make_temporary_reference(index_temp, loc);
4982 p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
4983 if (value_temp == NULL)
4984 call = Runtime::make_call(Runtime::MAPITER1, loc, 2, p1, p2);
4987 ref = Expression::make_temporary_reference(value_temp, loc);
4988 Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
4989 call = Runtime::make_call(Runtime::MAPITER2, loc, 3, p1, p2, p3);
4991 iter_init->add_statement(Statement::make_statement(call));
4993 *piter_init = iter_init;
4996 // mapiternext(&hiter)
4998 Block* post = new Block(enclosing, loc);
5000 ref = Expression::make_temporary_reference(hiter, loc);
5001 p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
5002 call = Runtime::make_call(Runtime::MAPITERNEXT, loc, 1, p1);
5003 post->add_statement(Statement::make_statement(call));
5008 // Lower a for range over a channel.
5011 For_range_statement::lower_range_channel(Gogo*,
5014 Named_object* range_object,
5015 Temporary_statement* range_temp,
5016 Temporary_statement* index_temp,
5017 Temporary_statement* value_temp,
5023 gcc_assert(value_temp == NULL);
5025 source_location loc = this->location();
5027 // The loop we generate:
5029 // index_temp, ok_temp = <-range
5033 // index = index_temp
5037 // We have no initialization code, no condition, and no post code.
5043 // Set *PITER_INIT to
5044 // index_temp, ok_temp = <-range
5049 Block* iter_init = new Block(body_block, loc);
5051 Temporary_statement* ok_temp =
5052 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
5053 iter_init->add_statement(ok_temp);
5055 Expression* cref = this->make_range_ref(range_object, range_temp, loc);
5056 Expression* iref = Expression::make_temporary_reference(index_temp, loc);
5057 Expression* oref = Expression::make_temporary_reference(ok_temp, loc);
5058 Statement* s = Statement::make_tuple_receive_assignment(iref, oref, cref,
5060 iter_init->add_statement(s);
5062 Block* then_block = new Block(iter_init, loc);
5063 s = Statement::make_break_statement(this->break_label(), loc);
5064 then_block->add_statement(s);
5066 oref = Expression::make_temporary_reference(ok_temp, loc);
5067 Expression* cond = Expression::make_unary(OPERATOR_NOT, oref, loc);
5068 s = Statement::make_if_statement(cond, then_block, NULL, loc);
5069 iter_init->add_statement(s);
5071 *piter_init = iter_init;
5074 // Return the break LABEL_EXPR.
5077 For_range_statement::break_label()
5079 if (this->break_label_ == NULL)
5080 this->break_label_ = new Unnamed_label(this->location());
5081 return this->break_label_;
5084 // Return the continue LABEL_EXPR.
5087 For_range_statement::continue_label()
5089 if (this->continue_label_ == NULL)
5090 this->continue_label_ = new Unnamed_label(this->location());
5091 return this->continue_label_;
5094 // Make a for statement with a range clause.
5096 For_range_statement*
5097 Statement::make_for_range_statement(Expression* index_var,
5098 Expression* value_var,
5100 source_location location)
5102 return new For_range_statement(index_var, value_var, range, location);