| 1 |
714 |
jeremybenn |
// 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 |
|
|
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 |
|
|
// Used to detect duplicate parameter/result names.
|
| 135 |
|
|
typedef std::map<std::string, const Typed_identifier*> Names;
|
| 136 |
|
|
|
| 137 |
|
|
// Peek at the current token from the lexer.
|
| 138 |
|
|
const Token*
|
| 139 |
|
|
peek_token();
|
| 140 |
|
|
|
| 141 |
|
|
// Consume the current token, return the next one.
|
| 142 |
|
|
const Token*
|
| 143 |
|
|
advance_token();
|
| 144 |
|
|
|
| 145 |
|
|
// Push a token back on the input stream.
|
| 146 |
|
|
void
|
| 147 |
|
|
unget_token(const Token&);
|
| 148 |
|
|
|
| 149 |
|
|
// The location of the current token.
|
| 150 |
|
|
Location
|
| 151 |
|
|
location();
|
| 152 |
|
|
|
| 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;
|
| 157 |
|
|
|
| 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
|
| 160 |
|
|
// real variable.
|
| 161 |
|
|
typedef Unordered_map(Named_object*, Named_object*) Type_switch_vars;
|
| 162 |
|
|
|
| 163 |
|
|
// Parser nonterminals.
|
| 164 |
|
|
void identifier_list(Typed_identifier_list*);
|
| 165 |
|
|
Expression_list* expression_list(Expression*, bool may_be_sink);
|
| 166 |
|
|
bool qualified_ident(std::string*, Named_object**);
|
| 167 |
|
|
Type* type();
|
| 168 |
|
|
bool type_may_start_here();
|
| 169 |
|
|
Type* type_name(bool issue_error);
|
| 170 |
|
|
Type* array_type(bool may_use_ellipsis);
|
| 171 |
|
|
Type* map_type();
|
| 172 |
|
|
Type* struct_type();
|
| 173 |
|
|
void field_decl(Struct_field_list*);
|
| 174 |
|
|
Type* pointer_type();
|
| 175 |
|
|
Type* channel_type();
|
| 176 |
|
|
void check_signature_names(const Typed_identifier_list*, Names*);
|
| 177 |
|
|
Function_type* signature(Typed_identifier*, Location);
|
| 178 |
|
|
bool parameters(Typed_identifier_list**, bool* is_varargs);
|
| 179 |
|
|
Typed_identifier_list* parameter_list(bool* is_varargs);
|
| 180 |
|
|
void parameter_decl(bool, Typed_identifier_list*, bool*, bool*);
|
| 181 |
|
|
bool result(Typed_identifier_list**);
|
| 182 |
|
|
Location block();
|
| 183 |
|
|
Type* interface_type();
|
| 184 |
|
|
void method_spec(Typed_identifier_list*);
|
| 185 |
|
|
void declaration();
|
| 186 |
|
|
bool declaration_may_start_here();
|
| 187 |
|
|
void decl(void (Parse::*)(void*), void*);
|
| 188 |
|
|
void list(void (Parse::*)(void*), void*, bool);
|
| 189 |
|
|
void const_decl();
|
| 190 |
|
|
void const_spec(Type**, Expression_list**);
|
| 191 |
|
|
void type_decl();
|
| 192 |
|
|
void type_spec(void*);
|
| 193 |
|
|
void var_decl();
|
| 194 |
|
|
void var_spec(void*);
|
| 195 |
|
|
void init_vars(const Typed_identifier_list*, Type*, Expression_list*,
|
| 196 |
|
|
bool is_coloneq, Location);
|
| 197 |
|
|
bool init_vars_from_call(const Typed_identifier_list*, Type*, Expression*,
|
| 198 |
|
|
bool is_coloneq, Location);
|
| 199 |
|
|
bool init_vars_from_map(const Typed_identifier_list*, Type*, Expression*,
|
| 200 |
|
|
bool is_coloneq, Location);
|
| 201 |
|
|
bool init_vars_from_receive(const Typed_identifier_list*, Type*,
|
| 202 |
|
|
Expression*, bool is_coloneq, Location);
|
| 203 |
|
|
bool init_vars_from_type_guard(const Typed_identifier_list*, Type*,
|
| 204 |
|
|
Expression*, bool is_coloneq,
|
| 205 |
|
|
Location);
|
| 206 |
|
|
Named_object* init_var(const Typed_identifier&, Type*, Expression*,
|
| 207 |
|
|
bool is_coloneq, bool type_from_init, bool* is_new);
|
| 208 |
|
|
Named_object* create_dummy_global(Type*, Expression*, Location);
|
| 209 |
|
|
void simple_var_decl_or_assignment(const std::string&, Location,
|
| 210 |
|
|
Range_clause*, Type_switch*);
|
| 211 |
|
|
void function_decl();
|
| 212 |
|
|
Typed_identifier* receiver();
|
| 213 |
|
|
Expression* operand(bool may_be_sink);
|
| 214 |
|
|
Expression* enclosing_var_reference(Named_object*, Named_object*,
|
| 215 |
|
|
Location);
|
| 216 |
|
|
Expression* composite_lit(Type*, int depth, Location);
|
| 217 |
|
|
Expression* function_lit();
|
| 218 |
|
|
Expression* create_closure(Named_object* function, Enclosing_vars*,
|
| 219 |
|
|
Location);
|
| 220 |
|
|
Expression* primary_expr(bool may_be_sink, bool may_be_composite_lit,
|
| 221 |
|
|
bool* is_type_switch);
|
| 222 |
|
|
Expression* selector(Expression*, bool* is_type_switch);
|
| 223 |
|
|
Expression* index(Expression*);
|
| 224 |
|
|
Expression* call(Expression*);
|
| 225 |
|
|
Expression* expression(Precedence, bool may_be_sink,
|
| 226 |
|
|
bool may_be_composite_lit, bool* is_type_switch);
|
| 227 |
|
|
bool expression_may_start_here();
|
| 228 |
|
|
Expression* unary_expr(bool may_be_sink, bool may_be_composite_lit,
|
| 229 |
|
|
bool* is_type_switch);
|
| 230 |
|
|
Expression* qualified_expr(Expression*, Location);
|
| 231 |
|
|
Expression* id_to_expression(const std::string&, Location);
|
| 232 |
|
|
void statement(Label*);
|
| 233 |
|
|
bool statement_may_start_here();
|
| 234 |
|
|
void labeled_stmt(const std::string&, Location);
|
| 235 |
|
|
Expression* simple_stat(bool, bool*, Range_clause*, Type_switch*);
|
| 236 |
|
|
bool simple_stat_may_start_here();
|
| 237 |
|
|
void statement_list();
|
| 238 |
|
|
bool statement_list_may_start_here();
|
| 239 |
|
|
void expression_stat(Expression*);
|
| 240 |
|
|
void send_stmt(Expression*);
|
| 241 |
|
|
void inc_dec_stat(Expression*);
|
| 242 |
|
|
void assignment(Expression*, Range_clause*);
|
| 243 |
|
|
void tuple_assignment(Expression_list*, Range_clause*);
|
| 244 |
|
|
void send();
|
| 245 |
|
|
void go_or_defer_stat();
|
| 246 |
|
|
void return_stat();
|
| 247 |
|
|
void if_stat();
|
| 248 |
|
|
void switch_stat(Label*);
|
| 249 |
|
|
Statement* expr_switch_body(Label*, Expression*, Location);
|
| 250 |
|
|
void expr_case_clause(Case_clauses*, bool* saw_default);
|
| 251 |
|
|
Expression_list* expr_switch_case(bool*);
|
| 252 |
|
|
Statement* type_switch_body(Label*, const Type_switch&, Location);
|
| 253 |
|
|
void type_case_clause(Named_object*, Type_case_clauses*, bool* saw_default);
|
| 254 |
|
|
void type_switch_case(std::vector<Type*>*, bool*);
|
| 255 |
|
|
void select_stat(Label*);
|
| 256 |
|
|
void comm_clause(Select_clauses*, bool* saw_default);
|
| 257 |
|
|
bool comm_case(bool*, Expression**, Expression**, Expression**,
|
| 258 |
|
|
std::string*, std::string*, bool*);
|
| 259 |
|
|
bool send_or_recv_stmt(bool*, Expression**, Expression**, Expression**,
|
| 260 |
|
|
std::string*, std::string*);
|
| 261 |
|
|
void for_stat(Label*);
|
| 262 |
|
|
void for_clause(Expression**, Block**);
|
| 263 |
|
|
void range_clause_decl(const Typed_identifier_list*, Range_clause*);
|
| 264 |
|
|
void range_clause_expr(const Expression_list*, Range_clause*);
|
| 265 |
|
|
void push_break_statement(Statement*, Label*);
|
| 266 |
|
|
void push_continue_statement(Statement*, Label*);
|
| 267 |
|
|
void pop_break_statement();
|
| 268 |
|
|
void pop_continue_statement();
|
| 269 |
|
|
Statement* find_bc_statement(const Bc_stack*, const std::string&);
|
| 270 |
|
|
void break_stat();
|
| 271 |
|
|
void continue_stat();
|
| 272 |
|
|
void goto_stat();
|
| 273 |
|
|
void package_clause();
|
| 274 |
|
|
void import_decl();
|
| 275 |
|
|
void import_spec(void*);
|
| 276 |
|
|
|
| 277 |
|
|
void reset_iota();
|
| 278 |
|
|
int iota_value();
|
| 279 |
|
|
void increment_iota();
|
| 280 |
|
|
|
| 281 |
|
|
// Skip past an error looking for a semicolon or OP. Return true if
|
| 282 |
|
|
// all is well, false if we found EOF.
|
| 283 |
|
|
bool
|
| 284 |
|
|
skip_past_error(Operator op);
|
| 285 |
|
|
|
| 286 |
|
|
// Verify that an expression is not a sink, and return either the
|
| 287 |
|
|
// expression or an error.
|
| 288 |
|
|
Expression*
|
| 289 |
|
|
verify_not_sink(Expression*);
|
| 290 |
|
|
|
| 291 |
|
|
// Return the statement associated with a label in a Bc_stack, or
|
| 292 |
|
|
// NULL.
|
| 293 |
|
|
Statement*
|
| 294 |
|
|
find_bc_statement(const Bc_stack*, const std::string&) const;
|
| 295 |
|
|
|
| 296 |
|
|
// Mark a variable as used.
|
| 297 |
|
|
void
|
| 298 |
|
|
mark_var_used(Named_object*);
|
| 299 |
|
|
|
| 300 |
|
|
// The lexer output we are parsing.
|
| 301 |
|
|
Lex* lex_;
|
| 302 |
|
|
// The current token.
|
| 303 |
|
|
Token token_;
|
| 304 |
|
|
// A token pushed back on the input stream.
|
| 305 |
|
|
Token unget_token_;
|
| 306 |
|
|
// Whether unget_token_ is valid.
|
| 307 |
|
|
bool unget_token_valid_;
|
| 308 |
|
|
// Whether the function we are parsing had errors in the signature.
|
| 309 |
|
|
bool is_erroneous_function_;
|
| 310 |
|
|
// The code we are generating.
|
| 311 |
|
|
Gogo* gogo_;
|
| 312 |
|
|
// A stack of statements for which break may be used.
|
| 313 |
|
|
Bc_stack* break_stack_;
|
| 314 |
|
|
// A stack of statements for which continue may be used.
|
| 315 |
|
|
Bc_stack* continue_stack_;
|
| 316 |
|
|
// The current iota value.
|
| 317 |
|
|
int iota_;
|
| 318 |
|
|
// References from the local function to variables defined in
|
| 319 |
|
|
// enclosing functions.
|
| 320 |
|
|
Enclosing_vars enclosing_vars_;
|
| 321 |
|
|
// Map from type switch variables to real variables.
|
| 322 |
|
|
Type_switch_vars type_switch_vars_;
|
| 323 |
|
|
};
|
| 324 |
|
|
|
| 325 |
|
|
|
| 326 |
|
|
#endif // !defined(GO_PARSE_H)
|