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
;
27 class Defer_statement
;
29 class Goto_unnamed_statement
;
30 class Label_statement
;
31 class Unnamed_label_statement
;
34 class For_range_statement
;
35 class Switch_statement
;
36 class Type_switch_statement
;
38 class Select_statement
;
42 class Translate_context
;
44 class Expression_list
;
46 class Call_expression
;
47 class Map_index_expression
;
48 class Receive_expression
;
50 class Type_case_clauses
;
52 class Typed_identifier_list
;
56 class Ast_dump_context
;
58 // A single statement.
63 // The types of statements.
64 enum Statement_classification
67 STATEMENT_VARIABLE_DECLARATION
,
75 STATEMENT_BREAK_OR_CONTINUE
,
77 STATEMENT_GOTO_UNNAMED
,
79 STATEMENT_UNNAMED_LABEL
,
81 STATEMENT_CONSTANT_SWITCH
,
85 // These statements types are created by the parser, but they
86 // disappear during the lowering pass.
87 STATEMENT_ASSIGNMENT_OPERATION
,
88 STATEMENT_TUPLE_ASSIGNMENT
,
89 STATEMENT_TUPLE_MAP_ASSIGNMENT
,
90 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
,
91 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
,
99 Statement(Statement_classification
, Location
);
101 virtual ~Statement();
103 // Make a variable declaration.
105 make_variable_declaration(Named_object
*);
107 // Make a statement which creates a temporary variable and
108 // initializes it to an expression. The block is used if the
109 // temporary variable has to be explicitly destroyed; the variable
110 // must still be added to the block. References to the temporary
111 // variable may be constructed using make_temporary_reference.
112 // Either the type or the initialization expression may be NULL, but
114 static Temporary_statement
*
115 make_temporary(Type
*, Expression
*, Location
);
117 // Make an assignment statement.
118 static Assignment_statement
*
119 make_assignment(Expression
*, Expression
*, Location
);
121 // Make an assignment operation (+=, etc.).
123 make_assignment_operation(Operator
, Expression
*, Expression
*,
126 // Make a tuple assignment statement.
128 make_tuple_assignment(Expression_list
*, Expression_list
*, Location
);
130 // Make an assignment from a map index to a pair of variables.
132 make_tuple_map_assignment(Expression
* val
, Expression
* present
,
133 Expression
*, Location
);
135 // Make an assignment from a nonblocking receive to a pair of
138 make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
139 Expression
* channel
, Location
);
141 // Make an assignment from a type guard to a pair of variables.
143 make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
144 Expression
* expr
, Type
* type
,
147 // Make an expression statement from an Expression. IS_IGNORED is
148 // true if the value is being explicitly ignored, as in an
151 make_statement(Expression
*, bool is_ignored
);
153 // Make a block statement from a Block. This is an embedded list of
154 // statements which may also include variable definitions.
155 static Block_statement
*
156 make_block_statement(Block
*, Location
);
158 // Make an increment statement.
160 make_inc_statement(Expression
*);
162 // Make a decrement statement.
164 make_dec_statement(Expression
*);
166 // Make a go statement.
168 make_go_statement(Call_expression
* call
, Location
);
170 // Make a defer statement.
172 make_defer_statement(Call_expression
* call
, Location
);
174 // Make a return statement. FUNCTION is a backpointer to the
175 // function that this statement is returning from.
176 static Return_statement
*
177 make_return_statement(Named_object
* function
, Expression_list
*, Location
);
179 // Make a statement that returns the result of a call expression.
180 // If the call does not return any results, this just returns the
181 // call expression as a statement, assuming that the function will
182 // end immediately afterward.
184 make_return_from_call(Named_object
* function
, Call_expression
*, Location
);
186 // Make a break statement.
188 make_break_statement(Unnamed_label
* label
, Location
);
190 // Make a continue statement.
192 make_continue_statement(Unnamed_label
* label
, Location
);
194 // Make a goto statement.
196 make_goto_statement(Label
* label
, Location
);
198 // Make a goto statement to an unnamed label.
200 make_goto_unnamed_statement(Unnamed_label
* label
, Location
);
202 // Make a label statement--where the label is defined.
204 make_label_statement(Label
* label
, Location
);
206 // Make an unnamed label statement--where the label is defined.
208 make_unnamed_label_statement(Unnamed_label
* label
);
210 // Make an if statement.
212 make_if_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
215 // Make a switch statement.
216 static Switch_statement
*
217 make_switch_statement(Expression
* switch_val
, Location
);
219 // Make a type switch statement.
220 static Type_switch_statement
*
221 make_type_switch_statement(Expression
*, Location
);
223 // Make a send statement.
224 static Send_statement
*
225 make_send_statement(Expression
* channel
, Expression
* val
, Location
);
227 // Make a select statement.
228 static Select_statement
*
229 make_select_statement(Location
);
231 // Make a for statement.
232 static For_statement
*
233 make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
236 // Make a for statement with a range clause.
237 static For_range_statement
*
238 make_for_range_statement(Expression
* index_var
, Expression
* value_var
,
239 Expression
* range
, Location
);
241 // Return the statement classification.
242 Statement_classification
243 classification() const
244 { return this->classification_
; }
246 // Get the statement location.
249 { return this->location_
; }
251 // Traverse the tree.
253 traverse(Block
*, size_t* index
, Traverse
*);
255 // Traverse the contents of this statement--the expressions and
256 // statements which it contains.
258 traverse_contents(Traverse
*);
260 // Lower a statement. This is called immediately after parsing to
261 // simplify statements for further processing. It returns the same
262 // Statement or a new one. FUNCTION is the function containing this
263 // statement. BLOCK is the block containing this statement.
264 // INSERTER can be used to insert new statements before this one.
266 lower(Gogo
* gogo
, Named_object
* function
, Block
* block
,
267 Statement_inserter
* inserter
)
268 { return this->do_lower(gogo
, function
, block
, inserter
); }
270 // Flatten a statement. This is called immediately after the order of
271 // evaluation rules are applied to statements. It returns the same
272 // Statement or a new one. FUNCTION is the function containing this
273 // statement. BLOCK is the block containing this statement.
274 // INSERTER can be used to insert new statements before this one.
276 flatten(Gogo
* gogo
, Named_object
* function
, Block
* block
,
277 Statement_inserter
* inserter
)
278 { return this->do_flatten(gogo
, function
, block
, inserter
); }
280 // Set type information for unnamed constants.
282 determine_types(Gogo
*);
284 // Check types in a statement. This simply checks that any
285 // expressions used by the statement have the right type.
287 check_types(Gogo
* gogo
)
288 { this->do_check_types(gogo
); }
290 // Return the cost of this statement for inlining purposes.
293 { return this->do_inlining_cost(); }
295 // Export data for this statement to BODY.
297 export_statement(Export_function_body
* efb
)
298 { this->do_export_statement(efb
); }
300 // Make implicit type conversions explicit.
303 { this->do_add_conversions(); }
305 // Read a statement from export data. The location should be used
306 // for the returned statement. Errors should be reported using the
307 // Import_function_body's location method.
309 import_statement(Import_function_body
*, Location
);
311 // Return whether this is a block statement.
313 is_block_statement() const
314 { return this->classification_
== STATEMENT_BLOCK
; }
316 // If this is an assignment statement, return it. Otherwise return
318 Assignment_statement
*
319 assignment_statement()
321 return this->convert
<Assignment_statement
, STATEMENT_ASSIGNMENT
>();
324 // If this is an temporary statement, return it. Otherwise return
327 temporary_statement()
329 return this->convert
<Temporary_statement
, STATEMENT_TEMPORARY
>();
332 // If this is a variable declaration statement, return it.
333 // Otherwise return NULL.
334 Variable_declaration_statement
*
335 variable_declaration_statement()
337 return this->convert
<Variable_declaration_statement
,
338 STATEMENT_VARIABLE_DECLARATION
>();
341 // If this is an expression statement, return it. Otherwise return
343 Expression_statement
*
344 expression_statement()
346 return this->convert
<Expression_statement
, STATEMENT_EXPRESSION
>();
349 // If this is an block statement, return it. Otherwise return
353 { return this->convert
<Block_statement
, STATEMENT_BLOCK
>(); }
355 // If this is a return statement, return it. Otherwise return NULL.
358 { return this->convert
<Return_statement
, STATEMENT_RETURN
>(); }
360 // If this is a thunk statement (a go or defer statement), return
361 // it. Otherwise return NULL.
365 // If this is a defer statement, return it. Otherwise return NULL.
368 { return this->convert
<Defer_statement
, STATEMENT_DEFER
>(); }
370 // If this is a goto statement, return it. Otherwise return NULL.
373 { return this->convert
<Goto_statement
, STATEMENT_GOTO
>(); }
375 // If this is a goto_unnamed statement, return it. Otherwise return NULL.
376 Goto_unnamed_statement
*
377 goto_unnamed_statement()
378 { return this->convert
<Goto_unnamed_statement
, STATEMENT_GOTO_UNNAMED
>(); }
380 // If this is a label statement, return it. Otherwise return NULL.
383 { return this->convert
<Label_statement
, STATEMENT_LABEL
>(); }
385 // If this is an unnamed_label statement, return it. Otherwise return NULL.
386 Unnamed_label_statement
*
387 unnamed_label_statement()
388 { return this->convert
<Unnamed_label_statement
, STATEMENT_UNNAMED_LABEL
>(); }
390 // If this is an if statement, return it. Otherwise return NULL.
393 { return this->convert
<If_statement
, STATEMENT_IF
>(); }
395 // If this is a for statement, return it. Otherwise return NULL.
398 { return this->convert
<For_statement
, STATEMENT_FOR
>(); }
400 // If this is a for statement over a range clause, return it.
401 // Otherwise return NULL.
403 for_range_statement()
404 { return this->convert
<For_range_statement
, STATEMENT_FOR_RANGE
>(); }
406 // If this is a switch statement, return it. Otherwise return NULL.
409 { return this->convert
<Switch_statement
, STATEMENT_SWITCH
>(); }
411 // If this is a type switch statement, return it. Otherwise return
413 Type_switch_statement
*
414 type_switch_statement()
415 { return this->convert
<Type_switch_statement
, STATEMENT_TYPE_SWITCH
>(); }
417 // If this is a send statement, return it. Otherwise return NULL.
420 { return this->convert
<Send_statement
, STATEMENT_SEND
>(); }
422 // If this is a select statement, return it. Otherwise return NULL.
425 { return this->convert
<Select_statement
, STATEMENT_SELECT
>(); }
427 // Return true if this statement may fall through--if after
428 // executing this statement we may go on to execute the following
429 // statement, if any.
431 may_fall_through() const
432 { return this->do_may_fall_through(); }
434 // Convert the statement to the backend representation.
436 get_backend(Translate_context
*);
438 // Dump AST representation of a statement to a dump context.
440 dump_statement(Ast_dump_context
*) const;
443 // Implemented by child class: traverse the tree.
445 do_traverse(Traverse
*) = 0;
447 // Implemented by the child class: lower this statement to a simpler
450 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
453 // Implemented by the child class: lower this statement to a simpler
456 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
459 // Implemented by child class: set type information for unnamed
460 // constants. Any statement which includes an expression needs to
463 do_determine_types(Gogo
*)
466 // Implemented by child class: check types of expressions used in a
469 do_check_types(Gogo
*)
472 // Implemented by child class: return the cost of this statement for
473 // inlining. The default cost is high, so we only need to define
474 // this method for statements that can be inlined.
479 // Implemented by child class: write export data for this statement
480 // to the string. This need only be implemented by classes that
481 // implement do_inlining_cost with a reasonable value.
483 do_export_statement(Export_function_body
*)
484 { go_unreachable(); }
486 // Implemented by child class: return true if this statement may
489 do_may_fall_through() const
492 // Implemented by child class: convert to backend representation.
494 do_get_backend(Translate_context
*) = 0;
496 // Implemented by child class: dump ast representation.
498 do_dump_statement(Ast_dump_context
*) const = 0;
500 // Implemented by child class: make implicit conversions explicit.
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; }
584 // Check if we can assign RHS to LHS. If we can, return true. If
585 // we can't, report an error and return false.
587 check_assignment_types(Expression
* lhs
, Type
* rhs_type
, Location
);
591 do_traverse(Traverse
* traverse
);
594 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
597 do_determine_types(Gogo
*);
600 do_check_types(Gogo
*);
607 do_export_statement(Export_function_body
*);
610 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
613 do_get_backend(Translate_context
*);
616 do_dump_statement(Ast_dump_context
*) const;
619 do_add_conversions();
622 // Left hand side--the lvalue.
624 // Right hand side--the rvalue.
626 // True if we can omit a write barrier from this assignment.
627 bool omit_write_barrier_
;
630 // A statement which creates and initializes a temporary variable.
632 class Temporary_statement
: public Statement
635 Temporary_statement(Type
* type
, Expression
* init
, Location location
)
636 : Statement(STATEMENT_TEMPORARY
, location
),
637 type_(type
), init_(init
), bvariable_(NULL
), is_address_taken_(false),
638 value_escapes_(false), assigned_(false), uses_(0)
641 // Return the type of the temporary variable.
645 // Return the initializer if there is one.
648 { return this->init_
; }
650 // Set the initializer.
652 set_init(Expression
* expr
)
653 { this->init_
= expr
; }
655 // Whether something takes the address of this temporary
659 { return this->is_address_taken_
; }
661 // Record that something takes the address of this temporary
664 set_is_address_taken()
665 { this->is_address_taken_
= true; }
667 // Whether the value escapes.
669 value_escapes() const
670 { return this->value_escapes_
; }
672 // Record that the value escapes.
675 { this->value_escapes_
= true; }
677 // Whether this temporary variable is assigned (after initialization).
680 { return this->assigned_
; }
682 // Record that this temporary variable is assigned.
685 { this->assigned_
= true; }
687 // Number of uses of this temporary variable.
690 { return this->uses_
; }
692 // Add one use of this temporary variable.
697 // Return the temporary variable. This should not be called until
698 // after the statement itself has been converted.
700 get_backend_variable(Translate_context
*) const;
702 // Import the declaration of a temporary.
704 do_import(Import_function_body
*, Location
);
708 do_traverse(Traverse
*);
711 do_determine_types(Gogo
*);
714 do_check_types(Gogo
*);
721 do_export_statement(Export_function_body
*);
724 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
727 do_get_backend(Translate_context
*);
730 do_dump_statement(Ast_dump_context
*) const;
733 do_add_conversions();
736 // The type of the temporary variable.
738 // The initial value of the temporary variable. This may be NULL.
740 // The backend representation of the temporary variable.
741 Bvariable
* bvariable_
;
742 // True if something takes the address of this temporary variable.
743 bool is_address_taken_
;
744 // True if the value assigned to this temporary variable escapes.
745 // This is used for select statements.
747 // True if this temporary variable is assigned (after initialization).
749 // Number of uses of this temporary variable.
753 // A variable declaration. This marks the point in the code where a
754 // variable is declared. The Variable is also attached to a Block.
756 class Variable_declaration_statement
: public Statement
759 Variable_declaration_statement(Named_object
* var
);
761 // The variable being declared.
764 { return this->var_
; }
766 // Import a variable declaration.
768 do_import(Import_function_body
*, Location
);
772 do_traverse(Traverse
*);
775 do_determine_types(Gogo
*);
778 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
785 do_export_statement(Export_function_body
*);
788 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
791 do_get_backend(Translate_context
*);
794 do_dump_statement(Ast_dump_context
*) const;
797 do_add_conversions();
803 // A return statement.
805 class Return_statement
: public Statement
808 Return_statement(Named_object
* function
, Expression_list
* vals
,
810 : Statement(STATEMENT_RETURN
, location
),
811 function_(function
), vals_(vals
), types_are_determined_(false),
815 // The list of values being returned. This may be NULL.
816 const Expression_list
*
818 { return this->vals_
; }
822 do_traverse(Traverse
* traverse
)
823 { return this->traverse_expression_list(traverse
, this->vals_
); }
826 do_determine_types(Gogo
*);
829 do_check_types(Gogo
*);
832 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
835 do_may_fall_through() const
843 do_export_statement(Export_function_body
*);
846 do_get_backend(Translate_context
*);
849 do_dump_statement(Ast_dump_context
*) const;
852 // A backpointer to the function we are returning from.
853 Named_object
* function_
;
854 // Return values. This may be NULL.
855 Expression_list
* vals_
;
856 // True if types have been determined.
857 bool types_are_determined_
;
858 // True if this statement has been lowered.
862 // An expression statement.
864 class Expression_statement
: public Statement
867 Expression_statement(Expression
* expr
, bool is_ignored
);
871 { return this->expr_
; }
875 do_traverse(Traverse
* traverse
)
876 { return this->traverse_expression(traverse
, &this->expr_
); }
879 do_determine_types(Gogo
*);
882 do_check_types(Gogo
*);
885 do_may_fall_through() const;
892 do_export_statement(Export_function_body
*);
895 do_get_backend(Translate_context
* context
);
898 do_dump_statement(Ast_dump_context
*) const;
902 // Whether the value of this expression is being explicitly ignored.
906 // A block statement--a list of statements which may include variable
909 class Block_statement
: public Statement
912 Block_statement(Block
* block
, Location location
)
913 : Statement(STATEMENT_BLOCK
, location
),
914 block_(block
), is_lowered_for_statement_(false)
917 // Return the actual block.
920 { return this->block_
; }
923 set_is_lowered_for_statement()
924 { this->is_lowered_for_statement_
= true; }
927 is_lowered_for_statement()
928 { return this->is_lowered_for_statement_
; }
930 // Export a block for a block statement.
932 export_block(Export_function_body
*, Block
*, bool is_lowered_for_statement
);
934 // Import a block statement, returning the block.
935 // *IS_LOWERED_FOR_STATEMENT reports whether this block statement
936 // was lowered from a for statement.
938 do_import(Import_function_body
*, Location
, bool* is_lowered_for_statement
);
942 do_traverse(Traverse
* traverse
)
943 { return this->block_
->traverse(traverse
); }
946 do_determine_types(Gogo
* gogo
)
947 { this->block_
->determine_types(gogo
); }
954 do_export_statement(Export_function_body
*);
957 do_may_fall_through() const
958 { return this->block_
->may_fall_through(); }
961 do_get_backend(Translate_context
* context
);
964 do_dump_statement(Ast_dump_context
*) const;
968 // True if this block statement represents a lowered for statement.
969 bool is_lowered_for_statement_
;
974 class Send_statement
: public Statement
977 Send_statement(Expression
* channel
, Expression
* val
,
979 : Statement(STATEMENT_SEND
, location
),
980 channel_(channel
), val_(val
)
985 { return this->channel_
; }
989 { return this->val_
; }
993 do_traverse(Traverse
* traverse
);
996 do_determine_types(Gogo
*);
999 do_check_types(Gogo
*);
1002 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1005 do_get_backend(Translate_context
*);
1008 do_dump_statement(Ast_dump_context
*) const;
1011 do_add_conversions();
1014 // The channel on which to send the value.
1015 Expression
* channel_
;
1016 // The value to send.
1020 // Select_clauses holds the clauses of a select statement. This is
1021 // built by the parser.
1023 class Select_clauses
1030 // Add a new clause. IS_SEND is true if this is a send clause,
1031 // false for a receive clause. For a send clause CHANNEL is the
1032 // channel and VAL is the value to send. For a receive clause
1033 // CHANNEL is the channel, VAL is either NULL or a Var_expression
1034 // for the variable to set, and CLOSED is either NULL or a
1035 // Var_expression to set to whether the channel is closed. If VAL
1036 // is NULL, VAR may be a variable to be initialized with the
1037 // received value, and CLOSEDVAR may be a variable to be initialized
1038 // with whether the channel is closed. IS_DEFAULT is true if this
1039 // is the default clause. STATEMENTS is the list of statements to
1042 add(bool is_send
, Expression
* channel
, Expression
* val
, Expression
* closed
,
1043 Named_object
* var
, Named_object
* closedvar
, bool is_default
,
1044 Block
* statements
, Location location
)
1046 this->clauses_
.push_back(Select_clause(is_send
, channel
, val
, closed
, var
,
1047 closedvar
, is_default
, statements
,
1053 { return this->clauses_
.size(); }
1056 has_default() const;
1058 // Traverse the select clauses.
1060 traverse(Traverse
*);
1062 // Lower statements.
1064 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*,
1065 Temporary_statement
*, int* send_count
, int* recv_count
);
1069 determine_types(Gogo
*);
1075 // Whether the select clauses may fall through to the statement
1076 // which follows the overall select statement.
1078 may_fall_through() const;
1080 // Convert to the backend representation.
1082 get_backend(Translate_context
*, Temporary_statement
* index
,
1083 Unnamed_label
* break_label
, Location
);
1085 // Dump AST representation.
1087 dump_clauses(Ast_dump_context
*) const;
1094 : channel_(NULL
), val_(NULL
), closed_(NULL
), var_(NULL
),
1095 closedvar_(NULL
), statements_(NULL
), is_send_(false),
1099 Select_clause(bool is_send
, Expression
* channel
, Expression
* val
,
1100 Expression
* closed
, Named_object
* var
,
1101 Named_object
* closedvar
, bool is_default
, Block
* statements
,
1103 : channel_(channel
), val_(val
), closed_(closed
), var_(var
),
1104 closedvar_(closedvar
), statements_(statements
), case_index_(0),
1105 location_(location
), is_send_(is_send
), is_default_(is_default
),
1106 is_lowered_(false), is_case_index_set_(false)
1107 { go_assert(is_default
? channel
== NULL
: channel
!= NULL
); }
1109 // Traverse the select clause.
1111 traverse(Traverse
*);
1113 // Lower statements.
1115 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*, int,
1116 Temporary_statement
*);
1120 determine_types(Gogo
*);
1126 // Return true if this is the default clause.
1129 { return this->is_default_
; }
1131 // Return the channel. This will return NULL for the default
1135 { return this->channel_
; }
1137 // Return true for a send, false for a receive.
1141 go_assert(!this->is_default_
);
1142 return this->is_send_
;
1145 // Return the value to send or the lvalue to receive into.
1148 { return this->val_
; }
1150 // Return the lvalue to set to whether the channel is closed
1154 { return this->closed_
; }
1156 // Return the variable to initialize, for "case a := <-ch".
1159 { return this->var_
; }
1161 // Return the variable to initialize to whether the channel
1162 // is closed, for "case a, c := <-ch".
1165 { return this->closedvar_
; }
1167 // Return the statements.
1170 { return this->statements_
; }
1172 // Return the location.
1175 { return this->location_
; }
1177 // Return the case index for this clause.
1181 go_assert(this->is_case_index_set_
);
1182 return this->case_index_
;
1185 // Set the case index.
1187 set_case_index(int i
)
1189 go_assert(!this->is_case_index_set_
);
1190 this->case_index_
= i
;
1191 this->is_case_index_set_
= true;
1194 // Whether this clause may fall through to the statement which
1195 // follows the overall select statement.
1197 may_fall_through() const;
1199 // Convert the statements to the backend representation.
1201 get_statements_backend(Translate_context
*);
1203 // Dump AST representation.
1205 dump_clause(Ast_dump_context
*) const;
1209 lower_send(Gogo
*, Block
*, Expression
*, Expression
*);
1212 lower_recv(Gogo
*, Named_object
*, Block
*, Expression
*, Expression
*,
1213 Temporary_statement
*);
1216 set_case(Gogo
*, Block
*, Expression
*, Expression
*, Expression
*);
1219 Expression
* channel_
;
1220 // The value to send or the lvalue to receive into.
1222 // The lvalue to set to whether the channel is closed on a
1224 Expression
* closed_
;
1225 // The variable to initialize, for "case a := <-ch".
1227 // The variable to initialize to whether the channel is closed,
1228 // for "case a, c := <-ch".
1229 Named_object
* closedvar_
;
1230 // The statements to execute.
1232 // The index of this clause in the switch statement. If
1233 // runtime.selectgo returns this index, this clause has been
1236 // The location of this clause.
1238 // Whether this is a send or a receive.
1240 // Whether this is the default.
1242 // Whether this has been lowered.
1244 // Whether the case index has been set.
1245 bool is_case_index_set_
;
1250 { return this->clauses_
.at(i
); }
1253 typedef std::vector
<Select_clause
> Clauses
;
1258 // A select statement.
1260 class Select_statement
: public Statement
1263 Select_statement(Location location
)
1264 : Statement(STATEMENT_SELECT
, location
),
1265 clauses_(NULL
), index_(NULL
), break_label_(NULL
), is_lowered_(false)
1270 add_clauses(Select_clauses
* clauses
)
1272 go_assert(this->clauses_
== NULL
);
1273 this->clauses_
= clauses
;
1276 // Return the break label for this select statement.
1282 do_traverse(Traverse
* traverse
)
1283 { return this->clauses_
->traverse(traverse
); }
1286 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1289 do_determine_types(Gogo
* gogo
)
1290 { this->clauses_
->determine_types(gogo
); }
1293 do_check_types(Gogo
*)
1294 { this->clauses_
->check_types(); }
1297 do_may_fall_through() const;
1300 do_get_backend(Translate_context
*);
1303 do_dump_statement(Ast_dump_context
*) const;
1306 // Lower a one-case select statement.
1308 lower_one_case(Gogo
*, Block
*);
1310 // Lower a two-case select statement with one defualt case.
1312 lower_two_case(Gogo
*, Block
*);
1314 // The select clauses.
1315 Select_clauses
* clauses_
;
1316 // A temporary that holds the index value returned by selectgo.
1317 Temporary_statement
* index_
;
1319 Unnamed_label
* break_label_
;
1320 // Whether this statement has been lowered.
1324 // A statement which requires a thunk: go or defer.
1326 class Thunk_statement
: public Statement
1329 Thunk_statement(Statement_classification
, Call_expression
*,
1332 // Return the call expression.
1335 { return this->call_
; }
1337 // Simplify a go or defer statement so that it only uses a single
1340 simplify_statement(Gogo
*, Named_object
*, Block
*);
1344 do_traverse(Traverse
* traverse
);
1347 do_determine_types(Gogo
*);
1350 do_check_types(Gogo
*);
1352 // Return the function and argument for the call.
1354 get_fn_and_arg(Expression
** pfn
, Expression
** parg
);
1357 // Return whether this is a simple go statement.
1359 is_simple(Function_type
*) const;
1361 // Return whether the thunk function is a constant.
1363 is_constant_function() const;
1365 // Build the struct to use for a complex case.
1367 build_struct(Function_type
* fntype
);
1371 build_thunk(Gogo
*, const std::string
&, Struct_type
*);
1373 // Set the name to use for thunk field N.
1375 thunk_field_param(int n
, char* buf
, size_t buflen
);
1377 // The function call to be executed in a separate thread (go) or
1384 class Go_statement
: public Thunk_statement
1387 Go_statement(Call_expression
* call
, Location location
)
1388 : Thunk_statement(STATEMENT_GO
, call
, location
)
1393 do_get_backend(Translate_context
*);
1396 do_dump_statement(Ast_dump_context
*) const;
1399 // A defer statement.
1401 class Defer_statement
: public Thunk_statement
1404 Defer_statement(Call_expression
* call
, Location location
)
1405 : Thunk_statement(STATEMENT_DEFER
, call
, location
),
1411 { this->on_stack_
= true; }
1415 do_get_backend(Translate_context
*);
1418 do_dump_statement(Ast_dump_context
*) const;
1422 defer_struct_type();
1427 // A goto statement.
1429 class Goto_statement
: public Statement
1432 Goto_statement(Label
* label
, Location location
)
1433 : Statement(STATEMENT_GOTO
, location
),
1437 // Return the label being jumped to.
1440 { return this->label_
; }
1442 // Import a goto statement.
1444 do_import(Import_function_body
*, Location
);
1448 do_traverse(Traverse
*);
1451 do_check_types(Gogo
*);
1454 do_may_fall_through() const
1458 do_get_backend(Translate_context
*);
1465 do_export_statement(Export_function_body
*);
1468 do_dump_statement(Ast_dump_context
*) const;
1474 // A goto statement to an unnamed label.
1476 class Goto_unnamed_statement
: public Statement
1479 Goto_unnamed_statement(Unnamed_label
* label
, Location location
)
1480 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
1485 unnamed_label() const
1486 { return this->label_
; }
1490 do_traverse(Traverse
*);
1493 do_may_fall_through() const
1497 do_get_backend(Translate_context
* context
);
1504 do_export_statement(Export_function_body
*);
1507 do_dump_statement(Ast_dump_context
*) const;
1510 Unnamed_label
* label_
;
1513 // A label statement.
1515 class Label_statement
: public Statement
1518 Label_statement(Label
* label
, Location location
)
1519 : Statement(STATEMENT_LABEL
, location
),
1523 // Return the label itself.
1526 { return this->label_
; }
1528 // Import a label or unnamed label.
1530 do_import(Import_function_body
*, Location
);
1534 do_traverse(Traverse
*);
1537 do_get_backend(Translate_context
*);
1544 do_export_statement(Export_function_body
*);
1547 do_dump_statement(Ast_dump_context
*) const;
1554 // An unnamed label statement.
1556 class Unnamed_label_statement
: public Statement
1559 Unnamed_label_statement(Unnamed_label
* label
);
1563 do_traverse(Traverse
*);
1566 do_get_backend(Translate_context
* context
);
1573 do_export_statement(Export_function_body
*);
1576 do_dump_statement(Ast_dump_context
*) const;
1580 Unnamed_label
* label_
;
1585 class If_statement
: public Statement
1588 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
1590 : Statement(STATEMENT_IF
, location
),
1591 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
1596 { return this->cond_
; }
1600 { return this->then_block_
; }
1604 { return this->else_block_
; }
1606 // Import an if statement.
1608 do_import(Import_function_body
*, Location
);
1612 do_traverse(Traverse
*);
1615 do_determine_types(Gogo
*);
1618 do_check_types(Gogo
*);
1625 do_export_statement(Export_function_body
*);
1628 do_may_fall_through() const;
1631 do_get_backend(Translate_context
*);
1634 do_dump_statement(Ast_dump_context
*) const;
1644 class For_statement
: public Statement
1647 For_statement(Block
* init
, Expression
* cond
, Block
* post
,
1649 : Statement(STATEMENT_FOR
, location
),
1650 init_(init
), cond_(cond
), post_(post
), statements_(NULL
),
1651 break_label_(NULL
), continue_label_(NULL
)
1654 // Add the statements.
1656 add_statements(Block
* statements
)
1658 go_assert(this->statements_
== NULL
);
1659 this->statements_
= statements
;
1662 // Return the break label for this for statement.
1666 // Return the continue label for this for statement.
1670 // Set the break and continue labels for this statement.
1672 set_break_continue_labels(Unnamed_label
* break_label
,
1673 Unnamed_label
* continue_label
);
1677 do_traverse(Traverse
*);
1680 do_determine_types(Gogo
*);
1683 do_check_types(Gogo
*);
1686 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1689 do_may_fall_through() const;
1692 do_get_backend(Translate_context
*)
1693 { go_unreachable(); }
1696 do_dump_statement(Ast_dump_context
*) const;
1699 // The initialization statements. This may be NULL.
1701 // The condition. This may be NULL.
1703 // The statements to run after each iteration. This may be NULL.
1705 // The statements in the loop itself.
1707 // The break label, if needed.
1708 Unnamed_label
* break_label_
;
1709 // The continue label, if needed.
1710 Unnamed_label
* continue_label_
;
1713 // A for statement over a range clause.
1715 class For_range_statement
: public Statement
1718 For_range_statement(Expression
* index_var
, Expression
* value_var
,
1719 Expression
* range
, Location location
)
1720 : Statement(STATEMENT_FOR_RANGE
, location
),
1721 index_var_(index_var
), value_var_(value_var
), range_(range
),
1722 statements_(NULL
), break_label_(NULL
), continue_label_(NULL
)
1725 // Add the statements.
1727 add_statements(Block
* statements
)
1729 go_assert(this->statements_
== NULL
);
1730 this->statements_
= statements
;
1733 // Return the break label for this for statement.
1737 // Return the continue label for this for statement.
1743 do_traverse(Traverse
*);
1746 do_determine_types(Gogo
*);
1749 do_check_types(Gogo
*);
1752 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1755 do_get_backend(Translate_context
*)
1756 { go_unreachable(); }
1759 do_dump_statement(Ast_dump_context
*) const;
1763 make_range_ref(Named_object
*, Temporary_statement
*, Location
);
1766 call_builtin(Gogo
*, const char* funcname
, Expression
* arg
, Location
);
1769 lower_range_array(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1770 Temporary_statement
*, Temporary_statement
*,
1771 Block
**, Expression
**, Block
**, Block
**);
1774 lower_range_slice(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1775 Temporary_statement
*, Temporary_statement
*,
1776 Block
**, Expression
**, Block
**, Block
**);
1779 lower_range_string(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1780 Temporary_statement
*, Temporary_statement
*,
1781 Block
**, Expression
**, Block
**, Block
**);
1784 lower_range_map(Gogo
*, Map_type
*, Block
*, Block
*, Named_object
*,
1785 Temporary_statement
*, Temporary_statement
*,
1786 Temporary_statement
*, Block
**, Expression
**, Block
**,
1790 lower_range_channel(Gogo
*, Block
*, Block
*, Named_object
*,
1791 Temporary_statement
*, Temporary_statement
*,
1792 Temporary_statement
*, Block
**, Expression
**, Block
**,
1796 lower_map_range_clear(Gogo
*, Type
*, Block
*, Expression
*, Named_object
*,
1797 Temporary_statement
*, Location
);
1800 lower_array_range_clear(Gogo
*, Type
*, Expression
*, Block
*,
1801 Named_object
*, Temporary_statement
*,
1804 // The variable which is set to the index value.
1805 Expression
* index_var_
;
1806 // The variable which is set to the element value. This may be
1808 Expression
* value_var_
;
1809 // The expression we are ranging over.
1811 // The statements in the block.
1813 // The break label, if needed.
1814 Unnamed_label
* break_label_
;
1815 // The continue label, if needed.
1816 Unnamed_label
* continue_label_
;
1819 // Class Case_clauses holds the clauses of a switch statement. This
1820 // is built by the parser.
1829 // Add a new clause. CASES is a list of case expressions; it may be
1830 // NULL. IS_DEFAULT is true if this is the default case.
1831 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1832 // after the statements the case clause should fall through to the
1835 add(Expression_list
* cases
, bool is_default
, Block
* statements
,
1836 bool is_fallthrough
, Location location
)
1838 this->clauses_
.push_back(Case_clause(cases
, is_default
, statements
,
1839 is_fallthrough
, location
));
1842 // Return whether there are no clauses.
1845 { return this->clauses_
.empty(); }
1847 // Traverse the case clauses.
1849 traverse(Traverse
*);
1851 // Lower for a nonconstant switch.
1853 lower(Gogo
*, Block
*, Temporary_statement
*, Unnamed_label
*) const;
1855 // Determine types of expressions. The Type parameter is the type
1856 // of the switch value.
1858 determine_types(Gogo
*, Type
*);
1860 // Check types. The Type parameter is the type of the switch value.
1864 // Return true if all the clauses are constant values.
1866 is_constant() const;
1868 // Return true if these clauses may fall through to the statements
1869 // following the switch statement.
1871 may_fall_through() const;
1873 // Return the body of a SWITCH_EXPR when all the clauses are
1876 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1877 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
1878 std::vector
<Bstatement
*>* all_statements
) const;
1880 // Dump the AST representation to a dump context.
1882 dump_clauses(Ast_dump_context
*) const;
1885 // For a constant switch we need to keep a record of constants we
1886 // have already seen.
1887 class Hash_integer_value
;
1888 class Eq_integer_value
;
1889 typedef Unordered_set_hash(Expression
*, Hash_integer_value
,
1890 Eq_integer_value
) Case_constants
;
1897 : cases_(NULL
), statements_(NULL
), is_default_(false),
1898 is_fallthrough_(false), location_(Linemap::unknown_location())
1901 Case_clause(Expression_list
* cases
, bool is_default
, Block
* statements
,
1902 bool is_fallthrough
, Location location
)
1903 : cases_(cases
), statements_(statements
), is_default_(is_default
),
1904 is_fallthrough_(is_fallthrough
), location_(location
)
1907 // Whether this clause falls through to the next clause.
1909 is_fallthrough() const
1910 { return this->is_fallthrough_
; }
1912 // Whether this is the default.
1915 { return this->is_default_
; }
1917 // The location of this clause.
1920 { return this->location_
; }
1924 traverse(Traverse
*);
1926 // Lower for a nonconstant switch.
1928 lower(Gogo
*, Block
*, Temporary_statement
*, Unnamed_label
*,
1929 Unnamed_label
*) const;
1933 determine_types(Gogo
*, Type
*);
1939 // Return true if all the case expressions are constant.
1941 is_constant() const;
1943 // Return true if this clause may fall through to execute the
1944 // statements following the switch statement. This is not the
1945 // same as whether this clause falls through to the next clause.
1947 may_fall_through() const;
1949 // Convert the case values and statements to the backend
1952 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1953 Case_constants
*, std::vector
<Bexpression
*>* cases
) const;
1955 // Dump the AST representation to a dump context.
1957 dump_clause(Ast_dump_context
*) const;
1960 // The list of case expressions.
1961 Expression_list
* cases_
;
1962 // The statements to execute.
1964 // Whether this is the default case.
1966 // Whether this falls through after the statements.
1967 bool is_fallthrough_
;
1968 // The location of this case clause.
1972 friend class Case_clause
;
1974 // The type of the list of clauses.
1975 typedef std::vector
<Case_clause
> Clauses
;
1977 // All the case clauses.
1981 // A switch statement.
1983 class Switch_statement
: public Statement
1986 Switch_statement(Expression
* val
, Location location
)
1987 : Statement(STATEMENT_SWITCH
, location
),
1988 val_(val
), clauses_(NULL
), break_label_(NULL
)
1993 add_clauses(Case_clauses
* clauses
)
1995 go_assert(this->clauses_
== NULL
);
1996 this->clauses_
= clauses
;
1999 // Return the break label for this switch statement.
2005 do_traverse(Traverse
*);
2008 do_determine_types(Gogo
*);
2011 do_check_types(Gogo
*);
2014 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
2017 do_get_backend(Translate_context
*)
2018 { go_unreachable(); }
2021 do_dump_statement(Ast_dump_context
*) const;
2024 do_may_fall_through() const;
2027 // The value to switch on. This may be NULL.
2029 // The case clauses.
2030 Case_clauses
* clauses_
;
2031 // The break label, if needed.
2032 Unnamed_label
* break_label_
;
2035 // Class Type_case_clauses holds the clauses of a type switch
2036 // statement. This is built by the parser.
2038 class Type_case_clauses
2045 // Add a new clause. TYPE is the type for this clause; it may be
2046 // NULL. IS_FALLTHROUGH is true if this falls through to the next
2047 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
2048 // if this is the default case. STATEMENTS is a block of
2049 // statements; it may be NULL.
2051 add(Type
* type
, bool is_fallthrough
, bool is_default
, Block
* statements
,
2054 this->clauses_
.push_back(Type_case_clause(type
, is_fallthrough
, is_default
,
2055 statements
, location
));
2058 // Return whether there are no clauses.
2061 { return this->clauses_
.empty(); }
2063 // Traverse the type case clauses.
2065 traverse(Traverse
*);
2067 // Check for duplicates.
2069 check_duplicates() const;
2071 // Determine types of expressions.
2073 determine_types(Gogo
*);
2079 // Lower to if and goto statements.
2081 lower(Gogo
*, Block
*, Temporary_statement
* descriptor_temp
,
2082 Unnamed_label
* break_label
) const;
2084 // Return true if these clauses may fall through to the statements
2085 // following the switch statement.
2087 may_fall_through() const;
2089 // Dump the AST representation to a dump context.
2091 dump_clauses(Ast_dump_context
*) const;
2094 // One type case clause.
2095 class Type_case_clause
2099 : type_(NULL
), statements_(NULL
), is_default_(false),
2100 location_(Linemap::unknown_location())
2103 Type_case_clause(Type
* type
, bool is_fallthrough
, bool is_default
,
2104 Block
* statements
, Location location
)
2105 : type_(type
), statements_(statements
), is_fallthrough_(is_fallthrough
),
2106 is_default_(is_default
), location_(location
)
2112 { return this->type_
; }
2114 // Whether this is the default.
2117 { return this->is_default_
; }
2119 // The location of this type clause.
2122 { return this->location_
; }
2126 traverse(Traverse
*);
2130 determine_types(Gogo
*);
2136 // Lower to if and goto statements.
2138 lower(Gogo
*, Block
*, Temporary_statement
* descriptor_temp
,
2139 Unnamed_label
* break_label
, Unnamed_label
** stmts_label
) const;
2141 // Return true if this clause may fall through to execute the
2142 // statements following the switch statement. This is not the
2143 // same as whether this clause falls through to the next clause.
2145 may_fall_through() const;
2147 // Dump the AST representation to a dump context.
2149 dump_clause(Ast_dump_context
*) const;
2152 // The type for this type clause.
2154 // The statements to execute.
2156 // Whether this falls through--this is true for "case T1, T2".
2157 bool is_fallthrough_
;
2158 // Whether this is the default case.
2160 // The location of this type case clause.
2164 friend class Type_case_clause
;
2166 // The type of the list of type clauses.
2167 typedef std::vector
<Type_case_clause
> Type_clauses
;
2169 // All the type case clauses.
2170 Type_clauses clauses_
;
2173 // A type switch statement.
2175 class Type_switch_statement
: public Statement
2178 Type_switch_statement(Expression
* expr
, Location location
)
2179 : Statement(STATEMENT_TYPE_SWITCH
, location
),
2180 expr_(expr
), clauses_(NULL
), break_label_(NULL
)
2185 add_clauses(Type_case_clauses
* clauses
)
2187 go_assert(this->clauses_
== NULL
);
2188 this->clauses_
= clauses
;
2191 // Return the break label for this type switch statement.
2197 do_traverse(Traverse
*);
2200 do_determine_types(Gogo
*);
2203 do_check_types(Gogo
*);
2206 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
2209 do_get_backend(Translate_context
*)
2210 { go_unreachable(); }
2213 do_dump_statement(Ast_dump_context
*) const;
2216 do_may_fall_through() const;
2219 // The expression we are switching on.
2221 // The type case clauses.
2222 Type_case_clauses
* clauses_
;
2223 // The break label, if needed.
2224 Unnamed_label
* break_label_
;
2227 #endif // !defined(GO_STATEMENTS_H)