blob: d55099fec2f10d44f3ad05f5dd90abf89aced335 [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
Damiend99b0522013-12-21 18:17:45 +000032#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030033#include "misc.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 George4abff752014-08-30 14:59:21 +010040scope_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 +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 George58ebde42014-05-21 20:32:59 +010074 scope->id_info_alloc = MICROPY_ALLOC_SCOPE_ID_INIT;
Damien George78035b92014-04-09 12:27:39 +010075 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
Damien George7ff996c2014-09-08 23:05:16 +010085id_info_t *scope_find_or_add_id(scope_t *scope, qstr qst, bool *added) {
86 id_info_t *id_info = scope_find(scope, qst);
Damien George6be0b0a2014-08-15 14:30:52 +010087 if (id_info != NULL) {
88 *added = false;
89 return id_info;
Damien429d7192013-10-04 19:53:11 +010090 }
91
92 // make sure we have enough memory
93 if (scope->id_info_len >= scope->id_info_alloc) {
Damien George58ebde42014-05-21 20:32:59 +010094 scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc, scope->id_info_alloc + MICROPY_ALLOC_SCOPE_ID_INC);
95 scope->id_info_alloc += MICROPY_ALLOC_SCOPE_ID_INC;
Damien429d7192013-10-04 19:53:11 +010096 }
97
Damien Georgecbd2f742014-01-19 11:48:48 +000098 // add new id to end of array of all ids; this seems to match CPython
99 // important thing is that function arguments are first, but that is
100 // handled by the compiler because it adds arguments before compiling the body
Damien George6be0b0a2014-08-15 14:30:52 +0100101 id_info = &scope->id_info[scope->id_info_len++];
Damien429d7192013-10-04 19:53:11 +0100102
Damien429d7192013-10-04 19:53:11 +0100103 id_info->kind = 0;
Damien George11d8cd52014-04-09 14:42:51 +0100104 id_info->flags = 0;
Damien27fb45e2013-10-20 15:07:49 +0100105 id_info->local_num = 0;
Damien George7ff996c2014-09-08 23:05:16 +0100106 id_info->qst = qst;
Damien429d7192013-10-04 19:53:11 +0100107 *added = true;
108 return id_info;
109}
110
Damien George7ff996c2014-09-08 23:05:16 +0100111id_info_t *scope_find(scope_t *scope, qstr qst) {
Damien George6be0b0a2014-08-15 14:30:52 +0100112 for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
Damien George7ff996c2014-09-08 23:05:16 +0100113 if (scope->id_info[i].qst == qst) {
Damien429d7192013-10-04 19:53:11 +0100114 return &scope->id_info[i];
115 }
116 }
117 return NULL;
118}
119
Damien George7ff996c2014-09-08 23:05:16 +0100120id_info_t *scope_find_global(scope_t *scope, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100121 while (scope->parent != NULL) {
122 scope = scope->parent;
123 }
Damien George7ff996c2014-09-08 23:05:16 +0100124 return scope_find(scope, qst);
Damien429d7192013-10-04 19:53:11 +0100125}
126
Damien George7ff996c2014-09-08 23:05:16 +0100127id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100128 if (scope->parent == NULL) {
129 return NULL;
130 }
131 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
Damien George7ff996c2014-09-08 23:05:16 +0100132 id_info_t *id = scope_find(s, qst);
Damien George6be0b0a2014-08-15 14:30:52 +0100133 if (id != NULL) {
134 return id;
Damien429d7192013-10-04 19:53:11 +0100135 }
136 }
137 return NULL;
138}
139
Damien George7ff996c2014-09-08 23:05:16 +0100140void scope_close_over_in_parents(scope_t *scope, qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100141 assert(scope->parent != NULL); // we should have at least 1 parent
142 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
Damien George6be0b0a2014-08-15 14:30:52 +0100143 bool added;
Damien George7ff996c2014-09-08 23:05:16 +0100144 id_info_t *id = scope_find_or_add_id(s, qst, &added);
Damien George6be0b0a2014-08-15 14:30:52 +0100145 if (added) {
146 // variable not previously declared in this scope, so declare it as free and keep searching parents
Damien429d7192013-10-04 19:53:11 +0100147 id->kind = ID_INFO_KIND_FREE;
148 } else {
149 // variable is declared in this scope, so finish
150 switch (id->kind) {
151 case ID_INFO_KIND_LOCAL: id->kind = ID_INFO_KIND_CELL; break; // variable local to this scope, close it over
152 case ID_INFO_KIND_FREE: break; // variable already closed over in a parent scope
153 case ID_INFO_KIND_CELL: break; // variable already closed over in this scope
154 default: assert(0); // TODO
155 }
156 return;
157 }
158 }
159 assert(0); // we should have found the variable in one of the parents
160}
161
Damien George7ff996c2014-09-08 23:05:16 +0100162void scope_declare_global(scope_t *scope, qstr qst) {
Damien415eb6f2013-10-05 12:19:06 +0100163 if (scope->kind == SCOPE_MODULE) {
164 printf("SyntaxError?: can't declare global in outer code\n");
165 return;
166 }
167 bool added;
Damien George7ff996c2014-09-08 23:05:16 +0100168 id_info_t *id_info = scope_find_or_add_id(scope, qst, &added);
Damien415eb6f2013-10-05 12:19:06 +0100169 if (!added) {
170 printf("SyntaxError?: identifier already declared something\n");
171 return;
172 }
173 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
174
175 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
Damien George7ff996c2014-09-08 23:05:16 +0100176 id_info = scope_find_global(scope, qst);
Damien415eb6f2013-10-05 12:19:06 +0100177 if (id_info != NULL) {
178 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
179 }
180}
181
Damien George7ff996c2014-09-08 23:05:16 +0100182void scope_declare_nonlocal(scope_t *scope, qstr qst) {
Damien415eb6f2013-10-05 12:19:06 +0100183 if (scope->kind == SCOPE_MODULE) {
184 printf("SyntaxError?: can't declare nonlocal in outer code\n");
185 return;
186 }
187 bool added;
Damien George7ff996c2014-09-08 23:05:16 +0100188 id_info_t *id_info = scope_find_or_add_id(scope, qst, &added);
Damien415eb6f2013-10-05 12:19:06 +0100189 if (!added) {
190 printf("SyntaxError?: identifier already declared something\n");
191 return;
192 }
Damien George7ff996c2014-09-08 23:05:16 +0100193 id_info_t *id_info2 = scope_find_local_in_parent(scope, qst);
Damien415eb6f2013-10-05 12:19:06 +0100194 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)) {
Damien George7ff996c2014-09-08 23:05:16 +0100195 printf("SyntaxError: no binding for nonlocal '%s' found\n", qstr_str(qst));
Damien415eb6f2013-10-05 12:19:06 +0100196 return;
197 }
198 id_info->kind = ID_INFO_KIND_FREE;
Damien George7ff996c2014-09-08 23:05:16 +0100199 scope_close_over_in_parents(scope, qst);
Damien415eb6f2013-10-05 12:19:06 +0100200}
201
Damien Georgecbd2f742014-01-19 11:48:48 +0000202#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100203void scope_print_info(scope_t *s) {
204 if (s->kind == SCOPE_MODULE) {
205 printf("code <module>\n");
206 } else if (s->kind == SCOPE_LAMBDA) {
207 printf("code <lambda>\n");
208 } else if (s->kind == SCOPE_LIST_COMP) {
209 printf("code <listcomp>\n");
210 } else if (s->kind == SCOPE_DICT_COMP) {
211 printf("code <dictcomp>\n");
212 } else if (s->kind == SCOPE_SET_COMP) {
213 printf("code <setcomp>\n");
214 } else if (s->kind == SCOPE_GEN_EXPR) {
215 printf("code <genexpr>\n");
216 } else {
217 printf("code %s\n", qstr_str(s->simple_name));
218 }
219 /*
220 printf("var global:");
221 for (int i = 0; i < s->id_info_len; i++) {
222 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100223 printf(" %s", qstr_str(s->id_info[i].qst));
Damien429d7192013-10-04 19:53:11 +0100224 }
225 }
226 printf("\n");
227 printf("var name:");
228 for (int i = 0; i < s->id_info_len; i++) {
229 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien George7ff996c2014-09-08 23:05:16 +0100230 printf(" %s", qstr_str(s->id_info[i].qst));
Damien429d7192013-10-04 19:53:11 +0100231 }
232 }
233 printf("\n");
234 printf("var local:");
235 for (int i = 0; i < s->id_info_len; i++) {
236 if (s->id_info[i].kind == ID_INFO_KIND_LOCAL) {
Damien George7ff996c2014-09-08 23:05:16 +0100237 printf(" %s", qstr_str(s->id_info[i].qst));
Damien429d7192013-10-04 19:53:11 +0100238 }
239 }
240 printf("\n");
241 printf("var free:");
242 for (int i = 0; i < s->id_info_len; i++) {
243 if (s->id_info[i].kind == ID_INFO_KIND_FREE) {
Damien George7ff996c2014-09-08 23:05:16 +0100244 printf(" %s", qstr_str(s->id_info[i].qst));
Damien429d7192013-10-04 19:53:11 +0100245 }
246 }
247 printf("\n");
248 */
Damien George8725f8f2014-02-15 19:33:11 +0000249 printf(" flags %04x\n", s->scope_flags);
Damien George2827d622014-04-27 15:50:52 +0100250 printf(" argcount %d\n", s->num_pos_args);
Damien429d7192013-10-04 19:53:11 +0100251 printf(" nlocals %d\n", s->num_locals);
252 printf(" stacksize %d\n", s->stack_size);
253}
Damien Georgecbd2f742014-01-19 11:48:48 +0000254#endif