1 // statements.h -- Go frontend statements. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #ifndef GO_STATEMENTS_H
8 #define GO_STATEMENTS_H
17 class Temporary_statement;
18 class Variable_declaration_statement;
19 class Return_statement;
20 class Thunk_statement;
21 class Label_statement;
23 class For_range_statement;
24 class Switch_statement;
25 class Type_switch_statement;
27 class Select_statement;
31 class Translate_context;
33 class Expression_list;
35 class Call_expression;
36 class Map_index_expression;
37 class Receive_expression;
39 class Type_case_clauses;
41 class Typed_identifier_list;
43 // This class is used to traverse assignments made by a statement
44 // which makes assignments.
46 class Traverse_assignments
49 Traverse_assignments()
52 virtual ~Traverse_assignments()
55 // This is called for a variable initialization.
57 initialize_variable(Named_object*) = 0;
59 // This is called for each assignment made by the statement. PLHS
60 // points to the left hand side, and PRHS points to the right hand
61 // side. PRHS may be NULL if there is no associated expression, as
62 // in the bool set by a non-blocking receive.
64 assignment(Expression** plhs, Expression** prhs) = 0;
66 // This is called for each expression which is not passed to the
67 // assignment function. This is used for some of the statements
68 // which assign two values, for which there is no expression which
69 // describes the value. For ++ and -- the value is passed to both
70 // the assignment method and the rhs method. IS_STORED is true if
71 // this value is being stored directly. It is false if the value is
72 // computed but not stored. IS_LOCAL is true if the value is being
73 // stored in a local variable or this is being called by a return
76 value(Expression**, bool is_stored, bool is_local) = 0;
79 // A single statement.
84 // The types of statements.
85 enum Statement_classification
88 STATEMENT_VARIABLE_DECLARATION,
96 STATEMENT_BREAK_OR_CONTINUE,
98 STATEMENT_GOTO_UNNAMED,
100 STATEMENT_UNNAMED_LABEL,
102 STATEMENT_CONSTANT_SWITCH,
106 // These statements types are created by the parser, but they
107 // disappear during the lowering pass.
108 STATEMENT_ASSIGNMENT_OPERATION,
109 STATEMENT_TUPLE_ASSIGNMENT,
110 STATEMENT_TUPLE_MAP_ASSIGNMENT,
111 STATEMENT_MAP_ASSIGNMENT,
112 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT,
113 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT,
118 STATEMENT_TYPE_SWITCH
121 Statement(Statement_classification, source_location);
123 virtual ~Statement();
125 // Make a variable declaration.
127 make_variable_declaration(Named_object*);
129 // Make a statement which creates a temporary variable and
130 // initializes it to an expression. The block is used if the
131 // temporary variable has to be explicitly destroyed; the variable
132 // must still be added to the block. References to the temporary
133 // variable may be constructed using make_temporary_reference.
134 // Either the type or the initialization expression may be NULL, but
136 static Temporary_statement*
137 make_temporary(Type*, Expression*, source_location);
139 // Make an assignment statement.
141 make_assignment(Expression*, Expression*, source_location);
143 // Make an assignment operation (+=, etc.).
145 make_assignment_operation(Operator, Expression*, Expression*,
148 // Make a tuple assignment statement.
150 make_tuple_assignment(Expression_list*, Expression_list*, source_location);
152 // Make an assignment from a map index to a pair of variables.
154 make_tuple_map_assignment(Expression* val, Expression* present,
155 Expression*, source_location);
157 // Make a statement which assigns a pair of values to a map.
159 make_map_assignment(Expression*, Expression* val,
160 Expression* should_set, source_location);
162 // Make an assignment from a nonblocking receive to a pair of
163 // variables. FOR_SELECT is true is this is being created for a
164 // case x, ok := <-c in a select statement.
166 make_tuple_receive_assignment(Expression* val, Expression* closed,
167 Expression* channel, bool for_select,
170 // Make an assignment from a type guard to a pair of variables.
172 make_tuple_type_guard_assignment(Expression* val, Expression* ok,
173 Expression* expr, Type* type,
176 // Make an expression statement from an Expression.
178 make_statement(Expression*);
180 // Make a block statement from a Block. This is an embedded list of
181 // statements which may also include variable definitions.
183 make_block_statement(Block*, source_location);
185 // Make an increment statement.
187 make_inc_statement(Expression*);
189 // Make a decrement statement.
191 make_dec_statement(Expression*);
193 // Make a go statement.
195 make_go_statement(Call_expression* call, source_location);
197 // Make a defer statement.
199 make_defer_statement(Call_expression* call, source_location);
201 // Make a return statement.
203 make_return_statement(const Typed_identifier_list*, Expression_list*,
206 // Make a break statement.
208 make_break_statement(Unnamed_label* label, source_location);
210 // Make a continue statement.
212 make_continue_statement(Unnamed_label* label, source_location);
214 // Make a goto statement.
216 make_goto_statement(Label* label, source_location);
218 // Make a goto statement to an unnamed label.
220 make_goto_unnamed_statement(Unnamed_label* label, source_location);
222 // Make a label statement--where the label is defined.
224 make_label_statement(Label* label, source_location);
226 // Make an unnamed label statement--where the label is defined.
228 make_unnamed_label_statement(Unnamed_label* label);
230 // Make an if statement.
232 make_if_statement(Expression* cond, Block* then_block, Block* else_block,
235 // Make a switch statement.
236 static Switch_statement*
237 make_switch_statement(Expression* switch_val, source_location);
239 // Make a type switch statement.
240 static Type_switch_statement*
241 make_type_switch_statement(Named_object* var, Expression*, source_location);
243 // Make a send statement.
244 static Send_statement*
245 make_send_statement(Expression* channel, Expression* val, source_location);
247 // Make a select statement.
248 static Select_statement*
249 make_select_statement(source_location);
251 // Make a for statement.
252 static For_statement*
253 make_for_statement(Block* init, Expression* cond, Block* post,
254 source_location location);
256 // Make a for statement with a range clause.
257 static For_range_statement*
258 make_for_range_statement(Expression* index_var, Expression* value_var,
259 Expression* range, source_location);
261 // Return the statement classification.
262 Statement_classification
263 classification() const
264 { return this->classification_; }
266 // Get the statement location.
269 { return this->location_; }
271 // Traverse the tree.
273 traverse(Block*, size_t* index, Traverse*);
275 // Traverse the contents of this statement--the expressions and
276 // statements which it contains.
278 traverse_contents(Traverse*);
280 // If this statement assigns some values, it calls a function for
281 // each value to which this statement assigns a value, and returns
282 // true. If this statement does not assign any values, it returns
285 traverse_assignments(Traverse_assignments* tassign);
287 // Lower a statement. This is called immediately after parsing to
288 // simplify statements for further processing. It returns the same
289 // Statement or a new one. FUNCTION is the function containing this
290 // statement. BLOCK is the block containing this statement.
292 lower(Gogo* gogo, Named_object* function, Block* block)
293 { return this->do_lower(gogo, function, block); }
295 // Set type information for unnamed constants.
299 // Check types in a statement. This simply checks that any
300 // expressions used by the statement have the right type.
302 check_types(Gogo* gogo)
303 { this->do_check_types(gogo); }
305 // Return whether this is a block statement.
307 is_block_statement() const
308 { return this->classification_ == STATEMENT_BLOCK; }
310 // If this is a variable declaration statement, return it.
311 // Otherwise return NULL.
312 Variable_declaration_statement*
313 variable_declaration_statement()
315 return this->convert<Variable_declaration_statement,
316 STATEMENT_VARIABLE_DECLARATION>();
319 // If this is a return statement, return it. Otherwise return NULL.
322 { return this->convert<Return_statement, STATEMENT_RETURN>(); }
324 // If this is a thunk statement (a go or defer statement), return
325 // it. Otherwise return NULL.
329 // If this is a label statement, return it. Otherwise return NULL.
332 { return this->convert<Label_statement, STATEMENT_LABEL>(); }
334 // If this is a for statement, return it. Otherwise return NULL.
337 { return this->convert<For_statement, STATEMENT_FOR>(); }
339 // If this is a for statement over a range clause, return it.
340 // Otherwise return NULL.
342 for_range_statement()
343 { return this->convert<For_range_statement, STATEMENT_FOR_RANGE>(); }
345 // If this is a switch statement, return it. Otherwise return NULL.
348 { return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
350 // If this is a type switch statement, return it. Otherwise return
352 Type_switch_statement*
353 type_switch_statement()
354 { return this->convert<Type_switch_statement, STATEMENT_TYPE_SWITCH>(); }
356 // If this is a select statement, return it. Otherwise return NULL.
359 { return this->convert<Select_statement, STATEMENT_SELECT>(); }
361 // Return true if this statement may fall through--if after
362 // executing this statement we may go on to execute the following
363 // statement, if any.
365 may_fall_through() const
366 { return this->do_may_fall_through(); }
368 // Return the tree for a statement. BLOCK is the enclosing block.
370 get_tree(Translate_context*);
373 // Implemented by child class: traverse the tree.
375 do_traverse(Traverse*) = 0;
377 // Implemented by child class: traverse assignments. Any statement
378 // which includes an assignment should implement this.
380 do_traverse_assignments(Traverse_assignments*)
383 // Implemented by the child class: lower this statement to a simpler
386 do_lower(Gogo*, Named_object*, Block*)
389 // Implemented by child class: set type information for unnamed
390 // constants. Any statement which includes an expression needs to
396 // Implemented by child class: check types of expressions used in a
399 do_check_types(Gogo*)
402 // Implemented by child class: return true if this statement may
405 do_may_fall_through() const
408 // Implemented by child class: return a tree.
410 do_get_tree(Translate_context*) = 0;
412 // Traverse an expression in a statement.
414 traverse_expression(Traverse*, Expression**);
416 // Traverse an expression list in a statement. The Expression_list
419 traverse_expression_list(Traverse*, Expression_list*);
421 // Traverse a type in a statement.
423 traverse_type(Traverse*, Type*);
425 // Build a tree node with one operand, setting the location. The
426 // first operand really has type "enum tree_code", but that enum is
429 build_stmt_1(int tree_code_value, tree);
431 // For children to call when they detect that they are in error.
435 // For children to call to report an error conveniently.
437 report_error(const char*);
439 // For children to return an error statement from lower().
441 make_error_statement(source_location);
444 // Convert to the desired statement classification, or return NULL.
445 // This is a controlled dynamic cast.
446 template<typename Statement_class, Statement_classification sc>
450 return (this->classification_ == sc
451 ? static_cast<Statement_class*>(this)
455 template<typename Statement_class, Statement_classification sc>
456 const Statement_class*
459 return (this->classification_ == sc
460 ? static_cast<const Statement_class*>(this)
464 // The statement classification.
465 Statement_classification classification_;
466 // The location in the input file of the start of this statement.
467 source_location location_;
470 // A statement which creates and initializes a temporary variable.
472 class Temporary_statement : public Statement
475 Temporary_statement(Type* type, Expression* init, source_location location)
476 : Statement(STATEMENT_TEMPORARY, location),
477 type_(type), init_(init), decl_(NULL), is_address_taken_(false)
480 // Return the type of the temporary variable.
484 // Return the initialization expression.
487 { return this->init_; }
489 // Record that something takes the address of this temporary
492 set_is_address_taken()
493 { this->is_address_taken_ = true; }
495 // Return the tree for the temporary variable itself. This should
496 // not be called until after the statement itself has been expanded.
502 do_traverse(Traverse*);
505 do_traverse_assignments(Traverse_assignments*);
508 do_determine_types();
511 do_check_types(Gogo*);
514 do_get_tree(Translate_context*);
517 // The type of the temporary variable.
519 // The initial value of the temporary variable. This may be NULL.
521 // The DECL for the temporary variable.
523 // True if something takes the address of this temporary variable.
524 bool is_address_taken_;
527 // A variable declaration. This marks the point in the code where a
528 // variable is declared. The Variable is also attached to a Block.
530 class Variable_declaration_statement : public Statement
533 Variable_declaration_statement(Named_object* var);
535 // The variable being declared.
538 { return this->var_; }
542 do_traverse(Traverse*);
545 do_traverse_assignments(Traverse_assignments*);
548 do_get_tree(Translate_context*);
554 // A return statement.
556 class Return_statement : public Statement
559 Return_statement(const Typed_identifier_list* results, Expression_list* vals,
560 source_location location)
561 : Statement(STATEMENT_RETURN, location),
562 results_(results), vals_(vals)
565 // The list of values being returned. This may be NULL.
566 const Expression_list*
568 { return this->vals_; }
572 do_traverse(Traverse* traverse)
573 { return this->traverse_expression_list(traverse, this->vals_); }
576 do_traverse_assignments(Traverse_assignments*);
579 do_lower(Gogo*, Named_object*, Block*);
582 do_determine_types();
585 do_check_types(Gogo*);
588 do_may_fall_through() const
592 do_get_tree(Translate_context*);
595 // The result types of the function we are returning from. This is
596 // here because in some of the traversals it is inconvenient to get
598 const Typed_identifier_list* results_;
599 // Return values. This may be NULL.
600 Expression_list* vals_;
605 class Send_statement : public Statement
608 Send_statement(Expression* channel, Expression* val,
609 source_location location)
610 : Statement(STATEMENT_SEND, location),
611 channel_(channel), val_(val), for_select_(false)
614 // Note that this is for a select statement.
617 { this->for_select_ = true; }
621 do_traverse(Traverse* traverse);
624 do_determine_types();
627 do_check_types(Gogo*);
630 do_get_tree(Translate_context*);
633 // The channel on which to send the value.
634 Expression* channel_;
635 // The value to send.
637 // Whether this is for a select statement.
641 // Select_clauses holds the clauses of a select statement. This is
642 // built by the parser.
651 // Add a new clause. IS_SEND is true if this is a send clause,
652 // false for a receive clause. For a send clause CHANNEL is the
653 // channel and VAL is the value to send. For a receive clause
654 // CHANNEL is the channel, VAL is either NULL or a Var_expression
655 // for the variable to set, and CLOSED is either NULL or a
656 // Var_expression to set to whether the channel is closed. If VAL
657 // is NULL, VAR may be a variable to be initialized with the
658 // received value, and CLOSEDVAR ma be a variable to be initialized
659 // with whether the channel is closed. IS_DEFAULT is true if this
660 // is the default clause. STATEMENTS is the list of statements to
663 add(bool is_send, Expression* channel, Expression* val, Expression* closed,
664 Named_object* var, Named_object* closedvar, bool is_default,
665 Block* statements, source_location location)
667 this->clauses_.push_back(Select_clause(is_send, channel, val, closed, var,
668 closedvar, is_default, statements,
672 // Traverse the select clauses.
678 lower(Gogo*, Named_object*, Block*);
684 // Whether the select clauses may fall through to the statement
685 // which follows the overall select statement.
687 may_fall_through() const;
689 // Return a tree implementing the select statement.
691 get_tree(Translate_context*, Unnamed_label* break_label, source_location);
699 : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
700 closedvar_(NULL), statements_(NULL), is_send_(false),
704 Select_clause(bool is_send, Expression* channel, Expression* val,
705 Expression* closed, Named_object* var,
706 Named_object* closedvar, bool is_default, Block* statements,
707 source_location location)
708 : channel_(channel), val_(val), closed_(closed), var_(var),
709 closedvar_(closedvar), statements_(statements), location_(location),
710 is_send_(is_send), is_default_(is_default), is_lowered_(false)
711 { gcc_assert(is_default ? channel == NULL : channel != NULL); }
713 // Traverse the select clause.
719 lower(Gogo*, Named_object*, Block*);
725 // Return true if this is the default clause.
728 { return this->is_default_; }
730 // Return the channel. This will return NULL for the default
734 { return this->channel_; }
736 // Return true for a send, false for a receive.
740 gcc_assert(!this->is_default_);
741 return this->is_send_;
744 // Return the statements.
747 { return this->statements_; }
749 // Return the location.
752 { return this->location_; }
754 // Whether this clause may fall through to the statement which
755 // follows the overall select statement.
757 may_fall_through() const;
759 // Return a tree for the statements to execute.
761 get_statements_tree(Translate_context*);
765 Expression* channel_;
766 // The value to send or the lvalue to receive into.
768 // The lvalue to set to whether the channel is closed on a
771 // The variable to initialize, for "case a := <-ch".
773 // The variable to initialize to whether the channel is closed,
774 // for "case a, c := <-ch".
775 Named_object* closedvar_;
776 // The statements to execute.
778 // The location of this clause.
779 source_location location_;
780 // Whether this is a send or a receive.
782 // Whether this is the default.
784 // Whether this has been lowered.
789 add_clause_tree(Translate_context*, int, Select_clause*, Unnamed_label*,
792 typedef std::vector<Select_clause> Clauses;
797 // A select statement.
799 class Select_statement : public Statement
802 Select_statement(source_location location)
803 : Statement(STATEMENT_SELECT, location),
804 clauses_(NULL), break_label_(NULL), is_lowered_(false)
809 add_clauses(Select_clauses* clauses)
811 gcc_assert(this->clauses_ == NULL);
812 this->clauses_ = clauses;
815 // Return the break label for this select statement.
821 do_traverse(Traverse* traverse)
822 { return this->clauses_->traverse(traverse); }
825 do_lower(Gogo*, Named_object*, Block*);
829 { this->clauses_->determine_types(); }
832 do_may_fall_through() const
833 { return this->clauses_->may_fall_through(); }
836 do_get_tree(Translate_context*);
839 // The select clauses.
840 Select_clauses* clauses_;
842 Unnamed_label* break_label_;
843 // Whether this statement has been lowered.
847 // A statement which requires a thunk: go or defer.
849 class Thunk_statement : public Statement
852 Thunk_statement(Statement_classification, Call_expression*,
855 // Return the call expression.
858 { return this->call_; }
860 // Simplify a go or defer statement so that it only uses a single
863 simplify_statement(Gogo*, Block*);
867 do_traverse(Traverse* traverse);
870 do_traverse_assignments(Traverse_assignments*);
873 do_determine_types();
876 do_check_types(Gogo*);
878 // Return the function and argument trees for the call.
880 get_fn_and_arg(Translate_context*, tree* pfn, tree* parg);
883 // Return whether this is a simple go statement.
885 is_simple(Function_type*) const;
887 // Build the struct to use for a complex case.
889 build_struct(Function_type* fntype);
893 build_thunk(Gogo*, const std::string&, Function_type* fntype);
895 // The field name used in the thunk structure for the function
897 static const char* const thunk_field_fn;
899 // The field name used in the thunk structure for the receiver, if
901 static const char* const thunk_field_receiver;
903 // Set the name to use for thunk field N.
905 thunk_field_param(int n, char* buf, size_t buflen);
907 // The function call to be executed in a separate thread (go) or
910 // The type used for a struct to pass to a thunk, if this is not a
912 Struct_type* struct_type_;
917 class Go_statement : public Thunk_statement
920 Go_statement(Call_expression* call, source_location location)
921 : Thunk_statement(STATEMENT_GO, call, location)
926 do_get_tree(Translate_context*);
929 // A defer statement.
931 class Defer_statement : public Thunk_statement
934 Defer_statement(Call_expression* call, source_location location)
935 : Thunk_statement(STATEMENT_DEFER, call, location)
940 do_get_tree(Translate_context*);
943 // A label statement.
945 class Label_statement : public Statement
948 Label_statement(Label* label, source_location location)
949 : Statement(STATEMENT_LABEL, location),
953 // Return the label itself.
956 { return this->label_; }
960 do_traverse(Traverse*);
963 do_get_tree(Translate_context*);
972 class For_statement : public Statement
975 For_statement(Block* init, Expression* cond, Block* post,
976 source_location location)
977 : Statement(STATEMENT_FOR, location),
978 init_(init), cond_(cond), post_(post), statements_(NULL),
979 break_label_(NULL), continue_label_(NULL)
982 // Add the statements.
984 add_statements(Block* statements)
986 gcc_assert(this->statements_ == NULL);
987 this->statements_ = statements;
990 // Return the break label for this for statement.
994 // Return the continue label for this for statement.
998 // Set the break and continue labels for this statement.
1000 set_break_continue_labels(Unnamed_label* break_label,
1001 Unnamed_label* continue_label);
1005 do_traverse(Traverse*);
1008 do_traverse_assignments(Traverse_assignments*)
1009 { gcc_unreachable(); }
1012 do_lower(Gogo*, Named_object*, Block*);
1015 do_get_tree(Translate_context*)
1016 { gcc_unreachable(); }
1019 // The initialization statements. This may be NULL.
1021 // The condition. This may be NULL.
1023 // The statements to run after each iteration. This may be NULL.
1025 // The statements in the loop itself.
1027 // The break label, if needed.
1028 Unnamed_label* break_label_;
1029 // The continue label, if needed.
1030 Unnamed_label* continue_label_;
1033 // A for statement over a range clause.
1035 class For_range_statement : public Statement
1038 For_range_statement(Expression* index_var, Expression* value_var,
1039 Expression* range, source_location location)
1040 : Statement(STATEMENT_FOR_RANGE, location),
1041 index_var_(index_var), value_var_(value_var), range_(range),
1042 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1045 // Add the statements.
1047 add_statements(Block* statements)
1049 gcc_assert(this->statements_ == NULL);
1050 this->statements_ = statements;
1053 // Return the break label for this for statement.
1057 // Return the continue label for this for statement.
1063 do_traverse(Traverse*);
1066 do_traverse_assignments(Traverse_assignments*)
1067 { gcc_unreachable(); }
1070 do_lower(Gogo*, Named_object*, Block*);
1073 do_get_tree(Translate_context*)
1074 { gcc_unreachable(); }
1078 make_range_ref(Named_object*, Temporary_statement*, source_location);
1081 call_builtin(Gogo*, const char* funcname, Expression* arg, source_location);
1084 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1085 Temporary_statement*, Temporary_statement*,
1086 Block**, Expression**, Block**, Block**);
1089 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1090 Temporary_statement*, Temporary_statement*,
1091 Block**, Expression**, Block**, Block**);
1094 lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1095 Temporary_statement*, Temporary_statement*,
1096 Block**, Expression**, Block**, Block**);
1099 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1100 Temporary_statement*, Temporary_statement*,
1101 Temporary_statement*, Block**, Expression**, Block**,
1104 // The variable which is set to the index value.
1105 Expression* index_var_;
1106 // The variable which is set to the element value. This may be
1108 Expression* value_var_;
1109 // The expression we are ranging over.
1111 // The statements in the block.
1113 // The break label, if needed.
1114 Unnamed_label* break_label_;
1115 // The continue label, if needed.
1116 Unnamed_label* continue_label_;
1119 // Class Case_clauses holds the clauses of a switch statement. This
1120 // is built by the parser.
1129 // Add a new clause. CASES is a list of case expressions; it may be
1130 // NULL. IS_DEFAULT is true if this is the default case.
1131 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1132 // after the statements the case clause should fall through to the
1135 add(Expression_list* cases, bool is_default, Block* statements,
1136 bool is_fallthrough, source_location location)
1138 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1139 is_fallthrough, location));
1142 // Return whether there are no clauses.
1145 { return this->clauses_.empty(); }
1147 // Traverse the case clauses.
1149 traverse(Traverse*);
1151 // Lower for a nonconstant switch.
1153 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1155 // Determine types of expressions. The Type parameter is the type
1156 // of the switch value.
1158 determine_types(Type*);
1160 // Check types. The Type parameter is the type of the switch value.
1164 // Return true if all the clauses are constant values.
1166 is_constant() const;
1168 // Return true if these clauses may fall through to the statements
1169 // following the switch statement.
1171 may_fall_through() const;
1173 // Return the body of a SWITCH_EXPR when all the clauses are
1176 get_constant_tree(Translate_context*, Unnamed_label* break_label) const;
1179 // For a constant tree we need to keep a record of constants we have
1180 // already seen. Note that INTEGER_CST trees are interned.
1181 typedef Unordered_set(tree) Case_constants;
1188 : cases_(NULL), statements_(NULL), is_default_(false),
1189 is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1192 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1193 bool is_fallthrough, source_location location)
1194 : cases_(cases), statements_(statements), is_default_(is_default),
1195 is_fallthrough_(is_fallthrough), location_(location)
1198 // Whether this clause falls through to the next clause.
1200 is_fallthrough() const
1201 { return this->is_fallthrough_; }
1203 // Whether this is the default.
1206 { return this->is_default_; }
1208 // The location of this clause.
1211 { return this->location_; }
1215 traverse(Traverse*);
1217 // Lower for a nonconstant switch.
1219 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1223 determine_types(Type*);
1229 // Return true if all the case expressions are constant.
1231 is_constant() const;
1233 // Return true if this clause may fall through to execute the
1234 // statements following the switch statement. This is not the
1235 // same as whether this clause falls through to the next clause.
1237 may_fall_through() const;
1239 // Build up the body of a SWITCH_EXPR when the case expressions
1242 get_constant_tree(Translate_context*, Unnamed_label* break_label,
1243 Case_constants* case_constants, tree* stmt_list) const;
1246 // The list of case expressions.
1247 Expression_list* cases_;
1248 // The statements to execute.
1250 // Whether this is the default case.
1252 // Whether this falls through after the statements.
1253 bool is_fallthrough_;
1254 // The location of this case clause.
1255 source_location location_;
1258 friend class Case_clause;
1260 // The type of the list of clauses.
1261 typedef std::vector<Case_clause> Clauses;
1263 // All the case clauses.
1267 // A switch statement.
1269 class Switch_statement : public Statement
1272 Switch_statement(Expression* val, source_location location)
1273 : Statement(STATEMENT_SWITCH, location),
1274 val_(val), clauses_(NULL), break_label_(NULL)
1279 add_clauses(Case_clauses* clauses)
1281 gcc_assert(this->clauses_ == NULL);
1282 this->clauses_ = clauses;
1285 // Return the break label for this switch statement.
1291 do_traverse(Traverse*);
1294 do_lower(Gogo*, Named_object*, Block*);
1297 do_get_tree(Translate_context*)
1298 { gcc_unreachable(); }
1301 // The value to switch on. This may be NULL.
1303 // The case clauses.
1304 Case_clauses* clauses_;
1305 // The break label, if needed.
1306 Unnamed_label* break_label_;
1309 // Class Type_case_clauses holds the clauses of a type switch
1310 // statement. This is built by the parser.
1312 class Type_case_clauses
1319 // Add a new clause. TYPE is the type for this clause; it may be
1320 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1321 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1322 // if this is the default case. STATEMENTS is a block of
1323 // statements; it may be NULL.
1325 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1326 source_location location)
1328 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1329 statements, location));
1332 // Return whether there are no clauses.
1335 { return this->clauses_.empty(); }
1337 // Traverse the type case clauses.
1339 traverse(Traverse*);
1341 // Check for duplicates.
1343 check_duplicates() const;
1345 // Lower to if and goto statements.
1347 lower(Block*, Temporary_statement* descriptor_temp,
1348 Unnamed_label* break_label) const;
1351 // One type case clause.
1352 class Type_case_clause
1356 : type_(NULL), statements_(NULL), is_default_(false),
1357 location_(UNKNOWN_LOCATION)
1360 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1361 Block* statements, source_location location)
1362 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1363 is_default_(is_default), location_(location)
1369 { return this->type_; }
1371 // Whether this is the default.
1374 { return this->is_default_; }
1376 // The location of this type clause.
1379 { return this->location_; }
1383 traverse(Traverse*);
1385 // Lower to if and goto statements.
1387 lower(Block*, Temporary_statement* descriptor_temp,
1388 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1391 // The type for this type clause.
1393 // The statements to execute.
1395 // Whether this falls through--this is true for "case T1, T2".
1396 bool is_fallthrough_;
1397 // Whether this is the default case.
1399 // The location of this type case clause.
1400 source_location location_;
1403 friend class Type_case_clause;
1405 // The type of the list of type clauses.
1406 typedef std::vector<Type_case_clause> Type_clauses;
1408 // All the type case clauses.
1409 Type_clauses clauses_;
1412 // A type switch statement.
1414 class Type_switch_statement : public Statement
1417 Type_switch_statement(Named_object* var, Expression* expr,
1418 source_location location)
1419 : Statement(STATEMENT_TYPE_SWITCH, location),
1420 var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1421 { gcc_assert(var == NULL || expr == NULL); }
1425 add_clauses(Type_case_clauses* clauses)
1427 gcc_assert(this->clauses_ == NULL);
1428 this->clauses_ = clauses;
1431 // Return the break label for this type switch statement.
1437 do_traverse(Traverse*);
1440 do_lower(Gogo*, Named_object*, Block*);
1443 do_get_tree(Translate_context*)
1444 { gcc_unreachable(); }
1447 // Get the type descriptor.
1449 get_type_descriptor(Translate_context*, Type*, tree);
1451 // The variable holding the value we are switching on.
1453 // The expression we are switching on if there is no variable.
1455 // The type case clauses.
1456 Type_case_clauses* clauses_;
1457 // The break label, if needed.
1458 Unnamed_label* break_label_;
1461 #endif // !defined(GO_STATEMENTS_H)