Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 27 | enum { |
| 28 | ID_INFO_KIND_GLOBAL_IMPLICIT, |
| 29 | ID_INFO_KIND_GLOBAL_EXPLICIT, |
| 30 | ID_INFO_KIND_LOCAL, // in a function f, written and only referenced by f |
| 31 | ID_INFO_KIND_CELL, // in a function f, read/written by children of f |
| 32 | ID_INFO_KIND_FREE, // in a function f, belongs to the parent of f |
| 33 | }; |
| 34 | |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 35 | enum { |
| 36 | ID_FLAG_IS_PARAM = 0x01, |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 37 | ID_FLAG_IS_STAR_PARAM = 0x02, |
| 38 | ID_FLAG_IS_DBL_STAR_PARAM = 0x04, |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 41 | typedef struct _id_info_t { |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 42 | uint8_t kind; |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 43 | uint8_t flags; |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 44 | // when it's an ID_INFO_KIND_LOCAL this is the unique number of the local |
| 45 | // 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] | 46 | uint16_t local_num; |
| 47 | qstr qstr; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 48 | } id_info_t; |
| 49 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 50 | // scope is a "block" in Python parlance |
| 51 | 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; |
| 52 | typedef struct _scope_t { |
| 53 | scope_kind_t kind; |
| 54 | struct _scope_t *parent; |
| 55 | struct _scope_t *next; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 56 | mp_parse_node_t pn; |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 57 | qstr source_file; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 58 | qstr simple_name; |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 59 | mp_raw_code_t *raw_code; |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 60 | uint8_t scope_flags; // see runtime0.h |
| 61 | uint8_t emit_options; // see compile.h |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 62 | uint16_t num_pos_args; |
| 63 | uint16_t num_kwonly_args; |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 64 | uint16_t num_locals; |
| 65 | uint16_t stack_size; // maximum size of the locals stack |
| 66 | uint16_t exc_stack_size; // maximum size of the exception stack |
| 67 | uint16_t id_info_alloc; |
| 68 | uint16_t id_info_len; |
| 69 | id_info_t *id_info; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 70 | } scope_t; |
| 71 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 72 | 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] | 73 | void scope_free(scope_t *scope); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 74 | id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added); |
| 75 | id_info_t *scope_find(scope_t *scope, qstr qstr); |
| 76 | id_info_t *scope_find_global(scope_t *scope, qstr qstr); |
| 77 | id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr); |
| 78 | void scope_close_over_in_parents(scope_t *scope, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 79 | void scope_declare_global(scope_t *scope, qstr qstr); |
| 80 | void scope_declare_nonlocal(scope_t *scope, qstr qstr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 81 | void scope_print_info(scope_t *s); |