blob: f9b1fb122b03dec36a9f20ada8a42038639a4aeb [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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
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 George4abff752014-08-30 14:59:21 +010031scope_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 +010032 scope_t *scope = m_new0(scope_t, 1);
Damien429d7192013-10-04 19:53:11 +010033 scope->kind = kind;
Damien429d7192013-10-04 19:53:11 +010034 scope->pn = pn;
Damien Georgecbd2f742014-01-19 11:48:48 +000035 scope->source_file = source_file;
Damien429d7192013-10-04 19:53:11 +010036 switch (kind) {
37 case SCOPE_MODULE:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020038 scope->simple_name = MP_QSTR__lt_module_gt_;
Damien429d7192013-10-04 19:53:11 +010039 break;
40 case SCOPE_FUNCTION:
41 case SCOPE_CLASS:
Damiend99b0522013-12-21 18:17:45 +000042 assert(MP_PARSE_NODE_IS_STRUCT(pn));
43 scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
Damien429d7192013-10-04 19:53:11 +010044 break;
45 case SCOPE_LAMBDA:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020046 scope->simple_name = MP_QSTR__lt_lambda_gt_;
Damien429d7192013-10-04 19:53:11 +010047 break;
48 case SCOPE_LIST_COMP:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020049 scope->simple_name = MP_QSTR__lt_listcomp_gt_;
Damien429d7192013-10-04 19:53:11 +010050 break;
51 case SCOPE_DICT_COMP:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020052 scope->simple_name = MP_QSTR__lt_dictcomp_gt_;
Damien429d7192013-10-04 19:53:11 +010053 break;
54 case SCOPE_SET_COMP:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020055 scope->simple_name = MP_QSTR__lt_setcomp_gt_;
Damien429d7192013-10-04 19:53:11 +010056 break;
57 case SCOPE_GEN_EXPR:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020058 scope->simple_name = MP_QSTR__lt_genexpr_gt_;
Damien429d7192013-10-04 19:53:11 +010059 break;
60 default:
61 assert(0);
62 }
Damien Georgedf8127a2014-04-13 11:04:33 +010063 scope->raw_code = mp_emit_glue_new_raw_code();
Damien6cdd3af2013-10-05 18:08:26 +010064 scope->emit_options = emit_options;
Damien George58ebde42014-05-21 20:32:59 +010065 scope->id_info_alloc = MICROPY_ALLOC_SCOPE_ID_INIT;
Damien George78035b92014-04-09 12:27:39 +010066 scope->id_info = m_new(id_info_t, scope->id_info_alloc);
Damien429d7192013-10-04 19:53:11 +010067
68 return scope;
69}
70
Paul Sokolovskyfd313582014-01-23 23:05:47 +020071void scope_free(scope_t *scope) {
72 m_del(id_info_t, scope->id_info, scope->id_info_alloc);
73 m_del(scope_t, scope, 1);
74}
75
Damien George7ff996c2014-09-08 23:05:16 +010076id_info_t *scope_find_or_add_id(scope_t *scope, qstr qst, bool *added) {
77 id_info_t *id_info = scope_find(scope, qst);
Damien George6be0b0a2014-08-15 14:30:52 +010078 if (id_info != NULL) {
79 *added = false;
80 return id_info;
Damien429d7192013-10-04 19:53:11 +010081 }
82
83 // make sure we have enough memory
84 if (scope->id_info_len >= scope->id_info_alloc) {
Damien George58ebde42014-05-21 20:32:59 +010085 scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc, scope->id_info_alloc + MICROPY_ALLOC_SCOPE_ID_INC);
86 scope->id_info_alloc += MICROPY_ALLOC_SCOPE_ID_INC;
Damien429d7192013-10-04 19:53:11 +010087 }
88
Damien Georgecbd2f742014-01-19 11:48:48 +000089 // add new id to end of array of all ids; this seems to match CPython
90 // important thing is that function arguments are first, but that is
91 // handled by the compiler because it adds arguments before compiling the body
Damien George6be0b0a2014-08-15 14:30:52 +010092 id_info = &scope->id_info[scope->id_info_len++];
Damien429d7192013-10-04 19:53:11 +010093
Damien429d7192013-10-04 19:53:11 +010094 id_info->kind = 0;
Damien George11d8cd52014-04-09 14:42:51 +010095 id_info->flags = 0;
Damien27fb45e2013-10-20 15:07:49 +010096 id_info->local_num = 0;
Damien George7ff996c2014-09-08 23:05:16 +010097 id_info->qst = qst;
Damien429d7192013-10-04 19:53:11 +010098 *added = true;
99 return id_info;
100}
101
Damien George7ff996c2014-09-08 23:05:16 +0100102id_info_t *scope_find(scope_t *scope, qstr qst) {
Damien George6be0b0a2014-08-15 14:30:52 +0100103 for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
Damien George7ff996c2014-09-08 23:05:16 +0100104 if (scope->id_info[i].qst == qst) {
Damien429d7192013-10-04 19:53:11 +0100105 return &scope->id_info[i];
106 }
107 }
108 return NULL;
109}
110
Damien George7ff996c2014-09-08 23:05:16 +0100111id_info_t *scope_find_global(scope_t *scope, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100112 while (scope->parent != NULL) {
113 scope = scope->parent;
114 }
Damien George7ff996c2014-09-08 23:05:16 +0100115 return scope_find(scope, qst);
Damien429d7192013-10-04 19:53:11 +0100116}
117
Damien George7ff996c2014-09-08 23:05:16 +0100118id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100119 if (scope->parent == NULL) {
120 return NULL;
121 }
122 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
Damien George7ff996c2014-09-08 23:05:16 +0100123 id_info_t *id = scope_find(s, qst);
Damien George6be0b0a2014-08-15 14:30:52 +0100124 if (id != NULL) {
125 return id;
Damien429d7192013-10-04 19:53:11 +0100126 }
127 }
128 return NULL;
129}
130
Damien George7ff996c2014-09-08 23:05:16 +0100131void scope_close_over_in_parents(scope_t *scope, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100132 assert(scope->parent != NULL); // we should have at least 1 parent
133 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
Damien George6be0b0a2014-08-15 14:30:52 +0100134 bool added;
Damien George7ff996c2014-09-08 23:05:16 +0100135 id_info_t *id = scope_find_or_add_id(s, qst, &added);
Damien George6be0b0a2014-08-15 14:30:52 +0100136 if (added) {
137 // variable not previously declared in this scope, so declare it as free and keep searching parents
Damien429d7192013-10-04 19:53:11 +0100138 id->kind = ID_INFO_KIND_FREE;
139 } else {
140 // variable is declared in this scope, so finish
141 switch (id->kind) {
142 case ID_INFO_KIND_LOCAL: id->kind = ID_INFO_KIND_CELL; break; // variable local to this scope, close it over
143 case ID_INFO_KIND_FREE: break; // variable already closed over in a parent scope
144 case ID_INFO_KIND_CELL: break; // variable already closed over in this scope
145 default: assert(0); // TODO
146 }
147 return;
148 }
149 }
150 assert(0); // we should have found the variable in one of the parents
151}