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