blob: 1d240bb63e33a57287d71dc83917db860828d694 [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
63id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
64 for (int i = 0; i < scope->id_info_len; i++) {
65 if (scope->id_info[i].qstr == qstr) {
66 *added = false;
67 return &scope->id_info[i];
68 }
69 }
70
71 // make sure we have enough memory
72 if (scope->id_info_len >= scope->id_info_alloc) {
Damien732407f2013-12-29 19:33:23 +000073 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 +010074 scope->id_info_alloc *= 2;
Damien429d7192013-10-04 19:53:11 +010075 }
76
Damien Georgecbd2f742014-01-19 11:48:48 +000077 // add new id to end of array of all ids; this seems to match CPython
78 // important thing is that function arguments are first, but that is
79 // handled by the compiler because it adds arguments before compiling the body
80 id_info_t *id_info = &scope->id_info[scope->id_info_len++];
Damien429d7192013-10-04 19:53:11 +010081
82 id_info->param = false;
83 id_info->kind = 0;
84 id_info->qstr = qstr;
Damien27fb45e2013-10-20 15:07:49 +010085 id_info->local_num = 0;
Damien429d7192013-10-04 19:53:11 +010086 *added = true;
87 return id_info;
88}
89
90id_info_t *scope_find(scope_t *scope, qstr qstr) {
91 for (int i = 0; i < scope->id_info_len; i++) {
92 if (scope->id_info[i].qstr == qstr) {
93 return &scope->id_info[i];
94 }
95 }
96 return NULL;
97}
98
99id_info_t *scope_find_global(scope_t *scope, qstr qstr) {
100 while (scope->parent != NULL) {
101 scope = scope->parent;
102 }
103 for (int i = 0; i < scope->id_info_len; i++) {
104 if (scope->id_info[i].qstr == qstr) {
105 return &scope->id_info[i];
106 }
107 }
108 return NULL;
109}
110
111id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr) {
112 if (scope->parent == NULL) {
113 return NULL;
114 }
115 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
116 for (int i = 0; i < s->id_info_len; i++) {
117 if (s->id_info[i].qstr == qstr) {
118 return &s->id_info[i];
119 }
120 }
121 }
122 return NULL;
123}
124
125void scope_close_over_in_parents(scope_t *scope, qstr qstr) {
126 assert(scope->parent != NULL); // we should have at least 1 parent
127 for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
128 id_info_t *id = NULL;
129 for (int i = 0; i < s->id_info_len; i++) {
130 if (s->id_info[i].qstr == qstr) {
131 id = &s->id_info[i];
132 break;
133 }
134 }
135 if (id == NULL) {
136 // variable not declared in this scope, so declare it as free and keep searching parents
137 bool added;
138 id = scope_find_or_add_id(s, qstr, &added);
139 assert(added);
140 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}
154
Damien415eb6f2013-10-05 12:19:06 +0100155void scope_declare_global(scope_t *scope, qstr qstr) {
156 if (scope->kind == SCOPE_MODULE) {
157 printf("SyntaxError?: can't declare global in outer code\n");
158 return;
159 }
160 bool added;
161 id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
162 if (!added) {
163 printf("SyntaxError?: identifier already declared something\n");
164 return;
165 }
166 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
167
168 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
169 id_info = scope_find_global(scope, qstr);
170 if (id_info != NULL) {
171 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
172 }
173}
174
175void scope_declare_nonlocal(scope_t *scope, qstr qstr) {
176 if (scope->kind == SCOPE_MODULE) {
177 printf("SyntaxError?: can't declare nonlocal in outer code\n");
178 return;
179 }
180 bool added;
181 id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
182 if (!added) {
183 printf("SyntaxError?: identifier already declared something\n");
184 return;
185 }
186 id_info_t *id_info2 = scope_find_local_in_parent(scope, qstr);
187 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)) {
188 printf("SyntaxError: no binding for nonlocal '%s' found\n", qstr_str(qstr));
189 return;
190 }
191 id_info->kind = ID_INFO_KIND_FREE;
192 scope_close_over_in_parents(scope, qstr);
193}
194
Damien Georgecbd2f742014-01-19 11:48:48 +0000195#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100196void scope_print_info(scope_t *s) {
197 if (s->kind == SCOPE_MODULE) {
198 printf("code <module>\n");
199 } else if (s->kind == SCOPE_LAMBDA) {
200 printf("code <lambda>\n");
201 } else if (s->kind == SCOPE_LIST_COMP) {
202 printf("code <listcomp>\n");
203 } else if (s->kind == SCOPE_DICT_COMP) {
204 printf("code <dictcomp>\n");
205 } else if (s->kind == SCOPE_SET_COMP) {
206 printf("code <setcomp>\n");
207 } else if (s->kind == SCOPE_GEN_EXPR) {
208 printf("code <genexpr>\n");
209 } else {
210 printf("code %s\n", qstr_str(s->simple_name));
211 }
212 /*
213 printf("var global:");
214 for (int i = 0; i < s->id_info_len; i++) {
215 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
216 printf(" %s", qstr_str(s->id_info[i].qstr));
217 }
218 }
219 printf("\n");
220 printf("var name:");
221 for (int i = 0; i < s->id_info_len; i++) {
222 if (s->id_info[i].kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
223 printf(" %s", qstr_str(s->id_info[i].qstr));
224 }
225 }
226 printf("\n");
227 printf("var local:");
228 for (int i = 0; i < s->id_info_len; i++) {
229 if (s->id_info[i].kind == ID_INFO_KIND_LOCAL) {
230 printf(" %s", qstr_str(s->id_info[i].qstr));
231 }
232 }
233 printf("\n");
234 printf("var free:");
235 for (int i = 0; i < s->id_info_len; i++) {
236 if (s->id_info[i].kind == ID_INFO_KIND_FREE) {
237 printf(" %s", qstr_str(s->id_info[i].qstr));
238 }
239 }
240 printf("\n");
241 */
242 printf(" flags %04x\n", s->flags);
243 printf(" argcount %d\n", s->num_params);
244 printf(" nlocals %d\n", s->num_locals);
245 printf(" stacksize %d\n", s->stack_size);
246}
Damien Georgecbd2f742014-01-19 11:48:48 +0000247#endif