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 // This class is used to traverse assignments made by a statement
59 // which makes assignments.
61 class Traverse_assignments
64 Traverse_assignments()
67 virtual ~Traverse_assignments()
70 // This is called for a variable initialization.
72 initialize_variable(Named_object
*) = 0;
74 // This is called for each assignment made by the statement. PLHS
75 // points to the left hand side, and PRHS points to the right hand
76 // side. PRHS may be NULL if there is no associated expression, as
77 // in the bool set by a non-blocking receive.
79 assignment(Expression
** plhs
, Expression
** prhs
) = 0;
81 // This is called for each expression which is not passed to the
82 // assignment function. This is used for some of the statements
83 // which assign two values, for which there is no expression which
84 // describes the value. For ++ and -- the value is passed to both
85 // the assignment method and the rhs method. IS_STORED is true if
86 // this value is being stored directly. It is false if the value is
87 // computed but not stored. IS_LOCAL is true if the value is being
88 // stored in a local variable or this is being called by a return
91 value(Expression
**, bool is_stored
, bool is_local
) = 0;
94 // A single statement.
99 // The types of statements.
100 enum Statement_classification
103 STATEMENT_VARIABLE_DECLARATION
,
105 STATEMENT_ASSIGNMENT
,
106 STATEMENT_EXPRESSION
,
111 STATEMENT_BREAK_OR_CONTINUE
,
113 STATEMENT_GOTO_UNNAMED
,
115 STATEMENT_UNNAMED_LABEL
,
117 STATEMENT_CONSTANT_SWITCH
,
121 // These statements types are created by the parser, but they
122 // disappear during the lowering pass.
123 STATEMENT_ASSIGNMENT_OPERATION
,
124 STATEMENT_TUPLE_ASSIGNMENT
,
125 STATEMENT_TUPLE_MAP_ASSIGNMENT
,
126 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
,
127 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
,
132 STATEMENT_TYPE_SWITCH
135 Statement(Statement_classification
, Location
);
137 virtual ~Statement();
139 // Make a variable declaration.
141 make_variable_declaration(Named_object
*);
143 // Make a statement which creates a temporary variable and
144 // initializes it to an expression. The block is used if the
145 // temporary variable has to be explicitly destroyed; the variable
146 // must still be added to the block. References to the temporary
147 // variable may be constructed using make_temporary_reference.
148 // Either the type or the initialization expression may be NULL, but
150 static Temporary_statement
*
151 make_temporary(Type
*, Expression
*, Location
);
153 // Make an assignment statement.
154 static Assignment_statement
*
155 make_assignment(Expression
*, Expression
*, Location
);
157 // Make an assignment operation (+=, etc.).
159 make_assignment_operation(Operator
, Expression
*, Expression
*,
162 // Make a tuple assignment statement.
164 make_tuple_assignment(Expression_list
*, Expression_list
*, Location
);
166 // Make an assignment from a map index to a pair of variables.
168 make_tuple_map_assignment(Expression
* val
, Expression
* present
,
169 Expression
*, Location
);
171 // Make an assignment from a nonblocking receive to a pair of
174 make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
175 Expression
* channel
, Location
);
177 // Make an assignment from a type guard to a pair of variables.
179 make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
180 Expression
* expr
, Type
* type
,
183 // Make an expression statement from an Expression. IS_IGNORED is
184 // true if the value is being explicitly ignored, as in an
187 make_statement(Expression
*, bool is_ignored
);
189 // Make a block statement from a Block. This is an embedded list of
190 // statements which may also include variable definitions.
191 static Block_statement
*
192 make_block_statement(Block
*, Location
);
194 // Make an increment statement.
196 make_inc_statement(Expression
*);
198 // Make a decrement statement.
200 make_dec_statement(Expression
*);
202 // Make a go statement.
204 make_go_statement(Call_expression
* call
, Location
);
206 // Make a defer statement.
208 make_defer_statement(Call_expression
* call
, Location
);
210 // Make a return statement.
211 static Return_statement
*
212 make_return_statement(Expression_list
*, Location
);
214 // Make a statement that returns the result of a call expression.
215 // If the call does not return any results, this just returns the
216 // call expression as a statement, assuming that the function will
217 // end immediately afterward.
219 make_return_from_call(Call_expression
*, Location
);
221 // Make a break statement.
223 make_break_statement(Unnamed_label
* label
, Location
);
225 // Make a continue statement.
227 make_continue_statement(Unnamed_label
* label
, Location
);
229 // Make a goto statement.
231 make_goto_statement(Label
* label
, Location
);
233 // Make a goto statement to an unnamed label.
235 make_goto_unnamed_statement(Unnamed_label
* label
, Location
);
237 // Make a label statement--where the label is defined.
239 make_label_statement(Label
* label
, Location
);
241 // Make an unnamed label statement--where the label is defined.
243 make_unnamed_label_statement(Unnamed_label
* label
);
245 // Make an if statement.
247 make_if_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
250 // Make a switch statement.
251 static Switch_statement
*
252 make_switch_statement(Expression
* switch_val
, Location
);
254 // Make a type switch statement.
255 static Type_switch_statement
*
256 make_type_switch_statement(const std::string
&, Expression
*, Location
);
258 // Make a send statement.
259 static Send_statement
*
260 make_send_statement(Expression
* channel
, Expression
* val
, Location
);
262 // Make a select statement.
263 static Select_statement
*
264 make_select_statement(Location
);
266 // Make a for statement.
267 static For_statement
*
268 make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
271 // Make a for statement with a range clause.
272 static For_range_statement
*
273 make_for_range_statement(Expression
* index_var
, Expression
* value_var
,
274 Expression
* range
, Location
);
276 // Return the statement classification.
277 Statement_classification
278 classification() const
279 { return this->classification_
; }
281 // Get the statement location.
284 { return this->location_
; }
286 // Traverse the tree.
288 traverse(Block
*, size_t* index
, Traverse
*);
290 // Traverse the contents of this statement--the expressions and
291 // statements which it contains.
293 traverse_contents(Traverse
*);
295 // If this statement assigns some values, it calls a function for
296 // each value to which this statement assigns a value, and returns
297 // true. If this statement does not assign any values, it returns
300 traverse_assignments(Traverse_assignments
* tassign
);
302 // Lower a statement. This is called immediately after parsing to
303 // simplify statements for further processing. It returns the same
304 // Statement or a new one. FUNCTION is the function containing this
305 // statement. BLOCK is the block containing this statement.
306 // INSERTER can be used to insert new statements before this one.
308 lower(Gogo
* gogo
, Named_object
* function
, Block
* block
,
309 Statement_inserter
* inserter
)
310 { return this->do_lower(gogo
, function
, block
, inserter
); }
312 // Flatten a statement. This is called immediately after the order of
313 // evaluation rules are applied to statements. It returns the same
314 // Statement or a new one. FUNCTION is the function containing this
315 // statement. BLOCK is the block containing this statement.
316 // INSERTER can be used to insert new statements before this one.
318 flatten(Gogo
* gogo
, Named_object
* function
, Block
* block
,
319 Statement_inserter
* inserter
)
320 { return this->do_flatten(gogo
, function
, block
, inserter
); }
322 // Set type information for unnamed constants.
326 // Check types in a statement. This simply checks that any
327 // expressions used by the statement have the right type.
329 check_types(Gogo
* gogo
)
330 { this->do_check_types(gogo
); }
332 // Return the cost of this statement for inlining purposes.
335 { return this->do_inlining_cost(); }
337 // Export data for this statement to BODY.
339 export_statement(Export_function_body
* efb
)
340 { this->do_export_statement(efb
); }
342 // Make implicit type conversions explicit.
345 { this->do_add_conversions(); }
347 // Read a statement from export data. The location should be used
348 // for the returned statement. Errors should be reported using the
349 // Import_function_body's location method.
351 import_statement(Import_function_body
*, Location
);
353 // Return whether this is a block statement.
355 is_block_statement() const
356 { return this->classification_
== STATEMENT_BLOCK
; }
358 // If this is an assignment statement, return it. Otherwise return
360 Assignment_statement
*
361 assignment_statement()
363 return this->convert
<Assignment_statement
, STATEMENT_ASSIGNMENT
>();
366 // If this is an temporary statement, return it. Otherwise return
369 temporary_statement()
371 return this->convert
<Temporary_statement
, STATEMENT_TEMPORARY
>();
374 // If this is a variable declaration statement, return it.
375 // Otherwise return NULL.
376 Variable_declaration_statement
*
377 variable_declaration_statement()
379 return this->convert
<Variable_declaration_statement
,
380 STATEMENT_VARIABLE_DECLARATION
>();
383 // If this is an expression statement, return it. Otherwise return
385 Expression_statement
*
386 expression_statement()
388 return this->convert
<Expression_statement
, STATEMENT_EXPRESSION
>();
391 // If this is an block statement, return it. Otherwise return
395 { return this->convert
<Block_statement
, STATEMENT_BLOCK
>(); }
397 // If this is a return statement, return it. Otherwise return NULL.
400 { return this->convert
<Return_statement
, STATEMENT_RETURN
>(); }
402 // If this is a thunk statement (a go or defer statement), return
403 // it. Otherwise return NULL.
407 // If this is a defer statement, return it. Otherwise return NULL.
410 { return this->convert
<Defer_statement
, STATEMENT_DEFER
>(); }
412 // If this is a goto statement, return it. Otherwise return NULL.
415 { return this->convert
<Goto_statement
, STATEMENT_GOTO
>(); }
417 // If this is a goto_unnamed statement, return it. Otherwise return NULL.
418 Goto_unnamed_statement
*
419 goto_unnamed_statement()
420 { return this->convert
<Goto_unnamed_statement
, STATEMENT_GOTO_UNNAMED
>(); }
422 // If this is a label statement, return it. Otherwise return NULL.
425 { return this->convert
<Label_statement
, STATEMENT_LABEL
>(); }
427 // If this is an unnamed_label statement, return it. Otherwise return NULL.
428 Unnamed_label_statement
*
429 unnamed_label_statement()
430 { return this->convert
<Unnamed_label_statement
, STATEMENT_UNNAMED_LABEL
>(); }
432 // If this is an if statement, return it. Otherwise return NULL.
435 { return this->convert
<If_statement
, STATEMENT_IF
>(); }
437 // If this is a for statement, return it. Otherwise return NULL.
440 { return this->convert
<For_statement
, STATEMENT_FOR
>(); }
442 // If this is a for statement over a range clause, return it.
443 // Otherwise return NULL.
445 for_range_statement()
446 { return this->convert
<For_range_statement
, STATEMENT_FOR_RANGE
>(); }
448 // If this is a switch statement, return it. Otherwise return NULL.
451 { return this->convert
<Switch_statement
, STATEMENT_SWITCH
>(); }
453 // If this is a type switch statement, return it. Otherwise return
455 Type_switch_statement
*
456 type_switch_statement()
457 { return this->convert
<Type_switch_statement
, STATEMENT_TYPE_SWITCH
>(); }
459 // If this is a send statement, return it. Otherwise return NULL.
462 { return this->convert
<Send_statement
, STATEMENT_SEND
>(); }
464 // If this is a select statement, return it. Otherwise return NULL.
467 { return this->convert
<Select_statement
, STATEMENT_SELECT
>(); }
469 // Return true if this statement may fall through--if after
470 // executing this statement we may go on to execute the following
471 // statement, if any.
473 may_fall_through() const
474 { return this->do_may_fall_through(); }
476 // Convert the statement to the backend representation.
478 get_backend(Translate_context
*);
480 // Dump AST representation of a statement to a dump context.
482 dump_statement(Ast_dump_context
*) const;
485 // Implemented by child class: traverse the tree.
487 do_traverse(Traverse
*) = 0;
489 // Implemented by child class: traverse assignments. Any statement
490 // which includes an assignment should implement this.
492 do_traverse_assignments(Traverse_assignments
*)
495 // Implemented by the child class: lower this statement to a simpler
498 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
501 // Implemented by the child class: lower this statement to a simpler
504 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
507 // Implemented by child class: set type information for unnamed
508 // constants. Any statement which includes an expression needs to
514 // Implemented by child class: check types of expressions used in a
517 do_check_types(Gogo
*)
520 // Implemented by child class: return the cost of this statement for
521 // inlining. The default cost is high, so we only need to define
522 // this method for statements that can be inlined.
527 // Implemented by child class: write export data for this statement
528 // to the string. This need only be implemented by classes that
529 // implement do_inlining_cost with a reasonable value.
531 do_export_statement(Export_function_body
*)
532 { go_unreachable(); }
534 // Implemented by child class: return true if this statement may
537 do_may_fall_through() const
540 // Implemented by child class: convert to backend representation.
542 do_get_backend(Translate_context
*) = 0;
544 // Implemented by child class: dump ast representation.
546 do_dump_statement(Ast_dump_context
*) const = 0;
548 // Implemented by child class: make implicit conversions explicit.
553 // Traverse an expression in a statement.
555 traverse_expression(Traverse
*, Expression
**);
557 // Traverse an expression list in a statement. The Expression_list
560 traverse_expression_list(Traverse
*, Expression_list
*);
562 // Traverse a type in a statement.
564 traverse_type(Traverse
*, Type
*);
566 // For children to call when they detect that they are in error.
570 // For children to call to report an error conveniently.
572 report_error(const char*);
574 // For children to return an error statement from lower().
576 make_error_statement(Location
);
579 // Convert to the desired statement classification, or return NULL.
580 // This is a controlled dynamic cast.
581 template<typename Statement_class
, Statement_classification sc
>
585 return (this->classification_
== sc
586 ? static_cast<Statement_class
*>(this)
590 template<typename Statement_class
, Statement_classification sc
>
591 const Statement_class
*
594 return (this->classification_
== sc
595 ? static_cast<const Statement_class
*>(this)
599 // The statement classification.
600 Statement_classification classification_
;
601 // The location in the input file of the start of this statement.
605 // An assignment statement.
607 class Assignment_statement
: public Statement
610 Assignment_statement(Expression
* lhs
, Expression
* rhs
,
612 : Statement(STATEMENT_ASSIGNMENT
, location
),
613 lhs_(lhs
), rhs_(rhs
), omit_write_barrier_(false)
618 { return this->lhs_
; }
622 { return this->rhs_
; }
625 omit_write_barrier() const
626 { return this->omit_write_barrier_
; }
629 set_omit_write_barrier()
630 { this->omit_write_barrier_
= true; }
634 do_traverse(Traverse
* traverse
);
637 do_traverse_assignments(Traverse_assignments
*);
640 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
643 do_determine_types();
646 do_check_types(Gogo
*);
653 do_export_statement(Export_function_body
*);
656 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
659 do_get_backend(Translate_context
*);
662 do_dump_statement(Ast_dump_context
*) const;
665 do_add_conversions();
668 // Left hand side--the lvalue.
670 // Right hand side--the rvalue.
672 // True if we can omit a write barrier from this assignment.
673 bool omit_write_barrier_
;
676 // A statement which creates and initializes a temporary variable.
678 class Temporary_statement
: public Statement
681 Temporary_statement(Type
* type
, Expression
* init
, Location location
)
682 : Statement(STATEMENT_TEMPORARY
, location
),
683 type_(type
), init_(init
), bvariable_(NULL
), is_address_taken_(false),
684 value_escapes_(false), assigned_(false), uses_(0)
687 // Return the type of the temporary variable.
691 // Return the initializer if there is one.
694 { return this->init_
; }
696 // Set the initializer.
698 set_init(Expression
* expr
)
699 { this->init_
= expr
; }
701 // Whether something takes the address of this temporary
705 { return this->is_address_taken_
; }
707 // Record that something takes the address of this temporary
710 set_is_address_taken()
711 { this->is_address_taken_
= true; }
713 // Whether the value escapes.
715 value_escapes() const
716 { return this->value_escapes_
; }
718 // Record that the value escapes.
721 { this->value_escapes_
= true; }
723 // Whether this temporary variable is assigned (after initialization).
726 { return this->assigned_
; }
728 // Record that this temporary variable is assigned.
731 { this->assigned_
= true; }
733 // Number of uses of this temporary variable.
736 { return this->uses_
; }
738 // Add one use of this temporary variable.
743 // Return the temporary variable. This should not be called until
744 // after the statement itself has been converted.
746 get_backend_variable(Translate_context
*) const;
748 // Import the declaration of a temporary.
750 do_import(Import_function_body
*, Location
);
754 do_traverse(Traverse
*);
757 do_traverse_assignments(Traverse_assignments
*);
760 do_determine_types();
763 do_check_types(Gogo
*);
770 do_export_statement(Export_function_body
*);
773 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
776 do_get_backend(Translate_context
*);
779 do_dump_statement(Ast_dump_context
*) const;
782 do_add_conversions();
785 // The type of the temporary variable.
787 // The initial value of the temporary variable. This may be NULL.
789 // The backend representation of the temporary variable.
790 Bvariable
* bvariable_
;
791 // True if something takes the address of this temporary variable.
792 bool is_address_taken_
;
793 // True if the value assigned to this temporary variable escapes.
794 // This is used for select statements.
796 // True if this temporary variable is assigned (after initialization).
798 // Number of uses of this temporary variable.
802 // A variable declaration. This marks the point in the code where a
803 // variable is declared. The Variable is also attached to a Block.
805 class Variable_declaration_statement
: public Statement
808 Variable_declaration_statement(Named_object
* var
);
810 // The variable being declared.
813 { return this->var_
; }
815 // Import a variable declaration.
817 do_import(Import_function_body
*, Location
);
821 do_traverse(Traverse
*);
824 do_traverse_assignments(Traverse_assignments
*);
827 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
834 do_export_statement(Export_function_body
*);
837 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
840 do_get_backend(Translate_context
*);
843 do_dump_statement(Ast_dump_context
*) const;
846 do_add_conversions();
852 // A return statement.
854 class Return_statement
: public Statement
857 Return_statement(Expression_list
* vals
, Location location
)
858 : Statement(STATEMENT_RETURN
, location
),
859 vals_(vals
), is_lowered_(false)
862 // The list of values being returned. This may be NULL.
863 const Expression_list
*
865 { return this->vals_
; }
869 do_traverse(Traverse
* traverse
)
870 { return this->traverse_expression_list(traverse
, this->vals_
); }
873 do_traverse_assignments(Traverse_assignments
*);
876 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
879 do_may_fall_through() const
887 do_export_statement(Export_function_body
*);
890 do_get_backend(Translate_context
*);
893 do_dump_statement(Ast_dump_context
*) const;
896 // Return values. This may be NULL.
897 Expression_list
* vals_
;
898 // True if this statement has been lowered.
902 // An expression statement.
904 class Expression_statement
: public Statement
907 Expression_statement(Expression
* expr
, bool is_ignored
);
911 { return this->expr_
; }
915 do_traverse(Traverse
* traverse
)
916 { return this->traverse_expression(traverse
, &this->expr_
); }
919 do_determine_types();
922 do_check_types(Gogo
*);
925 do_may_fall_through() const;
932 do_export_statement(Export_function_body
*);
935 do_get_backend(Translate_context
* context
);
938 do_dump_statement(Ast_dump_context
*) const;
942 // Whether the value of this expression is being explicitly ignored.
946 // A block statement--a list of statements which may include variable
949 class Block_statement
: public Statement
952 Block_statement(Block
* block
, Location location
)
953 : Statement(STATEMENT_BLOCK
, location
),
954 block_(block
), is_lowered_for_statement_(false)
957 // Return the actual block.
960 { return this->block_
; }
963 set_is_lowered_for_statement()
964 { this->is_lowered_for_statement_
= true; }
967 is_lowered_for_statement()
968 { return this->is_lowered_for_statement_
; }
970 // Export a block for a block statement.
972 export_block(Export_function_body
*, Block
*, bool is_lowered_for_statement
);
974 // Import a block statement, returning the block.
975 // *IS_LOWERED_FOR_STATEMENT reports whether this block statement
976 // was lowered from a for statement.
978 do_import(Import_function_body
*, Location
, bool* is_lowered_for_statement
);
982 do_traverse(Traverse
* traverse
)
983 { return this->block_
->traverse(traverse
); }
987 { this->block_
->determine_types(); }
994 do_export_statement(Export_function_body
*);
997 do_may_fall_through() const
998 { return this->block_
->may_fall_through(); }
1001 do_get_backend(Translate_context
* context
);
1004 do_dump_statement(Ast_dump_context
*) const;
1008 // True if this block statement represents a lowered for statement.
1009 bool is_lowered_for_statement_
;
1012 // A send statement.
1014 class Send_statement
: public Statement
1017 Send_statement(Expression
* channel
, Expression
* val
,
1019 : Statement(STATEMENT_SEND
, location
),
1020 channel_(channel
), val_(val
)
1025 { return this->channel_
; }
1029 { return this->val_
; }
1033 do_traverse(Traverse
* traverse
);
1036 do_determine_types();
1039 do_check_types(Gogo
*);
1042 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1045 do_get_backend(Translate_context
*);
1048 do_dump_statement(Ast_dump_context
*) const;
1051 do_add_conversions();
1054 // The channel on which to send the value.
1055 Expression
* channel_
;
1056 // The value to send.
1060 // Select_clauses holds the clauses of a select statement. This is
1061 // built by the parser.
1063 class Select_clauses
1070 // Add a new clause. IS_SEND is true if this is a send clause,
1071 // false for a receive clause. For a send clause CHANNEL is the
1072 // channel and VAL is the value to send. For a receive clause
1073 // CHANNEL is the channel, VAL is either NULL or a Var_expression
1074 // for the variable to set, and CLOSED is either NULL or a
1075 // Var_expression to set to whether the channel is closed. If VAL
1076 // is NULL, VAR may be a variable to be initialized with the
1077 // received value, and CLOSEDVAR may be a variable to be initialized
1078 // with whether the channel is closed. IS_DEFAULT is true if this
1079 // is the default clause. STATEMENTS is the list of statements to
1082 add(bool is_send
, Expression
* channel
, Expression
* val
, Expression
* closed
,
1083 Named_object
* var
, Named_object
* closedvar
, bool is_default
,
1084 Block
* statements
, Location location
)
1086 this->clauses_
.push_back(Select_clause(is_send
, channel
, val
, closed
, var
,
1087 closedvar
, is_default
, statements
,
1093 { return this->clauses_
.size(); }
1096 has_default() const;
1098 // Traverse the select clauses.
1100 traverse(Traverse
*);
1102 // Lower statements.
1104 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*,
1105 Temporary_statement
*, int* send_count
, int* recv_count
);
1115 // Whether the select clauses may fall through to the statement
1116 // which follows the overall select statement.
1118 may_fall_through() const;
1120 // Convert to the backend representation.
1122 get_backend(Translate_context
*, Temporary_statement
* index
,
1123 Unnamed_label
* break_label
, Location
);
1125 // Dump AST representation.
1127 dump_clauses(Ast_dump_context
*) const;
1134 : channel_(NULL
), val_(NULL
), closed_(NULL
), var_(NULL
),
1135 closedvar_(NULL
), statements_(NULL
), is_send_(false),
1139 Select_clause(bool is_send
, Expression
* channel
, Expression
* val
,
1140 Expression
* closed
, Named_object
* var
,
1141 Named_object
* closedvar
, bool is_default
, Block
* statements
,
1143 : channel_(channel
), val_(val
), closed_(closed
), var_(var
),
1144 closedvar_(closedvar
), statements_(statements
), case_index_(0),
1145 location_(location
), is_send_(is_send
), is_default_(is_default
),
1146 is_lowered_(false), is_case_index_set_(false)
1147 { go_assert(is_default
? channel
== NULL
: channel
!= NULL
); }
1149 // Traverse the select clause.
1151 traverse(Traverse
*);
1153 // Lower statements.
1155 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*, int,
1156 Temporary_statement
*);
1166 // Return true if this is the default clause.
1169 { return this->is_default_
; }
1171 // Return the channel. This will return NULL for the default
1175 { return this->channel_
; }
1177 // Return true for a send, false for a receive.
1181 go_assert(!this->is_default_
);
1182 return this->is_send_
;
1185 // Return the value to send or the lvalue to receive into.
1188 { return this->val_
; }
1190 // Return the lvalue to set to whether the channel is closed
1194 { return this->closed_
; }
1196 // Return the variable to initialize, for "case a := <-ch".
1199 { return this->var_
; }
1201 // Return the variable to initialize to whether the channel
1202 // is closed, for "case a, c := <-ch".
1205 { return this->closedvar_
; }
1207 // Return the statements.
1210 { return this->statements_
; }
1212 // Return the location.
1215 { return this->location_
; }
1217 // Return the case index for this clause.
1221 go_assert(this->is_case_index_set_
);
1222 return this->case_index_
;
1225 // Set the case index.
1227 set_case_index(int i
)
1229 go_assert(!this->is_case_index_set_
);
1230 this->case_index_
= i
;
1231 this->is_case_index_set_
= true;
1234 // Whether this clause may fall through to the statement which
1235 // follows the overall select statement.
1237 may_fall_through() const;
1239 // Convert the statements to the backend representation.
1241 get_statements_backend(Translate_context
*);
1243 // Dump AST representation.
1245 dump_clause(Ast_dump_context
*) const;
1249 lower_send(Block
*, Expression
*, Expression
*);
1252 lower_recv(Gogo
*, Named_object
*, Block
*, Expression
*, Expression
*,
1253 Temporary_statement
*);
1256 set_case(Block
*, Expression
*, Expression
*, Expression
*);
1259 Expression
* channel_
;
1260 // The value to send or the lvalue to receive into.
1262 // The lvalue to set to whether the channel is closed on a
1264 Expression
* closed_
;
1265 // The variable to initialize, for "case a := <-ch".
1267 // The variable to initialize to whether the channel is closed,
1268 // for "case a, c := <-ch".
1269 Named_object
* closedvar_
;
1270 // The statements to execute.
1272 // The index of this clause in the switch statement. If
1273 // runtime.selectgo returns this index, this clause has been
1276 // The location of this clause.
1278 // Whether this is a send or a receive.
1280 // Whether this is the default.
1282 // Whether this has been lowered.
1284 // Whether the case index has been set.
1285 bool is_case_index_set_
;
1290 { return this->clauses_
.at(i
); }
1293 typedef std::vector
<Select_clause
> Clauses
;
1298 // A select statement.
1300 class Select_statement
: public Statement
1303 Select_statement(Location location
)
1304 : Statement(STATEMENT_SELECT
, location
),
1305 clauses_(NULL
), index_(NULL
), break_label_(NULL
), is_lowered_(false)
1310 add_clauses(Select_clauses
* clauses
)
1312 go_assert(this->clauses_
== NULL
);
1313 this->clauses_
= clauses
;
1316 // Return the break label for this select statement.
1322 do_traverse(Traverse
* traverse
)
1323 { return this->clauses_
->traverse(traverse
); }
1326 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1329 do_determine_types()
1330 { this->clauses_
->determine_types(); }
1333 do_check_types(Gogo
*)
1334 { this->clauses_
->check_types(); }
1337 do_may_fall_through() const;
1340 do_get_backend(Translate_context
*);
1343 do_dump_statement(Ast_dump_context
*) const;
1346 // Lower a one-case select statement.
1348 lower_one_case(Block
*);
1350 // Lower a two-case select statement with one defualt case.
1352 lower_two_case(Block
*);
1354 // The select clauses.
1355 Select_clauses
* clauses_
;
1356 // A temporary that holds the index value returned by selectgo.
1357 Temporary_statement
* index_
;
1359 Unnamed_label
* break_label_
;
1360 // Whether this statement has been lowered.
1364 // A statement which requires a thunk: go or defer.
1366 class Thunk_statement
: public Statement
1369 Thunk_statement(Statement_classification
, Call_expression
*,
1372 // Return the call expression.
1375 { return this->call_
; }
1377 // Simplify a go or defer statement so that it only uses a single
1380 simplify_statement(Gogo
*, Named_object
*, Block
*);
1384 do_traverse(Traverse
* traverse
);
1387 do_traverse_assignments(Traverse_assignments
*);
1390 do_determine_types();
1393 do_check_types(Gogo
*);
1395 // Return the function and argument for the call.
1397 get_fn_and_arg(Expression
** pfn
, Expression
** parg
);
1400 // Return whether this is a simple go statement.
1402 is_simple(Function_type
*) const;
1404 // Return whether the thunk function is a constant.
1406 is_constant_function() const;
1408 // Build the struct to use for a complex case.
1410 build_struct(Function_type
* fntype
);
1414 build_thunk(Gogo
*, const std::string
&, Struct_type
*);
1416 // Set the name to use for thunk field N.
1418 thunk_field_param(int n
, char* buf
, size_t buflen
);
1420 // The function call to be executed in a separate thread (go) or
1427 class Go_statement
: public Thunk_statement
1430 Go_statement(Call_expression
* call
, Location location
)
1431 : Thunk_statement(STATEMENT_GO
, call
, location
)
1436 do_get_backend(Translate_context
*);
1439 do_dump_statement(Ast_dump_context
*) const;
1442 // A defer statement.
1444 class Defer_statement
: public Thunk_statement
1447 Defer_statement(Call_expression
* call
, Location location
)
1448 : Thunk_statement(STATEMENT_DEFER
, call
, location
),
1454 { this->on_stack_
= true; }
1458 do_get_backend(Translate_context
*);
1461 do_dump_statement(Ast_dump_context
*) const;
1465 defer_struct_type();
1470 // A goto statement.
1472 class Goto_statement
: public Statement
1475 Goto_statement(Label
* label
, Location location
)
1476 : Statement(STATEMENT_GOTO
, location
),
1480 // Return the label being jumped to.
1483 { return this->label_
; }
1485 // Import a goto statement.
1487 do_import(Import_function_body
*, Location
);
1491 do_traverse(Traverse
*);
1494 do_check_types(Gogo
*);
1497 do_may_fall_through() const
1501 do_get_backend(Translate_context
*);
1508 do_export_statement(Export_function_body
*);
1511 do_dump_statement(Ast_dump_context
*) const;
1517 // A goto statement to an unnamed label.
1519 class Goto_unnamed_statement
: public Statement
1522 Goto_unnamed_statement(Unnamed_label
* label
, Location location
)
1523 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
1528 unnamed_label() const
1529 { return this->label_
; }
1533 do_traverse(Traverse
*);
1536 do_may_fall_through() const
1540 do_get_backend(Translate_context
* context
);
1547 do_export_statement(Export_function_body
*);
1550 do_dump_statement(Ast_dump_context
*) const;
1553 Unnamed_label
* label_
;
1556 // A label statement.
1558 class Label_statement
: public Statement
1561 Label_statement(Label
* label
, Location location
)
1562 : Statement(STATEMENT_LABEL
, location
),
1566 // Return the label itself.
1569 { return this->label_
; }
1571 // Import a label or unnamed label.
1573 do_import(Import_function_body
*, Location
);
1577 do_traverse(Traverse
*);
1580 do_get_backend(Translate_context
*);
1587 do_export_statement(Export_function_body
*);
1590 do_dump_statement(Ast_dump_context
*) const;
1597 // An unnamed label statement.
1599 class Unnamed_label_statement
: public Statement
1602 Unnamed_label_statement(Unnamed_label
* label
);
1606 do_traverse(Traverse
*);
1609 do_get_backend(Translate_context
* context
);
1616 do_export_statement(Export_function_body
*);
1619 do_dump_statement(Ast_dump_context
*) const;
1623 Unnamed_label
* label_
;
1628 class If_statement
: public Statement
1631 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
1633 : Statement(STATEMENT_IF
, location
),
1634 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
1639 { return this->cond_
; }
1643 { return this->then_block_
; }
1647 { return this->else_block_
; }
1649 // Import an if statement.
1651 do_import(Import_function_body
*, Location
);
1655 do_traverse(Traverse
*);
1658 do_determine_types();
1661 do_check_types(Gogo
*);
1668 do_export_statement(Export_function_body
*);
1671 do_may_fall_through() const;
1674 do_get_backend(Translate_context
*);
1677 do_dump_statement(Ast_dump_context
*) const;
1687 class For_statement
: public Statement
1690 For_statement(Block
* init
, Expression
* cond
, Block
* post
,
1692 : Statement(STATEMENT_FOR
, location
),
1693 init_(init
), cond_(cond
), post_(post
), statements_(NULL
),
1694 break_label_(NULL
), continue_label_(NULL
)
1697 // Add the statements.
1699 add_statements(Block
* statements
)
1701 go_assert(this->statements_
== NULL
);
1702 this->statements_
= statements
;
1705 // Return the break label for this for statement.
1709 // Return the continue label for this for statement.
1713 // Set the break and continue labels for this statement.
1715 set_break_continue_labels(Unnamed_label
* break_label
,
1716 Unnamed_label
* continue_label
);
1720 do_traverse(Traverse
*);
1723 do_traverse_assignments(Traverse_assignments
*)
1724 { go_unreachable(); }
1727 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1730 do_may_fall_through() const;
1733 do_get_backend(Translate_context
*)
1734 { go_unreachable(); }
1737 do_dump_statement(Ast_dump_context
*) const;
1740 // The initialization statements. This may be NULL.
1742 // The condition. This may be NULL.
1744 // The statements to run after each iteration. This may be NULL.
1746 // The statements in the loop itself.
1748 // The break label, if needed.
1749 Unnamed_label
* break_label_
;
1750 // The continue label, if needed.
1751 Unnamed_label
* continue_label_
;
1754 // A for statement over a range clause.
1756 class For_range_statement
: public Statement
1759 For_range_statement(Expression
* index_var
, Expression
* value_var
,
1760 Expression
* range
, Location location
)
1761 : Statement(STATEMENT_FOR_RANGE
, location
),
1762 index_var_(index_var
), value_var_(value_var
), range_(range
),
1763 statements_(NULL
), break_label_(NULL
), continue_label_(NULL
)
1766 // Add the statements.
1768 add_statements(Block
* statements
)
1770 go_assert(this->statements_
== NULL
);
1771 this->statements_
= statements
;
1774 // Return the break label for this for statement.
1778 // Return the continue label for this for statement.
1784 do_traverse(Traverse
*);
1787 do_traverse_assignments(Traverse_assignments
*)
1788 { go_unreachable(); }
1791 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1794 do_get_backend(Translate_context
*)
1795 { go_unreachable(); }
1798 do_dump_statement(Ast_dump_context
*) const;
1802 make_range_ref(Named_object
*, Temporary_statement
*, Location
);
1805 call_builtin(Gogo
*, const char* funcname
, Expression
* arg
, Location
);
1808 lower_range_array(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1809 Temporary_statement
*, Temporary_statement
*,
1810 Block
**, Expression
**, Block
**, Block
**);
1813 lower_range_slice(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1814 Temporary_statement
*, Temporary_statement
*,
1815 Block
**, Expression
**, Block
**, Block
**);
1818 lower_range_string(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1819 Temporary_statement
*, Temporary_statement
*,
1820 Block
**, Expression
**, Block
**, Block
**);
1823 lower_range_map(Gogo
*, Map_type
*, Block
*, Block
*, Named_object
*,
1824 Temporary_statement
*, Temporary_statement
*,
1825 Temporary_statement
*, Block
**, Expression
**, Block
**,
1829 lower_range_channel(Gogo
*, Block
*, Block
*, Named_object
*,
1830 Temporary_statement
*, Temporary_statement
*,
1831 Temporary_statement
*, Block
**, Expression
**, Block
**,
1835 lower_map_range_clear(Type
*, Block
*, Expression
*, Named_object
*,
1836 Temporary_statement
*, Location
);
1839 lower_array_range_clear(Gogo
*, Type
*, Expression
*, Block
*,
1840 Named_object
*, Temporary_statement
*,
1843 // The variable which is set to the index value.
1844 Expression
* index_var_
;
1845 // The variable which is set to the element value. This may be
1847 Expression
* value_var_
;
1848 // The expression we are ranging over.
1850 // The statements in the block.
1852 // The break label, if needed.
1853 Unnamed_label
* break_label_
;
1854 // The continue label, if needed.
1855 Unnamed_label
* continue_label_
;
1858 // Class Case_clauses holds the clauses of a switch statement. This
1859 // is built by the parser.
1868 // Add a new clause. CASES is a list of case expressions; it may be
1869 // NULL. IS_DEFAULT is true if this is the default case.
1870 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1871 // after the statements the case clause should fall through to the
1874 add(Expression_list
* cases
, bool is_default
, Block
* statements
,
1875 bool is_fallthrough
, Location location
)
1877 this->clauses_
.push_back(Case_clause(cases
, is_default
, statements
,
1878 is_fallthrough
, location
));
1881 // Return whether there are no clauses.
1884 { return this->clauses_
.empty(); }
1886 // Traverse the case clauses.
1888 traverse(Traverse
*);
1890 // Lower for a nonconstant switch.
1892 lower(Block
*, Temporary_statement
*, Unnamed_label
*) const;
1894 // Determine types of expressions. The Type parameter is the type
1895 // of the switch value.
1897 determine_types(Type
*);
1899 // Check types. The Type parameter is the type of the switch value.
1903 // Return true if all the clauses are constant values.
1905 is_constant() const;
1907 // Return true if these clauses may fall through to the statements
1908 // following the switch statement.
1910 may_fall_through() const;
1912 // Return the body of a SWITCH_EXPR when all the clauses are
1915 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1916 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
1917 std::vector
<Bstatement
*>* all_statements
) const;
1919 // Dump the AST representation to a dump context.
1921 dump_clauses(Ast_dump_context
*) const;
1924 // For a constant switch we need to keep a record of constants we
1925 // have already seen.
1926 class Hash_integer_value
;
1927 class Eq_integer_value
;
1928 typedef Unordered_set_hash(Expression
*, Hash_integer_value
,
1929 Eq_integer_value
) Case_constants
;
1936 : cases_(NULL
), statements_(NULL
), is_default_(false),
1937 is_fallthrough_(false), location_(Linemap::unknown_location())
1940 Case_clause(Expression_list
* cases
, bool is_default
, Block
* statements
,
1941 bool is_fallthrough
, Location location
)
1942 : cases_(cases
), statements_(statements
), is_default_(is_default
),
1943 is_fallthrough_(is_fallthrough
), location_(location
)
1946 // Whether this clause falls through to the next clause.
1948 is_fallthrough() const
1949 { return this->is_fallthrough_
; }
1951 // Whether this is the default.
1954 { return this->is_default_
; }
1956 // The location of this clause.
1959 { return this->location_
; }
1963 traverse(Traverse
*);
1965 // Lower for a nonconstant switch.
1967 lower(Block
*, Temporary_statement
*, Unnamed_label
*, Unnamed_label
*) const;
1971 determine_types(Type
*);
1977 // Return true if all the case expressions are constant.
1979 is_constant() const;
1981 // Return true if this clause may fall through to execute the
1982 // statements following the switch statement. This is not the
1983 // same as whether this clause falls through to the next clause.
1985 may_fall_through() const;
1987 // Convert the case values and statements to the backend
1990 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1991 Case_constants
*, std::vector
<Bexpression
*>* cases
) const;
1993 // Dump the AST representation to a dump context.
1995 dump_clause(Ast_dump_context
*) const;
1998 // The list of case expressions.
1999 Expression_list
* cases_
;
2000 // The statements to execute.
2002 // Whether this is the default case.
2004 // Whether this falls through after the statements.
2005 bool is_fallthrough_
;
2006 // The location of this case clause.
2010 friend class Case_clause
;
2012 // The type of the list of clauses.
2013 typedef std::vector
<Case_clause
> Clauses
;
2015 // All the case clauses.
2019 // A switch statement.
2021 class Switch_statement
: public Statement
2024 Switch_statement(Expression
* val
, Location location
)
2025 : Statement(STATEMENT_SWITCH
, location
),
2026 val_(val
), clauses_(NULL
), break_label_(NULL
)
2031 add_clauses(Case_clauses
* clauses
)
2033 go_assert(this->clauses_
== NULL
);
2034 this->clauses_
= clauses
;
2037 // Return the break label for this switch statement.
2043 do_traverse(Traverse
*);
2046 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
2049 do_get_backend(Translate_context
*)
2050 { go_unreachable(); }
2053 do_dump_statement(Ast_dump_context
*) const;
2056 do_may_fall_through() const;
2059 // The value to switch on. This may be NULL.
2061 // The case clauses.
2062 Case_clauses
* clauses_
;
2063 // The break label, if needed.
2064 Unnamed_label
* break_label_
;
2067 // Class Type_case_clauses holds the clauses of a type switch
2068 // statement. This is built by the parser.
2070 class Type_case_clauses
2077 // Add a new clause. TYPE is the type for this clause; it may be
2078 // NULL. IS_FALLTHROUGH is true if this falls through to the next
2079 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
2080 // if this is the default case. STATEMENTS is a block of
2081 // statements; it may be NULL.
2083 add(Type
* type
, bool is_fallthrough
, bool is_default
, Block
* statements
,
2086 this->clauses_
.push_back(Type_case_clause(type
, is_fallthrough
, is_default
,
2087 statements
, location
));
2090 // Return whether there are no clauses.
2093 { return this->clauses_
.empty(); }
2095 // Traverse the type case clauses.
2097 traverse(Traverse
*);
2099 // Check for duplicates.
2101 check_duplicates() const;
2103 // Lower to if and goto statements.
2105 lower(Gogo
*, Type
*, Block
*, Temporary_statement
* descriptor_temp
,
2106 Unnamed_label
* break_label
) const;
2108 // Return true if these clauses may fall through to the statements
2109 // following the switch statement.
2111 may_fall_through() const;
2113 // Dump the AST representation to a dump context.
2115 dump_clauses(Ast_dump_context
*) const;
2118 // One type case clause.
2119 class Type_case_clause
2123 : type_(NULL
), statements_(NULL
), is_default_(false),
2124 location_(Linemap::unknown_location())
2127 Type_case_clause(Type
* type
, bool is_fallthrough
, bool is_default
,
2128 Block
* statements
, Location location
)
2129 : type_(type
), statements_(statements
), is_fallthrough_(is_fallthrough
),
2130 is_default_(is_default
), location_(location
)
2136 { return this->type_
; }
2138 // Whether this is the default.
2141 { return this->is_default_
; }
2143 // The location of this type clause.
2146 { return this->location_
; }
2150 traverse(Traverse
*);
2152 // Lower to if and goto statements.
2154 lower(Gogo
*, Type
*, Block
*, Temporary_statement
* descriptor_temp
,
2155 Unnamed_label
* break_label
, Unnamed_label
** stmts_label
) const;
2157 // Return true if this clause may fall through to execute the
2158 // statements following the switch statement. This is not the
2159 // same as whether this clause falls through to the next clause.
2161 may_fall_through() const;
2163 // Dump the AST representation to a dump context.
2165 dump_clause(Ast_dump_context
*) const;
2168 // The type for this type clause.
2170 // The statements to execute.
2172 // Whether this falls through--this is true for "case T1, T2".
2173 bool is_fallthrough_
;
2174 // Whether this is the default case.
2176 // The location of this type case clause.
2180 friend class Type_case_clause
;
2182 // The type of the list of type clauses.
2183 typedef std::vector
<Type_case_clause
> Type_clauses
;
2185 // All the type case clauses.
2186 Type_clauses clauses_
;
2189 // A type switch statement.
2191 class Type_switch_statement
: public Statement
2194 Type_switch_statement(const std::string
& name
, Expression
* expr
,
2196 : Statement(STATEMENT_TYPE_SWITCH
, location
),
2197 name_(name
), expr_(expr
), clauses_(NULL
), break_label_(NULL
)
2202 add_clauses(Type_case_clauses
* clauses
)
2204 go_assert(this->clauses_
== NULL
);
2205 this->clauses_
= clauses
;
2208 // Return the break label for this type switch statement.
2214 do_traverse(Traverse
*);
2217 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
2220 do_get_backend(Translate_context
*)
2221 { go_unreachable(); }
2224 do_dump_statement(Ast_dump_context
*) const;
2227 do_may_fall_through() const;
2230 // The name of the variable declared in the type switch guard. Empty if there
2231 // is no variable declared.
2233 // The expression we are switching on if there is no variable.
2235 // The type case clauses.
2236 Type_case_clauses
* clauses_
;
2237 // The break label, if needed.
2238 Unnamed_label
* break_label_
;
2241 #endif // !defined(GO_STATEMENTS_H)