pycore_symtable.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef Py_INTERNAL_SYMTABLE_H
  2. #define Py_INTERNAL_SYMTABLE_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifndef Py_BUILD_CORE
  7. # error "this header requires Py_BUILD_CORE define"
  8. #endif
  9. struct _mod; // Type defined in pycore_ast.h
  10. typedef enum _block_type {
  11. FunctionBlock, ClassBlock, ModuleBlock,
  12. // Used for annotations if 'from __future__ import annotations' is active.
  13. // Annotation blocks cannot bind names and are not evaluated.
  14. AnnotationBlock,
  15. // Used for generics and type aliases. These work mostly like functions
  16. // (see PEP 695 for details). The three different blocks function identically;
  17. // they are different enum entries only so that error messages can be more
  18. // precise.
  19. TypeVarBoundBlock, TypeAliasBlock, TypeParamBlock
  20. } _Py_block_ty;
  21. typedef enum _comprehension_type {
  22. NoComprehension = 0,
  23. ListComprehension = 1,
  24. DictComprehension = 2,
  25. SetComprehension = 3,
  26. GeneratorExpression = 4 } _Py_comprehension_ty;
  27. struct _symtable_entry;
  28. struct symtable {
  29. PyObject *st_filename; /* name of file being compiled,
  30. decoded from the filesystem encoding */
  31. struct _symtable_entry *st_cur; /* current symbol table entry */
  32. struct _symtable_entry *st_top; /* symbol table entry for module */
  33. PyObject *st_blocks; /* dict: map AST node addresses
  34. * to symbol table entries */
  35. PyObject *st_stack; /* list: stack of namespace info */
  36. PyObject *st_global; /* borrowed ref to st_top->ste_symbols */
  37. int st_nblocks; /* number of blocks used. kept for
  38. consistency with the corresponding
  39. compiler structure */
  40. PyObject *st_private; /* name of current class or NULL */
  41. PyFutureFeatures *st_future; /* module's future features that affect
  42. the symbol table */
  43. int recursion_depth; /* current recursion depth */
  44. int recursion_limit; /* recursion limit */
  45. };
  46. typedef struct _symtable_entry {
  47. PyObject_HEAD
  48. PyObject *ste_id; /* int: key in ste_table->st_blocks */
  49. PyObject *ste_symbols; /* dict: variable names to flags */
  50. PyObject *ste_name; /* string: name of current block */
  51. PyObject *ste_varnames; /* list of function parameters */
  52. PyObject *ste_children; /* list of child blocks */
  53. PyObject *ste_directives;/* locations of global and nonlocal statements */
  54. _Py_block_ty ste_type;
  55. int ste_nested; /* true if block is nested */
  56. unsigned ste_free : 1; /* true if block has free variables */
  57. unsigned ste_child_free : 1; /* true if a child block has free vars,
  58. including free refs to globals */
  59. unsigned ste_generator : 1; /* true if namespace is a generator */
  60. unsigned ste_coroutine : 1; /* true if namespace is a coroutine */
  61. _Py_comprehension_ty ste_comprehension; /* Kind of comprehension (if any) */
  62. unsigned ste_varargs : 1; /* true if block has varargs */
  63. unsigned ste_varkeywords : 1; /* true if block has varkeywords */
  64. unsigned ste_returns_value : 1; /* true if namespace uses return with
  65. an argument */
  66. unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
  67. closure over __class__
  68. should be created */
  69. unsigned ste_needs_classdict : 1; /* for class scopes, true if a closure
  70. over the class dict should be created */
  71. unsigned ste_comp_inlined : 1; /* true if this comprehension is inlined */
  72. unsigned ste_comp_iter_target : 1; /* true if visiting comprehension target */
  73. unsigned ste_can_see_class_scope : 1; /* true if this block can see names bound in an
  74. enclosing class scope */
  75. int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
  76. int ste_lineno; /* first line of block */
  77. int ste_col_offset; /* offset of first line of block */
  78. int ste_end_lineno; /* end line of block */
  79. int ste_end_col_offset; /* end offset of first line of block */
  80. int ste_opt_lineno; /* lineno of last exec or import * */
  81. int ste_opt_col_offset; /* offset of last exec or import * */
  82. struct symtable *ste_table;
  83. PyObject *ste_mangled_names; /* set of names for which mangling should be applied */
  84. } PySTEntryObject;
  85. extern PyTypeObject PySTEntry_Type;
  86. #define PySTEntry_Check(op) Py_IS_TYPE((op), &PySTEntry_Type)
  87. extern long _PyST_GetSymbol(PySTEntryObject *, PyObject *);
  88. extern int _PyST_GetScope(PySTEntryObject *, PyObject *);
  89. extern int _PyST_IsFunctionLike(PySTEntryObject *);
  90. extern struct symtable* _PySymtable_Build(
  91. struct _mod *mod,
  92. PyObject *filename,
  93. PyFutureFeatures *future);
  94. PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
  95. extern void _PySymtable_Free(struct symtable *);
  96. extern PyObject *_Py_MaybeMangle(PyObject *privateobj, PySTEntryObject *ste, PyObject *name);
  97. extern PyObject* _Py_Mangle(PyObject *p, PyObject *name);
  98. /* Flags for def-use information */
  99. #define DEF_GLOBAL 1 /* global stmt */
  100. #define DEF_LOCAL 2 /* assignment in code block */
  101. #define DEF_PARAM (2<<1) /* formal parameter */
  102. #define DEF_NONLOCAL (2<<2) /* nonlocal stmt */
  103. #define USE (2<<3) /* name is used */
  104. #define DEF_FREE (2<<4) /* name used but not defined in nested block */
  105. #define DEF_FREE_CLASS (2<<5) /* free variable from class's method */
  106. #define DEF_IMPORT (2<<6) /* assignment occurred via import */
  107. #define DEF_ANNOT (2<<7) /* this name is annotated */
  108. #define DEF_COMP_ITER (2<<8) /* this name is a comprehension iteration variable */
  109. #define DEF_TYPE_PARAM (2<<9) /* this name is a type parameter */
  110. #define DEF_COMP_CELL (2<<10) /* this name is a cell in an inlined comprehension */
  111. #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
  112. /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
  113. table. GLOBAL is returned from PyST_GetScope() for either of them.
  114. It is stored in ste_symbols at bits 13-16.
  115. */
  116. #define SCOPE_OFFSET 12
  117. #define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
  118. #define LOCAL 1
  119. #define GLOBAL_EXPLICIT 2
  120. #define GLOBAL_IMPLICIT 3
  121. #define FREE 4
  122. #define CELL 5
  123. #define GENERATOR 1
  124. #define GENERATOR_EXPRESSION 2
  125. // Used by symtablemodule.c
  126. extern struct symtable* _Py_SymtableStringObjectFlags(
  127. const char *str,
  128. PyObject *filename,
  129. int start,
  130. PyCompilerFlags *flags);
  131. int _PyFuture_FromAST(
  132. struct _mod * mod,
  133. PyObject *filename,
  134. PyFutureFeatures* futures);
  135. #ifdef __cplusplus
  136. }
  137. #endif
  138. #endif /* !Py_INTERNAL_SYMTABLE_H */