blob: 7ef81b0c8347259f1156a43f17b94e1ef5987bd8 [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
Damien George11d8cd52014-04-09 14:42:51 +01009enum {
10 ID_FLAG_IS_PARAM = 0x01,
Damien George2827d622014-04-27 15:50:52 +010011 ID_FLAG_IS_STAR_PARAM = 0x02,
12 ID_FLAG_IS_DBL_STAR_PARAM = 0x04,
Damien George11d8cd52014-04-09 14:42:51 +010013};
14
Damien429d7192013-10-04 19:53:11 +010015typedef struct _id_info_t {
Damien George78035b92014-04-09 12:27:39 +010016 uint8_t kind;
Damien George11d8cd52014-04-09 14:42:51 +010017 uint8_t flags;
Damien9ecbcff2013-12-11 00:41:43 +000018 // when it's an ID_INFO_KIND_LOCAL this is the unique number of the local
19 // whet it's an ID_INFO_KIND_CELL/FREE this is the unique number of the closed over variable
Damien George78035b92014-04-09 12:27:39 +010020 uint16_t local_num;
21 qstr qstr;
Damien429d7192013-10-04 19:53:11 +010022} id_info_t;
23
Damien429d7192013-10-04 19:53:11 +010024// scope is a "block" in Python parlance
25typedef enum { SCOPE_MODULE, SCOPE_FUNCTION, SCOPE_LAMBDA, SCOPE_LIST_COMP, SCOPE_DICT_COMP, SCOPE_SET_COMP, SCOPE_GEN_EXPR, SCOPE_CLASS } scope_kind_t;
26typedef struct _scope_t {
27 scope_kind_t kind;
28 struct _scope_t *parent;
29 struct _scope_t *next;
Damiend99b0522013-12-21 18:17:45 +000030 mp_parse_node_t pn;
Damien Georgecbd2f742014-01-19 11:48:48 +000031 qstr source_file;
Damien429d7192013-10-04 19:53:11 +010032 qstr simple_name;
Damien Georgedf8127a2014-04-13 11:04:33 +010033 mp_raw_code_t *raw_code;
Damien George78035b92014-04-09 12:27:39 +010034 uint8_t scope_flags; // see runtime0.h
35 uint8_t emit_options; // see compile.h
Damien George2827d622014-04-27 15:50:52 +010036 uint16_t num_pos_args;
37 uint16_t num_kwonly_args;
Damien George78035b92014-04-09 12:27:39 +010038 uint16_t num_locals;
39 uint16_t stack_size; // maximum size of the locals stack
40 uint16_t exc_stack_size; // maximum size of the exception stack
41 uint16_t id_info_alloc;
42 uint16_t id_info_len;
43 id_info_t *id_info;
Damien429d7192013-10-04 19:53:11 +010044} scope_t;
45
Damien Georgedf8127a2014-04-13 11:04:33 +010046scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint emit_options);
Paul Sokolovskyfd313582014-01-23 23:05:47 +020047void scope_free(scope_t *scope);
Damien429d7192013-10-04 19:53:11 +010048id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added);
49id_info_t *scope_find(scope_t *scope, qstr qstr);
50id_info_t *scope_find_global(scope_t *scope, qstr qstr);
51id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr);
52void scope_close_over_in_parents(scope_t *scope, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010053void scope_declare_global(scope_t *scope, qstr qstr);
54void scope_declare_nonlocal(scope_t *scope, qstr qstr);
Damien429d7192013-10-04 19:53:11 +010055void scope_print_info(scope_t *s);