blob: 1a6ae7b8ad6e15343e86c0ee410ad9f1d865a87e [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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
Damien429d7192013-10-04 19:53:11 +010027#include <assert.h>
28
Damien George51dfcb42015-01-01 20:27:54 +000029#include "py/scope.h"
Damien429d7192013-10-04 19:53:11 +010030
Damien Georgedd5353a2015-12-18 12:35:44 +000031#if MICROPY_ENABLE_COMPILER
32
Damien George3dea8c92016-09-30 12:34:05 +100033// these low numbered qstrs should fit in 8 bits
34STATIC const uint8_t scope_simple_name_table[] = {
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 George4abff752014-08-30 14:59:21 +010043scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_uint_t emit_options) {
Damien George78035b92014-04-09 12:27:39 +010044 scope_t *scope = m_new0(scope_t, 1);
Damien429d7192013-10-04 19:53:11 +010045 scope->kind = kind;
Damien429d7192013-10-04 19:53:11 +010046 scope->pn = pn;
Damien Georgecbd2f742014-01-19 11:48:48 +000047 scope->source_file = source_file;
Damien George3dea8c92016-09-30 12:34:05 +100048 if (kind == SCOPE_FUNCTION || kind == SCOPE_CLASS) {
49 assert(MP_PARSE_NODE_IS_STRUCT(pn));
50 scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
51 } else {
52 scope->simple_name = scope_simple_name_table[kind];
Damien429d7192013-10-04 19:53:11 +010053 }
Damien Georgedf8127a2014-04-13 11:04:33 +010054 scope->raw_code = mp_emit_glue_new_raw_code();
Damien6cdd3af2013-10-05 18:08:26 +010055 scope->emit_options = emit_options;
Damien George58ebde42014-05-21 20:32:59 +010056 scope->id_info_alloc = MICROPY_ALLOC_SCOPE_ID_INIT;
Damien George78035b92014-04-09 12:27:39 +010057 scope->id_info = m_new(id_info_t, scope->id_info_alloc);
Damien429d7192013-10-04 19:53:11 +010058
59 return scope;
60}
61
Paul Sokolovskyfd313582014-01-23 23:05:47 +020062void scope_free(scope_t *scope) {
63 m_del(id_info_t, scope->id_info, scope->id_info_alloc);
64 m_del(scope_t, scope, 1);
65}
66
Damien George7ff996c2014-09-08 23:05:16 +010067id_info_t *scope_find_or_add_id(scope_t *scope, qstr qst, bool *added) {
68 id_info_t *id_info = scope_find(scope, qst);
Damien George6be0b0a2014-08-15 14:30:52 +010069 if (id_info != NULL) {
70 *added = false;
71 return id_info;
Damien429d7192013-10-04 19:53:11 +010072 }
73
74 // make sure we have enough memory
75 if (scope->id_info_len >= scope->id_info_alloc) {
Damien George58ebde42014-05-21 20:32:59 +010076 scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc, scope->id_info_alloc + MICROPY_ALLOC_SCOPE_ID_INC);
77 scope->id_info_alloc += MICROPY_ALLOC_SCOPE_ID_INC;
Damien429d7192013-10-04 19:53:11 +010078 }
79
Damien Georgecbd2f742014-01-19 11:48:48 +000080 // add new id to end of array of all ids; this seems to match CPython
81 // important thing is that function arguments are first, but that is
82 // handled by the compiler because it adds arguments before compiling the body
Damien George6be0b0a2014-08-15 14:30:52 +010083 id_info = &scope->id_info[scope->id_info_len++];
Damien429d7192013-10-04 19:53:11 +010084
Damien429d7192013-10-04 19:53:11 +010085 id_info->kind = 0;
Damien George11d8cd52014-04-09 14:42:51 +010086 id_info->flags = 0;
Damien27fb45e2013-10-20 15:07:49 +010087 id_info->local_num = 0;
Damien George7ff996c2014-09-08 23:05:16 +010088 id_info->qst = qst;
Damien429d7192013-10-04 19:53:11 +010089 *added = true;
90 return id_info;
91}
92
Damien George7ff996c2014-09-08 23:05:16 +010093id_info_t *scope_find(scope_t *scope, qstr qst) {
Damien George6be0b0a2014-08-15 14:30:52 +010094 for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
Damien George7ff996c2014-09-08 23:05:16 +010095 if (scope->id_info[i].qst == qst) {
Damien429d7192013-10-04 19:53:11 +010096 return &scope->id_info[i];
97 }
98 }
99 return NULL;
100}
101
Damien George7ff996c2014-09-08 23:05:16 +0100102id_info_t *scope_find_global(scope_t *scope, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100103 while (scope->parent != NULL) {
104 scope = scope->parent;
105 }
Damien George7ff996c2014-09-08 23:05:16 +0100106 return scope_find(scope, qst);
Damien429d7192013-10-04 19:53:11 +0100107}
108
Damien George0d105172016-09-30 13:53:00 +1000109STATIC void scope_close_over_in_parents(scope_t *scope, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100110 assert(scope->parent != NULL); // we should have at least 1 parent
Damien George0d105172016-09-30 13:53:00 +1000111 for (scope_t *s = scope->parent;; s = s->parent) {
112 assert(s->parent != NULL); // we should not get to the outer scope
Damien George6be0b0a2014-08-15 14:30:52 +0100113 bool added;
Damien George7ff996c2014-09-08 23:05:16 +0100114 id_info_t *id = scope_find_or_add_id(s, qst, &added);
Damien George6be0b0a2014-08-15 14:30:52 +0100115 if (added) {
116 // variable not previously declared in this scope, so declare it as free and keep searching parents
Damien429d7192013-10-04 19:53:11 +0100117 id->kind = ID_INFO_KIND_FREE;
118 } else {
119 // variable is declared in this scope, so finish
Damien George0d105172016-09-30 13:53:00 +1000120 if (id->kind == ID_INFO_KIND_LOCAL) {
121 // variable local to this scope, close it over
122 id->kind = ID_INFO_KIND_CELL;
123 } else {
124 // ID_INFO_KIND_FREE: variable already closed over in a parent scope
125 // ID_INFO_KIND_CELL: variable already closed over in this scope
126 assert(id->kind == ID_INFO_KIND_FREE || id->kind == ID_INFO_KIND_CELL);
Damien429d7192013-10-04 19:53:11 +0100127 }
128 return;
129 }
130 }
Damien George0d105172016-09-30 13:53:00 +1000131}
132
133void scope_find_local_and_close_over(scope_t *scope, id_info_t *id, qstr qst) {
134 if (scope->parent != NULL) {
135 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
136 id_info_t *id2 = scope_find(s, qst);
137 if (id2 != NULL) {
138 if (id2->kind == ID_INFO_KIND_LOCAL || id2->kind == ID_INFO_KIND_CELL || id2->kind == ID_INFO_KIND_FREE) {
139 id->kind = ID_INFO_KIND_FREE;
140 scope_close_over_in_parents(scope, qst);
141 return;
142 }
143 break;
144 }
145 }
146 }
147 id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
Damien429d7192013-10-04 19:53:11 +0100148}
Damien Georgedd5353a2015-12-18 12:35:44 +0000149
150#endif // MICROPY_ENABLE_COMPILER