ptr_traits.h (pointer_traits): Fix typos.
[gcc.git] / gcc / go / gofrontend / statements.h.merge-left.r167407
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 Block;
15 class Function;
16 class Unnamed_label;
17 class Temporary_statement;
18 class Variable_declaration_statement;
19 class Return_statement;
20 class Thunk_statement;
21 class Label_statement;
22 class For_statement;
23 class For_range_statement;
24 class Switch_statement;
25 class Type_switch_statement;
26 class Select_statement;
27 class Variable;
28 class Named_object;
29 class Label;
30 class Translate_context;
31 class Expression;
32 class Expression_list;
33 class Struct_type;
34 class Call_expression;
35 class Map_index_expression;
36 class Receive_expression;
37 class Case_clauses;
38 class Type_case_clauses;
39 class Select_clauses;
40 class Typed_identifier_list;
41
42 // This class is used to traverse assignments made by a statement
43 // which makes assignments.
44
45 class Traverse_assignments
46 {
47 public:
48 Traverse_assignments()
49 { }
50
51 virtual ~Traverse_assignments()
52 { }
53
54 // This is called for a variable initialization.
55 virtual void
56 initialize_variable(Named_object*) = 0;
57
58 // This is called for each assignment made by the statement. PLHS
59 // points to the left hand side, and PRHS points to the right hand
60 // side. PRHS may be NULL if there is no associated expression, as
61 // in the bool set by a non-blocking receive.
62 virtual void
63 assignment(Expression** plhs, Expression** prhs) = 0;
64
65 // This is called for each expression which is not passed to the
66 // assignment function. This is used for some of the statements
67 // which assign two values, for which there is no expression which
68 // describes the value. For ++ and -- the value is passed to both
69 // the assignment method and the rhs method. IS_STORED is true if
70 // this value is being stored directly. It is false if the value is
71 // computed but not stored. IS_LOCAL is true if the value is being
72 // stored in a local variable or this is being called by a return
73 // statement.
74 virtual void
75 value(Expression**, bool is_stored, bool is_local) = 0;
76 };
77
78 // A single statement.
79
80 class Statement
81 {
82 public:
83 // The types of statements.
84 enum Statement_classification
85 {
86 STATEMENT_ERROR,
87 STATEMENT_VARIABLE_DECLARATION,
88 STATEMENT_TEMPORARY,
89 STATEMENT_ASSIGNMENT,
90 STATEMENT_EXPRESSION,
91 STATEMENT_BLOCK,
92 STATEMENT_GO,
93 STATEMENT_DEFER,
94 STATEMENT_RETURN,
95 STATEMENT_BREAK_OR_CONTINUE,
96 STATEMENT_GOTO,
97 STATEMENT_GOTO_UNNAMED,
98 STATEMENT_LABEL,
99 STATEMENT_UNNAMED_LABEL,
100 STATEMENT_IF,
101 STATEMENT_CONSTANT_SWITCH,
102 STATEMENT_SELECT,
103
104 // These statements types are created by the parser, but they
105 // disappear during the lowering pass.
106 STATEMENT_ASSIGNMENT_OPERATION,
107 STATEMENT_TUPLE_ASSIGNMENT,
108 STATEMENT_TUPLE_MAP_ASSIGNMENT,
109 STATEMENT_MAP_ASSIGNMENT,
110 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT,
111 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT,
112 STATEMENT_INCDEC,
113 STATEMENT_FOR,
114 STATEMENT_FOR_RANGE,
115 STATEMENT_SWITCH,
116 STATEMENT_TYPE_SWITCH
117 };
118
119 Statement(Statement_classification, source_location);
120
121 virtual ~Statement();
122
123 // Make a variable declaration.
124 static Statement*
125 make_variable_declaration(Named_object*);
126
127 // Make a statement which creates a temporary variable and
128 // initializes it to an expression. The block is used if the
129 // temporary variable has to be explicitly destroyed; the variable
130 // must still be added to the block. References to the temporary
131 // variable may be constructed using make_temporary_reference.
132 // Either the type or the initialization expression may be NULL, but
133 // not both.
134 static Temporary_statement*
135 make_temporary(Type*, Expression*, source_location);
136
137 // Make an assignment statement.
138 static Statement*
139 make_assignment(Expression*, Expression*, source_location);
140
141 // Make an assignment operation (+=, etc.).
142 static Statement*
143 make_assignment_operation(Operator, Expression*, Expression*,
144 source_location);
145
146 // Make a tuple assignment statement.
147 static Statement*
148 make_tuple_assignment(Expression_list*, Expression_list*, source_location);
149
150 // Make an assignment from a map index to a pair of variables.
151 static Statement*
152 make_tuple_map_assignment(Expression* val, Expression* present,
153 Expression*, source_location);
154
155 // Make a statement which assigns a pair of values to a map.
156 static Statement*
157 make_map_assignment(Expression*, Expression* val,
158 Expression* should_set, source_location);
159
160 // Make an assignment from a nonblocking receive to a pair of
161 // variables.
162 static Statement*
163 make_tuple_receive_assignment(Expression* val, Expression* success,
164 Expression* channel, source_location);
165
166 // Make an assignment from a type guard to a pair of variables.
167 static Statement*
168 make_tuple_type_guard_assignment(Expression* val, Expression* ok,
169 Expression* expr, Type* type,
170 source_location);
171
172 // Make an expression statement from an Expression.
173 static Statement*
174 make_statement(Expression*);
175
176 // Make a block statement from a Block. This is an embedded list of
177 // statements which may also include variable definitions.
178 static Statement*
179 make_block_statement(Block*, source_location);
180
181 // Make an increment statement.
182 static Statement*
183 make_inc_statement(Expression*);
184
185 // Make a decrement statement.
186 static Statement*
187 make_dec_statement(Expression*);
188
189 // Make a go statement.
190 static Statement*
191 make_go_statement(Call_expression* call, source_location);
192
193 // Make a defer statement.
194 static Statement*
195 make_defer_statement(Call_expression* call, source_location);
196
197 // Make a return statement.
198 static Statement*
199 make_return_statement(const Typed_identifier_list*, Expression_list*,
200 source_location);
201
202 // Make a break statement.
203 static Statement*
204 make_break_statement(Unnamed_label* label, source_location);
205
206 // Make a continue statement.
207 static Statement*
208 make_continue_statement(Unnamed_label* label, source_location);
209
210 // Make a goto statement.
211 static Statement*
212 make_goto_statement(Label* label, source_location);
213
214 // Make a goto statement to an unnamed label.
215 static Statement*
216 make_goto_unnamed_statement(Unnamed_label* label, source_location);
217
218 // Make a label statement--where the label is defined.
219 static Statement*
220 make_label_statement(Label* label, source_location);
221
222 // Make an unnamed label statement--where the label is defined.
223 static Statement*
224 make_unnamed_label_statement(Unnamed_label* label);
225
226 // Make an if statement.
227 static Statement*
228 make_if_statement(Expression* cond, Block* then_block, Block* else_block,
229 source_location);
230
231 // Make a switch statement.
232 static Switch_statement*
233 make_switch_statement(Expression* switch_val, source_location);
234
235 // Make a type switch statement.
236 static Type_switch_statement*
237 make_type_switch_statement(Named_object* var, Expression*, source_location);
238
239 // Make a select statement.
240 static Select_statement*
241 make_select_statement(source_location);
242
243 // Make a for statement.
244 static For_statement*
245 make_for_statement(Block* init, Expression* cond, Block* post,
246 source_location location);
247
248 // Make a for statement with a range clause.
249 static For_range_statement*
250 make_for_range_statement(Expression* index_var, Expression* value_var,
251 Expression* range, source_location);
252
253 // Return the statement classification.
254 Statement_classification
255 classification() const
256 { return this->classification_; }
257
258 // Get the statement location.
259 source_location
260 location() const
261 { return this->location_; }
262
263 // Traverse the tree.
264 int
265 traverse(Block*, size_t* index, Traverse*);
266
267 // Traverse the contents of this statement--the expressions and
268 // statements which it contains.
269 int
270 traverse_contents(Traverse*);
271
272 // If this statement assigns some values, it calls a function for
273 // each value to which this statement assigns a value, and returns
274 // true. If this statement does not assign any values, it returns
275 // false.
276 bool
277 traverse_assignments(Traverse_assignments* tassign);
278
279 // Lower a statement. This is called immediately after parsing to
280 // simplify statements for further processing. It returns the same
281 // Statement or a new one. BLOCK is the block containing this
282 // statement.
283 Statement*
284 lower(Gogo* gogo, Block* block)
285 { return this->do_lower(gogo, block); }
286
287 // Set type information for unnamed constants.
288 void
289 determine_types();
290
291 // Check types in a statement. This simply checks that any
292 // expressions used by the statement have the right type.
293 void
294 check_types(Gogo* gogo)
295 { this->do_check_types(gogo); }
296
297 // Return whether this is a block statement.
298 bool
299 is_block_statement() const
300 { return this->classification_ == STATEMENT_BLOCK; }
301
302 // If this is a variable declaration statement, return it.
303 // Otherwise return NULL.
304 Variable_declaration_statement*
305 variable_declaration_statement()
306 {
307 return this->convert<Variable_declaration_statement,
308 STATEMENT_VARIABLE_DECLARATION>();
309 }
310
311 // If this is a return statement, return it. Otherwise return NULL.
312 Return_statement*
313 return_statement()
314 { return this->convert<Return_statement, STATEMENT_RETURN>(); }
315
316 // If this is a thunk statement (a go or defer statement), return
317 // it. Otherwise return NULL.
318 Thunk_statement*
319 thunk_statement();
320
321 // If this is a label statement, return it. Otherwise return NULL.
322 Label_statement*
323 label_statement()
324 { return this->convert<Label_statement, STATEMENT_LABEL>(); }
325
326 // If this is a for statement, return it. Otherwise return NULL.
327 For_statement*
328 for_statement()
329 { return this->convert<For_statement, STATEMENT_FOR>(); }
330
331 // If this is a for statement over a range clause, return it.
332 // Otherwise return NULL.
333 For_range_statement*
334 for_range_statement()
335 { return this->convert<For_range_statement, STATEMENT_FOR_RANGE>(); }
336
337 // If this is a switch statement, return it. Otherwise return NULL.
338 Switch_statement*
339 switch_statement()
340 { return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
341
342 // If this is a type switch statement, return it. Otherwise return
343 // NULL.
344 Type_switch_statement*
345 type_switch_statement()
346 { return this->convert<Type_switch_statement, STATEMENT_TYPE_SWITCH>(); }
347
348 // If this is a select statement, return it. Otherwise return NULL.
349 Select_statement*
350 select_statement()
351 { return this->convert<Select_statement, STATEMENT_SELECT>(); }
352
353 // Return true if this statement may fall through--if after
354 // executing this statement we may go on to execute the following
355 // statement, if any.
356 bool
357 may_fall_through() const
358 { return this->do_may_fall_through(); }
359
360 // Return the tree for a statement. BLOCK is the enclosing block.
361 tree
362 get_tree(Translate_context*);
363
364 protected:
365 // Implemented by child class: traverse the tree.
366 virtual int
367 do_traverse(Traverse*) = 0;
368
369 // Implemented by child class: traverse assignments. Any statement
370 // which includes an assignment should implement this.
371 virtual bool
372 do_traverse_assignments(Traverse_assignments*)
373 { return false; }
374
375 // Implemented by the child class: lower this statement to a simpler
376 // one.
377 virtual Statement*
378 do_lower(Gogo*, Block*)
379 { return this; }
380
381 // Implemented by child class: set type information for unnamed
382 // constants. Any statement which includes an expression needs to
383 // implement this.
384 virtual void
385 do_determine_types()
386 { }
387
388 // Implemented by child class: check types of expressions used in a
389 // statement.
390 virtual void
391 do_check_types(Gogo*)
392 { }
393
394 // Implemented by child class: return true if this statement may
395 // fall through.
396 virtual bool
397 do_may_fall_through() const
398 { return true; }
399
400 // Implemented by child class: return a tree.
401 virtual tree
402 do_get_tree(Translate_context*) = 0;
403
404 // Traverse an expression in a statement.
405 int
406 traverse_expression(Traverse*, Expression**);
407
408 // Traverse an expression list in a statement. The Expression_list
409 // may be NULL.
410 int
411 traverse_expression_list(Traverse*, Expression_list*);
412
413 // Traverse a type in a statement.
414 int
415 traverse_type(Traverse*, Type*);
416
417 // Build a tree node with one operand, setting the location. The
418 // first operand really has type "enum tree_code", but that enum is
419 // not defined here.
420 tree
421 build_stmt_1(int tree_code_value, tree);
422
423 // For children to call when they detect that they are in error.
424 void
425 set_is_error();
426
427 // For children to call to report an error conveniently.
428 void
429 report_error(const char*);
430
431 // For children to return an error statement from lower().
432 static Statement*
433 make_error_statement(source_location);
434
435 private:
436 // Convert to the desired statement classification, or return NULL.
437 // This is a controlled dynamic cast.
438 template<typename Statement_class, Statement_classification sc>
439 Statement_class*
440 convert()
441 {
442 return (this->classification_ == sc
443 ? static_cast<Statement_class*>(this)
444 : NULL);
445 }
446
447 template<typename Statement_class, Statement_classification sc>
448 const Statement_class*
449 convert() const
450 {
451 return (this->classification_ == sc
452 ? static_cast<const Statement_class*>(this)
453 : NULL);
454 }
455
456 // The statement classification.
457 Statement_classification classification_;
458 // The location in the input file of the start of this statement.
459 source_location location_;
460 };
461
462 // A statement which creates and initializes a temporary variable.
463
464 class Temporary_statement : public Statement
465 {
466 public:
467 Temporary_statement(Type* type, Expression* init, source_location location)
468 : Statement(STATEMENT_TEMPORARY, location),
469 type_(type), init_(init), decl_(NULL), is_address_taken_(false)
470 { }
471
472 // Return the type of the temporary variable.
473 Type*
474 type() const;
475
476 // Return the initialization expression.
477 Expression*
478 init() const
479 { return this->init_; }
480
481 // Record that something takes the address of this temporary
482 // variable.
483 void
484 set_is_address_taken()
485 { this->is_address_taken_ = true; }
486
487 // Return the tree for the temporary variable itself. This should
488 // not be called until after the statement itself has been expanded.
489 tree
490 get_decl() const
491 {
492 gcc_assert(this->decl_ != NULL);
493 return this->decl_;
494 }
495
496 protected:
497 int
498 do_traverse(Traverse*);
499
500 bool
501 do_traverse_assignments(Traverse_assignments*);
502
503 void
504 do_determine_types();
505
506 void
507 do_check_types(Gogo*);
508
509 tree
510 do_get_tree(Translate_context*);
511
512 private:
513 // The type of the temporary variable.
514 Type* type_;
515 // The initial value of the temporary variable. This may be NULL.
516 Expression* init_;
517 // The DECL for the temporary variable.
518 tree decl_;
519 // True if something takes the address of this temporary variable.
520 bool is_address_taken_;
521 };
522
523 // A variable declaration. This marks the point in the code where a
524 // variable is declared. The Variable is also attached to a Block.
525
526 class Variable_declaration_statement : public Statement
527 {
528 public:
529 Variable_declaration_statement(Named_object* var);
530
531 // The variable being declared.
532 Named_object*
533 var()
534 { return this->var_; }
535
536 protected:
537 int
538 do_traverse(Traverse*);
539
540 bool
541 do_traverse_assignments(Traverse_assignments*);
542
543 tree
544 do_get_tree(Translate_context*);
545
546 private:
547 Named_object* var_;
548 };
549
550 // A return statement.
551
552 class Return_statement : public Statement
553 {
554 public:
555 Return_statement(const Typed_identifier_list* results, Expression_list* vals,
556 source_location location)
557 : Statement(STATEMENT_RETURN, location),
558 results_(results), vals_(vals)
559 { }
560
561 // The list of values being returned. This may be NULL.
562 const Expression_list*
563 vals() const
564 { return this->vals_; }
565
566 protected:
567 int
568 do_traverse(Traverse* traverse)
569 { return this->traverse_expression_list(traverse, this->vals_); }
570
571 bool
572 do_traverse_assignments(Traverse_assignments*);
573
574 Statement*
575 do_lower(Gogo*, Block*);
576
577 void
578 do_determine_types();
579
580 void
581 do_check_types(Gogo*);
582
583 bool
584 do_may_fall_through() const
585 { return false; }
586
587 tree
588 do_get_tree(Translate_context*);
589
590 private:
591 // The result types of the function we are returning from. This is
592 // here because in some of the traversals it is inconvenient to get
593 // it.
594 const Typed_identifier_list* results_;
595 // Return values. This may be NULL.
596 Expression_list* vals_;
597 };
598
599 // Select_clauses holds the clauses of a select statement. This is
600 // built by the parser.
601
602 class Select_clauses
603 {
604 public:
605 Select_clauses()
606 : clauses_()
607 { }
608
609 // Add a new clause. IS_SEND is true if this is a send clause,
610 // false for a receive clause. For a send clause CHANNEL is the
611 // channel and VAL is the value to send. For a receive clause
612 // CHANNEL is the channel and VAL is either NULL or a Var_expression
613 // for the variable to set; if VAL is NULL, VAR may be a variable
614 // which is initialized with the received value. IS_DEFAULT is true
615 // if this is the default clause. STATEMENTS is the list of
616 // statements to execute.
617 void
618 add(bool is_send, Expression* channel, Expression* val, Named_object* var,
619 bool is_default, Block* statements, source_location location)
620 {
621 this->clauses_.push_back(Select_clause(is_send, channel, val, var,
622 is_default, statements, location));
623 }
624
625 // Traverse the select clauses.
626 int
627 traverse(Traverse*);
628
629 // Lower statements.
630 void
631 lower(Block*);
632
633 // Determine types.
634 void
635 determine_types();
636
637 // Whether the select clauses may fall through to the statement
638 // which follows the overall select statement.
639 bool
640 may_fall_through() const;
641
642 // Return a tree implementing the select statement.
643 tree
644 get_tree(Translate_context*, Unnamed_label* break_label, source_location);
645
646 private:
647 // A single clause.
648 class Select_clause
649 {
650 public:
651 Select_clause()
652 : channel_(NULL), val_(NULL), var_(NULL), statements_(NULL),
653 is_send_(false), is_default_(false)
654 { }
655
656 Select_clause(bool is_send, Expression* channel, Expression* val,
657 Named_object* var, bool is_default, Block* statements,
658 source_location location)
659 : channel_(channel), val_(val), var_(var), statements_(statements),
660 location_(location), is_send_(is_send), is_default_(is_default),
661 is_lowered_(false)
662 { gcc_assert(is_default ? channel == NULL : channel != NULL); }
663
664 // Traverse the select clause.
665 int
666 traverse(Traverse*);
667
668 // Lower statements.
669 void
670 lower(Block*);
671
672 // Determine types.
673 void
674 determine_types();
675
676 // Return true if this is the default clause.
677 bool
678 is_default() const
679 { return this->is_default_; }
680
681 // Return the channel. This will return NULL for the default
682 // clause.
683 Expression*
684 channel() const
685 { return this->channel_; }
686
687 // Return the value. This will return NULL for the default
688 // clause, or for a receive clause for which no value was given.
689 Expression*
690 val() const
691 { return this->val_; }
692
693 // Return the variable to set when a receive clause is also a
694 // variable definition (v := <- ch). This will return NULL for
695 // the default case, or for a send clause, or for a receive clause
696 // which does not define a variable.
697 Named_object*
698 var() const
699 { return this->var_; }
700
701 // Return true for a send, false for a receive.
702 bool
703 is_send() const
704 {
705 gcc_assert(!this->is_default_);
706 return this->is_send_;
707 }
708
709 // Return the statements.
710 const Block*
711 statements() const
712 { return this->statements_; }
713
714 // Return the location.
715 source_location
716 location() const
717 { return this->location_; }
718
719 // Whether this clause may fall through to the statement which
720 // follows the overall select statement.
721 bool
722 may_fall_through() const;
723
724 // Return a tree for the statements to execute.
725 tree
726 get_statements_tree(Translate_context*);
727
728 private:
729 // The channel.
730 Expression* channel_;
731 // The value to send or the variable to set.
732 Expression* val_;
733 // The variable to initialize, for "case a := <- ch".
734 Named_object* var_;
735 // The statements to execute.
736 Block* statements_;
737 // The location of this clause.
738 source_location location_;
739 // Whether this is a send or a receive.
740 bool is_send_;
741 // Whether this is the default.
742 bool is_default_;
743 // Whether this has been lowered.
744 bool is_lowered_;
745 };
746
747 void
748 add_clause_tree(Translate_context*, int, Select_clause*, Unnamed_label*,
749 tree*);
750
751 typedef std::vector<Select_clause> Clauses;
752
753 Clauses clauses_;
754 };
755
756 // A select statement.
757
758 class Select_statement : public Statement
759 {
760 public:
761 Select_statement(source_location location)
762 : Statement(STATEMENT_SELECT, location),
763 clauses_(NULL), break_label_(NULL), is_lowered_(false)
764 { }
765
766 // Add the clauses.
767 void
768 add_clauses(Select_clauses* clauses)
769 {
770 gcc_assert(this->clauses_ == NULL);
771 this->clauses_ = clauses;
772 }
773
774 // Return the break label for this select statement.
775 Unnamed_label*
776 break_label();
777
778 protected:
779 int
780 do_traverse(Traverse* traverse)
781 { return this->clauses_->traverse(traverse); }
782
783 Statement*
784 do_lower(Gogo*, Block*);
785
786 void
787 do_determine_types()
788 { this->clauses_->determine_types(); }
789
790 bool
791 do_may_fall_through() const
792 { return this->clauses_->may_fall_through(); }
793
794 tree
795 do_get_tree(Translate_context*);
796
797 private:
798 // The select clauses.
799 Select_clauses* clauses_;
800 // The break label.
801 Unnamed_label* break_label_;
802 // Whether this statement has been lowered.
803 bool is_lowered_;
804 };
805
806 // A statement which requires a thunk: go or defer.
807
808 class Thunk_statement : public Statement
809 {
810 public:
811 Thunk_statement(Statement_classification, Call_expression*,
812 source_location);
813
814 // Return the call expression.
815 Expression*
816 call()
817 { return this->call_; }
818
819 // Simplify a go or defer statement so that it only uses a single
820 // parameter.
821 bool
822 simplify_statement(Gogo*, Block*);
823
824 protected:
825 int
826 do_traverse(Traverse* traverse);
827
828 bool
829 do_traverse_assignments(Traverse_assignments*);
830
831 void
832 do_determine_types();
833
834 void
835 do_check_types(Gogo*);
836
837 // Return the function and argument trees for the call.
838 void
839 get_fn_and_arg(Translate_context*, tree* pfn, tree* parg);
840
841 private:
842 // Return whether this is a simple go statement.
843 bool
844 is_simple(Function_type*) const;
845
846 // Build the struct to use for a complex case.
847 Struct_type*
848 build_struct(Function_type* fntype);
849
850 // Build the thunk.
851 void
852 build_thunk(Gogo*, const std::string&, Function_type* fntype);
853
854 // The field name used in the thunk structure for the function
855 // pointer.
856 static const char* const thunk_field_fn;
857
858 // The field name used in the thunk structure for the receiver, if
859 // there is one.
860 static const char* const thunk_field_receiver;
861
862 // Set the name to use for thunk field N.
863 void
864 thunk_field_param(int n, char* buf, size_t buflen);
865
866 // The function call to be executed in a separate thread (go) or
867 // later (defer).
868 Expression* call_;
869 // The type used for a struct to pass to a thunk, if this is not a
870 // simple call.
871 Struct_type* struct_type_;
872 };
873
874 // A go statement.
875
876 class Go_statement : public Thunk_statement
877 {
878 public:
879 Go_statement(Call_expression* call, source_location location)
880 : Thunk_statement(STATEMENT_GO, call, location)
881 { }
882
883 protected:
884 tree
885 do_get_tree(Translate_context*);
886 };
887
888 // A defer statement.
889
890 class Defer_statement : public Thunk_statement
891 {
892 public:
893 Defer_statement(Call_expression* call, source_location location)
894 : Thunk_statement(STATEMENT_DEFER, call, location)
895 { }
896
897 protected:
898 tree
899 do_get_tree(Translate_context*);
900 };
901
902 // A label statement.
903
904 class Label_statement : public Statement
905 {
906 public:
907 Label_statement(Label* label, source_location location)
908 : Statement(STATEMENT_LABEL, location),
909 label_(label)
910 { }
911
912 // Return the label itself.
913 const Label*
914 label() const
915 { return this->label_; }
916
917 protected:
918 int
919 do_traverse(Traverse*);
920
921 tree
922 do_get_tree(Translate_context*);
923
924 private:
925 // The label.
926 Label* label_;
927 };
928
929 // A for statement.
930
931 class For_statement : public Statement
932 {
933 public:
934 For_statement(Block* init, Expression* cond, Block* post,
935 source_location location)
936 : Statement(STATEMENT_FOR, location),
937 init_(init), cond_(cond), post_(post), statements_(NULL),
938 break_label_(NULL), continue_label_(NULL)
939 { }
940
941 // Add the statements.
942 void
943 add_statements(Block* statements)
944 {
945 gcc_assert(this->statements_ == NULL);
946 this->statements_ = statements;
947 }
948
949 // Return the break label for this for statement.
950 Unnamed_label*
951 break_label();
952
953 // Return the continue label for this for statement.
954 Unnamed_label*
955 continue_label();
956
957 // Set the break and continue labels for this statement.
958 void
959 set_break_continue_labels(Unnamed_label* break_label,
960 Unnamed_label* continue_label);
961
962 protected:
963 int
964 do_traverse(Traverse*);
965
966 bool
967 do_traverse_assignments(Traverse_assignments*)
968 { gcc_unreachable(); }
969
970 Statement*
971 do_lower(Gogo*, Block*);
972
973 tree
974 do_get_tree(Translate_context*)
975 { gcc_unreachable(); }
976
977 private:
978 // The initialization statements. This may be NULL.
979 Block* init_;
980 // The condition. This may be NULL.
981 Expression* cond_;
982 // The statements to run after each iteration. This may be NULL.
983 Block* post_;
984 // The statements in the loop itself.
985 Block* statements_;
986 // The break label, if needed.
987 Unnamed_label* break_label_;
988 // The continue label, if needed.
989 Unnamed_label* continue_label_;
990 };
991
992 // A for statement over a range clause.
993
994 class For_range_statement : public Statement
995 {
996 public:
997 For_range_statement(Expression* index_var, Expression* value_var,
998 Expression* range, source_location location)
999 : Statement(STATEMENT_FOR_RANGE, location),
1000 index_var_(index_var), value_var_(value_var), range_(range),
1001 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1002 { }
1003
1004 // Add the statements.
1005 void
1006 add_statements(Block* statements)
1007 {
1008 gcc_assert(this->statements_ == NULL);
1009 this->statements_ = statements;
1010 }
1011
1012 // Return the break label for this for statement.
1013 Unnamed_label*
1014 break_label();
1015
1016 // Return the continue label for this for statement.
1017 Unnamed_label*
1018 continue_label();
1019
1020 protected:
1021 int
1022 do_traverse(Traverse*);
1023
1024 bool
1025 do_traverse_assignments(Traverse_assignments*)
1026 { gcc_unreachable(); }
1027
1028 Statement*
1029 do_lower(Gogo*, Block*);
1030
1031 tree
1032 do_get_tree(Translate_context*)
1033 { gcc_unreachable(); }
1034
1035 private:
1036 Expression*
1037 make_range_ref(Named_object*, Temporary_statement*, source_location);
1038
1039 Expression*
1040 call_builtin(Gogo*, const char* funcname, Expression* arg, source_location);
1041
1042 void
1043 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1044 Temporary_statement*, Temporary_statement*,
1045 Block**, Expression**, Block**, Block**);
1046
1047 void
1048 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1049 Temporary_statement*, Temporary_statement*,
1050 Block**, Expression**, Block**, Block**);
1051
1052 void
1053 lower_range_map(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1054 Temporary_statement*, Temporary_statement*,
1055 Block**, Expression**, Block**, Block**);
1056
1057 void
1058 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1059 Temporary_statement*, Temporary_statement*,
1060 Temporary_statement*, Block**, Expression**, Block**,
1061 Block**);
1062
1063 // The variable which is set to the index value.
1064 Expression* index_var_;
1065 // The variable which is set to the element value. This may be
1066 // NULL.
1067 Expression* value_var_;
1068 // The expression we are ranging over.
1069 Expression* range_;
1070 // The statements in the block.
1071 Block* statements_;
1072 // The break label, if needed.
1073 Unnamed_label* break_label_;
1074 // The continue label, if needed.
1075 Unnamed_label* continue_label_;
1076 };
1077
1078 // Class Case_clauses holds the clauses of a switch statement. This
1079 // is built by the parser.
1080
1081 class Case_clauses
1082 {
1083 public:
1084 Case_clauses()
1085 : clauses_()
1086 { }
1087
1088 // Add a new clause. CASES is a list of case expressions; it may be
1089 // NULL. IS_DEFAULT is true if this is the default case.
1090 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1091 // after the statements the case clause should fall through to the
1092 // next clause.
1093 void
1094 add(Expression_list* cases, bool is_default, Block* statements,
1095 bool is_fallthrough, source_location location)
1096 {
1097 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1098 is_fallthrough, location));
1099 }
1100
1101 // Return whether there are no clauses.
1102 bool
1103 empty() const
1104 { return this->clauses_.empty(); }
1105
1106 // Traverse the case clauses.
1107 int
1108 traverse(Traverse*);
1109
1110 // Lower for a nonconstant switch.
1111 void
1112 lower(Block*, Temporary_statement*, Unnamed_label*) const;
1113
1114 // Determine types of expressions. The Type parameter is the type
1115 // of the switch value.
1116 void
1117 determine_types(Type*);
1118
1119 // Check types. The Type parameter is the type of the switch value.
1120 bool
1121 check_types(Type*);
1122
1123 // Return true if all the clauses are constant values.
1124 bool
1125 is_constant() const;
1126
1127 // Return true if these clauses may fall through to the statements
1128 // following the switch statement.
1129 bool
1130 may_fall_through() const;
1131
1132 // Return the body of a SWITCH_EXPR when all the clauses are
1133 // constants.
1134 tree
1135 get_constant_tree(Translate_context*, Unnamed_label* break_label) const;
1136
1137 private:
1138 // For a constant tree we need to keep a record of constants we have
1139 // already seen. Note that INTEGER_CST trees are interned.
1140 typedef Unordered_set(tree) Case_constants;
1141
1142 // One case clause.
1143 class Case_clause
1144 {
1145 public:
1146 Case_clause()
1147 : cases_(NULL), statements_(NULL), is_default_(false),
1148 is_fallthrough_(false), location_(UNKNOWN_LOCATION)
1149 { }
1150
1151 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1152 bool is_fallthrough, source_location location)
1153 : cases_(cases), statements_(statements), is_default_(is_default),
1154 is_fallthrough_(is_fallthrough), location_(location)
1155 { }
1156
1157 // Whether this clause falls through to the next clause.
1158 bool
1159 is_fallthrough() const
1160 { return this->is_fallthrough_; }
1161
1162 // Whether this is the default.
1163 bool
1164 is_default() const
1165 { return this->is_default_; }
1166
1167 // The location of this clause.
1168 source_location
1169 location() const
1170 { return this->location_; }
1171
1172 // Traversal.
1173 int
1174 traverse(Traverse*);
1175
1176 // Lower for a nonconstant switch.
1177 void
1178 lower(Block*, Temporary_statement*, Unnamed_label*, Unnamed_label*) const;
1179
1180 // Determine types.
1181 void
1182 determine_types(Type*);
1183
1184 // Check types.
1185 bool
1186 check_types(Type*);
1187
1188 // Return true if all the case expressions are constant.
1189 bool
1190 is_constant() const;
1191
1192 // Return true if this clause may fall through to execute the
1193 // statements following the switch statement. This is not the
1194 // same as whether this clause falls through to the next clause.
1195 bool
1196 may_fall_through() const;
1197
1198 // Build up the body of a SWITCH_EXPR when the case expressions
1199 // are constant.
1200 void
1201 get_constant_tree(Translate_context*, Unnamed_label* break_label,
1202 Case_constants* case_constants, tree* stmt_list) const;
1203
1204 private:
1205 // The list of case expressions.
1206 Expression_list* cases_;
1207 // The statements to execute.
1208 Block* statements_;
1209 // Whether this is the default case.
1210 bool is_default_;
1211 // Whether this falls through after the statements.
1212 bool is_fallthrough_;
1213 // The location of this case clause.
1214 source_location location_;
1215 };
1216
1217 friend class Case_clause;
1218
1219 // The type of the list of clauses.
1220 typedef std::vector<Case_clause> Clauses;
1221
1222 // All the case clauses.
1223 Clauses clauses_;
1224 };
1225
1226 // A switch statement.
1227
1228 class Switch_statement : public Statement
1229 {
1230 public:
1231 Switch_statement(Expression* val, source_location location)
1232 : Statement(STATEMENT_SWITCH, location),
1233 val_(val), clauses_(NULL), break_label_(NULL)
1234 { }
1235
1236 // Add the clauses.
1237 void
1238 add_clauses(Case_clauses* clauses)
1239 {
1240 gcc_assert(this->clauses_ == NULL);
1241 this->clauses_ = clauses;
1242 }
1243
1244 // Return the break label for this switch statement.
1245 Unnamed_label*
1246 break_label();
1247
1248 protected:
1249 int
1250 do_traverse(Traverse*);
1251
1252 Statement*
1253 do_lower(Gogo*, Block*);
1254
1255 tree
1256 do_get_tree(Translate_context*)
1257 { gcc_unreachable(); }
1258
1259 private:
1260 // The value to switch on. This may be NULL.
1261 Expression* val_;
1262 // The case clauses.
1263 Case_clauses* clauses_;
1264 // The break label, if needed.
1265 Unnamed_label* break_label_;
1266 };
1267
1268 // Class Type_case_clauses holds the clauses of a type switch
1269 // statement. This is built by the parser.
1270
1271 class Type_case_clauses
1272 {
1273 public:
1274 Type_case_clauses()
1275 : clauses_()
1276 { }
1277
1278 // Add a new clause. TYPE is the type for this clause; it may be
1279 // NULL. IS_FALLTHROUGH is true if this falls through to the next
1280 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
1281 // if this is the default case. STATEMENTS is a block of
1282 // statements; it may be NULL.
1283 void
1284 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
1285 source_location location)
1286 {
1287 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
1288 statements, location));
1289 }
1290
1291 // Return whether there are no clauses.
1292 bool
1293 empty() const
1294 { return this->clauses_.empty(); }
1295
1296 // Traverse the type case clauses.
1297 int
1298 traverse(Traverse*);
1299
1300 // Check for duplicates.
1301 void
1302 check_duplicates() const;
1303
1304 // Lower to if and goto statements.
1305 void
1306 lower(Block*, Temporary_statement* descriptor_temp,
1307 Unnamed_label* break_label) const;
1308
1309 private:
1310 // One type case clause.
1311 class Type_case_clause
1312 {
1313 public:
1314 Type_case_clause()
1315 : type_(NULL), statements_(NULL), is_default_(false),
1316 location_(UNKNOWN_LOCATION)
1317 { }
1318
1319 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
1320 Block* statements, source_location location)
1321 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
1322 is_default_(is_default), location_(location)
1323 { }
1324
1325 // The type.
1326 Type*
1327 type() const
1328 { return this->type_; }
1329
1330 // Whether this is the default.
1331 bool
1332 is_default() const
1333 { return this->is_default_; }
1334
1335 // The location of this type clause.
1336 source_location
1337 location() const
1338 { return this->location_; }
1339
1340 // Traversal.
1341 int
1342 traverse(Traverse*);
1343
1344 // Lower to if and goto statements.
1345 void
1346 lower(Block*, Temporary_statement* descriptor_temp,
1347 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
1348
1349 private:
1350 // The type for this type clause.
1351 Type* type_;
1352 // The statements to execute.
1353 Block* statements_;
1354 // Whether this falls through--this is true for "case T1, T2".
1355 bool is_fallthrough_;
1356 // Whether this is the default case.
1357 bool is_default_;
1358 // The location of this type case clause.
1359 source_location location_;
1360 };
1361
1362 friend class Type_case_clause;
1363
1364 // The type of the list of type clauses.
1365 typedef std::vector<Type_case_clause> Type_clauses;
1366
1367 // All the type case clauses.
1368 Type_clauses clauses_;
1369 };
1370
1371 // A type switch statement.
1372
1373 class Type_switch_statement : public Statement
1374 {
1375 public:
1376 Type_switch_statement(Named_object* var, Expression* expr,
1377 source_location location)
1378 : Statement(STATEMENT_TYPE_SWITCH, location),
1379 var_(var), expr_(expr), clauses_(NULL), break_label_(NULL)
1380 { gcc_assert(var == NULL || expr == NULL); }
1381
1382 // Add the clauses.
1383 void
1384 add_clauses(Type_case_clauses* clauses)
1385 {
1386 gcc_assert(this->clauses_ == NULL);
1387 this->clauses_ = clauses;
1388 }
1389
1390 // Return the break label for this type switch statement.
1391 Unnamed_label*
1392 break_label();
1393
1394 protected:
1395 int
1396 do_traverse(Traverse*);
1397
1398 Statement*
1399 do_lower(Gogo*, Block*);
1400
1401 tree
1402 do_get_tree(Translate_context*)
1403 { gcc_unreachable(); }
1404
1405 private:
1406 // Get the type descriptor.
1407 tree
1408 get_type_descriptor(Translate_context*, Type*, tree);
1409
1410 // The variable holding the value we are switching on.
1411 Named_object* var_;
1412 // The expression we are switching on if there is no variable.
1413 Expression* expr_;
1414 // The type case clauses.
1415 Type_case_clauses* clauses_;
1416 // The break label, if needed.
1417 Unnamed_label* break_label_;
1418 };
1419
1420 #endif // !defined(GO_STATEMENTS_H)