ptr_traits.h (pointer_traits): Fix typos.
[gcc.git] / gcc / go / gofrontend / statements.cc.merge-left.r167407
1 // statements.cc -- Go frontend statements.
2
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.
6
7 #include "go-system.h"
8
9 #include <gmp.h>
10
11 #ifndef ENABLE_BUILD_WITH_CXX
12 extern "C"
13 {
14 #endif
15
16 #include "intl.h"
17 #include "tree.h"
18 #include "gimple.h"
19 #include "convert.h"
20 #include "tree-iterator.h"
21 #include "tree-flow.h"
22 #include "real.h"
23
24 #ifndef ENABLE_BUILD_WITH_CXX
25 }
26 #endif
27
28 #include "go-c.h"
29 #include "types.h"
30 #include "expressions.h"
31 #include "gogo.h"
32 #include "statements.h"
33
34 // Class Statement.
35
36 Statement::Statement(Statement_classification classification,
37 source_location location)
38 : classification_(classification), location_(location)
39 {
40 }
41
42 Statement::~Statement()
43 {
44 }
45
46 // Traverse the tree. The work of walking the components is handled
47 // by the subclasses.
48
49 int
50 Statement::traverse(Block* block, size_t* pindex, Traverse* traverse)
51 {
52 if (this->classification_ == STATEMENT_ERROR)
53 return TRAVERSE_CONTINUE;
54
55 unsigned int traverse_mask = traverse->traverse_mask();
56
57 if ((traverse_mask & Traverse::traverse_statements) != 0)
58 {
59 int t = traverse->statement(block, pindex, this);
60 if (t == TRAVERSE_EXIT)
61 return TRAVERSE_EXIT;
62 else if (t == TRAVERSE_SKIP_COMPONENTS)
63 return TRAVERSE_CONTINUE;
64 }
65
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
68 // walk them.
69 return this->do_traverse(traverse);
70 }
71
72 // Traverse the contents of a statement.
73
74 int
75 Statement::traverse_contents(Traverse* traverse)
76 {
77 return this->do_traverse(traverse);
78 }
79
80 // Traverse assignments.
81
82 bool
83 Statement::traverse_assignments(Traverse_assignments* tassign)
84 {
85 if (this->classification_ == STATEMENT_ERROR)
86 return false;
87 return this->do_traverse_assignments(tassign);
88 }
89
90 // Traverse an expression in a statement. This is a helper function
91 // for child classes.
92
93 int
94 Statement::traverse_expression(Traverse* traverse, Expression** expr)
95 {
96 if ((traverse->traverse_mask()
97 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
98 return TRAVERSE_CONTINUE;
99 return Expression::traverse(expr, traverse);
100 }
101
102 // Traverse an expression list in a statement. This is a helper
103 // function for child classes.
104
105 int
106 Statement::traverse_expression_list(Traverse* traverse,
107 Expression_list* expr_list)
108 {
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);
114 }
115
116 // Traverse a type in a statement. This is a helper function for
117 // child classes.
118
119 int
120 Statement::traverse_type(Traverse* traverse, Type* type)
121 {
122 if ((traverse->traverse_mask()
123 & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
124 return TRAVERSE_CONTINUE;
125 return Type::traverse(type, traverse);
126 }
127
128 // Set type information for unnamed constants. This is really done by
129 // the child class.
130
131 void
132 Statement::determine_types()
133 {
134 this->do_determine_types();
135 }
136
137 // If this is a thunk statement, return it.
138
139 Thunk_statement*
140 Statement::thunk_statement()
141 {
142 Thunk_statement* ret = this->convert<Thunk_statement, STATEMENT_GO>();
143 if (ret == NULL)
144 ret = this->convert<Thunk_statement, STATEMENT_DEFER>();
145 return ret;
146 }
147
148 // Get a tree for a Statement. This is really done by the child
149 // class.
150
151 tree
152 Statement::get_tree(Translate_context* context)
153 {
154 if (this->classification_ == STATEMENT_ERROR)
155 return error_mark_node;
156
157 return this->do_get_tree(context);
158 }
159
160 // Build tree nodes and set locations.
161
162 tree
163 Statement::build_stmt_1(int tree_code_value, tree node)
164 {
165 tree ret = build1(static_cast<tree_code>(tree_code_value),
166 void_type_node, node);
167 SET_EXPR_LOCATION(ret, this->location_);
168 return ret;
169 }
170
171 // Note that this statement is erroneous. This is called by children
172 // when they discover an error.
173
174 void
175 Statement::set_is_error()
176 {
177 this->classification_ = STATEMENT_ERROR;
178 }
179
180 // For children to call to report an error conveniently.
181
182 void
183 Statement::report_error(const char* msg)
184 {
185 error_at(this->location_, "%s", msg);
186 this->set_is_error();
187 }
188
189 // An error statement, used to avoid crashing after we report an
190 // error.
191
192 class Error_statement : public Statement
193 {
194 public:
195 Error_statement(source_location location)
196 : Statement(STATEMENT_ERROR, location)
197 { }
198
199 protected:
200 int
201 do_traverse(Traverse*)
202 { return TRAVERSE_CONTINUE; }
203
204 tree
205 do_get_tree(Translate_context*)
206 { gcc_unreachable(); }
207 };
208
209 // Make an error statement.
210
211 Statement*
212 Statement::make_error_statement(source_location location)
213 {
214 return new Error_statement(location);
215 }
216
217 // Class Variable_declaration_statement.
218
219 Variable_declaration_statement::Variable_declaration_statement(
220 Named_object* var)
221 : Statement(STATEMENT_VARIABLE_DECLARATION, var->var_value()->location()),
222 var_(var)
223 {
224 }
225
226 // We don't actually traverse the variable here; it was traversed
227 // while traversing the Block.
228
229 int
230 Variable_declaration_statement::do_traverse(Traverse*)
231 {
232 return TRAVERSE_CONTINUE;
233 }
234
235 // Traverse the assignments in a variable declaration. Note that this
236 // traversal is different from the usual traversal.
237
238 bool
239 Variable_declaration_statement::do_traverse_assignments(
240 Traverse_assignments* tassign)
241 {
242 tassign->initialize_variable(this->var_);
243 return true;
244 }
245
246 // Return the tree for a variable declaration.
247
248 tree
249 Variable_declaration_statement::do_get_tree(Translate_context* context)
250 {
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();
255
256 tree init = variable->get_init_tree(context->gogo(), context->function());
257 if (init == error_mark_node)
258 return error_mark_node;
259
260 // If this variable lives on the heap, we need to allocate it now.
261 if (!variable->is_in_heap())
262 {
263 DECL_INITIAL(val) = init;
264 return this->build_stmt_1(DECL_EXPR, val);
265 }
266 else
267 {
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,
275 this->location());
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));
281 }
282 }
283
284 // Make a variable declaration.
285
286 Statement*
287 Statement::make_variable_declaration(Named_object* var)
288 {
289 return new Variable_declaration_statement(var);
290 }
291
292 // Class Temporary_statement.
293
294 // Return the type of the temporary variable.
295
296 Type*
297 Temporary_statement::type() const
298 {
299 return this->type_ != NULL ? this->type_ : this->init_->type();
300 }
301
302 // Traversal.
303
304 int
305 Temporary_statement::do_traverse(Traverse* traverse)
306 {
307 if (this->init_ == NULL)
308 return TRAVERSE_CONTINUE;
309 else
310 return this->traverse_expression(traverse, &this->init_);
311 }
312
313 // Traverse assignments.
314
315 bool
316 Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
317 {
318 if (this->init_ == NULL)
319 return false;
320 tassign->value(&this->init_, true, true);
321 return true;
322 }
323
324 // Determine types.
325
326 void
327 Temporary_statement::do_determine_types()
328 {
329 if (this->init_ != NULL)
330 {
331 if (this->type_ == NULL)
332 this->init_->determine_type_no_context();
333 else
334 {
335 Type_context context(this->type_, false);
336 this->init_->determine_type(&context);
337 }
338 }
339
340 if (this->type_ == NULL)
341 this->type_ = this->init_->type();
342
343 if (this->type_->is_abstract())
344 this->type_ = this->type_->make_non_abstract_type();
345 }
346
347 // Check types.
348
349 void
350 Temporary_statement::do_check_types(Gogo*)
351 {
352 if (this->type_ != NULL && this->init_ != NULL)
353 gcc_assert(Type::are_assignable(this->type_, this->init_->type(), NULL));
354 }
355
356 // Return a tree.
357
358 tree
359 Temporary_statement::do_get_tree(Translate_context* context)
360 {
361 gcc_assert(this->decl_ == NULL_TREE);
362 tree type_tree = this->type()->get_tree(context->gogo());
363 if (type_tree == error_mark_node)
364 {
365 this->decl_ = error_mark_node;
366 return error_mark_node;
367 }
368 // We can only use create_tmp_var if the type is not addressable.
369 if (!TREE_ADDRESSABLE(type_tree))
370 {
371 this->decl_ = create_tmp_var(type_tree, "GOTMP");
372 DECL_SOURCE_LOCATION(this->decl_) = this->location();
373 }
374 else
375 {
376 gcc_assert(context->function() != NULL && context->block() != NULL);
377 tree decl = build_decl(this->location(), VAR_DECL,
378 create_tmp_var_name("GOTMP"),
379 type_tree);
380 DECL_ARTIFICIAL(decl) = 1;
381 DECL_IGNORED_P(decl) = 1;
382 TREE_USED(decl) = 1;
383 gcc_assert(current_function_decl != NULL_TREE);
384 DECL_CONTEXT(decl) = current_function_decl;
385
386 // We have to add this variable to the block so that it winds up
387 // in a BIND_EXPR.
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;
392
393 this->decl_ = decl;
394 }
395 if (this->init_ != NULL)
396 DECL_INITIAL(this->decl_) =
397 Expression::convert_for_assignment(context, this->type(),
398 this->init_->type(),
399 this->init_->get_tree(context),
400 this->location());
401 if (this->is_address_taken_)
402 TREE_ADDRESSABLE(this->decl_) = 1;
403 return this->build_stmt_1(DECL_EXPR, this->decl_);
404 }
405
406 // Make and initialize a temporary variable in BLOCK.
407
408 Temporary_statement*
409 Statement::make_temporary(Type* type, Expression* init,
410 source_location location)
411 {
412 return new Temporary_statement(type, init, location);
413 }
414
415 // An assignment statement.
416
417 class Assignment_statement : public Statement
418 {
419 public:
420 Assignment_statement(Expression* lhs, Expression* rhs,
421 source_location location)
422 : Statement(STATEMENT_ASSIGNMENT, location),
423 lhs_(lhs), rhs_(rhs)
424 { }
425
426 protected:
427 int
428 do_traverse(Traverse* traverse);
429
430 bool
431 do_traverse_assignments(Traverse_assignments*);
432
433 void
434 do_determine_types();
435
436 void
437 do_check_types(Gogo*);
438
439 tree
440 do_get_tree(Translate_context*);
441
442 private:
443 // Left hand side--the lvalue.
444 Expression* lhs_;
445 // Right hand side--the rvalue.
446 Expression* rhs_;
447 };
448
449 // Traversal.
450
451 int
452 Assignment_statement::do_traverse(Traverse* traverse)
453 {
454 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
455 return TRAVERSE_EXIT;
456 return this->traverse_expression(traverse, &this->rhs_);
457 }
458
459 bool
460 Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
461 {
462 tassign->assignment(&this->lhs_, &this->rhs_);
463 return true;
464 }
465
466 // Set types for the assignment.
467
468 void
469 Assignment_statement::do_determine_types()
470 {
471 this->lhs_->determine_type_no_context();
472 Type_context context(this->lhs_->type(), false);
473 this->rhs_->determine_type(&context);
474 }
475
476 // Check types for an assignment.
477
478 void
479 Assignment_statement::do_check_types(Gogo*)
480 {
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())
486 {
487 if (!this->lhs_->type()->is_error_type())
488 this->report_error(_("invalid left hand side of assignment"));
489 return;
490 }
491
492 Type* lhs_type = this->lhs_->type();
493 Type* rhs_type = this->rhs_->type();
494 std::string reason;
495 if (!Type::are_assignable(lhs_type, rhs_type, &reason))
496 {
497 if (reason.empty())
498 error_at(this->location(), "incompatible types in assignment");
499 else
500 error_at(this->location(), "incompatible types in assignment (%s)",
501 reason.c_str());
502 this->set_is_error();
503 }
504
505 if (lhs_type->is_error_type()
506 || rhs_type->is_error_type()
507 || lhs_type->is_undefined()
508 || rhs_type->is_undefined())
509 {
510 // Make sure we get the error for an undefined type.
511 lhs_type->base();
512 rhs_type->base();
513 this->set_is_error();
514 }
515 }
516
517 // Build a tree for an assignment statement.
518
519 tree
520 Assignment_statement::do_get_tree(Translate_context* context)
521 {
522 tree rhs_tree = this->rhs_->get_tree(context);
523
524 if (this->lhs_->is_sink_expression())
525 return rhs_tree;
526
527 tree lhs_tree = this->lhs_->get_tree(context);
528
529 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
530 return error_mark_node;
531
532 rhs_tree = Expression::convert_for_assignment(context, this->lhs_->type(),
533 this->rhs_->type(), rhs_tree,
534 this->location());
535 if (rhs_tree == error_mark_node)
536 return error_mark_node;
537
538 return fold_build2_loc(this->location(), MODIFY_EXPR, void_type_node,
539 lhs_tree, rhs_tree);
540 }
541
542 // Make an assignment statement.
543
544 Statement*
545 Statement::make_assignment(Expression* lhs, Expression* rhs,
546 source_location location)
547 {
548 return new Assignment_statement(lhs, rhs, location);
549 }
550
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.
554
555 class Move_ordered_evals : public Traverse
556 {
557 public:
558 Move_ordered_evals(Block* block)
559 : Traverse(traverse_expressions),
560 block_(block)
561 { }
562
563 protected:
564 int
565 expression(Expression**);
566
567 private:
568 // The block where new temporary variables should be added.
569 Block* block_;
570 };
571
572 int
573 Move_ordered_evals::expression(Expression** pexpr)
574 {
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())
579 {
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);
584 }
585 return TRAVERSE_SKIP_COMPONENTS;
586 }
587
588 // An assignment operation statement.
589
590 class Assignment_operation_statement : public Statement
591 {
592 public:
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)
597 { }
598
599 protected:
600 int
601 do_traverse(Traverse*);
602
603 bool
604 do_traverse_assignments(Traverse_assignments*)
605 { gcc_unreachable(); }
606
607 Statement*
608 do_lower(Gogo*, Block*);
609
610 tree
611 do_get_tree(Translate_context*)
612 { gcc_unreachable(); }
613
614 private:
615 // The operator (OPERATOR_PLUSEQ, etc.).
616 Operator op_;
617 // Left hand side.
618 Expression* lhs_;
619 // Right hand side.
620 Expression* rhs_;
621 };
622
623 // Traversal.
624
625 int
626 Assignment_operation_statement::do_traverse(Traverse* traverse)
627 {
628 if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
629 return TRAVERSE_EXIT;
630 return this->traverse_expression(traverse, &this->rhs_);
631 }
632
633 // Lower an assignment operation statement to a regular assignment
634 // statement.
635
636 Statement*
637 Assignment_operation_statement::do_lower(Gogo*, Block* enclosing)
638 {
639 source_location loc = this->location();
640
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);
646
647 Expression* lval = this->lhs_->copy();
648
649 Operator op;
650 switch (this->op_)
651 {
652 case OPERATOR_PLUSEQ:
653 op = OPERATOR_PLUS;
654 break;
655 case OPERATOR_MINUSEQ:
656 op = OPERATOR_MINUS;
657 break;
658 case OPERATOR_OREQ:
659 op = OPERATOR_OR;
660 break;
661 case OPERATOR_XOREQ:
662 op = OPERATOR_XOR;
663 break;
664 case OPERATOR_MULTEQ:
665 op = OPERATOR_MULT;
666 break;
667 case OPERATOR_DIVEQ:
668 op = OPERATOR_DIV;
669 break;
670 case OPERATOR_MODEQ:
671 op = OPERATOR_MOD;
672 break;
673 case OPERATOR_LSHIFTEQ:
674 op = OPERATOR_LSHIFT;
675 break;
676 case OPERATOR_RSHIFTEQ:
677 op = OPERATOR_RSHIFT;
678 break;
679 case OPERATOR_ANDEQ:
680 op = OPERATOR_AND;
681 break;
682 case OPERATOR_BITCLEAREQ:
683 op = OPERATOR_BITCLEAR;
684 break;
685 default:
686 gcc_unreachable();
687 }
688
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())
692 {
693 delete b;
694 return s;
695 }
696 else
697 {
698 b->add_statement(s);
699 return Statement::make_block_statement(b, loc);
700 }
701 }
702
703 // Make an assignment operation statement.
704
705 Statement*
706 Statement::make_assignment_operation(Operator op, Expression* lhs,
707 Expression* rhs, source_location location)
708 {
709 return new Assignment_operation_statement(op, lhs, rhs, location);
710 }
711
712 // A tuple assignment statement. This differs from an assignment
713 // statement in that the right-hand-side expressions are evaluated in
714 // parallel.
715
716 class Tuple_assignment_statement : public Statement
717 {
718 public:
719 Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
720 source_location location)
721 : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
722 lhs_(lhs), rhs_(rhs)
723 { }
724
725 protected:
726 int
727 do_traverse(Traverse* traverse);
728
729 bool
730 do_traverse_assignments(Traverse_assignments*)
731 { gcc_unreachable(); }
732
733 Statement*
734 do_lower(Gogo*, Block*);
735
736 tree
737 do_get_tree(Translate_context*)
738 { gcc_unreachable(); }
739
740 private:
741 // Left hand side--a list of lvalues.
742 Expression_list* lhs_;
743 // Right hand side--a list of rvalues.
744 Expression_list* rhs_;
745 };
746
747 // Traversal.
748
749 int
750 Tuple_assignment_statement::do_traverse(Traverse* traverse)
751 {
752 if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
753 return TRAVERSE_EXIT;
754 return this->traverse_expression_list(traverse, this->rhs_);
755 }
756
757 // Lower a tuple assignment. We use temporary variables to split it
758 // up into a set of single assignments.
759
760 Statement*
761 Tuple_assignment_statement::do_lower(Gogo*, Block* enclosing)
762 {
763 source_location loc = this->location();
764
765 Block* b = new Block(enclosing, loc);
766
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();
772 ++plhs)
773 (*plhs)->traverse_subexpressions(&moe);
774
775 std::vector<Temporary_statement*> temps;
776 temps.reserve(this->lhs_->size());
777
778 Expression_list::const_iterator prhs = this->rhs_->begin();
779 for (Expression_list::const_iterator plhs = this->lhs_->begin();
780 plhs != this->lhs_->end();
781 ++plhs, ++prhs)
782 {
783 gcc_assert(prhs != this->rhs_->end());
784
785 if ((*plhs)->is_sink_expression())
786 {
787 b->add_statement(Statement::make_statement(*prhs));
788 continue;
789 }
790
791 Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
792 *prhs, loc);
793 b->add_statement(temp);
794 temps.push_back(temp);
795
796 }
797 gcc_assert(prhs == this->rhs_->end());
798
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();
803 ++plhs, ++prhs)
804 {
805 if ((*plhs)->is_sink_expression())
806 continue;
807
808 Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
809 Statement* s = Statement::make_assignment(*plhs, ref, loc);
810 b->add_statement(s);
811 ++ptemp;
812 }
813 gcc_assert(ptemp == temps.end());
814
815 return Statement::make_block_statement(b, loc);
816 }
817
818 // Make a tuple assignment statement.
819
820 Statement*
821 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
822 source_location location)
823 {
824 return new Tuple_assignment_statement(lhs, rhs, location);
825 }
826
827 // A tuple assignment from a map index expression.
828 // v, ok = m[k]
829
830 class Tuple_map_assignment_statement : public Statement
831 {
832 public:
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)
838 { }
839
840 protected:
841 int
842 do_traverse(Traverse* traverse);
843
844 bool
845 do_traverse_assignments(Traverse_assignments*)
846 { gcc_unreachable(); }
847
848 Statement*
849 do_lower(Gogo*, Block*);
850
851 tree
852 do_get_tree(Translate_context*)
853 { gcc_unreachable(); }
854
855 private:
856 // Lvalue which receives the value from the map.
857 Expression* val_;
858 // Lvalue which receives whether the key value was present.
859 Expression* present_;
860 // The map index expression.
861 Expression* map_index_;
862 };
863
864 // Traversal.
865
866 int
867 Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
868 {
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_);
873 }
874
875 // Lower a tuple map assignment.
876
877 Statement*
878 Tuple_map_assignment_statement::do_lower(Gogo*, Block* enclosing)
879 {
880 source_location loc = this->location();
881
882 Map_index_expression* map_index = this->map_index_->map_index_expression();
883 if (map_index == NULL)
884 {
885 this->report_error(_("expected map index on right hand side"));
886 return Statement::make_error_statement(loc);
887 }
888 Map_type* map_type = map_index->get_map_type();
889
890 Block* b = new Block(enclosing, loc);
891
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);
897
898 // Copy the key value into a temporary so that we can take its
899 // address without pushing the value onto the heap.
900
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);
905
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);
910
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);
915
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));
924
925 Typed_identifier_list* ret_types = new Typed_identifier_list();
926 ret_types->push_back(Typed_identifier("", Type::make_boolean_type(), bloc));
927
928 Function_type* fntype = Type::make_function_type(NULL, param_types,
929 ret_types, bloc);
930 Named_object* mapaccess2 =
931 Named_object::make_function_declaration("mapaccess2", NULL, fntype, bloc);
932 mapaccess2->func_declaration_value()->set_asm_name("runtime.mapaccess2");
933
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);
943
944 ref = Expression::make_temporary_reference(present_temp, loc);
945 Statement* s = Statement::make_assignment(ref, call, loc);
946 b->add_statement(s);
947
948 // val = val_temp
949 ref = Expression::make_temporary_reference(val_temp, loc);
950 s = Statement::make_assignment(this->val_, ref, loc);
951 b->add_statement(s);
952
953 // present = present_temp
954 ref = Expression::make_temporary_reference(present_temp, loc);
955 s = Statement::make_assignment(this->present_, ref, loc);
956 b->add_statement(s);
957
958 return Statement::make_block_statement(b, loc);
959 }
960
961 // Make a map assignment statement which returns a pair of values.
962
963 Statement*
964 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
965 Expression* map_index,
966 source_location location)
967 {
968 return new Tuple_map_assignment_statement(val, present, map_index, location);
969 }
970
971 // Assign a pair of entries to a map.
972 // m[k] = v, p
973
974 class Map_assignment_statement : public Statement
975 {
976 public:
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)
982 { }
983
984 protected:
985 int
986 do_traverse(Traverse* traverse);
987
988 bool
989 do_traverse_assignments(Traverse_assignments*)
990 { gcc_unreachable(); }
991
992 Statement*
993 do_lower(Gogo*, Block*);
994
995 tree
996 do_get_tree(Translate_context*)
997 { gcc_unreachable(); }
998
999 private:
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.
1003 Expression* val_;
1004 // Whether or not to add the value.
1005 Expression* should_set_;
1006 };
1007
1008 // Traverse a map assignment.
1009
1010 int
1011 Map_assignment_statement::do_traverse(Traverse* traverse)
1012 {
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_);
1017 }
1018
1019 // Lower a map assignment to a function call.
1020
1021 Statement*
1022 Map_assignment_statement::do_lower(Gogo*, Block* enclosing)
1023 {
1024 source_location loc = this->location();
1025
1026 Map_index_expression* map_index = this->map_index_->map_index_expression();
1027 if (map_index == NULL)
1028 {
1029 this->report_error(_("expected map index on left hand side"));
1030 return Statement::make_error_statement(loc);
1031 }
1032 Map_type* map_type = map_index->get_map_type();
1033
1034 Block* b = new Block(enclosing, loc);
1035
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,
1039 map_index->map(),
1040 loc);
1041 b->add_statement(map_temp);
1042
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);
1047
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);
1052
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,
1063 NULL, bloc);
1064 Named_object* mapassign2 =
1065 Named_object::make_function_declaration("mapassign2", NULL, fntype, bloc);
1066 mapassign2->func_declaration_value()->set_asm_name("runtime.mapassign2");
1067
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);
1080
1081 return Statement::make_block_statement(b, loc);
1082 }
1083
1084 // Make a statement which assigns a pair of entries to a map.
1085
1086 Statement*
1087 Statement::make_map_assignment(Expression* map_index,
1088 Expression* val, Expression* should_set,
1089 source_location location)
1090 {
1091 return new Map_assignment_statement(map_index, val, should_set, location);
1092 }
1093
1094 // A tuple assignment from a receive statement.
1095
1096 class Tuple_receive_assignment_statement : public Statement
1097 {
1098 public:
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)
1104 { }
1105
1106 protected:
1107 int
1108 do_traverse(Traverse* traverse);
1109
1110 bool
1111 do_traverse_assignments(Traverse_assignments*)
1112 { gcc_unreachable(); }
1113
1114 Statement*
1115 do_lower(Gogo*, Block*);
1116
1117 tree
1118 do_get_tree(Translate_context*)
1119 { gcc_unreachable(); }
1120
1121 private:
1122 // Lvalue which receives the value from the channel.
1123 Expression* val_;
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_;
1128 };
1129
1130 // Traversal.
1131
1132 int
1133 Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
1134 {
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_);
1139 }
1140
1141 // Lower to a function call.
1142
1143 Statement*
1144 Tuple_receive_assignment_statement::do_lower(Gogo*, Block* enclosing)
1145 {
1146 source_location loc = this->location();
1147
1148 Channel_type* channel_type = this->channel_->type()->channel_type();
1149 if (channel_type == NULL)
1150 {
1151 this->report_error(_("expected channel"));
1152 return Statement::make_error_statement(loc);
1153 }
1154 if (!channel_type->may_receive())
1155 {
1156 this->report_error(_("invalid receive on send-only channel"));
1157 return Statement::make_error_statement(loc);
1158 }
1159
1160 Block* b = new Block(enclosing, loc);
1161
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);
1167
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);
1172
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);
1177
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));
1184
1185 Typed_identifier_list* ret_types = new Typed_identifier_list();
1186 ret_types->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1187
1188 Function_type* fntype = Type::make_function_type(NULL, param_types,
1189 ret_types, bloc);
1190 Named_object* chanrecv2 =
1191 Named_object::make_function_declaration("chanrecv2", NULL, fntype, bloc);
1192 chanrecv2->func_declaration_value()->set_asm_name("runtime.chanrecv2");
1193
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);
1204
1205 // val = val_temp
1206 ref = Expression::make_temporary_reference(val_temp, loc);
1207 s = Statement::make_assignment(this->val_, ref, loc);
1208 b->add_statement(s);
1209
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);
1214
1215 return Statement::make_block_statement(b, loc);
1216 }
1217
1218 // Make a nonblocking receive statement.
1219
1220 Statement*
1221 Statement::make_tuple_receive_assignment(Expression* val, Expression* success,
1222 Expression* channel,
1223 source_location location)
1224 {
1225 return new Tuple_receive_assignment_statement(val, success, channel,
1226 location);
1227 }
1228
1229 // An assignment to a pair of values from a type guard. This is a
1230 // conditional type guard. v, ok = i.(type).
1231
1232 class Tuple_type_guard_assignment_statement : public Statement
1233 {
1234 public:
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)
1240 { }
1241
1242 protected:
1243 int
1244 do_traverse(Traverse*);
1245
1246 bool
1247 do_traverse_assignments(Traverse_assignments*)
1248 { gcc_unreachable(); }
1249
1250 Statement*
1251 do_lower(Gogo*, Block*);
1252
1253 tree
1254 do_get_tree(Translate_context*)
1255 { gcc_unreachable(); }
1256
1257 private:
1258 Call_expression*
1259 lower_to_empty_interface(const char*);
1260
1261 Call_expression*
1262 lower_to_type(const char*);
1263
1264 void
1265 lower_to_object_type(Block*, const char*);
1266
1267 // The variable which recieves the converted value.
1268 Expression* val_;
1269 // The variable which receives the indication of success.
1270 Expression* ok_;
1271 // The expression being converted.
1272 Expression* expr_;
1273 // The type to which the expression is being converted.
1274 Type* type_;
1275 };
1276
1277 // Traverse a type guard tuple assignment.
1278
1279 int
1280 Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
1281 {
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_);
1287 }
1288
1289 // Lower to a function call.
1290
1291 Statement*
1292 Tuple_type_guard_assignment_statement::do_lower(Gogo*, Block* enclosing)
1293 {
1294 source_location loc = this->location();
1295
1296 Type* expr_type = this->expr_->type();
1297 if (expr_type->interface_type() == NULL)
1298 {
1299 this->report_error(_("type assertion only valid for interface types"));
1300 return Statement::make_error_statement(loc);
1301 }
1302
1303 Block* b = new Block(enclosing, loc);
1304
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);
1310
1311 bool expr_is_empty = expr_type->interface_type()->is_empty();
1312 Call_expression* call;
1313 if (this->type_->interface_type() != NULL)
1314 {
1315 if (this->type_->interface_type()->is_empty())
1316 call = this->lower_to_empty_interface(expr_is_empty
1317 ? "ifaceE2E2"
1318 : "ifaceI2E2");
1319 else
1320 call = this->lower_to_type(expr_is_empty ? "ifaceE2I2" : "ifaceI2I2");
1321 }
1322 else if (this->type_->points_to() != NULL)
1323 call = this->lower_to_type(expr_is_empty ? "ifaceE2T2P" : "ifaceI2T2P");
1324 else
1325 {
1326 this->lower_to_object_type(b, expr_is_empty ? "ifaceE2T2" : "ifaceI2T2");
1327 call = NULL;
1328 }
1329
1330 if (call != NULL)
1331 {
1332 Expression* res = Expression::make_call_result(call, 0);
1333 Statement* s = Statement::make_assignment(this->val_, res, loc);
1334 b->add_statement(s);
1335
1336 res = Expression::make_call_result(call, 1);
1337 s = Statement::make_assignment(this->ok_, res, loc);
1338 b->add_statement(s);
1339 }
1340
1341 return Statement::make_block_statement(b, loc);
1342 }
1343
1344 // Lower a conversion to an empty interface type.
1345
1346 Call_expression*
1347 Tuple_type_guard_assignment_statement::lower_to_empty_interface(
1348 const char *fnname)
1349 {
1350 source_location loc = this->location();
1351
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,
1360 ret_types, bloc);
1361 Named_object* fn =
1362 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1363 std::string asm_name = "runtime.";
1364 asm_name += fnname;
1365 fn->func_declaration_value()->set_asm_name(asm_name);
1366
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);
1372 }
1373
1374 // Lower a conversion to a non-empty interface type or a pointer type.
1375
1376 Call_expression*
1377 Tuple_type_guard_assignment_statement::lower_to_type(const char* fnname)
1378 {
1379 source_location loc = this->location();
1380
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(),
1386 bloc));
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,
1392 ret_types, bloc);
1393 Named_object* fn =
1394 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1395 std::string asm_name = "runtime.";
1396 asm_name += fnname;
1397 fn->func_declaration_value()->set_asm_name(asm_name);
1398
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);
1405 }
1406
1407 // Lower a conversion to a non-interface non-pointer type.
1408
1409 void
1410 Tuple_type_guard_assignment_statement::lower_to_object_type(Block* b,
1411 const char *fnname)
1412 {
1413 source_location loc = this->location();
1414
1415 // var val_temp TYPE
1416 Temporary_statement* val_temp = Statement::make_temporary(this->type_,
1417 NULL, loc);
1418 b->add_statement(val_temp);
1419
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(),
1425 bloc));
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,
1432 ret_types, bloc);
1433 Named_object* fn =
1434 Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1435 std::string asm_name = "runtime.";
1436 asm_name += fnname;
1437 fn->func_declaration_value()->set_asm_name(asm_name);
1438
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);
1449
1450 // val = val_temp
1451 ref = Expression::make_temporary_reference(val_temp, loc);
1452 s = Statement::make_assignment(this->val_, ref, loc);
1453 b->add_statement(s);
1454 }
1455
1456 // Make an assignment from a type guard to a pair of variables.
1457
1458 Statement*
1459 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
1460 Expression* expr, Type* type,
1461 source_location location)
1462 {
1463 return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
1464 location);
1465 }
1466
1467 // An expression statement.
1468
1469 class Expression_statement : public Statement
1470 {
1471 public:
1472 Expression_statement(Expression* expr)
1473 : Statement(STATEMENT_EXPRESSION, expr->location()),
1474 expr_(expr)
1475 { }
1476
1477 protected:
1478 int
1479 do_traverse(Traverse* traverse)
1480 { return this->traverse_expression(traverse, &this->expr_); }
1481
1482 void
1483 do_determine_types()
1484 { this->expr_->determine_type_no_context(); }
1485
1486 bool
1487 do_may_fall_through() const;
1488
1489 tree
1490 do_get_tree(Translate_context* context)
1491 { return this->expr_->get_tree(context); }
1492
1493 private:
1494 Expression* expr_;
1495 };
1496
1497 // An expression statement may fall through unless it is a call to a
1498 // function which does not return.
1499
1500 bool
1501 Expression_statement::do_may_fall_through() const
1502 {
1503 const Call_expression* call = this->expr_->call_expression();
1504 if (call != NULL)
1505 {
1506 const Expression* fn = call->fn();
1507 const Func_expression* fe = fn->func_expression();
1508 if (fe != NULL)
1509 {
1510 const Named_object* no = fe->named_object();
1511
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();
1517 else
1518 fntype = NULL;
1519
1520 // The builtin function panic does not return.
1521 if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
1522 return false;
1523 }
1524 }
1525 return true;
1526 }
1527
1528 // Make an expression statement from an Expression.
1529
1530 Statement*
1531 Statement::make_statement(Expression* expr)
1532 {
1533 return new Expression_statement(expr);
1534 }
1535
1536 // A block statement--a list of statements which may include variable
1537 // definitions.
1538
1539 class Block_statement : public Statement
1540 {
1541 public:
1542 Block_statement(Block* block, source_location location)
1543 : Statement(STATEMENT_BLOCK, location),
1544 block_(block)
1545 { }
1546
1547 protected:
1548 int
1549 do_traverse(Traverse* traverse)
1550 { return this->block_->traverse(traverse); }
1551
1552 void
1553 do_determine_types()
1554 { this->block_->determine_types(); }
1555
1556 bool
1557 do_may_fall_through() const
1558 { return this->block_->may_fall_through(); }
1559
1560 tree
1561 do_get_tree(Translate_context* context)
1562 { return this->block_->get_tree(context); }
1563
1564 private:
1565 Block* block_;
1566 };
1567
1568 // Make a block statement.
1569
1570 Statement*
1571 Statement::make_block_statement(Block* block, source_location location)
1572 {
1573 return new Block_statement(block, location);
1574 }
1575
1576 // An increment or decrement statement.
1577
1578 class Inc_dec_statement : public Statement
1579 {
1580 public:
1581 Inc_dec_statement(bool is_inc, Expression* expr)
1582 : Statement(STATEMENT_INCDEC, expr->location()),
1583 expr_(expr), is_inc_(is_inc)
1584 { }
1585
1586 protected:
1587 int
1588 do_traverse(Traverse* traverse)
1589 { return this->traverse_expression(traverse, &this->expr_); }
1590
1591 bool
1592 do_traverse_assignments(Traverse_assignments*)
1593 { gcc_unreachable(); }
1594
1595 Statement*
1596 do_lower(Gogo*, Block*);
1597
1598 tree
1599 do_get_tree(Translate_context*)
1600 { gcc_unreachable(); }
1601
1602 private:
1603 // The l-value to increment or decrement.
1604 Expression* expr_;
1605 // Whether to increment or decrement.
1606 bool is_inc_;
1607 };
1608
1609 // Lower to += or -=.
1610
1611 Statement*
1612 Inc_dec_statement::do_lower(Gogo*, Block*)
1613 {
1614 source_location loc = this->location();
1615
1616 mpz_t oval;
1617 mpz_init_set_ui(oval, 1UL);
1618 Expression* oexpr = Expression::make_integer(&oval, NULL, loc);
1619 mpz_clear(oval);
1620
1621 Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
1622 return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
1623 }
1624
1625 // Make an increment statement.
1626
1627 Statement*
1628 Statement::make_inc_statement(Expression* expr)
1629 {
1630 return new Inc_dec_statement(true, expr);
1631 }
1632
1633 // Make a decrement statement.
1634
1635 Statement*
1636 Statement::make_dec_statement(Expression* expr)
1637 {
1638 return new Inc_dec_statement(false, expr);
1639 }
1640
1641 // Class Thunk_statement. This is the base class for go and defer
1642 // statements.
1643
1644 const char* const Thunk_statement::thunk_field_fn = "fn";
1645
1646 const char* const Thunk_statement::thunk_field_receiver = "receiver";
1647
1648 // Constructor.
1649
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)
1655 {
1656 }
1657
1658 // Return whether this is a simple statement which does not require a
1659 // thunk.
1660
1661 bool
1662 Thunk_statement::is_simple(Function_type* fntype) const
1663 {
1664 // We need a thunk to call a method, or to pass a variable number of
1665 // arguments.
1666 if (fntype->is_method() || fntype->is_varargs())
1667 return false;
1668
1669 // A defer statement requires a thunk to set up for whether the
1670 // function can call recover.
1671 if (this->classification() == STATEMENT_DEFER)
1672 return false;
1673
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)))
1680 return false;
1681
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();
1688 if (results != NULL
1689 && (results->size() > 1
1690 || (results->size() == 1
1691 && !results->begin()->type()->is_basic_type()
1692 && results->begin()->type()->points_to() == NULL)))
1693 return false;
1694
1695 // If this calls something which is not a simple function, then we
1696 // need a thunk.
1697 Expression* fn = this->call_->call_expression()->fn();
1698 if (fn->bound_method_expression() != NULL
1699 || fn->interface_field_reference_expression() != NULL)
1700 return false;
1701
1702 return true;
1703 }
1704
1705 // Traverse a thunk statement.
1706
1707 int
1708 Thunk_statement::do_traverse(Traverse* traverse)
1709 {
1710 return this->traverse_expression(traverse, &this->call_);
1711 }
1712
1713 // We implement traverse_assignment for a thunk statement because it
1714 // effectively copies the function call.
1715
1716 bool
1717 Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
1718 {
1719 Expression* fn = this->call_->call_expression()->fn();
1720 Expression* fn2 = fn;
1721 tassign->value(&fn2, true, false);
1722 return true;
1723 }
1724
1725 // Determine types in a thunk statement.
1726
1727 void
1728 Thunk_statement::do_determine_types()
1729 {
1730 this->call_->determine_type_no_context();
1731
1732 // Now that we know the types of the call, build the struct used to
1733 // pass parameters.
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);
1738 }
1739
1740 // Check types in a thunk statement.
1741
1742 void
1743 Thunk_statement::do_check_types(Gogo*)
1744 {
1745 Call_expression* ce = this->call_->call_expression();
1746 Function_type* fntype = ce->get_function_type();
1747 if (fntype != NULL && fntype->is_method())
1748 {
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"));
1753 }
1754 }
1755
1756 // The Traverse class used to find and simplify thunk statements.
1757
1758 class Simplify_thunk_traverse : public Traverse
1759 {
1760 public:
1761 Simplify_thunk_traverse(Gogo* gogo)
1762 : Traverse(traverse_blocks),
1763 gogo_(gogo)
1764 { }
1765
1766 int
1767 block(Block*);
1768
1769 private:
1770 Gogo* gogo_;
1771 };
1772
1773 int
1774 Simplify_thunk_traverse::block(Block* b)
1775 {
1776 // The parser ensures that thunk statements always appear at the end
1777 // of a block.
1778 if (b->statements()->size() < 1)
1779 return TRAVERSE_CONTINUE;
1780 Thunk_statement* stat = b->statements()->back()->thunk_statement();
1781 if (stat == NULL)
1782 return TRAVERSE_CONTINUE;
1783 if (stat->simplify_statement(this->gogo_, b))
1784 return TRAVERSE_SKIP_COMPONENTS;
1785 return TRAVERSE_CONTINUE;
1786 }
1787
1788 // Simplify all thunk statements.
1789
1790 void
1791 Gogo::simplify_thunk_statements()
1792 {
1793 Simplify_thunk_traverse thunk_traverse(this);
1794 this->traverse(&thunk_traverse);
1795 }
1796
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.
1803
1804 bool
1805 Thunk_statement::simplify_statement(Gogo* gogo, Block* block)
1806 {
1807 if (this->classification() == STATEMENT_ERROR)
1808 return false;
1809 if (this->call_->is_error_expression())
1810 return false;
1811
1812 Call_expression* ce = this->call_->call_expression();
1813 Function_type* fntype = ce->get_function_type();
1814 if (fntype == NULL || this->is_simple(fntype))
1815 return false;
1816
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;
1822
1823 source_location location = this->location();
1824
1825 std::string thunk_name = Gogo::thunk_name();
1826
1827 // Build the thunk.
1828 this->build_thunk(gogo, thunk_name, fntype);
1829
1830 // Generate code to call the thunk.
1831
1832 // Get the values to store into the struct which is the single
1833 // argument to the thunk.
1834
1835 Expression_list* vals = new Expression_list();
1836 if (fntype->is_builtin())
1837 ;
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)
1843 {
1844 vals->push_back(bound_method->method());
1845 Expression* first_arg = bound_method->first_argument();
1846
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);
1850
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();
1855 if (fatype != NULL)
1856 {
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);
1862 }
1863
1864 vals->push_back(first_arg);
1865 }
1866 else
1867 gcc_unreachable();
1868
1869 if (ce->args() != NULL)
1870 {
1871 for (Expression_list::const_iterator p = ce->args()->begin();
1872 p != ce->args()->end();
1873 ++p)
1874 vals->push_back(*p);
1875 }
1876
1877 // Build the struct.
1878 Expression* constructor =
1879 Expression::make_struct_composite_literal(this->struct_type_, vals,
1880 location);
1881
1882 // Allocate the initialized struct on the heap.
1883 constructor = Expression::make_heap_composite(constructor, location);
1884
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());
1888
1889 // Build the call.
1890 Expression* func = Expression::make_func_reference(named_thunk, NULL,
1891 location);
1892 Expression_list* params = new Expression_list();
1893 params->push_back(constructor);
1894 Call_expression* call = Expression::make_call(func, params, false, location);
1895
1896 // Build the simple go or defer statement.
1897 Statement* s;
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);
1902 else
1903 gcc_unreachable();
1904
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);
1909
1910 // We already ran the determine_types pass, so we need to run it now
1911 // for the new statement.
1912 s->determine_types();
1913
1914 // Sanity check.
1915 gogo->check_types_in_block(block);
1916
1917 // Return true to tell the block not to keep looking at statements.
1918 return true;
1919 }
1920
1921 // Set the name to use for thunk parameter N.
1922
1923 void
1924 Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
1925 {
1926 snprintf(buf, buflen, "a%d", n);
1927 }
1928
1929 // Build a new struct type to hold the parameters for a complicated
1930 // thunk statement. FNTYPE is the type of the function call.
1931
1932 Struct_type*
1933 Thunk_statement::build_struct(Function_type* fntype)
1934 {
1935 source_location location = this->location();
1936
1937 Struct_field_list* fields = new Struct_field_list();
1938
1939 Call_expression* ce = this->call_->call_expression();
1940 Expression* fn = ce->fn();
1941
1942 Interface_field_reference_expression* interface_method =
1943 fn->interface_field_reference_expression();
1944 if (interface_method != NULL)
1945 {
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(),
1950 location);
1951 fields->push_back(Struct_field(tid));
1952 }
1953 else if (!fntype->is_builtin())
1954 {
1955 // The function to call.
1956 Typed_identifier tid(Go_statement::thunk_field_fn, fntype, location);
1957 fields->push_back(Struct_field(tid));
1958 }
1959 else if (ce->is_recover_call())
1960 {
1961 // The predeclared recover function has no argument. However,
1962 // we add an argument when building recover thunks. Handle that
1963 // here.
1964 fields->push_back(Struct_field(Typed_identifier("can_recover",
1965 Type::make_boolean_type(),
1966 location)));
1967 }
1968
1969 if (fn->bound_method_expression() != NULL)
1970 {
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,
1977 location);
1978 fields->push_back(Struct_field(tid));
1979 }
1980
1981 const Expression_list* args = ce->args();
1982 if (args != NULL)
1983 {
1984 int i = 0;
1985 for (Expression_list::const_iterator p = args->begin();
1986 p != args->end();
1987 ++p, ++i)
1988 {
1989 char buf[50];
1990 this->thunk_field_param(i, buf, sizeof buf);
1991 fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
1992 location)));
1993 }
1994 }
1995
1996 return Type::make_struct_type(fields, location);
1997 }
1998
1999 // Build the thunk we are going to call. This is a brand new, albeit
2000 // artificial, function.
2001
2002 void
2003 Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
2004 Function_type* fntype)
2005 {
2006 source_location location = this->location();
2007
2008 Call_expression* ce = this->call_->call_expression();
2009
2010 bool may_call_recover = false;
2011 if (this->classification() == STATEMENT_DEFER)
2012 {
2013 Func_expression* fn = ce->fn()->func_expression();
2014 if (fn == NULL)
2015 may_call_recover = true;
2016 else
2017 {
2018 const Named_object* no = fn->named_object();
2019 if (!no->is_function())
2020 may_call_recover = true;
2021 else
2022 may_call_recover = no->func_value()->calls_recover();
2023 }
2024 }
2025
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,
2033 location));
2034
2035 Typed_identifier_list* thunk_results = NULL;
2036 if (may_call_recover)
2037 {
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(),
2043 location));
2044 }
2045
2046 Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
2047 thunk_results,
2048 location);
2049
2050 // Start building the thunk.
2051 Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
2052 location);
2053
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)
2058 {
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);
2063
2064 static Named_object* set_defer_retaddr;
2065 if (set_defer_retaddr == NULL)
2066 {
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));
2071
2072 Typed_identifier_list* result_types = new Typed_identifier_list();
2073 result_types->push_back(Typed_identifier("",
2074 Type::make_boolean_type(),
2075 bloc));
2076
2077 Function_type* t = Type::make_function_type(NULL, param_types,
2078 result_types, bloc);
2079 set_defer_retaddr =
2080 Named_object::make_function_declaration("__go_set_defer_retaddr",
2081 NULL, t, bloc);
2082 const char* n = "__go_set_defer_retaddr";
2083 set_defer_retaddr->func_declaration_value()->set_asm_name(n);
2084 }
2085
2086 Expression* fn = Expression::make_func_reference(set_defer_retaddr,
2087 NULL, location);
2088 Expression* call = Expression::make_call(fn, args, false, location);
2089
2090 // This is a hack to prevent the middle-end from deleting the
2091 // label.
2092 gogo->start_block(location);
2093 gogo->add_statement(Statement::make_goto_statement(retaddr_label,
2094 location));
2095 Block* then_block = gogo->finish_block(location);
2096 then_block->determine_types();
2097
2098 Statement* s = Statement::make_if_statement(call, then_block, NULL,
2099 location);
2100 s->determine_types();
2101 gogo->add_statement(s);
2102 }
2103
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());
2107
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,
2111 location);
2112 thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
2113 location);
2114
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();
2118
2119 Expression* func_to_call;
2120 unsigned int next_index;
2121 if (!fntype->is_builtin())
2122 {
2123 func_to_call = Expression::make_field_reference(thunk_parameter,
2124 0, location);
2125 next_index = 1;
2126 }
2127 else
2128 {
2129 gcc_assert(bound_method == NULL && interface_method == NULL);
2130 func_to_call = ce->fn();
2131 next_index = 0;
2132 }
2133
2134 if (bound_method != NULL)
2135 {
2136 Expression* r = Expression::make_field_reference(thunk_parameter, 1,
2137 location);
2138 // The main program passes in a function pointer from the
2139 // interface expression, so here we can make a bound method in
2140 // all cases.
2141 func_to_call = Expression::make_bound_method(r, func_to_call,
2142 location);
2143 next_index = 2;
2144 }
2145 else if (interface_method != NULL)
2146 {
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,
2150 name,
2151 location);
2152 }
2153
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)
2158 ++p;
2159 for (; p != fields->end(); ++p, ++next_index)
2160 {
2161 Expression* thunk_param = Expression::make_var_reference(named_parameter,
2162 location);
2163 thunk_param = Expression::make_unary(OPERATOR_MULT, thunk_param,
2164 location);
2165 Expression* param = Expression::make_field_reference(thunk_param,
2166 next_index,
2167 location);
2168 call_params->push_back(param);
2169 }
2170
2171 Expression* call = Expression::make_call(func_to_call, call_params, false,
2172 location);
2173 // We need to lower in case this is a builtin function.
2174 call = call->lower(gogo, function, -1);
2175 if (may_call_recover)
2176 {
2177 Call_expression* ce = call->call_expression();
2178 if (ce != NULL)
2179 ce->set_is_deferred();
2180 }
2181
2182 Statement* call_statement = Statement::make_statement(call);
2183
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();
2187
2188 gogo->add_statement(call_statement);
2189
2190 // If this is a defer statement, the label comes immediately after
2191 // the call.
2192 if (may_call_recover)
2193 {
2194 gogo->add_label_definition("retaddr", location);
2195
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,
2201 location));
2202 }
2203
2204 // That is all the thunk has to do.
2205 gogo->finish_function(location);
2206 }
2207
2208 // Get the function and argument trees.
2209
2210 void
2211 Thunk_statement::get_fn_and_arg(Translate_context* context, tree* pfn,
2212 tree* parg)
2213 {
2214 if (this->call_->is_error_expression())
2215 {
2216 *pfn = error_mark_node;
2217 *parg = error_mark_node;
2218 return;
2219 }
2220
2221 Call_expression* ce = this->call_->call_expression();
2222
2223 Expression* fn = ce->fn();
2224 *pfn = fn->get_tree(context);
2225
2226 const Expression_list* args = ce->args();
2227 if (args == NULL || args->empty())
2228 *parg = null_pointer_node;
2229 else
2230 {
2231 gcc_assert(args->size() == 1);
2232 *parg = args->front()->get_tree(context);
2233 }
2234 }
2235
2236 // Class Go_statement.
2237
2238 tree
2239 Go_statement::do_get_tree(Translate_context* context)
2240 {
2241 tree fn_tree;
2242 tree arg_tree;
2243 this->get_fn_and_arg(context, &fn_tree, &arg_tree);
2244
2245 static tree go_fndecl;
2246
2247 tree fn_arg_type = NULL_TREE;
2248 if (go_fndecl == NULL_TREE)
2249 {
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);
2254 }
2255
2256 return Gogo::call_builtin(&go_fndecl,
2257 this->location(),
2258 "__go_go",
2259 2,
2260 void_type_node,
2261 fn_arg_type,
2262 fn_tree,
2263 ptr_type_node,
2264 arg_tree);
2265 }
2266
2267 // Make a go statement.
2268
2269 Statement*
2270 Statement::make_go_statement(Call_expression* call, source_location location)
2271 {
2272 return new Go_statement(call, location);
2273 }
2274
2275 // Class Defer_statement.
2276
2277 tree
2278 Defer_statement::do_get_tree(Translate_context* context)
2279 {
2280 source_location loc = this->location();
2281
2282 tree fn_tree;
2283 tree arg_tree;
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;
2287
2288 static tree defer_fndecl;
2289
2290 tree fn_arg_type = NULL_TREE;
2291 if (defer_fndecl == NULL_TREE)
2292 {
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);
2297 }
2298
2299 tree defer_stack = context->function()->func_value()->defer_stack(loc);
2300
2301 return Gogo::call_builtin(&defer_fndecl,
2302 loc,
2303 "__go_defer",
2304 3,
2305 void_type_node,
2306 ptr_type_node,
2307 defer_stack,
2308 fn_arg_type,
2309 fn_tree,
2310 ptr_type_node,
2311 arg_tree);
2312 }
2313
2314 // Make a defer statement.
2315
2316 Statement*
2317 Statement::make_defer_statement(Call_expression* call,
2318 source_location location)
2319 {
2320 return new Defer_statement(call, location);
2321 }
2322
2323 // Class Return_statement.
2324
2325 // Traverse assignments. We treat each return value as a top level
2326 // RHS in an expression.
2327
2328 bool
2329 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2330 {
2331 Expression_list* vals = this->vals_;
2332 if (vals != NULL)
2333 {
2334 for (Expression_list::iterator p = vals->begin();
2335 p != vals->end();
2336 ++p)
2337 tassign->value(&*p, true, true);
2338 }
2339 return true;
2340 }
2341
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.
2349
2350 Statement*
2351 Return_statement::do_lower(Gogo*, Block* enclosing)
2352 {
2353 if (this->vals_ == NULL)
2354 return this;
2355
2356 const Typed_identifier_list* results = this->results_;
2357 if (results == NULL || results->empty())
2358 return this;
2359
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)
2366 {
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));
2372 delete this->vals_;
2373 this->vals_ = vals;
2374 }
2375
2376 if (results->front().name().empty())
2377 return this;
2378
2379 if (results_count != this->vals_->size())
2380 {
2381 // Presumably an error which will be reported in check_types.
2382 return this;
2383 }
2384
2385 // Assign to named return values and then return them.
2386
2387 source_location loc = this->location();
2388 const Block* top = enclosing;
2389 while (top->enclosing() != NULL)
2390 top = top->enclosing();
2391
2392 const Bindings *bindings = top->bindings();
2393 Block* b = new Block(enclosing, loc);
2394
2395 Expression_list* lhs = new Expression_list();
2396 Expression_list* rhs = new Expression_list();
2397
2398 Expression_list::const_iterator pe = this->vals_->begin();
2399 int i = 1;
2400 for (Typed_identifier_list::const_iterator pr = results->begin();
2401 pr != results->end();
2402 ++pr, ++pe, ++i)
2403 {
2404 Named_object* rv = bindings->lookup_local(pr->name());
2405 if (rv == NULL || !rv->is_result_variable())
2406 {
2407 // Presumably an error.
2408 delete b;
2409 delete lhs;
2410 delete rhs;
2411 return this;
2412 }
2413
2414 Expression* e = *pe;
2415
2416 // Check types now so that we give a good error message. The
2417 // result type is known. We determine the expression type
2418 // early.
2419
2420 Type *rvtype = rv->result_var_value()->type();
2421 Type_context type_context(rvtype, false);
2422 e->determine_type(&type_context);
2423
2424 std::string reason;
2425 if (Type::are_assignable(rvtype, e->type(), &reason))
2426 {
2427 Expression* ve = Expression::make_var_reference(rv, e->location());
2428 lhs->push_back(ve);
2429 rhs->push_back(e);
2430 }
2431 else
2432 {
2433 if (reason.empty())
2434 error_at(e->location(), "incompatible type for return value %d", i);
2435 else
2436 error_at(e->location(),
2437 "incompatible type for return value %d (%s)",
2438 i, reason.c_str());
2439 }
2440 }
2441 gcc_assert(lhs->size() == rhs->size());
2442
2443 if (lhs->empty())
2444 ;
2445 else if (lhs->size() == 1)
2446 {
2447 b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
2448 loc));
2449 delete lhs;
2450 delete rhs;
2451 }
2452 else
2453 b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
2454
2455 b->add_statement(Statement::make_return_statement(this->results_, NULL,
2456 loc));
2457
2458 return Statement::make_block_statement(b, loc);
2459 }
2460
2461 // Determine types.
2462
2463 void
2464 Return_statement::do_determine_types()
2465 {
2466 if (this->vals_ == NULL)
2467 return;
2468 const Typed_identifier_list* results = this->results_;
2469
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();
2475 ++pe)
2476 {
2477 if (results == NULL || pt == results->end())
2478 (*pe)->determine_type_no_context();
2479 else
2480 {
2481 Type_context context(pt->type(), false);
2482 (*pe)->determine_type(&context);
2483 ++pt;
2484 }
2485 }
2486 }
2487
2488 // Check types.
2489
2490 void
2491 Return_statement::do_check_types(Gogo*)
2492 {
2493 if (this->vals_ == NULL)
2494 return;
2495
2496 const Typed_identifier_list* results = this->results_;
2497 if (results == NULL)
2498 {
2499 this->report_error(_("return with value in function "
2500 "with no return type"));
2501 return;
2502 }
2503
2504 int i = 1;
2505 Typed_identifier_list::const_iterator pt = results->begin();
2506 for (Expression_list::const_iterator pe = this->vals_->begin();
2507 pe != this->vals_->end();
2508 ++pe, ++pt, ++i)
2509 {
2510 if (pt == results->end())
2511 {
2512 this->report_error(_("too many values in return statement"));
2513 return;
2514 }
2515 std::string reason;
2516 if (!Type::are_assignable(pt->type(), (*pe)->type(), &reason))
2517 {
2518 if (reason.empty())
2519 error_at(this->location(),
2520 "incompatible type for return value %d",
2521 i);
2522 else
2523 error_at(this->location(),
2524 "incompatible type for return value %d (%s)",
2525 i, reason.c_str());
2526 this->set_is_error();
2527 }
2528 else if (pt->type()->is_error_type()
2529 || (*pe)->type()->is_error_type()
2530 || pt->type()->is_undefined()
2531 || (*pe)->type()->is_undefined())
2532 {
2533 // Make sure we get the error for an undefined type.
2534 pt->type()->base();
2535 (*pe)->type()->base();
2536 this->set_is_error();
2537 }
2538 }
2539
2540 if (pt != results->end())
2541 this->report_error(_("not enough values in return statement"));
2542 }
2543
2544 // Build a RETURN_EXPR tree.
2545
2546 tree
2547 Return_statement::do_get_tree(Translate_context* context)
2548 {
2549 Function* function = context->function()->func_value();
2550 tree fndecl = function->get_decl();
2551
2552 const Typed_identifier_list* results = this->results_;
2553
2554 if (this->vals_ == NULL)
2555 {
2556 tree stmt_list = NULL_TREE;
2557 tree retval = function->return_value(context->gogo(),
2558 context->function(),
2559 this->location(),
2560 &stmt_list);
2561 tree set;
2562 if (retval == NULL_TREE)
2563 set = NULL_TREE;
2564 else
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),
2568 &stmt_list);
2569 return stmt_list;
2570 }
2571 else if (this->vals_->size() == 1)
2572 {
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);
2586 }
2587 else
2588 {
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);
2597 field != NULL_TREE;
2598 ++pv, ++pr, field = DECL_CHAIN(field))
2599 {
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(),
2605 (*pv)->type(), val,
2606 this->location());
2607 tree set = build2(MODIFY_EXPR, void_type_node,
2608 build3(COMPONENT_REF, TREE_TYPE(field),
2609 retvar, field, NULL_TREE),
2610 val);
2611 SET_EXPR_LOCATION(set, this->location());
2612 append_to_statement_list(set, &stmt_list);
2613 }
2614 tree set = build2(MODIFY_EXPR, void_type_node, DECL_RESULT(fndecl),
2615 retvar);
2616 append_to_statement_list(this->build_stmt_1(RETURN_EXPR, set),
2617 &stmt_list);
2618 return stmt_list;
2619 }
2620 }
2621
2622 // Make a return statement.
2623
2624 Statement*
2625 Statement::make_return_statement(const Typed_identifier_list* results,
2626 Expression_list* vals,
2627 source_location location)
2628 {
2629 return new Return_statement(results, vals, location);
2630 }
2631
2632 // A break or continue statement.
2633
2634 class Bc_statement : public Statement
2635 {
2636 public:
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)
2640 { }
2641
2642 bool
2643 is_break() const
2644 { return this->is_break_; }
2645
2646 protected:
2647 int
2648 do_traverse(Traverse*)
2649 { return TRAVERSE_CONTINUE; }
2650
2651 bool
2652 do_may_fall_through() const
2653 { return false; }
2654
2655 tree
2656 do_get_tree(Translate_context*)
2657 { return this->label_->get_goto(this->location()); }
2658
2659 private:
2660 // The label that this branches to.
2661 Unnamed_label* label_;
2662 // True if this is "break", false if it is "continue".
2663 bool is_break_;
2664 };
2665
2666 // Make a break statement.
2667
2668 Statement*
2669 Statement::make_break_statement(Unnamed_label* label, source_location location)
2670 {
2671 return new Bc_statement(true, label, location);
2672 }
2673
2674 // Make a continue statement.
2675
2676 Statement*
2677 Statement::make_continue_statement(Unnamed_label* label,
2678 source_location location)
2679 {
2680 return new Bc_statement(false, label, location);
2681 }
2682
2683 // A goto statement.
2684
2685 class Goto_statement : public Statement
2686 {
2687 public:
2688 Goto_statement(Label* label, source_location location)
2689 : Statement(STATEMENT_GOTO, location),
2690 label_(label)
2691 { }
2692
2693 protected:
2694 int
2695 do_traverse(Traverse*)
2696 { return TRAVERSE_CONTINUE; }
2697
2698 void
2699 do_check_types(Gogo*);
2700
2701 bool
2702 do_may_fall_through() const
2703 { return false; }
2704
2705 tree
2706 do_get_tree(Translate_context*);
2707
2708 private:
2709 Label* label_;
2710 };
2711
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.
2714
2715 void
2716 Goto_statement::do_check_types(Gogo*)
2717 {
2718 if (!this->label_->is_defined())
2719 {
2720 error_at(this->location(), "reference to undefined label %qs",
2721 Gogo::message_name(this->label_->name()).c_str());
2722 this->set_is_error();
2723 }
2724 }
2725
2726 // Return the tree for the goto statement.
2727
2728 tree
2729 Goto_statement::do_get_tree(Translate_context*)
2730 {
2731 return this->build_stmt_1(GOTO_EXPR, this->label_->get_decl());
2732 }
2733
2734 // Make a goto statement.
2735
2736 Statement*
2737 Statement::make_goto_statement(Label* label, source_location location)
2738 {
2739 return new Goto_statement(label, location);
2740 }
2741
2742 // A goto statement to an unnamed label.
2743
2744 class Goto_unnamed_statement : public Statement
2745 {
2746 public:
2747 Goto_unnamed_statement(Unnamed_label* label, source_location location)
2748 : Statement(STATEMENT_GOTO_UNNAMED, location),
2749 label_(label)
2750 { }
2751
2752 protected:
2753 int
2754 do_traverse(Traverse*)
2755 { return TRAVERSE_CONTINUE; }
2756
2757 bool
2758 do_may_fall_through() const
2759 { return false; }
2760
2761 tree
2762 do_get_tree(Translate_context*)
2763 { return this->label_->get_goto(this->location()); }
2764
2765 private:
2766 Unnamed_label* label_;
2767 };
2768
2769 // Make a goto statement to an unnamed label.
2770
2771 Statement*
2772 Statement::make_goto_unnamed_statement(Unnamed_label* label,
2773 source_location location)
2774 {
2775 return new Goto_unnamed_statement(label, location);
2776 }
2777
2778 // Class Label_statement.
2779
2780 // Traversal.
2781
2782 int
2783 Label_statement::do_traverse(Traverse*)
2784 {
2785 return TRAVERSE_CONTINUE;
2786 }
2787
2788 // Return a tree defining this label.
2789
2790 tree
2791 Label_statement::do_get_tree(Translate_context*)
2792 {
2793 return this->build_stmt_1(LABEL_EXPR, this->label_->get_decl());
2794 }
2795
2796 // Make a label statement.
2797
2798 Statement*
2799 Statement::make_label_statement(Label* label, source_location location)
2800 {
2801 return new Label_statement(label, location);
2802 }
2803
2804 // An unnamed label statement.
2805
2806 class Unnamed_label_statement : public Statement
2807 {
2808 public:
2809 Unnamed_label_statement(Unnamed_label* label)
2810 : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
2811 label_(label)
2812 { }
2813
2814 protected:
2815 int
2816 do_traverse(Traverse*)
2817 { return TRAVERSE_CONTINUE; }
2818
2819 tree
2820 do_get_tree(Translate_context*)
2821 { return this->label_->get_definition(); }
2822
2823 private:
2824 // The label.
2825 Unnamed_label* label_;
2826 };
2827
2828 // Make an unnamed label statement.
2829
2830 Statement*
2831 Statement::make_unnamed_label_statement(Unnamed_label* label)
2832 {
2833 return new Unnamed_label_statement(label);
2834 }
2835
2836 // An if statement.
2837
2838 class If_statement : public Statement
2839 {
2840 public:
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)
2845 { }
2846
2847 protected:
2848 int
2849 do_traverse(Traverse*);
2850
2851 void
2852 do_determine_types();
2853
2854 void
2855 do_check_types(Gogo*);
2856
2857 bool
2858 do_may_fall_through() const;
2859
2860 tree
2861 do_get_tree(Translate_context*);
2862
2863 private:
2864 Expression* cond_;
2865 Block* then_block_;
2866 Block* else_block_;
2867 };
2868
2869 // Traversal.
2870
2871 int
2872 If_statement::do_traverse(Traverse* traverse)
2873 {
2874 if (this->cond_ != NULL)
2875 {
2876 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
2877 return TRAVERSE_EXIT;
2878 }
2879 if (this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
2880 return TRAVERSE_EXIT;
2881 if (this->else_block_ != NULL)
2882 {
2883 if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
2884 return TRAVERSE_EXIT;
2885 }
2886 return TRAVERSE_CONTINUE;
2887 }
2888
2889 void
2890 If_statement::do_determine_types()
2891 {
2892 if (this->cond_ != NULL)
2893 {
2894 Type_context context(Type::lookup_bool_type(), false);
2895 this->cond_->determine_type(&context);
2896 }
2897 this->then_block_->determine_types();
2898 if (this->else_block_ != NULL)
2899 this->else_block_->determine_types();
2900 }
2901
2902 // Check types.
2903
2904 void
2905 If_statement::do_check_types(Gogo*)
2906 {
2907 if (this->cond_ != NULL)
2908 {
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"));
2914 }
2915 }
2916
2917 // Whether the overall statement may fall through.
2918
2919 bool
2920 If_statement::do_may_fall_through() const
2921 {
2922 return (this->else_block_ == NULL
2923 || this->then_block_->may_fall_through()
2924 || this->else_block_->may_fall_through());
2925 }
2926
2927 // Get tree.
2928
2929 tree
2930 If_statement::do_get_tree(Translate_context* context)
2931 {
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
2935 ? boolean_true_node
2936 : this->cond_->get_tree(context)),
2937 this->then_block_->get_tree(context),
2938 (this->else_block_ == NULL
2939 ? NULL_TREE
2940 : this->else_block_->get_tree(context)));
2941 SET_EXPR_LOCATION(ret, this->location());
2942 return ret;
2943 }
2944
2945 // Make an if statement.
2946
2947 Statement*
2948 Statement::make_if_statement(Expression* cond, Block* then_block,
2949 Block* else_block, source_location location)
2950 {
2951 return new If_statement(cond, then_block, else_block, location);
2952 }
2953
2954 // Class Case_clauses::Case_clause.
2955
2956 // Traversal.
2957
2958 int
2959 Case_clauses::Case_clause::traverse(Traverse* traverse)
2960 {
2961 if (this->cases_ != NULL
2962 && (traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
2963 {
2964 if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
2965 return TRAVERSE_EXIT;
2966 }
2967 if (this->statements_ != NULL)
2968 {
2969 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
2970 return TRAVERSE_EXIT;
2971 }
2972 return TRAVERSE_CONTINUE;
2973 }
2974
2975 // Check whether all the case expressions are integer constants.
2976
2977 bool
2978 Case_clauses::Case_clause::is_constant() const
2979 {
2980 if (this->cases_ != NULL)
2981 {
2982 for (Expression_list::const_iterator p = this->cases_->begin();
2983 p != this->cases_->end();
2984 ++p)
2985 if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
2986 return false;
2987 }
2988 return true;
2989 }
2990
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.
2995
2996 void
2997 Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
2998 Unnamed_label* start_label,
2999 Unnamed_label* finish_label) const
3000 {
3001 source_location loc = this->location_;
3002 Unnamed_label* next_case_label;
3003 if (this->cases_ == NULL || this->cases_->empty())
3004 {
3005 gcc_assert(this->is_default_);
3006 next_case_label = NULL;
3007 }
3008 else
3009 {
3010 Expression* cond = NULL;
3011
3012 for (Expression_list::const_iterator p = this->cases_->begin();
3013 p != this->cases_->end();
3014 ++p)
3015 {
3016 Expression* this_cond;
3017 if (val_temp == NULL)
3018 this_cond = *p;
3019 else
3020 {
3021 Expression* ref = Expression::make_temporary_reference(val_temp,
3022 loc);
3023 this_cond = Expression::make_binary(OPERATOR_EQEQ, ref, *p, loc);
3024 }
3025
3026 if (cond == NULL)
3027 cond = this_cond;
3028 else
3029 cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
3030 }
3031
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,
3035 loc);
3036 then_block->add_statement(s);
3037
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);
3042 }
3043
3044 if (start_label != NULL)
3045 b->add_statement(Statement::make_unnamed_label_statement(start_label));
3046
3047 if (this->statements_ != NULL)
3048 b->add_statement(Statement::make_block_statement(this->statements_, loc));
3049
3050 Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
3051 b->add_statement(s);
3052
3053 if (next_case_label != NULL)
3054 b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
3055 }
3056
3057 // Determine types.
3058
3059 void
3060 Case_clauses::Case_clause::determine_types(Type* type)
3061 {
3062 if (this->cases_ != NULL)
3063 {
3064 Type_context case_context(type, false);
3065 for (Expression_list::iterator p = this->cases_->begin();
3066 p != this->cases_->end();
3067 ++p)
3068 (*p)->determine_type(&case_context);
3069 }
3070 if (this->statements_ != NULL)
3071 this->statements_->determine_types();
3072 }
3073
3074 // Check types. Returns false if there was an error.
3075
3076 bool
3077 Case_clauses::Case_clause::check_types(Type* type)
3078 {
3079 if (this->cases_ != NULL)
3080 {
3081 for (Expression_list::iterator p = this->cases_->begin();
3082 p != this->cases_->end();
3083 ++p)
3084 {
3085 if (!Type::are_assignable(type, (*p)->type(), NULL)
3086 && !Type::are_assignable((*p)->type(), type, NULL))
3087 {
3088 error_at((*p)->location(),
3089 "type mismatch between switch value and case clause");
3090 return false;
3091 }
3092 }
3093 }
3094 return true;
3095 }
3096
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.
3100
3101 bool
3102 Case_clauses::Case_clause::may_fall_through() const
3103 {
3104 if (this->statements_ == NULL)
3105 return true;
3106 return this->statements_->may_fall_through();
3107 }
3108
3109 // Build up the body of a SWITCH_EXPR.
3110
3111 void
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
3116 {
3117 if (this->cases_ != NULL)
3118 {
3119 for (Expression_list::const_iterator p = this->cases_->begin();
3120 p != this->cases_->end();
3121 ++p)
3122 {
3123 Type* itype;
3124 mpz_t ival;
3125 mpz_init(ival);
3126 if (!(*p)->integer_constant_value(true, ival, &itype))
3127 gcc_unreachable();
3128 gcc_assert(itype != NULL);
3129 tree type_tree = itype->get_tree(context->gogo());
3130 tree val = Expression::integer_constant_tree(ival, type_tree);
3131 mpz_clear(ival);
3132
3133 if (val != error_mark_node)
3134 {
3135 gcc_assert(TREE_CODE(val) == INTEGER_CST);
3136
3137 std::pair<Case_constants::iterator, bool> ins =
3138 case_constants->insert(val);
3139 if (!ins.second)
3140 {
3141 // Value was already present.
3142 warning_at(this->location_, 0,
3143 "duplicate case value will never match");
3144 continue;
3145 }
3146
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),
3150 stmt_list);
3151 }
3152 }
3153 }
3154
3155 if (this->is_default_)
3156 {
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),
3160 stmt_list);
3161 }
3162
3163 if (this->statements_ != NULL)
3164 {
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);
3168 }
3169
3170 if (!this->is_fallthrough_)
3171 append_to_statement_list(break_label->get_goto(this->location_), stmt_list);
3172 }
3173
3174 // Class Case_clauses.
3175
3176 // Traversal.
3177
3178 int
3179 Case_clauses::traverse(Traverse* traverse)
3180 {
3181 for (Clauses::iterator p = this->clauses_.begin();
3182 p != this->clauses_.end();
3183 ++p)
3184 {
3185 if (p->traverse(traverse) == TRAVERSE_EXIT)
3186 return TRAVERSE_EXIT;
3187 }
3188 return TRAVERSE_CONTINUE;
3189 }
3190
3191 // Check whether all the case expressions are constant.
3192
3193 bool
3194 Case_clauses::is_constant() const
3195 {
3196 for (Clauses::const_iterator p = this->clauses_.begin();
3197 p != this->clauses_.end();
3198 ++p)
3199 if (!p->is_constant())
3200 return false;
3201 return true;
3202 }
3203
3204 // Lower case clauses for a nonconstant switch.
3205
3206 void
3207 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3208 Unnamed_label* break_label) const
3209 {
3210 // The default case.
3211 const Case_clause* default_case = NULL;
3212
3213 // The label for the fallthrough of the previous case.
3214 Unnamed_label* last_fallthrough_label = NULL;
3215
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;
3219
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
3222 // falls through.
3223 Unnamed_label* default_finish_label = NULL;
3224
3225 for (Clauses::const_iterator p = this->clauses_.begin();
3226 p != this->clauses_.end();
3227 ++p)
3228 {
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;
3232
3233 // The label to jump to after the end of the statements for this
3234 // case.
3235 Unnamed_label* finish_label = break_label;
3236
3237 last_fallthrough_label = NULL;
3238 if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3239 {
3240 finish_label = new Unnamed_label(p->location());
3241 last_fallthrough_label = finish_label;
3242 }
3243
3244 if (!p->is_default())
3245 p->lower(b, val_temp, start_label, finish_label);
3246 else
3247 {
3248 // We have to move the default case to the end, so that we
3249 // only use it if all the other tests fail.
3250 default_case = &*p;
3251 default_start_label = start_label;
3252 default_finish_label = finish_label;
3253 }
3254 }
3255
3256 if (default_case != NULL)
3257 default_case->lower(b, val_temp, default_start_label,
3258 default_finish_label);
3259
3260 }
3261
3262 // Determine types.
3263
3264 void
3265 Case_clauses::determine_types(Type* type)
3266 {
3267 for (Clauses::iterator p = this->clauses_.begin();
3268 p != this->clauses_.end();
3269 ++p)
3270 p->determine_types(type);
3271 }
3272
3273 // Check types. Returns false if there was an error.
3274
3275 bool
3276 Case_clauses::check_types(Type* type)
3277 {
3278 bool ret = true;
3279 for (Clauses::iterator p = this->clauses_.begin();
3280 p != this->clauses_.end();
3281 ++p)
3282 {
3283 if (!p->check_types(type))
3284 ret = false;
3285 }
3286 return ret;
3287 }
3288
3289 // Return true if these clauses may fall through to the statements
3290 // following the switch statement.
3291
3292 bool
3293 Case_clauses::may_fall_through() const
3294 {
3295 bool found_default = false;
3296 for (Clauses::const_iterator p = this->clauses_.begin();
3297 p != this->clauses_.end();
3298 ++p)
3299 {
3300 if (p->may_fall_through() && !p->is_fallthrough())
3301 return true;
3302 if (p->is_default())
3303 found_default = true;
3304 }
3305 return !found_default;
3306 }
3307
3308 // Return a tree when all case expressions are constants.
3309
3310 tree
3311 Case_clauses::get_constant_tree(Translate_context* context,
3312 Unnamed_label* break_label) const
3313 {
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();
3318 ++p)
3319 p->get_constant_tree(context, break_label, &case_constants,
3320 &stmt_list);
3321 return stmt_list;
3322 }
3323
3324 // A constant switch statement. A Switch_statement is lowered to this
3325 // when all the cases are constants.
3326
3327 class Constant_switch_statement : public Statement
3328 {
3329 public:
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)
3335 { }
3336
3337 protected:
3338 int
3339 do_traverse(Traverse*);
3340
3341 void
3342 do_determine_types();
3343
3344 void
3345 do_check_types(Gogo*);
3346
3347 bool
3348 do_may_fall_through() const;
3349
3350 tree
3351 do_get_tree(Translate_context*);
3352
3353 private:
3354 // The value to switch on.
3355 Expression* val_;
3356 // The case clauses.
3357 Case_clauses* clauses_;
3358 // The break label, if needed.
3359 Unnamed_label* break_label_;
3360 };
3361
3362 // Traversal.
3363
3364 int
3365 Constant_switch_statement::do_traverse(Traverse* traverse)
3366 {
3367 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3368 return TRAVERSE_EXIT;
3369 return this->clauses_->traverse(traverse);
3370 }
3371
3372 // Determine types.
3373
3374 void
3375 Constant_switch_statement::do_determine_types()
3376 {
3377 this->val_->determine_type_no_context();
3378 this->clauses_->determine_types(this->val_->type());
3379 }
3380
3381 // Check types.
3382
3383 void
3384 Constant_switch_statement::do_check_types(Gogo*)
3385 {
3386 if (!this->clauses_->check_types(this->val_->type()))
3387 this->set_is_error();
3388 }
3389
3390 // Return whether this switch may fall through.
3391
3392 bool
3393 Constant_switch_statement::do_may_fall_through() const
3394 {
3395 if (this->clauses_ == NULL)
3396 return true;
3397
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)
3401 return true;
3402
3403 return this->clauses_->may_fall_through();
3404 }
3405
3406 // Convert to GENERIC.
3407
3408 tree
3409 Constant_switch_statement::do_get_tree(Translate_context* context)
3410 {
3411 tree switch_val_tree = this->val_->get_tree(context);
3412
3413 Unnamed_label* break_label = this->break_label_;
3414 if (break_label == NULL)
3415 break_label = new Unnamed_label(this->location());
3416
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),
3420 NULL_TREE);
3421 SET_EXPR_LOCATION(s, this->location());
3422 append_to_statement_list(s, &stmt_list);
3423
3424 append_to_statement_list(break_label->get_definition(), &stmt_list);
3425
3426 return stmt_list;
3427 }
3428
3429 // Class Switch_statement.
3430
3431 // Traversal.
3432
3433 int
3434 Switch_statement::do_traverse(Traverse* traverse)
3435 {
3436 if (this->val_ != NULL)
3437 {
3438 if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3439 return TRAVERSE_EXIT;
3440 }
3441 return this->clauses_->traverse(traverse);
3442 }
3443
3444 // Lower a Switch_statement to a Constant_switch_statement or a series
3445 // of if statements.
3446
3447 Statement*
3448 Switch_statement::do_lower(Gogo*, Block* enclosing)
3449 {
3450 source_location loc = this->location();
3451
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);
3456
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);
3463
3464 Block* b = new Block(enclosing, loc);
3465
3466 if (this->clauses_->empty())
3467 {
3468 Expression* val = this->val_;
3469 if (val == NULL)
3470 val = Expression::make_boolean(true, loc);
3471 return Statement::make_statement(val);
3472 }
3473
3474 Temporary_statement* val_temp;
3475 if (this->val_ == NULL)
3476 val_temp = NULL;
3477 else
3478 {
3479 // var val_temp VAL_TYPE = VAL
3480 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3481 b->add_statement(val_temp);
3482 }
3483
3484 this->clauses_->lower(b, val_temp, this->break_label());
3485
3486 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3487 b->add_statement(s);
3488
3489 return Statement::make_block_statement(b, loc);
3490 }
3491
3492 // Return the break label for this switch statement, creating it if
3493 // necessary.
3494
3495 Unnamed_label*
3496 Switch_statement::break_label()
3497 {
3498 if (this->break_label_ == NULL)
3499 this->break_label_ = new Unnamed_label(this->location());
3500 return this->break_label_;
3501 }
3502
3503 // Make a switch statement.
3504
3505 Switch_statement*
3506 Statement::make_switch_statement(Expression* val, source_location location)
3507 {
3508 return new Switch_statement(val, location);
3509 }
3510
3511 // Class Type_case_clauses::Type_case_clause.
3512
3513 // Traversal.
3514
3515 int
3516 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3517 {
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;
3526 }
3527
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
3532 // statements.
3533
3534 void
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
3539 {
3540 source_location loc = this->location_;
3541
3542 Unnamed_label* next_case_label = NULL;
3543 if (!this->is_default_)
3544 {
3545 Type* type = this->type_;
3546
3547 Expression* cond;
3548 // The language permits case nil, which is of course a constant
3549 // rather than a type. It will appear here as an invalid
3550 // forwarding type.
3551 if (type->is_nil_constant_as_type())
3552 {
3553 Expression* ref =
3554 Expression::make_temporary_reference(descriptor_temp, loc);
3555 cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3556 Expression::make_nil(loc),
3557 loc);
3558 }
3559 else
3560 {
3561 Expression* func;
3562 if (type->interface_type() == NULL)
3563 {
3564 // func ifacetypeeq(*descriptor, *descriptor) bool
3565 static Named_object* ifacetypeeq;
3566 if (ifacetypeeq == NULL)
3567 {
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,
3573 bloc));
3574 param_types->push_back(Typed_identifier("b", descriptor_type,
3575 bloc));
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,
3581 param_types,
3582 ret_types,
3583 bloc);
3584 ifacetypeeq =
3585 Named_object::make_function_declaration("ifacetypeeq", NULL,
3586 fntype, bloc);
3587 const char* n = "runtime.ifacetypeeq";
3588 ifacetypeeq->func_declaration_value()->set_asm_name(n);
3589 }
3590
3591 // ifacetypeeq(descriptor_temp, DESCRIPTOR)
3592 func = Expression::make_func_reference(ifacetypeeq, NULL, loc);
3593 }
3594 else
3595 {
3596 // func ifaceI2Tp(*descriptor, *descriptor) bool
3597 static Named_object* ifaceI2Tp;
3598 if (ifaceI2Tp == NULL)
3599 {
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,
3605 bloc));
3606 param_types->push_back(Typed_identifier("b", descriptor_type,
3607 bloc));
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,
3613 param_types,
3614 ret_types,
3615 bloc);
3616 ifaceI2Tp =
3617 Named_object::make_function_declaration("ifaceI2Tp", NULL,
3618 fntype, bloc);
3619 const char* n = "runtime.ifaceI2Tp";
3620 ifaceI2Tp->func_declaration_value()->set_asm_name(n);
3621 }
3622
3623 // ifaceI2Tp(descriptor_temp, DESCRIPTOR)
3624 func = Expression::make_func_reference(ifaceI2Tp, NULL, loc);
3625 }
3626 Expression_list* params = new Expression_list();
3627 params->push_back(Expression::make_type_descriptor(type, loc));
3628 Expression* ref =
3629 Expression::make_temporary_reference(descriptor_temp, loc);
3630 params->push_back(ref);
3631 cond = Expression::make_call(func, params, false, loc);
3632 }
3633
3634 Unnamed_label* dest;
3635 if (!this->is_fallthrough_)
3636 {
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);
3641 }
3642 else
3643 {
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;
3649 }
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);
3655 }
3656
3657 if (this->statements_ != NULL
3658 || (!this->is_fallthrough_
3659 && stmts_label != NULL
3660 && *stmts_label != NULL))
3661 {
3662 gcc_assert(!this->is_fallthrough_);
3663 if (stmts_label != NULL && *stmts_label != NULL)
3664 {
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;
3671 }
3672 if (this->statements_ != NULL)
3673 b->add_statement(Statement::make_block_statement(this->statements_,
3674 loc));
3675 }
3676
3677 if (this->is_fallthrough_)
3678 gcc_assert(next_case_label == NULL);
3679 else
3680 {
3681 source_location gloc = (this->statements_ == NULL
3682 ? loc
3683 : this->statements_->end_location());
3684 b->add_statement(Statement::make_goto_unnamed_statement(break_label,
3685 gloc));
3686 if (next_case_label != NULL)
3687 {
3688 Statement* s =
3689 Statement::make_unnamed_label_statement(next_case_label);
3690 b->add_statement(s);
3691 }
3692 }
3693 }
3694
3695 // Class Type_case_clauses.
3696
3697 // Traversal.
3698
3699 int
3700 Type_case_clauses::traverse(Traverse* traverse)
3701 {
3702 for (Type_clauses::iterator p = this->clauses_.begin();
3703 p != this->clauses_.end();
3704 ++p)
3705 {
3706 if (p->traverse(traverse) == TRAVERSE_EXIT)
3707 return TRAVERSE_EXIT;
3708 }
3709 return TRAVERSE_CONTINUE;
3710 }
3711
3712 // Check for duplicate types.
3713
3714 void
3715 Type_case_clauses::check_duplicates() const
3716 {
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();
3722 ++p)
3723 {
3724 Type* t = p->type();
3725 if (t == NULL)
3726 continue;
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);
3730 if (!ins.second)
3731 error_at(p->location(), "duplicate type in switch");
3732 }
3733 }
3734
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.
3738
3739 void
3740 Type_case_clauses::lower(Block* b, Temporary_statement* descriptor_temp,
3741 Unnamed_label* break_label) const
3742 {
3743 const Type_case_clause* default_case = NULL;
3744
3745 Unnamed_label* stmts_label = NULL;
3746 for (Type_clauses::const_iterator p = this->clauses_.begin();
3747 p != this->clauses_.end();
3748 ++p)
3749 {
3750 if (!p->is_default())
3751 p->lower(b, descriptor_temp, break_label, &stmts_label);
3752 else
3753 {
3754 // We are generating a series of tests, which means that we
3755 // need to move the default case to the end.
3756 default_case = &*p;
3757 }
3758 }
3759 gcc_assert(stmts_label == NULL);
3760
3761 if (default_case != NULL)
3762 default_case->lower(b, descriptor_temp, break_label, NULL);
3763 }
3764
3765 // Class Type_switch_statement.
3766
3767 // Traversal.
3768
3769 int
3770 Type_switch_statement::do_traverse(Traverse* traverse)
3771 {
3772 if (this->var_ == NULL)
3773 {
3774 if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
3775 return TRAVERSE_EXIT;
3776 }
3777 if (this->clauses_ != NULL)
3778 return this->clauses_->traverse(traverse);
3779 return TRAVERSE_CONTINUE;
3780 }
3781
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.
3787
3788 Statement*
3789 Type_switch_statement::do_lower(Gogo*, Block* enclosing)
3790 {
3791 const source_location loc = this->location();
3792
3793 if (this->clauses_ != NULL)
3794 this->clauses_->check_duplicates();
3795
3796 Block* b = new Block(enclosing, loc);
3797
3798 Type* val_type = (this->var_ != NULL
3799 ? this->var_->var_value()->type()
3800 : this->expr_->type());
3801
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);
3807
3808 if (val_type->interface_type() == NULL)
3809 {
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,
3814 loc);
3815 Expression* rhs = Expression::make_type_descriptor(val_type, loc);
3816 Statement* s = Statement::make_assignment(lhs, rhs, loc);
3817 b->add_statement(s);
3818 }
3819 else
3820 {
3821 const source_location bloc = BUILTINS_LOCATION;
3822
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,
3830 ret_types, bloc);
3831 bool is_empty = val_type->interface_type()->is_empty();
3832 const char* fnname = is_empty ? "efacetype" : "ifacetype";
3833 Named_object* fn =
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);
3839
3840 // descriptor_temp = ifacetype(val_temp)
3841 Expression* func = Expression::make_func_reference(fn, NULL, loc);
3842 Expression_list* params = new Expression_list();
3843 Expression* ref;
3844 if (this->var_ == NULL)
3845 ref = this->expr_;
3846 else
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,
3851 loc);
3852 Statement* s = Statement::make_assignment(lhs, call, loc);
3853 b->add_statement(s);
3854 }
3855
3856 if (this->clauses_ != NULL)
3857 this->clauses_->lower(b, descriptor_temp, this->break_label());
3858
3859 Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3860 b->add_statement(s);
3861
3862 return Statement::make_block_statement(b, loc);
3863 }
3864
3865 // Return the break label for this type switch statement, creating it
3866 // if necessary.
3867
3868 Unnamed_label*
3869 Type_switch_statement::break_label()
3870 {
3871 if (this->break_label_ == NULL)
3872 this->break_label_ = new Unnamed_label(this->location());
3873 return this->break_label_;
3874 }
3875
3876 // Make a type switch statement.
3877
3878 Type_switch_statement*
3879 Statement::make_type_switch_statement(Named_object* var, Expression* expr,
3880 source_location location)
3881 {
3882 return new Type_switch_statement(var, expr, location);
3883 }
3884
3885 // Class Select_clauses::Select_clause.
3886
3887 // Traversal.
3888
3889 int
3890 Select_clauses::Select_clause::traverse(Traverse* traverse)
3891 {
3892 if (!this->is_lowered_
3893 && (traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
3894 {
3895 if (this->channel_ != NULL)
3896 {
3897 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
3898 return TRAVERSE_EXIT;
3899 }
3900 if (this->val_ != NULL)
3901 {
3902 if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
3903 return TRAVERSE_EXIT;
3904 }
3905 }
3906 if (this->statements_ != NULL)
3907 {
3908 if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3909 return TRAVERSE_EXIT;
3910 }
3911 return TRAVERSE_CONTINUE;
3912 }
3913
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.
3917
3918 void
3919 Select_clauses::Select_clause::lower(Block* b)
3920 {
3921 if (this->is_default_)
3922 {
3923 gcc_assert(this->channel_ == NULL && this->val_ == NULL);
3924 this->is_lowered_ = true;
3925 return;
3926 }
3927
3928 source_location loc = this->location_;
3929
3930 // Evaluate the channel before the select statement.
3931 Temporary_statement* channel_temp = Statement::make_temporary(NULL,
3932 this->channel_,
3933 loc);
3934 b->add_statement(channel_temp);
3935 this->channel_ = Expression::make_temporary_reference(channel_temp, loc);
3936
3937 // If this is a send clause, evaluate the value to send before the
3938 // select statement.
3939 Temporary_statement* val_temp = NULL;
3940 if (this->is_send_)
3941 {
3942 val_temp = Statement::make_temporary(NULL, this->val_, loc);
3943 b->add_statement(val_temp);
3944 }
3945
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);
3949 if (this->is_send_)
3950 {
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));
3956 }
3957 else
3958 {
3959 Receive_expression* recv = Expression::make_receive(ref, loc);
3960 recv->set_for_select();
3961 if (this->val_ != NULL)
3962 {
3963 gcc_assert(this->var_ == NULL);
3964 init->add_statement(Statement::make_assignment(this->val_, recv,
3965 loc));
3966 }
3967 else if (this->var_ != NULL)
3968 {
3969 this->var_->var_value()->set_init(recv);
3970 this->var_->var_value()->clear_type_from_chan_element();
3971 }
3972 else
3973 {
3974 recv->discarding_value();
3975 init->add_statement(Statement::make_statement(recv));
3976 }
3977 }
3978
3979 if (this->statements_ != NULL)
3980 init->add_statement(Statement::make_block_statement(this->statements_,
3981 loc));
3982
3983 this->statements_ = init;
3984
3985 // Now all references should be handled through the statements, not
3986 // through here.
3987 this->is_lowered_ = true;
3988 this->val_ = NULL;
3989 this->var_ = NULL;
3990 }
3991
3992 // Determine types.
3993
3994 void
3995 Select_clauses::Select_clause::determine_types()
3996 {
3997 gcc_assert(this->is_lowered_);
3998 if (this->statements_ != NULL)
3999 this->statements_->determine_types();
4000 }
4001
4002 // Whether this clause may fall through to the statement which follows
4003 // the overall select statement.
4004
4005 bool
4006 Select_clauses::Select_clause::may_fall_through() const
4007 {
4008 if (this->statements_ == NULL)
4009 return true;
4010 return this->statements_->may_fall_through();
4011 }
4012
4013 // Return a tree for the statements to execute.
4014
4015 tree
4016 Select_clauses::Select_clause::get_statements_tree(Translate_context* context)
4017 {
4018 if (this->statements_ == NULL)
4019 return NULL_TREE;
4020 return this->statements_->get_tree(context);
4021 }
4022
4023 // Class Select_clauses.
4024
4025 // Traversal.
4026
4027 int
4028 Select_clauses::traverse(Traverse* traverse)
4029 {
4030 for (Clauses::iterator p = this->clauses_.begin();
4031 p != this->clauses_.end();
4032 ++p)
4033 {
4034 if (p->traverse(traverse) == TRAVERSE_EXIT)
4035 return TRAVERSE_EXIT;
4036 }
4037 return TRAVERSE_CONTINUE;
4038 }
4039
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.
4043
4044 void
4045 Select_clauses::lower(Block* b)
4046 {
4047 for (Clauses::iterator p = this->clauses_.begin();
4048 p != this->clauses_.end();
4049 ++p)
4050 p->lower(b);
4051 }
4052
4053 // Determine types.
4054
4055 void
4056 Select_clauses::determine_types()
4057 {
4058 for (Clauses::iterator p = this->clauses_.begin();
4059 p != this->clauses_.end();
4060 ++p)
4061 p->determine_types();
4062 }
4063
4064 // Return whether these select clauses fall through to the statement
4065 // following the overall select statement.
4066
4067 bool
4068 Select_clauses::may_fall_through() const
4069 {
4070 for (Clauses::const_iterator p = this->clauses_.begin();
4071 p != this->clauses_.end();
4072 ++p)
4073 if (p->may_fall_through())
4074 return true;
4075 return false;
4076 }
4077
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)
4081 //
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
4087 // the case to run.
4088
4089 // FIXME: This doesn't handle channels which send interface types
4090 // where the receiver has a static type which matches that interface.
4091
4092 tree
4093 Select_clauses::get_tree(Translate_context* context,
4094 Unnamed_label *break_label,
4095 source_location location)
4096 {
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,
4100 count);
4101 Select_clause* default_clause = NULL;
4102 tree final_stmt_list = NULL_TREE;
4103 tree channel_type_tree = NULL_TREE;
4104
4105 size_t i = 0;
4106 for (Clauses::iterator p = this->clauses_.begin();
4107 p != this->clauses_.end();
4108 ++p)
4109 {
4110 if (p->is_default())
4111 {
4112 default_clause = &*p;
4113 --count;
4114 continue;
4115 }
4116
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);
4121
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;
4125
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;
4129
4130 ++i;
4131 }
4132 gcc_assert(i == count);
4133
4134 if (i == 0 && default_clause != NULL)
4135 {
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),
4140 &stmt_list);
4141 append_to_statement_list(break_label->get_definition(), &stmt_list);
4142 return stmt_list;
4143 }
4144
4145 tree pointer_chan_type_tree = (channel_type_tree == NULL_TREE
4146 ? ptr_type_node
4147 : build_pointer_type(channel_type_tree));
4148 tree chans_arg;
4149 tree pointer_boolean_type_tree = build_pointer_type(boolean_type_node);
4150 tree is_sends_arg;
4151
4152 if (i == 0)
4153 {
4154 chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4155 null_pointer_node);
4156 is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4157 null_pointer_node);
4158 }
4159 else
4160 {
4161 tree index_type_tree = build_index_type(size_int(count - 1));
4162 tree chan_array_type_tree = build_array_type(channel_type_tree,
4163 index_type_tree);
4164 tree chan_constructor = build_constructor(chan_array_type_tree,
4165 chan_init);
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);
4174
4175 tree is_send_array_type_tree = build_array_type(boolean_type_node,
4176 index_type_tree);
4177 tree is_send_constructor = build_constructor(is_send_array_type_tree,
4178 is_send_init);
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);
4187
4188 chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4189 build_fold_addr_expr_loc(location,
4190 chan_var));
4191 is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4192 build_fold_addr_expr_loc(location,
4193 is_send_var));
4194 }
4195
4196 static tree select_fndecl;
4197 tree call = Gogo::call_builtin(&select_fndecl,
4198 location,
4199 "__go_select",
4200 4,
4201 sizetype,
4202 sizetype,
4203 size_int(count),
4204 boolean_type_node,
4205 (default_clause == NULL
4206 ? boolean_false_node
4207 : boolean_true_node),
4208 pointer_chan_type_tree,
4209 chans_arg,
4210 pointer_boolean_type_tree,
4211 is_sends_arg);
4212
4213 tree stmt_list = NULL_TREE;
4214
4215 if (default_clause != NULL)
4216 this->add_clause_tree(context, 0, default_clause, break_label, &stmt_list);
4217
4218 i = 1;
4219 for (Clauses::iterator p = this->clauses_.begin();
4220 p != this->clauses_.end();
4221 ++p)
4222 {
4223 if (!p->is_default())
4224 {
4225 this->add_clause_tree(context, i, &*p, break_label, &stmt_list);
4226 ++i;
4227 }
4228 }
4229
4230 append_to_statement_list(break_label->get_definition(), &stmt_list);
4231
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);
4235
4236 return final_stmt_list;
4237 }
4238
4239 // Add the tree for CLAUSE to STMT_LIST.
4240
4241 void
4242 Select_clauses::add_clause_tree(Translate_context* context, int case_index,
4243 Select_clause* clause,
4244 Unnamed_label* bottom_label, tree* stmt_list)
4245 {
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),
4249 NULL_TREE, label),
4250 stmt_list);
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);
4256 }
4257
4258 // Class Select_statement.
4259
4260 // Return the break label for this switch statement, creating it if
4261 // necessary.
4262
4263 Unnamed_label*
4264 Select_statement::break_label()
4265 {
4266 if (this->break_label_ == NULL)
4267 this->break_label_ = new Unnamed_label(this->location());
4268 return this->break_label_;
4269 }
4270
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.
4275
4276 Statement*
4277 Select_statement::do_lower(Gogo*, Block* enclosing)
4278 {
4279 if (this->is_lowered_)
4280 return this;
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());
4286 }
4287
4288 // Return the tree for a select statement.
4289
4290 tree
4291 Select_statement::do_get_tree(Translate_context* context)
4292 {
4293 return this->clauses_->get_tree(context, this->break_label(),
4294 this->location());
4295 }
4296
4297 // Make a select statement.
4298
4299 Select_statement*
4300 Statement::make_select_statement(source_location location)
4301 {
4302 return new Select_statement(location);
4303 }
4304
4305 // Class For_statement.
4306
4307 // Traversal.
4308
4309 int
4310 For_statement::do_traverse(Traverse* traverse)
4311 {
4312 if (this->init_ != NULL)
4313 {
4314 if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
4315 return TRAVERSE_EXIT;
4316 }
4317 if (this->cond_ != NULL)
4318 {
4319 if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
4320 return TRAVERSE_EXIT;
4321 }
4322 if (this->post_ != NULL)
4323 {
4324 if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
4325 return TRAVERSE_EXIT;
4326 }
4327 return this->statements_->traverse(traverse);
4328 }
4329
4330 // Lower a For_statement into if statements and gotos. Getting rid of
4331 // complex statements make it easier to handle garbage collection.
4332
4333 Statement*
4334 For_statement::do_lower(Gogo*, Block* enclosing)
4335 {
4336 Statement* s;
4337 source_location loc = this->location();
4338
4339 Block* b = new Block(enclosing, this->location());
4340 if (this->init_ != NULL)
4341 {
4342 s = Statement::make_block_statement(this->init_,
4343 this->init_->start_location());
4344 b->add_statement(s);
4345 }
4346
4347 Unnamed_label* entry = NULL;
4348 if (this->cond_ != NULL)
4349 {
4350 entry = new Unnamed_label(this->location());
4351 b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
4352 }
4353
4354 Unnamed_label* top = new Unnamed_label(this->location());
4355 b->add_statement(Statement::make_unnamed_label_statement(top));
4356
4357 s = Statement::make_block_statement(this->statements_,
4358 this->statements_->start_location());
4359 b->add_statement(s);
4360
4361 source_location end_loc = this->statements_->end_location();
4362
4363 Unnamed_label* cont = this->continue_label_;
4364 if (cont != NULL)
4365 b->add_statement(Statement::make_unnamed_label_statement(cont));
4366
4367 if (this->post_ != NULL)
4368 {
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();
4373 }
4374
4375 if (this->cond_ == NULL)
4376 b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
4377 else
4378 {
4379 b->add_statement(Statement::make_unnamed_label_statement(entry));
4380
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);
4385
4386 s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
4387 b->add_statement(s);
4388 }
4389
4390 Unnamed_label* brk = this->break_label_;
4391 if (brk != NULL)
4392 b->add_statement(Statement::make_unnamed_label_statement(brk));
4393
4394 b->set_end_location(end_loc);
4395
4396 return Statement::make_block_statement(b, loc);
4397 }
4398
4399 // Return the break label, creating it if necessary.
4400
4401 Unnamed_label*
4402 For_statement::break_label()
4403 {
4404 if (this->break_label_ == NULL)
4405 this->break_label_ = new Unnamed_label(this->location());
4406 return this->break_label_;
4407 }
4408
4409 // Return the continue LABEL_EXPR.
4410
4411 Unnamed_label*
4412 For_statement::continue_label()
4413 {
4414 if (this->continue_label_ == NULL)
4415 this->continue_label_ = new Unnamed_label(this->location());
4416 return this->continue_label_;
4417 }
4418
4419 // Set the break and continue labels a for statement. This is used
4420 // when lowering a for range statement.
4421
4422 void
4423 For_statement::set_break_continue_labels(Unnamed_label* break_label,
4424 Unnamed_label* continue_label)
4425 {
4426 gcc_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
4427 this->break_label_ = break_label;
4428 this->continue_label_ = continue_label;
4429 }
4430
4431 // Make a for statement.
4432
4433 For_statement*
4434 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
4435 source_location location)
4436 {
4437 return new For_statement(init, cond, post, location);
4438 }
4439
4440 // Class For_range_statement.
4441
4442 // Traversal.
4443
4444 int
4445 For_range_statement::do_traverse(Traverse* traverse)
4446 {
4447 if (this->traverse_expression(traverse, &this->index_var_) == TRAVERSE_EXIT)
4448 return TRAVERSE_EXIT;
4449 if (this->value_var_ != NULL)
4450 {
4451 if (this->traverse_expression(traverse, &this->value_var_)
4452 == TRAVERSE_EXIT)
4453 return TRAVERSE_EXIT;
4454 }
4455 if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
4456 return TRAVERSE_EXIT;
4457 return this->statements_->traverse(traverse);
4458 }
4459
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
4462 // statements.
4463
4464 Statement*
4465 For_range_statement::do_lower(Gogo* gogo, Block* enclosing)
4466 {
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();
4472
4473 Type* index_type;
4474 Type* value_type = NULL;
4475 if (range_type->array_type() != NULL)
4476 {
4477 index_type = Type::lookup_integer_type("int");
4478 value_type = range_type->array_type()->element_type();
4479 }
4480 else if (range_type->is_string_type())
4481 {
4482 index_type = Type::lookup_integer_type("int");
4483 value_type = index_type;
4484 }
4485 else if (range_type->map_type() != NULL)
4486 {
4487 index_type = range_type->map_type()->key_type();
4488 value_type = range_type->map_type()->val_type();
4489 }
4490 else if (range_type->channel_type() != NULL)
4491 {
4492 index_type = range_type->channel_type()->element_type();
4493 if (this->value_var_ != NULL)
4494 {
4495 if (!this->value_var_->type()->is_error_type())
4496 this->report_error(_("too many variables for range clause "
4497 "with channel"));
4498 return Statement::make_error_statement(this->location());
4499 }
4500 }
4501 else
4502 {
4503 this->report_error(_("range clause must have "
4504 "array, slice, setring, map, or channel type"));
4505 return Statement::make_error_statement(this->location());
4506 }
4507
4508 source_location loc = this->location();
4509 Block* temp_block = new Block(enclosing, loc);
4510
4511 Named_object* range_object = NULL;
4512 Temporary_statement* range_temp = NULL;
4513 Var_expression* ve = this->range_->var_expression();
4514 if (ve != NULL)
4515 range_object = ve->named_object();
4516 else
4517 {
4518 range_temp = Statement::make_temporary(NULL, this->range_, loc);
4519 temp_block->add_statement(range_temp);
4520 }
4521
4522 Temporary_statement* index_temp = Statement::make_temporary(index_type,
4523 NULL, loc);
4524 temp_block->add_statement(index_temp);
4525
4526 Temporary_statement* value_temp = NULL;
4527 if (this->value_var_ != NULL)
4528 {
4529 value_temp = Statement::make_temporary(value_type, NULL, loc);
4530 temp_block->add_statement(value_temp);
4531 }
4532
4533 Block* body = new Block(temp_block, loc);
4534
4535 Block* init;
4536 Expression* cond;
4537 Block* iter_init;
4538 Block* post;
4539
4540 // Arrange to do a loop appropriate for the type. We will produce
4541 // for INIT ; COND ; POST {
4542 // ITER_INIT
4543 // INDEX = INDEX_TEMP
4544 // VALUE = VALUE_TEMP // If there is a value
4545 // original statements
4546 // }
4547
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,
4551 &post);
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,
4555 &post);
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,
4559 &post);
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,
4563 &post);
4564 else
4565 gcc_unreachable();
4566
4567 if (iter_init != NULL)
4568 body->add_statement(Statement::make_block_statement(iter_init, loc));
4569
4570 Statement* assign;
4571 Expression* index_ref = Expression::make_temporary_reference(index_temp, loc);
4572 if (this->value_var_ == NULL)
4573 {
4574 assign = Statement::make_assignment(this->index_var_, index_ref, loc);
4575 }
4576 else
4577 {
4578 Expression_list* lhs = new Expression_list();
4579 lhs->push_back(this->index_var_);
4580 lhs->push_back(this->value_var_);
4581
4582 Expression_list* rhs = new Expression_list();
4583 rhs->push_back(index_ref);
4584 rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4585
4586 assign = Statement::make_tuple_assignment(lhs, rhs, loc);
4587 }
4588 body->add_statement(assign);
4589
4590 body->add_statement(Statement::make_block_statement(this->statements_, loc));
4591
4592 body->set_end_location(this->statements_->end_location());
4593
4594 For_statement* loop = Statement::make_for_statement(init, cond, post,
4595 this->location());
4596 loop->add_statements(body);
4597 loop->set_break_continue_labels(this->break_label_, this->continue_label_);
4598
4599 temp_block->add_statement(loop);
4600
4601 return Statement::make_block_statement(temp_block, loc);
4602 }
4603
4604 // Return a reference to the range, which may be in RANGE_OBJECT or in
4605 // RANGE_TEMP.
4606
4607 Expression*
4608 For_range_statement::make_range_ref(Named_object* range_object,
4609 Temporary_statement* range_temp,
4610 source_location loc)
4611 {
4612 if (range_object != NULL)
4613 return Expression::make_var_reference(range_object, loc);
4614 else
4615 return Expression::make_temporary_reference(range_temp, loc);
4616 }
4617
4618 // Return a call to the predeclared function FUNCNAME passing a
4619 // reference to the temporary variable ARG.
4620
4621 Expression*
4622 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
4623 Expression* arg,
4624 source_location loc)
4625 {
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);
4632 }
4633
4634 // Lower a for range over an array or slice.
4635
4636 void
4637 For_range_statement::lower_range_array(Gogo* gogo,
4638 Block* enclosing,
4639 Block* body_block,
4640 Named_object* range_object,
4641 Temporary_statement* range_temp,
4642 Temporary_statement* index_temp,
4643 Temporary_statement* value_temp,
4644 Block** pinit,
4645 Expression** pcond,
4646 Block** piter_init,
4647 Block** ppost)
4648 {
4649 source_location loc = this->location();
4650
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
4657 // original body
4658 // }
4659
4660 // Set *PINIT to
4661 // var len_temp int
4662 // len_temp = len(range)
4663 // index_temp = 0
4664
4665 Block* init = new Block(enclosing, loc);
4666
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(),
4670 len_call, loc);
4671 init->add_statement(len_temp);
4672
4673 mpz_t zval;
4674 mpz_init_set_ui(zval, 0UL);
4675 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4676 mpz_clear(zval);
4677
4678 ref = Expression::make_temporary_reference(index_temp, loc);
4679 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4680 init->add_statement(s);
4681
4682 *pinit = init;
4683
4684 // Set *PCOND to
4685 // index_temp < len_temp
4686
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);
4690
4691 *pcond = lt;
4692
4693 // Set *PITER_INIT to
4694 // value_temp = range[index_temp]
4695
4696 Block* iter_init = NULL;
4697 if (value_temp != NULL)
4698 {
4699 iter_init = new Block(body_block, loc);
4700
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);
4704
4705 ref = Expression::make_temporary_reference(value_temp, loc);
4706 s = Statement::make_assignment(ref, index, loc);
4707
4708 iter_init->add_statement(s);
4709 }
4710 *piter_init = iter_init;
4711
4712 // Set *PPOST to
4713 // index_temp++
4714
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);
4719 *ppost = post;
4720 }
4721
4722 // Lower a for range over a string.
4723
4724 void
4725 For_range_statement::lower_range_string(Gogo* gogo,
4726 Block* enclosing,
4727 Block* body_block,
4728 Named_object* range_object,
4729 Temporary_statement* range_temp,
4730 Temporary_statement* index_temp,
4731 Temporary_statement* value_temp,
4732 Block** pinit,
4733 Expression** pcond,
4734 Block** piter_init,
4735 Block** ppost)
4736 {
4737 source_location loc = this->location();
4738
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 {
4744 // break
4745 // }
4746 // index = index_temp
4747 // value = value_temp
4748 // original body
4749 // }
4750
4751 // Set *PINIT to
4752 // var next_index_temp int
4753 // index_temp = 0
4754
4755 Block* init = new Block(enclosing, loc);
4756
4757 Temporary_statement* next_index_temp =
4758 Statement::make_temporary(index_temp->type(), NULL, loc);
4759 init->add_statement(next_index_temp);
4760
4761 mpz_t zval;
4762 mpz_init_set_ui(zval, 0UL);
4763 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4764
4765 Expression* ref = Expression::make_temporary_reference(index_temp, loc);
4766 Statement* s = Statement::make_assignment(ref, zexpr, loc);
4767
4768 init->add_statement(s);
4769 *pinit = init;
4770
4771 // The loop has no condition.
4772
4773 *pcond = NULL;
4774
4775 // Set *PITER_INIT to
4776 // next_index_temp = runtime.stringiter(range, index_temp)
4777 // or
4778 // next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
4779 // followed by
4780 // if next_index_temp == 0 {
4781 // break
4782 // }
4783
4784 Block* iter_init = new Block(body_block, loc);
4785
4786 Named_object* no;
4787 if (value_temp == NULL)
4788 {
4789 static Named_object* stringiter;
4790 if (stringiter == NULL)
4791 {
4792 source_location bloc = BUILTINS_LOCATION;
4793 Type* int_type = gogo->lookup_global("int")->type_value();
4794
4795 Typed_identifier_list* params = new Typed_identifier_list();
4796 params->push_back(Typed_identifier("s", Type::make_string_type(),
4797 bloc));
4798 params->push_back(Typed_identifier("k", int_type, bloc));
4799
4800 Typed_identifier_list* results = new Typed_identifier_list();
4801 results->push_back(Typed_identifier("", int_type, bloc));
4802
4803 Function_type* fntype = Type::make_function_type(NULL, params,
4804 results, bloc);
4805 stringiter = Named_object::make_function_declaration("stringiter",
4806 NULL, fntype,
4807 bloc);
4808 const char* n = "runtime.stringiter";
4809 stringiter->func_declaration_value()->set_asm_name(n);
4810 }
4811 no = stringiter;
4812 }
4813 else
4814 {
4815 static Named_object* stringiter2;
4816 if (stringiter2 == NULL)
4817 {
4818 source_location bloc = BUILTINS_LOCATION;
4819 Type* int_type = gogo->lookup_global("int")->type_value();
4820
4821 Typed_identifier_list* params = new Typed_identifier_list();
4822 params->push_back(Typed_identifier("s", Type::make_string_type(),
4823 bloc));
4824 params->push_back(Typed_identifier("k", int_type, bloc));
4825
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));
4829
4830 Function_type* fntype = Type::make_function_type(NULL, params,
4831 results, bloc);
4832 stringiter2 = Named_object::make_function_declaration("stringiter",
4833 NULL, fntype,
4834 bloc);
4835 const char* n = "runtime.stringiter2";
4836 stringiter2->func_declaration_value()->set_asm_name(n);
4837 }
4838 no = stringiter2;
4839 }
4840
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);
4846
4847 if (value_temp == NULL)
4848 {
4849 ref = Expression::make_temporary_reference(next_index_temp, loc);
4850 s = Statement::make_assignment(ref, call, loc);
4851 }
4852 else
4853 {
4854 Expression_list* lhs = new Expression_list();
4855 lhs->push_back(Expression::make_temporary_reference(next_index_temp,
4856 loc));
4857 lhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4858
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));
4862
4863 s = Statement::make_tuple_assignment(lhs, rhs, loc);
4864 }
4865 iter_init->add_statement(s);
4866
4867 ref = Expression::make_temporary_reference(next_index_temp, loc);
4868 zexpr = Expression::make_integer(&zval, NULL, loc);
4869 mpz_clear(zval);
4870 Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
4871
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);
4875
4876 s = Statement::make_if_statement(equals, then_block, NULL, loc);
4877 iter_init->add_statement(s);
4878
4879 *piter_init = iter_init;
4880
4881 // Set *PPOST to
4882 // index_temp = next_index_temp
4883
4884 Block* post = new Block(enclosing, loc);
4885
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);
4889
4890 post->add_statement(s);
4891 *ppost = post;
4892 }
4893
4894 // Lower a for range over a map.
4895
4896 void
4897 For_range_statement::lower_range_map(Gogo* gogo,
4898 Block* enclosing,
4899 Block* body_block,
4900 Named_object* range_object,
4901 Temporary_statement* range_temp,
4902 Temporary_statement* index_temp,
4903 Temporary_statement* value_temp,
4904 Block** pinit,
4905 Expression** pcond,
4906 Block** piter_init,
4907 Block** ppost)
4908 {
4909 source_location loc = this->location();
4910
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.
4914
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
4921 // original body
4922 // }
4923
4924 // Set *PINIT to
4925 // var hiter map_iteration_struct
4926 // runtime.mapiterinit(range, &hiter)
4927
4928 Block* init = new Block(enclosing, loc);
4929
4930 const unsigned long map_iteration_size = 4;
4931
4932 mpz_t ival;
4933 mpz_init_set_ui(ival, map_iteration_size);
4934 Expression* iexpr = Expression::make_integer(&ival, NULL, loc);
4935 mpz_clear(ival);
4936
4937 Type* byte_type = gogo->lookup_global("byte")->type_value();
4938 Type* ptr_type = Type::make_pointer_type(byte_type);
4939
4940 Type* map_iteration_type = Type::make_array_type(ptr_type, iexpr);
4941 Type* map_iteration_ptr = Type::make_pointer_type(map_iteration_type);
4942
4943 Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
4944 NULL, loc);
4945 init->add_statement(hiter);
4946
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,
4952 bloc);
4953
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);
4958
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));
4966
4967 *pinit = init;
4968
4969 // Set *PCOND to
4970 // hiter[0] != nil
4971
4972 ref = Expression::make_temporary_reference(hiter, loc);
4973
4974 mpz_t zval;
4975 mpz_init_set_ui(zval, 0UL);
4976 Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4977 mpz_clear(zval);
4978
4979 Expression* index = Expression::make_index(ref, zexpr, NULL, loc);
4980
4981 Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
4982 Expression::make_nil(loc),
4983 loc);
4984
4985 *pcond = ne;
4986
4987 // Set *PITER_INIT to
4988 // mapiter1(hiter, &index_temp)
4989 // or
4990 // mapiter2(hiter, &index_temp, &value_temp)
4991
4992 Block* iter_init = new Block(body_block, loc);
4993
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)
4999 {
5000 Type* pval_type = Type::make_pointer_type(value_temp->type());
5001 param_types->push_back(Typed_identifier("val", pval_type, bloc));
5002 }
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,
5006 fntype, bloc);
5007 n = value_temp == NULL ? "runtime.mapiter1" : "runtime.mapiter2";
5008 mapiter->func_declaration_value()->set_asm_name(n);
5009
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)
5017 {
5018 ref = Expression::make_temporary_reference(value_temp, loc);
5019 params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5020 }
5021 call = Expression::make_call(func, params, false, loc);
5022 iter_init->add_statement(Statement::make_statement(call));
5023
5024 *piter_init = iter_init;
5025
5026 // Set *PPOST to
5027 // mapiternext(&hiter)
5028
5029 Block* post = new Block(enclosing, loc);
5030
5031 static Named_object* mapiternext;
5032 if (mapiternext == NULL)
5033 {
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",
5038 NULL, fntype,
5039 bloc);
5040 const char* n = "runtime.mapiternext";
5041 mapiternext->func_declaration_value()->set_asm_name(n);
5042 }
5043
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));
5050
5051 *ppost = post;
5052 }
5053
5054 // Lower a for range over a channel.
5055
5056 void
5057 For_range_statement::lower_range_channel(Gogo* gogo,
5058 Block*,
5059 Block* body_block,
5060 Named_object* range_object,
5061 Temporary_statement* range_temp,
5062 Temporary_statement* index_temp,
5063 Temporary_statement* value_temp,
5064 Block** pinit,
5065 Expression** pcond,
5066 Block** piter_init,
5067 Block** ppost)
5068 {
5069 gcc_assert(value_temp == NULL);
5070
5071 source_location loc = this->location();
5072
5073 // The loop we generate:
5074 // for {
5075 // index_temp = <-range
5076 // if closed(range) {
5077 // break
5078 // }
5079 // index = index_temp
5080 // value = value_temp
5081 // original body
5082 // }
5083
5084 // We have no initialization code, no condition, and no post code.
5085
5086 *pinit = NULL;
5087 *pcond = NULL;
5088 *ppost = NULL;
5089
5090 // Set *PITER_INIT to
5091 // index_temp = <-range
5092 // if closed(range) {
5093 // break
5094 // }
5095
5096 Block* iter_init = new Block(body_block, loc);
5097
5098 Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5099 Expression* cond = this->call_builtin(gogo, "closed", ref, loc);
5100
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);
5106
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);
5110
5111 s = Statement::make_if_statement(cond, then_block, NULL, loc);
5112 iter_init->add_statement(s);
5113
5114 *piter_init = iter_init;
5115 }
5116
5117 // Return the break LABEL_EXPR.
5118
5119 Unnamed_label*
5120 For_range_statement::break_label()
5121 {
5122 if (this->break_label_ == NULL)
5123 this->break_label_ = new Unnamed_label(this->location());
5124 return this->break_label_;
5125 }
5126
5127 // Return the continue LABEL_EXPR.
5128
5129 Unnamed_label*
5130 For_range_statement::continue_label()
5131 {
5132 if (this->continue_label_ == NULL)
5133 this->continue_label_ = new Unnamed_label(this->location());
5134 return this->continue_label_;
5135 }
5136
5137 // Make a for statement with a range clause.
5138
5139 For_range_statement*
5140 Statement::make_for_range_statement(Expression* index_var,
5141 Expression* value_var,
5142 Expression* range,
5143 source_location location)
5144 {
5145 return new For_range_statement(index_var, value_var, range, location);
5146 }