OSDN Git Service

Don't crash on invalid parameters/results.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / parse.h
1 // parse.h -- Go frontend parser.     -*- 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_PARSE_H
8 #define GO_PARSE_H
9
10 class Set_iota_traverse;
11 class Lex;
12 class Gogo;
13 class Named_object;
14 class Type;
15 class Typed_identifier;
16 class Typed_identifier_list;
17 class Function_type;
18 class Block;
19 class Expression;
20 class Expression_list;
21 class Struct_field_list;
22 class Case_clauses;
23 class Type_case_clauses;
24 class Select_clauses;
25 class Statement;
26 class Label;
27
28 // Parse the program.
29
30 class Parse
31 {
32  public:
33   Parse(Lex*, Gogo*);
34
35   // Parse a program.
36   void
37   program();
38
39  private:
40   // Precedence values.
41   enum Precedence
42   {
43     PRECEDENCE_INVALID = -1,
44     PRECEDENCE_NORMAL = 0,
45     PRECEDENCE_OROR,
46     PRECEDENCE_ANDAND,
47     PRECEDENCE_CHANOP,
48     PRECEDENCE_RELOP,
49     PRECEDENCE_ADDOP,
50     PRECEDENCE_MULOP
51   };
52
53   // We use this when parsing the range clause of a for statement.
54   struct Range_clause
55   {
56     // Set to true if we found a range clause.
57     bool found;
58     // The index expression.
59     Expression* index;
60     // The value expression.
61     Expression* value;
62     // The range expression.
63     Expression* range;
64
65     Range_clause()
66       : found(false), index(NULL), value(NULL), range(NULL)
67     { }
68   };
69
70   // We use this when parsing the statement at the start of a switch,
71   // in order to recognize type switches.
72   struct Type_switch
73   {
74     // Set to true if we find a type switch.
75     bool found;
76     // The variable name.
77     std::string name;
78     // The location of the variable.
79     source_location location;
80     // The expression.
81     Expression* expr;
82
83     Type_switch()
84       : found(false), name(), location(UNKNOWN_LOCATION), expr(NULL)
85     { }
86   };
87
88   // A variable defined in an enclosing function referenced by the
89   // current function.
90   class Enclosing_var
91   {
92    public:
93     Enclosing_var(Named_object* var, Named_object* in_function,
94                   unsigned int index)
95       : var_(var), in_function_(in_function), index_(index)
96     { }
97
98     // We put these in a vector, so we need a default constructor.
99     Enclosing_var()
100       : var_(NULL), in_function_(NULL), index_(-1U)
101     { }
102
103     Named_object*
104     var() const
105     { return this->var_; }
106
107     Named_object*
108     in_function() const
109     { return this->in_function_; }
110
111     unsigned int
112     index() const
113     { return this->index_; }
114
115    private:
116     // The variable which is being referred to.
117     Named_object* var_;
118     // The function where the variable is defined.
119     Named_object* in_function_;
120     // The index of the field in this function's closure struct for
121     // this variable.
122     unsigned int index_;
123   };
124
125   // We store Enclosing_var entries in a set, so we need a comparator.
126   struct Enclosing_var_comparison
127   {
128     bool
129     operator()(const Enclosing_var&, const Enclosing_var&);
130   };
131
132   // A set of Enclosing_var entries.
133   typedef std::set<Enclosing_var, Enclosing_var_comparison> Enclosing_vars;
134
135   // Peek at the current token from the lexer.
136   const Token*
137   peek_token();
138
139   // Consume the current token, return the next one.
140   const Token*
141   advance_token();
142
143   // Push a token back on the input stream.
144   void
145   unget_token(const Token&);
146
147   // The location of the current token.
148   source_location
149   location();
150
151   // For break and continue we keep a stack of statements with
152   // associated labels (if any).  The top of the stack is used for a
153   // break or continue statement with no label.
154   typedef std::vector<std::pair<Statement*, const Label*> > Bc_stack;
155
156   // Parser nonterminals.
157   void identifier_list(Typed_identifier_list*);
158   Expression_list* expression_list(Expression*, bool may_be_sink);
159   bool qualified_ident(std::string*, Named_object**);
160   Type* type();
161   bool type_may_start_here();
162   Type* type_name(bool issue_error);
163   Type* array_type(bool may_use_ellipsis);
164   Type* map_type();
165   Type* struct_type();
166   void field_decl(Struct_field_list*);
167   Type* pointer_type();
168   Type* channel_type();
169   Function_type* signature(Typed_identifier*, source_location);
170   bool parameters(Typed_identifier_list**, bool* is_varargs);
171   Typed_identifier_list* parameter_list(bool* is_varargs);
172   void parameter_decl(bool, Typed_identifier_list*, bool*, bool*);
173   bool result(Typed_identifier_list**);
174   source_location block();
175   Type* interface_type();
176   void method_spec(Typed_identifier_list*);
177   void declaration();
178   bool declaration_may_start_here();
179   void decl(void (Parse::*)(void*), void*);
180   void list(void (Parse::*)(void*), void*, bool);
181   void const_decl();
182   void const_spec(Type**, Expression_list**);
183   void type_decl();
184   void type_spec(void*);
185   void var_decl();
186   void var_spec(void*);
187   void init_vars(const Typed_identifier_list*, Type*, Expression_list*,
188                  bool is_coloneq, source_location);
189   bool init_vars_from_call(const Typed_identifier_list*, Type*, Expression*,
190                            bool is_coloneq, source_location);
191   bool init_vars_from_map(const Typed_identifier_list*, Type*, Expression*,
192                           bool is_coloneq, source_location);
193   bool init_vars_from_receive(const Typed_identifier_list*, Type*,
194                               Expression*, bool is_coloneq, source_location);
195   bool init_vars_from_type_guard(const Typed_identifier_list*, Type*,
196                                  Expression*, bool is_coloneq,
197                                  source_location);
198   Named_object* init_var(const Typed_identifier&, Type*, Expression*,
199                          bool is_coloneq, bool type_from_init, bool* is_new);
200   void simple_var_decl_or_assignment(const std::string&, source_location,
201                                      Range_clause*, Type_switch*);
202   void function_decl();
203   Typed_identifier* receiver();
204   Expression* operand(bool may_be_sink);
205   Expression* enclosing_var_reference(Named_object*, Named_object*,
206                                       source_location);
207   Expression* composite_lit(Type*, int depth, source_location);
208   Expression* function_lit();
209   Expression* create_closure(Named_object* function, Enclosing_vars*,
210                              source_location);
211   Expression* primary_expr(bool may_be_sink, bool may_be_composite_lit,
212                            bool* is_type_switch);
213   Expression* selector(Expression*, bool* is_type_switch);
214   Expression* index(Expression*);
215   Expression* call(Expression*);
216   Expression* expression(Precedence, bool may_be_sink,
217                          bool may_be_composite_lit, bool* is_type_switch);
218   bool expression_may_start_here();
219   Expression* unary_expr(bool may_be_sink, bool may_be_composite_lit,
220                          bool* is_type_switch);
221   Expression* qualified_expr(Expression*, source_location);
222   Expression* id_to_expression(const std::string&, source_location);
223   void statement(const Label*);
224   bool statement_may_start_here();
225   void labeled_stmt(const std::string&, source_location);
226   Expression* simple_stat(bool, bool, Range_clause*, Type_switch*);
227   bool simple_stat_may_start_here();
228   void statement_list();
229   bool statement_list_may_start_here();
230   void expression_stat(Expression*);
231   void inc_dec_stat(Expression*);
232   void assignment(Expression*, Range_clause*);
233   void tuple_assignment(Expression_list*, Range_clause*);
234   void send();
235   void go_or_defer_stat();
236   void return_stat();
237   void if_stat();
238   void switch_stat(const Label*);
239   Statement* expr_switch_body(const Label*, Expression*, source_location);
240   void expr_case_clause(Case_clauses*);
241   Expression_list* expr_switch_case(bool*);
242   Statement* type_switch_body(const Label*, const Type_switch&,
243                               source_location);
244   void type_case_clause(Named_object*, Type_case_clauses*);
245   void type_switch_case(std::vector<Type*>*, bool*);
246   void select_stat(const Label*);
247   void comm_clause(Select_clauses*);
248   bool comm_case(bool*, Expression**, Expression**, std::string*, bool*);
249   bool send_or_recv_expr(bool*, Expression**, Expression**, std::string*);
250   void for_stat(const Label*);
251   void for_clause(Expression**, Block**);
252   void range_clause_decl(const Typed_identifier_list*, Range_clause*);
253   void range_clause_expr(const Expression_list*, Range_clause*);
254   void push_break_statement(Statement*, const Label*);
255   void push_continue_statement(Statement*, const Label*);
256   void pop_break_statement();
257   void pop_continue_statement();
258   Statement* find_bc_statement(const Bc_stack*, const std::string&);
259   void break_stat();
260   void continue_stat();
261   void goto_stat();
262   void package_clause();
263   void import_decl();
264   void import_spec(void*);
265
266   void reset_iota();
267   int iota_value();
268   void increment_iota();
269
270   // Skip past an error looking for a semicolon or OP.  Return true if
271   // all is well, false if we found EOF.
272   bool
273   skip_past_error(Operator op);
274
275   // Verify that an expression is not a sink, and return either the
276   // expression or an error.
277   Expression*
278   verify_not_sink(Expression*);
279
280   // Return the statement associated with a label in a Bc_stack, or
281   // NULL.
282   Statement*
283   find_bc_statement(const Bc_stack*, const std::string&) const;
284
285   // The lexer output we are parsing.
286   Lex* lex_;
287   // The current token.
288   Token token_;
289   // A token pushed back on the input stream.
290   Token unget_token_;
291   // Whether unget_token_ is valid.
292   bool unget_token_valid_;
293   // The code we are generating.
294   Gogo* gogo_;
295   // A stack of statements for which break may be used.
296   Bc_stack break_stack_;
297   // A stack of statements for which continue may be used.
298   Bc_stack continue_stack_;
299   // The current iota value.
300   int iota_;
301   // References from the local function to variables defined in
302   // enclosing functions.
303   Enclosing_vars enclosing_vars_;
304 };
305
306
307 #endif // !defined(GO_PARSE_H)