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, 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*, Location);
144 // Make an assignment statement.
146 make_assignment(Expression*, Expression*, 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*, 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*, 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, Location);
167 // Make an assignment from a nonblocking receive to a pair of
170 make_tuple_receive_assignment(Expression* val, Expression* closed,
171 Expression* channel, Location);
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. IS_IGNORED is
180 // true if the value is being explicitly ignored, as in an
183 make_statement(Expression*, bool is_ignored);
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*, 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, Location);
202 // Make a defer statement.
204 make_defer_statement(Call_expression* call, Location);
206 // Make a return statement.
207 static Return_statement*
208 make_return_statement(Expression_list*, Location);
210 // Make a break statement.
212 make_break_statement(Unnamed_label* label, Location);
214 // Make a continue statement.
216 make_continue_statement(Unnamed_label* label, Location);
218 // Make a goto statement.
220 make_goto_statement(Label* label, Location);
222 // Make a goto statement to an unnamed label.
224 make_goto_unnamed_statement(Unnamed_label* label, Location);
226 // Make a label statement--where the label is defined.
228 make_label_statement(Label* label, 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, Location);
243 // Make a type switch statement.
244 static Type_switch_statement*
245 make_type_switch_statement(Named_object* var, Expression*, Location);
247 // Make a send statement.
248 static Send_statement*
249 make_send_statement(Expression* channel, Expression* val, Location);
251 // Make a select statement.
252 static Select_statement*
253 make_select_statement(Location);
255 // Make a for statement.
256 static For_statement*
257 make_for_statement(Block* init, Expression* cond, Block* post,
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, 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(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.
478 // A statement which creates and initializes a temporary variable.
480 class Temporary_statement : public Statement
483 Temporary_statement(Type* type, Expression* init, Location location)
484 : Statement(STATEMENT_TEMPORARY, location),
485 type_(type), init_(init), bvariable_(NULL), are_hidden_fields_ok_(false),
486 is_address_taken_(false)
489 // Return the type of the temporary variable.
493 // Note that it is OK for this statement to set hidden fields.
495 set_hidden_fields_are_ok()
496 { this->are_hidden_fields_ok_ = true; }
498 // Record that something takes the address of this temporary
501 set_is_address_taken()
502 { this->is_address_taken_ = true; }
504 // Return the temporary variable. This should not be called until
505 // after the statement itself has been converted.
507 get_backend_variable(Translate_context*) const;
511 do_traverse(Traverse*);
514 do_traverse_assignments(Traverse_assignments*);
517 do_determine_types();
520 do_check_types(Gogo*);
523 do_get_backend(Translate_context*);
526 do_dump_statement(Ast_dump_context*) const;
529 // The type of the temporary variable.
531 // The initial value of the temporary variable. This may be NULL.
533 // The backend representation of the temporary variable.
534 Bvariable* bvariable_;
535 // True if this statement may set hidden fields when assigning the
536 // value to the temporary. This is used for generated method stubs.
537 bool are_hidden_fields_ok_;
538 // True if something takes the address of this temporary variable.
539 bool is_address_taken_;
542 // A variable declaration. This marks the point in the code where a
543 // variable is declared. The Variable is also attached to a Block.
545 class Variable_declaration_statement : public Statement
548 Variable_declaration_statement(Named_object* var);
550 // The variable being declared.
553 { return this->var_; }
557 do_traverse(Traverse*);
560 do_traverse_assignments(Traverse_assignments*);
563 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
566 do_get_backend(Translate_context*);
569 do_dump_statement(Ast_dump_context*) const;
575 // A return statement.
577 class Return_statement : public Statement
580 Return_statement(Expression_list* vals, Location location)
581 : Statement(STATEMENT_RETURN, location),
582 vals_(vals), are_hidden_fields_ok_(false), is_lowered_(false)
585 // The list of values being returned. This may be NULL.
586 const Expression_list*
588 { return this->vals_; }
590 // Note that it is OK for this return statement to set hidden
593 set_hidden_fields_are_ok()
594 { this->are_hidden_fields_ok_ = true; }
598 do_traverse(Traverse* traverse)
599 { return this->traverse_expression_list(traverse, this->vals_); }
602 do_traverse_assignments(Traverse_assignments*);
605 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
608 do_may_fall_through() const
612 do_get_backend(Translate_context*);
615 do_dump_statement(Ast_dump_context*) const;
618 // Return values. This may be NULL.
619 Expression_list* vals_;
620 // True if this statement may pass hidden fields in the return
621 // value. This is used for generated method stubs.
622 bool are_hidden_fields_ok_;
623 // True if this statement has been lowered.
629 class Send_statement : public Statement
632 Send_statement(Expression* channel, Expression* val,
634 : Statement(STATEMENT_SEND, location),
635 channel_(channel), val_(val)
640 do_traverse(Traverse* traverse);
643 do_determine_types();
646 do_check_types(Gogo*);
649 do_get_backend(Translate_context*);
652 do_dump_statement(Ast_dump_context*) const;
655 // The channel on which to send the value.
656 Expression* channel_;
657 // The value to send.
661 // Select_clauses holds the clauses of a select statement. This is
662 // built by the parser.
671 // Add a new clause. IS_SEND is true if this is a send clause,
672 // false for a receive clause. For a send clause CHANNEL is the
673 // channel and VAL is the value to send. For a receive clause
674 // CHANNEL is the channel, VAL is either NULL or a Var_expression
675 // for the variable to set, and CLOSED is either NULL or a
676 // Var_expression to set to whether the channel is closed. If VAL
677 // is NULL, VAR may be a variable to be initialized with the
678 // received value, and CLOSEDVAR ma be a variable to be initialized
679 // with whether the channel is closed. IS_DEFAULT is true if this
680 // is the default clause. STATEMENTS is the list of statements to
683 add(bool is_send, Expression* channel, Expression* val, Expression* closed,
684 Named_object* var, Named_object* closedvar, bool is_default,
685 Block* statements, Location location)
687 int index = static_cast<int>(this->clauses_.size());
688 this->clauses_.push_back(Select_clause(index, is_send, channel, val,
689 closed, var, closedvar, is_default,
690 statements, location));
695 { return this->clauses_.size(); }
697 // Traverse the select clauses.
703 lower(Gogo*, Named_object*, Block*, Temporary_statement*);
713 // Whether the select clauses may fall through to the statement
714 // which follows the overall select statement.
716 may_fall_through() const;
718 // Convert to the backend representation.
720 get_backend(Translate_context*, Temporary_statement* sel,
721 Unnamed_label* break_label, Location);
723 // Dump AST representation.
725 dump_clauses(Ast_dump_context*) const;
733 : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
734 closedvar_(NULL), statements_(NULL), is_send_(false),
738 Select_clause(int index, bool is_send, Expression* channel,
739 Expression* val, Expression* closed, Named_object* var,
740 Named_object* closedvar, bool is_default, Block* statements,
742 : index_(index), channel_(channel), val_(val), closed_(closed),
743 var_(var), closedvar_(closedvar), statements_(statements),
744 location_(location), is_send_(is_send), is_default_(is_default),
746 { go_assert(is_default ? channel == NULL : channel != NULL); }
748 // Return the index of this clause.
751 { return this->index_; }
753 // Traverse the select clause.
759 lower(Gogo*, Named_object*, Block*, Temporary_statement*);
769 // Return true if this is the default clause.
772 { return this->is_default_; }
774 // Return the channel. This will return NULL for the default
778 { return this->channel_; }
780 // Return true for a send, false for a receive.
784 go_assert(!this->is_default_);
785 return this->is_send_;
788 // Return the statements.
791 { return this->statements_; }
793 // Return the location.
796 { return this->location_; }
798 // Whether this clause may fall through to the statement which
799 // follows the overall select statement.
801 may_fall_through() const;
803 // Convert the statements to the backend representation.
805 get_statements_backend(Translate_context*);
807 // Dump AST representation.
809 dump_clause(Ast_dump_context*) const;
813 lower_default(Block*, Expression*, Expression*);
816 lower_send(Block*, Expression*, Expression*, Expression*);
819 lower_recv(Gogo*, Named_object*, Block*, Expression*, Expression*,
822 // The index of this case in the generated switch statement.
825 Expression* channel_;
826 // The value to send or the lvalue to receive into.
828 // The lvalue to set to whether the channel is closed on a
831 // The variable to initialize, for "case a := <-ch".
833 // The variable to initialize to whether the channel is closed,
834 // for "case a, c := <-ch".
835 Named_object* closedvar_;
836 // The statements to execute.
838 // The location of this clause.
840 // Whether this is a send or a receive.
842 // Whether this is the default.
844 // Whether this has been lowered.
848 typedef std::vector<Select_clause> Clauses;
853 // A select statement.
855 class Select_statement : public Statement
858 Select_statement(Location location)
859 : Statement(STATEMENT_SELECT, location),
860 clauses_(NULL), sel_(NULL), break_label_(NULL), is_lowered_(false)
865 add_clauses(Select_clauses* clauses)
867 go_assert(this->clauses_ == NULL);
868 this->clauses_ = clauses;
871 // Return the break label for this select statement.
877 do_traverse(Traverse* traverse)
878 { return this->clauses_->traverse(traverse); }
881 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
885 { this->clauses_->determine_types(); }
888 do_check_types(Gogo*)
889 { this->clauses_->check_types(); }
892 do_may_fall_through() const
893 { return this->clauses_->may_fall_through(); }
896 do_get_backend(Translate_context*);
899 do_dump_statement(Ast_dump_context*) const;
902 // The select clauses.
903 Select_clauses* clauses_;
904 // A temporary which holds the select structure we build up at runtime.
905 Temporary_statement* sel_;
907 Unnamed_label* break_label_;
908 // Whether this statement has been lowered.
912 // A statement which requires a thunk: go or defer.
914 class Thunk_statement : public Statement
917 Thunk_statement(Statement_classification, Call_expression*,
920 // Return the call expression.
923 { return this->call_; }
925 // Simplify a go or defer statement so that it only uses a single
928 simplify_statement(Gogo*, Named_object*, Block*);
932 do_traverse(Traverse* traverse);
935 do_traverse_assignments(Traverse_assignments*);
938 do_determine_types();
941 do_check_types(Gogo*);
943 // Return the function and argument for the call.
945 get_fn_and_arg(Expression** pfn, Expression** parg);
948 // Return whether this is a simple go statement.
950 is_simple(Function_type*) const;
952 // Return whether the thunk function is a constant.
954 is_constant_function() const;
956 // Build the struct to use for a complex case.
958 build_struct(Function_type* fntype);
962 build_thunk(Gogo*, const std::string&);
964 // Set the name to use for thunk field N.
966 thunk_field_param(int n, char* buf, size_t buflen);
968 // The function call to be executed in a separate thread (go) or
971 // The type used for a struct to pass to a thunk, if this is not a
973 Struct_type* struct_type_;
978 class Go_statement : public Thunk_statement
981 Go_statement(Call_expression* call, Location location)
982 : Thunk_statement(STATEMENT_GO, call, location)
987 do_get_backend(Translate_context*);
990 do_dump_statement(Ast_dump_context*) const;
993 // A defer statement.
995 class Defer_statement : public Thunk_statement
998 Defer_statement(Call_expression* call, Location location)
999 : Thunk_statement(STATEMENT_DEFER, call, location)
1004 do_get_backend(Translate_context*);
1007 do_dump_statement(Ast_dump_context*) const;
1010 // A label statement.
1012 class Label_statement : public Statement
1015 Label_statement(Label* label, Location location)
1016 : Statement(STATEMENT_LABEL, location),
1020 // Return the label itself.
1023 { return this->label_; }
1027 do_traverse(Traverse*);
1030 do_get_backend(Translate_context*);
1033 do_dump_statement(Ast_dump_context*) const;
1042 class For_statement : public Statement
1045 For_statement(Block* init, Expression* cond, Block* post,
1047 : Statement(STATEMENT_FOR, location),
1048 init_(init), cond_(cond), post_(post), statements_(NULL),
1049 break_label_(NULL), continue_label_(NULL)
1052 // Add the statements.
1054 add_statements(Block* statements)
1056 go_assert(this->statements_ == NULL);
1057 this->statements_ = statements;
1060 // Return the break label for this for statement.
1064 // Return the continue label for this for statement.
1068 // Set the break and continue labels for this statement.
1070 set_break_continue_labels(Unnamed_label* break_label,
1071 Unnamed_label* continue_label);
1075 do_traverse(Traverse*);
1078 do_traverse_assignments(Traverse_assignments*)
1079 { go_unreachable(); }
1082 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1085 do_get_backend(Translate_context*)
1086 { go_unreachable(); }
1089 do_dump_statement(Ast_dump_context*) const;
1092 // The initialization statements. This may be NULL.
1094 // The condition. This may be NULL.
1096 // The statements to run after each iteration. This may be NULL.
1098 // The statements in the loop itself.
1100 // The break label, if needed.
1101 Unnamed_label* break_label_;
1102 // The continue label, if needed.
1103 Unnamed_label* continue_label_;
1106 // A for statement over a range clause.
1108 class For_range_statement : public Statement
1111 For_range_statement(Expression* index_var, Expression* value_var,
1112 Expression* range, Location location)
1113 : Statement(STATEMENT_FOR_RANGE, location),
1114 index_var_(index_var), value_var_(value_var), range_(range),
1115 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1118 // Add the statements.
1120 add_statements(Block* statements)
1122 go_assert(this->statements_ == NULL);
1123 this->statements_ = statements;
1126 // Return the break label for this for statement.
1130 // Return the continue label for this for statement.
1136 do_traverse(Traverse*);
1139 do_traverse_assignments(Traverse_assignments*)
1140 { go_unreachable(); }
1143 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1146 do_get_backend(Translate_context*)
1147 { go_unreachable(); }
1150 do_dump_statement(Ast_dump_context*) const;
1154 make_range_ref(Named_object*, Temporary_statement*, Location);
1157 call_builtin(Gogo*, const char* funcname, Expression* arg, Location);
1160 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1161 Temporary_statement*, Temporary_statement*,
1162 Block**, Expression**, Block**, Block**);
1165 lower_range_slice(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1166 Temporary_statement*, Temporary_statement*,
1167 Block**, Expression**, Block**, Block**);
1170 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1171 Temporary_statement*, Temporary_statement*,
1172 Block**, Expression**, Block**, Block**);
1175 lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1176 Temporary_statement*, Temporary_statement*,
1177 Block**, Expression**, Block**, Block**);
1180 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1181 Temporary_statement*, Temporary_statement*,
1182 Temporary_statement*, Block**, Expression**, Block**,
1185 // The variable which is set to the index value.
1186 Expression* index_var_;
1187 // The variable which is set to the element value. This may be
1189 Expression* value_var_;
1190 // The expression we are ranging over.
1192 // The statements in the block.
1194 // The break label, if needed.
1195 Unnamed_label* break_label_;
1196 // The continue label, if needed.
1197 Unnamed_label* continue_label_;
1200 // Class Case_clauses holds the clauses of a switch statement. This
1201 // is built by the parser.
1210 // Add a new clause. CASES is a list of case expressions; it may be
1211 // NULL. IS_DEFAULT is true if this is the default case.
1212 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1213 // after the statements the case clause should fall through to the
1216 add(Expression_list* cases, bool is_default, Block* statements,
1217 bool is_fallthrough, Location location)
1219 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1220 is_fallthrough, location));
1223 // Return whether there are no clauses.
1226 { return this->clauses_.empty(); }
1228 // Traverse the case clauses.
1230 traverse(Traverse*);
1232 // Lower for a nonconstant switch.
1234 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1236 // Determine types of expressions. The Type parameter is the type
1237 // of the switch value.
1239 determine_types(Type*);
1241 // Check types. The Type parameter is the type of the switch value.
1245 // Return true if all the clauses are constant values.
1247 is_constant() const;
1249 // Return true if these clauses may fall through to the statements
1250 // following the switch statement.
1252 may_fall_through() const;
1254 // Return the body of a SWITCH_EXPR when all the clauses are
1257 get_backend(Translate_context*, Unnamed_label* break_label,
1258 std::vector<std::vector<Bexpression*> >* all_cases,
1259 std::vector<Bstatement*>* all_statements) const;
1261 // Dump the AST representation to a dump context.
1263 dump_clauses(Ast_dump_context*) const;
1266 // For a constant switch we need to keep a record of constants we
1267 // have already seen.
1268 class Hash_integer_value;
1269 class Eq_integer_value;
1270 typedef Unordered_set_hash(Expression*, Hash_integer_value,
1271 Eq_integer_value) Case_constants;
1278 : cases_(NULL), statements_(NULL), is_default_(false),
1279 is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1282 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1283 bool is_fallthrough, Location location)
1284 : cases_(cases), statements_(statements), is_default_(is_default),
1285 is_fallthrough_(is_fallthrough), location_(location)
1288 // Whether this clause falls through to the next clause.
1290 is_fallthrough() const
1291 { return this->is_fallthrough_; }
1293 // Whether this is the default.
1296 { return this->is_default_; }
1298 // The location of this clause.
1301 { return this->location_; }
1305 traverse(Traverse*);
1307 // Lower for a nonconstant switch.
1309 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1313 determine_types(Type*);
1319 // Return true if all the case expressions are constant.
1321 is_constant() const;
1323 // Return true if this clause may fall through to execute the
1324 // statements following the switch statement. This is not the
1325 // same as whether this clause falls through to the next clause.
1327 may_fall_through() const;
1329 // Convert the case values and statements to the backend
1332 get_backend(Translate_context*, Unnamed_label* break_label,
1333 Case_constants*, std::vector<Bexpression*>* cases) const;
1335 // Dump the AST representation to a dump context.
1337 dump_clause(Ast_dump_context*) const;
1340 // The list of case expressions.
1341 Expression_list* cases_;
1342 // The statements to execute.
1344 // Whether this is the default case.
1346 // Whether this falls through after the statements.
1347 bool is_fallthrough_;
1348 // The location of this case clause.
1352 friend class Case_clause;
1354 // The type of the list of clauses.
1355 typedef std::vector<Case_clause> Clauses;
1357 // All the case clauses.
1361 // A switch statement.
1363 class Switch_statement : public Statement
1366 Switch_statement(Expression* val, Location location)
1367 : Statement(STATEMENT_SWITCH, location),
1368 val_(val), clauses_(NULL), break_label_(NULL)
1373 add_clauses(Case_clauses* clauses)
1375 go_assert(this->clauses_ == NULL);
1376 this->clauses_ = clauses;
1379 // Return the break label for this switch statement.
1385 do_traverse(Traverse*);
1388 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1391 do_get_backend(Translate_context*)
1392 { go_unreachable(); }
1395 do_dump_statement(Ast_dump_context*) const;
1398 // The value to switch on. This may be NULL.
1400 // The case clauses.
1401 Case_clauses* clauses_;
1402 // The break label, if needed.
1403 Unnamed_label* break_label_;
1406 // Class Type_case_clauses holds the clauses of a type switch
1407 // statement. This is built by the parser.
1409 class Type_case_clauses
1416 // Add a new clause. TYPE is the type for this clause; it may be
1417 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1418 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1419 // if this is the default case. STATEMENTS is a block of
1420 // statements; it may be NULL.
1422 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1425 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1426 statements, location));
1429 // Return whether there are no clauses.
1432 { return this->clauses_.empty(); }
1434 // Traverse the type case clauses.
1436 traverse(Traverse*);
1438 // Check for duplicates.
1440 check_duplicates() const;
1442 // Lower to if and goto statements.
1444 lower(Block*, Temporary_statement* descriptor_temp,
1445 Unnamed_label* break_label) const;
1447 // Dump the AST representation to a dump context.
1449 dump_clauses(Ast_dump_context*) const;
1452 // One type case clause.
1453 class Type_case_clause
1457 : type_(NULL), statements_(NULL), is_default_(false),
1458 location_(UNKNOWN_LOCATION)
1461 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1462 Block* statements, Location location)
1463 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1464 is_default_(is_default), location_(location)
1470 { return this->type_; }
1472 // Whether this is the default.
1475 { return this->is_default_; }
1477 // The location of this type clause.
1480 { return this->location_; }
1484 traverse(Traverse*);
1486 // Lower to if and goto statements.
1488 lower(Block*, Temporary_statement* descriptor_temp,
1489 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1491 // Dump the AST representation to a dump context.
1493 dump_clause(Ast_dump_context*) const;
1496 // The type for this type clause.
1498 // The statements to execute.
1500 // Whether this falls through--this is true for "case T1, T2".
1501 bool is_fallthrough_;
1502 // Whether this is the default case.
1504 // The location of this type case clause.
1508 friend class Type_case_clause;
1510 // The type of the list of type clauses.
1511 typedef std::vector<Type_case_clause> Type_clauses;
1513 // All the type case clauses.
1514 Type_clauses clauses_;
1517 // A type switch statement.
1519 class Type_switch_statement : public Statement
1522 Type_switch_statement(Named_object* var, Expression* expr,
1524 : Statement(STATEMENT_TYPE_SWITCH, location),
1525 var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1526 { go_assert(var == NULL || expr == NULL); }
1530 add_clauses(Type_case_clauses* clauses)
1532 go_assert(this->clauses_ == NULL);
1533 this->clauses_ = clauses;
1536 // Return the break label for this type switch statement.
1542 do_traverse(Traverse*);
1545 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1548 do_get_backend(Translate_context*)
1549 { go_unreachable(); }
1552 do_dump_statement(Ast_dump_context*) const;
1555 // The variable holding the value we are switching on.
1557 // The expression we are switching on if there is no variable.
1559 // The type case clauses.
1560 Type_case_clauses* clauses_;
1561 // The break label, if needed.
1562 Unnamed_label* break_label_;
1565 #endif // !defined(GO_STATEMENTS_H)