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.
494 do_traverse(Traverse*);
497 do_traverse_assignments(Traverse_assignments*);
500 do_determine_types();
503 do_check_types(Gogo*);
506 do_get_tree(Translate_context*);
509 // The type of the temporary variable.
511 // The initial value of the temporary variable. This may be NULL.
513 // The DECL for the temporary variable.
515 // True if something takes the address of this temporary variable.
516 bool is_address_taken_;
519 // A variable declaration. This marks the point in the code where a
520 // variable is declared. The Variable is also attached to a Block.
522 class Variable_declaration_statement : public Statement
525 Variable_declaration_statement(Named_object* var);
527 // The variable being declared.
530 { return this->var_; }
534 do_traverse(Traverse*);
537 do_traverse_assignments(Traverse_assignments*);
540 do_get_tree(Translate_context*);
546 // A return statement.
548 class Return_statement : public Statement
551 Return_statement(const Typed_identifier_list* results, Expression_list* vals,
552 source_location location)
553 : Statement(STATEMENT_RETURN, location),
554 results_(results), vals_(vals)
557 // The list of values being returned. This may be NULL.
558 const Expression_list*
560 { return this->vals_; }
564 do_traverse(Traverse* traverse)
565 { return this->traverse_expression_list(traverse, this->vals_); }
568 do_traverse_assignments(Traverse_assignments*);
571 do_lower(Gogo*, Block*);
574 do_determine_types();
577 do_check_types(Gogo*);
580 do_may_fall_through() const
584 do_get_tree(Translate_context*);
587 // The result types of the function we are returning from. This is
588 // here because in some of the traversals it is inconvenient to get
590 const Typed_identifier_list* results_;
591 // Return values. This may be NULL.
592 Expression_list* vals_;
595 // Select_clauses holds the clauses of a select statement. This is
596 // built by the parser.
605 // Add a new clause. IS_SEND is true if this is a send clause,
606 // false for a receive clause. For a send clause CHANNEL is the
607 // channel and VAL is the value to send. For a receive clause
608 // CHANNEL is the channel and VAL is either NULL or a Var_expression
609 // for the variable to set; if VAL is NULL, VAR may be a variable
610 // which is initialized with the received value. IS_DEFAULT is true
611 // if this is the default clause. STATEMENTS is the list of
612 // statements to execute.
614 add(bool is_send, Expression* channel, Expression* val, Named_object* var,
615 bool is_default, Block* statements, source_location location)
617 this->clauses_.push_back(Select_clause(is_send, channel, val, var,
618 is_default, statements, location));
621 // Traverse the select clauses.
633 // Whether the select clauses may fall through to the statement
634 // which follows the overall select statement.
636 may_fall_through() const;
638 // Return a tree implementing the select statement.
640 get_tree(Translate_context*, Unnamed_label* break_label, source_location);
648 : channel_(NULL), val_(NULL), var_(NULL), statements_(NULL),
649 is_send_(false), is_default_(false)
652 Select_clause(bool is_send, Expression* channel, Expression* val,
653 Named_object* var, bool is_default, Block* statements,
654 source_location location)
655 : channel_(channel), val_(val), var_(var), statements_(statements),
656 location_(location), is_send_(is_send), is_default_(is_default),
658 { gcc_assert(is_default ? channel == NULL : channel != NULL); }
660 // Traverse the select clause.
672 // Return true if this is the default clause.
675 { return this->is_default_; }
677 // Return the channel. This will return NULL for the default
681 { return this->channel_; }
683 // Return the value. This will return NULL for the default
684 // clause, or for a receive clause for which no value was given.
687 { return this->val_; }
689 // Return the variable to set when a receive clause is also a
690 // variable definition (v := <- ch). This will return NULL for
691 // the default case, or for a send clause, or for a receive clause
692 // which does not define a variable.
695 { return this->var_; }
697 // Return true for a send, false for a receive.
701 gcc_assert(!this->is_default_);
702 return this->is_send_;
705 // Return the statements.
708 { return this->statements_; }
710 // Return the location.
713 { return this->location_; }
715 // Whether this clause may fall through to the statement which
716 // follows the overall select statement.
718 may_fall_through() const;
720 // Return a tree for the statements to execute.
722 get_statements_tree(Translate_context*);
726 Expression* channel_;
727 // The value to send or the variable to set.
729 // The variable to initialize, for "case a := <- ch".
731 // The statements to execute.
733 // The location of this clause.
734 source_location location_;
735 // Whether this is a send or a receive.
737 // Whether this is the default.
739 // Whether this has been lowered.
744 add_clause_tree(Translate_context*, int, Select_clause*, Unnamed_label*,
747 typedef std::vector<Select_clause> Clauses;
752 // A select statement.
754 class Select_statement : public Statement
757 Select_statement(source_location location)
758 : Statement(STATEMENT_SELECT, location),
759 clauses_(NULL), break_label_(NULL), is_lowered_(false)
764 add_clauses(Select_clauses* clauses)
766 gcc_assert(this->clauses_ == NULL);
767 this->clauses_ = clauses;
770 // Return the break label for this select statement.
776 do_traverse(Traverse* traverse)
777 { return this->clauses_->traverse(traverse); }
780 do_lower(Gogo*, Block*);
784 { this->clauses_->determine_types(); }
787 do_may_fall_through() const
788 { return this->clauses_->may_fall_through(); }
791 do_get_tree(Translate_context*);
794 // The select clauses.
795 Select_clauses* clauses_;
797 Unnamed_label* break_label_;
798 // Whether this statement has been lowered.
802 // A statement which requires a thunk: go or defer.
804 class Thunk_statement : public Statement
807 Thunk_statement(Statement_classification, Call_expression*,
810 // Return the call expression.
813 { return this->call_; }
815 // Simplify a go or defer statement so that it only uses a single
818 simplify_statement(Gogo*, Block*);
822 do_traverse(Traverse* traverse);
825 do_traverse_assignments(Traverse_assignments*);
828 do_determine_types();
831 do_check_types(Gogo*);
833 // Return the function and argument trees for the call.
835 get_fn_and_arg(Translate_context*, tree* pfn, tree* parg);
838 // Return whether this is a simple go statement.
840 is_simple(Function_type*) const;
842 // Build the struct to use for a complex case.
844 build_struct(Function_type* fntype);
848 build_thunk(Gogo*, const std::string&, Function_type* fntype);
850 // The field name used in the thunk structure for the function
852 static const char* const thunk_field_fn;
854 // The field name used in the thunk structure for the receiver, if
856 static const char* const thunk_field_receiver;
858 // Set the name to use for thunk field N.
860 thunk_field_param(int n, char* buf, size_t buflen);
862 // The function call to be executed in a separate thread (go) or
865 // The type used for a struct to pass to a thunk, if this is not a
867 Struct_type* struct_type_;
872 class Go_statement : public Thunk_statement
875 Go_statement(Call_expression* call, source_location location)
876 : Thunk_statement(STATEMENT_GO, call, location)
881 do_get_tree(Translate_context*);
884 // A defer statement.
886 class Defer_statement : public Thunk_statement
889 Defer_statement(Call_expression* call, source_location location)
890 : Thunk_statement(STATEMENT_DEFER, call, location)
895 do_get_tree(Translate_context*);
898 // A label statement.
900 class Label_statement : public Statement
903 Label_statement(Label* label, source_location location)
904 : Statement(STATEMENT_LABEL, location),
908 // Return the label itself.
911 { return this->label_; }
915 do_traverse(Traverse*);
918 do_get_tree(Translate_context*);
927 class For_statement : public Statement
930 For_statement(Block* init, Expression* cond, Block* post,
931 source_location location)
932 : Statement(STATEMENT_FOR, location),
933 init_(init), cond_(cond), post_(post), statements_(NULL),
934 break_label_(NULL), continue_label_(NULL)
937 // Add the statements.
939 add_statements(Block* statements)
941 gcc_assert(this->statements_ == NULL);
942 this->statements_ = statements;
945 // Return the break label for this for statement.
949 // Return the continue label for this for statement.
953 // Set the break and continue labels for this statement.
955 set_break_continue_labels(Unnamed_label* break_label,
956 Unnamed_label* continue_label);
960 do_traverse(Traverse*);
963 do_traverse_assignments(Traverse_assignments*)
964 { gcc_unreachable(); }
967 do_lower(Gogo*, Block*);
970 do_get_tree(Translate_context*)
971 { gcc_unreachable(); }
974 // The initialization statements. This may be NULL.
976 // The condition. This may be NULL.
978 // The statements to run after each iteration. This may be NULL.
980 // The statements in the loop itself.
982 // The break label, if needed.
983 Unnamed_label* break_label_;
984 // The continue label, if needed.
985 Unnamed_label* continue_label_;
988 // A for statement over a range clause.
990 class For_range_statement : public Statement
993 For_range_statement(Expression* index_var, Expression* value_var,
994 Expression* range, source_location location)
995 : Statement(STATEMENT_FOR_RANGE, location),
996 index_var_(index_var), value_var_(value_var), range_(range),
997 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1000 // Add the statements.
1002 add_statements(Block* statements)
1004 gcc_assert(this->statements_ == NULL);
1005 this->statements_ = statements;
1008 // Return the break label for this for statement.
1012 // Return the continue label for this for statement.
1018 do_traverse(Traverse*);
1021 do_traverse_assignments(Traverse_assignments*)
1022 { gcc_unreachable(); }
1025 do_lower(Gogo*, Block*);
1028 do_get_tree(Translate_context*)
1029 { gcc_unreachable(); }
1033 make_range_ref(Named_object*, Temporary_statement*, source_location);
1036 call_builtin(Gogo*, const char* funcname, Expression* arg, source_location);
1039 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1040 Temporary_statement*, Temporary_statement*,
1041 Block**, Expression**, Block**, Block**);
1044 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1045 Temporary_statement*, Temporary_statement*,
1046 Block**, Expression**, Block**, Block**);
1049 lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1050 Temporary_statement*, Temporary_statement*,
1051 Block**, Expression**, Block**, Block**);
1054 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1055 Temporary_statement*, Temporary_statement*,
1056 Temporary_statement*, Block**, Expression**, Block**,
1059 // The variable which is set to the index value.
1060 Expression* index_var_;
1061 // The variable which is set to the element value. This may be
1063 Expression* value_var_;
1064 // The expression we are ranging over.
1066 // The statements in the block.
1068 // The break label, if needed.
1069 Unnamed_label* break_label_;
1070 // The continue label, if needed.
1071 Unnamed_label* continue_label_;
1074 // Class Case_clauses holds the clauses of a switch statement. This
1075 // is built by the parser.
1084 // Add a new clause. CASES is a list of case expressions; it may be
1085 // NULL. IS_DEFAULT is true if this is the default case.
1086 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1087 // after the statements the case clause should fall through to the
1090 add(Expression_list* cases, bool is_default, Block* statements,
1091 bool is_fallthrough, source_location location)
1093 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1094 is_fallthrough, location));
1097 // Return whether there are no clauses.
1100 { return this->clauses_.empty(); }
1102 // Traverse the case clauses.
1104 traverse(Traverse*);
1106 // Lower for a nonconstant switch.
1108 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1110 // Determine types of expressions. The Type parameter is the type
1111 // of the switch value.
1113 determine_types(Type*);
1115 // Check types. The Type parameter is the type of the switch value.
1119 // Return true if all the clauses are constant values.
1121 is_constant() const;
1123 // Return true if these clauses may fall through to the statements
1124 // following the switch statement.
1126 may_fall_through() const;
1128 // Return the body of a SWITCH_EXPR when all the clauses are
1131 get_constant_tree(Translate_context*, Unnamed_label* break_label) const;
1134 // For a constant tree we need to keep a record of constants we have
1135 // already seen. Note that INTEGER_CST trees are interned.
1136 typedef Unordered_set(tree) Case_constants;
1143 : cases_(NULL), statements_(NULL), is_default_(false),
1144 is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1147 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1148 bool is_fallthrough, source_location location)
1149 : cases_(cases), statements_(statements), is_default_(is_default),
1150 is_fallthrough_(is_fallthrough), location_(location)
1153 // Whether this clause falls through to the next clause.
1155 is_fallthrough() const
1156 { return this->is_fallthrough_; }
1158 // Whether this is the default.
1161 { return this->is_default_; }
1163 // The location of this clause.
1166 { return this->location_; }
1170 traverse(Traverse*);
1172 // Lower for a nonconstant switch.
1174 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1178 determine_types(Type*);
1184 // Return true if all the case expressions are constant.
1186 is_constant() const;
1188 // Return true if this clause may fall through to execute the
1189 // statements following the switch statement. This is not the
1190 // same as whether this clause falls through to the next clause.
1192 may_fall_through() const;
1194 // Build up the body of a SWITCH_EXPR when the case expressions
1197 get_constant_tree(Translate_context*, Unnamed_label* break_label,
1198 Case_constants* case_constants, tree* stmt_list) const;
1201 // The list of case expressions.
1202 Expression_list* cases_;
1203 // The statements to execute.
1205 // Whether this is the default case.
1207 // Whether this falls through after the statements.
1208 bool is_fallthrough_;
1209 // The location of this case clause.
1210 source_location location_;
1213 friend class Case_clause;
1215 // The type of the list of clauses.
1216 typedef std::vector<Case_clause> Clauses;
1218 // All the case clauses.
1222 // A switch statement.
1224 class Switch_statement : public Statement
1227 Switch_statement(Expression* val, source_location location)
1228 : Statement(STATEMENT_SWITCH, location),
1229 val_(val), clauses_(NULL), break_label_(NULL)
1234 add_clauses(Case_clauses* clauses)
1236 gcc_assert(this->clauses_ == NULL);
1237 this->clauses_ = clauses;
1240 // Return the break label for this switch statement.
1246 do_traverse(Traverse*);
1249 do_lower(Gogo*, Block*);
1252 do_get_tree(Translate_context*)
1253 { gcc_unreachable(); }
1256 // The value to switch on. This may be NULL.
1258 // The case clauses.
1259 Case_clauses* clauses_;
1260 // The break label, if needed.
1261 Unnamed_label* break_label_;
1264 // Class Type_case_clauses holds the clauses of a type switch
1265 // statement. This is built by the parser.
1267 class Type_case_clauses
1274 // Add a new clause. TYPE is the type for this clause; it may be
1275 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1276 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1277 // if this is the default case. STATEMENTS is a block of
1278 // statements; it may be NULL.
1280 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1281 source_location location)
1283 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1284 statements, location));
1287 // Return whether there are no clauses.
1290 { return this->clauses_.empty(); }
1292 // Traverse the type case clauses.
1294 traverse(Traverse*);
1296 // Check for duplicates.
1298 check_duplicates() const;
1300 // Lower to if and goto statements.
1302 lower(Block*, Temporary_statement* descriptor_temp,
1303 Unnamed_label* break_label) const;
1306 // One type case clause.
1307 class Type_case_clause
1311 : type_(NULL), statements_(NULL), is_default_(false),
1312 location_(UNKNOWN_LOCATION)
1315 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1316 Block* statements, source_location location)
1317 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1318 is_default_(is_default), location_(location)
1324 { return this->type_; }
1326 // Whether this is the default.
1329 { return this->is_default_; }
1331 // The location of this type clause.
1334 { return this->location_; }
1338 traverse(Traverse*);
1340 // Lower to if and goto statements.
1342 lower(Block*, Temporary_statement* descriptor_temp,
1343 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1346 // The type for this type clause.
1348 // The statements to execute.
1350 // Whether this falls through--this is true for "case T1, T2".
1351 bool is_fallthrough_;
1352 // Whether this is the default case.
1354 // The location of this type case clause.
1355 source_location location_;
1358 friend class Type_case_clause;
1360 // The type of the list of type clauses.
1361 typedef std::vector<Type_case_clause> Type_clauses;
1363 // All the type case clauses.
1364 Type_clauses clauses_;
1367 // A type switch statement.
1369 class Type_switch_statement : public Statement
1372 Type_switch_statement(Named_object* var, Expression* expr,
1373 source_location location)
1374 : Statement(STATEMENT_TYPE_SWITCH, location),
1375 var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1376 { gcc_assert(var == NULL || expr == NULL); }
1380 add_clauses(Type_case_clauses* clauses)
1382 gcc_assert(this->clauses_ == NULL);
1383 this->clauses_ = clauses;
1386 // Return the break label for this type switch statement.
1392 do_traverse(Traverse*);
1395 do_lower(Gogo*, Block*);
1398 do_get_tree(Translate_context*)
1399 { gcc_unreachable(); }
1402 // Get the type descriptor.
1404 get_type_descriptor(Translate_context*, Type*, tree);
1406 // The variable holding the value we are switching on.
1408 // The expression we are switching on if there is no variable.
1410 // The type case clauses.
1411 Type_case_clauses* clauses_;
1412 // The break label, if needed.
1413 Unnamed_label* break_label_;
1416 #endif // !defined(GO_STATEMENTS_H)