symtable.h 4.8 KB

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