OSDN Git Service

Use temporary variables for calls with multiple results.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / statements.h
1 // statements.h -- Go frontend statements.     -*- C++ -*-
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 #ifndef GO_STATEMENTS_H
8 #define GO_STATEMENTS_H
9
10 #include "operator.h"
11
12 class Gogo;
13 class Traverse;
14 class Statement_inserter;
15 class Block;
16 class Function;
17 class Unnamed_label;
18 class Temporary_statement;
19 class Variable_declaration_statement;
20 class Return_statement;
21 class Thunk_statement;
22 class Label_statement;
23 class For_statement;
24 class For_range_statement;
25 class Switch_statement;
26 class Type_switch_statement;
27 class Send_statement;
28 class Select_statement;
29 class Variable;
30 class Named_object;
31 class Label;
32 class Translate_context;
33 class Expression;
34 class Expression_list;
35 class Struct_type;
36 class Call_expression;
37 class Map_index_expression;
38 class Receive_expression;
39 class Case_clauses;
40 class Type_case_clauses;
41 class Select_clauses;
42 class Typed_identifier_list;
43 class Bexpression;
44 class Bstatement;
45 class Bvariable;
46
47 // This class is used to traverse assignments made by a statement
48 // which makes assignments.
49
50 class Traverse_assignments
51 {
52  public:
53   Traverse_assignments()
54   { }
55
56   virtual ~Traverse_assignments()
57   { }
58
59   // This is called for a variable initialization.
60   virtual void
61   initialize_variable(Named_object*) = 0;
62
63   // This is called for each assignment made by the statement.  PLHS
64   // points to the left hand side, and PRHS points to the right hand
65   // side.  PRHS may be NULL if there is no associated expression, as
66   // in the bool set by a non-blocking receive.
67   virtual void
68   assignment(Expression** plhs, Expression** prhs) = 0;
69
70   // This is called for each expression which is not passed to the
71   // assignment function.  This is used for some of the statements
72   // which assign two values, for which there is no expression which
73   // describes the value.  For ++ and -- the value is passed to both
74   // the assignment method and the rhs method.  IS_STORED is true if
75   // this value is being stored directly.  It is false if the value is
76   // computed but not stored.  IS_LOCAL is true if the value is being
77   // stored in a local variable or this is being called by a return
78   // statement.
79   virtual void
80   value(Expression**, bool is_stored, bool is_local) = 0;
81 };
82
83 // A single statement.
84
85 class Statement
86 {
87  public:
88   // The types of statements.
89   enum Statement_classification
90   {
91     STATEMENT_ERROR,
92     STATEMENT_VARIABLE_DECLARATION,
93     STATEMENT_TEMPORARY,
94     STATEMENT_ASSIGNMENT,
95     STATEMENT_EXPRESSION,
96     STATEMENT_BLOCK,
97     STATEMENT_GO,
98     STATEMENT_DEFER,
99     STATEMENT_RETURN,
100     STATEMENT_BREAK_OR_CONTINUE,
101     STATEMENT_GOTO,
102     STATEMENT_GOTO_UNNAMED,
103     STATEMENT_LABEL,
104     STATEMENT_UNNAMED_LABEL,
105     STATEMENT_IF,
106     STATEMENT_CONSTANT_SWITCH,
107     STATEMENT_SEND,
108     STATEMENT_SELECT,
109
110     // These statements types are created by the parser, but they
111     // disappear during the lowering pass.
112     STATEMENT_ASSIGNMENT_OPERATION,
113     STATEMENT_TUPLE_ASSIGNMENT,
114     STATEMENT_TUPLE_MAP_ASSIGNMENT,
115     STATEMENT_MAP_ASSIGNMENT,
116     STATEMENT_TUPLE_RECEIVE_ASSIGNMENT,
117     STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT,
118     STATEMENT_INCDEC,
119     STATEMENT_FOR,
120     STATEMENT_FOR_RANGE,
121     STATEMENT_SWITCH,
122     STATEMENT_TYPE_SWITCH
123   };
124
125   Statement(Statement_classification, source_location);
126
127   virtual ~Statement();
128
129   // Make a variable declaration.
130   static Statement*
131   make_variable_declaration(Named_object*);
132
133   // Make a statement which creates a temporary variable and
134   // initializes it to an expression.  The block is used if the
135   // temporary variable has to be explicitly destroyed; the variable
136   // must still be added to the block.  References to the temporary
137   // variable may be constructed using make_temporary_reference.
138   // Either the type or the initialization expression may be NULL, but
139   // not both.
140   static Temporary_statement*
141   make_temporary(Type*, Expression*, source_location);
142
143   // Make an assignment statement.
144   static Statement*
145   make_assignment(Expression*, Expression*, source_location);
146
147   // Make an assignment operation (+=, etc.).
148   static Statement*
149   make_assignment_operation(Operator, Expression*, Expression*,
150                             source_location);
151
152   // Make a tuple assignment statement.
153   static Statement*
154   make_tuple_assignment(Expression_list*, Expression_list*, source_location);
155
156   // Make an assignment from a map index to a pair of variables.
157   static Statement*
158   make_tuple_map_assignment(Expression* val, Expression* present,
159                             Expression*, source_location);
160
161   // Make a statement which assigns a pair of values to a map.
162   static Statement*
163   make_map_assignment(Expression*, Expression* val,
164                       Expression* should_set, source_location);
165
166   // Make an assignment from a nonblocking receive to a pair of
167   // variables.  FOR_SELECT is true is this is being created for a
168   // case x, ok := <-c in a select statement.
169   static Statement*
170   make_tuple_receive_assignment(Expression* val, Expression* closed,
171                                 Expression* channel, bool for_select,
172                                 source_location);
173
174   // Make an assignment from a type guard to a pair of variables.
175   static Statement*
176   make_tuple_type_guard_assignment(Expression* val, Expression* ok,
177                                    Expression* expr, Type* type,
178                                    source_location);
179
180   // Make an expression statement from an Expression.
181   static Statement*
182   make_statement(Expression*);
183
184   // Make a block statement from a Block.  This is an embedded list of
185   // statements which may also include variable definitions.
186   static Statement*
187   make_block_statement(Block*, source_location);
188
189   // Make an increment statement.
190   static Statement*
191   make_inc_statement(Expression*);
192
193   // Make a decrement statement.
194   static Statement*
195   make_dec_statement(Expression*);
196
197   // Make a go statement.
198   static Statement*
199   make_go_statement(Call_expression* call, source_location);
200
201   // Make a defer statement.
202   static Statement*
203   make_defer_statement(Call_expression* call, source_location);
204
205   // Make a return statement.
206   static Statement*
207   make_return_statement(Expression_list*, source_location);
208
209   // Make a break statement.
210   static Statement*
211   make_break_statement(Unnamed_label* label, source_location);
212
213   // Make a continue statement.
214   static Statement*
215   make_continue_statement(Unnamed_label* label, source_location);
216
217   // Make a goto statement.
218   static Statement*
219   make_goto_statement(Label* label, source_location);
220
221   // Make a goto statement to an unnamed label.
222   static Statement*
223   make_goto_unnamed_statement(Unnamed_label* label, source_location);
224
225   // Make a label statement--where the label is defined.
226   static Statement*
227   make_label_statement(Label* label, source_location);
228
229   // Make an unnamed label statement--where the label is defined.
230   static Statement*
231   make_unnamed_label_statement(Unnamed_label* label);
232
233   // Make an if statement.
234   static Statement*
235   make_if_statement(Expression* cond, Block* then_block, Block* else_block,
236                     source_location);
237
238   // Make a switch statement.
239   static Switch_statement*
240   make_switch_statement(Expression* switch_val, source_location);
241
242   // Make a type switch statement.
243   static Type_switch_statement*
244   make_type_switch_statement(Named_object* var, Expression*, source_location);
245
246   // Make a send statement.
247   static Send_statement*
248   make_send_statement(Expression* channel, Expression* val, source_location);
249
250   // Make a select statement.
251   static Select_statement*
252   make_select_statement(source_location);
253
254   // Make a for statement.
255   static For_statement*
256   make_for_statement(Block* init, Expression* cond, Block* post,
257                      source_location location);
258
259   // Make a for statement with a range clause.
260   static For_range_statement*
261   make_for_range_statement(Expression* index_var, Expression* value_var,
262                            Expression* range, source_location);
263
264   // Return the statement classification.
265   Statement_classification
266   classification() const
267   { return this->classification_; }
268
269   // Get the statement location.
270   source_location
271   location() const
272   { return this->location_; }
273
274   // Traverse the tree.
275   int
276   traverse(Block*, size_t* index, Traverse*);
277
278   // Traverse the contents of this statement--the expressions and
279   // statements which it contains.
280   int
281   traverse_contents(Traverse*);
282
283   // If this statement assigns some values, it calls a function for
284   // each value to which this statement assigns a value, and returns
285   // true.  If this statement does not assign any values, it returns
286   // false.
287   bool
288   traverse_assignments(Traverse_assignments* tassign);
289
290   // Lower a statement.  This is called immediately after parsing to
291   // simplify statements for further processing.  It returns the same
292   // Statement or a new one.  FUNCTION is the function containing this
293   // statement.  BLOCK is the block containing this statement.
294   // INSERTER can be used to insert new statements before this one.
295   Statement*
296   lower(Gogo* gogo, Named_object* function, Block* block,
297         Statement_inserter* inserter)
298   { return this->do_lower(gogo, function, block, inserter); }
299
300   // Set type information for unnamed constants.
301   void
302   determine_types();
303
304   // Check types in a statement.  This simply checks that any
305   // expressions used by the statement have the right type.
306   void
307   check_types(Gogo* gogo)
308   { this->do_check_types(gogo); }
309
310   // Return whether this is a block statement.
311   bool
312   is_block_statement() const
313   { return this->classification_ == STATEMENT_BLOCK; }
314
315   // If this is a variable declaration statement, return it.
316   // Otherwise return NULL.
317   Variable_declaration_statement*
318   variable_declaration_statement()
319   {
320     return this->convert<Variable_declaration_statement,
321                          STATEMENT_VARIABLE_DECLARATION>();
322   }
323
324   // If this is a return statement, return it.  Otherwise return NULL.
325   Return_statement*
326   return_statement()
327   { return this->convert<Return_statement, STATEMENT_RETURN>(); }
328
329   // If this is a thunk statement (a go or defer statement), return
330   // it.  Otherwise return NULL.
331   Thunk_statement*
332   thunk_statement();
333
334   // If this is a label statement, return it.  Otherwise return NULL.
335   Label_statement*
336   label_statement()
337   { return this->convert<Label_statement, STATEMENT_LABEL>(); }
338
339   // If this is a for statement, return it.  Otherwise return NULL.
340   For_statement*
341   for_statement()
342   { return this->convert<For_statement, STATEMENT_FOR>(); }
343
344   // If this is a for statement over a range clause, return it.
345   // Otherwise return NULL.
346   For_range_statement*
347   for_range_statement()
348   { return this->convert<For_range_statement, STATEMENT_FOR_RANGE>(); }
349
350   // If this is a switch statement, return it.  Otherwise return NULL.
351   Switch_statement*
352   switch_statement()
353   { return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
354
355   // If this is a type switch statement, return it.  Otherwise return
356   // NULL.
357   Type_switch_statement*
358   type_switch_statement()
359   { return this->convert<Type_switch_statement, STATEMENT_TYPE_SWITCH>(); }
360
361   // If this is a select statement, return it.  Otherwise return NULL.
362   Select_statement*
363   select_statement()
364   { return this->convert<Select_statement, STATEMENT_SELECT>(); }
365
366   // Return true if this statement may fall through--if after
367   // executing this statement we may go on to execute the following
368   // statement, if any.
369   bool
370   may_fall_through() const
371   { return this->do_may_fall_through(); }
372
373   // Convert the statement to the backend representation.
374   Bstatement*
375   get_backend(Translate_context*);
376
377  protected:
378   // Implemented by child class: traverse the tree.
379   virtual int
380   do_traverse(Traverse*) = 0;
381
382   // Implemented by child class: traverse assignments.  Any statement
383   // which includes an assignment should implement this.
384   virtual bool
385   do_traverse_assignments(Traverse_assignments*)
386   { return false; }
387
388   // Implemented by the child class: lower this statement to a simpler
389   // one.
390   virtual Statement*
391   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
392   { return this; }
393
394   // Implemented by child class: set type information for unnamed
395   // constants.  Any statement which includes an expression needs to
396   // implement this.
397   virtual void
398   do_determine_types()
399   { }
400
401   // Implemented by child class: check types of expressions used in a
402   // statement.
403   virtual void
404   do_check_types(Gogo*)
405   { }
406
407   // Implemented by child class: return true if this statement may
408   // fall through.
409   virtual bool
410   do_may_fall_through() const
411   { return true; }
412
413   // Implemented by child class: convert to backend representation.
414   virtual Bstatement*
415   do_get_backend(Translate_context*) = 0;
416
417   // Traverse an expression in a statement.
418   int
419   traverse_expression(Traverse*, Expression**);
420
421   // Traverse an expression list in a statement.  The Expression_list
422   // may be NULL.
423   int
424   traverse_expression_list(Traverse*, Expression_list*);
425
426   // Traverse a type in a statement.
427   int
428   traverse_type(Traverse*, Type*);
429
430   // For children to call when they detect that they are in error.
431   void
432   set_is_error();
433
434   // For children to call to report an error conveniently.
435   void
436   report_error(const char*);
437
438   // For children to return an error statement from lower().
439   static Statement*
440   make_error_statement(source_location);
441
442  private:
443   // Convert to the desired statement classification, or return NULL.
444   // This is a controlled dynamic cast.
445   template<typename Statement_class, Statement_classification sc>
446   Statement_class*
447   convert()
448   {
449     return (this->classification_ == sc
450             ? static_cast<Statement_class*>(this)
451             : NULL);
452   }
453
454   template<typename Statement_class, Statement_classification sc>
455   const Statement_class*
456   convert() const
457   {
458     return (this->classification_ == sc
459             ? static_cast<const Statement_class*>(this)
460             : NULL);
461   }
462
463   // The statement classification.
464   Statement_classification classification_;
465   // The location in the input file of the start of this statement.
466   source_location location_;
467 };
468
469 // A statement which creates and initializes a temporary variable.
470
471 class Temporary_statement : public Statement
472 {
473  public:
474   Temporary_statement(Type* type, Expression* init, source_location location)
475     : Statement(STATEMENT_TEMPORARY, location),
476       type_(type), init_(init), bvariable_(NULL), is_address_taken_(false)
477   { }
478
479   // Return the type of the temporary variable.
480   Type*
481   type() const;
482
483   // Record that something takes the address of this temporary
484   // variable.
485   void
486   set_is_address_taken()
487   { this->is_address_taken_ = true; }
488
489   // Return the temporary variable.  This should not be called until
490   // after the statement itself has been converted.
491   Bvariable*
492   get_backend_variable(Translate_context*) const;
493
494  protected:
495   int
496   do_traverse(Traverse*);
497
498   bool
499   do_traverse_assignments(Traverse_assignments*);
500
501   void
502   do_determine_types();
503
504   void
505   do_check_types(Gogo*);
506
507   Bstatement*
508   do_get_backend(Translate_context*);
509
510  private:
511   // The type of the temporary variable.
512   Type* type_;
513   // The initial value of the temporary variable.  This may be NULL.
514   Expression* init_;
515   // The backend representation of the temporary variable.
516   Bvariable* bvariable_;
517   // True if something takes the address of this temporary variable.
518   bool is_address_taken_;
519 };
520
521 // A variable declaration.  This marks the point in the code where a
522 // variable is declared.  The Variable is also attached to a Block.
523
524 class Variable_declaration_statement : public Statement
525 {
526  public:
527   Variable_declaration_statement(Named_object* var);
528
529   // The variable being declared.
530   Named_object*
531   var()
532   { return this->var_; }
533
534  protected:
535   int
536   do_traverse(Traverse*);
537
538   bool
539   do_traverse_assignments(Traverse_assignments*);
540
541   Statement*
542   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
543
544   Bstatement*
545   do_get_backend(Translate_context*);
546
547  private:
548   Named_object* var_;
549 };
550
551 // A return statement.
552
553 class Return_statement : public Statement
554 {
555  public:
556   Return_statement(Expression_list* vals, source_location location)
557     : Statement(STATEMENT_RETURN, location),
558       vals_(vals), is_lowered_(false)
559   { }
560
561   // The list of values being returned.  This may be NULL.
562   const Expression_list*
563   vals() const
564   { return this->vals_; }
565
566  protected:
567   int
568   do_traverse(Traverse* traverse)
569   { return this->traverse_expression_list(traverse, this->vals_); }
570
571   bool
572   do_traverse_assignments(Traverse_assignments*);
573
574   Statement*
575   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
576
577   bool
578   do_may_fall_through() const
579   { return false; }
580
581   Bstatement*
582   do_get_backend(Translate_context*);
583
584  private:
585   // Return values.  This may be NULL.
586   Expression_list* vals_;
587   // True if this statement has been lowered.
588   bool is_lowered_;
589 };
590
591 // A send statement.
592
593 class Send_statement : public Statement
594 {
595  public:
596   Send_statement(Expression* channel, Expression* val,
597                  source_location location)
598     : Statement(STATEMENT_SEND, location),
599       channel_(channel), val_(val), for_select_(false)
600   { }
601
602   // Note that this is for a select statement.
603   void
604   set_for_select()
605   { this->for_select_ = true; }
606
607  protected:
608   int
609   do_traverse(Traverse* traverse);
610
611   void
612   do_determine_types();
613
614   void
615   do_check_types(Gogo*);
616
617   Bstatement*
618   do_get_backend(Translate_context*);
619
620  private:
621   // The channel on which to send the value.
622   Expression* channel_;
623   // The value to send.
624   Expression* val_;
625   // Whether this is for a select statement.
626   bool for_select_;
627 };
628
629 // Select_clauses holds the clauses of a select statement.  This is
630 // built by the parser.
631
632 class Select_clauses
633 {
634  public:
635   Select_clauses()
636     : clauses_()
637   { }
638
639   // Add a new clause.  IS_SEND is true if this is a send clause,
640   // false for a receive clause.  For a send clause CHANNEL is the
641   // channel and VAL is the value to send.  For a receive clause
642   // CHANNEL is the channel, VAL is either NULL or a Var_expression
643   // for the variable to set, and CLOSED is either NULL or a
644   // Var_expression to set to whether the channel is closed.  If VAL
645   // is NULL, VAR may be a variable to be initialized with the
646   // received value, and CLOSEDVAR ma be a variable to be initialized
647   // with whether the channel is closed.  IS_DEFAULT is true if this
648   // is the default clause.  STATEMENTS is the list of statements to
649   // execute.
650   void
651   add(bool is_send, Expression* channel, Expression* val, Expression* closed,
652       Named_object* var, Named_object* closedvar, bool is_default,
653       Block* statements, source_location location)
654   {
655     this->clauses_.push_back(Select_clause(is_send, channel, val, closed, var,
656                                            closedvar, is_default, statements,
657                                            location));
658   }
659
660   // Traverse the select clauses.
661   int
662   traverse(Traverse*);
663
664   // Lower statements.
665   void
666   lower(Gogo*, Named_object*, Block*);
667
668   // Determine types.
669   void
670   determine_types();
671
672   // Whether the select clauses may fall through to the statement
673   // which follows the overall select statement.
674   bool
675   may_fall_through() const;
676
677   // Convert to the backend representation.
678   Bstatement*
679   get_backend(Translate_context*, Unnamed_label* break_label, source_location);
680
681  private:
682   // A single clause.
683   class Select_clause
684   {
685    public:
686     Select_clause()
687       : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
688         closedvar_(NULL), statements_(NULL), is_send_(false),
689         is_default_(false)
690     { }
691
692     Select_clause(bool is_send, Expression* channel, Expression* val,
693                   Expression* closed, Named_object* var,
694                   Named_object* closedvar, bool is_default, Block* statements,
695                   source_location location)
696       : channel_(channel), val_(val), closed_(closed), var_(var),
697         closedvar_(closedvar), statements_(statements), location_(location),
698         is_send_(is_send), is_default_(is_default), is_lowered_(false)
699     { go_assert(is_default ? channel == NULL : channel != NULL); }
700
701     // Traverse the select clause.
702     int
703     traverse(Traverse*);
704
705     // Lower statements.
706     void
707     lower(Gogo*, Named_object*, Block*);
708
709     // Determine types.
710     void
711     determine_types();
712
713     // Return true if this is the default clause.
714     bool
715     is_default() const
716     { return this->is_default_; }
717
718     // Return the channel.  This will return NULL for the default
719     // clause.
720     Expression*
721     channel() const
722     { return this->channel_; }
723
724     // Return true for a send, false for a receive.
725     bool
726     is_send() const
727     {
728       go_assert(!this->is_default_);
729       return this->is_send_;
730     }
731
732     // Return the statements.
733     const Block*
734     statements() const
735     { return this->statements_; }
736
737     // Return the location.
738     source_location
739     location() const
740     { return this->location_; }
741
742     // Whether this clause may fall through to the statement which
743     // follows the overall select statement.
744     bool
745     may_fall_through() const;
746
747     // Convert the statements to the backend representation.
748     Bstatement*
749     get_statements_backend(Translate_context*);
750
751    private:
752     // The channel.
753     Expression* channel_;
754     // The value to send or the lvalue to receive into.
755     Expression* val_;
756     // The lvalue to set to whether the channel is closed on a
757     // receive.
758     Expression* closed_;
759     // The variable to initialize, for "case a := <-ch".
760     Named_object* var_;
761     // The variable to initialize to whether the channel is closed,
762     // for "case a, c := <-ch".
763     Named_object* closedvar_;
764     // The statements to execute.
765     Block* statements_;
766     // The location of this clause.
767     source_location location_;
768     // Whether this is a send or a receive.
769     bool is_send_;
770     // Whether this is the default.
771     bool is_default_;
772     // Whether this has been lowered.
773     bool is_lowered_;
774   };
775
776   void
777   add_clause_backend(Translate_context*, source_location, int index,
778                      int case_value, Select_clause*, Unnamed_label*,
779                      std::vector<std::vector<Bexpression*> >* cases,
780                      std::vector<Bstatement*>* clauses);
781
782   typedef std::vector<Select_clause> Clauses;
783
784   Clauses clauses_;
785 };
786
787 // A select statement.
788
789 class Select_statement : public Statement
790 {
791  public:
792   Select_statement(source_location location)
793     : Statement(STATEMENT_SELECT, location),
794       clauses_(NULL), break_label_(NULL), is_lowered_(false)
795   { }
796
797   // Add the clauses.
798   void
799   add_clauses(Select_clauses* clauses)
800   {
801     go_assert(this->clauses_ == NULL);
802     this->clauses_ = clauses;
803   }
804
805   // Return the break label for this select statement.
806   Unnamed_label*
807   break_label();
808
809  protected:
810   int
811   do_traverse(Traverse* traverse)
812   { return this->clauses_->traverse(traverse); }
813
814   Statement*
815   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
816
817   void
818   do_determine_types()
819   { this->clauses_->determine_types(); }
820
821   bool
822   do_may_fall_through() const
823   { return this->clauses_->may_fall_through(); }
824
825   Bstatement*
826   do_get_backend(Translate_context*);
827
828  private:
829   // The select clauses.
830   Select_clauses* clauses_;
831   // The break label.
832   Unnamed_label* break_label_;
833   // Whether this statement has been lowered.
834   bool is_lowered_;
835 };
836
837 // A statement which requires a thunk: go or defer.
838
839 class Thunk_statement : public Statement
840 {
841  public:
842   Thunk_statement(Statement_classification, Call_expression*,
843                   source_location);
844
845   // Return the call expression.
846   Expression*
847   call()
848   { return this->call_; }
849
850   // Simplify a go or defer statement so that it only uses a single
851   // parameter.
852   bool
853   simplify_statement(Gogo*, Named_object*, Block*);
854
855  protected:
856   int
857   do_traverse(Traverse* traverse);
858
859   bool
860   do_traverse_assignments(Traverse_assignments*);
861
862   void
863   do_determine_types();
864
865   void
866   do_check_types(Gogo*);
867
868   // Return the function and argument for the call.
869   bool
870   get_fn_and_arg(Expression** pfn, Expression** parg);
871
872  private:
873   // Return whether this is a simple go statement.
874   bool
875   is_simple(Function_type*) const;
876
877   // Build the struct to use for a complex case.
878   Struct_type*
879   build_struct(Function_type* fntype);
880
881   // Build the thunk.
882   void
883   build_thunk(Gogo*, const std::string&, Function_type* fntype);
884
885   // The field name used in the thunk structure for the function
886   // pointer.
887   static const char* const thunk_field_fn;
888
889   // The field name used in the thunk structure for the receiver, if
890   // there is one.
891   static const char* const thunk_field_receiver;
892
893   // Set the name to use for thunk field N.
894   void
895   thunk_field_param(int n, char* buf, size_t buflen);
896
897   // The function call to be executed in a separate thread (go) or
898   // later (defer).
899   Expression* call_;
900   // The type used for a struct to pass to a thunk, if this is not a
901   // simple call.
902   Struct_type* struct_type_;
903 };
904
905 // A go statement.
906
907 class Go_statement : public Thunk_statement
908 {
909  public:
910   Go_statement(Call_expression* call, source_location location)
911     : Thunk_statement(STATEMENT_GO, call, location)
912   { }
913
914  protected:
915   Bstatement*
916   do_get_backend(Translate_context*);
917 };
918
919 // A defer statement.
920
921 class Defer_statement : public Thunk_statement
922 {
923  public:
924   Defer_statement(Call_expression* call, source_location location)
925     : Thunk_statement(STATEMENT_DEFER, call, location)
926   { }
927
928  protected:
929   Bstatement*
930   do_get_backend(Translate_context*);
931 };
932
933 // A label statement.
934
935 class Label_statement : public Statement
936 {
937  public:
938   Label_statement(Label* label, source_location location)
939     : Statement(STATEMENT_LABEL, location),
940       label_(label)
941   { }
942
943   // Return the label itself.
944   const Label*
945   label() const
946   { return this->label_; }
947
948  protected:
949   int
950   do_traverse(Traverse*);
951
952   Bstatement*
953   do_get_backend(Translate_context*);
954
955  private:
956   // The label.
957   Label* label_;
958 };
959
960 // A for statement.
961
962 class For_statement : public Statement
963 {
964  public:
965   For_statement(Block* init, Expression* cond, Block* post,
966                 source_location location)
967     : Statement(STATEMENT_FOR, location),
968       init_(init), cond_(cond), post_(post), statements_(NULL),
969       break_label_(NULL), continue_label_(NULL)
970   { }
971
972   // Add the statements.
973   void
974   add_statements(Block* statements)
975   {
976     go_assert(this->statements_ == NULL);
977     this->statements_ = statements;
978   }
979
980   // Return the break label for this for statement.
981   Unnamed_label*
982   break_label();
983
984   // Return the continue label for this for statement.
985   Unnamed_label*
986   continue_label();
987
988   // Set the break and continue labels for this statement.
989   void
990   set_break_continue_labels(Unnamed_label* break_label,
991                             Unnamed_label* continue_label);
992
993  protected:
994   int
995   do_traverse(Traverse*);
996
997   bool
998   do_traverse_assignments(Traverse_assignments*)
999   { go_unreachable(); }
1000
1001   Statement*
1002   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1003
1004   Bstatement*
1005   do_get_backend(Translate_context*)
1006   { go_unreachable(); }
1007
1008  private:
1009   // The initialization statements.  This may be NULL.
1010   Block* init_;
1011   // The condition.  This may be NULL.
1012   Expression* cond_;
1013   // The statements to run after each iteration.  This may be NULL.
1014   Block* post_;
1015   // The statements in the loop itself.
1016   Block* statements_;
1017   // The break label, if needed.
1018   Unnamed_label* break_label_;
1019   // The continue label, if needed.
1020   Unnamed_label* continue_label_;
1021 };
1022
1023 // A for statement over a range clause.
1024
1025 class For_range_statement : public Statement
1026 {
1027  public:
1028   For_range_statement(Expression* index_var, Expression* value_var,
1029                       Expression* range, source_location location)
1030     : Statement(STATEMENT_FOR_RANGE, location),
1031       index_var_(index_var), value_var_(value_var), range_(range),
1032       statements_(NULL), break_label_(NULL), continue_label_(NULL)
1033   { }
1034
1035   // Add the statements.
1036   void
1037   add_statements(Block* statements)
1038   {
1039     go_assert(this->statements_ == NULL);
1040     this->statements_ = statements;
1041   }
1042
1043   // Return the break label for this for statement.
1044   Unnamed_label*
1045   break_label();
1046
1047   // Return the continue label for this for statement.
1048   Unnamed_label*
1049   continue_label();
1050
1051  protected:
1052   int
1053   do_traverse(Traverse*);
1054
1055   bool
1056   do_traverse_assignments(Traverse_assignments*)
1057   { go_unreachable(); }
1058
1059   Statement*
1060   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1061
1062   Bstatement*
1063   do_get_backend(Translate_context*)
1064   { go_unreachable(); }
1065
1066  private:
1067   Expression*
1068   make_range_ref(Named_object*, Temporary_statement*, source_location);
1069
1070   Expression*
1071   call_builtin(Gogo*, const char* funcname, Expression* arg, source_location);
1072
1073   void
1074   lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1075                     Temporary_statement*, Temporary_statement*,
1076                     Block**, Expression**, Block**, Block**);
1077
1078   void
1079   lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1080                      Temporary_statement*, Temporary_statement*,
1081                      Block**, Expression**, Block**, Block**);
1082
1083   void
1084   lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1085                   Temporary_statement*, Temporary_statement*,
1086                   Block**, Expression**, Block**, Block**);
1087
1088   void
1089   lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1090                       Temporary_statement*, Temporary_statement*,
1091                       Temporary_statement*, Block**, Expression**, Block**,
1092                       Block**);
1093
1094   // The variable which is set to the index value.
1095   Expression* index_var_;
1096   // The variable which is set to the element value.  This may be
1097   // NULL.
1098   Expression* value_var_;
1099   // The expression we are ranging over.
1100   Expression* range_;
1101   // The statements in the block.
1102   Block* statements_;
1103   // The break label, if needed.
1104   Unnamed_label* break_label_;
1105   // The continue label, if needed.
1106   Unnamed_label* continue_label_;
1107 };
1108
1109 // Class Case_clauses holds the clauses of a switch statement.  This
1110 // is built by the parser.
1111
1112 class Case_clauses
1113 {
1114  public:
1115   Case_clauses()
1116     : clauses_()
1117   { }
1118
1119   // Add a new clause.  CASES is a list of case expressions; it may be
1120   // NULL.  IS_DEFAULT is true if this is the default case.
1121   // STATEMENTS is a block of statements.  IS_FALLTHROUGH is true if
1122   // after the statements the case clause should fall through to the
1123   // next clause.
1124   void
1125   add(Expression_list* cases, bool is_default, Block* statements,
1126       bool is_fallthrough, source_location location)
1127   {
1128     this->clauses_.push_back(Case_clause(cases, is_default, statements,
1129                                          is_fallthrough, location));
1130   }
1131
1132   // Return whether there are no clauses.
1133   bool
1134   empty() const
1135   { return this->clauses_.empty(); }
1136
1137   // Traverse the case clauses.
1138   int
1139   traverse(Traverse*);
1140
1141   // Lower for a nonconstant switch.
1142   void
1143   lower(Block*, Temporary_statement*, Unnamed_label*) const;
1144
1145   // Determine types of expressions.  The Type parameter is the type
1146   // of the switch value.
1147   void
1148   determine_types(Type*);
1149
1150   // Check types.  The Type parameter is the type of the switch value.
1151   bool
1152   check_types(Type*);
1153
1154   // Return true if all the clauses are constant values.
1155   bool
1156   is_constant() const;
1157
1158   // Return true if these clauses may fall through to the statements
1159   // following the switch statement.
1160   bool
1161   may_fall_through() const;
1162
1163   // Return the body of a SWITCH_EXPR when all the clauses are
1164   // constants.
1165   void
1166   get_backend(Translate_context*, Unnamed_label* break_label,
1167               std::vector<std::vector<Bexpression*> >* all_cases,
1168               std::vector<Bstatement*>* all_statements) const;
1169
1170  private:
1171   // For a constant switch we need to keep a record of constants we
1172   // have already seen.
1173   class Hash_integer_value;
1174   class Eq_integer_value;
1175   typedef Unordered_set_hash(Expression*, Hash_integer_value,
1176                              Eq_integer_value) Case_constants;
1177
1178   // One case clause.
1179   class Case_clause
1180   {
1181    public:
1182     Case_clause()
1183       : cases_(NULL), statements_(NULL), is_default_(false),
1184         is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1185     { }
1186
1187     Case_clause(Expression_list* cases, bool is_default, Block* statements,
1188                 bool is_fallthrough, source_location location)
1189       : cases_(cases), statements_(statements), is_default_(is_default),
1190         is_fallthrough_(is_fallthrough), location_(location)
1191     { }
1192
1193     // Whether this clause falls through to the next clause.
1194     bool
1195     is_fallthrough() const
1196     { return this->is_fallthrough_; }
1197
1198     // Whether this is the default.
1199     bool
1200     is_default() const
1201     { return this->is_default_; }
1202
1203     // The location of this clause.
1204     source_location
1205     location() const
1206     { return this->location_; }
1207
1208     // Traversal.
1209     int
1210     traverse(Traverse*);
1211
1212     // Lower for a nonconstant switch.
1213     void
1214     lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1215
1216     // Determine types.
1217     void
1218     determine_types(Type*);
1219
1220     // Check types.
1221     bool
1222     check_types(Type*);
1223
1224     // Return true if all the case expressions are constant.
1225     bool
1226     is_constant() const;
1227
1228     // Return true if this clause may fall through to execute the
1229     // statements following the switch statement.  This is not the
1230     // same as whether this clause falls through to the next clause.
1231     bool
1232     may_fall_through() const;
1233
1234     // Convert the case values and statements to the backend
1235     // representation.
1236     Bstatement*
1237     get_backend(Translate_context*, Unnamed_label* break_label,
1238                 Case_constants*, std::vector<Bexpression*>* cases) const;
1239
1240    private:
1241     // The list of case expressions.
1242     Expression_list* cases_;
1243     // The statements to execute.
1244     Block* statements_;
1245     // Whether this is the default case.
1246     bool is_default_;
1247     // Whether this falls through after the statements.
1248     bool is_fallthrough_;
1249     // The location of this case clause.
1250     source_location location_;
1251   };
1252
1253   friend class Case_clause;
1254
1255   // The type of the list of clauses.
1256   typedef std::vector<Case_clause> Clauses;
1257
1258   // All the case clauses.
1259   Clauses clauses_;
1260 };
1261
1262 // A switch statement.
1263
1264 class Switch_statement : public Statement
1265 {
1266  public:
1267   Switch_statement(Expression* val, source_location location)
1268     : Statement(STATEMENT_SWITCH, location),
1269       val_(val), clauses_(NULL), break_label_(NULL)
1270   { }
1271
1272   // Add the clauses.
1273   void
1274   add_clauses(Case_clauses* clauses)
1275   {
1276     go_assert(this->clauses_ == NULL);
1277     this->clauses_ = clauses;
1278   }
1279
1280   // Return the break label for this switch statement.
1281   Unnamed_label*
1282   break_label();
1283
1284  protected:
1285   int
1286   do_traverse(Traverse*);
1287
1288   Statement*
1289   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1290
1291   Bstatement*
1292   do_get_backend(Translate_context*)
1293   { go_unreachable(); }
1294
1295  private:
1296   // The value to switch on.  This may be NULL.
1297   Expression* val_;
1298   // The case clauses.
1299   Case_clauses* clauses_;
1300   // The break label, if needed.
1301   Unnamed_label* break_label_;
1302 };
1303
1304 // Class Type_case_clauses holds the clauses of a type switch
1305 // statement.  This is built by the parser.
1306
1307 class Type_case_clauses
1308 {
1309  public:
1310   Type_case_clauses()
1311     : clauses_()
1312   { }
1313
1314   // Add a new clause.  TYPE is the type for this clause; it may be
1315   // NULL.  IS_FALLTHROUGH is true if this falls through to the next
1316   // clause; in this case STATEMENTS will be NULL.  IS_DEFAULT is true
1317   // if this is the default case.  STATEMENTS is a block of
1318   // statements; it may be NULL.
1319   void
1320   add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1321       source_location location)
1322   {
1323     this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1324                                               statements, location));
1325   }
1326
1327   // Return whether there are no clauses.
1328   bool
1329   empty() const
1330   { return this->clauses_.empty(); }
1331
1332   // Traverse the type case clauses.
1333   int
1334   traverse(Traverse*);
1335
1336   // Check for duplicates.
1337   void
1338   check_duplicates() const;
1339
1340   // Lower to if and goto statements.
1341   void
1342   lower(Block*, Temporary_statement* descriptor_temp,
1343         Unnamed_label* break_label) const;
1344
1345  private:
1346   // One type case clause.
1347   class Type_case_clause
1348   {
1349    public:
1350     Type_case_clause()
1351       : type_(NULL), statements_(NULL), is_default_(false),
1352         location_(UNKNOWN_LOCATION)
1353     { }
1354
1355     Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1356                      Block* statements, source_location location)
1357       : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1358         is_default_(is_default), location_(location)
1359     { }
1360
1361     // The type.
1362     Type*
1363     type() const
1364     { return this->type_; }
1365
1366     // Whether this is the default.
1367     bool
1368     is_default() const
1369     { return this->is_default_; }
1370
1371     // The location of this type clause.
1372     source_location
1373     location() const
1374     { return this->location_; }
1375
1376     // Traversal.
1377     int
1378     traverse(Traverse*);
1379
1380     // Lower to if and goto statements.
1381     void
1382     lower(Block*, Temporary_statement* descriptor_temp,
1383           Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1384
1385    private:
1386     // The type for this type clause.
1387     Type* type_;
1388     // The statements to execute.
1389     Block* statements_;
1390     // Whether this falls through--this is true for "case T1, T2".
1391     bool is_fallthrough_;
1392     // Whether this is the default case.
1393     bool is_default_;
1394     // The location of this type case clause.
1395     source_location location_;
1396   };
1397
1398   friend class Type_case_clause;
1399
1400   // The type of the list of type clauses.
1401   typedef std::vector<Type_case_clause> Type_clauses;
1402
1403   // All the type case clauses.
1404   Type_clauses clauses_;
1405 };
1406
1407 // A type switch statement.
1408
1409 class Type_switch_statement : public Statement
1410 {
1411  public:
1412   Type_switch_statement(Named_object* var, Expression* expr,
1413                         source_location location)
1414     : Statement(STATEMENT_TYPE_SWITCH, location),
1415       var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1416   { go_assert(var == NULL || expr == NULL); }
1417
1418   // Add the clauses.
1419   void
1420   add_clauses(Type_case_clauses* clauses)
1421   {
1422     go_assert(this->clauses_ == NULL);
1423     this->clauses_ = clauses;
1424   }
1425
1426   // Return the break label for this type switch statement.
1427   Unnamed_label*
1428   break_label();
1429
1430  protected:
1431   int
1432   do_traverse(Traverse*);
1433
1434   Statement*
1435   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1436
1437   Bstatement*
1438   do_get_backend(Translate_context*)
1439   { go_unreachable(); }
1440
1441  private:
1442   // The variable holding the value we are switching on.
1443   Named_object* var_;
1444   // The expression we are switching on if there is no variable.
1445   Expression* expr_;
1446   // The type case clauses.
1447   Type_case_clauses* clauses_;
1448   // The break label, if needed.
1449   Unnamed_label* break_label_;
1450 };
1451
1452 #endif // !defined(GO_STATEMENTS_H)