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,
53 // We use this when parsing the range clause of a for statement.
56 // Set to true if we found a range clause.
58 // The index expression.
60 // The value expression.
62 // The range expression.
66 : found(false), index(NULL
), value(NULL
), range(NULL
)
70 // We use this when parsing the statement at the start of a switch,
71 // in order to recognize type switches.
74 // Set to true if we find a type switch.
78 // The location of the variable.
79 source_location location
;
84 : found(false), name(), location(UNKNOWN_LOCATION
), expr(NULL
)
88 // A variable defined in an enclosing function referenced by the
93 Enclosing_var(Named_object
* var
, Named_object
* in_function
,
95 : var_(var
), in_function_(in_function
), index_(index
)
98 // We put these in a vector, so we need a default constructor.
100 : var_(NULL
), in_function_(NULL
), index_(-1U)
105 { return this->var_
; }
109 { return this->in_function_
; }
113 { return this->index_
; }
116 // The variable which is being referred to.
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
125 // We store Enclosing_var entries in a set, so we need a comparator.
126 struct Enclosing_var_comparison
129 operator()(const Enclosing_var
&, const Enclosing_var
&);
132 // A set of Enclosing_var entries.
133 typedef std::set
<Enclosing_var
, Enclosing_var_comparison
> Enclosing_vars
;
135 // Peek at the current token from the lexer.
139 // Consume the current token, return the next one.
143 // Push a token back on the input stream.
145 unget_token(const Token
&);
147 // The location of the current token.
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
;
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
**);
161 bool type_may_start_here();
162 Type
* type_name(bool issue_error
);
163 Type
* array_type(bool may_use_ellipsis
);
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
*);
178 bool declaration_may_start_here();
179 void decl(void (Parse::*)(void*), void*);
180 void list(void (Parse::*)(void*), void*, bool);
182 void const_spec(Type
**, Expression_list
**);
184 void type_spec(void*);
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
,
198 Named_object
* init_var(const Typed_identifier
&, Type
*, Expression
*,
199 bool is_coloneq
, bool type_from_init
, bool* is_new
);
200 Named_object
* create_dummy_global(Type
*, Expression
*, source_location
);
201 void simple_var_decl_or_assignment(const std::string
&, source_location
,
202 Range_clause
*, Type_switch
*);
203 void function_decl();
204 Typed_identifier
* receiver();
205 Expression
* operand(bool may_be_sink
);
206 Expression
* enclosing_var_reference(Named_object
*, Named_object
*,
208 Expression
* composite_lit(Type
*, int depth
, source_location
);
209 Expression
* function_lit();
210 Expression
* create_closure(Named_object
* function
, Enclosing_vars
*,
212 Expression
* primary_expr(bool may_be_sink
, bool may_be_composite_lit
,
213 bool* is_type_switch
);
214 Expression
* selector(Expression
*, bool* is_type_switch
);
215 Expression
* index(Expression
*);
216 Expression
* call(Expression
*);
217 Expression
* expression(Precedence
, bool may_be_sink
,
218 bool may_be_composite_lit
, bool* is_type_switch
);
219 bool expression_may_start_here();
220 Expression
* unary_expr(bool may_be_sink
, bool may_be_composite_lit
,
221 bool* is_type_switch
);
222 Expression
* qualified_expr(Expression
*, source_location
);
223 Expression
* id_to_expression(const std::string
&, source_location
);
224 void statement(const Label
*);
225 bool statement_may_start_here();
226 void labeled_stmt(const std::string
&, source_location
);
227 Expression
* simple_stat(bool, bool, Range_clause
*, Type_switch
*);
228 bool simple_stat_may_start_here();
229 void statement_list();
230 bool statement_list_may_start_here();
231 void expression_stat(Expression
*);
232 void inc_dec_stat(Expression
*);
233 void assignment(Expression
*, Range_clause
*);
234 void tuple_assignment(Expression_list
*, Range_clause
*);
236 void go_or_defer_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
&,
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
**, std::string
*, bool*);
250 bool send_or_recv_expr(bool*, Expression
**, Expression
**, std::string
*);
251 void for_stat(const Label
*);
252 void for_clause(Expression
**, Block
**);
253 void range_clause_decl(const Typed_identifier_list
*, Range_clause
*);
254 void range_clause_expr(const Expression_list
*, Range_clause
*);
255 void push_break_statement(Statement
*, const Label
*);
256 void push_continue_statement(Statement
*, const Label
*);
257 void pop_break_statement();
258 void pop_continue_statement();
259 Statement
* find_bc_statement(const Bc_stack
*, const std::string
&);
261 void continue_stat();
263 void package_clause();
265 void import_spec(void*);
269 void increment_iota();
271 // Skip past an error looking for a semicolon or OP. Return true if
272 // all is well, false if we found EOF.
274 skip_past_error(Operator op
);
276 // Verify that an expression is not a sink, and return either the
277 // expression or an error.
279 verify_not_sink(Expression
*);
281 // Return the statement associated with a label in a Bc_stack, or
284 find_bc_statement(const Bc_stack
*, const std::string
&) const;
286 // The lexer output we are parsing.
288 // The current token.
290 // A token pushed back on the input stream.
292 // Whether unget_token_ is valid.
293 bool unget_token_valid_
;
294 // The code we are generating.
296 // A stack of statements for which break may be used.
297 Bc_stack
* break_stack_
;
298 // A stack of statements for which continue may be used.
299 Bc_stack
* continue_stack_
;
300 // The current iota value.
302 // References from the local function to variables defined in
303 // enclosing functions.
304 Enclosing_vars enclosing_vars_
;
308 #endif // !defined(GO_PARSE_H)