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
;
21 class Expression_list
;
22 class Struct_field_list
;
24 class Type_case_clauses
;
44 PRECEDENCE_INVALID
= -1,
45 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.
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 // Used to detect duplicate parameter/result names.
136 typedef std::map
<std::string
, const Typed_identifier
*> Names
;
138 // Peek at the current token from the lexer.
142 // Consume the current token, return the next one.
146 // Push a token back on the input stream.
148 unget_token(const Token
&);
150 // The location of the current token.
154 // For break and continue we keep a stack of statements with
155 // associated labels (if any). The top of the stack is used for a
156 // break or continue statement with no label.
157 typedef std::vector
<std::pair
<Statement
*, Label
*> > Bc_stack
;
159 // Map from type switch variables to the variables they mask, so
160 // that a use of the type switch variable can become a use of the
162 typedef Unordered_map(Named_object
*, Named_object
*) Type_switch_vars
;
164 // Parser nonterminals.
165 void identifier_list(Typed_identifier_list
*);
166 Expression_list
* expression_list(Expression
*, bool may_be_sink
,
167 bool may_be_composite_lit
);
168 bool qualified_ident(std::string
*, Named_object
**);
170 bool type_may_start_here();
171 Type
* type_name(bool issue_error
);
172 Type
* array_type(bool may_use_ellipsis
);
175 void field_decl(Struct_field_list
*);
176 Type
* pointer_type();
177 Type
* channel_type();
178 void check_signature_names(const Typed_identifier_list
*, Names
*);
179 Function_type
* signature(Typed_identifier
*, Location
);
180 bool parameters(Typed_identifier_list
**, bool* is_varargs
);
181 Typed_identifier_list
* parameter_list(bool* is_varargs
);
182 void parameter_decl(bool, Typed_identifier_list
*, bool*, bool*);
183 bool result(Typed_identifier_list
**);
185 Type
* interface_type();
186 void method_spec(Typed_identifier_list
*);
188 bool declaration_may_start_here();
189 void decl(void (Parse::*)(void*), void*);
190 void list(void (Parse::*)(void*), void*, bool);
192 void const_spec(Type
**, Expression_list
**);
194 void type_spec(void*);
196 void var_spec(void*);
197 void init_vars(const Typed_identifier_list
*, Type
*, Expression_list
*,
198 bool is_coloneq
, Location
);
199 bool init_vars_from_call(const Typed_identifier_list
*, Type
*, Expression
*,
200 bool is_coloneq
, Location
);
201 bool init_vars_from_map(const Typed_identifier_list
*, Type
*, Expression
*,
202 bool is_coloneq
, Location
);
203 bool init_vars_from_receive(const Typed_identifier_list
*, Type
*,
204 Expression
*, bool is_coloneq
, Location
);
205 bool init_vars_from_type_guard(const Typed_identifier_list
*, Type
*,
206 Expression
*, bool is_coloneq
,
208 Named_object
* init_var(const Typed_identifier
&, Type
*, Expression
*,
209 bool is_coloneq
, bool type_from_init
, bool* is_new
);
210 Named_object
* create_dummy_global(Type
*, Expression
*, Location
);
211 void simple_var_decl_or_assignment(const std::string
&, Location
,
212 bool may_be_composite_lit
,
213 Range_clause
*, Type_switch
*);
214 void function_decl();
215 Typed_identifier
* receiver();
216 Expression
* operand(bool may_be_sink
);
217 Expression
* enclosing_var_reference(Named_object
*, Named_object
*,
219 Expression
* composite_lit(Type
*, int depth
, Location
);
220 Expression
* function_lit();
221 Expression
* create_closure(Named_object
* function
, Enclosing_vars
*,
223 Expression
* primary_expr(bool may_be_sink
, bool may_be_composite_lit
,
224 bool* is_type_switch
);
225 Expression
* selector(Expression
*, bool* is_type_switch
);
226 Expression
* index(Expression
*);
227 Expression
* call(Expression
*);
228 Expression
* expression(Precedence
, bool may_be_sink
,
229 bool may_be_composite_lit
, bool* is_type_switch
);
230 bool expression_may_start_here();
231 Expression
* unary_expr(bool may_be_sink
, bool may_be_composite_lit
,
232 bool* is_type_switch
);
233 Type
* reassociate_chan_direction(Channel_type
*, Location
);
234 Expression
* qualified_expr(Expression
*, Location
);
235 Expression
* id_to_expression(const std::string
&, Location
);
236 void statement(Label
*);
237 bool statement_may_start_here();
238 void labeled_stmt(const std::string
&, Location
);
239 Expression
* simple_stat(bool, bool*, Range_clause
*, Type_switch
*);
240 bool simple_stat_may_start_here();
241 void statement_list();
242 bool statement_list_may_start_here();
243 void expression_stat(Expression
*);
244 void send_stmt(Expression
*);
245 void inc_dec_stat(Expression
*);
246 void assignment(Expression
*, bool may_be_composite_lit
, Range_clause
*);
247 void tuple_assignment(Expression_list
*, bool may_be_composite_lit
,
250 void go_or_defer_stat();
253 void switch_stat(Label
*);
254 Statement
* expr_switch_body(Label
*, Expression
*, Location
);
255 void expr_case_clause(Case_clauses
*, bool* saw_default
);
256 Expression_list
* expr_switch_case(bool*);
257 Statement
* type_switch_body(Label
*, const Type_switch
&, Location
);
258 void type_case_clause(Named_object
*, Type_case_clauses
*, bool* saw_default
);
259 void type_switch_case(std::vector
<Type
*>*, bool*);
260 void select_stat(Label
*);
261 void comm_clause(Select_clauses
*, bool* saw_default
);
262 bool comm_case(bool*, Expression
**, Expression
**, Expression
**,
263 std::string
*, std::string
*, bool*);
264 bool send_or_recv_stmt(bool*, Expression
**, Expression
**, Expression
**,
265 std::string
*, std::string
*);
266 void for_stat(Label
*);
267 void for_clause(Expression
**, Block
**);
268 void range_clause_decl(const Typed_identifier_list
*, Range_clause
*);
269 void range_clause_expr(const Expression_list
*, Range_clause
*);
270 void push_break_statement(Statement
*, Label
*);
271 void push_continue_statement(Statement
*, Label
*);
272 void pop_break_statement();
273 void pop_continue_statement();
274 Statement
* find_bc_statement(const Bc_stack
*, const std::string
&);
276 void continue_stat();
278 void package_clause();
280 void import_spec(void*);
284 void increment_iota();
286 // Skip past an error looking for a semicolon or OP. Return true if
287 // all is well, false if we found EOF.
289 skip_past_error(Operator op
);
291 // Verify that an expression is not a sink, and return either the
292 // expression or an error.
294 verify_not_sink(Expression
*);
296 // Return the statement associated with a label in a Bc_stack, or
299 find_bc_statement(const Bc_stack
*, const std::string
&) const;
301 // Mark a variable as used.
303 mark_var_used(Named_object
*);
305 // The lexer output we are parsing.
307 // The current token.
309 // A token pushed back on the input stream.
311 // Whether unget_token_ is valid.
312 bool unget_token_valid_
;
313 // Whether the function we are parsing had errors in the signature.
314 bool is_erroneous_function_
;
315 // The code we are generating.
317 // A stack of statements for which break may be used.
318 Bc_stack
* break_stack_
;
319 // A stack of statements for which continue may be used.
320 Bc_stack
* continue_stack_
;
321 // The current iota value.
323 // References from the local function to variables defined in
324 // enclosing functions.
325 Enclosing_vars enclosing_vars_
;
326 // Map from type switch variables to real variables.
327 Type_switch_vars type_switch_vars_
;
331 #endif // !defined(GO_PARSE_H)