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 | |
| 9 | typedef struct _id_info_t { |
| 10 | bool param; |
| 11 | int kind; |
| 12 | qstr qstr; |
| 13 | int local_num; // when it's an ID_INFO_KIND_LOCAL this is the unique number of the local |
| 14 | } id_info_t; |
| 15 | |
| 16 | // taken from python source, Include/code.h |
| 17 | #define SCOPE_FLAG_OPTIMISED 0x0001 |
| 18 | #define SCOPE_FLAG_NEWLOCALS 0x0002 |
| 19 | #define SCOPE_FLAG_VARARGS 0x0004 |
| 20 | #define SCOPE_FLAG_VARKEYWORDS 0x0008 |
| 21 | #define SCOPE_FLAG_NESTED 0x0010 |
| 22 | #define SCOPE_FLAG_GENERATOR 0x0020 |
| 23 | /* The SCOPE_FLAG_NOFREE flag is set if there are no free or cell variables. |
| 24 | This information is redundant, but it allows a single flag test |
| 25 | to determine whether there is any extra work to be done when the |
| 26 | call frame is setup. |
| 27 | */ |
| 28 | #define SCOPE_FLAG_NOFREE 0x0040 |
| 29 | |
| 30 | // scope is a "block" in Python parlance |
| 31 | 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; |
| 32 | typedef struct _scope_t { |
| 33 | scope_kind_t kind; |
| 34 | struct _scope_t *parent; |
| 35 | struct _scope_t *next; |
| 36 | py_parse_node_t pn; |
| 37 | qstr simple_name; |
| 38 | int id_info_alloc; |
| 39 | int id_info_len; |
| 40 | id_info_t *id_info; |
| 41 | int flags; |
| 42 | int num_params; |
| 43 | /* not needed |
| 44 | int num_default_params; |
| 45 | int num_dict_params; |
| 46 | */ |
| 47 | int num_locals; |
| 48 | int stack_size; |
| 49 | int unique_code_id; |
| 50 | } scope_t; |
| 51 | |
| 52 | scope_t *scope_new(scope_kind_t kind, py_parse_node_t pn); |
| 53 | id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added); |
| 54 | id_info_t *scope_find(scope_t *scope, qstr qstr); |
| 55 | id_info_t *scope_find_global(scope_t *scope, qstr qstr); |
| 56 | id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr); |
| 57 | void scope_close_over_in_parents(scope_t *scope, qstr qstr); |
| 58 | void scope_print_info(scope_t *s); |