vectorizer cost model enhancement
[official-gcc.git] / gcc / go / gofrontend / statements.h
blobb128fa0a8eb1cb1e96168be75dc6281809eaf66f
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 // Set type information for unnamed constants.
310 void
311 determine_types();
313 // Check types in a statement. This simply checks that any
314 // expressions used by the statement have the right type.
315 void
316 check_types(Gogo* gogo)
317 { this->do_check_types(gogo); }
319 // Return whether this is a block statement.
320 bool
321 is_block_statement() const
322 { return this->classification_ == STATEMENT_BLOCK; }
324 // If this is a variable declaration statement, return it.
325 // Otherwise return NULL.
326 Variable_declaration_statement*
327 variable_declaration_statement()
329 return this->convert<Variable_declaration_statement,
330 STATEMENT_VARIABLE_DECLARATION>();
333 // If this is an expression statement, return it. Otherwise return
334 // NULL.
335 Expression_statement*
336 expression_statement()
338 return this->convert<Expression_statement, STATEMENT_EXPRESSION>();
341 // If this is a return statement, return it. Otherwise return NULL.
342 Return_statement*
343 return_statement()
344 { return this->convert<Return_statement, STATEMENT_RETURN>(); }
346 // If this is a thunk statement (a go or defer statement), return
347 // it. Otherwise return NULL.
348 Thunk_statement*
349 thunk_statement();
351 // If this is a label statement, return it. Otherwise return NULL.
352 Label_statement*
353 label_statement()
354 { return this->convert<Label_statement, STATEMENT_LABEL>(); }
356 // If this is a for statement, return it. Otherwise return NULL.
357 For_statement*
358 for_statement()
359 { return this->convert<For_statement, STATEMENT_FOR>(); }
361 // If this is a for statement over a range clause, return it.
362 // Otherwise return NULL.
363 For_range_statement*
364 for_range_statement()
365 { return this->convert<For_range_statement, STATEMENT_FOR_RANGE>(); }
367 // If this is a switch statement, return it. Otherwise return NULL.
368 Switch_statement*
369 switch_statement()
370 { return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
372 // If this is a type switch statement, return it. Otherwise return
373 // NULL.
374 Type_switch_statement*
375 type_switch_statement()
376 { return this->convert<Type_switch_statement, STATEMENT_TYPE_SWITCH>(); }
378 // If this is a select statement, return it. Otherwise return NULL.
379 Select_statement*
380 select_statement()
381 { return this->convert<Select_statement, STATEMENT_SELECT>(); }
383 // Return true if this statement may fall through--if after
384 // executing this statement we may go on to execute the following
385 // statement, if any.
386 bool
387 may_fall_through() const
388 { return this->do_may_fall_through(); }
390 // Convert the statement to the backend representation.
391 Bstatement*
392 get_backend(Translate_context*);
394 // Dump AST representation of a statement to a dump context.
395 void
396 dump_statement(Ast_dump_context*) const;
398 protected:
399 // Implemented by child class: traverse the tree.
400 virtual int
401 do_traverse(Traverse*) = 0;
403 // Implemented by child class: traverse assignments. Any statement
404 // which includes an assignment should implement this.
405 virtual bool
406 do_traverse_assignments(Traverse_assignments*)
407 { return false; }
409 // Implemented by the child class: lower this statement to a simpler
410 // one.
411 virtual Statement*
412 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
413 { return this; }
415 // Implemented by child class: set type information for unnamed
416 // constants. Any statement which includes an expression needs to
417 // implement this.
418 virtual void
419 do_determine_types()
422 // Implemented by child class: check types of expressions used in a
423 // statement.
424 virtual void
425 do_check_types(Gogo*)
428 // Implemented by child class: return true if this statement may
429 // fall through.
430 virtual bool
431 do_may_fall_through() const
432 { return true; }
434 // Implemented by child class: convert to backend representation.
435 virtual Bstatement*
436 do_get_backend(Translate_context*) = 0;
438 // Implemented by child class: dump ast representation.
439 virtual void
440 do_dump_statement(Ast_dump_context*) const = 0;
442 // Traverse an expression in a statement.
444 traverse_expression(Traverse*, Expression**);
446 // Traverse an expression list in a statement. The Expression_list
447 // may be NULL.
449 traverse_expression_list(Traverse*, Expression_list*);
451 // Traverse a type in a statement.
453 traverse_type(Traverse*, Type*);
455 // For children to call when they detect that they are in error.
456 void
457 set_is_error();
459 // For children to call to report an error conveniently.
460 void
461 report_error(const char*);
463 // For children to return an error statement from lower().
464 static Statement*
465 make_error_statement(Location);
467 private:
468 // Convert to the desired statement classification, or return NULL.
469 // This is a controlled dynamic cast.
470 template<typename Statement_class, Statement_classification sc>
471 Statement_class*
472 convert()
474 return (this->classification_ == sc
475 ? static_cast<Statement_class*>(this)
476 : NULL);
479 template<typename Statement_class, Statement_classification sc>
480 const Statement_class*
481 convert() const
483 return (this->classification_ == sc
484 ? static_cast<const Statement_class*>(this)
485 : NULL);
488 // The statement classification.
489 Statement_classification classification_;
490 // The location in the input file of the start of this statement.
491 Location location_;
494 // A statement which creates and initializes a temporary variable.
496 class Temporary_statement : public Statement
498 public:
499 Temporary_statement(Type* type, Expression* init, Location location)
500 : Statement(STATEMENT_TEMPORARY, location),
501 type_(type), init_(init), bvariable_(NULL), are_hidden_fields_ok_(false),
502 is_address_taken_(false)
505 // Return the type of the temporary variable.
506 Type*
507 type() const;
509 // Return the initializer if there is one.
510 Expression*
511 init() const
512 { return this->init_; }
514 // Note that it is OK for this statement to set hidden fields.
515 void
516 set_hidden_fields_are_ok()
517 { this->are_hidden_fields_ok_ = true; }
519 // Record that something takes the address of this temporary
520 // variable.
521 void
522 set_is_address_taken()
523 { this->is_address_taken_ = true; }
525 // Return the temporary variable. This should not be called until
526 // after the statement itself has been converted.
527 Bvariable*
528 get_backend_variable(Translate_context*) const;
530 protected:
532 do_traverse(Traverse*);
534 bool
535 do_traverse_assignments(Traverse_assignments*);
537 void
538 do_determine_types();
540 void
541 do_check_types(Gogo*);
543 Bstatement*
544 do_get_backend(Translate_context*);
546 void
547 do_dump_statement(Ast_dump_context*) const;
549 private:
550 // The type of the temporary variable.
551 Type* type_;
552 // The initial value of the temporary variable. This may be NULL.
553 Expression* init_;
554 // The backend representation of the temporary variable.
555 Bvariable* bvariable_;
556 // True if this statement may set hidden fields when assigning the
557 // value to the temporary. This is used for generated method stubs.
558 bool are_hidden_fields_ok_;
559 // True if something takes the address of this temporary variable.
560 bool is_address_taken_;
563 // A variable declaration. This marks the point in the code where a
564 // variable is declared. The Variable is also attached to a Block.
566 class Variable_declaration_statement : public Statement
568 public:
569 Variable_declaration_statement(Named_object* var);
571 // The variable being declared.
572 Named_object*
573 var()
574 { return this->var_; }
576 protected:
578 do_traverse(Traverse*);
580 bool
581 do_traverse_assignments(Traverse_assignments*);
583 Statement*
584 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
586 Bstatement*
587 do_get_backend(Translate_context*);
589 void
590 do_dump_statement(Ast_dump_context*) const;
592 private:
593 Named_object* var_;
596 // A return statement.
598 class Return_statement : public Statement
600 public:
601 Return_statement(Expression_list* vals, Location location)
602 : Statement(STATEMENT_RETURN, location),
603 vals_(vals), are_hidden_fields_ok_(false), is_lowered_(false)
606 // The list of values being returned. This may be NULL.
607 const Expression_list*
608 vals() const
609 { return this->vals_; }
611 // Note that it is OK for this return statement to set hidden
612 // fields.
613 void
614 set_hidden_fields_are_ok()
615 { this->are_hidden_fields_ok_ = true; }
617 protected:
619 do_traverse(Traverse* traverse)
620 { return this->traverse_expression_list(traverse, this->vals_); }
622 bool
623 do_traverse_assignments(Traverse_assignments*);
625 Statement*
626 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
628 bool
629 do_may_fall_through() const
630 { return false; }
632 Bstatement*
633 do_get_backend(Translate_context*);
635 void
636 do_dump_statement(Ast_dump_context*) const;
638 private:
639 // Return values. This may be NULL.
640 Expression_list* vals_;
641 // True if this statement may pass hidden fields in the return
642 // value. This is used for generated method stubs.
643 bool are_hidden_fields_ok_;
644 // True if this statement has been lowered.
645 bool is_lowered_;
648 // An expression statement.
650 class Expression_statement : public Statement
652 public:
653 Expression_statement(Expression* expr, bool is_ignored);
655 Expression*
656 expr()
657 { return this->expr_; }
659 protected:
661 do_traverse(Traverse* traverse)
662 { return this->traverse_expression(traverse, &this->expr_); }
664 void
665 do_determine_types();
667 void
668 do_check_types(Gogo*);
670 bool
671 do_may_fall_through() const;
673 Bstatement*
674 do_get_backend(Translate_context* context);
676 void
677 do_dump_statement(Ast_dump_context*) const;
679 private:
680 Expression* expr_;
681 // Whether the value of this expression is being explicitly ignored.
682 bool is_ignored_;
685 // A send statement.
687 class Send_statement : public Statement
689 public:
690 Send_statement(Expression* channel, Expression* val,
691 Location location)
692 : Statement(STATEMENT_SEND, location),
693 channel_(channel), val_(val)
696 protected:
698 do_traverse(Traverse* traverse);
700 void
701 do_determine_types();
703 void
704 do_check_types(Gogo*);
706 Bstatement*
707 do_get_backend(Translate_context*);
709 void
710 do_dump_statement(Ast_dump_context*) const;
712 private:
713 // The channel on which to send the value.
714 Expression* channel_;
715 // The value to send.
716 Expression* val_;
719 // Select_clauses holds the clauses of a select statement. This is
720 // built by the parser.
722 class Select_clauses
724 public:
725 Select_clauses()
726 : clauses_()
729 // Add a new clause. IS_SEND is true if this is a send clause,
730 // false for a receive clause. For a send clause CHANNEL is the
731 // channel and VAL is the value to send. For a receive clause
732 // CHANNEL is the channel, VAL is either NULL or a Var_expression
733 // for the variable to set, and CLOSED is either NULL or a
734 // Var_expression to set to whether the channel is closed. If VAL
735 // is NULL, VAR may be a variable to be initialized with the
736 // received value, and CLOSEDVAR ma be a variable to be initialized
737 // with whether the channel is closed. IS_DEFAULT is true if this
738 // is the default clause. STATEMENTS is the list of statements to
739 // execute.
740 void
741 add(bool is_send, Expression* channel, Expression* val, Expression* closed,
742 Named_object* var, Named_object* closedvar, bool is_default,
743 Block* statements, Location location)
745 int index = static_cast<int>(this->clauses_.size());
746 this->clauses_.push_back(Select_clause(index, is_send, channel, val,
747 closed, var, closedvar, is_default,
748 statements, location));
751 size_t
752 size() const
753 { return this->clauses_.size(); }
755 // Traverse the select clauses.
757 traverse(Traverse*);
759 // Lower statements.
760 void
761 lower(Gogo*, Named_object*, Block*, Temporary_statement*);
763 // Determine types.
764 void
765 determine_types();
767 // Check types.
768 void
769 check_types();
771 // Whether the select clauses may fall through to the statement
772 // which follows the overall select statement.
773 bool
774 may_fall_through() const;
776 // Convert to the backend representation.
777 Bstatement*
778 get_backend(Translate_context*, Temporary_statement* sel,
779 Unnamed_label* break_label, Location);
781 // Dump AST representation.
782 void
783 dump_clauses(Ast_dump_context*) const;
785 private:
786 // A single clause.
787 class Select_clause
789 public:
790 Select_clause()
791 : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
792 closedvar_(NULL), statements_(NULL), is_send_(false),
793 is_default_(false)
796 Select_clause(int index, bool is_send, Expression* channel,
797 Expression* val, Expression* closed, Named_object* var,
798 Named_object* closedvar, bool is_default, Block* statements,
799 Location location)
800 : index_(index), channel_(channel), val_(val), closed_(closed),
801 var_(var), closedvar_(closedvar), statements_(statements),
802 location_(location), is_send_(is_send), is_default_(is_default),
803 is_lowered_(false)
804 { go_assert(is_default ? channel == NULL : channel != NULL); }
806 // Return the index of this clause.
808 index() const
809 { return this->index_; }
811 // Traverse the select clause.
813 traverse(Traverse*);
815 // Lower statements.
816 void
817 lower(Gogo*, Named_object*, Block*, Temporary_statement*);
819 // Determine types.
820 void
821 determine_types();
823 // Check types.
824 void
825 check_types();
827 // Return true if this is the default clause.
828 bool
829 is_default() const
830 { return this->is_default_; }
832 // Return the channel. This will return NULL for the default
833 // clause.
834 Expression*
835 channel() const
836 { return this->channel_; }
838 // Return true for a send, false for a receive.
839 bool
840 is_send() const
842 go_assert(!this->is_default_);
843 return this->is_send_;
846 // Return the statements.
847 const Block*
848 statements() const
849 { return this->statements_; }
851 // Return the location.
852 Location
853 location() const
854 { return this->location_; }
856 // Whether this clause may fall through to the statement which
857 // follows the overall select statement.
858 bool
859 may_fall_through() const;
861 // Convert the statements to the backend representation.
862 Bstatement*
863 get_statements_backend(Translate_context*);
865 // Dump AST representation.
866 void
867 dump_clause(Ast_dump_context*) const;
869 private:
870 void
871 lower_default(Block*, Expression*, Expression*);
873 void
874 lower_send(Block*, Expression*, Expression*, Expression*);
876 void
877 lower_recv(Gogo*, Named_object*, Block*, Expression*, Expression*,
878 Expression*);
880 // The index of this case in the generated switch statement.
881 int index_;
882 // The channel.
883 Expression* channel_;
884 // The value to send or the lvalue to receive into.
885 Expression* val_;
886 // The lvalue to set to whether the channel is closed on a
887 // receive.
888 Expression* closed_;
889 // The variable to initialize, for "case a := <-ch".
890 Named_object* var_;
891 // The variable to initialize to whether the channel is closed,
892 // for "case a, c := <-ch".
893 Named_object* closedvar_;
894 // The statements to execute.
895 Block* statements_;
896 // The location of this clause.
897 Location location_;
898 // Whether this is a send or a receive.
899 bool is_send_;
900 // Whether this is the default.
901 bool is_default_;
902 // Whether this has been lowered.
903 bool is_lowered_;
906 typedef std::vector<Select_clause> Clauses;
908 Clauses clauses_;
911 // A select statement.
913 class Select_statement : public Statement
915 public:
916 Select_statement(Location location)
917 : Statement(STATEMENT_SELECT, location),
918 clauses_(NULL), sel_(NULL), break_label_(NULL), is_lowered_(false)
921 // Add the clauses.
922 void
923 add_clauses(Select_clauses* clauses)
925 go_assert(this->clauses_ == NULL);
926 this->clauses_ = clauses;
929 // Return the break label for this select statement.
930 Unnamed_label*
931 break_label();
933 protected:
935 do_traverse(Traverse* traverse)
936 { return this->clauses_->traverse(traverse); }
938 Statement*
939 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
941 void
942 do_determine_types()
943 { this->clauses_->determine_types(); }
945 void
946 do_check_types(Gogo*)
947 { this->clauses_->check_types(); }
949 bool
950 do_may_fall_through() const;
952 Bstatement*
953 do_get_backend(Translate_context*);
955 void
956 do_dump_statement(Ast_dump_context*) const;
958 private:
959 // The select clauses.
960 Select_clauses* clauses_;
961 // A temporary which holds the select structure we build up at runtime.
962 Temporary_statement* sel_;
963 // The break label.
964 Unnamed_label* break_label_;
965 // Whether this statement has been lowered.
966 bool is_lowered_;
969 // A statement which requires a thunk: go or defer.
971 class Thunk_statement : public Statement
973 public:
974 Thunk_statement(Statement_classification, Call_expression*,
975 Location);
977 // Return the call expression.
978 Expression*
979 call() const
980 { return this->call_; }
982 // Simplify a go or defer statement so that it only uses a single
983 // parameter.
984 bool
985 simplify_statement(Gogo*, Named_object*, Block*);
987 protected:
989 do_traverse(Traverse* traverse);
991 bool
992 do_traverse_assignments(Traverse_assignments*);
994 void
995 do_determine_types();
997 void
998 do_check_types(Gogo*);
1000 // Return the function and argument for the call.
1001 bool
1002 get_fn_and_arg(Expression** pfn, Expression** parg);
1004 private:
1005 // Return whether this is a simple go statement.
1006 bool
1007 is_simple(Function_type*) const;
1009 // Return whether the thunk function is a constant.
1010 bool
1011 is_constant_function() const;
1013 // Build the struct to use for a complex case.
1014 Struct_type*
1015 build_struct(Function_type* fntype);
1017 // Build the thunk.
1018 void
1019 build_thunk(Gogo*, const std::string&);
1021 // Set the name to use for thunk field N.
1022 void
1023 thunk_field_param(int n, char* buf, size_t buflen);
1025 // The function call to be executed in a separate thread (go) or
1026 // later (defer).
1027 Expression* call_;
1028 // The type used for a struct to pass to a thunk, if this is not a
1029 // simple call.
1030 Struct_type* struct_type_;
1033 // A go statement.
1035 class Go_statement : public Thunk_statement
1037 public:
1038 Go_statement(Call_expression* call, Location location)
1039 : Thunk_statement(STATEMENT_GO, call, location)
1042 protected:
1043 Bstatement*
1044 do_get_backend(Translate_context*);
1046 void
1047 do_dump_statement(Ast_dump_context*) const;
1050 // A defer statement.
1052 class Defer_statement : public Thunk_statement
1054 public:
1055 Defer_statement(Call_expression* call, Location location)
1056 : Thunk_statement(STATEMENT_DEFER, call, location)
1059 protected:
1060 Bstatement*
1061 do_get_backend(Translate_context*);
1063 void
1064 do_dump_statement(Ast_dump_context*) const;
1067 // A label statement.
1069 class Label_statement : public Statement
1071 public:
1072 Label_statement(Label* label, Location location)
1073 : Statement(STATEMENT_LABEL, location),
1074 label_(label)
1077 // Return the label itself.
1078 const Label*
1079 label() const
1080 { return this->label_; }
1082 protected:
1084 do_traverse(Traverse*);
1086 Bstatement*
1087 do_get_backend(Translate_context*);
1089 void
1090 do_dump_statement(Ast_dump_context*) const;
1092 private:
1093 // The label.
1094 Label* label_;
1097 // A for statement.
1099 class For_statement : public Statement
1101 public:
1102 For_statement(Block* init, Expression* cond, Block* post,
1103 Location location)
1104 : Statement(STATEMENT_FOR, location),
1105 init_(init), cond_(cond), post_(post), statements_(NULL),
1106 break_label_(NULL), continue_label_(NULL)
1109 // Add the statements.
1110 void
1111 add_statements(Block* statements)
1113 go_assert(this->statements_ == NULL);
1114 this->statements_ = statements;
1117 // Return the break label for this for statement.
1118 Unnamed_label*
1119 break_label();
1121 // Return the continue label for this for statement.
1122 Unnamed_label*
1123 continue_label();
1125 // Set the break and continue labels for this statement.
1126 void
1127 set_break_continue_labels(Unnamed_label* break_label,
1128 Unnamed_label* continue_label);
1130 protected:
1132 do_traverse(Traverse*);
1134 bool
1135 do_traverse_assignments(Traverse_assignments*)
1136 { go_unreachable(); }
1138 Statement*
1139 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1141 bool
1142 do_may_fall_through() const;
1144 Bstatement*
1145 do_get_backend(Translate_context*)
1146 { go_unreachable(); }
1148 void
1149 do_dump_statement(Ast_dump_context*) const;
1151 private:
1152 // The initialization statements. This may be NULL.
1153 Block* init_;
1154 // The condition. This may be NULL.
1155 Expression* cond_;
1156 // The statements to run after each iteration. This may be NULL.
1157 Block* post_;
1158 // The statements in the loop itself.
1159 Block* statements_;
1160 // The break label, if needed.
1161 Unnamed_label* break_label_;
1162 // The continue label, if needed.
1163 Unnamed_label* continue_label_;
1166 // A for statement over a range clause.
1168 class For_range_statement : public Statement
1170 public:
1171 For_range_statement(Expression* index_var, Expression* value_var,
1172 Expression* range, Location location)
1173 : Statement(STATEMENT_FOR_RANGE, location),
1174 index_var_(index_var), value_var_(value_var), range_(range),
1175 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1178 // Add the statements.
1179 void
1180 add_statements(Block* statements)
1182 go_assert(this->statements_ == NULL);
1183 this->statements_ = statements;
1186 // Return the break label for this for statement.
1187 Unnamed_label*
1188 break_label();
1190 // Return the continue label for this for statement.
1191 Unnamed_label*
1192 continue_label();
1194 protected:
1196 do_traverse(Traverse*);
1198 bool
1199 do_traverse_assignments(Traverse_assignments*)
1200 { go_unreachable(); }
1202 Statement*
1203 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1205 Bstatement*
1206 do_get_backend(Translate_context*)
1207 { go_unreachable(); }
1209 void
1210 do_dump_statement(Ast_dump_context*) const;
1212 private:
1213 Expression*
1214 make_range_ref(Named_object*, Temporary_statement*, Location);
1216 Expression*
1217 call_builtin(Gogo*, const char* funcname, Expression* arg, Location);
1219 void
1220 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1221 Temporary_statement*, Temporary_statement*,
1222 Block**, Expression**, Block**, Block**);
1224 void
1225 lower_range_slice(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1226 Temporary_statement*, Temporary_statement*,
1227 Block**, Expression**, Block**, Block**);
1229 void
1230 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1231 Temporary_statement*, Temporary_statement*,
1232 Block**, Expression**, Block**, Block**);
1234 void
1235 lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1236 Temporary_statement*, Temporary_statement*,
1237 Block**, Expression**, Block**, Block**);
1239 void
1240 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1241 Temporary_statement*, Temporary_statement*,
1242 Temporary_statement*, Block**, Expression**, Block**,
1243 Block**);
1245 // The variable which is set to the index value.
1246 Expression* index_var_;
1247 // The variable which is set to the element value. This may be
1248 // NULL.
1249 Expression* value_var_;
1250 // The expression we are ranging over.
1251 Expression* range_;
1252 // The statements in the block.
1253 Block* statements_;
1254 // The break label, if needed.
1255 Unnamed_label* break_label_;
1256 // The continue label, if needed.
1257 Unnamed_label* continue_label_;
1260 // Class Case_clauses holds the clauses of a switch statement. This
1261 // is built by the parser.
1263 class Case_clauses
1265 public:
1266 Case_clauses()
1267 : clauses_()
1270 // Add a new clause. CASES is a list of case expressions; it may be
1271 // NULL. IS_DEFAULT is true if this is the default case.
1272 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1273 // after the statements the case clause should fall through to the
1274 // next clause.
1275 void
1276 add(Expression_list* cases, bool is_default, Block* statements,
1277 bool is_fallthrough, Location location)
1279 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1280 is_fallthrough, location));
1283 // Return whether there are no clauses.
1284 bool
1285 empty() const
1286 { return this->clauses_.empty(); }
1288 // Traverse the case clauses.
1290 traverse(Traverse*);
1292 // Lower for a nonconstant switch.
1293 void
1294 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1296 // Determine types of expressions. The Type parameter is the type
1297 // of the switch value.
1298 void
1299 determine_types(Type*);
1301 // Check types. The Type parameter is the type of the switch value.
1302 bool
1303 check_types(Type*);
1305 // Return true if all the clauses are constant values.
1306 bool
1307 is_constant() const;
1309 // Return true if these clauses may fall through to the statements
1310 // following the switch statement.
1311 bool
1312 may_fall_through() const;
1314 // Return the body of a SWITCH_EXPR when all the clauses are
1315 // constants.
1316 void
1317 get_backend(Translate_context*, Unnamed_label* break_label,
1318 std::vector<std::vector<Bexpression*> >* all_cases,
1319 std::vector<Bstatement*>* all_statements) const;
1321 // Dump the AST representation to a dump context.
1322 void
1323 dump_clauses(Ast_dump_context*) const;
1325 private:
1326 // For a constant switch we need to keep a record of constants we
1327 // have already seen.
1328 class Hash_integer_value;
1329 class Eq_integer_value;
1330 typedef Unordered_set_hash(Expression*, Hash_integer_value,
1331 Eq_integer_value) Case_constants;
1333 // One case clause.
1334 class Case_clause
1336 public:
1337 Case_clause()
1338 : cases_(NULL), statements_(NULL), is_default_(false),
1339 is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1342 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1343 bool is_fallthrough, Location location)
1344 : cases_(cases), statements_(statements), is_default_(is_default),
1345 is_fallthrough_(is_fallthrough), location_(location)
1348 // Whether this clause falls through to the next clause.
1349 bool
1350 is_fallthrough() const
1351 { return this->is_fallthrough_; }
1353 // Whether this is the default.
1354 bool
1355 is_default() const
1356 { return this->is_default_; }
1358 // The location of this clause.
1359 Location
1360 location() const
1361 { return this->location_; }
1363 // Traversal.
1365 traverse(Traverse*);
1367 // Lower for a nonconstant switch.
1368 void
1369 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1371 // Determine types.
1372 void
1373 determine_types(Type*);
1375 // Check types.
1376 bool
1377 check_types(Type*);
1379 // Return true if all the case expressions are constant.
1380 bool
1381 is_constant() const;
1383 // Return true if this clause may fall through to execute the
1384 // statements following the switch statement. This is not the
1385 // same as whether this clause falls through to the next clause.
1386 bool
1387 may_fall_through() const;
1389 // Convert the case values and statements to the backend
1390 // representation.
1391 Bstatement*
1392 get_backend(Translate_context*, Unnamed_label* break_label,
1393 Case_constants*, std::vector<Bexpression*>* cases) const;
1395 // Dump the AST representation to a dump context.
1396 void
1397 dump_clause(Ast_dump_context*) const;
1399 private:
1400 // The list of case expressions.
1401 Expression_list* cases_;
1402 // The statements to execute.
1403 Block* statements_;
1404 // Whether this is the default case.
1405 bool is_default_;
1406 // Whether this falls through after the statements.
1407 bool is_fallthrough_;
1408 // The location of this case clause.
1409 Location location_;
1412 friend class Case_clause;
1414 // The type of the list of clauses.
1415 typedef std::vector<Case_clause> Clauses;
1417 // All the case clauses.
1418 Clauses clauses_;
1421 // A switch statement.
1423 class Switch_statement : public Statement
1425 public:
1426 Switch_statement(Expression* val, Location location)
1427 : Statement(STATEMENT_SWITCH, location),
1428 val_(val), clauses_(NULL), break_label_(NULL)
1431 // Add the clauses.
1432 void
1433 add_clauses(Case_clauses* clauses)
1435 go_assert(this->clauses_ == NULL);
1436 this->clauses_ = clauses;
1439 // Return the break label for this switch statement.
1440 Unnamed_label*
1441 break_label();
1443 protected:
1445 do_traverse(Traverse*);
1447 Statement*
1448 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1450 Bstatement*
1451 do_get_backend(Translate_context*)
1452 { go_unreachable(); }
1454 void
1455 do_dump_statement(Ast_dump_context*) const;
1457 bool
1458 do_may_fall_through() const;
1460 private:
1461 // The value to switch on. This may be NULL.
1462 Expression* val_;
1463 // The case clauses.
1464 Case_clauses* clauses_;
1465 // The break label, if needed.
1466 Unnamed_label* break_label_;
1469 // Class Type_case_clauses holds the clauses of a type switch
1470 // statement. This is built by the parser.
1472 class Type_case_clauses
1474 public:
1475 Type_case_clauses()
1476 : clauses_()
1479 // Add a new clause. TYPE is the type for this clause; it may be
1480 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1481 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1482 // if this is the default case. STATEMENTS is a block of
1483 // statements; it may be NULL.
1484 void
1485 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1486 Location location)
1488 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1489 statements, location));
1492 // Return whether there are no clauses.
1493 bool
1494 empty() const
1495 { return this->clauses_.empty(); }
1497 // Traverse the type case clauses.
1499 traverse(Traverse*);
1501 // Check for duplicates.
1502 void
1503 check_duplicates() const;
1505 // Lower to if and goto statements.
1506 void
1507 lower(Type*, Block*, Temporary_statement* descriptor_temp,
1508 Unnamed_label* break_label) const;
1510 // Return true if these clauses may fall through to the statements
1511 // following the switch statement.
1512 bool
1513 may_fall_through() const;
1515 // Dump the AST representation to a dump context.
1516 void
1517 dump_clauses(Ast_dump_context*) const;
1519 private:
1520 // One type case clause.
1521 class Type_case_clause
1523 public:
1524 Type_case_clause()
1525 : type_(NULL), statements_(NULL), is_default_(false),
1526 location_(UNKNOWN_LOCATION)
1529 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1530 Block* statements, Location location)
1531 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1532 is_default_(is_default), location_(location)
1535 // The type.
1536 Type*
1537 type() const
1538 { return this->type_; }
1540 // Whether this is the default.
1541 bool
1542 is_default() const
1543 { return this->is_default_; }
1545 // The location of this type clause.
1546 Location
1547 location() const
1548 { return this->location_; }
1550 // Traversal.
1552 traverse(Traverse*);
1554 // Lower to if and goto statements.
1555 void
1556 lower(Type*, Block*, Temporary_statement* descriptor_temp,
1557 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1559 // Return true if this clause may fall through to execute the
1560 // statements following the switch statement. This is not the
1561 // same as whether this clause falls through to the next clause.
1562 bool
1563 may_fall_through() const;
1565 // Dump the AST representation to a dump context.
1566 void
1567 dump_clause(Ast_dump_context*) const;
1569 private:
1570 // The type for this type clause.
1571 Type* type_;
1572 // The statements to execute.
1573 Block* statements_;
1574 // Whether this falls through--this is true for "case T1, T2".
1575 bool is_fallthrough_;
1576 // Whether this is the default case.
1577 bool is_default_;
1578 // The location of this type case clause.
1579 Location location_;
1582 friend class Type_case_clause;
1584 // The type of the list of type clauses.
1585 typedef std::vector<Type_case_clause> Type_clauses;
1587 // All the type case clauses.
1588 Type_clauses clauses_;
1591 // A type switch statement.
1593 class Type_switch_statement : public Statement
1595 public:
1596 Type_switch_statement(Named_object* var, Expression* expr,
1597 Location location)
1598 : Statement(STATEMENT_TYPE_SWITCH, location),
1599 var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1600 { go_assert(var == NULL || expr == NULL); }
1602 // Add the clauses.
1603 void
1604 add_clauses(Type_case_clauses* clauses)
1606 go_assert(this->clauses_ == NULL);
1607 this->clauses_ = clauses;
1610 // Return the break label for this type switch statement.
1611 Unnamed_label*
1612 break_label();
1614 protected:
1616 do_traverse(Traverse*);
1618 Statement*
1619 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1621 Bstatement*
1622 do_get_backend(Translate_context*)
1623 { go_unreachable(); }
1625 void
1626 do_dump_statement(Ast_dump_context*) const;
1628 bool
1629 do_may_fall_through() const;
1631 private:
1632 // The variable holding the value we are switching on.
1633 Named_object* var_;
1634 // The expression we are switching on if there is no variable.
1635 Expression* expr_;
1636 // The type case clauses.
1637 Type_case_clauses* clauses_;
1638 // The break label, if needed.
1639 Unnamed_label* break_label_;
1642 #endif // !defined(GO_STATEMENTS_H)