blob: 9a04c56f6150d573b8d82acdbc584a2800f37483 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001enum {
2 ID_INFO_KIND_GLOBAL_IMPLICIT,
3 ID_INFO_KIND_GLOBAL_EXPLICIT,
4 ID_INFO_KIND_LOCAL, // in a function f, written and only referenced by f
5 ID_INFO_KIND_CELL, // in a function f, read/written by children of f
6 ID_INFO_KIND_FREE, // in a function f, belongs to the parent of f
7};
8
9typedef struct _id_info_t {
Damien27fb45e2013-10-20 15:07:49 +010010 // TODO compress this info to make structure smaller in memory
Damien429d7192013-10-04 19:53:11 +010011 bool param;
12 int kind;
13 qstr qstr;
14 int local_num; // when it's an ID_INFO_KIND_LOCAL this is the unique number of the local
15} id_info_t;
16
17// taken from python source, Include/code.h
18#define SCOPE_FLAG_OPTIMISED 0x0001
19#define SCOPE_FLAG_NEWLOCALS 0x0002
20#define SCOPE_FLAG_VARARGS 0x0004
21#define SCOPE_FLAG_VARKEYWORDS 0x0008
22#define SCOPE_FLAG_NESTED 0x0010
23#define SCOPE_FLAG_GENERATOR 0x0020
24/* The SCOPE_FLAG_NOFREE flag is set if there are no free or cell variables.
25 This information is redundant, but it allows a single flag test
26 to determine whether there is any extra work to be done when the
27 call frame is setup.
28*/
29#define SCOPE_FLAG_NOFREE 0x0040
30
31// scope is a "block" in Python parlance
32typedef enum { SCOPE_MODULE, SCOPE_FUNCTION, SCOPE_LAMBDA, SCOPE_LIST_COMP, SCOPE_DICT_COMP, SCOPE_SET_COMP, SCOPE_GEN_EXPR, SCOPE_CLASS } scope_kind_t;
33typedef struct _scope_t {
34 scope_kind_t kind;
35 struct _scope_t *parent;
36 struct _scope_t *next;
37 py_parse_node_t pn;
38 qstr simple_name;
39 int id_info_alloc;
40 int id_info_len;
41 id_info_t *id_info;
42 int flags;
43 int num_params;
44 /* not needed
45 int num_default_params;
46 int num_dict_params;
47 */
48 int num_locals;
49 int stack_size;
Damienb05d7072013-10-05 13:37:10 +010050 uint unique_code_id;
Damien6cdd3af2013-10-05 18:08:26 +010051 uint emit_options;
Damien429d7192013-10-04 19:53:11 +010052} scope_t;
53
Damien6cdd3af2013-10-05 18:08:26 +010054scope_t *scope_new(scope_kind_t kind, py_parse_node_t pn, uint unique_code_id, uint emit_options);
Damien429d7192013-10-04 19:53:11 +010055id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added);
56id_info_t *scope_find(scope_t *scope, qstr qstr);
57id_info_t *scope_find_global(scope_t *scope, qstr qstr);
58id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr);
59void scope_close_over_in_parents(scope_t *scope, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010060void scope_declare_global(scope_t *scope, qstr qstr);
61void scope_declare_nonlocal(scope_t *scope, qstr qstr);
Damien429d7192013-10-04 19:53:11 +010062void scope_print_info(scope_t *s);