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 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 32 | #include "mpconfig.h" |
Paul Sokolovsky | 59c675a | 2014-06-21 22:43:22 +0300 | [diff] [blame] | 33 | #include "misc.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 | 4abff75 | 2014-08-30 14:59:21 +0100 | [diff] [blame] | 40 | scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_uint_t 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 | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 74 | scope->id_info_alloc = MICROPY_ALLOC_SCOPE_ID_INIT; |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 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 George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 85 | id_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 George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 87 | if (id_info != NULL) { |
| 88 | *added = false; |
| 89 | return id_info; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // make sure we have enough memory |
| 93 | if (scope->id_info_len >= scope->id_info_alloc) { |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 94 | 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; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 98 | // 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 George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 101 | id_info = &scope->id_info[scope->id_info_len++]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 102 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 103 | id_info->kind = 0; |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 104 | id_info->flags = 0; |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 105 | id_info->local_num = 0; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 106 | id_info->qst = qst; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 107 | *added = true; |
| 108 | return id_info; |
| 109 | } |
| 110 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 111 | id_info_t *scope_find(scope_t *scope, qstr qst) { |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 112 | for (mp_uint_t i = 0; i < scope->id_info_len; i++) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 113 | if (scope->id_info[i].qst == qst) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 114 | return &scope->id_info[i]; |
| 115 | } |
| 116 | } |
| 117 | return NULL; |
| 118 | } |
| 119 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 120 | id_info_t *scope_find_global(scope_t *scope, qstr qst) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 121 | while (scope->parent != NULL) { |
| 122 | scope = scope->parent; |
| 123 | } |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 124 | return scope_find(scope, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 127 | id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qst) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 128 | if (scope->parent == NULL) { |
| 129 | return NULL; |
| 130 | } |
| 131 | for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 132 | id_info_t *id = scope_find(s, qst); |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 133 | if (id != NULL) { |
| 134 | return id; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | return NULL; |
| 138 | } |
| 139 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 140 | void scope_close_over_in_parents(scope_t *scope, qstr qst) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 141 | 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 George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 143 | bool added; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 144 | id_info_t *id = scope_find_or_add_id(s, qst, &added); |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 145 | if (added) { |
| 146 | // variable not previously declared in this scope, so declare it as free and keep searching parents |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 147 | 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 George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 162 | void scope_declare_global(scope_t *scope, qstr qst) { |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 163 | if (scope->kind == SCOPE_MODULE) { |
| 164 | printf("SyntaxError?: can't declare global in outer code\n"); |
| 165 | return; |
| 166 | } |
| 167 | bool added; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 168 | id_info_t *id_info = scope_find_or_add_id(scope, qst, &added); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 169 | 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 George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 176 | id_info = scope_find_global(scope, qst); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 177 | if (id_info != NULL) { |
| 178 | id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; |
| 179 | } |
| 180 | } |
| 181 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 182 | void scope_declare_nonlocal(scope_t *scope, qstr qst) { |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 183 | if (scope->kind == SCOPE_MODULE) { |
| 184 | printf("SyntaxError?: can't declare nonlocal in outer code\n"); |
| 185 | return; |
| 186 | } |
| 187 | bool added; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 188 | id_info_t *id_info = scope_find_or_add_id(scope, qst, &added); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 189 | if (!added) { |
| 190 | printf("SyntaxError?: identifier already declared something\n"); |
| 191 | return; |
| 192 | } |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 193 | id_info_t *id_info2 = scope_find_local_in_parent(scope, qst); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 194 | 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 George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 195 | printf("SyntaxError: no binding for nonlocal '%s' found\n", qstr_str(qst)); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | id_info->kind = ID_INFO_KIND_FREE; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 199 | scope_close_over_in_parents(scope, qst); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 202 | #if MICROPY_EMIT_CPYTHON |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 203 | void 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 George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 223 | printf(" %s", qstr_str(s->id_info[i].qst)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 224 | } |
| 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 George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 230 | printf(" %s", qstr_str(s->id_info[i].qst)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 231 | } |
| 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 George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 237 | printf(" %s", qstr_str(s->id_info[i].qst)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 238 | } |
| 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 George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame^] | 244 | printf(" %s", qstr_str(s->id_info[i].qst)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | printf("\n"); |
| 248 | */ |
Damien George | 8725f8f | 2014-02-15 19:33:11 +0000 | [diff] [blame] | 249 | printf(" flags %04x\n", s->scope_flags); |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 250 | printf(" argcount %d\n", s->num_pos_args); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 251 | printf(" nlocals %d\n", s->num_locals); |
| 252 | printf(" stacksize %d\n", s->stack_size); |
| 253 | } |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 254 | #endif |