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() & Traverse::traverse_expressions) == 0)
112 return TRAVERSE_CONTINUE;
113 return expr_list->traverse(traverse);
116 // Traverse a type in a statement. This is a helper function for
120 Statement::traverse_type(Traverse* traverse, Type* type)
122 if ((traverse->traverse_mask()
123 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
124 return TRAVERSE_CONTINUE;
125 return Type::traverse(type, traverse);
128 // Set type information for unnamed constants. This is really done by
132 Statement::determine_types()
134 this->do_determine_types();
137 // If this is a thunk statement, return it.
140 Statement::thunk_statement()
142 Thunk_statement* ret = this->convert<Thunk_statement, STATEMENT_GO>();
144 ret = this->convert<Thunk_statement, STATEMENT_DEFER>();
148 // Get a tree for a Statement. This is really done by the child
152 Statement::get_tree(Translate_context* context)
154 if (this->classification_ == STATEMENT_ERROR)
155 return error_mark_node;
157 return this->do_get_tree(context);
160 // Build tree nodes and set locations.
163 Statement::build_stmt_1(int tree_code_value, tree node)
165 tree ret = build1(static_cast<tree_code>(tree_code_value),
166 void_type_node, node);
167 SET_EXPR_LOCATION(ret, this->location_);
171 // Note that this statement is erroneous. This is called by children
172 // when they discover an error.
175 Statement::set_is_error()
177 this->classification_ = STATEMENT_ERROR;
180 // For children to call to report an error conveniently.
183 Statement::report_error(const char* msg)
185 error_at(this->location_, "%s", msg);
186 this->set_is_error();
189 // An error statement, used to avoid crashing after we report an
192 class Error_statement : public Statement
195 Error_statement(source_location location)
196 : Statement(STATEMENT_ERROR, location)
201 do_traverse(Traverse*)
202 { return TRAVERSE_CONTINUE; }
205 do_get_tree(Translate_context*)
206 { gcc_unreachable(); }
209 // Make an error statement.
212 Statement::make_error_statement(source_location location)
214 return new Error_statement(location);
217 // Class Variable_declaration_statement.
219 Variable_declaration_statement::Variable_declaration_statement(
221 : Statement(STATEMENT_VARIABLE_DECLARATION, var->var_value()->location()),
226 // We don't actually traverse the variable here; it was traversed
227 // while traversing the Block.
230 Variable_declaration_statement::do_traverse(Traverse*)
232 return TRAVERSE_CONTINUE;
235 // Traverse the assignments in a variable declaration. Note that this
236 // traversal is different from the usual traversal.
239 Variable_declaration_statement::do_traverse_assignments(
240 Traverse_assignments* tassign)
242 tassign->initialize_variable(this->var_);
246 // Return the tree for a variable declaration.
249 Variable_declaration_statement::do_get_tree(Translate_context* context)
251 tree val = this->var_->get_tree(context->gogo(), context->function());
252 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
253 return error_mark_node;
254 Variable* variable = this->var_->var_value();
256 tree init = variable->get_init_tree(context->gogo(), context->function());
257 if (init == error_mark_node)
258 return error_mark_node;
260 // If this variable lives on the heap, we need to allocate it now.
261 if (!variable->is_in_heap())
263 DECL_INITIAL(val) = init;
264 return this->build_stmt_1(DECL_EXPR, val);
268 gcc_assert(TREE_CODE(val) == INDIRECT_REF);
269 tree decl = TREE_OPERAND(val, 0);
270 gcc_assert(TREE_CODE(decl) == VAR_DECL);
271 tree type = TREE_TYPE(decl);
272 gcc_assert(POINTER_TYPE_P(type));
273 tree size = TYPE_SIZE_UNIT(TREE_TYPE(type));
274 tree space = context->gogo()->allocate_memory(variable->type(), size,
276 space = fold_convert(TREE_TYPE(decl), space);
277 DECL_INITIAL(decl) = space;
278 return build2(COMPOUND_EXPR, void_type_node,
279 this->build_stmt_1(DECL_EXPR, decl),
280 build2(MODIFY_EXPR, void_type_node, val, init));
284 // Make a variable declaration.
287 Statement::make_variable_declaration(Named_object* var)
289 return new Variable_declaration_statement(var);
292 // Class Temporary_statement.
294 // Return the type of the temporary variable.
297 Temporary_statement::type() const
299 return this->type_ != NULL ? this->type_ : this->init_->type();
305 Temporary_statement::do_traverse(Traverse* traverse)
307 if (this->init_ == NULL)
308 return TRAVERSE_CONTINUE;
310 return this->traverse_expression(traverse, &this->init_);
313 // Traverse assignments.
316 Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
318 if (this->init_ == NULL)
320 tassign->value(&this->init_, true, true);
327 Temporary_statement::do_determine_types()
329 if (this->init_ != NULL)
331 if (this->type_ == NULL)
332 this->init_->determine_type_no_context();
335 Type_context context(this->type_, false);
336 this->init_->determine_type(&context);
340 if (this->type_ == NULL)
341 this->type_ = this->init_->type();
343 if (this->type_->is_abstract())
344 this->type_ = this->type_->make_non_abstract_type();
350 Temporary_statement::do_check_types(Gogo*)
352 if (this->type_ != NULL && this->init_ != NULL)
353 gcc_assert(Type::are_assignable(this->type_, this->init_->type(), NULL));
359 Temporary_statement::do_get_tree(Translate_context* context)
361 gcc_assert(this->decl_ == NULL_TREE);
362 tree type_tree = this->type()->get_tree(context->gogo());
363 if (type_tree == error_mark_node)
365 this->decl_ = error_mark_node;
366 return error_mark_node;
368 // We can only use create_tmp_var if the type is not addressable.
369 if (!TREE_ADDRESSABLE(type_tree))
371 this->decl_ = create_tmp_var(type_tree, "GOTMP");
372 DECL_SOURCE_LOCATION(this->decl_) = this->location();
376 gcc_assert(context->function() != NULL && context->block() != NULL);
377 tree decl = build_decl(this->location(), VAR_DECL,
378 create_tmp_var_name("GOTMP"),
380 DECL_ARTIFICIAL(decl) = 1;
381 DECL_IGNORED_P(decl) = 1;
383 gcc_assert(current_function_decl != NULL_TREE);
384 DECL_CONTEXT(decl) = current_function_decl;
386 // We have to add this variable to the block so that it winds up
388 tree block_tree = context->block_tree();
389 gcc_assert(block_tree != NULL_TREE);
390 DECL_CHAIN(decl) = BLOCK_VARS(block_tree);
391 BLOCK_VARS(block_tree) = decl;
395 if (this->init_ != NULL)
396 DECL_INITIAL(this->decl_) =
397 Expression::convert_for_assignment(context, this->type(),
399 this->init_->get_tree(context),
401 if (this->is_address_taken_)
402 TREE_ADDRESSABLE(this->decl_) = 1;
403 return this->build_stmt_1(DECL_EXPR, this->decl_);
406 // Make and initialize a temporary variable in BLOCK.
409 Statement::make_temporary(Type* type, Expression* init,
410 source_location location)
412 return new Temporary_statement(type, init, location);
415 // An assignment statement.
417 class Assignment_statement : public Statement
420 Assignment_statement(Expression* lhs, Expression* rhs,
421 source_location location)
422 : Statement(STATEMENT_ASSIGNMENT, location),
428 do_traverse(Traverse* traverse);
431 do_traverse_assignments(Traverse_assignments*);
434 do_determine_types();
437 do_check_types(Gogo*);
440 do_get_tree(Translate_context*);
443 // Left hand side--the lvalue.
445 // Right hand side--the rvalue.
452 Assignment_statement::do_traverse(Traverse* traverse)
454 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
455 return TRAVERSE_EXIT;
456 return this->traverse_expression(traverse, &this->rhs_);
460 Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
462 tassign->assignment(&this->lhs_, &this->rhs_);
466 // Set types for the assignment.
469 Assignment_statement::do_determine_types()
471 this->lhs_->determine_type_no_context();
472 Type_context context(this->lhs_->type(), false);
473 this->rhs_->determine_type(&context);
476 // Check types for an assignment.
479 Assignment_statement::do_check_types(Gogo*)
481 // The left hand side must be either addressable, a map index
482 // expression, or the blank identifier.
483 if (!this->lhs_->is_addressable()
484 && this->lhs_->map_index_expression() == NULL
485 && !this->lhs_->is_sink_expression())
487 if (!this->lhs_->type()->is_error_type())
488 this->report_error(_("invalid left hand side of assignment"));
492 Type* lhs_type = this->lhs_->type();
493 Type* rhs_type = this->rhs_->type();
495 if (!Type::are_assignable(lhs_type, rhs_type, &reason))
498 error_at(this->location(), "incompatible types in assignment");
500 error_at(this->location(), "incompatible types in assignment (%s)",
502 this->set_is_error();
505 if (lhs_type->is_error_type()
506 || rhs_type->is_error_type()
507 || lhs_type->is_undefined()
508 || rhs_type->is_undefined())
510 // Make sure we get the error for an undefined type.
513 this->set_is_error();
517 // Build a tree for an assignment statement.
520 Assignment_statement::do_get_tree(Translate_context* context)
522 tree rhs_tree = this->rhs_->get_tree(context);
524 if (this->lhs_->is_sink_expression())
527 tree lhs_tree = this->lhs_->get_tree(context);
529 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
530 return error_mark_node;
532 rhs_tree = Expression::convert_for_assignment(context, this->lhs_->type(),
533 this->rhs_->type(), rhs_tree,
535 if (rhs_tree == error_mark_node)
536 return error_mark_node;
538 return fold_build2_loc(this->location(), MODIFY_EXPR, void_type_node,
542 // Make an assignment statement.
545 Statement::make_assignment(Expression* lhs, Expression* rhs,
546 source_location location)
548 return new Assignment_statement(lhs, rhs, location);
551 // The Move_ordered_evals class is used to find any subexpressions of
552 // an expression that have an evaluation order dependency. It creates
553 // temporary variables to hold them.
555 class Move_ordered_evals : public Traverse
558 Move_ordered_evals(Block* block)
559 : Traverse(traverse_expressions),
565 expression(Expression**);
568 // The block where new temporary variables should be added.
573 Move_ordered_evals::expression(Expression** pexpr)
575 // We have to look at subexpressions first.
576 if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
577 return TRAVERSE_EXIT;
578 if ((*pexpr)->must_eval_in_order())
580 source_location loc = (*pexpr)->location();
581 Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
582 this->block_->add_statement(temp);
583 *pexpr = Expression::make_temporary_reference(temp, loc);
585 return TRAVERSE_SKIP_COMPONENTS;
588 // An assignment operation statement.
590 class Assignment_operation_statement : public Statement
593 Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
594 source_location location)
595 : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
596 op_(op), lhs_(lhs), rhs_(rhs)
601 do_traverse(Traverse*);
604 do_traverse_assignments(Traverse_assignments*)
605 { gcc_unreachable(); }
608 do_lower(Gogo*, Block*);
611 do_get_tree(Translate_context*)
612 { gcc_unreachable(); }
615 // The operator (OPERATOR_PLUSEQ, etc.).
626 Assignment_operation_statement::do_traverse(Traverse* traverse)
628 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
629 return TRAVERSE_EXIT;
630 return this->traverse_expression(traverse, &this->rhs_);
633 // Lower an assignment operation statement to a regular assignment
637 Assignment_operation_statement::do_lower(Gogo*, Block* enclosing)
639 source_location loc = this->location();
641 // We have to evaluate the left hand side expression only once. We
642 // do this by moving out any expression with side effects.
643 Block* b = new Block(enclosing, loc);
644 Move_ordered_evals moe(b);
645 this->lhs_->traverse_subexpressions(&moe);
647 Expression* lval = this->lhs_->copy();
652 case OPERATOR_PLUSEQ:
655 case OPERATOR_MINUSEQ:
664 case OPERATOR_MULTEQ:
673 case OPERATOR_LSHIFTEQ:
674 op = OPERATOR_LSHIFT;
676 case OPERATOR_RSHIFTEQ:
677 op = OPERATOR_RSHIFT;
682 case OPERATOR_BITCLEAREQ:
683 op = OPERATOR_BITCLEAR;
689 Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
690 Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
691 if (b->statements()->empty())
699 return Statement::make_block_statement(b, loc);
703 // Make an assignment operation statement.
706 Statement::make_assignment_operation(Operator op, Expression* lhs,
707 Expression* rhs, source_location location)
709 return new Assignment_operation_statement(op, lhs, rhs, location);
712 // A tuple assignment statement. This differs from an assignment
713 // statement in that the right-hand-side expressions are evaluated in
716 class Tuple_assignment_statement : public Statement
719 Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
720 source_location location)
721 : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
727 do_traverse(Traverse* traverse);
730 do_traverse_assignments(Traverse_assignments*)
731 { gcc_unreachable(); }
734 do_lower(Gogo*, Block*);
737 do_get_tree(Translate_context*)
738 { gcc_unreachable(); }
741 // Left hand side--a list of lvalues.
742 Expression_list* lhs_;
743 // Right hand side--a list of rvalues.
744 Expression_list* rhs_;
750 Tuple_assignment_statement::do_traverse(Traverse* traverse)
752 if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
753 return TRAVERSE_EXIT;
754 return this->traverse_expression_list(traverse, this->rhs_);
757 // Lower a tuple assignment. We use temporary variables to split it
758 // up into a set of single assignments.
761 Tuple_assignment_statement::do_lower(Gogo*, Block* enclosing)
763 source_location loc = this->location();
765 Block* b = new Block(enclosing, loc);
767 // First move out any subexpressions on the left hand side. The
768 // right hand side will be evaluated in the required order anyhow.
769 Move_ordered_evals moe(b);
770 for (Expression_list::const_iterator plhs = this->lhs_->begin();
771 plhs != this->lhs_->end();
773 (*plhs)->traverse_subexpressions(&moe);
775 std::vector<Temporary_statement*> temps;
776 temps.reserve(this->lhs_->size());
778 Expression_list::const_iterator prhs = this->rhs_->begin();
779 for (Expression_list::const_iterator plhs = this->lhs_->begin();
780 plhs != this->lhs_->end();
783 gcc_assert(prhs != this->rhs_->end());
785 if ((*plhs)->is_sink_expression())
787 b->add_statement(Statement::make_statement(*prhs));
791 Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
793 b->add_statement(temp);
794 temps.push_back(temp);
797 gcc_assert(prhs == this->rhs_->end());
799 prhs = this->rhs_->begin();
800 std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
801 for (Expression_list::const_iterator plhs = this->lhs_->begin();
802 plhs != this->lhs_->end();
805 if ((*plhs)->is_sink_expression())
808 Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
809 Statement* s = Statement::make_assignment(*plhs, ref, loc);
813 gcc_assert(ptemp == temps.end());
815 return Statement::make_block_statement(b, loc);
818 // Make a tuple assignment statement.
821 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
822 source_location location)
824 return new Tuple_assignment_statement(lhs, rhs, location);
827 // A tuple assignment from a map index expression.
830 class Tuple_map_assignment_statement : public Statement
833 Tuple_map_assignment_statement(Expression* val, Expression* present,
834 Expression* map_index,
835 source_location location)
836 : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
837 val_(val), present_(present), map_index_(map_index)
842 do_traverse(Traverse* traverse);
845 do_traverse_assignments(Traverse_assignments*)
846 { gcc_unreachable(); }
849 do_lower(Gogo*, Block*);
852 do_get_tree(Translate_context*)
853 { gcc_unreachable(); }
856 // Lvalue which receives the value from the map.
858 // Lvalue which receives whether the key value was present.
859 Expression* present_;
860 // The map index expression.
861 Expression* map_index_;
867 Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
869 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
870 || this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
871 return TRAVERSE_EXIT;
872 return this->traverse_expression(traverse, &this->map_index_);
875 // Lower a tuple map assignment.
878 Tuple_map_assignment_statement::do_lower(Gogo*, Block* enclosing)
880 source_location loc = this->location();
882 Map_index_expression* map_index = this->map_index_->map_index_expression();
883 if (map_index == NULL)
885 this->report_error(_("expected map index on right hand side"));
886 return Statement::make_error_statement(loc);
888 Map_type* map_type = map_index->get_map_type();
890 Block* b = new Block(enclosing, loc);
892 // Move out any subexpressions to make sure that functions are
893 // called in the required order.
894 Move_ordered_evals moe(b);
895 this->val_->traverse_subexpressions(&moe);
896 this->present_->traverse_subexpressions(&moe);
898 // Copy the key value into a temporary so that we can take its
899 // address without pushing the value onto the heap.
901 // var key_temp KEY_TYPE = MAP_INDEX
902 Temporary_statement* key_temp =
903 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
904 b->add_statement(key_temp);
906 // var val_temp VAL_TYPE
907 Temporary_statement* val_temp =
908 Statement::make_temporary(map_type->val_type(), NULL, loc);
909 b->add_statement(val_temp);
911 // var present_temp bool
912 Temporary_statement* present_temp =
913 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
914 b->add_statement(present_temp);
916 // func mapaccess2(hmap map[k]v, key *k, val *v) bool
917 source_location bloc = BUILTINS_LOCATION;
918 Typed_identifier_list* param_types = new Typed_identifier_list();
919 param_types->push_back(Typed_identifier("hmap", map_type, bloc));
920 Type* pkey_type = Type::make_pointer_type(map_type->key_type());
921 param_types->push_back(Typed_identifier("key", pkey_type, bloc));
922 Type* pval_type = Type::make_pointer_type(map_type->val_type());
923 param_types->push_back(Typed_identifier("val", pval_type, bloc));
925 Typed_identifier_list* ret_types = new Typed_identifier_list();
926 ret_types->push_back(Typed_identifier("", Type::make_boolean_type(), bloc));
928 Function_type* fntype = Type::make_function_type(NULL, param_types,
930 Named_object* mapaccess2 =
931 Named_object::make_function_declaration("mapaccess2", NULL, fntype, bloc);
932 mapaccess2->func_declaration_value()->set_asm_name("runtime.mapaccess2");
934 // present_temp = mapaccess2(MAP, &key_temp, &val_temp)
935 Expression* func = Expression::make_func_reference(mapaccess2, NULL, loc);
936 Expression_list* params = new Expression_list();
937 params->push_back(map_index->map());
938 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
939 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
940 ref = Expression::make_temporary_reference(val_temp, loc);
941 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
942 Expression* call = Expression::make_call(func, params, false, loc);
944 ref = Expression::make_temporary_reference(present_temp, loc);
945 Statement* s = Statement::make_assignment(ref, call, loc);
949 ref = Expression::make_temporary_reference(val_temp, loc);
950 s = Statement::make_assignment(this->val_, ref, loc);
953 // present = present_temp
954 ref = Expression::make_temporary_reference(present_temp, loc);
955 s = Statement::make_assignment(this->present_, ref, loc);
958 return Statement::make_block_statement(b, loc);
961 // Make a map assignment statement which returns a pair of values.
964 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
965 Expression* map_index,
966 source_location location)
968 return new Tuple_map_assignment_statement(val, present, map_index, location);
971 // Assign a pair of entries to a map.
974 class Map_assignment_statement : public Statement
977 Map_assignment_statement(Expression* map_index,
978 Expression* val, Expression* should_set,
979 source_location location)
980 : Statement(STATEMENT_MAP_ASSIGNMENT, location),
981 map_index_(map_index), val_(val), should_set_(should_set)
986 do_traverse(Traverse* traverse);
989 do_traverse_assignments(Traverse_assignments*)
990 { gcc_unreachable(); }
993 do_lower(Gogo*, Block*);
996 do_get_tree(Translate_context*)
997 { gcc_unreachable(); }
1000 // A reference to the map index which should be set or deleted.
1001 Expression* map_index_;
1002 // The value to add to the map.
1004 // Whether or not to add the value.
1005 Expression* should_set_;
1008 // Traverse a map assignment.
1011 Map_assignment_statement::do_traverse(Traverse* traverse)
1013 if (this->traverse_expression(traverse, &this->map_index_) == TRAVERSE_EXIT
1014 || this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
1015 return TRAVERSE_EXIT;
1016 return this->traverse_expression(traverse, &this->should_set_);
1019 // Lower a map assignment to a function call.
1022 Map_assignment_statement::do_lower(Gogo*, Block* enclosing)
1024 source_location loc = this->location();
1026 Map_index_expression* map_index = this->map_index_->map_index_expression();
1027 if (map_index == NULL)
1029 this->report_error(_("expected map index on left hand side"));
1030 return Statement::make_error_statement(loc);
1032 Map_type* map_type = map_index->get_map_type();
1034 Block* b = new Block(enclosing, loc);
1036 // Evaluate the map first to get order of evaluation right.
1037 // map_temp := m // we are evaluating m[k] = v, p
1038 Temporary_statement* map_temp = Statement::make_temporary(map_type,
1041 b->add_statement(map_temp);
1043 // var key_temp MAP_KEY_TYPE = k
1044 Temporary_statement* key_temp =
1045 Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1046 b->add_statement(key_temp);
1048 // var val_temp MAP_VAL_TYPE = v
1049 Temporary_statement* val_temp =
1050 Statement::make_temporary(map_type->val_type(), this->val_, loc);
1051 b->add_statement(val_temp);
1053 // func mapassign2(hmap map[k]v, key *k, val *v, p)
1054 source_location bloc = BUILTINS_LOCATION;
1055 Typed_identifier_list* param_types = new Typed_identifier_list();
1056 param_types->push_back(Typed_identifier("hmap", map_type, bloc));
1057 Type* pkey_type = Type::make_pointer_type(map_type->key_type());
1058 param_types->push_back(Typed_identifier("key", pkey_type, bloc));
1059 Type* pval_type = Type::make_pointer_type(map_type->val_type());
1060 param_types->push_back(Typed_identifier("val", pval_type, bloc));
1061 param_types->push_back(Typed_identifier("p", Type::lookup_bool_type(), bloc));
1062 Function_type* fntype = Type::make_function_type(NULL, param_types,
1064 Named_object* mapassign2 =
1065 Named_object::make_function_declaration("mapassign2", NULL, fntype, bloc);
1066 mapassign2->func_declaration_value()->set_asm_name("runtime.mapassign2");
1068 // mapassign2(map_temp, &key_temp, &val_temp, p)
1069 Expression* func = Expression::make_func_reference(mapassign2, NULL, loc);
1070 Expression_list* params = new Expression_list();
1071 params->push_back(Expression::make_temporary_reference(map_temp, loc));
1072 Expression* ref = Expression::make_temporary_reference(key_temp, loc);
1073 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1074 ref = Expression::make_temporary_reference(val_temp, loc);
1075 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1076 params->push_back(this->should_set_);
1077 Expression* call = Expression::make_call(func, params, false, loc);
1078 Statement* s = Statement::make_statement(call);
1079 b->add_statement(s);
1081 return Statement::make_block_statement(b, loc);
1084 // Make a statement which assigns a pair of entries to a map.
1087 Statement::make_map_assignment(Expression* map_index,
1088 Expression* val, Expression* should_set,
1089 source_location location)
1091 return new Map_assignment_statement(map_index, val, should_set, location);
1094 // A tuple assignment from a receive statement.
1096 class Tuple_receive_assignment_statement : public Statement
1099 Tuple_receive_assignment_statement(Expression* val, Expression* success,
1100 Expression* channel,
1101 source_location location)
1102 : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
1103 val_(val), success_(success), channel_(channel)
1108 do_traverse(Traverse* traverse);
1111 do_traverse_assignments(Traverse_assignments*)
1112 { gcc_unreachable(); }
1115 do_lower(Gogo*, Block*);
1118 do_get_tree(Translate_context*)
1119 { gcc_unreachable(); }
1122 // Lvalue which receives the value from the channel.
1124 // Lvalue which receives whether the read succeeded or failed.
1125 Expression* success_;
1126 // The channel on which we receive the value.
1127 Expression* channel_;
1133 Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
1135 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1136 || this->traverse_expression(traverse, &this->success_) == TRAVERSE_EXIT)
1137 return TRAVERSE_EXIT;
1138 return this->traverse_expression(traverse, &this->channel_);
1141 // Lower to a function call.
1144 Tuple_receive_assignment_statement::do_lower(Gogo*, Block* enclosing)
1146 source_location loc = this->location();
1148 Channel_type* channel_type = this->channel_->type()->channel_type();
1149 if (channel_type == NULL)
1151 this->report_error(_("expected channel"));
1152 return Statement::make_error_statement(loc);
1154 if (!channel_type->may_receive())
1156 this->report_error(_("invalid receive on send-only channel"));
1157 return Statement::make_error_statement(loc);
1160 Block* b = new Block(enclosing, loc);
1162 // Make sure that any subexpressions on the left hand side are
1163 // evaluated in the right order.
1164 Move_ordered_evals moe(b);
1165 this->val_->traverse_subexpressions(&moe);
1166 this->success_->traverse_subexpressions(&moe);
1168 // var val_temp ELEMENT_TYPE
1169 Temporary_statement* val_temp =
1170 Statement::make_temporary(channel_type->element_type(), NULL, loc);
1171 b->add_statement(val_temp);
1173 // var success_temp bool
1174 Temporary_statement* success_temp =
1175 Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
1176 b->add_statement(success_temp);
1178 // func chanrecv2(c chan T, val *T) bool
1179 source_location bloc = BUILTINS_LOCATION;
1180 Typed_identifier_list* param_types = new Typed_identifier_list();
1181 param_types->push_back(Typed_identifier("c", channel_type, bloc));
1182 Type* pelement_type = Type::make_pointer_type(channel_type->element_type());
1183 param_types->push_back(Typed_identifier("val", pelement_type, bloc));
1185 Typed_identifier_list* ret_types = new Typed_identifier_list();
1186 ret_types->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1188 Function_type* fntype = Type::make_function_type(NULL, param_types,
1190 Named_object* chanrecv2 =
1191 Named_object::make_function_declaration("chanrecv2", NULL, fntype, bloc);
1192 chanrecv2->func_declaration_value()->set_asm_name("runtime.chanrecv2");
1194 // success_temp = chanrecv2(channel, &val_temp)
1195 Expression* func = Expression::make_func_reference(chanrecv2, NULL, loc);
1196 Expression_list* params = new Expression_list();
1197 params->push_back(this->channel_);
1198 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1199 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1200 Expression* call = Expression::make_call(func, params, false, loc);
1201 ref = Expression::make_temporary_reference(success_temp, loc);
1202 Statement* s = Statement::make_assignment(ref, call, loc);
1203 b->add_statement(s);
1206 ref = Expression::make_temporary_reference(val_temp, loc);
1207 s = Statement::make_assignment(this->val_, ref, loc);
1208 b->add_statement(s);
1210 // success = success_temp
1211 ref = Expression::make_temporary_reference(success_temp, loc);
1212 s = Statement::make_assignment(this->success_, ref, loc);
1213 b->add_statement(s);
1215 return Statement::make_block_statement(b, loc);
1218 // Make a nonblocking receive statement.
1221 Statement::make_tuple_receive_assignment(Expression* val, Expression* success,
1222 Expression* channel,
1223 source_location location)
1225 return new Tuple_receive_assignment_statement(val, success, channel,
1229 // An assignment to a pair of values from a type guard. This is a
1230 // conditional type guard. v, ok = i.(type).
1232 class Tuple_type_guard_assignment_statement : public Statement
1235 Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
1236 Expression* expr, Type* type,
1237 source_location location)
1238 : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
1239 val_(val), ok_(ok), expr_(expr), type_(type)
1244 do_traverse(Traverse*);
1247 do_traverse_assignments(Traverse_assignments*)
1248 { gcc_unreachable(); }
1251 do_lower(Gogo*, Block*);
1254 do_get_tree(Translate_context*)
1255 { gcc_unreachable(); }
1259 lower_to_empty_interface(const char*);
1262 lower_to_type(const char*);
1265 lower_to_object_type(Block*, const char*);
1267 // The variable which recieves the converted value.
1269 // The variable which receives the indication of success.
1271 // The expression being converted.
1273 // The type to which the expression is being converted.
1277 // Traverse a type guard tuple assignment.
1280 Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
1282 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1283 || this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
1284 || this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
1285 return TRAVERSE_EXIT;
1286 return this->traverse_expression(traverse, &this->expr_);
1289 // Lower to a function call.
1292 Tuple_type_guard_assignment_statement::do_lower(Gogo*, Block* enclosing)
1294 source_location loc = this->location();
1296 Type* expr_type = this->expr_->type();
1297 if (expr_type->interface_type() == NULL)
1299 this->report_error(_("type assertion only valid for interface types"));
1300 return Statement::make_error_statement(loc);
1303 Block* b = new Block(enclosing, loc);
1305 // Make sure that any subexpressions on the left hand side are
1306 // evaluated in the right order.
1307 Move_ordered_evals moe(b);
1308 this->val_->traverse_subexpressions(&moe);
1309 this->ok_->traverse_subexpressions(&moe);
1311 bool expr_is_empty = expr_type->interface_type()->is_empty();
1312 Call_expression* call;
1313 if (this->type_->interface_type() != NULL)
1315 if (this->type_->interface_type()->is_empty())
1316 call = this->lower_to_empty_interface(expr_is_empty
1320 call = this->lower_to_type(expr_is_empty ? "ifaceE2I2" : "ifaceI2I2");
1322 else if (this->type_->points_to() != NULL)
1323 call = this->lower_to_type(expr_is_empty ? "ifaceE2T2P" : "ifaceI2T2P");
1326 this->lower_to_object_type(b, expr_is_empty ? "ifaceE2T2" : "ifaceI2T2");
1332 Expression* res = Expression::make_call_result(call, 0);
1333 Statement* s = Statement::make_assignment(this->val_, res, loc);
1334 b->add_statement(s);
1336 res = Expression::make_call_result(call, 1);
1337 s = Statement::make_assignment(this->ok_, res, loc);
1338 b->add_statement(s);
1341 return Statement::make_block_statement(b, loc);
1344 // Lower a conversion to an empty interface type.
1347 Tuple_type_guard_assignment_statement::lower_to_empty_interface(
1350 source_location loc = this->location();
1352 // func FNNAME(interface) (empty, bool)
1353 source_location bloc = BUILTINS_LOCATION;
1354 Typed_identifier_list* param_types = new Typed_identifier_list();
1355 param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
1356 Typed_identifier_list* ret_types = new Typed_identifier_list();
1357 ret_types->push_back(Typed_identifier("ret", this->type_, bloc));
1358 ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
1359 Function_type* fntype = Type::make_function_type(NULL, param_types,
1362 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1363 std::string asm_name = "runtime.";
1365 fn->func_declaration_value()->set_asm_name(asm_name);
1367 // val, ok = FNNAME(expr)
1368 Expression* func = Expression::make_func_reference(fn, NULL, loc);
1369 Expression_list* params = new Expression_list();
1370 params->push_back(this->expr_);
1371 return Expression::make_call(func, params, false, loc);
1374 // Lower a conversion to a non-empty interface type or a pointer type.
1377 Tuple_type_guard_assignment_statement::lower_to_type(const char* fnname)
1379 source_location loc = this->location();
1381 // func FNNAME(*descriptor, interface) (interface, bool)
1382 source_location bloc = BUILTINS_LOCATION;
1383 Typed_identifier_list* param_types = new Typed_identifier_list();
1384 param_types->push_back(Typed_identifier("inter",
1385 Type::make_type_descriptor_ptr_type(),
1387 param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
1388 Typed_identifier_list* ret_types = new Typed_identifier_list();
1389 ret_types->push_back(Typed_identifier("ret", this->type_, bloc));
1390 ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
1391 Function_type* fntype = Type::make_function_type(NULL, param_types,
1394 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1395 std::string asm_name = "runtime.";
1397 fn->func_declaration_value()->set_asm_name(asm_name);
1399 // val, ok = FNNAME(type_descriptor, expr)
1400 Expression* func = Expression::make_func_reference(fn, NULL, loc);
1401 Expression_list* params = new Expression_list();
1402 params->push_back(Expression::make_type_descriptor(this->type_, loc));
1403 params->push_back(this->expr_);
1404 return Expression::make_call(func, params, false, loc);
1407 // Lower a conversion to a non-interface non-pointer type.
1410 Tuple_type_guard_assignment_statement::lower_to_object_type(Block* b,
1413 source_location loc = this->location();
1415 // var val_temp TYPE
1416 Temporary_statement* val_temp = Statement::make_temporary(this->type_,
1418 b->add_statement(val_temp);
1420 // func FNNAME(*descriptor, interface, *T) bool
1421 source_location bloc = BUILTINS_LOCATION;
1422 Typed_identifier_list* param_types = new Typed_identifier_list();
1423 param_types->push_back(Typed_identifier("inter",
1424 Type::make_type_descriptor_ptr_type(),
1426 param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
1427 Type* ptype = Type::make_pointer_type(this->type_);
1428 param_types->push_back(Typed_identifier("v", ptype, bloc));
1429 Typed_identifier_list* ret_types = new Typed_identifier_list();
1430 ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
1431 Function_type* fntype = Type::make_function_type(NULL, param_types,
1434 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1435 std::string asm_name = "runtime.";
1437 fn->func_declaration_value()->set_asm_name(asm_name);
1439 // ok = FNNAME(type_descriptor, expr, &val_temp)
1440 Expression* func = Expression::make_func_reference(fn, NULL, loc);
1441 Expression_list* params = new Expression_list();
1442 params->push_back(Expression::make_type_descriptor(this->type_, loc));
1443 params->push_back(this->expr_);
1444 Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1445 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1446 Expression* call = Expression::make_call(func, params, false, loc);
1447 Statement* s = Statement::make_assignment(this->ok_, call, loc);
1448 b->add_statement(s);
1451 ref = Expression::make_temporary_reference(val_temp, loc);
1452 s = Statement::make_assignment(this->val_, ref, loc);
1453 b->add_statement(s);
1456 // Make an assignment from a type guard to a pair of variables.
1459 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
1460 Expression* expr, Type* type,
1461 source_location location)
1463 return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
1467 // An expression statement.
1469 class Expression_statement : public Statement
1472 Expression_statement(Expression* expr)
1473 : Statement(STATEMENT_EXPRESSION, expr->location()),
1479 do_traverse(Traverse* traverse)
1480 { return this->traverse_expression(traverse, &this->expr_); }
1483 do_determine_types()
1484 { this->expr_->determine_type_no_context(); }
1487 do_may_fall_through() const;
1490 do_get_tree(Translate_context* context)
1491 { return this->expr_->get_tree(context); }
1497 // An expression statement may fall through unless it is a call to a
1498 // function which does not return.
1501 Expression_statement::do_may_fall_through() const
1503 const Call_expression* call = this->expr_->call_expression();
1506 const Expression* fn = call->fn();
1507 const Func_expression* fe = fn->func_expression();
1510 const Named_object* no = fe->named_object();
1512 Function_type* fntype;
1513 if (no->is_function())
1514 fntype = no->func_value()->type();
1515 else if (no->is_function_declaration())
1516 fntype = no->func_declaration_value()->type();
1520 // The builtin function panic does not return.
1521 if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
1528 // Make an expression statement from an Expression.
1531 Statement::make_statement(Expression* expr)
1533 return new Expression_statement(expr);
1536 // A block statement--a list of statements which may include variable
1539 class Block_statement : public Statement
1542 Block_statement(Block* block, source_location location)
1543 : Statement(STATEMENT_BLOCK, location),
1549 do_traverse(Traverse* traverse)
1550 { return this->block_->traverse(traverse); }
1553 do_determine_types()
1554 { this->block_->determine_types(); }
1557 do_may_fall_through() const
1558 { return this->block_->may_fall_through(); }
1561 do_get_tree(Translate_context* context)
1562 { return this->block_->get_tree(context); }
1568 // Make a block statement.
1571 Statement::make_block_statement(Block* block, source_location location)
1573 return new Block_statement(block, location);
1576 // An increment or decrement statement.
1578 class Inc_dec_statement : public Statement
1581 Inc_dec_statement(bool is_inc, Expression* expr)
1582 : Statement(STATEMENT_INCDEC, expr->location()),
1583 expr_(expr), is_inc_(is_inc)
1588 do_traverse(Traverse* traverse)
1589 { return this->traverse_expression(traverse, &this->expr_); }
1592 do_traverse_assignments(Traverse_assignments*)
1593 { gcc_unreachable(); }
1596 do_lower(Gogo*, Block*);
1599 do_get_tree(Translate_context*)
1600 { gcc_unreachable(); }
1603 // The l-value to increment or decrement.
1605 // Whether to increment or decrement.
1609 // Lower to += or -=.
1612 Inc_dec_statement::do_lower(Gogo*, Block*)
1614 source_location loc = this->location();
1617 mpz_init_set_ui(oval, 1UL);
1618 Expression* oexpr = Expression::make_integer(&oval, NULL, loc);
1621 Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
1622 return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
1625 // Make an increment statement.
1628 Statement::make_inc_statement(Expression* expr)
1630 return new Inc_dec_statement(true, expr);
1633 // Make a decrement statement.
1636 Statement::make_dec_statement(Expression* expr)
1638 return new Inc_dec_statement(false, expr);
1641 // Class Thunk_statement. This is the base class for go and defer
1644 const char* const Thunk_statement::thunk_field_fn = "fn";
1646 const char* const Thunk_statement::thunk_field_receiver = "receiver";
1650 Thunk_statement::Thunk_statement(Statement_classification classification,
1651 Call_expression* call,
1652 source_location location)
1653 : Statement(classification, location),
1654 call_(call), struct_type_(NULL)
1658 // Return whether this is a simple statement which does not require a
1662 Thunk_statement::is_simple(Function_type* fntype) const
1664 // We need a thunk to call a method, or to pass a variable number of
1666 if (fntype->is_method() || fntype->is_varargs())
1669 // A defer statement requires a thunk to set up for whether the
1670 // function can call recover.
1671 if (this->classification() == STATEMENT_DEFER)
1674 // We can only permit a single parameter of pointer type.
1675 const Typed_identifier_list* parameters = fntype->parameters();
1676 if (parameters != NULL
1677 && (parameters->size() > 1
1678 || (parameters->size() == 1
1679 && parameters->begin()->type()->points_to() == NULL)))
1682 // If the function returns multiple values, or returns a type other
1683 // than integer, floating point, or pointer, then it may get a
1684 // hidden first parameter, in which case we need the more
1685 // complicated approach. This is true even though we are going to
1686 // ignore the return value.
1687 const Typed_identifier_list* results = fntype->results();
1689 && (results->size() > 1
1690 || (results->size() == 1
1691 && !results->begin()->type()->is_basic_type()
1692 && results->begin()->type()->points_to() == NULL)))
1695 // If this calls something which is not a simple function, then we
1697 Expression* fn = this->call_->call_expression()->fn();
1698 if (fn->bound_method_expression() != NULL
1699 || fn->interface_field_reference_expression() != NULL)
1705 // Traverse a thunk statement.
1708 Thunk_statement::do_traverse(Traverse* traverse)
1710 return this->traverse_expression(traverse, &this->call_);
1713 // We implement traverse_assignment for a thunk statement because it
1714 // effectively copies the function call.
1717 Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
1719 Expression* fn = this->call_->call_expression()->fn();
1720 Expression* fn2 = fn;
1721 tassign->value(&fn2, true, false);
1725 // Determine types in a thunk statement.
1728 Thunk_statement::do_determine_types()
1730 this->call_->determine_type_no_context();
1732 // Now that we know the types of the call, build the struct used to
1734 Function_type* fntype =
1735 this->call_->call_expression()->get_function_type();
1736 if (fntype != NULL && !this->is_simple(fntype))
1737 this->struct_type_ = this->build_struct(fntype);
1740 // Check types in a thunk statement.
1743 Thunk_statement::do_check_types(Gogo*)
1745 Call_expression* ce = this->call_->call_expression();
1746 Function_type* fntype = ce->get_function_type();
1747 if (fntype != NULL && fntype->is_method())
1749 Expression* fn = ce->fn();
1750 if (fn->bound_method_expression() == NULL
1751 && fn->interface_field_reference_expression() == NULL)
1752 this->report_error(_("no object for method call"));
1756 // The Traverse class used to find and simplify thunk statements.
1758 class Simplify_thunk_traverse : public Traverse
1761 Simplify_thunk_traverse(Gogo* gogo)
1762 : Traverse(traverse_blocks),
1774 Simplify_thunk_traverse::block(Block* b)
1776 // The parser ensures that thunk statements always appear at the end
1778 if (b->statements()->size() < 1)
1779 return TRAVERSE_CONTINUE;
1780 Thunk_statement* stat = b->statements()->back()->thunk_statement();
1782 return TRAVERSE_CONTINUE;
1783 if (stat->simplify_statement(this->gogo_, b))
1784 return TRAVERSE_SKIP_COMPONENTS;
1785 return TRAVERSE_CONTINUE;
1788 // Simplify all thunk statements.
1791 Gogo::simplify_thunk_statements()
1793 Simplify_thunk_traverse thunk_traverse(this);
1794 this->traverse(&thunk_traverse);
1797 // Simplify complex thunk statements into simple ones. A complicated
1798 // thunk statement is one which takes anything other than zero
1799 // parameters or a single pointer parameter. We rewrite it into code
1800 // which allocates a struct, stores the parameter values into the
1801 // struct, and does a simple go or defer statement which passes the
1802 // struct to a thunk. The thunk does the real call.
1805 Thunk_statement::simplify_statement(Gogo* gogo, Block* block)
1807 if (this->classification() == STATEMENT_ERROR)
1809 if (this->call_->is_error_expression())
1812 Call_expression* ce = this->call_->call_expression();
1813 Function_type* fntype = ce->get_function_type();
1814 if (fntype == NULL || this->is_simple(fntype))
1817 Expression* fn = ce->fn();
1818 Bound_method_expression* bound_method = fn->bound_method_expression();
1819 Interface_field_reference_expression* interface_method =
1820 fn->interface_field_reference_expression();
1821 const bool is_method = bound_method != NULL || interface_method != NULL;
1823 source_location location = this->location();
1825 std::string thunk_name = Gogo::thunk_name();
1828 this->build_thunk(gogo, thunk_name, fntype);
1830 // Generate code to call the thunk.
1832 // Get the values to store into the struct which is the single
1833 // argument to the thunk.
1835 Expression_list* vals = new Expression_list();
1836 if (fntype->is_builtin())
1838 else if (!is_method)
1839 vals->push_back(fn);
1840 else if (interface_method != NULL)
1841 vals->push_back(interface_method->expr());
1842 else if (bound_method != NULL)
1844 vals->push_back(bound_method->method());
1845 Expression* first_arg = bound_method->first_argument();
1847 // We always pass a pointer when calling a method.
1848 if (first_arg->type()->points_to() == NULL)
1849 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, location);
1851 // If we are calling a method which was inherited from an
1852 // embedded struct, and the method did not get a stub, then the
1853 // first type may be wrong.
1854 Type* fatype = bound_method->first_argument_type();
1857 if (fatype->points_to() == NULL)
1858 fatype = Type::make_pointer_type(fatype);
1859 Type* unsafe = Type::make_pointer_type(Type::make_void_type());
1860 first_arg = Expression::make_cast(unsafe, first_arg, location);
1861 first_arg = Expression::make_cast(fatype, first_arg, location);
1864 vals->push_back(first_arg);
1869 if (ce->args() != NULL)
1871 for (Expression_list::const_iterator p = ce->args()->begin();
1872 p != ce->args()->end();
1874 vals->push_back(*p);
1877 // Build the struct.
1878 Expression* constructor =
1879 Expression::make_struct_composite_literal(this->struct_type_, vals,
1882 // Allocate the initialized struct on the heap.
1883 constructor = Expression::make_heap_composite(constructor, location);
1885 // Look up the thunk.
1886 Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
1887 gcc_assert(named_thunk != NULL && named_thunk->is_function());
1890 Expression* func = Expression::make_func_reference(named_thunk, NULL,
1892 Expression_list* params = new Expression_list();
1893 params->push_back(constructor);
1894 Call_expression* call = Expression::make_call(func, params, false, location);
1896 // Build the simple go or defer statement.
1898 if (this->classification() == STATEMENT_GO)
1899 s = Statement::make_go_statement(call, location);
1900 else if (this->classification() == STATEMENT_DEFER)
1901 s = Statement::make_defer_statement(call, location);
1905 // The current block should end with the go statement.
1906 gcc_assert(block->statements()->size() >= 1);
1907 gcc_assert(block->statements()->back() == this);
1908 block->replace_statement(block->statements()->size() - 1, s);
1910 // We already ran the determine_types pass, so we need to run it now
1911 // for the new statement.
1912 s->determine_types();
1915 gogo->check_types_in_block(block);
1917 // Return true to tell the block not to keep looking at statements.
1921 // Set the name to use for thunk parameter N.
1924 Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
1926 snprintf(buf, buflen, "a%d", n);
1929 // Build a new struct type to hold the parameters for a complicated
1930 // thunk statement. FNTYPE is the type of the function call.
1933 Thunk_statement::build_struct(Function_type* fntype)
1935 source_location location = this->location();
1937 Struct_field_list* fields = new Struct_field_list();
1939 Call_expression* ce = this->call_->call_expression();
1940 Expression* fn = ce->fn();
1942 Interface_field_reference_expression* interface_method =
1943 fn->interface_field_reference_expression();
1944 if (interface_method != NULL)
1946 // If this thunk statement calls a method on an interface, we
1947 // pass the interface object to the thunk.
1948 Typed_identifier tid(Thunk_statement::thunk_field_fn,
1949 interface_method->expr()->type(),
1951 fields->push_back(Struct_field(tid));
1953 else if (!fntype->is_builtin())
1955 // The function to call.
1956 Typed_identifier tid(Go_statement::thunk_field_fn, fntype, location);
1957 fields->push_back(Struct_field(tid));
1959 else if (ce->is_recover_call())
1961 // The predeclared recover function has no argument. However,
1962 // we add an argument when building recover thunks. Handle that
1964 fields->push_back(Struct_field(Typed_identifier("can_recover",
1965 Type::make_boolean_type(),
1969 if (fn->bound_method_expression() != NULL)
1971 gcc_assert(fntype->is_method());
1972 Type* rtype = fntype->receiver()->type();
1973 // We always pass the receiver as a pointer.
1974 if (rtype->points_to() == NULL)
1975 rtype = Type::make_pointer_type(rtype);
1976 Typed_identifier tid(Thunk_statement::thunk_field_receiver, rtype,
1978 fields->push_back(Struct_field(tid));
1981 const Expression_list* args = ce->args();
1985 for (Expression_list::const_iterator p = args->begin();
1990 this->thunk_field_param(i, buf, sizeof buf);
1991 fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
1996 return Type::make_struct_type(fields, location);
1999 // Build the thunk we are going to call. This is a brand new, albeit
2000 // artificial, function.
2003 Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
2004 Function_type* fntype)
2006 source_location location = this->location();
2008 Call_expression* ce = this->call_->call_expression();
2010 bool may_call_recover = false;
2011 if (this->classification() == STATEMENT_DEFER)
2013 Func_expression* fn = ce->fn()->func_expression();
2015 may_call_recover = true;
2018 const Named_object* no = fn->named_object();
2019 if (!no->is_function())
2020 may_call_recover = true;
2022 may_call_recover = no->func_value()->calls_recover();
2026 // Build the type of the thunk. The thunk takes a single parameter,
2027 // which is a pointer to the special structure we build.
2028 const char* const parameter_name = "__go_thunk_parameter";
2029 Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
2030 Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
2031 thunk_parameters->push_back(Typed_identifier(parameter_name,
2032 pointer_to_struct_type,
2035 Typed_identifier_list* thunk_results = NULL;
2036 if (may_call_recover)
2038 // When deferring a function which may call recover, add a
2039 // return value, to disable tail call optimizations which will
2040 // break the way we check whether recover is permitted.
2041 thunk_results = new Typed_identifier_list();
2042 thunk_results->push_back(Typed_identifier("", Type::make_boolean_type(),
2046 Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
2050 // Start building the thunk.
2051 Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
2054 // For a defer statement, start with a call to
2055 // __go_set_defer_retaddr. */
2056 Label* retaddr_label = NULL;
2057 if (may_call_recover)
2059 retaddr_label = gogo->add_label_reference("retaddr");
2060 Expression* arg = Expression::make_label_addr(retaddr_label, location);
2061 Expression_list* args = new Expression_list();
2062 args->push_back(arg);
2064 static Named_object* set_defer_retaddr;
2065 if (set_defer_retaddr == NULL)
2067 const source_location bloc = BUILTINS_LOCATION;
2068 Typed_identifier_list* param_types = new Typed_identifier_list();
2069 Type *voidptr_type = Type::make_pointer_type(Type::make_void_type());
2070 param_types->push_back(Typed_identifier("r", voidptr_type, bloc));
2072 Typed_identifier_list* result_types = new Typed_identifier_list();
2073 result_types->push_back(Typed_identifier("",
2074 Type::make_boolean_type(),
2077 Function_type* t = Type::make_function_type(NULL, param_types,
2078 result_types, bloc);
2080 Named_object::make_function_declaration("__go_set_defer_retaddr",
2082 const char* n = "__go_set_defer_retaddr";
2083 set_defer_retaddr->func_declaration_value()->set_asm_name(n);
2086 Expression* fn = Expression::make_func_reference(set_defer_retaddr,
2088 Expression* call = Expression::make_call(fn, args, false, location);
2090 // This is a hack to prevent the middle-end from deleting the
2092 gogo->start_block(location);
2093 gogo->add_statement(Statement::make_goto_statement(retaddr_label,
2095 Block* then_block = gogo->finish_block(location);
2096 then_block->determine_types();
2098 Statement* s = Statement::make_if_statement(call, then_block, NULL,
2100 s->determine_types();
2101 gogo->add_statement(s);
2104 // Get a reference to the parameter.
2105 Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
2106 gcc_assert(named_parameter != NULL && named_parameter->is_variable());
2108 // Build the call. Note that the field names are the same as the
2109 // ones used in build_struct.
2110 Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
2112 thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
2115 Bound_method_expression* bound_method = ce->fn()->bound_method_expression();
2116 Interface_field_reference_expression* interface_method =
2117 ce->fn()->interface_field_reference_expression();
2119 Expression* func_to_call;
2120 unsigned int next_index;
2121 if (!fntype->is_builtin())
2123 func_to_call = Expression::make_field_reference(thunk_parameter,
2129 gcc_assert(bound_method == NULL && interface_method == NULL);
2130 func_to_call = ce->fn();
2134 if (bound_method != NULL)
2136 Expression* r = Expression::make_field_reference(thunk_parameter, 1,
2138 // The main program passes in a function pointer from the
2139 // interface expression, so here we can make a bound method in
2141 func_to_call = Expression::make_bound_method(r, func_to_call,
2145 else if (interface_method != NULL)
2147 // The main program passes the interface object.
2148 const std::string& name(interface_method->name());
2149 func_to_call = Expression::make_interface_field_reference(func_to_call,
2154 Expression_list* call_params = new Expression_list();
2155 const Struct_field_list* fields = this->struct_type_->fields();
2156 Struct_field_list::const_iterator p = fields->begin();
2157 for (unsigned int i = 0; i < next_index; ++i)
2159 for (; p != fields->end(); ++p, ++next_index)
2161 Expression* thunk_param = Expression::make_var_reference(named_parameter,
2163 thunk_param = Expression::make_unary(OPERATOR_MULT, thunk_param,
2165 Expression* param = Expression::make_field_reference(thunk_param,
2168 call_params->push_back(param);
2171 Expression* call = Expression::make_call(func_to_call, call_params, false,
2173 // We need to lower in case this is a builtin function.
2174 call = call->lower(gogo, function, -1);
2175 if (may_call_recover)
2177 Call_expression* ce = call->call_expression();
2179 ce->set_is_deferred();
2182 Statement* call_statement = Statement::make_statement(call);
2184 // We already ran the determine_types pass, so we need to run it
2185 // just for this statement now.
2186 call_statement->determine_types();
2188 gogo->add_statement(call_statement);
2190 // If this is a defer statement, the label comes immediately after
2192 if (may_call_recover)
2194 gogo->add_label_definition("retaddr", location);
2196 Expression_list* vals = new Expression_list();
2197 vals->push_back(Expression::make_boolean(false, location));
2198 const Typed_identifier_list* results =
2199 function->func_value()->type()->results();
2200 gogo->add_statement(Statement::make_return_statement(results, vals,
2204 // That is all the thunk has to do.
2205 gogo->finish_function(location);
2208 // Get the function and argument trees.
2211 Thunk_statement::get_fn_and_arg(Translate_context* context, tree* pfn,
2214 if (this->call_->is_error_expression())
2216 *pfn = error_mark_node;
2217 *parg = error_mark_node;
2221 Call_expression* ce = this->call_->call_expression();
2223 Expression* fn = ce->fn();
2224 *pfn = fn->get_tree(context);
2226 const Expression_list* args = ce->args();
2227 if (args == NULL || args->empty())
2228 *parg = null_pointer_node;
2231 gcc_assert(args->size() == 1);
2232 *parg = args->front()->get_tree(context);
2236 // Class Go_statement.
2239 Go_statement::do_get_tree(Translate_context* context)
2243 this->get_fn_and_arg(context, &fn_tree, &arg_tree);
2245 static tree go_fndecl;
2247 tree fn_arg_type = NULL_TREE;
2248 if (go_fndecl == NULL_TREE)
2250 // Only build FN_ARG_TYPE if we need it.
2251 tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
2252 tree subfntype = build_function_type(ptr_type_node, subargtypes);
2253 fn_arg_type = build_pointer_type(subfntype);
2256 return Gogo::call_builtin(&go_fndecl,
2267 // Make a go statement.
2270 Statement::make_go_statement(Call_expression* call, source_location location)
2272 return new Go_statement(call, location);
2275 // Class Defer_statement.
2278 Defer_statement::do_get_tree(Translate_context* context)
2280 source_location loc = this->location();
2284 this->get_fn_and_arg(context, &fn_tree, &arg_tree);
2285 if (fn_tree == error_mark_node || arg_tree == error_mark_node)
2286 return error_mark_node;
2288 static tree defer_fndecl;
2290 tree fn_arg_type = NULL_TREE;
2291 if (defer_fndecl == NULL_TREE)
2293 // Only build FN_ARG_TYPE if we need it.
2294 tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
2295 tree subfntype = build_function_type(ptr_type_node, subargtypes);
2296 fn_arg_type = build_pointer_type(subfntype);
2299 tree defer_stack = context->function()->func_value()->defer_stack(loc);
2301 return Gogo::call_builtin(&defer_fndecl,
2314 // Make a defer statement.
2317 Statement::make_defer_statement(Call_expression* call,
2318 source_location location)
2320 return new Defer_statement(call, location);
2323 // Class Return_statement.
2325 // Traverse assignments. We treat each return value as a top level
2326 // RHS in an expression.
2329 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2331 Expression_list* vals = this->vals_;
2334 for (Expression_list::iterator p = vals->begin();
2337 tassign->value(&*p, true, true);
2342 // Lower a return statement. If we are returning a function call
2343 // which returns multiple values which match the current function,
2344 // split up the call's results. If the function has named result
2345 // variables, and the return statement lists explicit values, then
2346 // implement it by assigning the values to the result variables and
2347 // changing the statement to not list any values. This lets
2348 // panic/recover work correctly.
2351 Return_statement::do_lower(Gogo*, Block* enclosing)
2353 if (this->vals_ == NULL)
2356 const Typed_identifier_list* results = this->results_;
2357 if (results == NULL || results->empty())
2360 // If the current function has multiple return values, and we are
2361 // returning a single call expression, split up the call expression.
2362 size_t results_count = results->size();
2363 if (results_count > 1
2364 && this->vals_->size() == 1
2365 && this->vals_->front()->call_expression() != NULL)
2367 Call_expression* call = this->vals_->front()->call_expression();
2368 size_t count = results->size();
2369 Expression_list* vals = new Expression_list;
2370 for (size_t i = 0; i < count; ++i)
2371 vals->push_back(Expression::make_call_result(call, i));
2376 if (results->front().name().empty())
2379 if (results_count != this->vals_->size())
2381 // Presumably an error which will be reported in check_types.
2385 // Assign to named return values and then return them.
2387 source_location loc = this->location();
2388 const Block* top = enclosing;
2389 while (top->enclosing() != NULL)
2390 top = top->enclosing();
2392 const Bindings *bindings = top->bindings();
2393 Block* b = new Block(enclosing, loc);
2395 Expression_list* lhs = new Expression_list();
2396 Expression_list* rhs = new Expression_list();
2398 Expression_list::const_iterator pe = this->vals_->begin();
2400 for (Typed_identifier_list::const_iterator pr = results->begin();
2401 pr != results->end();
2404 Named_object* rv = bindings->lookup_local(pr->name());
2405 if (rv == NULL || !rv->is_result_variable())
2407 // Presumably an error.
2414 Expression* e = *pe;
2416 // Check types now so that we give a good error message. The
2417 // result type is known. We determine the expression type
2420 Type *rvtype = rv->result_var_value()->type();
2421 Type_context type_context(rvtype, false);
2422 e->determine_type(&type_context);
2425 if (Type::are_assignable(rvtype, e->type(), &reason))
2427 Expression* ve = Expression::make_var_reference(rv, e->location());
2434 error_at(e->location(), "incompatible type for return value %d", i);
2436 error_at(e->location(),
2437 "incompatible type for return value %d (%s)",
2441 gcc_assert(lhs->size() == rhs->size());
2445 else if (lhs->size() == 1)
2447 b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
2453 b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
2455 b->add_statement(Statement::make_return_statement(this->results_, NULL,
2458 return Statement::make_block_statement(b, loc);
2464 Return_statement::do_determine_types()
2466 if (this->vals_ == NULL)
2468 const Typed_identifier_list* results = this->results_;
2470 Typed_identifier_list::const_iterator pt;
2471 if (results != NULL)
2472 pt = results->begin();
2473 for (Expression_list::iterator pe = this->vals_->begin();
2474 pe != this->vals_->end();
2477 if (results == NULL || pt == results->end())
2478 (*pe)->determine_type_no_context();
2481 Type_context context(pt->type(), false);
2482 (*pe)->determine_type(&context);
2491 Return_statement::do_check_types(Gogo*)
2493 if (this->vals_ == NULL)
2496 const Typed_identifier_list* results = this->results_;
2497 if (results == NULL)
2499 this->report_error(_("return with value in function "
2500 "with no return type"));
2505 Typed_identifier_list::const_iterator pt = results->begin();
2506 for (Expression_list::const_iterator pe = this->vals_->begin();
2507 pe != this->vals_->end();
2510 if (pt == results->end())
2512 this->report_error(_("too many values in return statement"));
2516 if (!Type::are_assignable(pt->type(), (*pe)->type(), &reason))
2519 error_at(this->location(),
2520 "incompatible type for return value %d",
2523 error_at(this->location(),
2524 "incompatible type for return value %d (%s)",
2526 this->set_is_error();
2528 else if (pt->type()->is_error_type()
2529 || (*pe)->type()->is_error_type()
2530 || pt->type()->is_undefined()
2531 || (*pe)->type()->is_undefined())
2533 // Make sure we get the error for an undefined type.
2535 (*pe)->type()->base();
2536 this->set_is_error();
2540 if (pt != results->end())
2541 this->report_error(_("not enough values in return statement"));
2544 // Build a RETURN_EXPR tree.
2547 Return_statement::do_get_tree(Translate_context* context)
2549 Function* function = context->function()->func_value();
2550 tree fndecl = function->get_decl();
2552 const Typed_identifier_list* results = this->results_;
2554 if (this->vals_ == NULL)
2556 tree stmt_list = NULL_TREE;
2557 tree retval = function->return_value(context->gogo(),
2558 context->function(),
2562 if (retval == NULL_TREE)
2565 set = fold_build2_loc(this->location(), MODIFY_EXPR, void_type_node,
2566 DECL_RESULT(fndecl), retval);
2567 append_to_statement_list(this->build_stmt_1(RETURN_EXPR, set),
2571 else if (this->vals_->size() == 1)
2573 gcc_assert(!VOID_TYPE_P(TREE_TYPE(TREE_TYPE(fndecl))));
2574 tree val = (*this->vals_->begin())->get_tree(context);
2575 if (val == error_mark_node)
2576 return error_mark_node;
2577 gcc_assert(results != NULL && results->size() == 1);
2578 val = Expression::convert_for_assignment(context,
2579 results->begin()->type(),
2580 (*this->vals_->begin())->type(),
2581 val, this->location());
2582 tree set = build2(MODIFY_EXPR, void_type_node,
2583 DECL_RESULT(fndecl), val);
2584 SET_EXPR_LOCATION(set, this->location());
2585 return this->build_stmt_1(RETURN_EXPR, set);
2589 gcc_assert(!VOID_TYPE_P(TREE_TYPE(TREE_TYPE(fndecl))));
2590 tree stmt_list = NULL_TREE;
2591 tree rettype = TREE_TYPE(DECL_RESULT(fndecl));
2592 tree retvar = create_tmp_var(rettype, "RESULT");
2593 gcc_assert(results != NULL && results->size() == this->vals_->size());
2594 Expression_list::const_iterator pv = this->vals_->begin();
2595 Typed_identifier_list::const_iterator pr = results->begin();
2596 for (tree field = TYPE_FIELDS(rettype);
2598 ++pv, ++pr, field = DECL_CHAIN(field))
2600 gcc_assert(pv != this->vals_->end());
2601 tree val = (*pv)->get_tree(context);
2602 if (val == error_mark_node)
2603 return error_mark_node;
2604 val = Expression::convert_for_assignment(context, pr->type(),
2607 tree set = build2(MODIFY_EXPR, void_type_node,
2608 build3(COMPONENT_REF, TREE_TYPE(field),
2609 retvar, field, NULL_TREE),
2611 SET_EXPR_LOCATION(set, this->location());
2612 append_to_statement_list(set, &stmt_list);
2614 tree set = build2(MODIFY_EXPR, void_type_node, DECL_RESULT(fndecl),
2616 append_to_statement_list(this->build_stmt_1(RETURN_EXPR, set),
2622 // Make a return statement.
2625 Statement::make_return_statement(const Typed_identifier_list* results,
2626 Expression_list* vals,
2627 source_location location)
2629 return new Return_statement(results, vals, location);
2632 // A break or continue statement.
2634 class Bc_statement : public Statement
2637 Bc_statement(bool is_break, Unnamed_label* label, source_location location)
2638 : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
2639 label_(label), is_break_(is_break)
2644 { return this->is_break_; }
2648 do_traverse(Traverse*)
2649 { return TRAVERSE_CONTINUE; }
2652 do_may_fall_through() const
2656 do_get_tree(Translate_context*)
2657 { return this->label_->get_goto(this->location()); }
2660 // The label that this branches to.
2661 Unnamed_label* label_;
2662 // True if this is "break", false if it is "continue".
2666 // Make a break statement.
2669 Statement::make_break_statement(Unnamed_label* label, source_location location)
2671 return new Bc_statement(true, label, location);
2674 // Make a continue statement.
2677 Statement::make_continue_statement(Unnamed_label* label,
2678 source_location location)
2680 return new Bc_statement(false, label, location);
2683 // A goto statement.
2685 class Goto_statement : public Statement
2688 Goto_statement(Label* label, source_location location)
2689 : Statement(STATEMENT_GOTO, location),
2695 do_traverse(Traverse*)
2696 { return TRAVERSE_CONTINUE; }
2699 do_check_types(Gogo*);
2702 do_may_fall_through() const
2706 do_get_tree(Translate_context*);
2712 // Check types for a label. There aren't any types per se, but we use
2713 // this to give an error if the label was never defined.
2716 Goto_statement::do_check_types(Gogo*)
2718 if (!this->label_->is_defined())
2720 error_at(this->location(), "reference to undefined label %qs",
2721 Gogo::message_name(this->label_->name()).c_str());
2722 this->set_is_error();
2726 // Return the tree for the goto statement.
2729 Goto_statement::do_get_tree(Translate_context*)
2731 return this->build_stmt_1(GOTO_EXPR, this->label_->get_decl());
2734 // Make a goto statement.
2737 Statement::make_goto_statement(Label* label, source_location location)
2739 return new Goto_statement(label, location);
2742 // A goto statement to an unnamed label.
2744 class Goto_unnamed_statement : public Statement
2747 Goto_unnamed_statement(Unnamed_label* label, source_location location)
2748 : Statement(STATEMENT_GOTO_UNNAMED, location),
2754 do_traverse(Traverse*)
2755 { return TRAVERSE_CONTINUE; }
2758 do_may_fall_through() const
2762 do_get_tree(Translate_context*)
2763 { return this->label_->get_goto(this->location()); }
2766 Unnamed_label* label_;
2769 // Make a goto statement to an unnamed label.
2772 Statement::make_goto_unnamed_statement(Unnamed_label* label,
2773 source_location location)
2775 return new Goto_unnamed_statement(label, location);
2778 // Class Label_statement.
2783 Label_statement::do_traverse(Traverse*)
2785 return TRAVERSE_CONTINUE;
2788 // Return a tree defining this label.
2791 Label_statement::do_get_tree(Translate_context*)
2793 return this->build_stmt_1(LABEL_EXPR, this->label_->get_decl());
2796 // Make a label statement.
2799 Statement::make_label_statement(Label* label, source_location location)
2801 return new Label_statement(label, location);
2804 // An unnamed label statement.
2806 class Unnamed_label_statement : public Statement
2809 Unnamed_label_statement(Unnamed_label* label)
2810 : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
2816 do_traverse(Traverse*)
2817 { return TRAVERSE_CONTINUE; }
2820 do_get_tree(Translate_context*)
2821 { return this->label_->get_definition(); }
2825 Unnamed_label* label_;
2828 // Make an unnamed label statement.
2831 Statement::make_unnamed_label_statement(Unnamed_label* label)
2833 return new Unnamed_label_statement(label);
2838 class If_statement : public Statement
2841 If_statement(Expression* cond, Block* then_block, Block* else_block,
2842 source_location location)
2843 : Statement(STATEMENT_IF, location),
2844 cond_(cond), then_block_(then_block), else_block_(else_block)
2849 do_traverse(Traverse*);
2852 do_determine_types();
2855 do_check_types(Gogo*);
2858 do_may_fall_through() const;
2861 do_get_tree(Translate_context*);
2872 If_statement::do_traverse(Traverse* traverse)
2874 if (this->cond_ != NULL)
2876 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
2877 return TRAVERSE_EXIT;
2879 if (this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
2880 return TRAVERSE_EXIT;
2881 if (this->else_block_ != NULL)
2883 if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
2884 return TRAVERSE_EXIT;
2886 return TRAVERSE_CONTINUE;
2890 If_statement::do_determine_types()
2892 if (this->cond_ != NULL)
2894 Type_context context(Type::lookup_bool_type(), false);
2895 this->cond_->determine_type(&context);
2897 this->then_block_->determine_types();
2898 if (this->else_block_ != NULL)
2899 this->else_block_->determine_types();
2905 If_statement::do_check_types(Gogo*)
2907 if (this->cond_ != NULL)
2909 Type* type = this->cond_->type();
2910 if (type->is_error_type())
2911 this->set_is_error();
2912 else if (!type->is_boolean_type())
2913 this->report_error(_("expected boolean expression"));
2917 // Whether the overall statement may fall through.
2920 If_statement::do_may_fall_through() const
2922 return (this->else_block_ == NULL
2923 || this->then_block_->may_fall_through()
2924 || this->else_block_->may_fall_through());
2930 If_statement::do_get_tree(Translate_context* context)
2932 gcc_assert(this->cond_ == NULL || this->cond_->type()->is_boolean_type());
2933 tree ret = build3(COND_EXPR, void_type_node,
2934 (this->cond_ == NULL
2936 : this->cond_->get_tree(context)),
2937 this->then_block_->get_tree(context),
2938 (this->else_block_ == NULL
2940 : this->else_block_->get_tree(context)));
2941 SET_EXPR_LOCATION(ret, this->location());
2945 // Make an if statement.
2948 Statement::make_if_statement(Expression* cond, Block* then_block,
2949 Block* else_block, source_location location)
2951 return new If_statement(cond, then_block, else_block, location);
2954 // Class Case_clauses::Case_clause.
2959 Case_clauses::Case_clause::traverse(Traverse* traverse)
2961 if (this->cases_ != NULL
2962 && (traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
2964 if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
2965 return TRAVERSE_EXIT;
2967 if (this->statements_ != NULL)
2969 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
2970 return TRAVERSE_EXIT;
2972 return TRAVERSE_CONTINUE;
2975 // Check whether all the case expressions are integer constants.
2978 Case_clauses::Case_clause::is_constant() const
2980 if (this->cases_ != NULL)
2982 for (Expression_list::const_iterator p = this->cases_->begin();
2983 p != this->cases_->end();
2985 if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
2991 // Lower a case clause for a nonconstant switch. VAL_TEMP is the
2992 // value we are switching on; it may be NULL. If START_LABEL is not
2993 // NULL, it goes at the start of the statements, after the condition
2994 // test. We branch to FINISH_LABEL at the end of the statements.
2997 Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
2998 Unnamed_label* start_label,
2999 Unnamed_label* finish_label) const
3001 source_location loc = this->location_;
3002 Unnamed_label* next_case_label;
3003 if (this->cases_ == NULL || this->cases_->empty())
3005 gcc_assert(this->is_default_);
3006 next_case_label = NULL;
3010 Expression* cond = NULL;
3012 for (Expression_list::const_iterator p = this->cases_->begin();
3013 p != this->cases_->end();
3016 Expression* this_cond;
3017 if (val_temp == NULL)
3021 Expression* ref = Expression::make_temporary_reference(val_temp,
3023 this_cond = Expression::make_binary(OPERATOR_EQEQ, ref, *p, loc);
3029 cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
3032 Block* then_block = new Block(b, loc);
3033 next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
3034 Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
3036 then_block->add_statement(s);
3038 // if !COND { goto NEXT_CASE_LABEL }
3039 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3040 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3041 b->add_statement(s);
3044 if (start_label != NULL)
3045 b->add_statement(Statement::make_unnamed_label_statement(start_label));
3047 if (this->statements_ != NULL)
3048 b->add_statement(Statement::make_block_statement(this->statements_, loc));
3050 Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
3051 b->add_statement(s);
3053 if (next_case_label != NULL)
3054 b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
3060 Case_clauses::Case_clause::determine_types(Type* type)
3062 if (this->cases_ != NULL)
3064 Type_context case_context(type, false);
3065 for (Expression_list::iterator p = this->cases_->begin();
3066 p != this->cases_->end();
3068 (*p)->determine_type(&case_context);
3070 if (this->statements_ != NULL)
3071 this->statements_->determine_types();
3074 // Check types. Returns false if there was an error.
3077 Case_clauses::Case_clause::check_types(Type* type)
3079 if (this->cases_ != NULL)
3081 for (Expression_list::iterator p = this->cases_->begin();
3082 p != this->cases_->end();
3085 if (!Type::are_assignable(type, (*p)->type(), NULL)
3086 && !Type::are_assignable((*p)->type(), type, NULL))
3088 error_at((*p)->location(),
3089 "type mismatch between switch value and case clause");
3097 // Return true if this clause may fall through to the following
3098 // statements. Note that this is not the same as whether the case
3099 // uses the "fallthrough" keyword.
3102 Case_clauses::Case_clause::may_fall_through() const
3104 if (this->statements_ == NULL)
3106 return this->statements_->may_fall_through();
3109 // Build up the body of a SWITCH_EXPR.
3112 Case_clauses::Case_clause::get_constant_tree(Translate_context* context,
3113 Unnamed_label* break_label,
3114 Case_constants* case_constants,
3115 tree* stmt_list) const
3117 if (this->cases_ != NULL)
3119 for (Expression_list::const_iterator p = this->cases_->begin();
3120 p != this->cases_->end();
3126 if (!(*p)->integer_constant_value(true, ival, &itype))
3128 gcc_assert(itype != NULL);
3129 tree type_tree = itype->get_tree(context->gogo());
3130 tree val = Expression::integer_constant_tree(ival, type_tree);
3133 if (val != error_mark_node)
3135 gcc_assert(TREE_CODE(val) == INTEGER_CST);
3137 std::pair<Case_constants::iterator, bool> ins =
3138 case_constants->insert(val);
3141 // Value was already present.
3142 warning_at(this->location_, 0,
3143 "duplicate case value will never match");
3147 tree label = create_artificial_label(this->location_);
3148 append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
3149 val, NULL_TREE, label),
3155 if (this->is_default_)
3157 tree label = create_artificial_label(this->location_);
3158 append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
3159 NULL_TREE, NULL_TREE, label),
3163 if (this->statements_ != NULL)
3165 tree block_tree = this->statements_->get_tree(context);
3166 if (block_tree != error_mark_node)
3167 append_to_statement_list(block_tree, stmt_list);
3170 if (!this->is_fallthrough_)
3171 append_to_statement_list(break_label->get_goto(this->location_), stmt_list);
3174 // Class Case_clauses.
3179 Case_clauses::traverse(Traverse* traverse)
3181 for (Clauses::iterator p = this->clauses_.begin();
3182 p != this->clauses_.end();
3185 if (p->traverse(traverse) == TRAVERSE_EXIT)
3186 return TRAVERSE_EXIT;
3188 return TRAVERSE_CONTINUE;
3191 // Check whether all the case expressions are constant.
3194 Case_clauses::is_constant() const
3196 for (Clauses::const_iterator p = this->clauses_.begin();
3197 p != this->clauses_.end();
3199 if (!p->is_constant())
3204 // Lower case clauses for a nonconstant switch.
3207 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3208 Unnamed_label* break_label) const
3210 // The default case.
3211 const Case_clause* default_case = NULL;
3213 // The label for the fallthrough of the previous case.
3214 Unnamed_label* last_fallthrough_label = NULL;
3216 // The label for the start of the default case. This is used if the
3217 // case before the default case falls through.
3218 Unnamed_label* default_start_label = NULL;
3220 // The label for the end of the default case. This normally winds
3221 // up as BREAK_LABEL, but it will be different if the default case
3223 Unnamed_label* default_finish_label = NULL;
3225 for (Clauses::const_iterator p = this->clauses_.begin();
3226 p != this->clauses_.end();
3229 // The label to use for the start of the statements for this
3230 // case. This is NULL unless the previous case falls through.
3231 Unnamed_label* start_label = last_fallthrough_label;
3233 // The label to jump to after the end of the statements for this
3235 Unnamed_label* finish_label = break_label;
3237 last_fallthrough_label = NULL;
3238 if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3240 finish_label = new Unnamed_label(p->location());
3241 last_fallthrough_label = finish_label;
3244 if (!p->is_default())
3245 p->lower(b, val_temp, start_label, finish_label);
3248 // We have to move the default case to the end, so that we
3249 // only use it if all the other tests fail.
3251 default_start_label = start_label;
3252 default_finish_label = finish_label;
3256 if (default_case != NULL)
3257 default_case->lower(b, val_temp, default_start_label,
3258 default_finish_label);
3265 Case_clauses::determine_types(Type* type)
3267 for (Clauses::iterator p = this->clauses_.begin();
3268 p != this->clauses_.end();
3270 p->determine_types(type);
3273 // Check types. Returns false if there was an error.
3276 Case_clauses::check_types(Type* type)
3279 for (Clauses::iterator p = this->clauses_.begin();
3280 p != this->clauses_.end();
3283 if (!p->check_types(type))
3289 // Return true if these clauses may fall through to the statements
3290 // following the switch statement.
3293 Case_clauses::may_fall_through() const
3295 bool found_default = false;
3296 for (Clauses::const_iterator p = this->clauses_.begin();
3297 p != this->clauses_.end();
3300 if (p->may_fall_through() && !p->is_fallthrough())
3302 if (p->is_default())
3303 found_default = true;
3305 return !found_default;
3308 // Return a tree when all case expressions are constants.
3311 Case_clauses::get_constant_tree(Translate_context* context,
3312 Unnamed_label* break_label) const
3314 Case_constants case_constants;
3315 tree stmt_list = NULL_TREE;
3316 for (Clauses::const_iterator p = this->clauses_.begin();
3317 p != this->clauses_.end();
3319 p->get_constant_tree(context, break_label, &case_constants,
3324 // A constant switch statement. A Switch_statement is lowered to this
3325 // when all the cases are constants.
3327 class Constant_switch_statement : public Statement
3330 Constant_switch_statement(Expression* val, Case_clauses* clauses,
3331 Unnamed_label* break_label,
3332 source_location location)
3333 : Statement(STATEMENT_CONSTANT_SWITCH, location),
3334 val_(val), clauses_(clauses), break_label_(break_label)
3339 do_traverse(Traverse*);
3342 do_determine_types();
3345 do_check_types(Gogo*);
3348 do_may_fall_through() const;
3351 do_get_tree(Translate_context*);
3354 // The value to switch on.
3356 // The case clauses.
3357 Case_clauses* clauses_;
3358 // The break label, if needed.
3359 Unnamed_label* break_label_;
3365 Constant_switch_statement::do_traverse(Traverse* traverse)
3367 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3368 return TRAVERSE_EXIT;
3369 return this->clauses_->traverse(traverse);
3375 Constant_switch_statement::do_determine_types()
3377 this->val_->determine_type_no_context();
3378 this->clauses_->determine_types(this->val_->type());
3384 Constant_switch_statement::do_check_types(Gogo*)
3386 if (!this->clauses_->check_types(this->val_->type()))
3387 this->set_is_error();
3390 // Return whether this switch may fall through.
3393 Constant_switch_statement::do_may_fall_through() const
3395 if (this->clauses_ == NULL)
3398 // If we have a break label, then some case needed it. That implies
3399 // that the switch statement as a whole can fall through.
3400 if (this->break_label_ != NULL)
3403 return this->clauses_->may_fall_through();
3406 // Convert to GENERIC.
3409 Constant_switch_statement::do_get_tree(Translate_context* context)
3411 tree switch_val_tree = this->val_->get_tree(context);
3413 Unnamed_label* break_label = this->break_label_;
3414 if (break_label == NULL)
3415 break_label = new Unnamed_label(this->location());
3417 tree stmt_list = NULL_TREE;
3418 tree s = build3(SWITCH_EXPR, void_type_node, switch_val_tree,
3419 this->clauses_->get_constant_tree(context, break_label),
3421 SET_EXPR_LOCATION(s, this->location());
3422 append_to_statement_list(s, &stmt_list);
3424 append_to_statement_list(break_label->get_definition(), &stmt_list);
3429 // Class Switch_statement.
3434 Switch_statement::do_traverse(Traverse* traverse)
3436 if (this->val_ != NULL)
3438 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3439 return TRAVERSE_EXIT;
3441 return this->clauses_->traverse(traverse);
3444 // Lower a Switch_statement to a Constant_switch_statement or a series
3445 // of if statements.
3448 Switch_statement::do_lower(Gogo*, Block* enclosing)
3450 source_location loc = this->location();
3452 if (this->val_ != NULL
3453 && (this->val_->is_error_expression()
3454 || this->val_->type()->is_error_type()))
3455 return Statement::make_error_statement(loc);
3457 if (this->val_ != NULL
3458 && this->val_->type()->integer_type() != NULL
3459 && !this->clauses_->empty()
3460 && this->clauses_->is_constant())
3461 return new Constant_switch_statement(this->val_, this->clauses_,
3462 this->break_label_, loc);
3464 Block* b = new Block(enclosing, loc);
3466 if (this->clauses_->empty())
3468 Expression* val = this->val_;
3470 val = Expression::make_boolean(true, loc);
3471 return Statement::make_statement(val);
3474 Temporary_statement* val_temp;
3475 if (this->val_ == NULL)
3479 // var val_temp VAL_TYPE = VAL
3480 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3481 b->add_statement(val_temp);
3484 this->clauses_->lower(b, val_temp, this->break_label());
3486 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3487 b->add_statement(s);
3489 return Statement::make_block_statement(b, loc);
3492 // Return the break label for this switch statement, creating it if
3496 Switch_statement::break_label()
3498 if (this->break_label_ == NULL)
3499 this->break_label_ = new Unnamed_label(this->location());
3500 return this->break_label_;
3503 // Make a switch statement.
3506 Statement::make_switch_statement(Expression* val, source_location location)
3508 return new Switch_statement(val, location);
3511 // Class Type_case_clauses::Type_case_clause.
3516 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3518 if (!this->is_default_
3519 && ((traverse->traverse_mask()
3520 & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3521 && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3522 return TRAVERSE_EXIT;
3523 if (this->statements_ != NULL)
3524 return this->statements_->traverse(traverse);
3525 return TRAVERSE_CONTINUE;
3528 // Lower one clause in a type switch. Add statements to the block B.
3529 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3530 // BREAK_LABEL is the label at the end of the type switch.
3531 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3535 Type_case_clauses::Type_case_clause::lower(Block* b,
3536 Temporary_statement* descriptor_temp,
3537 Unnamed_label* break_label,
3538 Unnamed_label** stmts_label) const
3540 source_location loc = this->location_;
3542 Unnamed_label* next_case_label = NULL;
3543 if (!this->is_default_)
3545 Type* type = this->type_;
3548 // The language permits case nil, which is of course a constant
3549 // rather than a type. It will appear here as an invalid
3551 if (type->is_nil_constant_as_type())
3554 Expression::make_temporary_reference(descriptor_temp, loc);
3555 cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3556 Expression::make_nil(loc),
3562 if (type->interface_type() == NULL)
3564 // func ifacetypeeq(*descriptor, *descriptor) bool
3565 static Named_object* ifacetypeeq;
3566 if (ifacetypeeq == NULL)
3568 const source_location bloc = BUILTINS_LOCATION;
3569 Typed_identifier_list* param_types =
3570 new Typed_identifier_list();
3571 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3572 param_types->push_back(Typed_identifier("a", descriptor_type,
3574 param_types->push_back(Typed_identifier("b", descriptor_type,
3576 Typed_identifier_list* ret_types =
3577 new Typed_identifier_list();
3578 Type* bool_type = Type::lookup_bool_type();
3579 ret_types->push_back(Typed_identifier("", bool_type, bloc));
3580 Function_type* fntype = Type::make_function_type(NULL,
3585 Named_object::make_function_declaration("ifacetypeeq", NULL,
3587 const char* n = "runtime.ifacetypeeq";
3588 ifacetypeeq->func_declaration_value()->set_asm_name(n);
3591 // ifacetypeeq(descriptor_temp, DESCRIPTOR)
3592 func = Expression::make_func_reference(ifacetypeeq, NULL, loc);
3596 // func ifaceI2Tp(*descriptor, *descriptor) bool
3597 static Named_object* ifaceI2Tp;
3598 if (ifaceI2Tp == NULL)
3600 const source_location bloc = BUILTINS_LOCATION;
3601 Typed_identifier_list* param_types =
3602 new Typed_identifier_list();
3603 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3604 param_types->push_back(Typed_identifier("a", descriptor_type,
3606 param_types->push_back(Typed_identifier("b", descriptor_type,
3608 Typed_identifier_list* ret_types =
3609 new Typed_identifier_list();
3610 Type* bool_type = Type::lookup_bool_type();
3611 ret_types->push_back(Typed_identifier("", bool_type, bloc));
3612 Function_type* fntype = Type::make_function_type(NULL,
3617 Named_object::make_function_declaration("ifaceI2Tp", NULL,
3619 const char* n = "runtime.ifaceI2Tp";
3620 ifaceI2Tp->func_declaration_value()->set_asm_name(n);
3623 // ifaceI2Tp(descriptor_temp, DESCRIPTOR)
3624 func = Expression::make_func_reference(ifaceI2Tp, NULL, loc);
3626 Expression_list* params = new Expression_list();
3627 params->push_back(Expression::make_type_descriptor(type, loc));
3629 Expression::make_temporary_reference(descriptor_temp, loc);
3630 params->push_back(ref);
3631 cond = Expression::make_call(func, params, false, loc);
3634 Unnamed_label* dest;
3635 if (!this->is_fallthrough_)
3637 // if !COND { goto NEXT_CASE_LABEL }
3638 next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
3639 dest = next_case_label;
3640 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3644 // if COND { goto STMTS_LABEL }
3645 gcc_assert(stmts_label != NULL);
3646 if (*stmts_label == NULL)
3647 *stmts_label = new Unnamed_label(UNKNOWN_LOCATION);
3648 dest = *stmts_label;
3650 Block* then_block = new Block(b, loc);
3651 Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
3652 then_block->add_statement(s);
3653 s = Statement::make_if_statement(cond, then_block, NULL, loc);
3654 b->add_statement(s);
3657 if (this->statements_ != NULL
3658 || (!this->is_fallthrough_
3659 && stmts_label != NULL
3660 && *stmts_label != NULL))
3662 gcc_assert(!this->is_fallthrough_);
3663 if (stmts_label != NULL && *stmts_label != NULL)
3665 gcc_assert(!this->is_default_);
3666 if (this->statements_ != NULL)
3667 (*stmts_label)->set_location(this->statements_->start_location());
3668 Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
3669 b->add_statement(s);
3670 *stmts_label = NULL;
3672 if (this->statements_ != NULL)
3673 b->add_statement(Statement::make_block_statement(this->statements_,
3677 if (this->is_fallthrough_)
3678 gcc_assert(next_case_label == NULL);
3681 source_location gloc = (this->statements_ == NULL
3683 : this->statements_->end_location());
3684 b->add_statement(Statement::make_goto_unnamed_statement(break_label,
3686 if (next_case_label != NULL)
3689 Statement::make_unnamed_label_statement(next_case_label);
3690 b->add_statement(s);
3695 // Class Type_case_clauses.
3700 Type_case_clauses::traverse(Traverse* traverse)
3702 for (Type_clauses::iterator p = this->clauses_.begin();
3703 p != this->clauses_.end();
3706 if (p->traverse(traverse) == TRAVERSE_EXIT)
3707 return TRAVERSE_EXIT;
3709 return TRAVERSE_CONTINUE;
3712 // Check for duplicate types.
3715 Type_case_clauses::check_duplicates() const
3717 typedef Unordered_set_hash(const Type*, Type_hash_identical,
3718 Type_identical) Types_seen;
3719 Types_seen types_seen;
3720 for (Type_clauses::const_iterator p = this->clauses_.begin();
3721 p != this->clauses_.end();
3724 Type* t = p->type();
3727 if (t->is_nil_constant_as_type())
3728 t = Type::make_nil_type();
3729 std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
3731 error_at(p->location(), "duplicate type in switch");
3735 // Lower the clauses in a type switch. Add statements to the block B.
3736 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3737 // BREAK_LABEL is the label at the end of the type switch.
3740 Type_case_clauses::lower(Block* b, Temporary_statement* descriptor_temp,
3741 Unnamed_label* break_label) const
3743 const Type_case_clause* default_case = NULL;
3745 Unnamed_label* stmts_label = NULL;
3746 for (Type_clauses::const_iterator p = this->clauses_.begin();
3747 p != this->clauses_.end();
3750 if (!p->is_default())
3751 p->lower(b, descriptor_temp, break_label, &stmts_label);
3754 // We are generating a series of tests, which means that we
3755 // need to move the default case to the end.
3759 gcc_assert(stmts_label == NULL);
3761 if (default_case != NULL)
3762 default_case->lower(b, descriptor_temp, break_label, NULL);
3765 // Class Type_switch_statement.
3770 Type_switch_statement::do_traverse(Traverse* traverse)
3772 if (this->var_ == NULL)
3774 if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
3775 return TRAVERSE_EXIT;
3777 if (this->clauses_ != NULL)
3778 return this->clauses_->traverse(traverse);
3779 return TRAVERSE_CONTINUE;
3782 // Lower a type switch statement to a series of if statements. The gc
3783 // compiler is able to generate a table in some cases. However, that
3784 // does not work for us because we may have type descriptors in
3785 // different shared libraries, so we can't compare them with simple
3786 // equality testing.
3789 Type_switch_statement::do_lower(Gogo*, Block* enclosing)
3791 const source_location loc = this->location();
3793 if (this->clauses_ != NULL)
3794 this->clauses_->check_duplicates();
3796 Block* b = new Block(enclosing, loc);
3798 Type* val_type = (this->var_ != NULL
3799 ? this->var_->var_value()->type()
3800 : this->expr_->type());
3802 // var descriptor_temp DESCRIPTOR_TYPE
3803 Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3804 Temporary_statement* descriptor_temp =
3805 Statement::make_temporary(descriptor_type, NULL, loc);
3806 b->add_statement(descriptor_temp);
3808 if (val_type->interface_type() == NULL)
3810 // Doing a type switch on a non-interface type. Should we issue
3811 // a warning for this case?
3812 // descriptor_temp = DESCRIPTOR
3813 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3815 Expression* rhs = Expression::make_type_descriptor(val_type, loc);
3816 Statement* s = Statement::make_assignment(lhs, rhs, loc);
3817 b->add_statement(s);
3821 const source_location bloc = BUILTINS_LOCATION;
3823 // func {efacetype,ifacetype}(*interface) *descriptor
3824 // FIXME: This should be inlined.
3825 Typed_identifier_list* param_types = new Typed_identifier_list();
3826 param_types->push_back(Typed_identifier("i", val_type, bloc));
3827 Typed_identifier_list* ret_types = new Typed_identifier_list();
3828 ret_types->push_back(Typed_identifier("", descriptor_type, bloc));
3829 Function_type* fntype = Type::make_function_type(NULL, param_types,
3831 bool is_empty = val_type->interface_type()->is_empty();
3832 const char* fnname = is_empty ? "efacetype" : "ifacetype";
3834 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
3835 const char* asm_name = (is_empty
3836 ? "runtime.efacetype"
3837 : "runtime.ifacetype");
3838 fn->func_declaration_value()->set_asm_name(asm_name);
3840 // descriptor_temp = ifacetype(val_temp)
3841 Expression* func = Expression::make_func_reference(fn, NULL, loc);
3842 Expression_list* params = new Expression_list();
3844 if (this->var_ == NULL)
3847 ref = Expression::make_var_reference(this->var_, loc);
3848 params->push_back(ref);
3849 Expression* call = Expression::make_call(func, params, false, loc);
3850 Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3852 Statement* s = Statement::make_assignment(lhs, call, loc);
3853 b->add_statement(s);
3856 if (this->clauses_ != NULL)
3857 this->clauses_->lower(b, descriptor_temp, this->break_label());
3859 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3860 b->add_statement(s);
3862 return Statement::make_block_statement(b, loc);
3865 // Return the break label for this type switch statement, creating it
3869 Type_switch_statement::break_label()
3871 if (this->break_label_ == NULL)
3872 this->break_label_ = new Unnamed_label(this->location());
3873 return this->break_label_;
3876 // Make a type switch statement.
3878 Type_switch_statement*
3879 Statement::make_type_switch_statement(Named_object* var, Expression* expr,
3880 source_location location)
3882 return new Type_switch_statement(var, expr, location);
3885 // Class Select_clauses::Select_clause.
3890 Select_clauses::Select_clause::traverse(Traverse* traverse)
3892 if (!this->is_lowered_
3893 && (traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
3895 if (this->channel_ != NULL)
3897 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
3898 return TRAVERSE_EXIT;
3900 if (this->val_ != NULL)
3902 if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
3903 return TRAVERSE_EXIT;
3906 if (this->statements_ != NULL)
3908 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3909 return TRAVERSE_EXIT;
3911 return TRAVERSE_CONTINUE;
3914 // Lowering. Here we pull out the channel and the send values, to
3915 // enforce the order of evaluation. We also add explicit send and
3916 // receive statements to the clauses.
3919 Select_clauses::Select_clause::lower(Block* b)
3921 if (this->is_default_)
3923 gcc_assert(this->channel_ == NULL && this->val_ == NULL);
3924 this->is_lowered_ = true;
3928 source_location loc = this->location_;
3930 // Evaluate the channel before the select statement.
3931 Temporary_statement* channel_temp = Statement::make_temporary(NULL,
3934 b->add_statement(channel_temp);
3935 this->channel_ = Expression::make_temporary_reference(channel_temp, loc);
3937 // If this is a send clause, evaluate the value to send before the
3938 // select statement.
3939 Temporary_statement* val_temp = NULL;
3942 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3943 b->add_statement(val_temp);
3946 // Add the send or receive before the rest of the statements if any.
3947 Block *init = new Block(b, loc);
3948 Expression* ref = Expression::make_temporary_reference(channel_temp, loc);
3951 Expression* ref2 = Expression::make_temporary_reference(val_temp, loc);
3952 Send_expression* send = Expression::make_send(ref, ref2, loc);
3953 send->discarding_value();
3954 send->set_for_select();
3955 init->add_statement(Statement::make_statement(send));
3959 Receive_expression* recv = Expression::make_receive(ref, loc);
3960 recv->set_for_select();
3961 if (this->val_ != NULL)
3963 gcc_assert(this->var_ == NULL);
3964 init->add_statement(Statement::make_assignment(this->val_, recv,
3967 else if (this->var_ != NULL)
3969 this->var_->var_value()->set_init(recv);
3970 this->var_->var_value()->clear_type_from_chan_element();
3974 recv->discarding_value();
3975 init->add_statement(Statement::make_statement(recv));
3979 if (this->statements_ != NULL)
3980 init->add_statement(Statement::make_block_statement(this->statements_,
3983 this->statements_ = init;
3985 // Now all references should be handled through the statements, not
3987 this->is_lowered_ = true;
3995 Select_clauses::Select_clause::determine_types()
3997 gcc_assert(this->is_lowered_);
3998 if (this->statements_ != NULL)
3999 this->statements_->determine_types();
4002 // Whether this clause may fall through to the statement which follows
4003 // the overall select statement.
4006 Select_clauses::Select_clause::may_fall_through() const
4008 if (this->statements_ == NULL)
4010 return this->statements_->may_fall_through();
4013 // Return a tree for the statements to execute.
4016 Select_clauses::Select_clause::get_statements_tree(Translate_context* context)
4018 if (this->statements_ == NULL)
4020 return this->statements_->get_tree(context);
4023 // Class Select_clauses.
4028 Select_clauses::traverse(Traverse* traverse)
4030 for (Clauses::iterator p = this->clauses_.begin();
4031 p != this->clauses_.end();
4034 if (p->traverse(traverse) == TRAVERSE_EXIT)
4035 return TRAVERSE_EXIT;
4037 return TRAVERSE_CONTINUE;
4040 // Lowering. Here we pull out the channel and the send values, to
4041 // enforce the order of evaluation. We also add explicit send and
4042 // receive statements to the clauses.
4045 Select_clauses::lower(Block* b)
4047 for (Clauses::iterator p = this->clauses_.begin();
4048 p != this->clauses_.end();
4056 Select_clauses::determine_types()
4058 for (Clauses::iterator p = this->clauses_.begin();
4059 p != this->clauses_.end();
4061 p->determine_types();
4064 // Return whether these select clauses fall through to the statement
4065 // following the overall select statement.
4068 Select_clauses::may_fall_through() const
4070 for (Clauses::const_iterator p = this->clauses_.begin();
4071 p != this->clauses_.end();
4073 if (p->may_fall_through())
4078 // Return a tree. We build a call to
4079 // size_t __go_select(size_t count, _Bool has_default,
4080 // channel* channels, _Bool* is_send)
4082 // There are COUNT entries in the CHANNELS and IS_SEND arrays. The
4083 // value in the IS_SEND array is true for send, false for receive.
4084 // __go_select returns an integer from 0 to COUNT, inclusive. A
4085 // return of 0 means that the default case should be run; this only
4086 // happens if HAS_DEFAULT is non-zero. Otherwise the number indicates
4089 // FIXME: This doesn't handle channels which send interface types
4090 // where the receiver has a static type which matches that interface.
4093 Select_clauses::get_tree(Translate_context* context,
4094 Unnamed_label *break_label,
4095 source_location location)
4097 size_t count = this->clauses_.size();
4098 VEC(constructor_elt, gc)* chan_init = VEC_alloc(constructor_elt, gc, count);
4099 VEC(constructor_elt, gc)* is_send_init = VEC_alloc(constructor_elt, gc,
4101 Select_clause* default_clause = NULL;
4102 tree final_stmt_list = NULL_TREE;
4103 tree channel_type_tree = NULL_TREE;
4106 for (Clauses::iterator p = this->clauses_.begin();
4107 p != this->clauses_.end();
4110 if (p->is_default())
4112 default_clause = &*p;
4117 tree channel_tree = p->channel()->get_tree(context);
4118 if (channel_tree == error_mark_node)
4119 return error_mark_node;
4120 channel_type_tree = TREE_TYPE(channel_tree);
4122 constructor_elt* elt = VEC_quick_push(constructor_elt, chan_init, NULL);
4123 elt->index = build_int_cstu(sizetype, i);
4124 elt->value = channel_tree;
4126 elt = VEC_quick_push(constructor_elt, is_send_init, NULL);
4127 elt->index = build_int_cstu(sizetype, i);
4128 elt->value = p->is_send() ? boolean_true_node : boolean_false_node;
4132 gcc_assert(i == count);
4134 if (i == 0 && default_clause != NULL)
4136 // There is only a default clause.
4137 gcc_assert(final_stmt_list == NULL_TREE);
4138 tree stmt_list = NULL_TREE;
4139 append_to_statement_list(default_clause->get_statements_tree(context),
4141 append_to_statement_list(break_label->get_definition(), &stmt_list);
4145 tree pointer_chan_type_tree = (channel_type_tree == NULL_TREE
4147 : build_pointer_type(channel_type_tree));
4149 tree pointer_boolean_type_tree = build_pointer_type(boolean_type_node);
4154 chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4156 is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4161 tree index_type_tree = build_index_type(size_int(count - 1));
4162 tree chan_array_type_tree = build_array_type(channel_type_tree,
4164 tree chan_constructor = build_constructor(chan_array_type_tree,
4166 tree chan_var = create_tmp_var(chan_array_type_tree, "CHAN");
4167 DECL_IGNORED_P(chan_var) = 0;
4168 DECL_INITIAL(chan_var) = chan_constructor;
4169 DECL_SOURCE_LOCATION(chan_var) = location;
4170 TREE_ADDRESSABLE(chan_var) = 1;
4171 tree decl_expr = build1(DECL_EXPR, void_type_node, chan_var);
4172 SET_EXPR_LOCATION(decl_expr, location);
4173 append_to_statement_list(decl_expr, &final_stmt_list);
4175 tree is_send_array_type_tree = build_array_type(boolean_type_node,
4177 tree is_send_constructor = build_constructor(is_send_array_type_tree,
4179 tree is_send_var = create_tmp_var(is_send_array_type_tree, "ISSEND");
4180 DECL_IGNORED_P(is_send_var) = 0;
4181 DECL_INITIAL(is_send_var) = is_send_constructor;
4182 DECL_SOURCE_LOCATION(is_send_var) = location;
4183 TREE_ADDRESSABLE(is_send_var) = 1;
4184 decl_expr = build1(DECL_EXPR, void_type_node, is_send_var);
4185 SET_EXPR_LOCATION(decl_expr, location);
4186 append_to_statement_list(decl_expr, &final_stmt_list);
4188 chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4189 build_fold_addr_expr_loc(location,
4191 is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4192 build_fold_addr_expr_loc(location,
4196 static tree select_fndecl;
4197 tree call = Gogo::call_builtin(&select_fndecl,
4205 (default_clause == NULL
4206 ? boolean_false_node
4207 : boolean_true_node),
4208 pointer_chan_type_tree,
4210 pointer_boolean_type_tree,
4213 tree stmt_list = NULL_TREE;
4215 if (default_clause != NULL)
4216 this->add_clause_tree(context, 0, default_clause, break_label, &stmt_list);
4219 for (Clauses::iterator p = this->clauses_.begin();
4220 p != this->clauses_.end();
4223 if (!p->is_default())
4225 this->add_clause_tree(context, i, &*p, break_label, &stmt_list);
4230 append_to_statement_list(break_label->get_definition(), &stmt_list);
4232 tree switch_stmt = build3(SWITCH_EXPR, sizetype, call, stmt_list, NULL_TREE);
4233 SET_EXPR_LOCATION(switch_stmt, location);
4234 append_to_statement_list(switch_stmt, &final_stmt_list);
4236 return final_stmt_list;
4239 // Add the tree for CLAUSE to STMT_LIST.
4242 Select_clauses::add_clause_tree(Translate_context* context, int case_index,
4243 Select_clause* clause,
4244 Unnamed_label* bottom_label, tree* stmt_list)
4246 tree label = create_artificial_label(clause->location());
4247 append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
4248 build_int_cst(sizetype, case_index),
4251 append_to_statement_list(clause->get_statements_tree(context), stmt_list);
4252 tree g = bottom_label->get_goto(clause->statements() == NULL
4253 ? clause->location()
4254 : clause->statements()->end_location());
4255 append_to_statement_list(g, stmt_list);
4258 // Class Select_statement.
4260 // Return the break label for this switch statement, creating it if
4264 Select_statement::break_label()
4266 if (this->break_label_ == NULL)
4267 this->break_label_ = new Unnamed_label(this->location());
4268 return this->break_label_;
4271 // Lower a select statement. This will still return a select
4272 // statement, but it will be modified to implement the order of
4273 // evaluation rules, and to include the send and receive statements as
4274 // explicit statements in the clauses.
4277 Select_statement::do_lower(Gogo*, Block* enclosing)
4279 if (this->is_lowered_)
4281 Block* b = new Block(enclosing, this->location());
4282 this->clauses_->lower(b);
4283 this->is_lowered_ = true;
4284 b->add_statement(this);
4285 return Statement::make_block_statement(b, this->location());
4288 // Return the tree for a select statement.
4291 Select_statement::do_get_tree(Translate_context* context)
4293 return this->clauses_->get_tree(context, this->break_label(),
4297 // Make a select statement.
4300 Statement::make_select_statement(source_location location)
4302 return new Select_statement(location);
4305 // Class For_statement.
4310 For_statement::do_traverse(Traverse* traverse)
4312 if (this->init_ != NULL)
4314 if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
4315 return TRAVERSE_EXIT;
4317 if (this->cond_ != NULL)
4319 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
4320 return TRAVERSE_EXIT;
4322 if (this->post_ != NULL)
4324 if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
4325 return TRAVERSE_EXIT;
4327 return this->statements_->traverse(traverse);
4330 // Lower a For_statement into if statements and gotos. Getting rid of
4331 // complex statements make it easier to handle garbage collection.
4334 For_statement::do_lower(Gogo*, Block* enclosing)
4337 source_location loc = this->location();
4339 Block* b = new Block(enclosing, this->location());
4340 if (this->init_ != NULL)
4342 s = Statement::make_block_statement(this->init_,
4343 this->init_->start_location());
4344 b->add_statement(s);
4347 Unnamed_label* entry = NULL;
4348 if (this->cond_ != NULL)
4350 entry = new Unnamed_label(this->location());
4351 b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
4354 Unnamed_label* top = new Unnamed_label(this->location());
4355 b->add_statement(Statement::make_unnamed_label_statement(top));
4357 s = Statement::make_block_statement(this->statements_,
4358 this->statements_->start_location());
4359 b->add_statement(s);
4361 source_location end_loc = this->statements_->end_location();
4363 Unnamed_label* cont = this->continue_label_;
4365 b->add_statement(Statement::make_unnamed_label_statement(cont));
4367 if (this->post_ != NULL)
4369 s = Statement::make_block_statement(this->post_,
4370 this->post_->start_location());
4371 b->add_statement(s);
4372 end_loc = this->post_->end_location();
4375 if (this->cond_ == NULL)
4376 b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
4379 b->add_statement(Statement::make_unnamed_label_statement(entry));
4381 source_location cond_loc = this->cond_->location();
4382 Block* then_block = new Block(b, cond_loc);
4383 s = Statement::make_goto_unnamed_statement(top, cond_loc);
4384 then_block->add_statement(s);
4386 s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
4387 b->add_statement(s);
4390 Unnamed_label* brk = this->break_label_;
4392 b->add_statement(Statement::make_unnamed_label_statement(brk));
4394 b->set_end_location(end_loc);
4396 return Statement::make_block_statement(b, loc);
4399 // Return the break label, creating it if necessary.
4402 For_statement::break_label()
4404 if (this->break_label_ == NULL)
4405 this->break_label_ = new Unnamed_label(this->location());
4406 return this->break_label_;
4409 // Return the continue LABEL_EXPR.
4412 For_statement::continue_label()
4414 if (this->continue_label_ == NULL)
4415 this->continue_label_ = new Unnamed_label(this->location());
4416 return this->continue_label_;
4419 // Set the break and continue labels a for statement. This is used
4420 // when lowering a for range statement.
4423 For_statement::set_break_continue_labels(Unnamed_label* break_label,
4424 Unnamed_label* continue_label)
4426 gcc_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
4427 this->break_label_ = break_label;
4428 this->continue_label_ = continue_label;
4431 // Make a for statement.
4434 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
4435 source_location location)
4437 return new For_statement(init, cond, post, location);
4440 // Class For_range_statement.
4445 For_range_statement::do_traverse(Traverse* traverse)
4447 if (this->traverse_expression(traverse, &this->index_var_) == TRAVERSE_EXIT)
4448 return TRAVERSE_EXIT;
4449 if (this->value_var_ != NULL)
4451 if (this->traverse_expression(traverse, &this->value_var_)
4453 return TRAVERSE_EXIT;
4455 if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
4456 return TRAVERSE_EXIT;
4457 return this->statements_->traverse(traverse);
4460 // Lower a for range statement. For simplicity we lower this into a
4461 // for statement, which will then be lowered in turn to goto
4465 For_range_statement::do_lower(Gogo* gogo, Block* enclosing)
4467 Type* range_type = this->range_->type();
4468 if (range_type->points_to() != NULL
4469 && range_type->points_to()->array_type() != NULL
4470 && !range_type->points_to()->is_open_array_type())
4471 range_type = range_type->points_to();
4474 Type* value_type = NULL;
4475 if (range_type->array_type() != NULL)
4477 index_type = Type::lookup_integer_type("int");
4478 value_type = range_type->array_type()->element_type();
4480 else if (range_type->is_string_type())
4482 index_type = Type::lookup_integer_type("int");
4483 value_type = index_type;
4485 else if (range_type->map_type() != NULL)
4487 index_type = range_type->map_type()->key_type();
4488 value_type = range_type->map_type()->val_type();
4490 else if (range_type->channel_type() != NULL)
4492 index_type = range_type->channel_type()->element_type();
4493 if (this->value_var_ != NULL)
4495 if (!this->value_var_->type()->is_error_type())
4496 this->report_error(_("too many variables for range clause "
4498 return Statement::make_error_statement(this->location());
4503 this->report_error(_("range clause must have "
4504 "array, slice, setring, map, or channel type"));
4505 return Statement::make_error_statement(this->location());
4508 source_location loc = this->location();
4509 Block* temp_block = new Block(enclosing, loc);
4511 Named_object* range_object = NULL;
4512 Temporary_statement* range_temp = NULL;
4513 Var_expression* ve = this->range_->var_expression();
4515 range_object = ve->named_object();
4518 range_temp = Statement::make_temporary(NULL, this->range_, loc);
4519 temp_block->add_statement(range_temp);
4522 Temporary_statement* index_temp = Statement::make_temporary(index_type,
4524 temp_block->add_statement(index_temp);
4526 Temporary_statement* value_temp = NULL;
4527 if (this->value_var_ != NULL)
4529 value_temp = Statement::make_temporary(value_type, NULL, loc);
4530 temp_block->add_statement(value_temp);
4533 Block* body = new Block(temp_block, loc);
4540 // Arrange to do a loop appropriate for the type. We will produce
4541 // for INIT ; COND ; POST {
4543 // INDEX = INDEX_TEMP
4544 // VALUE = VALUE_TEMP // If there is a value
4545 // original statements
4548 if (range_type->array_type() != NULL)
4549 this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
4550 index_temp, value_temp, &init, &cond, &iter_init,
4552 else if (range_type->is_string_type())
4553 this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
4554 index_temp, value_temp, &init, &cond, &iter_init,
4556 else if (range_type->map_type() != NULL)
4557 this->lower_range_map(gogo, temp_block, body, range_object, range_temp,
4558 index_temp, value_temp, &init, &cond, &iter_init,
4560 else if (range_type->channel_type() != NULL)
4561 this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
4562 index_temp, value_temp, &init, &cond, &iter_init,
4567 if (iter_init != NULL)
4568 body->add_statement(Statement::make_block_statement(iter_init, loc));
4571 Expression* index_ref = Expression::make_temporary_reference(index_temp, loc);
4572 if (this->value_var_ == NULL)
4574 assign = Statement::make_assignment(this->index_var_, index_ref, loc);
4578 Expression_list* lhs = new Expression_list();
4579 lhs->push_back(this->index_var_);
4580 lhs->push_back(this->value_var_);
4582 Expression_list* rhs = new Expression_list();
4583 rhs->push_back(index_ref);
4584 rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4586 assign = Statement::make_tuple_assignment(lhs, rhs, loc);
4588 body->add_statement(assign);
4590 body->add_statement(Statement::make_block_statement(this->statements_, loc));
4592 body->set_end_location(this->statements_->end_location());
4594 For_statement* loop = Statement::make_for_statement(init, cond, post,
4596 loop->add_statements(body);
4597 loop->set_break_continue_labels(this->break_label_, this->continue_label_);
4599 temp_block->add_statement(loop);
4601 return Statement::make_block_statement(temp_block, loc);
4604 // Return a reference to the range, which may be in RANGE_OBJECT or in
4608 For_range_statement::make_range_ref(Named_object* range_object,
4609 Temporary_statement* range_temp,
4610 source_location loc)
4612 if (range_object != NULL)
4613 return Expression::make_var_reference(range_object, loc);
4615 return Expression::make_temporary_reference(range_temp, loc);
4618 // Return a call to the predeclared function FUNCNAME passing a
4619 // reference to the temporary variable ARG.
4622 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
4624 source_location loc)
4626 Named_object* no = gogo->lookup_global(funcname);
4627 gcc_assert(no != NULL && no->is_function_declaration());
4628 Expression* func = Expression::make_func_reference(no, NULL, loc);
4629 Expression_list* params = new Expression_list();
4630 params->push_back(arg);
4631 return Expression::make_call(func, params, false, loc);
4634 // Lower a for range over an array or slice.
4637 For_range_statement::lower_range_array(Gogo* gogo,
4640 Named_object* range_object,
4641 Temporary_statement* range_temp,
4642 Temporary_statement* index_temp,
4643 Temporary_statement* value_temp,
4649 source_location loc = this->location();
4651 // The loop we generate:
4652 // len_temp := len(range)
4653 // for index_temp = 0; index_temp < len_temp; index_temp++ {
4654 // value_temp = range[index_temp]
4655 // index = index_temp
4656 // value = value_temp
4662 // len_temp = len(range)
4665 Block* init = new Block(enclosing, loc);
4667 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
4668 Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
4669 Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
4671 init->add_statement(len_temp);
4674 mpz_init_set_ui(zval, 0UL);
4675 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4678 ref = Expression::make_temporary_reference(index_temp, loc);
4679 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4680 init->add_statement(s);
4685 // index_temp < len_temp
4687 ref = Expression::make_temporary_reference(index_temp, loc);
4688 Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
4689 Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
4693 // Set *PITER_INIT to
4694 // value_temp = range[index_temp]
4696 Block* iter_init = NULL;
4697 if (value_temp != NULL)
4699 iter_init = new Block(body_block, loc);
4701 ref = this->make_range_ref(range_object, range_temp, loc);
4702 Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
4703 Expression* index = Expression::make_index(ref, ref2, NULL, loc);
4705 ref = Expression::make_temporary_reference(value_temp, loc);
4706 s = Statement::make_assignment(ref, index, loc);
4708 iter_init->add_statement(s);
4710 *piter_init = iter_init;
4715 Block* post = new Block(enclosing, loc);
4716 ref = Expression::make_temporary_reference(index_temp, loc);
4717 s = Statement::make_inc_statement(ref);
4718 post->add_statement(s);
4722 // Lower a for range over a string.
4725 For_range_statement::lower_range_string(Gogo* gogo,
4728 Named_object* range_object,
4729 Temporary_statement* range_temp,
4730 Temporary_statement* index_temp,
4731 Temporary_statement* value_temp,
4737 source_location loc = this->location();
4739 // The loop we generate:
4740 // var next_index_temp int
4741 // for index_temp = 0; ; index_temp = next_index_temp {
4742 // next_index_temp, value_temp = stringiter2(range, index_temp)
4743 // if next_index_temp == 0 {
4746 // index = index_temp
4747 // value = value_temp
4752 // var next_index_temp int
4755 Block* init = new Block(enclosing, loc);
4757 Temporary_statement* next_index_temp =
4758 Statement::make_temporary(index_temp->type(), NULL, loc);
4759 init->add_statement(next_index_temp);
4762 mpz_init_set_ui(zval, 0UL);
4763 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4765 Expression* ref = Expression::make_temporary_reference(index_temp, loc);
4766 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4768 init->add_statement(s);
4771 // The loop has no condition.
4775 // Set *PITER_INIT to
4776 // next_index_temp = runtime.stringiter(range, index_temp)
4778 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
4780 // if next_index_temp == 0 {
4784 Block* iter_init = new Block(body_block, loc);
4787 if (value_temp == NULL)
4789 static Named_object* stringiter;
4790 if (stringiter == NULL)
4792 source_location bloc = BUILTINS_LOCATION;
4793 Type* int_type = gogo->lookup_global("int")->type_value();
4795 Typed_identifier_list* params = new Typed_identifier_list();
4796 params->push_back(Typed_identifier("s", Type::make_string_type(),
4798 params->push_back(Typed_identifier("k", int_type, bloc));
4800 Typed_identifier_list* results = new Typed_identifier_list();
4801 results->push_back(Typed_identifier("", int_type, bloc));
4803 Function_type* fntype = Type::make_function_type(NULL, params,
4805 stringiter = Named_object::make_function_declaration("stringiter",
4808 const char* n = "runtime.stringiter";
4809 stringiter->func_declaration_value()->set_asm_name(n);
4815 static Named_object* stringiter2;
4816 if (stringiter2 == NULL)
4818 source_location bloc = BUILTINS_LOCATION;
4819 Type* int_type = gogo->lookup_global("int")->type_value();
4821 Typed_identifier_list* params = new Typed_identifier_list();
4822 params->push_back(Typed_identifier("s", Type::make_string_type(),
4824 params->push_back(Typed_identifier("k", int_type, bloc));
4826 Typed_identifier_list* results = new Typed_identifier_list();
4827 results->push_back(Typed_identifier("", int_type, bloc));
4828 results->push_back(Typed_identifier("", int_type, bloc));
4830 Function_type* fntype = Type::make_function_type(NULL, params,
4832 stringiter2 = Named_object::make_function_declaration("stringiter",
4835 const char* n = "runtime.stringiter2";
4836 stringiter2->func_declaration_value()->set_asm_name(n);
4841 Expression* func = Expression::make_func_reference(no, NULL, loc);
4842 Expression_list* params = new Expression_list();
4843 params->push_back(this->make_range_ref(range_object, range_temp, loc));
4844 params->push_back(Expression::make_temporary_reference(index_temp, loc));
4845 Call_expression* call = Expression::make_call(func, params, false, loc);
4847 if (value_temp == NULL)
4849 ref = Expression::make_temporary_reference(next_index_temp, loc);
4850 s = Statement::make_assignment(ref, call, loc);
4854 Expression_list* lhs = new Expression_list();
4855 lhs->push_back(Expression::make_temporary_reference(next_index_temp,
4857 lhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4859 Expression_list* rhs = new Expression_list();
4860 rhs->push_back(Expression::make_call_result(call, 0));
4861 rhs->push_back(Expression::make_call_result(call, 1));
4863 s = Statement::make_tuple_assignment(lhs, rhs, loc);
4865 iter_init->add_statement(s);
4867 ref = Expression::make_temporary_reference(next_index_temp, loc);
4868 zexpr = Expression::make_integer(&zval, NULL, loc);
4870 Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
4872 Block* then_block = new Block(iter_init, loc);
4873 s = Statement::make_break_statement(this->break_label(), loc);
4874 then_block->add_statement(s);
4876 s = Statement::make_if_statement(equals, then_block, NULL, loc);
4877 iter_init->add_statement(s);
4879 *piter_init = iter_init;
4882 // index_temp = next_index_temp
4884 Block* post = new Block(enclosing, loc);
4886 Expression* lhs = Expression::make_temporary_reference(index_temp, loc);
4887 Expression* rhs = Expression::make_temporary_reference(next_index_temp, loc);
4888 s = Statement::make_assignment(lhs, rhs, loc);
4890 post->add_statement(s);
4894 // Lower a for range over a map.
4897 For_range_statement::lower_range_map(Gogo* gogo,
4900 Named_object* range_object,
4901 Temporary_statement* range_temp,
4902 Temporary_statement* index_temp,
4903 Temporary_statement* value_temp,
4909 source_location loc = this->location();
4911 // The runtime uses a struct to handle ranges over a map. The
4912 // struct is four pointers long. The first pointer is NULL when we
4913 // have completed the iteration.
4915 // The loop we generate:
4916 // var hiter map_iteration_struct
4917 // for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
4918 // mapiter2(hiter, &index_temp, &value_temp)
4919 // index = index_temp
4920 // value = value_temp
4925 // var hiter map_iteration_struct
4926 // runtime.mapiterinit(range, &hiter)
4928 Block* init = new Block(enclosing, loc);
4930 const unsigned long map_iteration_size = 4;
4933 mpz_init_set_ui(ival, map_iteration_size);
4934 Expression* iexpr = Expression::make_integer(&ival, NULL, loc);
4937 Type* byte_type = gogo->lookup_global("byte")->type_value();
4938 Type* ptr_type = Type::make_pointer_type(byte_type);
4940 Type* map_iteration_type = Type::make_array_type(ptr_type, iexpr);
4941 Type* map_iteration_ptr = Type::make_pointer_type(map_iteration_type);
4943 Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
4945 init->add_statement(hiter);
4947 source_location bloc = BUILTINS_LOCATION;
4948 Typed_identifier_list* param_types = new Typed_identifier_list();
4949 param_types->push_back(Typed_identifier("map", this->range_->type(), bloc));
4950 param_types->push_back(Typed_identifier("it", map_iteration_ptr, bloc));
4951 Function_type* fntype = Type::make_function_type(NULL, param_types, NULL,
4954 Named_object* mapiterinit =
4955 Named_object::make_function_declaration("mapiterinit", NULL, fntype, bloc);
4956 const char* n = "runtime.mapiterinit";
4957 mapiterinit->func_declaration_value()->set_asm_name(n);
4959 Expression* func = Expression::make_func_reference(mapiterinit, NULL, loc);
4960 Expression_list* params = new Expression_list();
4961 params->push_back(this->make_range_ref(range_object, range_temp, loc));
4962 Expression* ref = Expression::make_temporary_reference(hiter, loc);
4963 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
4964 Expression* call = Expression::make_call(func, params, false, loc);
4965 init->add_statement(Statement::make_statement(call));
4972 ref = Expression::make_temporary_reference(hiter, loc);
4975 mpz_init_set_ui(zval, 0UL);
4976 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4979 Expression* index = Expression::make_index(ref, zexpr, NULL, loc);
4981 Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
4982 Expression::make_nil(loc),
4987 // Set *PITER_INIT to
4988 // mapiter1(hiter, &index_temp)
4990 // mapiter2(hiter, &index_temp, &value_temp)
4992 Block* iter_init = new Block(body_block, loc);
4994 param_types = new Typed_identifier_list();
4995 param_types->push_back(Typed_identifier("hiter", map_iteration_ptr, bloc));
4996 Type* pkey_type = Type::make_pointer_type(index_temp->type());
4997 param_types->push_back(Typed_identifier("key", pkey_type, bloc));
4998 if (value_temp != NULL)
5000 Type* pval_type = Type::make_pointer_type(value_temp->type());
5001 param_types->push_back(Typed_identifier("val", pval_type, bloc));
5003 fntype = Type::make_function_type(NULL, param_types, NULL, bloc);
5004 n = value_temp == NULL ? "mapiter1" : "mapiter2";
5005 Named_object* mapiter = Named_object::make_function_declaration(n, NULL,
5007 n = value_temp == NULL ? "runtime.mapiter1" : "runtime.mapiter2";
5008 mapiter->func_declaration_value()->set_asm_name(n);
5010 func = Expression::make_func_reference(mapiter, NULL, loc);
5011 params = new Expression_list();
5012 ref = Expression::make_temporary_reference(hiter, loc);
5013 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5014 ref = Expression::make_temporary_reference(index_temp, loc);
5015 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5016 if (value_temp != NULL)
5018 ref = Expression::make_temporary_reference(value_temp, loc);
5019 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5021 call = Expression::make_call(func, params, false, loc);
5022 iter_init->add_statement(Statement::make_statement(call));
5024 *piter_init = iter_init;
5027 // mapiternext(&hiter)
5029 Block* post = new Block(enclosing, loc);
5031 static Named_object* mapiternext;
5032 if (mapiternext == NULL)
5034 param_types = new Typed_identifier_list();
5035 param_types->push_back(Typed_identifier("it", map_iteration_ptr, bloc));
5036 fntype = Type::make_function_type(NULL, param_types, NULL, bloc);
5037 mapiternext = Named_object::make_function_declaration("mapiternext",
5040 const char* n = "runtime.mapiternext";
5041 mapiternext->func_declaration_value()->set_asm_name(n);
5044 func = Expression::make_func_reference(mapiternext, NULL, loc);
5045 params = new Expression_list();
5046 ref = Expression::make_temporary_reference(hiter, loc);
5047 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5048 call = Expression::make_call(func, params, false, loc);
5049 post->add_statement(Statement::make_statement(call));
5054 // Lower a for range over a channel.
5057 For_range_statement::lower_range_channel(Gogo* gogo,
5060 Named_object* range_object,
5061 Temporary_statement* range_temp,
5062 Temporary_statement* index_temp,
5063 Temporary_statement* value_temp,
5069 gcc_assert(value_temp == NULL);
5071 source_location loc = this->location();
5073 // The loop we generate:
5075 // index_temp = <-range
5076 // if closed(range) {
5079 // index = index_temp
5080 // value = value_temp
5084 // We have no initialization code, no condition, and no post code.
5090 // Set *PITER_INIT to
5091 // index_temp = <-range
5092 // if closed(range) {
5096 Block* iter_init = new Block(body_block, loc);
5098 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5099 Expression* cond = this->call_builtin(gogo, "closed", ref, loc);
5101 ref = this->make_range_ref(range_object, range_temp, loc);
5102 Expression* recv = Expression::make_receive(ref, loc);
5103 ref = Expression::make_temporary_reference(index_temp, loc);
5104 Statement* s = Statement::make_assignment(ref, recv, loc);
5105 iter_init->add_statement(s);
5107 Block* then_block = new Block(iter_init, loc);
5108 s = Statement::make_break_statement(this->break_label(), loc);
5109 then_block->add_statement(s);
5111 s = Statement::make_if_statement(cond, then_block, NULL, loc);
5112 iter_init->add_statement(s);
5114 *piter_init = iter_init;
5117 // Return the break LABEL_EXPR.
5120 For_range_statement::break_label()
5122 if (this->break_label_ == NULL)
5123 this->break_label_ = new Unnamed_label(this->location());
5124 return this->break_label_;
5127 // Return the continue LABEL_EXPR.
5130 For_range_statement::continue_label()
5132 if (this->continue_label_ == NULL)
5133 this->continue_label_ = new Unnamed_label(this->location());
5134 return this->continue_label_;
5137 // Make a for statement with a range clause.
5139 For_range_statement*
5140 Statement::make_for_range_statement(Expression* index_var,
5141 Expression* value_var,
5143 source_location location)
5145 return new For_range_statement(index_var, value_var, range, location);