1 // parse.h -- Go frontend parser. -*- 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.
10 class Set_iota_traverse;
15 class Typed_identifier;
16 class Typed_identifier_list;
20 class Expression_list;
21 class Struct_field_list;
23 class Type_case_clauses;
43 PRECEDENCE_INVALID = -1,
44 PRECEDENCE_NORMAL = 0,
52 // We use this when parsing the range clause of a for statement.
55 // Set to true if we found a range clause.
57 // The index expression.
59 // The value expression.
61 // The range expression.
65 : found(false), index(NULL), value(NULL), range(NULL)
69 // We use this when parsing the statement at the start of a switch,
70 // in order to recognize type switches.
73 // Set to true if we find a type switch.
77 // The location of the variable.
83 : found(false), name(), location(UNKNOWN_LOCATION), expr(NULL)
87 // A variable defined in an enclosing function referenced by the
92 Enclosing_var(Named_object* var, Named_object* in_function,
94 : var_(var), in_function_(in_function), index_(index)
97 // We put these in a vector, so we need a default constructor.
99 : var_(NULL), in_function_(NULL), index_(-1U)
104 { return this->var_; }
108 { return this->in_function_; }
112 { return this->index_; }
115 // The variable which is being referred to.
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
124 // We store Enclosing_var entries in a set, so we need a comparator.
125 struct Enclosing_var_comparison
128 operator()(const Enclosing_var&, const Enclosing_var&);
131 // A set of Enclosing_var entries.
132 typedef std::set<Enclosing_var, Enclosing_var_comparison> Enclosing_vars;
134 // Used to detect duplicate parameter/result names.
135 typedef std::map<std::string, const Typed_identifier*> Names;
137 // Peek at the current token from the lexer.
141 // Consume the current token, return the next one.
145 // Push a token back on the input stream.
147 unget_token(const Token&);
149 // The location of the current token.
153 // For break and continue we keep a stack of statements with
154 // associated labels (if any). The top of the stack is used for a
155 // break or continue statement with no label.
156 typedef std::vector<std::pair<Statement*, Label*> > Bc_stack;
158 // Map from type switch variables to the variables they mask, so
159 // that a use of the type switch variable can become a use of the
161 typedef Unordered_map(Named_object*, Named_object*) Type_switch_vars;
163 // Parser nonterminals.
164 void identifier_list(Typed_identifier_list*);
165 Expression_list* expression_list(Expression*, bool may_be_sink,
166 bool may_be_composite_lit);
167 bool qualified_ident(std::string*, Named_object**);
169 bool type_may_start_here();
170 Type* type_name(bool issue_error);
171 Type* array_type(bool may_use_ellipsis);
174 void field_decl(Struct_field_list*);
175 Type* pointer_type();
176 Type* channel_type();
177 void check_signature_names(const Typed_identifier_list*, Names*);
178 Function_type* signature(Typed_identifier*, Location);
179 bool parameters(Typed_identifier_list**, bool* is_varargs);
180 Typed_identifier_list* parameter_list(bool* is_varargs);
181 void parameter_decl(bool, Typed_identifier_list*, bool*, bool*);
182 bool result(Typed_identifier_list**);
184 Type* interface_type();
185 void method_spec(Typed_identifier_list*);
187 bool declaration_may_start_here();
188 void decl(void (Parse::*)(void*), void*);
189 void list(void (Parse::*)(void*), void*, bool);
191 void const_spec(Type**, Expression_list**);
193 void type_spec(void*);
195 void var_spec(void*);
196 void init_vars(const Typed_identifier_list*, Type*, Expression_list*,
197 bool is_coloneq, Location);
198 bool init_vars_from_call(const Typed_identifier_list*, Type*, Expression*,
199 bool is_coloneq, Location);
200 bool init_vars_from_map(const Typed_identifier_list*, Type*, Expression*,
201 bool is_coloneq, Location);
202 bool init_vars_from_receive(const Typed_identifier_list*, Type*,
203 Expression*, bool is_coloneq, Location);
204 bool init_vars_from_type_guard(const Typed_identifier_list*, Type*,
205 Expression*, bool is_coloneq,
207 Named_object* init_var(const Typed_identifier&, Type*, Expression*,
208 bool is_coloneq, bool type_from_init, bool* is_new);
209 Named_object* create_dummy_global(Type*, Expression*, Location);
210 void simple_var_decl_or_assignment(const std::string&, Location,
211 bool may_be_composite_lit,
212 Range_clause*, Type_switch*);
213 void function_decl();
214 Typed_identifier* receiver();
215 Expression* operand(bool may_be_sink);
216 Expression* enclosing_var_reference(Named_object*, Named_object*,
218 Expression* composite_lit(Type*, int depth, Location);
219 Expression* function_lit();
220 Expression* create_closure(Named_object* function, Enclosing_vars*,
222 Expression* primary_expr(bool may_be_sink, bool may_be_composite_lit,
223 bool* is_type_switch);
224 Expression* selector(Expression*, bool* is_type_switch);
225 Expression* index(Expression*);
226 Expression* call(Expression*);
227 Expression* expression(Precedence, bool may_be_sink,
228 bool may_be_composite_lit, bool* is_type_switch);
229 bool expression_may_start_here();
230 Expression* unary_expr(bool may_be_sink, bool may_be_composite_lit,
231 bool* is_type_switch);
232 Expression* qualified_expr(Expression*, Location);
233 Expression* id_to_expression(const std::string&, Location);
234 void statement(Label*);
235 bool statement_may_start_here();
236 void labeled_stmt(const std::string&, Location);
237 Expression* simple_stat(bool, bool*, Range_clause*, Type_switch*);
238 bool simple_stat_may_start_here();
239 void statement_list();
240 bool statement_list_may_start_here();
241 void expression_stat(Expression*);
242 void send_stmt(Expression*);
243 void inc_dec_stat(Expression*);
244 void assignment(Expression*, bool may_be_composite_lit, Range_clause*);
245 void tuple_assignment(Expression_list*, bool may_be_composite_lit,
248 void go_or_defer_stat();
251 void switch_stat(Label*);
252 Statement* expr_switch_body(Label*, Expression*, Location);
253 void expr_case_clause(Case_clauses*, bool* saw_default);
254 Expression_list* expr_switch_case(bool*);
255 Statement* type_switch_body(Label*, const Type_switch&, Location);
256 void type_case_clause(Named_object*, Type_case_clauses*, bool* saw_default);
257 void type_switch_case(std::vector<Type*>*, bool*);
258 void select_stat(Label*);
259 void comm_clause(Select_clauses*, bool* saw_default);
260 bool comm_case(bool*, Expression**, Expression**, Expression**,
261 std::string*, std::string*, bool*);
262 bool send_or_recv_stmt(bool*, Expression**, Expression**, Expression**,
263 std::string*, std::string*);
264 void for_stat(Label*);
265 void for_clause(Expression**, Block**);
266 void range_clause_decl(const Typed_identifier_list*, Range_clause*);
267 void range_clause_expr(const Expression_list*, Range_clause*);
268 void push_break_statement(Statement*, Label*);
269 void push_continue_statement(Statement*, Label*);
270 void pop_break_statement();
271 void pop_continue_statement();
272 Statement* find_bc_statement(const Bc_stack*, const std::string&);
274 void continue_stat();
276 void package_clause();
278 void import_spec(void*);
282 void increment_iota();
284 // Skip past an error looking for a semicolon or OP. Return true if
285 // all is well, false if we found EOF.
287 skip_past_error(Operator op);
289 // Verify that an expression is not a sink, and return either the
290 // expression or an error.
292 verify_not_sink(Expression*);
294 // Return the statement associated with a label in a Bc_stack, or
297 find_bc_statement(const Bc_stack*, const std::string&) const;
299 // Mark a variable as used.
301 mark_var_used(Named_object*);
303 // The lexer output we are parsing.
305 // The current token.
307 // A token pushed back on the input stream.
309 // Whether unget_token_ is valid.
310 bool unget_token_valid_;
311 // Whether the function we are parsing had errors in the signature.
312 bool is_erroneous_function_;
313 // The code we are generating.
315 // A stack of statements for which break may be used.
316 Bc_stack* break_stack_;
317 // A stack of statements for which continue may be used.
318 Bc_stack* continue_stack_;
319 // The current iota value.
321 // References from the local function to variables defined in
322 // enclosing functions.
323 Enclosing_vars enclosing_vars_;
324 // Map from type switch variables to real variables.
325 Type_switch_vars type_switch_vars_;
329 #endif // !defined(GO_PARSE_H)