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