d164414df7b8edd1d94502d30fae11a997d1ae63
[gcc.git] / gcc / go / gofrontend / parse.h.working
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_RELOP,
48 PRECEDENCE_ADDOP,
49 PRECEDENCE_MULOP
50 };
51
52 // We use this when parsing the range clause of a for statement.
53 struct Range_clause
54 {
55 // Set to true if we found a range clause.
56 bool found;
57 // The index expression.
58 Expression* index;
59 // The value expression.
60 Expression* value;
61 // The range expression.
62 Expression* range;
63
64 Range_clause()
65 : found(false), index(NULL), value(NULL), range(NULL)
66 { }
67 };
68
69 // We use this when parsing the statement at the start of a switch,
70 // in order to recognize type switches.
71 struct Type_switch
72 {
73 // Set to true if we find a type switch.
74 bool found;
75 // The variable name.
76 std::string name;
77 // The location of the variable.
78 source_location location;
79 // The expression.
80 Expression* expr;
81
82 Type_switch()
83 : found(false), name(), location(UNKNOWN_LOCATION), expr(NULL)
84 { }
85 };
86
87 // A variable defined in an enclosing function referenced by the
88 // current function.
89 class Enclosing_var
90 {
91 public:
92 Enclosing_var(Named_object* var, Named_object* in_function,
93 unsigned int index)
94 : var_(var), in_function_(in_function), index_(index)
95 { }
96
97 // We put these in a vector, so we need a default constructor.
98 Enclosing_var()
99 : var_(NULL), in_function_(NULL), index_(-1U)
100 { }
101
102 Named_object*
103 var() const
104 { return this->var_; }
105
106 Named_object*
107 in_function() const
108 { return this->in_function_; }
109
110 unsigned int
111 index() const
112 { return this->index_; }
113
114 private:
115 // The variable which is being referred to.
116 Named_object* var_;
117 // The function where the variable is defined.
118 Named_object* in_function_;
119 // The index of the field in this function's closure struct for
120 // this variable.
121 unsigned int index_;
122 };
123
124 // We store Enclosing_var entries in a set, so we need a comparator.
125 struct Enclosing_var_comparison
126 {
127 bool
128 operator()(const Enclosing_var&, const Enclosing_var&);
129 };
130
131 // A set of Enclosing_var entries.
132 typedef std::set<Enclosing_var, Enclosing_var_comparison> Enclosing_vars;
133
134 // Peek at the current token from the lexer.
135 const Token*
136 peek_token();
137
138 // Consume the current token, return the next one.
139 const Token*
140 advance_token();
141
142 // Push a token back on the input stream.
143 void
144 unget_token(const Token&);
145
146 // The location of the current token.
147 source_location
148 location();
149
150 // For break and continue we keep a stack of statements with
151 // associated labels (if any). The top of the stack is used for a
152 // break or continue statement with no label.
153 typedef std::vector<std::pair<Statement*, const Label*> > Bc_stack;
154
155 // Parser nonterminals.
156 void identifier_list(Typed_identifier_list*);
157 Expression_list* expression_list(Expression*, bool may_be_sink);
158 bool qualified_ident(std::string*, Named_object**);
159 Type* type();
160 bool type_may_start_here();
161 Type* type_name(bool issue_error);
162 Type* array_type(bool may_use_ellipsis);
163 Type* map_type();
164 Type* struct_type();
165 void field_decl(Struct_field_list*);
166 Type* pointer_type();
167 Type* channel_type();
168 Function_type* signature(Typed_identifier*, source_location);
169 bool parameters(Typed_identifier_list**, bool* is_varargs);
170 Typed_identifier_list* parameter_list(bool* is_varargs);
171 void parameter_decl(bool, Typed_identifier_list*, bool*, bool*);
172 bool result(Typed_identifier_list**);
173 source_location block();
174 Type* interface_type();
175 void method_spec(Typed_identifier_list*);
176 void declaration();
177 bool declaration_may_start_here();
178 void decl(void (Parse::*)(void*), void*);
179 void list(void (Parse::*)(void*), void*, bool);
180 void const_decl();
181 void const_spec(Type**, Expression_list**);
182 void type_decl();
183 void type_spec(void*);
184 void var_decl();
185 void var_spec(void*);
186 void init_vars(const Typed_identifier_list*, Type*, Expression_list*,
187 bool is_coloneq, source_location);
188 bool init_vars_from_call(const Typed_identifier_list*, Type*, Expression*,
189 bool is_coloneq, source_location);
190 bool init_vars_from_map(const Typed_identifier_list*, Type*, Expression*,
191 bool is_coloneq, source_location);
192 bool init_vars_from_receive(const Typed_identifier_list*, Type*,
193 Expression*, bool is_coloneq, source_location);
194 bool init_vars_from_type_guard(const Typed_identifier_list*, Type*,
195 Expression*, bool is_coloneq,
196 source_location);
197 Named_object* init_var(const Typed_identifier&, Type*, Expression*,
198 bool is_coloneq, bool type_from_init, bool* is_new);
199 Named_object* create_dummy_global(Type*, Expression*, source_location);
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 send_stmt(Expression*);
232 void inc_dec_stat(Expression*);
233 void assignment(Expression*, Range_clause*);
234 void tuple_assignment(Expression_list*, Range_clause*);
235 void send();
236 void go_or_defer_stat();
237 void return_stat();
238 void if_stat();
239 void switch_stat(const Label*);
240 Statement* expr_switch_body(const Label*, Expression*, source_location);
241 void expr_case_clause(Case_clauses*, bool* saw_default);
242 Expression_list* expr_switch_case(bool*);
243 Statement* type_switch_body(const Label*, const Type_switch&,
244 source_location);
245 void type_case_clause(Named_object*, Type_case_clauses*, bool* saw_default);
246 void type_switch_case(std::vector<Type*>*, bool*);
247 void select_stat(const Label*);
248 void comm_clause(Select_clauses*, bool* saw_default);
249 bool comm_case(bool*, Expression**, Expression**, Expression**,
250 std::string*, std::string*, bool*);
251 bool send_or_recv_stmt(bool*, Expression**, Expression**, Expression**,
252 std::string*, std::string*);
253 void for_stat(const Label*);
254 void for_clause(Expression**, Block**);
255 void range_clause_decl(const Typed_identifier_list*, Range_clause*);
256 void range_clause_expr(const Expression_list*, Range_clause*);
257 void push_break_statement(Statement*, const Label*);
258 void push_continue_statement(Statement*, const Label*);
259 void pop_break_statement();
260 void pop_continue_statement();
261 Statement* find_bc_statement(const Bc_stack*, const std::string&);
262 void break_stat();
263 void continue_stat();
264 void goto_stat();
265 void package_clause();
266 void import_decl();
267 void import_spec(void*);
268
269 void reset_iota();
270 int iota_value();
271 void increment_iota();
272
273 // Skip past an error looking for a semicolon or OP. Return true if
274 // all is well, false if we found EOF.
275 bool
276 skip_past_error(Operator op);
277
278 // Verify that an expression is not a sink, and return either the
279 // expression or an error.
280 Expression*
281 verify_not_sink(Expression*);
282
283 // Return the statement associated with a label in a Bc_stack, or
284 // NULL.
285 Statement*
286 find_bc_statement(const Bc_stack*, const std::string&) const;
287
288 // The lexer output we are parsing.
289 Lex* lex_;
290 // The current token.
291 Token token_;
292 // A token pushed back on the input stream.
293 Token unget_token_;
294 // Whether unget_token_ is valid.
295 bool unget_token_valid_;
296 // The code we are generating.
297 Gogo* gogo_;
298 // A stack of statements for which break may be used.
299 Bc_stack* break_stack_;
300 // A stack of statements for which continue may be used.
301 Bc_stack* continue_stack_;
302 // The current iota value.
303 int iota_;
304 // References from the local function to variables defined in
305 // enclosing functions.
306 Enclosing_vars enclosing_vars_;
307 };
308
309
310 #endif // !defined(GO_PARSE_H)