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