OSDN Git Service

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