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;
45 // This class is used to traverse assignments made by a statement
46 // which makes assignments.
48 class Traverse_assignments
51 Traverse_assignments()
54 virtual ~Traverse_assignments()
57 // This is called for a variable initialization.
59 initialize_variable(Named_object*) = 0;
61 // This is called for each assignment made by the statement. PLHS
62 // points to the left hand side, and PRHS points to the right hand
63 // side. PRHS may be NULL if there is no associated expression, as
64 // in the bool set by a non-blocking receive.
66 assignment(Expression** plhs, Expression** prhs) = 0;
68 // This is called for each expression which is not passed to the
69 // assignment function. This is used for some of the statements
70 // which assign two values, for which there is no expression which
71 // describes the value. For ++ and -- the value is passed to both
72 // the assignment method and the rhs method. IS_STORED is true if
73 // this value is being stored directly. It is false if the value is
74 // computed but not stored. IS_LOCAL is true if the value is being
75 // stored in a local variable or this is being called by a return
78 value(Expression**, bool is_stored, bool is_local) = 0;
81 // A single statement.
86 // The types of statements.
87 enum Statement_classification
90 STATEMENT_VARIABLE_DECLARATION,
98 STATEMENT_BREAK_OR_CONTINUE,
100 STATEMENT_GOTO_UNNAMED,
102 STATEMENT_UNNAMED_LABEL,
104 STATEMENT_CONSTANT_SWITCH,
108 // These statements types are created by the parser, but they
109 // disappear during the lowering pass.
110 STATEMENT_ASSIGNMENT_OPERATION,
111 STATEMENT_TUPLE_ASSIGNMENT,
112 STATEMENT_TUPLE_MAP_ASSIGNMENT,
113 STATEMENT_MAP_ASSIGNMENT,
114 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT,
115 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT,
120 STATEMENT_TYPE_SWITCH
123 Statement(Statement_classification, source_location);
125 virtual ~Statement();
127 // Make a variable declaration.
129 make_variable_declaration(Named_object*);
131 // Make a statement which creates a temporary variable and
132 // initializes it to an expression. The block is used if the
133 // temporary variable has to be explicitly destroyed; the variable
134 // must still be added to the block. References to the temporary
135 // variable may be constructed using make_temporary_reference.
136 // Either the type or the initialization expression may be NULL, but
138 static Temporary_statement*
139 make_temporary(Type*, Expression*, source_location);
141 // Make an assignment statement.
143 make_assignment(Expression*, Expression*, source_location);
145 // Make an assignment operation (+=, etc.).
147 make_assignment_operation(Operator, Expression*, Expression*,
150 // Make a tuple assignment statement.
152 make_tuple_assignment(Expression_list*, Expression_list*, source_location);
154 // Make an assignment from a map index to a pair of variables.
156 make_tuple_map_assignment(Expression* val, Expression* present,
157 Expression*, source_location);
159 // Make a statement which assigns a pair of values to a map.
161 make_map_assignment(Expression*, Expression* val,
162 Expression* should_set, source_location);
164 // Make an assignment from a nonblocking receive to a pair of
165 // variables. FOR_SELECT is true is this is being created for a
166 // case x, ok := <-c in a select statement.
168 make_tuple_receive_assignment(Expression* val, Expression* closed,
169 Expression* channel, bool for_select,
172 // Make an assignment from a type guard to a pair of variables.
174 make_tuple_type_guard_assignment(Expression* val, Expression* ok,
175 Expression* expr, Type* type,
178 // Make an expression statement from an Expression.
180 make_statement(Expression*);
182 // Make a block statement from a Block. This is an embedded list of
183 // statements which may also include variable definitions.
185 make_block_statement(Block*, source_location);
187 // Make an increment statement.
189 make_inc_statement(Expression*);
191 // Make a decrement statement.
193 make_dec_statement(Expression*);
195 // Make a go statement.
197 make_go_statement(Call_expression* call, source_location);
199 // Make a defer statement.
201 make_defer_statement(Call_expression* call, source_location);
203 // Make a return statement.
205 make_return_statement(Expression_list*, source_location);
207 // Make a break statement.
209 make_break_statement(Unnamed_label* label, source_location);
211 // Make a continue statement.
213 make_continue_statement(Unnamed_label* label, source_location);
215 // Make a goto statement.
217 make_goto_statement(Label* label, source_location);
219 // Make a goto statement to an unnamed label.
221 make_goto_unnamed_statement(Unnamed_label* label, source_location);
223 // Make a label statement--where the label is defined.
225 make_label_statement(Label* label, source_location);
227 // Make an unnamed label statement--where the label is defined.
229 make_unnamed_label_statement(Unnamed_label* label);
231 // Make an if statement.
233 make_if_statement(Expression* cond, Block* then_block, Block* else_block,
236 // Make a switch statement.
237 static Switch_statement*
238 make_switch_statement(Expression* switch_val, source_location);
240 // Make a type switch statement.
241 static Type_switch_statement*
242 make_type_switch_statement(Named_object* var, Expression*, source_location);
244 // Make a send statement.
245 static Send_statement*
246 make_send_statement(Expression* channel, Expression* val, source_location);
248 // Make a select statement.
249 static Select_statement*
250 make_select_statement(source_location);
252 // Make a for statement.
253 static For_statement*
254 make_for_statement(Block* init, Expression* cond, Block* post,
255 source_location location);
257 // Make a for statement with a range clause.
258 static For_range_statement*
259 make_for_range_statement(Expression* index_var, Expression* value_var,
260 Expression* range, source_location);
262 // Return the statement classification.
263 Statement_classification
264 classification() const
265 { return this->classification_; }
267 // Get the statement location.
270 { return this->location_; }
272 // Traverse the tree.
274 traverse(Block*, size_t* index, Traverse*);
276 // Traverse the contents of this statement--the expressions and
277 // statements which it contains.
279 traverse_contents(Traverse*);
281 // If this statement assigns some values, it calls a function for
282 // each value to which this statement assigns a value, and returns
283 // true. If this statement does not assign any values, it returns
286 traverse_assignments(Traverse_assignments* tassign);
288 // Lower a statement. This is called immediately after parsing to
289 // simplify statements for further processing. It returns the same
290 // Statement or a new one. FUNCTION is the function containing this
291 // statement. BLOCK is the block containing this statement.
293 lower(Gogo* gogo, Named_object* function, Block* block)
294 { return this->do_lower(gogo, function, block); }
296 // Set type information for unnamed constants.
300 // Check types in a statement. This simply checks that any
301 // expressions used by the statement have the right type.
303 check_types(Gogo* gogo)
304 { this->do_check_types(gogo); }
306 // Return whether this is a block statement.
308 is_block_statement() const
309 { return this->classification_ == STATEMENT_BLOCK; }
311 // If this is a variable declaration statement, return it.
312 // Otherwise return NULL.
313 Variable_declaration_statement*
314 variable_declaration_statement()
316 return this->convert<Variable_declaration_statement,
317 STATEMENT_VARIABLE_DECLARATION>();
320 // If this is a return statement, return it. Otherwise return NULL.
323 { return this->convert<Return_statement, STATEMENT_RETURN>(); }
325 // If this is a thunk statement (a go or defer statement), return
326 // it. Otherwise return NULL.
330 // If this is a label statement, return it. Otherwise return NULL.
333 { return this->convert<Label_statement, STATEMENT_LABEL>(); }
335 // If this is a for statement, return it. Otherwise return NULL.
338 { return this->convert<For_statement, STATEMENT_FOR>(); }
340 // If this is a for statement over a range clause, return it.
341 // Otherwise return NULL.
343 for_range_statement()
344 { return this->convert<For_range_statement, STATEMENT_FOR_RANGE>(); }
346 // If this is a switch statement, return it. Otherwise return NULL.
349 { return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
351 // If this is a type switch statement, return it. Otherwise return
353 Type_switch_statement*
354 type_switch_statement()
355 { return this->convert<Type_switch_statement, STATEMENT_TYPE_SWITCH>(); }
357 // If this is a select statement, return it. Otherwise return NULL.
360 { return this->convert<Select_statement, STATEMENT_SELECT>(); }
362 // Return true if this statement may fall through--if after
363 // executing this statement we may go on to execute the following
364 // statement, if any.
366 may_fall_through() const
367 { return this->do_may_fall_through(); }
369 // Return the tree for a statement. BLOCK is the enclosing block.
371 get_tree(Translate_context*);
374 // Implemented by child class: traverse the tree.
376 do_traverse(Traverse*) = 0;
378 // Implemented by child class: traverse assignments. Any statement
379 // which includes an assignment should implement this.
381 do_traverse_assignments(Traverse_assignments*)
384 // Implemented by the child class: lower this statement to a simpler
387 do_lower(Gogo*, Named_object*, Block*)
390 // Implemented by child class: set type information for unnamed
391 // constants. Any statement which includes an expression needs to
397 // Implemented by child class: check types of expressions used in a
400 do_check_types(Gogo*)
403 // Implemented by child class: return true if this statement may
406 do_may_fall_through() const
409 // Implemented by child class: return a tree.
411 do_get_tree(Translate_context*) = 0;
413 // Traverse an expression in a statement.
415 traverse_expression(Traverse*, Expression**);
417 // Traverse an expression list in a statement. The Expression_list
420 traverse_expression_list(Traverse*, Expression_list*);
422 // Traverse a type in a statement.
424 traverse_type(Traverse*, Type*);
426 // Build a tree node with one operand, setting the location. The
427 // first operand really has type "enum tree_code", but that enum is
430 build_stmt_1(int tree_code_value, tree);
432 // For children to call when they detect that they are in error.
436 // For children to call to report an error conveniently.
438 report_error(const char*);
440 // For children to return an error statement from lower().
442 make_error_statement(source_location);
445 // Convert to the desired statement classification, or return NULL.
446 // This is a controlled dynamic cast.
447 template<typename Statement_class, Statement_classification sc>
451 return (this->classification_ == sc
452 ? static_cast<Statement_class*>(this)
456 template<typename Statement_class, Statement_classification sc>
457 const Statement_class*
460 return (this->classification_ == sc
461 ? static_cast<const Statement_class*>(this)
465 // The statement classification.
466 Statement_classification classification_;
467 // The location in the input file of the start of this statement.
468 source_location location_;
471 // A statement which creates and initializes a temporary variable.
473 class Temporary_statement : public Statement
476 Temporary_statement(Type* type, Expression* init, source_location location)
477 : Statement(STATEMENT_TEMPORARY, location),
478 type_(type), init_(init), decl_(NULL), is_address_taken_(false)
481 // Return the type of the temporary variable.
485 // Return the initialization expression.
488 { return this->init_; }
490 // Record that something takes the address of this temporary
493 set_is_address_taken()
494 { this->is_address_taken_ = true; }
496 // Return the tree for the temporary variable itself. This should
497 // not be called until after the statement itself has been expanded.
503 do_traverse(Traverse*);
506 do_traverse_assignments(Traverse_assignments*);
509 do_determine_types();
512 do_check_types(Gogo*);
515 do_get_tree(Translate_context*);
518 // The type of the temporary variable.
520 // The initial value of the temporary variable. This may be NULL.
522 // The DECL for the temporary variable.
524 // True if something takes the address of this temporary variable.
525 bool is_address_taken_;
528 // A variable declaration. This marks the point in the code where a
529 // variable is declared. The Variable is also attached to a Block.
531 class Variable_declaration_statement : public Statement
534 Variable_declaration_statement(Named_object* var);
536 // The variable being declared.
539 { return this->var_; }
543 do_traverse(Traverse*);
546 do_traverse_assignments(Traverse_assignments*);
549 do_get_tree(Translate_context*);
555 // A return statement.
557 class Return_statement : public Statement
560 Return_statement(Expression_list* vals, source_location location)
561 : Statement(STATEMENT_RETURN, location),
562 vals_(vals), is_lowered_(false)
565 // The list of values being returned. This may be NULL.
566 const Expression_list*
568 { return this->vals_; }
572 do_traverse(Traverse* traverse)
573 { return this->traverse_expression_list(traverse, this->vals_); }
576 do_traverse_assignments(Traverse_assignments*);
579 do_lower(Gogo*, Named_object*, Block*);
582 do_may_fall_through() const
586 do_get_tree(Translate_context*);
589 // Return values. This may be NULL.
590 Expression_list* vals_;
591 // True if this statement has been lowered.
597 class Send_statement : public Statement
600 Send_statement(Expression* channel, Expression* val,
601 source_location location)
602 : Statement(STATEMENT_SEND, location),
603 channel_(channel), val_(val), for_select_(false)
606 // Note that this is for a select statement.
609 { this->for_select_ = true; }
613 do_traverse(Traverse* traverse);
616 do_determine_types();
619 do_check_types(Gogo*);
622 do_get_tree(Translate_context*);
625 // The channel on which to send the value.
626 Expression* channel_;
627 // The value to send.
629 // Whether this is for a select statement.
633 // Select_clauses holds the clauses of a select statement. This is
634 // built by the parser.
643 // Add a new clause. IS_SEND is true if this is a send clause,
644 // false for a receive clause. For a send clause CHANNEL is the
645 // channel and VAL is the value to send. For a receive clause
646 // CHANNEL is the channel, VAL is either NULL or a Var_expression
647 // for the variable to set, and CLOSED is either NULL or a
648 // Var_expression to set to whether the channel is closed. If VAL
649 // is NULL, VAR may be a variable to be initialized with the
650 // received value, and CLOSEDVAR ma be a variable to be initialized
651 // with whether the channel is closed. IS_DEFAULT is true if this
652 // is the default clause. STATEMENTS is the list of statements to
655 add(bool is_send, Expression* channel, Expression* val, Expression* closed,
656 Named_object* var, Named_object* closedvar, bool is_default,
657 Block* statements, source_location location)
659 this->clauses_.push_back(Select_clause(is_send, channel, val, closed, var,
660 closedvar, is_default, statements,
664 // Traverse the select clauses.
670 lower(Gogo*, Named_object*, Block*);
676 // Whether the select clauses may fall through to the statement
677 // which follows the overall select statement.
679 may_fall_through() const;
681 // Return a tree implementing the select statement.
683 get_tree(Translate_context*, Unnamed_label* break_label, source_location);
691 : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
692 closedvar_(NULL), statements_(NULL), is_send_(false),
696 Select_clause(bool is_send, Expression* channel, Expression* val,
697 Expression* closed, Named_object* var,
698 Named_object* closedvar, bool is_default, Block* statements,
699 source_location location)
700 : channel_(channel), val_(val), closed_(closed), var_(var),
701 closedvar_(closedvar), statements_(statements), location_(location),
702 is_send_(is_send), is_default_(is_default), is_lowered_(false)
703 { gcc_assert(is_default ? channel == NULL : channel != NULL); }
705 // Traverse the select clause.
711 lower(Gogo*, Named_object*, Block*);
717 // Return true if this is the default clause.
720 { return this->is_default_; }
722 // Return the channel. This will return NULL for the default
726 { return this->channel_; }
728 // Return true for a send, false for a receive.
732 gcc_assert(!this->is_default_);
733 return this->is_send_;
736 // Return the statements.
739 { return this->statements_; }
741 // Return the location.
744 { return this->location_; }
746 // Whether this clause may fall through to the statement which
747 // follows the overall select statement.
749 may_fall_through() const;
751 // Return a tree for the statements to execute.
753 get_statements_tree(Translate_context*);
757 Expression* channel_;
758 // The value to send or the lvalue to receive into.
760 // The lvalue to set to whether the channel is closed on a
763 // The variable to initialize, for "case a := <-ch".
765 // The variable to initialize to whether the channel is closed,
766 // for "case a, c := <-ch".
767 Named_object* closedvar_;
768 // The statements to execute.
770 // The location of this clause.
771 source_location location_;
772 // Whether this is a send or a receive.
774 // Whether this is the default.
776 // Whether this has been lowered.
781 add_clause_tree(Translate_context*, int, Select_clause*, Unnamed_label*,
784 typedef std::vector<Select_clause> Clauses;
789 // A select statement.
791 class Select_statement : public Statement
794 Select_statement(source_location location)
795 : Statement(STATEMENT_SELECT, location),
796 clauses_(NULL), break_label_(NULL), is_lowered_(false)
801 add_clauses(Select_clauses* clauses)
803 gcc_assert(this->clauses_ == NULL);
804 this->clauses_ = clauses;
807 // Return the break label for this select statement.
813 do_traverse(Traverse* traverse)
814 { return this->clauses_->traverse(traverse); }
817 do_lower(Gogo*, Named_object*, Block*);
821 { this->clauses_->determine_types(); }
824 do_may_fall_through() const
825 { return this->clauses_->may_fall_through(); }
828 do_get_tree(Translate_context*);
831 // The select clauses.
832 Select_clauses* clauses_;
834 Unnamed_label* break_label_;
835 // Whether this statement has been lowered.
839 // A statement which requires a thunk: go or defer.
841 class Thunk_statement : public Statement
844 Thunk_statement(Statement_classification, Call_expression*,
847 // Return the call expression.
850 { return this->call_; }
852 // Simplify a go or defer statement so that it only uses a single
855 simplify_statement(Gogo*, Block*);
859 do_traverse(Traverse* traverse);
862 do_traverse_assignments(Traverse_assignments*);
865 do_determine_types();
868 do_check_types(Gogo*);
870 // Return the function and argument trees for the call.
872 get_fn_and_arg(Translate_context*, tree* pfn, tree* parg);
875 // Return whether this is a simple go statement.
877 is_simple(Function_type*) const;
879 // Build the struct to use for a complex case.
881 build_struct(Function_type* fntype);
885 build_thunk(Gogo*, const std::string&, Function_type* fntype);
887 // The field name used in the thunk structure for the function
889 static const char* const thunk_field_fn;
891 // The field name used in the thunk structure for the receiver, if
893 static const char* const thunk_field_receiver;
895 // Set the name to use for thunk field N.
897 thunk_field_param(int n, char* buf, size_t buflen);
899 // The function call to be executed in a separate thread (go) or
902 // The type used for a struct to pass to a thunk, if this is not a
904 Struct_type* struct_type_;
909 class Go_statement : public Thunk_statement
912 Go_statement(Call_expression* call, source_location location)
913 : Thunk_statement(STATEMENT_GO, call, location)
918 do_get_tree(Translate_context*);
921 // A defer statement.
923 class Defer_statement : public Thunk_statement
926 Defer_statement(Call_expression* call, source_location location)
927 : Thunk_statement(STATEMENT_DEFER, call, location)
932 do_get_tree(Translate_context*);
935 // A label statement.
937 class Label_statement : public Statement
940 Label_statement(Label* label, source_location location)
941 : Statement(STATEMENT_LABEL, location),
945 // Return the label itself.
948 { return this->label_; }
952 do_traverse(Traverse*);
955 do_get_tree(Translate_context*);
964 class For_statement : public Statement
967 For_statement(Block* init, Expression* cond, Block* post,
968 source_location location)
969 : Statement(STATEMENT_FOR, location),
970 init_(init), cond_(cond), post_(post), statements_(NULL),
971 break_label_(NULL), continue_label_(NULL)
974 // Add the statements.
976 add_statements(Block* statements)
978 gcc_assert(this->statements_ == NULL);
979 this->statements_ = statements;
982 // Return the break label for this for statement.
986 // Return the continue label for this for statement.
990 // Set the break and continue labels for this statement.
992 set_break_continue_labels(Unnamed_label* break_label,
993 Unnamed_label* continue_label);
997 do_traverse(Traverse*);
1000 do_traverse_assignments(Traverse_assignments*)
1001 { gcc_unreachable(); }
1004 do_lower(Gogo*, Named_object*, Block*);
1007 do_get_tree(Translate_context*)
1008 { gcc_unreachable(); }
1011 // The initialization statements. This may be NULL.
1013 // The condition. This may be NULL.
1015 // The statements to run after each iteration. This may be NULL.
1017 // The statements in the loop itself.
1019 // The break label, if needed.
1020 Unnamed_label* break_label_;
1021 // The continue label, if needed.
1022 Unnamed_label* continue_label_;
1025 // A for statement over a range clause.
1027 class For_range_statement : public Statement
1030 For_range_statement(Expression* index_var, Expression* value_var,
1031 Expression* range, source_location location)
1032 : Statement(STATEMENT_FOR_RANGE, location),
1033 index_var_(index_var), value_var_(value_var), range_(range),
1034 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1037 // Add the statements.
1039 add_statements(Block* statements)
1041 gcc_assert(this->statements_ == NULL);
1042 this->statements_ = statements;
1045 // Return the break label for this for statement.
1049 // Return the continue label for this for statement.
1055 do_traverse(Traverse*);
1058 do_traverse_assignments(Traverse_assignments*)
1059 { gcc_unreachable(); }
1062 do_lower(Gogo*, Named_object*, Block*);
1065 do_get_tree(Translate_context*)
1066 { gcc_unreachable(); }
1070 make_range_ref(Named_object*, Temporary_statement*, source_location);
1073 call_builtin(Gogo*, const char* funcname, Expression* arg, source_location);
1076 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1077 Temporary_statement*, Temporary_statement*,
1078 Block**, Expression**, Block**, Block**);
1081 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1082 Temporary_statement*, Temporary_statement*,
1083 Block**, Expression**, Block**, Block**);
1086 lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1087 Temporary_statement*, Temporary_statement*,
1088 Block**, Expression**, Block**, Block**);
1091 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1092 Temporary_statement*, Temporary_statement*,
1093 Temporary_statement*, Block**, Expression**, Block**,
1096 // The variable which is set to the index value.
1097 Expression* index_var_;
1098 // The variable which is set to the element value. This may be
1100 Expression* value_var_;
1101 // The expression we are ranging over.
1103 // The statements in the block.
1105 // The break label, if needed.
1106 Unnamed_label* break_label_;
1107 // The continue label, if needed.
1108 Unnamed_label* continue_label_;
1111 // Class Case_clauses holds the clauses of a switch statement. This
1112 // is built by the parser.
1121 // Add a new clause. CASES is a list of case expressions; it may be
1122 // NULL. IS_DEFAULT is true if this is the default case.
1123 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1124 // after the statements the case clause should fall through to the
1127 add(Expression_list* cases, bool is_default, Block* statements,
1128 bool is_fallthrough, source_location location)
1130 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1131 is_fallthrough, location));
1134 // Return whether there are no clauses.
1137 { return this->clauses_.empty(); }
1139 // Traverse the case clauses.
1141 traverse(Traverse*);
1143 // Lower for a nonconstant switch.
1145 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1147 // Determine types of expressions. The Type parameter is the type
1148 // of the switch value.
1150 determine_types(Type*);
1152 // Check types. The Type parameter is the type of the switch value.
1156 // Return true if all the clauses are constant values.
1158 is_constant() const;
1160 // Return true if these clauses may fall through to the statements
1161 // following the switch statement.
1163 may_fall_through() const;
1165 // Return the body of a SWITCH_EXPR when all the clauses are
1168 get_backend(Translate_context*, Unnamed_label* break_label,
1169 std::vector<std::vector<Bexpression*> >* all_cases,
1170 std::vector<Bstatement*>* all_statements) const;
1173 // For a constant tree we need to keep a record of constants we have
1174 // already seen. Note that INTEGER_CST trees are interned.
1175 class Hash_integer_value;
1176 class Eq_integer_value;
1177 typedef Unordered_set_hash(Expression*, Hash_integer_value,
1178 Eq_integer_value) Case_constants;
1185 : cases_(NULL), statements_(NULL), is_default_(false),
1186 is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1189 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1190 bool is_fallthrough, source_location location)
1191 : cases_(cases), statements_(statements), is_default_(is_default),
1192 is_fallthrough_(is_fallthrough), location_(location)
1195 // Whether this clause falls through to the next clause.
1197 is_fallthrough() const
1198 { return this->is_fallthrough_; }
1200 // Whether this is the default.
1203 { return this->is_default_; }
1205 // The location of this clause.
1208 { return this->location_; }
1212 traverse(Traverse*);
1214 // Lower for a nonconstant switch.
1216 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1220 determine_types(Type*);
1226 // Return true if all the case expressions are constant.
1228 is_constant() const;
1230 // Return true if this clause may fall through to execute the
1231 // statements following the switch statement. This is not the
1232 // same as whether this clause falls through to the next clause.
1234 may_fall_through() const;
1236 // Convert the case values and statements to the backend
1239 get_backend(Translate_context*, Unnamed_label* break_label,
1240 Case_constants*, std::vector<Bexpression*>* cases) const;
1243 // The list of case expressions.
1244 Expression_list* cases_;
1245 // The statements to execute.
1247 // Whether this is the default case.
1249 // Whether this falls through after the statements.
1250 bool is_fallthrough_;
1251 // The location of this case clause.
1252 source_location location_;
1255 friend class Case_clause;
1257 // The type of the list of clauses.
1258 typedef std::vector<Case_clause> Clauses;
1260 // All the case clauses.
1264 // A switch statement.
1266 class Switch_statement : public Statement
1269 Switch_statement(Expression* val, source_location location)
1270 : Statement(STATEMENT_SWITCH, location),
1271 val_(val), clauses_(NULL), break_label_(NULL)
1276 add_clauses(Case_clauses* clauses)
1278 gcc_assert(this->clauses_ == NULL);
1279 this->clauses_ = clauses;
1282 // Return the break label for this switch statement.
1288 do_traverse(Traverse*);
1291 do_lower(Gogo*, Named_object*, Block*);
1294 do_get_tree(Translate_context*)
1295 { gcc_unreachable(); }
1298 // The value to switch on. This may be NULL.
1300 // The case clauses.
1301 Case_clauses* clauses_;
1302 // The break label, if needed.
1303 Unnamed_label* break_label_;
1306 // Class Type_case_clauses holds the clauses of a type switch
1307 // statement. This is built by the parser.
1309 class Type_case_clauses
1316 // Add a new clause. TYPE is the type for this clause; it may be
1317 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1318 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1319 // if this is the default case. STATEMENTS is a block of
1320 // statements; it may be NULL.
1322 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1323 source_location location)
1325 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1326 statements, location));
1329 // Return whether there are no clauses.
1332 { return this->clauses_.empty(); }
1334 // Traverse the type case clauses.
1336 traverse(Traverse*);
1338 // Check for duplicates.
1340 check_duplicates() const;
1342 // Lower to if and goto statements.
1344 lower(Block*, Temporary_statement* descriptor_temp,
1345 Unnamed_label* break_label) const;
1348 // One type case clause.
1349 class Type_case_clause
1353 : type_(NULL), statements_(NULL), is_default_(false),
1354 location_(UNKNOWN_LOCATION)
1357 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1358 Block* statements, source_location location)
1359 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1360 is_default_(is_default), location_(location)
1366 { return this->type_; }
1368 // Whether this is the default.
1371 { return this->is_default_; }
1373 // The location of this type clause.
1376 { return this->location_; }
1380 traverse(Traverse*);
1382 // Lower to if and goto statements.
1384 lower(Block*, Temporary_statement* descriptor_temp,
1385 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1388 // The type for this type clause.
1390 // The statements to execute.
1392 // Whether this falls through--this is true for "case T1, T2".
1393 bool is_fallthrough_;
1394 // Whether this is the default case.
1396 // The location of this type case clause.
1397 source_location location_;
1400 friend class Type_case_clause;
1402 // The type of the list of type clauses.
1403 typedef std::vector<Type_case_clause> Type_clauses;
1405 // All the type case clauses.
1406 Type_clauses clauses_;
1409 // A type switch statement.
1411 class Type_switch_statement : public Statement
1414 Type_switch_statement(Named_object* var, Expression* expr,
1415 source_location location)
1416 : Statement(STATEMENT_TYPE_SWITCH, location),
1417 var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1418 { gcc_assert(var == NULL || expr == NULL); }
1422 add_clauses(Type_case_clauses* clauses)
1424 gcc_assert(this->clauses_ == NULL);
1425 this->clauses_ = clauses;
1428 // Return the break label for this type switch statement.
1434 do_traverse(Traverse*);
1437 do_lower(Gogo*, Named_object*, Block*);
1440 do_get_tree(Translate_context*)
1441 { gcc_unreachable(); }
1444 // Get the type descriptor.
1446 get_type_descriptor(Translate_context*, Type*, tree);
1448 // The variable holding the value we are switching on.
1450 // The expression we are switching on if there is no variable.
1452 // The type case clauses.
1453 Type_case_clauses* clauses_;
1454 // The break label, if needed.
1455 Unnamed_label* break_label_;
1458 #endif // !defined(GO_STATEMENTS_H)