Improve fstack_protector effective target
[official-gcc.git] / gcc / go / gofrontend / parse.h
blob2ba84579e6c0dd29661d7de57eb1af8605026418
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.
7 #ifndef GO_PARSE_H
8 #define GO_PARSE_H
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 Channel_type;
18 class Function_type;
19 class Block;
20 class Expression;
21 class Expression_list;
22 class Struct_field_list;
23 class Case_clauses;
24 class Type_case_clauses;
25 class Select_clauses;
26 class Statement;
27 class Label;
29 // Parse the program.
31 class Parse
33 public:
34 Parse(Lex*, Gogo*);
36 // Parse a program.
37 void
38 program();
40 private:
41 // Precedence values.
42 enum Precedence
44 PRECEDENCE_INVALID = -1,
45 PRECEDENCE_NORMAL = 0,
46 PRECEDENCE_OROR,
47 PRECEDENCE_ANDAND,
48 PRECEDENCE_RELOP,
49 PRECEDENCE_ADDOP,
50 PRECEDENCE_MULOP
53 // We use this when parsing the range clause of a for statement.
54 struct Range_clause
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;
65 Range_clause()
66 : found(false), index(NULL), value(NULL), range(NULL)
67 { }
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
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 Location location;
80 // The expression.
81 Expression* expr;
83 Type_switch()
84 : found(false), name(), location(Linemap::unknown_location()),
85 expr(NULL)
86 { }
89 // A variable defined in an enclosing function referenced by the
90 // current function.
91 class Enclosing_var
93 public:
94 Enclosing_var(Named_object* var, Named_object* in_function,
95 unsigned int index)
96 : var_(var), in_function_(in_function), index_(index)
97 { }
99 // We put these in a vector, so we need a default constructor.
100 Enclosing_var()
101 : var_(NULL), in_function_(NULL), index_(-1U)
104 Named_object*
105 var() const
106 { return this->var_; }
108 Named_object*
109 in_function() const
110 { return this->in_function_; }
112 unsigned int
113 index() const
114 { return this->index_; }
116 private:
117 // The variable which is being referred to.
118 Named_object* var_;
119 // The function where the variable is defined.
120 Named_object* in_function_;
121 // The index of the field in this function's closure struct for
122 // this variable.
123 unsigned int index_;
126 // We store Enclosing_var entries in a set, so we need a comparator.
127 struct Enclosing_var_comparison
129 bool
130 operator()(const Enclosing_var&, const Enclosing_var&) const;
133 // A set of Enclosing_var entries.
134 typedef std::set<Enclosing_var, Enclosing_var_comparison> Enclosing_vars;
136 // Used to detect duplicate parameter/result names.
137 typedef std::map<std::string, const Typed_identifier*> Names;
139 // Peek at the current token from the lexer.
140 const Token*
141 peek_token();
143 // Consume the current token, return the next one.
144 const Token*
145 advance_token();
147 // Push a token back on the input stream.
148 void
149 unget_token(const Token&);
151 // The location of the current token.
152 Location
153 location();
155 // For break and continue we keep a stack of statements with
156 // associated labels (if any). The top of the stack is used for a
157 // break or continue statement with no label.
158 typedef std::vector<std::pair<Statement*, Label*> > Bc_stack;
160 // Parser nonterminals.
161 void identifier_list(Typed_identifier_list*);
162 Expression_list* expression_list(Expression*, bool may_be_sink,
163 bool may_be_composite_lit);
164 bool qualified_ident(std::string*, Named_object**);
165 Type* type();
166 bool type_may_start_here();
167 Type* type_name(bool issue_error);
168 Type* array_type(bool may_use_ellipsis);
169 Type* map_type();
170 Type* struct_type();
171 void field_decl(Struct_field_list*);
172 Type* pointer_type();
173 Type* channel_type();
174 void check_signature_names(const Typed_identifier_list*, Names*);
175 Function_type* signature(Typed_identifier*, Location);
176 bool parameters(Typed_identifier_list**, bool* is_varargs);
177 Typed_identifier_list* parameter_list(bool* is_varargs);
178 void parameter_decl(bool, Typed_identifier_list*, bool*, bool*, bool*);
179 bool result(Typed_identifier_list**);
180 Location block();
181 Type* interface_type(bool record);
182 void method_spec(Typed_identifier_list*);
183 void declaration();
184 bool declaration_may_start_here();
185 void decl(void (Parse::*)(void*, unsigned int), void*, unsigned int pragmas);
186 void list(void (Parse::*)(void*, unsigned int), void*, bool);
187 void const_decl();
188 void const_spec(Type**, Expression_list**);
189 void type_decl(unsigned int pragmas);
190 void type_spec(void*, unsigned int pragmas);
191 void var_decl();
192 void var_spec(void*, unsigned int pragmas);
193 void init_vars(const Typed_identifier_list*, Type*, Expression_list*,
194 bool is_coloneq, Location);
195 bool init_vars_from_call(const Typed_identifier_list*, Type*, Expression*,
196 bool is_coloneq, Location);
197 bool init_vars_from_map(const Typed_identifier_list*, Type*, Expression*,
198 bool is_coloneq, Location);
199 bool init_vars_from_receive(const Typed_identifier_list*, Type*,
200 Expression*, bool is_coloneq, Location);
201 bool init_vars_from_type_guard(const Typed_identifier_list*, Type*,
202 Expression*, bool is_coloneq,
203 Location);
204 Named_object* init_var(const Typed_identifier&, Type*, Expression*,
205 bool is_coloneq, bool type_from_init, bool* is_new,
206 Expression_list* vars, Expression_list* vals);
207 Named_object* create_dummy_global(Type*, Expression*, Location);
208 void finish_init_vars(Expression_list* vars, Expression_list* vals,
209 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(unsigned int pragmas);
214 Typed_identifier* receiver();
215 Expression* operand(bool may_be_sink, bool *is_parenthesized);
216 Expression* enclosing_var_reference(Named_object*, Named_object*,
217 bool may_be_sink, Location);
218 Expression* composite_lit(Type*, int depth, Location);
219 Expression* function_lit();
220 Expression* create_closure(Named_object* function, Enclosing_vars*,
221 Location);
222 Expression* primary_expr(bool may_be_sink, bool may_be_composite_lit,
223 bool* is_type_switch, bool* is_parenthesized);
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 *is_parenthesized);
230 bool expression_may_start_here();
231 Expression* unary_expr(bool may_be_sink, bool may_be_composite_lit,
232 bool* is_type_switch, bool* is_parenthesized);
233 Type* reassociate_chan_direction(Channel_type*, Location);
234 Expression* qualified_expr(Expression*, Location);
235 Expression* id_to_expression(const std::string&, Location, bool);
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*, bool may_be_composite_lit);
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,
248 Range_clause*);
249 void send();
250 void go_or_defer_stat();
251 void return_stat();
252 void if_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(const std::string&, Expression*, Type_case_clauses*,
259 bool* saw_default, std::vector<Named_object*>*);
260 void type_switch_case(std::vector<Type*>*, bool*);
261 void select_stat(Label*);
262 void comm_clause(Select_clauses*, bool* saw_default);
263 bool comm_case(bool*, Expression**, Expression**, Expression**,
264 std::string*, std::string*, bool*);
265 bool send_or_recv_stmt(bool*, Expression**, Expression**, Expression**,
266 std::string*, std::string*);
267 void for_stat(Label*);
268 void for_clause(Expression**, Block**);
269 void range_clause_decl(const Typed_identifier_list*, Range_clause*);
270 void range_clause_expr(const Expression_list*, Range_clause*);
271 void push_break_statement(Statement*, Label*);
272 void push_continue_statement(Statement*, Label*);
273 void pop_break_statement();
274 void pop_continue_statement();
275 Statement* find_bc_statement(const Bc_stack*, const std::string&);
276 void break_stat();
277 void continue_stat();
278 void goto_stat();
279 void package_clause();
280 void import_decl();
281 void import_spec(void*, unsigned int pragmas);
283 void reset_iota();
284 int iota_value();
285 void increment_iota();
287 // Skip past an error looking for a semicolon or OP. Return true if
288 // all is well, false if we found EOF.
289 bool
290 skip_past_error(Operator op);
292 // Verify that an expression is not a sink, and return either the
293 // expression or an error.
294 Expression*
295 verify_not_sink(Expression*);
297 // Return the statement associated with a label in a Bc_stack, or
298 // NULL.
299 Statement*
300 find_bc_statement(const Bc_stack*, const std::string&) const;
302 // Mark a variable as used.
303 void
304 mark_var_used(Named_object*);
306 // The lexer output we are parsing.
307 Lex* lex_;
308 // The current token.
309 Token token_;
310 // A token pushed back on the input stream.
311 Token unget_token_;
312 // Whether unget_token_ is valid.
313 bool unget_token_valid_;
314 // Whether the function we are parsing had errors in the signature.
315 bool is_erroneous_function_;
316 // The code we are generating.
317 Gogo* gogo_;
318 // A stack of statements for which break may be used.
319 Bc_stack* break_stack_;
320 // A stack of statements for which continue may be used.
321 Bc_stack* continue_stack_;
322 // The current iota value.
323 int iota_;
324 // References from the local function to variables defined in
325 // enclosing functions.
326 Enclosing_vars enclosing_vars_;
330 #endif // !defined(GO_PARSE_H)