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 | #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 | |
Damien George | 4abff75 | 2014-08-30 14:59:21 +0100 | [diff] [blame] | 33 | scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_uint_t emit_options) { |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 34 | scope_t *scope = m_new0(scope_t, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 35 | scope->kind = kind; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 36 | scope->pn = pn; |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 37 | scope->source_file = source_file; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 38 | switch (kind) { |
| 39 | case SCOPE_MODULE: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 40 | scope->simple_name = MP_QSTR__lt_module_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 41 | break; |
| 42 | case SCOPE_FUNCTION: |
| 43 | case SCOPE_CLASS: |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 44 | assert(MP_PARSE_NODE_IS_STRUCT(pn)); |
| 45 | scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 46 | break; |
| 47 | case SCOPE_LAMBDA: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 48 | scope->simple_name = MP_QSTR__lt_lambda_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 49 | break; |
| 50 | case SCOPE_LIST_COMP: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 51 | scope->simple_name = MP_QSTR__lt_listcomp_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 52 | break; |
| 53 | case SCOPE_DICT_COMP: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 54 | scope->simple_name = MP_QSTR__lt_dictcomp_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 55 | break; |
| 56 | case SCOPE_SET_COMP: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 57 | scope->simple_name = MP_QSTR__lt_setcomp_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 58 | break; |
| 59 | case SCOPE_GEN_EXPR: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 60 | scope->simple_name = MP_QSTR__lt_genexpr_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 61 | break; |
| 62 | default: |
| 63 | assert(0); |
| 64 | } |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 65 | scope->raw_code = mp_emit_glue_new_raw_code(); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 66 | scope->emit_options = emit_options; |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 67 | scope->id_info_alloc = MICROPY_ALLOC_SCOPE_ID_INIT; |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 68 | scope->id_info = m_new(id_info_t, scope->id_info_alloc); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 69 | |
| 70 | return scope; |
| 71 | } |
| 72 | |
Paul Sokolovsky | fd31358 | 2014-01-23 23:05:47 +0200 | [diff] [blame] | 73 | void scope_free(scope_t *scope) { |
| 74 | m_del(id_info_t, scope->id_info, scope->id_info_alloc); |
| 75 | m_del(scope_t, scope, 1); |
| 76 | } |
| 77 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 78 | id_info_t *scope_find_or_add_id(scope_t *scope, qstr qst, bool *added) { |
| 79 | id_info_t *id_info = scope_find(scope, qst); |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 80 | if (id_info != NULL) { |
| 81 | *added = false; |
| 82 | return id_info; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // make sure we have enough memory |
| 86 | if (scope->id_info_len >= scope->id_info_alloc) { |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 87 | scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc, scope->id_info_alloc + MICROPY_ALLOC_SCOPE_ID_INC); |
| 88 | scope->id_info_alloc += MICROPY_ALLOC_SCOPE_ID_INC; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 91 | // add new id to end of array of all ids; this seems to match CPython |
| 92 | // important thing is that function arguments are first, but that is |
| 93 | // handled by the compiler because it adds arguments before compiling the body |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 94 | id_info = &scope->id_info[scope->id_info_len++]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 95 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 96 | id_info->kind = 0; |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 97 | id_info->flags = 0; |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 98 | id_info->local_num = 0; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 99 | id_info->qst = qst; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 100 | *added = true; |
| 101 | return id_info; |
| 102 | } |
| 103 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 104 | id_info_t *scope_find(scope_t *scope, qstr qst) { |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 105 | for (mp_uint_t i = 0; i < scope->id_info_len; i++) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 106 | if (scope->id_info[i].qst == qst) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 107 | return &scope->id_info[i]; |
| 108 | } |
| 109 | } |
| 110 | return NULL; |
| 111 | } |
| 112 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 113 | id_info_t *scope_find_global(scope_t *scope, qstr qst) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 114 | while (scope->parent != NULL) { |
| 115 | scope = scope->parent; |
| 116 | } |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 117 | return scope_find(scope, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 120 | id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qst) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 121 | if (scope->parent == NULL) { |
| 122 | return NULL; |
| 123 | } |
| 124 | for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 125 | id_info_t *id = scope_find(s, qst); |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 126 | if (id != NULL) { |
| 127 | return id; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | return NULL; |
| 131 | } |
| 132 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 133 | void scope_close_over_in_parents(scope_t *scope, qstr qst) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 134 | assert(scope->parent != NULL); // we should have at least 1 parent |
| 135 | for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) { |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 136 | bool added; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 137 | id_info_t *id = scope_find_or_add_id(s, qst, &added); |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 138 | if (added) { |
| 139 | // 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] | 140 | id->kind = ID_INFO_KIND_FREE; |
| 141 | } else { |
| 142 | // variable is declared in this scope, so finish |
| 143 | switch (id->kind) { |
| 144 | case ID_INFO_KIND_LOCAL: id->kind = ID_INFO_KIND_CELL; break; // variable local to this scope, close it over |
| 145 | case ID_INFO_KIND_FREE: break; // variable already closed over in a parent scope |
| 146 | case ID_INFO_KIND_CELL: break; // variable already closed over in this scope |
| 147 | default: assert(0); // TODO |
| 148 | } |
| 149 | return; |
| 150 | } |
| 151 | } |
| 152 | assert(0); // we should have found the variable in one of the parents |
| 153 | } |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame^] | 154 | |
| 155 | #endif // MICROPY_ENABLE_COMPILER |