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 Export_function_body
;
19 class Import_function_body
;
20 class Assignment_statement
;
21 class Temporary_statement
;
22 class Variable_declaration_statement
;
23 class Expression_statement
;
24 class Block_statement
;
25 class Return_statement
;
26 class Thunk_statement
;
28 class Goto_unnamed_statement
;
29 class Label_statement
;
30 class Unnamed_label_statement
;
33 class For_range_statement
;
34 class Switch_statement
;
35 class Type_switch_statement
;
37 class Select_statement
;
41 class Translate_context
;
43 class Expression_list
;
45 class Call_expression
;
46 class Map_index_expression
;
47 class Receive_expression
;
49 class Type_case_clauses
;
51 class Typed_identifier_list
;
55 class Ast_dump_context
;
57 // This class is used to traverse assignments made by a statement
58 // which makes assignments.
60 class Traverse_assignments
63 Traverse_assignments()
66 virtual ~Traverse_assignments()
69 // This is called for a variable initialization.
71 initialize_variable(Named_object
*) = 0;
73 // This is called for each assignment made by the statement. PLHS
74 // points to the left hand side, and PRHS points to the right hand
75 // side. PRHS may be NULL if there is no associated expression, as
76 // in the bool set by a non-blocking receive.
78 assignment(Expression
** plhs
, Expression
** prhs
) = 0;
80 // This is called for each expression which is not passed to the
81 // assignment function. This is used for some of the statements
82 // which assign two values, for which there is no expression which
83 // describes the value. For ++ and -- the value is passed to both
84 // the assignment method and the rhs method. IS_STORED is true if
85 // this value is being stored directly. It is false if the value is
86 // computed but not stored. IS_LOCAL is true if the value is being
87 // stored in a local variable or this is being called by a return
90 value(Expression
**, bool is_stored
, bool is_local
) = 0;
93 // A single statement.
98 // The types of statements.
99 enum Statement_classification
102 STATEMENT_VARIABLE_DECLARATION
,
104 STATEMENT_ASSIGNMENT
,
105 STATEMENT_EXPRESSION
,
110 STATEMENT_BREAK_OR_CONTINUE
,
112 STATEMENT_GOTO_UNNAMED
,
114 STATEMENT_UNNAMED_LABEL
,
116 STATEMENT_CONSTANT_SWITCH
,
120 // These statements types are created by the parser, but they
121 // disappear during the lowering pass.
122 STATEMENT_ASSIGNMENT_OPERATION
,
123 STATEMENT_TUPLE_ASSIGNMENT
,
124 STATEMENT_TUPLE_MAP_ASSIGNMENT
,
125 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
,
126 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
,
131 STATEMENT_TYPE_SWITCH
134 Statement(Statement_classification
, Location
);
136 virtual ~Statement();
138 // Make a variable declaration.
140 make_variable_declaration(Named_object
*);
142 // Make a statement which creates a temporary variable and
143 // initializes it to an expression. The block is used if the
144 // temporary variable has to be explicitly destroyed; the variable
145 // must still be added to the block. References to the temporary
146 // variable may be constructed using make_temporary_reference.
147 // Either the type or the initialization expression may be NULL, but
149 static Temporary_statement
*
150 make_temporary(Type
*, Expression
*, Location
);
152 // Make an assignment statement.
153 static Assignment_statement
*
154 make_assignment(Expression
*, Expression
*, Location
);
156 // Make an assignment operation (+=, etc.).
158 make_assignment_operation(Operator
, Expression
*, Expression
*,
161 // Make a tuple assignment statement.
163 make_tuple_assignment(Expression_list
*, Expression_list
*, Location
);
165 // Make an assignment from a map index to a pair of variables.
167 make_tuple_map_assignment(Expression
* val
, Expression
* present
,
168 Expression
*, Location
);
170 // Make an assignment from a nonblocking receive to a pair of
173 make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
174 Expression
* channel
, Location
);
176 // Make an assignment from a type guard to a pair of variables.
178 make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
179 Expression
* expr
, Type
* type
,
182 // Make an expression statement from an Expression. IS_IGNORED is
183 // true if the value is being explicitly ignored, as in an
186 make_statement(Expression
*, bool is_ignored
);
188 // Make a block statement from a Block. This is an embedded list of
189 // statements which may also include variable definitions.
191 make_block_statement(Block
*, Location
);
193 // Make an increment statement.
195 make_inc_statement(Expression
*);
197 // Make a decrement statement.
199 make_dec_statement(Expression
*);
201 // Make a go statement.
203 make_go_statement(Call_expression
* call
, Location
);
205 // Make a defer statement.
207 make_defer_statement(Call_expression
* call
, Location
);
209 // Make a return statement.
210 static Return_statement
*
211 make_return_statement(Expression_list
*, Location
);
213 // Make a statement that returns the result of a call expression.
214 // If the call does not return any results, this just returns the
215 // call expression as a statement, assuming that the function will
216 // end immediately afterward.
218 make_return_from_call(Call_expression
*, Location
);
220 // Make a break statement.
222 make_break_statement(Unnamed_label
* label
, Location
);
224 // Make a continue statement.
226 make_continue_statement(Unnamed_label
* label
, Location
);
228 // Make a goto statement.
230 make_goto_statement(Label
* label
, Location
);
232 // Make a goto statement to an unnamed label.
234 make_goto_unnamed_statement(Unnamed_label
* label
, Location
);
236 // Make a label statement--where the label is defined.
238 make_label_statement(Label
* label
, Location
);
240 // Make an unnamed label statement--where the label is defined.
242 make_unnamed_label_statement(Unnamed_label
* label
);
244 // Make an if statement.
246 make_if_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
249 // Make a switch statement.
250 static Switch_statement
*
251 make_switch_statement(Expression
* switch_val
, Location
);
253 // Make a type switch statement.
254 static Type_switch_statement
*
255 make_type_switch_statement(const std::string
&, Expression
*, Location
);
257 // Make a send statement.
258 static Send_statement
*
259 make_send_statement(Expression
* channel
, Expression
* val
, Location
);
261 // Make a select statement.
262 static Select_statement
*
263 make_select_statement(Location
);
265 // Make a for statement.
266 static For_statement
*
267 make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
270 // Make a for statement with a range clause.
271 static For_range_statement
*
272 make_for_range_statement(Expression
* index_var
, Expression
* value_var
,
273 Expression
* range
, Location
);
275 // Return the statement classification.
276 Statement_classification
277 classification() const
278 { return this->classification_
; }
280 // Get the statement location.
283 { return this->location_
; }
285 // Traverse the tree.
287 traverse(Block
*, size_t* index
, Traverse
*);
289 // Traverse the contents of this statement--the expressions and
290 // statements which it contains.
292 traverse_contents(Traverse
*);
294 // If this statement assigns some values, it calls a function for
295 // each value to which this statement assigns a value, and returns
296 // true. If this statement does not assign any values, it returns
299 traverse_assignments(Traverse_assignments
* tassign
);
301 // Lower a statement. This is called immediately after parsing to
302 // simplify statements for further processing. It returns the same
303 // Statement or a new one. FUNCTION is the function containing this
304 // statement. BLOCK is the block containing this statement.
305 // INSERTER can be used to insert new statements before this one.
307 lower(Gogo
* gogo
, Named_object
* function
, Block
* block
,
308 Statement_inserter
* inserter
)
309 { return this->do_lower(gogo
, function
, block
, inserter
); }
311 // Flatten a statement. This is called immediately after the order of
312 // evaluation rules are applied to statements. It returns the same
313 // Statement or a new one. FUNCTION is the function containing this
314 // statement. BLOCK is the block containing this statement.
315 // INSERTER can be used to insert new statements before this one.
317 flatten(Gogo
* gogo
, Named_object
* function
, Block
* block
,
318 Statement_inserter
* inserter
)
319 { return this->do_flatten(gogo
, function
, block
, inserter
); }
321 // Set type information for unnamed constants.
325 // Check types in a statement. This simply checks that any
326 // expressions used by the statement have the right type.
328 check_types(Gogo
* gogo
)
329 { this->do_check_types(gogo
); }
331 // Return the cost of this statement for inlining purposes.
334 { return this->do_inlining_cost(); }
336 // Export data for this statement to BODY.
338 export_statement(Export_function_body
* efb
)
339 { this->do_export_statement(efb
); }
341 // Read a statement from export data. The location should be used
342 // for the returned statement. Errors should be reported using the
343 // Import_function_body's location method.
345 import_statement(Import_function_body
*, Location
);
347 // Return whether this is a block statement.
349 is_block_statement() const
350 { return this->classification_
== STATEMENT_BLOCK
; }
352 // If this is an assignment statement, return it. Otherwise return
354 Assignment_statement
*
355 assignment_statement()
357 return this->convert
<Assignment_statement
, STATEMENT_ASSIGNMENT
>();
360 // If this is an temporary statement, return it. Otherwise return
363 temporary_statement()
365 return this->convert
<Temporary_statement
, STATEMENT_TEMPORARY
>();
368 // If this is a variable declaration statement, return it.
369 // Otherwise return NULL.
370 Variable_declaration_statement
*
371 variable_declaration_statement()
373 return this->convert
<Variable_declaration_statement
,
374 STATEMENT_VARIABLE_DECLARATION
>();
377 // If this is an expression statement, return it. Otherwise return
379 Expression_statement
*
380 expression_statement()
382 return this->convert
<Expression_statement
, STATEMENT_EXPRESSION
>();
385 // If this is an block statement, return it. Otherwise return
389 { return this->convert
<Block_statement
, STATEMENT_BLOCK
>(); }
391 // If this is a return statement, return it. Otherwise return NULL.
394 { return this->convert
<Return_statement
, STATEMENT_RETURN
>(); }
396 // If this is a thunk statement (a go or defer statement), return
397 // it. Otherwise return NULL.
401 // If this is a goto statement, return it. Otherwise return NULL.
404 { return this->convert
<Goto_statement
, STATEMENT_GOTO
>(); }
406 // If this is a goto_unnamed statement, return it. Otherwise return NULL.
407 Goto_unnamed_statement
*
408 goto_unnamed_statement()
409 { return this->convert
<Goto_unnamed_statement
, STATEMENT_GOTO_UNNAMED
>(); }
411 // If this is a label statement, return it. Otherwise return NULL.
414 { return this->convert
<Label_statement
, STATEMENT_LABEL
>(); }
416 // If this is an unnamed_label statement, return it. Otherwise return NULL.
417 Unnamed_label_statement
*
418 unnamed_label_statement()
419 { return this->convert
<Unnamed_label_statement
, STATEMENT_UNNAMED_LABEL
>(); }
421 // If this is an if statement, return it. Otherwise return NULL.
424 { return this->convert
<If_statement
, STATEMENT_IF
>(); }
426 // If this is a for statement, return it. Otherwise return NULL.
429 { return this->convert
<For_statement
, STATEMENT_FOR
>(); }
431 // If this is a for statement over a range clause, return it.
432 // Otherwise return NULL.
434 for_range_statement()
435 { return this->convert
<For_range_statement
, STATEMENT_FOR_RANGE
>(); }
437 // If this is a switch statement, return it. Otherwise return NULL.
440 { return this->convert
<Switch_statement
, STATEMENT_SWITCH
>(); }
442 // If this is a type switch statement, return it. Otherwise return
444 Type_switch_statement
*
445 type_switch_statement()
446 { return this->convert
<Type_switch_statement
, STATEMENT_TYPE_SWITCH
>(); }
448 // If this is a send statement, return it. Otherwise return NULL.
451 { return this->convert
<Send_statement
, STATEMENT_SEND
>(); }
453 // If this is a select statement, return it. Otherwise return NULL.
456 { return this->convert
<Select_statement
, STATEMENT_SELECT
>(); }
458 // Return true if this statement may fall through--if after
459 // executing this statement we may go on to execute the following
460 // statement, if any.
462 may_fall_through() const
463 { return this->do_may_fall_through(); }
465 // Convert the statement to the backend representation.
467 get_backend(Translate_context
*);
469 // Dump AST representation of a statement to a dump context.
471 dump_statement(Ast_dump_context
*) const;
474 // Implemented by child class: traverse the tree.
476 do_traverse(Traverse
*) = 0;
478 // Implemented by child class: traverse assignments. Any statement
479 // which includes an assignment should implement this.
481 do_traverse_assignments(Traverse_assignments
*)
484 // Implemented by the child class: lower this statement to a simpler
487 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
490 // Implemented by the child class: lower this statement to a simpler
493 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
496 // Implemented by child class: set type information for unnamed
497 // constants. Any statement which includes an expression needs to
503 // Implemented by child class: check types of expressions used in a
506 do_check_types(Gogo
*)
509 // Implemented by child class: return the cost of this statement for
510 // inlining. The default cost is high, so we only need to define
511 // this method for statements that can be inlined.
516 // Implemented by child class: write export data for this statement
517 // to the string. This need only be implemented by classes that
518 // implement do_inlining_cost with a reasonable value.
520 do_export_statement(Export_function_body
*)
521 { go_unreachable(); }
523 // Implemented by child class: return true if this statement may
526 do_may_fall_through() const
529 // Implemented by child class: convert to backend representation.
531 do_get_backend(Translate_context
*) = 0;
533 // Implemented by child class: dump ast representation.
535 do_dump_statement(Ast_dump_context
*) const = 0;
537 // Traverse an expression in a statement.
539 traverse_expression(Traverse
*, Expression
**);
541 // Traverse an expression list in a statement. The Expression_list
544 traverse_expression_list(Traverse
*, Expression_list
*);
546 // Traverse a type in a statement.
548 traverse_type(Traverse
*, Type
*);
550 // For children to call when they detect that they are in error.
554 // For children to call to report an error conveniently.
556 report_error(const char*);
558 // For children to return an error statement from lower().
560 make_error_statement(Location
);
563 // Convert to the desired statement classification, or return NULL.
564 // This is a controlled dynamic cast.
565 template<typename Statement_class
, Statement_classification sc
>
569 return (this->classification_
== sc
570 ? static_cast<Statement_class
*>(this)
574 template<typename Statement_class
, Statement_classification sc
>
575 const Statement_class
*
578 return (this->classification_
== sc
579 ? static_cast<const Statement_class
*>(this)
583 // The statement classification.
584 Statement_classification classification_
;
585 // The location in the input file of the start of this statement.
589 // An assignment statement.
591 class Assignment_statement
: public Statement
594 Assignment_statement(Expression
* lhs
, Expression
* rhs
,
596 : Statement(STATEMENT_ASSIGNMENT
, location
),
597 lhs_(lhs
), rhs_(rhs
), omit_write_barrier_(false)
602 { return this->lhs_
; }
606 { return this->rhs_
; }
609 omit_write_barrier() const
610 { return this->omit_write_barrier_
; }
613 set_omit_write_barrier()
614 { this->omit_write_barrier_
= true; }
618 do_traverse(Traverse
* traverse
);
621 do_traverse_assignments(Traverse_assignments
*);
624 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
627 do_determine_types();
630 do_check_types(Gogo
*);
637 do_export_statement(Export_function_body
*);
640 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
643 do_get_backend(Translate_context
*);
646 do_dump_statement(Ast_dump_context
*) const;
649 // Left hand side--the lvalue.
651 // Right hand side--the rvalue.
653 // True if we can omit a write barrier from this assignment.
654 bool omit_write_barrier_
;
657 // A statement which creates and initializes a temporary variable.
659 class Temporary_statement
: public Statement
662 Temporary_statement(Type
* type
, Expression
* init
, Location location
)
663 : Statement(STATEMENT_TEMPORARY
, location
),
664 type_(type
), init_(init
), bvariable_(NULL
), is_address_taken_(false),
665 value_escapes_(false)
668 // Return the type of the temporary variable.
672 // Return the initializer if there is one.
675 { return this->init_
; }
677 // Record that something takes the address of this temporary
680 set_is_address_taken()
681 { this->is_address_taken_
= true; }
683 // Whether the value escapes.
685 value_escapes() const
686 { return this->value_escapes_
; }
688 // Record that the value escapes.
691 { this->value_escapes_
= true; }
693 // Return the temporary variable. This should not be called until
694 // after the statement itself has been converted.
696 get_backend_variable(Translate_context
*) const;
700 do_traverse(Traverse
*);
703 do_traverse_assignments(Traverse_assignments
*);
706 do_determine_types();
709 do_check_types(Gogo
*);
712 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
715 do_get_backend(Translate_context
*);
718 do_dump_statement(Ast_dump_context
*) const;
721 // The type of the temporary variable.
723 // The initial value of the temporary variable. This may be NULL.
725 // The backend representation of the temporary variable.
726 Bvariable
* bvariable_
;
727 // True if something takes the address of this temporary variable.
728 bool is_address_taken_
;
729 // True if the value assigned to this temporary variable escapes.
730 // This is used for select statements.
734 // A variable declaration. This marks the point in the code where a
735 // variable is declared. The Variable is also attached to a Block.
737 class Variable_declaration_statement
: public Statement
740 Variable_declaration_statement(Named_object
* var
);
742 // The variable being declared.
745 { return this->var_
; }
747 // Import a variable declaration.
749 do_import(Import_function_body
*, Location
);
753 do_traverse(Traverse
*);
756 do_traverse_assignments(Traverse_assignments
*);
759 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
766 do_export_statement(Export_function_body
*);
769 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
772 do_get_backend(Translate_context
*);
775 do_dump_statement(Ast_dump_context
*) const;
781 // A return statement.
783 class Return_statement
: public Statement
786 Return_statement(Expression_list
* vals
, Location location
)
787 : Statement(STATEMENT_RETURN
, location
),
788 vals_(vals
), is_lowered_(false)
791 // The list of values being returned. This may be NULL.
792 const Expression_list
*
794 { return this->vals_
; }
798 do_traverse(Traverse
* traverse
)
799 { return this->traverse_expression_list(traverse
, this->vals_
); }
802 do_traverse_assignments(Traverse_assignments
*);
805 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
808 do_may_fall_through() const
816 do_export_statement(Export_function_body
*);
819 do_get_backend(Translate_context
*);
822 do_dump_statement(Ast_dump_context
*) const;
825 // Return values. This may be NULL.
826 Expression_list
* vals_
;
827 // True if this statement has been lowered.
831 // An expression statement.
833 class Expression_statement
: public Statement
836 Expression_statement(Expression
* expr
, bool is_ignored
);
840 { return this->expr_
; }
844 do_traverse(Traverse
* traverse
)
845 { return this->traverse_expression(traverse
, &this->expr_
); }
848 do_determine_types();
851 do_check_types(Gogo
*);
854 do_may_fall_through() const;
857 do_get_backend(Translate_context
* context
);
860 do_dump_statement(Ast_dump_context
*) const;
864 // Whether the value of this expression is being explicitly ignored.
868 // A block statement--a list of statements which may include variable
871 class Block_statement
: public Statement
874 Block_statement(Block
* block
, Location location
)
875 : Statement(STATEMENT_BLOCK
, location
),
876 block_(block
), is_lowered_for_statement_(false)
879 // Return the actual block.
882 { return this->block_
; }
885 set_is_lowered_for_statement()
886 { this->is_lowered_for_statement_
= true; }
889 is_lowered_for_statement()
890 { return this->is_lowered_for_statement_
; }
894 do_traverse(Traverse
* traverse
)
895 { return this->block_
->traverse(traverse
); }
899 { this->block_
->determine_types(); }
906 do_export_statement(Export_function_body
*);
909 do_may_fall_through() const
910 { return this->block_
->may_fall_through(); }
913 do_get_backend(Translate_context
* context
);
916 do_dump_statement(Ast_dump_context
*) const;
920 // True if this block statement represents a lowered for statement.
921 bool is_lowered_for_statement_
;
926 class Send_statement
: public Statement
929 Send_statement(Expression
* channel
, Expression
* val
,
931 : Statement(STATEMENT_SEND
, location
),
932 channel_(channel
), val_(val
)
937 { return this->channel_
; }
941 { return this->val_
; }
945 do_traverse(Traverse
* traverse
);
948 do_determine_types();
951 do_check_types(Gogo
*);
954 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
957 do_get_backend(Translate_context
*);
960 do_dump_statement(Ast_dump_context
*) const;
963 // The channel on which to send the value.
964 Expression
* channel_
;
965 // The value to send.
969 // Select_clauses holds the clauses of a select statement. This is
970 // built by the parser.
979 // Add a new clause. IS_SEND is true if this is a send clause,
980 // false for a receive clause. For a send clause CHANNEL is the
981 // channel and VAL is the value to send. For a receive clause
982 // CHANNEL is the channel, VAL is either NULL or a Var_expression
983 // for the variable to set, and CLOSED is either NULL or a
984 // Var_expression to set to whether the channel is closed. If VAL
985 // is NULL, VAR may be a variable to be initialized with the
986 // received value, and CLOSEDVAR ma be a variable to be initialized
987 // with whether the channel is closed. IS_DEFAULT is true if this
988 // is the default clause. STATEMENTS is the list of statements to
991 add(bool is_send
, Expression
* channel
, Expression
* val
, Expression
* closed
,
992 Named_object
* var
, Named_object
* closedvar
, bool is_default
,
993 Block
* statements
, Location location
)
995 this->clauses_
.push_back(Select_clause(is_send
, channel
, val
, closed
, var
,
996 closedvar
, is_default
, statements
,
1002 { return this->clauses_
.size(); }
1004 // Traverse the select clauses.
1006 traverse(Traverse
*);
1008 // Lower statements.
1010 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*,
1011 Temporary_statement
*);
1021 // Whether the select clauses may fall through to the statement
1022 // which follows the overall select statement.
1024 may_fall_through() const;
1026 // Convert to the backend representation.
1028 get_backend(Translate_context
*, Temporary_statement
* index
,
1029 Unnamed_label
* break_label
, Location
);
1031 // Dump AST representation.
1033 dump_clauses(Ast_dump_context
*) const;
1041 : channel_(NULL
), val_(NULL
), closed_(NULL
), var_(NULL
),
1042 closedvar_(NULL
), statements_(NULL
), is_send_(false),
1046 Select_clause(bool is_send
, Expression
* channel
, Expression
* val
,
1047 Expression
* closed
, Named_object
* var
,
1048 Named_object
* closedvar
, bool is_default
, Block
* statements
,
1050 : channel_(channel
), val_(val
), closed_(closed
), var_(var
),
1051 closedvar_(closedvar
), statements_(statements
), location_(location
),
1052 is_send_(is_send
), is_default_(is_default
), is_lowered_(false)
1053 { go_assert(is_default
? channel
== NULL
: channel
!= NULL
); }
1055 // Traverse the select clause.
1057 traverse(Traverse
*);
1059 // Lower statements.
1061 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*, size_t,
1062 Temporary_statement
*);
1072 // Return true if this is the default clause.
1075 { return this->is_default_
; }
1077 // Return the channel. This will return NULL for the default
1081 { return this->channel_
; }
1083 // Return true for a send, false for a receive.
1087 go_assert(!this->is_default_
);
1088 return this->is_send_
;
1091 // Return the statements.
1094 { return this->statements_
; }
1096 // Return the location.
1099 { return this->location_
; }
1101 // Whether this clause may fall through to the statement which
1102 // follows the overall select statement.
1104 may_fall_through() const;
1106 // Convert the statements to the backend representation.
1108 get_statements_backend(Translate_context
*);
1110 // Dump AST representation.
1112 dump_clause(Ast_dump_context
*) const;
1115 // These values must match the values in libgo/go/runtime/select.go.
1124 lower_default(Block
*, Expression
*);
1127 lower_send(Block
*, Expression
*, Expression
*);
1130 lower_recv(Gogo
*, Named_object
*, Block
*, Expression
*, Expression
*,
1131 Temporary_statement
*);
1134 set_case(Block
*, Expression
*, Expression
*, Expression
*, int);
1137 Expression
* channel_
;
1138 // The value to send or the lvalue to receive into.
1140 // The lvalue to set to whether the channel is closed on a
1142 Expression
* closed_
;
1143 // The variable to initialize, for "case a := <-ch".
1145 // The variable to initialize to whether the channel is closed,
1146 // for "case a, c := <-ch".
1147 Named_object
* closedvar_
;
1148 // The statements to execute.
1150 // The location of this clause.
1152 // Whether this is a send or a receive.
1154 // Whether this is the default.
1156 // Whether this has been lowered.
1160 typedef std::vector
<Select_clause
> Clauses
;
1165 // A select statement.
1167 class Select_statement
: public Statement
1170 Select_statement(Location location
)
1171 : Statement(STATEMENT_SELECT
, location
),
1172 clauses_(NULL
), index_(NULL
), break_label_(NULL
), is_lowered_(false)
1177 add_clauses(Select_clauses
* clauses
)
1179 go_assert(this->clauses_
== NULL
);
1180 this->clauses_
= clauses
;
1183 // Return the break label for this select statement.
1189 do_traverse(Traverse
* traverse
)
1190 { return this->clauses_
->traverse(traverse
); }
1193 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1196 do_determine_types()
1197 { this->clauses_
->determine_types(); }
1200 do_check_types(Gogo
*)
1201 { this->clauses_
->check_types(); }
1204 do_may_fall_through() const;
1207 do_get_backend(Translate_context
*);
1210 do_dump_statement(Ast_dump_context
*) const;
1213 // The select clauses.
1214 Select_clauses
* clauses_
;
1215 // A temporary that holds the index value returned by selectgo.
1216 Temporary_statement
* index_
;
1218 Unnamed_label
* break_label_
;
1219 // Whether this statement has been lowered.
1223 // A statement which requires a thunk: go or defer.
1225 class Thunk_statement
: public Statement
1228 Thunk_statement(Statement_classification
, Call_expression
*,
1231 // Return the call expression.
1234 { return this->call_
; }
1236 // Simplify a go or defer statement so that it only uses a single
1239 simplify_statement(Gogo
*, Named_object
*, Block
*);
1243 do_traverse(Traverse
* traverse
);
1246 do_traverse_assignments(Traverse_assignments
*);
1249 do_determine_types();
1252 do_check_types(Gogo
*);
1254 // Return the function and argument for the call.
1256 get_fn_and_arg(Expression
** pfn
, Expression
** parg
);
1259 // Return whether this is a simple go statement.
1261 is_simple(Function_type
*) const;
1263 // Return whether the thunk function is a constant.
1265 is_constant_function() const;
1267 // Build the struct to use for a complex case.
1269 build_struct(Function_type
* fntype
);
1273 build_thunk(Gogo
*, const std::string
&);
1275 // Set the name to use for thunk field N.
1277 thunk_field_param(int n
, char* buf
, size_t buflen
);
1279 // The function call to be executed in a separate thread (go) or
1282 // The type used for a struct to pass to a thunk, if this is not a
1284 Struct_type
* struct_type_
;
1289 class Go_statement
: public Thunk_statement
1292 Go_statement(Call_expression
* call
, Location location
)
1293 : Thunk_statement(STATEMENT_GO
, call
, location
)
1298 do_get_backend(Translate_context
*);
1301 do_dump_statement(Ast_dump_context
*) const;
1304 // A defer statement.
1306 class Defer_statement
: public Thunk_statement
1309 Defer_statement(Call_expression
* call
, Location location
)
1310 : Thunk_statement(STATEMENT_DEFER
, call
, location
)
1315 do_get_backend(Translate_context
*);
1318 do_dump_statement(Ast_dump_context
*) const;
1321 // A goto statement.
1323 class Goto_statement
: public Statement
1326 Goto_statement(Label
* label
, Location location
)
1327 : Statement(STATEMENT_GOTO
, location
),
1331 // Return the label being jumped to.
1334 { return this->label_
; }
1338 do_traverse(Traverse
*);
1341 do_check_types(Gogo
*);
1344 do_may_fall_through() const
1348 do_get_backend(Translate_context
*);
1351 do_dump_statement(Ast_dump_context
*) const;
1357 // A goto statement to an unnamed label.
1359 class Goto_unnamed_statement
: public Statement
1362 Goto_unnamed_statement(Unnamed_label
* label
, Location location
)
1363 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
1368 unnamed_label() const
1369 { return this->label_
; }
1373 do_traverse(Traverse
*);
1376 do_may_fall_through() const
1380 do_get_backend(Translate_context
* context
);
1383 do_dump_statement(Ast_dump_context
*) const;
1386 Unnamed_label
* label_
;
1389 // A label statement.
1391 class Label_statement
: public Statement
1394 Label_statement(Label
* label
, Location location
)
1395 : Statement(STATEMENT_LABEL
, location
),
1399 // Return the label itself.
1402 { return this->label_
; }
1406 do_traverse(Traverse
*);
1409 do_get_backend(Translate_context
*);
1412 do_dump_statement(Ast_dump_context
*) const;
1419 // An unnamed label statement.
1421 class Unnamed_label_statement
: public Statement
1424 Unnamed_label_statement(Unnamed_label
* label
);
1428 do_traverse(Traverse
*);
1431 do_get_backend(Translate_context
* context
);
1434 do_dump_statement(Ast_dump_context
*) const;
1438 Unnamed_label
* label_
;
1443 class If_statement
: public Statement
1446 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
1448 : Statement(STATEMENT_IF
, location
),
1449 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
1454 { return this->cond_
; }
1458 do_traverse(Traverse
*);
1461 do_determine_types();
1464 do_check_types(Gogo
*);
1467 do_may_fall_through() const;
1470 do_get_backend(Translate_context
*);
1473 do_dump_statement(Ast_dump_context
*) const;
1483 class For_statement
: public Statement
1486 For_statement(Block
* init
, Expression
* cond
, Block
* post
,
1488 : Statement(STATEMENT_FOR
, location
),
1489 init_(init
), cond_(cond
), post_(post
), statements_(NULL
),
1490 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.
1509 // Set the break and continue labels for this statement.
1511 set_break_continue_labels(Unnamed_label
* break_label
,
1512 Unnamed_label
* continue_label
);
1516 do_traverse(Traverse
*);
1519 do_traverse_assignments(Traverse_assignments
*)
1520 { go_unreachable(); }
1523 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1526 do_may_fall_through() const;
1529 do_get_backend(Translate_context
*)
1530 { go_unreachable(); }
1533 do_dump_statement(Ast_dump_context
*) const;
1536 // The initialization statements. This may be NULL.
1538 // The condition. This may be NULL.
1540 // The statements to run after each iteration. This may be NULL.
1542 // The statements in the loop itself.
1544 // The break label, if needed.
1545 Unnamed_label
* break_label_
;
1546 // The continue label, if needed.
1547 Unnamed_label
* continue_label_
;
1550 // A for statement over a range clause.
1552 class For_range_statement
: public Statement
1555 For_range_statement(Expression
* index_var
, Expression
* value_var
,
1556 Expression
* range
, Location location
)
1557 : Statement(STATEMENT_FOR_RANGE
, location
),
1558 index_var_(index_var
), value_var_(value_var
), range_(range
),
1559 statements_(NULL
), break_label_(NULL
), continue_label_(NULL
)
1562 // Add the statements.
1564 add_statements(Block
* statements
)
1566 go_assert(this->statements_
== NULL
);
1567 this->statements_
= statements
;
1570 // Return the break label for this for statement.
1574 // Return the continue label for this for statement.
1580 do_traverse(Traverse
*);
1583 do_traverse_assignments(Traverse_assignments
*)
1584 { go_unreachable(); }
1587 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1590 do_get_backend(Translate_context
*)
1591 { go_unreachable(); }
1594 do_dump_statement(Ast_dump_context
*) const;
1598 make_range_ref(Named_object
*, Temporary_statement
*, Location
);
1601 call_builtin(Gogo
*, const char* funcname
, Expression
* arg
, Location
);
1604 lower_range_array(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1605 Temporary_statement
*, Temporary_statement
*,
1606 Block
**, Expression
**, Block
**, Block
**);
1609 lower_range_slice(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1610 Temporary_statement
*, Temporary_statement
*,
1611 Block
**, Expression
**, Block
**, Block
**);
1614 lower_range_string(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1615 Temporary_statement
*, Temporary_statement
*,
1616 Block
**, Expression
**, Block
**, Block
**);
1619 lower_range_map(Gogo
*, Map_type
*, Block
*, Block
*, Named_object
*,
1620 Temporary_statement
*, Temporary_statement
*,
1621 Temporary_statement
*, Block
**, Expression
**, Block
**,
1625 lower_range_channel(Gogo
*, Block
*, Block
*, Named_object
*,
1626 Temporary_statement
*, Temporary_statement
*,
1627 Temporary_statement
*, Block
**, Expression
**, Block
**,
1631 lower_map_range_clear(Type
*, Block
*, Expression
*, Named_object
*,
1632 Temporary_statement
*, Location
);
1635 lower_array_range_clear(Gogo
*, Type
*, Expression
*, Block
*,
1636 Named_object
*, Temporary_statement
*,
1639 // The variable which is set to the index value.
1640 Expression
* index_var_
;
1641 // The variable which is set to the element value. This may be
1643 Expression
* value_var_
;
1644 // The expression we are ranging over.
1646 // The statements in the block.
1648 // The break label, if needed.
1649 Unnamed_label
* break_label_
;
1650 // The continue label, if needed.
1651 Unnamed_label
* continue_label_
;
1654 // Class Case_clauses holds the clauses of a switch statement. This
1655 // is built by the parser.
1664 // Add a new clause. CASES is a list of case expressions; it may be
1665 // NULL. IS_DEFAULT is true if this is the default case.
1666 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1667 // after the statements the case clause should fall through to the
1670 add(Expression_list
* cases
, bool is_default
, Block
* statements
,
1671 bool is_fallthrough
, Location location
)
1673 this->clauses_
.push_back(Case_clause(cases
, is_default
, statements
,
1674 is_fallthrough
, location
));
1677 // Return whether there are no clauses.
1680 { return this->clauses_
.empty(); }
1682 // Traverse the case clauses.
1684 traverse(Traverse
*);
1686 // Lower for a nonconstant switch.
1688 lower(Block
*, Temporary_statement
*, Unnamed_label
*) const;
1690 // Determine types of expressions. The Type parameter is the type
1691 // of the switch value.
1693 determine_types(Type
*);
1695 // Check types. The Type parameter is the type of the switch value.
1699 // Return true if all the clauses are constant values.
1701 is_constant() const;
1703 // Return true if these clauses may fall through to the statements
1704 // following the switch statement.
1706 may_fall_through() const;
1708 // Return the body of a SWITCH_EXPR when all the clauses are
1711 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1712 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
1713 std::vector
<Bstatement
*>* all_statements
) const;
1715 // Dump the AST representation to a dump context.
1717 dump_clauses(Ast_dump_context
*) const;
1720 // For a constant switch we need to keep a record of constants we
1721 // have already seen.
1722 class Hash_integer_value
;
1723 class Eq_integer_value
;
1724 typedef Unordered_set_hash(Expression
*, Hash_integer_value
,
1725 Eq_integer_value
) Case_constants
;
1732 : cases_(NULL
), statements_(NULL
), is_default_(false),
1733 is_fallthrough_(false), location_(Linemap::unknown_location())
1736 Case_clause(Expression_list
* cases
, bool is_default
, Block
* statements
,
1737 bool is_fallthrough
, Location location
)
1738 : cases_(cases
), statements_(statements
), is_default_(is_default
),
1739 is_fallthrough_(is_fallthrough
), location_(location
)
1742 // Whether this clause falls through to the next clause.
1744 is_fallthrough() const
1745 { return this->is_fallthrough_
; }
1747 // Whether this is the default.
1750 { return this->is_default_
; }
1752 // The location of this clause.
1755 { return this->location_
; }
1759 traverse(Traverse
*);
1761 // Lower for a nonconstant switch.
1763 lower(Block
*, Temporary_statement
*, Unnamed_label
*, Unnamed_label
*) const;
1767 determine_types(Type
*);
1773 // Return true if all the case expressions are constant.
1775 is_constant() const;
1777 // Return true if this clause may fall through to execute the
1778 // statements following the switch statement. This is not the
1779 // same as whether this clause falls through to the next clause.
1781 may_fall_through() const;
1783 // Convert the case values and statements to the backend
1786 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1787 Case_constants
*, std::vector
<Bexpression
*>* cases
) const;
1789 // Dump the AST representation to a dump context.
1791 dump_clause(Ast_dump_context
*) const;
1794 // The list of case expressions.
1795 Expression_list
* cases_
;
1796 // The statements to execute.
1798 // Whether this is the default case.
1800 // Whether this falls through after the statements.
1801 bool is_fallthrough_
;
1802 // The location of this case clause.
1806 friend class Case_clause
;
1808 // The type of the list of clauses.
1809 typedef std::vector
<Case_clause
> Clauses
;
1811 // All the case clauses.
1815 // A switch statement.
1817 class Switch_statement
: public Statement
1820 Switch_statement(Expression
* val
, Location location
)
1821 : Statement(STATEMENT_SWITCH
, location
),
1822 val_(val
), clauses_(NULL
), break_label_(NULL
)
1827 add_clauses(Case_clauses
* clauses
)
1829 go_assert(this->clauses_
== NULL
);
1830 this->clauses_
= clauses
;
1833 // Return the break label for this switch statement.
1839 do_traverse(Traverse
*);
1842 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1845 do_get_backend(Translate_context
*)
1846 { go_unreachable(); }
1849 do_dump_statement(Ast_dump_context
*) const;
1852 do_may_fall_through() const;
1855 // The value to switch on. This may be NULL.
1857 // The case clauses.
1858 Case_clauses
* clauses_
;
1859 // The break label, if needed.
1860 Unnamed_label
* break_label_
;
1863 // Class Type_case_clauses holds the clauses of a type switch
1864 // statement. This is built by the parser.
1866 class Type_case_clauses
1873 // Add a new clause. TYPE is the type for this clause; it may be
1874 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1875 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1876 // if this is the default case. STATEMENTS is a block of
1877 // statements; it may be NULL.
1879 add(Type
* type
, bool is_fallthrough
, bool is_default
, Block
* statements
,
1882 this->clauses_
.push_back(Type_case_clause(type
, is_fallthrough
, is_default
,
1883 statements
, location
));
1886 // Return whether there are no clauses.
1889 { return this->clauses_
.empty(); }
1891 // Traverse the type case clauses.
1893 traverse(Traverse
*);
1895 // Check for duplicates.
1897 check_duplicates() const;
1899 // Lower to if and goto statements.
1901 lower(Type
*, Block
*, Temporary_statement
* descriptor_temp
,
1902 Unnamed_label
* break_label
) const;
1904 // Return true if these clauses may fall through to the statements
1905 // following the switch statement.
1907 may_fall_through() const;
1909 // Dump the AST representation to a dump context.
1911 dump_clauses(Ast_dump_context
*) const;
1914 // One type case clause.
1915 class Type_case_clause
1919 : type_(NULL
), statements_(NULL
), is_default_(false),
1920 location_(Linemap::unknown_location())
1923 Type_case_clause(Type
* type
, bool is_fallthrough
, bool is_default
,
1924 Block
* statements
, Location location
)
1925 : type_(type
), statements_(statements
), is_fallthrough_(is_fallthrough
),
1926 is_default_(is_default
), location_(location
)
1932 { return this->type_
; }
1934 // Whether this is the default.
1937 { return this->is_default_
; }
1939 // The location of this type clause.
1942 { return this->location_
; }
1946 traverse(Traverse
*);
1948 // Lower to if and goto statements.
1950 lower(Type
*, Block
*, Temporary_statement
* descriptor_temp
,
1951 Unnamed_label
* break_label
, Unnamed_label
** stmts_label
) const;
1953 // Return true if this clause may fall through to execute the
1954 // statements following the switch statement. This is not the
1955 // same as whether this clause falls through to the next clause.
1957 may_fall_through() const;
1959 // Dump the AST representation to a dump context.
1961 dump_clause(Ast_dump_context
*) const;
1964 // The type for this type clause.
1966 // The statements to execute.
1968 // Whether this falls through--this is true for "case T1, T2".
1969 bool is_fallthrough_
;
1970 // Whether this is the default case.
1972 // The location of this type case clause.
1976 friend class Type_case_clause
;
1978 // The type of the list of type clauses.
1979 typedef std::vector
<Type_case_clause
> Type_clauses
;
1981 // All the type case clauses.
1982 Type_clauses clauses_
;
1985 // A type switch statement.
1987 class Type_switch_statement
: public Statement
1990 Type_switch_statement(const std::string
& name
, Expression
* expr
,
1992 : Statement(STATEMENT_TYPE_SWITCH
, location
),
1993 name_(name
), expr_(expr
), clauses_(NULL
), break_label_(NULL
)
1998 add_clauses(Type_case_clauses
* clauses
)
2000 go_assert(this->clauses_
== NULL
);
2001 this->clauses_
= clauses
;
2004 // Return the break label for this type switch statement.
2010 do_traverse(Traverse
*);
2013 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
2016 do_get_backend(Translate_context
*)
2017 { go_unreachable(); }
2020 do_dump_statement(Ast_dump_context
*) const;
2023 do_may_fall_through() const;
2026 // The name of the variable declared in the type switch guard. Empty if there
2027 // is no variable declared.
2029 // The expression we are switching on if there is no variable.
2031 // The type case clauses.
2032 Type_case_clauses
* clauses_
;
2033 // The break label, if needed.
2034 Unnamed_label
* break_label_
;
2037 #endif // !defined(GO_STATEMENTS_H)