]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/go/gofrontend/statements.h
compiler: improve escape analysis on interface conversions
[thirdparty/gcc.git] / gcc / go / gofrontend / statements.h
1 // statements.h -- Go frontend statements. -*- C++ -*-
2
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.
6
7 #ifndef GO_STATEMENTS_H
8 #define GO_STATEMENTS_H
9
10 #include "operator.h"
11
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 Goto_statement;
28 class Goto_unnamed_statement;
29 class Label_statement;
30 class Unnamed_label_statement;
31 class If_statement;
32 class For_statement;
33 class For_range_statement;
34 class Switch_statement;
35 class Type_switch_statement;
36 class Send_statement;
37 class Select_statement;
38 class Variable;
39 class Named_object;
40 class Label;
41 class Translate_context;
42 class Expression;
43 class Expression_list;
44 class Struct_type;
45 class Call_expression;
46 class Map_index_expression;
47 class Receive_expression;
48 class Case_clauses;
49 class Type_case_clauses;
50 class Select_clauses;
51 class Typed_identifier_list;
52 class Bexpression;
53 class Bstatement;
54 class Bvariable;
55 class Ast_dump_context;
56
57 // This class is used to traverse assignments made by a statement
58 // which makes assignments.
59
60 class Traverse_assignments
61 {
62 public:
63 Traverse_assignments()
64 { }
65
66 virtual ~Traverse_assignments()
67 { }
68
69 // This is called for a variable initialization.
70 virtual void
71 initialize_variable(Named_object*) = 0;
72
73 // This is called for each assignment made by the statement. PLHS
74 // points to the left hand side, and PRHS points to the right hand
75 // side. PRHS may be NULL if there is no associated expression, as
76 // in the bool set by a non-blocking receive.
77 virtual void
78 assignment(Expression** plhs, Expression** prhs) = 0;
79
80 // This is called for each expression which is not passed to the
81 // assignment function. This is used for some of the statements
82 // which assign two values, for which there is no expression which
83 // describes the value. For ++ and -- the value is passed to both
84 // the assignment method and the rhs method. IS_STORED is true if
85 // this value is being stored directly. It is false if the value is
86 // computed but not stored. IS_LOCAL is true if the value is being
87 // stored in a local variable or this is being called by a return
88 // statement.
89 virtual void
90 value(Expression**, bool is_stored, bool is_local) = 0;
91 };
92
93 // A single statement.
94
95 class Statement
96 {
97 public:
98 // The types of statements.
99 enum Statement_classification
100 {
101 STATEMENT_ERROR,
102 STATEMENT_VARIABLE_DECLARATION,
103 STATEMENT_TEMPORARY,
104 STATEMENT_ASSIGNMENT,
105 STATEMENT_EXPRESSION,
106 STATEMENT_BLOCK,
107 STATEMENT_GO,
108 STATEMENT_DEFER,
109 STATEMENT_RETURN,
110 STATEMENT_BREAK_OR_CONTINUE,
111 STATEMENT_GOTO,
112 STATEMENT_GOTO_UNNAMED,
113 STATEMENT_LABEL,
114 STATEMENT_UNNAMED_LABEL,
115 STATEMENT_IF,
116 STATEMENT_CONSTANT_SWITCH,
117 STATEMENT_SEND,
118 STATEMENT_SELECT,
119
120 // These statements types are created by the parser, but they
121 // disappear during the lowering pass.
122 STATEMENT_ASSIGNMENT_OPERATION,
123 STATEMENT_TUPLE_ASSIGNMENT,
124 STATEMENT_TUPLE_MAP_ASSIGNMENT,
125 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT,
126 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT,
127 STATEMENT_INCDEC,
128 STATEMENT_FOR,
129 STATEMENT_FOR_RANGE,
130 STATEMENT_SWITCH,
131 STATEMENT_TYPE_SWITCH
132 };
133
134 Statement(Statement_classification, Location);
135
136 virtual ~Statement();
137
138 // Make a variable declaration.
139 static Statement*
140 make_variable_declaration(Named_object*);
141
142 // Make a statement which creates a temporary variable and
143 // initializes it to an expression. The block is used if the
144 // temporary variable has to be explicitly destroyed; the variable
145 // must still be added to the block. References to the temporary
146 // variable may be constructed using make_temporary_reference.
147 // Either the type or the initialization expression may be NULL, but
148 // not both.
149 static Temporary_statement*
150 make_temporary(Type*, Expression*, Location);
151
152 // Make an assignment statement.
153 static Assignment_statement*
154 make_assignment(Expression*, Expression*, Location);
155
156 // Make an assignment operation (+=, etc.).
157 static Statement*
158 make_assignment_operation(Operator, Expression*, Expression*,
159 Location);
160
161 // Make a tuple assignment statement.
162 static Statement*
163 make_tuple_assignment(Expression_list*, Expression_list*, Location);
164
165 // Make an assignment from a map index to a pair of variables.
166 static Statement*
167 make_tuple_map_assignment(Expression* val, Expression* present,
168 Expression*, Location);
169
170 // Make an assignment from a nonblocking receive to a pair of
171 // variables.
172 static Statement*
173 make_tuple_receive_assignment(Expression* val, Expression* closed,
174 Expression* channel, Location);
175
176 // Make an assignment from a type guard to a pair of variables.
177 static Statement*
178 make_tuple_type_guard_assignment(Expression* val, Expression* ok,
179 Expression* expr, Type* type,
180 Location);
181
182 // Make an expression statement from an Expression. IS_IGNORED is
183 // true if the value is being explicitly ignored, as in an
184 // assignment to _.
185 static Statement*
186 make_statement(Expression*, bool is_ignored);
187
188 // Make a block statement from a Block. This is an embedded list of
189 // statements which may also include variable definitions.
190 static Statement*
191 make_block_statement(Block*, Location);
192
193 // Make an increment statement.
194 static Statement*
195 make_inc_statement(Expression*);
196
197 // Make a decrement statement.
198 static Statement*
199 make_dec_statement(Expression*);
200
201 // Make a go statement.
202 static Statement*
203 make_go_statement(Call_expression* call, Location);
204
205 // Make a defer statement.
206 static Statement*
207 make_defer_statement(Call_expression* call, Location);
208
209 // Make a return statement.
210 static Return_statement*
211 make_return_statement(Expression_list*, Location);
212
213 // Make a statement that returns the result of a call expression.
214 // If the call does not return any results, this just returns the
215 // call expression as a statement, assuming that the function will
216 // end immediately afterward.
217 static Statement*
218 make_return_from_call(Call_expression*, Location);
219
220 // Make a break statement.
221 static Statement*
222 make_break_statement(Unnamed_label* label, Location);
223
224 // Make a continue statement.
225 static Statement*
226 make_continue_statement(Unnamed_label* label, Location);
227
228 // Make a goto statement.
229 static Statement*
230 make_goto_statement(Label* label, Location);
231
232 // Make a goto statement to an unnamed label.
233 static Statement*
234 make_goto_unnamed_statement(Unnamed_label* label, Location);
235
236 // Make a label statement--where the label is defined.
237 static Statement*
238 make_label_statement(Label* label, Location);
239
240 // Make an unnamed label statement--where the label is defined.
241 static Statement*
242 make_unnamed_label_statement(Unnamed_label* label);
243
244 // Make an if statement.
245 static Statement*
246 make_if_statement(Expression* cond, Block* then_block, Block* else_block,
247 Location);
248
249 // Make a switch statement.
250 static Switch_statement*
251 make_switch_statement(Expression* switch_val, Location);
252
253 // Make a type switch statement.
254 static Type_switch_statement*
255 make_type_switch_statement(const std::string&, Expression*, Location);
256
257 // Make a send statement.
258 static Send_statement*
259 make_send_statement(Expression* channel, Expression* val, Location);
260
261 // Make a select statement.
262 static Select_statement*
263 make_select_statement(Location);
264
265 // Make a for statement.
266 static For_statement*
267 make_for_statement(Block* init, Expression* cond, Block* post,
268 Location location);
269
270 // Make a for statement with a range clause.
271 static For_range_statement*
272 make_for_range_statement(Expression* index_var, Expression* value_var,
273 Expression* range, Location);
274
275 // Return the statement classification.
276 Statement_classification
277 classification() const
278 { return this->classification_; }
279
280 // Get the statement location.
281 Location
282 location() const
283 { return this->location_; }
284
285 // Traverse the tree.
286 int
287 traverse(Block*, size_t* index, Traverse*);
288
289 // Traverse the contents of this statement--the expressions and
290 // statements which it contains.
291 int
292 traverse_contents(Traverse*);
293
294 // If this statement assigns some values, it calls a function for
295 // each value to which this statement assigns a value, and returns
296 // true. If this statement does not assign any values, it returns
297 // false.
298 bool
299 traverse_assignments(Traverse_assignments* tassign);
300
301 // Lower a statement. This is called immediately after parsing to
302 // simplify statements for further processing. It returns the same
303 // Statement or a new one. FUNCTION is the function containing this
304 // statement. BLOCK is the block containing this statement.
305 // INSERTER can be used to insert new statements before this one.
306 Statement*
307 lower(Gogo* gogo, Named_object* function, Block* block,
308 Statement_inserter* inserter)
309 { return this->do_lower(gogo, function, block, inserter); }
310
311 // Flatten a statement. This is called immediately after the order of
312 // evaluation rules are applied to statements. It returns the same
313 // Statement or a new one. FUNCTION is the function containing this
314 // statement. BLOCK is the block containing this statement.
315 // INSERTER can be used to insert new statements before this one.
316 Statement*
317 flatten(Gogo* gogo, Named_object* function, Block* block,
318 Statement_inserter* inserter)
319 { return this->do_flatten(gogo, function, block, inserter); }
320
321 // Set type information for unnamed constants.
322 void
323 determine_types();
324
325 // Check types in a statement. This simply checks that any
326 // expressions used by the statement have the right type.
327 void
328 check_types(Gogo* gogo)
329 { this->do_check_types(gogo); }
330
331 // Return the cost of this statement for inlining purposes.
332 int
333 inlining_cost()
334 { return this->do_inlining_cost(); }
335
336 // Export data for this statement to BODY.
337 void
338 export_statement(Export_function_body* efb)
339 { this->do_export_statement(efb); }
340
341 // Make implicit type conversions explicit.
342 void
343 add_conversions()
344 { this->do_add_conversions(); }
345
346 // Read a statement from export data. The location should be used
347 // for the returned statement. Errors should be reported using the
348 // Import_function_body's location method.
349 static Statement*
350 import_statement(Import_function_body*, Location);
351
352 // Return whether this is a block statement.
353 bool
354 is_block_statement() const
355 { return this->classification_ == STATEMENT_BLOCK; }
356
357 // If this is an assignment statement, return it. Otherwise return
358 // NULL.
359 Assignment_statement*
360 assignment_statement()
361 {
362 return this->convert<Assignment_statement, STATEMENT_ASSIGNMENT>();
363 }
364
365 // If this is an temporary statement, return it. Otherwise return
366 // NULL.
367 Temporary_statement*
368 temporary_statement()
369 {
370 return this->convert<Temporary_statement, STATEMENT_TEMPORARY>();
371 }
372
373 // If this is a variable declaration statement, return it.
374 // Otherwise return NULL.
375 Variable_declaration_statement*
376 variable_declaration_statement()
377 {
378 return this->convert<Variable_declaration_statement,
379 STATEMENT_VARIABLE_DECLARATION>();
380 }
381
382 // If this is an expression statement, return it. Otherwise return
383 // NULL.
384 Expression_statement*
385 expression_statement()
386 {
387 return this->convert<Expression_statement, STATEMENT_EXPRESSION>();
388 }
389
390 // If this is an block statement, return it. Otherwise return
391 // NULL.
392 Block_statement*
393 block_statement()
394 { return this->convert<Block_statement, STATEMENT_BLOCK>(); }
395
396 // If this is a return statement, return it. Otherwise return NULL.
397 Return_statement*
398 return_statement()
399 { return this->convert<Return_statement, STATEMENT_RETURN>(); }
400
401 // If this is a thunk statement (a go or defer statement), return
402 // it. Otherwise return NULL.
403 Thunk_statement*
404 thunk_statement();
405
406 // If this is a goto statement, return it. Otherwise return NULL.
407 Goto_statement*
408 goto_statement()
409 { return this->convert<Goto_statement, STATEMENT_GOTO>(); }
410
411 // If this is a goto_unnamed statement, return it. Otherwise return NULL.
412 Goto_unnamed_statement*
413 goto_unnamed_statement()
414 { return this->convert<Goto_unnamed_statement, STATEMENT_GOTO_UNNAMED>(); }
415
416 // If this is a label statement, return it. Otherwise return NULL.
417 Label_statement*
418 label_statement()
419 { return this->convert<Label_statement, STATEMENT_LABEL>(); }
420
421 // If this is an unnamed_label statement, return it. Otherwise return NULL.
422 Unnamed_label_statement*
423 unnamed_label_statement()
424 { return this->convert<Unnamed_label_statement, STATEMENT_UNNAMED_LABEL>(); }
425
426 // If this is an if statement, return it. Otherwise return NULL.
427 If_statement*
428 if_statement()
429 { return this->convert<If_statement, STATEMENT_IF>(); }
430
431 // If this is a for statement, return it. Otherwise return NULL.
432 For_statement*
433 for_statement()
434 { return this->convert<For_statement, STATEMENT_FOR>(); }
435
436 // If this is a for statement over a range clause, return it.
437 // Otherwise return NULL.
438 For_range_statement*
439 for_range_statement()
440 { return this->convert<For_range_statement, STATEMENT_FOR_RANGE>(); }
441
442 // If this is a switch statement, return it. Otherwise return NULL.
443 Switch_statement*
444 switch_statement()
445 { return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
446
447 // If this is a type switch statement, return it. Otherwise return
448 // NULL.
449 Type_switch_statement*
450 type_switch_statement()
451 { return this->convert<Type_switch_statement, STATEMENT_TYPE_SWITCH>(); }
452
453 // If this is a send statement, return it. Otherwise return NULL.
454 Send_statement*
455 send_statement()
456 { return this->convert<Send_statement, STATEMENT_SEND>(); }
457
458 // If this is a select statement, return it. Otherwise return NULL.
459 Select_statement*
460 select_statement()
461 { return this->convert<Select_statement, STATEMENT_SELECT>(); }
462
463 // Return true if this statement may fall through--if after
464 // executing this statement we may go on to execute the following
465 // statement, if any.
466 bool
467 may_fall_through() const
468 { return this->do_may_fall_through(); }
469
470 // Convert the statement to the backend representation.
471 Bstatement*
472 get_backend(Translate_context*);
473
474 // Dump AST representation of a statement to a dump context.
475 void
476 dump_statement(Ast_dump_context*) const;
477
478 protected:
479 // Implemented by child class: traverse the tree.
480 virtual int
481 do_traverse(Traverse*) = 0;
482
483 // Implemented by child class: traverse assignments. Any statement
484 // which includes an assignment should implement this.
485 virtual bool
486 do_traverse_assignments(Traverse_assignments*)
487 { return false; }
488
489 // Implemented by the child class: lower this statement to a simpler
490 // one.
491 virtual Statement*
492 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
493 { return this; }
494
495 // Implemented by the child class: lower this statement to a simpler
496 // one.
497 virtual Statement*
498 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*)
499 { return this; }
500
501 // Implemented by child class: set type information for unnamed
502 // constants. Any statement which includes an expression needs to
503 // implement this.
504 virtual void
505 do_determine_types()
506 { }
507
508 // Implemented by child class: check types of expressions used in a
509 // statement.
510 virtual void
511 do_check_types(Gogo*)
512 { }
513
514 // Implemented by child class: return the cost of this statement for
515 // inlining. The default cost is high, so we only need to define
516 // this method for statements that can be inlined.
517 virtual int
518 do_inlining_cost()
519 { return 0x100000; }
520
521 // Implemented by child class: write export data for this statement
522 // to the string. This need only be implemented by classes that
523 // implement do_inlining_cost with a reasonable value.
524 virtual void
525 do_export_statement(Export_function_body*)
526 { go_unreachable(); }
527
528 // Implemented by child class: return true if this statement may
529 // fall through.
530 virtual bool
531 do_may_fall_through() const
532 { return true; }
533
534 // Implemented by child class: convert to backend representation.
535 virtual Bstatement*
536 do_get_backend(Translate_context*) = 0;
537
538 // Implemented by child class: dump ast representation.
539 virtual void
540 do_dump_statement(Ast_dump_context*) const = 0;
541
542 // Implemented by child class: make implicit conversions explicit.
543 virtual void
544 do_add_conversions()
545 { }
546
547 // Traverse an expression in a statement.
548 int
549 traverse_expression(Traverse*, Expression**);
550
551 // Traverse an expression list in a statement. The Expression_list
552 // may be NULL.
553 int
554 traverse_expression_list(Traverse*, Expression_list*);
555
556 // Traverse a type in a statement.
557 int
558 traverse_type(Traverse*, Type*);
559
560 // For children to call when they detect that they are in error.
561 void
562 set_is_error();
563
564 // For children to call to report an error conveniently.
565 void
566 report_error(const char*);
567
568 // For children to return an error statement from lower().
569 static Statement*
570 make_error_statement(Location);
571
572 private:
573 // Convert to the desired statement classification, or return NULL.
574 // This is a controlled dynamic cast.
575 template<typename Statement_class, Statement_classification sc>
576 Statement_class*
577 convert()
578 {
579 return (this->classification_ == sc
580 ? static_cast<Statement_class*>(this)
581 : NULL);
582 }
583
584 template<typename Statement_class, Statement_classification sc>
585 const Statement_class*
586 convert() const
587 {
588 return (this->classification_ == sc
589 ? static_cast<const Statement_class*>(this)
590 : NULL);
591 }
592
593 // The statement classification.
594 Statement_classification classification_;
595 // The location in the input file of the start of this statement.
596 Location location_;
597 };
598
599 // An assignment statement.
600
601 class Assignment_statement : public Statement
602 {
603 public:
604 Assignment_statement(Expression* lhs, Expression* rhs,
605 Location location)
606 : Statement(STATEMENT_ASSIGNMENT, location),
607 lhs_(lhs), rhs_(rhs), omit_write_barrier_(false)
608 { }
609
610 Expression*
611 lhs() const
612 { return this->lhs_; }
613
614 Expression*
615 rhs() const
616 { return this->rhs_; }
617
618 bool
619 omit_write_barrier() const
620 { return this->omit_write_barrier_; }
621
622 void
623 set_omit_write_barrier()
624 { this->omit_write_barrier_ = true; }
625
626 protected:
627 int
628 do_traverse(Traverse* traverse);
629
630 bool
631 do_traverse_assignments(Traverse_assignments*);
632
633 virtual Statement*
634 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
635
636 void
637 do_determine_types();
638
639 void
640 do_check_types(Gogo*);
641
642 int
643 do_inlining_cost()
644 { return 1; }
645
646 void
647 do_export_statement(Export_function_body*);
648
649 Statement*
650 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
651
652 Bstatement*
653 do_get_backend(Translate_context*);
654
655 void
656 do_dump_statement(Ast_dump_context*) const;
657
658 void
659 do_add_conversions();
660
661 private:
662 // Left hand side--the lvalue.
663 Expression* lhs_;
664 // Right hand side--the rvalue.
665 Expression* rhs_;
666 // True if we can omit a write barrier from this assignment.
667 bool omit_write_barrier_;
668 };
669
670 // A statement which creates and initializes a temporary variable.
671
672 class Temporary_statement : public Statement
673 {
674 public:
675 Temporary_statement(Type* type, Expression* init, Location location)
676 : Statement(STATEMENT_TEMPORARY, location),
677 type_(type), init_(init), bvariable_(NULL), is_address_taken_(false),
678 value_escapes_(false)
679 { }
680
681 // Return the type of the temporary variable.
682 Type*
683 type() const;
684
685 // Return the initializer if there is one.
686 Expression*
687 init() const
688 { return this->init_; }
689
690 // Record that something takes the address of this temporary
691 // variable.
692 void
693 set_is_address_taken()
694 { this->is_address_taken_ = true; }
695
696 // Whether the value escapes.
697 bool
698 value_escapes() const
699 { return this->value_escapes_; }
700
701 // Record that the value escapes.
702 void
703 set_value_escapes()
704 { this->value_escapes_ = true; }
705
706 // Return the temporary variable. This should not be called until
707 // after the statement itself has been converted.
708 Bvariable*
709 get_backend_variable(Translate_context*) const;
710
711 protected:
712 int
713 do_traverse(Traverse*);
714
715 bool
716 do_traverse_assignments(Traverse_assignments*);
717
718 void
719 do_determine_types();
720
721 void
722 do_check_types(Gogo*);
723
724 Statement*
725 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
726
727 Bstatement*
728 do_get_backend(Translate_context*);
729
730 void
731 do_dump_statement(Ast_dump_context*) const;
732
733 void
734 do_add_conversions();
735
736 private:
737 // The type of the temporary variable.
738 Type* type_;
739 // The initial value of the temporary variable. This may be NULL.
740 Expression* init_;
741 // The backend representation of the temporary variable.
742 Bvariable* bvariable_;
743 // True if something takes the address of this temporary variable.
744 bool is_address_taken_;
745 // True if the value assigned to this temporary variable escapes.
746 // This is used for select statements.
747 bool value_escapes_;
748 };
749
750 // A variable declaration. This marks the point in the code where a
751 // variable is declared. The Variable is also attached to a Block.
752
753 class Variable_declaration_statement : public Statement
754 {
755 public:
756 Variable_declaration_statement(Named_object* var);
757
758 // The variable being declared.
759 Named_object*
760 var()
761 { return this->var_; }
762
763 // Import a variable declaration.
764 static Statement*
765 do_import(Import_function_body*, Location);
766
767 protected:
768 int
769 do_traverse(Traverse*);
770
771 bool
772 do_traverse_assignments(Traverse_assignments*);
773
774 Statement*
775 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
776
777 int
778 do_inlining_cost()
779 { return 1; }
780
781 void
782 do_export_statement(Export_function_body*);
783
784 Statement*
785 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
786
787 Bstatement*
788 do_get_backend(Translate_context*);
789
790 void
791 do_dump_statement(Ast_dump_context*) const;
792
793 void
794 do_add_conversions();
795
796 private:
797 Named_object* var_;
798 };
799
800 // A return statement.
801
802 class Return_statement : public Statement
803 {
804 public:
805 Return_statement(Expression_list* vals, Location location)
806 : Statement(STATEMENT_RETURN, location),
807 vals_(vals), is_lowered_(false)
808 { }
809
810 // The list of values being returned. This may be NULL.
811 const Expression_list*
812 vals() const
813 { return this->vals_; }
814
815 protected:
816 int
817 do_traverse(Traverse* traverse)
818 { return this->traverse_expression_list(traverse, this->vals_); }
819
820 bool
821 do_traverse_assignments(Traverse_assignments*);
822
823 Statement*
824 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
825
826 bool
827 do_may_fall_through() const
828 { return false; }
829
830 int
831 do_inlining_cost()
832 { return 1; }
833
834 void
835 do_export_statement(Export_function_body*);
836
837 Bstatement*
838 do_get_backend(Translate_context*);
839
840 void
841 do_dump_statement(Ast_dump_context*) const;
842
843 private:
844 // Return values. This may be NULL.
845 Expression_list* vals_;
846 // True if this statement has been lowered.
847 bool is_lowered_;
848 };
849
850 // An expression statement.
851
852 class Expression_statement : public Statement
853 {
854 public:
855 Expression_statement(Expression* expr, bool is_ignored);
856
857 Expression*
858 expr()
859 { return this->expr_; }
860
861 protected:
862 int
863 do_traverse(Traverse* traverse)
864 { return this->traverse_expression(traverse, &this->expr_); }
865
866 void
867 do_determine_types();
868
869 void
870 do_check_types(Gogo*);
871
872 bool
873 do_may_fall_through() const;
874
875 Bstatement*
876 do_get_backend(Translate_context* context);
877
878 void
879 do_dump_statement(Ast_dump_context*) const;
880
881 private:
882 Expression* expr_;
883 // Whether the value of this expression is being explicitly ignored.
884 bool is_ignored_;
885 };
886
887 // A block statement--a list of statements which may include variable
888 // definitions.
889
890 class Block_statement : public Statement
891 {
892 public:
893 Block_statement(Block* block, Location location)
894 : Statement(STATEMENT_BLOCK, location),
895 block_(block), is_lowered_for_statement_(false)
896 { }
897
898 // Return the actual block.
899 Block*
900 block() const
901 { return this->block_; }
902
903 void
904 set_is_lowered_for_statement()
905 { this->is_lowered_for_statement_ = true; }
906
907 bool
908 is_lowered_for_statement()
909 { return this->is_lowered_for_statement_; }
910
911 protected:
912 int
913 do_traverse(Traverse* traverse)
914 { return this->block_->traverse(traverse); }
915
916 void
917 do_determine_types()
918 { this->block_->determine_types(); }
919
920 int
921 do_inlining_cost()
922 { return 0; }
923
924 void
925 do_export_statement(Export_function_body*);
926
927 bool
928 do_may_fall_through() const
929 { return this->block_->may_fall_through(); }
930
931 Bstatement*
932 do_get_backend(Translate_context* context);
933
934 void
935 do_dump_statement(Ast_dump_context*) const;
936
937 private:
938 Block* block_;
939 // True if this block statement represents a lowered for statement.
940 bool is_lowered_for_statement_;
941 };
942
943 // A send statement.
944
945 class Send_statement : public Statement
946 {
947 public:
948 Send_statement(Expression* channel, Expression* val,
949 Location location)
950 : Statement(STATEMENT_SEND, location),
951 channel_(channel), val_(val)
952 { }
953
954 Expression*
955 channel()
956 { return this->channel_; }
957
958 Expression*
959 val()
960 { return this->val_; }
961
962 protected:
963 int
964 do_traverse(Traverse* traverse);
965
966 void
967 do_determine_types();
968
969 void
970 do_check_types(Gogo*);
971
972 Statement*
973 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
974
975 Bstatement*
976 do_get_backend(Translate_context*);
977
978 void
979 do_dump_statement(Ast_dump_context*) const;
980
981 void
982 do_add_conversions();
983
984 private:
985 // The channel on which to send the value.
986 Expression* channel_;
987 // The value to send.
988 Expression* val_;
989 };
990
991 // Select_clauses holds the clauses of a select statement. This is
992 // built by the parser.
993
994 class Select_clauses
995 {
996 public:
997 Select_clauses()
998 : clauses_()
999 { }
1000
1001 // Add a new clause. IS_SEND is true if this is a send clause,
1002 // false for a receive clause. For a send clause CHANNEL is the
1003 // channel and VAL is the value to send. For a receive clause
1004 // CHANNEL is the channel, VAL is either NULL or a Var_expression
1005 // for the variable to set, and CLOSED is either NULL or a
1006 // Var_expression to set to whether the channel is closed. If VAL
1007 // is NULL, VAR may be a variable to be initialized with the
1008 // received value, and CLOSEDVAR ma be a variable to be initialized
1009 // with whether the channel is closed. IS_DEFAULT is true if this
1010 // is the default clause. STATEMENTS is the list of statements to
1011 // execute.
1012 void
1013 add(bool is_send, Expression* channel, Expression* val, Expression* closed,
1014 Named_object* var, Named_object* closedvar, bool is_default,
1015 Block* statements, Location location)
1016 {
1017 this->clauses_.push_back(Select_clause(is_send, channel, val, closed, var,
1018 closedvar, is_default, statements,
1019 location));
1020 }
1021
1022 size_t
1023 size() const
1024 { return this->clauses_.size(); }
1025
1026 // Traverse the select clauses.
1027 int
1028 traverse(Traverse*);
1029
1030 // Lower statements.
1031 void
1032 lower(Gogo*, Named_object*, Block*, Temporary_statement*,
1033 Temporary_statement*);
1034
1035 // Determine types.
1036 void
1037 determine_types();
1038
1039 // Check types.
1040 void
1041 check_types();
1042
1043 // Whether the select clauses may fall through to the statement
1044 // which follows the overall select statement.
1045 bool
1046 may_fall_through() const;
1047
1048 // Convert to the backend representation.
1049 Bstatement*
1050 get_backend(Translate_context*, Temporary_statement* index,
1051 Unnamed_label* break_label, Location);
1052
1053 // Dump AST representation.
1054 void
1055 dump_clauses(Ast_dump_context*) const;
1056
1057 private:
1058 // A single clause.
1059 class Select_clause
1060 {
1061 public:
1062 Select_clause()
1063 : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
1064 closedvar_(NULL), statements_(NULL), is_send_(false),
1065 is_default_(false)
1066 { }
1067
1068 Select_clause(bool is_send, Expression* channel, Expression* val,
1069 Expression* closed, Named_object* var,
1070 Named_object* closedvar, bool is_default, Block* statements,
1071 Location location)
1072 : channel_(channel), val_(val), closed_(closed), var_(var),
1073 closedvar_(closedvar), statements_(statements), location_(location),
1074 is_send_(is_send), is_default_(is_default), is_lowered_(false)
1075 { go_assert(is_default ? channel == NULL : channel != NULL); }
1076
1077 // Traverse the select clause.
1078 int
1079 traverse(Traverse*);
1080
1081 // Lower statements.
1082 void
1083 lower(Gogo*, Named_object*, Block*, Temporary_statement*, size_t,
1084 Temporary_statement*);
1085
1086 // Determine types.
1087 void
1088 determine_types();
1089
1090 // Check types.
1091 void
1092 check_types();
1093
1094 // Return true if this is the default clause.
1095 bool
1096 is_default() const
1097 { return this->is_default_; }
1098
1099 // Return the channel. This will return NULL for the default
1100 // clause.
1101 Expression*
1102 channel() const
1103 { return this->channel_; }
1104
1105 // Return true for a send, false for a receive.
1106 bool
1107 is_send() const
1108 {
1109 go_assert(!this->is_default_);
1110 return this->is_send_;
1111 }
1112
1113 // Return the statements.
1114 const Block*
1115 statements() const
1116 { return this->statements_; }
1117
1118 // Return the location.
1119 Location
1120 location() const
1121 { return this->location_; }
1122
1123 // Whether this clause may fall through to the statement which
1124 // follows the overall select statement.
1125 bool
1126 may_fall_through() const;
1127
1128 // Convert the statements to the backend representation.
1129 Bstatement*
1130 get_statements_backend(Translate_context*);
1131
1132 // Dump AST representation.
1133 void
1134 dump_clause(Ast_dump_context*) const;
1135
1136 private:
1137 // These values must match the values in libgo/go/runtime/select.go.
1138 enum
1139 {
1140 caseRecv = 1,
1141 caseSend = 2,
1142 caseDefault = 3,
1143 };
1144
1145 void
1146 lower_default(Block*, Expression*);
1147
1148 void
1149 lower_send(Block*, Expression*, Expression*);
1150
1151 void
1152 lower_recv(Gogo*, Named_object*, Block*, Expression*, Expression*,
1153 Temporary_statement*);
1154
1155 void
1156 set_case(Block*, Expression*, Expression*, Expression*, int);
1157
1158 // The channel.
1159 Expression* channel_;
1160 // The value to send or the lvalue to receive into.
1161 Expression* val_;
1162 // The lvalue to set to whether the channel is closed on a
1163 // receive.
1164 Expression* closed_;
1165 // The variable to initialize, for "case a := <-ch".
1166 Named_object* var_;
1167 // The variable to initialize to whether the channel is closed,
1168 // for "case a, c := <-ch".
1169 Named_object* closedvar_;
1170 // The statements to execute.
1171 Block* statements_;
1172 // The location of this clause.
1173 Location location_;
1174 // Whether this is a send or a receive.
1175 bool is_send_;
1176 // Whether this is the default.
1177 bool is_default_;
1178 // Whether this has been lowered.
1179 bool is_lowered_;
1180 };
1181
1182 typedef std::vector<Select_clause> Clauses;
1183
1184 Clauses clauses_;
1185 };
1186
1187 // A select statement.
1188
1189 class Select_statement : public Statement
1190 {
1191 public:
1192 Select_statement(Location location)
1193 : Statement(STATEMENT_SELECT, location),
1194 clauses_(NULL), index_(NULL), break_label_(NULL), is_lowered_(false)
1195 { }
1196
1197 // Add the clauses.
1198 void
1199 add_clauses(Select_clauses* clauses)
1200 {
1201 go_assert(this->clauses_ == NULL);
1202 this->clauses_ = clauses;
1203 }
1204
1205 // Return the break label for this select statement.
1206 Unnamed_label*
1207 break_label();
1208
1209 protected:
1210 int
1211 do_traverse(Traverse* traverse)
1212 { return this->clauses_->traverse(traverse); }
1213
1214 Statement*
1215 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1216
1217 void
1218 do_determine_types()
1219 { this->clauses_->determine_types(); }
1220
1221 void
1222 do_check_types(Gogo*)
1223 { this->clauses_->check_types(); }
1224
1225 bool
1226 do_may_fall_through() const;
1227
1228 Bstatement*
1229 do_get_backend(Translate_context*);
1230
1231 void
1232 do_dump_statement(Ast_dump_context*) const;
1233
1234 private:
1235 // The select clauses.
1236 Select_clauses* clauses_;
1237 // A temporary that holds the index value returned by selectgo.
1238 Temporary_statement* index_;
1239 // The break label.
1240 Unnamed_label* break_label_;
1241 // Whether this statement has been lowered.
1242 bool is_lowered_;
1243 };
1244
1245 // A statement which requires a thunk: go or defer.
1246
1247 class Thunk_statement : public Statement
1248 {
1249 public:
1250 Thunk_statement(Statement_classification, Call_expression*,
1251 Location);
1252
1253 // Return the call expression.
1254 Expression*
1255 call() const
1256 { return this->call_; }
1257
1258 // Simplify a go or defer statement so that it only uses a single
1259 // parameter.
1260 bool
1261 simplify_statement(Gogo*, Named_object*, Block*);
1262
1263 protected:
1264 int
1265 do_traverse(Traverse* traverse);
1266
1267 bool
1268 do_traverse_assignments(Traverse_assignments*);
1269
1270 void
1271 do_determine_types();
1272
1273 void
1274 do_check_types(Gogo*);
1275
1276 // Return the function and argument for the call.
1277 bool
1278 get_fn_and_arg(Expression** pfn, Expression** parg);
1279
1280 private:
1281 // Return whether this is a simple go statement.
1282 bool
1283 is_simple(Function_type*) const;
1284
1285 // Return whether the thunk function is a constant.
1286 bool
1287 is_constant_function() const;
1288
1289 // Build the struct to use for a complex case.
1290 Struct_type*
1291 build_struct(Function_type* fntype);
1292
1293 // Build the thunk.
1294 void
1295 build_thunk(Gogo*, const std::string&);
1296
1297 // Set the name to use for thunk field N.
1298 void
1299 thunk_field_param(int n, char* buf, size_t buflen);
1300
1301 // The function call to be executed in a separate thread (go) or
1302 // later (defer).
1303 Expression* call_;
1304 // The type used for a struct to pass to a thunk, if this is not a
1305 // simple call.
1306 Struct_type* struct_type_;
1307 };
1308
1309 // A go statement.
1310
1311 class Go_statement : public Thunk_statement
1312 {
1313 public:
1314 Go_statement(Call_expression* call, Location location)
1315 : Thunk_statement(STATEMENT_GO, call, location)
1316 { }
1317
1318 protected:
1319 Bstatement*
1320 do_get_backend(Translate_context*);
1321
1322 void
1323 do_dump_statement(Ast_dump_context*) const;
1324 };
1325
1326 // A defer statement.
1327
1328 class Defer_statement : public Thunk_statement
1329 {
1330 public:
1331 Defer_statement(Call_expression* call, Location location)
1332 : Thunk_statement(STATEMENT_DEFER, call, location)
1333 { }
1334
1335 protected:
1336 Bstatement*
1337 do_get_backend(Translate_context*);
1338
1339 void
1340 do_dump_statement(Ast_dump_context*) const;
1341 };
1342
1343 // A goto statement.
1344
1345 class Goto_statement : public Statement
1346 {
1347 public:
1348 Goto_statement(Label* label, Location location)
1349 : Statement(STATEMENT_GOTO, location),
1350 label_(label)
1351 { }
1352
1353 // Return the label being jumped to.
1354 Label*
1355 label() const
1356 { return this->label_; }
1357
1358 protected:
1359 int
1360 do_traverse(Traverse*);
1361
1362 void
1363 do_check_types(Gogo*);
1364
1365 bool
1366 do_may_fall_through() const
1367 { return false; }
1368
1369 Bstatement*
1370 do_get_backend(Translate_context*);
1371
1372 void
1373 do_dump_statement(Ast_dump_context*) const;
1374
1375 private:
1376 Label* label_;
1377 };
1378
1379 // A goto statement to an unnamed label.
1380
1381 class Goto_unnamed_statement : public Statement
1382 {
1383 public:
1384 Goto_unnamed_statement(Unnamed_label* label, Location location)
1385 : Statement(STATEMENT_GOTO_UNNAMED, location),
1386 label_(label)
1387 { }
1388
1389 Unnamed_label*
1390 unnamed_label() const
1391 { return this->label_; }
1392
1393 protected:
1394 int
1395 do_traverse(Traverse*);
1396
1397 bool
1398 do_may_fall_through() const
1399 { return false; }
1400
1401 Bstatement*
1402 do_get_backend(Translate_context* context);
1403
1404 void
1405 do_dump_statement(Ast_dump_context*) const;
1406
1407 private:
1408 Unnamed_label* label_;
1409 };
1410
1411 // A label statement.
1412
1413 class Label_statement : public Statement
1414 {
1415 public:
1416 Label_statement(Label* label, Location location)
1417 : Statement(STATEMENT_LABEL, location),
1418 label_(label)
1419 { }
1420
1421 // Return the label itself.
1422 Label*
1423 label() const
1424 { return this->label_; }
1425
1426 protected:
1427 int
1428 do_traverse(Traverse*);
1429
1430 Bstatement*
1431 do_get_backend(Translate_context*);
1432
1433 void
1434 do_dump_statement(Ast_dump_context*) const;
1435
1436 private:
1437 // The label.
1438 Label* label_;
1439 };
1440
1441 // An unnamed label statement.
1442
1443 class Unnamed_label_statement : public Statement
1444 {
1445 public:
1446 Unnamed_label_statement(Unnamed_label* label);
1447
1448 protected:
1449 int
1450 do_traverse(Traverse*);
1451
1452 Bstatement*
1453 do_get_backend(Translate_context* context);
1454
1455 void
1456 do_dump_statement(Ast_dump_context*) const;
1457
1458 private:
1459 // The label.
1460 Unnamed_label* label_;
1461 };
1462
1463 // An if statement.
1464
1465 class If_statement : public Statement
1466 {
1467 public:
1468 If_statement(Expression* cond, Block* then_block, Block* else_block,
1469 Location location)
1470 : Statement(STATEMENT_IF, location),
1471 cond_(cond), then_block_(then_block), else_block_(else_block)
1472 { }
1473
1474 Expression*
1475 condition() const
1476 { return this->cond_; }
1477
1478 protected:
1479 int
1480 do_traverse(Traverse*);
1481
1482 void
1483 do_determine_types();
1484
1485 void
1486 do_check_types(Gogo*);
1487
1488 bool
1489 do_may_fall_through() const;
1490
1491 Bstatement*
1492 do_get_backend(Translate_context*);
1493
1494 void
1495 do_dump_statement(Ast_dump_context*) const;
1496
1497 private:
1498 Expression* cond_;
1499 Block* then_block_;
1500 Block* else_block_;
1501 };
1502
1503 // A for statement.
1504
1505 class For_statement : public Statement
1506 {
1507 public:
1508 For_statement(Block* init, Expression* cond, Block* post,
1509 Location location)
1510 : Statement(STATEMENT_FOR, location),
1511 init_(init), cond_(cond), post_(post), statements_(NULL),
1512 break_label_(NULL), continue_label_(NULL)
1513 { }
1514
1515 // Add the statements.
1516 void
1517 add_statements(Block* statements)
1518 {
1519 go_assert(this->statements_ == NULL);
1520 this->statements_ = statements;
1521 }
1522
1523 // Return the break label for this for statement.
1524 Unnamed_label*
1525 break_label();
1526
1527 // Return the continue label for this for statement.
1528 Unnamed_label*
1529 continue_label();
1530
1531 // Set the break and continue labels for this statement.
1532 void
1533 set_break_continue_labels(Unnamed_label* break_label,
1534 Unnamed_label* continue_label);
1535
1536 protected:
1537 int
1538 do_traverse(Traverse*);
1539
1540 bool
1541 do_traverse_assignments(Traverse_assignments*)
1542 { go_unreachable(); }
1543
1544 Statement*
1545 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1546
1547 bool
1548 do_may_fall_through() const;
1549
1550 Bstatement*
1551 do_get_backend(Translate_context*)
1552 { go_unreachable(); }
1553
1554 void
1555 do_dump_statement(Ast_dump_context*) const;
1556
1557 private:
1558 // The initialization statements. This may be NULL.
1559 Block* init_;
1560 // The condition. This may be NULL.
1561 Expression* cond_;
1562 // The statements to run after each iteration. This may be NULL.
1563 Block* post_;
1564 // The statements in the loop itself.
1565 Block* statements_;
1566 // The break label, if needed.
1567 Unnamed_label* break_label_;
1568 // The continue label, if needed.
1569 Unnamed_label* continue_label_;
1570 };
1571
1572 // A for statement over a range clause.
1573
1574 class For_range_statement : public Statement
1575 {
1576 public:
1577 For_range_statement(Expression* index_var, Expression* value_var,
1578 Expression* range, Location location)
1579 : Statement(STATEMENT_FOR_RANGE, location),
1580 index_var_(index_var), value_var_(value_var), range_(range),
1581 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1582 { }
1583
1584 // Add the statements.
1585 void
1586 add_statements(Block* statements)
1587 {
1588 go_assert(this->statements_ == NULL);
1589 this->statements_ = statements;
1590 }
1591
1592 // Return the break label for this for statement.
1593 Unnamed_label*
1594 break_label();
1595
1596 // Return the continue label for this for statement.
1597 Unnamed_label*
1598 continue_label();
1599
1600 protected:
1601 int
1602 do_traverse(Traverse*);
1603
1604 bool
1605 do_traverse_assignments(Traverse_assignments*)
1606 { go_unreachable(); }
1607
1608 Statement*
1609 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1610
1611 Bstatement*
1612 do_get_backend(Translate_context*)
1613 { go_unreachable(); }
1614
1615 void
1616 do_dump_statement(Ast_dump_context*) const;
1617
1618 private:
1619 Expression*
1620 make_range_ref(Named_object*, Temporary_statement*, Location);
1621
1622 Call_expression*
1623 call_builtin(Gogo*, const char* funcname, Expression* arg, Location);
1624
1625 void
1626 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1627 Temporary_statement*, Temporary_statement*,
1628 Block**, Expression**, Block**, Block**);
1629
1630 void
1631 lower_range_slice(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1632 Temporary_statement*, Temporary_statement*,
1633 Block**, Expression**, Block**, Block**);
1634
1635 void
1636 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1637 Temporary_statement*, Temporary_statement*,
1638 Block**, Expression**, Block**, Block**);
1639
1640 void
1641 lower_range_map(Gogo*, Map_type*, Block*, Block*, Named_object*,
1642 Temporary_statement*, Temporary_statement*,
1643 Temporary_statement*, Block**, Expression**, Block**,
1644 Block**);
1645
1646 void
1647 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1648 Temporary_statement*, Temporary_statement*,
1649 Temporary_statement*, Block**, Expression**, Block**,
1650 Block**);
1651
1652 Statement*
1653 lower_map_range_clear(Type*, Block*, Expression*, Named_object*,
1654 Temporary_statement*, Location);
1655
1656 Statement*
1657 lower_array_range_clear(Gogo*, Type*, Expression*, Block*,
1658 Named_object*, Temporary_statement*,
1659 Location);
1660
1661 // The variable which is set to the index value.
1662 Expression* index_var_;
1663 // The variable which is set to the element value. This may be
1664 // NULL.
1665 Expression* value_var_;
1666 // The expression we are ranging over.
1667 Expression* range_;
1668 // The statements in the block.
1669 Block* statements_;
1670 // The break label, if needed.
1671 Unnamed_label* break_label_;
1672 // The continue label, if needed.
1673 Unnamed_label* continue_label_;
1674 };
1675
1676 // Class Case_clauses holds the clauses of a switch statement. This
1677 // is built by the parser.
1678
1679 class Case_clauses
1680 {
1681 public:
1682 Case_clauses()
1683 : clauses_()
1684 { }
1685
1686 // Add a new clause. CASES is a list of case expressions; it may be
1687 // NULL. IS_DEFAULT is true if this is the default case.
1688 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1689 // after the statements the case clause should fall through to the
1690 // next clause.
1691 void
1692 add(Expression_list* cases, bool is_default, Block* statements,
1693 bool is_fallthrough, Location location)
1694 {
1695 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1696 is_fallthrough, location));
1697 }
1698
1699 // Return whether there are no clauses.
1700 bool
1701 empty() const
1702 { return this->clauses_.empty(); }
1703
1704 // Traverse the case clauses.
1705 int
1706 traverse(Traverse*);
1707
1708 // Lower for a nonconstant switch.
1709 void
1710 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1711
1712 // Determine types of expressions. The Type parameter is the type
1713 // of the switch value.
1714 void
1715 determine_types(Type*);
1716
1717 // Check types. The Type parameter is the type of the switch value.
1718 bool
1719 check_types(Type*);
1720
1721 // Return true if all the clauses are constant values.
1722 bool
1723 is_constant() const;
1724
1725 // Return true if these clauses may fall through to the statements
1726 // following the switch statement.
1727 bool
1728 may_fall_through() const;
1729
1730 // Return the body of a SWITCH_EXPR when all the clauses are
1731 // constants.
1732 void
1733 get_backend(Translate_context*, Unnamed_label* break_label,
1734 std::vector<std::vector<Bexpression*> >* all_cases,
1735 std::vector<Bstatement*>* all_statements) const;
1736
1737 // Dump the AST representation to a dump context.
1738 void
1739 dump_clauses(Ast_dump_context*) const;
1740
1741 private:
1742 // For a constant switch we need to keep a record of constants we
1743 // have already seen.
1744 class Hash_integer_value;
1745 class Eq_integer_value;
1746 typedef Unordered_set_hash(Expression*, Hash_integer_value,
1747 Eq_integer_value) Case_constants;
1748
1749 // One case clause.
1750 class Case_clause
1751 {
1752 public:
1753 Case_clause()
1754 : cases_(NULL), statements_(NULL), is_default_(false),
1755 is_fallthrough_(false), location_(Linemap::unknown_location())
1756 { }
1757
1758 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1759 bool is_fallthrough, Location location)
1760 : cases_(cases), statements_(statements), is_default_(is_default),
1761 is_fallthrough_(is_fallthrough), location_(location)
1762 { }
1763
1764 // Whether this clause falls through to the next clause.
1765 bool
1766 is_fallthrough() const
1767 { return this->is_fallthrough_; }
1768
1769 // Whether this is the default.
1770 bool
1771 is_default() const
1772 { return this->is_default_; }
1773
1774 // The location of this clause.
1775 Location
1776 location() const
1777 { return this->location_; }
1778
1779 // Traversal.
1780 int
1781 traverse(Traverse*);
1782
1783 // Lower for a nonconstant switch.
1784 void
1785 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1786
1787 // Determine types.
1788 void
1789 determine_types(Type*);
1790
1791 // Check types.
1792 bool
1793 check_types(Type*);
1794
1795 // Return true if all the case expressions are constant.
1796 bool
1797 is_constant() const;
1798
1799 // Return true if this clause may fall through to execute the
1800 // statements following the switch statement. This is not the
1801 // same as whether this clause falls through to the next clause.
1802 bool
1803 may_fall_through() const;
1804
1805 // Convert the case values and statements to the backend
1806 // representation.
1807 Bstatement*
1808 get_backend(Translate_context*, Unnamed_label* break_label,
1809 Case_constants*, std::vector<Bexpression*>* cases) const;
1810
1811 // Dump the AST representation to a dump context.
1812 void
1813 dump_clause(Ast_dump_context*) const;
1814
1815 private:
1816 // The list of case expressions.
1817 Expression_list* cases_;
1818 // The statements to execute.
1819 Block* statements_;
1820 // Whether this is the default case.
1821 bool is_default_;
1822 // Whether this falls through after the statements.
1823 bool is_fallthrough_;
1824 // The location of this case clause.
1825 Location location_;
1826 };
1827
1828 friend class Case_clause;
1829
1830 // The type of the list of clauses.
1831 typedef std::vector<Case_clause> Clauses;
1832
1833 // All the case clauses.
1834 Clauses clauses_;
1835 };
1836
1837 // A switch statement.
1838
1839 class Switch_statement : public Statement
1840 {
1841 public:
1842 Switch_statement(Expression* val, Location location)
1843 : Statement(STATEMENT_SWITCH, location),
1844 val_(val), clauses_(NULL), break_label_(NULL)
1845 { }
1846
1847 // Add the clauses.
1848 void
1849 add_clauses(Case_clauses* clauses)
1850 {
1851 go_assert(this->clauses_ == NULL);
1852 this->clauses_ = clauses;
1853 }
1854
1855 // Return the break label for this switch statement.
1856 Unnamed_label*
1857 break_label();
1858
1859 protected:
1860 int
1861 do_traverse(Traverse*);
1862
1863 Statement*
1864 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1865
1866 Bstatement*
1867 do_get_backend(Translate_context*)
1868 { go_unreachable(); }
1869
1870 void
1871 do_dump_statement(Ast_dump_context*) const;
1872
1873 bool
1874 do_may_fall_through() const;
1875
1876 private:
1877 // The value to switch on. This may be NULL.
1878 Expression* val_;
1879 // The case clauses.
1880 Case_clauses* clauses_;
1881 // The break label, if needed.
1882 Unnamed_label* break_label_;
1883 };
1884
1885 // Class Type_case_clauses holds the clauses of a type switch
1886 // statement. This is built by the parser.
1887
1888 class Type_case_clauses
1889 {
1890 public:
1891 Type_case_clauses()
1892 : clauses_()
1893 { }
1894
1895 // Add a new clause. TYPE is the type for this clause; it may be
1896 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1897 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1898 // if this is the default case. STATEMENTS is a block of
1899 // statements; it may be NULL.
1900 void
1901 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1902 Location location)
1903 {
1904 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1905 statements, location));
1906 }
1907
1908 // Return whether there are no clauses.
1909 bool
1910 empty() const
1911 { return this->clauses_.empty(); }
1912
1913 // Traverse the type case clauses.
1914 int
1915 traverse(Traverse*);
1916
1917 // Check for duplicates.
1918 void
1919 check_duplicates() const;
1920
1921 // Lower to if and goto statements.
1922 void
1923 lower(Type*, Block*, Temporary_statement* descriptor_temp,
1924 Unnamed_label* break_label) const;
1925
1926 // Return true if these clauses may fall through to the statements
1927 // following the switch statement.
1928 bool
1929 may_fall_through() const;
1930
1931 // Dump the AST representation to a dump context.
1932 void
1933 dump_clauses(Ast_dump_context*) const;
1934
1935 private:
1936 // One type case clause.
1937 class Type_case_clause
1938 {
1939 public:
1940 Type_case_clause()
1941 : type_(NULL), statements_(NULL), is_default_(false),
1942 location_(Linemap::unknown_location())
1943 { }
1944
1945 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1946 Block* statements, Location location)
1947 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1948 is_default_(is_default), location_(location)
1949 { }
1950
1951 // The type.
1952 Type*
1953 type() const
1954 { return this->type_; }
1955
1956 // Whether this is the default.
1957 bool
1958 is_default() const
1959 { return this->is_default_; }
1960
1961 // The location of this type clause.
1962 Location
1963 location() const
1964 { return this->location_; }
1965
1966 // Traversal.
1967 int
1968 traverse(Traverse*);
1969
1970 // Lower to if and goto statements.
1971 void
1972 lower(Type*, Block*, Temporary_statement* descriptor_temp,
1973 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1974
1975 // Return true if this clause may fall through to execute the
1976 // statements following the switch statement. This is not the
1977 // same as whether this clause falls through to the next clause.
1978 bool
1979 may_fall_through() const;
1980
1981 // Dump the AST representation to a dump context.
1982 void
1983 dump_clause(Ast_dump_context*) const;
1984
1985 private:
1986 // The type for this type clause.
1987 Type* type_;
1988 // The statements to execute.
1989 Block* statements_;
1990 // Whether this falls through--this is true for "case T1, T2".
1991 bool is_fallthrough_;
1992 // Whether this is the default case.
1993 bool is_default_;
1994 // The location of this type case clause.
1995 Location location_;
1996 };
1997
1998 friend class Type_case_clause;
1999
2000 // The type of the list of type clauses.
2001 typedef std::vector<Type_case_clause> Type_clauses;
2002
2003 // All the type case clauses.
2004 Type_clauses clauses_;
2005 };
2006
2007 // A type switch statement.
2008
2009 class Type_switch_statement : public Statement
2010 {
2011 public:
2012 Type_switch_statement(const std::string& name, Expression* expr,
2013 Location location)
2014 : Statement(STATEMENT_TYPE_SWITCH, location),
2015 name_(name), expr_(expr), clauses_(NULL), break_label_(NULL)
2016 { }
2017
2018 // Add the clauses.
2019 void
2020 add_clauses(Type_case_clauses* clauses)
2021 {
2022 go_assert(this->clauses_ == NULL);
2023 this->clauses_ = clauses;
2024 }
2025
2026 // Return the break label for this type switch statement.
2027 Unnamed_label*
2028 break_label();
2029
2030 protected:
2031 int
2032 do_traverse(Traverse*);
2033
2034 Statement*
2035 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
2036
2037 Bstatement*
2038 do_get_backend(Translate_context*)
2039 { go_unreachable(); }
2040
2041 void
2042 do_dump_statement(Ast_dump_context*) const;
2043
2044 bool
2045 do_may_fall_through() const;
2046
2047 private:
2048 // The name of the variable declared in the type switch guard. Empty if there
2049 // is no variable declared.
2050 std::string name_;
2051 // The expression we are switching on if there is no variable.
2052 Expression* expr_;
2053 // The type case clauses.
2054 Type_case_clauses* clauses_;
2055 // The break label, if needed.
2056 Unnamed_label* break_label_;
2057 };
2058
2059 #endif // !defined(GO_STATEMENTS_H)