blob: 7eda5e05fd89e7b6e44af814ba3e67cb5780812a [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
Damien429d7192013-10-04 19:53:11 +010027#include <assert.h>
28
Damien George51dfcb42015-01-01 20:27:54 +000029#include "py/emit.h"
Damien429d7192013-10-04 19:53:11 +010030
Damien George542bd6b2015-03-26 14:42:40 +000031void mp_emit_common_get_id_for_load(scope_t *scope, qstr qst) {
32 // name adding/lookup
33 bool added;
34 id_info_t *id = scope_find_or_add_id(scope, qst, &added);
35 if (added) {
36#if MICROPY_EMIT_CPYTHON
37 if (qst == MP_QSTR_super && scope->kind == SCOPE_FUNCTION) {
38 // special case, super is a global, and also counts as use of __class__
39 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
40 id_info_t *id2 = scope_find_local_in_parent(scope, MP_QSTR___class__);
41 if (id2 != NULL) {
42 id2 = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
43 if (added) {
44 id2->kind = ID_INFO_KIND_FREE;
45 scope_close_over_in_parents(scope, MP_QSTR___class__);
46 }
47 }
48 } else
49#endif
50 {
51 id_info_t *id2 = scope_find_local_in_parent(scope, qst);
52 if (id2 != NULL && (id2->kind == ID_INFO_KIND_LOCAL || id2->kind == ID_INFO_KIND_CELL || id2->kind == ID_INFO_KIND_FREE)) {
53 id->kind = ID_INFO_KIND_FREE;
54 scope_close_over_in_parents(scope, qst);
55 } else {
56 id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
57 }
58 }
Damien429d7192013-10-04 19:53:11 +010059 }
60}
61
Damien George542bd6b2015-03-26 14:42:40 +000062void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst) {
63 // name adding/lookup
64 bool added;
65 id_info_t *id = scope_find_or_add_id(scope, qst, &added);
66 if (added) {
67 if (scope->kind == SCOPE_MODULE || scope->kind == SCOPE_CLASS) {
68 id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
69 } else {
70 id->kind = ID_INFO_KIND_LOCAL;
71 }
72 } else if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
73 // rebind as a local variable
74 id->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +010075 }
76}
77
Damien George542bd6b2015-03-26 14:42:40 +000078void mp_emit_common_id_op(emit_t *emit, const mp_emit_method_table_id_ops_t *emit_method_table, scope_t *scope, qstr qst) {
Damien4b03e772013-10-05 14:17:09 +010079 // assumes pass is greater than 1, ie that all identifiers are defined in the scope
80
Damien George7ff996c2014-09-08 23:05:16 +010081 id_info_t *id = scope_find(scope, qst);
Damien George542bd6b2015-03-26 14:42:40 +000082 assert(id != NULL);
Damien429d7192013-10-04 19:53:11 +010083
84 // call the emit backend with the correct code
Damien George542bd6b2015-03-26 14:42:40 +000085 if (id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
86 emit_method_table->name(emit, qst);
Damien429d7192013-10-04 19:53:11 +010087 } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien George542bd6b2015-03-26 14:42:40 +000088 emit_method_table->global(emit, qst);
Damien429d7192013-10-04 19:53:11 +010089 } else if (id->kind == ID_INFO_KIND_LOCAL) {
Damien George542bd6b2015-03-26 14:42:40 +000090 emit_method_table->fast(emit, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010091 } else {
Damien George542bd6b2015-03-26 14:42:40 +000092 assert(id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE);
93 emit_method_table->deref(emit, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010094 }
95}