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
14 class Statement_inserter;
18 class Temporary_statement;
19 class Variable_declaration_statement;
20 class Return_statement;
21 class Thunk_statement;
22 class Label_statement;
24 class For_range_statement;
25 class Switch_statement;
26 class Type_switch_statement;
28 class Select_statement;
32 class Translate_context;
34 class Expression_list;
36 class Call_expression;
37 class Map_index_expression;
38 class Receive_expression;
40 class Type_case_clauses;
42 class Typed_identifier_list;
46 class Ast_dump_context;
48 // This class is used to traverse assignments made by a statement
49 // which makes assignments.
51 class Traverse_assignments
54 Traverse_assignments()
57 virtual ~Traverse_assignments()
60 // This is called for a variable initialization.
62 initialize_variable(Named_object*) = 0;
64 // This is called for each assignment made by the statement. PLHS
65 // points to the left hand side, and PRHS points to the right hand
66 // side. PRHS may be NULL if there is no associated expression, as
67 // in the bool set by a non-blocking receive.
69 assignment(Expression** plhs, Expression** prhs) = 0;
71 // This is called for each expression which is not passed to the
72 // assignment function. This is used for some of the statements
73 // which assign two values, for which there is no expression which
74 // describes the value. For ++ and -- the value is passed to both
75 // the assignment method and the rhs method. IS_STORED is true if
76 // this value is being stored directly. It is false if the value is
77 // computed but not stored. IS_LOCAL is true if the value is being
78 // stored in a local variable or this is being called by a return
81 value(Expression**, bool is_stored, bool is_local) = 0;
84 // A single statement.
89 // The types of statements.
90 enum Statement_classification
93 STATEMENT_VARIABLE_DECLARATION,
101 STATEMENT_BREAK_OR_CONTINUE,
103 STATEMENT_GOTO_UNNAMED,
105 STATEMENT_UNNAMED_LABEL,
107 STATEMENT_CONSTANT_SWITCH,
111 // These statements types are created by the parser, but they
112 // disappear during the lowering pass.
113 STATEMENT_ASSIGNMENT_OPERATION,
114 STATEMENT_TUPLE_ASSIGNMENT,
115 STATEMENT_TUPLE_MAP_ASSIGNMENT,
116 STATEMENT_MAP_ASSIGNMENT,
117 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT,
118 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT,
123 STATEMENT_TYPE_SWITCH
126 Statement(Statement_classification, source_location);
128 virtual ~Statement();
130 // Make a variable declaration.
132 make_variable_declaration(Named_object*);
134 // Make a statement which creates a temporary variable and
135 // initializes it to an expression. The block is used if the
136 // temporary variable has to be explicitly destroyed; the variable
137 // must still be added to the block. References to the temporary
138 // variable may be constructed using make_temporary_reference.
139 // Either the type or the initialization expression may be NULL, but
141 static Temporary_statement*
142 make_temporary(Type*, Expression*, source_location);
144 // Make an assignment statement.
146 make_assignment(Expression*, Expression*, source_location);
148 // Make an assignment operation (+=, etc.).
150 make_assignment_operation(Operator, Expression*, Expression*,
153 // Make a tuple assignment statement.
155 make_tuple_assignment(Expression_list*, Expression_list*, source_location);
157 // Make an assignment from a map index to a pair of variables.
159 make_tuple_map_assignment(Expression* val, Expression* present,
160 Expression*, source_location);
162 // Make a statement which assigns a pair of values to a map.
164 make_map_assignment(Expression*, Expression* val,
165 Expression* should_set, source_location);
167 // Make an assignment from a nonblocking receive to a pair of
168 // variables. FOR_SELECT is true is this is being created for a
169 // case x, ok := <-c in a select statement.
171 make_tuple_receive_assignment(Expression* val, Expression* closed,
172 Expression* channel, bool for_select,
175 // Make an assignment from a type guard to a pair of variables.
177 make_tuple_type_guard_assignment(Expression* val, Expression* ok,
178 Expression* expr, Type* type,
181 // Make an expression statement from an Expression.
183 make_statement(Expression*);
185 // Make a block statement from a Block. This is an embedded list of
186 // statements which may also include variable definitions.
188 make_block_statement(Block*, source_location);
190 // Make an increment statement.
192 make_inc_statement(Expression*);
194 // Make a decrement statement.
196 make_dec_statement(Expression*);
198 // Make a go statement.
200 make_go_statement(Call_expression* call, source_location);
202 // Make a defer statement.
204 make_defer_statement(Call_expression* call, source_location);
206 // Make a return statement.
208 make_return_statement(Expression_list*, source_location);
210 // Make a break statement.
212 make_break_statement(Unnamed_label* label, source_location);
214 // Make a continue statement.
216 make_continue_statement(Unnamed_label* label, source_location);
218 // Make a goto statement.
220 make_goto_statement(Label* label, source_location);
222 // Make a goto statement to an unnamed label.
224 make_goto_unnamed_statement(Unnamed_label* label, source_location);
226 // Make a label statement--where the label is defined.
228 make_label_statement(Label* label, source_location);
230 // Make an unnamed label statement--where the label is defined.
232 make_unnamed_label_statement(Unnamed_label* label);
234 // Make an if statement.
236 make_if_statement(Expression* cond, Block* then_block, Block* else_block,
239 // Make a switch statement.
240 static Switch_statement*
241 make_switch_statement(Expression* switch_val, source_location);
243 // Make a type switch statement.
244 static Type_switch_statement*
245 make_type_switch_statement(Named_object* var, Expression*, source_location);
247 // Make a send statement.
248 static Send_statement*
249 make_send_statement(Expression* channel, Expression* val, source_location);
251 // Make a select statement.
252 static Select_statement*
253 make_select_statement(source_location);
255 // Make a for statement.
256 static For_statement*
257 make_for_statement(Block* init, Expression* cond, Block* post,
258 source_location location);
260 // Make a for statement with a range clause.
261 static For_range_statement*
262 make_for_range_statement(Expression* index_var, Expression* value_var,
263 Expression* range, source_location);
265 // Return the statement classification.
266 Statement_classification
267 classification() const
268 { return this->classification_; }
270 // Get the statement location.
273 { return this->location_; }
275 // Traverse the tree.
277 traverse(Block*, size_t* index, Traverse*);
279 // Traverse the contents of this statement--the expressions and
280 // statements which it contains.
282 traverse_contents(Traverse*);
284 // If this statement assigns some values, it calls a function for
285 // each value to which this statement assigns a value, and returns
286 // true. If this statement does not assign any values, it returns
289 traverse_assignments(Traverse_assignments* tassign);
291 // Lower a statement. This is called immediately after parsing to
292 // simplify statements for further processing. It returns the same
293 // Statement or a new one. FUNCTION is the function containing this
294 // statement. BLOCK is the block containing this statement.
295 // INSERTER can be used to insert new statements before this one.
297 lower(Gogo* gogo, Named_object* function, Block* block,
298 Statement_inserter* inserter)
299 { return this->do_lower(gogo, function, block, inserter); }
301 // Set type information for unnamed constants.
305 // Check types in a statement. This simply checks that any
306 // expressions used by the statement have the right type.
308 check_types(Gogo* gogo)
309 { this->do_check_types(gogo); }
311 // Return whether this is a block statement.
313 is_block_statement() const
314 { return this->classification_ == STATEMENT_BLOCK; }
316 // If this is a variable declaration statement, return it.
317 // Otherwise return NULL.
318 Variable_declaration_statement*
319 variable_declaration_statement()
321 return this->convert<Variable_declaration_statement,
322 STATEMENT_VARIABLE_DECLARATION>();
325 // If this is a return statement, return it. Otherwise return NULL.
328 { return this->convert<Return_statement, STATEMENT_RETURN>(); }
330 // If this is a thunk statement (a go or defer statement), return
331 // it. Otherwise return NULL.
335 // If this is a label statement, return it. Otherwise return NULL.
338 { return this->convert<Label_statement, STATEMENT_LABEL>(); }
340 // If this is a for statement, return it. Otherwise return NULL.
343 { return this->convert<For_statement, STATEMENT_FOR>(); }
345 // If this is a for statement over a range clause, return it.
346 // Otherwise return NULL.
348 for_range_statement()
349 { return this->convert<For_range_statement, STATEMENT_FOR_RANGE>(); }
351 // If this is a switch statement, return it. Otherwise return NULL.
354 { return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
356 // If this is a type switch statement, return it. Otherwise return
358 Type_switch_statement*
359 type_switch_statement()
360 { return this->convert<Type_switch_statement, STATEMENT_TYPE_SWITCH>(); }
362 // If this is a select statement, return it. Otherwise return NULL.
365 { return this->convert<Select_statement, STATEMENT_SELECT>(); }
367 // Return true if this statement may fall through--if after
368 // executing this statement we may go on to execute the following
369 // statement, if any.
371 may_fall_through() const
372 { return this->do_may_fall_through(); }
374 // Convert the statement to the backend representation.
376 get_backend(Translate_context*);
378 // Dump AST representation of a statement to a dump context.
380 dump_statement(Ast_dump_context*) const;
383 // Implemented by child class: traverse the tree.
385 do_traverse(Traverse*) = 0;
387 // Implemented by child class: traverse assignments. Any statement
388 // which includes an assignment should implement this.
390 do_traverse_assignments(Traverse_assignments*)
393 // Implemented by the child class: lower this statement to a simpler
396 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
399 // Implemented by child class: set type information for unnamed
400 // constants. Any statement which includes an expression needs to
406 // Implemented by child class: check types of expressions used in a
409 do_check_types(Gogo*)
412 // Implemented by child class: return true if this statement may
415 do_may_fall_through() const
418 // Implemented by child class: convert to backend representation.
420 do_get_backend(Translate_context*) = 0;
422 // Implemented by child class: dump ast representation.
424 do_dump_statement(Ast_dump_context*) const = 0;
426 // Traverse an expression in a statement.
428 traverse_expression(Traverse*, Expression**);
430 // Traverse an expression list in a statement. The Expression_list
433 traverse_expression_list(Traverse*, Expression_list*);
435 // Traverse a type in a statement.
437 traverse_type(Traverse*, Type*);
439 // For children to call when they detect that they are in error.
443 // For children to call to report an error conveniently.
445 report_error(const char*);
447 // For children to return an error statement from lower().
449 make_error_statement(source_location);
452 // Convert to the desired statement classification, or return NULL.
453 // This is a controlled dynamic cast.
454 template<typename Statement_class, Statement_classification sc>
458 return (this->classification_ == sc
459 ? static_cast<Statement_class*>(this)
463 template<typename Statement_class, Statement_classification sc>
464 const Statement_class*
467 return (this->classification_ == sc
468 ? static_cast<const Statement_class*>(this)
472 // The statement classification.
473 Statement_classification classification_;
474 // The location in the input file of the start of this statement.
475 source_location location_;
478 // A statement which creates and initializes a temporary variable.
480 class Temporary_statement : public Statement
483 Temporary_statement(Type* type, Expression* init, source_location location)
484 : Statement(STATEMENT_TEMPORARY, location),
485 type_(type), init_(init), bvariable_(NULL), is_address_taken_(false)
488 // Return the type of the temporary variable.
492 // Record that something takes the address of this temporary
495 set_is_address_taken()
496 { this->is_address_taken_ = true; }
498 // Return the temporary variable. This should not be called until
499 // after the statement itself has been converted.
501 get_backend_variable(Translate_context*) const;
505 do_traverse(Traverse*);
508 do_traverse_assignments(Traverse_assignments*);
511 do_determine_types();
514 do_check_types(Gogo*);
517 do_get_backend(Translate_context*);
520 do_dump_statement(Ast_dump_context*) const;
523 // The type of the temporary variable.
525 // The initial value of the temporary variable. This may be NULL.
527 // The backend representation of the temporary variable.
528 Bvariable* bvariable_;
529 // True if something takes the address of this temporary variable.
530 bool is_address_taken_;
533 // A variable declaration. This marks the point in the code where a
534 // variable is declared. The Variable is also attached to a Block.
536 class Variable_declaration_statement : public Statement
539 Variable_declaration_statement(Named_object* var);
541 // The variable being declared.
544 { return this->var_; }
548 do_traverse(Traverse*);
551 do_traverse_assignments(Traverse_assignments*);
554 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
557 do_get_backend(Translate_context*);
560 do_dump_statement(Ast_dump_context*) const;
566 // A return statement.
568 class Return_statement : public Statement
571 Return_statement(Expression_list* vals, source_location location)
572 : Statement(STATEMENT_RETURN, location),
573 vals_(vals), is_lowered_(false)
576 // The list of values being returned. This may be NULL.
577 const Expression_list*
579 { return this->vals_; }
583 do_traverse(Traverse* traverse)
584 { return this->traverse_expression_list(traverse, this->vals_); }
587 do_traverse_assignments(Traverse_assignments*);
590 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
593 do_may_fall_through() const
597 do_get_backend(Translate_context*);
600 do_dump_statement(Ast_dump_context*) const;
603 // Return values. This may be NULL.
604 Expression_list* vals_;
605 // True if this statement has been lowered.
611 class Send_statement : public Statement
614 Send_statement(Expression* channel, Expression* val,
615 source_location location)
616 : Statement(STATEMENT_SEND, location),
617 channel_(channel), val_(val), for_select_(false)
620 // Note that this is for a select statement.
623 { this->for_select_ = true; }
627 do_traverse(Traverse* traverse);
630 do_determine_types();
633 do_check_types(Gogo*);
636 do_get_backend(Translate_context*);
639 do_dump_statement(Ast_dump_context*) const;
642 // The channel on which to send the value.
643 Expression* channel_;
644 // The value to send.
646 // Whether this is for a select statement.
650 // Select_clauses holds the clauses of a select statement. This is
651 // built by the parser.
660 // Add a new clause. IS_SEND is true if this is a send clause,
661 // false for a receive clause. For a send clause CHANNEL is the
662 // channel and VAL is the value to send. For a receive clause
663 // CHANNEL is the channel, VAL is either NULL or a Var_expression
664 // for the variable to set, and CLOSED is either NULL or a
665 // Var_expression to set to whether the channel is closed. If VAL
666 // is NULL, VAR may be a variable to be initialized with the
667 // received value, and CLOSEDVAR ma be a variable to be initialized
668 // with whether the channel is closed. IS_DEFAULT is true if this
669 // is the default clause. STATEMENTS is the list of statements to
672 add(bool is_send, Expression* channel, Expression* val, Expression* closed,
673 Named_object* var, Named_object* closedvar, bool is_default,
674 Block* statements, source_location location)
676 this->clauses_.push_back(Select_clause(is_send, channel, val, closed, var,
677 closedvar, is_default, statements,
681 // Traverse the select clauses.
687 lower(Gogo*, Named_object*, Block*);
693 // Whether the select clauses may fall through to the statement
694 // which follows the overall select statement.
696 may_fall_through() const;
698 // Convert to the backend representation.
700 get_backend(Translate_context*, Unnamed_label* break_label, source_location);
702 // Dump AST representation.
704 dump_clauses(Ast_dump_context*) const;
712 : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
713 closedvar_(NULL), statements_(NULL), is_send_(false),
717 Select_clause(bool is_send, Expression* channel, Expression* val,
718 Expression* closed, Named_object* var,
719 Named_object* closedvar, bool is_default, Block* statements,
720 source_location location)
721 : channel_(channel), val_(val), closed_(closed), var_(var),
722 closedvar_(closedvar), statements_(statements), location_(location),
723 is_send_(is_send), is_default_(is_default), is_lowered_(false)
724 { go_assert(is_default ? channel == NULL : channel != NULL); }
726 // Traverse the select clause.
732 lower(Gogo*, Named_object*, Block*);
738 // Return true if this is the default clause.
741 { return this->is_default_; }
743 // Return the channel. This will return NULL for the default
747 { return this->channel_; }
749 // Return true for a send, false for a receive.
753 go_assert(!this->is_default_);
754 return this->is_send_;
757 // Return the statements.
760 { return this->statements_; }
762 // Return the location.
765 { return this->location_; }
767 // Whether this clause may fall through to the statement which
768 // follows the overall select statement.
770 may_fall_through() const;
772 // Convert the statements to the backend representation.
774 get_statements_backend(Translate_context*);
776 // Dump AST representation.
778 dump_clause(Ast_dump_context*) const;
782 Expression* channel_;
783 // The value to send or the lvalue to receive into.
785 // The lvalue to set to whether the channel is closed on a
788 // The variable to initialize, for "case a := <-ch".
790 // The variable to initialize to whether the channel is closed,
791 // for "case a, c := <-ch".
792 Named_object* closedvar_;
793 // The statements to execute.
795 // The location of this clause.
796 source_location location_;
797 // Whether this is a send or a receive.
799 // Whether this is the default.
801 // Whether this has been lowered.
806 add_clause_backend(Translate_context*, source_location, int index,
807 int case_value, Select_clause*, Unnamed_label*,
808 std::vector<std::vector<Bexpression*> >* cases,
809 std::vector<Bstatement*>* clauses);
811 typedef std::vector<Select_clause> Clauses;
816 // A select statement.
818 class Select_statement : public Statement
821 Select_statement(source_location location)
822 : Statement(STATEMENT_SELECT, location),
823 clauses_(NULL), break_label_(NULL), is_lowered_(false)
828 add_clauses(Select_clauses* clauses)
830 go_assert(this->clauses_ == NULL);
831 this->clauses_ = clauses;
834 // Return the break label for this select statement.
840 do_traverse(Traverse* traverse)
841 { return this->clauses_->traverse(traverse); }
844 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
848 { this->clauses_->determine_types(); }
851 do_may_fall_through() const
852 { return this->clauses_->may_fall_through(); }
855 do_get_backend(Translate_context*);
858 do_dump_statement(Ast_dump_context*) const;
861 // The select clauses.
862 Select_clauses* clauses_;
864 Unnamed_label* break_label_;
865 // Whether this statement has been lowered.
869 // A statement which requires a thunk: go or defer.
871 class Thunk_statement : public Statement
874 Thunk_statement(Statement_classification, Call_expression*,
877 // Return the call expression.
880 { return this->call_; }
882 // Simplify a go or defer statement so that it only uses a single
885 simplify_statement(Gogo*, Named_object*, Block*);
889 do_traverse(Traverse* traverse);
892 do_traverse_assignments(Traverse_assignments*);
895 do_determine_types();
898 do_check_types(Gogo*);
900 // Return the function and argument for the call.
902 get_fn_and_arg(Expression** pfn, Expression** parg);
905 // Return whether this is a simple go statement.
907 is_simple(Function_type*) const;
909 // Return whether the thunk function is a constant.
911 is_constant_function() const;
913 // Build the struct to use for a complex case.
915 build_struct(Function_type* fntype);
919 build_thunk(Gogo*, const std::string&);
921 // Set the name to use for thunk field N.
923 thunk_field_param(int n, char* buf, size_t buflen);
925 // The function call to be executed in a separate thread (go) or
928 // The type used for a struct to pass to a thunk, if this is not a
930 Struct_type* struct_type_;
935 class Go_statement : public Thunk_statement
938 Go_statement(Call_expression* call, source_location location)
939 : Thunk_statement(STATEMENT_GO, call, location)
944 do_get_backend(Translate_context*);
947 do_dump_statement(Ast_dump_context*) const;
950 // A defer statement.
952 class Defer_statement : public Thunk_statement
955 Defer_statement(Call_expression* call, source_location location)
956 : Thunk_statement(STATEMENT_DEFER, call, location)
961 do_get_backend(Translate_context*);
964 do_dump_statement(Ast_dump_context*) const;
967 // A label statement.
969 class Label_statement : public Statement
972 Label_statement(Label* label, source_location location)
973 : Statement(STATEMENT_LABEL, location),
977 // Return the label itself.
980 { return this->label_; }
984 do_traverse(Traverse*);
987 do_get_backend(Translate_context*);
990 do_dump_statement(Ast_dump_context*) const;
999 class For_statement : public Statement
1002 For_statement(Block* init, Expression* cond, Block* post,
1003 source_location location)
1004 : Statement(STATEMENT_FOR, location),
1005 init_(init), cond_(cond), post_(post), statements_(NULL),
1006 break_label_(NULL), continue_label_(NULL)
1009 // Add the statements.
1011 add_statements(Block* statements)
1013 go_assert(this->statements_ == NULL);
1014 this->statements_ = statements;
1017 // Return the break label for this for statement.
1021 // Return the continue label for this for statement.
1025 // Set the break and continue labels for this statement.
1027 set_break_continue_labels(Unnamed_label* break_label,
1028 Unnamed_label* continue_label);
1032 do_traverse(Traverse*);
1035 do_traverse_assignments(Traverse_assignments*)
1036 { go_unreachable(); }
1039 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1042 do_get_backend(Translate_context*)
1043 { go_unreachable(); }
1046 do_dump_statement(Ast_dump_context*) const;
1049 // The initialization statements. This may be NULL.
1051 // The condition. This may be NULL.
1053 // The statements to run after each iteration. This may be NULL.
1055 // The statements in the loop itself.
1057 // The break label, if needed.
1058 Unnamed_label* break_label_;
1059 // The continue label, if needed.
1060 Unnamed_label* continue_label_;
1063 // A for statement over a range clause.
1065 class For_range_statement : public Statement
1068 For_range_statement(Expression* index_var, Expression* value_var,
1069 Expression* range, source_location location)
1070 : Statement(STATEMENT_FOR_RANGE, location),
1071 index_var_(index_var), value_var_(value_var), range_(range),
1072 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1075 // Add the statements.
1077 add_statements(Block* statements)
1079 go_assert(this->statements_ == NULL);
1080 this->statements_ = statements;
1083 // Return the break label for this for statement.
1087 // Return the continue label for this for statement.
1093 do_traverse(Traverse*);
1096 do_traverse_assignments(Traverse_assignments*)
1097 { go_unreachable(); }
1100 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1103 do_get_backend(Translate_context*)
1104 { go_unreachable(); }
1107 do_dump_statement(Ast_dump_context*) const;
1111 make_range_ref(Named_object*, Temporary_statement*, source_location);
1114 call_builtin(Gogo*, const char* funcname, Expression* arg, source_location);
1117 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1118 Temporary_statement*, Temporary_statement*,
1119 Block**, Expression**, Block**, Block**);
1122 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1123 Temporary_statement*, Temporary_statement*,
1124 Block**, Expression**, Block**, Block**);
1127 lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1128 Temporary_statement*, Temporary_statement*,
1129 Block**, Expression**, Block**, Block**);
1132 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1133 Temporary_statement*, Temporary_statement*,
1134 Temporary_statement*, Block**, Expression**, Block**,
1137 // The variable which is set to the index value.
1138 Expression* index_var_;
1139 // The variable which is set to the element value. This may be
1141 Expression* value_var_;
1142 // The expression we are ranging over.
1144 // The statements in the block.
1146 // The break label, if needed.
1147 Unnamed_label* break_label_;
1148 // The continue label, if needed.
1149 Unnamed_label* continue_label_;
1152 // Class Case_clauses holds the clauses of a switch statement. This
1153 // is built by the parser.
1162 // Add a new clause. CASES is a list of case expressions; it may be
1163 // NULL. IS_DEFAULT is true if this is the default case.
1164 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1165 // after the statements the case clause should fall through to the
1168 add(Expression_list* cases, bool is_default, Block* statements,
1169 bool is_fallthrough, source_location location)
1171 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1172 is_fallthrough, location));
1175 // Return whether there are no clauses.
1178 { return this->clauses_.empty(); }
1180 // Traverse the case clauses.
1182 traverse(Traverse*);
1184 // Lower for a nonconstant switch.
1186 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1188 // Determine types of expressions. The Type parameter is the type
1189 // of the switch value.
1191 determine_types(Type*);
1193 // Check types. The Type parameter is the type of the switch value.
1197 // Return true if all the clauses are constant values.
1199 is_constant() const;
1201 // Return true if these clauses may fall through to the statements
1202 // following the switch statement.
1204 may_fall_through() const;
1206 // Return the body of a SWITCH_EXPR when all the clauses are
1209 get_backend(Translate_context*, Unnamed_label* break_label,
1210 std::vector<std::vector<Bexpression*> >* all_cases,
1211 std::vector<Bstatement*>* all_statements) const;
1213 // Dump the AST representation to a dump context.
1215 dump_clauses(Ast_dump_context*) const;
1218 // For a constant switch we need to keep a record of constants we
1219 // have already seen.
1220 class Hash_integer_value;
1221 class Eq_integer_value;
1222 typedef Unordered_set_hash(Expression*, Hash_integer_value,
1223 Eq_integer_value) Case_constants;
1230 : cases_(NULL), statements_(NULL), is_default_(false),
1231 is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1234 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1235 bool is_fallthrough, source_location location)
1236 : cases_(cases), statements_(statements), is_default_(is_default),
1237 is_fallthrough_(is_fallthrough), location_(location)
1240 // Whether this clause falls through to the next clause.
1242 is_fallthrough() const
1243 { return this->is_fallthrough_; }
1245 // Whether this is the default.
1248 { return this->is_default_; }
1250 // The location of this clause.
1253 { return this->location_; }
1257 traverse(Traverse*);
1259 // Lower for a nonconstant switch.
1261 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1265 determine_types(Type*);
1271 // Return true if all the case expressions are constant.
1273 is_constant() const;
1275 // Return true if this clause may fall through to execute the
1276 // statements following the switch statement. This is not the
1277 // same as whether this clause falls through to the next clause.
1279 may_fall_through() const;
1281 // Convert the case values and statements to the backend
1284 get_backend(Translate_context*, Unnamed_label* break_label,
1285 Case_constants*, std::vector<Bexpression*>* cases) const;
1287 // Dump the AST representation to a dump context.
1289 dump_clause(Ast_dump_context*) const;
1292 // The list of case expressions.
1293 Expression_list* cases_;
1294 // The statements to execute.
1296 // Whether this is the default case.
1298 // Whether this falls through after the statements.
1299 bool is_fallthrough_;
1300 // The location of this case clause.
1301 source_location location_;
1304 friend class Case_clause;
1306 // The type of the list of clauses.
1307 typedef std::vector<Case_clause> Clauses;
1309 // All the case clauses.
1313 // A switch statement.
1315 class Switch_statement : public Statement
1318 Switch_statement(Expression* val, source_location location)
1319 : Statement(STATEMENT_SWITCH, location),
1320 val_(val), clauses_(NULL), break_label_(NULL)
1325 add_clauses(Case_clauses* clauses)
1327 go_assert(this->clauses_ == NULL);
1328 this->clauses_ = clauses;
1331 // Return the break label for this switch statement.
1337 do_traverse(Traverse*);
1340 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1343 do_get_backend(Translate_context*)
1344 { go_unreachable(); }
1347 do_dump_statement(Ast_dump_context*) const;
1350 // The value to switch on. This may be NULL.
1352 // The case clauses.
1353 Case_clauses* clauses_;
1354 // The break label, if needed.
1355 Unnamed_label* break_label_;
1358 // Class Type_case_clauses holds the clauses of a type switch
1359 // statement. This is built by the parser.
1361 class Type_case_clauses
1368 // Add a new clause. TYPE is the type for this clause; it may be
1369 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1370 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1371 // if this is the default case. STATEMENTS is a block of
1372 // statements; it may be NULL.
1374 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1375 source_location location)
1377 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1378 statements, location));
1381 // Return whether there are no clauses.
1384 { return this->clauses_.empty(); }
1386 // Traverse the type case clauses.
1388 traverse(Traverse*);
1390 // Check for duplicates.
1392 check_duplicates() const;
1394 // Lower to if and goto statements.
1396 lower(Block*, Temporary_statement* descriptor_temp,
1397 Unnamed_label* break_label) const;
1399 // Dump the AST representation to a dump context.
1401 dump_clauses(Ast_dump_context*) const;
1404 // One type case clause.
1405 class Type_case_clause
1409 : type_(NULL), statements_(NULL), is_default_(false),
1410 location_(UNKNOWN_LOCATION)
1413 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1414 Block* statements, source_location location)
1415 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1416 is_default_(is_default), location_(location)
1422 { return this->type_; }
1424 // Whether this is the default.
1427 { return this->is_default_; }
1429 // The location of this type clause.
1432 { return this->location_; }
1436 traverse(Traverse*);
1438 // Lower to if and goto statements.
1440 lower(Block*, Temporary_statement* descriptor_temp,
1441 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1443 // Dump the AST representation to a dump context.
1445 dump_clause(Ast_dump_context*) const;
1448 // The type for this type clause.
1450 // The statements to execute.
1452 // Whether this falls through--this is true for "case T1, T2".
1453 bool is_fallthrough_;
1454 // Whether this is the default case.
1456 // The location of this type case clause.
1457 source_location location_;
1460 friend class Type_case_clause;
1462 // The type of the list of type clauses.
1463 typedef std::vector<Type_case_clause> Type_clauses;
1465 // All the type case clauses.
1466 Type_clauses clauses_;
1469 // A type switch statement.
1471 class Type_switch_statement : public Statement
1474 Type_switch_statement(Named_object* var, Expression* expr,
1475 source_location location)
1476 : Statement(STATEMENT_TYPE_SWITCH, location),
1477 var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1478 { go_assert(var == NULL || expr == NULL); }
1482 add_clauses(Type_case_clauses* clauses)
1484 go_assert(this->clauses_ == NULL);
1485 this->clauses_ = clauses;
1488 // Return the break label for this type switch statement.
1494 do_traverse(Traverse*);
1497 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1500 do_get_backend(Translate_context*)
1501 { go_unreachable(); }
1504 do_dump_statement(Ast_dump_context*) const;
1507 // The variable holding the value we are switching on.
1509 // The expression we are switching on if there is no variable.
1511 // The type case clauses.
1512 Type_case_clauses* clauses_;
1513 // The break label, if needed.
1514 Unnamed_label* break_label_;
1517 #endif // !defined(GO_STATEMENTS_H)