2014-04-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / go / gofrontend / statements.h
blob7d9bcfde8b7083cc2195ff17a5caa8c1ce06e56d
1 // statements.h -- Go frontend statements. -*- C++ -*-
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.
7 #ifndef GO_STATEMENTS_H
8 #define GO_STATEMENTS_H
10 #include "operator.h"
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 Expression_statement;
21 class Return_statement;
22 class Thunk_statement;
23 class Label_statement;
24 class For_statement;
25 class For_range_statement;
26 class Switch_statement;
27 class Type_switch_statement;
28 class Send_statement;
29 class Select_statement;
30 class Variable;
31 class Named_object;
32 class Label;
33 class Translate_context;
34 class Expression;
35 class Expression_list;
36 class Struct_type;
37 class Call_expression;
38 class Map_index_expression;
39 class Receive_expression;
40 class Case_clauses;
41 class Type_case_clauses;
42 class Select_clauses;
43 class Typed_identifier_list;
44 class Bexpression;
45 class Bstatement;
46 class Bvariable;
47 class Ast_dump_context;
49 // This class is used to traverse assignments made by a statement
50 // which makes assignments.
52 class Traverse_assignments
54 public:
55 Traverse_assignments()
56 { }
58 virtual ~Traverse_assignments()
59 { }
61 // This is called for a variable initialization.
62 virtual void
63 initialize_variable(Named_object*) = 0;
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;
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;
85 // A single statement.
87 class Statement
89 public:
90 // The types of statements.
91 enum Statement_classification
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,
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
127 Statement(Statement_classification, Location);
129 virtual ~Statement();
131 // Make a variable declaration.
132 static Statement*
133 make_variable_declaration(Named_object*);
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);
145 // Make an assignment statement.
146 static Statement*
147 make_assignment(Expression*, Expression*, Location);
149 // Make an assignment operation (+=, etc.).
150 static Statement*
151 make_assignment_operation(Operator, Expression*, Expression*,
152 Location);
154 // Make a tuple assignment statement.
155 static Statement*
156 make_tuple_assignment(Expression_list*, Expression_list*, Location);
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);
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);
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);
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);
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);
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);
191 // Make an increment statement.
192 static Statement*
193 make_inc_statement(Expression*);
195 // Make a decrement statement.
196 static Statement*
197 make_dec_statement(Expression*);
199 // Make a go statement.
200 static Statement*
201 make_go_statement(Call_expression* call, Location);
203 // Make a defer statement.
204 static Statement*
205 make_defer_statement(Call_expression* call, Location);
207 // Make a return statement.
208 static Return_statement*
209 make_return_statement(Expression_list*, Location);
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);
218 // Make a break statement.
219 static Statement*
220 make_break_statement(Unnamed_label* label, Location);
222 // Make a continue statement.
223 static Statement*
224 make_continue_statement(Unnamed_label* label, Location);
226 // Make a goto statement.
227 static Statement*
228 make_goto_statement(Label* label, Location);
230 // Make a goto statement to an unnamed label.
231 static Statement*
232 make_goto_unnamed_statement(Unnamed_label* label, Location);
234 // Make a label statement--where the label is defined.
235 static Statement*
236 make_label_statement(Label* label, Location);
238 // Make an unnamed label statement--where the label is defined.
239 static Statement*
240 make_unnamed_label_statement(Unnamed_label* label);
242 // Make an if statement.
243 static Statement*
244 make_if_statement(Expression* cond, Block* then_block, Block* else_block,
245 Location);
247 // Make a switch statement.
248 static Switch_statement*
249 make_switch_statement(Expression* switch_val, Location);
251 // Make a type switch statement.
252 static Type_switch_statement*
253 make_type_switch_statement(Named_object* var, Expression*, Location);
255 // Make a send statement.
256 static Send_statement*
257 make_send_statement(Expression* channel, Expression* val, Location);
259 // Make a select statement.
260 static Select_statement*
261 make_select_statement(Location);
263 // Make a for statement.
264 static For_statement*
265 make_for_statement(Block* init, Expression* cond, Block* post,
266 Location location);
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);
273 // Return the statement classification.
274 Statement_classification
275 classification() const
276 { return this->classification_; }
278 // Get the statement location.
279 Location
280 location() const
281 { return this->location_; }
283 // Traverse the tree.
285 traverse(Block*, size_t* index, Traverse*);
287 // Traverse the contents of this statement--the expressions and
288 // statements which it contains.
290 traverse_contents(Traverse*);
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);
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); }
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); }
319 // Set type information for unnamed constants.
320 void
321 determine_types();
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); }
329 // Return whether this is a block statement.
330 bool
331 is_block_statement() const
332 { return this->classification_ == STATEMENT_BLOCK; }
334 // If this is a variable declaration statement, return it.
335 // Otherwise return NULL.
336 Variable_declaration_statement*
337 variable_declaration_statement()
339 return this->convert<Variable_declaration_statement,
340 STATEMENT_VARIABLE_DECLARATION>();
343 // If this is an expression statement, return it. Otherwise return
344 // NULL.
345 Expression_statement*
346 expression_statement()
348 return this->convert<Expression_statement, STATEMENT_EXPRESSION>();
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>(); }
356 // If this is a thunk statement (a go or defer statement), return
357 // it. Otherwise return NULL.
358 Thunk_statement*
359 thunk_statement();
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>(); }
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>(); }
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>(); }
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>(); }
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>(); }
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>(); }
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(); }
400 // Convert the statement to the backend representation.
401 Bstatement*
402 get_backend(Translate_context*);
404 // Dump AST representation of a statement to a dump context.
405 void
406 dump_statement(Ast_dump_context*) const;
408 protected:
409 // Implemented by child class: traverse the tree.
410 virtual int
411 do_traverse(Traverse*) = 0;
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; }
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; }
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; }
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()
438 // Implemented by child class: check types of expressions used in a
439 // statement.
440 virtual void
441 do_check_types(Gogo*)
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; }
450 // Implemented by child class: convert to backend representation.
451 virtual Bstatement*
452 do_get_backend(Translate_context*) = 0;
454 // Implemented by child class: dump ast representation.
455 virtual void
456 do_dump_statement(Ast_dump_context*) const = 0;
458 // Traverse an expression in a statement.
460 traverse_expression(Traverse*, Expression**);
462 // Traverse an expression list in a statement. The Expression_list
463 // may be NULL.
465 traverse_expression_list(Traverse*, Expression_list*);
467 // Traverse a type in a statement.
469 traverse_type(Traverse*, Type*);
471 // For children to call when they detect that they are in error.
472 void
473 set_is_error();
475 // For children to call to report an error conveniently.
476 void
477 report_error(const char*);
479 // For children to return an error statement from lower().
480 static Statement*
481 make_error_statement(Location);
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()
490 return (this->classification_ == sc
491 ? static_cast<Statement_class*>(this)
492 : NULL);
495 template<typename Statement_class, Statement_classification sc>
496 const Statement_class*
497 convert() const
499 return (this->classification_ == sc
500 ? static_cast<const Statement_class*>(this)
501 : NULL);
504 // The statement classification.
505 Statement_classification classification_;
506 // The location in the input file of the start of this statement.
507 Location location_;
510 // A statement which creates and initializes a temporary variable.
512 class Temporary_statement : public Statement
514 public:
515 Temporary_statement(Type* type, Expression* init, Location location)
516 : Statement(STATEMENT_TEMPORARY, location),
517 type_(type), init_(init), bvariable_(NULL), are_hidden_fields_ok_(false),
518 is_address_taken_(false)
521 // Return the type of the temporary variable.
522 Type*
523 type() const;
525 // Return the initializer if there is one.
526 Expression*
527 init() const
528 { return this->init_; }
530 // Note that it is OK for this statement to set hidden fields.
531 void
532 set_hidden_fields_are_ok()
533 { this->are_hidden_fields_ok_ = true; }
535 // Record that something takes the address of this temporary
536 // variable.
537 void
538 set_is_address_taken()
539 { this->is_address_taken_ = true; }
541 // Return the temporary variable. This should not be called until
542 // after the statement itself has been converted.
543 Bvariable*
544 get_backend_variable(Translate_context*) const;
546 protected:
548 do_traverse(Traverse*);
550 bool
551 do_traverse_assignments(Traverse_assignments*);
553 void
554 do_determine_types();
556 void
557 do_check_types(Gogo*);
559 Bstatement*
560 do_get_backend(Translate_context*);
562 void
563 do_dump_statement(Ast_dump_context*) const;
565 private:
566 // The type of the temporary variable.
567 Type* type_;
568 // The initial value of the temporary variable. This may be NULL.
569 Expression* init_;
570 // The backend representation of the temporary variable.
571 Bvariable* bvariable_;
572 // True if this statement may set hidden fields when assigning the
573 // value to the temporary. This is used for generated method stubs.
574 bool are_hidden_fields_ok_;
575 // True if something takes the address of this temporary variable.
576 bool is_address_taken_;
579 // A variable declaration. This marks the point in the code where a
580 // variable is declared. The Variable is also attached to a Block.
582 class Variable_declaration_statement : public Statement
584 public:
585 Variable_declaration_statement(Named_object* var);
587 // The variable being declared.
588 Named_object*
589 var()
590 { return this->var_; }
592 protected:
594 do_traverse(Traverse*);
596 bool
597 do_traverse_assignments(Traverse_assignments*);
599 Statement*
600 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
602 Statement*
603 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
605 Bstatement*
606 do_get_backend(Translate_context*);
608 void
609 do_dump_statement(Ast_dump_context*) const;
611 private:
612 Named_object* var_;
615 // A return statement.
617 class Return_statement : public Statement
619 public:
620 Return_statement(Expression_list* vals, Location location)
621 : Statement(STATEMENT_RETURN, location),
622 vals_(vals), are_hidden_fields_ok_(false), is_lowered_(false)
625 // The list of values being returned. This may be NULL.
626 const Expression_list*
627 vals() const
628 { return this->vals_; }
630 // Note that it is OK for this return statement to set hidden
631 // fields.
632 void
633 set_hidden_fields_are_ok()
634 { this->are_hidden_fields_ok_ = true; }
636 protected:
638 do_traverse(Traverse* traverse)
639 { return this->traverse_expression_list(traverse, this->vals_); }
641 bool
642 do_traverse_assignments(Traverse_assignments*);
644 Statement*
645 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
647 bool
648 do_may_fall_through() const
649 { return false; }
651 Bstatement*
652 do_get_backend(Translate_context*);
654 void
655 do_dump_statement(Ast_dump_context*) const;
657 private:
658 // Return values. This may be NULL.
659 Expression_list* vals_;
660 // True if this statement may pass hidden fields in the return
661 // value. This is used for generated method stubs.
662 bool are_hidden_fields_ok_;
663 // True if this statement has been lowered.
664 bool is_lowered_;
667 // An expression statement.
669 class Expression_statement : public Statement
671 public:
672 Expression_statement(Expression* expr, bool is_ignored);
674 Expression*
675 expr()
676 { return this->expr_; }
678 protected:
680 do_traverse(Traverse* traverse)
681 { return this->traverse_expression(traverse, &this->expr_); }
683 void
684 do_determine_types();
686 void
687 do_check_types(Gogo*);
689 bool
690 do_may_fall_through() const;
692 Bstatement*
693 do_get_backend(Translate_context* context);
695 void
696 do_dump_statement(Ast_dump_context*) const;
698 private:
699 Expression* expr_;
700 // Whether the value of this expression is being explicitly ignored.
701 bool is_ignored_;
704 // A send statement.
706 class Send_statement : public Statement
708 public:
709 Send_statement(Expression* channel, Expression* val,
710 Location location)
711 : Statement(STATEMENT_SEND, location),
712 channel_(channel), val_(val)
715 protected:
717 do_traverse(Traverse* traverse);
719 void
720 do_determine_types();
722 void
723 do_check_types(Gogo*);
725 Bstatement*
726 do_get_backend(Translate_context*);
728 void
729 do_dump_statement(Ast_dump_context*) const;
731 private:
732 // The channel on which to send the value.
733 Expression* channel_;
734 // The value to send.
735 Expression* val_;
738 // Select_clauses holds the clauses of a select statement. This is
739 // built by the parser.
741 class Select_clauses
743 public:
744 Select_clauses()
745 : clauses_()
748 // Add a new clause. IS_SEND is true if this is a send clause,
749 // false for a receive clause. For a send clause CHANNEL is the
750 // channel and VAL is the value to send. For a receive clause
751 // CHANNEL is the channel, VAL is either NULL or a Var_expression
752 // for the variable to set, and CLOSED is either NULL or a
753 // Var_expression to set to whether the channel is closed. If VAL
754 // is NULL, VAR may be a variable to be initialized with the
755 // received value, and CLOSEDVAR ma be a variable to be initialized
756 // with whether the channel is closed. IS_DEFAULT is true if this
757 // is the default clause. STATEMENTS is the list of statements to
758 // execute.
759 void
760 add(bool is_send, Expression* channel, Expression* val, Expression* closed,
761 Named_object* var, Named_object* closedvar, bool is_default,
762 Block* statements, Location location)
764 int index = static_cast<int>(this->clauses_.size());
765 this->clauses_.push_back(Select_clause(index, is_send, channel, val,
766 closed, var, closedvar, is_default,
767 statements, location));
770 size_t
771 size() const
772 { return this->clauses_.size(); }
774 // Traverse the select clauses.
776 traverse(Traverse*);
778 // Lower statements.
779 void
780 lower(Gogo*, Named_object*, Block*, Temporary_statement*);
782 // Determine types.
783 void
784 determine_types();
786 // Check types.
787 void
788 check_types();
790 // Whether the select clauses may fall through to the statement
791 // which follows the overall select statement.
792 bool
793 may_fall_through() const;
795 // Convert to the backend representation.
796 Bstatement*
797 get_backend(Translate_context*, Temporary_statement* sel,
798 Unnamed_label* break_label, Location);
800 // Dump AST representation.
801 void
802 dump_clauses(Ast_dump_context*) const;
804 private:
805 // A single clause.
806 class Select_clause
808 public:
809 Select_clause()
810 : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
811 closedvar_(NULL), statements_(NULL), is_send_(false),
812 is_default_(false)
815 Select_clause(int index, bool is_send, Expression* channel,
816 Expression* val, Expression* closed, Named_object* var,
817 Named_object* closedvar, bool is_default, Block* statements,
818 Location location)
819 : index_(index), channel_(channel), val_(val), closed_(closed),
820 var_(var), closedvar_(closedvar), statements_(statements),
821 location_(location), is_send_(is_send), is_default_(is_default),
822 is_lowered_(false)
823 { go_assert(is_default ? channel == NULL : channel != NULL); }
825 // Return the index of this clause.
827 index() const
828 { return this->index_; }
830 // Traverse the select clause.
832 traverse(Traverse*);
834 // Lower statements.
835 void
836 lower(Gogo*, Named_object*, Block*, Temporary_statement*);
838 // Determine types.
839 void
840 determine_types();
842 // Check types.
843 void
844 check_types();
846 // Return true if this is the default clause.
847 bool
848 is_default() const
849 { return this->is_default_; }
851 // Return the channel. This will return NULL for the default
852 // clause.
853 Expression*
854 channel() const
855 { return this->channel_; }
857 // Return true for a send, false for a receive.
858 bool
859 is_send() const
861 go_assert(!this->is_default_);
862 return this->is_send_;
865 // Return the statements.
866 const Block*
867 statements() const
868 { return this->statements_; }
870 // Return the location.
871 Location
872 location() const
873 { return this->location_; }
875 // Whether this clause may fall through to the statement which
876 // follows the overall select statement.
877 bool
878 may_fall_through() const;
880 // Convert the statements to the backend representation.
881 Bstatement*
882 get_statements_backend(Translate_context*);
884 // Dump AST representation.
885 void
886 dump_clause(Ast_dump_context*) const;
888 private:
889 void
890 lower_default(Block*, Expression*, Expression*);
892 void
893 lower_send(Block*, Expression*, Expression*, Expression*);
895 void
896 lower_recv(Gogo*, Named_object*, Block*, Expression*, Expression*,
897 Expression*);
899 // The index of this case in the generated switch statement.
900 int index_;
901 // The channel.
902 Expression* channel_;
903 // The value to send or the lvalue to receive into.
904 Expression* val_;
905 // The lvalue to set to whether the channel is closed on a
906 // receive.
907 Expression* closed_;
908 // The variable to initialize, for "case a := <-ch".
909 Named_object* var_;
910 // The variable to initialize to whether the channel is closed,
911 // for "case a, c := <-ch".
912 Named_object* closedvar_;
913 // The statements to execute.
914 Block* statements_;
915 // The location of this clause.
916 Location location_;
917 // Whether this is a send or a receive.
918 bool is_send_;
919 // Whether this is the default.
920 bool is_default_;
921 // Whether this has been lowered.
922 bool is_lowered_;
925 typedef std::vector<Select_clause> Clauses;
927 Clauses clauses_;
930 // A select statement.
932 class Select_statement : public Statement
934 public:
935 Select_statement(Location location)
936 : Statement(STATEMENT_SELECT, location),
937 clauses_(NULL), sel_(NULL), break_label_(NULL), is_lowered_(false)
940 // Add the clauses.
941 void
942 add_clauses(Select_clauses* clauses)
944 go_assert(this->clauses_ == NULL);
945 this->clauses_ = clauses;
948 // Return the break label for this select statement.
949 Unnamed_label*
950 break_label();
952 protected:
954 do_traverse(Traverse* traverse)
955 { return this->clauses_->traverse(traverse); }
957 Statement*
958 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
960 void
961 do_determine_types()
962 { this->clauses_->determine_types(); }
964 void
965 do_check_types(Gogo*)
966 { this->clauses_->check_types(); }
968 bool
969 do_may_fall_through() const;
971 Bstatement*
972 do_get_backend(Translate_context*);
974 void
975 do_dump_statement(Ast_dump_context*) const;
977 private:
978 // The select clauses.
979 Select_clauses* clauses_;
980 // A temporary which holds the select structure we build up at runtime.
981 Temporary_statement* sel_;
982 // The break label.
983 Unnamed_label* break_label_;
984 // Whether this statement has been lowered.
985 bool is_lowered_;
988 // A statement which requires a thunk: go or defer.
990 class Thunk_statement : public Statement
992 public:
993 Thunk_statement(Statement_classification, Call_expression*,
994 Location);
996 // Return the call expression.
997 Expression*
998 call() const
999 { return this->call_; }
1001 // Simplify a go or defer statement so that it only uses a single
1002 // parameter.
1003 bool
1004 simplify_statement(Gogo*, Named_object*, Block*);
1006 protected:
1008 do_traverse(Traverse* traverse);
1010 bool
1011 do_traverse_assignments(Traverse_assignments*);
1013 void
1014 do_determine_types();
1016 void
1017 do_check_types(Gogo*);
1019 // Return the function and argument for the call.
1020 bool
1021 get_fn_and_arg(Expression** pfn, Expression** parg);
1023 private:
1024 // Return whether this is a simple go statement.
1025 bool
1026 is_simple(Function_type*) const;
1028 // Return whether the thunk function is a constant.
1029 bool
1030 is_constant_function() const;
1032 // Build the struct to use for a complex case.
1033 Struct_type*
1034 build_struct(Function_type* fntype);
1036 // Build the thunk.
1037 void
1038 build_thunk(Gogo*, const std::string&);
1040 // Set the name to use for thunk field N.
1041 void
1042 thunk_field_param(int n, char* buf, size_t buflen);
1044 // The function call to be executed in a separate thread (go) or
1045 // later (defer).
1046 Expression* call_;
1047 // The type used for a struct to pass to a thunk, if this is not a
1048 // simple call.
1049 Struct_type* struct_type_;
1052 // A go statement.
1054 class Go_statement : public Thunk_statement
1056 public:
1057 Go_statement(Call_expression* call, Location location)
1058 : Thunk_statement(STATEMENT_GO, call, location)
1061 protected:
1062 Bstatement*
1063 do_get_backend(Translate_context*);
1065 void
1066 do_dump_statement(Ast_dump_context*) const;
1069 // A defer statement.
1071 class Defer_statement : public Thunk_statement
1073 public:
1074 Defer_statement(Call_expression* call, Location location)
1075 : Thunk_statement(STATEMENT_DEFER, call, location)
1078 protected:
1079 Bstatement*
1080 do_get_backend(Translate_context*);
1082 void
1083 do_dump_statement(Ast_dump_context*) const;
1086 // A label statement.
1088 class Label_statement : public Statement
1090 public:
1091 Label_statement(Label* label, Location location)
1092 : Statement(STATEMENT_LABEL, location),
1093 label_(label)
1096 // Return the label itself.
1097 const Label*
1098 label() const
1099 { return this->label_; }
1101 protected:
1103 do_traverse(Traverse*);
1105 Bstatement*
1106 do_get_backend(Translate_context*);
1108 void
1109 do_dump_statement(Ast_dump_context*) const;
1111 private:
1112 // The label.
1113 Label* label_;
1116 // A for statement.
1118 class For_statement : public Statement
1120 public:
1121 For_statement(Block* init, Expression* cond, Block* post,
1122 Location location)
1123 : Statement(STATEMENT_FOR, location),
1124 init_(init), cond_(cond), post_(post), statements_(NULL),
1125 break_label_(NULL), continue_label_(NULL)
1128 // Add the statements.
1129 void
1130 add_statements(Block* statements)
1132 go_assert(this->statements_ == NULL);
1133 this->statements_ = statements;
1136 // Return the break label for this for statement.
1137 Unnamed_label*
1138 break_label();
1140 // Return the continue label for this for statement.
1141 Unnamed_label*
1142 continue_label();
1144 // Set the break and continue labels for this statement.
1145 void
1146 set_break_continue_labels(Unnamed_label* break_label,
1147 Unnamed_label* continue_label);
1149 protected:
1151 do_traverse(Traverse*);
1153 bool
1154 do_traverse_assignments(Traverse_assignments*)
1155 { go_unreachable(); }
1157 Statement*
1158 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1160 bool
1161 do_may_fall_through() const;
1163 Bstatement*
1164 do_get_backend(Translate_context*)
1165 { go_unreachable(); }
1167 void
1168 do_dump_statement(Ast_dump_context*) const;
1170 private:
1171 // The initialization statements. This may be NULL.
1172 Block* init_;
1173 // The condition. This may be NULL.
1174 Expression* cond_;
1175 // The statements to run after each iteration. This may be NULL.
1176 Block* post_;
1177 // The statements in the loop itself.
1178 Block* statements_;
1179 // The break label, if needed.
1180 Unnamed_label* break_label_;
1181 // The continue label, if needed.
1182 Unnamed_label* continue_label_;
1185 // A for statement over a range clause.
1187 class For_range_statement : public Statement
1189 public:
1190 For_range_statement(Expression* index_var, Expression* value_var,
1191 Expression* range, Location location)
1192 : Statement(STATEMENT_FOR_RANGE, location),
1193 index_var_(index_var), value_var_(value_var), range_(range),
1194 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1197 // Add the statements.
1198 void
1199 add_statements(Block* statements)
1201 go_assert(this->statements_ == NULL);
1202 this->statements_ = statements;
1205 // Return the break label for this for statement.
1206 Unnamed_label*
1207 break_label();
1209 // Return the continue label for this for statement.
1210 Unnamed_label*
1211 continue_label();
1213 protected:
1215 do_traverse(Traverse*);
1217 bool
1218 do_traverse_assignments(Traverse_assignments*)
1219 { go_unreachable(); }
1221 Statement*
1222 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1224 Bstatement*
1225 do_get_backend(Translate_context*)
1226 { go_unreachable(); }
1228 void
1229 do_dump_statement(Ast_dump_context*) const;
1231 private:
1232 Expression*
1233 make_range_ref(Named_object*, Temporary_statement*, Location);
1235 Expression*
1236 call_builtin(Gogo*, const char* funcname, Expression* arg, Location);
1238 void
1239 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1240 Temporary_statement*, Temporary_statement*,
1241 Block**, Expression**, Block**, Block**);
1243 void
1244 lower_range_slice(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1245 Temporary_statement*, Temporary_statement*,
1246 Block**, Expression**, Block**, Block**);
1248 void
1249 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1250 Temporary_statement*, Temporary_statement*,
1251 Block**, Expression**, Block**, Block**);
1253 void
1254 lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1255 Temporary_statement*, Temporary_statement*,
1256 Block**, Expression**, Block**, Block**);
1258 void
1259 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1260 Temporary_statement*, Temporary_statement*,
1261 Temporary_statement*, Block**, Expression**, Block**,
1262 Block**);
1264 // The variable which is set to the index value.
1265 Expression* index_var_;
1266 // The variable which is set to the element value. This may be
1267 // NULL.
1268 Expression* value_var_;
1269 // The expression we are ranging over.
1270 Expression* range_;
1271 // The statements in the block.
1272 Block* statements_;
1273 // The break label, if needed.
1274 Unnamed_label* break_label_;
1275 // The continue label, if needed.
1276 Unnamed_label* continue_label_;
1279 // Class Case_clauses holds the clauses of a switch statement. This
1280 // is built by the parser.
1282 class Case_clauses
1284 public:
1285 Case_clauses()
1286 : clauses_()
1289 // Add a new clause. CASES is a list of case expressions; it may be
1290 // NULL. IS_DEFAULT is true if this is the default case.
1291 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1292 // after the statements the case clause should fall through to the
1293 // next clause.
1294 void
1295 add(Expression_list* cases, bool is_default, Block* statements,
1296 bool is_fallthrough, Location location)
1298 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1299 is_fallthrough, location));
1302 // Return whether there are no clauses.
1303 bool
1304 empty() const
1305 { return this->clauses_.empty(); }
1307 // Traverse the case clauses.
1309 traverse(Traverse*);
1311 // Lower for a nonconstant switch.
1312 void
1313 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1315 // Determine types of expressions. The Type parameter is the type
1316 // of the switch value.
1317 void
1318 determine_types(Type*);
1320 // Check types. The Type parameter is the type of the switch value.
1321 bool
1322 check_types(Type*);
1324 // Return true if all the clauses are constant values.
1325 bool
1326 is_constant() const;
1328 // Return true if these clauses may fall through to the statements
1329 // following the switch statement.
1330 bool
1331 may_fall_through() const;
1333 // Return the body of a SWITCH_EXPR when all the clauses are
1334 // constants.
1335 void
1336 get_backend(Translate_context*, Unnamed_label* break_label,
1337 std::vector<std::vector<Bexpression*> >* all_cases,
1338 std::vector<Bstatement*>* all_statements) const;
1340 // Dump the AST representation to a dump context.
1341 void
1342 dump_clauses(Ast_dump_context*) const;
1344 private:
1345 // For a constant switch we need to keep a record of constants we
1346 // have already seen.
1347 class Hash_integer_value;
1348 class Eq_integer_value;
1349 typedef Unordered_set_hash(Expression*, Hash_integer_value,
1350 Eq_integer_value) Case_constants;
1352 // One case clause.
1353 class Case_clause
1355 public:
1356 Case_clause()
1357 : cases_(NULL), statements_(NULL), is_default_(false),
1358 is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1361 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1362 bool is_fallthrough, Location location)
1363 : cases_(cases), statements_(statements), is_default_(is_default),
1364 is_fallthrough_(is_fallthrough), location_(location)
1367 // Whether this clause falls through to the next clause.
1368 bool
1369 is_fallthrough() const
1370 { return this->is_fallthrough_; }
1372 // Whether this is the default.
1373 bool
1374 is_default() const
1375 { return this->is_default_; }
1377 // The location of this clause.
1378 Location
1379 location() const
1380 { return this->location_; }
1382 // Traversal.
1384 traverse(Traverse*);
1386 // Lower for a nonconstant switch.
1387 void
1388 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1390 // Determine types.
1391 void
1392 determine_types(Type*);
1394 // Check types.
1395 bool
1396 check_types(Type*);
1398 // Return true if all the case expressions are constant.
1399 bool
1400 is_constant() const;
1402 // Return true if this clause may fall through to execute the
1403 // statements following the switch statement. This is not the
1404 // same as whether this clause falls through to the next clause.
1405 bool
1406 may_fall_through() const;
1408 // Convert the case values and statements to the backend
1409 // representation.
1410 Bstatement*
1411 get_backend(Translate_context*, Unnamed_label* break_label,
1412 Case_constants*, std::vector<Bexpression*>* cases) const;
1414 // Dump the AST representation to a dump context.
1415 void
1416 dump_clause(Ast_dump_context*) const;
1418 private:
1419 // The list of case expressions.
1420 Expression_list* cases_;
1421 // The statements to execute.
1422 Block* statements_;
1423 // Whether this is the default case.
1424 bool is_default_;
1425 // Whether this falls through after the statements.
1426 bool is_fallthrough_;
1427 // The location of this case clause.
1428 Location location_;
1431 friend class Case_clause;
1433 // The type of the list of clauses.
1434 typedef std::vector<Case_clause> Clauses;
1436 // All the case clauses.
1437 Clauses clauses_;
1440 // A switch statement.
1442 class Switch_statement : public Statement
1444 public:
1445 Switch_statement(Expression* val, Location location)
1446 : Statement(STATEMENT_SWITCH, location),
1447 val_(val), clauses_(NULL), break_label_(NULL)
1450 // Add the clauses.
1451 void
1452 add_clauses(Case_clauses* clauses)
1454 go_assert(this->clauses_ == NULL);
1455 this->clauses_ = clauses;
1458 // Return the break label for this switch statement.
1459 Unnamed_label*
1460 break_label();
1462 protected:
1464 do_traverse(Traverse*);
1466 Statement*
1467 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1469 Bstatement*
1470 do_get_backend(Translate_context*)
1471 { go_unreachable(); }
1473 void
1474 do_dump_statement(Ast_dump_context*) const;
1476 bool
1477 do_may_fall_through() const;
1479 private:
1480 // The value to switch on. This may be NULL.
1481 Expression* val_;
1482 // The case clauses.
1483 Case_clauses* clauses_;
1484 // The break label, if needed.
1485 Unnamed_label* break_label_;
1488 // Class Type_case_clauses holds the clauses of a type switch
1489 // statement. This is built by the parser.
1491 class Type_case_clauses
1493 public:
1494 Type_case_clauses()
1495 : clauses_()
1498 // Add a new clause. TYPE is the type for this clause; it may be
1499 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1500 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1501 // if this is the default case. STATEMENTS is a block of
1502 // statements; it may be NULL.
1503 void
1504 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1505 Location location)
1507 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1508 statements, location));
1511 // Return whether there are no clauses.
1512 bool
1513 empty() const
1514 { return this->clauses_.empty(); }
1516 // Traverse the type case clauses.
1518 traverse(Traverse*);
1520 // Check for duplicates.
1521 void
1522 check_duplicates() const;
1524 // Lower to if and goto statements.
1525 void
1526 lower(Type*, Block*, Temporary_statement* descriptor_temp,
1527 Unnamed_label* break_label) const;
1529 // Return true if these clauses may fall through to the statements
1530 // following the switch statement.
1531 bool
1532 may_fall_through() const;
1534 // Dump the AST representation to a dump context.
1535 void
1536 dump_clauses(Ast_dump_context*) const;
1538 private:
1539 // One type case clause.
1540 class Type_case_clause
1542 public:
1543 Type_case_clause()
1544 : type_(NULL), statements_(NULL), is_default_(false),
1545 location_(UNKNOWN_LOCATION)
1548 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1549 Block* statements, Location location)
1550 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1551 is_default_(is_default), location_(location)
1554 // The type.
1555 Type*
1556 type() const
1557 { return this->type_; }
1559 // Whether this is the default.
1560 bool
1561 is_default() const
1562 { return this->is_default_; }
1564 // The location of this type clause.
1565 Location
1566 location() const
1567 { return this->location_; }
1569 // Traversal.
1571 traverse(Traverse*);
1573 // Lower to if and goto statements.
1574 void
1575 lower(Type*, Block*, Temporary_statement* descriptor_temp,
1576 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1578 // Return true if this clause may fall through to execute the
1579 // statements following the switch statement. This is not the
1580 // same as whether this clause falls through to the next clause.
1581 bool
1582 may_fall_through() const;
1584 // Dump the AST representation to a dump context.
1585 void
1586 dump_clause(Ast_dump_context*) const;
1588 private:
1589 // The type for this type clause.
1590 Type* type_;
1591 // The statements to execute.
1592 Block* statements_;
1593 // Whether this falls through--this is true for "case T1, T2".
1594 bool is_fallthrough_;
1595 // Whether this is the default case.
1596 bool is_default_;
1597 // The location of this type case clause.
1598 Location location_;
1601 friend class Type_case_clause;
1603 // The type of the list of type clauses.
1604 typedef std::vector<Type_case_clause> Type_clauses;
1606 // All the type case clauses.
1607 Type_clauses clauses_;
1610 // A type switch statement.
1612 class Type_switch_statement : public Statement
1614 public:
1615 Type_switch_statement(Named_object* var, Expression* expr,
1616 Location location)
1617 : Statement(STATEMENT_TYPE_SWITCH, location),
1618 var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1619 { go_assert(var == NULL || expr == NULL); }
1621 // Add the clauses.
1622 void
1623 add_clauses(Type_case_clauses* clauses)
1625 go_assert(this->clauses_ == NULL);
1626 this->clauses_ = clauses;
1629 // Return the break label for this type switch statement.
1630 Unnamed_label*
1631 break_label();
1633 protected:
1635 do_traverse(Traverse*);
1637 Statement*
1638 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1640 Bstatement*
1641 do_get_backend(Translate_context*)
1642 { go_unreachable(); }
1644 void
1645 do_dump_statement(Ast_dump_context*) const;
1647 bool
1648 do_may_fall_through() const;
1650 private:
1651 // The variable holding the value we are switching on.
1652 Named_object* var_;
1653 // The expression we are switching on if there is no variable.
1654 Expression* expr_;
1655 // The type case clauses.
1656 Type_case_clauses* clauses_;
1657 // The break label, if needed.
1658 Unnamed_label* break_label_;
1661 #endif // !defined(GO_STATEMENTS_H)