1 // statements.h -- Go frontend statements. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #ifndef GO_STATEMENTS_H
8 #define GO_STATEMENTS_H
14 class Statement_inserter
;
18 class Assignment_statement
;
19 class Temporary_statement
;
20 class Variable_declaration_statement
;
21 class Expression_statement
;
22 class Block_statement
;
23 class Return_statement
;
24 class Thunk_statement
;
26 class Goto_unnamed_statement
;
27 class Label_statement
;
28 class Unnamed_label_statement
;
31 class For_range_statement
;
32 class Switch_statement
;
33 class Type_switch_statement
;
35 class Select_statement
;
39 class Translate_context
;
41 class Expression_list
;
43 class Call_expression
;
44 class Map_index_expression
;
45 class Receive_expression
;
47 class Type_case_clauses
;
49 class Typed_identifier_list
;
53 class Ast_dump_context
;
55 // This class is used to traverse assignments made by a statement
56 // which makes assignments.
58 class Traverse_assignments
61 Traverse_assignments()
64 virtual ~Traverse_assignments()
67 // This is called for a variable initialization.
69 initialize_variable(Named_object
*) = 0;
71 // This is called for each assignment made by the statement. PLHS
72 // points to the left hand side, and PRHS points to the right hand
73 // side. PRHS may be NULL if there is no associated expression, as
74 // in the bool set by a non-blocking receive.
76 assignment(Expression
** plhs
, Expression
** prhs
) = 0;
78 // This is called for each expression which is not passed to the
79 // assignment function. This is used for some of the statements
80 // which assign two values, for which there is no expression which
81 // describes the value. For ++ and -- the value is passed to both
82 // the assignment method and the rhs method. IS_STORED is true if
83 // this value is being stored directly. It is false if the value is
84 // computed but not stored. IS_LOCAL is true if the value is being
85 // stored in a local variable or this is being called by a return
88 value(Expression
**, bool is_stored
, bool is_local
) = 0;
91 // A single statement.
96 // The types of statements.
97 enum Statement_classification
100 STATEMENT_VARIABLE_DECLARATION
,
102 STATEMENT_ASSIGNMENT
,
103 STATEMENT_EXPRESSION
,
108 STATEMENT_BREAK_OR_CONTINUE
,
110 STATEMENT_GOTO_UNNAMED
,
112 STATEMENT_UNNAMED_LABEL
,
114 STATEMENT_CONSTANT_SWITCH
,
118 // These statements types are created by the parser, but they
119 // disappear during the lowering pass.
120 STATEMENT_ASSIGNMENT_OPERATION
,
121 STATEMENT_TUPLE_ASSIGNMENT
,
122 STATEMENT_TUPLE_MAP_ASSIGNMENT
,
123 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT
,
124 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT
,
129 STATEMENT_TYPE_SWITCH
132 Statement(Statement_classification
, Location
);
134 virtual ~Statement();
136 // Make a variable declaration.
138 make_variable_declaration(Named_object
*);
140 // Make a statement which creates a temporary variable and
141 // initializes it to an expression. The block is used if the
142 // temporary variable has to be explicitly destroyed; the variable
143 // must still be added to the block. References to the temporary
144 // variable may be constructed using make_temporary_reference.
145 // Either the type or the initialization expression may be NULL, but
147 static Temporary_statement
*
148 make_temporary(Type
*, Expression
*, Location
);
150 // Make an assignment statement.
152 make_assignment(Expression
*, Expression
*, Location
);
154 // Make an assignment operation (+=, etc.).
156 make_assignment_operation(Operator
, Expression
*, Expression
*,
159 // Make a tuple assignment statement.
161 make_tuple_assignment(Expression_list
*, Expression_list
*, Location
);
163 // Make an assignment from a map index to a pair of variables.
165 make_tuple_map_assignment(Expression
* val
, Expression
* present
,
166 Expression
*, Location
);
168 // Make an assignment from a nonblocking receive to a pair of
171 make_tuple_receive_assignment(Expression
* val
, Expression
* closed
,
172 Expression
* channel
, Location
);
174 // Make an assignment from a type guard to a pair of variables.
176 make_tuple_type_guard_assignment(Expression
* val
, Expression
* ok
,
177 Expression
* expr
, Type
* type
,
180 // Make an expression statement from an Expression. IS_IGNORED is
181 // true if the value is being explicitly ignored, as in an
184 make_statement(Expression
*, bool is_ignored
);
186 // Make a block statement from a Block. This is an embedded list of
187 // statements which may also include variable definitions.
189 make_block_statement(Block
*, Location
);
191 // Make an increment statement.
193 make_inc_statement(Expression
*);
195 // Make a decrement statement.
197 make_dec_statement(Expression
*);
199 // Make a go statement.
201 make_go_statement(Call_expression
* call
, Location
);
203 // Make a defer statement.
205 make_defer_statement(Call_expression
* call
, Location
);
207 // Make a return statement.
208 static Return_statement
*
209 make_return_statement(Expression_list
*, Location
);
211 // Make a statement that returns the result of a call expression.
212 // If the call does not return any results, this just returns the
213 // call expression as a statement, assuming that the function will
214 // end immediately afterward.
216 make_return_from_call(Call_expression
*, Location
);
218 // Make a break statement.
220 make_break_statement(Unnamed_label
* label
, Location
);
222 // Make a continue statement.
224 make_continue_statement(Unnamed_label
* label
, Location
);
226 // Make a goto statement.
228 make_goto_statement(Label
* label
, Location
);
230 // Make a goto statement to an unnamed label.
232 make_goto_unnamed_statement(Unnamed_label
* label
, Location
);
234 // Make a label statement--where the label is defined.
236 make_label_statement(Label
* label
, Location
);
238 // Make an unnamed label statement--where the label is defined.
240 make_unnamed_label_statement(Unnamed_label
* label
);
242 // Make an if statement.
244 make_if_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
247 // Make a switch statement.
248 static Switch_statement
*
249 make_switch_statement(Expression
* switch_val
, Location
);
251 // Make a type switch statement.
252 static Type_switch_statement
*
253 make_type_switch_statement(const std::string
&, Expression
*, Location
);
255 // Make a send statement.
256 static Send_statement
*
257 make_send_statement(Expression
* channel
, Expression
* val
, Location
);
259 // Make a select statement.
260 static Select_statement
*
261 make_select_statement(Location
);
263 // Make a for statement.
264 static For_statement
*
265 make_for_statement(Block
* init
, Expression
* cond
, Block
* post
,
268 // Make a for statement with a range clause.
269 static For_range_statement
*
270 make_for_range_statement(Expression
* index_var
, Expression
* value_var
,
271 Expression
* range
, Location
);
273 // Return the statement classification.
274 Statement_classification
275 classification() const
276 { return this->classification_
; }
278 // Get the statement location.
281 { return this->location_
; }
283 // Traverse the tree.
285 traverse(Block
*, size_t* index
, Traverse
*);
287 // Traverse the contents of this statement--the expressions and
288 // statements which it contains.
290 traverse_contents(Traverse
*);
292 // If this statement assigns some values, it calls a function for
293 // each value to which this statement assigns a value, and returns
294 // true. If this statement does not assign any values, it returns
297 traverse_assignments(Traverse_assignments
* tassign
);
299 // Lower a statement. This is called immediately after parsing to
300 // simplify statements for further processing. It returns the same
301 // Statement or a new one. FUNCTION is the function containing this
302 // statement. BLOCK is the block containing this statement.
303 // INSERTER can be used to insert new statements before this one.
305 lower(Gogo
* gogo
, Named_object
* function
, Block
* block
,
306 Statement_inserter
* inserter
)
307 { return this->do_lower(gogo
, function
, block
, inserter
); }
309 // Flatten a statement. This is called immediately after the order of
310 // evaluation rules are applied to statements. It returns the same
311 // Statement or a new one. FUNCTION is the function containing this
312 // statement. BLOCK is the block containing this statement.
313 // INSERTER can be used to insert new statements before this one.
315 flatten(Gogo
* gogo
, Named_object
* function
, Block
* block
,
316 Statement_inserter
* inserter
)
317 { return this->do_flatten(gogo
, function
, block
, inserter
); }
319 // Set type information for unnamed constants.
323 // Check types in a statement. This simply checks that any
324 // expressions used by the statement have the right type.
326 check_types(Gogo
* gogo
)
327 { this->do_check_types(gogo
); }
329 // Return whether this is a block statement.
331 is_block_statement() const
332 { return this->classification_
== STATEMENT_BLOCK
; }
334 // If this is an assignment statement, return it. Otherwise return
336 Assignment_statement
*
337 assignment_statement()
339 return this->convert
<Assignment_statement
, STATEMENT_ASSIGNMENT
>();
342 // If this is an temporary statement, return it. Otherwise return
345 temporary_statement()
347 return this->convert
<Temporary_statement
, STATEMENT_TEMPORARY
>();
350 // If this is a variable declaration statement, return it.
351 // Otherwise return NULL.
352 Variable_declaration_statement
*
353 variable_declaration_statement()
355 return this->convert
<Variable_declaration_statement
,
356 STATEMENT_VARIABLE_DECLARATION
>();
359 // If this is an expression statement, return it. Otherwise return
361 Expression_statement
*
362 expression_statement()
364 return this->convert
<Expression_statement
, STATEMENT_EXPRESSION
>();
367 // If this is an block statement, return it. Otherwise return
371 { return this->convert
<Block_statement
, STATEMENT_BLOCK
>(); }
373 // If this is a return statement, return it. Otherwise return NULL.
376 { return this->convert
<Return_statement
, STATEMENT_RETURN
>(); }
378 // If this is a thunk statement (a go or defer statement), return
379 // it. Otherwise return NULL.
383 // If this is a goto statement, return it. Otherwise return NULL.
386 { return this->convert
<Goto_statement
, STATEMENT_GOTO
>(); }
388 // If this is a goto_unnamed statement, return it. Otherwise return NULL.
389 Goto_unnamed_statement
*
390 goto_unnamed_statement()
391 { return this->convert
<Goto_unnamed_statement
, STATEMENT_GOTO_UNNAMED
>(); }
393 // If this is a label statement, return it. Otherwise return NULL.
396 { return this->convert
<Label_statement
, STATEMENT_LABEL
>(); }
398 // If this is an unnamed_label statement, return it. Otherwise return NULL.
399 Unnamed_label_statement
*
400 unnamed_label_statement()
401 { return this->convert
<Unnamed_label_statement
, STATEMENT_UNNAMED_LABEL
>(); }
403 // If this is an if statement, return it. Otherwise return NULL.
406 { return this->convert
<If_statement
, STATEMENT_IF
>(); }
408 // If this is a for statement, return it. Otherwise return NULL.
411 { return this->convert
<For_statement
, STATEMENT_FOR
>(); }
413 // If this is a for statement over a range clause, return it.
414 // Otherwise return NULL.
416 for_range_statement()
417 { return this->convert
<For_range_statement
, STATEMENT_FOR_RANGE
>(); }
419 // If this is a switch statement, return it. Otherwise return NULL.
422 { return this->convert
<Switch_statement
, STATEMENT_SWITCH
>(); }
424 // If this is a type switch statement, return it. Otherwise return
426 Type_switch_statement
*
427 type_switch_statement()
428 { return this->convert
<Type_switch_statement
, STATEMENT_TYPE_SWITCH
>(); }
430 // If this is a send statement, return it. Otherwise return NULL.
433 { return this->convert
<Send_statement
, STATEMENT_SEND
>(); }
435 // If this is a select statement, return it. Otherwise return NULL.
438 { return this->convert
<Select_statement
, STATEMENT_SELECT
>(); }
440 // Return true if this statement may fall through--if after
441 // executing this statement we may go on to execute the following
442 // statement, if any.
444 may_fall_through() const
445 { return this->do_may_fall_through(); }
447 // Convert the statement to the backend representation.
449 get_backend(Translate_context
*);
451 // Dump AST representation of a statement to a dump context.
453 dump_statement(Ast_dump_context
*) const;
456 // Implemented by child class: traverse the tree.
458 do_traverse(Traverse
*) = 0;
460 // Implemented by child class: traverse assignments. Any statement
461 // which includes an assignment should implement this.
463 do_traverse_assignments(Traverse_assignments
*)
466 // Implemented by the child class: lower this statement to a simpler
469 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
472 // Implemented by the child class: lower this statement to a simpler
475 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*)
478 // Implemented by child class: set type information for unnamed
479 // constants. Any statement which includes an expression needs to
485 // Implemented by child class: check types of expressions used in a
488 do_check_types(Gogo
*)
491 // Implemented by child class: return true if this statement may
494 do_may_fall_through() const
497 // Implemented by child class: convert to backend representation.
499 do_get_backend(Translate_context
*) = 0;
501 // Implemented by child class: dump ast representation.
503 do_dump_statement(Ast_dump_context
*) const = 0;
505 // Traverse an expression in a statement.
507 traverse_expression(Traverse
*, Expression
**);
509 // Traverse an expression list in a statement. The Expression_list
512 traverse_expression_list(Traverse
*, Expression_list
*);
514 // Traverse a type in a statement.
516 traverse_type(Traverse
*, Type
*);
518 // For children to call when they detect that they are in error.
522 // For children to call to report an error conveniently.
524 report_error(const char*);
526 // For children to return an error statement from lower().
528 make_error_statement(Location
);
531 // Convert to the desired statement classification, or return NULL.
532 // This is a controlled dynamic cast.
533 template<typename Statement_class
, Statement_classification sc
>
537 return (this->classification_
== sc
538 ? static_cast<Statement_class
*>(this)
542 template<typename Statement_class
, Statement_classification sc
>
543 const Statement_class
*
546 return (this->classification_
== sc
547 ? static_cast<const Statement_class
*>(this)
551 // The statement classification.
552 Statement_classification classification_
;
553 // The location in the input file of the start of this statement.
557 // An assignment statement.
559 class Assignment_statement
: public Statement
562 Assignment_statement(Expression
* lhs
, Expression
* rhs
,
564 : Statement(STATEMENT_ASSIGNMENT
, location
),
570 { return this->lhs_
; }
574 { return this->rhs_
; }
578 do_traverse(Traverse
* traverse
);
581 do_traverse_assignments(Traverse_assignments
*);
584 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
587 do_determine_types();
590 do_check_types(Gogo
*);
593 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
596 do_get_backend(Translate_context
*);
599 do_dump_statement(Ast_dump_context
*) const;
602 // Left hand side--the lvalue.
604 // Right hand side--the rvalue.
608 // A statement which creates and initializes a temporary variable.
610 class Temporary_statement
: public Statement
613 Temporary_statement(Type
* type
, Expression
* init
, Location location
)
614 : Statement(STATEMENT_TEMPORARY
, location
),
615 type_(type
), init_(init
), bvariable_(NULL
), is_address_taken_(false)
618 // Return the type of the temporary variable.
622 // Return the initializer if there is one.
625 { return this->init_
; }
627 // Record that something takes the address of this temporary
630 set_is_address_taken()
631 { this->is_address_taken_
= true; }
633 // Return the temporary variable. This should not be called until
634 // after the statement itself has been converted.
636 get_backend_variable(Translate_context
*) const;
640 do_traverse(Traverse
*);
643 do_traverse_assignments(Traverse_assignments
*);
646 do_determine_types();
649 do_check_types(Gogo
*);
652 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
655 do_get_backend(Translate_context
*);
658 do_dump_statement(Ast_dump_context
*) const;
661 // The type of the temporary variable.
663 // The initial value of the temporary variable. This may be NULL.
665 // The backend representation of the temporary variable.
666 Bvariable
* bvariable_
;
667 // True if something takes the address of this temporary variable.
668 bool is_address_taken_
;
671 // A variable declaration. This marks the point in the code where a
672 // variable is declared. The Variable is also attached to a Block.
674 class Variable_declaration_statement
: public Statement
677 Variable_declaration_statement(Named_object
* var
);
679 // The variable being declared.
682 { return this->var_
; }
686 do_traverse(Traverse
*);
689 do_traverse_assignments(Traverse_assignments
*);
692 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
695 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
698 do_get_backend(Translate_context
*);
701 do_dump_statement(Ast_dump_context
*) const;
707 // A return statement.
709 class Return_statement
: public Statement
712 Return_statement(Expression_list
* vals
, Location location
)
713 : Statement(STATEMENT_RETURN
, location
),
714 vals_(vals
), is_lowered_(false)
717 // The list of values being returned. This may be NULL.
718 const Expression_list
*
720 { return this->vals_
; }
724 do_traverse(Traverse
* traverse
)
725 { return this->traverse_expression_list(traverse
, this->vals_
); }
728 do_traverse_assignments(Traverse_assignments
*);
731 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
734 do_may_fall_through() const
738 do_get_backend(Translate_context
*);
741 do_dump_statement(Ast_dump_context
*) const;
744 // Return values. This may be NULL.
745 Expression_list
* vals_
;
746 // True if this statement has been lowered.
750 // An expression statement.
752 class Expression_statement
: public Statement
755 Expression_statement(Expression
* expr
, bool is_ignored
);
759 { return this->expr_
; }
763 do_traverse(Traverse
* traverse
)
764 { return this->traverse_expression(traverse
, &this->expr_
); }
767 do_determine_types();
770 do_check_types(Gogo
*);
773 do_may_fall_through() const;
776 do_get_backend(Translate_context
* context
);
779 do_dump_statement(Ast_dump_context
*) const;
783 // Whether the value of this expression is being explicitly ignored.
787 // A block statement--a list of statements which may include variable
790 class Block_statement
: public Statement
793 Block_statement(Block
* block
, Location location
)
794 : Statement(STATEMENT_BLOCK
, location
),
795 block_(block
), is_lowered_for_statement_(false)
799 set_is_lowered_for_statement()
800 { this->is_lowered_for_statement_
= true; }
803 is_lowered_for_statement()
804 { return this->is_lowered_for_statement_
; }
808 do_traverse(Traverse
* traverse
)
809 { return this->block_
->traverse(traverse
); }
813 { this->block_
->determine_types(); }
816 do_may_fall_through() const
817 { return this->block_
->may_fall_through(); }
820 do_get_backend(Translate_context
* context
);
823 do_dump_statement(Ast_dump_context
*) const;
827 // True if this block statement represents a lowered for statement.
828 bool is_lowered_for_statement_
;
833 class Send_statement
: public Statement
836 Send_statement(Expression
* channel
, Expression
* val
,
838 : Statement(STATEMENT_SEND
, location
),
839 channel_(channel
), val_(val
)
844 { return this->channel_
; }
848 { return this->val_
; }
852 do_traverse(Traverse
* traverse
);
855 do_determine_types();
858 do_check_types(Gogo
*);
861 do_flatten(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
864 do_get_backend(Translate_context
*);
867 do_dump_statement(Ast_dump_context
*) const;
870 // The channel on which to send the value.
871 Expression
* channel_
;
872 // The value to send.
876 // Select_clauses holds the clauses of a select statement. This is
877 // built by the parser.
886 // Add a new clause. IS_SEND is true if this is a send clause,
887 // false for a receive clause. For a send clause CHANNEL is the
888 // channel and VAL is the value to send. For a receive clause
889 // CHANNEL is the channel, VAL is either NULL or a Var_expression
890 // for the variable to set, and CLOSED is either NULL or a
891 // Var_expression to set to whether the channel is closed. If VAL
892 // is NULL, VAR may be a variable to be initialized with the
893 // received value, and CLOSEDVAR ma be a variable to be initialized
894 // with whether the channel is closed. IS_DEFAULT is true if this
895 // is the default clause. STATEMENTS is the list of statements to
898 add(bool is_send
, Expression
* channel
, Expression
* val
, Expression
* closed
,
899 Named_object
* var
, Named_object
* closedvar
, bool is_default
,
900 Block
* statements
, Location location
)
902 this->clauses_
.push_back(Select_clause(is_send
, channel
, val
, closed
, var
,
903 closedvar
, is_default
, statements
,
909 { return this->clauses_
.size(); }
911 // Traverse the select clauses.
917 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*);
927 // Whether the select clauses may fall through to the statement
928 // which follows the overall select statement.
930 may_fall_through() const;
932 // Convert to the backend representation.
934 get_backend(Translate_context
*, Temporary_statement
* sel
,
935 Unnamed_label
* break_label
, Location
);
937 // Dump AST representation.
939 dump_clauses(Ast_dump_context
*) const;
947 : channel_(NULL
), val_(NULL
), closed_(NULL
), var_(NULL
),
948 closedvar_(NULL
), statements_(NULL
), is_send_(false),
952 Select_clause(bool is_send
, Expression
* channel
, Expression
* val
,
953 Expression
* closed
, Named_object
* var
,
954 Named_object
* closedvar
, bool is_default
, Block
* statements
,
956 : channel_(channel
), val_(val
), closed_(closed
), var_(var
),
957 closedvar_(closedvar
), statements_(statements
), location_(location
),
958 is_send_(is_send
), is_default_(is_default
), is_lowered_(false)
959 { go_assert(is_default
? channel
== NULL
: channel
!= NULL
); }
961 // Traverse the select clause.
967 lower(Gogo
*, Named_object
*, Block
*, Temporary_statement
*);
977 // Return true if this is the default clause.
980 { return this->is_default_
; }
982 // Return the channel. This will return NULL for the default
986 { return this->channel_
; }
988 // Return true for a send, false for a receive.
992 go_assert(!this->is_default_
);
993 return this->is_send_
;
996 // Return the statements.
999 { return this->statements_
; }
1001 // Return the location.
1004 { return this->location_
; }
1006 // Whether this clause may fall through to the statement which
1007 // follows the overall select statement.
1009 may_fall_through() const;
1011 // Convert the statements to the backend representation.
1013 get_statements_backend(Translate_context
*);
1015 // Dump AST representation.
1017 dump_clause(Ast_dump_context
*) const;
1021 lower_default(Block
*, Expression
*);
1024 lower_send(Block
*, Expression
*, Expression
*);
1027 lower_recv(Gogo
*, Named_object
*, Block
*, Expression
*, Expression
*);
1030 Expression
* channel_
;
1031 // The value to send or the lvalue to receive into.
1033 // The lvalue to set to whether the channel is closed on a
1035 Expression
* closed_
;
1036 // The variable to initialize, for "case a := <-ch".
1038 // The variable to initialize to whether the channel is closed,
1039 // for "case a, c := <-ch".
1040 Named_object
* closedvar_
;
1041 // The statements to execute.
1043 // The location of this clause.
1045 // Whether this is a send or a receive.
1047 // Whether this is the default.
1049 // Whether this has been lowered.
1053 typedef std::vector
<Select_clause
> Clauses
;
1058 // A select statement.
1060 class Select_statement
: public Statement
1063 Select_statement(Location location
)
1064 : Statement(STATEMENT_SELECT
, location
),
1065 clauses_(NULL
), sel_(NULL
), break_label_(NULL
), is_lowered_(false)
1070 add_clauses(Select_clauses
* clauses
)
1072 go_assert(this->clauses_
== NULL
);
1073 this->clauses_
= clauses
;
1076 // Return the break label for this select statement.
1082 do_traverse(Traverse
* traverse
)
1083 { return this->clauses_
->traverse(traverse
); }
1086 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1089 do_determine_types()
1090 { this->clauses_
->determine_types(); }
1093 do_check_types(Gogo
*)
1094 { this->clauses_
->check_types(); }
1097 do_may_fall_through() const;
1100 do_get_backend(Translate_context
*);
1103 do_dump_statement(Ast_dump_context
*) const;
1106 // The select clauses.
1107 Select_clauses
* clauses_
;
1108 // A temporary which holds the select structure we build up at runtime.
1109 Temporary_statement
* sel_
;
1111 Unnamed_label
* break_label_
;
1112 // Whether this statement has been lowered.
1116 // A statement which requires a thunk: go or defer.
1118 class Thunk_statement
: public Statement
1121 Thunk_statement(Statement_classification
, Call_expression
*,
1124 // Return the call expression.
1127 { return this->call_
; }
1129 // Simplify a go or defer statement so that it only uses a single
1132 simplify_statement(Gogo
*, Named_object
*, Block
*);
1136 do_traverse(Traverse
* traverse
);
1139 do_traverse_assignments(Traverse_assignments
*);
1142 do_determine_types();
1145 do_check_types(Gogo
*);
1147 // Return the function and argument for the call.
1149 get_fn_and_arg(Expression
** pfn
, Expression
** parg
);
1152 // Return whether this is a simple go statement.
1154 is_simple(Function_type
*) const;
1156 // Return whether the thunk function is a constant.
1158 is_constant_function() const;
1160 // Build the struct to use for a complex case.
1162 build_struct(Function_type
* fntype
);
1166 build_thunk(Gogo
*, const std::string
&);
1168 // Set the name to use for thunk field N.
1170 thunk_field_param(int n
, char* buf
, size_t buflen
);
1172 // The function call to be executed in a separate thread (go) or
1175 // The type used for a struct to pass to a thunk, if this is not a
1177 Struct_type
* struct_type_
;
1182 class Go_statement
: public Thunk_statement
1185 Go_statement(Call_expression
* call
, Location location
)
1186 : Thunk_statement(STATEMENT_GO
, call
, location
)
1191 do_get_backend(Translate_context
*);
1194 do_dump_statement(Ast_dump_context
*) const;
1197 // A defer statement.
1199 class Defer_statement
: public Thunk_statement
1202 Defer_statement(Call_expression
* call
, Location location
)
1203 : Thunk_statement(STATEMENT_DEFER
, call
, location
)
1208 do_get_backend(Translate_context
*);
1211 do_dump_statement(Ast_dump_context
*) const;
1214 // A goto statement.
1216 class Goto_statement
: public Statement
1219 Goto_statement(Label
* label
, Location location
)
1220 : Statement(STATEMENT_GOTO
, location
),
1224 // Return the label being jumped to.
1227 { return this->label_
; }
1231 do_traverse(Traverse
*);
1234 do_check_types(Gogo
*);
1237 do_may_fall_through() const
1241 do_get_backend(Translate_context
*);
1244 do_dump_statement(Ast_dump_context
*) const;
1250 // A goto statement to an unnamed label.
1252 class Goto_unnamed_statement
: public Statement
1255 Goto_unnamed_statement(Unnamed_label
* label
, Location location
)
1256 : Statement(STATEMENT_GOTO_UNNAMED
, location
),
1261 unnamed_label() const
1262 { return this->label_
; }
1266 do_traverse(Traverse
*);
1269 do_may_fall_through() const
1273 do_get_backend(Translate_context
* context
);
1276 do_dump_statement(Ast_dump_context
*) const;
1279 Unnamed_label
* label_
;
1282 // A label statement.
1284 class Label_statement
: public Statement
1287 Label_statement(Label
* label
, Location location
)
1288 : Statement(STATEMENT_LABEL
, location
),
1292 // Return the label itself.
1295 { return this->label_
; }
1299 do_traverse(Traverse
*);
1302 do_get_backend(Translate_context
*);
1305 do_dump_statement(Ast_dump_context
*) const;
1312 // An unnamed label statement.
1314 class Unnamed_label_statement
: public Statement
1317 Unnamed_label_statement(Unnamed_label
* label
);
1321 do_traverse(Traverse
*);
1324 do_get_backend(Translate_context
* context
);
1327 do_dump_statement(Ast_dump_context
*) const;
1331 Unnamed_label
* label_
;
1336 class If_statement
: public Statement
1339 If_statement(Expression
* cond
, Block
* then_block
, Block
* else_block
,
1341 : Statement(STATEMENT_IF
, location
),
1342 cond_(cond
), then_block_(then_block
), else_block_(else_block
)
1347 { return this->cond_
; }
1351 do_traverse(Traverse
*);
1354 do_determine_types();
1357 do_check_types(Gogo
*);
1360 do_may_fall_through() const;
1363 do_get_backend(Translate_context
*);
1366 do_dump_statement(Ast_dump_context
*) const;
1376 class For_statement
: public Statement
1379 For_statement(Block
* init
, Expression
* cond
, Block
* post
,
1381 : Statement(STATEMENT_FOR
, location
),
1382 init_(init
), cond_(cond
), post_(post
), statements_(NULL
),
1383 break_label_(NULL
), continue_label_(NULL
)
1386 // Add the statements.
1388 add_statements(Block
* statements
)
1390 go_assert(this->statements_
== NULL
);
1391 this->statements_
= statements
;
1394 // Return the break label for this for statement.
1398 // Return the continue label for this for statement.
1402 // Set the break and continue labels for this statement.
1404 set_break_continue_labels(Unnamed_label
* break_label
,
1405 Unnamed_label
* continue_label
);
1409 do_traverse(Traverse
*);
1412 do_traverse_assignments(Traverse_assignments
*)
1413 { go_unreachable(); }
1416 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1419 do_may_fall_through() const;
1422 do_get_backend(Translate_context
*)
1423 { go_unreachable(); }
1426 do_dump_statement(Ast_dump_context
*) const;
1429 // The initialization statements. This may be NULL.
1431 // The condition. This may be NULL.
1433 // The statements to run after each iteration. This may be NULL.
1435 // The statements in the loop itself.
1437 // The break label, if needed.
1438 Unnamed_label
* break_label_
;
1439 // The continue label, if needed.
1440 Unnamed_label
* continue_label_
;
1443 // A for statement over a range clause.
1445 class For_range_statement
: public Statement
1448 For_range_statement(Expression
* index_var
, Expression
* value_var
,
1449 Expression
* range
, Location location
)
1450 : Statement(STATEMENT_FOR_RANGE
, location
),
1451 index_var_(index_var
), value_var_(value_var
), range_(range
),
1452 statements_(NULL
), break_label_(NULL
), continue_label_(NULL
)
1455 // Add the statements.
1457 add_statements(Block
* statements
)
1459 go_assert(this->statements_
== NULL
);
1460 this->statements_
= statements
;
1463 // Return the break label for this for statement.
1467 // Return the continue label for this for statement.
1473 do_traverse(Traverse
*);
1476 do_traverse_assignments(Traverse_assignments
*)
1477 { go_unreachable(); }
1480 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1483 do_get_backend(Translate_context
*)
1484 { go_unreachable(); }
1487 do_dump_statement(Ast_dump_context
*) const;
1491 make_range_ref(Named_object
*, Temporary_statement
*, Location
);
1494 call_builtin(Gogo
*, const char* funcname
, Expression
* arg
, Location
);
1497 lower_range_array(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1498 Temporary_statement
*, Temporary_statement
*,
1499 Block
**, Expression
**, Block
**, Block
**);
1502 lower_range_slice(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1503 Temporary_statement
*, Temporary_statement
*,
1504 Block
**, Expression
**, Block
**, Block
**);
1507 lower_range_string(Gogo
*, Block
*, Block
*, Named_object
*, Temporary_statement
*,
1508 Temporary_statement
*, Temporary_statement
*,
1509 Block
**, Expression
**, Block
**, Block
**);
1512 lower_range_map(Gogo
*, Map_type
*, Block
*, Block
*, Named_object
*,
1513 Temporary_statement
*, Temporary_statement
*,
1514 Temporary_statement
*, Block
**, Expression
**, Block
**,
1518 lower_range_channel(Gogo
*, Block
*, Block
*, Named_object
*,
1519 Temporary_statement
*, Temporary_statement
*,
1520 Temporary_statement
*, Block
**, Expression
**, Block
**,
1523 // The variable which is set to the index value.
1524 Expression
* index_var_
;
1525 // The variable which is set to the element value. This may be
1527 Expression
* value_var_
;
1528 // The expression we are ranging over.
1530 // The statements in the block.
1532 // The break label, if needed.
1533 Unnamed_label
* break_label_
;
1534 // The continue label, if needed.
1535 Unnamed_label
* continue_label_
;
1538 // Class Case_clauses holds the clauses of a switch statement. This
1539 // is built by the parser.
1548 // Add a new clause. CASES is a list of case expressions; it may be
1549 // NULL. IS_DEFAULT is true if this is the default case.
1550 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1551 // after the statements the case clause should fall through to the
1554 add(Expression_list
* cases
, bool is_default
, Block
* statements
,
1555 bool is_fallthrough
, Location location
)
1557 this->clauses_
.push_back(Case_clause(cases
, is_default
, statements
,
1558 is_fallthrough
, location
));
1561 // Return whether there are no clauses.
1564 { return this->clauses_
.empty(); }
1566 // Traverse the case clauses.
1568 traverse(Traverse
*);
1570 // Lower for a nonconstant switch.
1572 lower(Block
*, Temporary_statement
*, Unnamed_label
*) const;
1574 // Determine types of expressions. The Type parameter is the type
1575 // of the switch value.
1577 determine_types(Type
*);
1579 // Check types. The Type parameter is the type of the switch value.
1583 // Return true if all the clauses are constant values.
1585 is_constant() const;
1587 // Return true if these clauses may fall through to the statements
1588 // following the switch statement.
1590 may_fall_through() const;
1592 // Return the body of a SWITCH_EXPR when all the clauses are
1595 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1596 std::vector
<std::vector
<Bexpression
*> >* all_cases
,
1597 std::vector
<Bstatement
*>* all_statements
) const;
1599 // Dump the AST representation to a dump context.
1601 dump_clauses(Ast_dump_context
*) const;
1604 // For a constant switch we need to keep a record of constants we
1605 // have already seen.
1606 class Hash_integer_value
;
1607 class Eq_integer_value
;
1608 typedef Unordered_set_hash(Expression
*, Hash_integer_value
,
1609 Eq_integer_value
) Case_constants
;
1616 : cases_(NULL
), statements_(NULL
), is_default_(false),
1617 is_fallthrough_(false), location_(Linemap::unknown_location())
1620 Case_clause(Expression_list
* cases
, bool is_default
, Block
* statements
,
1621 bool is_fallthrough
, Location location
)
1622 : cases_(cases
), statements_(statements
), is_default_(is_default
),
1623 is_fallthrough_(is_fallthrough
), location_(location
)
1626 // Whether this clause falls through to the next clause.
1628 is_fallthrough() const
1629 { return this->is_fallthrough_
; }
1631 // Whether this is the default.
1634 { return this->is_default_
; }
1636 // The location of this clause.
1639 { return this->location_
; }
1643 traverse(Traverse
*);
1645 // Lower for a nonconstant switch.
1647 lower(Block
*, Temporary_statement
*, Unnamed_label
*, Unnamed_label
*) const;
1651 determine_types(Type
*);
1657 // Return true if all the case expressions are constant.
1659 is_constant() const;
1661 // Return true if this clause may fall through to execute the
1662 // statements following the switch statement. This is not the
1663 // same as whether this clause falls through to the next clause.
1665 may_fall_through() const;
1667 // Convert the case values and statements to the backend
1670 get_backend(Translate_context
*, Unnamed_label
* break_label
,
1671 Case_constants
*, std::vector
<Bexpression
*>* cases
) const;
1673 // Dump the AST representation to a dump context.
1675 dump_clause(Ast_dump_context
*) const;
1678 // The list of case expressions.
1679 Expression_list
* cases_
;
1680 // The statements to execute.
1682 // Whether this is the default case.
1684 // Whether this falls through after the statements.
1685 bool is_fallthrough_
;
1686 // The location of this case clause.
1690 friend class Case_clause
;
1692 // The type of the list of clauses.
1693 typedef std::vector
<Case_clause
> Clauses
;
1695 // All the case clauses.
1699 // A switch statement.
1701 class Switch_statement
: public Statement
1704 Switch_statement(Expression
* val
, Location location
)
1705 : Statement(STATEMENT_SWITCH
, location
),
1706 val_(val
), clauses_(NULL
), break_label_(NULL
)
1711 add_clauses(Case_clauses
* clauses
)
1713 go_assert(this->clauses_
== NULL
);
1714 this->clauses_
= clauses
;
1717 // Return the break label for this switch statement.
1723 do_traverse(Traverse
*);
1726 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1729 do_get_backend(Translate_context
*)
1730 { go_unreachable(); }
1733 do_dump_statement(Ast_dump_context
*) const;
1736 do_may_fall_through() const;
1739 // The value to switch on. This may be NULL.
1741 // The case clauses.
1742 Case_clauses
* clauses_
;
1743 // The break label, if needed.
1744 Unnamed_label
* break_label_
;
1747 // Class Type_case_clauses holds the clauses of a type switch
1748 // statement. This is built by the parser.
1750 class Type_case_clauses
1757 // Add a new clause. TYPE is the type for this clause; it may be
1758 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1759 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1760 // if this is the default case. STATEMENTS is a block of
1761 // statements; it may be NULL.
1763 add(Type
* type
, bool is_fallthrough
, bool is_default
, Block
* statements
,
1766 this->clauses_
.push_back(Type_case_clause(type
, is_fallthrough
, is_default
,
1767 statements
, location
));
1770 // Return whether there are no clauses.
1773 { return this->clauses_
.empty(); }
1775 // Traverse the type case clauses.
1777 traverse(Traverse
*);
1779 // Check for duplicates.
1781 check_duplicates() const;
1783 // Lower to if and goto statements.
1785 lower(Type
*, Block
*, Temporary_statement
* descriptor_temp
,
1786 Unnamed_label
* break_label
) const;
1788 // Return true if these clauses may fall through to the statements
1789 // following the switch statement.
1791 may_fall_through() const;
1793 // Dump the AST representation to a dump context.
1795 dump_clauses(Ast_dump_context
*) const;
1798 // One type case clause.
1799 class Type_case_clause
1803 : type_(NULL
), statements_(NULL
), is_default_(false),
1804 location_(Linemap::unknown_location())
1807 Type_case_clause(Type
* type
, bool is_fallthrough
, bool is_default
,
1808 Block
* statements
, Location location
)
1809 : type_(type
), statements_(statements
), is_fallthrough_(is_fallthrough
),
1810 is_default_(is_default
), location_(location
)
1816 { return this->type_
; }
1818 // Whether this is the default.
1821 { return this->is_default_
; }
1823 // The location of this type clause.
1826 { return this->location_
; }
1830 traverse(Traverse
*);
1832 // Lower to if and goto statements.
1834 lower(Type
*, Block
*, Temporary_statement
* descriptor_temp
,
1835 Unnamed_label
* break_label
, Unnamed_label
** stmts_label
) const;
1837 // Return true if this clause may fall through to execute the
1838 // statements following the switch statement. This is not the
1839 // same as whether this clause falls through to the next clause.
1841 may_fall_through() const;
1843 // Dump the AST representation to a dump context.
1845 dump_clause(Ast_dump_context
*) const;
1848 // The type for this type clause.
1850 // The statements to execute.
1852 // Whether this falls through--this is true for "case T1, T2".
1853 bool is_fallthrough_
;
1854 // Whether this is the default case.
1856 // The location of this type case clause.
1860 friend class Type_case_clause
;
1862 // The type of the list of type clauses.
1863 typedef std::vector
<Type_case_clause
> Type_clauses
;
1865 // All the type case clauses.
1866 Type_clauses clauses_
;
1869 // A type switch statement.
1871 class Type_switch_statement
: public Statement
1874 Type_switch_statement(const std::string
& name
, Expression
* expr
,
1876 : Statement(STATEMENT_TYPE_SWITCH
, location
),
1877 name_(name
), expr_(expr
), clauses_(NULL
), break_label_(NULL
)
1882 add_clauses(Type_case_clauses
* clauses
)
1884 go_assert(this->clauses_
== NULL
);
1885 this->clauses_
= clauses
;
1888 // Return the break label for this type switch statement.
1894 do_traverse(Traverse
*);
1897 do_lower(Gogo
*, Named_object
*, Block
*, Statement_inserter
*);
1900 do_get_backend(Translate_context
*)
1901 { go_unreachable(); }
1904 do_dump_statement(Ast_dump_context
*) const;
1907 do_may_fall_through() const;
1910 // The name of the variable declared in the type switch guard. Empty if there
1911 // is no variable declared.
1913 // The expression we are switching on if there is no variable.
1915 // The type case clauses.
1916 Type_case_clauses
* clauses_
;
1917 // The break label, if needed.
1918 Unnamed_label
* break_label_
;
1921 #endif // !defined(GO_STATEMENTS_H)