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 for (; p != fields->end(); ++p, ++next_index)
2220 Expression* thunk_param = Expression::make_var_reference(named_parameter,
2222 thunk_param = Expression::make_unary(OPERATOR_MULT, thunk_param,
2224 Expression* param = Expression::make_field_reference(thunk_param,
2227 call_params->push_back(param);
2230 Expression* call = Expression::make_call(func_to_call, call_params, false,
2232 // We need to lower in case this is a builtin function.
2233 call = call->lower(gogo, function, -1);
2234 if (may_call_recover)
2236 Call_expression* ce = call->call_expression();
2238 ce->set_is_deferred();
2241 Statement* call_statement = Statement::make_statement(call);
2243 // We already ran the determine_types pass, so we need to run it
2244 // just for this statement now.
2245 call_statement->determine_types();
2247 gogo->add_statement(call_statement);
2249 // If this is a defer statement, the label comes immediately after
2251 if (may_call_recover)
2253 gogo->add_label_definition("retaddr", location);
2255 Expression_list* vals = new Expression_list();
2256 vals->push_back(Expression::make_boolean(false, location));
2257 const Typed_identifier_list* results =
2258 function->func_value()->type()->results();
2259 gogo->add_statement(Statement::make_return_statement(results, vals,
2263 // That is all the thunk has to do.
2264 gogo->finish_function(location);
2267 // Get the function and argument trees.
2270 Thunk_statement::get_fn_and_arg(Translate_context* context, tree* pfn,
2273 if (this->call_->is_error_expression())
2275 *pfn = error_mark_node;
2276 *parg = error_mark_node;
2280 Call_expression* ce = this->call_->call_expression();
2282 Expression* fn = ce->fn();
2283 *pfn = fn->get_tree(context);
2285 const Expression_list* args = ce->args();
2286 if (args == NULL || args->empty())
2287 *parg = null_pointer_node;
2290 gcc_assert(args->size() == 1);
2291 *parg = args->front()->get_tree(context);
2295 // Class Go_statement.
2298 Go_statement::do_get_tree(Translate_context* context)
2302 this->get_fn_and_arg(context, &fn_tree, &arg_tree);
2304 static tree go_fndecl;
2306 tree fn_arg_type = NULL_TREE;
2307 if (go_fndecl == NULL_TREE)
2309 // Only build FN_ARG_TYPE if we need it.
2310 tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
2311 tree subfntype = build_function_type(ptr_type_node, subargtypes);
2312 fn_arg_type = build_pointer_type(subfntype);
2315 return Gogo::call_builtin(&go_fndecl,
2326 // Make a go statement.
2329 Statement::make_go_statement(Call_expression* call, source_location location)
2331 return new Go_statement(call, location);
2334 // Class Defer_statement.
2337 Defer_statement::do_get_tree(Translate_context* context)
2339 source_location loc = this->location();
2343 this->get_fn_and_arg(context, &fn_tree, &arg_tree);
2344 if (fn_tree == error_mark_node || arg_tree == error_mark_node)
2345 return error_mark_node;
2347 static tree defer_fndecl;
2349 tree fn_arg_type = NULL_TREE;
2350 if (defer_fndecl == NULL_TREE)
2352 // Only build FN_ARG_TYPE if we need it.
2353 tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
2354 tree subfntype = build_function_type(ptr_type_node, subargtypes);
2355 fn_arg_type = build_pointer_type(subfntype);
2358 tree defer_stack = context->function()->func_value()->defer_stack(loc);
2360 return Gogo::call_builtin(&defer_fndecl,
2373 // Make a defer statement.
2376 Statement::make_defer_statement(Call_expression* call,
2377 source_location location)
2379 return new Defer_statement(call, location);
2382 // Class Return_statement.
2384 // Traverse assignments. We treat each return value as a top level
2385 // RHS in an expression.
2388 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2390 Expression_list* vals = this->vals_;
2393 for (Expression_list::iterator p = vals->begin();
2396 tassign->value(&*p, true, true);
2401 // Lower a return statement. If we are returning a function call
2402 // which returns multiple values which match the current function,
2403 // split up the call's results. If the function has named result
2404 // variables, and the return statement lists explicit values, then
2405 // implement it by assigning the values to the result variables and
2406 // changing the statement to not list any values. This lets
2407 // panic/recover work correctly.
2410 Return_statement::do_lower(Gogo*, Block* enclosing)
2412 if (this->vals_ == NULL)
2415 const Typed_identifier_list* results = this->results_;
2416 if (results == NULL || results->empty())
2419 // If the current function has multiple return values, and we are
2420 // returning a single call expression, split up the call expression.
2421 size_t results_count = results->size();
2422 if (results_count > 1
2423 && this->vals_->size() == 1
2424 && this->vals_->front()->call_expression() != NULL)
2426 Call_expression* call = this->vals_->front()->call_expression();
2427 size_t count = results->size();
2428 Expression_list* vals = new Expression_list;
2429 for (size_t i = 0; i < count; ++i)
2430 vals->push_back(Expression::make_call_result(call, i));
2435 if (results->front().name().empty())
2438 if (results_count != this->vals_->size())
2440 // Presumably an error which will be reported in check_types.
2444 // Assign to named return values and then return them.
2446 source_location loc = this->location();
2447 const Block* top = enclosing;
2448 while (top->enclosing() != NULL)
2449 top = top->enclosing();
2451 const Bindings *bindings = top->bindings();
2452 Block* b = new Block(enclosing, loc);
2454 Expression_list* lhs = new Expression_list();
2455 Expression_list* rhs = new Expression_list();
2457 Expression_list::const_iterator pe = this->vals_->begin();
2459 for (Typed_identifier_list::const_iterator pr = results->begin();
2460 pr != results->end();
2463 Named_object* rv = bindings->lookup_local(pr->name());
2464 if (rv == NULL || !rv->is_result_variable())
2466 // Presumably an error.
2473 Expression* e = *pe;
2475 // Check types now so that we give a good error message. The
2476 // result type is known. We determine the expression type
2479 Type *rvtype = rv->result_var_value()->type();
2480 Type_context type_context(rvtype, false);
2481 e->determine_type(&type_context);
2484 if (Type::are_assignable(rvtype, e->type(), &reason))
2486 Expression* ve = Expression::make_var_reference(rv, e->location());
2493 error_at(e->location(), "incompatible type for return value %d", i);
2495 error_at(e->location(),
2496 "incompatible type for return value %d (%s)",
2500 gcc_assert(lhs->size() == rhs->size());
2504 else if (lhs->size() == 1)
2506 b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
2512 b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
2514 b->add_statement(Statement::make_return_statement(this->results_, NULL,
2517 return Statement::make_block_statement(b, loc);
2523 Return_statement::do_determine_types()
2525 if (this->vals_ == NULL)
2527 const Typed_identifier_list* results = this->results_;
2529 Typed_identifier_list::const_iterator pt;
2530 if (results != NULL)
2531 pt = results->begin();
2532 for (Expression_list::iterator pe = this->vals_->begin();
2533 pe != this->vals_->end();
2536 if (results == NULL || pt == results->end())
2537 (*pe)->determine_type_no_context();
2540 Type_context context(pt->type(), false);
2541 (*pe)->determine_type(&context);
2550 Return_statement::do_check_types(Gogo*)
2552 if (this->vals_ == NULL)
2555 const Typed_identifier_list* results = this->results_;
2556 if (results == NULL)
2558 this->report_error(_("return with value in function "
2559 "with no return type"));
2564 Typed_identifier_list::const_iterator pt = results->begin();
2565 for (Expression_list::const_iterator pe = this->vals_->begin();
2566 pe != this->vals_->end();
2569 if (pt == results->end())
2571 this->report_error(_("too many values in return statement"));
2575 if (!Type::are_assignable(pt->type(), (*pe)->type(), &reason))
2578 error_at(this->location(),
2579 "incompatible type for return value %d",
2582 error_at(this->location(),
2583 "incompatible type for return value %d (%s)",
2585 this->set_is_error();
2587 else if (pt->type()->is_error_type()
2588 || (*pe)->type()->is_error_type()
2589 || pt->type()->is_undefined()
2590 || (*pe)->type()->is_undefined())
2592 // Make sure we get the error for an undefined type.
2594 (*pe)->type()->base();
2595 this->set_is_error();
2599 if (pt != results->end())
2600 this->report_error(_("not enough values in return statement"));
2603 // Build a RETURN_EXPR tree.
2606 Return_statement::do_get_tree(Translate_context* context)
2608 Function* function = context->function()->func_value();
2609 tree fndecl = function->get_decl();
2610 if (fndecl == error_mark_node || DECL_RESULT(fndecl) == error_mark_node)
2611 return error_mark_node;
2613 const Typed_identifier_list* results = this->results_;
2615 if (this->vals_ == NULL)
2617 tree stmt_list = NULL_TREE;
2618 tree retval = function->return_value(context->gogo(),
2619 context->function(),
2623 if (retval == NULL_TREE)
2625 else if (retval == error_mark_node)
2626 return error_mark_node;
2628 set = fold_build2_loc(this->location(), MODIFY_EXPR, void_type_node,
2629 DECL_RESULT(fndecl), retval);
2630 append_to_statement_list(this->build_stmt_1(RETURN_EXPR, set),
2634 else if (this->vals_->size() == 1)
2636 gcc_assert(!VOID_TYPE_P(TREE_TYPE(TREE_TYPE(fndecl))));
2637 tree val = (*this->vals_->begin())->get_tree(context);
2638 gcc_assert(results != NULL && results->size() == 1);
2639 val = Expression::convert_for_assignment(context,
2640 results->begin()->type(),
2641 (*this->vals_->begin())->type(),
2642 val, this->location());
2643 if (val == error_mark_node)
2644 return error_mark_node;
2645 tree set = build2(MODIFY_EXPR, void_type_node,
2646 DECL_RESULT(fndecl), val);
2647 SET_EXPR_LOCATION(set, this->location());
2648 return this->build_stmt_1(RETURN_EXPR, set);
2652 gcc_assert(!VOID_TYPE_P(TREE_TYPE(TREE_TYPE(fndecl))));
2653 tree stmt_list = NULL_TREE;
2654 tree rettype = TREE_TYPE(DECL_RESULT(fndecl));
2655 tree retvar = create_tmp_var(rettype, "RESULT");
2656 gcc_assert(results != NULL && results->size() == this->vals_->size());
2657 Expression_list::const_iterator pv = this->vals_->begin();
2658 Typed_identifier_list::const_iterator pr = results->begin();
2659 for (tree field = TYPE_FIELDS(rettype);
2661 ++pv, ++pr, field = DECL_CHAIN(field))
2663 gcc_assert(pv != this->vals_->end());
2664 tree val = (*pv)->get_tree(context);
2665 val = Expression::convert_for_assignment(context, pr->type(),
2668 if (val == error_mark_node)
2669 return error_mark_node;
2670 tree set = build2(MODIFY_EXPR, void_type_node,
2671 build3(COMPONENT_REF, TREE_TYPE(field),
2672 retvar, field, NULL_TREE),
2674 SET_EXPR_LOCATION(set, this->location());
2675 append_to_statement_list(set, &stmt_list);
2677 tree set = build2(MODIFY_EXPR, void_type_node, DECL_RESULT(fndecl),
2679 append_to_statement_list(this->build_stmt_1(RETURN_EXPR, set),
2685 // Make a return statement.
2688 Statement::make_return_statement(const Typed_identifier_list* results,
2689 Expression_list* vals,
2690 source_location location)
2692 return new Return_statement(results, vals, location);
2695 // A break or continue statement.
2697 class Bc_statement : public Statement
2700 Bc_statement(bool is_break, Unnamed_label* label, source_location location)
2701 : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
2702 label_(label), is_break_(is_break)
2707 { return this->is_break_; }
2711 do_traverse(Traverse*)
2712 { return TRAVERSE_CONTINUE; }
2715 do_may_fall_through() const
2719 do_get_tree(Translate_context*)
2720 { return this->label_->get_goto(this->location()); }
2723 // The label that this branches to.
2724 Unnamed_label* label_;
2725 // True if this is "break", false if it is "continue".
2729 // Make a break statement.
2732 Statement::make_break_statement(Unnamed_label* label, source_location location)
2734 return new Bc_statement(true, label, location);
2737 // Make a continue statement.
2740 Statement::make_continue_statement(Unnamed_label* label,
2741 source_location location)
2743 return new Bc_statement(false, label, location);
2746 // A goto statement.
2748 class Goto_statement : public Statement
2751 Goto_statement(Label* label, source_location location)
2752 : Statement(STATEMENT_GOTO, location),
2758 do_traverse(Traverse*)
2759 { return TRAVERSE_CONTINUE; }
2762 do_check_types(Gogo*);
2765 do_may_fall_through() const
2769 do_get_tree(Translate_context*);
2775 // Check types for a label. There aren't any types per se, but we use
2776 // this to give an error if the label was never defined.
2779 Goto_statement::do_check_types(Gogo*)
2781 if (!this->label_->is_defined())
2783 error_at(this->location(), "reference to undefined label %qs",
2784 Gogo::message_name(this->label_->name()).c_str());
2785 this->set_is_error();
2789 // Return the tree for the goto statement.
2792 Goto_statement::do_get_tree(Translate_context*)
2794 return this->build_stmt_1(GOTO_EXPR, this->label_->get_decl());
2797 // Make a goto statement.
2800 Statement::make_goto_statement(Label* label, source_location location)
2802 return new Goto_statement(label, location);
2805 // A goto statement to an unnamed label.
2807 class Goto_unnamed_statement : public Statement
2810 Goto_unnamed_statement(Unnamed_label* label, source_location location)
2811 : Statement(STATEMENT_GOTO_UNNAMED, location),
2817 do_traverse(Traverse*)
2818 { return TRAVERSE_CONTINUE; }
2821 do_may_fall_through() const
2825 do_get_tree(Translate_context*)
2826 { return this->label_->get_goto(this->location()); }
2829 Unnamed_label* label_;
2832 // Make a goto statement to an unnamed label.
2835 Statement::make_goto_unnamed_statement(Unnamed_label* label,
2836 source_location location)
2838 return new Goto_unnamed_statement(label, location);
2841 // Class Label_statement.
2846 Label_statement::do_traverse(Traverse*)
2848 return TRAVERSE_CONTINUE;
2851 // Return a tree defining this label.
2854 Label_statement::do_get_tree(Translate_context*)
2856 return this->build_stmt_1(LABEL_EXPR, this->label_->get_decl());
2859 // Make a label statement.
2862 Statement::make_label_statement(Label* label, source_location location)
2864 return new Label_statement(label, location);
2867 // An unnamed label statement.
2869 class Unnamed_label_statement : public Statement
2872 Unnamed_label_statement(Unnamed_label* label)
2873 : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
2879 do_traverse(Traverse*)
2880 { return TRAVERSE_CONTINUE; }
2883 do_get_tree(Translate_context*)
2884 { return this->label_->get_definition(); }
2888 Unnamed_label* label_;
2891 // Make an unnamed label statement.
2894 Statement::make_unnamed_label_statement(Unnamed_label* label)
2896 return new Unnamed_label_statement(label);
2901 class If_statement : public Statement
2904 If_statement(Expression* cond, Block* then_block, Block* else_block,
2905 source_location location)
2906 : Statement(STATEMENT_IF, location),
2907 cond_(cond), then_block_(then_block), else_block_(else_block)
2912 do_traverse(Traverse*);
2915 do_determine_types();
2918 do_check_types(Gogo*);
2921 do_may_fall_through() const;
2924 do_get_tree(Translate_context*);
2935 If_statement::do_traverse(Traverse* traverse)
2937 if (this->cond_ != NULL)
2939 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
2940 return TRAVERSE_EXIT;
2942 if (this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
2943 return TRAVERSE_EXIT;
2944 if (this->else_block_ != NULL)
2946 if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
2947 return TRAVERSE_EXIT;
2949 return TRAVERSE_CONTINUE;
2953 If_statement::do_determine_types()
2955 if (this->cond_ != NULL)
2957 Type_context context(Type::lookup_bool_type(), false);
2958 this->cond_->determine_type(&context);
2960 this->then_block_->determine_types();
2961 if (this->else_block_ != NULL)
2962 this->else_block_->determine_types();
2968 If_statement::do_check_types(Gogo*)
2970 if (this->cond_ != NULL)
2972 Type* type = this->cond_->type();
2973 if (type->is_error_type())
2974 this->set_is_error();
2975 else if (!type->is_boolean_type())
2976 this->report_error(_("expected boolean expression"));
2980 // Whether the overall statement may fall through.
2983 If_statement::do_may_fall_through() const
2985 return (this->else_block_ == NULL
2986 || this->then_block_->may_fall_through()
2987 || this->else_block_->may_fall_through());
2993 If_statement::do_get_tree(Translate_context* context)
2995 gcc_assert(this->cond_ == NULL
2996 || this->cond_->type()->is_boolean_type()
2997 || this->cond_->type()->is_error_type());
2998 tree cond_tree = (this->cond_ == NULL
3000 : this->cond_->get_tree(context));
3001 tree then_tree = this->then_block_->get_tree(context);
3002 tree else_tree = (this->else_block_ == NULL
3004 : this->else_block_->get_tree(context));
3005 if (cond_tree == error_mark_node
3006 || then_tree == error_mark_node
3007 || else_tree == error_mark_node)
3008 return error_mark_node;
3009 tree ret = build3(COND_EXPR, void_type_node, cond_tree, then_tree,
3011 SET_EXPR_LOCATION(ret, this->location());
3015 // Make an if statement.
3018 Statement::make_if_statement(Expression* cond, Block* then_block,
3019 Block* else_block, source_location location)
3021 return new If_statement(cond, then_block, else_block, location);
3024 // Class Case_clauses::Case_clause.
3029 Case_clauses::Case_clause::traverse(Traverse* traverse)
3031 if (this->cases_ != NULL
3032 && (traverse->traverse_mask()
3033 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3035 if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
3036 return TRAVERSE_EXIT;
3038 if (this->statements_ != NULL)
3040 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3041 return TRAVERSE_EXIT;
3043 return TRAVERSE_CONTINUE;
3046 // Check whether all the case expressions are integer constants.
3049 Case_clauses::Case_clause::is_constant() const
3051 if (this->cases_ != NULL)
3053 for (Expression_list::const_iterator p = this->cases_->begin();
3054 p != this->cases_->end();
3056 if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
3062 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
3063 // value we are switching on; it may be NULL. If START_LABEL is not
3064 // NULL, it goes at the start of the statements, after the condition
3065 // test. We branch to FINISH_LABEL at the end of the statements.
3068 Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
3069 Unnamed_label* start_label,
3070 Unnamed_label* finish_label) const
3072 source_location loc = this->location_;
3073 Unnamed_label* next_case_label;
3074 if (this->cases_ == NULL || this->cases_->empty())
3076 gcc_assert(this->is_default_);
3077 next_case_label = NULL;
3081 Expression* cond = NULL;
3083 for (Expression_list::const_iterator p = this->cases_->begin();
3084 p != this->cases_->end();
3087 Expression* this_cond;
3088 if (val_temp == NULL)
3092 Expression* ref = Expression::make_temporary_reference(val_temp,
3094 this_cond = Expression::make_binary(OPERATOR_EQEQ, ref, *p, loc);
3100 cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
3103 Block* then_block = new Block(b, loc);
3104 next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
3105 Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
3107 then_block->add_statement(s);
3109 // if !COND { goto NEXT_CASE_LABEL }
3110 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3111 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3112 b->add_statement(s);
3115 if (start_label != NULL)
3116 b->add_statement(Statement::make_unnamed_label_statement(start_label));
3118 if (this->statements_ != NULL)
3119 b->add_statement(Statement::make_block_statement(this->statements_, loc));
3121 Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
3122 b->add_statement(s);
3124 if (next_case_label != NULL)
3125 b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
3131 Case_clauses::Case_clause::determine_types(Type* type)
3133 if (this->cases_ != NULL)
3135 Type_context case_context(type, false);
3136 for (Expression_list::iterator p = this->cases_->begin();
3137 p != this->cases_->end();
3139 (*p)->determine_type(&case_context);
3141 if (this->statements_ != NULL)
3142 this->statements_->determine_types();
3145 // Check types. Returns false if there was an error.
3148 Case_clauses::Case_clause::check_types(Type* type)
3150 if (this->cases_ != NULL)
3152 for (Expression_list::iterator p = this->cases_->begin();
3153 p != this->cases_->end();
3156 if (!Type::are_assignable(type, (*p)->type(), NULL)
3157 && !Type::are_assignable((*p)->type(), type, NULL))
3159 error_at((*p)->location(),
3160 "type mismatch between switch value and case clause");
3168 // Return true if this clause may fall through to the following
3169 // statements. Note that this is not the same as whether the case
3170 // uses the "fallthrough" keyword.
3173 Case_clauses::Case_clause::may_fall_through() const
3175 if (this->statements_ == NULL)
3177 return this->statements_->may_fall_through();
3180 // Build up the body of a SWITCH_EXPR.
3183 Case_clauses::Case_clause::get_constant_tree(Translate_context* context,
3184 Unnamed_label* break_label,
3185 Case_constants* case_constants,
3186 tree* stmt_list) const
3188 if (this->cases_ != NULL)
3190 for (Expression_list::const_iterator p = this->cases_->begin();
3191 p != this->cases_->end();
3197 if (!(*p)->integer_constant_value(true, ival, &itype))
3199 gcc_assert(itype != NULL);
3200 tree type_tree = itype->get_tree(context->gogo());
3201 tree val = Expression::integer_constant_tree(ival, type_tree);
3204 if (val != error_mark_node)
3206 gcc_assert(TREE_CODE(val) == INTEGER_CST);
3208 std::pair<Case_constants::iterator, bool> ins =
3209 case_constants->insert(val);
3212 // Value was already present.
3213 warning_at(this->location_, 0,
3214 "duplicate case value will never match");
3218 tree label = create_artificial_label(this->location_);
3219 append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
3220 val, NULL_TREE, label),
3226 if (this->is_default_)
3228 tree label = create_artificial_label(this->location_);
3229 append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
3230 NULL_TREE, NULL_TREE, label),
3234 if (this->statements_ != NULL)
3236 tree block_tree = this->statements_->get_tree(context);
3237 if (block_tree != error_mark_node)
3238 append_to_statement_list(block_tree, stmt_list);
3241 if (!this->is_fallthrough_)
3242 append_to_statement_list(break_label->get_goto(this->location_), stmt_list);
3245 // Class Case_clauses.
3250 Case_clauses::traverse(Traverse* traverse)
3252 for (Clauses::iterator p = this->clauses_.begin();
3253 p != this->clauses_.end();
3256 if (p->traverse(traverse) == TRAVERSE_EXIT)
3257 return TRAVERSE_EXIT;
3259 return TRAVERSE_CONTINUE;
3262 // Check whether all the case expressions are constant.
3265 Case_clauses::is_constant() const
3267 for (Clauses::const_iterator p = this->clauses_.begin();
3268 p != this->clauses_.end();
3270 if (!p->is_constant())
3275 // Lower case clauses for a nonconstant switch.
3278 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3279 Unnamed_label* break_label) const
3281 // The default case.
3282 const Case_clause* default_case = NULL;
3284 // The label for the fallthrough of the previous case.
3285 Unnamed_label* last_fallthrough_label = NULL;
3287 // The label for the start of the default case. This is used if the
3288 // case before the default case falls through.
3289 Unnamed_label* default_start_label = NULL;
3291 // The label for the end of the default case. This normally winds
3292 // up as BREAK_LABEL, but it will be different if the default case
3294 Unnamed_label* default_finish_label = NULL;
3296 for (Clauses::const_iterator p = this->clauses_.begin();
3297 p != this->clauses_.end();
3300 // The label to use for the start of the statements for this
3301 // case. This is NULL unless the previous case falls through.
3302 Unnamed_label* start_label = last_fallthrough_label;
3304 // The label to jump to after the end of the statements for this
3306 Unnamed_label* finish_label = break_label;
3308 last_fallthrough_label = NULL;
3309 if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3311 finish_label = new Unnamed_label(p->location());
3312 last_fallthrough_label = finish_label;
3315 if (!p->is_default())
3316 p->lower(b, val_temp, start_label, finish_label);
3319 // We have to move the default case to the end, so that we
3320 // only use it if all the other tests fail.
3322 default_start_label = start_label;
3323 default_finish_label = finish_label;
3327 if (default_case != NULL)
3328 default_case->lower(b, val_temp, default_start_label,
3329 default_finish_label);
3336 Case_clauses::determine_types(Type* type)
3338 for (Clauses::iterator p = this->clauses_.begin();
3339 p != this->clauses_.end();
3341 p->determine_types(type);
3344 // Check types. Returns false if there was an error.
3347 Case_clauses::check_types(Type* type)
3350 for (Clauses::iterator p = this->clauses_.begin();
3351 p != this->clauses_.end();
3354 if (!p->check_types(type))
3360 // Return true if these clauses may fall through to the statements
3361 // following the switch statement.
3364 Case_clauses::may_fall_through() const
3366 bool found_default = false;
3367 for (Clauses::const_iterator p = this->clauses_.begin();
3368 p != this->clauses_.end();
3371 if (p->may_fall_through() && !p->is_fallthrough())
3373 if (p->is_default())
3374 found_default = true;
3376 return !found_default;
3379 // Return a tree when all case expressions are constants.
3382 Case_clauses::get_constant_tree(Translate_context* context,
3383 Unnamed_label* break_label) const
3385 Case_constants case_constants;
3386 tree stmt_list = NULL_TREE;
3387 for (Clauses::const_iterator p = this->clauses_.begin();
3388 p != this->clauses_.end();
3390 p->get_constant_tree(context, break_label, &case_constants,
3395 // A constant switch statement. A Switch_statement is lowered to this
3396 // when all the cases are constants.
3398 class Constant_switch_statement : public Statement
3401 Constant_switch_statement(Expression* val, Case_clauses* clauses,
3402 Unnamed_label* break_label,
3403 source_location location)
3404 : Statement(STATEMENT_CONSTANT_SWITCH, location),
3405 val_(val), clauses_(clauses), break_label_(break_label)
3410 do_traverse(Traverse*);
3413 do_determine_types();
3416 do_check_types(Gogo*);
3419 do_may_fall_through() const;
3422 do_get_tree(Translate_context*);
3425 // The value to switch on.
3427 // The case clauses.
3428 Case_clauses* clauses_;
3429 // The break label, if needed.
3430 Unnamed_label* break_label_;
3436 Constant_switch_statement::do_traverse(Traverse* traverse)
3438 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3439 return TRAVERSE_EXIT;
3440 return this->clauses_->traverse(traverse);
3446 Constant_switch_statement::do_determine_types()
3448 this->val_->determine_type_no_context();
3449 this->clauses_->determine_types(this->val_->type());
3455 Constant_switch_statement::do_check_types(Gogo*)
3457 if (!this->clauses_->check_types(this->val_->type()))
3458 this->set_is_error();
3461 // Return whether this switch may fall through.
3464 Constant_switch_statement::do_may_fall_through() const
3466 if (this->clauses_ == NULL)
3469 // If we have a break label, then some case needed it. That implies
3470 // that the switch statement as a whole can fall through.
3471 if (this->break_label_ != NULL)
3474 return this->clauses_->may_fall_through();
3477 // Convert to GENERIC.
3480 Constant_switch_statement::do_get_tree(Translate_context* context)
3482 tree switch_val_tree = this->val_->get_tree(context);
3484 Unnamed_label* break_label = this->break_label_;
3485 if (break_label == NULL)
3486 break_label = new Unnamed_label(this->location());
3488 tree stmt_list = NULL_TREE;
3489 tree s = build3(SWITCH_EXPR, void_type_node, switch_val_tree,
3490 this->clauses_->get_constant_tree(context, break_label),
3492 SET_EXPR_LOCATION(s, this->location());
3493 append_to_statement_list(s, &stmt_list);
3495 append_to_statement_list(break_label->get_definition(), &stmt_list);
3500 // Class Switch_statement.
3505 Switch_statement::do_traverse(Traverse* traverse)
3507 if (this->val_ != NULL)
3509 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3510 return TRAVERSE_EXIT;
3512 return this->clauses_->traverse(traverse);
3515 // Lower a Switch_statement to a Constant_switch_statement or a series
3516 // of if statements.
3519 Switch_statement::do_lower(Gogo*, Block* enclosing)
3521 source_location loc = this->location();
3523 if (this->val_ != NULL
3524 && (this->val_->is_error_expression()
3525 || this->val_->type()->is_error_type()))
3526 return Statement::make_error_statement(loc);
3528 if (this->val_ != NULL
3529 && this->val_->type()->integer_type() != NULL
3530 && !this->clauses_->empty()
3531 && this->clauses_->is_constant())
3532 return new Constant_switch_statement(this->val_, this->clauses_,
3533 this->break_label_, loc);
3535 Block* b = new Block(enclosing, loc);
3537 if (this->clauses_->empty())
3539 Expression* val = this->val_;
3541 val = Expression::make_boolean(true, loc);
3542 return Statement::make_statement(val);
3545 Temporary_statement* val_temp;
3546 if (this->val_ == NULL)
3550 // var val_temp VAL_TYPE = VAL
3551 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3552 b->add_statement(val_temp);
3555 this->clauses_->lower(b, val_temp, this->break_label());
3557 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3558 b->add_statement(s);
3560 return Statement::make_block_statement(b, loc);
3563 // Return the break label for this switch statement, creating it if
3567 Switch_statement::break_label()
3569 if (this->break_label_ == NULL)
3570 this->break_label_ = new Unnamed_label(this->location());
3571 return this->break_label_;
3574 // Make a switch statement.
3577 Statement::make_switch_statement(Expression* val, source_location location)
3579 return new Switch_statement(val, location);
3582 // Class Type_case_clauses::Type_case_clause.
3587 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3589 if (!this->is_default_
3590 && ((traverse->traverse_mask()
3591 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3592 && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3593 return TRAVERSE_EXIT;
3594 if (this->statements_ != NULL)
3595 return this->statements_->traverse(traverse);
3596 return TRAVERSE_CONTINUE;
3599 // Lower one clause in a type switch. Add statements to the block B.
3600 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3601 // BREAK_LABEL is the label at the end of the type switch.
3602 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3606 Type_case_clauses::Type_case_clause::lower(Block* b,
3607 Temporary_statement* descriptor_temp,
3608 Unnamed_label* break_label,
3609 Unnamed_label** stmts_label) const
3611 source_location loc = this->location_;
3613 Unnamed_label* next_case_label = NULL;
3614 if (!this->is_default_)
3616 Type* type = this->type_;
3619 // The language permits case nil, which is of course a constant
3620 // rather than a type. It will appear here as an invalid
3622 if (type->is_nil_constant_as_type())
3625 Expression::make_temporary_reference(descriptor_temp, loc);
3626 cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3627 Expression::make_nil(loc),
3633 if (type->interface_type() == NULL)
3635 // func ifacetypeeq(*descriptor, *descriptor) bool
3636 static Named_object* ifacetypeeq;
3637 if (ifacetypeeq == NULL)
3639 const source_location bloc = BUILTINS_LOCATION;
3640 Typed_identifier_list* param_types =
3641 new Typed_identifier_list();
3642 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3643 param_types->push_back(Typed_identifier("a", descriptor_type,
3645 param_types->push_back(Typed_identifier("b", descriptor_type,
3647 Typed_identifier_list* ret_types =
3648 new Typed_identifier_list();
3649 Type* bool_type = Type::lookup_bool_type();
3650 ret_types->push_back(Typed_identifier("", bool_type, bloc));
3651 Function_type* fntype = Type::make_function_type(NULL,
3656 Named_object::make_function_declaration("ifacetypeeq", NULL,
3658 const char* n = "runtime.ifacetypeeq";
3659 ifacetypeeq->func_declaration_value()->set_asm_name(n);
3662 // ifacetypeeq(descriptor_temp, DESCRIPTOR)
3663 func = Expression::make_func_reference(ifacetypeeq, NULL, loc);
3667 // func ifaceI2Tp(*descriptor, *descriptor) bool
3668 static Named_object* ifaceI2Tp;
3669 if (ifaceI2Tp == NULL)
3671 const source_location bloc = BUILTINS_LOCATION;
3672 Typed_identifier_list* param_types =
3673 new Typed_identifier_list();
3674 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3675 param_types->push_back(Typed_identifier("a", descriptor_type,
3677 param_types->push_back(Typed_identifier("b", descriptor_type,
3679 Typed_identifier_list* ret_types =
3680 new Typed_identifier_list();
3681 Type* bool_type = Type::lookup_bool_type();
3682 ret_types->push_back(Typed_identifier("", bool_type, bloc));
3683 Function_type* fntype = Type::make_function_type(NULL,
3688 Named_object::make_function_declaration("ifaceI2Tp", NULL,
3690 const char* n = "runtime.ifaceI2Tp";
3691 ifaceI2Tp->func_declaration_value()->set_asm_name(n);
3694 // ifaceI2Tp(descriptor_temp, DESCRIPTOR)
3695 func = Expression::make_func_reference(ifaceI2Tp, NULL, loc);
3697 Expression_list* params = new Expression_list();
3698 params->push_back(Expression::make_type_descriptor(type, loc));
3700 Expression::make_temporary_reference(descriptor_temp, loc);
3701 params->push_back(ref);
3702 cond = Expression::make_call(func, params, false, loc);
3705 Unnamed_label* dest;
3706 if (!this->is_fallthrough_)
3708 // if !COND { goto NEXT_CASE_LABEL }
3709 next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
3710 dest = next_case_label;
3711 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3715 // if COND { goto STMTS_LABEL }
3716 gcc_assert(stmts_label != NULL);
3717 if (*stmts_label == NULL)
3718 *stmts_label = new Unnamed_label(UNKNOWN_LOCATION);
3719 dest = *stmts_label;
3721 Block* then_block = new Block(b, loc);
3722 Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
3723 then_block->add_statement(s);
3724 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3725 b->add_statement(s);
3728 if (this->statements_ != NULL
3729 || (!this->is_fallthrough_
3730 && stmts_label != NULL
3731 && *stmts_label != NULL))
3733 gcc_assert(!this->is_fallthrough_);
3734 if (stmts_label != NULL && *stmts_label != NULL)
3736 gcc_assert(!this->is_default_);
3737 if (this->statements_ != NULL)
3738 (*stmts_label)->set_location(this->statements_->start_location());
3739 Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
3740 b->add_statement(s);
3741 *stmts_label = NULL;
3743 if (this->statements_ != NULL)
3744 b->add_statement(Statement::make_block_statement(this->statements_,
3748 if (this->is_fallthrough_)
3749 gcc_assert(next_case_label == NULL);
3752 source_location gloc = (this->statements_ == NULL
3754 : this->statements_->end_location());
3755 b->add_statement(Statement::make_goto_unnamed_statement(break_label,
3757 if (next_case_label != NULL)
3760 Statement::make_unnamed_label_statement(next_case_label);
3761 b->add_statement(s);
3766 // Class Type_case_clauses.
3771 Type_case_clauses::traverse(Traverse* traverse)
3773 for (Type_clauses::iterator p = this->clauses_.begin();
3774 p != this->clauses_.end();
3777 if (p->traverse(traverse) == TRAVERSE_EXIT)
3778 return TRAVERSE_EXIT;
3780 return TRAVERSE_CONTINUE;
3783 // Check for duplicate types.
3786 Type_case_clauses::check_duplicates() const
3788 typedef Unordered_set_hash(const Type*, Type_hash_identical,
3789 Type_identical) Types_seen;
3790 Types_seen types_seen;
3791 for (Type_clauses::const_iterator p = this->clauses_.begin();
3792 p != this->clauses_.end();
3795 Type* t = p->type();
3798 if (t->is_nil_constant_as_type())
3799 t = Type::make_nil_type();
3800 std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
3802 error_at(p->location(), "duplicate type in switch");
3806 // Lower the clauses in a type switch. Add statements to the block B.
3807 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3808 // BREAK_LABEL is the label at the end of the type switch.
3811 Type_case_clauses::lower(Block* b, Temporary_statement* descriptor_temp,
3812 Unnamed_label* break_label) const
3814 const Type_case_clause* default_case = NULL;
3816 Unnamed_label* stmts_label = NULL;
3817 for (Type_clauses::const_iterator p = this->clauses_.begin();
3818 p != this->clauses_.end();
3821 if (!p->is_default())
3822 p->lower(b, descriptor_temp, break_label, &stmts_label);
3825 // We are generating a series of tests, which means that we
3826 // need to move the default case to the end.
3830 gcc_assert(stmts_label == NULL);
3832 if (default_case != NULL)
3833 default_case->lower(b, descriptor_temp, break_label, NULL);
3836 // Class Type_switch_statement.
3841 Type_switch_statement::do_traverse(Traverse* traverse)
3843 if (this->var_ == NULL)
3845 if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
3846 return TRAVERSE_EXIT;
3848 if (this->clauses_ != NULL)
3849 return this->clauses_->traverse(traverse);
3850 return TRAVERSE_CONTINUE;
3853 // Lower a type switch statement to a series of if statements. The gc
3854 // compiler is able to generate a table in some cases. However, that
3855 // does not work for us because we may have type descriptors in
3856 // different shared libraries, so we can't compare them with simple
3857 // equality testing.
3860 Type_switch_statement::do_lower(Gogo*, Block* enclosing)
3862 const source_location loc = this->location();
3864 if (this->clauses_ != NULL)
3865 this->clauses_->check_duplicates();
3867 Block* b = new Block(enclosing, loc);
3869 Type* val_type = (this->var_ != NULL
3870 ? this->var_->var_value()->type()
3871 : this->expr_->type());
3873 // var descriptor_temp DESCRIPTOR_TYPE
3874 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3875 Temporary_statement* descriptor_temp =
3876 Statement::make_temporary(descriptor_type, NULL, loc);
3877 b->add_statement(descriptor_temp);
3879 if (val_type->interface_type() == NULL)
3881 // Doing a type switch on a non-interface type. Should we issue
3882 // a warning for this case?
3883 // descriptor_temp = DESCRIPTOR
3884 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3886 Expression* rhs = Expression::make_type_descriptor(val_type, loc);
3887 Statement* s = Statement::make_assignment(lhs, rhs, loc);
3888 b->add_statement(s);
3892 const source_location bloc = BUILTINS_LOCATION;
3894 // func {efacetype,ifacetype}(*interface) *descriptor
3895 // FIXME: This should be inlined.
3896 Typed_identifier_list* param_types = new Typed_identifier_list();
3897 param_types->push_back(Typed_identifier("i", val_type, bloc));
3898 Typed_identifier_list* ret_types = new Typed_identifier_list();
3899 ret_types->push_back(Typed_identifier("", descriptor_type, bloc));
3900 Function_type* fntype = Type::make_function_type(NULL, param_types,
3902 bool is_empty = val_type->interface_type()->is_empty();
3903 const char* fnname = is_empty ? "efacetype" : "ifacetype";
3905 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
3906 const char* asm_name = (is_empty
3907 ? "runtime.efacetype"
3908 : "runtime.ifacetype");
3909 fn->func_declaration_value()->set_asm_name(asm_name);
3911 // descriptor_temp = ifacetype(val_temp)
3912 Expression* func = Expression::make_func_reference(fn, NULL, loc);
3913 Expression_list* params = new Expression_list();
3915 if (this->var_ == NULL)
3918 ref = Expression::make_var_reference(this->var_, loc);
3919 params->push_back(ref);
3920 Expression* call = Expression::make_call(func, params, false, loc);
3921 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3923 Statement* s = Statement::make_assignment(lhs, call, loc);
3924 b->add_statement(s);
3927 if (this->clauses_ != NULL)
3928 this->clauses_->lower(b, descriptor_temp, this->break_label());
3930 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3931 b->add_statement(s);
3933 return Statement::make_block_statement(b, loc);
3936 // Return the break label for this type switch statement, creating it
3940 Type_switch_statement::break_label()
3942 if (this->break_label_ == NULL)
3943 this->break_label_ = new Unnamed_label(this->location());
3944 return this->break_label_;
3947 // Make a type switch statement.
3949 Type_switch_statement*
3950 Statement::make_type_switch_statement(Named_object* var, Expression* expr,
3951 source_location location)
3953 return new Type_switch_statement(var, expr, location);
3956 // Class Select_clauses::Select_clause.
3961 Select_clauses::Select_clause::traverse(Traverse* traverse)
3963 if (!this->is_lowered_
3964 && (traverse->traverse_mask()
3965 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3967 if (this->channel_ != NULL)
3969 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
3970 return TRAVERSE_EXIT;
3972 if (this->val_ != NULL)
3974 if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
3975 return TRAVERSE_EXIT;
3978 if (this->statements_ != NULL)
3980 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3981 return TRAVERSE_EXIT;
3983 return TRAVERSE_CONTINUE;
3986 // Lowering. Here we pull out the channel and the send values, to
3987 // enforce the order of evaluation. We also add explicit send and
3988 // receive statements to the clauses.
3991 Select_clauses::Select_clause::lower(Block* b)
3993 if (this->is_default_)
3995 gcc_assert(this->channel_ == NULL && this->val_ == NULL);
3996 this->is_lowered_ = true;
4000 source_location loc = this->location_;
4002 // Evaluate the channel before the select statement.
4003 Temporary_statement* channel_temp = Statement::make_temporary(NULL,
4006 b->add_statement(channel_temp);
4007 this->channel_ = Expression::make_temporary_reference(channel_temp, loc);
4009 // If this is a send clause, evaluate the value to send before the
4010 // select statement.
4011 Temporary_statement* val_temp = NULL;
4014 val_temp = Statement::make_temporary(NULL, this->val_, loc);
4015 b->add_statement(val_temp);
4018 // Add the send or receive before the rest of the statements if any.
4019 Block *init = new Block(b, loc);
4020 Expression* ref = Expression::make_temporary_reference(channel_temp, loc);
4023 Expression* ref2 = Expression::make_temporary_reference(val_temp, loc);
4024 Send_expression* send = Expression::make_send(ref, ref2, loc);
4025 send->discarding_value();
4026 send->set_for_select();
4027 init->add_statement(Statement::make_statement(send));
4031 Receive_expression* recv = Expression::make_receive(ref, loc);
4032 recv->set_for_select();
4033 if (this->val_ != NULL)
4035 gcc_assert(this->var_ == NULL);
4036 init->add_statement(Statement::make_assignment(this->val_, recv,
4039 else if (this->var_ != NULL)
4041 this->var_->var_value()->set_init(recv);
4042 this->var_->var_value()->clear_type_from_chan_element();
4046 recv->discarding_value();
4047 init->add_statement(Statement::make_statement(recv));
4051 if (this->statements_ != NULL)
4052 init->add_statement(Statement::make_block_statement(this->statements_,
4055 this->statements_ = init;
4057 // Now all references should be handled through the statements, not
4059 this->is_lowered_ = true;
4067 Select_clauses::Select_clause::determine_types()
4069 gcc_assert(this->is_lowered_);
4070 if (this->statements_ != NULL)
4071 this->statements_->determine_types();
4074 // Whether this clause may fall through to the statement which follows
4075 // the overall select statement.
4078 Select_clauses::Select_clause::may_fall_through() const
4080 if (this->statements_ == NULL)
4082 return this->statements_->may_fall_through();
4085 // Return a tree for the statements to execute.
4088 Select_clauses::Select_clause::get_statements_tree(Translate_context* context)
4090 if (this->statements_ == NULL)
4092 return this->statements_->get_tree(context);
4095 // Class Select_clauses.
4100 Select_clauses::traverse(Traverse* traverse)
4102 for (Clauses::iterator p = this->clauses_.begin();
4103 p != this->clauses_.end();
4106 if (p->traverse(traverse) == TRAVERSE_EXIT)
4107 return TRAVERSE_EXIT;
4109 return TRAVERSE_CONTINUE;
4112 // Lowering. Here we pull out the channel and the send values, to
4113 // enforce the order of evaluation. We also add explicit send and
4114 // receive statements to the clauses.
4117 Select_clauses::lower(Block* b)
4119 for (Clauses::iterator p = this->clauses_.begin();
4120 p != this->clauses_.end();
4128 Select_clauses::determine_types()
4130 for (Clauses::iterator p = this->clauses_.begin();
4131 p != this->clauses_.end();
4133 p->determine_types();
4136 // Return whether these select clauses fall through to the statement
4137 // following the overall select statement.
4140 Select_clauses::may_fall_through() const
4142 for (Clauses::const_iterator p = this->clauses_.begin();
4143 p != this->clauses_.end();
4145 if (p->may_fall_through())
4150 // Return a tree. We build a call to
4151 // size_t __go_select(size_t count, _Bool has_default,
4152 // channel* channels, _Bool* is_send)
4154 // There are COUNT entries in the CHANNELS and IS_SEND arrays. The
4155 // value in the IS_SEND array is true for send, false for receive.
4156 // __go_select returns an integer from 0 to COUNT, inclusive. A
4157 // return of 0 means that the default case should be run; this only
4158 // happens if HAS_DEFAULT is non-zero. Otherwise the number indicates
4161 // FIXME: This doesn't handle channels which send interface types
4162 // where the receiver has a static type which matches that interface.
4165 Select_clauses::get_tree(Translate_context* context,
4166 Unnamed_label *break_label,
4167 source_location location)
4169 size_t count = this->clauses_.size();
4170 VEC(constructor_elt, gc)* chan_init = VEC_alloc(constructor_elt, gc, count);
4171 VEC(constructor_elt, gc)* is_send_init = VEC_alloc(constructor_elt, gc,
4173 Select_clause* default_clause = NULL;
4174 tree final_stmt_list = NULL_TREE;
4175 tree channel_type_tree = NULL_TREE;
4178 for (Clauses::iterator p = this->clauses_.begin();
4179 p != this->clauses_.end();
4182 if (p->is_default())
4184 default_clause = &*p;
4189 if (p->channel()->type()->channel_type() == NULL)
4191 // We should have given an error in the send or receive
4192 // statement we created via lowering.
4193 gcc_assert(saw_errors());
4194 return error_mark_node;
4197 tree channel_tree = p->channel()->get_tree(context);
4198 if (channel_tree == error_mark_node)
4199 return error_mark_node;
4200 channel_type_tree = TREE_TYPE(channel_tree);
4202 constructor_elt* elt = VEC_quick_push(constructor_elt, chan_init, NULL);
4203 elt->index = build_int_cstu(sizetype, i);
4204 elt->value = channel_tree;
4206 elt = VEC_quick_push(constructor_elt, is_send_init, NULL);
4207 elt->index = build_int_cstu(sizetype, i);
4208 elt->value = p->is_send() ? boolean_true_node : boolean_false_node;
4212 gcc_assert(i == count);
4214 if (i == 0 && default_clause != NULL)
4216 // There is only a default clause.
4217 gcc_assert(final_stmt_list == NULL_TREE);
4218 tree stmt_list = NULL_TREE;
4219 append_to_statement_list(default_clause->get_statements_tree(context),
4221 append_to_statement_list(break_label->get_definition(), &stmt_list);
4225 tree pointer_chan_type_tree = (channel_type_tree == NULL_TREE
4227 : build_pointer_type(channel_type_tree));
4229 tree pointer_boolean_type_tree = build_pointer_type(boolean_type_node);
4234 chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4236 is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4241 tree index_type_tree = build_index_type(size_int(count - 1));
4242 tree chan_array_type_tree = build_array_type(channel_type_tree,
4244 tree chan_constructor = build_constructor(chan_array_type_tree,
4246 tree chan_var = create_tmp_var(chan_array_type_tree, "CHAN");
4247 DECL_IGNORED_P(chan_var) = 0;
4248 DECL_INITIAL(chan_var) = chan_constructor;
4249 DECL_SOURCE_LOCATION(chan_var) = location;
4250 TREE_ADDRESSABLE(chan_var) = 1;
4251 tree decl_expr = build1(DECL_EXPR, void_type_node, chan_var);
4252 SET_EXPR_LOCATION(decl_expr, location);
4253 append_to_statement_list(decl_expr, &final_stmt_list);
4255 tree is_send_array_type_tree = build_array_type(boolean_type_node,
4257 tree is_send_constructor = build_constructor(is_send_array_type_tree,
4259 tree is_send_var = create_tmp_var(is_send_array_type_tree, "ISSEND");
4260 DECL_IGNORED_P(is_send_var) = 0;
4261 DECL_INITIAL(is_send_var) = is_send_constructor;
4262 DECL_SOURCE_LOCATION(is_send_var) = location;
4263 TREE_ADDRESSABLE(is_send_var) = 1;
4264 decl_expr = build1(DECL_EXPR, void_type_node, is_send_var);
4265 SET_EXPR_LOCATION(decl_expr, location);
4266 append_to_statement_list(decl_expr, &final_stmt_list);
4268 chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4269 build_fold_addr_expr_loc(location,
4271 is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4272 build_fold_addr_expr_loc(location,
4276 static tree select_fndecl;
4277 tree call = Gogo::call_builtin(&select_fndecl,
4285 (default_clause == NULL
4286 ? boolean_false_node
4287 : boolean_true_node),
4288 pointer_chan_type_tree,
4290 pointer_boolean_type_tree,
4292 if (call == error_mark_node)
4293 return error_mark_node;
4295 tree stmt_list = NULL_TREE;
4297 if (default_clause != NULL)
4298 this->add_clause_tree(context, 0, default_clause, break_label, &stmt_list);
4301 for (Clauses::iterator p = this->clauses_.begin();
4302 p != this->clauses_.end();
4305 if (!p->is_default())
4307 this->add_clause_tree(context, i, &*p, break_label, &stmt_list);
4312 append_to_statement_list(break_label->get_definition(), &stmt_list);
4314 tree switch_stmt = build3(SWITCH_EXPR, sizetype, call, stmt_list, NULL_TREE);
4315 SET_EXPR_LOCATION(switch_stmt, location);
4316 append_to_statement_list(switch_stmt, &final_stmt_list);
4318 return final_stmt_list;
4321 // Add the tree for CLAUSE to STMT_LIST.
4324 Select_clauses::add_clause_tree(Translate_context* context, int case_index,
4325 Select_clause* clause,
4326 Unnamed_label* bottom_label, tree* stmt_list)
4328 tree label = create_artificial_label(clause->location());
4329 append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
4330 build_int_cst(sizetype, case_index),
4333 append_to_statement_list(clause->get_statements_tree(context), stmt_list);
4334 tree g = bottom_label->get_goto(clause->statements() == NULL
4335 ? clause->location()
4336 : clause->statements()->end_location());
4337 append_to_statement_list(g, stmt_list);
4340 // Class Select_statement.
4342 // Return the break label for this switch statement, creating it if
4346 Select_statement::break_label()
4348 if (this->break_label_ == NULL)
4349 this->break_label_ = new Unnamed_label(this->location());
4350 return this->break_label_;
4353 // Lower a select statement. This will still return a select
4354 // statement, but it will be modified to implement the order of
4355 // evaluation rules, and to include the send and receive statements as
4356 // explicit statements in the clauses.
4359 Select_statement::do_lower(Gogo*, Block* enclosing)
4361 if (this->is_lowered_)
4363 Block* b = new Block(enclosing, this->location());
4364 this->clauses_->lower(b);
4365 this->is_lowered_ = true;
4366 b->add_statement(this);
4367 return Statement::make_block_statement(b, this->location());
4370 // Return the tree for a select statement.
4373 Select_statement::do_get_tree(Translate_context* context)
4375 return this->clauses_->get_tree(context, this->break_label(),
4379 // Make a select statement.
4382 Statement::make_select_statement(source_location location)
4384 return new Select_statement(location);
4387 // Class For_statement.
4392 For_statement::do_traverse(Traverse* traverse)
4394 if (this->init_ != NULL)
4396 if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
4397 return TRAVERSE_EXIT;
4399 if (this->cond_ != NULL)
4401 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
4402 return TRAVERSE_EXIT;
4404 if (this->post_ != NULL)
4406 if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
4407 return TRAVERSE_EXIT;
4409 return this->statements_->traverse(traverse);
4412 // Lower a For_statement into if statements and gotos. Getting rid of
4413 // complex statements make it easier to handle garbage collection.
4416 For_statement::do_lower(Gogo*, Block* enclosing)
4419 source_location loc = this->location();
4421 Block* b = new Block(enclosing, this->location());
4422 if (this->init_ != NULL)
4424 s = Statement::make_block_statement(this->init_,
4425 this->init_->start_location());
4426 b->add_statement(s);
4429 Unnamed_label* entry = NULL;
4430 if (this->cond_ != NULL)
4432 entry = new Unnamed_label(this->location());
4433 b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
4436 Unnamed_label* top = new Unnamed_label(this->location());
4437 b->add_statement(Statement::make_unnamed_label_statement(top));
4439 s = Statement::make_block_statement(this->statements_,
4440 this->statements_->start_location());
4441 b->add_statement(s);
4443 source_location end_loc = this->statements_->end_location();
4445 Unnamed_label* cont = this->continue_label_;
4447 b->add_statement(Statement::make_unnamed_label_statement(cont));
4449 if (this->post_ != NULL)
4451 s = Statement::make_block_statement(this->post_,
4452 this->post_->start_location());
4453 b->add_statement(s);
4454 end_loc = this->post_->end_location();
4457 if (this->cond_ == NULL)
4458 b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
4461 b->add_statement(Statement::make_unnamed_label_statement(entry));
4463 source_location cond_loc = this->cond_->location();
4464 Block* then_block = new Block(b, cond_loc);
4465 s = Statement::make_goto_unnamed_statement(top, cond_loc);
4466 then_block->add_statement(s);
4468 s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
4469 b->add_statement(s);
4472 Unnamed_label* brk = this->break_label_;
4474 b->add_statement(Statement::make_unnamed_label_statement(brk));
4476 b->set_end_location(end_loc);
4478 return Statement::make_block_statement(b, loc);
4481 // Return the break label, creating it if necessary.
4484 For_statement::break_label()
4486 if (this->break_label_ == NULL)
4487 this->break_label_ = new Unnamed_label(this->location());
4488 return this->break_label_;
4491 // Return the continue LABEL_EXPR.
4494 For_statement::continue_label()
4496 if (this->continue_label_ == NULL)
4497 this->continue_label_ = new Unnamed_label(this->location());
4498 return this->continue_label_;
4501 // Set the break and continue labels a for statement. This is used
4502 // when lowering a for range statement.
4505 For_statement::set_break_continue_labels(Unnamed_label* break_label,
4506 Unnamed_label* continue_label)
4508 gcc_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
4509 this->break_label_ = break_label;
4510 this->continue_label_ = continue_label;
4513 // Make a for statement.
4516 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
4517 source_location location)
4519 return new For_statement(init, cond, post, location);
4522 // Class For_range_statement.
4527 For_range_statement::do_traverse(Traverse* traverse)
4529 if (this->traverse_expression(traverse, &this->index_var_) == TRAVERSE_EXIT)
4530 return TRAVERSE_EXIT;
4531 if (this->value_var_ != NULL)
4533 if (this->traverse_expression(traverse, &this->value_var_)
4535 return TRAVERSE_EXIT;
4537 if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
4538 return TRAVERSE_EXIT;
4539 return this->statements_->traverse(traverse);
4542 // Lower a for range statement. For simplicity we lower this into a
4543 // for statement, which will then be lowered in turn to goto
4547 For_range_statement::do_lower(Gogo* gogo, Block* enclosing)
4549 Type* range_type = this->range_->type();
4550 if (range_type->points_to() != NULL
4551 && range_type->points_to()->array_type() != NULL
4552 && !range_type->points_to()->is_open_array_type())
4553 range_type = range_type->points_to();
4556 Type* value_type = NULL;
4557 if (range_type->array_type() != NULL)
4559 index_type = Type::lookup_integer_type("int");
4560 value_type = range_type->array_type()->element_type();
4562 else if (range_type->is_string_type())
4564 index_type = Type::lookup_integer_type("int");
4565 value_type = index_type;
4567 else if (range_type->map_type() != NULL)
4569 index_type = range_type->map_type()->key_type();
4570 value_type = range_type->map_type()->val_type();
4572 else if (range_type->channel_type() != NULL)
4574 index_type = range_type->channel_type()->element_type();
4575 if (this->value_var_ != NULL)
4577 if (!this->value_var_->type()->is_error_type())
4578 this->report_error(_("too many variables for range clause "
4580 return Statement::make_error_statement(this->location());
4585 this->report_error(_("range clause must have "
4586 "array, slice, setring, map, or channel type"));
4587 return Statement::make_error_statement(this->location());
4590 source_location loc = this->location();
4591 Block* temp_block = new Block(enclosing, loc);
4593 Named_object* range_object = NULL;
4594 Temporary_statement* range_temp = NULL;
4595 Var_expression* ve = this->range_->var_expression();
4597 range_object = ve->named_object();
4600 range_temp = Statement::make_temporary(NULL, this->range_, loc);
4601 temp_block->add_statement(range_temp);
4604 Temporary_statement* index_temp = Statement::make_temporary(index_type,
4606 temp_block->add_statement(index_temp);
4608 Temporary_statement* value_temp = NULL;
4609 if (this->value_var_ != NULL)
4611 value_temp = Statement::make_temporary(value_type, NULL, loc);
4612 temp_block->add_statement(value_temp);
4615 Block* body = new Block(temp_block, loc);
4622 // Arrange to do a loop appropriate for the type. We will produce
4623 // for INIT ; COND ; POST {
4625 // INDEX = INDEX_TEMP
4626 // VALUE = VALUE_TEMP // If there is a value
4627 // original statements
4630 if (range_type->array_type() != NULL)
4631 this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
4632 index_temp, value_temp, &init, &cond, &iter_init,
4634 else if (range_type->is_string_type())
4635 this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
4636 index_temp, value_temp, &init, &cond, &iter_init,
4638 else if (range_type->map_type() != NULL)
4639 this->lower_range_map(gogo, temp_block, body, range_object, range_temp,
4640 index_temp, value_temp, &init, &cond, &iter_init,
4642 else if (range_type->channel_type() != NULL)
4643 this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
4644 index_temp, value_temp, &init, &cond, &iter_init,
4649 if (iter_init != NULL)
4650 body->add_statement(Statement::make_block_statement(iter_init, loc));
4653 Expression* index_ref = Expression::make_temporary_reference(index_temp, loc);
4654 if (this->value_var_ == NULL)
4656 assign = Statement::make_assignment(this->index_var_, index_ref, loc);
4660 Expression_list* lhs = new Expression_list();
4661 lhs->push_back(this->index_var_);
4662 lhs->push_back(this->value_var_);
4664 Expression_list* rhs = new Expression_list();
4665 rhs->push_back(index_ref);
4666 rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4668 assign = Statement::make_tuple_assignment(lhs, rhs, loc);
4670 body->add_statement(assign);
4672 body->add_statement(Statement::make_block_statement(this->statements_, loc));
4674 body->set_end_location(this->statements_->end_location());
4676 For_statement* loop = Statement::make_for_statement(init, cond, post,
4678 loop->add_statements(body);
4679 loop->set_break_continue_labels(this->break_label_, this->continue_label_);
4681 temp_block->add_statement(loop);
4683 return Statement::make_block_statement(temp_block, loc);
4686 // Return a reference to the range, which may be in RANGE_OBJECT or in
4690 For_range_statement::make_range_ref(Named_object* range_object,
4691 Temporary_statement* range_temp,
4692 source_location loc)
4694 if (range_object != NULL)
4695 return Expression::make_var_reference(range_object, loc);
4697 return Expression::make_temporary_reference(range_temp, loc);
4700 // Return a call to the predeclared function FUNCNAME passing a
4701 // reference to the temporary variable ARG.
4704 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
4706 source_location loc)
4708 Named_object* no = gogo->lookup_global(funcname);
4709 gcc_assert(no != NULL && no->is_function_declaration());
4710 Expression* func = Expression::make_func_reference(no, NULL, loc);
4711 Expression_list* params = new Expression_list();
4712 params->push_back(arg);
4713 return Expression::make_call(func, params, false, loc);
4716 // Lower a for range over an array or slice.
4719 For_range_statement::lower_range_array(Gogo* gogo,
4722 Named_object* range_object,
4723 Temporary_statement* range_temp,
4724 Temporary_statement* index_temp,
4725 Temporary_statement* value_temp,
4731 source_location loc = this->location();
4733 // The loop we generate:
4734 // len_temp := len(range)
4735 // for index_temp = 0; index_temp < len_temp; index_temp++ {
4736 // value_temp = range[index_temp]
4737 // index = index_temp
4738 // value = value_temp
4744 // len_temp = len(range)
4747 Block* init = new Block(enclosing, loc);
4749 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
4750 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
4751 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
4753 init->add_statement(len_temp);
4756 mpz_init_set_ui(zval, 0UL);
4757 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4760 ref = Expression::make_temporary_reference(index_temp, loc);
4761 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4762 init->add_statement(s);
4767 // index_temp < len_temp
4769 ref = Expression::make_temporary_reference(index_temp, loc);
4770 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
4771 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
4775 // Set *PITER_INIT to
4776 // value_temp = range[index_temp]
4778 Block* iter_init = NULL;
4779 if (value_temp != NULL)
4781 iter_init = new Block(body_block, loc);
4783 ref = this->make_range_ref(range_object, range_temp, loc);
4784 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
4785 Expression* index = Expression::make_index(ref, ref2, NULL, loc);
4787 ref = Expression::make_temporary_reference(value_temp, loc);
4788 s = Statement::make_assignment(ref, index, loc);
4790 iter_init->add_statement(s);
4792 *piter_init = iter_init;
4797 Block* post = new Block(enclosing, loc);
4798 ref = Expression::make_temporary_reference(index_temp, loc);
4799 s = Statement::make_inc_statement(ref);
4800 post->add_statement(s);
4804 // Lower a for range over a string.
4807 For_range_statement::lower_range_string(Gogo* gogo,
4810 Named_object* range_object,
4811 Temporary_statement* range_temp,
4812 Temporary_statement* index_temp,
4813 Temporary_statement* value_temp,
4819 source_location loc = this->location();
4821 // The loop we generate:
4822 // var next_index_temp int
4823 // for index_temp = 0; ; index_temp = next_index_temp {
4824 // next_index_temp, value_temp = stringiter2(range, index_temp)
4825 // if next_index_temp == 0 {
4828 // index = index_temp
4829 // value = value_temp
4834 // var next_index_temp int
4837 Block* init = new Block(enclosing, loc);
4839 Temporary_statement* next_index_temp =
4840 Statement::make_temporary(index_temp->type(), NULL, loc);
4841 init->add_statement(next_index_temp);
4844 mpz_init_set_ui(zval, 0UL);
4845 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4847 Expression* ref = Expression::make_temporary_reference(index_temp, loc);
4848 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4850 init->add_statement(s);
4853 // The loop has no condition.
4857 // Set *PITER_INIT to
4858 // next_index_temp = runtime.stringiter(range, index_temp)
4860 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
4862 // if next_index_temp == 0 {
4866 Block* iter_init = new Block(body_block, loc);
4869 if (value_temp == NULL)
4871 static Named_object* stringiter;
4872 if (stringiter == NULL)
4874 source_location bloc = BUILTINS_LOCATION;
4875 Type* int_type = gogo->lookup_global("int")->type_value();
4877 Typed_identifier_list* params = new Typed_identifier_list();
4878 params->push_back(Typed_identifier("s", Type::make_string_type(),
4880 params->push_back(Typed_identifier("k", int_type, bloc));
4882 Typed_identifier_list* results = new Typed_identifier_list();
4883 results->push_back(Typed_identifier("", int_type, bloc));
4885 Function_type* fntype = Type::make_function_type(NULL, params,
4887 stringiter = Named_object::make_function_declaration("stringiter",
4890 const char* n = "runtime.stringiter";
4891 stringiter->func_declaration_value()->set_asm_name(n);
4897 static Named_object* stringiter2;
4898 if (stringiter2 == NULL)
4900 source_location bloc = BUILTINS_LOCATION;
4901 Type* int_type = gogo->lookup_global("int")->type_value();
4903 Typed_identifier_list* params = new Typed_identifier_list();
4904 params->push_back(Typed_identifier("s", Type::make_string_type(),
4906 params->push_back(Typed_identifier("k", int_type, bloc));
4908 Typed_identifier_list* results = new Typed_identifier_list();
4909 results->push_back(Typed_identifier("", int_type, bloc));
4910 results->push_back(Typed_identifier("", int_type, bloc));
4912 Function_type* fntype = Type::make_function_type(NULL, params,
4914 stringiter2 = Named_object::make_function_declaration("stringiter",
4917 const char* n = "runtime.stringiter2";
4918 stringiter2->func_declaration_value()->set_asm_name(n);
4923 Expression* func = Expression::make_func_reference(no, NULL, loc);
4924 Expression_list* params = new Expression_list();
4925 params->push_back(this->make_range_ref(range_object, range_temp, loc));
4926 params->push_back(Expression::make_temporary_reference(index_temp, loc));
4927 Call_expression* call = Expression::make_call(func, params, false, loc);
4929 if (value_temp == NULL)
4931 ref = Expression::make_temporary_reference(next_index_temp, loc);
4932 s = Statement::make_assignment(ref, call, loc);
4936 Expression_list* lhs = new Expression_list();
4937 lhs->push_back(Expression::make_temporary_reference(next_index_temp,
4939 lhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4941 Expression_list* rhs = new Expression_list();
4942 rhs->push_back(Expression::make_call_result(call, 0));
4943 rhs->push_back(Expression::make_call_result(call, 1));
4945 s = Statement::make_tuple_assignment(lhs, rhs, loc);
4947 iter_init->add_statement(s);
4949 ref = Expression::make_temporary_reference(next_index_temp, loc);
4950 zexpr = Expression::make_integer(&zval, NULL, loc);
4952 Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
4954 Block* then_block = new Block(iter_init, loc);
4955 s = Statement::make_break_statement(this->break_label(), loc);
4956 then_block->add_statement(s);
4958 s = Statement::make_if_statement(equals, then_block, NULL, loc);
4959 iter_init->add_statement(s);
4961 *piter_init = iter_init;
4964 // index_temp = next_index_temp
4966 Block* post = new Block(enclosing, loc);
4968 Expression* lhs = Expression::make_temporary_reference(index_temp, loc);
4969 Expression* rhs = Expression::make_temporary_reference(next_index_temp, loc);
4970 s = Statement::make_assignment(lhs, rhs, loc);
4972 post->add_statement(s);
4976 // Lower a for range over a map.
4979 For_range_statement::lower_range_map(Gogo* gogo,
4982 Named_object* range_object,
4983 Temporary_statement* range_temp,
4984 Temporary_statement* index_temp,
4985 Temporary_statement* value_temp,
4991 source_location loc = this->location();
4993 // The runtime uses a struct to handle ranges over a map. The
4994 // struct is four pointers long. The first pointer is NULL when we
4995 // have completed the iteration.
4997 // The loop we generate:
4998 // var hiter map_iteration_struct
4999 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
5000 // mapiter2(hiter, &index_temp, &value_temp)
5001 // index = index_temp
5002 // value = value_temp
5007 // var hiter map_iteration_struct
5008 // runtime.mapiterinit(range, &hiter)
5010 Block* init = new Block(enclosing, loc);
5012 const unsigned long map_iteration_size = 4;
5015 mpz_init_set_ui(ival, map_iteration_size);
5016 Expression* iexpr = Expression::make_integer(&ival, NULL, loc);
5019 Type* byte_type = gogo->lookup_global("byte")->type_value();
5020 Type* ptr_type = Type::make_pointer_type(byte_type);
5022 Type* map_iteration_type = Type::make_array_type(ptr_type, iexpr);
5023 Type* map_iteration_ptr = Type::make_pointer_type(map_iteration_type);
5025 Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
5027 init->add_statement(hiter);
5029 source_location bloc = BUILTINS_LOCATION;
5030 Typed_identifier_list* param_types = new Typed_identifier_list();
5031 param_types->push_back(Typed_identifier("map", this->range_->type(), bloc));
5032 param_types->push_back(Typed_identifier("it", map_iteration_ptr, bloc));
5033 Function_type* fntype = Type::make_function_type(NULL, param_types, NULL,
5036 Named_object* mapiterinit =
5037 Named_object::make_function_declaration("mapiterinit", NULL, fntype, bloc);
5038 const char* n = "runtime.mapiterinit";
5039 mapiterinit->func_declaration_value()->set_asm_name(n);
5041 Expression* func = Expression::make_func_reference(mapiterinit, NULL, loc);
5042 Expression_list* params = new Expression_list();
5043 params->push_back(this->make_range_ref(range_object, range_temp, loc));
5044 Expression* ref = Expression::make_temporary_reference(hiter, loc);
5045 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5046 Expression* call = Expression::make_call(func, params, false, loc);
5047 init->add_statement(Statement::make_statement(call));
5054 ref = Expression::make_temporary_reference(hiter, loc);
5057 mpz_init_set_ui(zval, 0UL);
5058 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
5061 Expression* index = Expression::make_index(ref, zexpr, NULL, loc);
5063 Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
5064 Expression::make_nil(loc),
5069 // Set *PITER_INIT to
5070 // mapiter1(hiter, &index_temp)
5072 // mapiter2(hiter, &index_temp, &value_temp)
5074 Block* iter_init = new Block(body_block, loc);
5076 param_types = new Typed_identifier_list();
5077 param_types->push_back(Typed_identifier("hiter", map_iteration_ptr, bloc));
5078 Type* pkey_type = Type::make_pointer_type(index_temp->type());
5079 param_types->push_back(Typed_identifier("key", pkey_type, bloc));
5080 if (value_temp != NULL)
5082 Type* pval_type = Type::make_pointer_type(value_temp->type());
5083 param_types->push_back(Typed_identifier("val", pval_type, bloc));
5085 fntype = Type::make_function_type(NULL, param_types, NULL, bloc);
5086 n = value_temp == NULL ? "mapiter1" : "mapiter2";
5087 Named_object* mapiter = Named_object::make_function_declaration(n, NULL,
5089 n = value_temp == NULL ? "runtime.mapiter1" : "runtime.mapiter2";
5090 mapiter->func_declaration_value()->set_asm_name(n);
5092 func = Expression::make_func_reference(mapiter, NULL, loc);
5093 params = new Expression_list();
5094 ref = Expression::make_temporary_reference(hiter, loc);
5095 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5096 ref = Expression::make_temporary_reference(index_temp, loc);
5097 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5098 if (value_temp != NULL)
5100 ref = Expression::make_temporary_reference(value_temp, loc);
5101 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5103 call = Expression::make_call(func, params, false, loc);
5104 iter_init->add_statement(Statement::make_statement(call));
5106 *piter_init = iter_init;
5109 // mapiternext(&hiter)
5111 Block* post = new Block(enclosing, loc);
5113 static Named_object* mapiternext;
5114 if (mapiternext == NULL)
5116 param_types = new Typed_identifier_list();
5117 param_types->push_back(Typed_identifier("it", map_iteration_ptr, bloc));
5118 fntype = Type::make_function_type(NULL, param_types, NULL, bloc);
5119 mapiternext = Named_object::make_function_declaration("mapiternext",
5122 const char* n = "runtime.mapiternext";
5123 mapiternext->func_declaration_value()->set_asm_name(n);
5126 func = Expression::make_func_reference(mapiternext, NULL, loc);
5127 params = new Expression_list();
5128 ref = Expression::make_temporary_reference(hiter, loc);
5129 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5130 call = Expression::make_call(func, params, false, loc);
5131 post->add_statement(Statement::make_statement(call));
5136 // Lower a for range over a channel.
5139 For_range_statement::lower_range_channel(Gogo* gogo,
5142 Named_object* range_object,
5143 Temporary_statement* range_temp,
5144 Temporary_statement* index_temp,
5145 Temporary_statement* value_temp,
5151 gcc_assert(value_temp == NULL);
5153 source_location loc = this->location();
5155 // The loop we generate:
5157 // index_temp = <-range
5158 // if closed(range) {
5161 // index = index_temp
5162 // value = value_temp
5166 // We have no initialization code, no condition, and no post code.
5172 // Set *PITER_INIT to
5173 // index_temp = <-range
5174 // if closed(range) {
5178 Block* iter_init = new Block(body_block, loc);
5180 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5181 Expression* cond = this->call_builtin(gogo, "closed", ref, loc);
5183 ref = this->make_range_ref(range_object, range_temp, loc);
5184 Expression* recv = Expression::make_receive(ref, loc);
5185 ref = Expression::make_temporary_reference(index_temp, loc);
5186 Statement* s = Statement::make_assignment(ref, recv, loc);
5187 iter_init->add_statement(s);
5189 Block* then_block = new Block(iter_init, loc);
5190 s = Statement::make_break_statement(this->break_label(), loc);
5191 then_block->add_statement(s);
5193 s = Statement::make_if_statement(cond, then_block, NULL, loc);
5194 iter_init->add_statement(s);
5196 *piter_init = iter_init;
5199 // Return the break LABEL_EXPR.
5202 For_range_statement::break_label()
5204 if (this->break_label_ == NULL)
5205 this->break_label_ = new Unnamed_label(this->location());
5206 return this->break_label_;
5209 // Return the continue LABEL_EXPR.
5212 For_range_statement::continue_label()
5214 if (this->continue_label_ == NULL)
5215 this->continue_label_ = new Unnamed_label(this->location());
5216 return this->continue_label_;
5219 // Make a for statement with a range clause.
5221 For_range_statement*
5222 Statement::make_for_range_statement(Expression* index_var,
5223 Expression* value_var,
5225 source_location location)
5227 return new For_range_statement(index_var, value_var, range, location);