OSDN Git Service

Avoid crash when selecting on non-channel.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / statements.cc
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 // Return the tree for the temporary variable.
303
304 tree
305 Temporary_statement::get_decl() const
306 {
307   if (this->decl_ == NULL)
308     {
309       gcc_assert(saw_errors());
310       return error_mark_node;
311     }
312   return this->decl_;
313 }
314
315 // Traversal.
316
317 int
318 Temporary_statement::do_traverse(Traverse* traverse)
319 {
320   if (this->init_ == NULL)
321     return TRAVERSE_CONTINUE;
322   else
323     return this->traverse_expression(traverse, &this->init_);
324 }
325
326 // Traverse assignments.
327
328 bool
329 Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
330 {
331   if (this->init_ == NULL)
332     return false;
333   tassign->value(&this->init_, true, true);
334   return true;
335 }
336
337 // Determine types.
338
339 void
340 Temporary_statement::do_determine_types()
341 {
342   if (this->init_ != NULL)
343     {
344       if (this->type_ == NULL)
345         this->init_->determine_type_no_context();
346       else
347         {
348           Type_context context(this->type_, false);
349           this->init_->determine_type(&context);
350         }
351     }
352
353   if (this->type_ == NULL)
354     this->type_ = this->init_->type();
355
356   if (this->type_->is_abstract())
357     this->type_ = this->type_->make_non_abstract_type();
358 }
359
360 // Check types.
361
362 void
363 Temporary_statement::do_check_types(Gogo*)
364 {
365   if (this->type_ != NULL && this->init_ != NULL)
366     {
367       std::string reason;
368       if (!Type::are_assignable(this->type_, this->init_->type(), &reason))
369         {
370           if (reason.empty())
371             error_at(this->location(), "incompatible types in assignment");
372           else
373             error_at(this->location(), "incompatible types in assignment (%s)",
374                      reason.c_str());
375           this->set_is_error();
376         }
377     }
378 }
379
380 // Return a tree.
381
382 tree
383 Temporary_statement::do_get_tree(Translate_context* context)
384 {
385   gcc_assert(this->decl_ == NULL_TREE);
386   tree type_tree = this->type()->get_tree(context->gogo());
387   if (type_tree == error_mark_node)
388     {
389       this->decl_ = error_mark_node;
390       return error_mark_node;
391     }
392   // We can only use create_tmp_var if the type is not addressable.
393   if (!TREE_ADDRESSABLE(type_tree))
394     {
395       this->decl_ = create_tmp_var(type_tree, "GOTMP");
396       DECL_SOURCE_LOCATION(this->decl_) = this->location();
397     }
398   else
399     {
400       gcc_assert(context->function() != NULL && context->block() != NULL);
401       tree decl = build_decl(this->location(), VAR_DECL,
402                              create_tmp_var_name("GOTMP"),
403                              type_tree);
404       DECL_ARTIFICIAL(decl) = 1;
405       DECL_IGNORED_P(decl) = 1;
406       TREE_USED(decl) = 1;
407       gcc_assert(current_function_decl != NULL_TREE);
408       DECL_CONTEXT(decl) = current_function_decl;
409
410       // We have to add this variable to the block so that it winds up
411       // in a BIND_EXPR.
412       tree block_tree = context->block_tree();
413       gcc_assert(block_tree != NULL_TREE);
414       DECL_CHAIN(decl) = BLOCK_VARS(block_tree);
415       BLOCK_VARS(block_tree) = decl;
416
417       this->decl_ = decl;
418     }
419   if (this->init_ != NULL)
420     DECL_INITIAL(this->decl_) =
421       Expression::convert_for_assignment(context, this->type(),
422                                          this->init_->type(),
423                                          this->init_->get_tree(context),
424                                          this->location());
425   if (this->is_address_taken_)
426     TREE_ADDRESSABLE(this->decl_) = 1;
427   return this->build_stmt_1(DECL_EXPR, this->decl_);
428 }
429
430 // Make and initialize a temporary variable in BLOCK.
431
432 Temporary_statement*
433 Statement::make_temporary(Type* type, Expression* init,
434                           source_location location)
435 {
436   return new Temporary_statement(type, init, location);
437 }
438
439 // An assignment statement.
440
441 class Assignment_statement : public Statement
442 {
443  public:
444   Assignment_statement(Expression* lhs, Expression* rhs,
445                        source_location location)
446     : Statement(STATEMENT_ASSIGNMENT, location),
447       lhs_(lhs), rhs_(rhs)
448   { }
449
450  protected:
451   int
452   do_traverse(Traverse* traverse);
453
454   bool
455   do_traverse_assignments(Traverse_assignments*);
456
457   void
458   do_determine_types();
459
460   void
461   do_check_types(Gogo*);
462
463   tree
464   do_get_tree(Translate_context*);
465
466  private:
467   // Left hand side--the lvalue.
468   Expression* lhs_;
469   // Right hand side--the rvalue.
470   Expression* rhs_;
471 };
472
473 // Traversal.
474
475 int
476 Assignment_statement::do_traverse(Traverse* traverse)
477 {
478   if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
479     return TRAVERSE_EXIT;
480   return this->traverse_expression(traverse, &this->rhs_);
481 }
482
483 bool
484 Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
485 {
486   tassign->assignment(&this->lhs_, &this->rhs_);
487   return true;
488 }
489
490 // Set types for the assignment.
491
492 void
493 Assignment_statement::do_determine_types()
494 {
495   this->lhs_->determine_type_no_context();
496   Type_context context(this->lhs_->type(), false);
497   this->rhs_->determine_type(&context);
498 }
499
500 // Check types for an assignment.
501
502 void
503 Assignment_statement::do_check_types(Gogo*)
504 {
505   // The left hand side must be either addressable, a map index
506   // expression, or the blank identifier.
507   if (!this->lhs_->is_addressable()
508       && this->lhs_->map_index_expression() == NULL
509       && !this->lhs_->is_sink_expression())
510     {
511       if (!this->lhs_->type()->is_error_type())
512         this->report_error(_("invalid left hand side of assignment"));
513       return;
514     }
515
516   Type* lhs_type = this->lhs_->type();
517   Type* rhs_type = this->rhs_->type();
518   std::string reason;
519   if (!Type::are_assignable(lhs_type, rhs_type, &reason))
520     {
521       if (reason.empty())
522         error_at(this->location(), "incompatible types in assignment");
523       else
524         error_at(this->location(), "incompatible types in assignment (%s)",
525                  reason.c_str());
526       this->set_is_error();
527     }
528
529   if (lhs_type->is_error_type()
530       || rhs_type->is_error_type()
531       || lhs_type->is_undefined()
532       || rhs_type->is_undefined())
533     {
534       // Make sure we get the error for an undefined type.
535       lhs_type->base();
536       rhs_type->base();
537       this->set_is_error();
538     }
539 }
540
541 // Build a tree for an assignment statement.
542
543 tree
544 Assignment_statement::do_get_tree(Translate_context* context)
545 {
546   tree rhs_tree = this->rhs_->get_tree(context);
547
548   if (this->lhs_->is_sink_expression())
549     return rhs_tree;
550
551   tree lhs_tree = this->lhs_->get_tree(context);
552
553   if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
554     return error_mark_node;
555
556   rhs_tree = Expression::convert_for_assignment(context, this->lhs_->type(),
557                                                 this->rhs_->type(), rhs_tree,
558                                                 this->location());
559   if (rhs_tree == error_mark_node)
560     return error_mark_node;
561
562   return fold_build2_loc(this->location(), MODIFY_EXPR, void_type_node,
563                          lhs_tree, rhs_tree);
564 }
565
566 // Make an assignment statement.
567
568 Statement*
569 Statement::make_assignment(Expression* lhs, Expression* rhs,
570                            source_location location)
571 {
572   return new Assignment_statement(lhs, rhs, location);
573 }
574
575 // The Move_ordered_evals class is used to find any subexpressions of
576 // an expression that have an evaluation order dependency.  It creates
577 // temporary variables to hold them.
578
579 class Move_ordered_evals : public Traverse
580 {
581  public:
582   Move_ordered_evals(Block* block)
583     : Traverse(traverse_expressions),
584       block_(block)
585   { }
586
587  protected:
588   int
589   expression(Expression**);
590
591  private:
592   // The block where new temporary variables should be added.
593   Block* block_;
594 };
595
596 int
597 Move_ordered_evals::expression(Expression** pexpr)
598 {
599   // We have to look at subexpressions first.
600   if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
601     return TRAVERSE_EXIT;
602   if ((*pexpr)->must_eval_in_order())
603     {
604       source_location loc = (*pexpr)->location();
605       Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
606       this->block_->add_statement(temp);
607       *pexpr = Expression::make_temporary_reference(temp, loc);
608     }
609   return TRAVERSE_SKIP_COMPONENTS;
610 }
611
612 // An assignment operation statement.
613
614 class Assignment_operation_statement : public Statement
615 {
616  public:
617   Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
618                                  source_location location)
619     : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
620       op_(op), lhs_(lhs), rhs_(rhs)
621   { }
622
623  protected:
624   int
625   do_traverse(Traverse*);
626
627   bool
628   do_traverse_assignments(Traverse_assignments*)
629   { gcc_unreachable(); }
630
631   Statement*
632   do_lower(Gogo*, Block*);
633
634   tree
635   do_get_tree(Translate_context*)
636   { gcc_unreachable(); }
637
638  private:
639   // The operator (OPERATOR_PLUSEQ, etc.).
640   Operator op_;
641   // Left hand side.
642   Expression* lhs_;
643   // Right hand side.
644   Expression* rhs_;
645 };
646
647 // Traversal.
648
649 int
650 Assignment_operation_statement::do_traverse(Traverse* traverse)
651 {
652   if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
653     return TRAVERSE_EXIT;
654   return this->traverse_expression(traverse, &this->rhs_);
655 }
656
657 // Lower an assignment operation statement to a regular assignment
658 // statement.
659
660 Statement*
661 Assignment_operation_statement::do_lower(Gogo*, Block* enclosing)
662 {
663   source_location loc = this->location();
664
665   // We have to evaluate the left hand side expression only once.  We
666   // do this by moving out any expression with side effects.
667   Block* b = new Block(enclosing, loc);
668   Move_ordered_evals moe(b);
669   this->lhs_->traverse_subexpressions(&moe);
670
671   Expression* lval = this->lhs_->copy();
672
673   Operator op;
674   switch (this->op_)
675     {
676     case OPERATOR_PLUSEQ:
677       op = OPERATOR_PLUS;
678       break;
679     case OPERATOR_MINUSEQ:
680       op = OPERATOR_MINUS;
681       break;
682     case OPERATOR_OREQ:
683       op = OPERATOR_OR;
684       break;
685     case OPERATOR_XOREQ:
686       op = OPERATOR_XOR;
687       break;
688     case OPERATOR_MULTEQ:
689       op = OPERATOR_MULT;
690       break;
691     case OPERATOR_DIVEQ:
692       op = OPERATOR_DIV;
693       break;
694     case OPERATOR_MODEQ:
695       op = OPERATOR_MOD;
696       break;
697     case OPERATOR_LSHIFTEQ:
698       op = OPERATOR_LSHIFT;
699       break;
700     case OPERATOR_RSHIFTEQ:
701       op = OPERATOR_RSHIFT;
702       break;
703     case OPERATOR_ANDEQ:
704       op = OPERATOR_AND;
705       break;
706     case OPERATOR_BITCLEAREQ:
707       op = OPERATOR_BITCLEAR;
708       break;
709     default:
710       gcc_unreachable();
711     }
712
713   Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
714   Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
715   if (b->statements()->empty())
716     {
717       delete b;
718       return s;
719     }
720   else
721     {
722       b->add_statement(s);
723       return Statement::make_block_statement(b, loc);
724     }
725 }
726
727 // Make an assignment operation statement.
728
729 Statement*
730 Statement::make_assignment_operation(Operator op, Expression* lhs,
731                                      Expression* rhs, source_location location)
732 {
733   return new Assignment_operation_statement(op, lhs, rhs, location);
734 }
735
736 // A tuple assignment statement.  This differs from an assignment
737 // statement in that the right-hand-side expressions are evaluated in
738 // parallel.
739
740 class Tuple_assignment_statement : public Statement
741 {
742  public:
743   Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
744                              source_location location)
745     : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
746       lhs_(lhs), rhs_(rhs)
747   { }
748
749  protected:
750   int
751   do_traverse(Traverse* traverse);
752
753   bool
754   do_traverse_assignments(Traverse_assignments*)
755   { gcc_unreachable(); }
756
757   Statement*
758   do_lower(Gogo*, Block*);
759
760   tree
761   do_get_tree(Translate_context*)
762   { gcc_unreachable(); }
763
764  private:
765   // Left hand side--a list of lvalues.
766   Expression_list* lhs_;
767   // Right hand side--a list of rvalues.
768   Expression_list* rhs_;
769 };
770
771 // Traversal.
772
773 int
774 Tuple_assignment_statement::do_traverse(Traverse* traverse)
775 {
776   if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
777     return TRAVERSE_EXIT;
778   return this->traverse_expression_list(traverse, this->rhs_);
779 }
780
781 // Lower a tuple assignment.  We use temporary variables to split it
782 // up into a set of single assignments.
783
784 Statement*
785 Tuple_assignment_statement::do_lower(Gogo*, Block* enclosing)
786 {
787   source_location loc = this->location();
788
789   Block* b = new Block(enclosing, loc);
790   
791   // First move out any subexpressions on the left hand side.  The
792   // right hand side will be evaluated in the required order anyhow.
793   Move_ordered_evals moe(b);
794   for (Expression_list::const_iterator plhs = this->lhs_->begin();
795        plhs != this->lhs_->end();
796        ++plhs)
797     (*plhs)->traverse_subexpressions(&moe);
798
799   std::vector<Temporary_statement*> temps;
800   temps.reserve(this->lhs_->size());
801
802   Expression_list::const_iterator prhs = this->rhs_->begin();
803   for (Expression_list::const_iterator plhs = this->lhs_->begin();
804        plhs != this->lhs_->end();
805        ++plhs, ++prhs)
806     {
807       gcc_assert(prhs != this->rhs_->end());
808
809       if ((*plhs)->is_error_expression()
810           || (*plhs)->type()->is_error_type()
811           || (*prhs)->is_error_expression()
812           || (*prhs)->type()->is_error_type())
813         continue;
814
815       if ((*plhs)->is_sink_expression())
816         {
817           b->add_statement(Statement::make_statement(*prhs));
818           continue;
819         }
820
821       Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
822                                                             *prhs, loc);
823       b->add_statement(temp);
824       temps.push_back(temp);
825
826     }
827   gcc_assert(prhs == this->rhs_->end());
828
829   prhs = this->rhs_->begin();
830   std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
831   for (Expression_list::const_iterator plhs = this->lhs_->begin();
832        plhs != this->lhs_->end();
833        ++plhs, ++prhs)
834     {
835       if ((*plhs)->is_error_expression()
836           || (*plhs)->type()->is_error_type()
837           || (*prhs)->is_error_expression()
838           || (*prhs)->type()->is_error_type())
839         continue;
840
841       if ((*plhs)->is_sink_expression())
842         continue;
843
844       Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
845       Statement* s = Statement::make_assignment(*plhs, ref, loc);
846       b->add_statement(s);
847       ++ptemp;
848     }
849   gcc_assert(ptemp == temps.end());
850
851   return Statement::make_block_statement(b, loc);
852 }
853
854 // Make a tuple assignment statement.
855
856 Statement*
857 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
858                                  source_location location)
859 {
860   return new Tuple_assignment_statement(lhs, rhs, location);
861 }
862
863 // A tuple assignment from a map index expression.
864 //   v, ok = m[k]
865
866 class Tuple_map_assignment_statement : public Statement
867 {
868 public:
869   Tuple_map_assignment_statement(Expression* val, Expression* present,
870                                  Expression* map_index,
871                                  source_location location)
872     : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
873       val_(val), present_(present), map_index_(map_index)
874   { }
875
876  protected:
877   int
878   do_traverse(Traverse* traverse);
879
880   bool
881   do_traverse_assignments(Traverse_assignments*)
882   { gcc_unreachable(); }
883
884   Statement*
885   do_lower(Gogo*, Block*);
886
887   tree
888   do_get_tree(Translate_context*)
889   { gcc_unreachable(); }
890
891  private:
892   // Lvalue which receives the value from the map.
893   Expression* val_;
894   // Lvalue which receives whether the key value was present.
895   Expression* present_;
896   // The map index expression.
897   Expression* map_index_;
898 };
899
900 // Traversal.
901
902 int
903 Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
904 {
905   if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
906       || this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
907     return TRAVERSE_EXIT;
908   return this->traverse_expression(traverse, &this->map_index_);
909 }
910
911 // Lower a tuple map assignment.
912
913 Statement*
914 Tuple_map_assignment_statement::do_lower(Gogo*, Block* enclosing)
915 {
916   source_location loc = this->location();
917
918   Map_index_expression* map_index = this->map_index_->map_index_expression();
919   if (map_index == NULL)
920     {
921       this->report_error(_("expected map index on right hand side"));
922       return Statement::make_error_statement(loc);
923     }
924   Map_type* map_type = map_index->get_map_type();
925   if (map_type == NULL)
926     return Statement::make_error_statement(loc);
927
928   Block* b = new Block(enclosing, loc);
929
930   // Move out any subexpressions to make sure that functions are
931   // called in the required order.
932   Move_ordered_evals moe(b);
933   this->val_->traverse_subexpressions(&moe);
934   this->present_->traverse_subexpressions(&moe);
935
936   // Copy the key value into a temporary so that we can take its
937   // address without pushing the value onto the heap.
938
939   // var key_temp KEY_TYPE = MAP_INDEX
940   Temporary_statement* key_temp =
941     Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
942   b->add_statement(key_temp);
943
944   // var val_temp VAL_TYPE
945   Temporary_statement* val_temp =
946     Statement::make_temporary(map_type->val_type(), NULL, loc);
947   b->add_statement(val_temp);
948
949   // var present_temp bool
950   Temporary_statement* present_temp =
951     Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
952   b->add_statement(present_temp);
953
954   // func mapaccess2(hmap map[k]v, key *k, val *v) bool
955   source_location bloc = BUILTINS_LOCATION;
956   Typed_identifier_list* param_types = new Typed_identifier_list();
957   param_types->push_back(Typed_identifier("hmap", map_type, bloc));
958   Type* pkey_type = Type::make_pointer_type(map_type->key_type());
959   param_types->push_back(Typed_identifier("key", pkey_type, bloc));
960   Type* pval_type = Type::make_pointer_type(map_type->val_type());
961   param_types->push_back(Typed_identifier("val", pval_type, bloc));
962
963   Typed_identifier_list* ret_types = new Typed_identifier_list();
964   ret_types->push_back(Typed_identifier("", Type::make_boolean_type(), bloc));
965
966   Function_type* fntype = Type::make_function_type(NULL, param_types,
967                                                    ret_types, bloc);
968   Named_object* mapaccess2 =
969     Named_object::make_function_declaration("mapaccess2", NULL, fntype, bloc);
970   mapaccess2->func_declaration_value()->set_asm_name("runtime.mapaccess2");
971
972   // present_temp = mapaccess2(MAP, &key_temp, &val_temp)
973   Expression* func = Expression::make_func_reference(mapaccess2, NULL, loc);
974   Expression_list* params = new Expression_list();
975   params->push_back(map_index->map());
976   Expression* ref = Expression::make_temporary_reference(key_temp, loc);
977   params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
978   ref = Expression::make_temporary_reference(val_temp, loc);
979   params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
980   Expression* call = Expression::make_call(func, params, false, loc);
981
982   ref = Expression::make_temporary_reference(present_temp, loc);
983   Statement* s = Statement::make_assignment(ref, call, loc);
984   b->add_statement(s);
985
986   // val = val_temp
987   ref = Expression::make_temporary_reference(val_temp, loc);
988   s = Statement::make_assignment(this->val_, ref, loc);
989   b->add_statement(s);
990
991   // present = present_temp
992   ref = Expression::make_temporary_reference(present_temp, loc);
993   s = Statement::make_assignment(this->present_, ref, loc);
994   b->add_statement(s);
995
996   return Statement::make_block_statement(b, loc);
997 }
998
999 // Make a map assignment statement which returns a pair of values.
1000
1001 Statement*
1002 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
1003                                      Expression* map_index,
1004                                      source_location location)
1005 {
1006   return new Tuple_map_assignment_statement(val, present, map_index, location);
1007 }
1008
1009 // Assign a pair of entries to a map.
1010 //   m[k] = v, p
1011
1012 class Map_assignment_statement : public Statement
1013 {
1014  public:
1015   Map_assignment_statement(Expression* map_index,
1016                            Expression* val, Expression* should_set,
1017                            source_location location)
1018     : Statement(STATEMENT_MAP_ASSIGNMENT, location),
1019       map_index_(map_index), val_(val), should_set_(should_set)
1020   { }
1021
1022  protected:
1023   int
1024   do_traverse(Traverse* traverse);
1025
1026   bool
1027   do_traverse_assignments(Traverse_assignments*)
1028   { gcc_unreachable(); }
1029
1030   Statement*
1031   do_lower(Gogo*, Block*);
1032
1033   tree
1034   do_get_tree(Translate_context*)
1035   { gcc_unreachable(); }
1036
1037  private:
1038   // A reference to the map index which should be set or deleted.
1039   Expression* map_index_;
1040   // The value to add to the map.
1041   Expression* val_;
1042   // Whether or not to add the value.
1043   Expression* should_set_;
1044 };
1045
1046 // Traverse a map assignment.
1047
1048 int
1049 Map_assignment_statement::do_traverse(Traverse* traverse)
1050 {
1051   if (this->traverse_expression(traverse, &this->map_index_) == TRAVERSE_EXIT
1052       || this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
1053     return TRAVERSE_EXIT;
1054   return this->traverse_expression(traverse, &this->should_set_);
1055 }
1056
1057 // Lower a map assignment to a function call.
1058
1059 Statement*
1060 Map_assignment_statement::do_lower(Gogo*, Block* enclosing)
1061 {
1062   source_location loc = this->location();
1063
1064   Map_index_expression* map_index = this->map_index_->map_index_expression();
1065   if (map_index == NULL)
1066     {
1067       this->report_error(_("expected map index on left hand side"));
1068       return Statement::make_error_statement(loc);
1069     }
1070   Map_type* map_type = map_index->get_map_type();
1071   if (map_type == NULL)
1072     return Statement::make_error_statement(loc);
1073
1074   Block* b = new Block(enclosing, loc);
1075
1076   // Evaluate the map first to get order of evaluation right.
1077   // map_temp := m // we are evaluating m[k] = v, p
1078   Temporary_statement* map_temp = Statement::make_temporary(map_type,
1079                                                             map_index->map(),
1080                                                             loc);
1081   b->add_statement(map_temp);
1082
1083   // var key_temp MAP_KEY_TYPE = k
1084   Temporary_statement* key_temp =
1085     Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
1086   b->add_statement(key_temp);
1087
1088   // var val_temp MAP_VAL_TYPE = v
1089   Temporary_statement* val_temp =
1090     Statement::make_temporary(map_type->val_type(), this->val_, loc);
1091   b->add_statement(val_temp);
1092
1093   // func mapassign2(hmap map[k]v, key *k, val *v, p)
1094   source_location bloc = BUILTINS_LOCATION;
1095   Typed_identifier_list* param_types = new Typed_identifier_list();
1096   param_types->push_back(Typed_identifier("hmap", map_type, bloc));
1097   Type* pkey_type = Type::make_pointer_type(map_type->key_type());
1098   param_types->push_back(Typed_identifier("key", pkey_type, bloc));
1099   Type* pval_type = Type::make_pointer_type(map_type->val_type());
1100   param_types->push_back(Typed_identifier("val", pval_type, bloc));
1101   param_types->push_back(Typed_identifier("p", Type::lookup_bool_type(), bloc));
1102   Function_type* fntype = Type::make_function_type(NULL, param_types,
1103                                                    NULL, bloc);
1104   Named_object* mapassign2 =
1105     Named_object::make_function_declaration("mapassign2", NULL, fntype, bloc);
1106   mapassign2->func_declaration_value()->set_asm_name("runtime.mapassign2");
1107
1108   // mapassign2(map_temp, &key_temp, &val_temp, p)
1109   Expression* func = Expression::make_func_reference(mapassign2, NULL, loc);
1110   Expression_list* params = new Expression_list();
1111   params->push_back(Expression::make_temporary_reference(map_temp, loc));
1112   Expression* ref = Expression::make_temporary_reference(key_temp, loc);
1113   params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1114   ref = Expression::make_temporary_reference(val_temp, loc);
1115   params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1116   params->push_back(this->should_set_);
1117   Expression* call = Expression::make_call(func, params, false, loc);
1118   Statement* s = Statement::make_statement(call);
1119   b->add_statement(s);
1120
1121   return Statement::make_block_statement(b, loc);
1122 }
1123
1124 // Make a statement which assigns a pair of entries to a map.
1125
1126 Statement*
1127 Statement::make_map_assignment(Expression* map_index,
1128                                Expression* val, Expression* should_set,
1129                                source_location location)
1130 {
1131   return new Map_assignment_statement(map_index, val, should_set, location);
1132 }
1133
1134 // A tuple assignment from a receive statement.
1135
1136 class Tuple_receive_assignment_statement : public Statement
1137 {
1138  public:
1139   Tuple_receive_assignment_statement(Expression* val, Expression* success,
1140                                      Expression* channel,
1141                                      source_location location)
1142     : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
1143       val_(val), success_(success), channel_(channel)
1144   { }
1145
1146  protected:
1147   int
1148   do_traverse(Traverse* traverse);
1149
1150   bool
1151   do_traverse_assignments(Traverse_assignments*)
1152   { gcc_unreachable(); }
1153
1154   Statement*
1155   do_lower(Gogo*, Block*);
1156
1157   tree
1158   do_get_tree(Translate_context*)
1159   { gcc_unreachable(); }
1160
1161  private:
1162   // Lvalue which receives the value from the channel.
1163   Expression* val_;
1164   // Lvalue which receives whether the read succeeded or failed.
1165   Expression* success_;
1166   // The channel on which we receive the value.
1167   Expression* channel_;
1168 };
1169
1170 // Traversal.
1171
1172 int
1173 Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
1174 {
1175   if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1176       || this->traverse_expression(traverse, &this->success_) == TRAVERSE_EXIT)
1177     return TRAVERSE_EXIT;
1178   return this->traverse_expression(traverse, &this->channel_);
1179 }
1180
1181 // Lower to a function call.
1182
1183 Statement*
1184 Tuple_receive_assignment_statement::do_lower(Gogo*, Block* enclosing)
1185 {
1186   source_location loc = this->location();
1187
1188   Channel_type* channel_type = this->channel_->type()->channel_type();
1189   if (channel_type == NULL)
1190     {
1191       this->report_error(_("expected channel"));
1192       return Statement::make_error_statement(loc);
1193     }
1194   if (!channel_type->may_receive())
1195     {
1196       this->report_error(_("invalid receive on send-only channel"));
1197       return Statement::make_error_statement(loc);
1198     }
1199
1200   Block* b = new Block(enclosing, loc);
1201
1202   // Make sure that any subexpressions on the left hand side are
1203   // evaluated in the right order.
1204   Move_ordered_evals moe(b);
1205   this->val_->traverse_subexpressions(&moe);
1206   this->success_->traverse_subexpressions(&moe);
1207
1208   // var val_temp ELEMENT_TYPE
1209   Temporary_statement* val_temp =
1210     Statement::make_temporary(channel_type->element_type(), NULL, loc);
1211   b->add_statement(val_temp);
1212
1213   // var success_temp bool
1214   Temporary_statement* success_temp =
1215     Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
1216   b->add_statement(success_temp);
1217
1218   // func chanrecv2(c chan T, val *T) bool
1219   source_location bloc = BUILTINS_LOCATION;
1220   Typed_identifier_list* param_types = new Typed_identifier_list();
1221   param_types->push_back(Typed_identifier("c", channel_type, bloc));
1222   Type* pelement_type = Type::make_pointer_type(channel_type->element_type());
1223   param_types->push_back(Typed_identifier("val", pelement_type, bloc));
1224
1225   Typed_identifier_list* ret_types = new Typed_identifier_list();
1226   ret_types->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1227
1228   Function_type* fntype = Type::make_function_type(NULL, param_types,
1229                                                    ret_types, bloc);
1230   Named_object* chanrecv2 =
1231     Named_object::make_function_declaration("chanrecv2", NULL, fntype, bloc);
1232   chanrecv2->func_declaration_value()->set_asm_name("runtime.chanrecv2");
1233
1234   // success_temp = chanrecv2(channel, &val_temp)
1235   Expression* func = Expression::make_func_reference(chanrecv2, NULL, loc);
1236   Expression_list* params = new Expression_list();
1237   params->push_back(this->channel_);
1238   Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1239   params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1240   Expression* call = Expression::make_call(func, params, false, loc);
1241   ref = Expression::make_temporary_reference(success_temp, loc);
1242   Statement* s = Statement::make_assignment(ref, call, loc);
1243   b->add_statement(s);
1244
1245   // val = val_temp
1246   ref = Expression::make_temporary_reference(val_temp, loc);
1247   s = Statement::make_assignment(this->val_, ref, loc);
1248   b->add_statement(s);
1249
1250   // success = success_temp
1251   ref = Expression::make_temporary_reference(success_temp, loc);
1252   s = Statement::make_assignment(this->success_, ref, loc);
1253   b->add_statement(s);
1254
1255   return Statement::make_block_statement(b, loc);
1256 }
1257
1258 // Make a nonblocking receive statement.
1259
1260 Statement*
1261 Statement::make_tuple_receive_assignment(Expression* val, Expression* success,
1262                                          Expression* channel,
1263                                          source_location location)
1264 {
1265   return new Tuple_receive_assignment_statement(val, success, channel,
1266                                                 location);
1267 }
1268
1269 // An assignment to a pair of values from a type guard.  This is a
1270 // conditional type guard.  v, ok = i.(type).
1271
1272 class Tuple_type_guard_assignment_statement : public Statement
1273 {
1274  public:
1275   Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
1276                                         Expression* expr, Type* type,
1277                                         source_location location)
1278     : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
1279       val_(val), ok_(ok), expr_(expr), type_(type)
1280   { }
1281
1282  protected:
1283   int
1284   do_traverse(Traverse*);
1285
1286   bool
1287   do_traverse_assignments(Traverse_assignments*)
1288   { gcc_unreachable(); }
1289
1290   Statement*
1291   do_lower(Gogo*, Block*);
1292
1293   tree
1294   do_get_tree(Translate_context*)
1295   { gcc_unreachable(); }
1296
1297  private:
1298   Call_expression*
1299   lower_to_empty_interface(const char*);
1300
1301   Call_expression*
1302   lower_to_type(const char*);
1303
1304   void
1305   lower_to_object_type(Block*, const char*);
1306
1307   // The variable which recieves the converted value.
1308   Expression* val_;
1309   // The variable which receives the indication of success.
1310   Expression* ok_;
1311   // The expression being converted.
1312   Expression* expr_;
1313   // The type to which the expression is being converted.
1314   Type* type_;
1315 };
1316
1317 // Traverse a type guard tuple assignment.
1318
1319 int
1320 Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
1321 {
1322   if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
1323       || this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
1324       || this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
1325     return TRAVERSE_EXIT;
1326   return this->traverse_expression(traverse, &this->expr_);
1327 }
1328
1329 // Lower to a function call.
1330
1331 Statement*
1332 Tuple_type_guard_assignment_statement::do_lower(Gogo*, Block* enclosing)
1333 {
1334   source_location loc = this->location();
1335
1336   Type* expr_type = this->expr_->type();
1337   if (expr_type->interface_type() == NULL)
1338     {
1339       if (!expr_type->is_error_type() && !this->type_->is_error_type())
1340         this->report_error(_("type assertion only valid for interface types"));
1341       return Statement::make_error_statement(loc);
1342     }
1343
1344   Block* b = new Block(enclosing, loc);
1345
1346   // Make sure that any subexpressions on the left hand side are
1347   // evaluated in the right order.
1348   Move_ordered_evals moe(b);
1349   this->val_->traverse_subexpressions(&moe);
1350   this->ok_->traverse_subexpressions(&moe);
1351
1352   bool expr_is_empty = expr_type->interface_type()->is_empty();
1353   Call_expression* call;
1354   if (this->type_->interface_type() != NULL)
1355     {
1356       if (this->type_->interface_type()->is_empty())
1357         call = this->lower_to_empty_interface(expr_is_empty
1358                                               ? "ifaceE2E2"
1359                                               : "ifaceI2E2");
1360       else
1361         call = this->lower_to_type(expr_is_empty ? "ifaceE2I2" : "ifaceI2I2");
1362     }
1363   else if (this->type_->points_to() != NULL)
1364     call = this->lower_to_type(expr_is_empty ? "ifaceE2T2P" : "ifaceI2T2P");
1365   else
1366     {
1367       this->lower_to_object_type(b, expr_is_empty ? "ifaceE2T2" : "ifaceI2T2");
1368       call = NULL;
1369     }
1370
1371   if (call != NULL)
1372     {
1373       Expression* res = Expression::make_call_result(call, 0);
1374       Statement* s = Statement::make_assignment(this->val_, res, loc);
1375       b->add_statement(s);
1376
1377       res = Expression::make_call_result(call, 1);
1378       s = Statement::make_assignment(this->ok_, res, loc);
1379       b->add_statement(s);
1380     }
1381
1382   return Statement::make_block_statement(b, loc);
1383 }
1384
1385 // Lower a conversion to an empty interface type.
1386
1387 Call_expression*
1388 Tuple_type_guard_assignment_statement::lower_to_empty_interface(
1389     const char *fnname)
1390 {
1391   source_location loc = this->location();
1392
1393   // func FNNAME(interface) (empty, bool)
1394   source_location bloc = BUILTINS_LOCATION;
1395   Typed_identifier_list* param_types = new Typed_identifier_list();
1396   param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
1397   Typed_identifier_list* ret_types = new Typed_identifier_list();
1398   ret_types->push_back(Typed_identifier("ret", this->type_, bloc));
1399   ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
1400   Function_type* fntype = Type::make_function_type(NULL, param_types,
1401                                                    ret_types, bloc);
1402   Named_object* fn =
1403     Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1404   std::string asm_name = "runtime.";
1405   asm_name += fnname;
1406   fn->func_declaration_value()->set_asm_name(asm_name);
1407
1408   // val, ok = FNNAME(expr)
1409   Expression* func = Expression::make_func_reference(fn, NULL, loc);
1410   Expression_list* params = new Expression_list();
1411   params->push_back(this->expr_);
1412   return Expression::make_call(func, params, false, loc);
1413 }
1414
1415 // Lower a conversion to a non-empty interface type or a pointer type.
1416
1417 Call_expression*
1418 Tuple_type_guard_assignment_statement::lower_to_type(const char* fnname)
1419 {
1420   source_location loc = this->location();
1421
1422   // func FNNAME(*descriptor, interface) (interface, bool)
1423   source_location bloc = BUILTINS_LOCATION;
1424   Typed_identifier_list* param_types = new Typed_identifier_list();
1425   param_types->push_back(Typed_identifier("inter",
1426                                           Type::make_type_descriptor_ptr_type(),
1427                                           bloc));
1428   param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
1429   Typed_identifier_list* ret_types = new Typed_identifier_list();
1430   ret_types->push_back(Typed_identifier("ret", this->type_, bloc));
1431   ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
1432   Function_type* fntype = Type::make_function_type(NULL, param_types,
1433                                                    ret_types, bloc);
1434   Named_object* fn =
1435     Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1436   std::string asm_name = "runtime.";
1437   asm_name += fnname;
1438   fn->func_declaration_value()->set_asm_name(asm_name);
1439
1440   // val, ok = FNNAME(type_descriptor, expr)
1441   Expression* func = Expression::make_func_reference(fn, NULL, loc);
1442   Expression_list* params = new Expression_list();
1443   params->push_back(Expression::make_type_descriptor(this->type_, loc));
1444   params->push_back(this->expr_);
1445   return Expression::make_call(func, params, false, loc);
1446 }
1447
1448 // Lower a conversion to a non-interface non-pointer type.
1449
1450 void
1451 Tuple_type_guard_assignment_statement::lower_to_object_type(Block* b,
1452                                                             const char *fnname)
1453 {
1454   source_location loc = this->location();
1455
1456   // var val_temp TYPE
1457   Temporary_statement* val_temp = Statement::make_temporary(this->type_,
1458                                                             NULL, loc);
1459   b->add_statement(val_temp);
1460
1461   // func FNNAME(*descriptor, interface, *T) bool
1462   source_location bloc = BUILTINS_LOCATION;
1463   Typed_identifier_list* param_types = new Typed_identifier_list();
1464   param_types->push_back(Typed_identifier("inter",
1465                                           Type::make_type_descriptor_ptr_type(),
1466                                           bloc));
1467   param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
1468   Type* ptype = Type::make_pointer_type(this->type_);
1469   param_types->push_back(Typed_identifier("v", ptype, bloc));
1470   Typed_identifier_list* ret_types = new Typed_identifier_list();
1471   ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
1472   Function_type* fntype = Type::make_function_type(NULL, param_types,
1473                                                    ret_types, bloc);
1474   Named_object* fn =
1475     Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
1476   std::string asm_name = "runtime.";
1477   asm_name += fnname;
1478   fn->func_declaration_value()->set_asm_name(asm_name);
1479
1480   // ok = FNNAME(type_descriptor, expr, &val_temp)
1481   Expression* func = Expression::make_func_reference(fn, NULL, loc);
1482   Expression_list* params = new Expression_list();
1483   params->push_back(Expression::make_type_descriptor(this->type_, loc));
1484   params->push_back(this->expr_);
1485   Expression* ref = Expression::make_temporary_reference(val_temp, loc);
1486   params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
1487   Expression* call = Expression::make_call(func, params, false, loc);
1488   Statement* s = Statement::make_assignment(this->ok_, call, loc);
1489   b->add_statement(s);
1490
1491   // val = val_temp
1492   ref = Expression::make_temporary_reference(val_temp, loc);
1493   s = Statement::make_assignment(this->val_, ref, loc);
1494   b->add_statement(s);
1495 }
1496
1497 // Make an assignment from a type guard to a pair of variables.
1498
1499 Statement*
1500 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
1501                                             Expression* expr, Type* type,
1502                                             source_location location)
1503 {
1504   return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
1505                                                    location);
1506 }
1507
1508 // An expression statement.
1509
1510 class Expression_statement : public Statement
1511 {
1512  public:
1513   Expression_statement(Expression* expr)
1514     : Statement(STATEMENT_EXPRESSION, expr->location()),
1515       expr_(expr)
1516   { }
1517
1518  protected:
1519   int
1520   do_traverse(Traverse* traverse)
1521   { return this->traverse_expression(traverse, &this->expr_); }
1522
1523   void
1524   do_determine_types()
1525   { this->expr_->determine_type_no_context(); }
1526
1527   bool
1528   do_may_fall_through() const;
1529
1530   tree
1531   do_get_tree(Translate_context* context)
1532   { return this->expr_->get_tree(context); }
1533
1534  private:
1535   Expression* expr_;
1536 };
1537
1538 // An expression statement may fall through unless it is a call to a
1539 // function which does not return.
1540
1541 bool
1542 Expression_statement::do_may_fall_through() const
1543 {
1544   const Call_expression* call = this->expr_->call_expression();
1545   if (call != NULL)
1546     {
1547       const Expression* fn = call->fn();
1548       const Func_expression* fe = fn->func_expression();
1549       if (fe != NULL)
1550         {
1551           const Named_object* no = fe->named_object();
1552
1553           Function_type* fntype;
1554           if (no->is_function())
1555             fntype = no->func_value()->type();
1556           else if (no->is_function_declaration())
1557             fntype = no->func_declaration_value()->type();
1558           else
1559             fntype = NULL;
1560
1561           // The builtin function panic does not return.
1562           if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
1563             return false;
1564         }
1565     }
1566   return true;
1567 }
1568
1569 // Make an expression statement from an Expression.
1570
1571 Statement*
1572 Statement::make_statement(Expression* expr)
1573 {
1574   return new Expression_statement(expr);
1575 }
1576
1577 // A block statement--a list of statements which may include variable
1578 // definitions.
1579
1580 class Block_statement : public Statement
1581 {
1582  public:
1583   Block_statement(Block* block, source_location location)
1584     : Statement(STATEMENT_BLOCK, location),
1585       block_(block)
1586   { }
1587
1588  protected:
1589   int
1590   do_traverse(Traverse* traverse)
1591   { return this->block_->traverse(traverse); }
1592
1593   void
1594   do_determine_types()
1595   { this->block_->determine_types(); }
1596
1597   bool
1598   do_may_fall_through() const
1599   { return this->block_->may_fall_through(); }
1600
1601   tree
1602   do_get_tree(Translate_context* context)
1603   { return this->block_->get_tree(context); }
1604
1605  private:
1606   Block* block_;
1607 };
1608
1609 // Make a block statement.
1610
1611 Statement*
1612 Statement::make_block_statement(Block* block, source_location location)
1613 {
1614   return new Block_statement(block, location);
1615 }
1616
1617 // An increment or decrement statement.
1618
1619 class Inc_dec_statement : public Statement
1620 {
1621  public:
1622   Inc_dec_statement(bool is_inc, Expression* expr)
1623     : Statement(STATEMENT_INCDEC, expr->location()),
1624       expr_(expr), is_inc_(is_inc)
1625   { }
1626
1627  protected:
1628   int
1629   do_traverse(Traverse* traverse)
1630   { return this->traverse_expression(traverse, &this->expr_); }
1631
1632   bool
1633   do_traverse_assignments(Traverse_assignments*)
1634   { gcc_unreachable(); }
1635
1636   Statement*
1637   do_lower(Gogo*, Block*);
1638
1639   tree
1640   do_get_tree(Translate_context*)
1641   { gcc_unreachable(); }
1642
1643  private:
1644   // The l-value to increment or decrement.
1645   Expression* expr_;
1646   // Whether to increment or decrement.
1647   bool is_inc_;
1648 };
1649
1650 // Lower to += or -=.
1651
1652 Statement*
1653 Inc_dec_statement::do_lower(Gogo*, Block*)
1654 {
1655   source_location loc = this->location();
1656
1657   mpz_t oval;
1658   mpz_init_set_ui(oval, 1UL);
1659   Expression* oexpr = Expression::make_integer(&oval, NULL, loc);
1660   mpz_clear(oval);
1661
1662   Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
1663   return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
1664 }
1665
1666 // Make an increment statement.
1667
1668 Statement*
1669 Statement::make_inc_statement(Expression* expr)
1670 {
1671   return new Inc_dec_statement(true, expr);
1672 }
1673
1674 // Make a decrement statement.
1675
1676 Statement*
1677 Statement::make_dec_statement(Expression* expr)
1678 {
1679   return new Inc_dec_statement(false, expr);
1680 }
1681
1682 // Class Thunk_statement.  This is the base class for go and defer
1683 // statements.
1684
1685 const char* const Thunk_statement::thunk_field_fn = "fn";
1686
1687 const char* const Thunk_statement::thunk_field_receiver = "receiver";
1688
1689 // Constructor.
1690
1691 Thunk_statement::Thunk_statement(Statement_classification classification,
1692                                  Call_expression* call,
1693                                  source_location location)
1694     : Statement(classification, location),
1695       call_(call), struct_type_(NULL)
1696 {
1697 }
1698
1699 // Return whether this is a simple statement which does not require a
1700 // thunk.
1701
1702 bool
1703 Thunk_statement::is_simple(Function_type* fntype) const
1704 {
1705   // We need a thunk to call a method, or to pass a variable number of
1706   // arguments.
1707   if (fntype->is_method() || fntype->is_varargs())
1708     return false;
1709
1710   // A defer statement requires a thunk to set up for whether the
1711   // function can call recover.
1712   if (this->classification() == STATEMENT_DEFER)
1713     return false;
1714
1715   // We can only permit a single parameter of pointer type.
1716   const Typed_identifier_list* parameters = fntype->parameters();
1717   if (parameters != NULL
1718       && (parameters->size() > 1
1719           || (parameters->size() == 1
1720               && parameters->begin()->type()->points_to() == NULL)))
1721     return false;
1722
1723   // If the function returns multiple values, or returns a type other
1724   // than integer, floating point, or pointer, then it may get a
1725   // hidden first parameter, in which case we need the more
1726   // complicated approach.  This is true even though we are going to
1727   // ignore the return value.
1728   const Typed_identifier_list* results = fntype->results();
1729   if (results != NULL
1730       && (results->size() > 1
1731           || (results->size() == 1
1732               && !results->begin()->type()->is_basic_type()
1733               && results->begin()->type()->points_to() == NULL)))
1734     return false;
1735
1736   // If this calls something which is not a simple function, then we
1737   // need a thunk.
1738   Expression* fn = this->call_->call_expression()->fn();
1739   if (fn->bound_method_expression() != NULL
1740       || fn->interface_field_reference_expression() != NULL)
1741     return false;
1742
1743   return true;
1744 }
1745
1746 // Traverse a thunk statement.
1747
1748 int
1749 Thunk_statement::do_traverse(Traverse* traverse)
1750 {
1751   return this->traverse_expression(traverse, &this->call_);
1752 }
1753
1754 // We implement traverse_assignment for a thunk statement because it
1755 // effectively copies the function call.
1756
1757 bool
1758 Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
1759 {
1760   Expression* fn = this->call_->call_expression()->fn();
1761   Expression* fn2 = fn;
1762   tassign->value(&fn2, true, false);
1763   return true;
1764 }
1765
1766 // Determine types in a thunk statement.
1767
1768 void
1769 Thunk_statement::do_determine_types()
1770 {
1771   this->call_->determine_type_no_context();
1772
1773   // Now that we know the types of the call, build the struct used to
1774   // pass parameters.
1775   Function_type* fntype =
1776     this->call_->call_expression()->get_function_type();
1777   if (fntype != NULL && !this->is_simple(fntype))
1778     this->struct_type_ = this->build_struct(fntype);
1779 }
1780
1781 // Check types in a thunk statement.
1782
1783 void
1784 Thunk_statement::do_check_types(Gogo*)
1785 {
1786   Call_expression* ce = this->call_->call_expression();
1787   Function_type* fntype = ce->get_function_type();
1788   if (fntype != NULL && fntype->is_method())
1789     {
1790       Expression* fn = ce->fn();
1791       if (fn->bound_method_expression() == NULL
1792           && fn->interface_field_reference_expression() == NULL)
1793         this->report_error(_("no object for method call"));
1794     }
1795 }
1796
1797 // The Traverse class used to find and simplify thunk statements.
1798
1799 class Simplify_thunk_traverse : public Traverse
1800 {
1801  public:
1802   Simplify_thunk_traverse(Gogo* gogo)
1803     : Traverse(traverse_blocks),
1804       gogo_(gogo)
1805   { }
1806
1807   int
1808   block(Block*);
1809
1810  private:
1811   Gogo* gogo_;
1812 };
1813
1814 int
1815 Simplify_thunk_traverse::block(Block* b)
1816 {
1817   // The parser ensures that thunk statements always appear at the end
1818   // of a block.
1819   if (b->statements()->size() < 1)
1820     return TRAVERSE_CONTINUE;
1821   Thunk_statement* stat = b->statements()->back()->thunk_statement();
1822   if (stat == NULL)
1823     return TRAVERSE_CONTINUE;
1824   if (stat->simplify_statement(this->gogo_, b))
1825     return TRAVERSE_SKIP_COMPONENTS;
1826   return TRAVERSE_CONTINUE;
1827 }
1828
1829 // Simplify all thunk statements.
1830
1831 void
1832 Gogo::simplify_thunk_statements()
1833 {
1834   Simplify_thunk_traverse thunk_traverse(this);
1835   this->traverse(&thunk_traverse);
1836 }
1837
1838 // Simplify complex thunk statements into simple ones.  A complicated
1839 // thunk statement is one which takes anything other than zero
1840 // parameters or a single pointer parameter.  We rewrite it into code
1841 // which allocates a struct, stores the parameter values into the
1842 // struct, and does a simple go or defer statement which passes the
1843 // struct to a thunk.  The thunk does the real call.
1844
1845 bool
1846 Thunk_statement::simplify_statement(Gogo* gogo, Block* block)
1847 {
1848   if (this->classification() == STATEMENT_ERROR)
1849     return false;
1850   if (this->call_->is_error_expression())
1851     return false;
1852
1853   Call_expression* ce = this->call_->call_expression();
1854   Function_type* fntype = ce->get_function_type();
1855   if (fntype == NULL)
1856     {
1857       gcc_assert(saw_errors());
1858       this->set_is_error();
1859       return false;
1860     }
1861   if (this->is_simple(fntype))
1862     return false;
1863
1864   Expression* fn = ce->fn();
1865   Bound_method_expression* bound_method = fn->bound_method_expression();
1866   Interface_field_reference_expression* interface_method =
1867     fn->interface_field_reference_expression();
1868   const bool is_method = bound_method != NULL || interface_method != NULL;
1869
1870   source_location location = this->location();
1871
1872   std::string thunk_name = Gogo::thunk_name();
1873
1874   // Build the thunk.
1875   this->build_thunk(gogo, thunk_name, fntype);
1876
1877   // Generate code to call the thunk.
1878
1879   // Get the values to store into the struct which is the single
1880   // argument to the thunk.
1881
1882   Expression_list* vals = new Expression_list();
1883   if (fntype->is_builtin())
1884     ;
1885   else if (!is_method)
1886     vals->push_back(fn);
1887   else if (interface_method != NULL)
1888     vals->push_back(interface_method->expr());
1889   else if (bound_method != NULL)
1890     {
1891       vals->push_back(bound_method->method());
1892       Expression* first_arg = bound_method->first_argument();
1893
1894       // We always pass a pointer when calling a method.
1895       if (first_arg->type()->points_to() == NULL)
1896         first_arg = Expression::make_unary(OPERATOR_AND, first_arg, location);
1897
1898       // If we are calling a method which was inherited from an
1899       // embedded struct, and the method did not get a stub, then the
1900       // first type may be wrong.
1901       Type* fatype = bound_method->first_argument_type();
1902       if (fatype != NULL)
1903         {
1904           if (fatype->points_to() == NULL)
1905             fatype = Type::make_pointer_type(fatype);
1906           Type* unsafe = Type::make_pointer_type(Type::make_void_type());
1907           first_arg = Expression::make_cast(unsafe, first_arg, location);
1908           first_arg = Expression::make_cast(fatype, first_arg, location);
1909         }
1910
1911       vals->push_back(first_arg);
1912     }
1913   else
1914     gcc_unreachable();
1915
1916   if (ce->args() != NULL)
1917     {
1918       for (Expression_list::const_iterator p = ce->args()->begin();
1919            p != ce->args()->end();
1920            ++p)
1921         vals->push_back(*p);
1922     }
1923
1924   // Build the struct.
1925   Expression* constructor =
1926     Expression::make_struct_composite_literal(this->struct_type_, vals,
1927                                               location);
1928
1929   // Allocate the initialized struct on the heap.
1930   constructor = Expression::make_heap_composite(constructor, location);
1931
1932   // Look up the thunk.
1933   Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
1934   gcc_assert(named_thunk != NULL && named_thunk->is_function());
1935
1936   // Build the call.
1937   Expression* func = Expression::make_func_reference(named_thunk, NULL,
1938                                                      location);
1939   Expression_list* params = new Expression_list();
1940   params->push_back(constructor);
1941   Call_expression* call = Expression::make_call(func, params, false, location);
1942
1943   // Build the simple go or defer statement.
1944   Statement* s;
1945   if (this->classification() == STATEMENT_GO)
1946     s = Statement::make_go_statement(call, location);
1947   else if (this->classification() == STATEMENT_DEFER)
1948     s = Statement::make_defer_statement(call, location);
1949   else
1950     gcc_unreachable();
1951
1952   // The current block should end with the go statement.
1953   gcc_assert(block->statements()->size() >= 1);
1954   gcc_assert(block->statements()->back() == this);
1955   block->replace_statement(block->statements()->size() - 1, s);
1956
1957   // We already ran the determine_types pass, so we need to run it now
1958   // for the new statement.
1959   s->determine_types();
1960
1961   // Sanity check.
1962   gogo->check_types_in_block(block);
1963
1964   // Return true to tell the block not to keep looking at statements.
1965   return true;
1966 }
1967
1968 // Set the name to use for thunk parameter N.
1969
1970 void
1971 Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
1972 {
1973   snprintf(buf, buflen, "a%d", n);
1974 }
1975
1976 // Build a new struct type to hold the parameters for a complicated
1977 // thunk statement.  FNTYPE is the type of the function call.
1978
1979 Struct_type*
1980 Thunk_statement::build_struct(Function_type* fntype)
1981 {
1982   source_location location = this->location();
1983
1984   Struct_field_list* fields = new Struct_field_list();
1985
1986   Call_expression* ce = this->call_->call_expression();
1987   Expression* fn = ce->fn();
1988
1989   Interface_field_reference_expression* interface_method =
1990     fn->interface_field_reference_expression();
1991   if (interface_method != NULL)
1992     {
1993       // If this thunk statement calls a method on an interface, we
1994       // pass the interface object to the thunk.
1995       Typed_identifier tid(Thunk_statement::thunk_field_fn,
1996                            interface_method->expr()->type(),
1997                            location);
1998       fields->push_back(Struct_field(tid));
1999     }
2000   else if (!fntype->is_builtin())
2001     {
2002       // The function to call.
2003       Typed_identifier tid(Go_statement::thunk_field_fn, fntype, location);
2004       fields->push_back(Struct_field(tid));
2005     }
2006   else if (ce->is_recover_call())
2007     {
2008       // The predeclared recover function has no argument.  However,
2009       // we add an argument when building recover thunks.  Handle that
2010       // here.
2011       fields->push_back(Struct_field(Typed_identifier("can_recover",
2012                                                       Type::make_boolean_type(),
2013                                                       location)));
2014     }
2015
2016   if (fn->bound_method_expression() != NULL)
2017     {
2018       gcc_assert(fntype->is_method());
2019       Type* rtype = fntype->receiver()->type();
2020       // We always pass the receiver as a pointer.
2021       if (rtype->points_to() == NULL)
2022         rtype = Type::make_pointer_type(rtype);
2023       Typed_identifier tid(Thunk_statement::thunk_field_receiver, rtype,
2024                            location);
2025       fields->push_back(Struct_field(tid));
2026     }
2027
2028   const Expression_list* args = ce->args();
2029   if (args != NULL)
2030     {
2031       int i = 0;
2032       for (Expression_list::const_iterator p = args->begin();
2033            p != args->end();
2034            ++p, ++i)
2035         {
2036           char buf[50];
2037           this->thunk_field_param(i, buf, sizeof buf);
2038           fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
2039                                                           location)));
2040         }
2041     }
2042
2043   return Type::make_struct_type(fields, location);
2044 }
2045
2046 // Build the thunk we are going to call.  This is a brand new, albeit
2047 // artificial, function.
2048
2049 void
2050 Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
2051                              Function_type* fntype)
2052 {
2053   source_location location = this->location();
2054
2055   Call_expression* ce = this->call_->call_expression();
2056
2057   bool may_call_recover = false;
2058   if (this->classification() == STATEMENT_DEFER)
2059     {
2060       Func_expression* fn = ce->fn()->func_expression();
2061       if (fn == NULL)
2062         may_call_recover = true;
2063       else
2064         {
2065           const Named_object* no = fn->named_object();
2066           if (!no->is_function())
2067             may_call_recover = true;
2068           else
2069             may_call_recover = no->func_value()->calls_recover();
2070         }
2071     }
2072
2073   // Build the type of the thunk.  The thunk takes a single parameter,
2074   // which is a pointer to the special structure we build.
2075   const char* const parameter_name = "__go_thunk_parameter";
2076   Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
2077   Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
2078   thunk_parameters->push_back(Typed_identifier(parameter_name,
2079                                                pointer_to_struct_type,
2080                                                location));
2081
2082   Typed_identifier_list* thunk_results = NULL;
2083   if (may_call_recover)
2084     {
2085       // When deferring a function which may call recover, add a
2086       // return value, to disable tail call optimizations which will
2087       // break the way we check whether recover is permitted.
2088       thunk_results = new Typed_identifier_list();
2089       thunk_results->push_back(Typed_identifier("", Type::make_boolean_type(),
2090                                                 location));
2091     }
2092
2093   Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
2094                                                        thunk_results,
2095                                                        location);
2096
2097   // Start building the thunk.
2098   Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
2099                                                 location);
2100
2101   // For a defer statement, start with a call to
2102   // __go_set_defer_retaddr.  */
2103   Label* retaddr_label = NULL; 
2104   if (may_call_recover)
2105     {
2106       retaddr_label = gogo->add_label_reference("retaddr");
2107       Expression* arg = Expression::make_label_addr(retaddr_label, location);
2108       Expression_list* args = new Expression_list();
2109       args->push_back(arg);
2110
2111       static Named_object* set_defer_retaddr;
2112       if (set_defer_retaddr == NULL)
2113         {
2114           const source_location bloc = BUILTINS_LOCATION;
2115           Typed_identifier_list* param_types = new Typed_identifier_list();
2116           Type *voidptr_type = Type::make_pointer_type(Type::make_void_type());
2117           param_types->push_back(Typed_identifier("r", voidptr_type, bloc));
2118
2119           Typed_identifier_list* result_types = new Typed_identifier_list();
2120           result_types->push_back(Typed_identifier("",
2121                                                    Type::make_boolean_type(),
2122                                                    bloc));
2123
2124           Function_type* t = Type::make_function_type(NULL, param_types,
2125                                                       result_types, bloc);
2126           set_defer_retaddr =
2127             Named_object::make_function_declaration("__go_set_defer_retaddr",
2128                                                     NULL, t, bloc);
2129           const char* n = "__go_set_defer_retaddr";
2130           set_defer_retaddr->func_declaration_value()->set_asm_name(n);
2131         }
2132
2133       Expression* fn = Expression::make_func_reference(set_defer_retaddr,
2134                                                        NULL, location);
2135       Expression* call = Expression::make_call(fn, args, false, location);
2136
2137       // This is a hack to prevent the middle-end from deleting the
2138       // label.
2139       gogo->start_block(location);
2140       gogo->add_statement(Statement::make_goto_statement(retaddr_label,
2141                                                          location));
2142       Block* then_block = gogo->finish_block(location);
2143       then_block->determine_types();
2144
2145       Statement* s = Statement::make_if_statement(call, then_block, NULL,
2146                                                   location);
2147       s->determine_types();
2148       gogo->add_statement(s);
2149     }
2150
2151   // Get a reference to the parameter.
2152   Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
2153   gcc_assert(named_parameter != NULL && named_parameter->is_variable());
2154
2155   // Build the call.  Note that the field names are the same as the
2156   // ones used in build_struct.
2157   Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
2158                                                                location);
2159   thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
2160                                            location);
2161
2162   Bound_method_expression* bound_method = ce->fn()->bound_method_expression();
2163   Interface_field_reference_expression* interface_method =
2164     ce->fn()->interface_field_reference_expression();
2165
2166   Expression* func_to_call;
2167   unsigned int next_index;
2168   if (!fntype->is_builtin())
2169     {
2170       func_to_call = Expression::make_field_reference(thunk_parameter,
2171                                                       0, location);
2172       next_index = 1;
2173     }
2174   else
2175     {
2176       gcc_assert(bound_method == NULL && interface_method == NULL);
2177       func_to_call = ce->fn();
2178       next_index = 0;
2179     }
2180
2181   if (bound_method != NULL)
2182     {
2183       Expression* r = Expression::make_field_reference(thunk_parameter, 1,
2184                                                        location);
2185       // The main program passes in a function pointer from the
2186       // interface expression, so here we can make a bound method in
2187       // all cases.
2188       func_to_call = Expression::make_bound_method(r, func_to_call,
2189                                                    location);
2190       next_index = 2;
2191     }
2192   else if (interface_method != NULL)
2193     {
2194       // The main program passes the interface object.
2195       const std::string& name(interface_method->name());
2196       func_to_call = Expression::make_interface_field_reference(func_to_call,
2197                                                                 name,
2198                                                                 location);
2199     }
2200
2201   Expression_list* call_params = new Expression_list();
2202   const Struct_field_list* fields = this->struct_type_->fields();
2203   Struct_field_list::const_iterator p = fields->begin();
2204   for (unsigned int i = 0; i < next_index; ++i)
2205     ++p;
2206   for (; p != fields->end(); ++p, ++next_index)
2207     {
2208       Expression* thunk_param = Expression::make_var_reference(named_parameter,
2209                                                                location);
2210       thunk_param = Expression::make_unary(OPERATOR_MULT, thunk_param,
2211                                            location);
2212       Expression* param = Expression::make_field_reference(thunk_param,
2213                                                            next_index,
2214                                                            location);
2215       call_params->push_back(param);
2216     }
2217
2218   Expression* call = Expression::make_call(func_to_call, call_params, false,
2219                                            location);
2220   // We need to lower in case this is a builtin function.
2221   call = call->lower(gogo, function, -1);
2222   if (may_call_recover)
2223     {
2224       Call_expression* ce = call->call_expression();
2225       if (ce != NULL)
2226         ce->set_is_deferred();
2227     }
2228
2229   Statement* call_statement = Statement::make_statement(call);
2230
2231   // We already ran the determine_types pass, so we need to run it
2232   // just for this statement now.
2233   call_statement->determine_types();
2234
2235   gogo->add_statement(call_statement);
2236
2237   // If this is a defer statement, the label comes immediately after
2238   // the call.
2239   if (may_call_recover)
2240     {
2241       gogo->add_label_definition("retaddr", location);
2242
2243       Expression_list* vals = new Expression_list();
2244       vals->push_back(Expression::make_boolean(false, location));
2245       const Typed_identifier_list* results =
2246         function->func_value()->type()->results();
2247       gogo->add_statement(Statement::make_return_statement(results, vals,
2248                                                           location));
2249     }
2250
2251   // That is all the thunk has to do.
2252   gogo->finish_function(location);
2253 }
2254
2255 // Get the function and argument trees.
2256
2257 void
2258 Thunk_statement::get_fn_and_arg(Translate_context* context, tree* pfn,
2259                                 tree* parg)
2260 {
2261   if (this->call_->is_error_expression())
2262     {
2263       *pfn = error_mark_node;
2264       *parg = error_mark_node;
2265       return;
2266     }
2267
2268   Call_expression* ce = this->call_->call_expression();
2269
2270   Expression* fn = ce->fn();
2271   *pfn = fn->get_tree(context);
2272
2273   const Expression_list* args = ce->args();
2274   if (args == NULL || args->empty())
2275     *parg = null_pointer_node;
2276   else
2277     {
2278       gcc_assert(args->size() == 1);
2279       *parg = args->front()->get_tree(context);
2280     }
2281 }
2282
2283 // Class Go_statement.
2284
2285 tree
2286 Go_statement::do_get_tree(Translate_context* context)
2287 {
2288   tree fn_tree;
2289   tree arg_tree;
2290   this->get_fn_and_arg(context, &fn_tree, &arg_tree);
2291
2292   static tree go_fndecl;
2293
2294   tree fn_arg_type = NULL_TREE;
2295   if (go_fndecl == NULL_TREE)
2296     {
2297       // Only build FN_ARG_TYPE if we need it.
2298       tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
2299       tree subfntype = build_function_type(ptr_type_node, subargtypes);
2300       fn_arg_type = build_pointer_type(subfntype);
2301     }
2302
2303   return Gogo::call_builtin(&go_fndecl,
2304                             this->location(),
2305                             "__go_go",
2306                             2,
2307                             void_type_node,
2308                             fn_arg_type,
2309                             fn_tree,
2310                             ptr_type_node,
2311                             arg_tree);
2312 }
2313
2314 // Make a go statement.
2315
2316 Statement*
2317 Statement::make_go_statement(Call_expression* call, source_location location)
2318 {
2319   return new Go_statement(call, location);
2320 }
2321
2322 // Class Defer_statement.
2323
2324 tree
2325 Defer_statement::do_get_tree(Translate_context* context)
2326 {
2327   source_location loc = this->location();
2328
2329   tree fn_tree;
2330   tree arg_tree;
2331   this->get_fn_and_arg(context, &fn_tree, &arg_tree);
2332   if (fn_tree == error_mark_node || arg_tree == error_mark_node)
2333     return error_mark_node;
2334
2335   static tree defer_fndecl;
2336
2337   tree fn_arg_type = NULL_TREE;
2338   if (defer_fndecl == NULL_TREE)
2339     {
2340       // Only build FN_ARG_TYPE if we need it.
2341       tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
2342       tree subfntype = build_function_type(ptr_type_node, subargtypes);
2343       fn_arg_type = build_pointer_type(subfntype);
2344     }
2345
2346   tree defer_stack = context->function()->func_value()->defer_stack(loc);
2347
2348   return Gogo::call_builtin(&defer_fndecl,
2349                             loc,
2350                             "__go_defer",
2351                             3,
2352                             void_type_node,
2353                             ptr_type_node,
2354                             defer_stack,
2355                             fn_arg_type,
2356                             fn_tree,
2357                             ptr_type_node,
2358                             arg_tree);
2359 }
2360
2361 // Make a defer statement.
2362
2363 Statement*
2364 Statement::make_defer_statement(Call_expression* call,
2365                                 source_location location)
2366 {
2367   return new Defer_statement(call, location);
2368 }
2369
2370 // Class Return_statement.
2371
2372 // Traverse assignments.  We treat each return value as a top level
2373 // RHS in an expression.
2374
2375 bool
2376 Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
2377 {
2378   Expression_list* vals = this->vals_;
2379   if (vals != NULL)
2380     {
2381       for (Expression_list::iterator p = vals->begin();
2382            p != vals->end();
2383            ++p)
2384         tassign->value(&*p, true, true);
2385     }
2386   return true;
2387 }
2388
2389 // Lower a return statement.  If we are returning a function call
2390 // which returns multiple values which match the current function,
2391 // split up the call's results.  If the function has named result
2392 // variables, and the return statement lists explicit values, then
2393 // implement it by assigning the values to the result variables and
2394 // changing the statement to not list any values.  This lets
2395 // panic/recover work correctly.
2396
2397 Statement*
2398 Return_statement::do_lower(Gogo*, Block* enclosing)
2399 {
2400   if (this->vals_ == NULL)
2401     return this;
2402
2403   const Typed_identifier_list* results = this->results_;
2404   if (results == NULL || results->empty())
2405     return this;
2406
2407   // If the current function has multiple return values, and we are
2408   // returning a single call expression, split up the call expression.
2409   size_t results_count = results->size();
2410   if (results_count > 1
2411       && this->vals_->size() == 1
2412       && this->vals_->front()->call_expression() != NULL)
2413     {
2414       Call_expression* call = this->vals_->front()->call_expression();
2415       size_t count = results->size();
2416       Expression_list* vals = new Expression_list;
2417       for (size_t i = 0; i < count; ++i)
2418         vals->push_back(Expression::make_call_result(call, i));
2419       delete this->vals_;
2420       this->vals_ = vals;
2421     }
2422
2423   if (results->front().name().empty())
2424     return this;
2425
2426   if (results_count != this->vals_->size())
2427     {
2428       // Presumably an error which will be reported in check_types.
2429       return this;
2430     }
2431
2432   // Assign to named return values and then return them.
2433
2434   source_location loc = this->location();
2435   const Block* top = enclosing;
2436   while (top->enclosing() != NULL)
2437     top = top->enclosing();
2438
2439   const Bindings *bindings = top->bindings();
2440   Block* b = new Block(enclosing, loc);
2441
2442   Expression_list* lhs = new Expression_list();
2443   Expression_list* rhs = new Expression_list();
2444
2445   Expression_list::const_iterator pe = this->vals_->begin();
2446   int i = 1;
2447   for (Typed_identifier_list::const_iterator pr = results->begin();
2448        pr != results->end();
2449        ++pr, ++pe, ++i)
2450     {
2451       Named_object* rv = bindings->lookup_local(pr->name());
2452       if (rv == NULL || !rv->is_result_variable())
2453         {
2454           // Presumably an error.
2455           delete b;
2456           delete lhs;
2457           delete rhs;
2458           return this;
2459         }
2460
2461       Expression* e = *pe;
2462
2463       // Check types now so that we give a good error message.  The
2464       // result type is known.  We determine the expression type
2465       // early.
2466
2467       Type *rvtype = rv->result_var_value()->type();
2468       Type_context type_context(rvtype, false);
2469       e->determine_type(&type_context);
2470
2471       std::string reason;
2472       if (Type::are_assignable(rvtype, e->type(), &reason))
2473         {
2474           Expression* ve = Expression::make_var_reference(rv, e->location());
2475           lhs->push_back(ve);
2476           rhs->push_back(e);
2477         }
2478       else
2479         {
2480           if (reason.empty())
2481             error_at(e->location(), "incompatible type for return value %d", i);
2482           else
2483             error_at(e->location(),
2484                      "incompatible type for return value %d (%s)",
2485                      i, reason.c_str());
2486         }
2487     }
2488   gcc_assert(lhs->size() == rhs->size());
2489
2490   if (lhs->empty())
2491     ;
2492   else if (lhs->size() == 1)
2493     {
2494       b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
2495                                                   loc));
2496       delete lhs;
2497       delete rhs;
2498     }
2499   else
2500     b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
2501
2502   b->add_statement(Statement::make_return_statement(this->results_, NULL,
2503                                                     loc));
2504
2505   return Statement::make_block_statement(b, loc);
2506 }
2507
2508 // Determine types.
2509
2510 void
2511 Return_statement::do_determine_types()
2512 {
2513   if (this->vals_ == NULL)
2514     return;
2515   const Typed_identifier_list* results = this->results_;
2516
2517   Typed_identifier_list::const_iterator pt;
2518   if (results != NULL)
2519     pt = results->begin();
2520   for (Expression_list::iterator pe = this->vals_->begin();
2521        pe != this->vals_->end();
2522        ++pe)
2523     {
2524       if (results == NULL || pt == results->end())
2525         (*pe)->determine_type_no_context();
2526       else
2527         {
2528           Type_context context(pt->type(), false);
2529           (*pe)->determine_type(&context);
2530           ++pt;
2531         }
2532     }
2533 }
2534
2535 // Check types.
2536
2537 void
2538 Return_statement::do_check_types(Gogo*)
2539 {
2540   if (this->vals_ == NULL)
2541     return;
2542
2543   const Typed_identifier_list* results = this->results_;
2544   if (results == NULL)
2545     {
2546       this->report_error(_("return with value in function "
2547                            "with no return type"));
2548       return;
2549     }
2550
2551   int i = 1;
2552   Typed_identifier_list::const_iterator pt = results->begin();
2553   for (Expression_list::const_iterator pe = this->vals_->begin();
2554        pe != this->vals_->end();
2555        ++pe, ++pt, ++i)
2556     {
2557       if (pt == results->end())
2558         {
2559           this->report_error(_("too many values in return statement"));
2560           return;
2561         }
2562       std::string reason;
2563       if (!Type::are_assignable(pt->type(), (*pe)->type(), &reason))
2564         {
2565           if (reason.empty())
2566             error_at(this->location(),
2567                      "incompatible type for return value %d",
2568                      i);
2569           else
2570             error_at(this->location(),
2571                      "incompatible type for return value %d (%s)",
2572                      i, reason.c_str());
2573           this->set_is_error();
2574         }
2575       else if (pt->type()->is_error_type()
2576                || (*pe)->type()->is_error_type()
2577                || pt->type()->is_undefined()
2578                || (*pe)->type()->is_undefined())
2579         {
2580           // Make sure we get the error for an undefined type.
2581           pt->type()->base();
2582           (*pe)->type()->base();
2583           this->set_is_error();
2584         }
2585     }
2586
2587   if (pt != results->end())
2588     this->report_error(_("not enough values in return statement"));
2589 }
2590
2591 // Build a RETURN_EXPR tree.
2592
2593 tree
2594 Return_statement::do_get_tree(Translate_context* context)
2595 {
2596   Function* function = context->function()->func_value();
2597   tree fndecl = function->get_decl();
2598   if (fndecl == error_mark_node || DECL_RESULT(fndecl) == error_mark_node)
2599     return error_mark_node;
2600
2601   const Typed_identifier_list* results = this->results_;
2602
2603   if (this->vals_ == NULL)
2604     {
2605       tree stmt_list = NULL_TREE;
2606       tree retval = function->return_value(context->gogo(),
2607                                            context->function(),
2608                                            this->location(),
2609                                            &stmt_list);
2610       tree set;
2611       if (retval == NULL_TREE)
2612         set = NULL_TREE;
2613       else if (retval == error_mark_node)
2614         return error_mark_node;
2615       else
2616         set = fold_build2_loc(this->location(), MODIFY_EXPR, void_type_node,
2617                               DECL_RESULT(fndecl), retval);
2618       append_to_statement_list(this->build_stmt_1(RETURN_EXPR, set),
2619                                &stmt_list);
2620       return stmt_list;
2621     }
2622   else if (this->vals_->size() == 1)
2623     {
2624       gcc_assert(!VOID_TYPE_P(TREE_TYPE(TREE_TYPE(fndecl))));
2625       tree val = (*this->vals_->begin())->get_tree(context);
2626       gcc_assert(results != NULL && results->size() == 1);
2627       val = Expression::convert_for_assignment(context,
2628                                                results->begin()->type(),
2629                                                (*this->vals_->begin())->type(),
2630                                                val, this->location());
2631       if (val == error_mark_node)
2632         return error_mark_node;
2633       tree set = build2(MODIFY_EXPR, void_type_node,
2634                         DECL_RESULT(fndecl), val);
2635       SET_EXPR_LOCATION(set, this->location());
2636       return this->build_stmt_1(RETURN_EXPR, set);
2637     }
2638   else
2639     {
2640       gcc_assert(!VOID_TYPE_P(TREE_TYPE(TREE_TYPE(fndecl))));
2641       tree stmt_list = NULL_TREE;
2642       tree rettype = TREE_TYPE(DECL_RESULT(fndecl));
2643       tree retvar = create_tmp_var(rettype, "RESULT");
2644       gcc_assert(results != NULL && results->size() == this->vals_->size());
2645       Expression_list::const_iterator pv = this->vals_->begin();
2646       Typed_identifier_list::const_iterator pr = results->begin();
2647       for (tree field = TYPE_FIELDS(rettype);
2648            field != NULL_TREE;
2649            ++pv, ++pr, field = DECL_CHAIN(field))
2650         {
2651           gcc_assert(pv != this->vals_->end());
2652           tree val = (*pv)->get_tree(context);
2653           val = Expression::convert_for_assignment(context, pr->type(),
2654                                                    (*pv)->type(), val,
2655                                                    this->location());
2656           if (val == error_mark_node)
2657             return error_mark_node;
2658           tree set = build2(MODIFY_EXPR, void_type_node,
2659                             build3(COMPONENT_REF, TREE_TYPE(field),
2660                                    retvar, field, NULL_TREE),
2661                             val);
2662           SET_EXPR_LOCATION(set, this->location());
2663           append_to_statement_list(set, &stmt_list);
2664         }
2665       tree set = build2(MODIFY_EXPR, void_type_node, DECL_RESULT(fndecl),
2666                         retvar);
2667       append_to_statement_list(this->build_stmt_1(RETURN_EXPR, set),
2668                                &stmt_list);
2669       return stmt_list;
2670     }
2671 }
2672
2673 // Make a return statement.
2674
2675 Statement*
2676 Statement::make_return_statement(const Typed_identifier_list* results,
2677                                  Expression_list* vals,
2678                                  source_location location)
2679 {
2680   return new Return_statement(results, vals, location);
2681 }
2682
2683 // A break or continue statement.
2684
2685 class Bc_statement : public Statement
2686 {
2687  public:
2688   Bc_statement(bool is_break, Unnamed_label* label, source_location location)
2689     : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
2690       label_(label), is_break_(is_break)
2691   { }
2692
2693   bool
2694   is_break() const
2695   { return this->is_break_; }
2696
2697  protected:
2698   int
2699   do_traverse(Traverse*)
2700   { return TRAVERSE_CONTINUE; }
2701
2702   bool
2703   do_may_fall_through() const
2704   { return false; }
2705
2706   tree
2707   do_get_tree(Translate_context*)
2708   { return this->label_->get_goto(this->location()); }
2709
2710  private:
2711   // The label that this branches to.
2712   Unnamed_label* label_;
2713   // True if this is "break", false if it is "continue".
2714   bool is_break_;
2715 };
2716
2717 // Make a break statement.
2718
2719 Statement*
2720 Statement::make_break_statement(Unnamed_label* label, source_location location)
2721 {
2722   return new Bc_statement(true, label, location);
2723 }
2724
2725 // Make a continue statement.
2726
2727 Statement*
2728 Statement::make_continue_statement(Unnamed_label* label,
2729                                    source_location location)
2730 {
2731   return new Bc_statement(false, label, location);
2732 }
2733
2734 // A goto statement.
2735
2736 class Goto_statement : public Statement
2737 {
2738  public:
2739   Goto_statement(Label* label, source_location location)
2740     : Statement(STATEMENT_GOTO, location),
2741       label_(label)
2742   { }
2743
2744  protected:
2745   int
2746   do_traverse(Traverse*)
2747   { return TRAVERSE_CONTINUE; }
2748
2749   void
2750   do_check_types(Gogo*);
2751
2752   bool
2753   do_may_fall_through() const
2754   { return false; }
2755
2756   tree
2757   do_get_tree(Translate_context*);
2758
2759  private:
2760   Label* label_;
2761 };
2762
2763 // Check types for a label.  There aren't any types per se, but we use
2764 // this to give an error if the label was never defined.
2765
2766 void
2767 Goto_statement::do_check_types(Gogo*)
2768 {
2769   if (!this->label_->is_defined())
2770     {
2771       error_at(this->location(), "reference to undefined label %qs",
2772                Gogo::message_name(this->label_->name()).c_str());
2773       this->set_is_error();
2774     }
2775 }
2776
2777 // Return the tree for the goto statement.
2778
2779 tree
2780 Goto_statement::do_get_tree(Translate_context*)
2781 {
2782   return this->build_stmt_1(GOTO_EXPR, this->label_->get_decl());
2783 }
2784
2785 // Make a goto statement.
2786
2787 Statement*
2788 Statement::make_goto_statement(Label* label, source_location location)
2789 {
2790   return new Goto_statement(label, location);
2791 }
2792
2793 // A goto statement to an unnamed label.
2794
2795 class Goto_unnamed_statement : public Statement
2796 {
2797  public:
2798   Goto_unnamed_statement(Unnamed_label* label, source_location location)
2799     : Statement(STATEMENT_GOTO_UNNAMED, location),
2800       label_(label)
2801   { }
2802
2803  protected:
2804   int
2805   do_traverse(Traverse*)
2806   { return TRAVERSE_CONTINUE; }
2807
2808   bool
2809   do_may_fall_through() const
2810   { return false; }
2811
2812   tree
2813   do_get_tree(Translate_context*)
2814   { return this->label_->get_goto(this->location()); }
2815
2816  private:
2817   Unnamed_label* label_;
2818 };
2819
2820 // Make a goto statement to an unnamed label.
2821
2822 Statement*
2823 Statement::make_goto_unnamed_statement(Unnamed_label* label,
2824                                        source_location location)
2825 {
2826   return new Goto_unnamed_statement(label, location);
2827 }
2828
2829 // Class Label_statement.
2830
2831 // Traversal.
2832
2833 int
2834 Label_statement::do_traverse(Traverse*)
2835 {
2836   return TRAVERSE_CONTINUE;
2837 }
2838
2839 // Return a tree defining this label.
2840
2841 tree
2842 Label_statement::do_get_tree(Translate_context*)
2843 {
2844   return this->build_stmt_1(LABEL_EXPR, this->label_->get_decl());
2845 }
2846
2847 // Make a label statement.
2848
2849 Statement*
2850 Statement::make_label_statement(Label* label, source_location location)
2851 {
2852   return new Label_statement(label, location);
2853 }
2854
2855 // An unnamed label statement.
2856
2857 class Unnamed_label_statement : public Statement
2858 {
2859  public:
2860   Unnamed_label_statement(Unnamed_label* label)
2861     : Statement(STATEMENT_UNNAMED_LABEL, label->location()),
2862       label_(label)
2863   { }
2864
2865  protected:
2866   int
2867   do_traverse(Traverse*)
2868   { return TRAVERSE_CONTINUE; }
2869
2870   tree
2871   do_get_tree(Translate_context*)
2872   { return this->label_->get_definition(); }
2873
2874  private:
2875   // The label.
2876   Unnamed_label* label_;
2877 };
2878
2879 // Make an unnamed label statement.
2880
2881 Statement*
2882 Statement::make_unnamed_label_statement(Unnamed_label* label)
2883 {
2884   return new Unnamed_label_statement(label);
2885 }
2886
2887 // An if statement.
2888
2889 class If_statement : public Statement
2890 {
2891  public:
2892   If_statement(Expression* cond, Block* then_block, Block* else_block,
2893                source_location location)
2894     : Statement(STATEMENT_IF, location),
2895       cond_(cond), then_block_(then_block), else_block_(else_block)
2896   { }
2897
2898  protected:
2899   int
2900   do_traverse(Traverse*);
2901
2902   void
2903   do_determine_types();
2904
2905   void
2906   do_check_types(Gogo*);
2907
2908   bool
2909   do_may_fall_through() const;
2910
2911   tree
2912   do_get_tree(Translate_context*);
2913
2914  private:
2915   Expression* cond_;
2916   Block* then_block_;
2917   Block* else_block_;
2918 };
2919
2920 // Traversal.
2921
2922 int
2923 If_statement::do_traverse(Traverse* traverse)
2924 {
2925   if (this->cond_ != NULL)
2926     {
2927       if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
2928         return TRAVERSE_EXIT;
2929     }
2930   if (this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
2931     return TRAVERSE_EXIT;
2932   if (this->else_block_ != NULL)
2933     {
2934       if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
2935         return TRAVERSE_EXIT;
2936     }
2937   return TRAVERSE_CONTINUE;
2938 }
2939
2940 void
2941 If_statement::do_determine_types()
2942 {
2943   if (this->cond_ != NULL)
2944     {
2945       Type_context context(Type::lookup_bool_type(), false);
2946       this->cond_->determine_type(&context);
2947     }
2948   this->then_block_->determine_types();
2949   if (this->else_block_ != NULL)
2950     this->else_block_->determine_types();
2951 }
2952
2953 // Check types.
2954
2955 void
2956 If_statement::do_check_types(Gogo*)
2957 {
2958   if (this->cond_ != NULL)
2959     {
2960       Type* type = this->cond_->type();
2961       if (type->is_error_type())
2962         this->set_is_error();
2963       else if (!type->is_boolean_type())
2964         this->report_error(_("expected boolean expression"));
2965     }
2966 }
2967
2968 // Whether the overall statement may fall through.
2969
2970 bool
2971 If_statement::do_may_fall_through() const
2972 {
2973   return (this->else_block_ == NULL
2974           || this->then_block_->may_fall_through()
2975           || this->else_block_->may_fall_through());
2976 }
2977
2978 // Get tree.
2979
2980 tree
2981 If_statement::do_get_tree(Translate_context* context)
2982 {
2983   gcc_assert(this->cond_ == NULL || this->cond_->type()->is_boolean_type());
2984   tree ret = build3(COND_EXPR, void_type_node,
2985                     (this->cond_ == NULL
2986                      ? boolean_true_node
2987                      : this->cond_->get_tree(context)),
2988                     this->then_block_->get_tree(context),
2989                     (this->else_block_ == NULL
2990                      ? NULL_TREE
2991                      : this->else_block_->get_tree(context)));
2992   SET_EXPR_LOCATION(ret, this->location());
2993   return ret;
2994 }
2995
2996 // Make an if statement.
2997
2998 Statement*
2999 Statement::make_if_statement(Expression* cond, Block* then_block,
3000                              Block* else_block, source_location location)
3001 {
3002   return new If_statement(cond, then_block, else_block, location);
3003 }
3004
3005 // Class Case_clauses::Case_clause.
3006
3007 // Traversal.
3008
3009 int
3010 Case_clauses::Case_clause::traverse(Traverse* traverse)
3011 {
3012   if (this->cases_ != NULL
3013       && (traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
3014     {
3015       if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
3016         return TRAVERSE_EXIT;
3017     }
3018   if (this->statements_ != NULL)
3019     {
3020       if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3021         return TRAVERSE_EXIT;
3022     }
3023   return TRAVERSE_CONTINUE;
3024 }
3025
3026 // Check whether all the case expressions are integer constants.
3027
3028 bool
3029 Case_clauses::Case_clause::is_constant() const
3030 {
3031   if (this->cases_ != NULL)
3032     {
3033       for (Expression_list::const_iterator p = this->cases_->begin();
3034            p != this->cases_->end();
3035            ++p)
3036         if (!(*p)->is_constant() || (*p)->type()->integer_type() == NULL)
3037           return false;
3038     }
3039   return true;
3040 }
3041
3042 // Lower a case clause for a nonconstant switch.  VAL_TEMP is the
3043 // value we are switching on; it may be NULL.  If START_LABEL is not
3044 // NULL, it goes at the start of the statements, after the condition
3045 // test.  We branch to FINISH_LABEL at the end of the statements.
3046
3047 void
3048 Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
3049                                  Unnamed_label* start_label,
3050                                  Unnamed_label* finish_label) const
3051 {
3052   source_location loc = this->location_;
3053   Unnamed_label* next_case_label;
3054   if (this->cases_ == NULL || this->cases_->empty())
3055     {
3056       gcc_assert(this->is_default_);
3057       next_case_label = NULL;
3058     }
3059   else
3060     {
3061       Expression* cond = NULL;
3062
3063       for (Expression_list::const_iterator p = this->cases_->begin();
3064            p != this->cases_->end();
3065            ++p)
3066         {
3067           Expression* this_cond;
3068           if (val_temp == NULL)
3069             this_cond = *p;
3070           else
3071             {
3072               Expression* ref = Expression::make_temporary_reference(val_temp,
3073                                                                      loc);
3074               this_cond = Expression::make_binary(OPERATOR_EQEQ, ref, *p, loc);
3075             }
3076
3077           if (cond == NULL)
3078             cond = this_cond;
3079           else
3080             cond = Expression::make_binary(OPERATOR_OROR, cond, this_cond, loc);
3081         }
3082
3083       Block* then_block = new Block(b, loc);
3084       next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
3085       Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
3086                                                             loc);
3087       then_block->add_statement(s);
3088
3089       // if !COND { goto NEXT_CASE_LABEL }
3090       cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3091       s = Statement::make_if_statement(cond, then_block, NULL, loc);
3092       b->add_statement(s);
3093     }
3094
3095   if (start_label != NULL)
3096     b->add_statement(Statement::make_unnamed_label_statement(start_label));
3097
3098   if (this->statements_ != NULL)
3099     b->add_statement(Statement::make_block_statement(this->statements_, loc));
3100
3101   Statement* s = Statement::make_goto_unnamed_statement(finish_label, loc);
3102   b->add_statement(s);
3103
3104   if (next_case_label != NULL)
3105     b->add_statement(Statement::make_unnamed_label_statement(next_case_label));
3106 }
3107
3108 // Determine types.
3109
3110 void
3111 Case_clauses::Case_clause::determine_types(Type* type)
3112 {
3113   if (this->cases_ != NULL)
3114     {
3115       Type_context case_context(type, false);
3116       for (Expression_list::iterator p = this->cases_->begin();
3117            p != this->cases_->end();
3118            ++p)
3119         (*p)->determine_type(&case_context);
3120     }
3121   if (this->statements_ != NULL)
3122     this->statements_->determine_types();
3123 }
3124
3125 // Check types.  Returns false if there was an error.
3126
3127 bool
3128 Case_clauses::Case_clause::check_types(Type* type)
3129 {
3130   if (this->cases_ != NULL)
3131     {
3132       for (Expression_list::iterator p = this->cases_->begin();
3133            p != this->cases_->end();
3134            ++p)
3135         {
3136           if (!Type::are_assignable(type, (*p)->type(), NULL)
3137               && !Type::are_assignable((*p)->type(), type, NULL))
3138             {
3139               error_at((*p)->location(),
3140                        "type mismatch between switch value and case clause");
3141               return false;
3142             }
3143         }
3144     }
3145   return true;
3146 }
3147
3148 // Return true if this clause may fall through to the following
3149 // statements.  Note that this is not the same as whether the case
3150 // uses the "fallthrough" keyword.
3151
3152 bool
3153 Case_clauses::Case_clause::may_fall_through() const
3154 {
3155   if (this->statements_ == NULL)
3156     return true;
3157   return this->statements_->may_fall_through();
3158 }
3159
3160 // Build up the body of a SWITCH_EXPR.
3161
3162 void
3163 Case_clauses::Case_clause::get_constant_tree(Translate_context* context,
3164                                              Unnamed_label* break_label,
3165                                              Case_constants* case_constants,
3166                                              tree* stmt_list) const
3167 {
3168   if (this->cases_ != NULL)
3169     {
3170       for (Expression_list::const_iterator p = this->cases_->begin();
3171            p != this->cases_->end();
3172            ++p)
3173         {
3174           Type* itype;
3175           mpz_t ival;
3176           mpz_init(ival);
3177           if (!(*p)->integer_constant_value(true, ival, &itype))
3178             gcc_unreachable();
3179           gcc_assert(itype != NULL);
3180           tree type_tree = itype->get_tree(context->gogo());
3181           tree val = Expression::integer_constant_tree(ival, type_tree);
3182           mpz_clear(ival);
3183
3184           if (val != error_mark_node)
3185             {
3186               gcc_assert(TREE_CODE(val) == INTEGER_CST);
3187
3188               std::pair<Case_constants::iterator, bool> ins =
3189                 case_constants->insert(val);
3190               if (!ins.second)
3191                 {
3192                   // Value was already present.
3193                   warning_at(this->location_, 0,
3194                              "duplicate case value will never match");
3195                   continue;
3196                 }
3197
3198               tree label = create_artificial_label(this->location_);
3199               append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
3200                                               val, NULL_TREE, label),
3201                                        stmt_list);
3202             }
3203         }
3204     }
3205
3206   if (this->is_default_)
3207     {
3208       tree label = create_artificial_label(this->location_);
3209       append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
3210                                       NULL_TREE, NULL_TREE, label),
3211                                stmt_list);
3212     }
3213
3214   if (this->statements_ != NULL)
3215     {
3216       tree block_tree = this->statements_->get_tree(context);
3217       if (block_tree != error_mark_node)
3218         append_to_statement_list(block_tree, stmt_list);
3219     }
3220
3221   if (!this->is_fallthrough_)
3222     append_to_statement_list(break_label->get_goto(this->location_), stmt_list);
3223 }
3224
3225 // Class Case_clauses.
3226
3227 // Traversal.
3228
3229 int
3230 Case_clauses::traverse(Traverse* traverse)
3231 {
3232   for (Clauses::iterator p = this->clauses_.begin();
3233        p != this->clauses_.end();
3234        ++p)
3235     {
3236       if (p->traverse(traverse) == TRAVERSE_EXIT)
3237         return TRAVERSE_EXIT;
3238     }
3239   return TRAVERSE_CONTINUE;
3240 }
3241
3242 // Check whether all the case expressions are constant.
3243
3244 bool
3245 Case_clauses::is_constant() const
3246 {
3247   for (Clauses::const_iterator p = this->clauses_.begin();
3248        p != this->clauses_.end();
3249        ++p)
3250     if (!p->is_constant())
3251       return false;
3252   return true;
3253 }
3254
3255 // Lower case clauses for a nonconstant switch.
3256
3257 void
3258 Case_clauses::lower(Block* b, Temporary_statement* val_temp,
3259                     Unnamed_label* break_label) const
3260 {
3261   // The default case.
3262   const Case_clause* default_case = NULL;
3263
3264   // The label for the fallthrough of the previous case.
3265   Unnamed_label* last_fallthrough_label = NULL;
3266
3267   // The label for the start of the default case.  This is used if the
3268   // case before the default case falls through.
3269   Unnamed_label* default_start_label = NULL;
3270
3271   // The label for the end of the default case.  This normally winds
3272   // up as BREAK_LABEL, but it will be different if the default case
3273   // falls through.
3274   Unnamed_label* default_finish_label = NULL;
3275
3276   for (Clauses::const_iterator p = this->clauses_.begin();
3277        p != this->clauses_.end();
3278        ++p)
3279     {
3280       // The label to use for the start of the statements for this
3281       // case.  This is NULL unless the previous case falls through.
3282       Unnamed_label* start_label = last_fallthrough_label;
3283
3284       // The label to jump to after the end of the statements for this
3285       // case.
3286       Unnamed_label* finish_label = break_label;
3287
3288       last_fallthrough_label = NULL;
3289       if (p->is_fallthrough() && p + 1 != this->clauses_.end())
3290         {
3291           finish_label = new Unnamed_label(p->location());
3292           last_fallthrough_label = finish_label;
3293         }
3294
3295       if (!p->is_default())
3296         p->lower(b, val_temp, start_label, finish_label);
3297       else
3298         {
3299           // We have to move the default case to the end, so that we
3300           // only use it if all the other tests fail.
3301           default_case = &*p;
3302           default_start_label = start_label;
3303           default_finish_label = finish_label;
3304         }
3305     }
3306
3307   if (default_case != NULL)
3308     default_case->lower(b, val_temp, default_start_label,
3309                         default_finish_label);
3310       
3311 }
3312
3313 // Determine types.
3314
3315 void
3316 Case_clauses::determine_types(Type* type)
3317 {
3318   for (Clauses::iterator p = this->clauses_.begin();
3319        p != this->clauses_.end();
3320        ++p)
3321     p->determine_types(type);
3322 }
3323
3324 // Check types.  Returns false if there was an error.
3325
3326 bool
3327 Case_clauses::check_types(Type* type)
3328 {
3329   bool ret = true;
3330   for (Clauses::iterator p = this->clauses_.begin();
3331        p != this->clauses_.end();
3332        ++p)
3333     {
3334       if (!p->check_types(type))
3335         ret = false;
3336     }
3337   return ret;
3338 }
3339
3340 // Return true if these clauses may fall through to the statements
3341 // following the switch statement.
3342
3343 bool
3344 Case_clauses::may_fall_through() const
3345 {
3346   bool found_default = false;
3347   for (Clauses::const_iterator p = this->clauses_.begin();
3348        p != this->clauses_.end();
3349        ++p)
3350     {
3351       if (p->may_fall_through() && !p->is_fallthrough())
3352         return true;
3353       if (p->is_default())
3354         found_default = true;
3355     }
3356   return !found_default;
3357 }
3358
3359 // Return a tree when all case expressions are constants.
3360
3361 tree
3362 Case_clauses::get_constant_tree(Translate_context* context,
3363                                 Unnamed_label* break_label) const
3364 {
3365   Case_constants case_constants;
3366   tree stmt_list = NULL_TREE;
3367   for (Clauses::const_iterator p = this->clauses_.begin();
3368        p != this->clauses_.end();
3369        ++p)
3370     p->get_constant_tree(context, break_label, &case_constants,
3371                          &stmt_list);
3372   return stmt_list;
3373 }
3374
3375 // A constant switch statement.  A Switch_statement is lowered to this
3376 // when all the cases are constants.
3377
3378 class Constant_switch_statement : public Statement
3379 {
3380  public:
3381   Constant_switch_statement(Expression* val, Case_clauses* clauses,
3382                             Unnamed_label* break_label,
3383                             source_location location)
3384     : Statement(STATEMENT_CONSTANT_SWITCH, location),
3385       val_(val), clauses_(clauses), break_label_(break_label)
3386   { }
3387
3388  protected:
3389   int
3390   do_traverse(Traverse*);
3391
3392   void
3393   do_determine_types();
3394
3395   void
3396   do_check_types(Gogo*);
3397
3398   bool
3399   do_may_fall_through() const;
3400
3401   tree
3402   do_get_tree(Translate_context*);
3403
3404  private:
3405   // The value to switch on.
3406   Expression* val_;
3407   // The case clauses.
3408   Case_clauses* clauses_;
3409   // The break label, if needed.
3410   Unnamed_label* break_label_;
3411 };
3412
3413 // Traversal.
3414
3415 int
3416 Constant_switch_statement::do_traverse(Traverse* traverse)
3417 {
3418   if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3419     return TRAVERSE_EXIT;
3420   return this->clauses_->traverse(traverse);
3421 }
3422
3423 // Determine types.
3424
3425 void
3426 Constant_switch_statement::do_determine_types()
3427 {
3428   this->val_->determine_type_no_context();
3429   this->clauses_->determine_types(this->val_->type());
3430 }
3431
3432 // Check types.
3433
3434 void
3435 Constant_switch_statement::do_check_types(Gogo*)
3436 {
3437   if (!this->clauses_->check_types(this->val_->type()))
3438     this->set_is_error();
3439 }
3440
3441 // Return whether this switch may fall through.
3442
3443 bool
3444 Constant_switch_statement::do_may_fall_through() const
3445 {
3446   if (this->clauses_ == NULL)
3447     return true;
3448
3449   // If we have a break label, then some case needed it.  That implies
3450   // that the switch statement as a whole can fall through.
3451   if (this->break_label_ != NULL)
3452     return true;
3453
3454   return this->clauses_->may_fall_through();
3455 }
3456
3457 // Convert to GENERIC.
3458
3459 tree
3460 Constant_switch_statement::do_get_tree(Translate_context* context)
3461 {
3462   tree switch_val_tree = this->val_->get_tree(context);
3463
3464   Unnamed_label* break_label = this->break_label_;
3465   if (break_label == NULL)
3466     break_label = new Unnamed_label(this->location());
3467
3468   tree stmt_list = NULL_TREE;
3469   tree s = build3(SWITCH_EXPR, void_type_node, switch_val_tree,
3470                   this->clauses_->get_constant_tree(context, break_label),
3471                   NULL_TREE);
3472   SET_EXPR_LOCATION(s, this->location());
3473   append_to_statement_list(s, &stmt_list);
3474
3475   append_to_statement_list(break_label->get_definition(), &stmt_list);
3476
3477   return stmt_list;
3478 }
3479
3480 // Class Switch_statement.
3481
3482 // Traversal.
3483
3484 int
3485 Switch_statement::do_traverse(Traverse* traverse)
3486 {
3487   if (this->val_ != NULL)
3488     {
3489       if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
3490         return TRAVERSE_EXIT;
3491     }
3492   return this->clauses_->traverse(traverse);
3493 }
3494
3495 // Lower a Switch_statement to a Constant_switch_statement or a series
3496 // of if statements.
3497
3498 Statement*
3499 Switch_statement::do_lower(Gogo*, Block* enclosing)
3500 {
3501   source_location loc = this->location();
3502
3503   if (this->val_ != NULL
3504       && (this->val_->is_error_expression()
3505           || this->val_->type()->is_error_type()))
3506     return Statement::make_error_statement(loc);
3507
3508   if (this->val_ != NULL
3509       && this->val_->type()->integer_type() != NULL
3510       && !this->clauses_->empty()
3511       && this->clauses_->is_constant())
3512     return new Constant_switch_statement(this->val_, this->clauses_,
3513                                          this->break_label_, loc);
3514
3515   Block* b = new Block(enclosing, loc);
3516
3517   if (this->clauses_->empty())
3518     {
3519       Expression* val = this->val_;
3520       if (val == NULL)
3521         val = Expression::make_boolean(true, loc);
3522       return Statement::make_statement(val);
3523     }
3524
3525   Temporary_statement* val_temp;
3526   if (this->val_ == NULL)
3527     val_temp = NULL;
3528   else
3529     {
3530       // var val_temp VAL_TYPE = VAL
3531       val_temp = Statement::make_temporary(NULL, this->val_, loc);
3532       b->add_statement(val_temp);
3533     }
3534
3535   this->clauses_->lower(b, val_temp, this->break_label());
3536
3537   Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3538   b->add_statement(s);
3539
3540   return Statement::make_block_statement(b, loc);
3541 }
3542
3543 // Return the break label for this switch statement, creating it if
3544 // necessary.
3545
3546 Unnamed_label*
3547 Switch_statement::break_label()
3548 {
3549   if (this->break_label_ == NULL)
3550     this->break_label_ = new Unnamed_label(this->location());
3551   return this->break_label_;
3552 }
3553
3554 // Make a switch statement.
3555
3556 Switch_statement*
3557 Statement::make_switch_statement(Expression* val, source_location location)
3558 {
3559   return new Switch_statement(val, location);
3560 }
3561
3562 // Class Type_case_clauses::Type_case_clause.
3563
3564 // Traversal.
3565
3566 int
3567 Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
3568 {
3569   if (!this->is_default_
3570       && ((traverse->traverse_mask()
3571            & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
3572       && Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3573     return TRAVERSE_EXIT;
3574   if (this->statements_ != NULL)
3575     return this->statements_->traverse(traverse);
3576   return TRAVERSE_CONTINUE;
3577 }
3578
3579 // Lower one clause in a type switch.  Add statements to the block B.
3580 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3581 // BREAK_LABEL is the label at the end of the type switch.
3582 // *STMTS_LABEL, if not NULL, is a label to put at the start of the
3583 // statements.
3584
3585 void
3586 Type_case_clauses::Type_case_clause::lower(Block* b,
3587                                            Temporary_statement* descriptor_temp,
3588                                            Unnamed_label* break_label,
3589                                            Unnamed_label** stmts_label) const
3590 {
3591   source_location loc = this->location_;
3592
3593   Unnamed_label* next_case_label = NULL;
3594   if (!this->is_default_)
3595     {
3596       Type* type = this->type_;
3597
3598       Expression* cond;
3599       // The language permits case nil, which is of course a constant
3600       // rather than a type.  It will appear here as an invalid
3601       // forwarding type.
3602       if (type->is_nil_constant_as_type())
3603         {
3604           Expression* ref =
3605             Expression::make_temporary_reference(descriptor_temp, loc);
3606           cond = Expression::make_binary(OPERATOR_EQEQ, ref,
3607                                          Expression::make_nil(loc),
3608                                          loc);
3609         }
3610       else
3611         {
3612           Expression* func;
3613           if (type->interface_type() == NULL)
3614             {
3615               // func ifacetypeeq(*descriptor, *descriptor) bool
3616               static Named_object* ifacetypeeq;
3617               if (ifacetypeeq == NULL)
3618                 {
3619                   const source_location bloc = BUILTINS_LOCATION;
3620                   Typed_identifier_list* param_types =
3621                     new Typed_identifier_list();
3622                   Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3623                   param_types->push_back(Typed_identifier("a", descriptor_type,
3624                                                           bloc));
3625                   param_types->push_back(Typed_identifier("b", descriptor_type,
3626                                                           bloc));
3627                   Typed_identifier_list* ret_types =
3628                     new Typed_identifier_list();
3629                   Type* bool_type = Type::lookup_bool_type();
3630                   ret_types->push_back(Typed_identifier("", bool_type, bloc));
3631                   Function_type* fntype = Type::make_function_type(NULL,
3632                                                                    param_types,
3633                                                                    ret_types,
3634                                                                    bloc);
3635                   ifacetypeeq =
3636                     Named_object::make_function_declaration("ifacetypeeq", NULL,
3637                                                             fntype, bloc);
3638                   const char* n = "runtime.ifacetypeeq";
3639                   ifacetypeeq->func_declaration_value()->set_asm_name(n);
3640                 }
3641
3642               // ifacetypeeq(descriptor_temp, DESCRIPTOR)
3643               func = Expression::make_func_reference(ifacetypeeq, NULL, loc);
3644             }
3645           else
3646             {
3647               // func ifaceI2Tp(*descriptor, *descriptor) bool
3648               static Named_object* ifaceI2Tp;
3649               if (ifaceI2Tp == NULL)
3650                 {
3651                   const source_location bloc = BUILTINS_LOCATION;
3652                   Typed_identifier_list* param_types =
3653                     new Typed_identifier_list();
3654                   Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3655                   param_types->push_back(Typed_identifier("a", descriptor_type,
3656                                                           bloc));
3657                   param_types->push_back(Typed_identifier("b", descriptor_type,
3658                                                           bloc));
3659                   Typed_identifier_list* ret_types =
3660                     new Typed_identifier_list();
3661                   Type* bool_type = Type::lookup_bool_type();
3662                   ret_types->push_back(Typed_identifier("", bool_type, bloc));
3663                   Function_type* fntype = Type::make_function_type(NULL,
3664                                                                    param_types,
3665                                                                    ret_types,
3666                                                                    bloc);
3667                   ifaceI2Tp =
3668                     Named_object::make_function_declaration("ifaceI2Tp", NULL,
3669                                                             fntype, bloc);
3670                   const char* n = "runtime.ifaceI2Tp";
3671                   ifaceI2Tp->func_declaration_value()->set_asm_name(n);
3672                 }
3673
3674               // ifaceI2Tp(descriptor_temp, DESCRIPTOR)
3675               func = Expression::make_func_reference(ifaceI2Tp, NULL, loc);
3676             }
3677           Expression_list* params = new Expression_list();
3678           params->push_back(Expression::make_type_descriptor(type, loc));
3679           Expression* ref =
3680             Expression::make_temporary_reference(descriptor_temp, loc);
3681           params->push_back(ref);
3682           cond = Expression::make_call(func, params, false, loc);
3683         }
3684
3685       Unnamed_label* dest;
3686       if (!this->is_fallthrough_)
3687         {
3688           // if !COND { goto NEXT_CASE_LABEL }
3689           next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
3690           dest = next_case_label;
3691           cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
3692         }
3693       else
3694         {
3695           // if COND { goto STMTS_LABEL }
3696           gcc_assert(stmts_label != NULL);
3697           if (*stmts_label == NULL)
3698             *stmts_label = new Unnamed_label(UNKNOWN_LOCATION);
3699           dest = *stmts_label;
3700         }
3701       Block* then_block = new Block(b, loc);
3702       Statement* s = Statement::make_goto_unnamed_statement(dest, loc);
3703       then_block->add_statement(s);
3704       s = Statement::make_if_statement(cond, then_block, NULL, loc);
3705       b->add_statement(s);
3706     }
3707
3708   if (this->statements_ != NULL
3709       || (!this->is_fallthrough_
3710           && stmts_label != NULL
3711           && *stmts_label != NULL))
3712     {
3713       gcc_assert(!this->is_fallthrough_);
3714       if (stmts_label != NULL && *stmts_label != NULL)
3715         {
3716           gcc_assert(!this->is_default_);
3717           if (this->statements_ != NULL)
3718             (*stmts_label)->set_location(this->statements_->start_location());
3719           Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
3720           b->add_statement(s);
3721           *stmts_label = NULL;
3722         }
3723       if (this->statements_ != NULL)
3724         b->add_statement(Statement::make_block_statement(this->statements_,
3725                                                          loc));
3726     }
3727
3728   if (this->is_fallthrough_)
3729     gcc_assert(next_case_label == NULL);
3730   else
3731     {
3732       source_location gloc = (this->statements_ == NULL
3733                               ? loc
3734                               : this->statements_->end_location());
3735       b->add_statement(Statement::make_goto_unnamed_statement(break_label,
3736                                                               gloc));
3737       if (next_case_label != NULL)
3738         {
3739           Statement* s =
3740             Statement::make_unnamed_label_statement(next_case_label);
3741           b->add_statement(s);
3742         }
3743     }
3744 }
3745
3746 // Class Type_case_clauses.
3747
3748 // Traversal.
3749
3750 int
3751 Type_case_clauses::traverse(Traverse* traverse)
3752 {
3753   for (Type_clauses::iterator p = this->clauses_.begin();
3754        p != this->clauses_.end();
3755        ++p)
3756     {
3757       if (p->traverse(traverse) == TRAVERSE_EXIT)
3758         return TRAVERSE_EXIT;
3759     }
3760   return TRAVERSE_CONTINUE;
3761 }
3762
3763 // Check for duplicate types.
3764
3765 void
3766 Type_case_clauses::check_duplicates() const
3767 {
3768   typedef Unordered_set_hash(const Type*, Type_hash_identical,
3769                              Type_identical) Types_seen;
3770   Types_seen types_seen;
3771   for (Type_clauses::const_iterator p = this->clauses_.begin();
3772        p != this->clauses_.end();
3773        ++p)
3774     {
3775       Type* t = p->type();
3776       if (t == NULL)
3777         continue;
3778       if (t->is_nil_constant_as_type())
3779         t = Type::make_nil_type();
3780       std::pair<Types_seen::iterator, bool> ins = types_seen.insert(t);
3781       if (!ins.second)
3782         error_at(p->location(), "duplicate type in switch");
3783     }
3784 }
3785
3786 // Lower the clauses in a type switch.  Add statements to the block B.
3787 // The type descriptor we are switching on is in DESCRIPTOR_TEMP.
3788 // BREAK_LABEL is the label at the end of the type switch.
3789
3790 void
3791 Type_case_clauses::lower(Block* b, Temporary_statement* descriptor_temp,
3792                          Unnamed_label* break_label) const
3793 {
3794   const Type_case_clause* default_case = NULL;
3795
3796   Unnamed_label* stmts_label = NULL;
3797   for (Type_clauses::const_iterator p = this->clauses_.begin();
3798        p != this->clauses_.end();
3799        ++p)
3800     {
3801       if (!p->is_default())
3802         p->lower(b, descriptor_temp, break_label, &stmts_label);
3803       else
3804         {
3805           // We are generating a series of tests, which means that we
3806           // need to move the default case to the end.
3807           default_case = &*p;
3808         }
3809     }
3810   gcc_assert(stmts_label == NULL);
3811
3812   if (default_case != NULL)
3813     default_case->lower(b, descriptor_temp, break_label, NULL);
3814 }
3815
3816 // Class Type_switch_statement.
3817
3818 // Traversal.
3819
3820 int
3821 Type_switch_statement::do_traverse(Traverse* traverse)
3822 {
3823   if (this->var_ == NULL)
3824     {
3825       if (this->traverse_expression(traverse, &this->expr_) == TRAVERSE_EXIT)
3826         return TRAVERSE_EXIT;
3827     }
3828   if (this->clauses_ != NULL)
3829     return this->clauses_->traverse(traverse);
3830   return TRAVERSE_CONTINUE;
3831 }
3832
3833 // Lower a type switch statement to a series of if statements.  The gc
3834 // compiler is able to generate a table in some cases.  However, that
3835 // does not work for us because we may have type descriptors in
3836 // different shared libraries, so we can't compare them with simple
3837 // equality testing.
3838
3839 Statement*
3840 Type_switch_statement::do_lower(Gogo*, Block* enclosing)
3841 {
3842   const source_location loc = this->location();
3843
3844   if (this->clauses_ != NULL)
3845     this->clauses_->check_duplicates();
3846
3847   Block* b = new Block(enclosing, loc);
3848
3849   Type* val_type = (this->var_ != NULL
3850                     ? this->var_->var_value()->type()
3851                     : this->expr_->type());
3852
3853   // var descriptor_temp DESCRIPTOR_TYPE
3854   Type* descriptor_type = Type::make_type_descriptor_ptr_type();
3855   Temporary_statement* descriptor_temp =
3856     Statement::make_temporary(descriptor_type, NULL, loc);
3857   b->add_statement(descriptor_temp);
3858
3859   if (val_type->interface_type() == NULL)
3860     {
3861       // Doing a type switch on a non-interface type.  Should we issue
3862       // a warning for this case?
3863       // descriptor_temp = DESCRIPTOR
3864       Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3865                                                              loc);
3866       Expression* rhs = Expression::make_type_descriptor(val_type, loc);
3867       Statement* s = Statement::make_assignment(lhs, rhs, loc);
3868       b->add_statement(s);
3869     }
3870   else
3871     {
3872       const source_location bloc = BUILTINS_LOCATION;
3873
3874       // func {efacetype,ifacetype}(*interface) *descriptor
3875       // FIXME: This should be inlined.
3876       Typed_identifier_list* param_types = new Typed_identifier_list();
3877       param_types->push_back(Typed_identifier("i", val_type, bloc));
3878       Typed_identifier_list* ret_types = new Typed_identifier_list();
3879       ret_types->push_back(Typed_identifier("", descriptor_type, bloc));
3880       Function_type* fntype = Type::make_function_type(NULL, param_types,
3881                                                        ret_types, bloc);
3882       bool is_empty = val_type->interface_type()->is_empty();
3883       const char* fnname = is_empty ? "efacetype" : "ifacetype";
3884       Named_object* fn =
3885         Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
3886       const char* asm_name = (is_empty
3887                               ? "runtime.efacetype"
3888                               : "runtime.ifacetype");
3889       fn->func_declaration_value()->set_asm_name(asm_name);
3890
3891       // descriptor_temp = ifacetype(val_temp)
3892       Expression* func = Expression::make_func_reference(fn, NULL, loc);
3893       Expression_list* params = new Expression_list();
3894       Expression* ref;
3895       if (this->var_ == NULL)
3896         ref = this->expr_;
3897       else
3898         ref = Expression::make_var_reference(this->var_, loc);
3899       params->push_back(ref);
3900       Expression* call = Expression::make_call(func, params, false, loc);
3901       Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
3902                                                              loc);
3903       Statement* s = Statement::make_assignment(lhs, call, loc);
3904       b->add_statement(s);
3905     }
3906
3907   if (this->clauses_ != NULL)
3908     this->clauses_->lower(b, descriptor_temp, this->break_label());
3909
3910   Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
3911   b->add_statement(s);
3912
3913   return Statement::make_block_statement(b, loc);
3914 }
3915
3916 // Return the break label for this type switch statement, creating it
3917 // if necessary.
3918
3919 Unnamed_label*
3920 Type_switch_statement::break_label()
3921 {
3922   if (this->break_label_ == NULL)
3923     this->break_label_ = new Unnamed_label(this->location());
3924   return this->break_label_;
3925 }
3926
3927 // Make a type switch statement.
3928
3929 Type_switch_statement*
3930 Statement::make_type_switch_statement(Named_object* var, Expression* expr,
3931                                       source_location location)
3932 {
3933   return new Type_switch_statement(var, expr, location);
3934 }
3935
3936 // Class Select_clauses::Select_clause.
3937
3938 // Traversal.
3939
3940 int
3941 Select_clauses::Select_clause::traverse(Traverse* traverse)
3942 {
3943   if (!this->is_lowered_
3944       && (traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
3945     {
3946       if (this->channel_ != NULL)
3947         {
3948           if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
3949             return TRAVERSE_EXIT;
3950         }
3951       if (this->val_ != NULL)
3952         {
3953           if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
3954             return TRAVERSE_EXIT;
3955         }
3956     }
3957   if (this->statements_ != NULL)
3958     {
3959       if (this->statements_->traverse(traverse) == TRAVERSE_EXIT)
3960         return TRAVERSE_EXIT;
3961     }
3962   return TRAVERSE_CONTINUE;
3963 }
3964
3965 // Lowering.  Here we pull out the channel and the send values, to
3966 // enforce the order of evaluation.  We also add explicit send and
3967 // receive statements to the clauses.
3968
3969 void
3970 Select_clauses::Select_clause::lower(Block* b)
3971 {
3972   if (this->is_default_)
3973     {
3974       gcc_assert(this->channel_ == NULL && this->val_ == NULL);
3975       this->is_lowered_ = true;
3976       return;
3977     }
3978
3979   source_location loc = this->location_;
3980
3981   // Evaluate the channel before the select statement.
3982   Temporary_statement* channel_temp = Statement::make_temporary(NULL,
3983                                                                 this->channel_,
3984                                                                 loc);
3985   b->add_statement(channel_temp);
3986   this->channel_ = Expression::make_temporary_reference(channel_temp, loc);
3987
3988   // If this is a send clause, evaluate the value to send before the
3989   // select statement.
3990   Temporary_statement* val_temp = NULL;
3991   if (this->is_send_)
3992     {
3993       val_temp = Statement::make_temporary(NULL, this->val_, loc);
3994       b->add_statement(val_temp);
3995     }
3996
3997   // Add the send or receive before the rest of the statements if any.
3998   Block *init = new Block(b, loc);
3999   Expression* ref = Expression::make_temporary_reference(channel_temp, loc);
4000   if (this->is_send_)
4001     {
4002       Expression* ref2 = Expression::make_temporary_reference(val_temp, loc);
4003       Send_expression* send = Expression::make_send(ref, ref2, loc);
4004       send->discarding_value();
4005       send->set_for_select();
4006       init->add_statement(Statement::make_statement(send));
4007     }
4008   else
4009     {
4010       Receive_expression* recv = Expression::make_receive(ref, loc);
4011       recv->set_for_select();
4012       if (this->val_ != NULL)
4013         {
4014           gcc_assert(this->var_ == NULL);
4015           init->add_statement(Statement::make_assignment(this->val_, recv,
4016                                                          loc));
4017         }
4018       else if (this->var_ != NULL)
4019         {
4020           this->var_->var_value()->set_init(recv);
4021           this->var_->var_value()->clear_type_from_chan_element();
4022         }
4023       else
4024         {
4025           recv->discarding_value();
4026           init->add_statement(Statement::make_statement(recv));
4027         }
4028     }
4029
4030   if (this->statements_ != NULL)
4031     init->add_statement(Statement::make_block_statement(this->statements_,
4032                                                         loc));
4033
4034   this->statements_ = init;
4035
4036   // Now all references should be handled through the statements, not
4037   // through here.
4038   this->is_lowered_ = true;
4039   this->val_ = NULL;
4040   this->var_ = NULL;
4041 }
4042
4043 // Determine types.
4044
4045 void
4046 Select_clauses::Select_clause::determine_types()
4047 {
4048   gcc_assert(this->is_lowered_);
4049   if (this->statements_ != NULL)
4050     this->statements_->determine_types();
4051 }
4052
4053 // Whether this clause may fall through to the statement which follows
4054 // the overall select statement.
4055
4056 bool
4057 Select_clauses::Select_clause::may_fall_through() const
4058 {
4059   if (this->statements_ == NULL)
4060     return true;
4061   return this->statements_->may_fall_through();
4062 }
4063
4064 // Return a tree for the statements to execute.
4065
4066 tree
4067 Select_clauses::Select_clause::get_statements_tree(Translate_context* context)
4068 {
4069   if (this->statements_ == NULL)
4070     return NULL_TREE;
4071   return this->statements_->get_tree(context);
4072 }
4073
4074 // Class Select_clauses.
4075
4076 // Traversal.
4077
4078 int
4079 Select_clauses::traverse(Traverse* traverse)
4080 {
4081   for (Clauses::iterator p = this->clauses_.begin();
4082        p != this->clauses_.end();
4083        ++p)
4084     {
4085       if (p->traverse(traverse) == TRAVERSE_EXIT)
4086         return TRAVERSE_EXIT;
4087     }
4088   return TRAVERSE_CONTINUE;
4089 }
4090
4091 // Lowering.  Here we pull out the channel and the send values, to
4092 // enforce the order of evaluation.  We also add explicit send and
4093 // receive statements to the clauses.
4094
4095 void
4096 Select_clauses::lower(Block* b)
4097 {
4098   for (Clauses::iterator p = this->clauses_.begin();
4099        p != this->clauses_.end();
4100        ++p)
4101     p->lower(b);
4102 }
4103
4104 // Determine types.
4105
4106 void
4107 Select_clauses::determine_types()
4108 {
4109   for (Clauses::iterator p = this->clauses_.begin();
4110        p != this->clauses_.end();
4111        ++p)
4112     p->determine_types();
4113 }
4114
4115 // Return whether these select clauses fall through to the statement
4116 // following the overall select statement.
4117
4118 bool
4119 Select_clauses::may_fall_through() const
4120 {
4121   for (Clauses::const_iterator p = this->clauses_.begin();
4122        p != this->clauses_.end();
4123        ++p)
4124     if (p->may_fall_through())
4125       return true;
4126   return false;
4127 }
4128
4129 // Return a tree.  We build a call to
4130 //   size_t __go_select(size_t count, _Bool has_default,
4131 //                      channel* channels, _Bool* is_send)
4132 //
4133 // There are COUNT entries in the CHANNELS and IS_SEND arrays.  The
4134 // value in the IS_SEND array is true for send, false for receive.
4135 // __go_select returns an integer from 0 to COUNT, inclusive.  A
4136 // return of 0 means that the default case should be run; this only
4137 // happens if HAS_DEFAULT is non-zero.  Otherwise the number indicates
4138 // the case to run.
4139
4140 // FIXME: This doesn't handle channels which send interface types
4141 // where the receiver has a static type which matches that interface.
4142
4143 tree
4144 Select_clauses::get_tree(Translate_context* context,
4145                          Unnamed_label *break_label,
4146                          source_location location)
4147 {
4148   size_t count = this->clauses_.size();
4149   VEC(constructor_elt, gc)* chan_init = VEC_alloc(constructor_elt, gc, count);
4150   VEC(constructor_elt, gc)* is_send_init = VEC_alloc(constructor_elt, gc,
4151                                                      count);
4152   Select_clause* default_clause = NULL;
4153   tree final_stmt_list = NULL_TREE;
4154   tree channel_type_tree = NULL_TREE;
4155
4156   size_t i = 0;
4157   for (Clauses::iterator p = this->clauses_.begin();
4158        p != this->clauses_.end();
4159        ++p)
4160     {
4161       if (p->is_default())
4162         {
4163           default_clause = &*p;
4164           --count;
4165           continue;
4166         }
4167
4168       if (p->channel()->type()->channel_type() == NULL)
4169         {
4170           // We should have given an error in the send or receive
4171           // statement we created via lowering.
4172           gcc_assert(saw_errors());
4173           return error_mark_node;
4174         }
4175
4176       tree channel_tree = p->channel()->get_tree(context);
4177       if (channel_tree == error_mark_node)
4178         return error_mark_node;
4179       channel_type_tree = TREE_TYPE(channel_tree);
4180
4181       constructor_elt* elt = VEC_quick_push(constructor_elt, chan_init, NULL);
4182       elt->index = build_int_cstu(sizetype, i);
4183       elt->value = channel_tree;
4184
4185       elt = VEC_quick_push(constructor_elt, is_send_init, NULL);
4186       elt->index = build_int_cstu(sizetype, i);
4187       elt->value = p->is_send() ? boolean_true_node : boolean_false_node;
4188
4189       ++i;
4190     }
4191   gcc_assert(i == count);
4192
4193   if (i == 0 && default_clause != NULL)
4194     {
4195       // There is only a default clause.
4196       gcc_assert(final_stmt_list == NULL_TREE);
4197       tree stmt_list = NULL_TREE;
4198       append_to_statement_list(default_clause->get_statements_tree(context),
4199                                &stmt_list);
4200       append_to_statement_list(break_label->get_definition(), &stmt_list);
4201       return stmt_list;
4202     }
4203
4204   tree pointer_chan_type_tree = (channel_type_tree == NULL_TREE
4205                                  ? ptr_type_node
4206                                  : build_pointer_type(channel_type_tree));
4207   tree chans_arg;
4208   tree pointer_boolean_type_tree = build_pointer_type(boolean_type_node);
4209   tree is_sends_arg;
4210
4211   if (i == 0)
4212     {
4213       chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4214                                    null_pointer_node);
4215       is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4216                                       null_pointer_node);
4217     }
4218   else
4219     {
4220       tree index_type_tree = build_index_type(size_int(count - 1));
4221       tree chan_array_type_tree = build_array_type(channel_type_tree,
4222                                                    index_type_tree);
4223       tree chan_constructor = build_constructor(chan_array_type_tree,
4224                                                 chan_init);
4225       tree chan_var = create_tmp_var(chan_array_type_tree, "CHAN");
4226       DECL_IGNORED_P(chan_var) = 0;
4227       DECL_INITIAL(chan_var) = chan_constructor;
4228       DECL_SOURCE_LOCATION(chan_var) = location;
4229       TREE_ADDRESSABLE(chan_var) = 1;
4230       tree decl_expr = build1(DECL_EXPR, void_type_node, chan_var);
4231       SET_EXPR_LOCATION(decl_expr, location);
4232       append_to_statement_list(decl_expr, &final_stmt_list);
4233
4234       tree is_send_array_type_tree = build_array_type(boolean_type_node,
4235                                                       index_type_tree);
4236       tree is_send_constructor = build_constructor(is_send_array_type_tree,
4237                                                    is_send_init);
4238       tree is_send_var = create_tmp_var(is_send_array_type_tree, "ISSEND");
4239       DECL_IGNORED_P(is_send_var) = 0;
4240       DECL_INITIAL(is_send_var) = is_send_constructor;
4241       DECL_SOURCE_LOCATION(is_send_var) = location;
4242       TREE_ADDRESSABLE(is_send_var) = 1;
4243       decl_expr = build1(DECL_EXPR, void_type_node, is_send_var);
4244       SET_EXPR_LOCATION(decl_expr, location);
4245       append_to_statement_list(decl_expr, &final_stmt_list);
4246
4247       chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
4248                                    build_fold_addr_expr_loc(location,
4249                                                             chan_var));
4250       is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
4251                                       build_fold_addr_expr_loc(location,
4252                                                                is_send_var));
4253     }
4254
4255   static tree select_fndecl;
4256   tree call = Gogo::call_builtin(&select_fndecl,
4257                                  location,
4258                                  "__go_select",
4259                                  4,
4260                                  sizetype,
4261                                  sizetype,
4262                                  size_int(count),
4263                                  boolean_type_node,
4264                                  (default_clause == NULL
4265                                   ? boolean_false_node
4266                                   : boolean_true_node),
4267                                  pointer_chan_type_tree,
4268                                  chans_arg,
4269                                  pointer_boolean_type_tree,
4270                                  is_sends_arg);
4271   if (call == error_mark_node)
4272     return error_mark_node;
4273
4274   tree stmt_list = NULL_TREE;
4275
4276   if (default_clause != NULL)
4277     this->add_clause_tree(context, 0, default_clause, break_label, &stmt_list);
4278
4279   i = 1;
4280   for (Clauses::iterator p = this->clauses_.begin();
4281        p != this->clauses_.end();
4282        ++p)
4283     {
4284       if (!p->is_default())
4285         {
4286           this->add_clause_tree(context, i, &*p, break_label, &stmt_list);
4287           ++i;
4288         }
4289     }
4290
4291   append_to_statement_list(break_label->get_definition(), &stmt_list);
4292
4293   tree switch_stmt = build3(SWITCH_EXPR, sizetype, call, stmt_list, NULL_TREE);
4294   SET_EXPR_LOCATION(switch_stmt, location);
4295   append_to_statement_list(switch_stmt, &final_stmt_list);
4296
4297   return final_stmt_list;
4298 }
4299
4300 // Add the tree for CLAUSE to STMT_LIST.
4301
4302 void
4303 Select_clauses::add_clause_tree(Translate_context* context, int case_index,
4304                                 Select_clause* clause,
4305                                 Unnamed_label* bottom_label, tree* stmt_list)
4306 {
4307   tree label = create_artificial_label(clause->location());
4308   append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
4309                                   build_int_cst(sizetype, case_index),
4310                                   NULL_TREE, label),
4311                            stmt_list);
4312   append_to_statement_list(clause->get_statements_tree(context), stmt_list);
4313   tree g = bottom_label->get_goto(clause->statements() == NULL
4314                                   ? clause->location()
4315                                   : clause->statements()->end_location());
4316   append_to_statement_list(g, stmt_list);
4317 }
4318
4319 // Class Select_statement.
4320
4321 // Return the break label for this switch statement, creating it if
4322 // necessary.
4323
4324 Unnamed_label*
4325 Select_statement::break_label()
4326 {
4327   if (this->break_label_ == NULL)
4328     this->break_label_ = new Unnamed_label(this->location());
4329   return this->break_label_;
4330 }
4331
4332 // Lower a select statement.  This will still return a select
4333 // statement, but it will be modified to implement the order of
4334 // evaluation rules, and to include the send and receive statements as
4335 // explicit statements in the clauses.
4336
4337 Statement*
4338 Select_statement::do_lower(Gogo*, Block* enclosing)
4339 {
4340   if (this->is_lowered_)
4341     return this;
4342   Block* b = new Block(enclosing, this->location());
4343   this->clauses_->lower(b);
4344   this->is_lowered_ = true;
4345   b->add_statement(this);
4346   return Statement::make_block_statement(b, this->location());
4347 }
4348
4349 // Return the tree for a select statement.
4350
4351 tree
4352 Select_statement::do_get_tree(Translate_context* context)
4353 {
4354   return this->clauses_->get_tree(context, this->break_label(),
4355                                   this->location());
4356 }
4357
4358 // Make a select statement.
4359
4360 Select_statement*
4361 Statement::make_select_statement(source_location location)
4362 {
4363   return new Select_statement(location);
4364 }
4365
4366 // Class For_statement.
4367
4368 // Traversal.
4369
4370 int
4371 For_statement::do_traverse(Traverse* traverse)
4372 {
4373   if (this->init_ != NULL)
4374     {
4375       if (this->init_->traverse(traverse) == TRAVERSE_EXIT)
4376         return TRAVERSE_EXIT;
4377     }
4378   if (this->cond_ != NULL)
4379     {
4380       if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
4381         return TRAVERSE_EXIT;
4382     }
4383   if (this->post_ != NULL)
4384     {
4385       if (this->post_->traverse(traverse) == TRAVERSE_EXIT)
4386         return TRAVERSE_EXIT;
4387     }
4388   return this->statements_->traverse(traverse);
4389 }
4390
4391 // Lower a For_statement into if statements and gotos.  Getting rid of
4392 // complex statements make it easier to handle garbage collection.
4393
4394 Statement*
4395 For_statement::do_lower(Gogo*, Block* enclosing)
4396 {
4397   Statement* s;
4398   source_location loc = this->location();
4399
4400   Block* b = new Block(enclosing, this->location());
4401   if (this->init_ != NULL)
4402     {
4403       s = Statement::make_block_statement(this->init_,
4404                                           this->init_->start_location());
4405       b->add_statement(s);
4406     }
4407
4408   Unnamed_label* entry = NULL;
4409   if (this->cond_ != NULL)
4410     {
4411       entry = new Unnamed_label(this->location());
4412       b->add_statement(Statement::make_goto_unnamed_statement(entry, loc));
4413     }
4414
4415   Unnamed_label* top = new Unnamed_label(this->location());
4416   b->add_statement(Statement::make_unnamed_label_statement(top));
4417
4418   s = Statement::make_block_statement(this->statements_,
4419                                       this->statements_->start_location());
4420   b->add_statement(s);
4421
4422   source_location end_loc = this->statements_->end_location();
4423
4424   Unnamed_label* cont = this->continue_label_;
4425   if (cont != NULL)
4426     b->add_statement(Statement::make_unnamed_label_statement(cont));
4427
4428   if (this->post_ != NULL)
4429     {
4430       s = Statement::make_block_statement(this->post_,
4431                                           this->post_->start_location());
4432       b->add_statement(s);
4433       end_loc = this->post_->end_location();
4434     }
4435
4436   if (this->cond_ == NULL)
4437     b->add_statement(Statement::make_goto_unnamed_statement(top, end_loc));
4438   else
4439     {
4440       b->add_statement(Statement::make_unnamed_label_statement(entry));
4441
4442       source_location cond_loc = this->cond_->location();
4443       Block* then_block = new Block(b, cond_loc);
4444       s = Statement::make_goto_unnamed_statement(top, cond_loc);
4445       then_block->add_statement(s);
4446
4447       s = Statement::make_if_statement(this->cond_, then_block, NULL, cond_loc);
4448       b->add_statement(s);
4449     }
4450
4451   Unnamed_label* brk = this->break_label_;
4452   if (brk != NULL)
4453     b->add_statement(Statement::make_unnamed_label_statement(brk));
4454
4455   b->set_end_location(end_loc);
4456
4457   return Statement::make_block_statement(b, loc);
4458 }
4459
4460 // Return the break label, creating it if necessary.
4461
4462 Unnamed_label*
4463 For_statement::break_label()
4464 {
4465   if (this->break_label_ == NULL)
4466     this->break_label_ = new Unnamed_label(this->location());
4467   return this->break_label_;
4468 }
4469
4470 // Return the continue LABEL_EXPR.
4471
4472 Unnamed_label*
4473 For_statement::continue_label()
4474 {
4475   if (this->continue_label_ == NULL)
4476     this->continue_label_ = new Unnamed_label(this->location());
4477   return this->continue_label_;
4478 }
4479
4480 // Set the break and continue labels a for statement.  This is used
4481 // when lowering a for range statement.
4482
4483 void
4484 For_statement::set_break_continue_labels(Unnamed_label* break_label,
4485                                          Unnamed_label* continue_label)
4486 {
4487   gcc_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
4488   this->break_label_ = break_label;
4489   this->continue_label_ = continue_label;
4490 }
4491
4492 // Make a for statement.
4493
4494 For_statement*
4495 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
4496                               source_location location)
4497 {
4498   return new For_statement(init, cond, post, location);
4499 }
4500
4501 // Class For_range_statement.
4502
4503 // Traversal.
4504
4505 int
4506 For_range_statement::do_traverse(Traverse* traverse)
4507 {
4508   if (this->traverse_expression(traverse, &this->index_var_) == TRAVERSE_EXIT)
4509     return TRAVERSE_EXIT;
4510   if (this->value_var_ != NULL)
4511     {
4512       if (this->traverse_expression(traverse, &this->value_var_)
4513           == TRAVERSE_EXIT)
4514         return TRAVERSE_EXIT;
4515     }
4516   if (this->traverse_expression(traverse, &this->range_) == TRAVERSE_EXIT)
4517     return TRAVERSE_EXIT;
4518   return this->statements_->traverse(traverse);
4519 }
4520
4521 // Lower a for range statement.  For simplicity we lower this into a
4522 // for statement, which will then be lowered in turn to goto
4523 // statements.
4524
4525 Statement*
4526 For_range_statement::do_lower(Gogo* gogo, Block* enclosing)
4527 {
4528   Type* range_type = this->range_->type();
4529   if (range_type->points_to() != NULL
4530       && range_type->points_to()->array_type() != NULL
4531       && !range_type->points_to()->is_open_array_type())
4532     range_type = range_type->points_to();
4533
4534   Type* index_type;
4535   Type* value_type = NULL;
4536   if (range_type->array_type() != NULL)
4537     {
4538       index_type = Type::lookup_integer_type("int");
4539       value_type = range_type->array_type()->element_type();
4540     }
4541   else if (range_type->is_string_type())
4542     {
4543       index_type = Type::lookup_integer_type("int");
4544       value_type = index_type;
4545     }
4546   else if (range_type->map_type() != NULL)
4547     {
4548       index_type = range_type->map_type()->key_type();
4549       value_type = range_type->map_type()->val_type();
4550     }
4551   else if (range_type->channel_type() != NULL)
4552     {
4553       index_type = range_type->channel_type()->element_type();
4554       if (this->value_var_ != NULL)
4555         {
4556           if (!this->value_var_->type()->is_error_type())
4557             this->report_error(_("too many variables for range clause "
4558                                  "with channel"));
4559           return Statement::make_error_statement(this->location());
4560         }
4561     }
4562   else
4563     {
4564       this->report_error(_("range clause must have "
4565                            "array, slice, setring, map, or channel type"));
4566       return Statement::make_error_statement(this->location());
4567     }
4568
4569   source_location loc = this->location();
4570   Block* temp_block = new Block(enclosing, loc);
4571
4572   Named_object* range_object = NULL;
4573   Temporary_statement* range_temp = NULL;
4574   Var_expression* ve = this->range_->var_expression();
4575   if (ve != NULL)
4576     range_object = ve->named_object();
4577   else
4578     {
4579       range_temp = Statement::make_temporary(NULL, this->range_, loc);
4580       temp_block->add_statement(range_temp);
4581     }
4582
4583   Temporary_statement* index_temp = Statement::make_temporary(index_type,
4584                                                               NULL, loc);
4585   temp_block->add_statement(index_temp);
4586
4587   Temporary_statement* value_temp = NULL;
4588   if (this->value_var_ != NULL)
4589     {
4590       value_temp = Statement::make_temporary(value_type, NULL, loc);
4591       temp_block->add_statement(value_temp);
4592     }
4593
4594   Block* body = new Block(temp_block, loc);
4595
4596   Block* init;
4597   Expression* cond;
4598   Block* iter_init;
4599   Block* post;
4600
4601   // Arrange to do a loop appropriate for the type.  We will produce
4602   //   for INIT ; COND ; POST {
4603   //           ITER_INIT
4604   //           INDEX = INDEX_TEMP
4605   //           VALUE = VALUE_TEMP // If there is a value
4606   //           original statements
4607   //   }
4608
4609   if (range_type->array_type() != NULL)
4610     this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
4611                             index_temp, value_temp, &init, &cond, &iter_init,
4612                             &post);
4613   else if (range_type->is_string_type())
4614     this->lower_range_string(gogo, temp_block, body, range_object, range_temp,
4615                              index_temp, value_temp, &init, &cond, &iter_init,
4616                              &post);
4617   else if (range_type->map_type() != NULL)
4618     this->lower_range_map(gogo, temp_block, body, range_object, range_temp,
4619                           index_temp, value_temp, &init, &cond, &iter_init,
4620                           &post);
4621   else if (range_type->channel_type() != NULL)
4622     this->lower_range_channel(gogo, temp_block, body, range_object, range_temp,
4623                               index_temp, value_temp, &init, &cond, &iter_init,
4624                               &post);
4625   else
4626     gcc_unreachable();
4627
4628   if (iter_init != NULL)
4629     body->add_statement(Statement::make_block_statement(iter_init, loc));
4630
4631   Statement* assign;
4632   Expression* index_ref = Expression::make_temporary_reference(index_temp, loc);
4633   if (this->value_var_ == NULL)
4634     {
4635       assign = Statement::make_assignment(this->index_var_, index_ref, loc);
4636     }
4637   else
4638     {
4639       Expression_list* lhs = new Expression_list();
4640       lhs->push_back(this->index_var_);
4641       lhs->push_back(this->value_var_);
4642
4643       Expression_list* rhs = new Expression_list();
4644       rhs->push_back(index_ref);
4645       rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4646
4647       assign = Statement::make_tuple_assignment(lhs, rhs, loc);
4648     }
4649   body->add_statement(assign);
4650
4651   body->add_statement(Statement::make_block_statement(this->statements_, loc));
4652
4653   body->set_end_location(this->statements_->end_location());
4654
4655   For_statement* loop = Statement::make_for_statement(init, cond, post,
4656                                                       this->location());
4657   loop->add_statements(body);
4658   loop->set_break_continue_labels(this->break_label_, this->continue_label_);
4659
4660   temp_block->add_statement(loop);
4661
4662   return Statement::make_block_statement(temp_block, loc);
4663 }
4664
4665 // Return a reference to the range, which may be in RANGE_OBJECT or in
4666 // RANGE_TEMP.
4667
4668 Expression*
4669 For_range_statement::make_range_ref(Named_object* range_object,
4670                                     Temporary_statement* range_temp,
4671                                     source_location loc)
4672 {
4673   if (range_object != NULL)
4674     return Expression::make_var_reference(range_object, loc);
4675   else
4676     return Expression::make_temporary_reference(range_temp, loc);
4677 }
4678
4679 // Return a call to the predeclared function FUNCNAME passing a
4680 // reference to the temporary variable ARG.
4681
4682 Expression*
4683 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
4684                                   Expression* arg,
4685                                   source_location loc)
4686 {
4687   Named_object* no = gogo->lookup_global(funcname);
4688   gcc_assert(no != NULL && no->is_function_declaration());
4689   Expression* func = Expression::make_func_reference(no, NULL, loc);
4690   Expression_list* params = new Expression_list();
4691   params->push_back(arg);
4692   return Expression::make_call(func, params, false, loc);
4693 }
4694
4695 // Lower a for range over an array or slice.
4696
4697 void
4698 For_range_statement::lower_range_array(Gogo* gogo,
4699                                        Block* enclosing,
4700                                        Block* body_block,
4701                                        Named_object* range_object,
4702                                        Temporary_statement* range_temp,
4703                                        Temporary_statement* index_temp,
4704                                        Temporary_statement* value_temp,
4705                                        Block** pinit,
4706                                        Expression** pcond,
4707                                        Block** piter_init,
4708                                        Block** ppost)
4709 {
4710   source_location loc = this->location();
4711
4712   // The loop we generate:
4713   //   len_temp := len(range)
4714   //   for index_temp = 0; index_temp < len_temp; index_temp++ {
4715   //           value_temp = range[index_temp]
4716   //           index = index_temp
4717   //           value = value_temp
4718   //           original body
4719   //   }
4720
4721   // Set *PINIT to
4722   //   var len_temp int
4723   //   len_temp = len(range)
4724   //   index_temp = 0
4725
4726   Block* init = new Block(enclosing, loc);
4727
4728   Expression* ref = this->make_range_ref(range_object, range_temp, loc);
4729   Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
4730   Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
4731                                                             len_call, loc);
4732   init->add_statement(len_temp);
4733
4734   mpz_t zval;
4735   mpz_init_set_ui(zval, 0UL);
4736   Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4737   mpz_clear(zval);
4738
4739   ref = Expression::make_temporary_reference(index_temp, loc);
4740   Statement* s = Statement::make_assignment(ref, zexpr, loc);
4741   init->add_statement(s);
4742
4743   *pinit = init;
4744
4745   // Set *PCOND to
4746   //   index_temp < len_temp
4747
4748   ref = Expression::make_temporary_reference(index_temp, loc);
4749   Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
4750   Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
4751
4752   *pcond = lt;
4753
4754   // Set *PITER_INIT to
4755   //   value_temp = range[index_temp]
4756
4757   Block* iter_init = NULL;
4758   if (value_temp != NULL)
4759     {
4760       iter_init = new Block(body_block, loc);
4761
4762       ref = this->make_range_ref(range_object, range_temp, loc);
4763       Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
4764       Expression* index = Expression::make_index(ref, ref2, NULL, loc);
4765
4766       ref = Expression::make_temporary_reference(value_temp, loc);
4767       s = Statement::make_assignment(ref, index, loc);
4768
4769       iter_init->add_statement(s);
4770     }
4771   *piter_init = iter_init;
4772
4773   // Set *PPOST to
4774   //   index_temp++
4775
4776   Block* post = new Block(enclosing, loc);
4777   ref = Expression::make_temporary_reference(index_temp, loc);
4778   s = Statement::make_inc_statement(ref);
4779   post->add_statement(s);
4780   *ppost = post;
4781 }
4782
4783 // Lower a for range over a string.
4784
4785 void
4786 For_range_statement::lower_range_string(Gogo* gogo,
4787                                         Block* enclosing,
4788                                         Block* body_block,
4789                                         Named_object* range_object,
4790                                         Temporary_statement* range_temp,
4791                                         Temporary_statement* index_temp,
4792                                         Temporary_statement* value_temp,
4793                                         Block** pinit,
4794                                         Expression** pcond,
4795                                         Block** piter_init,
4796                                         Block** ppost)
4797 {
4798   source_location loc = this->location();
4799
4800   // The loop we generate:
4801   //   var next_index_temp int
4802   //   for index_temp = 0; ; index_temp = next_index_temp {
4803   //           next_index_temp, value_temp = stringiter2(range, index_temp)
4804   //           if next_index_temp == 0 {
4805   //                   break
4806   //           }
4807   //           index = index_temp
4808   //           value = value_temp
4809   //           original body
4810   //   }
4811
4812   // Set *PINIT to
4813   //   var next_index_temp int
4814   //   index_temp = 0
4815
4816   Block* init = new Block(enclosing, loc);
4817
4818   Temporary_statement* next_index_temp =
4819     Statement::make_temporary(index_temp->type(), NULL, loc);
4820   init->add_statement(next_index_temp);
4821
4822   mpz_t zval;
4823   mpz_init_set_ui(zval, 0UL);
4824   Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
4825
4826   Expression* ref = Expression::make_temporary_reference(index_temp, loc);
4827   Statement* s = Statement::make_assignment(ref, zexpr, loc);
4828
4829   init->add_statement(s);
4830   *pinit = init;
4831
4832   // The loop has no condition.
4833
4834   *pcond = NULL;
4835
4836   // Set *PITER_INIT to
4837   //   next_index_temp = runtime.stringiter(range, index_temp)
4838   // or
4839   //   next_index_temp, value_temp = runtime.stringiter2(range, index_temp)
4840   // followed by
4841   //   if next_index_temp == 0 {
4842   //           break
4843   //   }
4844
4845   Block* iter_init = new Block(body_block, loc);
4846
4847   Named_object* no;
4848   if (value_temp == NULL)
4849     {
4850       static Named_object* stringiter;
4851       if (stringiter == NULL)
4852         {
4853           source_location bloc = BUILTINS_LOCATION;
4854           Type* int_type = gogo->lookup_global("int")->type_value();
4855
4856           Typed_identifier_list* params = new Typed_identifier_list();
4857           params->push_back(Typed_identifier("s", Type::make_string_type(),
4858                                              bloc));
4859           params->push_back(Typed_identifier("k", int_type, bloc));
4860
4861           Typed_identifier_list* results = new Typed_identifier_list();
4862           results->push_back(Typed_identifier("", int_type, bloc));
4863
4864           Function_type* fntype = Type::make_function_type(NULL, params,
4865                                                            results, bloc);
4866           stringiter = Named_object::make_function_declaration("stringiter",
4867                                                                NULL, fntype,
4868                                                                bloc);
4869           const char* n = "runtime.stringiter";
4870           stringiter->func_declaration_value()->set_asm_name(n);
4871         }
4872       no = stringiter;
4873     }
4874   else
4875     {
4876       static Named_object* stringiter2;
4877       if (stringiter2 == NULL)
4878         {
4879           source_location bloc = BUILTINS_LOCATION;
4880           Type* int_type = gogo->lookup_global("int")->type_value();
4881
4882           Typed_identifier_list* params = new Typed_identifier_list();
4883           params->push_back(Typed_identifier("s", Type::make_string_type(),
4884                                              bloc));
4885           params->push_back(Typed_identifier("k", int_type, bloc));
4886
4887           Typed_identifier_list* results = new Typed_identifier_list();
4888           results->push_back(Typed_identifier("", int_type, bloc));
4889           results->push_back(Typed_identifier("", int_type, bloc));
4890
4891           Function_type* fntype = Type::make_function_type(NULL, params,
4892                                                            results, bloc);
4893           stringiter2 = Named_object::make_function_declaration("stringiter",
4894                                                                 NULL, fntype,
4895                                                                 bloc);
4896           const char* n = "runtime.stringiter2";
4897           stringiter2->func_declaration_value()->set_asm_name(n);
4898         }
4899       no = stringiter2;
4900     }
4901
4902   Expression* func = Expression::make_func_reference(no, NULL, loc);
4903   Expression_list* params = new Expression_list();
4904   params->push_back(this->make_range_ref(range_object, range_temp, loc));
4905   params->push_back(Expression::make_temporary_reference(index_temp, loc));
4906   Call_expression* call = Expression::make_call(func, params, false, loc);
4907
4908   if (value_temp == NULL)
4909     {
4910       ref = Expression::make_temporary_reference(next_index_temp, loc);
4911       s = Statement::make_assignment(ref, call, loc);
4912     }
4913   else
4914     {
4915       Expression_list* lhs = new Expression_list();
4916       lhs->push_back(Expression::make_temporary_reference(next_index_temp,
4917                                                           loc));
4918       lhs->push_back(Expression::make_temporary_reference(value_temp, loc));
4919
4920       Expression_list* rhs = new Expression_list();
4921       rhs->push_back(Expression::make_call_result(call, 0));
4922       rhs->push_back(Expression::make_call_result(call, 1));
4923
4924       s = Statement::make_tuple_assignment(lhs, rhs, loc);
4925     }
4926   iter_init->add_statement(s);
4927
4928   ref = Expression::make_temporary_reference(next_index_temp, loc);
4929   zexpr = Expression::make_integer(&zval, NULL, loc);
4930   mpz_clear(zval);
4931   Expression* equals = Expression::make_binary(OPERATOR_EQEQ, ref, zexpr, loc);
4932
4933   Block* then_block = new Block(iter_init, loc);
4934   s = Statement::make_break_statement(this->break_label(), loc);
4935   then_block->add_statement(s);
4936
4937   s = Statement::make_if_statement(equals, then_block, NULL, loc);
4938   iter_init->add_statement(s);
4939
4940   *piter_init = iter_init;
4941
4942   // Set *PPOST to
4943   //   index_temp = next_index_temp
4944
4945   Block* post = new Block(enclosing, loc);
4946
4947   Expression* lhs = Expression::make_temporary_reference(index_temp, loc);
4948   Expression* rhs = Expression::make_temporary_reference(next_index_temp, loc);
4949   s = Statement::make_assignment(lhs, rhs, loc);
4950
4951   post->add_statement(s);
4952   *ppost = post;
4953 }
4954
4955 // Lower a for range over a map.
4956
4957 void
4958 For_range_statement::lower_range_map(Gogo* gogo,
4959                                      Block* enclosing,
4960                                      Block* body_block,
4961                                      Named_object* range_object,
4962                                      Temporary_statement* range_temp,
4963                                      Temporary_statement* index_temp,
4964                                      Temporary_statement* value_temp,
4965                                      Block** pinit,
4966                                      Expression** pcond,
4967                                      Block** piter_init,
4968                                      Block** ppost)
4969 {
4970   source_location loc = this->location();
4971
4972   // The runtime uses a struct to handle ranges over a map.  The
4973   // struct is four pointers long.  The first pointer is NULL when we
4974   // have completed the iteration.
4975
4976   // The loop we generate:
4977   //   var hiter map_iteration_struct
4978   //   for mapiterinit(range, &hiter); hiter[0] != nil; mapiternext(&hiter) {
4979   //           mapiter2(hiter, &index_temp, &value_temp)
4980   //           index = index_temp
4981   //           value = value_temp
4982   //           original body
4983   //   }
4984
4985   // Set *PINIT to
4986   //   var hiter map_iteration_struct
4987   //   runtime.mapiterinit(range, &hiter)
4988
4989   Block* init = new Block(enclosing, loc);
4990
4991   const unsigned long map_iteration_size = 4;
4992
4993   mpz_t ival;
4994   mpz_init_set_ui(ival, map_iteration_size);
4995   Expression* iexpr = Expression::make_integer(&ival, NULL, loc);
4996   mpz_clear(ival);
4997
4998   Type* byte_type = gogo->lookup_global("byte")->type_value();
4999   Type* ptr_type = Type::make_pointer_type(byte_type);
5000
5001   Type* map_iteration_type = Type::make_array_type(ptr_type, iexpr);
5002   Type* map_iteration_ptr = Type::make_pointer_type(map_iteration_type);
5003
5004   Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
5005                                                          NULL, loc);
5006   init->add_statement(hiter);
5007
5008   source_location bloc = BUILTINS_LOCATION;
5009   Typed_identifier_list* param_types = new Typed_identifier_list();
5010   param_types->push_back(Typed_identifier("map", this->range_->type(), bloc));
5011   param_types->push_back(Typed_identifier("it", map_iteration_ptr, bloc));
5012   Function_type* fntype = Type::make_function_type(NULL, param_types, NULL,
5013                                                    bloc);
5014
5015   Named_object* mapiterinit =
5016     Named_object::make_function_declaration("mapiterinit", NULL, fntype, bloc);
5017   const char* n = "runtime.mapiterinit";
5018   mapiterinit->func_declaration_value()->set_asm_name(n);
5019
5020   Expression* func = Expression::make_func_reference(mapiterinit, NULL, loc);
5021   Expression_list* params = new Expression_list();
5022   params->push_back(this->make_range_ref(range_object, range_temp, loc));
5023   Expression* ref = Expression::make_temporary_reference(hiter, loc);
5024   params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5025   Expression* call = Expression::make_call(func, params, false, loc);
5026   init->add_statement(Statement::make_statement(call));
5027
5028   *pinit = init;
5029
5030   // Set *PCOND to
5031   //   hiter[0] != nil
5032
5033   ref = Expression::make_temporary_reference(hiter, loc);
5034
5035   mpz_t zval;
5036   mpz_init_set_ui(zval, 0UL);
5037   Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
5038   mpz_clear(zval);
5039
5040   Expression* index = Expression::make_index(ref, zexpr, NULL, loc);
5041
5042   Expression* ne = Expression::make_binary(OPERATOR_NOTEQ, index,
5043                                            Expression::make_nil(loc),
5044                                            loc);
5045
5046   *pcond = ne;
5047
5048   // Set *PITER_INIT to
5049   //   mapiter1(hiter, &index_temp)
5050   // or
5051   //   mapiter2(hiter, &index_temp, &value_temp)
5052
5053   Block* iter_init = new Block(body_block, loc);
5054
5055   param_types = new Typed_identifier_list();
5056   param_types->push_back(Typed_identifier("hiter", map_iteration_ptr, bloc));
5057   Type* pkey_type = Type::make_pointer_type(index_temp->type());
5058   param_types->push_back(Typed_identifier("key", pkey_type, bloc));
5059   if (value_temp != NULL)
5060     {
5061       Type* pval_type = Type::make_pointer_type(value_temp->type());
5062       param_types->push_back(Typed_identifier("val", pval_type, bloc));
5063     }
5064   fntype = Type::make_function_type(NULL, param_types, NULL, bloc);
5065   n = value_temp == NULL ? "mapiter1" : "mapiter2";
5066   Named_object* mapiter = Named_object::make_function_declaration(n, NULL,
5067                                                                   fntype, bloc);
5068   n = value_temp == NULL ? "runtime.mapiter1" : "runtime.mapiter2";
5069   mapiter->func_declaration_value()->set_asm_name(n);
5070
5071   func = Expression::make_func_reference(mapiter, NULL, loc);
5072   params = new Expression_list();
5073   ref = Expression::make_temporary_reference(hiter, loc);
5074   params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5075   ref = Expression::make_temporary_reference(index_temp, loc);
5076   params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5077   if (value_temp != NULL)
5078     {
5079       ref = Expression::make_temporary_reference(value_temp, loc);
5080       params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5081     }
5082   call = Expression::make_call(func, params, false, loc);
5083   iter_init->add_statement(Statement::make_statement(call));
5084
5085   *piter_init = iter_init;
5086
5087   // Set *PPOST to
5088   //   mapiternext(&hiter)
5089
5090   Block* post = new Block(enclosing, loc);
5091
5092   static Named_object* mapiternext;
5093   if (mapiternext == NULL)
5094     {
5095       param_types = new Typed_identifier_list();
5096       param_types->push_back(Typed_identifier("it", map_iteration_ptr, bloc));
5097       fntype = Type::make_function_type(NULL, param_types, NULL, bloc);
5098       mapiternext = Named_object::make_function_declaration("mapiternext",
5099                                                             NULL, fntype,
5100                                                             bloc);
5101       const char* n = "runtime.mapiternext";
5102       mapiternext->func_declaration_value()->set_asm_name(n);
5103     }
5104
5105   func = Expression::make_func_reference(mapiternext, NULL, loc);
5106   params = new Expression_list();
5107   ref = Expression::make_temporary_reference(hiter, loc);
5108   params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
5109   call = Expression::make_call(func, params, false, loc);
5110   post->add_statement(Statement::make_statement(call));
5111
5112   *ppost = post;
5113 }
5114
5115 // Lower a for range over a channel.
5116
5117 void
5118 For_range_statement::lower_range_channel(Gogo* gogo,
5119                                          Block*,
5120                                          Block* body_block,
5121                                          Named_object* range_object,
5122                                          Temporary_statement* range_temp,
5123                                          Temporary_statement* index_temp,
5124                                          Temporary_statement* value_temp,
5125                                          Block** pinit,
5126                                          Expression** pcond,
5127                                          Block** piter_init,
5128                                          Block** ppost)
5129 {
5130   gcc_assert(value_temp == NULL);
5131
5132   source_location loc = this->location();
5133
5134   // The loop we generate:
5135   //   for {
5136   //           index_temp = <-range
5137   //           if closed(range) {
5138   //                   break
5139   //           }
5140   //           index = index_temp
5141   //           value = value_temp
5142   //           original body
5143   //   }
5144
5145   // We have no initialization code, no condition, and no post code.
5146
5147   *pinit = NULL;
5148   *pcond = NULL;
5149   *ppost = NULL;
5150
5151   // Set *PITER_INIT to
5152   //   index_temp = <-range
5153   //   if closed(range) {
5154   //           break
5155   //   }
5156
5157   Block* iter_init = new Block(body_block, loc);
5158
5159   Expression* ref = this->make_range_ref(range_object, range_temp, loc);
5160   Expression* cond = this->call_builtin(gogo, "closed", ref, loc);
5161
5162   ref = this->make_range_ref(range_object, range_temp, loc);
5163   Expression* recv = Expression::make_receive(ref, loc);
5164   ref = Expression::make_temporary_reference(index_temp, loc);
5165   Statement* s = Statement::make_assignment(ref, recv, loc);
5166   iter_init->add_statement(s);
5167
5168   Block* then_block = new Block(iter_init, loc);
5169   s = Statement::make_break_statement(this->break_label(), loc);
5170   then_block->add_statement(s);
5171
5172   s = Statement::make_if_statement(cond, then_block, NULL, loc);
5173   iter_init->add_statement(s);
5174
5175   *piter_init = iter_init;
5176 }
5177
5178 // Return the break LABEL_EXPR.
5179
5180 Unnamed_label*
5181 For_range_statement::break_label()
5182 {
5183   if (this->break_label_ == NULL)
5184     this->break_label_ = new Unnamed_label(this->location());
5185   return this->break_label_;
5186 }
5187
5188 // Return the continue LABEL_EXPR.
5189
5190 Unnamed_label*
5191 For_range_statement::continue_label()
5192 {
5193   if (this->continue_label_ == NULL)
5194     this->continue_label_ = new Unnamed_label(this->location());
5195   return this->continue_label_;
5196 }
5197
5198 // Make a for statement with a range clause.
5199
5200 For_range_statement*
5201 Statement::make_for_range_statement(Expression* index_var,
5202                                     Expression* value_var,
5203                                     Expression* range,
5204                                     source_location location)
5205 {
5206   return new For_range_statement(index_var, value_var, range, location);
5207 }