blob: 632e5275218bc1f8ada6bd8b50522ced7a60f706 [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 Georgedd5353a2015-12-18 12:35:44 +000031#if MICROPY_ENABLE_COMPILER
32
Damien George4abff752014-08-30 14:59:21 +010033scope_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 +010034 scope_t *scope = m_new0(scope_t, 1);
Damien429d7192013-10-04 19:53:11 +010035 scope->kind = kind;
Damien429d7192013-10-04 19:53:11 +010036 scope->pn = pn;
Damien Georgecbd2f742014-01-19 11:48:48 +000037 scope->source_file = source_file;
Damien429d7192013-10-04 19:53:11 +010038 switch (kind) {
39 case SCOPE_MODULE:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020040 scope->simple_name = MP_QSTR__lt_module_gt_;
Damien429d7192013-10-04 19:53:11 +010041 break;
42 case SCOPE_FUNCTION:
43 case SCOPE_CLASS:
Damiend99b0522013-12-21 18:17:45 +000044 assert(MP_PARSE_NODE_IS_STRUCT(pn));
45 scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
Damien429d7192013-10-04 19:53:11 +010046 break;
47 case SCOPE_LAMBDA:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020048 scope->simple_name = MP_QSTR__lt_lambda_gt_;
Damien429d7192013-10-04 19:53:11 +010049 break;
50 case SCOPE_LIST_COMP:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020051 scope->simple_name = MP_QSTR__lt_listcomp_gt_;
Damien429d7192013-10-04 19:53:11 +010052 break;
53 case SCOPE_DICT_COMP:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020054 scope->simple_name = MP_QSTR__lt_dictcomp_gt_;
Damien429d7192013-10-04 19:53:11 +010055 break;
56 case SCOPE_SET_COMP:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020057 scope->simple_name = MP_QSTR__lt_setcomp_gt_;
Damien429d7192013-10-04 19:53:11 +010058 break;
59 case SCOPE_GEN_EXPR:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020060 scope->simple_name = MP_QSTR__lt_genexpr_gt_;
Damien429d7192013-10-04 19:53:11 +010061 break;
62 default:
63 assert(0);
64 }
Damien Georgedf8127a2014-04-13 11:04:33 +010065 scope->raw_code = mp_emit_glue_new_raw_code();
Damien6cdd3af2013-10-05 18:08:26 +010066 scope->emit_options = emit_options;
Damien George58ebde42014-05-21 20:32:59 +010067 scope->id_info_alloc = MICROPY_ALLOC_SCOPE_ID_INIT;
Damien George78035b92014-04-09 12:27:39 +010068 scope->id_info = m_new(id_info_t, scope->id_info_alloc);
Damien429d7192013-10-04 19:53:11 +010069
70 return scope;
71}
72
Paul Sokolovskyfd313582014-01-23 23:05:47 +020073void 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 George7ff996c2014-09-08 23:05:16 +010078id_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 George6be0b0a2014-08-15 14:30:52 +010080 if (id_info != NULL) {
81 *added = false;
82 return id_info;
Damien429d7192013-10-04 19:53:11 +010083 }
84
85 // make sure we have enough memory
86 if (scope->id_info_len >= scope->id_info_alloc) {
Damien George58ebde42014-05-21 20:32:59 +010087 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;
Damien429d7192013-10-04 19:53:11 +010089 }
90
Damien Georgecbd2f742014-01-19 11:48:48 +000091 // 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 George6be0b0a2014-08-15 14:30:52 +010094 id_info = &scope->id_info[scope->id_info_len++];
Damien429d7192013-10-04 19:53:11 +010095
Damien429d7192013-10-04 19:53:11 +010096 id_info->kind = 0;
Damien George11d8cd52014-04-09 14:42:51 +010097 id_info->flags = 0;
Damien27fb45e2013-10-20 15:07:49 +010098 id_info->local_num = 0;
Damien George7ff996c2014-09-08 23:05:16 +010099 id_info->qst = qst;
Damien429d7192013-10-04 19:53:11 +0100100 *added = true;
101 return id_info;
102}
103
Damien George7ff996c2014-09-08 23:05:16 +0100104id_info_t *scope_find(scope_t *scope, qstr qst) {
Damien George6be0b0a2014-08-15 14:30:52 +0100105 for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
Damien George7ff996c2014-09-08 23:05:16 +0100106 if (scope->id_info[i].qst == qst) {
Damien429d7192013-10-04 19:53:11 +0100107 return &scope->id_info[i];
108 }
109 }
110 return NULL;
111}
112
Damien George7ff996c2014-09-08 23:05:16 +0100113id_info_t *scope_find_global(scope_t *scope, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100114 while (scope->parent != NULL) {
115 scope = scope->parent;
116 }
Damien George7ff996c2014-09-08 23:05:16 +0100117 return scope_find(scope, qst);
Damien429d7192013-10-04 19:53:11 +0100118}
119
Damien George7ff996c2014-09-08 23:05:16 +0100120id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100121 if (scope->parent == NULL) {
122 return NULL;
123 }
124 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
Damien George7ff996c2014-09-08 23:05:16 +0100125 id_info_t *id = scope_find(s, qst);
Damien George6be0b0a2014-08-15 14:30:52 +0100126 if (id != NULL) {
127 return id;
Damien429d7192013-10-04 19:53:11 +0100128 }
129 }
130 return NULL;
131}
132
Damien George7ff996c2014-09-08 23:05:16 +0100133void scope_close_over_in_parents(scope_t *scope, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100134 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 George6be0b0a2014-08-15 14:30:52 +0100136 bool added;
Damien George7ff996c2014-09-08 23:05:16 +0100137 id_info_t *id = scope_find_or_add_id(s, qst, &added);
Damien George6be0b0a2014-08-15 14:30:52 +0100138 if (added) {
139 // variable not previously declared in this scope, so declare it as free and keep searching parents
Damien429d7192013-10-04 19:53:11 +0100140 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 Georgedd5353a2015-12-18 12:35:44 +0000154
155#endif // MICROPY_ENABLE_COMPILER