Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / gcc / go / gofrontend / statements.h
blob3d1ee33a3e514700e063efebf22c12888d9d8c56
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
10 #include "operator.h"
12 class Gogo;
13 class Traverse;
14 class Statement_inserter;
15 class Block;
16 class Function;
17 class Unnamed_label;
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;
28 class Goto_statement;
29 class Goto_unnamed_statement;
30 class Label_statement;
31 class Unnamed_label_statement;
32 class If_statement;
33 class For_statement;
34 class For_range_statement;
35 class Switch_statement;
36 class Type_switch_statement;
37 class Send_statement;
38 class Select_statement;
39 class Variable;
40 class Named_object;
41 class Label;
42 class Translate_context;
43 class Expression;
44 class Expression_list;
45 class Struct_type;
46 class Call_expression;
47 class Map_index_expression;
48 class Receive_expression;
49 class Case_clauses;
50 class Type_case_clauses;
51 class Select_clauses;
52 class Typed_identifier_list;
53 class Bexpression;
54 class Bstatement;
55 class Bvariable;
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
63 public:
64 Traverse_assignments()
65 { }
67 virtual ~Traverse_assignments()
68 { }
70 // This is called for a variable initialization.
71 virtual void
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.
78 virtual void
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
89 // statement.
90 virtual void
91 value(Expression**, bool is_stored, bool is_local) = 0;
94 // A single statement.
96 class Statement
98 public:
99 // The types of statements.
100 enum Statement_classification
102 STATEMENT_ERROR,
103 STATEMENT_VARIABLE_DECLARATION,
104 STATEMENT_TEMPORARY,
105 STATEMENT_ASSIGNMENT,
106 STATEMENT_EXPRESSION,
107 STATEMENT_BLOCK,
108 STATEMENT_GO,
109 STATEMENT_DEFER,
110 STATEMENT_RETURN,
111 STATEMENT_BREAK_OR_CONTINUE,
112 STATEMENT_GOTO,
113 STATEMENT_GOTO_UNNAMED,
114 STATEMENT_LABEL,
115 STATEMENT_UNNAMED_LABEL,
116 STATEMENT_IF,
117 STATEMENT_CONSTANT_SWITCH,
118 STATEMENT_SEND,
119 STATEMENT_SELECT,
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,
128 STATEMENT_INCDEC,
129 STATEMENT_FOR,
130 STATEMENT_FOR_RANGE,
131 STATEMENT_SWITCH,
132 STATEMENT_TYPE_SWITCH
135 Statement(Statement_classification, Location);
137 virtual ~Statement();
139 // Make a variable declaration.
140 static Statement*
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
149 // not both.
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.).
158 static Statement*
159 make_assignment_operation(Operator, Expression*, Expression*,
160 Location);
162 // Make a tuple assignment statement.
163 static Statement*
164 make_tuple_assignment(Expression_list*, Expression_list*, Location);
166 // Make an assignment from a map index to a pair of variables.
167 static Statement*
168 make_tuple_map_assignment(Expression* val, Expression* present,
169 Expression*, Location);
171 // Make an assignment from a nonblocking receive to a pair of
172 // variables.
173 static Statement*
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.
178 static Statement*
179 make_tuple_type_guard_assignment(Expression* val, Expression* ok,
180 Expression* expr, Type* type,
181 Location);
183 // Make an expression statement from an Expression. IS_IGNORED is
184 // true if the value is being explicitly ignored, as in an
185 // assignment to _.
186 static Statement*
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.
195 static Statement*
196 make_inc_statement(Expression*);
198 // Make a decrement statement.
199 static Statement*
200 make_dec_statement(Expression*);
202 // Make a go statement.
203 static Statement*
204 make_go_statement(Call_expression* call, Location);
206 // Make a defer statement.
207 static 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.
218 static Statement*
219 make_return_from_call(Call_expression*, Location);
221 // Make a break statement.
222 static Statement*
223 make_break_statement(Unnamed_label* label, Location);
225 // Make a continue statement.
226 static Statement*
227 make_continue_statement(Unnamed_label* label, Location);
229 // Make a goto statement.
230 static Statement*
231 make_goto_statement(Label* label, Location);
233 // Make a goto statement to an unnamed label.
234 static Statement*
235 make_goto_unnamed_statement(Unnamed_label* label, Location);
237 // Make a label statement--where the label is defined.
238 static Statement*
239 make_label_statement(Label* label, Location);
241 // Make an unnamed label statement--where the label is defined.
242 static Statement*
243 make_unnamed_label_statement(Unnamed_label* label);
245 // Make an if statement.
246 static Statement*
247 make_if_statement(Expression* cond, Block* then_block, Block* else_block,
248 Location);
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,
269 Location location);
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.
282 Location
283 location() const
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
298 // false.
299 bool
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.
307 Statement*
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.
317 Statement*
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.
323 void
324 determine_types();
326 // Check types in a statement. This simply checks that any
327 // expressions used by the statement have the right type.
328 void
329 check_types(Gogo* gogo)
330 { this->do_check_types(gogo); }
332 // Return the cost of this statement for inlining purposes.
334 inlining_cost()
335 { return this->do_inlining_cost(); }
337 // Export data for this statement to BODY.
338 void
339 export_statement(Export_function_body* efb)
340 { this->do_export_statement(efb); }
342 // Make implicit type conversions explicit.
343 void
344 add_conversions()
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.
350 static Statement*
351 import_statement(Import_function_body*, Location);
353 // Return whether this is a block statement.
354 bool
355 is_block_statement() const
356 { return this->classification_ == STATEMENT_BLOCK; }
358 // If this is an assignment statement, return it. Otherwise return
359 // NULL.
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
367 // NULL.
368 Temporary_statement*
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
384 // NULL.
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
392 // NULL.
393 Block_statement*
394 block_statement()
395 { return this->convert<Block_statement, STATEMENT_BLOCK>(); }
397 // If this is a return statement, return it. Otherwise return NULL.
398 Return_statement*
399 return_statement()
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.
404 Thunk_statement*
405 thunk_statement();
407 // If this is a defer statement, return it. Otherwise return NULL.
408 Defer_statement*
409 defer_statement()
410 { return this->convert<Defer_statement, STATEMENT_DEFER>(); }
412 // If this is a goto statement, return it. Otherwise return NULL.
413 Goto_statement*
414 goto_statement()
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.
423 Label_statement*
424 label_statement()
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.
433 If_statement*
434 if_statement()
435 { return this->convert<If_statement, STATEMENT_IF>(); }
437 // If this is a for statement, return it. Otherwise return NULL.
438 For_statement*
439 for_statement()
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.
444 For_range_statement*
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.
449 Switch_statement*
450 switch_statement()
451 { return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
453 // If this is a type switch statement, return it. Otherwise return
454 // NULL.
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.
460 Send_statement*
461 send_statement()
462 { return this->convert<Send_statement, STATEMENT_SEND>(); }
464 // If this is a select statement, return it. Otherwise return NULL.
465 Select_statement*
466 select_statement()
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.
472 bool
473 may_fall_through() const
474 { return this->do_may_fall_through(); }
476 // Convert the statement to the backend representation.
477 Bstatement*
478 get_backend(Translate_context*);
480 // Dump AST representation of a statement to a dump context.
481 void
482 dump_statement(Ast_dump_context*) const;
484 protected:
485 // Implemented by child class: traverse the tree.
486 virtual int
487 do_traverse(Traverse*) = 0;
489 // Implemented by child class: traverse assignments. Any statement
490 // which includes an assignment should implement this.
491 virtual bool
492 do_traverse_assignments(Traverse_assignments*)
493 { return false; }
495 // Implemented by the child class: lower this statement to a simpler
496 // one.
497 virtual Statement*
498 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
499 { return this; }
501 // Implemented by the child class: lower this statement to a simpler
502 // one.
503 virtual Statement*
504 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*)
505 { return this; }
507 // Implemented by child class: set type information for unnamed
508 // constants. Any statement which includes an expression needs to
509 // implement this.
510 virtual void
511 do_determine_types()
514 // Implemented by child class: check types of expressions used in a
515 // statement.
516 virtual void
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.
523 virtual int
524 do_inlining_cost()
525 { return 0x100000; }
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.
530 virtual void
531 do_export_statement(Export_function_body*)
532 { go_unreachable(); }
534 // Implemented by child class: return true if this statement may
535 // fall through.
536 virtual bool
537 do_may_fall_through() const
538 { return true; }
540 // Implemented by child class: convert to backend representation.
541 virtual Bstatement*
542 do_get_backend(Translate_context*) = 0;
544 // Implemented by child class: dump ast representation.
545 virtual void
546 do_dump_statement(Ast_dump_context*) const = 0;
548 // Implemented by child class: make implicit conversions explicit.
549 virtual void
550 do_add_conversions()
553 // Traverse an expression in a statement.
555 traverse_expression(Traverse*, Expression**);
557 // Traverse an expression list in a statement. The Expression_list
558 // may be NULL.
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.
567 void
568 set_is_error();
570 // For children to call to report an error conveniently.
571 void
572 report_error(const char*);
574 // For children to return an error statement from lower().
575 static Statement*
576 make_error_statement(Location);
578 private:
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>
582 Statement_class*
583 convert()
585 return (this->classification_ == sc
586 ? static_cast<Statement_class*>(this)
587 : NULL);
590 template<typename Statement_class, Statement_classification sc>
591 const Statement_class*
592 convert() const
594 return (this->classification_ == sc
595 ? static_cast<const Statement_class*>(this)
596 : NULL);
599 // The statement classification.
600 Statement_classification classification_;
601 // The location in the input file of the start of this statement.
602 Location location_;
605 // An assignment statement.
607 class Assignment_statement : public Statement
609 public:
610 Assignment_statement(Expression* lhs, Expression* rhs,
611 Location location)
612 : Statement(STATEMENT_ASSIGNMENT, location),
613 lhs_(lhs), rhs_(rhs), omit_write_barrier_(false)
616 Expression*
617 lhs() const
618 { return this->lhs_; }
620 Expression*
621 rhs() const
622 { return this->rhs_; }
624 bool
625 omit_write_barrier() const
626 { return this->omit_write_barrier_; }
628 void
629 set_omit_write_barrier()
630 { this->omit_write_barrier_ = true; }
632 protected:
634 do_traverse(Traverse* traverse);
636 bool
637 do_traverse_assignments(Traverse_assignments*);
639 virtual Statement*
640 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
642 void
643 do_determine_types();
645 void
646 do_check_types(Gogo*);
649 do_inlining_cost()
650 { return 1; }
652 void
653 do_export_statement(Export_function_body*);
655 Statement*
656 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
658 Bstatement*
659 do_get_backend(Translate_context*);
661 void
662 do_dump_statement(Ast_dump_context*) const;
664 void
665 do_add_conversions();
667 private:
668 // Left hand side--the lvalue.
669 Expression* lhs_;
670 // Right hand side--the rvalue.
671 Expression* rhs_;
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
680 public:
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.
688 Type*
689 type() const;
691 // Return the initializer if there is one.
692 Expression*
693 init() const
694 { return this->init_; }
696 // Set the initializer.
697 void
698 set_init(Expression* expr)
699 { this->init_ = expr; }
701 // Whether something takes the address of this temporary
702 // variable.
703 bool
704 is_address_taken()
705 { return this->is_address_taken_; }
707 // Record that something takes the address of this temporary
708 // variable.
709 void
710 set_is_address_taken()
711 { this->is_address_taken_ = true; }
713 // Whether the value escapes.
714 bool
715 value_escapes() const
716 { return this->value_escapes_; }
718 // Record that the value escapes.
719 void
720 set_value_escapes()
721 { this->value_escapes_ = true; }
723 // Whether this temporary variable is assigned (after initialization).
724 bool
725 assigned()
726 { return this->assigned_; }
728 // Record that this temporary variable is assigned.
729 void
730 set_assigned()
731 { this->assigned_ = true; }
733 // Number of uses of this temporary variable.
735 uses()
736 { return this->uses_; }
738 // Add one use of this temporary variable.
739 void
740 add_use()
741 { this->uses_++; }
743 // Return the temporary variable. This should not be called until
744 // after the statement itself has been converted.
745 Bvariable*
746 get_backend_variable(Translate_context*) const;
748 // Import the declaration of a temporary.
749 static Statement*
750 do_import(Import_function_body*, Location);
752 protected:
754 do_traverse(Traverse*);
756 bool
757 do_traverse_assignments(Traverse_assignments*);
759 void
760 do_determine_types();
762 void
763 do_check_types(Gogo*);
766 do_inlining_cost()
767 { return 1; }
769 void
770 do_export_statement(Export_function_body*);
772 Statement*
773 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
775 Bstatement*
776 do_get_backend(Translate_context*);
778 void
779 do_dump_statement(Ast_dump_context*) const;
781 void
782 do_add_conversions();
784 private:
785 // The type of the temporary variable.
786 Type* type_;
787 // The initial value of the temporary variable. This may be NULL.
788 Expression* init_;
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.
795 bool value_escapes_;
796 // True if this temporary variable is assigned (after initialization).
797 bool assigned_;
798 // Number of uses of this temporary variable.
799 int uses_;
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
807 public:
808 Variable_declaration_statement(Named_object* var);
810 // The variable being declared.
811 Named_object*
812 var()
813 { return this->var_; }
815 // Import a variable declaration.
816 static Statement*
817 do_import(Import_function_body*, Location);
819 protected:
821 do_traverse(Traverse*);
823 bool
824 do_traverse_assignments(Traverse_assignments*);
826 Statement*
827 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
830 do_inlining_cost()
831 { return 1; }
833 void
834 do_export_statement(Export_function_body*);
836 Statement*
837 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
839 Bstatement*
840 do_get_backend(Translate_context*);
842 void
843 do_dump_statement(Ast_dump_context*) const;
845 void
846 do_add_conversions();
848 private:
849 Named_object* var_;
852 // A return statement.
854 class Return_statement : public Statement
856 public:
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*
864 vals() const
865 { return this->vals_; }
867 protected:
869 do_traverse(Traverse* traverse)
870 { return this->traverse_expression_list(traverse, this->vals_); }
872 bool
873 do_traverse_assignments(Traverse_assignments*);
875 Statement*
876 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
878 bool
879 do_may_fall_through() const
880 { return false; }
883 do_inlining_cost()
884 { return 1; }
886 void
887 do_export_statement(Export_function_body*);
889 Bstatement*
890 do_get_backend(Translate_context*);
892 void
893 do_dump_statement(Ast_dump_context*) const;
895 private:
896 // Return values. This may be NULL.
897 Expression_list* vals_;
898 // True if this statement has been lowered.
899 bool is_lowered_;
902 // An expression statement.
904 class Expression_statement : public Statement
906 public:
907 Expression_statement(Expression* expr, bool is_ignored);
909 Expression*
910 expr()
911 { return this->expr_; }
913 protected:
915 do_traverse(Traverse* traverse)
916 { return this->traverse_expression(traverse, &this->expr_); }
918 void
919 do_determine_types();
921 void
922 do_check_types(Gogo*);
924 bool
925 do_may_fall_through() const;
928 do_inlining_cost()
929 { return 0; }
931 void
932 do_export_statement(Export_function_body*);
934 Bstatement*
935 do_get_backend(Translate_context* context);
937 void
938 do_dump_statement(Ast_dump_context*) const;
940 private:
941 Expression* expr_;
942 // Whether the value of this expression is being explicitly ignored.
943 bool is_ignored_;
946 // A block statement--a list of statements which may include variable
947 // definitions.
949 class Block_statement : public Statement
951 public:
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.
958 Block*
959 block() const
960 { return this->block_; }
962 void
963 set_is_lowered_for_statement()
964 { this->is_lowered_for_statement_ = true; }
966 bool
967 is_lowered_for_statement()
968 { return this->is_lowered_for_statement_; }
970 // Export a block for a block statement.
971 static void
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.
977 static Block*
978 do_import(Import_function_body*, Location, bool* is_lowered_for_statement);
980 protected:
982 do_traverse(Traverse* traverse)
983 { return this->block_->traverse(traverse); }
985 void
986 do_determine_types()
987 { this->block_->determine_types(); }
990 do_inlining_cost()
991 { return 0; }
993 void
994 do_export_statement(Export_function_body*);
996 bool
997 do_may_fall_through() const
998 { return this->block_->may_fall_through(); }
1000 Bstatement*
1001 do_get_backend(Translate_context* context);
1003 void
1004 do_dump_statement(Ast_dump_context*) const;
1006 private:
1007 Block* block_;
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
1016 public:
1017 Send_statement(Expression* channel, Expression* val,
1018 Location location)
1019 : Statement(STATEMENT_SEND, location),
1020 channel_(channel), val_(val)
1023 Expression*
1024 channel()
1025 { return this->channel_; }
1027 Expression*
1028 val()
1029 { return this->val_; }
1031 protected:
1033 do_traverse(Traverse* traverse);
1035 void
1036 do_determine_types();
1038 void
1039 do_check_types(Gogo*);
1041 Statement*
1042 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
1044 Bstatement*
1045 do_get_backend(Translate_context*);
1047 void
1048 do_dump_statement(Ast_dump_context*) const;
1050 void
1051 do_add_conversions();
1053 private:
1054 // The channel on which to send the value.
1055 Expression* channel_;
1056 // The value to send.
1057 Expression* val_;
1060 // Select_clauses holds the clauses of a select statement. This is
1061 // built by the parser.
1063 class Select_clauses
1065 public:
1066 Select_clauses()
1067 : 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
1080 // execute.
1081 void
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,
1088 location));
1091 size_t
1092 size() const
1093 { return this->clauses_.size(); }
1095 bool
1096 has_default() const;
1098 // Traverse the select clauses.
1100 traverse(Traverse*);
1102 // Lower statements.
1103 void
1104 lower(Gogo*, Named_object*, Block*, Temporary_statement*,
1105 Temporary_statement*, int* send_count, int* recv_count);
1107 // Determine types.
1108 void
1109 determine_types();
1111 // Check types.
1112 void
1113 check_types();
1115 // Whether the select clauses may fall through to the statement
1116 // which follows the overall select statement.
1117 bool
1118 may_fall_through() const;
1120 // Convert to the backend representation.
1121 Bstatement*
1122 get_backend(Translate_context*, Temporary_statement* index,
1123 Unnamed_label* break_label, Location);
1125 // Dump AST representation.
1126 void
1127 dump_clauses(Ast_dump_context*) const;
1129 // A single clause.
1130 class Select_clause
1132 public:
1133 Select_clause()
1134 : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
1135 closedvar_(NULL), statements_(NULL), is_send_(false),
1136 is_default_(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,
1142 Location location)
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.
1154 void
1155 lower(Gogo*, Named_object*, Block*, Temporary_statement*, int,
1156 Temporary_statement*);
1158 // Determine types.
1159 void
1160 determine_types();
1162 // Check types.
1163 void
1164 check_types();
1166 // Return true if this is the default clause.
1167 bool
1168 is_default() const
1169 { return this->is_default_; }
1171 // Return the channel. This will return NULL for the default
1172 // clause.
1173 Expression*
1174 channel() const
1175 { return this->channel_; }
1177 // Return true for a send, false for a receive.
1178 bool
1179 is_send() const
1181 go_assert(!this->is_default_);
1182 return this->is_send_;
1185 // Return the value to send or the lvalue to receive into.
1186 Expression*
1187 val() const
1188 { return this->val_; }
1190 // Return the lvalue to set to whether the channel is closed
1191 // on a receive.
1192 Expression*
1193 closed() const
1194 { return this->closed_; }
1196 // Return the variable to initialize, for "case a := <-ch".
1197 Named_object*
1198 var() const
1199 { return this->var_; }
1201 // Return the variable to initialize to whether the channel
1202 // is closed, for "case a, c := <-ch".
1203 Named_object*
1204 closedvar() const
1205 { return this->closedvar_; }
1207 // Return the statements.
1208 Block*
1209 statements() const
1210 { return this->statements_; }
1212 // Return the location.
1213 Location
1214 location() const
1215 { return this->location_; }
1217 // Return the case index for this clause.
1219 case_index() const
1221 go_assert(this->is_case_index_set_);
1222 return this->case_index_;
1225 // Set the case index.
1226 void
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.
1236 bool
1237 may_fall_through() const;
1239 // Convert the statements to the backend representation.
1240 Bstatement*
1241 get_statements_backend(Translate_context*);
1243 // Dump AST representation.
1244 void
1245 dump_clause(Ast_dump_context*) const;
1247 private:
1248 void
1249 lower_send(Block*, Expression*, Expression*);
1251 void
1252 lower_recv(Gogo*, Named_object*, Block*, Expression*, Expression*,
1253 Temporary_statement*);
1255 void
1256 set_case(Block*, Expression*, Expression*, Expression*);
1258 // The channel.
1259 Expression* channel_;
1260 // The value to send or the lvalue to receive into.
1261 Expression* val_;
1262 // The lvalue to set to whether the channel is closed on a
1263 // receive.
1264 Expression* closed_;
1265 // The variable to initialize, for "case a := <-ch".
1266 Named_object* var_;
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.
1271 Block* statements_;
1272 // The index of this clause in the switch statement. If
1273 // runtime.selectgo returns this index, this clause has been
1274 // chosen.
1275 int case_index_;
1276 // The location of this clause.
1277 Location location_;
1278 // Whether this is a send or a receive.
1279 bool is_send_;
1280 // Whether this is the default.
1281 bool is_default_;
1282 // Whether this has been lowered.
1283 bool is_lowered_;
1284 // Whether the case index has been set.
1285 bool is_case_index_set_;
1288 Select_clause&
1289 at(size_t i)
1290 { return this->clauses_.at(i); }
1292 private:
1293 typedef std::vector<Select_clause> Clauses;
1295 Clauses clauses_;
1298 // A select statement.
1300 class Select_statement : public Statement
1302 public:
1303 Select_statement(Location location)
1304 : Statement(STATEMENT_SELECT, location),
1305 clauses_(NULL), index_(NULL), break_label_(NULL), is_lowered_(false)
1308 // Add the clauses.
1309 void
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.
1317 Unnamed_label*
1318 break_label();
1320 protected:
1322 do_traverse(Traverse* traverse)
1323 { return this->clauses_->traverse(traverse); }
1325 Statement*
1326 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1328 void
1329 do_determine_types()
1330 { this->clauses_->determine_types(); }
1332 void
1333 do_check_types(Gogo*)
1334 { this->clauses_->check_types(); }
1336 bool
1337 do_may_fall_through() const;
1339 Bstatement*
1340 do_get_backend(Translate_context*);
1342 void
1343 do_dump_statement(Ast_dump_context*) const;
1345 private:
1346 // Lower a one-case select statement.
1347 Statement*
1348 lower_one_case(Block*);
1350 // Lower a two-case select statement with one defualt case.
1351 Statement*
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_;
1358 // The break label.
1359 Unnamed_label* break_label_;
1360 // Whether this statement has been lowered.
1361 bool is_lowered_;
1364 // A statement which requires a thunk: go or defer.
1366 class Thunk_statement : public Statement
1368 public:
1369 Thunk_statement(Statement_classification, Call_expression*,
1370 Location);
1372 // Return the call expression.
1373 Expression*
1374 call() const
1375 { return this->call_; }
1377 // Simplify a go or defer statement so that it only uses a single
1378 // parameter.
1379 bool
1380 simplify_statement(Gogo*, Named_object*, Block*);
1382 protected:
1384 do_traverse(Traverse* traverse);
1386 bool
1387 do_traverse_assignments(Traverse_assignments*);
1389 void
1390 do_determine_types();
1392 void
1393 do_check_types(Gogo*);
1395 // Return the function and argument for the call.
1396 bool
1397 get_fn_and_arg(Expression** pfn, Expression** parg);
1399 private:
1400 // Return whether this is a simple go statement.
1401 bool
1402 is_simple(Function_type*) const;
1404 // Return whether the thunk function is a constant.
1405 bool
1406 is_constant_function() const;
1408 // Build the struct to use for a complex case.
1409 Struct_type*
1410 build_struct(Function_type* fntype);
1412 // Build the thunk.
1413 void
1414 build_thunk(Gogo*, const std::string&, Struct_type*);
1416 // Set the name to use for thunk field N.
1417 void
1418 thunk_field_param(int n, char* buf, size_t buflen);
1420 // The function call to be executed in a separate thread (go) or
1421 // later (defer).
1422 Expression* call_;
1425 // A go statement.
1427 class Go_statement : public Thunk_statement
1429 public:
1430 Go_statement(Call_expression* call, Location location)
1431 : Thunk_statement(STATEMENT_GO, call, location)
1434 protected:
1435 Bstatement*
1436 do_get_backend(Translate_context*);
1438 void
1439 do_dump_statement(Ast_dump_context*) const;
1442 // A defer statement.
1444 class Defer_statement : public Thunk_statement
1446 public:
1447 Defer_statement(Call_expression* call, Location location)
1448 : Thunk_statement(STATEMENT_DEFER, call, location),
1449 on_stack_(false)
1452 void
1453 set_on_stack()
1454 { this->on_stack_ = true; }
1456 protected:
1457 Bstatement*
1458 do_get_backend(Translate_context*);
1460 void
1461 do_dump_statement(Ast_dump_context*) const;
1463 private:
1464 static Type*
1465 defer_struct_type();
1467 bool on_stack_;
1470 // A goto statement.
1472 class Goto_statement : public Statement
1474 public:
1475 Goto_statement(Label* label, Location location)
1476 : Statement(STATEMENT_GOTO, location),
1477 label_(label)
1480 // Return the label being jumped to.
1481 Label*
1482 label() const
1483 { return this->label_; }
1485 // Import a goto statement.
1486 static Statement*
1487 do_import(Import_function_body*, Location);
1489 protected:
1491 do_traverse(Traverse*);
1493 void
1494 do_check_types(Gogo*);
1496 bool
1497 do_may_fall_through() const
1498 { return false; }
1500 Bstatement*
1501 do_get_backend(Translate_context*);
1504 do_inlining_cost()
1505 { return 5; }
1507 void
1508 do_export_statement(Export_function_body*);
1510 void
1511 do_dump_statement(Ast_dump_context*) const;
1513 private:
1514 Label* label_;
1517 // A goto statement to an unnamed label.
1519 class Goto_unnamed_statement : public Statement
1521 public:
1522 Goto_unnamed_statement(Unnamed_label* label, Location location)
1523 : Statement(STATEMENT_GOTO_UNNAMED, location),
1524 label_(label)
1527 Unnamed_label*
1528 unnamed_label() const
1529 { return this->label_; }
1531 protected:
1533 do_traverse(Traverse*);
1535 bool
1536 do_may_fall_through() const
1537 { return false; }
1539 Bstatement*
1540 do_get_backend(Translate_context* context);
1543 do_inlining_cost()
1544 { return 5; }
1546 void
1547 do_export_statement(Export_function_body*);
1549 void
1550 do_dump_statement(Ast_dump_context*) const;
1552 private:
1553 Unnamed_label* label_;
1556 // A label statement.
1558 class Label_statement : public Statement
1560 public:
1561 Label_statement(Label* label, Location location)
1562 : Statement(STATEMENT_LABEL, location),
1563 label_(label)
1566 // Return the label itself.
1567 Label*
1568 label() const
1569 { return this->label_; }
1571 // Import a label or unnamed label.
1572 static Statement*
1573 do_import(Import_function_body*, Location);
1575 protected:
1577 do_traverse(Traverse*);
1579 Bstatement*
1580 do_get_backend(Translate_context*);
1583 do_inlining_cost()
1584 { return 1; }
1586 void
1587 do_export_statement(Export_function_body*);
1589 void
1590 do_dump_statement(Ast_dump_context*) const;
1592 private:
1593 // The label.
1594 Label* label_;
1597 // An unnamed label statement.
1599 class Unnamed_label_statement : public Statement
1601 public:
1602 Unnamed_label_statement(Unnamed_label* label);
1604 protected:
1606 do_traverse(Traverse*);
1608 Bstatement*
1609 do_get_backend(Translate_context* context);
1612 do_inlining_cost()
1613 { return 1; }
1615 void
1616 do_export_statement(Export_function_body*);
1618 void
1619 do_dump_statement(Ast_dump_context*) const;
1621 private:
1622 // The label.
1623 Unnamed_label* label_;
1626 // An if statement.
1628 class If_statement : public Statement
1630 public:
1631 If_statement(Expression* cond, Block* then_block, Block* else_block,
1632 Location location)
1633 : Statement(STATEMENT_IF, location),
1634 cond_(cond), then_block_(then_block), else_block_(else_block)
1637 Expression*
1638 condition() const
1639 { return this->cond_; }
1641 Block*
1642 then_block() const
1643 { return this->then_block_; }
1645 Block*
1646 else_block() const
1647 { return this->else_block_; }
1649 // Import an if statement.
1650 static Statement*
1651 do_import(Import_function_body*, Location);
1653 protected:
1655 do_traverse(Traverse*);
1657 void
1658 do_determine_types();
1660 void
1661 do_check_types(Gogo*);
1664 do_inlining_cost()
1665 { return 5; }
1667 void
1668 do_export_statement(Export_function_body*);
1670 bool
1671 do_may_fall_through() const;
1673 Bstatement*
1674 do_get_backend(Translate_context*);
1676 void
1677 do_dump_statement(Ast_dump_context*) const;
1679 private:
1680 Expression* cond_;
1681 Block* then_block_;
1682 Block* else_block_;
1685 // A for statement.
1687 class For_statement : public Statement
1689 public:
1690 For_statement(Block* init, Expression* cond, Block* post,
1691 Location location)
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.
1698 void
1699 add_statements(Block* statements)
1701 go_assert(this->statements_ == NULL);
1702 this->statements_ = statements;
1705 // Return the break label for this for statement.
1706 Unnamed_label*
1707 break_label();
1709 // Return the continue label for this for statement.
1710 Unnamed_label*
1711 continue_label();
1713 // Set the break and continue labels for this statement.
1714 void
1715 set_break_continue_labels(Unnamed_label* break_label,
1716 Unnamed_label* continue_label);
1718 protected:
1720 do_traverse(Traverse*);
1722 bool
1723 do_traverse_assignments(Traverse_assignments*)
1724 { go_unreachable(); }
1726 Statement*
1727 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1729 bool
1730 do_may_fall_through() const;
1732 Bstatement*
1733 do_get_backend(Translate_context*)
1734 { go_unreachable(); }
1736 void
1737 do_dump_statement(Ast_dump_context*) const;
1739 private:
1740 // The initialization statements. This may be NULL.
1741 Block* init_;
1742 // The condition. This may be NULL.
1743 Expression* cond_;
1744 // The statements to run after each iteration. This may be NULL.
1745 Block* post_;
1746 // The statements in the loop itself.
1747 Block* statements_;
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
1758 public:
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.
1767 void
1768 add_statements(Block* statements)
1770 go_assert(this->statements_ == NULL);
1771 this->statements_ = statements;
1774 // Return the break label for this for statement.
1775 Unnamed_label*
1776 break_label();
1778 // Return the continue label for this for statement.
1779 Unnamed_label*
1780 continue_label();
1782 protected:
1784 do_traverse(Traverse*);
1786 bool
1787 do_traverse_assignments(Traverse_assignments*)
1788 { go_unreachable(); }
1790 Statement*
1791 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1793 Bstatement*
1794 do_get_backend(Translate_context*)
1795 { go_unreachable(); }
1797 void
1798 do_dump_statement(Ast_dump_context*) const;
1800 private:
1801 Expression*
1802 make_range_ref(Named_object*, Temporary_statement*, Location);
1804 Call_expression*
1805 call_builtin(Gogo*, const char* funcname, Expression* arg, Location);
1807 void
1808 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1809 Temporary_statement*, Temporary_statement*,
1810 Block**, Expression**, Block**, Block**);
1812 void
1813 lower_range_slice(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1814 Temporary_statement*, Temporary_statement*,
1815 Block**, Expression**, Block**, Block**);
1817 void
1818 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1819 Temporary_statement*, Temporary_statement*,
1820 Block**, Expression**, Block**, Block**);
1822 void
1823 lower_range_map(Gogo*, Map_type*, Block*, Block*, Named_object*,
1824 Temporary_statement*, Temporary_statement*,
1825 Temporary_statement*, Block**, Expression**, Block**,
1826 Block**);
1828 void
1829 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1830 Temporary_statement*, Temporary_statement*,
1831 Temporary_statement*, Block**, Expression**, Block**,
1832 Block**);
1834 Statement*
1835 lower_map_range_clear(Type*, Block*, Expression*, Named_object*,
1836 Temporary_statement*, Location);
1838 Statement*
1839 lower_array_range_clear(Gogo*, Type*, Expression*, Block*,
1840 Named_object*, Temporary_statement*,
1841 Location);
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
1846 // NULL.
1847 Expression* value_var_;
1848 // The expression we are ranging over.
1849 Expression* range_;
1850 // The statements in the block.
1851 Block* statements_;
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.
1861 class Case_clauses
1863 public:
1864 Case_clauses()
1865 : clauses_()
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
1872 // next clause.
1873 void
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.
1882 bool
1883 empty() const
1884 { return this->clauses_.empty(); }
1886 // Traverse the case clauses.
1888 traverse(Traverse*);
1890 // Lower for a nonconstant switch.
1891 void
1892 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1894 // Determine types of expressions. The Type parameter is the type
1895 // of the switch value.
1896 void
1897 determine_types(Type*);
1899 // Check types. The Type parameter is the type of the switch value.
1900 bool
1901 check_types(Type*);
1903 // Return true if all the clauses are constant values.
1904 bool
1905 is_constant() const;
1907 // Return true if these clauses may fall through to the statements
1908 // following the switch statement.
1909 bool
1910 may_fall_through() const;
1912 // Return the body of a SWITCH_EXPR when all the clauses are
1913 // constants.
1914 void
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.
1920 void
1921 dump_clauses(Ast_dump_context*) const;
1923 private:
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;
1931 // One case clause.
1932 class Case_clause
1934 public:
1935 Case_clause()
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.
1947 bool
1948 is_fallthrough() const
1949 { return this->is_fallthrough_; }
1951 // Whether this is the default.
1952 bool
1953 is_default() const
1954 { return this->is_default_; }
1956 // The location of this clause.
1957 Location
1958 location() const
1959 { return this->location_; }
1961 // Traversal.
1963 traverse(Traverse*);
1965 // Lower for a nonconstant switch.
1966 void
1967 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1969 // Determine types.
1970 void
1971 determine_types(Type*);
1973 // Check types.
1974 bool
1975 check_types(Type*);
1977 // Return true if all the case expressions are constant.
1978 bool
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.
1984 bool
1985 may_fall_through() const;
1987 // Convert the case values and statements to the backend
1988 // representation.
1989 Bstatement*
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.
1994 void
1995 dump_clause(Ast_dump_context*) const;
1997 private:
1998 // The list of case expressions.
1999 Expression_list* cases_;
2000 // The statements to execute.
2001 Block* statements_;
2002 // Whether this is the default case.
2003 bool is_default_;
2004 // Whether this falls through after the statements.
2005 bool is_fallthrough_;
2006 // The location of this case clause.
2007 Location location_;
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.
2016 Clauses clauses_;
2019 // A switch statement.
2021 class Switch_statement : public Statement
2023 public:
2024 Switch_statement(Expression* val, Location location)
2025 : Statement(STATEMENT_SWITCH, location),
2026 val_(val), clauses_(NULL), break_label_(NULL)
2029 // Add the clauses.
2030 void
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.
2038 Unnamed_label*
2039 break_label();
2041 protected:
2043 do_traverse(Traverse*);
2045 Statement*
2046 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
2048 Bstatement*
2049 do_get_backend(Translate_context*)
2050 { go_unreachable(); }
2052 void
2053 do_dump_statement(Ast_dump_context*) const;
2055 bool
2056 do_may_fall_through() const;
2058 private:
2059 // The value to switch on. This may be NULL.
2060 Expression* val_;
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
2072 public:
2073 Type_case_clauses()
2074 : 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.
2082 void
2083 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
2084 Location location)
2086 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
2087 statements, location));
2090 // Return whether there are no clauses.
2091 bool
2092 empty() const
2093 { return this->clauses_.empty(); }
2095 // Traverse the type case clauses.
2097 traverse(Traverse*);
2099 // Check for duplicates.
2100 void
2101 check_duplicates() const;
2103 // Lower to if and goto statements.
2104 void
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.
2110 bool
2111 may_fall_through() const;
2113 // Dump the AST representation to a dump context.
2114 void
2115 dump_clauses(Ast_dump_context*) const;
2117 private:
2118 // One type case clause.
2119 class Type_case_clause
2121 public:
2122 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)
2133 // The type.
2134 Type*
2135 type() const
2136 { return this->type_; }
2138 // Whether this is the default.
2139 bool
2140 is_default() const
2141 { return this->is_default_; }
2143 // The location of this type clause.
2144 Location
2145 location() const
2146 { return this->location_; }
2148 // Traversal.
2150 traverse(Traverse*);
2152 // Lower to if and goto statements.
2153 void
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.
2160 bool
2161 may_fall_through() const;
2163 // Dump the AST representation to a dump context.
2164 void
2165 dump_clause(Ast_dump_context*) const;
2167 private:
2168 // The type for this type clause.
2169 Type* type_;
2170 // The statements to execute.
2171 Block* statements_;
2172 // Whether this falls through--this is true for "case T1, T2".
2173 bool is_fallthrough_;
2174 // Whether this is the default case.
2175 bool is_default_;
2176 // The location of this type case clause.
2177 Location location_;
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
2193 public:
2194 Type_switch_statement(const std::string& name, Expression* expr,
2195 Location location)
2196 : Statement(STATEMENT_TYPE_SWITCH, location),
2197 name_(name), expr_(expr), clauses_(NULL), break_label_(NULL)
2200 // Add the clauses.
2201 void
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.
2209 Unnamed_label*
2210 break_label();
2212 protected:
2214 do_traverse(Traverse*);
2216 Statement*
2217 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
2219 Bstatement*
2220 do_get_backend(Translate_context*)
2221 { go_unreachable(); }
2223 void
2224 do_dump_statement(Ast_dump_context*) const;
2226 bool
2227 do_may_fall_through() const;
2229 private:
2230 // The name of the variable declared in the type switch guard. Empty if there
2231 // is no variable declared.
2232 std::string name_;
2233 // The expression we are switching on if there is no variable.
2234 Expression* expr_;
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)