scope.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This file is part of the MicroPython 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. #ifndef MICROPY_INCLUDED_PY_SCOPE_H
  27. #define MICROPY_INCLUDED_PY_SCOPE_H
  28. #include "py/parse.h"
  29. #include "py/emitglue.h"
  30. typedef enum {
  31. ID_INFO_KIND_UNDECIDED,
  32. ID_INFO_KIND_GLOBAL_IMPLICIT,
  33. ID_INFO_KIND_GLOBAL_IMPLICIT_ASSIGNED,
  34. ID_INFO_KIND_GLOBAL_EXPLICIT,
  35. ID_INFO_KIND_LOCAL, // in a function f, written and only referenced by f
  36. ID_INFO_KIND_CELL, // in a function f, read/written by children of f
  37. ID_INFO_KIND_FREE, // in a function f, belongs to the parent of f
  38. } id_info_kind_t;
  39. enum {
  40. ID_FLAG_IS_PARAM = 0x01,
  41. ID_FLAG_IS_STAR_PARAM = 0x02,
  42. ID_FLAG_IS_DBL_STAR_PARAM = 0x04,
  43. ID_FLAG_VIPER_TYPE_POS = 4,
  44. };
  45. typedef struct _id_info_t {
  46. uint8_t kind;
  47. uint8_t flags;
  48. // when it's an ID_INFO_KIND_LOCAL this is the unique number of the local
  49. // when it's an ID_INFO_KIND_CELL/FREE this is the unique number of the closed over variable
  50. uint16_t local_num;
  51. qstr qst;
  52. } id_info_t;
  53. #define SCOPE_IS_FUNC_LIKE(s) ((s) >= SCOPE_LAMBDA)
  54. #define SCOPE_IS_COMP_LIKE(s) (SCOPE_LIST_COMP <= (s) && (s) <= SCOPE_GEN_EXPR)
  55. // scope is a "block" in Python parlance
  56. typedef enum {
  57. SCOPE_MODULE,
  58. SCOPE_CLASS,
  59. SCOPE_LAMBDA,
  60. SCOPE_LIST_COMP,
  61. SCOPE_DICT_COMP,
  62. SCOPE_SET_COMP,
  63. SCOPE_GEN_EXPR,
  64. SCOPE_FUNCTION,
  65. } scope_kind_t;
  66. typedef struct _scope_t {
  67. scope_kind_t kind;
  68. struct _scope_t *parent;
  69. struct _scope_t *next;
  70. mp_parse_node_t pn;
  71. mp_raw_code_t *raw_code;
  72. #if MICROPY_DEBUG_PRINTERS
  73. size_t raw_code_data_len; // for mp_bytecode_print
  74. #endif
  75. uint16_t simple_name; // a qstr
  76. uint16_t scope_flags; // see runtime0.h
  77. uint16_t emit_options; // see emitglue.h
  78. uint16_t num_pos_args;
  79. uint16_t num_kwonly_args;
  80. uint16_t num_def_pos_args;
  81. uint16_t num_locals;
  82. uint16_t stack_size; // maximum size of the locals stack
  83. uint16_t exc_stack_size; // maximum size of the exception stack
  84. uint16_t id_info_alloc;
  85. uint16_t id_info_len;
  86. id_info_t *id_info;
  87. } scope_t;
  88. scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, mp_uint_t emit_options);
  89. void scope_free(scope_t *scope);
  90. id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, id_info_kind_t kind);
  91. id_info_t *scope_find(scope_t *scope, qstr qstr);
  92. id_info_t *scope_find_global(scope_t *scope, qstr qstr);
  93. void scope_check_to_close_over(scope_t *scope, id_info_t *id);
  94. #endif // MICROPY_INCLUDED_PY_SCOPE_H