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;
26 class Select_statement;
30 class Translate_context;
32 class Expression_list;
34 class Call_expression;
35 class Map_index_expression;
36 class Receive_expression;
38 class Type_case_clauses;
40 class Typed_identifier_list;
42 // This class is used to traverse assignments made by a statement
43 // which makes assignments.
45 class Traverse_assignments
48 Traverse_assignments()
51 virtual ~Traverse_assignments()
54 // This is called for a variable initialization.
56 initialize_variable(Named_object*) = 0;
58 // This is called for each assignment made by the statement. PLHS
59 // points to the left hand side, and PRHS points to the right hand
60 // side. PRHS may be NULL if there is no associated expression, as
61 // in the bool set by a non-blocking receive.
63 assignment(Expression** plhs, Expression** prhs) = 0;
65 // This is called for each expression which is not passed to the
66 // assignment function. This is used for some of the statements
67 // which assign two values, for which there is no expression which
68 // describes the value. For ++ and -- the value is passed to both
69 // the assignment method and the rhs method. IS_STORED is true if
70 // this value is being stored directly. It is false if the value is
71 // computed but not stored. IS_LOCAL is true if the value is being
72 // stored in a local variable or this is being called by a return
75 value(Expression**, bool is_stored, bool is_local) = 0;
78 // A single statement.
83 // The types of statements.
84 enum Statement_classification
87 STATEMENT_VARIABLE_DECLARATION,
95 STATEMENT_BREAK_OR_CONTINUE,
97 STATEMENT_GOTO_UNNAMED,
99 STATEMENT_UNNAMED_LABEL,
101 STATEMENT_CONSTANT_SWITCH,
104 // These statements types are created by the parser, but they
105 // disappear during the lowering pass.
106 STATEMENT_ASSIGNMENT_OPERATION,
107 STATEMENT_TUPLE_ASSIGNMENT,
108 STATEMENT_TUPLE_MAP_ASSIGNMENT,
109 STATEMENT_MAP_ASSIGNMENT,
110 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT,
111 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT,
116 STATEMENT_TYPE_SWITCH
119 Statement(Statement_classification, source_location);
121 virtual ~Statement();
123 // Make a variable declaration.
125 make_variable_declaration(Named_object*);
127 // Make a statement which creates a temporary variable and
128 // initializes it to an expression. The block is used if the
129 // temporary variable has to be explicitly destroyed; the variable
130 // must still be added to the block. References to the temporary
131 // variable may be constructed using make_temporary_reference.
132 // Either the type or the initialization expression may be NULL, but
134 static Temporary_statement*
135 make_temporary(Type*, Expression*, source_location);
137 // Make an assignment statement.
139 make_assignment(Expression*, Expression*, source_location);
141 // Make an assignment operation (+=, etc.).
143 make_assignment_operation(Operator, Expression*, Expression*,
146 // Make a tuple assignment statement.
148 make_tuple_assignment(Expression_list*, Expression_list*, source_location);
150 // Make an assignment from a map index to a pair of variables.
152 make_tuple_map_assignment(Expression* val, Expression* present,
153 Expression*, source_location);
155 // Make a statement which assigns a pair of values to a map.
157 make_map_assignment(Expression*, Expression* val,
158 Expression* should_set, source_location);
160 // Make an assignment from a nonblocking receive to a pair of
163 make_tuple_receive_assignment(Expression* val, Expression* success,
164 Expression* channel, source_location);
166 // Make an assignment from a type guard to a pair of variables.
168 make_tuple_type_guard_assignment(Expression* val, Expression* ok,
169 Expression* expr, Type* type,
172 // Make an expression statement from an Expression.
174 make_statement(Expression*);
176 // Make a block statement from a Block. This is an embedded list of
177 // statements which may also include variable definitions.
179 make_block_statement(Block*, source_location);
181 // Make an increment statement.
183 make_inc_statement(Expression*);
185 // Make a decrement statement.
187 make_dec_statement(Expression*);
189 // Make a go statement.
191 make_go_statement(Call_expression* call, source_location);
193 // Make a defer statement.
195 make_defer_statement(Call_expression* call, source_location);
197 // Make a return statement.
199 make_return_statement(const Typed_identifier_list*, Expression_list*,
202 // Make a break statement.
204 make_break_statement(Unnamed_label* label, source_location);
206 // Make a continue statement.
208 make_continue_statement(Unnamed_label* label, source_location);
210 // Make a goto statement.
212 make_goto_statement(Label* label, source_location);
214 // Make a goto statement to an unnamed label.
216 make_goto_unnamed_statement(Unnamed_label* label, source_location);
218 // Make a label statement--where the label is defined.
220 make_label_statement(Label* label, source_location);
222 // Make an unnamed label statement--where the label is defined.
224 make_unnamed_label_statement(Unnamed_label* label);
226 // Make an if statement.
228 make_if_statement(Expression* cond, Block* then_block, Block* else_block,
231 // Make a switch statement.
232 static Switch_statement*
233 make_switch_statement(Expression* switch_val, source_location);
235 // Make a type switch statement.
236 static Type_switch_statement*
237 make_type_switch_statement(Named_object* var, Expression*, source_location);
239 // Make a select statement.
240 static Select_statement*
241 make_select_statement(source_location);
243 // Make a for statement.
244 static For_statement*
245 make_for_statement(Block* init, Expression* cond, Block* post,
246 source_location location);
248 // Make a for statement with a range clause.
249 static For_range_statement*
250 make_for_range_statement(Expression* index_var, Expression* value_var,
251 Expression* range, source_location);
253 // Return the statement classification.
254 Statement_classification
255 classification() const
256 { return this->classification_; }
258 // Get the statement location.
261 { return this->location_; }
263 // Traverse the tree.
265 traverse(Block*, size_t* index, Traverse*);
267 // Traverse the contents of this statement--the expressions and
268 // statements which it contains.
270 traverse_contents(Traverse*);
272 // If this statement assigns some values, it calls a function for
273 // each value to which this statement assigns a value, and returns
274 // true. If this statement does not assign any values, it returns
277 traverse_assignments(Traverse_assignments* tassign);
279 // Lower a statement. This is called immediately after parsing to
280 // simplify statements for further processing. It returns the same
281 // Statement or a new one. BLOCK is the block containing this
284 lower(Gogo* gogo, Block* block)
285 { return this->do_lower(gogo, block); }
287 // Set type information for unnamed constants.
291 // Check types in a statement. This simply checks that any
292 // expressions used by the statement have the right type.
294 check_types(Gogo* gogo)
295 { this->do_check_types(gogo); }
297 // Return whether this is a block statement.
299 is_block_statement() const
300 { return this->classification_ == STATEMENT_BLOCK; }
302 // If this is a variable declaration statement, return it.
303 // Otherwise return NULL.
304 Variable_declaration_statement*
305 variable_declaration_statement()
307 return this->convert<Variable_declaration_statement,
308 STATEMENT_VARIABLE_DECLARATION>();
311 // If this is a return statement, return it. Otherwise return NULL.
314 { return this->convert<Return_statement, STATEMENT_RETURN>(); }
316 // If this is a thunk statement (a go or defer statement), return
317 // it. Otherwise return NULL.
321 // If this is a label statement, return it. Otherwise return NULL.
324 { return this->convert<Label_statement, STATEMENT_LABEL>(); }
326 // If this is a for statement, return it. Otherwise return NULL.
329 { return this->convert<For_statement, STATEMENT_FOR>(); }
331 // If this is a for statement over a range clause, return it.
332 // Otherwise return NULL.
334 for_range_statement()
335 { return this->convert<For_range_statement, STATEMENT_FOR_RANGE>(); }
337 // If this is a switch statement, return it. Otherwise return NULL.
340 { return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
342 // If this is a type switch statement, return it. Otherwise return
344 Type_switch_statement*
345 type_switch_statement()
346 { return this->convert<Type_switch_statement, STATEMENT_TYPE_SWITCH>(); }
348 // If this is a select statement, return it. Otherwise return NULL.
351 { return this->convert<Select_statement, STATEMENT_SELECT>(); }
353 // Return true if this statement may fall through--if after
354 // executing this statement we may go on to execute the following
355 // statement, if any.
357 may_fall_through() const
358 { return this->do_may_fall_through(); }
360 // Return the tree for a statement. BLOCK is the enclosing block.
362 get_tree(Translate_context*);
365 // Implemented by child class: traverse the tree.
367 do_traverse(Traverse*) = 0;
369 // Implemented by child class: traverse assignments. Any statement
370 // which includes an assignment should implement this.
372 do_traverse_assignments(Traverse_assignments*)
375 // Implemented by the child class: lower this statement to a simpler
378 do_lower(Gogo*, Block*)
381 // Implemented by child class: set type information for unnamed
382 // constants. Any statement which includes an expression needs to
388 // Implemented by child class: check types of expressions used in a
391 do_check_types(Gogo*)
394 // Implemented by child class: return true if this statement may
397 do_may_fall_through() const
400 // Implemented by child class: return a tree.
402 do_get_tree(Translate_context*) = 0;
404 // Traverse an expression in a statement.
406 traverse_expression(Traverse*, Expression**);
408 // Traverse an expression list in a statement. The Expression_list
411 traverse_expression_list(Traverse*, Expression_list*);
413 // Traverse a type in a statement.
415 traverse_type(Traverse*, Type*);
417 // Build a tree node with one operand, setting the location. The
418 // first operand really has type "enum tree_code", but that enum is
421 build_stmt_1(int tree_code_value, tree);
423 // For children to call when they detect that they are in error.
427 // For children to call to report an error conveniently.
429 report_error(const char*);
431 // For children to return an error statement from lower().
433 make_error_statement(source_location);
436 // Convert to the desired statement classification, or return NULL.
437 // This is a controlled dynamic cast.
438 template<typename Statement_class, Statement_classification sc>
442 return (this->classification_ == sc
443 ? static_cast<Statement_class*>(this)
447 template<typename Statement_class, Statement_classification sc>
448 const Statement_class*
451 return (this->classification_ == sc
452 ? static_cast<const Statement_class*>(this)
456 // The statement classification.
457 Statement_classification classification_;
458 // The location in the input file of the start of this statement.
459 source_location location_;
462 // A statement which creates and initializes a temporary variable.
464 class Temporary_statement : public Statement
467 Temporary_statement(Type* type, Expression* init, source_location location)
468 : Statement(STATEMENT_TEMPORARY, location),
469 type_(type), init_(init), decl_(NULL), is_address_taken_(false)
472 // Return the type of the temporary variable.
476 // Return the initialization expression.
479 { return this->init_; }
481 // Record that something takes the address of this temporary
484 set_is_address_taken()
485 { this->is_address_taken_ = true; }
487 // Return the tree for the temporary variable itself. This should
488 // not be called until after the statement itself has been expanded.
492 gcc_assert(this->decl_ != NULL);
498 do_traverse(Traverse*);
501 do_traverse_assignments(Traverse_assignments*);
504 do_determine_types();
507 do_check_types(Gogo*);
510 do_get_tree(Translate_context*);
513 // The type of the temporary variable.
515 // The initial value of the temporary variable. This may be NULL.
517 // The DECL for the temporary variable.
519 // True if something takes the address of this temporary variable.
520 bool is_address_taken_;
523 // A variable declaration. This marks the point in the code where a
524 // variable is declared. The Variable is also attached to a Block.
526 class Variable_declaration_statement : public Statement
529 Variable_declaration_statement(Named_object* var);
531 // The variable being declared.
534 { return this->var_; }
538 do_traverse(Traverse*);
541 do_traverse_assignments(Traverse_assignments*);
544 do_get_tree(Translate_context*);
550 // A return statement.
552 class Return_statement : public Statement
555 Return_statement(const Typed_identifier_list* results, Expression_list* vals,
556 source_location location)
557 : Statement(STATEMENT_RETURN, location),
558 results_(results), vals_(vals)
561 // The list of values being returned. This may be NULL.
562 const Expression_list*
564 { return this->vals_; }
568 do_traverse(Traverse* traverse)
569 { return this->traverse_expression_list(traverse, this->vals_); }
572 do_traverse_assignments(Traverse_assignments*);
575 do_lower(Gogo*, Block*);
578 do_determine_types();
581 do_check_types(Gogo*);
584 do_may_fall_through() const
588 do_get_tree(Translate_context*);
591 // The result types of the function we are returning from. This is
592 // here because in some of the traversals it is inconvenient to get
594 const Typed_identifier_list* results_;
595 // Return values. This may be NULL.
596 Expression_list* vals_;
599 // Select_clauses holds the clauses of a select statement. This is
600 // built by the parser.
609 // Add a new clause. IS_SEND is true if this is a send clause,
610 // false for a receive clause. For a send clause CHANNEL is the
611 // channel and VAL is the value to send. For a receive clause
612 // CHANNEL is the channel and VAL is either NULL or a Var_expression
613 // for the variable to set; if VAL is NULL, VAR may be a variable
614 // which is initialized with the received value. IS_DEFAULT is true
615 // if this is the default clause. STATEMENTS is the list of
616 // statements to execute.
618 add(bool is_send, Expression* channel, Expression* val, Named_object* var,
619 bool is_default, Block* statements, source_location location)
621 this->clauses_.push_back(Select_clause(is_send, channel, val, var,
622 is_default, statements, location));
625 // Traverse the select clauses.
637 // Whether the select clauses may fall through to the statement
638 // which follows the overall select statement.
640 may_fall_through() const;
642 // Return a tree implementing the select statement.
644 get_tree(Translate_context*, Unnamed_label* break_label, source_location);
652 : channel_(NULL), val_(NULL), var_(NULL), statements_(NULL),
653 is_send_(false), is_default_(false)
656 Select_clause(bool is_send, Expression* channel, Expression* val,
657 Named_object* var, bool is_default, Block* statements,
658 source_location location)
659 : channel_(channel), val_(val), var_(var), statements_(statements),
660 location_(location), is_send_(is_send), is_default_(is_default),
662 { gcc_assert(is_default ? channel == NULL : channel != NULL); }
664 // Traverse the select clause.
676 // Return true if this is the default clause.
679 { return this->is_default_; }
681 // Return the channel. This will return NULL for the default
685 { return this->channel_; }
687 // Return the value. This will return NULL for the default
688 // clause, or for a receive clause for which no value was given.
691 { return this->val_; }
693 // Return the variable to set when a receive clause is also a
694 // variable definition (v := <- ch). This will return NULL for
695 // the default case, or for a send clause, or for a receive clause
696 // which does not define a variable.
699 { return this->var_; }
701 // Return true for a send, false for a receive.
705 gcc_assert(!this->is_default_);
706 return this->is_send_;
709 // Return the statements.
712 { return this->statements_; }
714 // Return the location.
717 { return this->location_; }
719 // Whether this clause may fall through to the statement which
720 // follows the overall select statement.
722 may_fall_through() const;
724 // Return a tree for the statements to execute.
726 get_statements_tree(Translate_context*);
730 Expression* channel_;
731 // The value to send or the variable to set.
733 // The variable to initialize, for "case a := <- ch".
735 // The statements to execute.
737 // The location of this clause.
738 source_location location_;
739 // Whether this is a send or a receive.
741 // Whether this is the default.
743 // Whether this has been lowered.
748 add_clause_tree(Translate_context*, int, Select_clause*, Unnamed_label*,
751 typedef std::vector<Select_clause> Clauses;
756 // A select statement.
758 class Select_statement : public Statement
761 Select_statement(source_location location)
762 : Statement(STATEMENT_SELECT, location),
763 clauses_(NULL), break_label_(NULL), is_lowered_(false)
768 add_clauses(Select_clauses* clauses)
770 gcc_assert(this->clauses_ == NULL);
771 this->clauses_ = clauses;
774 // Return the break label for this select statement.
780 do_traverse(Traverse* traverse)
781 { return this->clauses_->traverse(traverse); }
784 do_lower(Gogo*, Block*);
788 { this->clauses_->determine_types(); }
791 do_may_fall_through() const
792 { return this->clauses_->may_fall_through(); }
795 do_get_tree(Translate_context*);
798 // The select clauses.
799 Select_clauses* clauses_;
801 Unnamed_label* break_label_;
802 // Whether this statement has been lowered.
806 // A statement which requires a thunk: go or defer.
808 class Thunk_statement : public Statement
811 Thunk_statement(Statement_classification, Call_expression*,
814 // Return the call expression.
817 { return this->call_; }
819 // Simplify a go or defer statement so that it only uses a single
822 simplify_statement(Gogo*, Block*);
826 do_traverse(Traverse* traverse);
829 do_traverse_assignments(Traverse_assignments*);
832 do_determine_types();
835 do_check_types(Gogo*);
837 // Return the function and argument trees for the call.
839 get_fn_and_arg(Translate_context*, tree* pfn, tree* parg);
842 // Return whether this is a simple go statement.
844 is_simple(Function_type*) const;
846 // Build the struct to use for a complex case.
848 build_struct(Function_type* fntype);
852 build_thunk(Gogo*, const std::string&, Function_type* fntype);
854 // The field name used in the thunk structure for the function
856 static const char* const thunk_field_fn;
858 // The field name used in the thunk structure for the receiver, if
860 static const char* const thunk_field_receiver;
862 // Set the name to use for thunk field N.
864 thunk_field_param(int n, char* buf, size_t buflen);
866 // The function call to be executed in a separate thread (go) or
869 // The type used for a struct to pass to a thunk, if this is not a
871 Struct_type* struct_type_;
876 class Go_statement : public Thunk_statement
879 Go_statement(Call_expression* call, source_location location)
880 : Thunk_statement(STATEMENT_GO, call, location)
885 do_get_tree(Translate_context*);
888 // A defer statement.
890 class Defer_statement : public Thunk_statement
893 Defer_statement(Call_expression* call, source_location location)
894 : Thunk_statement(STATEMENT_DEFER, call, location)
899 do_get_tree(Translate_context*);
902 // A label statement.
904 class Label_statement : public Statement
907 Label_statement(Label* label, source_location location)
908 : Statement(STATEMENT_LABEL, location),
912 // Return the label itself.
915 { return this->label_; }
919 do_traverse(Traverse*);
922 do_get_tree(Translate_context*);
931 class For_statement : public Statement
934 For_statement(Block* init, Expression* cond, Block* post,
935 source_location location)
936 : Statement(STATEMENT_FOR, location),
937 init_(init), cond_(cond), post_(post), statements_(NULL),
938 break_label_(NULL), continue_label_(NULL)
941 // Add the statements.
943 add_statements(Block* statements)
945 gcc_assert(this->statements_ == NULL);
946 this->statements_ = statements;
949 // Return the break label for this for statement.
953 // Return the continue label for this for statement.
957 // Set the break and continue labels for this statement.
959 set_break_continue_labels(Unnamed_label* break_label,
960 Unnamed_label* continue_label);
964 do_traverse(Traverse*);
967 do_traverse_assignments(Traverse_assignments*)
968 { gcc_unreachable(); }
971 do_lower(Gogo*, Block*);
974 do_get_tree(Translate_context*)
975 { gcc_unreachable(); }
978 // The initialization statements. This may be NULL.
980 // The condition. This may be NULL.
982 // The statements to run after each iteration. This may be NULL.
984 // The statements in the loop itself.
986 // The break label, if needed.
987 Unnamed_label* break_label_;
988 // The continue label, if needed.
989 Unnamed_label* continue_label_;
992 // A for statement over a range clause.
994 class For_range_statement : public Statement
997 For_range_statement(Expression* index_var, Expression* value_var,
998 Expression* range, source_location location)
999 : Statement(STATEMENT_FOR_RANGE, location),
1000 index_var_(index_var), value_var_(value_var), range_(range),
1001 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1004 // Add the statements.
1006 add_statements(Block* statements)
1008 gcc_assert(this->statements_ == NULL);
1009 this->statements_ = statements;
1012 // Return the break label for this for statement.
1016 // Return the continue label for this for statement.
1022 do_traverse(Traverse*);
1025 do_traverse_assignments(Traverse_assignments*)
1026 { gcc_unreachable(); }
1029 do_lower(Gogo*, Block*);
1032 do_get_tree(Translate_context*)
1033 { gcc_unreachable(); }
1037 make_range_ref(Named_object*, Temporary_statement*, source_location);
1040 call_builtin(Gogo*, const char* funcname, Expression* arg, source_location);
1043 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1044 Temporary_statement*, Temporary_statement*,
1045 Block**, Expression**, Block**, Block**);
1048 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1049 Temporary_statement*, Temporary_statement*,
1050 Block**, Expression**, Block**, Block**);
1053 lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1054 Temporary_statement*, Temporary_statement*,
1055 Block**, Expression**, Block**, Block**);
1058 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1059 Temporary_statement*, Temporary_statement*,
1060 Temporary_statement*, Block**, Expression**, Block**,
1063 // The variable which is set to the index value.
1064 Expression* index_var_;
1065 // The variable which is set to the element value. This may be
1067 Expression* value_var_;
1068 // The expression we are ranging over.
1070 // The statements in the block.
1072 // The break label, if needed.
1073 Unnamed_label* break_label_;
1074 // The continue label, if needed.
1075 Unnamed_label* continue_label_;
1078 // Class Case_clauses holds the clauses of a switch statement. This
1079 // is built by the parser.
1088 // Add a new clause. CASES is a list of case expressions; it may be
1089 // NULL. IS_DEFAULT is true if this is the default case.
1090 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1091 // after the statements the case clause should fall through to the
1094 add(Expression_list* cases, bool is_default, Block* statements,
1095 bool is_fallthrough, source_location location)
1097 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1098 is_fallthrough, location));
1101 // Return whether there are no clauses.
1104 { return this->clauses_.empty(); }
1106 // Traverse the case clauses.
1108 traverse(Traverse*);
1110 // Lower for a nonconstant switch.
1112 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1114 // Determine types of expressions. The Type parameter is the type
1115 // of the switch value.
1117 determine_types(Type*);
1119 // Check types. The Type parameter is the type of the switch value.
1123 // Return true if all the clauses are constant values.
1125 is_constant() const;
1127 // Return true if these clauses may fall through to the statements
1128 // following the switch statement.
1130 may_fall_through() const;
1132 // Return the body of a SWITCH_EXPR when all the clauses are
1135 get_constant_tree(Translate_context*, Unnamed_label* break_label) const;
1138 // For a constant tree we need to keep a record of constants we have
1139 // already seen. Note that INTEGER_CST trees are interned.
1140 typedef Unordered_set(tree) Case_constants;
1147 : cases_(NULL), statements_(NULL), is_default_(false),
1148 is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1151 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1152 bool is_fallthrough, source_location location)
1153 : cases_(cases), statements_(statements), is_default_(is_default),
1154 is_fallthrough_(is_fallthrough), location_(location)
1157 // Whether this clause falls through to the next clause.
1159 is_fallthrough() const
1160 { return this->is_fallthrough_; }
1162 // Whether this is the default.
1165 { return this->is_default_; }
1167 // The location of this clause.
1170 { return this->location_; }
1174 traverse(Traverse*);
1176 // Lower for a nonconstant switch.
1178 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1182 determine_types(Type*);
1188 // Return true if all the case expressions are constant.
1190 is_constant() const;
1192 // Return true if this clause may fall through to execute the
1193 // statements following the switch statement. This is not the
1194 // same as whether this clause falls through to the next clause.
1196 may_fall_through() const;
1198 // Build up the body of a SWITCH_EXPR when the case expressions
1201 get_constant_tree(Translate_context*, Unnamed_label* break_label,
1202 Case_constants* case_constants, tree* stmt_list) const;
1205 // The list of case expressions.
1206 Expression_list* cases_;
1207 // The statements to execute.
1209 // Whether this is the default case.
1211 // Whether this falls through after the statements.
1212 bool is_fallthrough_;
1213 // The location of this case clause.
1214 source_location location_;
1217 friend class Case_clause;
1219 // The type of the list of clauses.
1220 typedef std::vector<Case_clause> Clauses;
1222 // All the case clauses.
1226 // A switch statement.
1228 class Switch_statement : public Statement
1231 Switch_statement(Expression* val, source_location location)
1232 : Statement(STATEMENT_SWITCH, location),
1233 val_(val), clauses_(NULL), break_label_(NULL)
1238 add_clauses(Case_clauses* clauses)
1240 gcc_assert(this->clauses_ == NULL);
1241 this->clauses_ = clauses;
1244 // Return the break label for this switch statement.
1250 do_traverse(Traverse*);
1253 do_lower(Gogo*, Block*);
1256 do_get_tree(Translate_context*)
1257 { gcc_unreachable(); }
1260 // The value to switch on. This may be NULL.
1262 // The case clauses.
1263 Case_clauses* clauses_;
1264 // The break label, if needed.
1265 Unnamed_label* break_label_;
1268 // Class Type_case_clauses holds the clauses of a type switch
1269 // statement. This is built by the parser.
1271 class Type_case_clauses
1278 // Add a new clause. TYPE is the type for this clause; it may be
1279 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1280 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1281 // if this is the default case. STATEMENTS is a block of
1282 // statements; it may be NULL.
1284 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1285 source_location location)
1287 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1288 statements, location));
1291 // Return whether there are no clauses.
1294 { return this->clauses_.empty(); }
1296 // Traverse the type case clauses.
1298 traverse(Traverse*);
1300 // Check for duplicates.
1302 check_duplicates() const;
1304 // Lower to if and goto statements.
1306 lower(Block*, Temporary_statement* descriptor_temp,
1307 Unnamed_label* break_label) const;
1310 // One type case clause.
1311 class Type_case_clause
1315 : type_(NULL), statements_(NULL), is_default_(false),
1316 location_(UNKNOWN_LOCATION)
1319 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1320 Block* statements, source_location location)
1321 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1322 is_default_(is_default), location_(location)
1328 { return this->type_; }
1330 // Whether this is the default.
1333 { return this->is_default_; }
1335 // The location of this type clause.
1338 { return this->location_; }
1342 traverse(Traverse*);
1344 // Lower to if and goto statements.
1346 lower(Block*, Temporary_statement* descriptor_temp,
1347 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1350 // The type for this type clause.
1352 // The statements to execute.
1354 // Whether this falls through--this is true for "case T1, T2".
1355 bool is_fallthrough_;
1356 // Whether this is the default case.
1358 // The location of this type case clause.
1359 source_location location_;
1362 friend class Type_case_clause;
1364 // The type of the list of type clauses.
1365 typedef std::vector<Type_case_clause> Type_clauses;
1367 // All the type case clauses.
1368 Type_clauses clauses_;
1371 // A type switch statement.
1373 class Type_switch_statement : public Statement
1376 Type_switch_statement(Named_object* var, Expression* expr,
1377 source_location location)
1378 : Statement(STATEMENT_TYPE_SWITCH, location),
1379 var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1380 { gcc_assert(var == NULL || expr == NULL); }
1384 add_clauses(Type_case_clauses* clauses)
1386 gcc_assert(this->clauses_ == NULL);
1387 this->clauses_ = clauses;
1390 // Return the break label for this type switch statement.
1396 do_traverse(Traverse*);
1399 do_lower(Gogo*, Block*);
1402 do_get_tree(Translate_context*)
1403 { gcc_unreachable(); }
1406 // Get the type descriptor.
1408 get_type_descriptor(Translate_context*, Type*, tree);
1410 // The variable holding the value we are switching on.
1412 // The expression we are switching on if there is no variable.
1414 // The type case clauses.
1415 Type_case_clauses* clauses_;
1416 // The break label, if needed.
1417 Unnamed_label* break_label_;
1420 #endif // !defined(GO_STATEMENTS_H)