blob: 1f602ac9c07562c642420680cb8c7d09da95c148 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdint.h>
2#include <stdio.h>
3#include <string.h>
4#include <assert.h>
5
6#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00007#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +01009#include "parse.h"
10#include "scope.h"
11
Damien Georgecbd2f742014-01-19 11:48:48 +000012scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint unique_code_id, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +010013 scope_t *scope = m_new(scope_t, 1);
14 scope->kind = kind;
15 scope->parent = NULL;
16 scope->next = NULL;
17 scope->pn = pn;
Damien Georgecbd2f742014-01-19 11:48:48 +000018 scope->source_file = source_file;
Damien429d7192013-10-04 19:53:11 +010019 switch (kind) {
20 case SCOPE_MODULE:
Damien George55baff42014-01-21 21:40:13 +000021 scope->simple_name = QSTR_FROM_STR_STATIC("<module>");
Damien429d7192013-10-04 19:53:11 +010022 break;
23 case SCOPE_FUNCTION:
24 case SCOPE_CLASS:
Damiend99b0522013-12-21 18:17:45 +000025 assert(MP_PARSE_NODE_IS_STRUCT(pn));
26 scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
Damien429d7192013-10-04 19:53:11 +010027 break;
28 case SCOPE_LAMBDA:
Damien George55baff42014-01-21 21:40:13 +000029 scope->simple_name = QSTR_FROM_STR_STATIC("<lambda>");
Damien429d7192013-10-04 19:53:11 +010030 break;
31 case SCOPE_LIST_COMP:
Damien George55baff42014-01-21 21:40:13 +000032 scope->simple_name = QSTR_FROM_STR_STATIC("<listcomp>");
Damien429d7192013-10-04 19:53:11 +010033 break;
34 case SCOPE_DICT_COMP:
Damien George55baff42014-01-21 21:40:13 +000035 scope->simple_name = QSTR_FROM_STR_STATIC("<dictcomp>");
Damien429d7192013-10-04 19:53:11 +010036 break;
37 case SCOPE_SET_COMP:
Damien George55baff42014-01-21 21:40:13 +000038 scope->simple_name = QSTR_FROM_STR_STATIC("<setcomp>");
Damien429d7192013-10-04 19:53:11 +010039 break;
40 case SCOPE_GEN_EXPR:
Damien George55baff42014-01-21 21:40:13 +000041 scope->simple_name = QSTR_FROM_STR_STATIC("<genexpr>");
Damien429d7192013-10-04 19:53:11 +010042 break;
43 default:
44 assert(0);
45 }
46 scope->id_info_alloc = 8;
47 scope->id_info_len = 0;
48 scope->id_info = m_new(id_info_t, scope->id_info_alloc);
49
50 scope->flags = 0;
51 scope->num_params = 0;
52 /* not needed
53 scope->num_default_params = 0;
54 scope->num_dict_params = 0;
55 */
56 scope->num_locals = 0;
Damienb05d7072013-10-05 13:37:10 +010057 scope->unique_code_id = unique_code_id;
Damien6cdd3af2013-10-05 18:08:26 +010058 scope->emit_options = emit_options;
Damien429d7192013-10-04 19:53:11 +010059
60 return scope;
61}
62
Paul Sokolovskyfd313582014-01-23 23:05:47 +020063void scope_free(scope_t *scope) {
64 m_del(id_info_t, scope->id_info, scope->id_info_alloc);
65 m_del(scope_t, scope, 1);
66}
67
Damien429d7192013-10-04 19:53:11 +010068id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
69 for (int i = 0; i < scope->id_info_len; i++) {
70 if (scope->id_info[i].qstr == qstr) {
71 *added = false;
72 return &scope->id_info[i];
73 }
74 }
75
76 // make sure we have enough memory
77 if (scope->id_info_len >= scope->id_info_alloc) {
Damien732407f2013-12-29 19:33:23 +000078 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 +010079 scope->id_info_alloc *= 2;
Damien429d7192013-10-04 19:53:11 +010080 }
81
Damien Georgecbd2f742014-01-19 11:48:48 +000082 // add new id to end of array of all ids; this seems to match CPython
83 // important thing is that function arguments are first, but that is
84 // handled by the compiler because it adds arguments before compiling the body
85 id_info_t *id_info = &scope->id_info[scope->id_info_len++];
Damien429d7192013-10-04 19:53:11 +010086
87 id_info->param = false;
88 id_info->kind = 0;
89 id_info->qstr = qstr;
Damien27fb45e2013-10-20 15:07:49 +010090 id_info->local_num = 0;
Damien429d7192013-10-04 19:53:11 +010091 *added = true;
92 return id_info;
93}
94
95id_info_t *scope_find(scope_t *scope, qstr qstr) {
96 for (int i = 0; i < scope->id_info_len; i++) {
97 if (scope->id_info[i].qstr == qstr) {
98 return &scope->id_info[i];
99 }
100 }
101 return NULL;
102}
103
104id_info_t *scope_find_global(scope_t *scope, qstr qstr) {
105 while (scope->parent != NULL) {
106 scope = scope->parent;
107 }
108 for (int i = 0; i < scope->id_info_len; i++) {
109 if (scope->id_info[i].qstr == qstr) {
110 return &scope->id_info[i];
111 }
112 }
113 return NULL;
114}
115
116id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr) {
117 if (scope->parent == NULL) {
118 return NULL;
119 }
120 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
121 for (int i = 0; i < s->id_info_len; i++) {
122 if (s->id_info[i].qstr == qstr) {
123 return &s->id_info[i];
124 }
125 }
126 }
127 return NULL;
128}
129
130void scope_close_over_in_parents(scope_t *scope, qstr qstr) {
131 assert(scope->parent != NULL); // we should have at least 1 parent
132 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
133 id_info_t *id = NULL;
134 for (int i = 0; i < s->id_info_len; i++) {
135 if (s->id_info[i].qstr == qstr) {
136 id = &s->id_info[i];
137 break;
138 }
139 }
140 if (id == NULL) {
141 // variable not declared in this scope, so declare it as free and keep searching parents
142 bool added;
143 id = scope_find_or_add_id(s, qstr, &added);
144 assert(added);
145 id->kind = ID_INFO_KIND_FREE;
146 } else {
147 // variable is declared in this scope, so finish
148 switch (id->kind) {
149 case ID_INFO_KIND_LOCAL: id->kind = ID_INFO_KIND_CELL; break; // variable local to this scope, close it over
150 case ID_INFO_KIND_FREE: break; // variable already closed over in a parent scope
151 case ID_INFO_KIND_CELL: break; // variable already closed over in this scope
152 default: assert(0); // TODO
153 }
154 return;
155 }
156 }
157 assert(0); // we should have found the variable in one of the parents
158}
159
Damien415eb6f2013-10-05 12:19:06 +0100160void scope_declare_global(scope_t *scope, qstr qstr) {
161 if (scope->kind == SCOPE_MODULE) {
162 printf("SyntaxError?: can't declare global in outer code\n");
163 return;
164 }
165 bool added;
166 id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
167 if (!added) {
168 printf("SyntaxError?: identifier already declared something\n");
169 return;
170 }
171 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
172
173 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
174 id_info = scope_find_global(scope, qstr);
175 if (id_info != NULL) {
176 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
177 }
178}
179
180void scope_declare_nonlocal(scope_t *scope, qstr qstr) {
181 if (scope->kind == SCOPE_MODULE) {
182 printf("SyntaxError?: can't declare nonlocal in outer code\n");
183 return;
184 }
185 bool added;
186 id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
187 if (!added) {
188 printf("SyntaxError?: identifier already declared something\n");
189 return;
190 }
191 id_info_t *id_info2 = scope_find_local_in_parent(scope, qstr);
192 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)) {
193 printf("SyntaxError: no binding for nonlocal '%s' found\n", qstr_str(qstr));
194 return;
195 }
196 id_info->kind = ID_INFO_KIND_FREE;
197 scope_close_over_in_parents(scope, qstr);
198}
199
Damien Georgecbd2f742014-01-19 11:48:48 +0000200#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100201void scope_print_info(scope_t *s) {
202 if (s->kind == SCOPE_MODULE) {
203 printf("code <module>\n");
204 } else if (s->kind == SCOPE_LAMBDA) {
205 printf("code <lambda>\n");
206 } else if (s->kind == SCOPE_LIST_COMP) {
207 printf("code <listcomp>\n");
208 } else if (s->kind == SCOPE_DICT_COMP) {
209 printf("code <dictcomp>\n");
210 } else if (s->kind == SCOPE_SET_COMP) {
211 printf("code <setcomp>\n");
212 } else if (s->kind == SCOPE_GEN_EXPR) {
213 printf("code <genexpr>\n");
214 } else {
215 printf("code %s\n", qstr_str(s->simple_name));
216 }
217 /*
218 printf("var global:");
219 for (int i = 0; i < s->id_info_len; i++) {
220 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
221 printf(" %s", qstr_str(s->id_info[i].qstr));
222 }
223 }
224 printf("\n");
225 printf("var name:");
226 for (int i = 0; i < s->id_info_len; i++) {
227 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
228 printf(" %s", qstr_str(s->id_info[i].qstr));
229 }
230 }
231 printf("\n");
232 printf("var local:");
233 for (int i = 0; i < s->id_info_len; i++) {
234 if (s->id_info[i].kind == ID_INFO_KIND_LOCAL) {
235 printf(" %s", qstr_str(s->id_info[i].qstr));
236 }
237 }
238 printf("\n");
239 printf("var free:");
240 for (int i = 0; i < s->id_info_len; i++) {
241 if (s->id_info[i].kind == ID_INFO_KIND_FREE) {
242 printf(" %s", qstr_str(s->id_info[i].qstr));
243 }
244 }
245 printf("\n");
246 */
247 printf(" flags %04x\n", s->flags);
248 printf(" argcount %d\n", s->num_params);
249 printf(" nlocals %d\n", s->num_locals);
250 printf(" stacksize %d\n", s->stack_size);
251}
Damien Georgecbd2f742014-01-19 11:48:48 +0000252#endif