OSDN Git Service

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