Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame^] | 1 | /* |
| 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 | |
xbe | efe3422 | 2014-03-16 00:14:26 -0700 | [diff] [blame] | 27 | #include <stdbool.h> |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 28 | #include <stdint.h> |
| 29 | #include <stdio.h> |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 30 | #include <assert.h> |
| 31 | |
| 32 | #include "misc.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 33 | #include "mpconfig.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 34 | #include "qstr.h" |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 35 | #include "obj.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 36 | #include "parse.h" |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 37 | #include "emitglue.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 38 | #include "scope.h" |
| 39 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 40 | scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint emit_options) { |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 41 | scope_t *scope = m_new0(scope_t, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 42 | scope->kind = kind; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 43 | scope->pn = pn; |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 44 | scope->source_file = source_file; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 45 | switch (kind) { |
| 46 | case SCOPE_MODULE: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 47 | scope->simple_name = MP_QSTR__lt_module_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 48 | break; |
| 49 | case SCOPE_FUNCTION: |
| 50 | case SCOPE_CLASS: |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 51 | assert(MP_PARSE_NODE_IS_STRUCT(pn)); |
| 52 | scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 53 | break; |
| 54 | case SCOPE_LAMBDA: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 55 | scope->simple_name = MP_QSTR__lt_lambda_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 56 | break; |
| 57 | case SCOPE_LIST_COMP: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 58 | scope->simple_name = MP_QSTR__lt_listcomp_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 59 | break; |
| 60 | case SCOPE_DICT_COMP: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 61 | scope->simple_name = MP_QSTR__lt_dictcomp_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 62 | break; |
| 63 | case SCOPE_SET_COMP: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 64 | scope->simple_name = MP_QSTR__lt_setcomp_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 65 | break; |
| 66 | case SCOPE_GEN_EXPR: |
Paul Sokolovsky | ab5d082 | 2014-01-24 00:22:00 +0200 | [diff] [blame] | 67 | scope->simple_name = MP_QSTR__lt_genexpr_gt_; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 68 | break; |
| 69 | default: |
| 70 | assert(0); |
| 71 | } |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 72 | scope->raw_code = mp_emit_glue_new_raw_code(); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 73 | scope->emit_options = emit_options; |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 74 | scope->id_info_alloc = 8; |
| 75 | scope->id_info = m_new(id_info_t, scope->id_info_alloc); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 76 | |
| 77 | return scope; |
| 78 | } |
| 79 | |
Paul Sokolovsky | fd31358 | 2014-01-23 23:05:47 +0200 | [diff] [blame] | 80 | void 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 | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 85 | id_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) { |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 95 | scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc, scope->id_info_alloc * 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 96 | scope->id_info_alloc *= 2; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 99 | // 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++]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 103 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 104 | id_info->kind = 0; |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 105 | id_info->flags = 0; |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 106 | id_info->local_num = 0; |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 107 | id_info->qstr = qstr; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 108 | *added = true; |
| 109 | return id_info; |
| 110 | } |
| 111 | |
| 112 | id_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 | |
| 121 | id_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 | |
| 133 | id_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 | |
| 147 | void 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 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 177 | void 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 | |
| 197 | void 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 George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 217 | #if MICROPY_EMIT_CPYTHON |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 218 | void 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 George | 8725f8f | 2014-02-15 19:33:11 +0000 | [diff] [blame] | 264 | printf(" flags %04x\n", s->scope_flags); |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 265 | printf(" argcount %d\n", s->num_pos_args); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 266 | printf(" nlocals %d\n", s->num_locals); |
| 267 | printf(" stacksize %d\n", s->stack_size); |
| 268 | } |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 269 | #endif |