1 // statements.h -- Go frontend statements. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #ifndef GO_STATEMENTS_H
8 #define GO_STATEMENTS_H
14 class Statement_inserter
;
18 class Assignment_statement
;
19 class Temporary_statement
;
20 class Variable_declaration_statement
;
21 class Expression_statement
;
22 class Block_statement
;
23 class Return_statement
;
24 class Thunk_statement
;
26 class Goto_unnamed_statement
;
27 class Label_statement
;
28 class Unnamed_label_statement
;
31 class For_range_statement
;
32 class Switch_statement
;
33 class Type_switch_statement
;
35 class Select_statement
;
39 class Translate_context
;
41 class Expression_list
;
43 class Call_expression
;
44 class Map_index_expression
;
45 class Receive_expression
;
47 class Type_case_clauses
;
49 class Typed_identifier_list
;
53 class Ast_dump_context
;
55 // This class is used to traverse assignments made by a statement
56 // which makes assignments.
58 class Traverse_assignments
61 Traverse_assignments()
64 virtual ~Traverse_assignments()
67 // This is called for a variable initialization.
69 initialize_variable(Named_object
*) = 0;
71 // This is called for each assignment made by the statement. PLHS
72 // points to the left hand side, and PRHS points to the right hand
73 // side. PRHS may be NULL if there is no associated expression, as
74 // in the bool set by a non-blocking receive.
76 assignment(Expression
** plhs
, Expression
** prhs
) = 0;
78 // This is called for each expression which is not passed to the
79 // assignment function. This is used for some of the statements
80 // which assign two values, for which there is no expression which
81 // describes the value. For ++ and -- the value is passed to both
82 // the assignment method and the rhs method. IS_STORED is true if
83 // this value is being stored directly. It is false if the value is
84 // computed but not stored. IS_LOCAL is true if the value is being
85 // stored in a local variable or this is being called by a return
88 value(Expression
**, bool is_stored
, bool is_local
) = 0;
91 // A single statement.
96 // The types of statements.
97 enum Statement_classification
100 STATEMENT_VARIABLE_DECLARATION
,
102 STATEMENT_ASSIGNMENT
,
103 STATEMENT_EXPRESSION
,
108 STATEMENT_BREAK_OR_CONTINUE
,
110 STATEMENT_GOTO_UNNAMED
,
112 STATEMENT_UNNAMED_LABEL
,
114 STATEMENT_CONSTANT_SWITCH
,
118 // These statements types are created by the parser, but they
119 // disappear during the lowering pass.
120 STATEMENT_ASSIGNMENT_OPERATION
,
121 STATEMENT_TUPLE_ASSIGNMENT
,
122 STATEMENT_TUPLE_MAP_ASSIGNMENT
,
123 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
,
124 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
,
129 STATEMENT_TYPE_SWITCH
132 Statement(Statement_classification
, Location
);
134 virtual ~Statement();
136 // Make a variable declaration.
138 make_variable_declaration(Named_object
*);
140 // Make a statement which creates a temporary variable and
141 // initializes it to an expression. The block is used if the
142 // temporary variable has to be explicitly destroyed; the variable
143 // must still be added to the block. References to the temporary
144 // variable may be constructed using make_temporary_reference.
145 // Either the type or the initialization expression may be NULL, but
147 static Temporary_statement
*
148 make_temporary(Type
*, Expression
*, Location
);
150 // Make an assignment statement.
151 static Assignment_statement
*
152 make_assignment(Expression
*, Expression
*, Location
);
154 // Make an assignment operation (+=, etc.).
156 make_assignment_operation(Operator
, Expression
*, Expression
*,
159 // Make a tuple assignment statement.
161 make_tuple_assignment(Expression_list
*, Expression_list
*, Location
);
163 // Make an assignment from a map index to a pair of variables.
165 make_tuple_map_assignment(Expression
* val
, Expression
* present
,
166 Expression
*, Location
);
168 // Make an assignment from a nonblocking receive to a pair of
171 make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
172 Expression
* channel
, Location
);
174 // Make an assignment from a type guard to a pair of variables.
176 make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
177 Expression
* expr
, Type
* type
,
180 // Make an expression statement from an Expression. IS_IGNORED is
181 // true if the value is being explicitly ignored, as in an
184 make_statement(Expression
*, bool is_ignored
);
186 // Make a block statement from a Block. This is an embedded list of
187 // statements which may also include variable definitions.
189 make_block_statement(Block
*, Location
);
191 // Make an increment statement.
193 make_inc_statement(Expression
*);
195 // Make a decrement statement.
197 make_dec_statement(Expression
*);
199 // Make a go statement.
201 make_go_statement(Call_expression
* call
, Location
);
203 // Make a defer statement.
205 make_defer_statement(Call_expression
* call
, Location
);
207 // Make a return statement.
208 static Return_statement
*
209 make_return_statement(Expression_list
*, Location
);
211 // Make a statement that returns the result of a call expression.
212 // If the call does not return any results, this just returns the
213 // call expression as a statement, assuming that the function will
214 // end immediately afterward.
216 make_return_from_call(Call_expression
*, Location
);
218 // Make a break statement.
220 make_break_statement(Unnamed_label
* label
, Location
);
222 // Make a continue statement.
224 make_continue_statement(Unnamed_label
* label
, Location
);
226 // Make a goto statement.
228 make_goto_statement(Label
* label
, Location
);
230 // Make a goto statement to an unnamed label.
232 make_goto_unnamed_statement(Unnamed_label
* label
, Location
);
234 // Make a label statement--where the label is defined.
236 make_label_statement(Label
* label
, Location
);
238 // Make an unnamed label statement--where the label is defined.
240 make_unnamed_label_statement(Unnamed_label
* label
);
242 // Make an if statement.
244 make_if_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
247 // Make a switch statement.
248 static Switch_statement
*
249 make_switch_statement(Expression
* switch_val
, Location
);
251 // Make a type switch statement.
252 static Type_switch_statement
*
253 make_type_switch_statement(const std::string
&, Expression
*, Location
);
255 // Make a send statement.
256 static Send_statement
*
257 make_send_statement(Expression
* channel
, Expression
* val
, Location
);
259 // Make a select statement.
260 static Select_statement
*
261 make_select_statement(Location
);
263 // Make a for statement.
264 static For_statement
*
265 make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
268 // Make a for statement with a range clause.
269 static For_range_statement
*
270 make_for_range_statement(Expression
* index_var
, Expression
* value_var
,
271 Expression
* range
, Location
);
273 // Return the statement classification.
274 Statement_classification
275 classification() const
276 { return this->classification_
; }
278 // Get the statement location.
281 { return this->location_
; }
283 // Traverse the tree.
285 traverse(Block
*, size_t* index
, Traverse
*);
287 // Traverse the contents of this statement--the expressions and
288 // statements which it contains.
290 traverse_contents(Traverse
*);
292 // If this statement assigns some values, it calls a function for
293 // each value to which this statement assigns a value, and returns
294 // true. If this statement does not assign any values, it returns
297 traverse_assignments(Traverse_assignments
* tassign
);
299 // Lower a statement. This is called immediately after parsing to
300 // simplify statements for further processing. It returns the same
301 // Statement or a new one. FUNCTION is the function containing this
302 // statement. BLOCK is the block containing this statement.
303 // INSERTER can be used to insert new statements before this one.
305 lower(Gogo
* gogo
, Named_object
* function
, Block
* block
,
306 Statement_inserter
* inserter
)
307 { return this->do_lower(gogo
, function
, block
, inserter
); }
309 // Flatten a statement. This is called immediately after the order of
310 // evaluation rules are applied to statements. It returns the same
311 // Statement or a new one. FUNCTION is the function containing this
312 // statement. BLOCK is the block containing this statement.
313 // INSERTER can be used to insert new statements before this one.
315 flatten(Gogo
* gogo
, Named_object
* function
, Block
* block
,
316 Statement_inserter
* inserter
)
317 { return this->do_flatten(gogo
, function
, block
, inserter
); }
319 // Set type information for unnamed constants.
323 // Check types in a statement. This simply checks that any
324 // expressions used by the statement have the right type.
326 check_types(Gogo
* gogo
)
327 { this->do_check_types(gogo
); }
329 // Return whether this is a block statement.
331 is_block_statement() const
332 { return this->classification_
== STATEMENT_BLOCK
; }
334 // If this is an assignment statement, return it. Otherwise return
336 Assignment_statement
*
337 assignment_statement()
339 return this->convert
<Assignment_statement
, STATEMENT_ASSIGNMENT
>();
342 // If this is an temporary statement, return it. Otherwise return
345 temporary_statement()
347 return this->convert
<Temporary_statement
, STATEMENT_TEMPORARY
>();
350 // If this is a variable declaration statement, return it.
351 // Otherwise return NULL.
352 Variable_declaration_statement
*
353 variable_declaration_statement()
355 return this->convert
<Variable_declaration_statement
,
356 STATEMENT_VARIABLE_DECLARATION
>();
359 // If this is an expression statement, return it. Otherwise return
361 Expression_statement
*
362 expression_statement()
364 return this->convert
<Expression_statement
, STATEMENT_EXPRESSION
>();
367 // If this is an block statement, return it. Otherwise return
371 { return this->convert
<Block_statement
, STATEMENT_BLOCK
>(); }
373 // If this is a return statement, return it. Otherwise return NULL.
376 { return this->convert
<Return_statement
, STATEMENT_RETURN
>(); }
378 // If this is a thunk statement (a go or defer statement), return
379 // it. Otherwise return NULL.
383 // If this is a goto statement, return it. Otherwise return NULL.
386 { return this->convert
<Goto_statement
, STATEMENT_GOTO
>(); }
388 // If this is a goto_unnamed statement, return it. Otherwise return NULL.
389 Goto_unnamed_statement
*
390 goto_unnamed_statement()
391 { return this->convert
<Goto_unnamed_statement
, STATEMENT_GOTO_UNNAMED
>(); }
393 // If this is a label statement, return it. Otherwise return NULL.
396 { return this->convert
<Label_statement
, STATEMENT_LABEL
>(); }
398 // If this is an unnamed_label statement, return it. Otherwise return NULL.
399 Unnamed_label_statement
*
400 unnamed_label_statement()
401 { return this->convert
<Unnamed_label_statement
, STATEMENT_UNNAMED_LABEL
>(); }
403 // If this is an if statement, return it. Otherwise return NULL.
406 { return this->convert
<If_statement
, STATEMENT_IF
>(); }
408 // If this is a for statement, return it. Otherwise return NULL.
411 { return this->convert
<For_statement
, STATEMENT_FOR
>(); }
413 // If this is a for statement over a range clause, return it.
414 // Otherwise return NULL.
416 for_range_statement()
417 { return this->convert
<For_range_statement
, STATEMENT_FOR_RANGE
>(); }
419 // If this is a switch statement, return it. Otherwise return NULL.
422 { return this->convert
<Switch_statement
, STATEMENT_SWITCH
>(); }
424 // If this is a type switch statement, return it. Otherwise return
426 Type_switch_statement
*
427 type_switch_statement()
428 { return this->convert
<Type_switch_statement
, STATEMENT_TYPE_SWITCH
>(); }
430 // If this is a send statement, return it. Otherwise return NULL.
433 { return this->convert
<Send_statement
, STATEMENT_SEND
>(); }
435 // If this is a select statement, return it. Otherwise return NULL.
438 { return this->convert
<Select_statement
, STATEMENT_SELECT
>(); }
440 // Return true if this statement may fall through--if after
441 // executing this statement we may go on to execute the following
442 // statement, if any.
444 may_fall_through() const
445 { return this->do_may_fall_through(); }
447 // Convert the statement to the backend representation.
449 get_backend(Translate_context
*);
451 // Dump AST representation of a statement to a dump context.
453 dump_statement(Ast_dump_context
*) const;
456 // Implemented by child class: traverse the tree.
458 do_traverse(Traverse
*) = 0;
460 // Implemented by child class: traverse assignments. Any statement
461 // which includes an assignment should implement this.
463 do_traverse_assignments(Traverse_assignments
*)
466 // Implemented by the child class: lower this statement to a simpler
469 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
472 // Implemented by the child class: lower this statement to a simpler
475 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
478 // Implemented by child class: set type information for unnamed
479 // constants. Any statement which includes an expression needs to
485 // Implemented by child class: check types of expressions used in a
488 do_check_types(Gogo
*)
491 // Implemented by child class: return true if this statement may
494 do_may_fall_through() const
497 // Implemented by child class: convert to backend representation.
499 do_get_backend(Translate_context
*) = 0;
501 // Implemented by child class: dump ast representation.
503 do_dump_statement(Ast_dump_context
*) const = 0;
505 // Traverse an expression in a statement.
507 traverse_expression(Traverse
*, Expression
**);
509 // Traverse an expression list in a statement. The Expression_list
512 traverse_expression_list(Traverse
*, Expression_list
*);
514 // Traverse a type in a statement.
516 traverse_type(Traverse
*, Type
*);
518 // For children to call when they detect that they are in error.
522 // For children to call to report an error conveniently.
524 report_error(const char*);
526 // For children to return an error statement from lower().
528 make_error_statement(Location
);
531 // Convert to the desired statement classification, or return NULL.
532 // This is a controlled dynamic cast.
533 template<typename Statement_class
, Statement_classification sc
>
537 return (this->classification_
== sc
538 ? static_cast<Statement_class
*>(this)
542 template<typename Statement_class
, Statement_classification sc
>
543 const Statement_class
*
546 return (this->classification_
== sc
547 ? static_cast<const Statement_class
*>(this)
551 // The statement classification.
552 Statement_classification classification_
;
553 // The location in the input file of the start of this statement.
557 // An assignment statement.
559 class Assignment_statement
: public Statement
562 Assignment_statement(Expression
* lhs
, Expression
* rhs
,
564 : Statement(STATEMENT_ASSIGNMENT
, location
),
565 lhs_(lhs
), rhs_(rhs
), omit_write_barrier_(false)
570 { return this->lhs_
; }
574 { return this->rhs_
; }
577 omit_write_barrier() const
578 { return this->omit_write_barrier_
; }
581 set_omit_write_barrier()
582 { this->omit_write_barrier_
= true; }
586 do_traverse(Traverse
* traverse
);
589 do_traverse_assignments(Traverse_assignments
*);
592 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
595 do_determine_types();
598 do_check_types(Gogo
*);
601 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
604 do_get_backend(Translate_context
*);
607 do_dump_statement(Ast_dump_context
*) const;
610 // Left hand side--the lvalue.
612 // Right hand side--the rvalue.
614 // True if we can omit a write barrier from this assignment.
615 bool omit_write_barrier_
;
618 // A statement which creates and initializes a temporary variable.
620 class Temporary_statement
: public Statement
623 Temporary_statement(Type
* type
, Expression
* init
, Location location
)
624 : Statement(STATEMENT_TEMPORARY
, location
),
625 type_(type
), init_(init
), bvariable_(NULL
), is_address_taken_(false),
626 value_escapes_(false)
629 // Return the type of the temporary variable.
633 // Return the initializer if there is one.
636 { return this->init_
; }
638 // Record that something takes the address of this temporary
641 set_is_address_taken()
642 { this->is_address_taken_
= true; }
644 // Whether the value escapes.
646 value_escapes() const
647 { return this->value_escapes_
; }
649 // Record that the value escapes.
652 { this->value_escapes_
= true; }
654 // Return the temporary variable. This should not be called until
655 // after the statement itself has been converted.
657 get_backend_variable(Translate_context
*) const;
661 do_traverse(Traverse
*);
664 do_traverse_assignments(Traverse_assignments
*);
667 do_determine_types();
670 do_check_types(Gogo
*);
673 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
676 do_get_backend(Translate_context
*);
679 do_dump_statement(Ast_dump_context
*) const;
682 // The type of the temporary variable.
684 // The initial value of the temporary variable. This may be NULL.
686 // The backend representation of the temporary variable.
687 Bvariable
* bvariable_
;
688 // True if something takes the address of this temporary variable.
689 bool is_address_taken_
;
690 // True if the value assigned to this temporary variable escapes.
691 // This is used for select statements.
695 // A variable declaration. This marks the point in the code where a
696 // variable is declared. The Variable is also attached to a Block.
698 class Variable_declaration_statement
: public Statement
701 Variable_declaration_statement(Named_object
* var
);
703 // The variable being declared.
706 { return this->var_
; }
710 do_traverse(Traverse
*);
713 do_traverse_assignments(Traverse_assignments
*);
716 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
719 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
722 do_get_backend(Translate_context
*);
725 do_dump_statement(Ast_dump_context
*) const;
731 // A return statement.
733 class Return_statement
: public Statement
736 Return_statement(Expression_list
* vals
, Location location
)
737 : Statement(STATEMENT_RETURN
, location
),
738 vals_(vals
), is_lowered_(false)
741 // The list of values being returned. This may be NULL.
742 const Expression_list
*
744 { return this->vals_
; }
748 do_traverse(Traverse
* traverse
)
749 { return this->traverse_expression_list(traverse
, this->vals_
); }
752 do_traverse_assignments(Traverse_assignments
*);
755 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
758 do_may_fall_through() const
762 do_get_backend(Translate_context
*);
765 do_dump_statement(Ast_dump_context
*) const;
768 // Return values. This may be NULL.
769 Expression_list
* vals_
;
770 // True if this statement has been lowered.
774 // An expression statement.
776 class Expression_statement
: public Statement
779 Expression_statement(Expression
* expr
, bool is_ignored
);
783 { return this->expr_
; }
787 do_traverse(Traverse
* traverse
)
788 { return this->traverse_expression(traverse
, &this->expr_
); }
791 do_determine_types();
794 do_check_types(Gogo
*);
797 do_may_fall_through() const;
800 do_get_backend(Translate_context
* context
);
803 do_dump_statement(Ast_dump_context
*) const;
807 // Whether the value of this expression is being explicitly ignored.
811 // A block statement--a list of statements which may include variable
814 class Block_statement
: public Statement
817 Block_statement(Block
* block
, Location location
)
818 : Statement(STATEMENT_BLOCK
, location
),
819 block_(block
), is_lowered_for_statement_(false)
823 set_is_lowered_for_statement()
824 { this->is_lowered_for_statement_
= true; }
827 is_lowered_for_statement()
828 { return this->is_lowered_for_statement_
; }
832 do_traverse(Traverse
* traverse
)
833 { return this->block_
->traverse(traverse
); }
837 { this->block_
->determine_types(); }
840 do_may_fall_through() const
841 { return this->block_
->may_fall_through(); }
844 do_get_backend(Translate_context
* context
);
847 do_dump_statement(Ast_dump_context
*) const;
851 // True if this block statement represents a lowered for statement.
852 bool is_lowered_for_statement_
;
857 class Send_statement
: public Statement
860 Send_statement(Expression
* channel
, Expression
* val
,
862 : Statement(STATEMENT_SEND
, location
),
863 channel_(channel
), val_(val
)
868 { return this->channel_
; }
872 { return this->val_
; }
876 do_traverse(Traverse
* traverse
);
879 do_determine_types();
882 do_check_types(Gogo
*);
885 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
888 do_get_backend(Translate_context
*);
891 do_dump_statement(Ast_dump_context
*) const;
894 // The channel on which to send the value.
895 Expression
* channel_
;
896 // The value to send.
900 // Select_clauses holds the clauses of a select statement. This is
901 // built by the parser.
910 // Add a new clause. IS_SEND is true if this is a send clause,
911 // false for a receive clause. For a send clause CHANNEL is the
912 // channel and VAL is the value to send. For a receive clause
913 // CHANNEL is the channel, VAL is either NULL or a Var_expression
914 // for the variable to set, and CLOSED is either NULL or a
915 // Var_expression to set to whether the channel is closed. If VAL
916 // is NULL, VAR may be a variable to be initialized with the
917 // received value, and CLOSEDVAR ma be a variable to be initialized
918 // with whether the channel is closed. IS_DEFAULT is true if this
919 // is the default clause. STATEMENTS is the list of statements to
922 add(bool is_send
, Expression
* channel
, Expression
* val
, Expression
* closed
,
923 Named_object
* var
, Named_object
* closedvar
, bool is_default
,
924 Block
* statements
, Location location
)
926 this->clauses_
.push_back(Select_clause(is_send
, channel
, val
, closed
, var
,
927 closedvar
, is_default
, statements
,
933 { return this->clauses_
.size(); }
935 // Traverse the select clauses.
941 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*,
942 Temporary_statement
*);
952 // Whether the select clauses may fall through to the statement
953 // which follows the overall select statement.
955 may_fall_through() const;
957 // Convert to the backend representation.
959 get_backend(Translate_context
*, Temporary_statement
* index
,
960 Unnamed_label
* break_label
, Location
);
962 // Dump AST representation.
964 dump_clauses(Ast_dump_context
*) const;
972 : channel_(NULL
), val_(NULL
), closed_(NULL
), var_(NULL
),
973 closedvar_(NULL
), statements_(NULL
), is_send_(false),
977 Select_clause(bool is_send
, Expression
* channel
, Expression
* val
,
978 Expression
* closed
, Named_object
* var
,
979 Named_object
* closedvar
, bool is_default
, Block
* statements
,
981 : channel_(channel
), val_(val
), closed_(closed
), var_(var
),
982 closedvar_(closedvar
), statements_(statements
), location_(location
),
983 is_send_(is_send
), is_default_(is_default
), is_lowered_(false)
984 { go_assert(is_default
? channel
== NULL
: channel
!= NULL
); }
986 // Traverse the select clause.
992 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*, size_t,
993 Temporary_statement
*);
1003 // Return true if this is the default clause.
1006 { return this->is_default_
; }
1008 // Return the channel. This will return NULL for the default
1012 { return this->channel_
; }
1014 // Return true for a send, false for a receive.
1018 go_assert(!this->is_default_
);
1019 return this->is_send_
;
1022 // Return the statements.
1025 { return this->statements_
; }
1027 // Return the location.
1030 { return this->location_
; }
1032 // Whether this clause may fall through to the statement which
1033 // follows the overall select statement.
1035 may_fall_through() const;
1037 // Convert the statements to the backend representation.
1039 get_statements_backend(Translate_context
*);
1041 // Dump AST representation.
1043 dump_clause(Ast_dump_context
*) const;
1046 // These values must match the values in libgo/go/runtime/select.go.
1055 lower_default(Block
*, Expression
*);
1058 lower_send(Block
*, Expression
*, Expression
*);
1061 lower_recv(Gogo
*, Named_object
*, Block
*, Expression
*, Expression
*,
1062 Temporary_statement
*);
1065 set_case(Block
*, Expression
*, Expression
*, Expression
*, int);
1068 Expression
* channel_
;
1069 // The value to send or the lvalue to receive into.
1071 // The lvalue to set to whether the channel is closed on a
1073 Expression
* closed_
;
1074 // The variable to initialize, for "case a := <-ch".
1076 // The variable to initialize to whether the channel is closed,
1077 // for "case a, c := <-ch".
1078 Named_object
* closedvar_
;
1079 // The statements to execute.
1081 // The location of this clause.
1083 // Whether this is a send or a receive.
1085 // Whether this is the default.
1087 // Whether this has been lowered.
1091 typedef std::vector
<Select_clause
> Clauses
;
1096 // A select statement.
1098 class Select_statement
: public Statement
1101 Select_statement(Location location
)
1102 : Statement(STATEMENT_SELECT
, location
),
1103 clauses_(NULL
), index_(NULL
), break_label_(NULL
), is_lowered_(false)
1108 add_clauses(Select_clauses
* clauses
)
1110 go_assert(this->clauses_
== NULL
);
1111 this->clauses_
= clauses
;
1114 // Return the break label for this select statement.
1120 do_traverse(Traverse
* traverse
)
1121 { return this->clauses_
->traverse(traverse
); }
1124 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1127 do_determine_types()
1128 { this->clauses_
->determine_types(); }
1131 do_check_types(Gogo
*)
1132 { this->clauses_
->check_types(); }
1135 do_may_fall_through() const;
1138 do_get_backend(Translate_context
*);
1141 do_dump_statement(Ast_dump_context
*) const;
1144 // The select clauses.
1145 Select_clauses
* clauses_
;
1146 // A temporary that holds the index value returned by selectgo.
1147 Temporary_statement
* index_
;
1149 Unnamed_label
* break_label_
;
1150 // Whether this statement has been lowered.
1154 // A statement which requires a thunk: go or defer.
1156 class Thunk_statement
: public Statement
1159 Thunk_statement(Statement_classification
, Call_expression
*,
1162 // Return the call expression.
1165 { return this->call_
; }
1167 // Simplify a go or defer statement so that it only uses a single
1170 simplify_statement(Gogo
*, Named_object
*, Block
*);
1174 do_traverse(Traverse
* traverse
);
1177 do_traverse_assignments(Traverse_assignments
*);
1180 do_determine_types();
1183 do_check_types(Gogo
*);
1185 // Return the function and argument for the call.
1187 get_fn_and_arg(Expression
** pfn
, Expression
** parg
);
1190 // Return whether this is a simple go statement.
1192 is_simple(Function_type
*) const;
1194 // Return whether the thunk function is a constant.
1196 is_constant_function() const;
1198 // Build the struct to use for a complex case.
1200 build_struct(Function_type
* fntype
);
1204 build_thunk(Gogo
*, const std::string
&);
1206 // Set the name to use for thunk field N.
1208 thunk_field_param(int n
, char* buf
, size_t buflen
);
1210 // The function call to be executed in a separate thread (go) or
1213 // The type used for a struct to pass to a thunk, if this is not a
1215 Struct_type
* struct_type_
;
1220 class Go_statement
: public Thunk_statement
1223 Go_statement(Call_expression
* call
, Location location
)
1224 : Thunk_statement(STATEMENT_GO
, call
, location
)
1229 do_get_backend(Translate_context
*);
1232 do_dump_statement(Ast_dump_context
*) const;
1235 // A defer statement.
1237 class Defer_statement
: public Thunk_statement
1240 Defer_statement(Call_expression
* call
, Location location
)
1241 : Thunk_statement(STATEMENT_DEFER
, call
, location
)
1246 do_get_backend(Translate_context
*);
1249 do_dump_statement(Ast_dump_context
*) const;
1252 // A goto statement.
1254 class Goto_statement
: public Statement
1257 Goto_statement(Label
* label
, Location location
)
1258 : Statement(STATEMENT_GOTO
, location
),
1262 // Return the label being jumped to.
1265 { return this->label_
; }
1269 do_traverse(Traverse
*);
1272 do_check_types(Gogo
*);
1275 do_may_fall_through() const
1279 do_get_backend(Translate_context
*);
1282 do_dump_statement(Ast_dump_context
*) const;
1288 // A goto statement to an unnamed label.
1290 class Goto_unnamed_statement
: public Statement
1293 Goto_unnamed_statement(Unnamed_label
* label
, Location location
)
1294 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
1299 unnamed_label() const
1300 { return this->label_
; }
1304 do_traverse(Traverse
*);
1307 do_may_fall_through() const
1311 do_get_backend(Translate_context
* context
);
1314 do_dump_statement(Ast_dump_context
*) const;
1317 Unnamed_label
* label_
;
1320 // A label statement.
1322 class Label_statement
: public Statement
1325 Label_statement(Label
* label
, Location location
)
1326 : Statement(STATEMENT_LABEL
, location
),
1330 // Return the label itself.
1333 { return this->label_
; }
1337 do_traverse(Traverse
*);
1340 do_get_backend(Translate_context
*);
1343 do_dump_statement(Ast_dump_context
*) const;
1350 // An unnamed label statement.
1352 class Unnamed_label_statement
: public Statement
1355 Unnamed_label_statement(Unnamed_label
* label
);
1359 do_traverse(Traverse
*);
1362 do_get_backend(Translate_context
* context
);
1365 do_dump_statement(Ast_dump_context
*) const;
1369 Unnamed_label
* label_
;
1374 class If_statement
: public Statement
1377 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
1379 : Statement(STATEMENT_IF
, location
),
1380 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
1385 { return this->cond_
; }
1389 do_traverse(Traverse
*);
1392 do_determine_types();
1395 do_check_types(Gogo
*);
1398 do_may_fall_through() const;
1401 do_get_backend(Translate_context
*);
1404 do_dump_statement(Ast_dump_context
*) const;
1414 class For_statement
: public Statement
1417 For_statement(Block
* init
, Expression
* cond
, Block
* post
,
1419 : Statement(STATEMENT_FOR
, location
),
1420 init_(init
), cond_(cond
), post_(post
), statements_(NULL
),
1421 break_label_(NULL
), continue_label_(NULL
)
1424 // Add the statements.
1426 add_statements(Block
* statements
)
1428 go_assert(this->statements_
== NULL
);
1429 this->statements_
= statements
;
1432 // Return the break label for this for statement.
1436 // Return the continue label for this for statement.
1440 // Set the break and continue labels for this statement.
1442 set_break_continue_labels(Unnamed_label
* break_label
,
1443 Unnamed_label
* continue_label
);
1447 do_traverse(Traverse
*);
1450 do_traverse_assignments(Traverse_assignments
*)
1451 { go_unreachable(); }
1454 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1457 do_may_fall_through() const;
1460 do_get_backend(Translate_context
*)
1461 { go_unreachable(); }
1464 do_dump_statement(Ast_dump_context
*) const;
1467 // The initialization statements. This may be NULL.
1469 // The condition. This may be NULL.
1471 // The statements to run after each iteration. This may be NULL.
1473 // The statements in the loop itself.
1475 // The break label, if needed.
1476 Unnamed_label
* break_label_
;
1477 // The continue label, if needed.
1478 Unnamed_label
* continue_label_
;
1481 // A for statement over a range clause.
1483 class For_range_statement
: public Statement
1486 For_range_statement(Expression
* index_var
, Expression
* value_var
,
1487 Expression
* range
, Location location
)
1488 : Statement(STATEMENT_FOR_RANGE
, location
),
1489 index_var_(index_var
), value_var_(value_var
), range_(range
),
1490 statements_(NULL
), break_label_(NULL
), continue_label_(NULL
)
1493 // Add the statements.
1495 add_statements(Block
* statements
)
1497 go_assert(this->statements_
== NULL
);
1498 this->statements_
= statements
;
1501 // Return the break label for this for statement.
1505 // Return the continue label for this for statement.
1511 do_traverse(Traverse
*);
1514 do_traverse_assignments(Traverse_assignments
*)
1515 { go_unreachable(); }
1518 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1521 do_get_backend(Translate_context
*)
1522 { go_unreachable(); }
1525 do_dump_statement(Ast_dump_context
*) const;
1529 make_range_ref(Named_object
*, Temporary_statement
*, Location
);
1532 call_builtin(Gogo
*, const char* funcname
, Expression
* arg
, Location
);
1535 lower_range_array(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1536 Temporary_statement
*, Temporary_statement
*,
1537 Block
**, Expression
**, Block
**, Block
**);
1540 lower_range_slice(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1541 Temporary_statement
*, Temporary_statement
*,
1542 Block
**, Expression
**, Block
**, Block
**);
1545 lower_range_string(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1546 Temporary_statement
*, Temporary_statement
*,
1547 Block
**, Expression
**, Block
**, Block
**);
1550 lower_range_map(Gogo
*, Map_type
*, Block
*, Block
*, Named_object
*,
1551 Temporary_statement
*, Temporary_statement
*,
1552 Temporary_statement
*, Block
**, Expression
**, Block
**,
1556 lower_range_channel(Gogo
*, Block
*, Block
*, Named_object
*,
1557 Temporary_statement
*, Temporary_statement
*,
1558 Temporary_statement
*, Block
**, Expression
**, Block
**,
1561 // The variable which is set to the index value.
1562 Expression
* index_var_
;
1563 // The variable which is set to the element value. This may be
1565 Expression
* value_var_
;
1566 // The expression we are ranging over.
1568 // The statements in the block.
1570 // The break label, if needed.
1571 Unnamed_label
* break_label_
;
1572 // The continue label, if needed.
1573 Unnamed_label
* continue_label_
;
1576 // Class Case_clauses holds the clauses of a switch statement. This
1577 // is built by the parser.
1586 // Add a new clause. CASES is a list of case expressions; it may be
1587 // NULL. IS_DEFAULT is true if this is the default case.
1588 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1589 // after the statements the case clause should fall through to the
1592 add(Expression_list
* cases
, bool is_default
, Block
* statements
,
1593 bool is_fallthrough
, Location location
)
1595 this->clauses_
.push_back(Case_clause(cases
, is_default
, statements
,
1596 is_fallthrough
, location
));
1599 // Return whether there are no clauses.
1602 { return this->clauses_
.empty(); }
1604 // Traverse the case clauses.
1606 traverse(Traverse
*);
1608 // Lower for a nonconstant switch.
1610 lower(Block
*, Temporary_statement
*, Unnamed_label
*) const;
1612 // Determine types of expressions. The Type parameter is the type
1613 // of the switch value.
1615 determine_types(Type
*);
1617 // Check types. The Type parameter is the type of the switch value.
1621 // Return true if all the clauses are constant values.
1623 is_constant() const;
1625 // Return true if these clauses may fall through to the statements
1626 // following the switch statement.
1628 may_fall_through() const;
1630 // Return the body of a SWITCH_EXPR when all the clauses are
1633 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1634 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
1635 std::vector
<Bstatement
*>* all_statements
) const;
1637 // Dump the AST representation to a dump context.
1639 dump_clauses(Ast_dump_context
*) const;
1642 // For a constant switch we need to keep a record of constants we
1643 // have already seen.
1644 class Hash_integer_value
;
1645 class Eq_integer_value
;
1646 typedef Unordered_set_hash(Expression
*, Hash_integer_value
,
1647 Eq_integer_value
) Case_constants
;
1654 : cases_(NULL
), statements_(NULL
), is_default_(false),
1655 is_fallthrough_(false), location_(Linemap::unknown_location())
1658 Case_clause(Expression_list
* cases
, bool is_default
, Block
* statements
,
1659 bool is_fallthrough
, Location location
)
1660 : cases_(cases
), statements_(statements
), is_default_(is_default
),
1661 is_fallthrough_(is_fallthrough
), location_(location
)
1664 // Whether this clause falls through to the next clause.
1666 is_fallthrough() const
1667 { return this->is_fallthrough_
; }
1669 // Whether this is the default.
1672 { return this->is_default_
; }
1674 // The location of this clause.
1677 { return this->location_
; }
1681 traverse(Traverse
*);
1683 // Lower for a nonconstant switch.
1685 lower(Block
*, Temporary_statement
*, Unnamed_label
*, Unnamed_label
*) const;
1689 determine_types(Type
*);
1695 // Return true if all the case expressions are constant.
1697 is_constant() const;
1699 // Return true if this clause may fall through to execute the
1700 // statements following the switch statement. This is not the
1701 // same as whether this clause falls through to the next clause.
1703 may_fall_through() const;
1705 // Convert the case values and statements to the backend
1708 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1709 Case_constants
*, std::vector
<Bexpression
*>* cases
) const;
1711 // Dump the AST representation to a dump context.
1713 dump_clause(Ast_dump_context
*) const;
1716 // The list of case expressions.
1717 Expression_list
* cases_
;
1718 // The statements to execute.
1720 // Whether this is the default case.
1722 // Whether this falls through after the statements.
1723 bool is_fallthrough_
;
1724 // The location of this case clause.
1728 friend class Case_clause
;
1730 // The type of the list of clauses.
1731 typedef std::vector
<Case_clause
> Clauses
;
1733 // All the case clauses.
1737 // A switch statement.
1739 class Switch_statement
: public Statement
1742 Switch_statement(Expression
* val
, Location location
)
1743 : Statement(STATEMENT_SWITCH
, location
),
1744 val_(val
), clauses_(NULL
), break_label_(NULL
)
1749 add_clauses(Case_clauses
* clauses
)
1751 go_assert(this->clauses_
== NULL
);
1752 this->clauses_
= clauses
;
1755 // Return the break label for this switch statement.
1761 do_traverse(Traverse
*);
1764 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1767 do_get_backend(Translate_context
*)
1768 { go_unreachable(); }
1771 do_dump_statement(Ast_dump_context
*) const;
1774 do_may_fall_through() const;
1777 // The value to switch on. This may be NULL.
1779 // The case clauses.
1780 Case_clauses
* clauses_
;
1781 // The break label, if needed.
1782 Unnamed_label
* break_label_
;
1785 // Class Type_case_clauses holds the clauses of a type switch
1786 // statement. This is built by the parser.
1788 class Type_case_clauses
1795 // Add a new clause. TYPE is the type for this clause; it may be
1796 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1797 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1798 // if this is the default case. STATEMENTS is a block of
1799 // statements; it may be NULL.
1801 add(Type
* type
, bool is_fallthrough
, bool is_default
, Block
* statements
,
1804 this->clauses_
.push_back(Type_case_clause(type
, is_fallthrough
, is_default
,
1805 statements
, location
));
1808 // Return whether there are no clauses.
1811 { return this->clauses_
.empty(); }
1813 // Traverse the type case clauses.
1815 traverse(Traverse
*);
1817 // Check for duplicates.
1819 check_duplicates() const;
1821 // Lower to if and goto statements.
1823 lower(Type
*, Block
*, Temporary_statement
* descriptor_temp
,
1824 Unnamed_label
* break_label
) const;
1826 // Return true if these clauses may fall through to the statements
1827 // following the switch statement.
1829 may_fall_through() const;
1831 // Dump the AST representation to a dump context.
1833 dump_clauses(Ast_dump_context
*) const;
1836 // One type case clause.
1837 class Type_case_clause
1841 : type_(NULL
), statements_(NULL
), is_default_(false),
1842 location_(Linemap::unknown_location())
1845 Type_case_clause(Type
* type
, bool is_fallthrough
, bool is_default
,
1846 Block
* statements
, Location location
)
1847 : type_(type
), statements_(statements
), is_fallthrough_(is_fallthrough
),
1848 is_default_(is_default
), location_(location
)
1854 { return this->type_
; }
1856 // Whether this is the default.
1859 { return this->is_default_
; }
1861 // The location of this type clause.
1864 { return this->location_
; }
1868 traverse(Traverse
*);
1870 // Lower to if and goto statements.
1872 lower(Type
*, Block
*, Temporary_statement
* descriptor_temp
,
1873 Unnamed_label
* break_label
, Unnamed_label
** stmts_label
) const;
1875 // Return true if this clause may fall through to execute the
1876 // statements following the switch statement. This is not the
1877 // same as whether this clause falls through to the next clause.
1879 may_fall_through() const;
1881 // Dump the AST representation to a dump context.
1883 dump_clause(Ast_dump_context
*) const;
1886 // The type for this type clause.
1888 // The statements to execute.
1890 // Whether this falls through--this is true for "case T1, T2".
1891 bool is_fallthrough_
;
1892 // Whether this is the default case.
1894 // The location of this type case clause.
1898 friend class Type_case_clause
;
1900 // The type of the list of type clauses.
1901 typedef std::vector
<Type_case_clause
> Type_clauses
;
1903 // All the type case clauses.
1904 Type_clauses clauses_
;
1907 // A type switch statement.
1909 class Type_switch_statement
: public Statement
1912 Type_switch_statement(const std::string
& name
, Expression
* expr
,
1914 : Statement(STATEMENT_TYPE_SWITCH
, location
),
1915 name_(name
), expr_(expr
), clauses_(NULL
), break_label_(NULL
)
1920 add_clauses(Type_case_clauses
* clauses
)
1922 go_assert(this->clauses_
== NULL
);
1923 this->clauses_
= clauses
;
1926 // Return the break label for this type switch statement.
1932 do_traverse(Traverse
*);
1935 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1938 do_get_backend(Translate_context
*)
1939 { go_unreachable(); }
1942 do_dump_statement(Ast_dump_context
*) const;
1945 do_may_fall_through() const;
1948 // The name of the variable declared in the type switch guard. Empty if there
1949 // is no variable declared.
1951 // The expression we are switching on if there is no variable.
1953 // The type case clauses.
1954 Type_case_clauses
* clauses_
;
1955 // The break label, if needed.
1956 Unnamed_label
* break_label_
;
1959 #endif // !defined(GO_STATEMENTS_H)