Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1 | enum { |
| 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 George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 9 | enum { |
| 10 | ID_FLAG_IS_PARAM = 0x01, |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 11 | ID_FLAG_IS_STAR_PARAM = 0x02, |
| 12 | ID_FLAG_IS_DBL_STAR_PARAM = 0x04, |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 13 | }; |
| 14 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 15 | typedef struct _id_info_t { |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 16 | uint8_t kind; |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 17 | uint8_t flags; |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 18 | // 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 George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 20 | uint16_t local_num; |
| 21 | qstr qstr; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 22 | } id_info_t; |
| 23 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 24 | // scope is a "block" in Python parlance |
| 25 | typedef enum { SCOPE_MODULE, SCOPE_FUNCTION, SCOPE_LAMBDA, SCOPE_LIST_COMP, SCOPE_DICT_COMP, SCOPE_SET_COMP, SCOPE_GEN_EXPR, SCOPE_CLASS } scope_kind_t; |
| 26 | typedef struct _scope_t { |
| 27 | scope_kind_t kind; |
| 28 | struct _scope_t *parent; |
| 29 | struct _scope_t *next; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 30 | mp_parse_node_t pn; |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 31 | qstr source_file; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 32 | qstr simple_name; |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 33 | mp_raw_code_t *raw_code; |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 34 | uint8_t scope_flags; // see runtime0.h |
| 35 | uint8_t emit_options; // see compile.h |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 36 | uint16_t num_pos_args; |
| 37 | uint16_t num_kwonly_args; |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 38 | 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; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 44 | } scope_t; |
| 45 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 46 | scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint emit_options); |
Paul Sokolovsky | fd31358 | 2014-01-23 23:05:47 +0200 | [diff] [blame] | 47 | void scope_free(scope_t *scope); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 48 | id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added); |
| 49 | id_info_t *scope_find(scope_t *scope, qstr qstr); |
| 50 | id_info_t *scope_find_global(scope_t *scope, qstr qstr); |
| 51 | id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr); |
| 52 | void scope_close_over_in_parents(scope_t *scope, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 53 | void scope_declare_global(scope_t *scope, qstr qstr); |
| 54 | void scope_declare_nonlocal(scope_t *scope, qstr qstr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 55 | void scope_print_info(scope_t *s); |