c: Add support for byte arrays in C2Y
[official-gcc.git] / gcc / go / gofrontend / statements.h
blob530011cfc9050f41b71ef0d66fffcae5336eac4c
1 // statements.h -- Go frontend statements. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #ifndef GO_STATEMENTS_H
8 #define GO_STATEMENTS_H
10 #include "operator.h"
12 class Gogo;
13 class Traverse;
14 class Statement_inserter;
15 class Block;
16 class Function;
17 class Unnamed_label;
18 class Export_function_body;
19 class Import_function_body;
20 class Assignment_statement;
21 class Temporary_statement;
22 class Variable_declaration_statement;
23 class Expression_statement;
24 class Block_statement;
25 class Return_statement;
26 class Thunk_statement;
27 class Defer_statement;
28 class Goto_statement;
29 class Goto_unnamed_statement;
30 class Label_statement;
31 class Unnamed_label_statement;
32 class If_statement;
33 class For_statement;
34 class For_range_statement;
35 class Switch_statement;
36 class Type_switch_statement;
37 class Send_statement;
38 class Select_statement;
39 class Variable;
40 class Named_object;
41 class Label;
42 class Translate_context;
43 class Expression;
44 class Expression_list;
45 class Struct_type;
46 class Call_expression;
47 class Map_index_expression;
48 class Receive_expression;
49 class Case_clauses;
50 class Type_case_clauses;
51 class Select_clauses;
52 class Typed_identifier_list;
53 class Bexpression;
54 class Bstatement;
55 class Bvariable;
56 class Ast_dump_context;
58 // A single statement.
60 class Statement
62 public:
63 // The types of statements.
64 enum Statement_classification
66 STATEMENT_ERROR,
67 STATEMENT_VARIABLE_DECLARATION,
68 STATEMENT_TEMPORARY,
69 STATEMENT_ASSIGNMENT,
70 STATEMENT_EXPRESSION,
71 STATEMENT_BLOCK,
72 STATEMENT_GO,
73 STATEMENT_DEFER,
74 STATEMENT_RETURN,
75 STATEMENT_BREAK_OR_CONTINUE,
76 STATEMENT_GOTO,
77 STATEMENT_GOTO_UNNAMED,
78 STATEMENT_LABEL,
79 STATEMENT_UNNAMED_LABEL,
80 STATEMENT_IF,
81 STATEMENT_CONSTANT_SWITCH,
82 STATEMENT_SEND,
83 STATEMENT_SELECT,
85 // These statements types are created by the parser, but they
86 // disappear during the lowering pass.
87 STATEMENT_ASSIGNMENT_OPERATION,
88 STATEMENT_TUPLE_ASSIGNMENT,
89 STATEMENT_TUPLE_MAP_ASSIGNMENT,
90 STATEMENT_TUPLE_RECEIVE_ASSIGNMENT,
91 STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT,
92 STATEMENT_INCDEC,
93 STATEMENT_FOR,
94 STATEMENT_FOR_RANGE,
95 STATEMENT_SWITCH,
96 STATEMENT_TYPE_SWITCH
99 Statement(Statement_classification, Location);
101 virtual ~Statement();
103 // Make a variable declaration.
104 static Statement*
105 make_variable_declaration(Named_object*);
107 // Make a statement which creates a temporary variable and
108 // initializes it to an expression. The block is used if the
109 // temporary variable has to be explicitly destroyed; the variable
110 // must still be added to the block. References to the temporary
111 // variable may be constructed using make_temporary_reference.
112 // Either the type or the initialization expression may be NULL, but
113 // not both.
114 static Temporary_statement*
115 make_temporary(Type*, Expression*, Location);
117 // Make an assignment statement.
118 static Assignment_statement*
119 make_assignment(Expression*, Expression*, Location);
121 // Make an assignment operation (+=, etc.).
122 static Statement*
123 make_assignment_operation(Operator, Expression*, Expression*,
124 Location);
126 // Make a tuple assignment statement.
127 static Statement*
128 make_tuple_assignment(Expression_list*, Expression_list*, Location);
130 // Make an assignment from a map index to a pair of variables.
131 static Statement*
132 make_tuple_map_assignment(Expression* val, Expression* present,
133 Expression*, Location);
135 // Make an assignment from a nonblocking receive to a pair of
136 // variables.
137 static Statement*
138 make_tuple_receive_assignment(Expression* val, Expression* closed,
139 Expression* channel, Location);
141 // Make an assignment from a type guard to a pair of variables.
142 static Statement*
143 make_tuple_type_guard_assignment(Expression* val, Expression* ok,
144 Expression* expr, Type* type,
145 Location);
147 // Make an expression statement from an Expression. IS_IGNORED is
148 // true if the value is being explicitly ignored, as in an
149 // assignment to _.
150 static Statement*
151 make_statement(Expression*, bool is_ignored);
153 // Make a block statement from a Block. This is an embedded list of
154 // statements which may also include variable definitions.
155 static Block_statement*
156 make_block_statement(Block*, Location);
158 // Make an increment statement.
159 static Statement*
160 make_inc_statement(Expression*);
162 // Make a decrement statement.
163 static Statement*
164 make_dec_statement(Expression*);
166 // Make a go statement.
167 static Statement*
168 make_go_statement(Call_expression* call, Location);
170 // Make a defer statement.
171 static Statement*
172 make_defer_statement(Call_expression* call, Location);
174 // Make a return statement. FUNCTION is a backpointer to the
175 // function that this statement is returning from.
176 static Return_statement*
177 make_return_statement(Named_object* function, Expression_list*, Location);
179 // Make a statement that returns the result of a call expression.
180 // If the call does not return any results, this just returns the
181 // call expression as a statement, assuming that the function will
182 // end immediately afterward.
183 static Statement*
184 make_return_from_call(Named_object* function, Call_expression*, Location);
186 // Make a break statement.
187 static Statement*
188 make_break_statement(Unnamed_label* label, Location);
190 // Make a continue statement.
191 static Statement*
192 make_continue_statement(Unnamed_label* label, Location);
194 // Make a goto statement.
195 static Statement*
196 make_goto_statement(Label* label, Location);
198 // Make a goto statement to an unnamed label.
199 static Statement*
200 make_goto_unnamed_statement(Unnamed_label* label, Location);
202 // Make a label statement--where the label is defined.
203 static Statement*
204 make_label_statement(Label* label, Location);
206 // Make an unnamed label statement--where the label is defined.
207 static Statement*
208 make_unnamed_label_statement(Unnamed_label* label);
210 // Make an if statement.
211 static Statement*
212 make_if_statement(Expression* cond, Block* then_block, Block* else_block,
213 Location);
215 // Make a switch statement.
216 static Switch_statement*
217 make_switch_statement(Expression* switch_val, Location);
219 // Make a type switch statement.
220 static Type_switch_statement*
221 make_type_switch_statement(Expression*, Location);
223 // Make a send statement.
224 static Send_statement*
225 make_send_statement(Expression* channel, Expression* val, Location);
227 // Make a select statement.
228 static Select_statement*
229 make_select_statement(Location);
231 // Make a for statement.
232 static For_statement*
233 make_for_statement(Block* init, Expression* cond, Block* post,
234 Location location);
236 // Make a for statement with a range clause.
237 static For_range_statement*
238 make_for_range_statement(Expression* index_var, Expression* value_var,
239 Expression* range, Location);
241 // Return the statement classification.
242 Statement_classification
243 classification() const
244 { return this->classification_; }
246 // Get the statement location.
247 Location
248 location() const
249 { return this->location_; }
251 // Traverse the tree.
253 traverse(Block*, size_t* index, Traverse*);
255 // Traverse the contents of this statement--the expressions and
256 // statements which it contains.
258 traverse_contents(Traverse*);
260 // Lower a statement. This is called immediately after parsing to
261 // simplify statements for further processing. It returns the same
262 // Statement or a new one. FUNCTION is the function containing this
263 // statement. BLOCK is the block containing this statement.
264 // INSERTER can be used to insert new statements before this one.
265 Statement*
266 lower(Gogo* gogo, Named_object* function, Block* block,
267 Statement_inserter* inserter)
268 { return this->do_lower(gogo, function, block, inserter); }
270 // Flatten a statement. This is called immediately after the order of
271 // evaluation rules are applied to statements. It returns the same
272 // Statement or a new one. FUNCTION is the function containing this
273 // statement. BLOCK is the block containing this statement.
274 // INSERTER can be used to insert new statements before this one.
275 Statement*
276 flatten(Gogo* gogo, Named_object* function, Block* block,
277 Statement_inserter* inserter)
278 { return this->do_flatten(gogo, function, block, inserter); }
280 // Set type information for unnamed constants.
281 void
282 determine_types(Gogo*);
284 // Check types in a statement. This simply checks that any
285 // expressions used by the statement have the right type.
286 void
287 check_types(Gogo* gogo)
288 { this->do_check_types(gogo); }
290 // Return the cost of this statement for inlining purposes.
292 inlining_cost()
293 { return this->do_inlining_cost(); }
295 // Export data for this statement to BODY.
296 void
297 export_statement(Export_function_body* efb)
298 { this->do_export_statement(efb); }
300 // Make implicit type conversions explicit.
301 void
302 add_conversions()
303 { this->do_add_conversions(); }
305 // Read a statement from export data. The location should be used
306 // for the returned statement. Errors should be reported using the
307 // Import_function_body's location method.
308 static Statement*
309 import_statement(Import_function_body*, Location);
311 // Return whether this is a block statement.
312 bool
313 is_block_statement() const
314 { return this->classification_ == STATEMENT_BLOCK; }
316 // If this is an assignment statement, return it. Otherwise return
317 // NULL.
318 Assignment_statement*
319 assignment_statement()
321 return this->convert<Assignment_statement, STATEMENT_ASSIGNMENT>();
324 // If this is an temporary statement, return it. Otherwise return
325 // NULL.
326 Temporary_statement*
327 temporary_statement()
329 return this->convert<Temporary_statement, STATEMENT_TEMPORARY>();
332 // If this is a variable declaration statement, return it.
333 // Otherwise return NULL.
334 Variable_declaration_statement*
335 variable_declaration_statement()
337 return this->convert<Variable_declaration_statement,
338 STATEMENT_VARIABLE_DECLARATION>();
341 // If this is an expression statement, return it. Otherwise return
342 // NULL.
343 Expression_statement*
344 expression_statement()
346 return this->convert<Expression_statement, STATEMENT_EXPRESSION>();
349 // If this is an block statement, return it. Otherwise return
350 // NULL.
351 Block_statement*
352 block_statement()
353 { return this->convert<Block_statement, STATEMENT_BLOCK>(); }
355 // If this is a return statement, return it. Otherwise return NULL.
356 Return_statement*
357 return_statement()
358 { return this->convert<Return_statement, STATEMENT_RETURN>(); }
360 // If this is a thunk statement (a go or defer statement), return
361 // it. Otherwise return NULL.
362 Thunk_statement*
363 thunk_statement();
365 // If this is a defer statement, return it. Otherwise return NULL.
366 Defer_statement*
367 defer_statement()
368 { return this->convert<Defer_statement, STATEMENT_DEFER>(); }
370 // If this is a goto statement, return it. Otherwise return NULL.
371 Goto_statement*
372 goto_statement()
373 { return this->convert<Goto_statement, STATEMENT_GOTO>(); }
375 // If this is a goto_unnamed statement, return it. Otherwise return NULL.
376 Goto_unnamed_statement*
377 goto_unnamed_statement()
378 { return this->convert<Goto_unnamed_statement, STATEMENT_GOTO_UNNAMED>(); }
380 // If this is a label statement, return it. Otherwise return NULL.
381 Label_statement*
382 label_statement()
383 { return this->convert<Label_statement, STATEMENT_LABEL>(); }
385 // If this is an unnamed_label statement, return it. Otherwise return NULL.
386 Unnamed_label_statement*
387 unnamed_label_statement()
388 { return this->convert<Unnamed_label_statement, STATEMENT_UNNAMED_LABEL>(); }
390 // If this is an if statement, return it. Otherwise return NULL.
391 If_statement*
392 if_statement()
393 { return this->convert<If_statement, STATEMENT_IF>(); }
395 // If this is a for statement, return it. Otherwise return NULL.
396 For_statement*
397 for_statement()
398 { return this->convert<For_statement, STATEMENT_FOR>(); }
400 // If this is a for statement over a range clause, return it.
401 // Otherwise return NULL.
402 For_range_statement*
403 for_range_statement()
404 { return this->convert<For_range_statement, STATEMENT_FOR_RANGE>(); }
406 // If this is a switch statement, return it. Otherwise return NULL.
407 Switch_statement*
408 switch_statement()
409 { return this->convert<Switch_statement, STATEMENT_SWITCH>(); }
411 // If this is a type switch statement, return it. Otherwise return
412 // NULL.
413 Type_switch_statement*
414 type_switch_statement()
415 { return this->convert<Type_switch_statement, STATEMENT_TYPE_SWITCH>(); }
417 // If this is a send statement, return it. Otherwise return NULL.
418 Send_statement*
419 send_statement()
420 { return this->convert<Send_statement, STATEMENT_SEND>(); }
422 // If this is a select statement, return it. Otherwise return NULL.
423 Select_statement*
424 select_statement()
425 { return this->convert<Select_statement, STATEMENT_SELECT>(); }
427 // Return true if this statement may fall through--if after
428 // executing this statement we may go on to execute the following
429 // statement, if any.
430 bool
431 may_fall_through() const
432 { return this->do_may_fall_through(); }
434 // Convert the statement to the backend representation.
435 Bstatement*
436 get_backend(Translate_context*);
438 // Dump AST representation of a statement to a dump context.
439 void
440 dump_statement(Ast_dump_context*) const;
442 protected:
443 // Implemented by child class: traverse the tree.
444 virtual int
445 do_traverse(Traverse*) = 0;
447 // Implemented by the child class: lower this statement to a simpler
448 // one.
449 virtual Statement*
450 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
451 { return this; }
453 // Implemented by the child class: lower this statement to a simpler
454 // one.
455 virtual Statement*
456 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*)
457 { return this; }
459 // Implemented by child class: set type information for unnamed
460 // constants. Any statement which includes an expression needs to
461 // implement this.
462 virtual void
463 do_determine_types(Gogo*)
466 // Implemented by child class: check types of expressions used in a
467 // statement.
468 virtual void
469 do_check_types(Gogo*)
472 // Implemented by child class: return the cost of this statement for
473 // inlining. The default cost is high, so we only need to define
474 // this method for statements that can be inlined.
475 virtual int
476 do_inlining_cost()
477 { return 0x100000; }
479 // Implemented by child class: write export data for this statement
480 // to the string. This need only be implemented by classes that
481 // implement do_inlining_cost with a reasonable value.
482 virtual void
483 do_export_statement(Export_function_body*)
484 { go_unreachable(); }
486 // Implemented by child class: return true if this statement may
487 // fall through.
488 virtual bool
489 do_may_fall_through() const
490 { return true; }
492 // Implemented by child class: convert to backend representation.
493 virtual Bstatement*
494 do_get_backend(Translate_context*) = 0;
496 // Implemented by child class: dump ast representation.
497 virtual void
498 do_dump_statement(Ast_dump_context*) const = 0;
500 // Implemented by child class: make implicit conversions explicit.
501 virtual void
502 do_add_conversions()
505 // Traverse an expression in a statement.
507 traverse_expression(Traverse*, Expression**);
509 // Traverse an expression list in a statement. The Expression_list
510 // may be NULL.
512 traverse_expression_list(Traverse*, Expression_list*);
514 // Traverse a type in a statement.
516 traverse_type(Traverse*, Type*);
518 // For children to call when they detect that they are in error.
519 void
520 set_is_error();
522 // For children to call to report an error conveniently.
523 void
524 report_error(const char*);
526 // For children to return an error statement from lower().
527 static Statement*
528 make_error_statement(Location);
530 private:
531 // Convert to the desired statement classification, or return NULL.
532 // This is a controlled dynamic cast.
533 template<typename Statement_class, Statement_classification sc>
534 Statement_class*
535 convert()
537 return (this->classification_ == sc
538 ? static_cast<Statement_class*>(this)
539 : NULL);
542 template<typename Statement_class, Statement_classification sc>
543 const Statement_class*
544 convert() const
546 return (this->classification_ == sc
547 ? static_cast<const Statement_class*>(this)
548 : NULL);
551 // The statement classification.
552 Statement_classification classification_;
553 // The location in the input file of the start of this statement.
554 Location location_;
557 // An assignment statement.
559 class Assignment_statement : public Statement
561 public:
562 Assignment_statement(Expression* lhs, Expression* rhs,
563 Location location)
564 : Statement(STATEMENT_ASSIGNMENT, location),
565 lhs_(lhs), rhs_(rhs), omit_write_barrier_(false)
568 Expression*
569 lhs() const
570 { return this->lhs_; }
572 Expression*
573 rhs() const
574 { return this->rhs_; }
576 bool
577 omit_write_barrier() const
578 { return this->omit_write_barrier_; }
580 void
581 set_omit_write_barrier()
582 { this->omit_write_barrier_ = true; }
584 // Check if we can assign RHS to LHS. If we can, return true. If
585 // we can't, report an error and return false.
586 static bool
587 check_assignment_types(Expression* lhs, Type* rhs_type, Location);
589 protected:
591 do_traverse(Traverse* traverse);
593 virtual Statement*
594 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
596 void
597 do_determine_types(Gogo*);
599 void
600 do_check_types(Gogo*);
603 do_inlining_cost()
604 { return 1; }
606 void
607 do_export_statement(Export_function_body*);
609 Statement*
610 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
612 Bstatement*
613 do_get_backend(Translate_context*);
615 void
616 do_dump_statement(Ast_dump_context*) const;
618 void
619 do_add_conversions();
621 private:
622 // Left hand side--the lvalue.
623 Expression* lhs_;
624 // Right hand side--the rvalue.
625 Expression* rhs_;
626 // True if we can omit a write barrier from this assignment.
627 bool omit_write_barrier_;
630 // A statement which creates and initializes a temporary variable.
632 class Temporary_statement : public Statement
634 public:
635 Temporary_statement(Type* type, Expression* init, Location location)
636 : Statement(STATEMENT_TEMPORARY, location),
637 type_(type), init_(init), bvariable_(NULL), is_address_taken_(false),
638 value_escapes_(false), assigned_(false), uses_(0)
641 // Return the type of the temporary variable.
642 Type*
643 type() const;
645 // Return the initializer if there is one.
646 Expression*
647 init() const
648 { return this->init_; }
650 // Set the initializer.
651 void
652 set_init(Expression* expr)
653 { this->init_ = expr; }
655 // Whether something takes the address of this temporary
656 // variable.
657 bool
658 is_address_taken()
659 { return this->is_address_taken_; }
661 // Record that something takes the address of this temporary
662 // variable.
663 void
664 set_is_address_taken()
665 { this->is_address_taken_ = true; }
667 // Whether the value escapes.
668 bool
669 value_escapes() const
670 { return this->value_escapes_; }
672 // Record that the value escapes.
673 void
674 set_value_escapes()
675 { this->value_escapes_ = true; }
677 // Whether this temporary variable is assigned (after initialization).
678 bool
679 assigned()
680 { return this->assigned_; }
682 // Record that this temporary variable is assigned.
683 void
684 set_assigned()
685 { this->assigned_ = true; }
687 // Number of uses of this temporary variable.
689 uses()
690 { return this->uses_; }
692 // Add one use of this temporary variable.
693 void
694 add_use()
695 { this->uses_++; }
697 // Return the temporary variable. This should not be called until
698 // after the statement itself has been converted.
699 Bvariable*
700 get_backend_variable(Translate_context*) const;
702 // Import the declaration of a temporary.
703 static Statement*
704 do_import(Import_function_body*, Location);
706 protected:
708 do_traverse(Traverse*);
710 void
711 do_determine_types(Gogo*);
713 void
714 do_check_types(Gogo*);
717 do_inlining_cost()
718 { return 1; }
720 void
721 do_export_statement(Export_function_body*);
723 Statement*
724 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
726 Bstatement*
727 do_get_backend(Translate_context*);
729 void
730 do_dump_statement(Ast_dump_context*) const;
732 void
733 do_add_conversions();
735 private:
736 // The type of the temporary variable.
737 Type* type_;
738 // The initial value of the temporary variable. This may be NULL.
739 Expression* init_;
740 // The backend representation of the temporary variable.
741 Bvariable* bvariable_;
742 // True if something takes the address of this temporary variable.
743 bool is_address_taken_;
744 // True if the value assigned to this temporary variable escapes.
745 // This is used for select statements.
746 bool value_escapes_;
747 // True if this temporary variable is assigned (after initialization).
748 bool assigned_;
749 // Number of uses of this temporary variable.
750 int uses_;
753 // A variable declaration. This marks the point in the code where a
754 // variable is declared. The Variable is also attached to a Block.
756 class Variable_declaration_statement : public Statement
758 public:
759 Variable_declaration_statement(Named_object* var);
761 // The variable being declared.
762 Named_object*
763 var()
764 { return this->var_; }
766 // Import a variable declaration.
767 static Statement*
768 do_import(Import_function_body*, Location);
770 protected:
772 do_traverse(Traverse*);
774 void
775 do_determine_types(Gogo*);
777 Statement*
778 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
781 do_inlining_cost()
782 { return 1; }
784 void
785 do_export_statement(Export_function_body*);
787 Statement*
788 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
790 Bstatement*
791 do_get_backend(Translate_context*);
793 void
794 do_dump_statement(Ast_dump_context*) const;
796 void
797 do_add_conversions();
799 private:
800 Named_object* var_;
803 // A return statement.
805 class Return_statement : public Statement
807 public:
808 Return_statement(Named_object* function, Expression_list* vals,
809 Location location)
810 : Statement(STATEMENT_RETURN, location),
811 function_(function), vals_(vals), types_are_determined_(false),
812 is_lowered_(false)
815 // The list of values being returned. This may be NULL.
816 const Expression_list*
817 vals() const
818 { return this->vals_; }
820 protected:
822 do_traverse(Traverse* traverse)
823 { return this->traverse_expression_list(traverse, this->vals_); }
825 void
826 do_determine_types(Gogo*);
828 void
829 do_check_types(Gogo*);
831 Statement*
832 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
834 bool
835 do_may_fall_through() const
836 { return false; }
839 do_inlining_cost()
840 { return 1; }
842 void
843 do_export_statement(Export_function_body*);
845 Bstatement*
846 do_get_backend(Translate_context*);
848 void
849 do_dump_statement(Ast_dump_context*) const;
851 private:
852 // A backpointer to the function we are returning from.
853 Named_object* function_;
854 // Return values. This may be NULL.
855 Expression_list* vals_;
856 // True if types have been determined.
857 bool types_are_determined_;
858 // True if this statement has been lowered.
859 bool is_lowered_;
862 // An expression statement.
864 class Expression_statement : public Statement
866 public:
867 Expression_statement(Expression* expr, bool is_ignored);
869 Expression*
870 expr()
871 { return this->expr_; }
873 protected:
875 do_traverse(Traverse* traverse)
876 { return this->traverse_expression(traverse, &this->expr_); }
878 void
879 do_determine_types(Gogo*);
881 void
882 do_check_types(Gogo*);
884 bool
885 do_may_fall_through() const;
888 do_inlining_cost()
889 { return 0; }
891 void
892 do_export_statement(Export_function_body*);
894 Bstatement*
895 do_get_backend(Translate_context* context);
897 void
898 do_dump_statement(Ast_dump_context*) const;
900 private:
901 Expression* expr_;
902 // Whether the value of this expression is being explicitly ignored.
903 bool is_ignored_;
906 // A block statement--a list of statements which may include variable
907 // definitions.
909 class Block_statement : public Statement
911 public:
912 Block_statement(Block* block, Location location)
913 : Statement(STATEMENT_BLOCK, location),
914 block_(block), is_lowered_for_statement_(false)
917 // Return the actual block.
918 Block*
919 block() const
920 { return this->block_; }
922 void
923 set_is_lowered_for_statement()
924 { this->is_lowered_for_statement_ = true; }
926 bool
927 is_lowered_for_statement()
928 { return this->is_lowered_for_statement_; }
930 // Export a block for a block statement.
931 static void
932 export_block(Export_function_body*, Block*, bool is_lowered_for_statement);
934 // Import a block statement, returning the block.
935 // *IS_LOWERED_FOR_STATEMENT reports whether this block statement
936 // was lowered from a for statement.
937 static Block*
938 do_import(Import_function_body*, Location, bool* is_lowered_for_statement);
940 protected:
942 do_traverse(Traverse* traverse)
943 { return this->block_->traverse(traverse); }
945 void
946 do_determine_types(Gogo* gogo)
947 { this->block_->determine_types(gogo); }
950 do_inlining_cost()
951 { return 0; }
953 void
954 do_export_statement(Export_function_body*);
956 bool
957 do_may_fall_through() const
958 { return this->block_->may_fall_through(); }
960 Bstatement*
961 do_get_backend(Translate_context* context);
963 void
964 do_dump_statement(Ast_dump_context*) const;
966 private:
967 Block* block_;
968 // True if this block statement represents a lowered for statement.
969 bool is_lowered_for_statement_;
972 // A send statement.
974 class Send_statement : public Statement
976 public:
977 Send_statement(Expression* channel, Expression* val,
978 Location location)
979 : Statement(STATEMENT_SEND, location),
980 channel_(channel), val_(val)
983 Expression*
984 channel()
985 { return this->channel_; }
987 Expression*
988 val()
989 { return this->val_; }
991 protected:
993 do_traverse(Traverse* traverse);
995 void
996 do_determine_types(Gogo*);
998 void
999 do_check_types(Gogo*);
1001 Statement*
1002 do_flatten(Gogo*, Named_object*, Block*, Statement_inserter*);
1004 Bstatement*
1005 do_get_backend(Translate_context*);
1007 void
1008 do_dump_statement(Ast_dump_context*) const;
1010 void
1011 do_add_conversions();
1013 private:
1014 // The channel on which to send the value.
1015 Expression* channel_;
1016 // The value to send.
1017 Expression* val_;
1020 // Select_clauses holds the clauses of a select statement. This is
1021 // built by the parser.
1023 class Select_clauses
1025 public:
1026 Select_clauses()
1027 : clauses_()
1030 // Add a new clause. IS_SEND is true if this is a send clause,
1031 // false for a receive clause. For a send clause CHANNEL is the
1032 // channel and VAL is the value to send. For a receive clause
1033 // CHANNEL is the channel, VAL is either NULL or a Var_expression
1034 // for the variable to set, and CLOSED is either NULL or a
1035 // Var_expression to set to whether the channel is closed. If VAL
1036 // is NULL, VAR may be a variable to be initialized with the
1037 // received value, and CLOSEDVAR may be a variable to be initialized
1038 // with whether the channel is closed. IS_DEFAULT is true if this
1039 // is the default clause. STATEMENTS is the list of statements to
1040 // execute.
1041 void
1042 add(bool is_send, Expression* channel, Expression* val, Expression* closed,
1043 Named_object* var, Named_object* closedvar, bool is_default,
1044 Block* statements, Location location)
1046 this->clauses_.push_back(Select_clause(is_send, channel, val, closed, var,
1047 closedvar, is_default, statements,
1048 location));
1051 size_t
1052 size() const
1053 { return this->clauses_.size(); }
1055 bool
1056 has_default() const;
1058 // Traverse the select clauses.
1060 traverse(Traverse*);
1062 // Lower statements.
1063 void
1064 lower(Gogo*, Named_object*, Block*, Temporary_statement*,
1065 Temporary_statement*, int* send_count, int* recv_count);
1067 // Determine types.
1068 void
1069 determine_types(Gogo*);
1071 // Check types.
1072 void
1073 check_types();
1075 // Whether the select clauses may fall through to the statement
1076 // which follows the overall select statement.
1077 bool
1078 may_fall_through() const;
1080 // Convert to the backend representation.
1081 Bstatement*
1082 get_backend(Translate_context*, Temporary_statement* index,
1083 Unnamed_label* break_label, Location);
1085 // Dump AST representation.
1086 void
1087 dump_clauses(Ast_dump_context*) const;
1089 // A single clause.
1090 class Select_clause
1092 public:
1093 Select_clause()
1094 : channel_(NULL), val_(NULL), closed_(NULL), var_(NULL),
1095 closedvar_(NULL), statements_(NULL), is_send_(false),
1096 is_default_(false)
1099 Select_clause(bool is_send, Expression* channel, Expression* val,
1100 Expression* closed, Named_object* var,
1101 Named_object* closedvar, bool is_default, Block* statements,
1102 Location location)
1103 : channel_(channel), val_(val), closed_(closed), var_(var),
1104 closedvar_(closedvar), statements_(statements), case_index_(0),
1105 location_(location), is_send_(is_send), is_default_(is_default),
1106 is_lowered_(false), is_case_index_set_(false)
1107 { go_assert(is_default ? channel == NULL : channel != NULL); }
1109 // Traverse the select clause.
1111 traverse(Traverse*);
1113 // Lower statements.
1114 void
1115 lower(Gogo*, Named_object*, Block*, Temporary_statement*, int,
1116 Temporary_statement*);
1118 // Determine types.
1119 void
1120 determine_types(Gogo*);
1122 // Check types.
1123 void
1124 check_types();
1126 // Return true if this is the default clause.
1127 bool
1128 is_default() const
1129 { return this->is_default_; }
1131 // Return the channel. This will return NULL for the default
1132 // clause.
1133 Expression*
1134 channel() const
1135 { return this->channel_; }
1137 // Return true for a send, false for a receive.
1138 bool
1139 is_send() const
1141 go_assert(!this->is_default_);
1142 return this->is_send_;
1145 // Return the value to send or the lvalue to receive into.
1146 Expression*
1147 val() const
1148 { return this->val_; }
1150 // Return the lvalue to set to whether the channel is closed
1151 // on a receive.
1152 Expression*
1153 closed() const
1154 { return this->closed_; }
1156 // Return the variable to initialize, for "case a := <-ch".
1157 Named_object*
1158 var() const
1159 { return this->var_; }
1161 // Return the variable to initialize to whether the channel
1162 // is closed, for "case a, c := <-ch".
1163 Named_object*
1164 closedvar() const
1165 { return this->closedvar_; }
1167 // Return the statements.
1168 Block*
1169 statements() const
1170 { return this->statements_; }
1172 // Return the location.
1173 Location
1174 location() const
1175 { return this->location_; }
1177 // Return the case index for this clause.
1179 case_index() const
1181 go_assert(this->is_case_index_set_);
1182 return this->case_index_;
1185 // Set the case index.
1186 void
1187 set_case_index(int i)
1189 go_assert(!this->is_case_index_set_);
1190 this->case_index_ = i;
1191 this->is_case_index_set_ = true;
1194 // Whether this clause may fall through to the statement which
1195 // follows the overall select statement.
1196 bool
1197 may_fall_through() const;
1199 // Convert the statements to the backend representation.
1200 Bstatement*
1201 get_statements_backend(Translate_context*);
1203 // Dump AST representation.
1204 void
1205 dump_clause(Ast_dump_context*) const;
1207 private:
1208 void
1209 lower_send(Gogo*, Block*, Expression*, Expression*);
1211 void
1212 lower_recv(Gogo*, Named_object*, Block*, Expression*, Expression*,
1213 Temporary_statement*);
1215 void
1216 set_case(Gogo*, Block*, Expression*, Expression*, Expression*);
1218 // The channel.
1219 Expression* channel_;
1220 // The value to send or the lvalue to receive into.
1221 Expression* val_;
1222 // The lvalue to set to whether the channel is closed on a
1223 // receive.
1224 Expression* closed_;
1225 // The variable to initialize, for "case a := <-ch".
1226 Named_object* var_;
1227 // The variable to initialize to whether the channel is closed,
1228 // for "case a, c := <-ch".
1229 Named_object* closedvar_;
1230 // The statements to execute.
1231 Block* statements_;
1232 // The index of this clause in the switch statement. If
1233 // runtime.selectgo returns this index, this clause has been
1234 // chosen.
1235 int case_index_;
1236 // The location of this clause.
1237 Location location_;
1238 // Whether this is a send or a receive.
1239 bool is_send_;
1240 // Whether this is the default.
1241 bool is_default_;
1242 // Whether this has been lowered.
1243 bool is_lowered_;
1244 // Whether the case index has been set.
1245 bool is_case_index_set_;
1248 Select_clause&
1249 at(size_t i)
1250 { return this->clauses_.at(i); }
1252 private:
1253 typedef std::vector<Select_clause> Clauses;
1255 Clauses clauses_;
1258 // A select statement.
1260 class Select_statement : public Statement
1262 public:
1263 Select_statement(Location location)
1264 : Statement(STATEMENT_SELECT, location),
1265 clauses_(NULL), index_(NULL), break_label_(NULL), is_lowered_(false)
1268 // Add the clauses.
1269 void
1270 add_clauses(Select_clauses* clauses)
1272 go_assert(this->clauses_ == NULL);
1273 this->clauses_ = clauses;
1276 // Return the break label for this select statement.
1277 Unnamed_label*
1278 break_label();
1280 protected:
1282 do_traverse(Traverse* traverse)
1283 { return this->clauses_->traverse(traverse); }
1285 Statement*
1286 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1288 void
1289 do_determine_types(Gogo* gogo)
1290 { this->clauses_->determine_types(gogo); }
1292 void
1293 do_check_types(Gogo*)
1294 { this->clauses_->check_types(); }
1296 bool
1297 do_may_fall_through() const;
1299 Bstatement*
1300 do_get_backend(Translate_context*);
1302 void
1303 do_dump_statement(Ast_dump_context*) const;
1305 private:
1306 // Lower a one-case select statement.
1307 Statement*
1308 lower_one_case(Gogo*, Block*);
1310 // Lower a two-case select statement with one defualt case.
1311 Statement*
1312 lower_two_case(Gogo*, Block*);
1314 // The select clauses.
1315 Select_clauses* clauses_;
1316 // A temporary that holds the index value returned by selectgo.
1317 Temporary_statement* index_;
1318 // The break label.
1319 Unnamed_label* break_label_;
1320 // Whether this statement has been lowered.
1321 bool is_lowered_;
1324 // A statement which requires a thunk: go or defer.
1326 class Thunk_statement : public Statement
1328 public:
1329 Thunk_statement(Statement_classification, Call_expression*,
1330 Location);
1332 // Return the call expression.
1333 Expression*
1334 call() const
1335 { return this->call_; }
1337 // Simplify a go or defer statement so that it only uses a single
1338 // parameter.
1339 bool
1340 simplify_statement(Gogo*, Named_object*, Block*);
1342 protected:
1344 do_traverse(Traverse* traverse);
1346 void
1347 do_determine_types(Gogo*);
1349 void
1350 do_check_types(Gogo*);
1352 // Return the function and argument for the call.
1353 bool
1354 get_fn_and_arg(Expression** pfn, Expression** parg);
1356 private:
1357 // Return whether this is a simple go statement.
1358 bool
1359 is_simple(Function_type*) const;
1361 // Return whether the thunk function is a constant.
1362 bool
1363 is_constant_function() const;
1365 // Build the struct to use for a complex case.
1366 Struct_type*
1367 build_struct(Function_type* fntype);
1369 // Build the thunk.
1370 void
1371 build_thunk(Gogo*, const std::string&, Struct_type*);
1373 // Set the name to use for thunk field N.
1374 void
1375 thunk_field_param(int n, char* buf, size_t buflen);
1377 // The function call to be executed in a separate thread (go) or
1378 // later (defer).
1379 Expression* call_;
1382 // A go statement.
1384 class Go_statement : public Thunk_statement
1386 public:
1387 Go_statement(Call_expression* call, Location location)
1388 : Thunk_statement(STATEMENT_GO, call, location)
1391 protected:
1392 Bstatement*
1393 do_get_backend(Translate_context*);
1395 void
1396 do_dump_statement(Ast_dump_context*) const;
1399 // A defer statement.
1401 class Defer_statement : public Thunk_statement
1403 public:
1404 Defer_statement(Call_expression* call, Location location)
1405 : Thunk_statement(STATEMENT_DEFER, call, location),
1406 on_stack_(false)
1409 void
1410 set_on_stack()
1411 { this->on_stack_ = true; }
1413 protected:
1414 Bstatement*
1415 do_get_backend(Translate_context*);
1417 void
1418 do_dump_statement(Ast_dump_context*) const;
1420 private:
1421 static Type*
1422 defer_struct_type();
1424 bool on_stack_;
1427 // A goto statement.
1429 class Goto_statement : public Statement
1431 public:
1432 Goto_statement(Label* label, Location location)
1433 : Statement(STATEMENT_GOTO, location),
1434 label_(label)
1437 // Return the label being jumped to.
1438 Label*
1439 label() const
1440 { return this->label_; }
1442 // Import a goto statement.
1443 static Statement*
1444 do_import(Import_function_body*, Location);
1446 protected:
1448 do_traverse(Traverse*);
1450 void
1451 do_check_types(Gogo*);
1453 bool
1454 do_may_fall_through() const
1455 { return false; }
1457 Bstatement*
1458 do_get_backend(Translate_context*);
1461 do_inlining_cost()
1462 { return 5; }
1464 void
1465 do_export_statement(Export_function_body*);
1467 void
1468 do_dump_statement(Ast_dump_context*) const;
1470 private:
1471 Label* label_;
1474 // A goto statement to an unnamed label.
1476 class Goto_unnamed_statement : public Statement
1478 public:
1479 Goto_unnamed_statement(Unnamed_label* label, Location location)
1480 : Statement(STATEMENT_GOTO_UNNAMED, location),
1481 label_(label)
1484 Unnamed_label*
1485 unnamed_label() const
1486 { return this->label_; }
1488 protected:
1490 do_traverse(Traverse*);
1492 bool
1493 do_may_fall_through() const
1494 { return false; }
1496 Bstatement*
1497 do_get_backend(Translate_context* context);
1500 do_inlining_cost()
1501 { return 5; }
1503 void
1504 do_export_statement(Export_function_body*);
1506 void
1507 do_dump_statement(Ast_dump_context*) const;
1509 private:
1510 Unnamed_label* label_;
1513 // A label statement.
1515 class Label_statement : public Statement
1517 public:
1518 Label_statement(Label* label, Location location)
1519 : Statement(STATEMENT_LABEL, location),
1520 label_(label)
1523 // Return the label itself.
1524 Label*
1525 label() const
1526 { return this->label_; }
1528 // Import a label or unnamed label.
1529 static Statement*
1530 do_import(Import_function_body*, Location);
1532 protected:
1534 do_traverse(Traverse*);
1536 Bstatement*
1537 do_get_backend(Translate_context*);
1540 do_inlining_cost()
1541 { return 1; }
1543 void
1544 do_export_statement(Export_function_body*);
1546 void
1547 do_dump_statement(Ast_dump_context*) const;
1549 private:
1550 // The label.
1551 Label* label_;
1554 // An unnamed label statement.
1556 class Unnamed_label_statement : public Statement
1558 public:
1559 Unnamed_label_statement(Unnamed_label* label);
1561 protected:
1563 do_traverse(Traverse*);
1565 Bstatement*
1566 do_get_backend(Translate_context* context);
1569 do_inlining_cost()
1570 { return 1; }
1572 void
1573 do_export_statement(Export_function_body*);
1575 void
1576 do_dump_statement(Ast_dump_context*) const;
1578 private:
1579 // The label.
1580 Unnamed_label* label_;
1583 // An if statement.
1585 class If_statement : public Statement
1587 public:
1588 If_statement(Expression* cond, Block* then_block, Block* else_block,
1589 Location location)
1590 : Statement(STATEMENT_IF, location),
1591 cond_(cond), then_block_(then_block), else_block_(else_block)
1594 Expression*
1595 condition() const
1596 { return this->cond_; }
1598 Block*
1599 then_block() const
1600 { return this->then_block_; }
1602 Block*
1603 else_block() const
1604 { return this->else_block_; }
1606 // Import an if statement.
1607 static Statement*
1608 do_import(Import_function_body*, Location);
1610 protected:
1612 do_traverse(Traverse*);
1614 void
1615 do_determine_types(Gogo*);
1617 void
1618 do_check_types(Gogo*);
1621 do_inlining_cost()
1622 { return 5; }
1624 void
1625 do_export_statement(Export_function_body*);
1627 bool
1628 do_may_fall_through() const;
1630 Bstatement*
1631 do_get_backend(Translate_context*);
1633 void
1634 do_dump_statement(Ast_dump_context*) const;
1636 private:
1637 Expression* cond_;
1638 Block* then_block_;
1639 Block* else_block_;
1642 // A for statement.
1644 class For_statement : public Statement
1646 public:
1647 For_statement(Block* init, Expression* cond, Block* post,
1648 Location location)
1649 : Statement(STATEMENT_FOR, location),
1650 init_(init), cond_(cond), post_(post), statements_(NULL),
1651 break_label_(NULL), continue_label_(NULL)
1654 // Add the statements.
1655 void
1656 add_statements(Block* statements)
1658 go_assert(this->statements_ == NULL);
1659 this->statements_ = statements;
1662 // Return the break label for this for statement.
1663 Unnamed_label*
1664 break_label();
1666 // Return the continue label for this for statement.
1667 Unnamed_label*
1668 continue_label();
1670 // Set the break and continue labels for this statement.
1671 void
1672 set_break_continue_labels(Unnamed_label* break_label,
1673 Unnamed_label* continue_label);
1675 protected:
1677 do_traverse(Traverse*);
1679 void
1680 do_determine_types(Gogo*);
1682 void
1683 do_check_types(Gogo*);
1685 Statement*
1686 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1688 bool
1689 do_may_fall_through() const;
1691 Bstatement*
1692 do_get_backend(Translate_context*)
1693 { go_unreachable(); }
1695 void
1696 do_dump_statement(Ast_dump_context*) const;
1698 private:
1699 // The initialization statements. This may be NULL.
1700 Block* init_;
1701 // The condition. This may be NULL.
1702 Expression* cond_;
1703 // The statements to run after each iteration. This may be NULL.
1704 Block* post_;
1705 // The statements in the loop itself.
1706 Block* statements_;
1707 // The break label, if needed.
1708 Unnamed_label* break_label_;
1709 // The continue label, if needed.
1710 Unnamed_label* continue_label_;
1713 // A for statement over a range clause.
1715 class For_range_statement : public Statement
1717 public:
1718 For_range_statement(Expression* index_var, Expression* value_var,
1719 Expression* range, Location location)
1720 : Statement(STATEMENT_FOR_RANGE, location),
1721 index_var_(index_var), value_var_(value_var), range_(range),
1722 statements_(NULL), break_label_(NULL), continue_label_(NULL)
1725 // Add the statements.
1726 void
1727 add_statements(Block* statements)
1729 go_assert(this->statements_ == NULL);
1730 this->statements_ = statements;
1733 // Return the break label for this for statement.
1734 Unnamed_label*
1735 break_label();
1737 // Return the continue label for this for statement.
1738 Unnamed_label*
1739 continue_label();
1741 protected:
1743 do_traverse(Traverse*);
1745 void
1746 do_determine_types(Gogo*);
1748 void
1749 do_check_types(Gogo*);
1751 Statement*
1752 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
1754 Bstatement*
1755 do_get_backend(Translate_context*)
1756 { go_unreachable(); }
1758 void
1759 do_dump_statement(Ast_dump_context*) const;
1761 private:
1762 Expression*
1763 make_range_ref(Named_object*, Temporary_statement*, Location);
1765 Call_expression*
1766 call_builtin(Gogo*, const char* funcname, Expression* arg, Location);
1768 void
1769 lower_range_array(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1770 Temporary_statement*, Temporary_statement*,
1771 Block**, Expression**, Block**, Block**);
1773 void
1774 lower_range_slice(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1775 Temporary_statement*, Temporary_statement*,
1776 Block**, Expression**, Block**, Block**);
1778 void
1779 lower_range_string(Gogo*, Block*, Block*, Named_object*, Temporary_statement*,
1780 Temporary_statement*, Temporary_statement*,
1781 Block**, Expression**, Block**, Block**);
1783 void
1784 lower_range_map(Gogo*, Map_type*, Block*, Block*, Named_object*,
1785 Temporary_statement*, Temporary_statement*,
1786 Temporary_statement*, Block**, Expression**, Block**,
1787 Block**);
1789 void
1790 lower_range_channel(Gogo*, Block*, Block*, Named_object*,
1791 Temporary_statement*, Temporary_statement*,
1792 Temporary_statement*, Block**, Expression**, Block**,
1793 Block**);
1795 Statement*
1796 lower_map_range_clear(Gogo*, Type*, Block*, Expression*, Named_object*,
1797 Temporary_statement*, Location);
1799 Statement*
1800 lower_array_range_clear(Gogo*, Type*, Expression*, Block*,
1801 Named_object*, Temporary_statement*,
1802 Location);
1804 // The variable which is set to the index value.
1805 Expression* index_var_;
1806 // The variable which is set to the element value. This may be
1807 // NULL.
1808 Expression* value_var_;
1809 // The expression we are ranging over.
1810 Expression* range_;
1811 // The statements in the block.
1812 Block* statements_;
1813 // The break label, if needed.
1814 Unnamed_label* break_label_;
1815 // The continue label, if needed.
1816 Unnamed_label* continue_label_;
1819 // Class Case_clauses holds the clauses of a switch statement. This
1820 // is built by the parser.
1822 class Case_clauses
1824 public:
1825 Case_clauses()
1826 : clauses_()
1829 // Add a new clause. CASES is a list of case expressions; it may be
1830 // NULL. IS_DEFAULT is true if this is the default case.
1831 // STATEMENTS is a block of statements. IS_FALLTHROUGH is true if
1832 // after the statements the case clause should fall through to the
1833 // next clause.
1834 void
1835 add(Expression_list* cases, bool is_default, Block* statements,
1836 bool is_fallthrough, Location location)
1838 this->clauses_.push_back(Case_clause(cases, is_default, statements,
1839 is_fallthrough, location));
1842 // Return whether there are no clauses.
1843 bool
1844 empty() const
1845 { return this->clauses_.empty(); }
1847 // Traverse the case clauses.
1849 traverse(Traverse*);
1851 // Lower for a nonconstant switch.
1852 void
1853 lower(Gogo*, Block*, Temporary_statement*, Unnamed_label*) const;
1855 // Determine types of expressions. The Type parameter is the type
1856 // of the switch value.
1857 void
1858 determine_types(Gogo*, Type*);
1860 // Check types. The Type parameter is the type of the switch value.
1861 bool
1862 check_types(Type*);
1864 // Return true if all the clauses are constant values.
1865 bool
1866 is_constant() const;
1868 // Return true if these clauses may fall through to the statements
1869 // following the switch statement.
1870 bool
1871 may_fall_through() const;
1873 // Return the body of a SWITCH_EXPR when all the clauses are
1874 // constants.
1875 void
1876 get_backend(Translate_context*, Unnamed_label* break_label,
1877 std::vector<std::vector<Bexpression*> >* all_cases,
1878 std::vector<Bstatement*>* all_statements) const;
1880 // Dump the AST representation to a dump context.
1881 void
1882 dump_clauses(Ast_dump_context*) const;
1884 private:
1885 // For a constant switch we need to keep a record of constants we
1886 // have already seen.
1887 class Hash_integer_value;
1888 class Eq_integer_value;
1889 typedef Unordered_set_hash(Expression*, Hash_integer_value,
1890 Eq_integer_value) Case_constants;
1892 // One case clause.
1893 class Case_clause
1895 public:
1896 Case_clause()
1897 : cases_(NULL), statements_(NULL), is_default_(false),
1898 is_fallthrough_(false), location_(Linemap::unknown_location())
1901 Case_clause(Expression_list* cases, bool is_default, Block* statements,
1902 bool is_fallthrough, Location location)
1903 : cases_(cases), statements_(statements), is_default_(is_default),
1904 is_fallthrough_(is_fallthrough), location_(location)
1907 // Whether this clause falls through to the next clause.
1908 bool
1909 is_fallthrough() const
1910 { return this->is_fallthrough_; }
1912 // Whether this is the default.
1913 bool
1914 is_default() const
1915 { return this->is_default_; }
1917 // The location of this clause.
1918 Location
1919 location() const
1920 { return this->location_; }
1922 // Traversal.
1924 traverse(Traverse*);
1926 // Lower for a nonconstant switch.
1927 void
1928 lower(Gogo*, Block*, Temporary_statement*, Unnamed_label*,
1929 Unnamed_label*) const;
1931 // Determine types.
1932 void
1933 determine_types(Gogo*, Type*);
1935 // Check types.
1936 bool
1937 check_types(Type*);
1939 // Return true if all the case expressions are constant.
1940 bool
1941 is_constant() const;
1943 // Return true if this clause may fall through to execute the
1944 // statements following the switch statement. This is not the
1945 // same as whether this clause falls through to the next clause.
1946 bool
1947 may_fall_through() const;
1949 // Convert the case values and statements to the backend
1950 // representation.
1951 Bstatement*
1952 get_backend(Translate_context*, Unnamed_label* break_label,
1953 Case_constants*, std::vector<Bexpression*>* cases) const;
1955 // Dump the AST representation to a dump context.
1956 void
1957 dump_clause(Ast_dump_context*) const;
1959 private:
1960 // The list of case expressions.
1961 Expression_list* cases_;
1962 // The statements to execute.
1963 Block* statements_;
1964 // Whether this is the default case.
1965 bool is_default_;
1966 // Whether this falls through after the statements.
1967 bool is_fallthrough_;
1968 // The location of this case clause.
1969 Location location_;
1972 friend class Case_clause;
1974 // The type of the list of clauses.
1975 typedef std::vector<Case_clause> Clauses;
1977 // All the case clauses.
1978 Clauses clauses_;
1981 // A switch statement.
1983 class Switch_statement : public Statement
1985 public:
1986 Switch_statement(Expression* val, Location location)
1987 : Statement(STATEMENT_SWITCH, location),
1988 val_(val), clauses_(NULL), break_label_(NULL)
1991 // Add the clauses.
1992 void
1993 add_clauses(Case_clauses* clauses)
1995 go_assert(this->clauses_ == NULL);
1996 this->clauses_ = clauses;
1999 // Return the break label for this switch statement.
2000 Unnamed_label*
2001 break_label();
2003 protected:
2005 do_traverse(Traverse*);
2007 void
2008 do_determine_types(Gogo*);
2010 void
2011 do_check_types(Gogo*);
2013 Statement*
2014 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
2016 Bstatement*
2017 do_get_backend(Translate_context*)
2018 { go_unreachable(); }
2020 void
2021 do_dump_statement(Ast_dump_context*) const;
2023 bool
2024 do_may_fall_through() const;
2026 private:
2027 // The value to switch on. This may be NULL.
2028 Expression* val_;
2029 // The case clauses.
2030 Case_clauses* clauses_;
2031 // The break label, if needed.
2032 Unnamed_label* break_label_;
2035 // Class Type_case_clauses holds the clauses of a type switch
2036 // statement. This is built by the parser.
2038 class Type_case_clauses
2040 public:
2041 Type_case_clauses()
2042 : clauses_()
2045 // Add a new clause. TYPE is the type for this clause; it may be
2046 // NULL. IS_FALLTHROUGH is true if this falls through to the next
2047 // clause; in this case STATEMENTS will be NULL. IS_DEFAULT is true
2048 // if this is the default case. STATEMENTS is a block of
2049 // statements; it may be NULL.
2050 void
2051 add(Type* type, bool is_fallthrough, bool is_default, Block* statements,
2052 Location location)
2054 this->clauses_.push_back(Type_case_clause(type, is_fallthrough, is_default,
2055 statements, location));
2058 // Return whether there are no clauses.
2059 bool
2060 empty() const
2061 { return this->clauses_.empty(); }
2063 // Traverse the type case clauses.
2065 traverse(Traverse*);
2067 // Check for duplicates.
2068 void
2069 check_duplicates() const;
2071 // Determine types of expressions.
2072 void
2073 determine_types(Gogo*);
2075 // Check types.
2076 bool
2077 check_types(Type*);
2079 // Lower to if and goto statements.
2080 void
2081 lower(Gogo*, Block*, Temporary_statement* descriptor_temp,
2082 Unnamed_label* break_label) const;
2084 // Return true if these clauses may fall through to the statements
2085 // following the switch statement.
2086 bool
2087 may_fall_through() const;
2089 // Dump the AST representation to a dump context.
2090 void
2091 dump_clauses(Ast_dump_context*) const;
2093 private:
2094 // One type case clause.
2095 class Type_case_clause
2097 public:
2098 Type_case_clause()
2099 : type_(NULL), statements_(NULL), is_default_(false),
2100 location_(Linemap::unknown_location())
2103 Type_case_clause(Type* type, bool is_fallthrough, bool is_default,
2104 Block* statements, Location location)
2105 : type_(type), statements_(statements), is_fallthrough_(is_fallthrough),
2106 is_default_(is_default), location_(location)
2109 // The type.
2110 Type*
2111 type() const
2112 { return this->type_; }
2114 // Whether this is the default.
2115 bool
2116 is_default() const
2117 { return this->is_default_; }
2119 // The location of this type clause.
2120 Location
2121 location() const
2122 { return this->location_; }
2124 // Traversal.
2126 traverse(Traverse*);
2128 // Determine types.
2129 void
2130 determine_types(Gogo*);
2132 // Check types.
2133 bool
2134 check_types(Type*);
2136 // Lower to if and goto statements.
2137 void
2138 lower(Gogo*, Block*, Temporary_statement* descriptor_temp,
2139 Unnamed_label* break_label, Unnamed_label** stmts_label) const;
2141 // Return true if this clause may fall through to execute the
2142 // statements following the switch statement. This is not the
2143 // same as whether this clause falls through to the next clause.
2144 bool
2145 may_fall_through() const;
2147 // Dump the AST representation to a dump context.
2148 void
2149 dump_clause(Ast_dump_context*) const;
2151 private:
2152 // The type for this type clause.
2153 Type* type_;
2154 // The statements to execute.
2155 Block* statements_;
2156 // Whether this falls through--this is true for "case T1, T2".
2157 bool is_fallthrough_;
2158 // Whether this is the default case.
2159 bool is_default_;
2160 // The location of this type case clause.
2161 Location location_;
2164 friend class Type_case_clause;
2166 // The type of the list of type clauses.
2167 typedef std::vector<Type_case_clause> Type_clauses;
2169 // All the type case clauses.
2170 Type_clauses clauses_;
2173 // A type switch statement.
2175 class Type_switch_statement : public Statement
2177 public:
2178 Type_switch_statement(Expression* expr, Location location)
2179 : Statement(STATEMENT_TYPE_SWITCH, location),
2180 expr_(expr), clauses_(NULL), break_label_(NULL)
2183 // Add the clauses.
2184 void
2185 add_clauses(Type_case_clauses* clauses)
2187 go_assert(this->clauses_ == NULL);
2188 this->clauses_ = clauses;
2191 // Return the break label for this type switch statement.
2192 Unnamed_label*
2193 break_label();
2195 protected:
2197 do_traverse(Traverse*);
2199 void
2200 do_determine_types(Gogo*);
2202 void
2203 do_check_types(Gogo*);
2205 Statement*
2206 do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
2208 Bstatement*
2209 do_get_backend(Translate_context*)
2210 { go_unreachable(); }
2212 void
2213 do_dump_statement(Ast_dump_context*) const;
2215 bool
2216 do_may_fall_through() const;
2218 private:
2219 // The expression we are switching on.
2220 Expression* expr_;
2221 // The type case clauses.
2222 Type_case_clauses* clauses_;
2223 // The break label, if needed.
2224 Unnamed_label* break_label_;
2227 #endif // !defined(GO_STATEMENTS_H)