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 // Make implicit type conversions explicit.
344 { this->do_add_conversions(); }
346 // Read a statement from export data. The location should be used
347 // for the returned statement. Errors should be reported using the
348 // Import_function_body's location method.
350 import_statement(Import_function_body
*, Location
);
352 // Return whether this is a block statement.
354 is_block_statement() const
355 { return this->classification_
== STATEMENT_BLOCK
; }
357 // If this is an assignment statement, return it. Otherwise return
359 Assignment_statement
*
360 assignment_statement()
362 return this->convert
<Assignment_statement
, STATEMENT_ASSIGNMENT
>();
365 // If this is an temporary statement, return it. Otherwise return
368 temporary_statement()
370 return this->convert
<Temporary_statement
, STATEMENT_TEMPORARY
>();
373 // If this is a variable declaration statement, return it.
374 // Otherwise return NULL.
375 Variable_declaration_statement
*
376 variable_declaration_statement()
378 return this->convert
<Variable_declaration_statement
,
379 STATEMENT_VARIABLE_DECLARATION
>();
382 // If this is an expression statement, return it. Otherwise return
384 Expression_statement
*
385 expression_statement()
387 return this->convert
<Expression_statement
, STATEMENT_EXPRESSION
>();
390 // If this is an block statement, return it. Otherwise return
394 { return this->convert
<Block_statement
, STATEMENT_BLOCK
>(); }
396 // If this is a return statement, return it. Otherwise return NULL.
399 { return this->convert
<Return_statement
, STATEMENT_RETURN
>(); }
401 // If this is a thunk statement (a go or defer statement), return
402 // it. Otherwise return NULL.
406 // If this is a goto statement, return it. Otherwise return NULL.
409 { return this->convert
<Goto_statement
, STATEMENT_GOTO
>(); }
411 // If this is a goto_unnamed statement, return it. Otherwise return NULL.
412 Goto_unnamed_statement
*
413 goto_unnamed_statement()
414 { return this->convert
<Goto_unnamed_statement
, STATEMENT_GOTO_UNNAMED
>(); }
416 // If this is a label statement, return it. Otherwise return NULL.
419 { return this->convert
<Label_statement
, STATEMENT_LABEL
>(); }
421 // If this is an unnamed_label statement, return it. Otherwise return NULL.
422 Unnamed_label_statement
*
423 unnamed_label_statement()
424 { return this->convert
<Unnamed_label_statement
, STATEMENT_UNNAMED_LABEL
>(); }
426 // If this is an if statement, return it. Otherwise return NULL.
429 { return this->convert
<If_statement
, STATEMENT_IF
>(); }
431 // If this is a for statement, return it. Otherwise return NULL.
434 { return this->convert
<For_statement
, STATEMENT_FOR
>(); }
436 // If this is a for statement over a range clause, return it.
437 // Otherwise return NULL.
439 for_range_statement()
440 { return this->convert
<For_range_statement
, STATEMENT_FOR_RANGE
>(); }
442 // If this is a switch statement, return it. Otherwise return NULL.
445 { return this->convert
<Switch_statement
, STATEMENT_SWITCH
>(); }
447 // If this is a type switch statement, return it. Otherwise return
449 Type_switch_statement
*
450 type_switch_statement()
451 { return this->convert
<Type_switch_statement
, STATEMENT_TYPE_SWITCH
>(); }
453 // If this is a send statement, return it. Otherwise return NULL.
456 { return this->convert
<Send_statement
, STATEMENT_SEND
>(); }
458 // If this is a select statement, return it. Otherwise return NULL.
461 { return this->convert
<Select_statement
, STATEMENT_SELECT
>(); }
463 // Return true if this statement may fall through--if after
464 // executing this statement we may go on to execute the following
465 // statement, if any.
467 may_fall_through() const
468 { return this->do_may_fall_through(); }
470 // Convert the statement to the backend representation.
472 get_backend(Translate_context
*);
474 // Dump AST representation of a statement to a dump context.
476 dump_statement(Ast_dump_context
*) const;
479 // Implemented by child class: traverse the tree.
481 do_traverse(Traverse
*) = 0;
483 // Implemented by child class: traverse assignments. Any statement
484 // which includes an assignment should implement this.
486 do_traverse_assignments(Traverse_assignments
*)
489 // Implemented by the child class: lower this statement to a simpler
492 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
495 // Implemented by the child class: lower this statement to a simpler
498 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
501 // Implemented by child class: set type information for unnamed
502 // constants. Any statement which includes an expression needs to
508 // Implemented by child class: check types of expressions used in a
511 do_check_types(Gogo
*)
514 // Implemented by child class: return the cost of this statement for
515 // inlining. The default cost is high, so we only need to define
516 // this method for statements that can be inlined.
521 // Implemented by child class: write export data for this statement
522 // to the string. This need only be implemented by classes that
523 // implement do_inlining_cost with a reasonable value.
525 do_export_statement(Export_function_body
*)
526 { go_unreachable(); }
528 // Implemented by child class: return true if this statement may
531 do_may_fall_through() const
534 // Implemented by child class: convert to backend representation.
536 do_get_backend(Translate_context
*) = 0;
538 // Implemented by child class: dump ast representation.
540 do_dump_statement(Ast_dump_context
*) const = 0;
542 // Implemented by child class: make implicit conversions explicit.
547 // Traverse an expression in a statement.
549 traverse_expression(Traverse
*, Expression
**);
551 // Traverse an expression list in a statement. The Expression_list
554 traverse_expression_list(Traverse
*, Expression_list
*);
556 // Traverse a type in a statement.
558 traverse_type(Traverse
*, Type
*);
560 // For children to call when they detect that they are in error.
564 // For children to call to report an error conveniently.
566 report_error(const char*);
568 // For children to return an error statement from lower().
570 make_error_statement(Location
);
573 // Convert to the desired statement classification, or return NULL.
574 // This is a controlled dynamic cast.
575 template<typename Statement_class
, Statement_classification sc
>
579 return (this->classification_
== sc
580 ? static_cast<Statement_class
*>(this)
584 template<typename Statement_class
, Statement_classification sc
>
585 const Statement_class
*
588 return (this->classification_
== sc
589 ? static_cast<const Statement_class
*>(this)
593 // The statement classification.
594 Statement_classification classification_
;
595 // The location in the input file of the start of this statement.
599 // An assignment statement.
601 class Assignment_statement
: public Statement
604 Assignment_statement(Expression
* lhs
, Expression
* rhs
,
606 : Statement(STATEMENT_ASSIGNMENT
, location
),
607 lhs_(lhs
), rhs_(rhs
), omit_write_barrier_(false)
612 { return this->lhs_
; }
616 { return this->rhs_
; }
619 omit_write_barrier() const
620 { return this->omit_write_barrier_
; }
623 set_omit_write_barrier()
624 { this->omit_write_barrier_
= true; }
628 do_traverse(Traverse
* traverse
);
631 do_traverse_assignments(Traverse_assignments
*);
634 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
637 do_determine_types();
640 do_check_types(Gogo
*);
647 do_export_statement(Export_function_body
*);
650 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
653 do_get_backend(Translate_context
*);
656 do_dump_statement(Ast_dump_context
*) const;
659 do_add_conversions();
662 // Left hand side--the lvalue.
664 // Right hand side--the rvalue.
666 // True if we can omit a write barrier from this assignment.
667 bool omit_write_barrier_
;
670 // A statement which creates and initializes a temporary variable.
672 class Temporary_statement
: public Statement
675 Temporary_statement(Type
* type
, Expression
* init
, Location location
)
676 : Statement(STATEMENT_TEMPORARY
, location
),
677 type_(type
), init_(init
), bvariable_(NULL
), is_address_taken_(false),
678 value_escapes_(false)
681 // Return the type of the temporary variable.
685 // Return the initializer if there is one.
688 { return this->init_
; }
690 // Record that something takes the address of this temporary
693 set_is_address_taken()
694 { this->is_address_taken_
= true; }
696 // Whether the value escapes.
698 value_escapes() const
699 { return this->value_escapes_
; }
701 // Record that the value escapes.
704 { this->value_escapes_
= true; }
706 // Return the temporary variable. This should not be called until
707 // after the statement itself has been converted.
709 get_backend_variable(Translate_context
*) const;
713 do_traverse(Traverse
*);
716 do_traverse_assignments(Traverse_assignments
*);
719 do_determine_types();
722 do_check_types(Gogo
*);
725 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
728 do_get_backend(Translate_context
*);
731 do_dump_statement(Ast_dump_context
*) const;
734 do_add_conversions();
737 // The type of the temporary variable.
739 // The initial value of the temporary variable. This may be NULL.
741 // The backend representation of the temporary variable.
742 Bvariable
* bvariable_
;
743 // True if something takes the address of this temporary variable.
744 bool is_address_taken_
;
745 // True if the value assigned to this temporary variable escapes.
746 // This is used for select statements.
750 // A variable declaration. This marks the point in the code where a
751 // variable is declared. The Variable is also attached to a Block.
753 class Variable_declaration_statement
: public Statement
756 Variable_declaration_statement(Named_object
* var
);
758 // The variable being declared.
761 { return this->var_
; }
763 // Import a variable declaration.
765 do_import(Import_function_body
*, Location
);
769 do_traverse(Traverse
*);
772 do_traverse_assignments(Traverse_assignments
*);
775 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
782 do_export_statement(Export_function_body
*);
785 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
788 do_get_backend(Translate_context
*);
791 do_dump_statement(Ast_dump_context
*) const;
794 do_add_conversions();
800 // A return statement.
802 class Return_statement
: public Statement
805 Return_statement(Expression_list
* vals
, Location location
)
806 : Statement(STATEMENT_RETURN
, location
),
807 vals_(vals
), is_lowered_(false)
810 // The list of values being returned. This may be NULL.
811 const Expression_list
*
813 { return this->vals_
; }
817 do_traverse(Traverse
* traverse
)
818 { return this->traverse_expression_list(traverse
, this->vals_
); }
821 do_traverse_assignments(Traverse_assignments
*);
824 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
827 do_may_fall_through() const
835 do_export_statement(Export_function_body
*);
838 do_get_backend(Translate_context
*);
841 do_dump_statement(Ast_dump_context
*) const;
844 // Return values. This may be NULL.
845 Expression_list
* vals_
;
846 // True if this statement has been lowered.
850 // An expression statement.
852 class Expression_statement
: public Statement
855 Expression_statement(Expression
* expr
, bool is_ignored
);
859 { return this->expr_
; }
863 do_traverse(Traverse
* traverse
)
864 { return this->traverse_expression(traverse
, &this->expr_
); }
867 do_determine_types();
870 do_check_types(Gogo
*);
873 do_may_fall_through() const;
876 do_get_backend(Translate_context
* context
);
879 do_dump_statement(Ast_dump_context
*) const;
883 // Whether the value of this expression is being explicitly ignored.
887 // A block statement--a list of statements which may include variable
890 class Block_statement
: public Statement
893 Block_statement(Block
* block
, Location location
)
894 : Statement(STATEMENT_BLOCK
, location
),
895 block_(block
), is_lowered_for_statement_(false)
898 // Return the actual block.
901 { return this->block_
; }
904 set_is_lowered_for_statement()
905 { this->is_lowered_for_statement_
= true; }
908 is_lowered_for_statement()
909 { return this->is_lowered_for_statement_
; }
913 do_traverse(Traverse
* traverse
)
914 { return this->block_
->traverse(traverse
); }
918 { this->block_
->determine_types(); }
925 do_export_statement(Export_function_body
*);
928 do_may_fall_through() const
929 { return this->block_
->may_fall_through(); }
932 do_get_backend(Translate_context
* context
);
935 do_dump_statement(Ast_dump_context
*) const;
939 // True if this block statement represents a lowered for statement.
940 bool is_lowered_for_statement_
;
945 class Send_statement
: public Statement
948 Send_statement(Expression
* channel
, Expression
* val
,
950 : Statement(STATEMENT_SEND
, location
),
951 channel_(channel
), val_(val
)
956 { return this->channel_
; }
960 { return this->val_
; }
964 do_traverse(Traverse
* traverse
);
967 do_determine_types();
970 do_check_types(Gogo
*);
973 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
976 do_get_backend(Translate_context
*);
979 do_dump_statement(Ast_dump_context
*) const;
982 do_add_conversions();
985 // The channel on which to send the value.
986 Expression
* channel_
;
987 // The value to send.
991 // Select_clauses holds the clauses of a select statement. This is
992 // built by the parser.
1001 // Add a new clause. IS_SEND is true if this is a send clause,
1002 // false for a receive clause. For a send clause CHANNEL is the
1003 // channel and VAL is the value to send. For a receive clause
1004 // CHANNEL is the channel, VAL is either NULL or a Var_expression
1005 // for the variable to set, and CLOSED is either NULL or a
1006 // Var_expression to set to whether the channel is closed. If VAL
1007 // is NULL, VAR may be a variable to be initialized with the
1008 // received value, and CLOSEDVAR ma be a variable to be initialized
1009 // with whether the channel is closed. IS_DEFAULT is true if this
1010 // is the default clause. STATEMENTS is the list of statements to
1013 add(bool is_send
, Expression
* channel
, Expression
* val
, Expression
* closed
,
1014 Named_object
* var
, Named_object
* closedvar
, bool is_default
,
1015 Block
* statements
, Location location
)
1017 this->clauses_
.push_back(Select_clause(is_send
, channel
, val
, closed
, var
,
1018 closedvar
, is_default
, statements
,
1024 { return this->clauses_
.size(); }
1026 // Traverse the select clauses.
1028 traverse(Traverse
*);
1030 // Lower statements.
1032 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*,
1033 Temporary_statement
*);
1043 // Whether the select clauses may fall through to the statement
1044 // which follows the overall select statement.
1046 may_fall_through() const;
1048 // Convert to the backend representation.
1050 get_backend(Translate_context
*, Temporary_statement
* index
,
1051 Unnamed_label
* break_label
, Location
);
1053 // Dump AST representation.
1055 dump_clauses(Ast_dump_context
*) const;
1063 : channel_(NULL
), val_(NULL
), closed_(NULL
), var_(NULL
),
1064 closedvar_(NULL
), statements_(NULL
), is_send_(false),
1068 Select_clause(bool is_send
, Expression
* channel
, Expression
* val
,
1069 Expression
* closed
, Named_object
* var
,
1070 Named_object
* closedvar
, bool is_default
, Block
* statements
,
1072 : channel_(channel
), val_(val
), closed_(closed
), var_(var
),
1073 closedvar_(closedvar
), statements_(statements
), location_(location
),
1074 is_send_(is_send
), is_default_(is_default
), is_lowered_(false)
1075 { go_assert(is_default
? channel
== NULL
: channel
!= NULL
); }
1077 // Traverse the select clause.
1079 traverse(Traverse
*);
1081 // Lower statements.
1083 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*, size_t,
1084 Temporary_statement
*);
1094 // Return true if this is the default clause.
1097 { return this->is_default_
; }
1099 // Return the channel. This will return NULL for the default
1103 { return this->channel_
; }
1105 // Return true for a send, false for a receive.
1109 go_assert(!this->is_default_
);
1110 return this->is_send_
;
1113 // Return the statements.
1116 { return this->statements_
; }
1118 // Return the location.
1121 { return this->location_
; }
1123 // Whether this clause may fall through to the statement which
1124 // follows the overall select statement.
1126 may_fall_through() const;
1128 // Convert the statements to the backend representation.
1130 get_statements_backend(Translate_context
*);
1132 // Dump AST representation.
1134 dump_clause(Ast_dump_context
*) const;
1137 // These values must match the values in libgo/go/runtime/select.go.
1146 lower_default(Block
*, Expression
*);
1149 lower_send(Block
*, Expression
*, Expression
*);
1152 lower_recv(Gogo
*, Named_object
*, Block
*, Expression
*, Expression
*,
1153 Temporary_statement
*);
1156 set_case(Block
*, Expression
*, Expression
*, Expression
*, int);
1159 Expression
* channel_
;
1160 // The value to send or the lvalue to receive into.
1162 // The lvalue to set to whether the channel is closed on a
1164 Expression
* closed_
;
1165 // The variable to initialize, for "case a := <-ch".
1167 // The variable to initialize to whether the channel is closed,
1168 // for "case a, c := <-ch".
1169 Named_object
* closedvar_
;
1170 // The statements to execute.
1172 // The location of this clause.
1174 // Whether this is a send or a receive.
1176 // Whether this is the default.
1178 // Whether this has been lowered.
1182 typedef std::vector
<Select_clause
> Clauses
;
1187 // A select statement.
1189 class Select_statement
: public Statement
1192 Select_statement(Location location
)
1193 : Statement(STATEMENT_SELECT
, location
),
1194 clauses_(NULL
), index_(NULL
), break_label_(NULL
), is_lowered_(false)
1199 add_clauses(Select_clauses
* clauses
)
1201 go_assert(this->clauses_
== NULL
);
1202 this->clauses_
= clauses
;
1205 // Return the break label for this select statement.
1211 do_traverse(Traverse
* traverse
)
1212 { return this->clauses_
->traverse(traverse
); }
1215 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1218 do_determine_types()
1219 { this->clauses_
->determine_types(); }
1222 do_check_types(Gogo
*)
1223 { this->clauses_
->check_types(); }
1226 do_may_fall_through() const;
1229 do_get_backend(Translate_context
*);
1232 do_dump_statement(Ast_dump_context
*) const;
1235 // The select clauses.
1236 Select_clauses
* clauses_
;
1237 // A temporary that holds the index value returned by selectgo.
1238 Temporary_statement
* index_
;
1240 Unnamed_label
* break_label_
;
1241 // Whether this statement has been lowered.
1245 // A statement which requires a thunk: go or defer.
1247 class Thunk_statement
: public Statement
1250 Thunk_statement(Statement_classification
, Call_expression
*,
1253 // Return the call expression.
1256 { return this->call_
; }
1258 // Simplify a go or defer statement so that it only uses a single
1261 simplify_statement(Gogo
*, Named_object
*, Block
*);
1265 do_traverse(Traverse
* traverse
);
1268 do_traverse_assignments(Traverse_assignments
*);
1271 do_determine_types();
1274 do_check_types(Gogo
*);
1276 // Return the function and argument for the call.
1278 get_fn_and_arg(Expression
** pfn
, Expression
** parg
);
1281 // Return whether this is a simple go statement.
1283 is_simple(Function_type
*) const;
1285 // Return whether the thunk function is a constant.
1287 is_constant_function() const;
1289 // Build the struct to use for a complex case.
1291 build_struct(Function_type
* fntype
);
1295 build_thunk(Gogo
*, const std::string
&);
1297 // Set the name to use for thunk field N.
1299 thunk_field_param(int n
, char* buf
, size_t buflen
);
1301 // The function call to be executed in a separate thread (go) or
1304 // The type used for a struct to pass to a thunk, if this is not a
1306 Struct_type
* struct_type_
;
1311 class Go_statement
: public Thunk_statement
1314 Go_statement(Call_expression
* call
, Location location
)
1315 : Thunk_statement(STATEMENT_GO
, call
, location
)
1320 do_get_backend(Translate_context
*);
1323 do_dump_statement(Ast_dump_context
*) const;
1326 // A defer statement.
1328 class Defer_statement
: public Thunk_statement
1331 Defer_statement(Call_expression
* call
, Location location
)
1332 : Thunk_statement(STATEMENT_DEFER
, call
, location
)
1337 do_get_backend(Translate_context
*);
1340 do_dump_statement(Ast_dump_context
*) const;
1343 // A goto statement.
1345 class Goto_statement
: public Statement
1348 Goto_statement(Label
* label
, Location location
)
1349 : Statement(STATEMENT_GOTO
, location
),
1353 // Return the label being jumped to.
1356 { return this->label_
; }
1360 do_traverse(Traverse
*);
1363 do_check_types(Gogo
*);
1366 do_may_fall_through() const
1370 do_get_backend(Translate_context
*);
1373 do_dump_statement(Ast_dump_context
*) const;
1379 // A goto statement to an unnamed label.
1381 class Goto_unnamed_statement
: public Statement
1384 Goto_unnamed_statement(Unnamed_label
* label
, Location location
)
1385 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
1390 unnamed_label() const
1391 { return this->label_
; }
1395 do_traverse(Traverse
*);
1398 do_may_fall_through() const
1402 do_get_backend(Translate_context
* context
);
1405 do_dump_statement(Ast_dump_context
*) const;
1408 Unnamed_label
* label_
;
1411 // A label statement.
1413 class Label_statement
: public Statement
1416 Label_statement(Label
* label
, Location location
)
1417 : Statement(STATEMENT_LABEL
, location
),
1421 // Return the label itself.
1424 { return this->label_
; }
1428 do_traverse(Traverse
*);
1431 do_get_backend(Translate_context
*);
1434 do_dump_statement(Ast_dump_context
*) const;
1441 // An unnamed label statement.
1443 class Unnamed_label_statement
: public Statement
1446 Unnamed_label_statement(Unnamed_label
* label
);
1450 do_traverse(Traverse
*);
1453 do_get_backend(Translate_context
* context
);
1456 do_dump_statement(Ast_dump_context
*) const;
1460 Unnamed_label
* label_
;
1465 class If_statement
: public Statement
1468 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
1470 : Statement(STATEMENT_IF
, location
),
1471 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
1476 { return this->cond_
; }
1480 do_traverse(Traverse
*);
1483 do_determine_types();
1486 do_check_types(Gogo
*);
1489 do_may_fall_through() const;
1492 do_get_backend(Translate_context
*);
1495 do_dump_statement(Ast_dump_context
*) const;
1505 class For_statement
: public Statement
1508 For_statement(Block
* init
, Expression
* cond
, Block
* post
,
1510 : Statement(STATEMENT_FOR
, location
),
1511 init_(init
), cond_(cond
), post_(post
), statements_(NULL
),
1512 break_label_(NULL
), continue_label_(NULL
)
1515 // Add the statements.
1517 add_statements(Block
* statements
)
1519 go_assert(this->statements_
== NULL
);
1520 this->statements_
= statements
;
1523 // Return the break label for this for statement.
1527 // Return the continue label for this for statement.
1531 // Set the break and continue labels for this statement.
1533 set_break_continue_labels(Unnamed_label
* break_label
,
1534 Unnamed_label
* continue_label
);
1538 do_traverse(Traverse
*);
1541 do_traverse_assignments(Traverse_assignments
*)
1542 { go_unreachable(); }
1545 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1548 do_may_fall_through() const;
1551 do_get_backend(Translate_context
*)
1552 { go_unreachable(); }
1555 do_dump_statement(Ast_dump_context
*) const;
1558 // The initialization statements. This may be NULL.
1560 // The condition. This may be NULL.
1562 // The statements to run after each iteration. This may be NULL.
1564 // The statements in the loop itself.
1566 // The break label, if needed.
1567 Unnamed_label
* break_label_
;
1568 // The continue label, if needed.
1569 Unnamed_label
* continue_label_
;
1572 // A for statement over a range clause.
1574 class For_range_statement
: public Statement
1577 For_range_statement(Expression
* index_var
, Expression
* value_var
,
1578 Expression
* range
, Location location
)
1579 : Statement(STATEMENT_FOR_RANGE
, location
),
1580 index_var_(index_var
), value_var_(value_var
), range_(range
),
1581 statements_(NULL
), break_label_(NULL
), continue_label_(NULL
)
1584 // Add the statements.
1586 add_statements(Block
* statements
)
1588 go_assert(this->statements_
== NULL
);
1589 this->statements_
= statements
;
1592 // Return the break label for this for statement.
1596 // Return the continue label for this for statement.
1602 do_traverse(Traverse
*);
1605 do_traverse_assignments(Traverse_assignments
*)
1606 { go_unreachable(); }
1609 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1612 do_get_backend(Translate_context
*)
1613 { go_unreachable(); }
1616 do_dump_statement(Ast_dump_context
*) const;
1620 make_range_ref(Named_object
*, Temporary_statement
*, Location
);
1623 call_builtin(Gogo
*, const char* funcname
, Expression
* arg
, Location
);
1626 lower_range_array(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1627 Temporary_statement
*, Temporary_statement
*,
1628 Block
**, Expression
**, Block
**, Block
**);
1631 lower_range_slice(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1632 Temporary_statement
*, Temporary_statement
*,
1633 Block
**, Expression
**, Block
**, Block
**);
1636 lower_range_string(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1637 Temporary_statement
*, Temporary_statement
*,
1638 Block
**, Expression
**, Block
**, Block
**);
1641 lower_range_map(Gogo
*, Map_type
*, Block
*, Block
*, Named_object
*,
1642 Temporary_statement
*, Temporary_statement
*,
1643 Temporary_statement
*, Block
**, Expression
**, Block
**,
1647 lower_range_channel(Gogo
*, Block
*, Block
*, Named_object
*,
1648 Temporary_statement
*, Temporary_statement
*,
1649 Temporary_statement
*, Block
**, Expression
**, Block
**,
1653 lower_map_range_clear(Type
*, Block
*, Expression
*, Named_object
*,
1654 Temporary_statement
*, Location
);
1657 lower_array_range_clear(Gogo
*, Type
*, Expression
*, Block
*,
1658 Named_object
*, Temporary_statement
*,
1661 // The variable which is set to the index value.
1662 Expression
* index_var_
;
1663 // The variable which is set to the element value. This may be
1665 Expression
* value_var_
;
1666 // The expression we are ranging over.
1668 // The statements in the block.
1670 // The break label, if needed.
1671 Unnamed_label
* break_label_
;
1672 // The continue label, if needed.
1673 Unnamed_label
* continue_label_
;
1676 // Class Case_clauses holds the clauses of a switch statement. This
1677 // is built by the parser.
1686 // Add a new clause. CASES is a list of case expressions; it may be
1687 // NULL. IS_DEFAULT is true if this is the default case.
1688 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1689 // after the statements the case clause should fall through to the
1692 add(Expression_list
* cases
, bool is_default
, Block
* statements
,
1693 bool is_fallthrough
, Location location
)
1695 this->clauses_
.push_back(Case_clause(cases
, is_default
, statements
,
1696 is_fallthrough
, location
));
1699 // Return whether there are no clauses.
1702 { return this->clauses_
.empty(); }
1704 // Traverse the case clauses.
1706 traverse(Traverse
*);
1708 // Lower for a nonconstant switch.
1710 lower(Block
*, Temporary_statement
*, Unnamed_label
*) const;
1712 // Determine types of expressions. The Type parameter is the type
1713 // of the switch value.
1715 determine_types(Type
*);
1717 // Check types. The Type parameter is the type of the switch value.
1721 // Return true if all the clauses are constant values.
1723 is_constant() const;
1725 // Return true if these clauses may fall through to the statements
1726 // following the switch statement.
1728 may_fall_through() const;
1730 // Return the body of a SWITCH_EXPR when all the clauses are
1733 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1734 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
1735 std::vector
<Bstatement
*>* all_statements
) const;
1737 // Dump the AST representation to a dump context.
1739 dump_clauses(Ast_dump_context
*) const;
1742 // For a constant switch we need to keep a record of constants we
1743 // have already seen.
1744 class Hash_integer_value
;
1745 class Eq_integer_value
;
1746 typedef Unordered_set_hash(Expression
*, Hash_integer_value
,
1747 Eq_integer_value
) Case_constants
;
1754 : cases_(NULL
), statements_(NULL
), is_default_(false),
1755 is_fallthrough_(false), location_(Linemap::unknown_location())
1758 Case_clause(Expression_list
* cases
, bool is_default
, Block
* statements
,
1759 bool is_fallthrough
, Location location
)
1760 : cases_(cases
), statements_(statements
), is_default_(is_default
),
1761 is_fallthrough_(is_fallthrough
), location_(location
)
1764 // Whether this clause falls through to the next clause.
1766 is_fallthrough() const
1767 { return this->is_fallthrough_
; }
1769 // Whether this is the default.
1772 { return this->is_default_
; }
1774 // The location of this clause.
1777 { return this->location_
; }
1781 traverse(Traverse
*);
1783 // Lower for a nonconstant switch.
1785 lower(Block
*, Temporary_statement
*, Unnamed_label
*, Unnamed_label
*) const;
1789 determine_types(Type
*);
1795 // Return true if all the case expressions are constant.
1797 is_constant() const;
1799 // Return true if this clause may fall through to execute the
1800 // statements following the switch statement. This is not the
1801 // same as whether this clause falls through to the next clause.
1803 may_fall_through() const;
1805 // Convert the case values and statements to the backend
1808 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1809 Case_constants
*, std::vector
<Bexpression
*>* cases
) const;
1811 // Dump the AST representation to a dump context.
1813 dump_clause(Ast_dump_context
*) const;
1816 // The list of case expressions.
1817 Expression_list
* cases_
;
1818 // The statements to execute.
1820 // Whether this is the default case.
1822 // Whether this falls through after the statements.
1823 bool is_fallthrough_
;
1824 // The location of this case clause.
1828 friend class Case_clause
;
1830 // The type of the list of clauses.
1831 typedef std::vector
<Case_clause
> Clauses
;
1833 // All the case clauses.
1837 // A switch statement.
1839 class Switch_statement
: public Statement
1842 Switch_statement(Expression
* val
, Location location
)
1843 : Statement(STATEMENT_SWITCH
, location
),
1844 val_(val
), clauses_(NULL
), break_label_(NULL
)
1849 add_clauses(Case_clauses
* clauses
)
1851 go_assert(this->clauses_
== NULL
);
1852 this->clauses_
= clauses
;
1855 // Return the break label for this switch statement.
1861 do_traverse(Traverse
*);
1864 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1867 do_get_backend(Translate_context
*)
1868 { go_unreachable(); }
1871 do_dump_statement(Ast_dump_context
*) const;
1874 do_may_fall_through() const;
1877 // The value to switch on. This may be NULL.
1879 // The case clauses.
1880 Case_clauses
* clauses_
;
1881 // The break label, if needed.
1882 Unnamed_label
* break_label_
;
1885 // Class Type_case_clauses holds the clauses of a type switch
1886 // statement. This is built by the parser.
1888 class Type_case_clauses
1895 // Add a new clause. TYPE is the type for this clause; it may be
1896 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1897 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1898 // if this is the default case. STATEMENTS is a block of
1899 // statements; it may be NULL.
1901 add(Type
* type
, bool is_fallthrough
, bool is_default
, Block
* statements
,
1904 this->clauses_
.push_back(Type_case_clause(type
, is_fallthrough
, is_default
,
1905 statements
, location
));
1908 // Return whether there are no clauses.
1911 { return this->clauses_
.empty(); }
1913 // Traverse the type case clauses.
1915 traverse(Traverse
*);
1917 // Check for duplicates.
1919 check_duplicates() const;
1921 // Lower to if and goto statements.
1923 lower(Type
*, Block
*, Temporary_statement
* descriptor_temp
,
1924 Unnamed_label
* break_label
) const;
1926 // Return true if these clauses may fall through to the statements
1927 // following the switch statement.
1929 may_fall_through() const;
1931 // Dump the AST representation to a dump context.
1933 dump_clauses(Ast_dump_context
*) const;
1936 // One type case clause.
1937 class Type_case_clause
1941 : type_(NULL
), statements_(NULL
), is_default_(false),
1942 location_(Linemap::unknown_location())
1945 Type_case_clause(Type
* type
, bool is_fallthrough
, bool is_default
,
1946 Block
* statements
, Location location
)
1947 : type_(type
), statements_(statements
), is_fallthrough_(is_fallthrough
),
1948 is_default_(is_default
), location_(location
)
1954 { return this->type_
; }
1956 // Whether this is the default.
1959 { return this->is_default_
; }
1961 // The location of this type clause.
1964 { return this->location_
; }
1968 traverse(Traverse
*);
1970 // Lower to if and goto statements.
1972 lower(Type
*, Block
*, Temporary_statement
* descriptor_temp
,
1973 Unnamed_label
* break_label
, Unnamed_label
** stmts_label
) const;
1975 // Return true if this clause may fall through to execute the
1976 // statements following the switch statement. This is not the
1977 // same as whether this clause falls through to the next clause.
1979 may_fall_through() const;
1981 // Dump the AST representation to a dump context.
1983 dump_clause(Ast_dump_context
*) const;
1986 // The type for this type clause.
1988 // The statements to execute.
1990 // Whether this falls through--this is true for "case T1, T2".
1991 bool is_fallthrough_
;
1992 // Whether this is the default case.
1994 // The location of this type case clause.
1998 friend class Type_case_clause
;
2000 // The type of the list of type clauses.
2001 typedef std::vector
<Type_case_clause
> Type_clauses
;
2003 // All the type case clauses.
2004 Type_clauses clauses_
;
2007 // A type switch statement.
2009 class Type_switch_statement
: public Statement
2012 Type_switch_statement(const std::string
& name
, Expression
* expr
,
2014 : Statement(STATEMENT_TYPE_SWITCH
, location
),
2015 name_(name
), expr_(expr
), clauses_(NULL
), break_label_(NULL
)
2020 add_clauses(Type_case_clauses
* clauses
)
2022 go_assert(this->clauses_
== NULL
);
2023 this->clauses_
= clauses
;
2026 // Return the break label for this type switch statement.
2032 do_traverse(Traverse
*);
2035 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
2038 do_get_backend(Translate_context
*)
2039 { go_unreachable(); }
2042 do_dump_statement(Ast_dump_context
*) const;
2045 do_may_fall_through() const;
2048 // The name of the variable declared in the type switch guard. Empty if there
2049 // is no variable declared.
2051 // The expression we are switching on if there is no variable.
2053 // The type case clauses.
2054 Type_case_clauses
* clauses_
;
2055 // The break label, if needed.
2056 Unnamed_label
* break_label_
;
2059 #endif // !defined(GO_STATEMENTS_H)