OSDN Git Service

016ca189569918afaf6a186c32b26164e9c3093b
[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, 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*, Location);
143
144   // Make an assignment statement.
145   static Statement*
146   make_assignment(Expression*, Expression*, Location);
147
148   // Make an assignment operation (+=, etc.).
149   static Statement*
150   make_assignment_operation(Operator, Expression*, Expression*,
151                             Location);
152
153   // Make a tuple assignment statement.
154   static Statement*
155   make_tuple_assignment(Expression_list*, Expression_list*, 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*, 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, Location);
166
167   // Make an assignment from a nonblocking receive to a pair of
168   // variables.
169   static Statement*
170   make_tuple_receive_assignment(Expression* val, Expression* closed,
171                                 Expression* channel, Location);
172
173   // Make an assignment from a type guard to a pair of variables.
174   static Statement*
175   make_tuple_type_guard_assignment(Expression* val, Expression* ok,
176                                    Expression* expr, Type* type,
177                                    Location);
178
179   // Make an expression statement from an Expression.  IS_IGNORED is
180   // true if the value is being explicitly ignored, as in an
181   // assignment to _.
182   static Statement*
183   make_statement(Expression*, bool is_ignored);
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*, 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, Location);
201
202   // Make a defer statement.
203   static Statement*
204   make_defer_statement(Call_expression* call, Location);
205
206   // Make a return statement.
207   static Return_statement*
208   make_return_statement(Expression_list*, Location);
209
210   // Make a break statement.
211   static Statement*
212   make_break_statement(Unnamed_label* label, Location);
213
214   // Make a continue statement.
215   static Statement*
216   make_continue_statement(Unnamed_label* label, Location);
217
218   // Make a goto statement.
219   static Statement*
220   make_goto_statement(Label* label, Location);
221
222   // Make a goto statement to an unnamed label.
223   static Statement*
224   make_goto_unnamed_statement(Unnamed_label* label, Location);
225
226   // Make a label statement--where the label is defined.
227   static Statement*
228   make_label_statement(Label* label, 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                     Location);
238
239   // Make a switch statement.
240   static Switch_statement*
241   make_switch_statement(Expression* switch_val, Location);
242
243   // Make a type switch statement.
244   static Type_switch_statement*
245   make_type_switch_statement(Named_object* var, Expression*, Location);
246
247   // Make a send statement.
248   static Send_statement*
249   make_send_statement(Expression* channel, Expression* val, Location);
250
251   // Make a select statement.
252   static Select_statement*
253   make_select_statement(Location);
254
255   // Make a for statement.
256   static For_statement*
257   make_for_statement(Block* init, Expression* cond, Block* post,
258                      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, Location);
264
265   // Return the statement classification.
266   Statement_classification
267   classification() const
268   { return this->classification_; }
269
270   // Get the statement location.
271   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(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   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, Location location)
484     : Statement(STATEMENT_TEMPORARY, location),
485       type_(type), init_(init), bvariable_(NULL), are_hidden_fields_ok_(false),
486       is_address_taken_(false)
487   { }
488
489   // Return the type of the temporary variable.
490   Type*
491   type() const;
492
493   // Note that it is OK for this statement to set hidden fields.
494   void
495   set_hidden_fields_are_ok()
496   { this->are_hidden_fields_ok_ = true; }
497
498   // Record that something takes the address of this temporary
499   // variable.
500   void
501   set_is_address_taken()
502   { this->is_address_taken_ = true; }
503
504   // Return the temporary variable.  This should not be called until
505   // after the statement itself has been converted.
506   Bvariable*
507   get_backend_variable(Translate_context*) const;
508
509  protected:
510   int
511   do_traverse(Traverse*);
512
513   bool
514   do_traverse_assignments(Traverse_assignments*);
515
516   void
517   do_determine_types();
518
519   void
520   do_check_types(Gogo*);
521
522   Bstatement*
523   do_get_backend(Translate_context*);
524
525   void
526   do_dump_statement(Ast_dump_context*) const;
527
528  private:
529   // The type of the temporary variable.
530   Type* type_;
531   // The initial value of the temporary variable.  This may be NULL.
532   Expression* init_;
533   // The backend representation of the temporary variable.
534   Bvariable* bvariable_;
535   // True if this statement may set hidden fields when assigning the
536   // value to the temporary.  This is used for generated method stubs.
537   bool are_hidden_fields_ok_;
538   // True if something takes the address of this temporary variable.
539   bool is_address_taken_;
540 };
541
542 // A variable declaration.  This marks the point in the code where a
543 // variable is declared.  The Variable is also attached to a Block.
544
545 class Variable_declaration_statement : public Statement
546 {
547  public:
548   Variable_declaration_statement(Named_object* var);
549
550   // The variable being declared.
551   Named_object*
552   var()
553   { return this->var_; }
554
555  protected:
556   int
557   do_traverse(Traverse*);
558
559   bool
560   do_traverse_assignments(Traverse_assignments*);
561
562   Statement*
563   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
564
565   Bstatement*
566   do_get_backend(Translate_context*);
567
568   void
569   do_dump_statement(Ast_dump_context*) const;
570
571  private:
572   Named_object* var_;
573 };
574
575 // A return statement.
576
577 class Return_statement : public Statement
578 {
579  public:
580   Return_statement(Expression_list* vals, Location location)
581     : Statement(STATEMENT_RETURN, location),
582       vals_(vals), are_hidden_fields_ok_(false), is_lowered_(false)
583   { }
584
585   // The list of values being returned.  This may be NULL.
586   const Expression_list*
587   vals() const
588   { return this->vals_; }
589
590   // Note that it is OK for this return statement to set hidden
591   // fields.
592   void
593   set_hidden_fields_are_ok()
594   { this->are_hidden_fields_ok_ = true; }
595
596  protected:
597   int
598   do_traverse(Traverse* traverse)
599   { return this->traverse_expression_list(traverse, this->vals_); }
600
601   bool
602   do_traverse_assignments(Traverse_assignments*);
603
604   Statement*
605   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
606
607   bool
608   do_may_fall_through() const
609   { return false; }
610
611   Bstatement*
612   do_get_backend(Translate_context*);
613
614   void
615   do_dump_statement(Ast_dump_context*) const;
616
617  private:
618   // Return values.  This may be NULL.
619   Expression_list* vals_;
620   // True if this statement may pass hidden fields in the return
621   // value.  This is used for generated method stubs.
622   bool are_hidden_fields_ok_;
623   // True if this statement has been lowered.
624   bool is_lowered_;
625 };
626
627 // A send statement.
628
629 class Send_statement : public Statement
630 {
631  public:
632   Send_statement(Expression* channel, Expression* val,
633                  Location location)
634     : Statement(STATEMENT_SEND, location),
635       channel_(channel), val_(val)
636   { }
637
638  protected:
639   int
640   do_traverse(Traverse* traverse);
641
642   void
643   do_determine_types();
644
645   void
646   do_check_types(Gogo*);
647
648   Bstatement*
649   do_get_backend(Translate_context*);
650
651   void
652   do_dump_statement(Ast_dump_context*) const;
653
654  private:
655   // The channel on which to send the value.
656   Expression* channel_;
657   // The value to send.
658   Expression* val_;
659 };
660
661 // Select_clauses holds the clauses of a select statement.  This is
662 // built by the parser.
663
664 class Select_clauses
665 {
666  public:
667   Select_clauses()
668     : clauses_()
669   { }
670
671   // Add a new clause.  IS_SEND is true if this is a send clause,
672   // false for a receive clause.  For a send clause CHANNEL is the
673   // channel and VAL is the value to send.  For a receive clause
674   // CHANNEL is the channel, VAL is either NULL or a Var_expression
675   // for the variable to set, and CLOSED is either NULL or a
676   // Var_expression to set to whether the channel is closed.  If VAL
677   // is NULL, VAR may be a variable to be initialized with the
678   // received value, and CLOSEDVAR ma be a variable to be initialized
679   // with whether the channel is closed.  IS_DEFAULT is true if this
680   // is the default clause.  STATEMENTS is the list of statements to
681   // execute.
682   void
683   add(bool is_send, Expression* channel, Expression* val, Expression* closed,
684       Named_object* var, Named_object* closedvar, bool is_default,
685       Block* statements, Location location)
686   {
687     int index = static_cast<int>(this->clauses_.size());
688     this->clauses_.push_back(Select_clause(index, is_send, channel, val,
689                                            closed, var, closedvar, is_default,
690                                            statements, location));
691   }
692
693   size_t
694   size() const
695   { return this->clauses_.size(); }
696
697   // Traverse the select clauses.
698   int
699   traverse(Traverse*);
700
701   // Lower statements.
702   void
703   lower(Gogo*, Named_object*, Block*, Temporary_statement*);
704
705   // Determine types.
706   void
707   determine_types();
708
709   // Check types.
710   void
711   check_types();
712
713   // Whether the select clauses may fall through to the statement
714   // which follows the overall select statement.
715   bool
716   may_fall_through() const;
717
718   // Convert to the backend representation.
719   Bstatement*
720   get_backend(Translate_context*, Temporary_statement* sel,
721               Unnamed_label* break_label, Location);
722
723   // Dump AST representation.
724   void
725   dump_clauses(Ast_dump_context*) const;
726
727  private:
728   // A single clause.
729   class Select_clause
730   {
731    public:
732     Select_clause()
733       : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
734         closedvar_(NULL), statements_(NULL), is_send_(false),
735         is_default_(false)
736     { }
737
738     Select_clause(int index, bool is_send, Expression* channel,
739                   Expression* val, Expression* closed, Named_object* var,
740                   Named_object* closedvar, bool is_default, Block* statements,
741                   Location location)
742       : index_(index), channel_(channel), val_(val), closed_(closed),
743         var_(var), closedvar_(closedvar), statements_(statements),
744         location_(location), is_send_(is_send), is_default_(is_default),
745         is_lowered_(false)
746     { go_assert(is_default ? channel == NULL : channel != NULL); }
747
748     // Return the index of this clause.
749     int
750     index() const
751     { return this->index_; }
752
753     // Traverse the select clause.
754     int
755     traverse(Traverse*);
756
757     // Lower statements.
758     void
759     lower(Gogo*, Named_object*, Block*, Temporary_statement*);
760
761     // Determine types.
762     void
763     determine_types();
764
765     // Check types.
766     void
767     check_types();
768
769     // Return true if this is the default clause.
770     bool
771     is_default() const
772     { return this->is_default_; }
773
774     // Return the channel.  This will return NULL for the default
775     // clause.
776     Expression*
777     channel() const
778     { return this->channel_; }
779
780     // Return true for a send, false for a receive.
781     bool
782     is_send() const
783     {
784       go_assert(!this->is_default_);
785       return this->is_send_;
786     }
787
788     // Return the statements.
789     const Block*
790     statements() const
791     { return this->statements_; }
792
793     // Return the location.
794     Location
795     location() const
796     { return this->location_; }
797
798     // Whether this clause may fall through to the statement which
799     // follows the overall select statement.
800     bool
801     may_fall_through() const;
802
803     // Convert the statements to the backend representation.
804     Bstatement*
805     get_statements_backend(Translate_context*);
806
807     // Dump AST representation.
808     void
809     dump_clause(Ast_dump_context*) const;
810
811    private:
812     void
813     lower_default(Block*, Expression*, Expression*);
814
815     void
816     lower_send(Block*, Expression*, Expression*, Expression*);
817
818     void
819     lower_recv(Gogo*, Named_object*, Block*, Expression*, Expression*,
820                Expression*);
821
822     // The index of this case in the generated switch statement.
823     int index_;
824     // The channel.
825     Expression* channel_;
826     // The value to send or the lvalue to receive into.
827     Expression* val_;
828     // The lvalue to set to whether the channel is closed on a
829     // receive.
830     Expression* closed_;
831     // The variable to initialize, for "case a := <-ch".
832     Named_object* var_;
833     // The variable to initialize to whether the channel is closed,
834     // for "case a, c := <-ch".
835     Named_object* closedvar_;
836     // The statements to execute.
837     Block* statements_;
838     // The location of this clause.
839     Location location_;
840     // Whether this is a send or a receive.
841     bool is_send_;
842     // Whether this is the default.
843     bool is_default_;
844     // Whether this has been lowered.
845     bool is_lowered_;
846   };
847
848   typedef std::vector<Select_clause> Clauses;
849
850   Clauses clauses_;
851 };
852
853 // A select statement.
854
855 class Select_statement : public Statement
856 {
857  public:
858   Select_statement(Location location)
859     : Statement(STATEMENT_SELECT, location),
860       clauses_(NULL), sel_(NULL), break_label_(NULL), is_lowered_(false)
861   { }
862
863   // Add the clauses.
864   void
865   add_clauses(Select_clauses* clauses)
866   {
867     go_assert(this->clauses_ == NULL);
868     this->clauses_ = clauses;
869   }
870
871   // Return the break label for this select statement.
872   Unnamed_label*
873   break_label();
874
875  protected:
876   int
877   do_traverse(Traverse* traverse)
878   { return this->clauses_->traverse(traverse); }
879
880   Statement*
881   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
882
883   void
884   do_determine_types()
885   { this->clauses_->determine_types(); }
886
887   void
888   do_check_types(Gogo*)
889   { this->clauses_->check_types(); }
890
891   bool
892   do_may_fall_through() const
893   { return this->clauses_->may_fall_through(); }
894
895   Bstatement*
896   do_get_backend(Translate_context*);
897
898   void
899   do_dump_statement(Ast_dump_context*) const;
900
901  private:
902   // The select clauses.
903   Select_clauses* clauses_;
904   // A temporary which holds the select structure we build up at runtime.
905   Temporary_statement* sel_;
906   // The break label.
907   Unnamed_label* break_label_;
908   // Whether this statement has been lowered.
909   bool is_lowered_;
910 };
911
912 // A statement which requires a thunk: go or defer.
913
914 class Thunk_statement : public Statement
915 {
916  public:
917   Thunk_statement(Statement_classification, Call_expression*,
918                   Location);
919
920   // Return the call expression.
921   Expression*
922   call() const
923   { return this->call_; }
924
925   // Simplify a go or defer statement so that it only uses a single
926   // parameter.
927   bool
928   simplify_statement(Gogo*, Named_object*, Block*);
929
930  protected:
931   int
932   do_traverse(Traverse* traverse);
933
934   bool
935   do_traverse_assignments(Traverse_assignments*);
936
937   void
938   do_determine_types();
939
940   void
941   do_check_types(Gogo*);
942
943   // Return the function and argument for the call.
944   bool
945   get_fn_and_arg(Expression** pfn, Expression** parg);
946
947  private:
948   // Return whether this is a simple go statement.
949   bool
950   is_simple(Function_type*) const;
951
952   // Return whether the thunk function is a constant.
953   bool
954   is_constant_function() const;
955
956   // Build the struct to use for a complex case.
957   Struct_type*
958   build_struct(Function_type* fntype);
959
960   // Build the thunk.
961   void
962   build_thunk(Gogo*, const std::string&);
963
964   // Set the name to use for thunk field N.
965   void
966   thunk_field_param(int n, char* buf, size_t buflen);
967
968   // The function call to be executed in a separate thread (go) or
969   // later (defer).
970   Expression* call_;
971   // The type used for a struct to pass to a thunk, if this is not a
972   // simple call.
973   Struct_type* struct_type_;
974 };
975
976 // A go statement.
977
978 class Go_statement : public Thunk_statement
979 {
980  public:
981   Go_statement(Call_expression* call, Location location)
982     : Thunk_statement(STATEMENT_GO, call, location)
983   { }
984
985  protected:
986   Bstatement*
987   do_get_backend(Translate_context*);
988
989   void
990   do_dump_statement(Ast_dump_context*) const;
991 };
992
993 // A defer statement.
994
995 class Defer_statement : public Thunk_statement
996 {
997  public:
998   Defer_statement(Call_expression* call, Location location)
999     : Thunk_statement(STATEMENT_DEFER, call, location)
1000   { }
1001
1002  protected:
1003   Bstatement*
1004   do_get_backend(Translate_context*);
1005
1006   void
1007   do_dump_statement(Ast_dump_context*) const;
1008 };
1009
1010 // A label statement.
1011
1012 class Label_statement : public Statement
1013 {
1014  public:
1015   Label_statement(Label* label, Location location)
1016     : Statement(STATEMENT_LABEL, location),
1017       label_(label)
1018   { }
1019
1020   // Return the label itself.
1021   const Label*
1022   label() const
1023   { return this->label_; }
1024
1025  protected:
1026   int
1027   do_traverse(Traverse*);
1028
1029   Bstatement*
1030   do_get_backend(Translate_context*);
1031
1032   void
1033   do_dump_statement(Ast_dump_context*) const;
1034
1035  private:
1036   // The label.
1037   Label* label_;
1038 };
1039
1040 // A for statement.
1041
1042 class For_statement : public Statement
1043 {
1044  public:
1045   For_statement(Block* init, Expression* cond, Block* post,
1046                 Location location)
1047     : Statement(STATEMENT_FOR, location),
1048       init_(init), cond_(cond), post_(post), statements_(NULL),
1049       break_label_(NULL), continue_label_(NULL)
1050   { }
1051
1052   // Add the statements.
1053   void
1054   add_statements(Block* statements)
1055   {
1056     go_assert(this->statements_ == NULL);
1057     this->statements_ = statements;
1058   }
1059
1060   // Return the break label for this for statement.
1061   Unnamed_label*
1062   break_label();
1063
1064   // Return the continue label for this for statement.
1065   Unnamed_label*
1066   continue_label();
1067
1068   // Set the break and continue labels for this statement.
1069   void
1070   set_break_continue_labels(Unnamed_label* break_label,
1071                             Unnamed_label* continue_label);
1072
1073  protected:
1074   int
1075   do_traverse(Traverse*);
1076
1077   bool
1078   do_traverse_assignments(Traverse_assignments*)
1079   { go_unreachable(); }
1080
1081   Statement*
1082   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1083
1084   Bstatement*
1085   do_get_backend(Translate_context*)
1086   { go_unreachable(); }
1087
1088   void
1089   do_dump_statement(Ast_dump_context*) const;
1090
1091  private:
1092   // The initialization statements.  This may be NULL.
1093   Block* init_;
1094   // The condition.  This may be NULL.
1095   Expression* cond_;
1096   // The statements to run after each iteration.  This may be NULL.
1097   Block* post_;
1098   // The statements in the loop itself.
1099   Block* statements_;
1100   // The break label, if needed.
1101   Unnamed_label* break_label_;
1102   // The continue label, if needed.
1103   Unnamed_label* continue_label_;
1104 };
1105
1106 // A for statement over a range clause.
1107
1108 class For_range_statement : public Statement
1109 {
1110  public:
1111   For_range_statement(Expression* index_var, Expression* value_var,
1112                       Expression* range, Location location)
1113     : Statement(STATEMENT_FOR_RANGE, location),
1114       index_var_(index_var), value_var_(value_var), range_(range),
1115       statements_(NULL), break_label_(NULL), continue_label_(NULL)
1116   { }
1117
1118   // Add the statements.
1119   void
1120   add_statements(Block* statements)
1121   {
1122     go_assert(this->statements_ == NULL);
1123     this->statements_ = statements;
1124   }
1125
1126   // Return the break label for this for statement.
1127   Unnamed_label*
1128   break_label();
1129
1130   // Return the continue label for this for statement.
1131   Unnamed_label*
1132   continue_label();
1133
1134  protected:
1135   int
1136   do_traverse(Traverse*);
1137
1138   bool
1139   do_traverse_assignments(Traverse_assignments*)
1140   { go_unreachable(); }
1141
1142   Statement*
1143   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1144
1145   Bstatement*
1146   do_get_backend(Translate_context*)
1147   { go_unreachable(); }
1148
1149   void
1150   do_dump_statement(Ast_dump_context*) const;
1151
1152  private:
1153   Expression*
1154   make_range_ref(Named_object*, Temporary_statement*, Location);
1155
1156   Expression*
1157   call_builtin(Gogo*, const char* funcname, Expression* arg, Location);
1158
1159   void
1160   lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1161                     Temporary_statement*, Temporary_statement*,
1162                     Block**, Expression**, Block**, Block**);
1163
1164   void
1165   lower_range_slice(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1166                     Temporary_statement*, Temporary_statement*,
1167                     Block**, Expression**, Block**, Block**);
1168
1169   void
1170   lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1171                      Temporary_statement*, Temporary_statement*,
1172                      Block**, Expression**, Block**, Block**);
1173
1174   void
1175   lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1176                   Temporary_statement*, Temporary_statement*,
1177                   Block**, Expression**, Block**, Block**);
1178
1179   void
1180   lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1181                       Temporary_statement*, Temporary_statement*,
1182                       Temporary_statement*, Block**, Expression**, Block**,
1183                       Block**);
1184
1185   // The variable which is set to the index value.
1186   Expression* index_var_;
1187   // The variable which is set to the element value.  This may be
1188   // NULL.
1189   Expression* value_var_;
1190   // The expression we are ranging over.
1191   Expression* range_;
1192   // The statements in the block.
1193   Block* statements_;
1194   // The break label, if needed.
1195   Unnamed_label* break_label_;
1196   // The continue label, if needed.
1197   Unnamed_label* continue_label_;
1198 };
1199
1200 // Class Case_clauses holds the clauses of a switch statement.  This
1201 // is built by the parser.
1202
1203 class Case_clauses
1204 {
1205  public:
1206   Case_clauses()
1207     : clauses_()
1208   { }
1209
1210   // Add a new clause.  CASES is a list of case expressions; it may be
1211   // NULL.  IS_DEFAULT is true if this is the default case.
1212   // STATEMENTS is a block of statements.  IS_FALLTHROUGH is true if
1213   // after the statements the case clause should fall through to the
1214   // next clause.
1215   void
1216   add(Expression_list* cases, bool is_default, Block* statements,
1217       bool is_fallthrough, Location location)
1218   {
1219     this->clauses_.push_back(Case_clause(cases, is_default, statements,
1220                                          is_fallthrough, location));
1221   }
1222
1223   // Return whether there are no clauses.
1224   bool
1225   empty() const
1226   { return this->clauses_.empty(); }
1227
1228   // Traverse the case clauses.
1229   int
1230   traverse(Traverse*);
1231
1232   // Lower for a nonconstant switch.
1233   void
1234   lower(Block*, Temporary_statement*, Unnamed_label*) const;
1235
1236   // Determine types of expressions.  The Type parameter is the type
1237   // of the switch value.
1238   void
1239   determine_types(Type*);
1240
1241   // Check types.  The Type parameter is the type of the switch value.
1242   bool
1243   check_types(Type*);
1244
1245   // Return true if all the clauses are constant values.
1246   bool
1247   is_constant() const;
1248
1249   // Return true if these clauses may fall through to the statements
1250   // following the switch statement.
1251   bool
1252   may_fall_through() const;
1253
1254   // Return the body of a SWITCH_EXPR when all the clauses are
1255   // constants.
1256   void
1257   get_backend(Translate_context*, Unnamed_label* break_label,
1258               std::vector<std::vector<Bexpression*> >* all_cases,
1259               std::vector<Bstatement*>* all_statements) const;
1260
1261   // Dump the AST representation to a dump context.
1262   void
1263   dump_clauses(Ast_dump_context*) const;
1264   
1265  private:
1266   // For a constant switch we need to keep a record of constants we
1267   // have already seen.
1268   class Hash_integer_value;
1269   class Eq_integer_value;
1270   typedef Unordered_set_hash(Expression*, Hash_integer_value,
1271                              Eq_integer_value) Case_constants;
1272
1273   // One case clause.
1274   class Case_clause
1275   {
1276    public:
1277     Case_clause()
1278       : cases_(NULL), statements_(NULL), is_default_(false),
1279         is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1280     { }
1281
1282     Case_clause(Expression_list* cases, bool is_default, Block* statements,
1283                 bool is_fallthrough, Location location)
1284       : cases_(cases), statements_(statements), is_default_(is_default),
1285         is_fallthrough_(is_fallthrough), location_(location)
1286     { }
1287
1288     // Whether this clause falls through to the next clause.
1289     bool
1290     is_fallthrough() const
1291     { return this->is_fallthrough_; }
1292
1293     // Whether this is the default.
1294     bool
1295     is_default() const
1296     { return this->is_default_; }
1297
1298     // The location of this clause.
1299     Location
1300     location() const
1301     { return this->location_; }
1302
1303     // Traversal.
1304     int
1305     traverse(Traverse*);
1306
1307     // Lower for a nonconstant switch.
1308     void
1309     lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1310
1311     // Determine types.
1312     void
1313     determine_types(Type*);
1314
1315     // Check types.
1316     bool
1317     check_types(Type*);
1318
1319     // Return true if all the case expressions are constant.
1320     bool
1321     is_constant() const;
1322
1323     // Return true if this clause may fall through to execute the
1324     // statements following the switch statement.  This is not the
1325     // same as whether this clause falls through to the next clause.
1326     bool
1327     may_fall_through() const;
1328
1329     // Convert the case values and statements to the backend
1330     // representation.
1331     Bstatement*
1332     get_backend(Translate_context*, Unnamed_label* break_label,
1333                 Case_constants*, std::vector<Bexpression*>* cases) const;
1334
1335     // Dump the AST representation to a dump context.
1336     void
1337     dump_clause(Ast_dump_context*) const;
1338   
1339    private:
1340     // The list of case expressions.
1341     Expression_list* cases_;
1342     // The statements to execute.
1343     Block* statements_;
1344     // Whether this is the default case.
1345     bool is_default_;
1346     // Whether this falls through after the statements.
1347     bool is_fallthrough_;
1348     // The location of this case clause.
1349     Location location_;
1350   };
1351
1352   friend class Case_clause;
1353
1354   // The type of the list of clauses.
1355   typedef std::vector<Case_clause> Clauses;
1356
1357   // All the case clauses.
1358   Clauses clauses_;
1359 };
1360
1361 // A switch statement.
1362
1363 class Switch_statement : public Statement
1364 {
1365  public:
1366   Switch_statement(Expression* val, Location location)
1367     : Statement(STATEMENT_SWITCH, location),
1368       val_(val), clauses_(NULL), break_label_(NULL)
1369   { }
1370
1371   // Add the clauses.
1372   void
1373   add_clauses(Case_clauses* clauses)
1374   {
1375     go_assert(this->clauses_ == NULL);
1376     this->clauses_ = clauses;
1377   }
1378
1379   // Return the break label for this switch statement.
1380   Unnamed_label*
1381   break_label();
1382
1383  protected:
1384   int
1385   do_traverse(Traverse*);
1386
1387   Statement*
1388   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1389
1390   Bstatement*
1391   do_get_backend(Translate_context*)
1392   { go_unreachable(); }
1393
1394   void
1395   do_dump_statement(Ast_dump_context*) const;
1396
1397  private:
1398   // The value to switch on.  This may be NULL.
1399   Expression* val_;
1400   // The case clauses.
1401   Case_clauses* clauses_;
1402   // The break label, if needed.
1403   Unnamed_label* break_label_;
1404 };
1405
1406 // Class Type_case_clauses holds the clauses of a type switch
1407 // statement.  This is built by the parser.
1408
1409 class Type_case_clauses
1410 {
1411  public:
1412   Type_case_clauses()
1413     : clauses_()
1414   { }
1415
1416   // Add a new clause.  TYPE is the type for this clause; it may be
1417   // NULL.  IS_FALLTHROUGH is true if this falls through to the next
1418   // clause; in this case STATEMENTS will be NULL.  IS_DEFAULT is true
1419   // if this is the default case.  STATEMENTS is a block of
1420   // statements; it may be NULL.
1421   void
1422   add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1423       Location location)
1424   {
1425     this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1426                                               statements, location));
1427   }
1428
1429   // Return whether there are no clauses.
1430   bool
1431   empty() const
1432   { return this->clauses_.empty(); }
1433
1434   // Traverse the type case clauses.
1435   int
1436   traverse(Traverse*);
1437
1438   // Check for duplicates.
1439   void
1440   check_duplicates() const;
1441
1442   // Lower to if and goto statements.
1443   void
1444   lower(Block*, Temporary_statement* descriptor_temp,
1445         Unnamed_label* break_label) const;
1446
1447   // Dump the AST representation to a dump context.
1448   void
1449   dump_clauses(Ast_dump_context*) const;
1450
1451  private:
1452   // One type case clause.
1453   class Type_case_clause
1454   {
1455    public:
1456     Type_case_clause()
1457       : type_(NULL), statements_(NULL), is_default_(false),
1458         location_(UNKNOWN_LOCATION)
1459     { }
1460
1461     Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1462                      Block* statements, Location location)
1463       : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1464         is_default_(is_default), location_(location)
1465     { }
1466
1467     // The type.
1468     Type*
1469     type() const
1470     { return this->type_; }
1471
1472     // Whether this is the default.
1473     bool
1474     is_default() const
1475     { return this->is_default_; }
1476
1477     // The location of this type clause.
1478     Location
1479     location() const
1480     { return this->location_; }
1481
1482     // Traversal.
1483     int
1484     traverse(Traverse*);
1485
1486     // Lower to if and goto statements.
1487     void
1488     lower(Block*, Temporary_statement* descriptor_temp,
1489           Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1490
1491     // Dump the AST representation to a dump context.
1492     void
1493     dump_clause(Ast_dump_context*) const;
1494
1495    private:
1496     // The type for this type clause.
1497     Type* type_;
1498     // The statements to execute.
1499     Block* statements_;
1500     // Whether this falls through--this is true for "case T1, T2".
1501     bool is_fallthrough_;
1502     // Whether this is the default case.
1503     bool is_default_;
1504     // The location of this type case clause.
1505     Location location_;
1506   };
1507
1508   friend class Type_case_clause;
1509
1510   // The type of the list of type clauses.
1511   typedef std::vector<Type_case_clause> Type_clauses;
1512
1513   // All the type case clauses.
1514   Type_clauses clauses_;
1515 };
1516
1517 // A type switch statement.
1518
1519 class Type_switch_statement : public Statement
1520 {
1521  public:
1522   Type_switch_statement(Named_object* var, Expression* expr,
1523                         Location location)
1524     : Statement(STATEMENT_TYPE_SWITCH, location),
1525       var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1526   { go_assert(var == NULL || expr == NULL); }
1527
1528   // Add the clauses.
1529   void
1530   add_clauses(Type_case_clauses* clauses)
1531   {
1532     go_assert(this->clauses_ == NULL);
1533     this->clauses_ = clauses;
1534   }
1535
1536   // Return the break label for this type switch statement.
1537   Unnamed_label*
1538   break_label();
1539
1540  protected:
1541   int
1542   do_traverse(Traverse*);
1543
1544   Statement*
1545   do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1546
1547   Bstatement*
1548   do_get_backend(Translate_context*)
1549   { go_unreachable(); }
1550
1551   void
1552   do_dump_statement(Ast_dump_context*) const;
1553
1554  private:
1555   // The variable holding the value we are switching on.
1556   Named_object* var_;
1557   // The expression we are switching on if there is no variable.
1558   Expression* expr_;
1559   // The type case clauses.
1560   Type_case_clauses* clauses_;
1561   // The break label, if needed.
1562   Unnamed_label* break_label_;
1563 };
1564
1565 #endif // !defined(GO_STATEMENTS_H)