blob: 8d3f7bada2428d32d55250d1021baf7bf1e28aa0 [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
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
Damien429d7192013-10-04 19:53:11 +010030#include <assert.h>
31
32#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000033#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000034#include "qstr.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010035#include "obj.h"
Damien429d7192013-10-04 19:53:11 +010036#include "parse.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010037#include "emitglue.h"
Damien429d7192013-10-04 19:53:11 +010038#include "scope.h"
39
Damien Georgedf8127a2014-04-13 11:04:33 +010040scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint emit_options) {
Damien George78035b92014-04-09 12:27:39 +010041 scope_t *scope = m_new0(scope_t, 1);
Damien429d7192013-10-04 19:53:11 +010042 scope->kind = kind;
Damien429d7192013-10-04 19:53:11 +010043 scope->pn = pn;
Damien Georgecbd2f742014-01-19 11:48:48 +000044 scope->source_file = source_file;
Damien429d7192013-10-04 19:53:11 +010045 switch (kind) {
46 case SCOPE_MODULE:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020047 scope->simple_name = MP_QSTR__lt_module_gt_;
Damien429d7192013-10-04 19:53:11 +010048 break;
49 case SCOPE_FUNCTION:
50 case SCOPE_CLASS:
Damiend99b0522013-12-21 18:17:45 +000051 assert(MP_PARSE_NODE_IS_STRUCT(pn));
52 scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
Damien429d7192013-10-04 19:53:11 +010053 break;
54 case SCOPE_LAMBDA:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020055 scope->simple_name = MP_QSTR__lt_lambda_gt_;
Damien429d7192013-10-04 19:53:11 +010056 break;
57 case SCOPE_LIST_COMP:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020058 scope->simple_name = MP_QSTR__lt_listcomp_gt_;
Damien429d7192013-10-04 19:53:11 +010059 break;
60 case SCOPE_DICT_COMP:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020061 scope->simple_name = MP_QSTR__lt_dictcomp_gt_;
Damien429d7192013-10-04 19:53:11 +010062 break;
63 case SCOPE_SET_COMP:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020064 scope->simple_name = MP_QSTR__lt_setcomp_gt_;
Damien429d7192013-10-04 19:53:11 +010065 break;
66 case SCOPE_GEN_EXPR:
Paul Sokolovskyab5d0822014-01-24 00:22:00 +020067 scope->simple_name = MP_QSTR__lt_genexpr_gt_;
Damien429d7192013-10-04 19:53:11 +010068 break;
69 default:
70 assert(0);
71 }
Damien Georgedf8127a2014-04-13 11:04:33 +010072 scope->raw_code = mp_emit_glue_new_raw_code();
Damien6cdd3af2013-10-05 18:08:26 +010073 scope->emit_options = emit_options;
Damien George78035b92014-04-09 12:27:39 +010074 scope->id_info_alloc = 8;
75 scope->id_info = m_new(id_info_t, scope->id_info_alloc);
Damien429d7192013-10-04 19:53:11 +010076
77 return scope;
78}
79
Paul Sokolovskyfd313582014-01-23 23:05:47 +020080void scope_free(scope_t *scope) {
81 m_del(id_info_t, scope->id_info, scope->id_info_alloc);
82 m_del(scope_t, scope, 1);
83}
84
Damien429d7192013-10-04 19:53:11 +010085id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
86 for (int i = 0; i < scope->id_info_len; i++) {
87 if (scope->id_info[i].qstr == qstr) {
88 *added = false;
89 return &scope->id_info[i];
90 }
91 }
92
93 // make sure we have enough memory
94 if (scope->id_info_len >= scope->id_info_alloc) {
Damien732407f2013-12-29 19:33:23 +000095 scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc, scope->id_info_alloc * 2);
Damien429d7192013-10-04 19:53:11 +010096 scope->id_info_alloc *= 2;
Damien429d7192013-10-04 19:53:11 +010097 }
98
Damien Georgecbd2f742014-01-19 11:48:48 +000099 // add new id to end of array of all ids; this seems to match CPython
100 // important thing is that function arguments are first, but that is
101 // handled by the compiler because it adds arguments before compiling the body
102 id_info_t *id_info = &scope->id_info[scope->id_info_len++];
Damien429d7192013-10-04 19:53:11 +0100103
Damien429d7192013-10-04 19:53:11 +0100104 id_info->kind = 0;
Damien George11d8cd52014-04-09 14:42:51 +0100105 id_info->flags = 0;
Damien27fb45e2013-10-20 15:07:49 +0100106 id_info->local_num = 0;
Damien George11d8cd52014-04-09 14:42:51 +0100107 id_info->qstr = qstr;
Damien429d7192013-10-04 19:53:11 +0100108 *added = true;
109 return id_info;
110}
111
112id_info_t *scope_find(scope_t *scope, qstr qstr) {
113 for (int i = 0; i < scope->id_info_len; i++) {
114 if (scope->id_info[i].qstr == qstr) {
115 return &scope->id_info[i];
116 }
117 }
118 return NULL;
119}
120
121id_info_t *scope_find_global(scope_t *scope, qstr qstr) {
122 while (scope->parent != NULL) {
123 scope = scope->parent;
124 }
125 for (int i = 0; i < scope->id_info_len; i++) {
126 if (scope->id_info[i].qstr == qstr) {
127 return &scope->id_info[i];
128 }
129 }
130 return NULL;
131}
132
133id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr) {
134 if (scope->parent == NULL) {
135 return NULL;
136 }
137 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
138 for (int i = 0; i < s->id_info_len; i++) {
139 if (s->id_info[i].qstr == qstr) {
140 return &s->id_info[i];
141 }
142 }
143 }
144 return NULL;
145}
146
147void scope_close_over_in_parents(scope_t *scope, qstr qstr) {
148 assert(scope->parent != NULL); // we should have at least 1 parent
149 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
150 id_info_t *id = NULL;
151 for (int i = 0; i < s->id_info_len; i++) {
152 if (s->id_info[i].qstr == qstr) {
153 id = &s->id_info[i];
154 break;
155 }
156 }
157 if (id == NULL) {
158 // variable not declared in this scope, so declare it as free and keep searching parents
159 bool added;
160 id = scope_find_or_add_id(s, qstr, &added);
161 assert(added);
162 id->kind = ID_INFO_KIND_FREE;
163 } else {
164 // variable is declared in this scope, so finish
165 switch (id->kind) {
166 case ID_INFO_KIND_LOCAL: id->kind = ID_INFO_KIND_CELL; break; // variable local to this scope, close it over
167 case ID_INFO_KIND_FREE: break; // variable already closed over in a parent scope
168 case ID_INFO_KIND_CELL: break; // variable already closed over in this scope
169 default: assert(0); // TODO
170 }
171 return;
172 }
173 }
174 assert(0); // we should have found the variable in one of the parents
175}
176
Damien415eb6f2013-10-05 12:19:06 +0100177void scope_declare_global(scope_t *scope, qstr qstr) {
178 if (scope->kind == SCOPE_MODULE) {
179 printf("SyntaxError?: can't declare global in outer code\n");
180 return;
181 }
182 bool added;
183 id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
184 if (!added) {
185 printf("SyntaxError?: identifier already declared something\n");
186 return;
187 }
188 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
189
190 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
191 id_info = scope_find_global(scope, qstr);
192 if (id_info != NULL) {
193 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
194 }
195}
196
197void scope_declare_nonlocal(scope_t *scope, qstr qstr) {
198 if (scope->kind == SCOPE_MODULE) {
199 printf("SyntaxError?: can't declare nonlocal in outer code\n");
200 return;
201 }
202 bool added;
203 id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
204 if (!added) {
205 printf("SyntaxError?: identifier already declared something\n");
206 return;
207 }
208 id_info_t *id_info2 = scope_find_local_in_parent(scope, qstr);
209 if (id_info2 == NULL || !(id_info2->kind == ID_INFO_KIND_LOCAL || id_info2->kind == ID_INFO_KIND_CELL || id_info2->kind == ID_INFO_KIND_FREE)) {
210 printf("SyntaxError: no binding for nonlocal '%s' found\n", qstr_str(qstr));
211 return;
212 }
213 id_info->kind = ID_INFO_KIND_FREE;
214 scope_close_over_in_parents(scope, qstr);
215}
216
Damien Georgecbd2f742014-01-19 11:48:48 +0000217#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100218void scope_print_info(scope_t *s) {
219 if (s->kind == SCOPE_MODULE) {
220 printf("code <module>\n");
221 } else if (s->kind == SCOPE_LAMBDA) {
222 printf("code <lambda>\n");
223 } else if (s->kind == SCOPE_LIST_COMP) {
224 printf("code <listcomp>\n");
225 } else if (s->kind == SCOPE_DICT_COMP) {
226 printf("code <dictcomp>\n");
227 } else if (s->kind == SCOPE_SET_COMP) {
228 printf("code <setcomp>\n");
229 } else if (s->kind == SCOPE_GEN_EXPR) {
230 printf("code <genexpr>\n");
231 } else {
232 printf("code %s\n", qstr_str(s->simple_name));
233 }
234 /*
235 printf("var global:");
236 for (int i = 0; i < s->id_info_len; i++) {
237 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
238 printf(" %s", qstr_str(s->id_info[i].qstr));
239 }
240 }
241 printf("\n");
242 printf("var name:");
243 for (int i = 0; i < s->id_info_len; i++) {
244 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
245 printf(" %s", qstr_str(s->id_info[i].qstr));
246 }
247 }
248 printf("\n");
249 printf("var local:");
250 for (int i = 0; i < s->id_info_len; i++) {
251 if (s->id_info[i].kind == ID_INFO_KIND_LOCAL) {
252 printf(" %s", qstr_str(s->id_info[i].qstr));
253 }
254 }
255 printf("\n");
256 printf("var free:");
257 for (int i = 0; i < s->id_info_len; i++) {
258 if (s->id_info[i].kind == ID_INFO_KIND_FREE) {
259 printf(" %s", qstr_str(s->id_info[i].qstr));
260 }
261 }
262 printf("\n");
263 */
Damien George8725f8f2014-02-15 19:33:11 +0000264 printf(" flags %04x\n", s->scope_flags);
Damien George2827d622014-04-27 15:50:52 +0100265 printf(" argcount %d\n", s->num_pos_args);
Damien429d7192013-10-04 19:53:11 +0100266 printf(" nlocals %d\n", s->num_locals);
267 printf(" stacksize %d\n", s->stack_size);
268}
Damien Georgecbd2f742014-01-19 11:48:48 +0000269#endif