Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 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 | #include <assert.h> |
| 28 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 29 | #include "py/scope.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 30 | |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 31 | #if MICROPY_ENABLE_COMPILER |
| 32 | |
Romain Goyet | bd63c26 | 2020-04-10 16:09:29 -0400 | [diff] [blame] | 33 | // These low numbered qstrs should fit in 8 bits. See assertions below. |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 34 | static const uint8_t scope_simple_name_table[] = { |
Damien George | 3dea8c9 | 2016-09-30 12:34:05 +1000 | [diff] [blame] | 35 | [SCOPE_MODULE] = MP_QSTR__lt_module_gt_, |
| 36 | [SCOPE_LAMBDA] = MP_QSTR__lt_lambda_gt_, |
| 37 | [SCOPE_LIST_COMP] = MP_QSTR__lt_listcomp_gt_, |
| 38 | [SCOPE_DICT_COMP] = MP_QSTR__lt_dictcomp_gt_, |
| 39 | [SCOPE_SET_COMP] = MP_QSTR__lt_setcomp_gt_, |
| 40 | [SCOPE_GEN_EXPR] = MP_QSTR__lt_genexpr_gt_, |
| 41 | }; |
| 42 | |
Damien George | f2040bf | 2021-10-22 22:22:47 +1100 | [diff] [blame] | 43 | scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, mp_uint_t emit_options) { |
Romain Goyet | bd63c26 | 2020-04-10 16:09:29 -0400 | [diff] [blame] | 44 | // Make sure those qstrs indeed fit in an uint8_t. |
| 45 | MP_STATIC_ASSERT(MP_QSTR__lt_module_gt_ <= UINT8_MAX); |
| 46 | MP_STATIC_ASSERT(MP_QSTR__lt_lambda_gt_ <= UINT8_MAX); |
| 47 | MP_STATIC_ASSERT(MP_QSTR__lt_listcomp_gt_ <= UINT8_MAX); |
| 48 | MP_STATIC_ASSERT(MP_QSTR__lt_dictcomp_gt_ <= UINT8_MAX); |
| 49 | MP_STATIC_ASSERT(MP_QSTR__lt_setcomp_gt_ <= UINT8_MAX); |
| 50 | MP_STATIC_ASSERT(MP_QSTR__lt_genexpr_gt_ <= UINT8_MAX); |
| 51 | |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 52 | scope_t *scope = m_new0(scope_t, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 53 | scope->kind = kind; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 54 | scope->pn = pn; |
Damien George | 3dea8c9 | 2016-09-30 12:34:05 +1000 | [diff] [blame] | 55 | if (kind == SCOPE_FUNCTION || kind == SCOPE_CLASS) { |
| 56 | assert(MP_PARSE_NODE_IS_STRUCT(pn)); |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 57 | scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t *)pn)->nodes[0]); |
Damien George | 3dea8c9 | 2016-09-30 12:34:05 +1000 | [diff] [blame] | 58 | } else { |
| 59 | scope->simple_name = scope_simple_name_table[kind]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 60 | } |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 61 | scope->raw_code = mp_emit_glue_new_raw_code(); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 62 | scope->emit_options = emit_options; |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 63 | scope->id_info_alloc = MICROPY_ALLOC_SCOPE_ID_INIT; |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 64 | scope->id_info = m_new(id_info_t, scope->id_info_alloc); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 65 | |
| 66 | return scope; |
| 67 | } |
| 68 | |
Paul Sokolovsky | fd31358 | 2014-01-23 23:05:47 +0200 | [diff] [blame] | 69 | void scope_free(scope_t *scope) { |
| 70 | m_del(id_info_t, scope->id_info, scope->id_info_alloc); |
| 71 | m_del(scope_t, scope, 1); |
| 72 | } |
| 73 | |
Emil Renner Berthing | 6324c3e | 2020-10-03 10:42:57 +0200 | [diff] [blame] | 74 | id_info_t *scope_find_or_add_id(scope_t *scope, qstr qst, id_info_kind_t kind) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 75 | id_info_t *id_info = scope_find(scope, qst); |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 76 | if (id_info != NULL) { |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 77 | return id_info; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // make sure we have enough memory |
| 81 | if (scope->id_info_len >= scope->id_info_alloc) { |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 82 | scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc, scope->id_info_alloc + MICROPY_ALLOC_SCOPE_ID_INC); |
| 83 | scope->id_info_alloc += MICROPY_ALLOC_SCOPE_ID_INC; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 86 | // add new id to end of array of all ids; this seems to match CPython |
| 87 | // important thing is that function arguments are first, but that is |
| 88 | // handled by the compiler because it adds arguments before compiling the body |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 89 | id_info = &scope->id_info[scope->id_info_len++]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 90 | |
Damien George | e328a5d | 2018-10-27 22:41:21 +1100 | [diff] [blame] | 91 | id_info->kind = kind; |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 92 | id_info->flags = 0; |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 93 | id_info->local_num = 0; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 94 | id_info->qst = qst; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 95 | return id_info; |
| 96 | } |
| 97 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 98 | id_info_t *scope_find(scope_t *scope, qstr qst) { |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 99 | for (mp_uint_t i = 0; i < scope->id_info_len; i++) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 100 | if (scope->id_info[i].qst == qst) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 101 | return &scope->id_info[i]; |
| 102 | } |
| 103 | } |
| 104 | return NULL; |
| 105 | } |
| 106 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 107 | id_info_t *scope_find_global(scope_t *scope, qstr qst) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 108 | while (scope->parent != NULL) { |
| 109 | scope = scope->parent; |
| 110 | } |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 111 | return scope_find(scope, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 114 | static void scope_close_over_in_parents(scope_t *scope, qstr qst) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 115 | assert(scope->parent != NULL); // we should have at least 1 parent |
Damien George | 0d10517 | 2016-09-30 13:53:00 +1000 | [diff] [blame] | 116 | for (scope_t *s = scope->parent;; s = s->parent) { |
| 117 | assert(s->parent != NULL); // we should not get to the outer scope |
Damien George | e328a5d | 2018-10-27 22:41:21 +1100 | [diff] [blame] | 118 | id_info_t *id = scope_find_or_add_id(s, qst, ID_INFO_KIND_UNDECIDED); |
| 119 | if (id->kind == ID_INFO_KIND_UNDECIDED) { |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 120 | // variable not previously declared in this scope, so declare it as free and keep searching parents |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 121 | id->kind = ID_INFO_KIND_FREE; |
| 122 | } else { |
| 123 | // variable is declared in this scope, so finish |
Damien George | 0d10517 | 2016-09-30 13:53:00 +1000 | [diff] [blame] | 124 | if (id->kind == ID_INFO_KIND_LOCAL) { |
| 125 | // variable local to this scope, close it over |
| 126 | id->kind = ID_INFO_KIND_CELL; |
| 127 | } else { |
| 128 | // ID_INFO_KIND_FREE: variable already closed over in a parent scope |
| 129 | // ID_INFO_KIND_CELL: variable already closed over in this scope |
| 130 | assert(id->kind == ID_INFO_KIND_FREE || id->kind == ID_INFO_KIND_CELL); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 131 | } |
| 132 | return; |
| 133 | } |
| 134 | } |
Damien George | 0d10517 | 2016-09-30 13:53:00 +1000 | [diff] [blame] | 135 | } |
| 136 | |
Damien George | 9201f46 | 2018-10-26 16:48:07 +1100 | [diff] [blame] | 137 | void scope_check_to_close_over(scope_t *scope, id_info_t *id) { |
Damien George | 0d10517 | 2016-09-30 13:53:00 +1000 | [diff] [blame] | 138 | if (scope->parent != NULL) { |
| 139 | for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) { |
Damien George | 9201f46 | 2018-10-26 16:48:07 +1100 | [diff] [blame] | 140 | id_info_t *id2 = scope_find(s, id->qst); |
Damien George | 0d10517 | 2016-09-30 13:53:00 +1000 | [diff] [blame] | 141 | if (id2 != NULL) { |
| 142 | if (id2->kind == ID_INFO_KIND_LOCAL || id2->kind == ID_INFO_KIND_CELL || id2->kind == ID_INFO_KIND_FREE) { |
| 143 | id->kind = ID_INFO_KIND_FREE; |
Damien George | 9201f46 | 2018-10-26 16:48:07 +1100 | [diff] [blame] | 144 | scope_close_over_in_parents(scope, id->qst); |
Damien George | 0d10517 | 2016-09-30 13:53:00 +1000 | [diff] [blame] | 145 | } |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 150 | } |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 151 | |
| 152 | #endif // MICROPY_ENABLE_COMPILER |