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
17 class Temporary_statement;
18 class Variable_declaration_statement;
19 class Return_statement;
20 class Thunk_statement;
21 class Label_statement;
23 class For_range_statement;
24 class Switch_statement;
25 class Type_switch_statement;
27 class Select_statement;
31 class Translate_context;
33 class Expression_list;
35 class Call_expression;
36 class Map_index_expression;
37 class Receive_expression;
39 class Type_case_clauses;
41 class Typed_identifier_list;
46 // This class is used to traverse assignments made by a statement
47 // which makes assignments.
49 class Traverse_assignments
52 Traverse_assignments()
55 virtual ~Traverse_assignments()
58 // This is called for a variable initialization.
60 initialize_variable(Named_object*) = 0;
62 // This is called for each assignment made by the statement. PLHS
63 // points to the left hand side, and PRHS points to the right hand
64 // side. PRHS may be NULL if there is no associated expression, as
65 // in the bool set by a non-blocking receive.
67 assignment(Expression** plhs, Expression** prhs) = 0;
69 // This is called for each expression which is not passed to the
70 // assignment function. This is used for some of the statements
71 // which assign two values, for which there is no expression which
72 // describes the value. For ++ and -- the value is passed to both
73 // the assignment method and the rhs method. IS_STORED is true if
74 // this value is being stored directly. It is false if the value is
75 // computed but not stored. IS_LOCAL is true if the value is being
76 // stored in a local variable or this is being called by a return
79 value(Expression**, bool is_stored, bool is_local) = 0;
82 // A single statement.
87 // The types of statements.
88 enum Statement_classification
91 STATEMENT_VARIABLE_DECLARATION,
99 STATEMENT_BREAK_OR_CONTINUE,
101 STATEMENT_GOTO_UNNAMED,
103 STATEMENT_UNNAMED_LABEL,
105 STATEMENT_CONSTANT_SWITCH,
109 // These statements types are created by the parser, but they
110 // disappear during the lowering pass.
111 STATEMENT_ASSIGNMENT_OPERATION,
112 STATEMENT_TUPLE_ASSIGNMENT,
113 STATEMENT_TUPLE_MAP_ASSIGNMENT,
114 STATEMENT_MAP_ASSIGNMENT,
115 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT,
116 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT,
121 STATEMENT_TYPE_SWITCH
124 Statement(Statement_classification, source_location);
126 virtual ~Statement();
128 // Make a variable declaration.
130 make_variable_declaration(Named_object*);
132 // Make a statement which creates a temporary variable and
133 // initializes it to an expression. The block is used if the
134 // temporary variable has to be explicitly destroyed; the variable
135 // must still be added to the block. References to the temporary
136 // variable may be constructed using make_temporary_reference.
137 // Either the type or the initialization expression may be NULL, but
139 static Temporary_statement*
140 make_temporary(Type*, Expression*, source_location);
142 // Make an assignment statement.
144 make_assignment(Expression*, Expression*, source_location);
146 // Make an assignment operation (+=, etc.).
148 make_assignment_operation(Operator, Expression*, Expression*,
151 // Make a tuple assignment statement.
153 make_tuple_assignment(Expression_list*, Expression_list*, source_location);
155 // Make an assignment from a map index to a pair of variables.
157 make_tuple_map_assignment(Expression* val, Expression* present,
158 Expression*, source_location);
160 // Make a statement which assigns a pair of values to a map.
162 make_map_assignment(Expression*, Expression* val,
163 Expression* should_set, source_location);
165 // Make an assignment from a nonblocking receive to a pair of
166 // variables. FOR_SELECT is true is this is being created for a
167 // case x, ok := <-c in a select statement.
169 make_tuple_receive_assignment(Expression* val, Expression* closed,
170 Expression* channel, bool for_select,
173 // Make an assignment from a type guard to a pair of variables.
175 make_tuple_type_guard_assignment(Expression* val, Expression* ok,
176 Expression* expr, Type* type,
179 // Make an expression statement from an Expression.
181 make_statement(Expression*);
183 // Make a block statement from a Block. This is an embedded list of
184 // statements which may also include variable definitions.
186 make_block_statement(Block*, source_location);
188 // Make an increment statement.
190 make_inc_statement(Expression*);
192 // Make a decrement statement.
194 make_dec_statement(Expression*);
196 // Make a go statement.
198 make_go_statement(Call_expression* call, source_location);
200 // Make a defer statement.
202 make_defer_statement(Call_expression* call, source_location);
204 // Make a return statement.
206 make_return_statement(Expression_list*, source_location);
208 // Make a break statement.
210 make_break_statement(Unnamed_label* label, source_location);
212 // Make a continue statement.
214 make_continue_statement(Unnamed_label* label, source_location);
216 // Make a goto statement.
218 make_goto_statement(Label* label, source_location);
220 // Make a goto statement to an unnamed label.
222 make_goto_unnamed_statement(Unnamed_label* label, source_location);
224 // Make a label statement--where the label is defined.
226 make_label_statement(Label* label, source_location);
228 // Make an unnamed label statement--where the label is defined.
230 make_unnamed_label_statement(Unnamed_label* label);
232 // Make an if statement.
234 make_if_statement(Expression* cond, Block* then_block, Block* else_block,
237 // Make a switch statement.
238 static Switch_statement*
239 make_switch_statement(Expression* switch_val, source_location);
241 // Make a type switch statement.
242 static Type_switch_statement*
243 make_type_switch_statement(Named_object* var, Expression*, source_location);
245 // Make a send statement.
246 static Send_statement*
247 make_send_statement(Expression* channel, Expression* val, source_location);
249 // Make a select statement.
250 static Select_statement*
251 make_select_statement(source_location);
253 // Make a for statement.
254 static For_statement*
255 make_for_statement(Block* init, Expression* cond, Block* post,
256 source_location location);
258 // Make a for statement with a range clause.
259 static For_range_statement*
260 make_for_range_statement(Expression* index_var, Expression* value_var,
261 Expression* range, source_location);
263 // Return the statement classification.
264 Statement_classification
265 classification() const
266 { return this->classification_; }
268 // Get the statement location.
271 { return this->location_; }
273 // Traverse the tree.
275 traverse(Block*, size_t* index, Traverse*);
277 // Traverse the contents of this statement--the expressions and
278 // statements which it contains.
280 traverse_contents(Traverse*);
282 // If this statement assigns some values, it calls a function for
283 // each value to which this statement assigns a value, and returns
284 // true. If this statement does not assign any values, it returns
287 traverse_assignments(Traverse_assignments* tassign);
289 // Lower a statement. This is called immediately after parsing to
290 // simplify statements for further processing. It returns the same
291 // Statement or a new one. FUNCTION is the function containing this
292 // statement. BLOCK is the block containing this statement.
294 lower(Gogo* gogo, Named_object* function, Block* block)
295 { return this->do_lower(gogo, function, block); }
297 // Set type information for unnamed constants.
301 // Check types in a statement. This simply checks that any
302 // expressions used by the statement have the right type.
304 check_types(Gogo* gogo)
305 { this->do_check_types(gogo); }
307 // Return whether this is a block statement.
309 is_block_statement() const
310 { return this->classification_ == STATEMENT_BLOCK; }
312 // If this is a variable declaration statement, return it.
313 // Otherwise return NULL.
314 Variable_declaration_statement*
315 variable_declaration_statement()
317 return this->convert<Variable_declaration_statement,
318 STATEMENT_VARIABLE_DECLARATION>();
321 // If this is a return statement, return it. Otherwise return NULL.
324 { return this->convert<Return_statement, STATEMENT_RETURN>(); }
326 // If this is a thunk statement (a go or defer statement), return
327 // it. Otherwise return NULL.
331 // If this is a label statement, return it. Otherwise return NULL.
334 { return this->convert<Label_statement, STATEMENT_LABEL>(); }
336 // If this is a for statement, return it. Otherwise return NULL.
339 { return this->convert<For_statement, STATEMENT_FOR>(); }
341 // If this is a for statement over a range clause, return it.
342 // Otherwise return NULL.
344 for_range_statement()
345 { return this->convert<For_range_statement, STATEMENT_FOR_RANGE>(); }
347 // If this is a switch statement, return it. Otherwise return NULL.
350 { return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
352 // If this is a type switch statement, return it. Otherwise return
354 Type_switch_statement*
355 type_switch_statement()
356 { return this->convert<Type_switch_statement, STATEMENT_TYPE_SWITCH>(); }
358 // If this is a select statement, return it. Otherwise return NULL.
361 { return this->convert<Select_statement, STATEMENT_SELECT>(); }
363 // Return true if this statement may fall through--if after
364 // executing this statement we may go on to execute the following
365 // statement, if any.
367 may_fall_through() const
368 { return this->do_may_fall_through(); }
370 // Convert the statement to the backend representation.
372 get_backend(Translate_context*);
375 // Implemented by child class: traverse the tree.
377 do_traverse(Traverse*) = 0;
379 // Implemented by child class: traverse assignments. Any statement
380 // which includes an assignment should implement this.
382 do_traverse_assignments(Traverse_assignments*)
385 // Implemented by the child class: lower this statement to a simpler
388 do_lower(Gogo*, Named_object*, Block*)
391 // Implemented by child class: set type information for unnamed
392 // constants. Any statement which includes an expression needs to
398 // Implemented by child class: check types of expressions used in a
401 do_check_types(Gogo*)
404 // Implemented by child class: return true if this statement may
407 do_may_fall_through() const
410 // Implemented by child class: convert to backend representation.
412 do_get_backend(Translate_context*) = 0;
414 // Traverse an expression in a statement.
416 traverse_expression(Traverse*, Expression**);
418 // Traverse an expression list in a statement. The Expression_list
421 traverse_expression_list(Traverse*, Expression_list*);
423 // Traverse a type in a statement.
425 traverse_type(Traverse*, Type*);
427 // Build a tree node with one operand, setting the location. The
428 // first operand really has type "enum tree_code", but that enum is
431 build_stmt_1(int tree_code_value, tree);
433 // For children to call when they detect that they are in error.
437 // For children to call to report an error conveniently.
439 report_error(const char*);
441 // For children to return an error statement from lower().
443 make_error_statement(source_location);
446 // Convert to the desired statement classification, or return NULL.
447 // This is a controlled dynamic cast.
448 template<typename Statement_class, Statement_classification sc>
452 return (this->classification_ == sc
453 ? static_cast<Statement_class*>(this)
457 template<typename Statement_class, Statement_classification sc>
458 const Statement_class*
461 return (this->classification_ == sc
462 ? static_cast<const Statement_class*>(this)
466 // The statement classification.
467 Statement_classification classification_;
468 // The location in the input file of the start of this statement.
469 source_location location_;
472 // A statement which creates and initializes a temporary variable.
474 class Temporary_statement : public Statement
477 Temporary_statement(Type* type, Expression* init, source_location location)
478 : Statement(STATEMENT_TEMPORARY, location),
479 type_(type), init_(init), bvariable_(NULL), is_address_taken_(false)
482 // Return the type of the temporary variable.
486 // Record that something takes the address of this temporary
489 set_is_address_taken()
490 { this->is_address_taken_ = true; }
492 // Return the temporary variable. This should not be called until
493 // after the statement itself has been converted.
495 get_backend_variable(Translate_context*) const;
499 do_traverse(Traverse*);
502 do_traverse_assignments(Traverse_assignments*);
505 do_determine_types();
508 do_check_types(Gogo*);
511 do_get_backend(Translate_context*);
514 // The type of the temporary variable.
516 // The initial value of the temporary variable. This may be NULL.
518 // The backend representation of the temporary variable.
519 Bvariable* bvariable_;
520 // True if something takes the address of this temporary variable.
521 bool is_address_taken_;
524 // A variable declaration. This marks the point in the code where a
525 // variable is declared. The Variable is also attached to a Block.
527 class Variable_declaration_statement : public Statement
530 Variable_declaration_statement(Named_object* var);
532 // The variable being declared.
535 { return this->var_; }
539 do_traverse(Traverse*);
542 do_traverse_assignments(Traverse_assignments*);
545 do_get_backend(Translate_context*);
551 // A return statement.
553 class Return_statement : public Statement
556 Return_statement(Expression_list* vals, source_location location)
557 : Statement(STATEMENT_RETURN, location),
558 vals_(vals), is_lowered_(false)
561 // The list of values being returned. This may be NULL.
562 const Expression_list*
564 { return this->vals_; }
568 do_traverse(Traverse* traverse)
569 { return this->traverse_expression_list(traverse, this->vals_); }
572 do_traverse_assignments(Traverse_assignments*);
575 do_lower(Gogo*, Named_object*, Block*);
578 do_may_fall_through() const
582 do_get_backend(Translate_context*);
585 // Return values. This may be NULL.
586 Expression_list* vals_;
587 // True if this statement has been lowered.
593 class Send_statement : public Statement
596 Send_statement(Expression* channel, Expression* val,
597 source_location location)
598 : Statement(STATEMENT_SEND, location),
599 channel_(channel), val_(val), for_select_(false)
602 // Note that this is for a select statement.
605 { this->for_select_ = true; }
609 do_traverse(Traverse* traverse);
612 do_determine_types();
615 do_check_types(Gogo*);
618 do_get_backend(Translate_context*);
621 // The channel on which to send the value.
622 Expression* channel_;
623 // The value to send.
625 // Whether this is for a select statement.
629 // Select_clauses holds the clauses of a select statement. This is
630 // built by the parser.
639 // Add a new clause. IS_SEND is true if this is a send clause,
640 // false for a receive clause. For a send clause CHANNEL is the
641 // channel and VAL is the value to send. For a receive clause
642 // CHANNEL is the channel, VAL is either NULL or a Var_expression
643 // for the variable to set, and CLOSED is either NULL or a
644 // Var_expression to set to whether the channel is closed. If VAL
645 // is NULL, VAR may be a variable to be initialized with the
646 // received value, and CLOSEDVAR ma be a variable to be initialized
647 // with whether the channel is closed. IS_DEFAULT is true if this
648 // is the default clause. STATEMENTS is the list of statements to
651 add(bool is_send, Expression* channel, Expression* val, Expression* closed,
652 Named_object* var, Named_object* closedvar, bool is_default,
653 Block* statements, source_location location)
655 this->clauses_.push_back(Select_clause(is_send, channel, val, closed, var,
656 closedvar, is_default, statements,
660 // Traverse the select clauses.
666 lower(Gogo*, Named_object*, Block*);
672 // Whether the select clauses may fall through to the statement
673 // which follows the overall select statement.
675 may_fall_through() const;
677 // Convert to the backend representation.
679 get_backend(Translate_context*, Unnamed_label* break_label, source_location);
687 : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
688 closedvar_(NULL), statements_(NULL), is_send_(false),
692 Select_clause(bool is_send, Expression* channel, Expression* val,
693 Expression* closed, Named_object* var,
694 Named_object* closedvar, bool is_default, Block* statements,
695 source_location location)
696 : channel_(channel), val_(val), closed_(closed), var_(var),
697 closedvar_(closedvar), statements_(statements), location_(location),
698 is_send_(is_send), is_default_(is_default), is_lowered_(false)
699 { gcc_assert(is_default ? channel == NULL : channel != NULL); }
701 // Traverse the select clause.
707 lower(Gogo*, Named_object*, Block*);
713 // Return true if this is the default clause.
716 { return this->is_default_; }
718 // Return the channel. This will return NULL for the default
722 { return this->channel_; }
724 // Return true for a send, false for a receive.
728 gcc_assert(!this->is_default_);
729 return this->is_send_;
732 // Return the statements.
735 { return this->statements_; }
737 // Return the location.
740 { return this->location_; }
742 // Whether this clause may fall through to the statement which
743 // follows the overall select statement.
745 may_fall_through() const;
747 // Return a tree for the statements to execute.
749 get_statements_backend(Translate_context*);
753 Expression* channel_;
754 // The value to send or the lvalue to receive into.
756 // The lvalue to set to whether the channel is closed on a
759 // The variable to initialize, for "case a := <-ch".
761 // The variable to initialize to whether the channel is closed,
762 // for "case a, c := <-ch".
763 Named_object* closedvar_;
764 // The statements to execute.
766 // The location of this clause.
767 source_location location_;
768 // Whether this is a send or a receive.
770 // Whether this is the default.
772 // Whether this has been lowered.
777 add_clause_backend(Translate_context*, source_location, int index,
778 int case_value, Select_clause*, Unnamed_label*,
779 std::vector<std::vector<Bexpression*> >* cases,
780 std::vector<Bstatement*>* clauses);
782 typedef std::vector<Select_clause> Clauses;
787 // A select statement.
789 class Select_statement : public Statement
792 Select_statement(source_location location)
793 : Statement(STATEMENT_SELECT, location),
794 clauses_(NULL), break_label_(NULL), is_lowered_(false)
799 add_clauses(Select_clauses* clauses)
801 gcc_assert(this->clauses_ == NULL);
802 this->clauses_ = clauses;
805 // Return the break label for this select statement.
811 do_traverse(Traverse* traverse)
812 { return this->clauses_->traverse(traverse); }
815 do_lower(Gogo*, Named_object*, Block*);
819 { this->clauses_->determine_types(); }
822 do_may_fall_through() const
823 { return this->clauses_->may_fall_through(); }
826 do_get_backend(Translate_context*);
829 // The select clauses.
830 Select_clauses* clauses_;
832 Unnamed_label* break_label_;
833 // Whether this statement has been lowered.
837 // A statement which requires a thunk: go or defer.
839 class Thunk_statement : public Statement
842 Thunk_statement(Statement_classification, Call_expression*,
845 // Return the call expression.
848 { return this->call_; }
850 // Simplify a go or defer statement so that it only uses a single
853 simplify_statement(Gogo*, Named_object*, Block*);
857 do_traverse(Traverse* traverse);
860 do_traverse_assignments(Traverse_assignments*);
863 do_determine_types();
866 do_check_types(Gogo*);
868 // Return the function and argument trees for the call.
870 get_fn_and_arg(Expression** pfn, Expression** parg);
873 // Return whether this is a simple go statement.
875 is_simple(Function_type*) const;
877 // Build the struct to use for a complex case.
879 build_struct(Function_type* fntype);
883 build_thunk(Gogo*, const std::string&, Function_type* fntype);
885 // The field name used in the thunk structure for the function
887 static const char* const thunk_field_fn;
889 // The field name used in the thunk structure for the receiver, if
891 static const char* const thunk_field_receiver;
893 // Set the name to use for thunk field N.
895 thunk_field_param(int n, char* buf, size_t buflen);
897 // The function call to be executed in a separate thread (go) or
900 // The type used for a struct to pass to a thunk, if this is not a
902 Struct_type* struct_type_;
907 class Go_statement : public Thunk_statement
910 Go_statement(Call_expression* call, source_location location)
911 : Thunk_statement(STATEMENT_GO, call, location)
916 do_get_backend(Translate_context*);
919 // A defer statement.
921 class Defer_statement : public Thunk_statement
924 Defer_statement(Call_expression* call, source_location location)
925 : Thunk_statement(STATEMENT_DEFER, call, location)
930 do_get_backend(Translate_context*);
933 // A label statement.
935 class Label_statement : public Statement
938 Label_statement(Label* label, source_location location)
939 : Statement(STATEMENT_LABEL, location),
943 // Return the label itself.
946 { return this->label_; }
950 do_traverse(Traverse*);
953 do_get_backend(Translate_context*);
962 class For_statement : public Statement
965 For_statement(Block* init, Expression* cond, Block* post,
966 source_location location)
967 : Statement(STATEMENT_FOR, location),
968 init_(init), cond_(cond), post_(post), statements_(NULL),
969 break_label_(NULL), continue_label_(NULL)
972 // Add the statements.
974 add_statements(Block* statements)
976 gcc_assert(this->statements_ == NULL);
977 this->statements_ = statements;
980 // Return the break label for this for statement.
984 // Return the continue label for this for statement.
988 // Set the break and continue labels for this statement.
990 set_break_continue_labels(Unnamed_label* break_label,
991 Unnamed_label* continue_label);
995 do_traverse(Traverse*);
998 do_traverse_assignments(Traverse_assignments*)
999 { gcc_unreachable(); }
1002 do_lower(Gogo*, Named_object*, Block*);
1005 do_get_backend(Translate_context*)
1006 { gcc_unreachable(); }
1009 // The initialization statements. This may be NULL.
1011 // The condition. This may be NULL.
1013 // The statements to run after each iteration. This may be NULL.
1015 // The statements in the loop itself.
1017 // The break label, if needed.
1018 Unnamed_label* break_label_;
1019 // The continue label, if needed.
1020 Unnamed_label* continue_label_;
1023 // A for statement over a range clause.
1025 class For_range_statement : public Statement
1028 For_range_statement(Expression* index_var, Expression* value_var,
1029 Expression* range, source_location location)
1030 : Statement(STATEMENT_FOR_RANGE, location),
1031 index_var_(index_var), value_var_(value_var), range_(range),
1032 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1035 // Add the statements.
1037 add_statements(Block* statements)
1039 gcc_assert(this->statements_ == NULL);
1040 this->statements_ = statements;
1043 // Return the break label for this for statement.
1047 // Return the continue label for this for statement.
1053 do_traverse(Traverse*);
1056 do_traverse_assignments(Traverse_assignments*)
1057 { gcc_unreachable(); }
1060 do_lower(Gogo*, Named_object*, Block*);
1063 do_get_backend(Translate_context*)
1064 { gcc_unreachable(); }
1068 make_range_ref(Named_object*, Temporary_statement*, source_location);
1071 call_builtin(Gogo*, const char* funcname, Expression* arg, source_location);
1074 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1075 Temporary_statement*, Temporary_statement*,
1076 Block**, Expression**, Block**, Block**);
1079 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1080 Temporary_statement*, Temporary_statement*,
1081 Block**, Expression**, Block**, Block**);
1084 lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1085 Temporary_statement*, Temporary_statement*,
1086 Block**, Expression**, Block**, Block**);
1089 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1090 Temporary_statement*, Temporary_statement*,
1091 Temporary_statement*, Block**, Expression**, Block**,
1094 // The variable which is set to the index value.
1095 Expression* index_var_;
1096 // The variable which is set to the element value. This may be
1098 Expression* value_var_;
1099 // The expression we are ranging over.
1101 // The statements in the block.
1103 // The break label, if needed.
1104 Unnamed_label* break_label_;
1105 // The continue label, if needed.
1106 Unnamed_label* continue_label_;
1109 // Class Case_clauses holds the clauses of a switch statement. This
1110 // is built by the parser.
1119 // Add a new clause. CASES is a list of case expressions; it may be
1120 // NULL. IS_DEFAULT is true if this is the default case.
1121 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1122 // after the statements the case clause should fall through to the
1125 add(Expression_list* cases, bool is_default, Block* statements,
1126 bool is_fallthrough, source_location location)
1128 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1129 is_fallthrough, location));
1132 // Return whether there are no clauses.
1135 { return this->clauses_.empty(); }
1137 // Traverse the case clauses.
1139 traverse(Traverse*);
1141 // Lower for a nonconstant switch.
1143 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1145 // Determine types of expressions. The Type parameter is the type
1146 // of the switch value.
1148 determine_types(Type*);
1150 // Check types. The Type parameter is the type of the switch value.
1154 // Return true if all the clauses are constant values.
1156 is_constant() const;
1158 // Return true if these clauses may fall through to the statements
1159 // following the switch statement.
1161 may_fall_through() const;
1163 // Return the body of a SWITCH_EXPR when all the clauses are
1166 get_backend(Translate_context*, Unnamed_label* break_label,
1167 std::vector<std::vector<Bexpression*> >* all_cases,
1168 std::vector<Bstatement*>* all_statements) const;
1171 // For a constant tree we need to keep a record of constants we have
1172 // already seen. Note that INTEGER_CST trees are interned.
1173 class Hash_integer_value;
1174 class Eq_integer_value;
1175 typedef Unordered_set_hash(Expression*, Hash_integer_value,
1176 Eq_integer_value) Case_constants;
1183 : cases_(NULL), statements_(NULL), is_default_(false),
1184 is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1187 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1188 bool is_fallthrough, source_location location)
1189 : cases_(cases), statements_(statements), is_default_(is_default),
1190 is_fallthrough_(is_fallthrough), location_(location)
1193 // Whether this clause falls through to the next clause.
1195 is_fallthrough() const
1196 { return this->is_fallthrough_; }
1198 // Whether this is the default.
1201 { return this->is_default_; }
1203 // The location of this clause.
1206 { return this->location_; }
1210 traverse(Traverse*);
1212 // Lower for a nonconstant switch.
1214 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1218 determine_types(Type*);
1224 // Return true if all the case expressions are constant.
1226 is_constant() const;
1228 // Return true if this clause may fall through to execute the
1229 // statements following the switch statement. This is not the
1230 // same as whether this clause falls through to the next clause.
1232 may_fall_through() const;
1234 // Convert the case values and statements to the backend
1237 get_backend(Translate_context*, Unnamed_label* break_label,
1238 Case_constants*, std::vector<Bexpression*>* cases) const;
1241 // The list of case expressions.
1242 Expression_list* cases_;
1243 // The statements to execute.
1245 // Whether this is the default case.
1247 // Whether this falls through after the statements.
1248 bool is_fallthrough_;
1249 // The location of this case clause.
1250 source_location location_;
1253 friend class Case_clause;
1255 // The type of the list of clauses.
1256 typedef std::vector<Case_clause> Clauses;
1258 // All the case clauses.
1262 // A switch statement.
1264 class Switch_statement : public Statement
1267 Switch_statement(Expression* val, source_location location)
1268 : Statement(STATEMENT_SWITCH, location),
1269 val_(val), clauses_(NULL), break_label_(NULL)
1274 add_clauses(Case_clauses* clauses)
1276 gcc_assert(this->clauses_ == NULL);
1277 this->clauses_ = clauses;
1280 // Return the break label for this switch statement.
1286 do_traverse(Traverse*);
1289 do_lower(Gogo*, Named_object*, Block*);
1292 do_get_backend(Translate_context*)
1293 { gcc_unreachable(); }
1296 // The value to switch on. This may be NULL.
1298 // The case clauses.
1299 Case_clauses* clauses_;
1300 // The break label, if needed.
1301 Unnamed_label* break_label_;
1304 // Class Type_case_clauses holds the clauses of a type switch
1305 // statement. This is built by the parser.
1307 class Type_case_clauses
1314 // Add a new clause. TYPE is the type for this clause; it may be
1315 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1316 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1317 // if this is the default case. STATEMENTS is a block of
1318 // statements; it may be NULL.
1320 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1321 source_location location)
1323 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1324 statements, location));
1327 // Return whether there are no clauses.
1330 { return this->clauses_.empty(); }
1332 // Traverse the type case clauses.
1334 traverse(Traverse*);
1336 // Check for duplicates.
1338 check_duplicates() const;
1340 // Lower to if and goto statements.
1342 lower(Block*, Temporary_statement* descriptor_temp,
1343 Unnamed_label* break_label) const;
1346 // One type case clause.
1347 class Type_case_clause
1351 : type_(NULL), statements_(NULL), is_default_(false),
1352 location_(UNKNOWN_LOCATION)
1355 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1356 Block* statements, source_location location)
1357 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1358 is_default_(is_default), location_(location)
1364 { return this->type_; }
1366 // Whether this is the default.
1369 { return this->is_default_; }
1371 // The location of this type clause.
1374 { return this->location_; }
1378 traverse(Traverse*);
1380 // Lower to if and goto statements.
1382 lower(Block*, Temporary_statement* descriptor_temp,
1383 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1386 // The type for this type clause.
1388 // The statements to execute.
1390 // Whether this falls through--this is true for "case T1, T2".
1391 bool is_fallthrough_;
1392 // Whether this is the default case.
1394 // The location of this type case clause.
1395 source_location location_;
1398 friend class Type_case_clause;
1400 // The type of the list of type clauses.
1401 typedef std::vector<Type_case_clause> Type_clauses;
1403 // All the type case clauses.
1404 Type_clauses clauses_;
1407 // A type switch statement.
1409 class Type_switch_statement : public Statement
1412 Type_switch_statement(Named_object* var, Expression* expr,
1413 source_location location)
1414 : Statement(STATEMENT_TYPE_SWITCH, location),
1415 var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1416 { gcc_assert(var == NULL || expr == NULL); }
1420 add_clauses(Type_case_clauses* clauses)
1422 gcc_assert(this->clauses_ == NULL);
1423 this->clauses_ = clauses;
1426 // Return the break label for this type switch statement.
1432 do_traverse(Traverse*);
1435 do_lower(Gogo*, Named_object*, Block*);
1438 do_get_backend(Translate_context*)
1439 { gcc_unreachable(); }
1442 // Get the type descriptor.
1444 get_type_descriptor(Translate_context*, Type*, tree);
1446 // The variable holding the value we are switching on.
1448 // The expression we are switching on if there is no variable.
1450 // The type case clauses.
1451 Type_case_clauses* clauses_;
1452 // The break label, if needed.
1453 Unnamed_label* break_label_;
1456 #endif // !defined(GO_STATEMENTS_H)