common.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from __future__ import annotations
  2. import sys
  3. import sysconfig
  4. from typing import Any, Dict, Final
  5. from mypy.util import unnamed_function
  6. PREFIX: Final = "CPyPy_" # Python wrappers
  7. NATIVE_PREFIX: Final = "CPyDef_" # Native functions etc.
  8. DUNDER_PREFIX: Final = "CPyDunder_" # Wrappers for exposing dunder methods to the API
  9. REG_PREFIX: Final = "cpy_r_" # Registers
  10. STATIC_PREFIX: Final = "CPyStatic_" # Static variables (for literals etc.)
  11. TYPE_PREFIX: Final = "CPyType_" # Type object struct
  12. MODULE_PREFIX: Final = "CPyModule_" # Cached modules
  13. ATTR_PREFIX: Final = "_" # Attributes
  14. ENV_ATTR_NAME: Final = "__mypyc_env__"
  15. NEXT_LABEL_ATTR_NAME: Final = "__mypyc_next_label__"
  16. TEMP_ATTR_NAME: Final = "__mypyc_temp__"
  17. LAMBDA_NAME: Final = "__mypyc_lambda__"
  18. PROPSET_PREFIX: Final = "__mypyc_setter__"
  19. SELF_NAME: Final = "__mypyc_self__"
  20. # Max short int we accept as a literal is based on 32-bit platforms,
  21. # so that we can just always emit the same code.
  22. TOP_LEVEL_NAME: Final = "__top_level__" # Special function representing module top level
  23. # Maximal number of subclasses for a class to trigger fast path in isinstance() checks.
  24. FAST_ISINSTANCE_MAX_SUBCLASSES: Final = 2
  25. # Size of size_t, if configured.
  26. SIZEOF_SIZE_T_SYSCONFIG: Final = sysconfig.get_config_var("SIZEOF_SIZE_T")
  27. SIZEOF_SIZE_T: Final = (
  28. int(SIZEOF_SIZE_T_SYSCONFIG)
  29. if SIZEOF_SIZE_T_SYSCONFIG is not None
  30. else (sys.maxsize + 1).bit_length() // 8
  31. )
  32. IS_32_BIT_PLATFORM: Final = int(SIZEOF_SIZE_T) == 4
  33. PLATFORM_SIZE = 4 if IS_32_BIT_PLATFORM else 8
  34. # Maximum value for a short tagged integer.
  35. MAX_SHORT_INT: Final = 2 ** (8 * int(SIZEOF_SIZE_T) - 2) - 1
  36. # Minimum value for a short tagged integer.
  37. MIN_SHORT_INT: Final = -(MAX_SHORT_INT) - 1
  38. # Maximum value for a short tagged integer represented as a C integer literal.
  39. #
  40. # Note: Assume that the compiled code uses the same bit width as mypyc
  41. MAX_LITERAL_SHORT_INT: Final = MAX_SHORT_INT
  42. MIN_LITERAL_SHORT_INT: Final = -MAX_LITERAL_SHORT_INT - 1
  43. # Description of the C type used to track the definedness of attributes and
  44. # the presence of argument default values that have types with overlapping
  45. # error values. Each tracked attribute/argument has a dedicated bit in the
  46. # relevant bitmap.
  47. BITMAP_TYPE: Final = "uint32_t"
  48. BITMAP_BITS: Final = 32
  49. # Runtime C library files
  50. RUNTIME_C_FILES: Final = [
  51. "init.c",
  52. "getargs.c",
  53. "getargsfast.c",
  54. "int_ops.c",
  55. "float_ops.c",
  56. "str_ops.c",
  57. "bytes_ops.c",
  58. "list_ops.c",
  59. "dict_ops.c",
  60. "set_ops.c",
  61. "tuple_ops.c",
  62. "exc_ops.c",
  63. "misc_ops.c",
  64. "generic_ops.c",
  65. ]
  66. JsonDict = Dict[str, Any]
  67. def shared_lib_name(group_name: str) -> str:
  68. """Given a group name, return the actual name of its extension module.
  69. (This just adds a suffix to the final component.)
  70. """
  71. return f"{group_name}__mypyc"
  72. def short_name(name: str) -> str:
  73. if name.startswith("builtins."):
  74. return name[9:]
  75. return name
  76. def use_fastcall(capi_version: tuple[int, int]) -> bool:
  77. # We can use METH_FASTCALL for faster wrapper functions on Python 3.7+.
  78. return capi_version >= (3, 7)
  79. def use_vectorcall(capi_version: tuple[int, int]) -> bool:
  80. # We can use vectorcalls to make calls on Python 3.8+ (PEP 590).
  81. return capi_version >= (3, 8)
  82. def use_method_vectorcall(capi_version: tuple[int, int]) -> bool:
  83. # We can use a dedicated vectorcall API to call methods on Python 3.9+.
  84. return capi_version >= (3, 9)
  85. def get_id_from_name(name: str, fullname: str, line: int) -> str:
  86. """Create a unique id for a function.
  87. This creates an id that is unique for any given function definition, so that it can be used as
  88. a dictionary key. This is usually the fullname of the function, but this is different in that
  89. it handles the case where the function is named '_', in which case multiple different functions
  90. could have the same name."""
  91. if unnamed_function(name):
  92. return f"{fullname}.{line}"
  93. else:
  94. return fullname
  95. def short_id_from_name(func_name: str, shortname: str, line: int | None) -> str:
  96. if unnamed_function(func_name):
  97. assert line is not None
  98. partial_name = f"{shortname}.{line}"
  99. else:
  100. partial_name = shortname
  101. return partial_name
  102. def bitmap_name(index: int) -> str:
  103. if index == 0:
  104. return "__bitmap"
  105. return f"__bitmap{index + 1}"