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
;
46 // This class is used to traverse assignments made by a statement
47 // which makes assignments.
49 class Traverse_assignments
52 Traverse_assignments()
55 virtual ~Traverse_assignments()
58 // This is called for a variable initialization.
60 initialize_variable(Named_object
*) = 0;
62 // This is called for each assignment made by the statement. PLHS
63 // points to the left hand side, and PRHS points to the right hand
64 // side. PRHS may be NULL if there is no associated expression, as
65 // in the bool set by a non-blocking receive.
67 assignment(Expression
** plhs
, Expression
** prhs
) = 0;
69 // This is called for each expression which is not passed to the
70 // assignment function. This is used for some of the statements
71 // which assign two values, for which there is no expression which
72 // describes the value. For ++ and -- the value is passed to both
73 // the assignment method and the rhs method. IS_STORED is true if
74 // this value is being stored directly. It is false if the value is
75 // computed but not stored. IS_LOCAL is true if the value is being
76 // stored in a local variable or this is being called by a return
79 value(Expression
**, bool is_stored
, bool is_local
) = 0;
82 // A single statement.
87 // The types of statements.
88 enum Statement_classification
91 STATEMENT_VARIABLE_DECLARATION
,
99 STATEMENT_BREAK_OR_CONTINUE
,
101 STATEMENT_GOTO_UNNAMED
,
103 STATEMENT_UNNAMED_LABEL
,
105 STATEMENT_CONSTANT_SWITCH
,
109 // These statements types are created by the parser, but they
110 // disappear during the lowering pass.
111 STATEMENT_ASSIGNMENT_OPERATION
,
112 STATEMENT_TUPLE_ASSIGNMENT
,
113 STATEMENT_TUPLE_MAP_ASSIGNMENT
,
114 STATEMENT_MAP_ASSIGNMENT
,
115 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
,
116 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
,
121 STATEMENT_TYPE_SWITCH
124 Statement(Statement_classification
, source_location
);
126 virtual ~Statement();
128 // Make a variable declaration.
130 make_variable_declaration(Named_object
*);
132 // Make a statement which creates a temporary variable and
133 // initializes it to an expression. The block is used if the
134 // temporary variable has to be explicitly destroyed; the variable
135 // must still be added to the block. References to the temporary
136 // variable may be constructed using make_temporary_reference.
137 // Either the type or the initialization expression may be NULL, but
139 static Temporary_statement
*
140 make_temporary(Type
*, Expression
*, source_location
);
142 // Make an assignment statement.
144 make_assignment(Expression
*, Expression
*, source_location
);
146 // Make an assignment operation (+=, etc.).
148 make_assignment_operation(Operator
, Expression
*, Expression
*,
151 // Make a tuple assignment statement.
153 make_tuple_assignment(Expression_list
*, Expression_list
*, source_location
);
155 // Make an assignment from a map index to a pair of variables.
157 make_tuple_map_assignment(Expression
* val
, Expression
* present
,
158 Expression
*, source_location
);
160 // Make a statement which assigns a pair of values to a map.
162 make_map_assignment(Expression
*, Expression
* val
,
163 Expression
* should_set
, source_location
);
165 // Make an assignment from a nonblocking receive to a pair of
166 // variables. FOR_SELECT is true is this is being created for a
167 // case x, ok := <-c in a select statement.
169 make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
170 Expression
* channel
, bool for_select
,
173 // Make an assignment from a type guard to a pair of variables.
175 make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
176 Expression
* expr
, Type
* type
,
179 // Make an expression statement from an Expression.
181 make_statement(Expression
*);
183 // Make a block statement from a Block. This is an embedded list of
184 // statements which may also include variable definitions.
186 make_block_statement(Block
*, source_location
);
188 // Make an increment statement.
190 make_inc_statement(Expression
*);
192 // Make a decrement statement.
194 make_dec_statement(Expression
*);
196 // Make a go statement.
198 make_go_statement(Call_expression
* call
, source_location
);
200 // Make a defer statement.
202 make_defer_statement(Call_expression
* call
, source_location
);
204 // Make a return statement.
206 make_return_statement(Expression_list
*, source_location
);
208 // Make a break statement.
210 make_break_statement(Unnamed_label
* label
, source_location
);
212 // Make a continue statement.
214 make_continue_statement(Unnamed_label
* label
, source_location
);
216 // Make a goto statement.
218 make_goto_statement(Label
* label
, source_location
);
220 // Make a goto statement to an unnamed label.
222 make_goto_unnamed_statement(Unnamed_label
* label
, source_location
);
224 // Make a label statement--where the label is defined.
226 make_label_statement(Label
* label
, source_location
);
228 // Make an unnamed label statement--where the label is defined.
230 make_unnamed_label_statement(Unnamed_label
* label
);
232 // Make an if statement.
234 make_if_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
237 // Make a switch statement.
238 static Switch_statement
*
239 make_switch_statement(Expression
* switch_val
, source_location
);
241 // Make a type switch statement.
242 static Type_switch_statement
*
243 make_type_switch_statement(Named_object
* var
, Expression
*, source_location
);
245 // Make a send statement.
246 static Send_statement
*
247 make_send_statement(Expression
* channel
, Expression
* val
, source_location
);
249 // Make a select statement.
250 static Select_statement
*
251 make_select_statement(source_location
);
253 // Make a for statement.
254 static For_statement
*
255 make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
256 source_location location
);
258 // Make a for statement with a range clause.
259 static For_range_statement
*
260 make_for_range_statement(Expression
* index_var
, Expression
* value_var
,
261 Expression
* range
, source_location
);
263 // Return the statement classification.
264 Statement_classification
265 classification() const
266 { return this->classification_
; }
268 // Get the statement location.
271 { return this->location_
; }
273 // Traverse the tree.
275 traverse(Block
*, size_t* index
, Traverse
*);
277 // Traverse the contents of this statement--the expressions and
278 // statements which it contains.
280 traverse_contents(Traverse
*);
282 // If this statement assigns some values, it calls a function for
283 // each value to which this statement assigns a value, and returns
284 // true. If this statement does not assign any values, it returns
287 traverse_assignments(Traverse_assignments
* tassign
);
289 // Lower a statement. This is called immediately after parsing to
290 // simplify statements for further processing. It returns the same
291 // Statement or a new one. FUNCTION is the function containing this
292 // statement. BLOCK is the block containing this statement.
294 lower(Gogo
* gogo
, Named_object
* function
, Block
* block
)
295 { return this->do_lower(gogo
, function
, block
); }
297 // Set type information for unnamed constants.
301 // Check types in a statement. This simply checks that any
302 // expressions used by the statement have the right type.
304 check_types(Gogo
* gogo
)
305 { this->do_check_types(gogo
); }
307 // Return whether this is a block statement.
309 is_block_statement() const
310 { return this->classification_
== STATEMENT_BLOCK
; }
312 // If this is a variable declaration statement, return it.
313 // Otherwise return NULL.
314 Variable_declaration_statement
*
315 variable_declaration_statement()
317 return this->convert
<Variable_declaration_statement
,
318 STATEMENT_VARIABLE_DECLARATION
>();
321 // If this is a return statement, return it. Otherwise return NULL.
324 { return this->convert
<Return_statement
, STATEMENT_RETURN
>(); }
326 // If this is a thunk statement (a go or defer statement), return
327 // it. Otherwise return NULL.
331 // If this is a label statement, return it. Otherwise return NULL.
334 { return this->convert
<Label_statement
, STATEMENT_LABEL
>(); }
336 // If this is a for statement, return it. Otherwise return NULL.
339 { return this->convert
<For_statement
, STATEMENT_FOR
>(); }
341 // If this is a for statement over a range clause, return it.
342 // Otherwise return NULL.
344 for_range_statement()
345 { return this->convert
<For_range_statement
, STATEMENT_FOR_RANGE
>(); }
347 // If this is a switch statement, return it. Otherwise return NULL.
350 { return this->convert
<Switch_statement
, STATEMENT_SWITCH
>(); }
352 // If this is a type switch statement, return it. Otherwise return
354 Type_switch_statement
*
355 type_switch_statement()
356 { return this->convert
<Type_switch_statement
, STATEMENT_TYPE_SWITCH
>(); }
358 // If this is a select statement, return it. Otherwise return NULL.
361 { return this->convert
<Select_statement
, STATEMENT_SELECT
>(); }
363 // Return true if this statement may fall through--if after
364 // executing this statement we may go on to execute the following
365 // statement, if any.
367 may_fall_through() const
368 { return this->do_may_fall_through(); }
370 // Convert the statement to the backend representation.
372 get_backend(Translate_context
*);
375 // Implemented by child class: traverse the tree.
377 do_traverse(Traverse
*) = 0;
379 // Implemented by child class: traverse assignments. Any statement
380 // which includes an assignment should implement this.
382 do_traverse_assignments(Traverse_assignments
*)
385 // Implemented by the child class: lower this statement to a simpler
388 do_lower(Gogo
*, Named_object
*, Block
*)
391 // Implemented by child class: set type information for unnamed
392 // constants. Any statement which includes an expression needs to
398 // Implemented by child class: check types of expressions used in a
401 do_check_types(Gogo
*)
404 // Implemented by child class: return true if this statement may
407 do_may_fall_through() const
410 // Implemented by child class: convert to backend representation.
412 do_get_backend(Translate_context
*) = 0;
414 // Traverse an expression in a statement.
416 traverse_expression(Traverse
*, Expression
**);
418 // Traverse an expression list in a statement. The Expression_list
421 traverse_expression_list(Traverse
*, Expression_list
*);
423 // Traverse a type in a statement.
425 traverse_type(Traverse
*, Type
*);
427 // For children to call when they detect that they are in error.
431 // For children to call to report an error conveniently.
433 report_error(const char*);
435 // For children to return an error statement from lower().
437 make_error_statement(source_location
);
440 // Convert to the desired statement classification, or return NULL.
441 // This is a controlled dynamic cast.
442 template<typename Statement_class
, Statement_classification sc
>
446 return (this->classification_
== sc
447 ? static_cast<Statement_class
*>(this)
451 template<typename Statement_class
, Statement_classification sc
>
452 const Statement_class
*
455 return (this->classification_
== sc
456 ? static_cast<const Statement_class
*>(this)
460 // The statement classification.
461 Statement_classification classification_
;
462 // The location in the input file of the start of this statement.
463 source_location location_
;
466 // A statement which creates and initializes a temporary variable.
468 class Temporary_statement
: public Statement
471 Temporary_statement(Type
* type
, Expression
* init
, source_location location
)
472 : Statement(STATEMENT_TEMPORARY
, location
),
473 type_(type
), init_(init
), bvariable_(NULL
), is_address_taken_(false)
476 // Return the type of the temporary variable.
480 // Record that something takes the address of this temporary
483 set_is_address_taken()
484 { this->is_address_taken_
= true; }
486 // Return the temporary variable. This should not be called until
487 // after the statement itself has been converted.
489 get_backend_variable(Translate_context
*) const;
493 do_traverse(Traverse
*);
496 do_traverse_assignments(Traverse_assignments
*);
499 do_determine_types();
502 do_check_types(Gogo
*);
505 do_get_backend(Translate_context
*);
508 // The type of the temporary variable.
510 // The initial value of the temporary variable. This may be NULL.
512 // The backend representation of the temporary variable.
513 Bvariable
* bvariable_
;
514 // True if something takes the address of this temporary variable.
515 bool is_address_taken_
;
518 // A variable declaration. This marks the point in the code where a
519 // variable is declared. The Variable is also attached to a Block.
521 class Variable_declaration_statement
: public Statement
524 Variable_declaration_statement(Named_object
* var
);
526 // The variable being declared.
529 { return this->var_
; }
533 do_traverse(Traverse
*);
536 do_traverse_assignments(Traverse_assignments
*);
539 do_get_backend(Translate_context
*);
545 // A return statement.
547 class Return_statement
: public Statement
550 Return_statement(Expression_list
* vals
, source_location location
)
551 : Statement(STATEMENT_RETURN
, location
),
552 vals_(vals
), is_lowered_(false)
555 // The list of values being returned. This may be NULL.
556 const Expression_list
*
558 { return this->vals_
; }
562 do_traverse(Traverse
* traverse
)
563 { return this->traverse_expression_list(traverse
, this->vals_
); }
566 do_traverse_assignments(Traverse_assignments
*);
569 do_lower(Gogo
*, Named_object
*, Block
*);
572 do_may_fall_through() const
576 do_get_backend(Translate_context
*);
579 // Return values. This may be NULL.
580 Expression_list
* vals_
;
581 // True if this statement has been lowered.
587 class Send_statement
: public Statement
590 Send_statement(Expression
* channel
, Expression
* val
,
591 source_location location
)
592 : Statement(STATEMENT_SEND
, location
),
593 channel_(channel
), val_(val
), for_select_(false)
596 // Note that this is for a select statement.
599 { this->for_select_
= true; }
603 do_traverse(Traverse
* traverse
);
606 do_determine_types();
609 do_check_types(Gogo
*);
612 do_get_backend(Translate_context
*);
615 // The channel on which to send the value.
616 Expression
* channel_
;
617 // The value to send.
619 // Whether this is for a select statement.
623 // Select_clauses holds the clauses of a select statement. This is
624 // built by the parser.
633 // Add a new clause. IS_SEND is true if this is a send clause,
634 // false for a receive clause. For a send clause CHANNEL is the
635 // channel and VAL is the value to send. For a receive clause
636 // CHANNEL is the channel, VAL is either NULL or a Var_expression
637 // for the variable to set, and CLOSED is either NULL or a
638 // Var_expression to set to whether the channel is closed. If VAL
639 // is NULL, VAR may be a variable to be initialized with the
640 // received value, and CLOSEDVAR ma be a variable to be initialized
641 // with whether the channel is closed. IS_DEFAULT is true if this
642 // is the default clause. STATEMENTS is the list of statements to
645 add(bool is_send
, Expression
* channel
, Expression
* val
, Expression
* closed
,
646 Named_object
* var
, Named_object
* closedvar
, bool is_default
,
647 Block
* statements
, source_location location
)
649 this->clauses_
.push_back(Select_clause(is_send
, channel
, val
, closed
, var
,
650 closedvar
, is_default
, statements
,
654 // Traverse the select clauses.
660 lower(Gogo
*, Named_object
*, Block
*);
666 // Whether the select clauses may fall through to the statement
667 // which follows the overall select statement.
669 may_fall_through() const;
671 // Convert to the backend representation.
673 get_backend(Translate_context
*, Unnamed_label
* break_label
, source_location
);
681 : channel_(NULL
), val_(NULL
), closed_(NULL
), var_(NULL
),
682 closedvar_(NULL
), statements_(NULL
), is_send_(false),
686 Select_clause(bool is_send
, Expression
* channel
, Expression
* val
,
687 Expression
* closed
, Named_object
* var
,
688 Named_object
* closedvar
, bool is_default
, Block
* statements
,
689 source_location location
)
690 : channel_(channel
), val_(val
), closed_(closed
), var_(var
),
691 closedvar_(closedvar
), statements_(statements
), location_(location
),
692 is_send_(is_send
), is_default_(is_default
), is_lowered_(false)
693 { go_assert(is_default
? channel
== NULL
: channel
!= NULL
); }
695 // Traverse the select clause.
701 lower(Gogo
*, Named_object
*, Block
*);
707 // Return true if this is the default clause.
710 { return this->is_default_
; }
712 // Return the channel. This will return NULL for the default
716 { return this->channel_
; }
718 // Return true for a send, false for a receive.
722 go_assert(!this->is_default_
);
723 return this->is_send_
;
726 // Return the statements.
729 { return this->statements_
; }
731 // Return the location.
734 { return this->location_
; }
736 // Whether this clause may fall through to the statement which
737 // follows the overall select statement.
739 may_fall_through() const;
741 // Convert the statements to the backend representation.
743 get_statements_backend(Translate_context
*);
747 Expression
* channel_
;
748 // The value to send or the lvalue to receive into.
750 // The lvalue to set to whether the channel is closed on a
753 // The variable to initialize, for "case a := <-ch".
755 // The variable to initialize to whether the channel is closed,
756 // for "case a, c := <-ch".
757 Named_object
* closedvar_
;
758 // The statements to execute.
760 // The location of this clause.
761 source_location location_
;
762 // Whether this is a send or a receive.
764 // Whether this is the default.
766 // Whether this has been lowered.
771 add_clause_backend(Translate_context
*, source_location
, int index
,
772 int case_value
, Select_clause
*, Unnamed_label
*,
773 std::vector
<std::vector
<Bexpression
*> >* cases
,
774 std::vector
<Bstatement
*>* clauses
);
776 typedef std::vector
<Select_clause
> Clauses
;
781 // A select statement.
783 class Select_statement
: public Statement
786 Select_statement(source_location location
)
787 : Statement(STATEMENT_SELECT
, location
),
788 clauses_(NULL
), break_label_(NULL
), is_lowered_(false)
793 add_clauses(Select_clauses
* clauses
)
795 go_assert(this->clauses_
== NULL
);
796 this->clauses_
= clauses
;
799 // Return the break label for this select statement.
805 do_traverse(Traverse
* traverse
)
806 { return this->clauses_
->traverse(traverse
); }
809 do_lower(Gogo
*, Named_object
*, Block
*);
813 { this->clauses_
->determine_types(); }
816 do_may_fall_through() const
817 { return this->clauses_
->may_fall_through(); }
820 do_get_backend(Translate_context
*);
823 // The select clauses.
824 Select_clauses
* clauses_
;
826 Unnamed_label
* break_label_
;
827 // Whether this statement has been lowered.
831 // A statement which requires a thunk: go or defer.
833 class Thunk_statement
: public Statement
836 Thunk_statement(Statement_classification
, Call_expression
*,
839 // Return the call expression.
842 { return this->call_
; }
844 // Simplify a go or defer statement so that it only uses a single
847 simplify_statement(Gogo
*, Named_object
*, Block
*);
851 do_traverse(Traverse
* traverse
);
854 do_traverse_assignments(Traverse_assignments
*);
857 do_determine_types();
860 do_check_types(Gogo
*);
862 // Return the function and argument for the call.
864 get_fn_and_arg(Expression
** pfn
, Expression
** parg
);
867 // Return whether this is a simple go statement.
869 is_simple(Function_type
*) const;
871 // Build the struct to use for a complex case.
873 build_struct(Function_type
* fntype
);
877 build_thunk(Gogo
*, const std::string
&, Function_type
* fntype
);
879 // The field name used in the thunk structure for the function
881 static const char* const thunk_field_fn
;
883 // The field name used in the thunk structure for the receiver, if
885 static const char* const thunk_field_receiver
;
887 // Set the name to use for thunk field N.
889 thunk_field_param(int n
, char* buf
, size_t buflen
);
891 // The function call to be executed in a separate thread (go) or
894 // The type used for a struct to pass to a thunk, if this is not a
896 Struct_type
* struct_type_
;
901 class Go_statement
: public Thunk_statement
904 Go_statement(Call_expression
* call
, source_location location
)
905 : Thunk_statement(STATEMENT_GO
, call
, location
)
910 do_get_backend(Translate_context
*);
913 // A defer statement.
915 class Defer_statement
: public Thunk_statement
918 Defer_statement(Call_expression
* call
, source_location location
)
919 : Thunk_statement(STATEMENT_DEFER
, call
, location
)
924 do_get_backend(Translate_context
*);
927 // A label statement.
929 class Label_statement
: public Statement
932 Label_statement(Label
* label
, source_location location
)
933 : Statement(STATEMENT_LABEL
, location
),
937 // Return the label itself.
940 { return this->label_
; }
944 do_traverse(Traverse
*);
947 do_get_backend(Translate_context
*);
956 class For_statement
: public Statement
959 For_statement(Block
* init
, Expression
* cond
, Block
* post
,
960 source_location location
)
961 : Statement(STATEMENT_FOR
, location
),
962 init_(init
), cond_(cond
), post_(post
), statements_(NULL
),
963 break_label_(NULL
), continue_label_(NULL
)
966 // Add the statements.
968 add_statements(Block
* statements
)
970 go_assert(this->statements_
== NULL
);
971 this->statements_
= statements
;
974 // Return the break label for this for statement.
978 // Return the continue label for this for statement.
982 // Set the break and continue labels for this statement.
984 set_break_continue_labels(Unnamed_label
* break_label
,
985 Unnamed_label
* continue_label
);
989 do_traverse(Traverse
*);
992 do_traverse_assignments(Traverse_assignments
*)
993 { gcc_unreachable(); }
996 do_lower(Gogo
*, Named_object
*, Block
*);
999 do_get_backend(Translate_context
*)
1000 { gcc_unreachable(); }
1003 // The initialization statements. This may be NULL.
1005 // The condition. This may be NULL.
1007 // The statements to run after each iteration. This may be NULL.
1009 // The statements in the loop itself.
1011 // The break label, if needed.
1012 Unnamed_label
* break_label_
;
1013 // The continue label, if needed.
1014 Unnamed_label
* continue_label_
;
1017 // A for statement over a range clause.
1019 class For_range_statement
: public Statement
1022 For_range_statement(Expression
* index_var
, Expression
* value_var
,
1023 Expression
* range
, source_location location
)
1024 : Statement(STATEMENT_FOR_RANGE
, location
),
1025 index_var_(index_var
), value_var_(value_var
), range_(range
),
1026 statements_(NULL
), break_label_(NULL
), continue_label_(NULL
)
1029 // Add the statements.
1031 add_statements(Block
* statements
)
1033 go_assert(this->statements_
== NULL
);
1034 this->statements_
= statements
;
1037 // Return the break label for this for statement.
1041 // Return the continue label for this for statement.
1047 do_traverse(Traverse
*);
1050 do_traverse_assignments(Traverse_assignments
*)
1051 { gcc_unreachable(); }
1054 do_lower(Gogo
*, Named_object
*, Block
*);
1057 do_get_backend(Translate_context
*)
1058 { gcc_unreachable(); }
1062 make_range_ref(Named_object
*, Temporary_statement
*, source_location
);
1065 call_builtin(Gogo
*, const char* funcname
, Expression
* arg
, source_location
);
1068 lower_range_array(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1069 Temporary_statement
*, Temporary_statement
*,
1070 Block
**, Expression
**, Block
**, Block
**);
1073 lower_range_string(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1074 Temporary_statement
*, Temporary_statement
*,
1075 Block
**, Expression
**, Block
**, Block
**);
1078 lower_range_map(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1079 Temporary_statement
*, Temporary_statement
*,
1080 Block
**, Expression
**, Block
**, Block
**);
1083 lower_range_channel(Gogo
*, Block
*, Block
*, Named_object
*,
1084 Temporary_statement
*, Temporary_statement
*,
1085 Temporary_statement
*, Block
**, Expression
**, Block
**,
1088 // The variable which is set to the index value.
1089 Expression
* index_var_
;
1090 // The variable which is set to the element value. This may be
1092 Expression
* value_var_
;
1093 // The expression we are ranging over.
1095 // The statements in the block.
1097 // The break label, if needed.
1098 Unnamed_label
* break_label_
;
1099 // The continue label, if needed.
1100 Unnamed_label
* continue_label_
;
1103 // Class Case_clauses holds the clauses of a switch statement. This
1104 // is built by the parser.
1113 // Add a new clause. CASES is a list of case expressions; it may be
1114 // NULL. IS_DEFAULT is true if this is the default case.
1115 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1116 // after the statements the case clause should fall through to the
1119 add(Expression_list
* cases
, bool is_default
, Block
* statements
,
1120 bool is_fallthrough
, source_location location
)
1122 this->clauses_
.push_back(Case_clause(cases
, is_default
, statements
,
1123 is_fallthrough
, location
));
1126 // Return whether there are no clauses.
1129 { return this->clauses_
.empty(); }
1131 // Traverse the case clauses.
1133 traverse(Traverse
*);
1135 // Lower for a nonconstant switch.
1137 lower(Block
*, Temporary_statement
*, Unnamed_label
*) const;
1139 // Determine types of expressions. The Type parameter is the type
1140 // of the switch value.
1142 determine_types(Type
*);
1144 // Check types. The Type parameter is the type of the switch value.
1148 // Return true if all the clauses are constant values.
1150 is_constant() const;
1152 // Return true if these clauses may fall through to the statements
1153 // following the switch statement.
1155 may_fall_through() const;
1157 // Return the body of a SWITCH_EXPR when all the clauses are
1160 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1161 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
1162 std::vector
<Bstatement
*>* all_statements
) const;
1165 // For a constant switch we need to keep a record of constants we
1166 // have already seen.
1167 class Hash_integer_value
;
1168 class Eq_integer_value
;
1169 typedef Unordered_set_hash(Expression
*, Hash_integer_value
,
1170 Eq_integer_value
) Case_constants
;
1177 : cases_(NULL
), statements_(NULL
), is_default_(false),
1178 is_fallthrough_(false), location_(UNKNOWN_LOCATION
)
1181 Case_clause(Expression_list
* cases
, bool is_default
, Block
* statements
,
1182 bool is_fallthrough
, source_location location
)
1183 : cases_(cases
), statements_(statements
), is_default_(is_default
),
1184 is_fallthrough_(is_fallthrough
), location_(location
)
1187 // Whether this clause falls through to the next clause.
1189 is_fallthrough() const
1190 { return this->is_fallthrough_
; }
1192 // Whether this is the default.
1195 { return this->is_default_
; }
1197 // The location of this clause.
1200 { return this->location_
; }
1204 traverse(Traverse
*);
1206 // Lower for a nonconstant switch.
1208 lower(Block
*, Temporary_statement
*, Unnamed_label
*, Unnamed_label
*) const;
1212 determine_types(Type
*);
1218 // Return true if all the case expressions are constant.
1220 is_constant() const;
1222 // Return true if this clause may fall through to execute the
1223 // statements following the switch statement. This is not the
1224 // same as whether this clause falls through to the next clause.
1226 may_fall_through() const;
1228 // Convert the case values and statements to the backend
1231 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1232 Case_constants
*, std::vector
<Bexpression
*>* cases
) const;
1235 // The list of case expressions.
1236 Expression_list
* cases_
;
1237 // The statements to execute.
1239 // Whether this is the default case.
1241 // Whether this falls through after the statements.
1242 bool is_fallthrough_
;
1243 // The location of this case clause.
1244 source_location location_
;
1247 friend class Case_clause
;
1249 // The type of the list of clauses.
1250 typedef std::vector
<Case_clause
> Clauses
;
1252 // All the case clauses.
1256 // A switch statement.
1258 class Switch_statement
: public Statement
1261 Switch_statement(Expression
* val
, source_location location
)
1262 : Statement(STATEMENT_SWITCH
, location
),
1263 val_(val
), clauses_(NULL
), break_label_(NULL
)
1268 add_clauses(Case_clauses
* clauses
)
1270 go_assert(this->clauses_
== NULL
);
1271 this->clauses_
= clauses
;
1274 // Return the break label for this switch statement.
1280 do_traverse(Traverse
*);
1283 do_lower(Gogo
*, Named_object
*, Block
*);
1286 do_get_backend(Translate_context
*)
1287 { gcc_unreachable(); }
1290 // The value to switch on. This may be NULL.
1292 // The case clauses.
1293 Case_clauses
* clauses_
;
1294 // The break label, if needed.
1295 Unnamed_label
* break_label_
;
1298 // Class Type_case_clauses holds the clauses of a type switch
1299 // statement. This is built by the parser.
1301 class Type_case_clauses
1308 // Add a new clause. TYPE is the type for this clause; it may be
1309 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1310 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1311 // if this is the default case. STATEMENTS is a block of
1312 // statements; it may be NULL.
1314 add(Type
* type
, bool is_fallthrough
, bool is_default
, Block
* statements
,
1315 source_location location
)
1317 this->clauses_
.push_back(Type_case_clause(type
, is_fallthrough
, is_default
,
1318 statements
, location
));
1321 // Return whether there are no clauses.
1324 { return this->clauses_
.empty(); }
1326 // Traverse the type case clauses.
1328 traverse(Traverse
*);
1330 // Check for duplicates.
1332 check_duplicates() const;
1334 // Lower to if and goto statements.
1336 lower(Block
*, Temporary_statement
* descriptor_temp
,
1337 Unnamed_label
* break_label
) const;
1340 // One type case clause.
1341 class Type_case_clause
1345 : type_(NULL
), statements_(NULL
), is_default_(false),
1346 location_(UNKNOWN_LOCATION
)
1349 Type_case_clause(Type
* type
, bool is_fallthrough
, bool is_default
,
1350 Block
* statements
, source_location location
)
1351 : type_(type
), statements_(statements
), is_fallthrough_(is_fallthrough
),
1352 is_default_(is_default
), location_(location
)
1358 { return this->type_
; }
1360 // Whether this is the default.
1363 { return this->is_default_
; }
1365 // The location of this type clause.
1368 { return this->location_
; }
1372 traverse(Traverse
*);
1374 // Lower to if and goto statements.
1376 lower(Block
*, Temporary_statement
* descriptor_temp
,
1377 Unnamed_label
* break_label
, Unnamed_label
** stmts_label
) const;
1380 // The type for this type clause.
1382 // The statements to execute.
1384 // Whether this falls through--this is true for "case T1, T2".
1385 bool is_fallthrough_
;
1386 // Whether this is the default case.
1388 // The location of this type case clause.
1389 source_location location_
;
1392 friend class Type_case_clause
;
1394 // The type of the list of type clauses.
1395 typedef std::vector
<Type_case_clause
> Type_clauses
;
1397 // All the type case clauses.
1398 Type_clauses clauses_
;
1401 // A type switch statement.
1403 class Type_switch_statement
: public Statement
1406 Type_switch_statement(Named_object
* var
, Expression
* expr
,
1407 source_location location
)
1408 : Statement(STATEMENT_TYPE_SWITCH
, location
),
1409 var_(var
), expr_(expr
), clauses_(NULL
), break_label_(NULL
)
1410 { go_assert(var
== NULL
|| expr
== NULL
); }
1414 add_clauses(Type_case_clauses
* clauses
)
1416 go_assert(this->clauses_
== NULL
);
1417 this->clauses_
= clauses
;
1420 // Return the break label for this type switch statement.
1426 do_traverse(Traverse
*);
1429 do_lower(Gogo
*, Named_object
*, Block
*);
1432 do_get_backend(Translate_context
*)
1433 { gcc_unreachable(); }
1436 // The variable holding the value we are switching on.
1438 // The expression we are switching on if there is no variable.
1440 // The type case clauses.
1441 Type_case_clauses
* clauses_
;
1442 // The break label, if needed.
1443 Unnamed_label
* break_label_
;
1446 #endif // !defined(GO_STATEMENTS_H)