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"
32 #include "statements.h"
36 Statement::Statement(Statement_classification classification,
37 source_location location)
38 : classification_(classification), location_(location)
42 Statement::~Statement()
46 // Traverse the tree. The work of walking the components is handled
50 Statement::traverse(Block* block, size_t* pindex, Traverse* traverse)
52 if (this->classification_ == STATEMENT_ERROR)
53 return TRAVERSE_CONTINUE;
55 unsigned int traverse_mask = traverse->traverse_mask();
57 if ((traverse_mask & Traverse::traverse_statements) != 0)
59 int t = traverse->statement(block, pindex, this);
60 if (t == TRAVERSE_EXIT)
62 else if (t == TRAVERSE_SKIP_COMPONENTS)
63 return TRAVERSE_CONTINUE;
66 // No point in checking traverse_mask here--a statement may contain
67 // other blocks or statements, and if we got here we always want to
69 return this->do_traverse(traverse);
72 // Traverse the contents of a statement.
75 Statement::traverse_contents(Traverse* traverse)
77 return this->do_traverse(traverse);
80 // Traverse assignments.
83 Statement::traverse_assignments(Traverse_assignments* tassign)
85 if (this->classification_ == STATEMENT_ERROR)
87 return this->do_traverse_assignments(tassign);
90 // Traverse an expression in a statement. This is a helper function
94 Statement::traverse_expression(Traverse* traverse, Expression** expr)
96 if ((traverse->traverse_mask()
97 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
98 return TRAVERSE_CONTINUE;
99 return Expression::traverse(expr, traverse);
102 // Traverse an expression list in a statement. This is a helper
103 // function for child classes.
106 Statement::traverse_expression_list(Traverse* traverse,
107 Expression_list* expr_list)
109 if (expr_list == NULL)
110 return TRAVERSE_CONTINUE;
111 if ((traverse->traverse_mask()
112 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
113 return TRAVERSE_CONTINUE;
114 return expr_list->traverse(traverse);
117 // Traverse a type in a statement. This is a helper function for
121 Statement::traverse_type(Traverse* traverse, Type* type)
123 if ((traverse->traverse_mask()
124 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
125 return TRAVERSE_CONTINUE;
126 return Type::traverse(type, traverse);
129 // Set type information for unnamed constants. This is really done by
133 Statement::determine_types()
135 this->do_determine_types();
138 // If this is a thunk statement, return it.
141 Statement::thunk_statement()
143 Thunk_statement* ret = this->convert<Thunk_statement, STATEMENT_GO>();
145 ret = this->convert<Thunk_statement, STATEMENT_DEFER>();
149 // Get a tree for a Statement. This is really done by the child
153 Statement::get_tree(Translate_context* context)
155 if (this->classification_ == STATEMENT_ERROR)
156 return error_mark_node;
158 return this->do_get_tree(context);
161 // Build tree nodes and set locations.
164 Statement::build_stmt_1(int tree_code_value, tree node)
166 tree ret = build1(static_cast<tree_code>(tree_code_value),
167 void_type_node, node);
168 SET_EXPR_LOCATION(ret, this->location_);
172 // Note that this statement is erroneous. This is called by children
173 // when they discover an error.
176 Statement::set_is_error()
178 this->classification_ = STATEMENT_ERROR;
181 // For children to call to report an error conveniently.
184 Statement::report_error(const char* msg)
186 error_at(this->location_, "%s", msg);
187 this->set_is_error();
190 // An error statement, used to avoid crashing after we report an
193 class Error_statement : public Statement
196 Error_statement(source_location location)
197 : Statement(STATEMENT_ERROR, location)
202 do_traverse(Traverse*)
203 { return TRAVERSE_CONTINUE; }
206 do_get_tree(Translate_context*)
207 { gcc_unreachable(); }
210 // Make an error statement.
213 Statement::make_error_statement(source_location location)
215 return new Error_statement(location);
218 // Class Variable_declaration_statement.
220 Variable_declaration_statement::Variable_declaration_statement(
222 : Statement(STATEMENT_VARIABLE_DECLARATION, var->var_value()->location()),
227 // We don't actually traverse the variable here; it was traversed
228 // while traversing the Block.
231 Variable_declaration_statement::do_traverse(Traverse*)
233 return TRAVERSE_CONTINUE;
236 // Traverse the assignments in a variable declaration. Note that this
237 // traversal is different from the usual traversal.
240 Variable_declaration_statement::do_traverse_assignments(
241 Traverse_assignments* tassign)
243 tassign->initialize_variable(this->var_);
247 // Return the tree for a variable declaration.
250 Variable_declaration_statement::do_get_tree(Translate_context* context)
252 tree val = this->var_->get_tree(context->gogo(), context->function());
253 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
254 return error_mark_node;
255 Variable* variable = this->var_->var_value();
257 tree init = variable->get_init_tree(context->gogo(), context->function());
258 if (init == error_mark_node)
259 return error_mark_node;
261 // If this variable lives on the heap, we need to allocate it now.
262 if (!variable->is_in_heap())
264 DECL_INITIAL(val) = init;
265 return this->build_stmt_1(DECL_EXPR, val);
269 gcc_assert(TREE_CODE(val) == INDIRECT_REF);
270 tree decl = TREE_OPERAND(val, 0);
271 gcc_assert(TREE_CODE(decl) == VAR_DECL);
272 tree type = TREE_TYPE(decl);
273 gcc_assert(POINTER_TYPE_P(type));
274 tree size = TYPE_SIZE_UNIT(TREE_TYPE(type));
275 tree space = context->gogo()->allocate_memory(variable->type(), size,
277 space = fold_convert(TREE_TYPE(decl), space);
278 DECL_INITIAL(decl) = space;
279 return build2(COMPOUND_EXPR, void_type_node,
280 this->build_stmt_1(DECL_EXPR, decl),
281 build2(MODIFY_EXPR, void_type_node, val, init));
285 // Make a variable declaration.
288 Statement::make_variable_declaration(Named_object* var)
290 return new Variable_declaration_statement(var);
293 // Class Temporary_statement.
295 // Return the type of the temporary variable.
298 Temporary_statement::type() const
300 return this->type_ != NULL ? this->type_ : this->init_->type();
303 // Return the tree for the temporary variable.
306 Temporary_statement::get_decl() const
308 if (this->decl_ == NULL)
310 gcc_assert(saw_errors());
311 return error_mark_node;
319 Temporary_statement::do_traverse(Traverse* traverse)
321 if (this->init_ == NULL)
322 return TRAVERSE_CONTINUE;
324 return this->traverse_expression(traverse, &this->init_);
327 // Traverse assignments.
330 Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
332 if (this->init_ == NULL)
334 tassign->value(&this->init_, true, true);
341 Temporary_statement::do_determine_types()
343 if (this->type_ != NULL && this->type_->is_abstract())
344 this->type_ = this->type_->make_non_abstract_type();
346 if (this->init_ != NULL)
348 if (this->type_ == NULL)
349 this->init_->determine_type_no_context();
352 Type_context context(this->type_, false);
353 this->init_->determine_type(&context);
357 if (this->type_ == NULL)
359 this->type_ = this->init_->type();
360 gcc_assert(!this->type_->is_abstract());
367 Temporary_statement::do_check_types(Gogo*)
369 if (this->type_ != NULL && this->init_ != NULL)
372 if (!Type::are_assignable(this->type_, this->init_->type(), &reason))
375 error_at(this->location(), "incompatible types in assignment");
377 error_at(this->location(), "incompatible types in assignment (%s)",
379 this->set_is_error();
387 Temporary_statement::do_get_tree(Translate_context* context)
389 gcc_assert(this->decl_ == NULL_TREE);
390 tree type_tree = this->type()->get_tree(context->gogo());
391 if (type_tree == error_mark_node)
393 this->decl_ = error_mark_node;
394 return error_mark_node;
396 // We can only use create_tmp_var if the type is not addressable.
397 if (!TREE_ADDRESSABLE(type_tree))
399 this->decl_ = create_tmp_var(type_tree, "GOTMP");
400 DECL_SOURCE_LOCATION(this->decl_) = this->location();
404 gcc_assert(context->function() != NULL && context->block() != NULL);
405 tree decl = build_decl(this->location(), VAR_DECL,
406 create_tmp_var_name("GOTMP"),
408 DECL_ARTIFICIAL(decl) = 1;
409 DECL_IGNORED_P(decl) = 1;
411 gcc_assert(current_function_decl != NULL_TREE);
412 DECL_CONTEXT(decl) = current_function_decl;
414 // We have to add this variable to the block so that it winds up
416 tree block_tree = context->block_tree();
417 gcc_assert(block_tree != NULL_TREE);
418 DECL_CHAIN(decl) = BLOCK_VARS(block_tree);
419 BLOCK_VARS(block_tree) = decl;
423 if (this->init_ != NULL)
424 DECL_INITIAL(this->decl_) =
425 Expression::convert_for_assignment(context, this->type(),
427 this->init_->get_tree(context),
429 if (this->is_address_taken_)
430 TREE_ADDRESSABLE(this->decl_) = 1;
431 return this->build_stmt_1(DECL_EXPR, this->decl_);
434 // Make and initialize a temporary variable in BLOCK.
437 Statement::make_temporary(Type* type, Expression* init,
438 source_location location)
440 return new Temporary_statement(type, init, location);
443 // An assignment statement.
445 class Assignment_statement : public Statement
448 Assignment_statement(Expression* lhs, Expression* rhs,
449 source_location location)
450 : Statement(STATEMENT_ASSIGNMENT, location),
456 do_traverse(Traverse* traverse);
459 do_traverse_assignments(Traverse_assignments*);
462 do_determine_types();
465 do_check_types(Gogo*);
468 do_get_tree(Translate_context*);
471 // Left hand side--the lvalue.
473 // Right hand side--the rvalue.
480 Assignment_statement::do_traverse(Traverse* traverse)
482 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
483 return TRAVERSE_EXIT;
484 return this->traverse_expression(traverse, &this->rhs_);
488 Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
490 tassign->assignment(&this->lhs_, &this->rhs_);
494 // Set types for the assignment.
497 Assignment_statement::do_determine_types()
499 this->lhs_->determine_type_no_context();
500 Type_context context(this->lhs_->type(), false);
501 this->rhs_->determine_type(&context);
504 // Check types for an assignment.
507 Assignment_statement::do_check_types(Gogo*)
509 // The left hand side must be either addressable, a map index
510 // expression, or the blank identifier.
511 if (!this->lhs_->is_addressable()
512 && this->lhs_->map_index_expression() == NULL
513 && !this->lhs_->is_sink_expression())
515 if (!this->lhs_->type()->is_error_type())
516 this->report_error(_("invalid left hand side of assignment"));
520 Type* lhs_type = this->lhs_->type();
521 Type* rhs_type = this->rhs_->type();
523 if (!Type::are_assignable(lhs_type, rhs_type, &reason))
526 error_at(this->location(), "incompatible types in assignment");
528 error_at(this->location(), "incompatible types in assignment (%s)",
530 this->set_is_error();
533 if (lhs_type->is_error_type()
534 || rhs_type->is_error_type()
535 || lhs_type->is_undefined()
536 || rhs_type->is_undefined())
538 // Make sure we get the error for an undefined type.
541 this->set_is_error();
545 // Build a tree for an assignment statement.
548 Assignment_statement::do_get_tree(Translate_context* context)
550 tree rhs_tree = this->rhs_->get_tree(context);
552 if (this->lhs_->is_sink_expression())
555 tree lhs_tree = this->lhs_->get_tree(context);
557 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
558 return error_mark_node;
560 rhs_tree = Expression::convert_for_assignment(context, this->lhs_->type(),
561 this->rhs_->type(), rhs_tree,
563 if (rhs_tree == error_mark_node)
564 return error_mark_node;
566 return fold_build2_loc(this->location(), MODIFY_EXPR, void_type_node,
570 // Make an assignment statement.
573 Statement::make_assignment(Expression* lhs, Expression* rhs,
574 source_location location)
576 return new Assignment_statement(lhs, rhs, location);
579 // The Move_ordered_evals class is used to find any subexpressions of
580 // an expression that have an evaluation order dependency. It creates
581 // temporary variables to hold them.
583 class Move_ordered_evals : public Traverse
586 Move_ordered_evals(Block* block)
587 : Traverse(traverse_expressions),
593 expression(Expression**);
596 // The block where new temporary variables should be added.
601 Move_ordered_evals::expression(Expression** pexpr)
603 // We have to look at subexpressions first.
604 if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
605 return TRAVERSE_EXIT;
606 if ((*pexpr)->must_eval_in_order())
608 source_location loc = (*pexpr)->location();
609 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
610 this->block_->add_statement(temp);
611 *pexpr = Expression::make_temporary_reference(temp, loc);
613 return TRAVERSE_SKIP_COMPONENTS;
616 // An assignment operation statement.
618 class Assignment_operation_statement : public Statement
621 Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
622 source_location location)
623 : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
624 op_(op), lhs_(lhs), rhs_(rhs)
629 do_traverse(Traverse*);
632 do_traverse_assignments(Traverse_assignments*)
633 { gcc_unreachable(); }
636 do_lower(Gogo*, Block*);
639 do_get_tree(Translate_context*)
640 { gcc_unreachable(); }
643 // The operator (OPERATOR_PLUSEQ, etc.).
654 Assignment_operation_statement::do_traverse(Traverse* traverse)
656 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
657 return TRAVERSE_EXIT;
658 return this->traverse_expression(traverse, &this->rhs_);
661 // Lower an assignment operation statement to a regular assignment
665 Assignment_operation_statement::do_lower(Gogo*, Block* enclosing)
667 source_location loc = this->location();
669 // We have to evaluate the left hand side expression only once. We
670 // do this by moving out any expression with side effects.
671 Block* b = new Block(enclosing, loc);
672 Move_ordered_evals moe(b);
673 this->lhs_->traverse_subexpressions(&moe);
675 Expression* lval = this->lhs_->copy();
680 case OPERATOR_PLUSEQ:
683 case OPERATOR_MINUSEQ:
692 case OPERATOR_MULTEQ:
701 case OPERATOR_LSHIFTEQ:
702 op = OPERATOR_LSHIFT;
704 case OPERATOR_RSHIFTEQ:
705 op = OPERATOR_RSHIFT;
710 case OPERATOR_BITCLEAREQ:
711 op = OPERATOR_BITCLEAR;
717 Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
718 Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
719 if (b->statements()->empty())
727 return Statement::make_block_statement(b, loc);
731 // Make an assignment operation statement.
734 Statement::make_assignment_operation(Operator op, Expression* lhs,
735 Expression* rhs, source_location location)
737 return new Assignment_operation_statement(op, lhs, rhs, location);
740 // A tuple assignment statement. This differs from an assignment
741 // statement in that the right-hand-side expressions are evaluated in
744 class Tuple_assignment_statement : public Statement
747 Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
748 source_location location)
749 : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
755 do_traverse(Traverse* traverse);
758 do_traverse_assignments(Traverse_assignments*)
759 { gcc_unreachable(); }
762 do_lower(Gogo*, Block*);
765 do_get_tree(Translate_context*)
766 { gcc_unreachable(); }
769 // Left hand side--a list of lvalues.
770 Expression_list* lhs_;
771 // Right hand side--a list of rvalues.
772 Expression_list* rhs_;
778 Tuple_assignment_statement::do_traverse(Traverse* traverse)
780 if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
781 return TRAVERSE_EXIT;
782 return this->traverse_expression_list(traverse, this->rhs_);
785 // Lower a tuple assignment. We use temporary variables to split it
786 // up into a set of single assignments.
789 Tuple_assignment_statement::do_lower(Gogo*, Block* enclosing)
791 source_location loc = this->location();
793 Block* b = new Block(enclosing, loc);
795 // First move out any subexpressions on the left hand side. The
796 // right hand side will be evaluated in the required order anyhow.
797 Move_ordered_evals moe(b);
798 for (Expression_list::const_iterator plhs = this->lhs_->begin();
799 plhs != this->lhs_->end();
801 (*plhs)->traverse_subexpressions(&moe);
803 std::vector<Temporary_statement*> temps;
804 temps.reserve(this->lhs_->size());
806 Expression_list::const_iterator prhs = this->rhs_->begin();
807 for (Expression_list::const_iterator plhs = this->lhs_->begin();
808 plhs != this->lhs_->end();
811 gcc_assert(prhs != this->rhs_->end());
813 if ((*plhs)->is_error_expression()
814 || (*plhs)->type()->is_error_type()
815 || (*prhs)->is_error_expression()
816 || (*prhs)->type()->is_error_type())
819 if ((*plhs)->is_sink_expression())
821 b->add_statement(Statement::make_statement(*prhs));
825 Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
827 b->add_statement(temp);
828 temps.push_back(temp);
831 gcc_assert(prhs == this->rhs_->end());
833 prhs = this->rhs_->begin();
834 std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
835 for (Expression_list::const_iterator plhs = this->lhs_->begin();
836 plhs != this->lhs_->end();
839 if ((*plhs)->is_error_expression()
840 || (*plhs)->type()->is_error_type()
841 || (*prhs)->is_error_expression()
842 || (*prhs)->type()->is_error_type())
845 if ((*plhs)->is_sink_expression())
848 Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
849 Statement* s = Statement::make_assignment(*plhs, ref, loc);
853 gcc_assert(ptemp == temps.end());
855 return Statement::make_block_statement(b, loc);
858 // Make a tuple assignment statement.
861 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
862 source_location location)
864 return new Tuple_assignment_statement(lhs, rhs, location);
867 // A tuple assignment from a map index expression.
870 class Tuple_map_assignment_statement : public Statement
873 Tuple_map_assignment_statement(Expression* val, Expression* present,
874 Expression* map_index,
875 source_location location)
876 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
877 val_(val), present_(present), map_index_(map_index)
882 do_traverse(Traverse* traverse);
885 do_traverse_assignments(Traverse_assignments*)
886 { gcc_unreachable(); }
889 do_lower(Gogo*, Block*);
892 do_get_tree(Translate_context*)
893 { gcc_unreachable(); }
896 // Lvalue which receives the value from the map.
898 // Lvalue which receives whether the key value was present.
899 Expression* present_;
900 // The map index expression.
901 Expression* map_index_;
907 Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
909 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
910 || this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
911 return TRAVERSE_EXIT;
912 return this->traverse_expression(traverse, &this->map_index_);
915 // Lower a tuple map assignment.
918 Tuple_map_assignment_statement::do_lower(Gogo*, Block* enclosing)
920 source_location loc = this->location();
922 Map_index_expression* map_index = this->map_index_->map_index_expression();
923 if (map_index == NULL)
925 this->report_error(_("expected map index on right hand side"));
926 return Statement::make_error_statement(loc);
928 Map_type* map_type = map_index->get_map_type();
929 if (map_type == NULL)
930 return Statement::make_error_statement(loc);
932 Block* b = new Block(enclosing, loc);
934 // Move out any subexpressions to make sure that functions are
935 // called in the required order.
936 Move_ordered_evals moe(b);
937 this->val_->traverse_subexpressions(&moe);
938 this->present_->traverse_subexpressions(&moe);
940 // Copy the key value into a temporary so that we can take its
941 // address without pushing the value onto the heap.
943 // var key_temp KEY_TYPE = MAP_INDEX
944 Temporary_statement* key_temp =
945 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
946 b->add_statement(key_temp);
948 // var val_temp VAL_TYPE
949 Temporary_statement* val_temp =
950 Statement::make_temporary(map_type->val_type(), NULL, loc);
951 b->add_statement(val_temp);
953 // var present_temp bool
954 Temporary_statement* present_temp =
955 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
956 b->add_statement(present_temp);
958 // func mapaccess2(hmap map[k]v, key *k, val *v) bool
959 source_location bloc = BUILTINS_LOCATION;
960 Typed_identifier_list* param_types = new Typed_identifier_list();
961 param_types->push_back(Typed_identifier("hmap", map_type, bloc));
962 Type* pkey_type = Type::make_pointer_type(map_type->key_type());
963 param_types->push_back(Typed_identifier("key", pkey_type, bloc));
964 Type* pval_type = Type::make_pointer_type(map_type->val_type());
965 param_types->push_back(Typed_identifier("val", pval_type, bloc));
967 Typed_identifier_list* ret_types = new Typed_identifier_list();
968 ret_types->push_back(Typed_identifier("", Type::make_boolean_type(), bloc));
970 Function_type* fntype = Type::make_function_type(NULL, param_types,
972 Named_object* mapaccess2 =
973 Named_object::make_function_declaration("mapaccess2", NULL, fntype, bloc);
974 mapaccess2->func_declaration_value()->set_asm_name("runtime.mapaccess2");
976 // present_temp = mapaccess2(MAP, &key_temp, &val_temp)
977 Expression* func = Expression::make_func_reference(mapaccess2, NULL, loc);
978 Expression_list* params = new Expression_list();
979 params->push_back(map_index->map());
980 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
981 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
982 ref = Expression::make_temporary_reference(val_temp, loc);
983 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
984 Expression* call = Expression::make_call(func, params, false, loc);
986 ref = Expression::make_temporary_reference(present_temp, loc);
987 Statement* s = Statement::make_assignment(ref, call, loc);
991 ref = Expression::make_temporary_reference(val_temp, loc);
992 s = Statement::make_assignment(this->val_, ref, loc);
995 // present = present_temp
996 ref = Expression::make_temporary_reference(present_temp, loc);
997 s = Statement::make_assignment(this->present_, ref, loc);
1000 return Statement::make_block_statement(b, loc);
1003 // Make a map assignment statement which returns a pair of values.
1006 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
1007 Expression* map_index,
1008 source_location location)
1010 return new Tuple_map_assignment_statement(val, present, map_index, location);
1013 // Assign a pair of entries to a map.
1016 class Map_assignment_statement : public Statement
1019 Map_assignment_statement(Expression* map_index,
1020 Expression* val, Expression* should_set,
1021 source_location location)
1022 : Statement(STATEMENT_MAP_ASSIGNMENT, location),
1023 map_index_(map_index), val_(val), should_set_(should_set)
1028 do_traverse(Traverse* traverse);
1031 do_traverse_assignments(Traverse_assignments*)
1032 { gcc_unreachable(); }
1035 do_lower(Gogo*, Block*);
1038 do_get_tree(Translate_context*)
1039 { gcc_unreachable(); }
1042 // A reference to the map index which should be set or deleted.
1043 Expression* map_index_;
1044 // The value to add to the map.
1046 // Whether or not to add the value.
1047 Expression* should_set_;
1050 // Traverse a map assignment.
1053 Map_assignment_statement::do_traverse(Traverse* traverse)
1055 if (this->traverse_expression(traverse, &this->map_index_) == TRAVERSE_EXIT
1056 || this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
1057 return TRAVERSE_EXIT;
1058 return this->traverse_expression(traverse, &this->should_set_);
1061 // Lower a map assignment to a function call.
1064 Map_assignment_statement::do_lower(Gogo*, Block* enclosing)
1066 source_location loc = this->location();
1068 Map_index_expression* map_index = this->map_index_->map_index_expression();
1069 if (map_index == NULL)
1071 this->report_error(_("expected map index on left hand side"));
1072 return Statement::make_error_statement(loc);
1074 Map_type* map_type = map_index->get_map_type();
1075 if (map_type == NULL)
1076 return Statement::make_error_statement(loc);
1078 Block* b = new Block(enclosing, loc);
1080 // Evaluate the map first to get order of evaluation right.
1081 // map_temp := m // we are evaluating m[k] = v, p
1082 Temporary_statement* map_temp = Statement::make_temporary(map_type,
1085 b->add_statement(map_temp);
1087 // var key_temp MAP_KEY_TYPE = k
1088 Temporary_statement* key_temp =
1089 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1090 b->add_statement(key_temp);
1092 // var val_temp MAP_VAL_TYPE = v
1093 Temporary_statement* val_temp =
1094 Statement::make_temporary(map_type->val_type(), this->val_, loc);
1095 b->add_statement(val_temp);
1097 // func mapassign2(hmap map[k]v, key *k, val *v, p)
1098 source_location bloc = BUILTINS_LOCATION;
1099 Typed_identifier_list* param_types = new Typed_identifier_list();
1100 param_types->push_back(Typed_identifier("hmap", map_type, bloc));
1101 Type* pkey_type = Type::make_pointer_type(map_type->key_type());
1102 param_types->push_back(Typed_identifier("key", pkey_type, bloc));
1103 Type* pval_type = Type::make_pointer_type(map_type->val_type());
1104 param_types->push_back(Typed_identifier("val", pval_type, bloc));
1105 param_types->push_back(Typed_identifier("p", Type::lookup_bool_type(), bloc));
1106 Function_type* fntype = Type::make_function_type(NULL, param_types,
1108 Named_object* mapassign2 =
1109 Named_object::make_function_declaration("mapassign2", NULL, fntype, bloc);
1110 mapassign2->func_declaration_value()->set_asm_name("runtime.mapassign2");
1112 // mapassign2(map_temp, &key_temp, &val_temp, p)
1113 Expression* func = Expression::make_func_reference(mapassign2, NULL, loc);
1114 Expression_list* params = new Expression_list();
1115 params->push_back(Expression::make_temporary_reference(map_temp, loc));
1116 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
1117 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1118 ref = Expression::make_temporary_reference(val_temp, loc);
1119 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1120 params->push_back(this->should_set_);
1121 Expression* call = Expression::make_call(func, params, false, loc);
1122 Statement* s = Statement::make_statement(call);
1123 b->add_statement(s);
1125 return Statement::make_block_statement(b, loc);
1128 // Make a statement which assigns a pair of entries to a map.
1131 Statement::make_map_assignment(Expression* map_index,
1132 Expression* val, Expression* should_set,
1133 source_location location)
1135 return new Map_assignment_statement(map_index, val, should_set, location);
1138 // A tuple assignment from a receive statement.
1140 class Tuple_receive_assignment_statement : public Statement
1143 Tuple_receive_assignment_statement(Expression* val, Expression* success,
1144 Expression* channel,
1145 source_location location)
1146 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
1147 val_(val), success_(success), channel_(channel)
1152 do_traverse(Traverse* traverse);
1155 do_traverse_assignments(Traverse_assignments*)
1156 { gcc_unreachable(); }
1159 do_lower(Gogo*, Block*);
1162 do_get_tree(Translate_context*)
1163 { gcc_unreachable(); }
1166 // Lvalue which receives the value from the channel.
1168 // Lvalue which receives whether the read succeeded or failed.
1169 Expression* success_;
1170 // The channel on which we receive the value.
1171 Expression* channel_;
1177 Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
1179 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1180 || this->traverse_expression(traverse, &this->success_) == TRAVERSE_EXIT)
1181 return TRAVERSE_EXIT;
1182 return this->traverse_expression(traverse, &this->channel_);
1185 // Lower to a function call.
1188 Tuple_receive_assignment_statement::do_lower(Gogo*, Block* enclosing)
1190 source_location loc = this->location();
1192 Channel_type* channel_type = this->channel_->type()->channel_type();
1193 if (channel_type == NULL)
1195 this->report_error(_("expected channel"));
1196 return Statement::make_error_statement(loc);
1198 if (!channel_type->may_receive())
1200 this->report_error(_("invalid receive on send-only channel"));
1201 return Statement::make_error_statement(loc);
1204 Block* b = new Block(enclosing, loc);
1206 // Make sure that any subexpressions on the left hand side are
1207 // evaluated in the right order.
1208 Move_ordered_evals moe(b);
1209 this->val_->traverse_subexpressions(&moe);
1210 this->success_->traverse_subexpressions(&moe);
1212 // var val_temp ELEMENT_TYPE
1213 Temporary_statement* val_temp =
1214 Statement::make_temporary(channel_type->element_type(), NULL, loc);
1215 b->add_statement(val_temp);
1217 // var success_temp bool
1218 Temporary_statement* success_temp =
1219 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
1220 b->add_statement(success_temp);
1222 // func chanrecv2(c chan T, val *T) bool
1223 source_location bloc = BUILTINS_LOCATION;
1224 Typed_identifier_list* param_types = new Typed_identifier_list();
1225 param_types->push_back(Typed_identifier("c", channel_type, bloc));
1226 Type* pelement_type = Type::make_pointer_type(channel_type->element_type());
1227 param_types->push_back(Typed_identifier("val", pelement_type, bloc));
1229 Typed_identifier_list* ret_types = new Typed_identifier_list();
1230 ret_types->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1232 Function_type* fntype = Type::make_function_type(NULL, param_types,
1234 Named_object* chanrecv2 =
1235 Named_object::make_function_declaration("chanrecv2", NULL, fntype, bloc);
1236 chanrecv2->func_declaration_value()->set_asm_name("runtime.chanrecv2");
1238 // success_temp = chanrecv2(channel, &val_temp)
1239 Expression* func = Expression::make_func_reference(chanrecv2, NULL, loc);
1240 Expression_list* params = new Expression_list();
1241 params->push_back(this->channel_);
1242 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1243 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1244 Expression* call = Expression::make_call(func, params, false, loc);
1245 ref = Expression::make_temporary_reference(success_temp, loc);
1246 Statement* s = Statement::make_assignment(ref, call, loc);
1247 b->add_statement(s);
1250 ref = Expression::make_temporary_reference(val_temp, loc);
1251 s = Statement::make_assignment(this->val_, ref, loc);
1252 b->add_statement(s);
1254 // success = success_temp
1255 ref = Expression::make_temporary_reference(success_temp, loc);
1256 s = Statement::make_assignment(this->success_, ref, loc);
1257 b->add_statement(s);
1259 return Statement::make_block_statement(b, loc);
1262 // Make a nonblocking receive statement.
1265 Statement::make_tuple_receive_assignment(Expression* val, Expression* success,
1266 Expression* channel,
1267 source_location location)
1269 return new Tuple_receive_assignment_statement(val, success, channel,
1273 // An assignment to a pair of values from a type guard. This is a
1274 // conditional type guard. v, ok = i.(type).
1276 class Tuple_type_guard_assignment_statement : public Statement
1279 Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
1280 Expression* expr, Type* type,
1281 source_location location)
1282 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
1283 val_(val), ok_(ok), expr_(expr), type_(type)
1288 do_traverse(Traverse*);
1291 do_traverse_assignments(Traverse_assignments*)
1292 { gcc_unreachable(); }
1295 do_lower(Gogo*, Block*);
1298 do_get_tree(Translate_context*)
1299 { gcc_unreachable(); }
1303 lower_to_empty_interface(const char*);
1306 lower_to_type(const char*);
1309 lower_to_object_type(Block*, const char*);
1311 // The variable which recieves the converted value.
1313 // The variable which receives the indication of success.
1315 // The expression being converted.
1317 // The type to which the expression is being converted.
1321 // Traverse a type guard tuple assignment.
1324 Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
1326 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1327 || this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
1328 || this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
1329 return TRAVERSE_EXIT;
1330 return this->traverse_expression(traverse, &this->expr_);
1333 // Lower to a function call.
1336 Tuple_type_guard_assignment_statement::do_lower(Gogo*, Block* enclosing)
1338 source_location loc = this->location();
1340 Type* expr_type = this->expr_->type();
1341 if (expr_type->interface_type() == NULL)
1343 if (!expr_type->is_error_type() && !this->type_->is_error_type())
1344 this->report_error(_("type assertion only valid for interface types"));
1345 return Statement::make_error_statement(loc);
1348 Block* b = new Block(enclosing, loc);
1350 // Make sure that any subexpressions on the left hand side are
1351 // evaluated in the right order.
1352 Move_ordered_evals moe(b);
1353 this->val_->traverse_subexpressions(&moe);
1354 this->ok_->traverse_subexpressions(&moe);
1356 bool expr_is_empty = expr_type->interface_type()->is_empty();
1357 Call_expression* call;
1358 if (this->type_->interface_type() != NULL)
1360 if (this->type_->interface_type()->is_empty())
1361 call = this->lower_to_empty_interface(expr_is_empty
1365 call = this->lower_to_type(expr_is_empty ? "ifaceE2I2" : "ifaceI2I2");
1367 else if (this->type_->points_to() != NULL)
1368 call = this->lower_to_type(expr_is_empty ? "ifaceE2T2P" : "ifaceI2T2P");
1371 this->lower_to_object_type(b, expr_is_empty ? "ifaceE2T2" : "ifaceI2T2");
1377 Expression* res = Expression::make_call_result(call, 0);
1378 Statement* s = Statement::make_assignment(this->val_, res, loc);
1379 b->add_statement(s);
1381 res = Expression::make_call_result(call, 1);
1382 s = Statement::make_assignment(this->ok_, res, loc);
1383 b->add_statement(s);
1386 return Statement::make_block_statement(b, loc);
1389 // Lower a conversion to an empty interface type.
1392 Tuple_type_guard_assignment_statement::lower_to_empty_interface(
1395 source_location loc = this->location();
1397 // func FNNAME(interface) (empty, bool)
1398 source_location bloc = BUILTINS_LOCATION;
1399 Typed_identifier_list* param_types = new Typed_identifier_list();
1400 param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
1401 Typed_identifier_list* ret_types = new Typed_identifier_list();
1402 ret_types->push_back(Typed_identifier("ret", this->type_, bloc));
1403 ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
1404 Function_type* fntype = Type::make_function_type(NULL, param_types,
1407 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1408 std::string asm_name = "runtime.";
1410 fn->func_declaration_value()->set_asm_name(asm_name);
1412 // val, ok = FNNAME(expr)
1413 Expression* func = Expression::make_func_reference(fn, NULL, loc);
1414 Expression_list* params = new Expression_list();
1415 params->push_back(this->expr_);
1416 return Expression::make_call(func, params, false, loc);
1419 // Lower a conversion to a non-empty interface type or a pointer type.
1422 Tuple_type_guard_assignment_statement::lower_to_type(const char* fnname)
1424 source_location loc = this->location();
1426 // func FNNAME(*descriptor, interface) (interface, bool)
1427 source_location bloc = BUILTINS_LOCATION;
1428 Typed_identifier_list* param_types = new Typed_identifier_list();
1429 param_types->push_back(Typed_identifier("inter",
1430 Type::make_type_descriptor_ptr_type(),
1432 param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
1433 Typed_identifier_list* ret_types = new Typed_identifier_list();
1434 ret_types->push_back(Typed_identifier("ret", this->type_, bloc));
1435 ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
1436 Function_type* fntype = Type::make_function_type(NULL, param_types,
1439 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1440 std::string asm_name = "runtime.";
1442 fn->func_declaration_value()->set_asm_name(asm_name);
1444 // val, ok = FNNAME(type_descriptor, expr)
1445 Expression* func = Expression::make_func_reference(fn, NULL, loc);
1446 Expression_list* params = new Expression_list();
1447 params->push_back(Expression::make_type_descriptor(this->type_, loc));
1448 params->push_back(this->expr_);
1449 return Expression::make_call(func, params, false, loc);
1452 // Lower a conversion to a non-interface non-pointer type.
1455 Tuple_type_guard_assignment_statement::lower_to_object_type(Block* b,
1458 source_location loc = this->location();
1460 // var val_temp TYPE
1461 Temporary_statement* val_temp = Statement::make_temporary(this->type_,
1463 b->add_statement(val_temp);
1465 // func FNNAME(*descriptor, interface, *T) bool
1466 source_location bloc = BUILTINS_LOCATION;
1467 Typed_identifier_list* param_types = new Typed_identifier_list();
1468 param_types->push_back(Typed_identifier("inter",
1469 Type::make_type_descriptor_ptr_type(),
1471 param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
1472 Type* ptype = Type::make_pointer_type(this->type_);
1473 param_types->push_back(Typed_identifier("v", ptype, bloc));
1474 Typed_identifier_list* ret_types = new Typed_identifier_list();
1475 ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
1476 Function_type* fntype = Type::make_function_type(NULL, param_types,
1479 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1480 std::string asm_name = "runtime.";
1482 fn->func_declaration_value()->set_asm_name(asm_name);
1484 // ok = FNNAME(type_descriptor, expr, &val_temp)
1485 Expression* func = Expression::make_func_reference(fn, NULL, loc);
1486 Expression_list* params = new Expression_list();
1487 params->push_back(Expression::make_type_descriptor(this->type_, loc));
1488 params->push_back(this->expr_);
1489 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1490 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1491 Expression* call = Expression::make_call(func, params, false, loc);
1492 Statement* s = Statement::make_assignment(this->ok_, call, loc);
1493 b->add_statement(s);
1496 ref = Expression::make_temporary_reference(val_temp, loc);
1497 s = Statement::make_assignment(this->val_, ref, loc);
1498 b->add_statement(s);
1501 // Make an assignment from a type guard to a pair of variables.
1504 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
1505 Expression* expr, Type* type,
1506 source_location location)
1508 return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
1512 // An expression statement.
1514 class Expression_statement : public Statement
1517 Expression_statement(Expression* expr)
1518 : Statement(STATEMENT_EXPRESSION, expr->location()),
1524 do_traverse(Traverse* traverse)
1525 { return this->traverse_expression(traverse, &this->expr_); }
1528 do_determine_types()
1529 { this->expr_->determine_type_no_context(); }
1532 do_may_fall_through() const;
1535 do_get_tree(Translate_context* context)
1536 { return this->expr_->get_tree(context); }
1542 // An expression statement may fall through unless it is a call to a
1543 // function which does not return.
1546 Expression_statement::do_may_fall_through() const
1548 const Call_expression* call = this->expr_->call_expression();
1551 const Expression* fn = call->fn();
1552 const Func_expression* fe = fn->func_expression();
1555 const Named_object* no = fe->named_object();
1557 Function_type* fntype;
1558 if (no->is_function())
1559 fntype = no->func_value()->type();
1560 else if (no->is_function_declaration())
1561 fntype = no->func_declaration_value()->type();
1565 // The builtin function panic does not return.
1566 if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
1573 // Make an expression statement from an Expression.
1576 Statement::make_statement(Expression* expr)
1578 return new Expression_statement(expr);
1581 // A block statement--a list of statements which may include variable
1584 class Block_statement : public Statement
1587 Block_statement(Block* block, source_location location)
1588 : Statement(STATEMENT_BLOCK, location),
1594 do_traverse(Traverse* traverse)
1595 { return this->block_->traverse(traverse); }
1598 do_determine_types()
1599 { this->block_->determine_types(); }
1602 do_may_fall_through() const
1603 { return this->block_->may_fall_through(); }
1606 do_get_tree(Translate_context* context)
1607 { return this->block_->get_tree(context); }
1613 // Make a block statement.
1616 Statement::make_block_statement(Block* block, source_location location)
1618 return new Block_statement(block, location);
1621 // An increment or decrement statement.
1623 class Inc_dec_statement : public Statement
1626 Inc_dec_statement(bool is_inc, Expression* expr)
1627 : Statement(STATEMENT_INCDEC, expr->location()),
1628 expr_(expr), is_inc_(is_inc)
1633 do_traverse(Traverse* traverse)
1634 { return this->traverse_expression(traverse, &this->expr_); }
1637 do_traverse_assignments(Traverse_assignments*)
1638 { gcc_unreachable(); }
1641 do_lower(Gogo*, Block*);
1644 do_get_tree(Translate_context*)
1645 { gcc_unreachable(); }
1648 // The l-value to increment or decrement.
1650 // Whether to increment or decrement.
1654 // Lower to += or -=.
1657 Inc_dec_statement::do_lower(Gogo*, Block*)
1659 source_location loc = this->location();
1662 mpz_init_set_ui(oval, 1UL);
1663 Expression* oexpr = Expression::make_integer(&oval, NULL, loc);
1666 Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
1667 return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
1670 // Make an increment statement.
1673 Statement::make_inc_statement(Expression* expr)
1675 return new Inc_dec_statement(true, expr);
1678 // Make a decrement statement.
1681 Statement::make_dec_statement(Expression* expr)
1683 return new Inc_dec_statement(false, expr);
1686 // Class Thunk_statement. This is the base class for go and defer
1689 const char* const Thunk_statement::thunk_field_fn = "fn";
1691 const char* const Thunk_statement::thunk_field_receiver = "receiver";
1695 Thunk_statement::Thunk_statement(Statement_classification classification,
1696 Call_expression* call,
1697 source_location location)
1698 : Statement(classification, location),
1699 call_(call), struct_type_(NULL)
1703 // Return whether this is a simple statement which does not require a
1707 Thunk_statement::is_simple(Function_type* fntype) const
1709 // We need a thunk to call a method, or to pass a variable number of
1711 if (fntype->is_method() || fntype->is_varargs())
1714 // A defer statement requires a thunk to set up for whether the
1715 // function can call recover.
1716 if (this->classification() == STATEMENT_DEFER)
1719 // We can only permit a single parameter of pointer type.
1720 const Typed_identifier_list* parameters = fntype->parameters();
1721 if (parameters != NULL
1722 && (parameters->size() > 1
1723 || (parameters->size() == 1
1724 && parameters->begin()->type()->points_to() == NULL)))
1727 // If the function returns multiple values, or returns a type other
1728 // than integer, floating point, or pointer, then it may get a
1729 // hidden first parameter, in which case we need the more
1730 // complicated approach. This is true even though we are going to
1731 // ignore the return value.
1732 const Typed_identifier_list* results = fntype->results();
1734 && (results->size() > 1
1735 || (results->size() == 1
1736 && !results->begin()->type()->is_basic_type()
1737 && results->begin()->type()->points_to() == NULL)))
1740 // If this calls something which is not a simple function, then we
1742 Expression* fn = this->call_->call_expression()->fn();
1743 if (fn->bound_method_expression() != NULL
1744 || fn->interface_field_reference_expression() != NULL)
1750 // Traverse a thunk statement.
1753 Thunk_statement::do_traverse(Traverse* traverse)
1755 return this->traverse_expression(traverse, &this->call_);
1758 // We implement traverse_assignment for a thunk statement because it
1759 // effectively copies the function call.
1762 Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
1764 Expression* fn = this->call_->call_expression()->fn();
1765 Expression* fn2 = fn;
1766 tassign->value(&fn2, true, false);
1770 // Determine types in a thunk statement.
1773 Thunk_statement::do_determine_types()
1775 this->call_->determine_type_no_context();
1777 // Now that we know the types of the call, build the struct used to
1779 Call_expression* ce = this->call_->call_expression();
1782 Function_type* fntype = ce->get_function_type();
1783 if (fntype != NULL && !this->is_simple(fntype))
1784 this->struct_type_ = this->build_struct(fntype);
1787 // Check types in a thunk statement.
1790 Thunk_statement::do_check_types(Gogo*)
1792 Call_expression* ce = this->call_->call_expression();
1795 if (!this->call_->is_error_expression())
1796 this->report_error("expected call expression");
1799 Function_type* fntype = ce->get_function_type();
1800 if (fntype != NULL && fntype->is_method())
1802 Expression* fn = ce->fn();
1803 if (fn->bound_method_expression() == NULL
1804 && fn->interface_field_reference_expression() == NULL)
1805 this->report_error(_("no object for method call"));
1809 // The Traverse class used to find and simplify thunk statements.
1811 class Simplify_thunk_traverse : public Traverse
1814 Simplify_thunk_traverse(Gogo* gogo)
1815 : Traverse(traverse_blocks),
1827 Simplify_thunk_traverse::block(Block* b)
1829 // The parser ensures that thunk statements always appear at the end
1831 if (b->statements()->size() < 1)
1832 return TRAVERSE_CONTINUE;
1833 Thunk_statement* stat = b->statements()->back()->thunk_statement();
1835 return TRAVERSE_CONTINUE;
1836 if (stat->simplify_statement(this->gogo_, b))
1837 return TRAVERSE_SKIP_COMPONENTS;
1838 return TRAVERSE_CONTINUE;
1841 // Simplify all thunk statements.
1844 Gogo::simplify_thunk_statements()
1846 Simplify_thunk_traverse thunk_traverse(this);
1847 this->traverse(&thunk_traverse);
1850 // Simplify complex thunk statements into simple ones. A complicated
1851 // thunk statement is one which takes anything other than zero
1852 // parameters or a single pointer parameter. We rewrite it into code
1853 // which allocates a struct, stores the parameter values into the
1854 // struct, and does a simple go or defer statement which passes the
1855 // struct to a thunk. The thunk does the real call.
1858 Thunk_statement::simplify_statement(Gogo* gogo, Block* block)
1860 if (this->classification() == STATEMENT_ERROR)
1862 if (this->call_->is_error_expression())
1865 Call_expression* ce = this->call_->call_expression();
1866 Function_type* fntype = ce->get_function_type();
1869 gcc_assert(saw_errors());
1870 this->set_is_error();
1873 if (this->is_simple(fntype))
1876 Expression* fn = ce->fn();
1877 Bound_method_expression* bound_method = fn->bound_method_expression();
1878 Interface_field_reference_expression* interface_method =
1879 fn->interface_field_reference_expression();
1880 const bool is_method = bound_method != NULL || interface_method != NULL;
1882 source_location location = this->location();
1884 std::string thunk_name = Gogo::thunk_name();
1887 this->build_thunk(gogo, thunk_name, fntype);
1889 // Generate code to call the thunk.
1891 // Get the values to store into the struct which is the single
1892 // argument to the thunk.
1894 Expression_list* vals = new Expression_list();
1895 if (fntype->is_builtin())
1897 else if (!is_method)
1898 vals->push_back(fn);
1899 else if (interface_method != NULL)
1900 vals->push_back(interface_method->expr());
1901 else if (bound_method != NULL)
1903 vals->push_back(bound_method->method());
1904 Expression* first_arg = bound_method->first_argument();
1906 // We always pass a pointer when calling a method.
1907 if (first_arg->type()->points_to() == NULL)
1908 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, location);
1910 // If we are calling a method which was inherited from an
1911 // embedded struct, and the method did not get a stub, then the
1912 // first type may be wrong.
1913 Type* fatype = bound_method->first_argument_type();
1916 if (fatype->points_to() == NULL)
1917 fatype = Type::make_pointer_type(fatype);
1918 Type* unsafe = Type::make_pointer_type(Type::make_void_type());
1919 first_arg = Expression::make_cast(unsafe, first_arg, location);
1920 first_arg = Expression::make_cast(fatype, first_arg, location);
1923 vals->push_back(first_arg);
1928 if (ce->args() != NULL)
1930 for (Expression_list::const_iterator p = ce->args()->begin();
1931 p != ce->args()->end();
1933 vals->push_back(*p);
1936 // Build the struct.
1937 Expression* constructor =
1938 Expression::make_struct_composite_literal(this->struct_type_, vals,
1941 // Allocate the initialized struct on the heap.
1942 constructor = Expression::make_heap_composite(constructor, location);
1944 // Look up the thunk.
1945 Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
1946 gcc_assert(named_thunk != NULL && named_thunk->is_function());
1949 Expression* func = Expression::make_func_reference(named_thunk, NULL,
1951 Expression_list* params = new Expression_list();
1952 params->push_back(constructor);
1953 Call_expression* call = Expression::make_call(func, params, false, location);
1955 // Build the simple go or defer statement.
1957 if (this->classification() == STATEMENT_GO)
1958 s = Statement::make_go_statement(call, location);
1959 else if (this->classification() == STATEMENT_DEFER)
1960 s = Statement::make_defer_statement(call, location);
1964 // The current block should end with the go statement.
1965 gcc_assert(block->statements()->size() >= 1);
1966 gcc_assert(block->statements()->back() == this);
1967 block->replace_statement(block->statements()->size() - 1, s);
1969 // We already ran the determine_types pass, so we need to run it now
1970 // for the new statement.
1971 s->determine_types();
1974 gogo->check_types_in_block(block);
1976 // Return true to tell the block not to keep looking at statements.
1980 // Set the name to use for thunk parameter N.
1983 Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
1985 snprintf(buf, buflen, "a%d", n);
1988 // Build a new struct type to hold the parameters for a complicated
1989 // thunk statement. FNTYPE is the type of the function call.
1992 Thunk_statement::build_struct(Function_type* fntype)
1994 source_location location = this->location();
1996 Struct_field_list* fields = new Struct_field_list();
1998 Call_expression* ce = this->call_->call_expression();
1999 Expression* fn = ce->fn();
2001 Interface_field_reference_expression* interface_method =
2002 fn->interface_field_reference_expression();
2003 if (interface_method != NULL)
2005 // If this thunk statement calls a method on an interface, we
2006 // pass the interface object to the thunk.
2007 Typed_identifier tid(Thunk_statement::thunk_field_fn,
2008 interface_method->expr()->type(),
2010 fields->push_back(Struct_field(tid));
2012 else if (!fntype->is_builtin())
2014 // The function to call.
2015 Typed_identifier tid(Go_statement::thunk_field_fn, fntype, location);
2016 fields->push_back(Struct_field(tid));
2018 else if (ce->is_recover_call())
2020 // The predeclared recover function has no argument. However,
2021 // we add an argument when building recover thunks. Handle that
2023 fields->push_back(Struct_field(Typed_identifier("can_recover",
2024 Type::make_boolean_type(),
2028 if (fn->bound_method_expression() != NULL)
2030 gcc_assert(fntype->is_method());
2031 Type* rtype = fntype->receiver()->type();
2032 // We always pass the receiver as a pointer.
2033 if (rtype->points_to() == NULL)
2034 rtype = Type::make_pointer_type(rtype);
2035 Typed_identifier tid(Thunk_statement::thunk_field_receiver, rtype,
2037 fields->push_back(Struct_field(tid));
2040 const Expression_list* args = ce->args();
2044 for (Expression_list::const_iterator p = args->begin();
2049 this->thunk_field_param(i, buf, sizeof buf);
2050 fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
2055 return Type::make_struct_type(fields, location);
2058 // Build the thunk we are going to call. This is a brand new, albeit
2059 // artificial, function.
2062 Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
2063 Function_type* fntype)
2065 source_location location = this->location();
2067 Call_expression* ce = this->call_->call_expression();
2069 bool may_call_recover = false;
2070 if (this->classification() == STATEMENT_DEFER)
2072 Func_expression* fn = ce->fn()->func_expression();
2074 may_call_recover = true;
2077 const Named_object* no = fn->named_object();
2078 if (!no->is_function())
2079 may_call_recover = true;
2081 may_call_recover = no->func_value()->calls_recover();
2085 // Build the type of the thunk. The thunk takes a single parameter,
2086 // which is a pointer to the special structure we build.
2087 const char* const parameter_name = "__go_thunk_parameter";
2088 Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
2089 Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
2090 thunk_parameters->push_back(Typed_identifier(parameter_name,
2091 pointer_to_struct_type,
2094 Typed_identifier_list* thunk_results = NULL;
2095 if (may_call_recover)
2097 // When deferring a function which may call recover, add a
2098 // return value, to disable tail call optimizations which will
2099 // break the way we check whether recover is permitted.
2100 thunk_results = new Typed_identifier_list();
2101 thunk_results->push_back(Typed_identifier("", Type::make_boolean_type(),
2105 Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
2109 // Start building the thunk.
2110 Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
2113 // For a defer statement, start with a call to
2114 // __go_set_defer_retaddr. */
2115 Label* retaddr_label = NULL;
2116 if (may_call_recover)
2118 retaddr_label = gogo->add_label_reference("retaddr");
2119 Expression* arg = Expression::make_label_addr(retaddr_label, location);
2120 Expression_list* args = new Expression_list();
2121 args->push_back(arg);
2123 static Named_object* set_defer_retaddr;
2124 if (set_defer_retaddr == NULL)
2126 const source_location bloc = BUILTINS_LOCATION;
2127 Typed_identifier_list* param_types = new Typed_identifier_list();
2128 Type *voidptr_type = Type::make_pointer_type(Type::make_void_type());
2129 param_types->push_back(Typed_identifier("r", voidptr_type, bloc));
2131 Typed_identifier_list* result_types = new Typed_identifier_list();
2132 result_types->push_back(Typed_identifier("",
2133 Type::make_boolean_type(),
2136 Function_type* t = Type::make_function_type(NULL, param_types,
2137 result_types, bloc);
2139 Named_object::make_function_declaration("__go_set_defer_retaddr",
2141 const char* n = "__go_set_defer_retaddr";
2142 set_defer_retaddr->func_declaration_value()->set_asm_name(n);
2145 Expression* fn = Expression::make_func_reference(set_defer_retaddr,
2147 Expression* call = Expression::make_call(fn, args, false, location);
2149 // This is a hack to prevent the middle-end from deleting the
2151 gogo->start_block(location);
2152 gogo->add_statement(Statement::make_goto_statement(retaddr_label,
2154 Block* then_block = gogo->finish_block(location);
2155 then_block->determine_types();
2157 Statement* s = Statement::make_if_statement(call, then_block, NULL,
2159 s->determine_types();
2160 gogo->add_statement(s);
2163 // Get a reference to the parameter.
2164 Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
2165 gcc_assert(named_parameter != NULL && named_parameter->is_variable());
2167 // Build the call. Note that the field names are the same as the
2168 // ones used in build_struct.
2169 Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
2171 thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
2174 Bound_method_expression* bound_method = ce->fn()->bound_method_expression();
2175 Interface_field_reference_expression* interface_method =
2176 ce->fn()->interface_field_reference_expression();
2178 Expression* func_to_call;
2179 unsigned int next_index;
2180 if (!fntype->is_builtin())
2182 func_to_call = Expression::make_field_reference(thunk_parameter,
2188 gcc_assert(bound_method == NULL && interface_method == NULL);
2189 func_to_call = ce->fn();
2193 if (bound_method != NULL)
2195 Expression* r = Expression::make_field_reference(thunk_parameter, 1,
2197 // The main program passes in a function pointer from the
2198 // interface expression, so here we can make a bound method in
2200 func_to_call = Expression::make_bound_method(r, func_to_call,
2204 else if (interface_method != NULL)
2206 // The main program passes the interface object.
2207 const std::string& name(interface_method->name());
2208 func_to_call = Expression::make_interface_field_reference(func_to_call,
2213 Expression_list* call_params = new Expression_list();
2214 const Struct_field_list* fields = this->struct_type_->fields();
2215 Struct_field_list::const_iterator p = fields->begin();
2216 for (unsigned int i = 0; i < next_index; ++i)
2218 bool is_recover_call = ce->is_recover_call();
2219 Expression* recover_arg = NULL;
2220 for (; p != fields->end(); ++p, ++next_index)
2222 Expression* thunk_param = Expression::make_var_reference(named_parameter,
2224 thunk_param = Expression::make_unary(OPERATOR_MULT, thunk_param,
2226 Expression* param = Expression::make_field_reference(thunk_param,
2229 if (!is_recover_call)
2230 call_params->push_back(param);
2233 gcc_assert(call_params->empty());
2234 recover_arg = param;
2238 if (call_params->empty())
2244 Expression* call = Expression::make_call(func_to_call, call_params, false,
2246 // We need to lower in case this is a builtin function.
2247 call = call->lower(gogo, function, -1);
2248 Call_expression* call_ce = call->call_expression();
2249 if (call_ce != NULL && may_call_recover)
2250 call_ce->set_is_deferred();
2252 Statement* call_statement = Statement::make_statement(call);
2254 // We already ran the determine_types pass, so we need to run it
2255 // just for this statement now.
2256 call_statement->determine_types();
2259 call->check_types(gogo);
2261 if (call_ce != NULL && recover_arg != NULL)
2262 call_ce->set_recover_arg(recover_arg);
2264 gogo->add_statement(call_statement);
2266 // If this is a defer statement, the label comes immediately after
2268 if (may_call_recover)
2270 gogo->add_label_definition("retaddr", location);
2272 Expression_list* vals = new Expression_list();
2273 vals->push_back(Expression::make_boolean(false, location));
2274 const Typed_identifier_list* results =
2275 function->func_value()->type()->results();
2276 gogo->add_statement(Statement::make_return_statement(results, vals,
2280 // That is all the thunk has to do.
2281 gogo->finish_function(location);
2284 // Get the function and argument trees.
2287 Thunk_statement::get_fn_and_arg(Translate_context* context, tree* pfn,
2290 if (this->call_->is_error_expression())
2292 *pfn = error_mark_node;
2293 *parg = error_mark_node;
2297 Call_expression* ce = this->call_->call_expression();
2299 Expression* fn = ce->fn();
2300 *pfn = fn->get_tree(context);
2302 const Expression_list* args = ce->args();
2303 if (args == NULL || args->empty())
2304 *parg = null_pointer_node;
2307 gcc_assert(args->size() == 1);
2308 *parg = args->front()->get_tree(context);
2312 // Class Go_statement.
2315 Go_statement::do_get_tree(Translate_context* context)
2319 this->get_fn_and_arg(context, &fn_tree, &arg_tree);
2321 static tree go_fndecl;
2323 tree fn_arg_type = NULL_TREE;
2324 if (go_fndecl == NULL_TREE)
2326 // Only build FN_ARG_TYPE if we need it.
2327 tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
2328 tree subfntype = build_function_type(ptr_type_node, subargtypes);
2329 fn_arg_type = build_pointer_type(subfntype);
2332 return Gogo::call_builtin(&go_fndecl,
2343 // Make a go statement.
2346 Statement::make_go_statement(Call_expression* call, source_location location)
2348 return new Go_statement(call, location);
2351 // Class Defer_statement.
2354 Defer_statement::do_get_tree(Translate_context* context)
2356 source_location loc = this->location();
2360 this->get_fn_and_arg(context, &fn_tree, &arg_tree);
2361 if (fn_tree == error_mark_node || arg_tree == error_mark_node)
2362 return error_mark_node;
2364 static tree defer_fndecl;
2366 tree fn_arg_type = NULL_TREE;
2367 if (defer_fndecl == NULL_TREE)
2369 // Only build FN_ARG_TYPE if we need it.
2370 tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
2371 tree subfntype = build_function_type(ptr_type_node, subargtypes);
2372 fn_arg_type = build_pointer_type(subfntype);
2375 tree defer_stack = context->function()->func_value()->defer_stack(loc);
2377 return Gogo::call_builtin(&defer_fndecl,
2390 // Make a defer statement.
2393 Statement::make_defer_statement(Call_expression* call,
2394 source_location location)
2396 return new Defer_statement(call, location);
2399 // Class Return_statement.
2401 // Traverse assignments. We treat each return value as a top level
2402 // RHS in an expression.
2405 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2407 Expression_list* vals = this->vals_;
2410 for (Expression_list::iterator p = vals->begin();
2413 tassign->value(&*p, true, true);
2418 // Lower a return statement. If we are returning a function call
2419 // which returns multiple values which match the current function,
2420 // split up the call's results. If the function has named result
2421 // variables, and the return statement lists explicit values, then
2422 // implement it by assigning the values to the result variables and
2423 // changing the statement to not list any values. This lets
2424 // panic/recover work correctly.
2427 Return_statement::do_lower(Gogo*, Block* enclosing)
2429 if (this->vals_ == NULL)
2432 const Typed_identifier_list* results = this->results_;
2433 if (results == NULL || results->empty())
2436 // If the current function has multiple return values, and we are
2437 // returning a single call expression, split up the call expression.
2438 size_t results_count = results->size();
2439 if (results_count > 1
2440 && this->vals_->size() == 1
2441 && this->vals_->front()->call_expression() != NULL)
2443 Call_expression* call = this->vals_->front()->call_expression();
2444 size_t count = results->size();
2445 Expression_list* vals = new Expression_list;
2446 for (size_t i = 0; i < count; ++i)
2447 vals->push_back(Expression::make_call_result(call, i));
2452 if (results->front().name().empty())
2455 if (results_count != this->vals_->size())
2457 // Presumably an error which will be reported in check_types.
2461 // Assign to named return values and then return them.
2463 source_location loc = this->location();
2464 const Block* top = enclosing;
2465 while (top->enclosing() != NULL)
2466 top = top->enclosing();
2468 const Bindings *bindings = top->bindings();
2469 Block* b = new Block(enclosing, loc);
2471 Expression_list* lhs = new Expression_list();
2472 Expression_list* rhs = new Expression_list();
2474 Expression_list::const_iterator pe = this->vals_->begin();
2476 for (Typed_identifier_list::const_iterator pr = results->begin();
2477 pr != results->end();
2480 Named_object* rv = bindings->lookup_local(pr->name());
2481 if (rv == NULL || !rv->is_result_variable())
2483 // Presumably an error.
2490 Expression* e = *pe;
2492 // Check types now so that we give a good error message. The
2493 // result type is known. We determine the expression type
2496 Type *rvtype = rv->result_var_value()->type();
2497 Type_context type_context(rvtype, false);
2498 e->determine_type(&type_context);
2501 if (Type::are_assignable(rvtype, e->type(), &reason))
2503 Expression* ve = Expression::make_var_reference(rv, e->location());
2510 error_at(e->location(), "incompatible type for return value %d", i);
2512 error_at(e->location(),
2513 "incompatible type for return value %d (%s)",
2517 gcc_assert(lhs->size() == rhs->size());
2521 else if (lhs->size() == 1)
2523 b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
2529 b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
2531 b->add_statement(Statement::make_return_statement(this->results_, NULL,
2534 return Statement::make_block_statement(b, loc);
2540 Return_statement::do_determine_types()
2542 if (this->vals_ == NULL)
2544 const Typed_identifier_list* results = this->results_;
2546 Typed_identifier_list::const_iterator pt;
2547 if (results != NULL)
2548 pt = results->begin();
2549 for (Expression_list::iterator pe = this->vals_->begin();
2550 pe != this->vals_->end();
2553 if (results == NULL || pt == results->end())
2554 (*pe)->determine_type_no_context();
2557 Type_context context(pt->type(), false);
2558 (*pe)->determine_type(&context);
2567 Return_statement::do_check_types(Gogo*)
2569 if (this->vals_ == NULL)
2572 const Typed_identifier_list* results = this->results_;
2573 if (results == NULL)
2575 this->report_error(_("return with value in function "
2576 "with no return type"));
2581 Typed_identifier_list::const_iterator pt = results->begin();
2582 for (Expression_list::const_iterator pe = this->vals_->begin();
2583 pe != this->vals_->end();
2586 if (pt == results->end())
2588 this->report_error(_("too many values in return statement"));
2592 if (!Type::are_assignable(pt->type(), (*pe)->type(), &reason))
2595 error_at(this->location(),
2596 "incompatible type for return value %d",
2599 error_at(this->location(),
2600 "incompatible type for return value %d (%s)",
2602 this->set_is_error();
2604 else if (pt->type()->is_error_type()
2605 || (*pe)->type()->is_error_type()
2606 || pt->type()->is_undefined()
2607 || (*pe)->type()->is_undefined())
2609 // Make sure we get the error for an undefined type.
2611 (*pe)->type()->base();
2612 this->set_is_error();
2616 if (pt != results->end())
2617 this->report_error(_("not enough values in return statement"));
2620 // Build a RETURN_EXPR tree.
2623 Return_statement::do_get_tree(Translate_context* context)
2625 Function* function = context->function()->func_value();
2626 tree fndecl = function->get_decl();
2627 if (fndecl == error_mark_node || DECL_RESULT(fndecl) == error_mark_node)
2628 return error_mark_node;
2630 const Typed_identifier_list* results = this->results_;
2632 if (this->vals_ == NULL)
2634 tree stmt_list = NULL_TREE;
2635 tree retval = function->return_value(context->gogo(),
2636 context->function(),
2640 if (retval == NULL_TREE)
2642 else if (retval == error_mark_node)
2643 return error_mark_node;
2645 set = fold_build2_loc(this->location(), MODIFY_EXPR, void_type_node,
2646 DECL_RESULT(fndecl), retval);
2647 append_to_statement_list(this->build_stmt_1(RETURN_EXPR, set),
2651 else if (this->vals_->size() == 1)
2653 gcc_assert(!VOID_TYPE_P(TREE_TYPE(TREE_TYPE(fndecl))));
2654 tree val = (*this->vals_->begin())->get_tree(context);
2655 gcc_assert(results != NULL && results->size() == 1);
2656 val = Expression::convert_for_assignment(context,
2657 results->begin()->type(),
2658 (*this->vals_->begin())->type(),
2659 val, this->location());
2660 if (val == error_mark_node)
2661 return error_mark_node;
2662 tree set = build2(MODIFY_EXPR, void_type_node,
2663 DECL_RESULT(fndecl), val);
2664 SET_EXPR_LOCATION(set, this->location());
2665 return this->build_stmt_1(RETURN_EXPR, set);
2669 gcc_assert(!VOID_TYPE_P(TREE_TYPE(TREE_TYPE(fndecl))));
2670 tree stmt_list = NULL_TREE;
2671 tree rettype = TREE_TYPE(DECL_RESULT(fndecl));
2672 tree retvar = create_tmp_var(rettype, "RESULT");
2673 gcc_assert(results != NULL && results->size() == this->vals_->size());
2674 Expression_list::const_iterator pv = this->vals_->begin();
2675 Typed_identifier_list::const_iterator pr = results->begin();
2676 for (tree field = TYPE_FIELDS(rettype);
2678 ++pv, ++pr, field = DECL_CHAIN(field))
2680 gcc_assert(pv != this->vals_->end());
2681 tree val = (*pv)->get_tree(context);
2682 val = Expression::convert_for_assignment(context, pr->type(),
2685 if (val == error_mark_node)
2686 return error_mark_node;
2687 tree set = build2(MODIFY_EXPR, void_type_node,
2688 build3(COMPONENT_REF, TREE_TYPE(field),
2689 retvar, field, NULL_TREE),
2691 SET_EXPR_LOCATION(set, this->location());
2692 append_to_statement_list(set, &stmt_list);
2694 tree set = build2(MODIFY_EXPR, void_type_node, DECL_RESULT(fndecl),
2696 append_to_statement_list(this->build_stmt_1(RETURN_EXPR, set),
2702 // Make a return statement.
2705 Statement::make_return_statement(const Typed_identifier_list* results,
2706 Expression_list* vals,
2707 source_location location)
2709 return new Return_statement(results, vals, location);
2712 // A break or continue statement.
2714 class Bc_statement : public Statement
2717 Bc_statement(bool is_break, Unnamed_label* label, source_location location)
2718 : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
2719 label_(label), is_break_(is_break)
2724 { return this->is_break_; }
2728 do_traverse(Traverse*)
2729 { return TRAVERSE_CONTINUE; }
2732 do_may_fall_through() const
2736 do_get_tree(Translate_context*)
2737 { return this->label_->get_goto(this->location()); }
2740 // The label that this branches to.
2741 Unnamed_label* label_;
2742 // True if this is "break", false if it is "continue".
2746 // Make a break statement.
2749 Statement::make_break_statement(Unnamed_label* label, source_location location)
2751 return new Bc_statement(true, label, location);
2754 // Make a continue statement.
2757 Statement::make_continue_statement(Unnamed_label* label,
2758 source_location location)
2760 return new Bc_statement(false, label, location);
2763 // A goto statement.
2765 class Goto_statement : public Statement
2768 Goto_statement(Label* label, source_location location)
2769 : Statement(STATEMENT_GOTO, location),
2775 do_traverse(Traverse*)
2776 { return TRAVERSE_CONTINUE; }
2779 do_check_types(Gogo*);
2782 do_may_fall_through() const
2786 do_get_tree(Translate_context*);
2792 // Check types for a label. There aren't any types per se, but we use
2793 // this to give an error if the label was never defined.
2796 Goto_statement::do_check_types(Gogo*)
2798 if (!this->label_->is_defined())
2800 error_at(this->location(), "reference to undefined label %qs",
2801 Gogo::message_name(this->label_->name()).c_str());
2802 this->set_is_error();
2806 // Return the tree for the goto statement.
2809 Goto_statement::do_get_tree(Translate_context*)
2811 return this->build_stmt_1(GOTO_EXPR, this->label_->get_decl());
2814 // Make a goto statement.
2817 Statement::make_goto_statement(Label* label, source_location location)
2819 return new Goto_statement(label, location);
2822 // A goto statement to an unnamed label.
2824 class Goto_unnamed_statement : public Statement
2827 Goto_unnamed_statement(Unnamed_label* label, source_location location)
2828 : Statement(STATEMENT_GOTO_UNNAMED, location),
2834 do_traverse(Traverse*)
2835 { return TRAVERSE_CONTINUE; }
2838 do_may_fall_through() const
2842 do_get_tree(Translate_context*)
2843 { return this->label_->get_goto(this->location()); }
2846 Unnamed_label* label_;
2849 // Make a goto statement to an unnamed label.
2852 Statement::make_goto_unnamed_statement(Unnamed_label* label,
2853 source_location location)
2855 return new Goto_unnamed_statement(label, location);
2858 // Class Label_statement.
2863 Label_statement::do_traverse(Traverse*)
2865 return TRAVERSE_CONTINUE;
2868 // Return a tree defining this label.
2871 Label_statement::do_get_tree(Translate_context*)
2873 return this->build_stmt_1(LABEL_EXPR, this->label_->get_decl());
2876 // Make a label statement.
2879 Statement::make_label_statement(Label* label, source_location location)
2881 return new Label_statement(label, location);
2884 // An unnamed label statement.
2886 class Unnamed_label_statement : public Statement
2889 Unnamed_label_statement(Unnamed_label* label)
2890 : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
2896 do_traverse(Traverse*)
2897 { return TRAVERSE_CONTINUE; }
2900 do_get_tree(Translate_context*)
2901 { return this->label_->get_definition(); }
2905 Unnamed_label* label_;
2908 // Make an unnamed label statement.
2911 Statement::make_unnamed_label_statement(Unnamed_label* label)
2913 return new Unnamed_label_statement(label);
2918 class If_statement : public Statement
2921 If_statement(Expression* cond, Block* then_block, Block* else_block,
2922 source_location location)
2923 : Statement(STATEMENT_IF, location),
2924 cond_(cond), then_block_(then_block), else_block_(else_block)
2929 do_traverse(Traverse*);
2932 do_determine_types();
2935 do_check_types(Gogo*);
2938 do_may_fall_through() const;
2941 do_get_tree(Translate_context*);
2952 If_statement::do_traverse(Traverse* traverse)
2954 if (this->cond_ != NULL)
2956 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
2957 return TRAVERSE_EXIT;
2959 if (this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
2960 return TRAVERSE_EXIT;
2961 if (this->else_block_ != NULL)
2963 if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
2964 return TRAVERSE_EXIT;
2966 return TRAVERSE_CONTINUE;
2970 If_statement::do_determine_types()
2972 if (this->cond_ != NULL)
2974 Type_context context(Type::lookup_bool_type(), false);
2975 this->cond_->determine_type(&context);
2977 this->then_block_->determine_types();
2978 if (this->else_block_ != NULL)
2979 this->else_block_->determine_types();
2985 If_statement::do_check_types(Gogo*)
2987 if (this->cond_ != NULL)
2989 Type* type = this->cond_->type();
2990 if (type->is_error_type())
2991 this->set_is_error();
2992 else if (!type->is_boolean_type())
2993 this->report_error(_("expected boolean expression"));
2997 // Whether the overall statement may fall through.
3000 If_statement::do_may_fall_through() const
3002 return (this->else_block_ == NULL
3003 || this->then_block_->may_fall_through()
3004 || this->else_block_->may_fall_through());
3010 If_statement::do_get_tree(Translate_context* context)
3012 gcc_assert(this->cond_ == NULL
3013 || this->cond_->type()->is_boolean_type()
3014 || this->cond_->type()->is_error_type());
3015 tree cond_tree = (this->cond_ == NULL
3017 : this->cond_->get_tree(context));
3018 tree then_tree = this->then_block_->get_tree(context);
3019 tree else_tree = (this->else_block_ == NULL
3021 : this->else_block_->get_tree(context));
3022 if (cond_tree == error_mark_node
3023 || then_tree == error_mark_node
3024 || else_tree == error_mark_node)
3025 return error_mark_node;
3026 tree ret = build3(COND_EXPR, void_type_node, cond_tree, then_tree,
3028 SET_EXPR_LOCATION(ret, this->location());
3032 // Make an if statement.
3035 Statement::make_if_statement(Expression* cond, Block* then_block,
3036 Block* else_block, source_location location)
3038 return new If_statement(cond, then_block, else_block, location);
3041 // Class Case_clauses::Case_clause.
3046 Case_clauses::Case_clause::traverse(Traverse* traverse)
3048 if (this->cases_ != NULL
3049 && (traverse->traverse_mask()
3050 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3052 if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
3053 return TRAVERSE_EXIT;
3055 if (this->statements_ != NULL)
3057 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3058 return TRAVERSE_EXIT;
3060 return TRAVERSE_CONTINUE;
3063 // Check whether all the case expressions are integer constants.
3066 Case_clauses::Case_clause::is_constant() const
3068 if (this->cases_ != NULL)
3070 for (Expression_list::const_iterator p = this->cases_->begin();
3071 p != this->cases_->end();
3073 if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
3079 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3080 // value we are switching on; it may be NULL. If START_LABEL is not
3081 // NULL, it goes at the start of the statements, after the condition
3082 // test. We branch to FINISH_LABEL at the end of the statements.
3085 Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
3086 Unnamed_label* start_label,
3087 Unnamed_label* finish_label) const
3089 source_location loc = this->location_;
3090 Unnamed_label* next_case_label;
3091 if (this->cases_ == NULL || this->cases_->empty())
3093 gcc_assert(this->is_default_);
3094 next_case_label = NULL;
3098 Expression* cond = NULL;
3100 for (Expression_list::const_iterator p = this->cases_->begin();
3101 p != this->cases_->end();
3104 Expression* this_cond;
3105 if (val_temp == NULL)
3109 Expression* ref = Expression::make_temporary_reference(val_temp,
3111 this_cond = Expression::make_binary(OPERATOR_EQEQ, ref, *p, loc);
3117 cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
3120 Block* then_block = new Block(b, loc);
3121 next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
3122 Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
3124 then_block->add_statement(s);
3126 // if !COND { goto NEXT_CASE_LABEL }
3127 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3128 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3129 b->add_statement(s);
3132 if (start_label != NULL)
3133 b->add_statement(Statement::make_unnamed_label_statement(start_label));
3135 if (this->statements_ != NULL)
3136 b->add_statement(Statement::make_block_statement(this->statements_, loc));
3138 Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
3139 b->add_statement(s);
3141 if (next_case_label != NULL)
3142 b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
3148 Case_clauses::Case_clause::determine_types(Type* type)
3150 if (this->cases_ != NULL)
3152 Type_context case_context(type, false);
3153 for (Expression_list::iterator p = this->cases_->begin();
3154 p != this->cases_->end();
3156 (*p)->determine_type(&case_context);
3158 if (this->statements_ != NULL)
3159 this->statements_->determine_types();
3162 // Check types. Returns false if there was an error.
3165 Case_clauses::Case_clause::check_types(Type* type)
3167 if (this->cases_ != NULL)
3169 for (Expression_list::iterator p = this->cases_->begin();
3170 p != this->cases_->end();
3173 if (!Type::are_assignable(type, (*p)->type(), NULL)
3174 && !Type::are_assignable((*p)->type(), type, NULL))
3176 error_at((*p)->location(),
3177 "type mismatch between switch value and case clause");
3185 // Return true if this clause may fall through to the following
3186 // statements. Note that this is not the same as whether the case
3187 // uses the "fallthrough" keyword.
3190 Case_clauses::Case_clause::may_fall_through() const
3192 if (this->statements_ == NULL)
3194 return this->statements_->may_fall_through();
3197 // Build up the body of a SWITCH_EXPR.
3200 Case_clauses::Case_clause::get_constant_tree(Translate_context* context,
3201 Unnamed_label* break_label,
3202 Case_constants* case_constants,
3203 tree* stmt_list) const
3205 if (this->cases_ != NULL)
3207 for (Expression_list::const_iterator p = this->cases_->begin();
3208 p != this->cases_->end();
3214 if (!(*p)->integer_constant_value(true, ival, &itype))
3216 // Something went wrong. This can happen with a
3217 // negative constant and an unsigned switch value.
3218 gcc_assert(saw_errors());
3221 gcc_assert(itype != NULL);
3222 tree type_tree = itype->get_tree(context->gogo());
3223 tree val = Expression::integer_constant_tree(ival, type_tree);
3226 if (val != error_mark_node)
3228 gcc_assert(TREE_CODE(val) == INTEGER_CST);
3230 std::pair<Case_constants::iterator, bool> ins =
3231 case_constants->insert(val);
3234 // Value was already present.
3235 warning_at(this->location_, 0,
3236 "duplicate case value will never match");
3240 tree label = create_artificial_label(this->location_);
3241 append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
3242 val, NULL_TREE, label),
3248 if (this->is_default_)
3250 tree label = create_artificial_label(this->location_);
3251 append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
3252 NULL_TREE, NULL_TREE, label),
3256 if (this->statements_ != NULL)
3258 tree block_tree = this->statements_->get_tree(context);
3259 if (block_tree != error_mark_node)
3260 append_to_statement_list(block_tree, stmt_list);
3263 if (!this->is_fallthrough_)
3264 append_to_statement_list(break_label->get_goto(this->location_), stmt_list);
3267 // Class Case_clauses.
3272 Case_clauses::traverse(Traverse* traverse)
3274 for (Clauses::iterator p = this->clauses_.begin();
3275 p != this->clauses_.end();
3278 if (p->traverse(traverse) == TRAVERSE_EXIT)
3279 return TRAVERSE_EXIT;
3281 return TRAVERSE_CONTINUE;
3284 // Check whether all the case expressions are constant.
3287 Case_clauses::is_constant() const
3289 for (Clauses::const_iterator p = this->clauses_.begin();
3290 p != this->clauses_.end();
3292 if (!p->is_constant())
3297 // Lower case clauses for a nonconstant switch.
3300 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3301 Unnamed_label* break_label) const
3303 // The default case.
3304 const Case_clause* default_case = NULL;
3306 // The label for the fallthrough of the previous case.
3307 Unnamed_label* last_fallthrough_label = NULL;
3309 // The label for the start of the default case. This is used if the
3310 // case before the default case falls through.
3311 Unnamed_label* default_start_label = NULL;
3313 // The label for the end of the default case. This normally winds
3314 // up as BREAK_LABEL, but it will be different if the default case
3316 Unnamed_label* default_finish_label = NULL;
3318 for (Clauses::const_iterator p = this->clauses_.begin();
3319 p != this->clauses_.end();
3322 // The label to use for the start of the statements for this
3323 // case. This is NULL unless the previous case falls through.
3324 Unnamed_label* start_label = last_fallthrough_label;
3326 // The label to jump to after the end of the statements for this
3328 Unnamed_label* finish_label = break_label;
3330 last_fallthrough_label = NULL;
3331 if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3333 finish_label = new Unnamed_label(p->location());
3334 last_fallthrough_label = finish_label;
3337 if (!p->is_default())
3338 p->lower(b, val_temp, start_label, finish_label);
3341 // We have to move the default case to the end, so that we
3342 // only use it if all the other tests fail.
3344 default_start_label = start_label;
3345 default_finish_label = finish_label;
3349 if (default_case != NULL)
3350 default_case->lower(b, val_temp, default_start_label,
3351 default_finish_label);
3358 Case_clauses::determine_types(Type* type)
3360 for (Clauses::iterator p = this->clauses_.begin();
3361 p != this->clauses_.end();
3363 p->determine_types(type);
3366 // Check types. Returns false if there was an error.
3369 Case_clauses::check_types(Type* type)
3372 for (Clauses::iterator p = this->clauses_.begin();
3373 p != this->clauses_.end();
3376 if (!p->check_types(type))
3382 // Return true if these clauses may fall through to the statements
3383 // following the switch statement.
3386 Case_clauses::may_fall_through() const
3388 bool found_default = false;
3389 for (Clauses::const_iterator p = this->clauses_.begin();
3390 p != this->clauses_.end();
3393 if (p->may_fall_through() && !p->is_fallthrough())
3395 if (p->is_default())
3396 found_default = true;
3398 return !found_default;
3401 // Return a tree when all case expressions are constants.
3404 Case_clauses::get_constant_tree(Translate_context* context,
3405 Unnamed_label* break_label) const
3407 Case_constants case_constants;
3408 tree stmt_list = NULL_TREE;
3409 for (Clauses::const_iterator p = this->clauses_.begin();
3410 p != this->clauses_.end();
3412 p->get_constant_tree(context, break_label, &case_constants,
3417 // A constant switch statement. A Switch_statement is lowered to this
3418 // when all the cases are constants.
3420 class Constant_switch_statement : public Statement
3423 Constant_switch_statement(Expression* val, Case_clauses* clauses,
3424 Unnamed_label* break_label,
3425 source_location location)
3426 : Statement(STATEMENT_CONSTANT_SWITCH, location),
3427 val_(val), clauses_(clauses), break_label_(break_label)
3432 do_traverse(Traverse*);
3435 do_determine_types();
3438 do_check_types(Gogo*);
3441 do_may_fall_through() const;
3444 do_get_tree(Translate_context*);
3447 // The value to switch on.
3449 // The case clauses.
3450 Case_clauses* clauses_;
3451 // The break label, if needed.
3452 Unnamed_label* break_label_;
3458 Constant_switch_statement::do_traverse(Traverse* traverse)
3460 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3461 return TRAVERSE_EXIT;
3462 return this->clauses_->traverse(traverse);
3468 Constant_switch_statement::do_determine_types()
3470 this->val_->determine_type_no_context();
3471 this->clauses_->determine_types(this->val_->type());
3477 Constant_switch_statement::do_check_types(Gogo*)
3479 if (!this->clauses_->check_types(this->val_->type()))
3480 this->set_is_error();
3483 // Return whether this switch may fall through.
3486 Constant_switch_statement::do_may_fall_through() const
3488 if (this->clauses_ == NULL)
3491 // If we have a break label, then some case needed it. That implies
3492 // that the switch statement as a whole can fall through.
3493 if (this->break_label_ != NULL)
3496 return this->clauses_->may_fall_through();
3499 // Convert to GENERIC.
3502 Constant_switch_statement::do_get_tree(Translate_context* context)
3504 tree switch_val_tree = this->val_->get_tree(context);
3506 Unnamed_label* break_label = this->break_label_;
3507 if (break_label == NULL)
3508 break_label = new Unnamed_label(this->location());
3510 tree stmt_list = NULL_TREE;
3511 tree s = build3(SWITCH_EXPR, void_type_node, switch_val_tree,
3512 this->clauses_->get_constant_tree(context, break_label),
3514 SET_EXPR_LOCATION(s, this->location());
3515 append_to_statement_list(s, &stmt_list);
3517 append_to_statement_list(break_label->get_definition(), &stmt_list);
3522 // Class Switch_statement.
3527 Switch_statement::do_traverse(Traverse* traverse)
3529 if (this->val_ != NULL)
3531 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3532 return TRAVERSE_EXIT;
3534 return this->clauses_->traverse(traverse);
3537 // Lower a Switch_statement to a Constant_switch_statement or a series
3538 // of if statements.
3541 Switch_statement::do_lower(Gogo*, Block* enclosing)
3543 source_location loc = this->location();
3545 if (this->val_ != NULL
3546 && (this->val_->is_error_expression()
3547 || this->val_->type()->is_error_type()))
3548 return Statement::make_error_statement(loc);
3550 if (this->val_ != NULL
3551 && this->val_->type()->integer_type() != NULL
3552 && !this->clauses_->empty()
3553 && this->clauses_->is_constant())
3554 return new Constant_switch_statement(this->val_, this->clauses_,
3555 this->break_label_, loc);
3557 Block* b = new Block(enclosing, loc);
3559 if (this->clauses_->empty())
3561 Expression* val = this->val_;
3563 val = Expression::make_boolean(true, loc);
3564 return Statement::make_statement(val);
3567 Temporary_statement* val_temp;
3568 if (this->val_ == NULL)
3572 // var val_temp VAL_TYPE = VAL
3573 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3574 b->add_statement(val_temp);
3577 this->clauses_->lower(b, val_temp, this->break_label());
3579 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3580 b->add_statement(s);
3582 return Statement::make_block_statement(b, loc);
3585 // Return the break label for this switch statement, creating it if
3589 Switch_statement::break_label()
3591 if (this->break_label_ == NULL)
3592 this->break_label_ = new Unnamed_label(this->location());
3593 return this->break_label_;
3596 // Make a switch statement.
3599 Statement::make_switch_statement(Expression* val, source_location location)
3601 return new Switch_statement(val, location);
3604 // Class Type_case_clauses::Type_case_clause.
3609 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3611 if (!this->is_default_
3612 && ((traverse->traverse_mask()
3613 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3614 && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3615 return TRAVERSE_EXIT;
3616 if (this->statements_ != NULL)
3617 return this->statements_->traverse(traverse);
3618 return TRAVERSE_CONTINUE;
3621 // Lower one clause in a type switch. Add statements to the block B.
3622 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3623 // BREAK_LABEL is the label at the end of the type switch.
3624 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3628 Type_case_clauses::Type_case_clause::lower(Block* b,
3629 Temporary_statement* descriptor_temp,
3630 Unnamed_label* break_label,
3631 Unnamed_label** stmts_label) const
3633 source_location loc = this->location_;
3635 Unnamed_label* next_case_label = NULL;
3636 if (!this->is_default_)
3638 Type* type = this->type_;
3641 // The language permits case nil, which is of course a constant
3642 // rather than a type. It will appear here as an invalid
3644 if (type->is_nil_constant_as_type())
3647 Expression::make_temporary_reference(descriptor_temp, loc);
3648 cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3649 Expression::make_nil(loc),
3655 if (type->interface_type() == NULL)
3657 // func ifacetypeeq(*descriptor, *descriptor) bool
3658 static Named_object* ifacetypeeq;
3659 if (ifacetypeeq == NULL)
3661 const source_location bloc = BUILTINS_LOCATION;
3662 Typed_identifier_list* param_types =
3663 new Typed_identifier_list();
3664 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3665 param_types->push_back(Typed_identifier("a", descriptor_type,
3667 param_types->push_back(Typed_identifier("b", descriptor_type,
3669 Typed_identifier_list* ret_types =
3670 new Typed_identifier_list();
3671 Type* bool_type = Type::lookup_bool_type();
3672 ret_types->push_back(Typed_identifier("", bool_type, bloc));
3673 Function_type* fntype = Type::make_function_type(NULL,
3678 Named_object::make_function_declaration("ifacetypeeq", NULL,
3680 const char* n = "runtime.ifacetypeeq";
3681 ifacetypeeq->func_declaration_value()->set_asm_name(n);
3684 // ifacetypeeq(descriptor_temp, DESCRIPTOR)
3685 func = Expression::make_func_reference(ifacetypeeq, NULL, loc);
3689 // func ifaceI2Tp(*descriptor, *descriptor) bool
3690 static Named_object* ifaceI2Tp;
3691 if (ifaceI2Tp == NULL)
3693 const source_location bloc = BUILTINS_LOCATION;
3694 Typed_identifier_list* param_types =
3695 new Typed_identifier_list();
3696 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3697 param_types->push_back(Typed_identifier("a", descriptor_type,
3699 param_types->push_back(Typed_identifier("b", descriptor_type,
3701 Typed_identifier_list* ret_types =
3702 new Typed_identifier_list();
3703 Type* bool_type = Type::lookup_bool_type();
3704 ret_types->push_back(Typed_identifier("", bool_type, bloc));
3705 Function_type* fntype = Type::make_function_type(NULL,
3710 Named_object::make_function_declaration("ifaceI2Tp", NULL,
3712 const char* n = "runtime.ifaceI2Tp";
3713 ifaceI2Tp->func_declaration_value()->set_asm_name(n);
3716 // ifaceI2Tp(descriptor_temp, DESCRIPTOR)
3717 func = Expression::make_func_reference(ifaceI2Tp, NULL, loc);
3719 Expression_list* params = new Expression_list();
3720 params->push_back(Expression::make_type_descriptor(type, loc));
3722 Expression::make_temporary_reference(descriptor_temp, loc);
3723 params->push_back(ref);
3724 cond = Expression::make_call(func, params, false, loc);
3727 Unnamed_label* dest;
3728 if (!this->is_fallthrough_)
3730 // if !COND { goto NEXT_CASE_LABEL }
3731 next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
3732 dest = next_case_label;
3733 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3737 // if COND { goto STMTS_LABEL }
3738 gcc_assert(stmts_label != NULL);
3739 if (*stmts_label == NULL)
3740 *stmts_label = new Unnamed_label(UNKNOWN_LOCATION);
3741 dest = *stmts_label;
3743 Block* then_block = new Block(b, loc);
3744 Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
3745 then_block->add_statement(s);
3746 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3747 b->add_statement(s);
3750 if (this->statements_ != NULL
3751 || (!this->is_fallthrough_
3752 && stmts_label != NULL
3753 && *stmts_label != NULL))
3755 gcc_assert(!this->is_fallthrough_);
3756 if (stmts_label != NULL && *stmts_label != NULL)
3758 gcc_assert(!this->is_default_);
3759 if (this->statements_ != NULL)
3760 (*stmts_label)->set_location(this->statements_->start_location());
3761 Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
3762 b->add_statement(s);
3763 *stmts_label = NULL;
3765 if (this->statements_ != NULL)
3766 b->add_statement(Statement::make_block_statement(this->statements_,
3770 if (this->is_fallthrough_)
3771 gcc_assert(next_case_label == NULL);
3774 source_location gloc = (this->statements_ == NULL
3776 : this->statements_->end_location());
3777 b->add_statement(Statement::make_goto_unnamed_statement(break_label,
3779 if (next_case_label != NULL)
3782 Statement::make_unnamed_label_statement(next_case_label);
3783 b->add_statement(s);
3788 // Class Type_case_clauses.
3793 Type_case_clauses::traverse(Traverse* traverse)
3795 for (Type_clauses::iterator p = this->clauses_.begin();
3796 p != this->clauses_.end();
3799 if (p->traverse(traverse) == TRAVERSE_EXIT)
3800 return TRAVERSE_EXIT;
3802 return TRAVERSE_CONTINUE;
3805 // Check for duplicate types.
3808 Type_case_clauses::check_duplicates() const
3810 typedef Unordered_set_hash(const Type*, Type_hash_identical,
3811 Type_identical) Types_seen;
3812 Types_seen types_seen;
3813 for (Type_clauses::const_iterator p = this->clauses_.begin();
3814 p != this->clauses_.end();
3817 Type* t = p->type();
3820 if (t->is_nil_constant_as_type())
3821 t = Type::make_nil_type();
3822 std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
3824 error_at(p->location(), "duplicate type in switch");
3828 // Lower the clauses in a type switch. Add statements to the block B.
3829 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3830 // BREAK_LABEL is the label at the end of the type switch.
3833 Type_case_clauses::lower(Block* b, Temporary_statement* descriptor_temp,
3834 Unnamed_label* break_label) const
3836 const Type_case_clause* default_case = NULL;
3838 Unnamed_label* stmts_label = NULL;
3839 for (Type_clauses::const_iterator p = this->clauses_.begin();
3840 p != this->clauses_.end();
3843 if (!p->is_default())
3844 p->lower(b, descriptor_temp, break_label, &stmts_label);
3847 // We are generating a series of tests, which means that we
3848 // need to move the default case to the end.
3852 gcc_assert(stmts_label == NULL);
3854 if (default_case != NULL)
3855 default_case->lower(b, descriptor_temp, break_label, NULL);
3858 // Class Type_switch_statement.
3863 Type_switch_statement::do_traverse(Traverse* traverse)
3865 if (this->var_ == NULL)
3867 if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
3868 return TRAVERSE_EXIT;
3870 if (this->clauses_ != NULL)
3871 return this->clauses_->traverse(traverse);
3872 return TRAVERSE_CONTINUE;
3875 // Lower a type switch statement to a series of if statements. The gc
3876 // compiler is able to generate a table in some cases. However, that
3877 // does not work for us because we may have type descriptors in
3878 // different shared libraries, so we can't compare them with simple
3879 // equality testing.
3882 Type_switch_statement::do_lower(Gogo*, Block* enclosing)
3884 const source_location loc = this->location();
3886 if (this->clauses_ != NULL)
3887 this->clauses_->check_duplicates();
3889 Block* b = new Block(enclosing, loc);
3891 Type* val_type = (this->var_ != NULL
3892 ? this->var_->var_value()->type()
3893 : this->expr_->type());
3895 // var descriptor_temp DESCRIPTOR_TYPE
3896 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3897 Temporary_statement* descriptor_temp =
3898 Statement::make_temporary(descriptor_type, NULL, loc);
3899 b->add_statement(descriptor_temp);
3901 if (val_type->interface_type() == NULL)
3903 // Doing a type switch on a non-interface type. Should we issue
3904 // a warning for this case?
3905 // descriptor_temp = DESCRIPTOR
3906 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3908 Expression* rhs = Expression::make_type_descriptor(val_type, loc);
3909 Statement* s = Statement::make_assignment(lhs, rhs, loc);
3910 b->add_statement(s);
3914 const source_location bloc = BUILTINS_LOCATION;
3916 // func {efacetype,ifacetype}(*interface) *descriptor
3917 // FIXME: This should be inlined.
3918 Typed_identifier_list* param_types = new Typed_identifier_list();
3919 param_types->push_back(Typed_identifier("i", val_type, bloc));
3920 Typed_identifier_list* ret_types = new Typed_identifier_list();
3921 ret_types->push_back(Typed_identifier("", descriptor_type, bloc));
3922 Function_type* fntype = Type::make_function_type(NULL, param_types,
3924 bool is_empty = val_type->interface_type()->is_empty();
3925 const char* fnname = is_empty ? "efacetype" : "ifacetype";
3927 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
3928 const char* asm_name = (is_empty
3929 ? "runtime.efacetype"
3930 : "runtime.ifacetype");
3931 fn->func_declaration_value()->set_asm_name(asm_name);
3933 // descriptor_temp = ifacetype(val_temp)
3934 Expression* func = Expression::make_func_reference(fn, NULL, loc);
3935 Expression_list* params = new Expression_list();
3937 if (this->var_ == NULL)
3940 ref = Expression::make_var_reference(this->var_, loc);
3941 params->push_back(ref);
3942 Expression* call = Expression::make_call(func, params, false, loc);
3943 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3945 Statement* s = Statement::make_assignment(lhs, call, loc);
3946 b->add_statement(s);
3949 if (this->clauses_ != NULL)
3950 this->clauses_->lower(b, descriptor_temp, this->break_label());
3952 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3953 b->add_statement(s);
3955 return Statement::make_block_statement(b, loc);
3958 // Return the break label for this type switch statement, creating it
3962 Type_switch_statement::break_label()
3964 if (this->break_label_ == NULL)
3965 this->break_label_ = new Unnamed_label(this->location());
3966 return this->break_label_;
3969 // Make a type switch statement.
3971 Type_switch_statement*
3972 Statement::make_type_switch_statement(Named_object* var, Expression* expr,
3973 source_location location)
3975 return new Type_switch_statement(var, expr, location);
3978 // Class Select_clauses::Select_clause.
3983 Select_clauses::Select_clause::traverse(Traverse* traverse)
3985 if (!this->is_lowered_
3986 && (traverse->traverse_mask()
3987 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3989 if (this->channel_ != NULL)
3991 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
3992 return TRAVERSE_EXIT;
3994 if (this->val_ != NULL)
3996 if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
3997 return TRAVERSE_EXIT;
4000 if (this->statements_ != NULL)
4002 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
4003 return TRAVERSE_EXIT;
4005 return TRAVERSE_CONTINUE;
4008 // Lowering. Here we pull out the channel and the send values, to
4009 // enforce the order of evaluation. We also add explicit send and
4010 // receive statements to the clauses.
4013 Select_clauses::Select_clause::lower(Block* b)
4015 if (this->is_default_)
4017 gcc_assert(this->channel_ == NULL && this->val_ == NULL);
4018 this->is_lowered_ = true;
4022 source_location loc = this->location_;
4024 // Evaluate the channel before the select statement.
4025 Temporary_statement* channel_temp = Statement::make_temporary(NULL,
4028 b->add_statement(channel_temp);
4029 this->channel_ = Expression::make_temporary_reference(channel_temp, loc);
4031 // If this is a send clause, evaluate the value to send before the
4032 // select statement.
4033 Temporary_statement* val_temp = NULL;
4036 val_temp = Statement::make_temporary(NULL, this->val_, loc);
4037 b->add_statement(val_temp);
4040 // Add the send or receive before the rest of the statements if any.
4041 Block *init = new Block(b, loc);
4042 Expression* ref = Expression::make_temporary_reference(channel_temp, loc);
4045 Expression* ref2 = Expression::make_temporary_reference(val_temp, loc);
4046 Send_expression* send = Expression::make_send(ref, ref2, loc);
4047 send->discarding_value();
4048 send->set_for_select();
4049 init->add_statement(Statement::make_statement(send));
4053 Receive_expression* recv = Expression::make_receive(ref, loc);
4054 recv->set_for_select();
4055 if (this->val_ != NULL)
4057 gcc_assert(this->var_ == NULL);
4058 init->add_statement(Statement::make_assignment(this->val_, recv,
4061 else if (this->var_ != NULL)
4063 this->var_->var_value()->set_init(recv);
4064 this->var_->var_value()->clear_type_from_chan_element();
4068 recv->discarding_value();
4069 init->add_statement(Statement::make_statement(recv));
4073 if (this->statements_ != NULL)
4074 init->add_statement(Statement::make_block_statement(this->statements_,
4077 this->statements_ = init;
4079 // Now all references should be handled through the statements, not
4081 this->is_lowered_ = true;
4089 Select_clauses::Select_clause::determine_types()
4091 gcc_assert(this->is_lowered_);
4092 if (this->statements_ != NULL)
4093 this->statements_->determine_types();
4096 // Whether this clause may fall through to the statement which follows
4097 // the overall select statement.
4100 Select_clauses::Select_clause::may_fall_through() const
4102 if (this->statements_ == NULL)
4104 return this->statements_->may_fall_through();
4107 // Return a tree for the statements to execute.
4110 Select_clauses::Select_clause::get_statements_tree(Translate_context* context)
4112 if (this->statements_ == NULL)
4114 return this->statements_->get_tree(context);
4117 // Class Select_clauses.
4122 Select_clauses::traverse(Traverse* traverse)
4124 for (Clauses::iterator p = this->clauses_.begin();
4125 p != this->clauses_.end();
4128 if (p->traverse(traverse) == TRAVERSE_EXIT)
4129 return TRAVERSE_EXIT;
4131 return TRAVERSE_CONTINUE;
4134 // Lowering. Here we pull out the channel and the send values, to
4135 // enforce the order of evaluation. We also add explicit send and
4136 // receive statements to the clauses.
4139 Select_clauses::lower(Block* b)
4141 for (Clauses::iterator p = this->clauses_.begin();
4142 p != this->clauses_.end();
4150 Select_clauses::determine_types()
4152 for (Clauses::iterator p = this->clauses_.begin();
4153 p != this->clauses_.end();
4155 p->determine_types();
4158 // Return whether these select clauses fall through to the statement
4159 // following the overall select statement.
4162 Select_clauses::may_fall_through() const
4164 for (Clauses::const_iterator p = this->clauses_.begin();
4165 p != this->clauses_.end();
4167 if (p->may_fall_through())
4172 // Return a tree. We build a call to
4173 // size_t __go_select(size_t count, _Bool has_default,
4174 // channel* channels, _Bool* is_send)
4176 // There are COUNT entries in the CHANNELS and IS_SEND arrays. The
4177 // value in the IS_SEND array is true for send, false for receive.
4178 // __go_select returns an integer from 0 to COUNT, inclusive. A
4179 // return of 0 means that the default case should be run; this only
4180 // happens if HAS_DEFAULT is non-zero. Otherwise the number indicates
4183 // FIXME: This doesn't handle channels which send interface types
4184 // where the receiver has a static type which matches that interface.
4187 Select_clauses::get_tree(Translate_context* context,
4188 Unnamed_label *break_label,
4189 source_location location)
4191 size_t count = this->clauses_.size();
4192 VEC(constructor_elt, gc)* chan_init = VEC_alloc(constructor_elt, gc, count);
4193 VEC(constructor_elt, gc)* is_send_init = VEC_alloc(constructor_elt, gc,
4195 Select_clause* default_clause = NULL;
4196 tree final_stmt_list = NULL_TREE;
4197 tree channel_type_tree = NULL_TREE;
4200 for (Clauses::iterator p = this->clauses_.begin();
4201 p != this->clauses_.end();
4204 if (p->is_default())
4206 default_clause = &*p;
4211 if (p->channel()->type()->channel_type() == NULL)
4213 // We should have given an error in the send or receive
4214 // statement we created via lowering.
4215 gcc_assert(saw_errors());
4216 return error_mark_node;
4219 tree channel_tree = p->channel()->get_tree(context);
4220 if (channel_tree == error_mark_node)
4221 return error_mark_node;
4222 channel_type_tree = TREE_TYPE(channel_tree);
4224 constructor_elt* elt = VEC_quick_push(constructor_elt, chan_init, NULL);
4225 elt->index = build_int_cstu(sizetype, i);
4226 elt->value = channel_tree;
4228 elt = VEC_quick_push(constructor_elt, is_send_init, NULL);
4229 elt->index = build_int_cstu(sizetype, i);
4230 elt->value = p->is_send() ? boolean_true_node : boolean_false_node;
4234 gcc_assert(i == count);
4236 if (i == 0 && default_clause != NULL)
4238 // There is only a default clause.
4239 gcc_assert(final_stmt_list == NULL_TREE);
4240 tree stmt_list = NULL_TREE;
4241 append_to_statement_list(default_clause->get_statements_tree(context),
4243 append_to_statement_list(break_label->get_definition(), &stmt_list);
4247 tree pointer_chan_type_tree = (channel_type_tree == NULL_TREE
4249 : build_pointer_type(channel_type_tree));
4251 tree pointer_boolean_type_tree = build_pointer_type(boolean_type_node);
4256 chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4258 is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4263 tree index_type_tree = build_index_type(size_int(count - 1));
4264 tree chan_array_type_tree = build_array_type(channel_type_tree,
4266 tree chan_constructor = build_constructor(chan_array_type_tree,
4268 tree chan_var = create_tmp_var(chan_array_type_tree, "CHAN");
4269 DECL_IGNORED_P(chan_var) = 0;
4270 DECL_INITIAL(chan_var) = chan_constructor;
4271 DECL_SOURCE_LOCATION(chan_var) = location;
4272 TREE_ADDRESSABLE(chan_var) = 1;
4273 tree decl_expr = build1(DECL_EXPR, void_type_node, chan_var);
4274 SET_EXPR_LOCATION(decl_expr, location);
4275 append_to_statement_list(decl_expr, &final_stmt_list);
4277 tree is_send_array_type_tree = build_array_type(boolean_type_node,
4279 tree is_send_constructor = build_constructor(is_send_array_type_tree,
4281 tree is_send_var = create_tmp_var(is_send_array_type_tree, "ISSEND");
4282 DECL_IGNORED_P(is_send_var) = 0;
4283 DECL_INITIAL(is_send_var) = is_send_constructor;
4284 DECL_SOURCE_LOCATION(is_send_var) = location;
4285 TREE_ADDRESSABLE(is_send_var) = 1;
4286 decl_expr = build1(DECL_EXPR, void_type_node, is_send_var);
4287 SET_EXPR_LOCATION(decl_expr, location);
4288 append_to_statement_list(decl_expr, &final_stmt_list);
4290 chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4291 build_fold_addr_expr_loc(location,
4293 is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4294 build_fold_addr_expr_loc(location,
4298 static tree select_fndecl;
4299 tree call = Gogo::call_builtin(&select_fndecl,
4307 (default_clause == NULL
4308 ? boolean_false_node
4309 : boolean_true_node),
4310 pointer_chan_type_tree,
4312 pointer_boolean_type_tree,
4314 if (call == error_mark_node)
4315 return error_mark_node;
4317 tree stmt_list = NULL_TREE;
4319 if (default_clause != NULL)
4320 this->add_clause_tree(context, 0, default_clause, break_label, &stmt_list);
4323 for (Clauses::iterator p = this->clauses_.begin();
4324 p != this->clauses_.end();
4327 if (!p->is_default())
4329 this->add_clause_tree(context, i, &*p, break_label, &stmt_list);
4334 append_to_statement_list(break_label->get_definition(), &stmt_list);
4336 tree switch_stmt = build3(SWITCH_EXPR, sizetype, call, stmt_list, NULL_TREE);
4337 SET_EXPR_LOCATION(switch_stmt, location);
4338 append_to_statement_list(switch_stmt, &final_stmt_list);
4340 return final_stmt_list;
4343 // Add the tree for CLAUSE to STMT_LIST.
4346 Select_clauses::add_clause_tree(Translate_context* context, int case_index,
4347 Select_clause* clause,
4348 Unnamed_label* bottom_label, tree* stmt_list)
4350 tree label = create_artificial_label(clause->location());
4351 append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
4352 build_int_cst(sizetype, case_index),
4355 append_to_statement_list(clause->get_statements_tree(context), stmt_list);
4356 tree g = bottom_label->get_goto(clause->statements() == NULL
4357 ? clause->location()
4358 : clause->statements()->end_location());
4359 append_to_statement_list(g, stmt_list);
4362 // Class Select_statement.
4364 // Return the break label for this switch statement, creating it if
4368 Select_statement::break_label()
4370 if (this->break_label_ == NULL)
4371 this->break_label_ = new Unnamed_label(this->location());
4372 return this->break_label_;
4375 // Lower a select statement. This will still return a select
4376 // statement, but it will be modified to implement the order of
4377 // evaluation rules, and to include the send and receive statements as
4378 // explicit statements in the clauses.
4381 Select_statement::do_lower(Gogo*, Block* enclosing)
4383 if (this->is_lowered_)
4385 Block* b = new Block(enclosing, this->location());
4386 this->clauses_->lower(b);
4387 this->is_lowered_ = true;
4388 b->add_statement(this);
4389 return Statement::make_block_statement(b, this->location());
4392 // Return the tree for a select statement.
4395 Select_statement::do_get_tree(Translate_context* context)
4397 return this->clauses_->get_tree(context, this->break_label(),
4401 // Make a select statement.
4404 Statement::make_select_statement(source_location location)
4406 return new Select_statement(location);
4409 // Class For_statement.
4414 For_statement::do_traverse(Traverse* traverse)
4416 if (this->init_ != NULL)
4418 if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
4419 return TRAVERSE_EXIT;
4421 if (this->cond_ != NULL)
4423 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
4424 return TRAVERSE_EXIT;
4426 if (this->post_ != NULL)
4428 if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
4429 return TRAVERSE_EXIT;
4431 return this->statements_->traverse(traverse);
4434 // Lower a For_statement into if statements and gotos. Getting rid of
4435 // complex statements make it easier to handle garbage collection.
4438 For_statement::do_lower(Gogo*, Block* enclosing)
4441 source_location loc = this->location();
4443 Block* b = new Block(enclosing, this->location());
4444 if (this->init_ != NULL)
4446 s = Statement::make_block_statement(this->init_,
4447 this->init_->start_location());
4448 b->add_statement(s);
4451 Unnamed_label* entry = NULL;
4452 if (this->cond_ != NULL)
4454 entry = new Unnamed_label(this->location());
4455 b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
4458 Unnamed_label* top = new Unnamed_label(this->location());
4459 b->add_statement(Statement::make_unnamed_label_statement(top));
4461 s = Statement::make_block_statement(this->statements_,
4462 this->statements_->start_location());
4463 b->add_statement(s);
4465 source_location end_loc = this->statements_->end_location();
4467 Unnamed_label* cont = this->continue_label_;
4469 b->add_statement(Statement::make_unnamed_label_statement(cont));
4471 if (this->post_ != NULL)
4473 s = Statement::make_block_statement(this->post_,
4474 this->post_->start_location());
4475 b->add_statement(s);
4476 end_loc = this->post_->end_location();
4479 if (this->cond_ == NULL)
4480 b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
4483 b->add_statement(Statement::make_unnamed_label_statement(entry));
4485 source_location cond_loc = this->cond_->location();
4486 Block* then_block = new Block(b, cond_loc);
4487 s = Statement::make_goto_unnamed_statement(top, cond_loc);
4488 then_block->add_statement(s);
4490 s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
4491 b->add_statement(s);
4494 Unnamed_label* brk = this->break_label_;
4496 b->add_statement(Statement::make_unnamed_label_statement(brk));
4498 b->set_end_location(end_loc);
4500 return Statement::make_block_statement(b, loc);
4503 // Return the break label, creating it if necessary.
4506 For_statement::break_label()
4508 if (this->break_label_ == NULL)
4509 this->break_label_ = new Unnamed_label(this->location());
4510 return this->break_label_;
4513 // Return the continue LABEL_EXPR.
4516 For_statement::continue_label()
4518 if (this->continue_label_ == NULL)
4519 this->continue_label_ = new Unnamed_label(this->location());
4520 return this->continue_label_;
4523 // Set the break and continue labels a for statement. This is used
4524 // when lowering a for range statement.
4527 For_statement::set_break_continue_labels(Unnamed_label* break_label,
4528 Unnamed_label* continue_label)
4530 gcc_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
4531 this->break_label_ = break_label;
4532 this->continue_label_ = continue_label;
4535 // Make a for statement.
4538 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
4539 source_location location)
4541 return new For_statement(init, cond, post, location);
4544 // Class For_range_statement.
4549 For_range_statement::do_traverse(Traverse* traverse)
4551 if (this->traverse_expression(traverse, &this->index_var_) == TRAVERSE_EXIT)
4552 return TRAVERSE_EXIT;
4553 if (this->value_var_ != NULL)
4555 if (this->traverse_expression(traverse, &this->value_var_)
4557 return TRAVERSE_EXIT;
4559 if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
4560 return TRAVERSE_EXIT;
4561 return this->statements_->traverse(traverse);
4564 // Lower a for range statement. For simplicity we lower this into a
4565 // for statement, which will then be lowered in turn to goto
4569 For_range_statement::do_lower(Gogo* gogo, Block* enclosing)
4571 Type* range_type = this->range_->type();
4572 if (range_type->points_to() != NULL
4573 && range_type->points_to()->array_type() != NULL
4574 && !range_type->points_to()->is_open_array_type())
4575 range_type = range_type->points_to();
4578 Type* value_type = NULL;
4579 if (range_type->array_type() != NULL)
4581 index_type = Type::lookup_integer_type("int");
4582 value_type = range_type->array_type()->element_type();
4584 else if (range_type->is_string_type())
4586 index_type = Type::lookup_integer_type("int");
4587 value_type = index_type;
4589 else if (range_type->map_type() != NULL)
4591 index_type = range_type->map_type()->key_type();
4592 value_type = range_type->map_type()->val_type();
4594 else if (range_type->channel_type() != NULL)
4596 index_type = range_type->channel_type()->element_type();
4597 if (this->value_var_ != NULL)
4599 if (!this->value_var_->type()->is_error_type())
4600 this->report_error(_("too many variables for range clause "
4602 return Statement::make_error_statement(this->location());
4607 this->report_error(_("range clause must have "
4608 "array, slice, setring, map, or channel type"));
4609 return Statement::make_error_statement(this->location());
4612 source_location loc = this->location();
4613 Block* temp_block = new Block(enclosing, loc);
4615 Named_object* range_object = NULL;
4616 Temporary_statement* range_temp = NULL;
4617 Var_expression* ve = this->range_->var_expression();
4619 range_object = ve->named_object();
4622 range_temp = Statement::make_temporary(NULL, this->range_, loc);
4623 temp_block->add_statement(range_temp);
4626 Temporary_statement* index_temp = Statement::make_temporary(index_type,
4628 temp_block->add_statement(index_temp);
4630 Temporary_statement* value_temp = NULL;
4631 if (this->value_var_ != NULL)
4633 value_temp = Statement::make_temporary(value_type, NULL, loc);
4634 temp_block->add_statement(value_temp);
4637 Block* body = new Block(temp_block, loc);
4644 // Arrange to do a loop appropriate for the type. We will produce
4645 // for INIT ; COND ; POST {
4647 // INDEX = INDEX_TEMP
4648 // VALUE = VALUE_TEMP // If there is a value
4649 // original statements
4652 if (range_type->array_type() != NULL)
4653 this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
4654 index_temp, value_temp, &init, &cond, &iter_init,
4656 else if (range_type->is_string_type())
4657 this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
4658 index_temp, value_temp, &init, &cond, &iter_init,
4660 else if (range_type->map_type() != NULL)
4661 this->lower_range_map(gogo, temp_block, body, range_object, range_temp,
4662 index_temp, value_temp, &init, &cond, &iter_init,
4664 else if (range_type->channel_type() != NULL)
4665 this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
4666 index_temp, value_temp, &init, &cond, &iter_init,
4671 if (iter_init != NULL)
4672 body->add_statement(Statement::make_block_statement(iter_init, loc));
4675 Expression* index_ref = Expression::make_temporary_reference(index_temp, loc);
4676 if (this->value_var_ == NULL)
4678 assign = Statement::make_assignment(this->index_var_, index_ref, loc);
4682 Expression_list* lhs = new Expression_list();
4683 lhs->push_back(this->index_var_);
4684 lhs->push_back(this->value_var_);
4686 Expression_list* rhs = new Expression_list();
4687 rhs->push_back(index_ref);
4688 rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4690 assign = Statement::make_tuple_assignment(lhs, rhs, loc);
4692 body->add_statement(assign);
4694 body->add_statement(Statement::make_block_statement(this->statements_, loc));
4696 body->set_end_location(this->statements_->end_location());
4698 For_statement* loop = Statement::make_for_statement(init, cond, post,
4700 loop->add_statements(body);
4701 loop->set_break_continue_labels(this->break_label_, this->continue_label_);
4703 temp_block->add_statement(loop);
4705 return Statement::make_block_statement(temp_block, loc);
4708 // Return a reference to the range, which may be in RANGE_OBJECT or in
4712 For_range_statement::make_range_ref(Named_object* range_object,
4713 Temporary_statement* range_temp,
4714 source_location loc)
4716 if (range_object != NULL)
4717 return Expression::make_var_reference(range_object, loc);
4719 return Expression::make_temporary_reference(range_temp, loc);
4722 // Return a call to the predeclared function FUNCNAME passing a
4723 // reference to the temporary variable ARG.
4726 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
4728 source_location loc)
4730 Named_object* no = gogo->lookup_global(funcname);
4731 gcc_assert(no != NULL && no->is_function_declaration());
4732 Expression* func = Expression::make_func_reference(no, NULL, loc);
4733 Expression_list* params = new Expression_list();
4734 params->push_back(arg);
4735 return Expression::make_call(func, params, false, loc);
4738 // Lower a for range over an array or slice.
4741 For_range_statement::lower_range_array(Gogo* gogo,
4744 Named_object* range_object,
4745 Temporary_statement* range_temp,
4746 Temporary_statement* index_temp,
4747 Temporary_statement* value_temp,
4753 source_location loc = this->location();
4755 // The loop we generate:
4756 // len_temp := len(range)
4757 // for index_temp = 0; index_temp < len_temp; index_temp++ {
4758 // value_temp = range[index_temp]
4759 // index = index_temp
4760 // value = value_temp
4766 // len_temp = len(range)
4769 Block* init = new Block(enclosing, loc);
4771 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
4772 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
4773 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
4775 init->add_statement(len_temp);
4778 mpz_init_set_ui(zval, 0UL);
4779 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4782 ref = Expression::make_temporary_reference(index_temp, loc);
4783 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4784 init->add_statement(s);
4789 // index_temp < len_temp
4791 ref = Expression::make_temporary_reference(index_temp, loc);
4792 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
4793 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
4797 // Set *PITER_INIT to
4798 // value_temp = range[index_temp]
4800 Block* iter_init = NULL;
4801 if (value_temp != NULL)
4803 iter_init = new Block(body_block, loc);
4805 ref = this->make_range_ref(range_object, range_temp, loc);
4806 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
4807 Expression* index = Expression::make_index(ref, ref2, NULL, loc);
4809 ref = Expression::make_temporary_reference(value_temp, loc);
4810 s = Statement::make_assignment(ref, index, loc);
4812 iter_init->add_statement(s);
4814 *piter_init = iter_init;
4819 Block* post = new Block(enclosing, loc);
4820 ref = Expression::make_temporary_reference(index_temp, loc);
4821 s = Statement::make_inc_statement(ref);
4822 post->add_statement(s);
4826 // Lower a for range over a string.
4829 For_range_statement::lower_range_string(Gogo* gogo,
4832 Named_object* range_object,
4833 Temporary_statement* range_temp,
4834 Temporary_statement* index_temp,
4835 Temporary_statement* value_temp,
4841 source_location loc = this->location();
4843 // The loop we generate:
4844 // var next_index_temp int
4845 // for index_temp = 0; ; index_temp = next_index_temp {
4846 // next_index_temp, value_temp = stringiter2(range, index_temp)
4847 // if next_index_temp == 0 {
4850 // index = index_temp
4851 // value = value_temp
4856 // var next_index_temp int
4859 Block* init = new Block(enclosing, loc);
4861 Temporary_statement* next_index_temp =
4862 Statement::make_temporary(index_temp->type(), NULL, loc);
4863 init->add_statement(next_index_temp);
4866 mpz_init_set_ui(zval, 0UL);
4867 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4869 Expression* ref = Expression::make_temporary_reference(index_temp, loc);
4870 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4872 init->add_statement(s);
4875 // The loop has no condition.
4879 // Set *PITER_INIT to
4880 // next_index_temp = runtime.stringiter(range, index_temp)
4882 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
4884 // if next_index_temp == 0 {
4888 Block* iter_init = new Block(body_block, loc);
4891 if (value_temp == NULL)
4893 static Named_object* stringiter;
4894 if (stringiter == NULL)
4896 source_location bloc = BUILTINS_LOCATION;
4897 Type* int_type = gogo->lookup_global("int")->type_value();
4899 Typed_identifier_list* params = new Typed_identifier_list();
4900 params->push_back(Typed_identifier("s", Type::make_string_type(),
4902 params->push_back(Typed_identifier("k", int_type, bloc));
4904 Typed_identifier_list* results = new Typed_identifier_list();
4905 results->push_back(Typed_identifier("", int_type, bloc));
4907 Function_type* fntype = Type::make_function_type(NULL, params,
4909 stringiter = Named_object::make_function_declaration("stringiter",
4912 const char* n = "runtime.stringiter";
4913 stringiter->func_declaration_value()->set_asm_name(n);
4919 static Named_object* stringiter2;
4920 if (stringiter2 == NULL)
4922 source_location bloc = BUILTINS_LOCATION;
4923 Type* int_type = gogo->lookup_global("int")->type_value();
4925 Typed_identifier_list* params = new Typed_identifier_list();
4926 params->push_back(Typed_identifier("s", Type::make_string_type(),
4928 params->push_back(Typed_identifier("k", int_type, bloc));
4930 Typed_identifier_list* results = new Typed_identifier_list();
4931 results->push_back(Typed_identifier("", int_type, bloc));
4932 results->push_back(Typed_identifier("", int_type, bloc));
4934 Function_type* fntype = Type::make_function_type(NULL, params,
4936 stringiter2 = Named_object::make_function_declaration("stringiter",
4939 const char* n = "runtime.stringiter2";
4940 stringiter2->func_declaration_value()->set_asm_name(n);
4945 Expression* func = Expression::make_func_reference(no, NULL, loc);
4946 Expression_list* params = new Expression_list();
4947 params->push_back(this->make_range_ref(range_object, range_temp, loc));
4948 params->push_back(Expression::make_temporary_reference(index_temp, loc));
4949 Call_expression* call = Expression::make_call(func, params, false, loc);
4951 if (value_temp == NULL)
4953 ref = Expression::make_temporary_reference(next_index_temp, loc);
4954 s = Statement::make_assignment(ref, call, loc);
4958 Expression_list* lhs = new Expression_list();
4959 lhs->push_back(Expression::make_temporary_reference(next_index_temp,
4961 lhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4963 Expression_list* rhs = new Expression_list();
4964 rhs->push_back(Expression::make_call_result(call, 0));
4965 rhs->push_back(Expression::make_call_result(call, 1));
4967 s = Statement::make_tuple_assignment(lhs, rhs, loc);
4969 iter_init->add_statement(s);
4971 ref = Expression::make_temporary_reference(next_index_temp, loc);
4972 zexpr = Expression::make_integer(&zval, NULL, loc);
4974 Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
4976 Block* then_block = new Block(iter_init, loc);
4977 s = Statement::make_break_statement(this->break_label(), loc);
4978 then_block->add_statement(s);
4980 s = Statement::make_if_statement(equals, then_block, NULL, loc);
4981 iter_init->add_statement(s);
4983 *piter_init = iter_init;
4986 // index_temp = next_index_temp
4988 Block* post = new Block(enclosing, loc);
4990 Expression* lhs = Expression::make_temporary_reference(index_temp, loc);
4991 Expression* rhs = Expression::make_temporary_reference(next_index_temp, loc);
4992 s = Statement::make_assignment(lhs, rhs, loc);
4994 post->add_statement(s);
4998 // Lower a for range over a map.
5001 For_range_statement::lower_range_map(Gogo* gogo,
5004 Named_object* range_object,
5005 Temporary_statement* range_temp,
5006 Temporary_statement* index_temp,
5007 Temporary_statement* value_temp,
5013 source_location loc = this->location();
5015 // The runtime uses a struct to handle ranges over a map. The
5016 // struct is four pointers long. The first pointer is NULL when we
5017 // have completed the iteration.
5019 // The loop we generate:
5020 // var hiter map_iteration_struct
5021 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
5022 // mapiter2(hiter, &index_temp, &value_temp)
5023 // index = index_temp
5024 // value = value_temp
5029 // var hiter map_iteration_struct
5030 // runtime.mapiterinit(range, &hiter)
5032 Block* init = new Block(enclosing, loc);
5034 const unsigned long map_iteration_size = 4;
5037 mpz_init_set_ui(ival, map_iteration_size);
5038 Expression* iexpr = Expression::make_integer(&ival, NULL, loc);
5041 Type* byte_type = gogo->lookup_global("byte")->type_value();
5042 Type* ptr_type = Type::make_pointer_type(byte_type);
5044 Type* map_iteration_type = Type::make_array_type(ptr_type, iexpr);
5045 Type* map_iteration_ptr = Type::make_pointer_type(map_iteration_type);
5047 Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
5049 init->add_statement(hiter);
5051 source_location bloc = BUILTINS_LOCATION;
5052 Typed_identifier_list* param_types = new Typed_identifier_list();
5053 param_types->push_back(Typed_identifier("map", this->range_->type(), bloc));
5054 param_types->push_back(Typed_identifier("it", map_iteration_ptr, bloc));
5055 Function_type* fntype = Type::make_function_type(NULL, param_types, NULL,
5058 Named_object* mapiterinit =
5059 Named_object::make_function_declaration("mapiterinit", NULL, fntype, bloc);
5060 const char* n = "runtime.mapiterinit";
5061 mapiterinit->func_declaration_value()->set_asm_name(n);
5063 Expression* func = Expression::make_func_reference(mapiterinit, NULL, loc);
5064 Expression_list* params = new Expression_list();
5065 params->push_back(this->make_range_ref(range_object, range_temp, loc));
5066 Expression* ref = Expression::make_temporary_reference(hiter, loc);
5067 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5068 Expression* call = Expression::make_call(func, params, false, loc);
5069 init->add_statement(Statement::make_statement(call));
5076 ref = Expression::make_temporary_reference(hiter, loc);
5079 mpz_init_set_ui(zval, 0UL);
5080 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
5083 Expression* index = Expression::make_index(ref, zexpr, NULL, loc);
5085 Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
5086 Expression::make_nil(loc),
5091 // Set *PITER_INIT to
5092 // mapiter1(hiter, &index_temp)
5094 // mapiter2(hiter, &index_temp, &value_temp)
5096 Block* iter_init = new Block(body_block, loc);
5098 param_types = new Typed_identifier_list();
5099 param_types->push_back(Typed_identifier("hiter", map_iteration_ptr, bloc));
5100 Type* pkey_type = Type::make_pointer_type(index_temp->type());
5101 param_types->push_back(Typed_identifier("key", pkey_type, bloc));
5102 if (value_temp != NULL)
5104 Type* pval_type = Type::make_pointer_type(value_temp->type());
5105 param_types->push_back(Typed_identifier("val", pval_type, bloc));
5107 fntype = Type::make_function_type(NULL, param_types, NULL, bloc);
5108 n = value_temp == NULL ? "mapiter1" : "mapiter2";
5109 Named_object* mapiter = Named_object::make_function_declaration(n, NULL,
5111 n = value_temp == NULL ? "runtime.mapiter1" : "runtime.mapiter2";
5112 mapiter->func_declaration_value()->set_asm_name(n);
5114 func = Expression::make_func_reference(mapiter, NULL, loc);
5115 params = new Expression_list();
5116 ref = Expression::make_temporary_reference(hiter, loc);
5117 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5118 ref = Expression::make_temporary_reference(index_temp, loc);
5119 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5120 if (value_temp != NULL)
5122 ref = Expression::make_temporary_reference(value_temp, loc);
5123 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5125 call = Expression::make_call(func, params, false, loc);
5126 iter_init->add_statement(Statement::make_statement(call));
5128 *piter_init = iter_init;
5131 // mapiternext(&hiter)
5133 Block* post = new Block(enclosing, loc);
5135 static Named_object* mapiternext;
5136 if (mapiternext == NULL)
5138 param_types = new Typed_identifier_list();
5139 param_types->push_back(Typed_identifier("it", map_iteration_ptr, bloc));
5140 fntype = Type::make_function_type(NULL, param_types, NULL, bloc);
5141 mapiternext = Named_object::make_function_declaration("mapiternext",
5144 const char* n = "runtime.mapiternext";
5145 mapiternext->func_declaration_value()->set_asm_name(n);
5148 func = Expression::make_func_reference(mapiternext, NULL, loc);
5149 params = new Expression_list();
5150 ref = Expression::make_temporary_reference(hiter, loc);
5151 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5152 call = Expression::make_call(func, params, false, loc);
5153 post->add_statement(Statement::make_statement(call));
5158 // Lower a for range over a channel.
5161 For_range_statement::lower_range_channel(Gogo* gogo,
5164 Named_object* range_object,
5165 Temporary_statement* range_temp,
5166 Temporary_statement* index_temp,
5167 Temporary_statement* value_temp,
5173 gcc_assert(value_temp == NULL);
5175 source_location loc = this->location();
5177 // The loop we generate:
5179 // index_temp = <-range
5180 // if closed(range) {
5183 // index = index_temp
5184 // value = value_temp
5188 // We have no initialization code, no condition, and no post code.
5194 // Set *PITER_INIT to
5195 // index_temp = <-range
5196 // if closed(range) {
5200 Block* iter_init = new Block(body_block, loc);
5202 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5203 Expression* cond = this->call_builtin(gogo, "closed", ref, loc);
5205 ref = this->make_range_ref(range_object, range_temp, loc);
5206 Expression* recv = Expression::make_receive(ref, loc);
5207 ref = Expression::make_temporary_reference(index_temp, loc);
5208 Statement* s = Statement::make_assignment(ref, recv, loc);
5209 iter_init->add_statement(s);
5211 Block* then_block = new Block(iter_init, loc);
5212 s = Statement::make_break_statement(this->break_label(), loc);
5213 then_block->add_statement(s);
5215 s = Statement::make_if_statement(cond, then_block, NULL, loc);
5216 iter_init->add_statement(s);
5218 *piter_init = iter_init;
5221 // Return the break LABEL_EXPR.
5224 For_range_statement::break_label()
5226 if (this->break_label_ == NULL)
5227 this->break_label_ = new Unnamed_label(this->location());
5228 return this->break_label_;
5231 // Return the continue LABEL_EXPR.
5234 For_range_statement::continue_label()
5236 if (this->continue_label_ == NULL)
5237 this->continue_label_ = new Unnamed_label(this->location());
5238 return this->continue_label_;
5241 // Make a for statement with a range clause.
5243 For_range_statement*
5244 Statement::make_for_range_statement(Expression* index_var,
5245 Expression* value_var,
5247 source_location location)
5249 return new For_range_statement(index_var, value_var, range, location);