symtable.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef Py_LIMITED_API
  2. #ifndef Py_SYMTABLE_H
  3. #define Py_SYMTABLE_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #include "Python-ast.h" /* mod_ty */
  8. /* XXX(ncoghlan): This is a weird mix of public names and interpreter internal
  9. * names.
  10. */
  11. typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock }
  12. _Py_block_ty;
  13. struct _symtable_entry;
  14. struct symtable {
  15. PyObject *st_filename; /* name of file being compiled,
  16. decoded from the filesystem encoding */
  17. struct _symtable_entry *st_cur; /* current symbol table entry */
  18. struct _symtable_entry *st_top; /* symbol table entry for module */
  19. PyObject *st_blocks; /* dict: map AST node addresses
  20. * to symbol table entries */
  21. PyObject *st_stack; /* list: stack of namespace info */
  22. PyObject *st_global; /* borrowed ref to st_top->ste_symbols */
  23. int st_nblocks; /* number of blocks used. kept for
  24. consistency with the corresponding
  25. compiler structure */
  26. PyObject *st_private; /* name of current class or NULL */
  27. PyFutureFeatures *st_future; /* module's future features that affect
  28. the symbol table */
  29. int recursion_depth; /* current recursion depth */
  30. int recursion_limit; /* recursion limit */
  31. };
  32. typedef struct _symtable_entry {
  33. PyObject_HEAD
  34. PyObject *ste_id; /* int: key in ste_table->st_blocks */
  35. PyObject *ste_symbols; /* dict: variable names to flags */
  36. PyObject *ste_name; /* string: name of current block */
  37. PyObject *ste_varnames; /* list of function parameters */
  38. PyObject *ste_children; /* list of child blocks */
  39. PyObject *ste_directives;/* locations of global and nonlocal statements */
  40. _Py_block_ty ste_type; /* module, class, or function */
  41. int ste_nested; /* true if block is nested */
  42. unsigned ste_free : 1; /* true if block has free variables */
  43. unsigned ste_child_free : 1; /* true if a child block has free vars,
  44. including free refs to globals */
  45. unsigned ste_generator : 1; /* true if namespace is a generator */
  46. unsigned ste_coroutine : 1; /* true if namespace is a coroutine */
  47. unsigned ste_comprehension : 1; /* true if namespace is a list comprehension */
  48. unsigned ste_varargs : 1; /* true if block has varargs */
  49. unsigned ste_varkeywords : 1; /* true if block has varkeywords */
  50. unsigned ste_returns_value : 1; /* true if namespace uses return with
  51. an argument */
  52. unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
  53. closure over __class__
  54. should be created */
  55. unsigned ste_comp_iter_target : 1; /* true if visiting comprehension target */
  56. int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
  57. int ste_lineno; /* first line of block */
  58. int ste_col_offset; /* offset of first line of block */
  59. int ste_opt_lineno; /* lineno of last exec or import * */
  60. int ste_opt_col_offset; /* offset of last exec or import * */
  61. struct symtable *ste_table;
  62. } PySTEntryObject;
  63. PyAPI_DATA(PyTypeObject) PySTEntry_Type;
  64. #define PySTEntry_Check(op) (Py_TYPE(op) == &PySTEntry_Type)
  65. PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *);
  66. PyAPI_FUNC(struct symtable *) PySymtable_Build(
  67. mod_ty mod,
  68. const char *filename, /* decoded from the filesystem encoding */
  69. PyFutureFeatures *future);
  70. PyAPI_FUNC(struct symtable *) PySymtable_BuildObject(
  71. mod_ty mod,
  72. PyObject *filename,
  73. PyFutureFeatures *future);
  74. PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
  75. PyAPI_FUNC(void) PySymtable_Free(struct symtable *);
  76. /* Flags for def-use information */
  77. #define DEF_GLOBAL 1 /* global stmt */
  78. #define DEF_LOCAL 2 /* assignment in code block */
  79. #define DEF_PARAM 2<<1 /* formal parameter */
  80. #define DEF_NONLOCAL 2<<2 /* nonlocal stmt */
  81. #define USE 2<<3 /* name is used */
  82. #define DEF_FREE 2<<4 /* name used but not defined in nested block */
  83. #define DEF_FREE_CLASS 2<<5 /* free variable from class's method */
  84. #define DEF_IMPORT 2<<6 /* assignment occurred via import */
  85. #define DEF_ANNOT 2<<7 /* this name is annotated */
  86. #define DEF_COMP_ITER 2<<8 /* this name is a comprehension iteration variable */
  87. #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
  88. /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
  89. table. GLOBAL is returned from PyST_GetScope() for either of them.
  90. It is stored in ste_symbols at bits 12-15.
  91. */
  92. #define SCOPE_OFFSET 11
  93. #define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
  94. #define LOCAL 1
  95. #define GLOBAL_EXPLICIT 2
  96. #define GLOBAL_IMPLICIT 3
  97. #define FREE 4
  98. #define CELL 5
  99. #define GENERATOR 1
  100. #define GENERATOR_EXPRESSION 2
  101. #ifdef __cplusplus
  102. }
  103. #endif
  104. #endif /* !Py_SYMTABLE_H */
  105. #endif /* !Py_LIMITED_API */